xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 31d166642c7c601c65eccf0ff2e0afe9a0538be2)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 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 
9 /* Local includes */
10 #include "i40e.h"
11 #include "i40e_diag.h"
12 #include "i40e_xsk.h"
13 #include <net/udp_tunnel.h>
14 #include <net/xdp_sock.h>
15 /* All i40e tracepoints are defined by the include below, which
16  * must be included exactly once across the whole kernel with
17  * CREATE_TRACE_POINTS defined
18  */
19 #define CREATE_TRACE_POINTS
20 #include "i40e_trace.h"
21 
22 const char i40e_driver_name[] = "i40e";
23 static const char i40e_driver_string[] =
24 			"Intel(R) Ethernet Connection XL710 Network Driver";
25 
26 #define DRV_KERN "-k"
27 
28 #define DRV_VERSION_MAJOR 2
29 #define DRV_VERSION_MINOR 8
30 #define DRV_VERSION_BUILD 20
31 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
32 	     __stringify(DRV_VERSION_MINOR) "." \
33 	     __stringify(DRV_VERSION_BUILD)    DRV_KERN
34 const char i40e_driver_version_str[] = DRV_VERSION;
35 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
36 
37 /* a bit of forward declarations */
38 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
39 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
40 static int i40e_add_vsi(struct i40e_vsi *vsi);
41 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
42 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
43 static int i40e_setup_misc_vector(struct i40e_pf *pf);
44 static void i40e_determine_queue_usage(struct i40e_pf *pf);
45 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
46 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
47 static int i40e_reset(struct i40e_pf *pf);
48 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
49 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
50 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
51 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
52 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
53 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
54 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
55 static int i40e_get_capabilities(struct i40e_pf *pf,
56 				 enum i40e_admin_queue_opc list_type);
57 
58 
59 /* i40e_pci_tbl - PCI Device ID Table
60  *
61  * Last entry must be all 0s
62  *
63  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64  *   Class, Class Mask, private data (not used) }
65  */
66 static const struct pci_device_id i40e_pci_tbl[] = {
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
90 	/* required last entry */
91 	{0, }
92 };
93 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94 
95 #define I40E_MAX_VF_COUNT 128
96 static int debug = -1;
97 module_param(debug, uint, 0);
98 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
99 
100 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102 MODULE_LICENSE("GPL v2");
103 MODULE_VERSION(DRV_VERSION);
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 /**
108  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
109  * @hw:   pointer to the HW structure
110  * @mem:  ptr to mem struct to fill out
111  * @size: size of memory requested
112  * @alignment: what to align the allocation to
113  **/
114 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
115 			    u64 size, u32 alignment)
116 {
117 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118 
119 	mem->size = ALIGN(size, alignment);
120 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
121 				     GFP_KERNEL);
122 	if (!mem->va)
123 		return -ENOMEM;
124 
125 	return 0;
126 }
127 
128 /**
129  * i40e_free_dma_mem_d - OS specific memory free for shared code
130  * @hw:   pointer to the HW structure
131  * @mem:  ptr to mem struct to free
132  **/
133 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
134 {
135 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
136 
137 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
138 	mem->va = NULL;
139 	mem->pa = 0;
140 	mem->size = 0;
141 
142 	return 0;
143 }
144 
145 /**
146  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
147  * @hw:   pointer to the HW structure
148  * @mem:  ptr to mem struct to fill out
149  * @size: size of memory requested
150  **/
151 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
152 			     u32 size)
153 {
154 	mem->size = size;
155 	mem->va = kzalloc(size, GFP_KERNEL);
156 
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_virt_mem_d - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
169 {
170 	/* it's ok to kfree a NULL pointer */
171 	kfree(mem->va);
172 	mem->va = NULL;
173 	mem->size = 0;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_get_lump - find a lump of free generic resource
180  * @pf: board private structure
181  * @pile: the pile of resource to search
182  * @needed: the number of items needed
183  * @id: an owner id to stick on the items assigned
184  *
185  * Returns the base item index of the lump, or negative for error
186  *
187  * The search_hint trick and lack of advanced fit-finding only work
188  * because we're highly likely to have all the same size lump requests.
189  * Linear search time and any fragmentation should be minimal.
190  **/
191 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
192 			 u16 needed, u16 id)
193 {
194 	int ret = -ENOMEM;
195 	int i, j;
196 
197 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
198 		dev_info(&pf->pdev->dev,
199 			 "param err: pile=%s needed=%d id=0x%04x\n",
200 			 pile ? "<valid>" : "<null>", needed, id);
201 		return -EINVAL;
202 	}
203 
204 	/* start the linear search with an imperfect hint */
205 	i = pile->search_hint;
206 	while (i < pile->num_entries) {
207 		/* skip already allocated entries */
208 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
209 			i++;
210 			continue;
211 		}
212 
213 		/* do we have enough in this lump? */
214 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
215 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
216 				break;
217 		}
218 
219 		if (j == needed) {
220 			/* there was enough, so assign it to the requestor */
221 			for (j = 0; j < needed; j++)
222 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
223 			ret = i;
224 			pile->search_hint = i + j;
225 			break;
226 		}
227 
228 		/* not enough, so skip over it and continue looking */
229 		i += j;
230 	}
231 
232 	return ret;
233 }
234 
235 /**
236  * i40e_put_lump - return a lump of generic resource
237  * @pile: the pile of resource to search
238  * @index: the base item index
239  * @id: the owner id of the items assigned
240  *
241  * Returns the count of items in the lump
242  **/
243 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
244 {
245 	int valid_id = (id | I40E_PILE_VALID_BIT);
246 	int count = 0;
247 	int i;
248 
249 	if (!pile || index >= pile->num_entries)
250 		return -EINVAL;
251 
252 	for (i = index;
253 	     i < pile->num_entries && pile->list[i] == valid_id;
254 	     i++) {
255 		pile->list[i] = 0;
256 		count++;
257 	}
258 
259 	if (count && index < pile->search_hint)
260 		pile->search_hint = index;
261 
262 	return count;
263 }
264 
265 /**
266  * i40e_find_vsi_from_id - searches for the vsi with the given id
267  * @pf: the pf structure to search for the vsi
268  * @id: id of the vsi it is searching for
269  **/
270 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
271 {
272 	int i;
273 
274 	for (i = 0; i < pf->num_alloc_vsi; i++)
275 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
276 			return pf->vsi[i];
277 
278 	return NULL;
279 }
280 
281 /**
282  * i40e_service_event_schedule - Schedule the service task to wake up
283  * @pf: board private structure
284  *
285  * If not already scheduled, this puts the task into the work queue
286  **/
287 void i40e_service_event_schedule(struct i40e_pf *pf)
288 {
289 	if ((!test_bit(__I40E_DOWN, pf->state) &&
290 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
291 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
292 		queue_work(i40e_wq, &pf->service_task);
293 }
294 
295 /**
296  * i40e_tx_timeout - Respond to a Tx Hang
297  * @netdev: network interface device structure
298  *
299  * If any port has noticed a Tx timeout, it is likely that the whole
300  * device is munged, not just the one netdev port, so go for the full
301  * reset.
302  **/
303 static void i40e_tx_timeout(struct net_device *netdev)
304 {
305 	struct i40e_netdev_priv *np = netdev_priv(netdev);
306 	struct i40e_vsi *vsi = np->vsi;
307 	struct i40e_pf *pf = vsi->back;
308 	struct i40e_ring *tx_ring = NULL;
309 	unsigned int i, hung_queue = 0;
310 	u32 head, val;
311 
312 	pf->tx_timeout_count++;
313 
314 	/* find the stopped queue the same way the stack does */
315 	for (i = 0; i < netdev->num_tx_queues; i++) {
316 		struct netdev_queue *q;
317 		unsigned long trans_start;
318 
319 		q = netdev_get_tx_queue(netdev, i);
320 		trans_start = q->trans_start;
321 		if (netif_xmit_stopped(q) &&
322 		    time_after(jiffies,
323 			       (trans_start + netdev->watchdog_timeo))) {
324 			hung_queue = i;
325 			break;
326 		}
327 	}
328 
329 	if (i == netdev->num_tx_queues) {
330 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
331 	} else {
332 		/* now that we have an index, find the tx_ring struct */
333 		for (i = 0; i < vsi->num_queue_pairs; i++) {
334 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
335 				if (hung_queue ==
336 				    vsi->tx_rings[i]->queue_index) {
337 					tx_ring = vsi->tx_rings[i];
338 					break;
339 				}
340 			}
341 		}
342 	}
343 
344 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
345 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
346 	else if (time_before(jiffies,
347 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
348 		return;   /* don't do any new action before the next timeout */
349 
350 	/* don't kick off another recovery if one is already pending */
351 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
352 		return;
353 
354 	if (tx_ring) {
355 		head = i40e_get_head(tx_ring);
356 		/* Read interrupt register */
357 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 			val = rd32(&pf->hw,
359 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 						tx_ring->vsi->base_vector - 1));
361 		else
362 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363 
364 		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",
365 			    vsi->seid, hung_queue, tx_ring->next_to_clean,
366 			    head, tx_ring->next_to_use,
367 			    readl(tx_ring->tail), val);
368 	}
369 
370 	pf->tx_timeout_last_recovery = jiffies;
371 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 		    pf->tx_timeout_recovery_level, hung_queue);
373 
374 	switch (pf->tx_timeout_recovery_level) {
375 	case 1:
376 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
377 		break;
378 	case 2:
379 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
380 		break;
381 	case 3:
382 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
383 		break;
384 	default:
385 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
386 		break;
387 	}
388 
389 	i40e_service_event_schedule(pf);
390 	pf->tx_timeout_recovery_level++;
391 }
392 
393 /**
394  * i40e_get_vsi_stats_struct - Get System Network Statistics
395  * @vsi: the VSI we care about
396  *
397  * Returns the address of the device statistics structure.
398  * The statistics are actually updated from the service task.
399  **/
400 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401 {
402 	return &vsi->net_stats;
403 }
404 
405 /**
406  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
407  * @ring: Tx ring to get statistics from
408  * @stats: statistics entry to be updated
409  **/
410 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
411 					    struct rtnl_link_stats64 *stats)
412 {
413 	u64 bytes, packets;
414 	unsigned int start;
415 
416 	do {
417 		start = u64_stats_fetch_begin_irq(&ring->syncp);
418 		packets = ring->stats.packets;
419 		bytes   = ring->stats.bytes;
420 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
421 
422 	stats->tx_packets += packets;
423 	stats->tx_bytes   += bytes;
424 }
425 
426 /**
427  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
428  * @netdev: network interface device structure
429  * @stats: data structure to store statistics
430  *
431  * Returns the address of the device statistics structure.
432  * The statistics are actually updated from the service task.
433  **/
434 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
435 				  struct rtnl_link_stats64 *stats)
436 {
437 	struct i40e_netdev_priv *np = netdev_priv(netdev);
438 	struct i40e_vsi *vsi = np->vsi;
439 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
440 	struct i40e_ring *ring;
441 	int i;
442 
443 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
444 		return;
445 
446 	if (!vsi->tx_rings)
447 		return;
448 
449 	rcu_read_lock();
450 	for (i = 0; i < vsi->num_queue_pairs; i++) {
451 		u64 bytes, packets;
452 		unsigned int start;
453 
454 		ring = READ_ONCE(vsi->tx_rings[i]);
455 		if (!ring)
456 			continue;
457 		i40e_get_netdev_stats_struct_tx(ring, stats);
458 
459 		if (i40e_enabled_xdp_vsi(vsi)) {
460 			ring++;
461 			i40e_get_netdev_stats_struct_tx(ring, stats);
462 		}
463 
464 		ring++;
465 		do {
466 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
467 			packets = ring->stats.packets;
468 			bytes   = ring->stats.bytes;
469 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
470 
471 		stats->rx_packets += packets;
472 		stats->rx_bytes   += bytes;
473 
474 	}
475 	rcu_read_unlock();
476 
477 	/* following stats updated by i40e_watchdog_subtask() */
478 	stats->multicast	= vsi_stats->multicast;
479 	stats->tx_errors	= vsi_stats->tx_errors;
480 	stats->tx_dropped	= vsi_stats->tx_dropped;
481 	stats->rx_errors	= vsi_stats->rx_errors;
482 	stats->rx_dropped	= vsi_stats->rx_dropped;
483 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
484 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
485 }
486 
487 /**
488  * i40e_vsi_reset_stats - Resets all stats of the given vsi
489  * @vsi: the VSI to have its stats reset
490  **/
491 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
492 {
493 	struct rtnl_link_stats64 *ns;
494 	int i;
495 
496 	if (!vsi)
497 		return;
498 
499 	ns = i40e_get_vsi_stats_struct(vsi);
500 	memset(ns, 0, sizeof(*ns));
501 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
502 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
503 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
504 	if (vsi->rx_rings && vsi->rx_rings[0]) {
505 		for (i = 0; i < vsi->num_queue_pairs; i++) {
506 			memset(&vsi->rx_rings[i]->stats, 0,
507 			       sizeof(vsi->rx_rings[i]->stats));
508 			memset(&vsi->rx_rings[i]->rx_stats, 0,
509 			       sizeof(vsi->rx_rings[i]->rx_stats));
510 			memset(&vsi->tx_rings[i]->stats, 0,
511 			       sizeof(vsi->tx_rings[i]->stats));
512 			memset(&vsi->tx_rings[i]->tx_stats, 0,
513 			       sizeof(vsi->tx_rings[i]->tx_stats));
514 		}
515 	}
516 	vsi->stat_offsets_loaded = false;
517 }
518 
519 /**
520  * i40e_pf_reset_stats - Reset all of the stats for the given PF
521  * @pf: the PF to be reset
522  **/
523 void i40e_pf_reset_stats(struct i40e_pf *pf)
524 {
525 	int i;
526 
527 	memset(&pf->stats, 0, sizeof(pf->stats));
528 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
529 	pf->stat_offsets_loaded = false;
530 
531 	for (i = 0; i < I40E_MAX_VEB; i++) {
532 		if (pf->veb[i]) {
533 			memset(&pf->veb[i]->stats, 0,
534 			       sizeof(pf->veb[i]->stats));
535 			memset(&pf->veb[i]->stats_offsets, 0,
536 			       sizeof(pf->veb[i]->stats_offsets));
537 			pf->veb[i]->stat_offsets_loaded = false;
538 		}
539 	}
540 	pf->hw_csum_rx_error = 0;
541 }
542 
543 /**
544  * i40e_stat_update48 - read and update a 48 bit stat from the chip
545  * @hw: ptr to the hardware info
546  * @hireg: the high 32 bit reg to read
547  * @loreg: the low 32 bit reg to read
548  * @offset_loaded: has the initial offset been loaded yet
549  * @offset: ptr to current offset value
550  * @stat: ptr to the stat
551  *
552  * Since the device stats are not reset at PFReset, they likely will not
553  * be zeroed when the driver starts.  We'll save the first values read
554  * and use them as offsets to be subtracted from the raw values in order
555  * to report stats that count from zero.  In the process, we also manage
556  * the potential roll-over.
557  **/
558 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
559 			       bool offset_loaded, u64 *offset, u64 *stat)
560 {
561 	u64 new_data;
562 
563 	if (hw->device_id == I40E_DEV_ID_QEMU) {
564 		new_data = rd32(hw, loreg);
565 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
566 	} else {
567 		new_data = rd64(hw, loreg);
568 	}
569 	if (!offset_loaded)
570 		*offset = new_data;
571 	if (likely(new_data >= *offset))
572 		*stat = new_data - *offset;
573 	else
574 		*stat = (new_data + BIT_ULL(48)) - *offset;
575 	*stat &= 0xFFFFFFFFFFFFULL;
576 }
577 
578 /**
579  * i40e_stat_update32 - read and update a 32 bit stat from the chip
580  * @hw: ptr to the hardware info
581  * @reg: the hw reg to read
582  * @offset_loaded: has the initial offset been loaded yet
583  * @offset: ptr to current offset value
584  * @stat: ptr to the stat
585  **/
586 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
587 			       bool offset_loaded, u64 *offset, u64 *stat)
588 {
589 	u32 new_data;
590 
591 	new_data = rd32(hw, reg);
592 	if (!offset_loaded)
593 		*offset = new_data;
594 	if (likely(new_data >= *offset))
595 		*stat = (u32)(new_data - *offset);
596 	else
597 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
598 }
599 
600 /**
601  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
602  * @hw: ptr to the hardware info
603  * @reg: the hw reg to read and clear
604  * @stat: ptr to the stat
605  **/
606 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
607 {
608 	u32 new_data = rd32(hw, reg);
609 
610 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
611 	*stat += new_data;
612 }
613 
614 /**
615  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
616  * @vsi: the VSI to be updated
617  **/
618 void i40e_update_eth_stats(struct i40e_vsi *vsi)
619 {
620 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
621 	struct i40e_pf *pf = vsi->back;
622 	struct i40e_hw *hw = &pf->hw;
623 	struct i40e_eth_stats *oes;
624 	struct i40e_eth_stats *es;     /* device's eth stats */
625 
626 	es = &vsi->eth_stats;
627 	oes = &vsi->eth_stats_offsets;
628 
629 	/* Gather up the stats that the hw collects */
630 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
631 			   vsi->stat_offsets_loaded,
632 			   &oes->tx_errors, &es->tx_errors);
633 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
634 			   vsi->stat_offsets_loaded,
635 			   &oes->rx_discards, &es->rx_discards);
636 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
637 			   vsi->stat_offsets_loaded,
638 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
639 
640 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
641 			   I40E_GLV_GORCL(stat_idx),
642 			   vsi->stat_offsets_loaded,
643 			   &oes->rx_bytes, &es->rx_bytes);
644 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
645 			   I40E_GLV_UPRCL(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unicast, &es->rx_unicast);
648 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
649 			   I40E_GLV_MPRCL(stat_idx),
650 			   vsi->stat_offsets_loaded,
651 			   &oes->rx_multicast, &es->rx_multicast);
652 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
653 			   I40E_GLV_BPRCL(stat_idx),
654 			   vsi->stat_offsets_loaded,
655 			   &oes->rx_broadcast, &es->rx_broadcast);
656 
657 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
658 			   I40E_GLV_GOTCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->tx_bytes, &es->tx_bytes);
661 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
662 			   I40E_GLV_UPTCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->tx_unicast, &es->tx_unicast);
665 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
666 			   I40E_GLV_MPTCL(stat_idx),
667 			   vsi->stat_offsets_loaded,
668 			   &oes->tx_multicast, &es->tx_multicast);
669 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
670 			   I40E_GLV_BPTCL(stat_idx),
671 			   vsi->stat_offsets_loaded,
672 			   &oes->tx_broadcast, &es->tx_broadcast);
673 	vsi->stat_offsets_loaded = true;
674 }
675 
676 /**
677  * i40e_update_veb_stats - Update Switch component statistics
678  * @veb: the VEB being updated
679  **/
680 static void i40e_update_veb_stats(struct i40e_veb *veb)
681 {
682 	struct i40e_pf *pf = veb->pf;
683 	struct i40e_hw *hw = &pf->hw;
684 	struct i40e_eth_stats *oes;
685 	struct i40e_eth_stats *es;     /* device's eth stats */
686 	struct i40e_veb_tc_stats *veb_oes;
687 	struct i40e_veb_tc_stats *veb_es;
688 	int i, idx = 0;
689 
690 	idx = veb->stats_idx;
691 	es = &veb->stats;
692 	oes = &veb->stats_offsets;
693 	veb_es = &veb->tc_stats;
694 	veb_oes = &veb->tc_stats_offsets;
695 
696 	/* Gather up the stats that the hw collects */
697 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
698 			   veb->stat_offsets_loaded,
699 			   &oes->tx_discards, &es->tx_discards);
700 	if (hw->revision_id > 0)
701 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
702 				   veb->stat_offsets_loaded,
703 				   &oes->rx_unknown_protocol,
704 				   &es->rx_unknown_protocol);
705 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
706 			   veb->stat_offsets_loaded,
707 			   &oes->rx_bytes, &es->rx_bytes);
708 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
709 			   veb->stat_offsets_loaded,
710 			   &oes->rx_unicast, &es->rx_unicast);
711 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
712 			   veb->stat_offsets_loaded,
713 			   &oes->rx_multicast, &es->rx_multicast);
714 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_broadcast, &es->rx_broadcast);
717 
718 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->tx_bytes, &es->tx_bytes);
721 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->tx_unicast, &es->tx_unicast);
724 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->tx_multicast, &es->tx_multicast);
727 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_broadcast, &es->tx_broadcast);
730 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
731 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
732 				   I40E_GLVEBTC_RPCL(i, idx),
733 				   veb->stat_offsets_loaded,
734 				   &veb_oes->tc_rx_packets[i],
735 				   &veb_es->tc_rx_packets[i]);
736 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
737 				   I40E_GLVEBTC_RBCL(i, idx),
738 				   veb->stat_offsets_loaded,
739 				   &veb_oes->tc_rx_bytes[i],
740 				   &veb_es->tc_rx_bytes[i]);
741 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
742 				   I40E_GLVEBTC_TPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_tx_packets[i],
745 				   &veb_es->tc_tx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
747 				   I40E_GLVEBTC_TBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_tx_bytes[i],
750 				   &veb_es->tc_tx_bytes[i]);
751 	}
752 	veb->stat_offsets_loaded = true;
753 }
754 
755 /**
756  * i40e_update_vsi_stats - Update the vsi statistics counters.
757  * @vsi: the VSI to be updated
758  *
759  * There are a few instances where we store the same stat in a
760  * couple of different structs.  This is partly because we have
761  * the netdev stats that need to be filled out, which is slightly
762  * different from the "eth_stats" defined by the chip and used in
763  * VF communications.  We sort it out here.
764  **/
765 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
766 {
767 	struct i40e_pf *pf = vsi->back;
768 	struct rtnl_link_stats64 *ons;
769 	struct rtnl_link_stats64 *ns;   /* netdev stats */
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	u32 tx_restart, tx_busy;
773 	struct i40e_ring *p;
774 	u32 rx_page, rx_buf;
775 	u64 bytes, packets;
776 	unsigned int start;
777 	u64 tx_linearize;
778 	u64 tx_force_wb;
779 	u64 rx_p, rx_b;
780 	u64 tx_p, tx_b;
781 	u16 q;
782 
783 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
784 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
785 		return;
786 
787 	ns = i40e_get_vsi_stats_struct(vsi);
788 	ons = &vsi->net_stats_offsets;
789 	es = &vsi->eth_stats;
790 	oes = &vsi->eth_stats_offsets;
791 
792 	/* Gather up the netdev and vsi stats that the driver collects
793 	 * on the fly during packet processing
794 	 */
795 	rx_b = rx_p = 0;
796 	tx_b = tx_p = 0;
797 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
798 	rx_page = 0;
799 	rx_buf = 0;
800 	rcu_read_lock();
801 	for (q = 0; q < vsi->num_queue_pairs; q++) {
802 		/* locate Tx ring */
803 		p = READ_ONCE(vsi->tx_rings[q]);
804 
805 		do {
806 			start = u64_stats_fetch_begin_irq(&p->syncp);
807 			packets = p->stats.packets;
808 			bytes = p->stats.bytes;
809 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
810 		tx_b += bytes;
811 		tx_p += packets;
812 		tx_restart += p->tx_stats.restart_queue;
813 		tx_busy += p->tx_stats.tx_busy;
814 		tx_linearize += p->tx_stats.tx_linearize;
815 		tx_force_wb += p->tx_stats.tx_force_wb;
816 
817 		/* Rx queue is part of the same block as Tx queue */
818 		p = &p[1];
819 		do {
820 			start = u64_stats_fetch_begin_irq(&p->syncp);
821 			packets = p->stats.packets;
822 			bytes = p->stats.bytes;
823 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
824 		rx_b += bytes;
825 		rx_p += packets;
826 		rx_buf += p->rx_stats.alloc_buff_failed;
827 		rx_page += p->rx_stats.alloc_page_failed;
828 	}
829 	rcu_read_unlock();
830 	vsi->tx_restart = tx_restart;
831 	vsi->tx_busy = tx_busy;
832 	vsi->tx_linearize = tx_linearize;
833 	vsi->tx_force_wb = tx_force_wb;
834 	vsi->rx_page_failed = rx_page;
835 	vsi->rx_buf_failed = rx_buf;
836 
837 	ns->rx_packets = rx_p;
838 	ns->rx_bytes = rx_b;
839 	ns->tx_packets = tx_p;
840 	ns->tx_bytes = tx_b;
841 
842 	/* update netdev stats from eth stats */
843 	i40e_update_eth_stats(vsi);
844 	ons->tx_errors = oes->tx_errors;
845 	ns->tx_errors = es->tx_errors;
846 	ons->multicast = oes->rx_multicast;
847 	ns->multicast = es->rx_multicast;
848 	ons->rx_dropped = oes->rx_discards;
849 	ns->rx_dropped = es->rx_discards;
850 	ons->tx_dropped = oes->tx_discards;
851 	ns->tx_dropped = es->tx_discards;
852 
853 	/* pull in a couple PF stats if this is the main vsi */
854 	if (vsi == pf->vsi[pf->lan_vsi]) {
855 		ns->rx_crc_errors = pf->stats.crc_errors;
856 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
857 		ns->rx_length_errors = pf->stats.rx_length_errors;
858 	}
859 }
860 
861 /**
862  * i40e_update_pf_stats - Update the PF statistics counters.
863  * @pf: the PF to be updated
864  **/
865 static void i40e_update_pf_stats(struct i40e_pf *pf)
866 {
867 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
868 	struct i40e_hw_port_stats *nsd = &pf->stats;
869 	struct i40e_hw *hw = &pf->hw;
870 	u32 val;
871 	int i;
872 
873 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
874 			   I40E_GLPRT_GORCL(hw->port),
875 			   pf->stat_offsets_loaded,
876 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
877 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
878 			   I40E_GLPRT_GOTCL(hw->port),
879 			   pf->stat_offsets_loaded,
880 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
881 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
882 			   pf->stat_offsets_loaded,
883 			   &osd->eth.rx_discards,
884 			   &nsd->eth.rx_discards);
885 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
886 			   I40E_GLPRT_UPRCL(hw->port),
887 			   pf->stat_offsets_loaded,
888 			   &osd->eth.rx_unicast,
889 			   &nsd->eth.rx_unicast);
890 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
891 			   I40E_GLPRT_MPRCL(hw->port),
892 			   pf->stat_offsets_loaded,
893 			   &osd->eth.rx_multicast,
894 			   &nsd->eth.rx_multicast);
895 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
896 			   I40E_GLPRT_BPRCL(hw->port),
897 			   pf->stat_offsets_loaded,
898 			   &osd->eth.rx_broadcast,
899 			   &nsd->eth.rx_broadcast);
900 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
901 			   I40E_GLPRT_UPTCL(hw->port),
902 			   pf->stat_offsets_loaded,
903 			   &osd->eth.tx_unicast,
904 			   &nsd->eth.tx_unicast);
905 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
906 			   I40E_GLPRT_MPTCL(hw->port),
907 			   pf->stat_offsets_loaded,
908 			   &osd->eth.tx_multicast,
909 			   &nsd->eth.tx_multicast);
910 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
911 			   I40E_GLPRT_BPTCL(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->eth.tx_broadcast,
914 			   &nsd->eth.tx_broadcast);
915 
916 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
917 			   pf->stat_offsets_loaded,
918 			   &osd->tx_dropped_link_down,
919 			   &nsd->tx_dropped_link_down);
920 
921 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
922 			   pf->stat_offsets_loaded,
923 			   &osd->crc_errors, &nsd->crc_errors);
924 
925 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
926 			   pf->stat_offsets_loaded,
927 			   &osd->illegal_bytes, &nsd->illegal_bytes);
928 
929 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->mac_local_faults,
932 			   &nsd->mac_local_faults);
933 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
934 			   pf->stat_offsets_loaded,
935 			   &osd->mac_remote_faults,
936 			   &nsd->mac_remote_faults);
937 
938 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->rx_length_errors,
941 			   &nsd->rx_length_errors);
942 
943 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
944 			   pf->stat_offsets_loaded,
945 			   &osd->link_xon_rx, &nsd->link_xon_rx);
946 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->link_xon_tx, &nsd->link_xon_tx);
949 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
952 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
953 			   pf->stat_offsets_loaded,
954 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
955 
956 	for (i = 0; i < 8; i++) {
957 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
958 				   pf->stat_offsets_loaded,
959 				   &osd->priority_xoff_rx[i],
960 				   &nsd->priority_xoff_rx[i]);
961 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
962 				   pf->stat_offsets_loaded,
963 				   &osd->priority_xon_rx[i],
964 				   &nsd->priority_xon_rx[i]);
965 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
966 				   pf->stat_offsets_loaded,
967 				   &osd->priority_xon_tx[i],
968 				   &nsd->priority_xon_tx[i]);
969 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
970 				   pf->stat_offsets_loaded,
971 				   &osd->priority_xoff_tx[i],
972 				   &nsd->priority_xoff_tx[i]);
973 		i40e_stat_update32(hw,
974 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
975 				   pf->stat_offsets_loaded,
976 				   &osd->priority_xon_2_xoff[i],
977 				   &nsd->priority_xon_2_xoff[i]);
978 	}
979 
980 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
981 			   I40E_GLPRT_PRC64L(hw->port),
982 			   pf->stat_offsets_loaded,
983 			   &osd->rx_size_64, &nsd->rx_size_64);
984 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
985 			   I40E_GLPRT_PRC127L(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->rx_size_127, &nsd->rx_size_127);
988 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
989 			   I40E_GLPRT_PRC255L(hw->port),
990 			   pf->stat_offsets_loaded,
991 			   &osd->rx_size_255, &nsd->rx_size_255);
992 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
993 			   I40E_GLPRT_PRC511L(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->rx_size_511, &nsd->rx_size_511);
996 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
997 			   I40E_GLPRT_PRC1023L(hw->port),
998 			   pf->stat_offsets_loaded,
999 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1000 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1001 			   I40E_GLPRT_PRC1522L(hw->port),
1002 			   pf->stat_offsets_loaded,
1003 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1004 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1005 			   I40E_GLPRT_PRC9522L(hw->port),
1006 			   pf->stat_offsets_loaded,
1007 			   &osd->rx_size_big, &nsd->rx_size_big);
1008 
1009 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1010 			   I40E_GLPRT_PTC64L(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->tx_size_64, &nsd->tx_size_64);
1013 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1014 			   I40E_GLPRT_PTC127L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->tx_size_127, &nsd->tx_size_127);
1017 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1018 			   I40E_GLPRT_PTC255L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->tx_size_255, &nsd->tx_size_255);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1022 			   I40E_GLPRT_PTC511L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->tx_size_511, &nsd->tx_size_511);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1026 			   I40E_GLPRT_PTC1023L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1030 			   I40E_GLPRT_PTC1522L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1034 			   I40E_GLPRT_PTC9522L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->tx_size_big, &nsd->tx_size_big);
1037 
1038 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_undersize, &nsd->rx_undersize);
1041 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->rx_fragments, &nsd->rx_fragments);
1044 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->rx_oversize, &nsd->rx_oversize);
1047 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_jabber, &nsd->rx_jabber);
1050 
1051 	/* FDIR stats */
1052 	i40e_stat_update_and_clear32(hw,
1053 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1054 			&nsd->fd_atr_match);
1055 	i40e_stat_update_and_clear32(hw,
1056 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1057 			&nsd->fd_sb_match);
1058 	i40e_stat_update_and_clear32(hw,
1059 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1060 			&nsd->fd_atr_tunnel_match);
1061 
1062 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1063 	nsd->tx_lpi_status =
1064 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1065 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1066 	nsd->rx_lpi_status =
1067 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1068 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1069 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1072 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1075 
1076 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1077 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1078 		nsd->fd_sb_status = true;
1079 	else
1080 		nsd->fd_sb_status = false;
1081 
1082 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1083 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1084 		nsd->fd_atr_status = true;
1085 	else
1086 		nsd->fd_atr_status = false;
1087 
1088 	pf->stat_offsets_loaded = true;
1089 }
1090 
1091 /**
1092  * i40e_update_stats - Update the various statistics counters.
1093  * @vsi: the VSI to be updated
1094  *
1095  * Update the various stats for this VSI and its related entities.
1096  **/
1097 void i40e_update_stats(struct i40e_vsi *vsi)
1098 {
1099 	struct i40e_pf *pf = vsi->back;
1100 
1101 	if (vsi == pf->vsi[pf->lan_vsi])
1102 		i40e_update_pf_stats(pf);
1103 
1104 	i40e_update_vsi_stats(vsi);
1105 }
1106 
1107 /**
1108  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1109  * @vsi: the VSI to be searched
1110  * @macaddr: the MAC address
1111  * @vlan: the vlan
1112  *
1113  * Returns ptr to the filter object or NULL
1114  **/
1115 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1116 						const u8 *macaddr, s16 vlan)
1117 {
1118 	struct i40e_mac_filter *f;
1119 	u64 key;
1120 
1121 	if (!vsi || !macaddr)
1122 		return NULL;
1123 
1124 	key = i40e_addr_to_hkey(macaddr);
1125 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1126 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1127 		    (vlan == f->vlan))
1128 			return f;
1129 	}
1130 	return NULL;
1131 }
1132 
1133 /**
1134  * i40e_find_mac - Find a mac addr in the macvlan filters list
1135  * @vsi: the VSI to be searched
1136  * @macaddr: the MAC address we are searching for
1137  *
1138  * Returns the first filter with the provided MAC address or NULL if
1139  * MAC address was not found
1140  **/
1141 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1142 {
1143 	struct i40e_mac_filter *f;
1144 	u64 key;
1145 
1146 	if (!vsi || !macaddr)
1147 		return NULL;
1148 
1149 	key = i40e_addr_to_hkey(macaddr);
1150 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1151 		if ((ether_addr_equal(macaddr, f->macaddr)))
1152 			return f;
1153 	}
1154 	return NULL;
1155 }
1156 
1157 /**
1158  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns true if VSI is in vlan mode or false otherwise
1162  **/
1163 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1164 {
1165 	/* If we have a PVID, always operate in VLAN mode */
1166 	if (vsi->info.pvid)
1167 		return true;
1168 
1169 	/* We need to operate in VLAN mode whenever we have any filters with
1170 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1171 	 * time, incurring search cost repeatedly. However, we can notice two
1172 	 * things:
1173 	 *
1174 	 * 1) the only place where we can gain a VLAN filter is in
1175 	 *    i40e_add_filter.
1176 	 *
1177 	 * 2) the only place where filters are actually removed is in
1178 	 *    i40e_sync_filters_subtask.
1179 	 *
1180 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1181 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1182 	 * we have to perform the full search after deleting filters in
1183 	 * i40e_sync_filters_subtask, but we already have to search
1184 	 * filters here and can perform the check at the same time. This
1185 	 * results in avoiding embedding a loop for VLAN mode inside another
1186 	 * loop over all the filters, and should maintain correctness as noted
1187 	 * above.
1188 	 */
1189 	return vsi->has_vlan_filter;
1190 }
1191 
1192 /**
1193  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1194  * @vsi: the VSI to configure
1195  * @tmp_add_list: list of filters ready to be added
1196  * @tmp_del_list: list of filters ready to be deleted
1197  * @vlan_filters: the number of active VLAN filters
1198  *
1199  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1200  * behave as expected. If we have any active VLAN filters remaining or about
1201  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1202  * so that they only match against untagged traffic. If we no longer have any
1203  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1204  * so that they match against both tagged and untagged traffic. In this way,
1205  * we ensure that we correctly receive the desired traffic. This ensures that
1206  * when we have an active VLAN we will receive only untagged traffic and
1207  * traffic matching active VLANs. If we have no active VLANs then we will
1208  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1209  *
1210  * Finally, in a similar fashion, this function also corrects filters when
1211  * there is an active PVID assigned to this VSI.
1212  *
1213  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1214  *
1215  * This function is only expected to be called from within
1216  * i40e_sync_vsi_filters.
1217  *
1218  * NOTE: This function expects to be called while under the
1219  * mac_filter_hash_lock
1220  */
1221 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1222 					 struct hlist_head *tmp_add_list,
1223 					 struct hlist_head *tmp_del_list,
1224 					 int vlan_filters)
1225 {
1226 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1227 	struct i40e_mac_filter *f, *add_head;
1228 	struct i40e_new_mac_filter *new;
1229 	struct hlist_node *h;
1230 	int bkt, new_vlan;
1231 
1232 	/* To determine if a particular filter needs to be replaced we
1233 	 * have the three following conditions:
1234 	 *
1235 	 * a) if we have a PVID assigned, then all filters which are
1236 	 *    not marked as VLAN=PVID must be replaced with filters that
1237 	 *    are.
1238 	 * b) otherwise, if we have any active VLANS, all filters
1239 	 *    which are marked as VLAN=-1 must be replaced with
1240 	 *    filters marked as VLAN=0
1241 	 * c) finally, if we do not have any active VLANS, all filters
1242 	 *    which are marked as VLAN=0 must be replaced with filters
1243 	 *    marked as VLAN=-1
1244 	 */
1245 
1246 	/* Update the filters about to be added in place */
1247 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1248 		if (pvid && new->f->vlan != pvid)
1249 			new->f->vlan = pvid;
1250 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1251 			new->f->vlan = 0;
1252 		else if (!vlan_filters && new->f->vlan == 0)
1253 			new->f->vlan = I40E_VLAN_ANY;
1254 	}
1255 
1256 	/* Update the remaining active filters */
1257 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1258 		/* Combine the checks for whether a filter needs to be changed
1259 		 * and then determine the new VLAN inside the if block, in
1260 		 * order to avoid duplicating code for adding the new filter
1261 		 * then deleting the old filter.
1262 		 */
1263 		if ((pvid && f->vlan != pvid) ||
1264 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1265 		    (!vlan_filters && f->vlan == 0)) {
1266 			/* Determine the new vlan we will be adding */
1267 			if (pvid)
1268 				new_vlan = pvid;
1269 			else if (vlan_filters)
1270 				new_vlan = 0;
1271 			else
1272 				new_vlan = I40E_VLAN_ANY;
1273 
1274 			/* Create the new filter */
1275 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1276 			if (!add_head)
1277 				return -ENOMEM;
1278 
1279 			/* Create a temporary i40e_new_mac_filter */
1280 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1281 			if (!new)
1282 				return -ENOMEM;
1283 
1284 			new->f = add_head;
1285 			new->state = add_head->state;
1286 
1287 			/* Add the new filter to the tmp list */
1288 			hlist_add_head(&new->hlist, tmp_add_list);
1289 
1290 			/* Put the original filter into the delete list */
1291 			f->state = I40E_FILTER_REMOVE;
1292 			hash_del(&f->hlist);
1293 			hlist_add_head(&f->hlist, tmp_del_list);
1294 		}
1295 	}
1296 
1297 	vsi->has_vlan_filter = !!vlan_filters;
1298 
1299 	return 0;
1300 }
1301 
1302 /**
1303  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1304  * @vsi: the PF Main VSI - inappropriate for any other VSI
1305  * @macaddr: the MAC address
1306  *
1307  * Remove whatever filter the firmware set up so the driver can manage
1308  * its own filtering intelligently.
1309  **/
1310 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1311 {
1312 	struct i40e_aqc_remove_macvlan_element_data element;
1313 	struct i40e_pf *pf = vsi->back;
1314 
1315 	/* Only appropriate for the PF main VSI */
1316 	if (vsi->type != I40E_VSI_MAIN)
1317 		return;
1318 
1319 	memset(&element, 0, sizeof(element));
1320 	ether_addr_copy(element.mac_addr, macaddr);
1321 	element.vlan_tag = 0;
1322 	/* Ignore error returns, some firmware does it this way... */
1323 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1324 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1325 
1326 	memset(&element, 0, sizeof(element));
1327 	ether_addr_copy(element.mac_addr, macaddr);
1328 	element.vlan_tag = 0;
1329 	/* ...and some firmware does it this way. */
1330 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1331 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1332 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1333 }
1334 
1335 /**
1336  * i40e_add_filter - Add a mac/vlan filter to the VSI
1337  * @vsi: the VSI to be searched
1338  * @macaddr: the MAC address
1339  * @vlan: the vlan
1340  *
1341  * Returns ptr to the filter object or NULL when no memory available.
1342  *
1343  * NOTE: This function is expected to be called with mac_filter_hash_lock
1344  * being held.
1345  **/
1346 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1347 					const u8 *macaddr, s16 vlan)
1348 {
1349 	struct i40e_mac_filter *f;
1350 	u64 key;
1351 
1352 	if (!vsi || !macaddr)
1353 		return NULL;
1354 
1355 	f = i40e_find_filter(vsi, macaddr, vlan);
1356 	if (!f) {
1357 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1358 		if (!f)
1359 			return NULL;
1360 
1361 		/* Update the boolean indicating if we need to function in
1362 		 * VLAN mode.
1363 		 */
1364 		if (vlan >= 0)
1365 			vsi->has_vlan_filter = true;
1366 
1367 		ether_addr_copy(f->macaddr, macaddr);
1368 		f->vlan = vlan;
1369 		f->state = I40E_FILTER_NEW;
1370 		INIT_HLIST_NODE(&f->hlist);
1371 
1372 		key = i40e_addr_to_hkey(macaddr);
1373 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1374 
1375 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1376 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1377 	}
1378 
1379 	/* If we're asked to add a filter that has been marked for removal, it
1380 	 * is safe to simply restore it to active state. __i40e_del_filter
1381 	 * will have simply deleted any filters which were previously marked
1382 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1383 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1384 	 * task, just restore this filter to the ACTIVE state so that the
1385 	 * sync task leaves it in place
1386 	 */
1387 	if (f->state == I40E_FILTER_REMOVE)
1388 		f->state = I40E_FILTER_ACTIVE;
1389 
1390 	return f;
1391 }
1392 
1393 /**
1394  * __i40e_del_filter - Remove a specific filter from the VSI
1395  * @vsi: VSI to remove from
1396  * @f: the filter to remove from the list
1397  *
1398  * This function should be called instead of i40e_del_filter only if you know
1399  * the exact filter you will remove already, such as via i40e_find_filter or
1400  * i40e_find_mac.
1401  *
1402  * NOTE: This function is expected to be called with mac_filter_hash_lock
1403  * being held.
1404  * ANOTHER NOTE: This function MUST be called from within the context of
1405  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1406  * instead of list_for_each_entry().
1407  **/
1408 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1409 {
1410 	if (!f)
1411 		return;
1412 
1413 	/* If the filter was never added to firmware then we can just delete it
1414 	 * directly and we don't want to set the status to remove or else an
1415 	 * admin queue command will unnecessarily fire.
1416 	 */
1417 	if ((f->state == I40E_FILTER_FAILED) ||
1418 	    (f->state == I40E_FILTER_NEW)) {
1419 		hash_del(&f->hlist);
1420 		kfree(f);
1421 	} else {
1422 		f->state = I40E_FILTER_REMOVE;
1423 	}
1424 
1425 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1426 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1427 }
1428 
1429 /**
1430  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1431  * @vsi: the VSI to be searched
1432  * @macaddr: the MAC address
1433  * @vlan: the VLAN
1434  *
1435  * NOTE: This function is expected to be called with mac_filter_hash_lock
1436  * being held.
1437  * ANOTHER NOTE: This function MUST be called from within the context of
1438  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1439  * instead of list_for_each_entry().
1440  **/
1441 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1442 {
1443 	struct i40e_mac_filter *f;
1444 
1445 	if (!vsi || !macaddr)
1446 		return;
1447 
1448 	f = i40e_find_filter(vsi, macaddr, vlan);
1449 	__i40e_del_filter(vsi, f);
1450 }
1451 
1452 /**
1453  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1454  * @vsi: the VSI to be searched
1455  * @macaddr: the mac address to be filtered
1456  *
1457  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1458  * go through all the macvlan filters and add a macvlan filter for each
1459  * unique vlan that already exists. If a PVID has been assigned, instead only
1460  * add the macaddr to that VLAN.
1461  *
1462  * Returns last filter added on success, else NULL
1463  **/
1464 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1465 					    const u8 *macaddr)
1466 {
1467 	struct i40e_mac_filter *f, *add = NULL;
1468 	struct hlist_node *h;
1469 	int bkt;
1470 
1471 	if (vsi->info.pvid)
1472 		return i40e_add_filter(vsi, macaddr,
1473 				       le16_to_cpu(vsi->info.pvid));
1474 
1475 	if (!i40e_is_vsi_in_vlan(vsi))
1476 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1477 
1478 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1479 		if (f->state == I40E_FILTER_REMOVE)
1480 			continue;
1481 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1482 		if (!add)
1483 			return NULL;
1484 	}
1485 
1486 	return add;
1487 }
1488 
1489 /**
1490  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1491  * @vsi: the VSI to be searched
1492  * @macaddr: the mac address to be removed
1493  *
1494  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1495  * associated with.
1496  *
1497  * Returns 0 for success, or error
1498  **/
1499 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1500 {
1501 	struct i40e_mac_filter *f;
1502 	struct hlist_node *h;
1503 	bool found = false;
1504 	int bkt;
1505 
1506 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1507 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1508 		if (ether_addr_equal(macaddr, f->macaddr)) {
1509 			__i40e_del_filter(vsi, f);
1510 			found = true;
1511 		}
1512 	}
1513 
1514 	if (found)
1515 		return 0;
1516 	else
1517 		return -ENOENT;
1518 }
1519 
1520 /**
1521  * i40e_set_mac - NDO callback to set mac address
1522  * @netdev: network interface device structure
1523  * @p: pointer to an address structure
1524  *
1525  * Returns 0 on success, negative on failure
1526  **/
1527 static int i40e_set_mac(struct net_device *netdev, void *p)
1528 {
1529 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1530 	struct i40e_vsi *vsi = np->vsi;
1531 	struct i40e_pf *pf = vsi->back;
1532 	struct i40e_hw *hw = &pf->hw;
1533 	struct sockaddr *addr = p;
1534 
1535 	if (!is_valid_ether_addr(addr->sa_data))
1536 		return -EADDRNOTAVAIL;
1537 
1538 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1539 		netdev_info(netdev, "already using mac address %pM\n",
1540 			    addr->sa_data);
1541 		return 0;
1542 	}
1543 
1544 	if (test_bit(__I40E_DOWN, pf->state) ||
1545 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1546 		return -EADDRNOTAVAIL;
1547 
1548 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1549 		netdev_info(netdev, "returning to hw mac address %pM\n",
1550 			    hw->mac.addr);
1551 	else
1552 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1553 
1554 	/* Copy the address first, so that we avoid a possible race with
1555 	 * .set_rx_mode().
1556 	 * - Remove old address from MAC filter
1557 	 * - Copy new address
1558 	 * - Add new address to MAC filter
1559 	 */
1560 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1561 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1562 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1563 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1564 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1565 
1566 	if (vsi->type == I40E_VSI_MAIN) {
1567 		i40e_status ret;
1568 
1569 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1570 						addr->sa_data, NULL);
1571 		if (ret)
1572 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1573 				    i40e_stat_str(hw, ret),
1574 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1575 	}
1576 
1577 	/* schedule our worker thread which will take care of
1578 	 * applying the new filter changes
1579 	 */
1580 	i40e_service_event_schedule(pf);
1581 	return 0;
1582 }
1583 
1584 /**
1585  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1586  * @vsi: vsi structure
1587  * @seed: RSS hash seed
1588  **/
1589 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1590 			      u8 *lut, u16 lut_size)
1591 {
1592 	struct i40e_pf *pf = vsi->back;
1593 	struct i40e_hw *hw = &pf->hw;
1594 	int ret = 0;
1595 
1596 	if (seed) {
1597 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1598 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1599 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1600 		if (ret) {
1601 			dev_info(&pf->pdev->dev,
1602 				 "Cannot set RSS key, err %s aq_err %s\n",
1603 				 i40e_stat_str(hw, ret),
1604 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1605 			return ret;
1606 		}
1607 	}
1608 	if (lut) {
1609 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1610 
1611 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1612 		if (ret) {
1613 			dev_info(&pf->pdev->dev,
1614 				 "Cannot set RSS lut, err %s aq_err %s\n",
1615 				 i40e_stat_str(hw, ret),
1616 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1617 			return ret;
1618 		}
1619 	}
1620 	return ret;
1621 }
1622 
1623 /**
1624  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1625  * @vsi: VSI structure
1626  **/
1627 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1628 {
1629 	struct i40e_pf *pf = vsi->back;
1630 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1631 	u8 *lut;
1632 	int ret;
1633 
1634 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1635 		return 0;
1636 	if (!vsi->rss_size)
1637 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1638 				      vsi->num_queue_pairs);
1639 	if (!vsi->rss_size)
1640 		return -EINVAL;
1641 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1642 	if (!lut)
1643 		return -ENOMEM;
1644 
1645 	/* Use the user configured hash keys and lookup table if there is one,
1646 	 * otherwise use default
1647 	 */
1648 	if (vsi->rss_lut_user)
1649 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1650 	else
1651 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1652 	if (vsi->rss_hkey_user)
1653 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1654 	else
1655 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1656 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1657 	kfree(lut);
1658 	return ret;
1659 }
1660 
1661 /**
1662  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1663  * @vsi: the VSI being configured,
1664  * @ctxt: VSI context structure
1665  * @enabled_tc: number of traffic classes to enable
1666  *
1667  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1668  **/
1669 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1670 					   struct i40e_vsi_context *ctxt,
1671 					   u8 enabled_tc)
1672 {
1673 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1674 	int i, override_q, pow, num_qps, ret;
1675 	u8 netdev_tc = 0, offset = 0;
1676 
1677 	if (vsi->type != I40E_VSI_MAIN)
1678 		return -EINVAL;
1679 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1680 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1681 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1682 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1683 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1684 
1685 	/* find the next higher power-of-2 of num queue pairs */
1686 	pow = ilog2(num_qps);
1687 	if (!is_power_of_2(num_qps))
1688 		pow++;
1689 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1690 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1691 
1692 	/* Setup queue offset/count for all TCs for given VSI */
1693 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1694 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1695 		/* See if the given TC is enabled for the given VSI */
1696 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1697 			offset = vsi->mqprio_qopt.qopt.offset[i];
1698 			qcount = vsi->mqprio_qopt.qopt.count[i];
1699 			if (qcount > max_qcount)
1700 				max_qcount = qcount;
1701 			vsi->tc_config.tc_info[i].qoffset = offset;
1702 			vsi->tc_config.tc_info[i].qcount = qcount;
1703 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1704 		} else {
1705 			/* TC is not enabled so set the offset to
1706 			 * default queue and allocate one queue
1707 			 * for the given TC.
1708 			 */
1709 			vsi->tc_config.tc_info[i].qoffset = 0;
1710 			vsi->tc_config.tc_info[i].qcount = 1;
1711 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1712 		}
1713 	}
1714 
1715 	/* Set actual Tx/Rx queue pairs */
1716 	vsi->num_queue_pairs = offset + qcount;
1717 
1718 	/* Setup queue TC[0].qmap for given VSI context */
1719 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1720 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1721 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1722 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1723 
1724 	/* Reconfigure RSS for main VSI with max queue count */
1725 	vsi->rss_size = max_qcount;
1726 	ret = i40e_vsi_config_rss(vsi);
1727 	if (ret) {
1728 		dev_info(&vsi->back->pdev->dev,
1729 			 "Failed to reconfig rss for num_queues (%u)\n",
1730 			 max_qcount);
1731 		return ret;
1732 	}
1733 	vsi->reconfig_rss = true;
1734 	dev_dbg(&vsi->back->pdev->dev,
1735 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1736 
1737 	/* Find queue count available for channel VSIs and starting offset
1738 	 * for channel VSIs
1739 	 */
1740 	override_q = vsi->mqprio_qopt.qopt.count[0];
1741 	if (override_q && override_q < vsi->num_queue_pairs) {
1742 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1743 		vsi->next_base_queue = override_q;
1744 	}
1745 	return 0;
1746 }
1747 
1748 /**
1749  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1750  * @vsi: the VSI being setup
1751  * @ctxt: VSI context structure
1752  * @enabled_tc: Enabled TCs bitmap
1753  * @is_add: True if called before Add VSI
1754  *
1755  * Setup VSI queue mapping for enabled traffic classes.
1756  **/
1757 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1758 				     struct i40e_vsi_context *ctxt,
1759 				     u8 enabled_tc,
1760 				     bool is_add)
1761 {
1762 	struct i40e_pf *pf = vsi->back;
1763 	u16 sections = 0;
1764 	u8 netdev_tc = 0;
1765 	u16 numtc = 1;
1766 	u16 qcount;
1767 	u8 offset;
1768 	u16 qmap;
1769 	int i;
1770 	u16 num_tc_qps = 0;
1771 
1772 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1773 	offset = 0;
1774 
1775 	/* Number of queues per enabled TC */
1776 	num_tc_qps = vsi->alloc_queue_pairs;
1777 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1778 		/* Find numtc from enabled TC bitmap */
1779 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1780 			if (enabled_tc & BIT(i)) /* TC is enabled */
1781 				numtc++;
1782 		}
1783 		if (!numtc) {
1784 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1785 			numtc = 1;
1786 		}
1787 		num_tc_qps = num_tc_qps / numtc;
1788 		num_tc_qps = min_t(int, num_tc_qps,
1789 				   i40e_pf_get_max_q_per_tc(pf));
1790 	}
1791 
1792 	vsi->tc_config.numtc = numtc;
1793 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1794 
1795 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1796 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1797 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1798 
1799 	/* Setup queue offset/count for all TCs for given VSI */
1800 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1801 		/* See if the given TC is enabled for the given VSI */
1802 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1803 			/* TC is enabled */
1804 			int pow, num_qps;
1805 
1806 			switch (vsi->type) {
1807 			case I40E_VSI_MAIN:
1808 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1809 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1810 				    vsi->tc_config.enabled_tc != 1) {
1811 					qcount = min_t(int, pf->alloc_rss_size,
1812 						       num_tc_qps);
1813 					break;
1814 				}
1815 				/* fall through */
1816 			case I40E_VSI_FDIR:
1817 			case I40E_VSI_SRIOV:
1818 			case I40E_VSI_VMDQ2:
1819 			default:
1820 				qcount = num_tc_qps;
1821 				WARN_ON(i != 0);
1822 				break;
1823 			}
1824 			vsi->tc_config.tc_info[i].qoffset = offset;
1825 			vsi->tc_config.tc_info[i].qcount = qcount;
1826 
1827 			/* find the next higher power-of-2 of num queue pairs */
1828 			num_qps = qcount;
1829 			pow = 0;
1830 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1831 				pow++;
1832 				num_qps >>= 1;
1833 			}
1834 
1835 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1836 			qmap =
1837 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1838 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1839 
1840 			offset += qcount;
1841 		} else {
1842 			/* TC is not enabled so set the offset to
1843 			 * default queue and allocate one queue
1844 			 * for the given TC.
1845 			 */
1846 			vsi->tc_config.tc_info[i].qoffset = 0;
1847 			vsi->tc_config.tc_info[i].qcount = 1;
1848 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1849 
1850 			qmap = 0;
1851 		}
1852 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1853 	}
1854 
1855 	/* Set actual Tx/Rx queue pairs */
1856 	vsi->num_queue_pairs = offset;
1857 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1858 		if (vsi->req_queue_pairs > 0)
1859 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1860 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1861 			vsi->num_queue_pairs = pf->num_lan_msix;
1862 	}
1863 
1864 	/* Scheduler section valid can only be set for ADD VSI */
1865 	if (is_add) {
1866 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1867 
1868 		ctxt->info.up_enable_bits = enabled_tc;
1869 	}
1870 	if (vsi->type == I40E_VSI_SRIOV) {
1871 		ctxt->info.mapping_flags |=
1872 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1873 		for (i = 0; i < vsi->num_queue_pairs; i++)
1874 			ctxt->info.queue_mapping[i] =
1875 					       cpu_to_le16(vsi->base_queue + i);
1876 	} else {
1877 		ctxt->info.mapping_flags |=
1878 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1879 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1880 	}
1881 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1882 }
1883 
1884 /**
1885  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1886  * @netdev: the netdevice
1887  * @addr: address to add
1888  *
1889  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1890  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1891  */
1892 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1893 {
1894 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1895 	struct i40e_vsi *vsi = np->vsi;
1896 
1897 	if (i40e_add_mac_filter(vsi, addr))
1898 		return 0;
1899 	else
1900 		return -ENOMEM;
1901 }
1902 
1903 /**
1904  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1905  * @netdev: the netdevice
1906  * @addr: address to add
1907  *
1908  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1909  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1910  */
1911 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1912 {
1913 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1914 	struct i40e_vsi *vsi = np->vsi;
1915 
1916 	/* Under some circumstances, we might receive a request to delete
1917 	 * our own device address from our uc list. Because we store the
1918 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1919 	 * such requests and not delete our device address from this list.
1920 	 */
1921 	if (ether_addr_equal(addr, netdev->dev_addr))
1922 		return 0;
1923 
1924 	i40e_del_mac_filter(vsi, addr);
1925 
1926 	return 0;
1927 }
1928 
1929 /**
1930  * i40e_set_rx_mode - NDO callback to set the netdev filters
1931  * @netdev: network interface device structure
1932  **/
1933 static void i40e_set_rx_mode(struct net_device *netdev)
1934 {
1935 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1936 	struct i40e_vsi *vsi = np->vsi;
1937 
1938 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1939 
1940 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1941 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1942 
1943 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1944 
1945 	/* check for other flag changes */
1946 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1947 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1948 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1949 	}
1950 }
1951 
1952 /**
1953  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1954  * @vsi: Pointer to VSI struct
1955  * @from: Pointer to list which contains MAC filter entries - changes to
1956  *        those entries needs to be undone.
1957  *
1958  * MAC filter entries from this list were slated for deletion.
1959  **/
1960 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1961 					 struct hlist_head *from)
1962 {
1963 	struct i40e_mac_filter *f;
1964 	struct hlist_node *h;
1965 
1966 	hlist_for_each_entry_safe(f, h, from, hlist) {
1967 		u64 key = i40e_addr_to_hkey(f->macaddr);
1968 
1969 		/* Move the element back into MAC filter list*/
1970 		hlist_del(&f->hlist);
1971 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1972 	}
1973 }
1974 
1975 /**
1976  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1977  * @vsi: Pointer to vsi struct
1978  * @from: Pointer to list which contains MAC filter entries - changes to
1979  *        those entries needs to be undone.
1980  *
1981  * MAC filter entries from this list were slated for addition.
1982  **/
1983 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1984 					 struct hlist_head *from)
1985 {
1986 	struct i40e_new_mac_filter *new;
1987 	struct hlist_node *h;
1988 
1989 	hlist_for_each_entry_safe(new, h, from, hlist) {
1990 		/* We can simply free the wrapper structure */
1991 		hlist_del(&new->hlist);
1992 		kfree(new);
1993 	}
1994 }
1995 
1996 /**
1997  * i40e_next_entry - Get the next non-broadcast filter from a list
1998  * @next: pointer to filter in list
1999  *
2000  * Returns the next non-broadcast filter in the list. Required so that we
2001  * ignore broadcast filters within the list, since these are not handled via
2002  * the normal firmware update path.
2003  */
2004 static
2005 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2006 {
2007 	hlist_for_each_entry_continue(next, hlist) {
2008 		if (!is_broadcast_ether_addr(next->f->macaddr))
2009 			return next;
2010 	}
2011 
2012 	return NULL;
2013 }
2014 
2015 /**
2016  * i40e_update_filter_state - Update filter state based on return data
2017  * from firmware
2018  * @count: Number of filters added
2019  * @add_list: return data from fw
2020  * @add_head: pointer to first filter in current batch
2021  *
2022  * MAC filter entries from list were slated to be added to device. Returns
2023  * number of successful filters. Note that 0 does NOT mean success!
2024  **/
2025 static int
2026 i40e_update_filter_state(int count,
2027 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2028 			 struct i40e_new_mac_filter *add_head)
2029 {
2030 	int retval = 0;
2031 	int i;
2032 
2033 	for (i = 0; i < count; i++) {
2034 		/* Always check status of each filter. We don't need to check
2035 		 * the firmware return status because we pre-set the filter
2036 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2037 		 * request to the adminq. Thus, if it no longer matches then
2038 		 * we know the filter is active.
2039 		 */
2040 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2041 			add_head->state = I40E_FILTER_FAILED;
2042 		} else {
2043 			add_head->state = I40E_FILTER_ACTIVE;
2044 			retval++;
2045 		}
2046 
2047 		add_head = i40e_next_filter(add_head);
2048 		if (!add_head)
2049 			break;
2050 	}
2051 
2052 	return retval;
2053 }
2054 
2055 /**
2056  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2057  * @vsi: ptr to the VSI
2058  * @vsi_name: name to display in messages
2059  * @list: the list of filters to send to firmware
2060  * @num_del: the number of filters to delete
2061  * @retval: Set to -EIO on failure to delete
2062  *
2063  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2064  * *retval instead of a return value so that success does not force ret_val to
2065  * be set to 0. This ensures that a sequence of calls to this function
2066  * preserve the previous value of *retval on successful delete.
2067  */
2068 static
2069 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2070 			  struct i40e_aqc_remove_macvlan_element_data *list,
2071 			  int num_del, int *retval)
2072 {
2073 	struct i40e_hw *hw = &vsi->back->hw;
2074 	i40e_status aq_ret;
2075 	int aq_err;
2076 
2077 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2078 	aq_err = hw->aq.asq_last_status;
2079 
2080 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2081 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2082 		*retval = -EIO;
2083 		dev_info(&vsi->back->pdev->dev,
2084 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2085 			 vsi_name, i40e_stat_str(hw, aq_ret),
2086 			 i40e_aq_str(hw, aq_err));
2087 	}
2088 }
2089 
2090 /**
2091  * i40e_aqc_add_filters - Request firmware to add a set of filters
2092  * @vsi: ptr to the VSI
2093  * @vsi_name: name to display in messages
2094  * @list: the list of filters to send to firmware
2095  * @add_head: Position in the add hlist
2096  * @num_add: the number of filters to add
2097  *
2098  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2099  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2100  * space for more filters.
2101  */
2102 static
2103 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2104 			  struct i40e_aqc_add_macvlan_element_data *list,
2105 			  struct i40e_new_mac_filter *add_head,
2106 			  int num_add)
2107 {
2108 	struct i40e_hw *hw = &vsi->back->hw;
2109 	int aq_err, fcnt;
2110 
2111 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2112 	aq_err = hw->aq.asq_last_status;
2113 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2114 
2115 	if (fcnt != num_add) {
2116 		if (vsi->type == I40E_VSI_MAIN) {
2117 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2118 			dev_warn(&vsi->back->pdev->dev,
2119 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2120 				 i40e_aq_str(hw, aq_err), vsi_name);
2121 		} else if (vsi->type == I40E_VSI_SRIOV ||
2122 			   vsi->type == I40E_VSI_VMDQ1 ||
2123 			   vsi->type == I40E_VSI_VMDQ2) {
2124 			dev_warn(&vsi->back->pdev->dev,
2125 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2126 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2127 		} else {
2128 			dev_warn(&vsi->back->pdev->dev,
2129 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2130 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2131 		}
2132 	}
2133 }
2134 
2135 /**
2136  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2137  * @vsi: pointer to the VSI
2138  * @vsi_name: the VSI name
2139  * @f: filter data
2140  *
2141  * This function sets or clears the promiscuous broadcast flags for VLAN
2142  * filters in order to properly receive broadcast frames. Assumes that only
2143  * broadcast filters are passed.
2144  *
2145  * Returns status indicating success or failure;
2146  **/
2147 static i40e_status
2148 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2149 			  struct i40e_mac_filter *f)
2150 {
2151 	bool enable = f->state == I40E_FILTER_NEW;
2152 	struct i40e_hw *hw = &vsi->back->hw;
2153 	i40e_status aq_ret;
2154 
2155 	if (f->vlan == I40E_VLAN_ANY) {
2156 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2157 						   vsi->seid,
2158 						   enable,
2159 						   NULL);
2160 	} else {
2161 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2162 							    vsi->seid,
2163 							    enable,
2164 							    f->vlan,
2165 							    NULL);
2166 	}
2167 
2168 	if (aq_ret) {
2169 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2170 		dev_warn(&vsi->back->pdev->dev,
2171 			 "Error %s, forcing overflow promiscuous on %s\n",
2172 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2173 			 vsi_name);
2174 	}
2175 
2176 	return aq_ret;
2177 }
2178 
2179 /**
2180  * i40e_set_promiscuous - set promiscuous mode
2181  * @pf: board private structure
2182  * @promisc: promisc on or off
2183  *
2184  * There are different ways of setting promiscuous mode on a PF depending on
2185  * what state/environment we're in.  This identifies and sets it appropriately.
2186  * Returns 0 on success.
2187  **/
2188 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2189 {
2190 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2191 	struct i40e_hw *hw = &pf->hw;
2192 	i40e_status aq_ret;
2193 
2194 	if (vsi->type == I40E_VSI_MAIN &&
2195 	    pf->lan_veb != I40E_NO_VEB &&
2196 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2197 		/* set defport ON for Main VSI instead of true promisc
2198 		 * this way we will get all unicast/multicast and VLAN
2199 		 * promisc behavior but will not get VF or VMDq traffic
2200 		 * replicated on the Main VSI.
2201 		 */
2202 		if (promisc)
2203 			aq_ret = i40e_aq_set_default_vsi(hw,
2204 							 vsi->seid,
2205 							 NULL);
2206 		else
2207 			aq_ret = i40e_aq_clear_default_vsi(hw,
2208 							   vsi->seid,
2209 							   NULL);
2210 		if (aq_ret) {
2211 			dev_info(&pf->pdev->dev,
2212 				 "Set default VSI failed, err %s, aq_err %s\n",
2213 				 i40e_stat_str(hw, aq_ret),
2214 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2215 		}
2216 	} else {
2217 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2218 						  hw,
2219 						  vsi->seid,
2220 						  promisc, NULL,
2221 						  true);
2222 		if (aq_ret) {
2223 			dev_info(&pf->pdev->dev,
2224 				 "set unicast promisc failed, err %s, aq_err %s\n",
2225 				 i40e_stat_str(hw, aq_ret),
2226 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2227 		}
2228 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2229 						  hw,
2230 						  vsi->seid,
2231 						  promisc, NULL);
2232 		if (aq_ret) {
2233 			dev_info(&pf->pdev->dev,
2234 				 "set multicast promisc failed, err %s, aq_err %s\n",
2235 				 i40e_stat_str(hw, aq_ret),
2236 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2237 		}
2238 	}
2239 
2240 	if (!aq_ret)
2241 		pf->cur_promisc = promisc;
2242 
2243 	return aq_ret;
2244 }
2245 
2246 /**
2247  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2248  * @vsi: ptr to the VSI
2249  *
2250  * Push any outstanding VSI filter changes through the AdminQ.
2251  *
2252  * Returns 0 or error value
2253  **/
2254 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2255 {
2256 	struct hlist_head tmp_add_list, tmp_del_list;
2257 	struct i40e_mac_filter *f;
2258 	struct i40e_new_mac_filter *new, *add_head = NULL;
2259 	struct i40e_hw *hw = &vsi->back->hw;
2260 	bool old_overflow, new_overflow;
2261 	unsigned int failed_filters = 0;
2262 	unsigned int vlan_filters = 0;
2263 	char vsi_name[16] = "PF";
2264 	int filter_list_len = 0;
2265 	i40e_status aq_ret = 0;
2266 	u32 changed_flags = 0;
2267 	struct hlist_node *h;
2268 	struct i40e_pf *pf;
2269 	int num_add = 0;
2270 	int num_del = 0;
2271 	int retval = 0;
2272 	u16 cmd_flags;
2273 	int list_size;
2274 	int bkt;
2275 
2276 	/* empty array typed pointers, kcalloc later */
2277 	struct i40e_aqc_add_macvlan_element_data *add_list;
2278 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2279 
2280 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2281 		usleep_range(1000, 2000);
2282 	pf = vsi->back;
2283 
2284 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2285 
2286 	if (vsi->netdev) {
2287 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2288 		vsi->current_netdev_flags = vsi->netdev->flags;
2289 	}
2290 
2291 	INIT_HLIST_HEAD(&tmp_add_list);
2292 	INIT_HLIST_HEAD(&tmp_del_list);
2293 
2294 	if (vsi->type == I40E_VSI_SRIOV)
2295 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2296 	else if (vsi->type != I40E_VSI_MAIN)
2297 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2298 
2299 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2300 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2301 
2302 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2303 		/* Create a list of filters to delete. */
2304 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2305 			if (f->state == I40E_FILTER_REMOVE) {
2306 				/* Move the element into temporary del_list */
2307 				hash_del(&f->hlist);
2308 				hlist_add_head(&f->hlist, &tmp_del_list);
2309 
2310 				/* Avoid counting removed filters */
2311 				continue;
2312 			}
2313 			if (f->state == I40E_FILTER_NEW) {
2314 				/* Create a temporary i40e_new_mac_filter */
2315 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2316 				if (!new)
2317 					goto err_no_memory_locked;
2318 
2319 				/* Store pointer to the real filter */
2320 				new->f = f;
2321 				new->state = f->state;
2322 
2323 				/* Add it to the hash list */
2324 				hlist_add_head(&new->hlist, &tmp_add_list);
2325 			}
2326 
2327 			/* Count the number of active (current and new) VLAN
2328 			 * filters we have now. Does not count filters which
2329 			 * are marked for deletion.
2330 			 */
2331 			if (f->vlan > 0)
2332 				vlan_filters++;
2333 		}
2334 
2335 		retval = i40e_correct_mac_vlan_filters(vsi,
2336 						       &tmp_add_list,
2337 						       &tmp_del_list,
2338 						       vlan_filters);
2339 		if (retval)
2340 			goto err_no_memory_locked;
2341 
2342 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2343 	}
2344 
2345 	/* Now process 'del_list' outside the lock */
2346 	if (!hlist_empty(&tmp_del_list)) {
2347 		filter_list_len = hw->aq.asq_buf_size /
2348 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2349 		list_size = filter_list_len *
2350 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2351 		del_list = kzalloc(list_size, GFP_ATOMIC);
2352 		if (!del_list)
2353 			goto err_no_memory;
2354 
2355 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2356 			cmd_flags = 0;
2357 
2358 			/* handle broadcast filters by updating the broadcast
2359 			 * promiscuous flag and release filter list.
2360 			 */
2361 			if (is_broadcast_ether_addr(f->macaddr)) {
2362 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2363 
2364 				hlist_del(&f->hlist);
2365 				kfree(f);
2366 				continue;
2367 			}
2368 
2369 			/* add to delete list */
2370 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2371 			if (f->vlan == I40E_VLAN_ANY) {
2372 				del_list[num_del].vlan_tag = 0;
2373 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2374 			} else {
2375 				del_list[num_del].vlan_tag =
2376 					cpu_to_le16((u16)(f->vlan));
2377 			}
2378 
2379 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2380 			del_list[num_del].flags = cmd_flags;
2381 			num_del++;
2382 
2383 			/* flush a full buffer */
2384 			if (num_del == filter_list_len) {
2385 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2386 						     num_del, &retval);
2387 				memset(del_list, 0, list_size);
2388 				num_del = 0;
2389 			}
2390 			/* Release memory for MAC filter entries which were
2391 			 * synced up with HW.
2392 			 */
2393 			hlist_del(&f->hlist);
2394 			kfree(f);
2395 		}
2396 
2397 		if (num_del) {
2398 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2399 					     num_del, &retval);
2400 		}
2401 
2402 		kfree(del_list);
2403 		del_list = NULL;
2404 	}
2405 
2406 	if (!hlist_empty(&tmp_add_list)) {
2407 		/* Do all the adds now. */
2408 		filter_list_len = hw->aq.asq_buf_size /
2409 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2410 		list_size = filter_list_len *
2411 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2412 		add_list = kzalloc(list_size, GFP_ATOMIC);
2413 		if (!add_list)
2414 			goto err_no_memory;
2415 
2416 		num_add = 0;
2417 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2418 			/* handle broadcast filters by updating the broadcast
2419 			 * promiscuous flag instead of adding a MAC filter.
2420 			 */
2421 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2422 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2423 							      new->f))
2424 					new->state = I40E_FILTER_FAILED;
2425 				else
2426 					new->state = I40E_FILTER_ACTIVE;
2427 				continue;
2428 			}
2429 
2430 			/* add to add array */
2431 			if (num_add == 0)
2432 				add_head = new;
2433 			cmd_flags = 0;
2434 			ether_addr_copy(add_list[num_add].mac_addr,
2435 					new->f->macaddr);
2436 			if (new->f->vlan == I40E_VLAN_ANY) {
2437 				add_list[num_add].vlan_tag = 0;
2438 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2439 			} else {
2440 				add_list[num_add].vlan_tag =
2441 					cpu_to_le16((u16)(new->f->vlan));
2442 			}
2443 			add_list[num_add].queue_number = 0;
2444 			/* set invalid match method for later detection */
2445 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2446 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2447 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2448 			num_add++;
2449 
2450 			/* flush a full buffer */
2451 			if (num_add == filter_list_len) {
2452 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2453 						     add_head, num_add);
2454 				memset(add_list, 0, list_size);
2455 				num_add = 0;
2456 			}
2457 		}
2458 		if (num_add) {
2459 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2460 					     num_add);
2461 		}
2462 		/* Now move all of the filters from the temp add list back to
2463 		 * the VSI's list.
2464 		 */
2465 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2466 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2467 			/* Only update the state if we're still NEW */
2468 			if (new->f->state == I40E_FILTER_NEW)
2469 				new->f->state = new->state;
2470 			hlist_del(&new->hlist);
2471 			kfree(new);
2472 		}
2473 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2474 		kfree(add_list);
2475 		add_list = NULL;
2476 	}
2477 
2478 	/* Determine the number of active and failed filters. */
2479 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2480 	vsi->active_filters = 0;
2481 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2482 		if (f->state == I40E_FILTER_ACTIVE)
2483 			vsi->active_filters++;
2484 		else if (f->state == I40E_FILTER_FAILED)
2485 			failed_filters++;
2486 	}
2487 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2488 
2489 	/* Check if we are able to exit overflow promiscuous mode. We can
2490 	 * safely exit if we didn't just enter, we no longer have any failed
2491 	 * filters, and we have reduced filters below the threshold value.
2492 	 */
2493 	if (old_overflow && !failed_filters &&
2494 	    vsi->active_filters < vsi->promisc_threshold) {
2495 		dev_info(&pf->pdev->dev,
2496 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2497 			 vsi_name);
2498 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2499 		vsi->promisc_threshold = 0;
2500 	}
2501 
2502 	/* if the VF is not trusted do not do promisc */
2503 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2504 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2505 		goto out;
2506 	}
2507 
2508 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2509 
2510 	/* If we are entering overflow promiscuous, we need to calculate a new
2511 	 * threshold for when we are safe to exit
2512 	 */
2513 	if (!old_overflow && new_overflow)
2514 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2515 
2516 	/* check for changes in promiscuous modes */
2517 	if (changed_flags & IFF_ALLMULTI) {
2518 		bool cur_multipromisc;
2519 
2520 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2521 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2522 							       vsi->seid,
2523 							       cur_multipromisc,
2524 							       NULL);
2525 		if (aq_ret) {
2526 			retval = i40e_aq_rc_to_posix(aq_ret,
2527 						     hw->aq.asq_last_status);
2528 			dev_info(&pf->pdev->dev,
2529 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2530 				 vsi_name,
2531 				 i40e_stat_str(hw, aq_ret),
2532 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2533 		}
2534 	}
2535 
2536 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2537 		bool cur_promisc;
2538 
2539 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2540 			       new_overflow);
2541 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2542 		if (aq_ret) {
2543 			retval = i40e_aq_rc_to_posix(aq_ret,
2544 						     hw->aq.asq_last_status);
2545 			dev_info(&pf->pdev->dev,
2546 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2547 				 cur_promisc ? "on" : "off",
2548 				 vsi_name,
2549 				 i40e_stat_str(hw, aq_ret),
2550 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2551 		}
2552 	}
2553 out:
2554 	/* if something went wrong then set the changed flag so we try again */
2555 	if (retval)
2556 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2557 
2558 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2559 	return retval;
2560 
2561 err_no_memory:
2562 	/* Restore elements on the temporary add and delete lists */
2563 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2564 err_no_memory_locked:
2565 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2566 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2567 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2568 
2569 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2570 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2571 	return -ENOMEM;
2572 }
2573 
2574 /**
2575  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2576  * @pf: board private structure
2577  **/
2578 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2579 {
2580 	int v;
2581 
2582 	if (!pf)
2583 		return;
2584 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2585 		return;
2586 
2587 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2588 		if (pf->vsi[v] &&
2589 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2590 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2591 
2592 			if (ret) {
2593 				/* come back and try again later */
2594 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2595 					pf->state);
2596 				break;
2597 			}
2598 		}
2599 	}
2600 }
2601 
2602 /**
2603  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2604  * @vsi: the vsi
2605  **/
2606 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2607 {
2608 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2609 		return I40E_RXBUFFER_2048;
2610 	else
2611 		return I40E_RXBUFFER_3072;
2612 }
2613 
2614 /**
2615  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2616  * @netdev: network interface device structure
2617  * @new_mtu: new value for maximum frame size
2618  *
2619  * Returns 0 on success, negative on failure
2620  **/
2621 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2622 {
2623 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2624 	struct i40e_vsi *vsi = np->vsi;
2625 	struct i40e_pf *pf = vsi->back;
2626 
2627 	if (i40e_enabled_xdp_vsi(vsi)) {
2628 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2629 
2630 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2631 			return -EINVAL;
2632 	}
2633 
2634 	netdev_info(netdev, "changing MTU from %d to %d\n",
2635 		    netdev->mtu, new_mtu);
2636 	netdev->mtu = new_mtu;
2637 	if (netif_running(netdev))
2638 		i40e_vsi_reinit_locked(vsi);
2639 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2640 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2641 	return 0;
2642 }
2643 
2644 /**
2645  * i40e_ioctl - Access the hwtstamp interface
2646  * @netdev: network interface device structure
2647  * @ifr: interface request data
2648  * @cmd: ioctl command
2649  **/
2650 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2651 {
2652 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2653 	struct i40e_pf *pf = np->vsi->back;
2654 
2655 	switch (cmd) {
2656 	case SIOCGHWTSTAMP:
2657 		return i40e_ptp_get_ts_config(pf, ifr);
2658 	case SIOCSHWTSTAMP:
2659 		return i40e_ptp_set_ts_config(pf, ifr);
2660 	default:
2661 		return -EOPNOTSUPP;
2662 	}
2663 }
2664 
2665 /**
2666  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2667  * @vsi: the vsi being adjusted
2668  **/
2669 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2670 {
2671 	struct i40e_vsi_context ctxt;
2672 	i40e_status ret;
2673 
2674 	/* Don't modify stripping options if a port VLAN is active */
2675 	if (vsi->info.pvid)
2676 		return;
2677 
2678 	if ((vsi->info.valid_sections &
2679 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2680 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2681 		return;  /* already enabled */
2682 
2683 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2684 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2685 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2686 
2687 	ctxt.seid = vsi->seid;
2688 	ctxt.info = vsi->info;
2689 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2690 	if (ret) {
2691 		dev_info(&vsi->back->pdev->dev,
2692 			 "update vlan stripping failed, err %s aq_err %s\n",
2693 			 i40e_stat_str(&vsi->back->hw, ret),
2694 			 i40e_aq_str(&vsi->back->hw,
2695 				     vsi->back->hw.aq.asq_last_status));
2696 	}
2697 }
2698 
2699 /**
2700  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2701  * @vsi: the vsi being adjusted
2702  **/
2703 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2704 {
2705 	struct i40e_vsi_context ctxt;
2706 	i40e_status ret;
2707 
2708 	/* Don't modify stripping options if a port VLAN is active */
2709 	if (vsi->info.pvid)
2710 		return;
2711 
2712 	if ((vsi->info.valid_sections &
2713 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2714 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2715 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2716 		return;  /* already disabled */
2717 
2718 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2719 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2720 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2721 
2722 	ctxt.seid = vsi->seid;
2723 	ctxt.info = vsi->info;
2724 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2725 	if (ret) {
2726 		dev_info(&vsi->back->pdev->dev,
2727 			 "update vlan stripping failed, err %s aq_err %s\n",
2728 			 i40e_stat_str(&vsi->back->hw, ret),
2729 			 i40e_aq_str(&vsi->back->hw,
2730 				     vsi->back->hw.aq.asq_last_status));
2731 	}
2732 }
2733 
2734 /**
2735  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2736  * @vsi: the vsi being configured
2737  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2738  *
2739  * This is a helper function for adding a new MAC/VLAN filter with the
2740  * specified VLAN for each existing MAC address already in the hash table.
2741  * This function does *not* perform any accounting to update filters based on
2742  * VLAN mode.
2743  *
2744  * NOTE: this function expects to be called while under the
2745  * mac_filter_hash_lock
2746  **/
2747 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2748 {
2749 	struct i40e_mac_filter *f, *add_f;
2750 	struct hlist_node *h;
2751 	int bkt;
2752 
2753 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2754 		if (f->state == I40E_FILTER_REMOVE)
2755 			continue;
2756 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2757 		if (!add_f) {
2758 			dev_info(&vsi->back->pdev->dev,
2759 				 "Could not add vlan filter %d for %pM\n",
2760 				 vid, f->macaddr);
2761 			return -ENOMEM;
2762 		}
2763 	}
2764 
2765 	return 0;
2766 }
2767 
2768 /**
2769  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2770  * @vsi: the VSI being configured
2771  * @vid: VLAN id to be added
2772  **/
2773 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2774 {
2775 	int err;
2776 
2777 	if (vsi->info.pvid)
2778 		return -EINVAL;
2779 
2780 	/* The network stack will attempt to add VID=0, with the intention to
2781 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2782 	 * these packets by default when configured to receive untagged
2783 	 * packets, so we don't need to add a filter for this case.
2784 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2785 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2786 	 * Thus, we do not want to actually add a filter for VID=0
2787 	 */
2788 	if (!vid)
2789 		return 0;
2790 
2791 	/* Locked once because all functions invoked below iterates list*/
2792 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2793 	err = i40e_add_vlan_all_mac(vsi, vid);
2794 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2795 	if (err)
2796 		return err;
2797 
2798 	/* schedule our worker thread which will take care of
2799 	 * applying the new filter changes
2800 	 */
2801 	i40e_service_event_schedule(vsi->back);
2802 	return 0;
2803 }
2804 
2805 /**
2806  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2807  * @vsi: the vsi being configured
2808  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2809  *
2810  * This function should be used to remove all VLAN filters which match the
2811  * given VID. It does not schedule the service event and does not take the
2812  * mac_filter_hash_lock so it may be combined with other operations under
2813  * a single invocation of the mac_filter_hash_lock.
2814  *
2815  * NOTE: this function expects to be called while under the
2816  * mac_filter_hash_lock
2817  */
2818 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2819 {
2820 	struct i40e_mac_filter *f;
2821 	struct hlist_node *h;
2822 	int bkt;
2823 
2824 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2825 		if (f->vlan == vid)
2826 			__i40e_del_filter(vsi, f);
2827 	}
2828 }
2829 
2830 /**
2831  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2832  * @vsi: the VSI being configured
2833  * @vid: VLAN id to be removed
2834  **/
2835 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2836 {
2837 	if (!vid || vsi->info.pvid)
2838 		return;
2839 
2840 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2841 	i40e_rm_vlan_all_mac(vsi, vid);
2842 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2843 
2844 	/* schedule our worker thread which will take care of
2845 	 * applying the new filter changes
2846 	 */
2847 	i40e_service_event_schedule(vsi->back);
2848 }
2849 
2850 /**
2851  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2852  * @netdev: network interface to be adjusted
2853  * @proto: unused protocol value
2854  * @vid: vlan id to be added
2855  *
2856  * net_device_ops implementation for adding vlan ids
2857  **/
2858 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2859 				__always_unused __be16 proto, u16 vid)
2860 {
2861 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2862 	struct i40e_vsi *vsi = np->vsi;
2863 	int ret = 0;
2864 
2865 	if (vid >= VLAN_N_VID)
2866 		return -EINVAL;
2867 
2868 	ret = i40e_vsi_add_vlan(vsi, vid);
2869 	if (!ret)
2870 		set_bit(vid, vsi->active_vlans);
2871 
2872 	return ret;
2873 }
2874 
2875 /**
2876  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2877  * @netdev: network interface to be adjusted
2878  * @proto: unused protocol value
2879  * @vid: vlan id to be added
2880  **/
2881 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2882 				    __always_unused __be16 proto, u16 vid)
2883 {
2884 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2885 	struct i40e_vsi *vsi = np->vsi;
2886 
2887 	if (vid >= VLAN_N_VID)
2888 		return;
2889 	set_bit(vid, vsi->active_vlans);
2890 }
2891 
2892 /**
2893  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2894  * @netdev: network interface to be adjusted
2895  * @proto: unused protocol value
2896  * @vid: vlan id to be removed
2897  *
2898  * net_device_ops implementation for removing vlan ids
2899  **/
2900 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2901 				 __always_unused __be16 proto, u16 vid)
2902 {
2903 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2904 	struct i40e_vsi *vsi = np->vsi;
2905 
2906 	/* return code is ignored as there is nothing a user
2907 	 * can do about failure to remove and a log message was
2908 	 * already printed from the other function
2909 	 */
2910 	i40e_vsi_kill_vlan(vsi, vid);
2911 
2912 	clear_bit(vid, vsi->active_vlans);
2913 
2914 	return 0;
2915 }
2916 
2917 /**
2918  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2919  * @vsi: the vsi being brought back up
2920  **/
2921 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2922 {
2923 	u16 vid;
2924 
2925 	if (!vsi->netdev)
2926 		return;
2927 
2928 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2929 		i40e_vlan_stripping_enable(vsi);
2930 	else
2931 		i40e_vlan_stripping_disable(vsi);
2932 
2933 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2934 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2935 					vid);
2936 }
2937 
2938 /**
2939  * i40e_vsi_add_pvid - Add pvid for the VSI
2940  * @vsi: the vsi being adjusted
2941  * @vid: the vlan id to set as a PVID
2942  **/
2943 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2944 {
2945 	struct i40e_vsi_context ctxt;
2946 	i40e_status ret;
2947 
2948 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2949 	vsi->info.pvid = cpu_to_le16(vid);
2950 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2951 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2952 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2953 
2954 	ctxt.seid = vsi->seid;
2955 	ctxt.info = vsi->info;
2956 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2957 	if (ret) {
2958 		dev_info(&vsi->back->pdev->dev,
2959 			 "add pvid failed, err %s aq_err %s\n",
2960 			 i40e_stat_str(&vsi->back->hw, ret),
2961 			 i40e_aq_str(&vsi->back->hw,
2962 				     vsi->back->hw.aq.asq_last_status));
2963 		return -ENOENT;
2964 	}
2965 
2966 	return 0;
2967 }
2968 
2969 /**
2970  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2971  * @vsi: the vsi being adjusted
2972  *
2973  * Just use the vlan_rx_register() service to put it back to normal
2974  **/
2975 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2976 {
2977 	vsi->info.pvid = 0;
2978 
2979 	i40e_vlan_stripping_disable(vsi);
2980 }
2981 
2982 /**
2983  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2984  * @vsi: ptr to the VSI
2985  *
2986  * If this function returns with an error, then it's possible one or
2987  * more of the rings is populated (while the rest are not).  It is the
2988  * callers duty to clean those orphaned rings.
2989  *
2990  * Return 0 on success, negative on failure
2991  **/
2992 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2993 {
2994 	int i, err = 0;
2995 
2996 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2997 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2998 
2999 	if (!i40e_enabled_xdp_vsi(vsi))
3000 		return err;
3001 
3002 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3003 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3004 
3005 	return err;
3006 }
3007 
3008 /**
3009  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3010  * @vsi: ptr to the VSI
3011  *
3012  * Free VSI's transmit software resources
3013  **/
3014 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3015 {
3016 	int i;
3017 
3018 	if (vsi->tx_rings) {
3019 		for (i = 0; i < vsi->num_queue_pairs; i++)
3020 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3021 				i40e_free_tx_resources(vsi->tx_rings[i]);
3022 	}
3023 
3024 	if (vsi->xdp_rings) {
3025 		for (i = 0; i < vsi->num_queue_pairs; i++)
3026 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3027 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3028 	}
3029 }
3030 
3031 /**
3032  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3033  * @vsi: ptr to the VSI
3034  *
3035  * If this function returns with an error, then it's possible one or
3036  * more of the rings is populated (while the rest are not).  It is the
3037  * callers duty to clean those orphaned rings.
3038  *
3039  * Return 0 on success, negative on failure
3040  **/
3041 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3042 {
3043 	int i, err = 0;
3044 
3045 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3046 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3047 	return err;
3048 }
3049 
3050 /**
3051  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3052  * @vsi: ptr to the VSI
3053  *
3054  * Free all receive software resources
3055  **/
3056 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3057 {
3058 	int i;
3059 
3060 	if (!vsi->rx_rings)
3061 		return;
3062 
3063 	for (i = 0; i < vsi->num_queue_pairs; i++)
3064 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3065 			i40e_free_rx_resources(vsi->rx_rings[i]);
3066 }
3067 
3068 /**
3069  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3070  * @ring: The Tx ring to configure
3071  *
3072  * This enables/disables XPS for a given Tx descriptor ring
3073  * based on the TCs enabled for the VSI that ring belongs to.
3074  **/
3075 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3076 {
3077 	int cpu;
3078 
3079 	if (!ring->q_vector || !ring->netdev || ring->ch)
3080 		return;
3081 
3082 	/* We only initialize XPS once, so as not to overwrite user settings */
3083 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3084 		return;
3085 
3086 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3087 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3088 			    ring->queue_index);
3089 }
3090 
3091 /**
3092  * i40e_xsk_umem - Retrieve the AF_XDP ZC if XDP and ZC is enabled
3093  * @ring: The Tx or Rx ring
3094  *
3095  * Returns the UMEM or NULL.
3096  **/
3097 static struct xdp_umem *i40e_xsk_umem(struct i40e_ring *ring)
3098 {
3099 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3100 	int qid = ring->queue_index;
3101 
3102 	if (ring_is_xdp(ring))
3103 		qid -= ring->vsi->alloc_queue_pairs;
3104 
3105 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3106 		return NULL;
3107 
3108 	return xdp_get_umem_from_qid(ring->vsi->netdev, qid);
3109 }
3110 
3111 /**
3112  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3113  * @ring: The Tx ring to configure
3114  *
3115  * Configure the Tx descriptor ring in the HMC context.
3116  **/
3117 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3118 {
3119 	struct i40e_vsi *vsi = ring->vsi;
3120 	u16 pf_q = vsi->base_queue + ring->queue_index;
3121 	struct i40e_hw *hw = &vsi->back->hw;
3122 	struct i40e_hmc_obj_txq tx_ctx;
3123 	i40e_status err = 0;
3124 	u32 qtx_ctl = 0;
3125 
3126 	if (ring_is_xdp(ring))
3127 		ring->xsk_umem = i40e_xsk_umem(ring);
3128 
3129 	/* some ATR related tx ring init */
3130 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3131 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3132 		ring->atr_count = 0;
3133 	} else {
3134 		ring->atr_sample_rate = 0;
3135 	}
3136 
3137 	/* configure XPS */
3138 	i40e_config_xps_tx_ring(ring);
3139 
3140 	/* clear the context structure first */
3141 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3142 
3143 	tx_ctx.new_context = 1;
3144 	tx_ctx.base = (ring->dma / 128);
3145 	tx_ctx.qlen = ring->count;
3146 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3147 					       I40E_FLAG_FD_ATR_ENABLED));
3148 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3149 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3150 	if (vsi->type != I40E_VSI_FDIR)
3151 		tx_ctx.head_wb_ena = 1;
3152 	tx_ctx.head_wb_addr = ring->dma +
3153 			      (ring->count * sizeof(struct i40e_tx_desc));
3154 
3155 	/* As part of VSI creation/update, FW allocates certain
3156 	 * Tx arbitration queue sets for each TC enabled for
3157 	 * the VSI. The FW returns the handles to these queue
3158 	 * sets as part of the response buffer to Add VSI,
3159 	 * Update VSI, etc. AQ commands. It is expected that
3160 	 * these queue set handles be associated with the Tx
3161 	 * queues by the driver as part of the TX queue context
3162 	 * initialization. This has to be done regardless of
3163 	 * DCB as by default everything is mapped to TC0.
3164 	 */
3165 
3166 	if (ring->ch)
3167 		tx_ctx.rdylist =
3168 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3169 
3170 	else
3171 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3172 
3173 	tx_ctx.rdylist_act = 0;
3174 
3175 	/* clear the context in the HMC */
3176 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3177 	if (err) {
3178 		dev_info(&vsi->back->pdev->dev,
3179 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3180 			 ring->queue_index, pf_q, err);
3181 		return -ENOMEM;
3182 	}
3183 
3184 	/* set the context in the HMC */
3185 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3186 	if (err) {
3187 		dev_info(&vsi->back->pdev->dev,
3188 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3189 			 ring->queue_index, pf_q, err);
3190 		return -ENOMEM;
3191 	}
3192 
3193 	/* Now associate this queue with this PCI function */
3194 	if (ring->ch) {
3195 		if (ring->ch->type == I40E_VSI_VMDQ2)
3196 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3197 		else
3198 			return -EINVAL;
3199 
3200 		qtx_ctl |= (ring->ch->vsi_number <<
3201 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3202 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3203 	} else {
3204 		if (vsi->type == I40E_VSI_VMDQ2) {
3205 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3206 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3207 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3208 		} else {
3209 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3210 		}
3211 	}
3212 
3213 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3214 		    I40E_QTX_CTL_PF_INDX_MASK);
3215 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3216 	i40e_flush(hw);
3217 
3218 	/* cache tail off for easier writes later */
3219 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3220 
3221 	return 0;
3222 }
3223 
3224 /**
3225  * i40e_configure_rx_ring - Configure a receive ring context
3226  * @ring: The Rx ring to configure
3227  *
3228  * Configure the Rx descriptor ring in the HMC context.
3229  **/
3230 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3231 {
3232 	struct i40e_vsi *vsi = ring->vsi;
3233 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3234 	u16 pf_q = vsi->base_queue + ring->queue_index;
3235 	struct i40e_hw *hw = &vsi->back->hw;
3236 	struct i40e_hmc_obj_rxq rx_ctx;
3237 	i40e_status err = 0;
3238 	bool ok;
3239 	int ret;
3240 
3241 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3242 
3243 	/* clear the context structure first */
3244 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3245 
3246 	if (ring->vsi->type == I40E_VSI_MAIN)
3247 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3248 
3249 	ring->xsk_umem = i40e_xsk_umem(ring);
3250 	if (ring->xsk_umem) {
3251 		ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
3252 				   XDP_PACKET_HEADROOM;
3253 		/* For AF_XDP ZC, we disallow packets to span on
3254 		 * multiple buffers, thus letting us skip that
3255 		 * handling in the fast-path.
3256 		 */
3257 		chain_len = 1;
3258 		ring->zca.free = i40e_zca_free;
3259 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3260 						 MEM_TYPE_ZERO_COPY,
3261 						 &ring->zca);
3262 		if (ret)
3263 			return ret;
3264 		dev_info(&vsi->back->pdev->dev,
3265 			 "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
3266 			 ring->queue_index);
3267 
3268 	} else {
3269 		ring->rx_buf_len = vsi->rx_buf_len;
3270 		if (ring->vsi->type == I40E_VSI_MAIN) {
3271 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3272 							 MEM_TYPE_PAGE_SHARED,
3273 							 NULL);
3274 			if (ret)
3275 				return ret;
3276 		}
3277 	}
3278 
3279 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3280 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3281 
3282 	rx_ctx.base = (ring->dma / 128);
3283 	rx_ctx.qlen = ring->count;
3284 
3285 	/* use 32 byte descriptors */
3286 	rx_ctx.dsize = 1;
3287 
3288 	/* descriptor type is always zero
3289 	 * rx_ctx.dtype = 0;
3290 	 */
3291 	rx_ctx.hsplit_0 = 0;
3292 
3293 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3294 	if (hw->revision_id == 0)
3295 		rx_ctx.lrxqthresh = 0;
3296 	else
3297 		rx_ctx.lrxqthresh = 1;
3298 	rx_ctx.crcstrip = 1;
3299 	rx_ctx.l2tsel = 1;
3300 	/* this controls whether VLAN is stripped from inner headers */
3301 	rx_ctx.showiv = 0;
3302 	/* set the prefena field to 1 because the manual says to */
3303 	rx_ctx.prefena = 1;
3304 
3305 	/* clear the context in the HMC */
3306 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3307 	if (err) {
3308 		dev_info(&vsi->back->pdev->dev,
3309 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3310 			 ring->queue_index, pf_q, err);
3311 		return -ENOMEM;
3312 	}
3313 
3314 	/* set the context in the HMC */
3315 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3316 	if (err) {
3317 		dev_info(&vsi->back->pdev->dev,
3318 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3319 			 ring->queue_index, pf_q, err);
3320 		return -ENOMEM;
3321 	}
3322 
3323 	/* configure Rx buffer alignment */
3324 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3325 		clear_ring_build_skb_enabled(ring);
3326 	else
3327 		set_ring_build_skb_enabled(ring);
3328 
3329 	/* cache tail for quicker writes, and clear the reg before use */
3330 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3331 	writel(0, ring->tail);
3332 
3333 	ok = ring->xsk_umem ?
3334 	     i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
3335 	     !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3336 	if (!ok) {
3337 		/* Log this in case the user has forgotten to give the kernel
3338 		 * any buffers, even later in the application.
3339 		 */
3340 		dev_info(&vsi->back->pdev->dev,
3341 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3342 			 ring->xsk_umem ? "UMEM enabled " : "",
3343 			 ring->queue_index, pf_q);
3344 	}
3345 
3346 	return 0;
3347 }
3348 
3349 /**
3350  * i40e_vsi_configure_tx - Configure the VSI for Tx
3351  * @vsi: VSI structure describing this set of rings and resources
3352  *
3353  * Configure the Tx VSI for operation.
3354  **/
3355 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3356 {
3357 	int err = 0;
3358 	u16 i;
3359 
3360 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3361 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3362 
3363 	if (!i40e_enabled_xdp_vsi(vsi))
3364 		return err;
3365 
3366 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3367 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3368 
3369 	return err;
3370 }
3371 
3372 /**
3373  * i40e_vsi_configure_rx - Configure the VSI for Rx
3374  * @vsi: the VSI being configured
3375  *
3376  * Configure the Rx VSI for operation.
3377  **/
3378 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3379 {
3380 	int err = 0;
3381 	u16 i;
3382 
3383 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3384 		vsi->max_frame = I40E_MAX_RXBUFFER;
3385 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3386 #if (PAGE_SIZE < 8192)
3387 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3388 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3389 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3390 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3391 #endif
3392 	} else {
3393 		vsi->max_frame = I40E_MAX_RXBUFFER;
3394 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3395 						       I40E_RXBUFFER_2048;
3396 	}
3397 
3398 	/* set up individual rings */
3399 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3400 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3401 
3402 	return err;
3403 }
3404 
3405 /**
3406  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3407  * @vsi: ptr to the VSI
3408  **/
3409 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3410 {
3411 	struct i40e_ring *tx_ring, *rx_ring;
3412 	u16 qoffset, qcount;
3413 	int i, n;
3414 
3415 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3416 		/* Reset the TC information */
3417 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3418 			rx_ring = vsi->rx_rings[i];
3419 			tx_ring = vsi->tx_rings[i];
3420 			rx_ring->dcb_tc = 0;
3421 			tx_ring->dcb_tc = 0;
3422 		}
3423 		return;
3424 	}
3425 
3426 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3427 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3428 			continue;
3429 
3430 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3431 		qcount = vsi->tc_config.tc_info[n].qcount;
3432 		for (i = qoffset; i < (qoffset + qcount); i++) {
3433 			rx_ring = vsi->rx_rings[i];
3434 			tx_ring = vsi->tx_rings[i];
3435 			rx_ring->dcb_tc = n;
3436 			tx_ring->dcb_tc = n;
3437 		}
3438 	}
3439 }
3440 
3441 /**
3442  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3443  * @vsi: ptr to the VSI
3444  **/
3445 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3446 {
3447 	if (vsi->netdev)
3448 		i40e_set_rx_mode(vsi->netdev);
3449 }
3450 
3451 /**
3452  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3453  * @vsi: Pointer to the targeted VSI
3454  *
3455  * This function replays the hlist on the hw where all the SB Flow Director
3456  * filters were saved.
3457  **/
3458 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3459 {
3460 	struct i40e_fdir_filter *filter;
3461 	struct i40e_pf *pf = vsi->back;
3462 	struct hlist_node *node;
3463 
3464 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3465 		return;
3466 
3467 	/* Reset FDir counters as we're replaying all existing filters */
3468 	pf->fd_tcp4_filter_cnt = 0;
3469 	pf->fd_udp4_filter_cnt = 0;
3470 	pf->fd_sctp4_filter_cnt = 0;
3471 	pf->fd_ip4_filter_cnt = 0;
3472 
3473 	hlist_for_each_entry_safe(filter, node,
3474 				  &pf->fdir_filter_list, fdir_node) {
3475 		i40e_add_del_fdir(vsi, filter, true);
3476 	}
3477 }
3478 
3479 /**
3480  * i40e_vsi_configure - Set up the VSI for action
3481  * @vsi: the VSI being configured
3482  **/
3483 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3484 {
3485 	int err;
3486 
3487 	i40e_set_vsi_rx_mode(vsi);
3488 	i40e_restore_vlan(vsi);
3489 	i40e_vsi_config_dcb_rings(vsi);
3490 	err = i40e_vsi_configure_tx(vsi);
3491 	if (!err)
3492 		err = i40e_vsi_configure_rx(vsi);
3493 
3494 	return err;
3495 }
3496 
3497 /**
3498  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3499  * @vsi: the VSI being configured
3500  **/
3501 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3502 {
3503 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3504 	struct i40e_pf *pf = vsi->back;
3505 	struct i40e_hw *hw = &pf->hw;
3506 	u16 vector;
3507 	int i, q;
3508 	u32 qp;
3509 
3510 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3511 	 * and PFINT_LNKLSTn registers, e.g.:
3512 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3513 	 */
3514 	qp = vsi->base_queue;
3515 	vector = vsi->base_vector;
3516 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3517 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3518 
3519 		q_vector->rx.next_update = jiffies + 1;
3520 		q_vector->rx.target_itr =
3521 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3522 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3523 		     q_vector->rx.target_itr);
3524 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3525 
3526 		q_vector->tx.next_update = jiffies + 1;
3527 		q_vector->tx.target_itr =
3528 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3529 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3530 		     q_vector->tx.target_itr);
3531 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3532 
3533 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3534 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3535 
3536 		/* Linked list for the queuepairs assigned to this vector */
3537 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3538 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3539 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3540 			u32 val;
3541 
3542 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3543 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3544 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3545 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3546 			      (I40E_QUEUE_TYPE_TX <<
3547 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3548 
3549 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3550 
3551 			if (has_xdp) {
3552 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3553 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3554 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3555 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3556 				      (I40E_QUEUE_TYPE_TX <<
3557 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3558 
3559 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3560 			}
3561 
3562 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3563 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3564 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3565 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3566 			      (I40E_QUEUE_TYPE_RX <<
3567 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3568 
3569 			/* Terminate the linked list */
3570 			if (q == (q_vector->num_ringpairs - 1))
3571 				val |= (I40E_QUEUE_END_OF_LIST <<
3572 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3573 
3574 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3575 			qp++;
3576 		}
3577 	}
3578 
3579 	i40e_flush(hw);
3580 }
3581 
3582 /**
3583  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3584  * @pf: pointer to private device data structure
3585  **/
3586 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3587 {
3588 	struct i40e_hw *hw = &pf->hw;
3589 	u32 val;
3590 
3591 	/* clear things first */
3592 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3593 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3594 
3595 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3596 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3597 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3598 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3599 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3600 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3601 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3602 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3603 
3604 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3605 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3606 
3607 	if (pf->flags & I40E_FLAG_PTP)
3608 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3609 
3610 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3611 
3612 	/* SW_ITR_IDX = 0, but don't change INTENA */
3613 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3614 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3615 
3616 	/* OTHER_ITR_IDX = 0 */
3617 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3618 }
3619 
3620 /**
3621  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3622  * @vsi: the VSI being configured
3623  **/
3624 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3625 {
3626 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3627 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3628 	struct i40e_pf *pf = vsi->back;
3629 	struct i40e_hw *hw = &pf->hw;
3630 	u32 val;
3631 
3632 	/* set the ITR configuration */
3633 	q_vector->rx.next_update = jiffies + 1;
3634 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3635 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3636 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3637 	q_vector->tx.next_update = jiffies + 1;
3638 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3639 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3640 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3641 
3642 	i40e_enable_misc_int_causes(pf);
3643 
3644 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3645 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3646 
3647 	/* Associate the queue pair to the vector and enable the queue int */
3648 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3649 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3650 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3651 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3652 
3653 	wr32(hw, I40E_QINT_RQCTL(0), val);
3654 
3655 	if (i40e_enabled_xdp_vsi(vsi)) {
3656 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3657 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3658 		      (I40E_QUEUE_TYPE_TX
3659 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3660 
3661 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3662 	}
3663 
3664 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3665 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3666 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3667 
3668 	wr32(hw, I40E_QINT_TQCTL(0), val);
3669 	i40e_flush(hw);
3670 }
3671 
3672 /**
3673  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3674  * @pf: board private structure
3675  **/
3676 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3677 {
3678 	struct i40e_hw *hw = &pf->hw;
3679 
3680 	wr32(hw, I40E_PFINT_DYN_CTL0,
3681 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3682 	i40e_flush(hw);
3683 }
3684 
3685 /**
3686  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3687  * @pf: board private structure
3688  **/
3689 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3690 {
3691 	struct i40e_hw *hw = &pf->hw;
3692 	u32 val;
3693 
3694 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3695 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3696 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3697 
3698 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3699 	i40e_flush(hw);
3700 }
3701 
3702 /**
3703  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3704  * @irq: interrupt number
3705  * @data: pointer to a q_vector
3706  **/
3707 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3708 {
3709 	struct i40e_q_vector *q_vector = data;
3710 
3711 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3712 		return IRQ_HANDLED;
3713 
3714 	napi_schedule_irqoff(&q_vector->napi);
3715 
3716 	return IRQ_HANDLED;
3717 }
3718 
3719 /**
3720  * i40e_irq_affinity_notify - Callback for affinity changes
3721  * @notify: context as to what irq was changed
3722  * @mask: the new affinity mask
3723  *
3724  * This is a callback function used by the irq_set_affinity_notifier function
3725  * so that we may register to receive changes to the irq affinity masks.
3726  **/
3727 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3728 				     const cpumask_t *mask)
3729 {
3730 	struct i40e_q_vector *q_vector =
3731 		container_of(notify, struct i40e_q_vector, affinity_notify);
3732 
3733 	cpumask_copy(&q_vector->affinity_mask, mask);
3734 }
3735 
3736 /**
3737  * i40e_irq_affinity_release - Callback for affinity notifier release
3738  * @ref: internal core kernel usage
3739  *
3740  * This is a callback function used by the irq_set_affinity_notifier function
3741  * to inform the current notification subscriber that they will no longer
3742  * receive notifications.
3743  **/
3744 static void i40e_irq_affinity_release(struct kref *ref) {}
3745 
3746 /**
3747  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3748  * @vsi: the VSI being configured
3749  * @basename: name for the vector
3750  *
3751  * Allocates MSI-X vectors and requests interrupts from the kernel.
3752  **/
3753 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3754 {
3755 	int q_vectors = vsi->num_q_vectors;
3756 	struct i40e_pf *pf = vsi->back;
3757 	int base = vsi->base_vector;
3758 	int rx_int_idx = 0;
3759 	int tx_int_idx = 0;
3760 	int vector, err;
3761 	int irq_num;
3762 	int cpu;
3763 
3764 	for (vector = 0; vector < q_vectors; vector++) {
3765 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3766 
3767 		irq_num = pf->msix_entries[base + vector].vector;
3768 
3769 		if (q_vector->tx.ring && q_vector->rx.ring) {
3770 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3771 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3772 			tx_int_idx++;
3773 		} else if (q_vector->rx.ring) {
3774 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3775 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3776 		} else if (q_vector->tx.ring) {
3777 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3778 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3779 		} else {
3780 			/* skip this unused q_vector */
3781 			continue;
3782 		}
3783 		err = request_irq(irq_num,
3784 				  vsi->irq_handler,
3785 				  0,
3786 				  q_vector->name,
3787 				  q_vector);
3788 		if (err) {
3789 			dev_info(&pf->pdev->dev,
3790 				 "MSIX request_irq failed, error: %d\n", err);
3791 			goto free_queue_irqs;
3792 		}
3793 
3794 		/* register for affinity change notifications */
3795 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3796 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3797 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3798 		/* Spread affinity hints out across online CPUs.
3799 		 *
3800 		 * get_cpu_mask returns a static constant mask with
3801 		 * a permanent lifetime so it's ok to pass to
3802 		 * irq_set_affinity_hint without making a copy.
3803 		 */
3804 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3805 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3806 	}
3807 
3808 	vsi->irqs_ready = true;
3809 	return 0;
3810 
3811 free_queue_irqs:
3812 	while (vector) {
3813 		vector--;
3814 		irq_num = pf->msix_entries[base + vector].vector;
3815 		irq_set_affinity_notifier(irq_num, NULL);
3816 		irq_set_affinity_hint(irq_num, NULL);
3817 		free_irq(irq_num, &vsi->q_vectors[vector]);
3818 	}
3819 	return err;
3820 }
3821 
3822 /**
3823  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3824  * @vsi: the VSI being un-configured
3825  **/
3826 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3827 {
3828 	struct i40e_pf *pf = vsi->back;
3829 	struct i40e_hw *hw = &pf->hw;
3830 	int base = vsi->base_vector;
3831 	int i;
3832 
3833 	/* disable interrupt causation from each queue */
3834 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3835 		u32 val;
3836 
3837 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3838 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3839 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3840 
3841 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3842 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3843 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3844 
3845 		if (!i40e_enabled_xdp_vsi(vsi))
3846 			continue;
3847 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3848 	}
3849 
3850 	/* disable each interrupt */
3851 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3852 		for (i = vsi->base_vector;
3853 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3854 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3855 
3856 		i40e_flush(hw);
3857 		for (i = 0; i < vsi->num_q_vectors; i++)
3858 			synchronize_irq(pf->msix_entries[i + base].vector);
3859 	} else {
3860 		/* Legacy and MSI mode - this stops all interrupt handling */
3861 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3862 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3863 		i40e_flush(hw);
3864 		synchronize_irq(pf->pdev->irq);
3865 	}
3866 }
3867 
3868 /**
3869  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3870  * @vsi: the VSI being configured
3871  **/
3872 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3873 {
3874 	struct i40e_pf *pf = vsi->back;
3875 	int i;
3876 
3877 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3878 		for (i = 0; i < vsi->num_q_vectors; i++)
3879 			i40e_irq_dynamic_enable(vsi, i);
3880 	} else {
3881 		i40e_irq_dynamic_enable_icr0(pf);
3882 	}
3883 
3884 	i40e_flush(&pf->hw);
3885 	return 0;
3886 }
3887 
3888 /**
3889  * i40e_free_misc_vector - Free the vector that handles non-queue events
3890  * @pf: board private structure
3891  **/
3892 static void i40e_free_misc_vector(struct i40e_pf *pf)
3893 {
3894 	/* Disable ICR 0 */
3895 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3896 	i40e_flush(&pf->hw);
3897 
3898 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3899 		synchronize_irq(pf->msix_entries[0].vector);
3900 		free_irq(pf->msix_entries[0].vector, pf);
3901 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3902 	}
3903 }
3904 
3905 /**
3906  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3907  * @irq: interrupt number
3908  * @data: pointer to a q_vector
3909  *
3910  * This is the handler used for all MSI/Legacy interrupts, and deals
3911  * with both queue and non-queue interrupts.  This is also used in
3912  * MSIX mode to handle the non-queue interrupts.
3913  **/
3914 static irqreturn_t i40e_intr(int irq, void *data)
3915 {
3916 	struct i40e_pf *pf = (struct i40e_pf *)data;
3917 	struct i40e_hw *hw = &pf->hw;
3918 	irqreturn_t ret = IRQ_NONE;
3919 	u32 icr0, icr0_remaining;
3920 	u32 val, ena_mask;
3921 
3922 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3923 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3924 
3925 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3926 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3927 		goto enable_intr;
3928 
3929 	/* if interrupt but no bits showing, must be SWINT */
3930 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3931 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3932 		pf->sw_int_count++;
3933 
3934 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3935 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3936 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3937 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3938 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3939 	}
3940 
3941 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3942 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3943 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3944 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3945 
3946 		/* We do not have a way to disarm Queue causes while leaving
3947 		 * interrupt enabled for all other causes, ideally
3948 		 * interrupt should be disabled while we are in NAPI but
3949 		 * this is not a performance path and napi_schedule()
3950 		 * can deal with rescheduling.
3951 		 */
3952 		if (!test_bit(__I40E_DOWN, pf->state))
3953 			napi_schedule_irqoff(&q_vector->napi);
3954 	}
3955 
3956 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3957 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3958 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3959 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3960 	}
3961 
3962 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3963 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3964 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3965 	}
3966 
3967 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3968 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3969 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3970 	}
3971 
3972 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3973 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3974 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3975 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3976 		val = rd32(hw, I40E_GLGEN_RSTAT);
3977 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3978 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3979 		if (val == I40E_RESET_CORER) {
3980 			pf->corer_count++;
3981 		} else if (val == I40E_RESET_GLOBR) {
3982 			pf->globr_count++;
3983 		} else if (val == I40E_RESET_EMPR) {
3984 			pf->empr_count++;
3985 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3986 		}
3987 	}
3988 
3989 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3990 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3991 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3992 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3993 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3994 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3995 	}
3996 
3997 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3998 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3999 
4000 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4001 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4002 			i40e_ptp_tx_hwtstamp(pf);
4003 		}
4004 	}
4005 
4006 	/* If a critical error is pending we have no choice but to reset the
4007 	 * device.
4008 	 * Report and mask out any remaining unexpected interrupts.
4009 	 */
4010 	icr0_remaining = icr0 & ena_mask;
4011 	if (icr0_remaining) {
4012 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4013 			 icr0_remaining);
4014 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4015 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4016 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4017 			dev_info(&pf->pdev->dev, "device will be reset\n");
4018 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4019 			i40e_service_event_schedule(pf);
4020 		}
4021 		ena_mask &= ~icr0_remaining;
4022 	}
4023 	ret = IRQ_HANDLED;
4024 
4025 enable_intr:
4026 	/* re-enable interrupt causes */
4027 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4028 	if (!test_bit(__I40E_DOWN, pf->state) ||
4029 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4030 		i40e_service_event_schedule(pf);
4031 		i40e_irq_dynamic_enable_icr0(pf);
4032 	}
4033 
4034 	return ret;
4035 }
4036 
4037 /**
4038  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4039  * @tx_ring:  tx ring to clean
4040  * @budget:   how many cleans we're allowed
4041  *
4042  * Returns true if there's any budget left (e.g. the clean is finished)
4043  **/
4044 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4045 {
4046 	struct i40e_vsi *vsi = tx_ring->vsi;
4047 	u16 i = tx_ring->next_to_clean;
4048 	struct i40e_tx_buffer *tx_buf;
4049 	struct i40e_tx_desc *tx_desc;
4050 
4051 	tx_buf = &tx_ring->tx_bi[i];
4052 	tx_desc = I40E_TX_DESC(tx_ring, i);
4053 	i -= tx_ring->count;
4054 
4055 	do {
4056 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4057 
4058 		/* if next_to_watch is not set then there is no work pending */
4059 		if (!eop_desc)
4060 			break;
4061 
4062 		/* prevent any other reads prior to eop_desc */
4063 		smp_rmb();
4064 
4065 		/* if the descriptor isn't done, no work yet to do */
4066 		if (!(eop_desc->cmd_type_offset_bsz &
4067 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4068 			break;
4069 
4070 		/* clear next_to_watch to prevent false hangs */
4071 		tx_buf->next_to_watch = NULL;
4072 
4073 		tx_desc->buffer_addr = 0;
4074 		tx_desc->cmd_type_offset_bsz = 0;
4075 		/* move past filter desc */
4076 		tx_buf++;
4077 		tx_desc++;
4078 		i++;
4079 		if (unlikely(!i)) {
4080 			i -= tx_ring->count;
4081 			tx_buf = tx_ring->tx_bi;
4082 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4083 		}
4084 		/* unmap skb header data */
4085 		dma_unmap_single(tx_ring->dev,
4086 				 dma_unmap_addr(tx_buf, dma),
4087 				 dma_unmap_len(tx_buf, len),
4088 				 DMA_TO_DEVICE);
4089 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4090 			kfree(tx_buf->raw_buf);
4091 
4092 		tx_buf->raw_buf = NULL;
4093 		tx_buf->tx_flags = 0;
4094 		tx_buf->next_to_watch = NULL;
4095 		dma_unmap_len_set(tx_buf, len, 0);
4096 		tx_desc->buffer_addr = 0;
4097 		tx_desc->cmd_type_offset_bsz = 0;
4098 
4099 		/* move us past the eop_desc for start of next FD desc */
4100 		tx_buf++;
4101 		tx_desc++;
4102 		i++;
4103 		if (unlikely(!i)) {
4104 			i -= tx_ring->count;
4105 			tx_buf = tx_ring->tx_bi;
4106 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4107 		}
4108 
4109 		/* update budget accounting */
4110 		budget--;
4111 	} while (likely(budget));
4112 
4113 	i += tx_ring->count;
4114 	tx_ring->next_to_clean = i;
4115 
4116 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4117 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4118 
4119 	return budget > 0;
4120 }
4121 
4122 /**
4123  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4124  * @irq: interrupt number
4125  * @data: pointer to a q_vector
4126  **/
4127 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4128 {
4129 	struct i40e_q_vector *q_vector = data;
4130 	struct i40e_vsi *vsi;
4131 
4132 	if (!q_vector->tx.ring)
4133 		return IRQ_HANDLED;
4134 
4135 	vsi = q_vector->tx.ring->vsi;
4136 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4137 
4138 	return IRQ_HANDLED;
4139 }
4140 
4141 /**
4142  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4143  * @vsi: the VSI being configured
4144  * @v_idx: vector index
4145  * @qp_idx: queue pair index
4146  **/
4147 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4148 {
4149 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4150 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4151 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4152 
4153 	tx_ring->q_vector = q_vector;
4154 	tx_ring->next = q_vector->tx.ring;
4155 	q_vector->tx.ring = tx_ring;
4156 	q_vector->tx.count++;
4157 
4158 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4159 	if (i40e_enabled_xdp_vsi(vsi)) {
4160 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4161 
4162 		xdp_ring->q_vector = q_vector;
4163 		xdp_ring->next = q_vector->tx.ring;
4164 		q_vector->tx.ring = xdp_ring;
4165 		q_vector->tx.count++;
4166 	}
4167 
4168 	rx_ring->q_vector = q_vector;
4169 	rx_ring->next = q_vector->rx.ring;
4170 	q_vector->rx.ring = rx_ring;
4171 	q_vector->rx.count++;
4172 }
4173 
4174 /**
4175  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4176  * @vsi: the VSI being configured
4177  *
4178  * This function maps descriptor rings to the queue-specific vectors
4179  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4180  * one vector per queue pair, but on a constrained vector budget, we
4181  * group the queue pairs as "efficiently" as possible.
4182  **/
4183 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4184 {
4185 	int qp_remaining = vsi->num_queue_pairs;
4186 	int q_vectors = vsi->num_q_vectors;
4187 	int num_ringpairs;
4188 	int v_start = 0;
4189 	int qp_idx = 0;
4190 
4191 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4192 	 * group them so there are multiple queues per vector.
4193 	 * It is also important to go through all the vectors available to be
4194 	 * sure that if we don't use all the vectors, that the remaining vectors
4195 	 * are cleared. This is especially important when decreasing the
4196 	 * number of queues in use.
4197 	 */
4198 	for (; v_start < q_vectors; v_start++) {
4199 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4200 
4201 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4202 
4203 		q_vector->num_ringpairs = num_ringpairs;
4204 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4205 
4206 		q_vector->rx.count = 0;
4207 		q_vector->tx.count = 0;
4208 		q_vector->rx.ring = NULL;
4209 		q_vector->tx.ring = NULL;
4210 
4211 		while (num_ringpairs--) {
4212 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4213 			qp_idx++;
4214 			qp_remaining--;
4215 		}
4216 	}
4217 }
4218 
4219 /**
4220  * i40e_vsi_request_irq - Request IRQ from the OS
4221  * @vsi: the VSI being configured
4222  * @basename: name for the vector
4223  **/
4224 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4225 {
4226 	struct i40e_pf *pf = vsi->back;
4227 	int err;
4228 
4229 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4230 		err = i40e_vsi_request_irq_msix(vsi, basename);
4231 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4232 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4233 				  pf->int_name, pf);
4234 	else
4235 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4236 				  pf->int_name, pf);
4237 
4238 	if (err)
4239 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4240 
4241 	return err;
4242 }
4243 
4244 #ifdef CONFIG_NET_POLL_CONTROLLER
4245 /**
4246  * i40e_netpoll - A Polling 'interrupt' handler
4247  * @netdev: network interface device structure
4248  *
4249  * This is used by netconsole to send skbs without having to re-enable
4250  * interrupts.  It's not called while the normal interrupt routine is executing.
4251  **/
4252 static void i40e_netpoll(struct net_device *netdev)
4253 {
4254 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4255 	struct i40e_vsi *vsi = np->vsi;
4256 	struct i40e_pf *pf = vsi->back;
4257 	int i;
4258 
4259 	/* if interface is down do nothing */
4260 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4261 		return;
4262 
4263 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4264 		for (i = 0; i < vsi->num_q_vectors; i++)
4265 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4266 	} else {
4267 		i40e_intr(pf->pdev->irq, netdev);
4268 	}
4269 }
4270 #endif
4271 
4272 #define I40E_QTX_ENA_WAIT_COUNT 50
4273 
4274 /**
4275  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4276  * @pf: the PF being configured
4277  * @pf_q: the PF queue
4278  * @enable: enable or disable state of the queue
4279  *
4280  * This routine will wait for the given Tx queue of the PF to reach the
4281  * enabled or disabled state.
4282  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4283  * multiple retries; else will return 0 in case of success.
4284  **/
4285 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4286 {
4287 	int i;
4288 	u32 tx_reg;
4289 
4290 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4291 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4292 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4293 			break;
4294 
4295 		usleep_range(10, 20);
4296 	}
4297 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4298 		return -ETIMEDOUT;
4299 
4300 	return 0;
4301 }
4302 
4303 /**
4304  * i40e_control_tx_q - Start or stop a particular Tx queue
4305  * @pf: the PF structure
4306  * @pf_q: the PF queue to configure
4307  * @enable: start or stop the queue
4308  *
4309  * This function enables or disables a single queue. Note that any delay
4310  * required after the operation is expected to be handled by the caller of
4311  * this function.
4312  **/
4313 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4314 {
4315 	struct i40e_hw *hw = &pf->hw;
4316 	u32 tx_reg;
4317 	int i;
4318 
4319 	/* warn the TX unit of coming changes */
4320 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4321 	if (!enable)
4322 		usleep_range(10, 20);
4323 
4324 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4325 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4326 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4327 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4328 			break;
4329 		usleep_range(1000, 2000);
4330 	}
4331 
4332 	/* Skip if the queue is already in the requested state */
4333 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4334 		return;
4335 
4336 	/* turn on/off the queue */
4337 	if (enable) {
4338 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4339 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4340 	} else {
4341 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4342 	}
4343 
4344 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4345 }
4346 
4347 /**
4348  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4349  * @seid: VSI SEID
4350  * @pf: the PF structure
4351  * @pf_q: the PF queue to configure
4352  * @is_xdp: true if the queue is used for XDP
4353  * @enable: start or stop the queue
4354  **/
4355 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4356 			   bool is_xdp, bool enable)
4357 {
4358 	int ret;
4359 
4360 	i40e_control_tx_q(pf, pf_q, enable);
4361 
4362 	/* wait for the change to finish */
4363 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4364 	if (ret) {
4365 		dev_info(&pf->pdev->dev,
4366 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4367 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4368 			 (enable ? "en" : "dis"));
4369 	}
4370 
4371 	return ret;
4372 }
4373 
4374 /**
4375  * i40e_vsi_control_tx - Start or stop a VSI's rings
4376  * @vsi: the VSI being configured
4377  * @enable: start or stop the rings
4378  **/
4379 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4380 {
4381 	struct i40e_pf *pf = vsi->back;
4382 	int i, pf_q, ret = 0;
4383 
4384 	pf_q = vsi->base_queue;
4385 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4386 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4387 					     pf_q,
4388 					     false /*is xdp*/, enable);
4389 		if (ret)
4390 			break;
4391 
4392 		if (!i40e_enabled_xdp_vsi(vsi))
4393 			continue;
4394 
4395 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4396 					     pf_q + vsi->alloc_queue_pairs,
4397 					     true /*is xdp*/, enable);
4398 		if (ret)
4399 			break;
4400 	}
4401 	return ret;
4402 }
4403 
4404 /**
4405  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4406  * @pf: the PF being configured
4407  * @pf_q: the PF queue
4408  * @enable: enable or disable state of the queue
4409  *
4410  * This routine will wait for the given Rx queue of the PF to reach the
4411  * enabled or disabled state.
4412  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4413  * multiple retries; else will return 0 in case of success.
4414  **/
4415 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4416 {
4417 	int i;
4418 	u32 rx_reg;
4419 
4420 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4421 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4422 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4423 			break;
4424 
4425 		usleep_range(10, 20);
4426 	}
4427 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4428 		return -ETIMEDOUT;
4429 
4430 	return 0;
4431 }
4432 
4433 /**
4434  * i40e_control_rx_q - Start or stop a particular Rx queue
4435  * @pf: the PF structure
4436  * @pf_q: the PF queue to configure
4437  * @enable: start or stop the queue
4438  *
4439  * This function enables or disables a single queue. Note that
4440  * any delay required after the operation is expected to be
4441  * handled by the caller of this function.
4442  **/
4443 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4444 {
4445 	struct i40e_hw *hw = &pf->hw;
4446 	u32 rx_reg;
4447 	int i;
4448 
4449 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4450 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4451 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4452 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4453 			break;
4454 		usleep_range(1000, 2000);
4455 	}
4456 
4457 	/* Skip if the queue is already in the requested state */
4458 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4459 		return;
4460 
4461 	/* turn on/off the queue */
4462 	if (enable)
4463 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4464 	else
4465 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4466 
4467 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4468 }
4469 
4470 /**
4471  * i40e_control_wait_rx_q
4472  * @pf: the PF structure
4473  * @pf_q: queue being configured
4474  * @enable: start or stop the rings
4475  *
4476  * This function enables or disables a single queue along with waiting
4477  * for the change to finish. The caller of this function should handle
4478  * the delays needed in the case of disabling queues.
4479  **/
4480 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4481 {
4482 	int ret = 0;
4483 
4484 	i40e_control_rx_q(pf, pf_q, enable);
4485 
4486 	/* wait for the change to finish */
4487 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4488 	if (ret)
4489 		return ret;
4490 
4491 	return ret;
4492 }
4493 
4494 /**
4495  * i40e_vsi_control_rx - Start or stop a VSI's rings
4496  * @vsi: the VSI being configured
4497  * @enable: start or stop the rings
4498  **/
4499 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4500 {
4501 	struct i40e_pf *pf = vsi->back;
4502 	int i, pf_q, ret = 0;
4503 
4504 	pf_q = vsi->base_queue;
4505 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4506 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4507 		if (ret) {
4508 			dev_info(&pf->pdev->dev,
4509 				 "VSI seid %d Rx ring %d %sable timeout\n",
4510 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4511 			break;
4512 		}
4513 	}
4514 
4515 	/* Due to HW errata, on Rx disable only, the register can indicate done
4516 	 * before it really is. Needs 50ms to be sure
4517 	 */
4518 	if (!enable)
4519 		mdelay(50);
4520 
4521 	return ret;
4522 }
4523 
4524 /**
4525  * i40e_vsi_start_rings - Start a VSI's rings
4526  * @vsi: the VSI being configured
4527  **/
4528 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4529 {
4530 	int ret = 0;
4531 
4532 	/* do rx first for enable and last for disable */
4533 	ret = i40e_vsi_control_rx(vsi, true);
4534 	if (ret)
4535 		return ret;
4536 	ret = i40e_vsi_control_tx(vsi, true);
4537 
4538 	return ret;
4539 }
4540 
4541 /**
4542  * i40e_vsi_stop_rings - Stop a VSI's rings
4543  * @vsi: the VSI being configured
4544  **/
4545 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4546 {
4547 	/* When port TX is suspended, don't wait */
4548 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4549 		return i40e_vsi_stop_rings_no_wait(vsi);
4550 
4551 	/* do rx first for enable and last for disable
4552 	 * Ignore return value, we need to shutdown whatever we can
4553 	 */
4554 	i40e_vsi_control_tx(vsi, false);
4555 	i40e_vsi_control_rx(vsi, false);
4556 }
4557 
4558 /**
4559  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4560  * @vsi: the VSI being shutdown
4561  *
4562  * This function stops all the rings for a VSI but does not delay to verify
4563  * that rings have been disabled. It is expected that the caller is shutting
4564  * down multiple VSIs at once and will delay together for all the VSIs after
4565  * initiating the shutdown. This is particularly useful for shutting down lots
4566  * of VFs together. Otherwise, a large delay can be incurred while configuring
4567  * each VSI in serial.
4568  **/
4569 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4570 {
4571 	struct i40e_pf *pf = vsi->back;
4572 	int i, pf_q;
4573 
4574 	pf_q = vsi->base_queue;
4575 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4576 		i40e_control_tx_q(pf, pf_q, false);
4577 		i40e_control_rx_q(pf, pf_q, false);
4578 	}
4579 }
4580 
4581 /**
4582  * i40e_vsi_free_irq - Free the irq association with the OS
4583  * @vsi: the VSI being configured
4584  **/
4585 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4586 {
4587 	struct i40e_pf *pf = vsi->back;
4588 	struct i40e_hw *hw = &pf->hw;
4589 	int base = vsi->base_vector;
4590 	u32 val, qp;
4591 	int i;
4592 
4593 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4594 		if (!vsi->q_vectors)
4595 			return;
4596 
4597 		if (!vsi->irqs_ready)
4598 			return;
4599 
4600 		vsi->irqs_ready = false;
4601 		for (i = 0; i < vsi->num_q_vectors; i++) {
4602 			int irq_num;
4603 			u16 vector;
4604 
4605 			vector = i + base;
4606 			irq_num = pf->msix_entries[vector].vector;
4607 
4608 			/* free only the irqs that were actually requested */
4609 			if (!vsi->q_vectors[i] ||
4610 			    !vsi->q_vectors[i]->num_ringpairs)
4611 				continue;
4612 
4613 			/* clear the affinity notifier in the IRQ descriptor */
4614 			irq_set_affinity_notifier(irq_num, NULL);
4615 			/* remove our suggested affinity mask for this IRQ */
4616 			irq_set_affinity_hint(irq_num, NULL);
4617 			synchronize_irq(irq_num);
4618 			free_irq(irq_num, vsi->q_vectors[i]);
4619 
4620 			/* Tear down the interrupt queue link list
4621 			 *
4622 			 * We know that they come in pairs and always
4623 			 * the Rx first, then the Tx.  To clear the
4624 			 * link list, stick the EOL value into the
4625 			 * next_q field of the registers.
4626 			 */
4627 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4628 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4629 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4630 			val |= I40E_QUEUE_END_OF_LIST
4631 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4632 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4633 
4634 			while (qp != I40E_QUEUE_END_OF_LIST) {
4635 				u32 next;
4636 
4637 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4638 
4639 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4640 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4641 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4642 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4643 
4644 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4645 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4646 
4647 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4648 
4649 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4650 
4651 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4652 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4653 
4654 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4655 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4656 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4657 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4658 
4659 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4660 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4661 
4662 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4663 				qp = next;
4664 			}
4665 		}
4666 	} else {
4667 		free_irq(pf->pdev->irq, pf);
4668 
4669 		val = rd32(hw, I40E_PFINT_LNKLST0);
4670 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4671 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4672 		val |= I40E_QUEUE_END_OF_LIST
4673 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4674 		wr32(hw, I40E_PFINT_LNKLST0, val);
4675 
4676 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4677 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4678 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4679 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4680 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4681 
4682 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4683 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4684 
4685 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4686 
4687 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4688 
4689 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4690 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4691 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4692 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4693 
4694 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4695 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4696 
4697 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4698 	}
4699 }
4700 
4701 /**
4702  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4703  * @vsi: the VSI being configured
4704  * @v_idx: Index of vector to be freed
4705  *
4706  * This function frees the memory allocated to the q_vector.  In addition if
4707  * NAPI is enabled it will delete any references to the NAPI struct prior
4708  * to freeing the q_vector.
4709  **/
4710 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4711 {
4712 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4713 	struct i40e_ring *ring;
4714 
4715 	if (!q_vector)
4716 		return;
4717 
4718 	/* disassociate q_vector from rings */
4719 	i40e_for_each_ring(ring, q_vector->tx)
4720 		ring->q_vector = NULL;
4721 
4722 	i40e_for_each_ring(ring, q_vector->rx)
4723 		ring->q_vector = NULL;
4724 
4725 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4726 	if (vsi->netdev)
4727 		netif_napi_del(&q_vector->napi);
4728 
4729 	vsi->q_vectors[v_idx] = NULL;
4730 
4731 	kfree_rcu(q_vector, rcu);
4732 }
4733 
4734 /**
4735  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4736  * @vsi: the VSI being un-configured
4737  *
4738  * This frees the memory allocated to the q_vectors and
4739  * deletes references to the NAPI struct.
4740  **/
4741 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4742 {
4743 	int v_idx;
4744 
4745 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4746 		i40e_free_q_vector(vsi, v_idx);
4747 }
4748 
4749 /**
4750  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4751  * @pf: board private structure
4752  **/
4753 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4754 {
4755 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4756 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4757 		pci_disable_msix(pf->pdev);
4758 		kfree(pf->msix_entries);
4759 		pf->msix_entries = NULL;
4760 		kfree(pf->irq_pile);
4761 		pf->irq_pile = NULL;
4762 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4763 		pci_disable_msi(pf->pdev);
4764 	}
4765 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4766 }
4767 
4768 /**
4769  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4770  * @pf: board private structure
4771  *
4772  * We go through and clear interrupt specific resources and reset the structure
4773  * to pre-load conditions
4774  **/
4775 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4776 {
4777 	int i;
4778 
4779 	i40e_free_misc_vector(pf);
4780 
4781 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4782 		      I40E_IWARP_IRQ_PILE_ID);
4783 
4784 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4785 	for (i = 0; i < pf->num_alloc_vsi; i++)
4786 		if (pf->vsi[i])
4787 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4788 	i40e_reset_interrupt_capability(pf);
4789 }
4790 
4791 /**
4792  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4793  * @vsi: the VSI being configured
4794  **/
4795 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4796 {
4797 	int q_idx;
4798 
4799 	if (!vsi->netdev)
4800 		return;
4801 
4802 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4803 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4804 
4805 		if (q_vector->rx.ring || q_vector->tx.ring)
4806 			napi_enable(&q_vector->napi);
4807 	}
4808 }
4809 
4810 /**
4811  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4812  * @vsi: the VSI being configured
4813  **/
4814 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4815 {
4816 	int q_idx;
4817 
4818 	if (!vsi->netdev)
4819 		return;
4820 
4821 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4822 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4823 
4824 		if (q_vector->rx.ring || q_vector->tx.ring)
4825 			napi_disable(&q_vector->napi);
4826 	}
4827 }
4828 
4829 /**
4830  * i40e_vsi_close - Shut down a VSI
4831  * @vsi: the vsi to be quelled
4832  **/
4833 static void i40e_vsi_close(struct i40e_vsi *vsi)
4834 {
4835 	struct i40e_pf *pf = vsi->back;
4836 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4837 		i40e_down(vsi);
4838 	i40e_vsi_free_irq(vsi);
4839 	i40e_vsi_free_tx_resources(vsi);
4840 	i40e_vsi_free_rx_resources(vsi);
4841 	vsi->current_netdev_flags = 0;
4842 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4843 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4844 		set_bit(__I40E_CLIENT_RESET, pf->state);
4845 }
4846 
4847 /**
4848  * i40e_quiesce_vsi - Pause a given VSI
4849  * @vsi: the VSI being paused
4850  **/
4851 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4852 {
4853 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4854 		return;
4855 
4856 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4857 	if (vsi->netdev && netif_running(vsi->netdev))
4858 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4859 	else
4860 		i40e_vsi_close(vsi);
4861 }
4862 
4863 /**
4864  * i40e_unquiesce_vsi - Resume a given VSI
4865  * @vsi: the VSI being resumed
4866  **/
4867 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4868 {
4869 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4870 		return;
4871 
4872 	if (vsi->netdev && netif_running(vsi->netdev))
4873 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4874 	else
4875 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4876 }
4877 
4878 /**
4879  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4880  * @pf: the PF
4881  **/
4882 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4883 {
4884 	int v;
4885 
4886 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4887 		if (pf->vsi[v])
4888 			i40e_quiesce_vsi(pf->vsi[v]);
4889 	}
4890 }
4891 
4892 /**
4893  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4894  * @pf: the PF
4895  **/
4896 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4897 {
4898 	int v;
4899 
4900 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4901 		if (pf->vsi[v])
4902 			i40e_unquiesce_vsi(pf->vsi[v]);
4903 	}
4904 }
4905 
4906 /**
4907  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4908  * @vsi: the VSI being configured
4909  *
4910  * Wait until all queues on a given VSI have been disabled.
4911  **/
4912 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4913 {
4914 	struct i40e_pf *pf = vsi->back;
4915 	int i, pf_q, ret;
4916 
4917 	pf_q = vsi->base_queue;
4918 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4919 		/* Check and wait for the Tx queue */
4920 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4921 		if (ret) {
4922 			dev_info(&pf->pdev->dev,
4923 				 "VSI seid %d Tx ring %d disable timeout\n",
4924 				 vsi->seid, pf_q);
4925 			return ret;
4926 		}
4927 
4928 		if (!i40e_enabled_xdp_vsi(vsi))
4929 			goto wait_rx;
4930 
4931 		/* Check and wait for the XDP Tx queue */
4932 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4933 				       false);
4934 		if (ret) {
4935 			dev_info(&pf->pdev->dev,
4936 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4937 				 vsi->seid, pf_q);
4938 			return ret;
4939 		}
4940 wait_rx:
4941 		/* Check and wait for the Rx queue */
4942 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4943 		if (ret) {
4944 			dev_info(&pf->pdev->dev,
4945 				 "VSI seid %d Rx ring %d disable timeout\n",
4946 				 vsi->seid, pf_q);
4947 			return ret;
4948 		}
4949 	}
4950 
4951 	return 0;
4952 }
4953 
4954 #ifdef CONFIG_I40E_DCB
4955 /**
4956  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4957  * @pf: the PF
4958  *
4959  * This function waits for the queues to be in disabled state for all the
4960  * VSIs that are managed by this PF.
4961  **/
4962 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4963 {
4964 	int v, ret = 0;
4965 
4966 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4967 		if (pf->vsi[v]) {
4968 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4969 			if (ret)
4970 				break;
4971 		}
4972 	}
4973 
4974 	return ret;
4975 }
4976 
4977 #endif
4978 
4979 /**
4980  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4981  * @pf: pointer to PF
4982  *
4983  * Get TC map for ISCSI PF type that will include iSCSI TC
4984  * and LAN TC.
4985  **/
4986 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4987 {
4988 	struct i40e_dcb_app_priority_table app;
4989 	struct i40e_hw *hw = &pf->hw;
4990 	u8 enabled_tc = 1; /* TC0 is always enabled */
4991 	u8 tc, i;
4992 	/* Get the iSCSI APP TLV */
4993 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4994 
4995 	for (i = 0; i < dcbcfg->numapps; i++) {
4996 		app = dcbcfg->app[i];
4997 		if (app.selector == I40E_APP_SEL_TCPIP &&
4998 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
4999 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5000 			enabled_tc |= BIT(tc);
5001 			break;
5002 		}
5003 	}
5004 
5005 	return enabled_tc;
5006 }
5007 
5008 /**
5009  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5010  * @dcbcfg: the corresponding DCBx configuration structure
5011  *
5012  * Return the number of TCs from given DCBx configuration
5013  **/
5014 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5015 {
5016 	int i, tc_unused = 0;
5017 	u8 num_tc = 0;
5018 	u8 ret = 0;
5019 
5020 	/* Scan the ETS Config Priority Table to find
5021 	 * traffic class enabled for a given priority
5022 	 * and create a bitmask of enabled TCs
5023 	 */
5024 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5025 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5026 
5027 	/* Now scan the bitmask to check for
5028 	 * contiguous TCs starting with TC0
5029 	 */
5030 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5031 		if (num_tc & BIT(i)) {
5032 			if (!tc_unused) {
5033 				ret++;
5034 			} else {
5035 				pr_err("Non-contiguous TC - Disabling DCB\n");
5036 				return 1;
5037 			}
5038 		} else {
5039 			tc_unused = 1;
5040 		}
5041 	}
5042 
5043 	/* There is always at least TC0 */
5044 	if (!ret)
5045 		ret = 1;
5046 
5047 	return ret;
5048 }
5049 
5050 /**
5051  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5052  * @dcbcfg: the corresponding DCBx configuration structure
5053  *
5054  * Query the current DCB configuration and return the number of
5055  * traffic classes enabled from the given DCBX config
5056  **/
5057 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5058 {
5059 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5060 	u8 enabled_tc = 1;
5061 	u8 i;
5062 
5063 	for (i = 0; i < num_tc; i++)
5064 		enabled_tc |= BIT(i);
5065 
5066 	return enabled_tc;
5067 }
5068 
5069 /**
5070  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5071  * @pf: PF being queried
5072  *
5073  * Query the current MQPRIO configuration and return the number of
5074  * traffic classes enabled.
5075  **/
5076 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5077 {
5078 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5079 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5080 	u8 enabled_tc = 1, i;
5081 
5082 	for (i = 1; i < num_tc; i++)
5083 		enabled_tc |= BIT(i);
5084 	return enabled_tc;
5085 }
5086 
5087 /**
5088  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5089  * @pf: PF being queried
5090  *
5091  * Return number of traffic classes enabled for the given PF
5092  **/
5093 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5094 {
5095 	struct i40e_hw *hw = &pf->hw;
5096 	u8 i, enabled_tc = 1;
5097 	u8 num_tc = 0;
5098 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5099 
5100 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5101 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5102 
5103 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5104 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5105 		return 1;
5106 
5107 	/* SFP mode will be enabled for all TCs on port */
5108 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5109 		return i40e_dcb_get_num_tc(dcbcfg);
5110 
5111 	/* MFP mode return count of enabled TCs for this PF */
5112 	if (pf->hw.func_caps.iscsi)
5113 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5114 	else
5115 		return 1; /* Only TC0 */
5116 
5117 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5118 		if (enabled_tc & BIT(i))
5119 			num_tc++;
5120 	}
5121 	return num_tc;
5122 }
5123 
5124 /**
5125  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5126  * @pf: PF being queried
5127  *
5128  * Return a bitmap for enabled traffic classes for this PF.
5129  **/
5130 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5131 {
5132 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5133 		return i40e_mqprio_get_enabled_tc(pf);
5134 
5135 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5136 	 * default TC
5137 	 */
5138 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5139 		return I40E_DEFAULT_TRAFFIC_CLASS;
5140 
5141 	/* SFP mode we want PF to be enabled for all TCs */
5142 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5143 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5144 
5145 	/* MFP enabled and iSCSI PF type */
5146 	if (pf->hw.func_caps.iscsi)
5147 		return i40e_get_iscsi_tc_map(pf);
5148 	else
5149 		return I40E_DEFAULT_TRAFFIC_CLASS;
5150 }
5151 
5152 /**
5153  * i40e_vsi_get_bw_info - Query VSI BW Information
5154  * @vsi: the VSI being queried
5155  *
5156  * Returns 0 on success, negative value on failure
5157  **/
5158 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5159 {
5160 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5161 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5162 	struct i40e_pf *pf = vsi->back;
5163 	struct i40e_hw *hw = &pf->hw;
5164 	i40e_status ret;
5165 	u32 tc_bw_max;
5166 	int i;
5167 
5168 	/* Get the VSI level BW configuration */
5169 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5170 	if (ret) {
5171 		dev_info(&pf->pdev->dev,
5172 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5173 			 i40e_stat_str(&pf->hw, ret),
5174 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5175 		return -EINVAL;
5176 	}
5177 
5178 	/* Get the VSI level BW configuration per TC */
5179 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5180 					       NULL);
5181 	if (ret) {
5182 		dev_info(&pf->pdev->dev,
5183 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5184 			 i40e_stat_str(&pf->hw, ret),
5185 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5186 		return -EINVAL;
5187 	}
5188 
5189 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5190 		dev_info(&pf->pdev->dev,
5191 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5192 			 bw_config.tc_valid_bits,
5193 			 bw_ets_config.tc_valid_bits);
5194 		/* Still continuing */
5195 	}
5196 
5197 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5198 	vsi->bw_max_quanta = bw_config.max_bw;
5199 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5200 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5201 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5202 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5203 		vsi->bw_ets_limit_credits[i] =
5204 					le16_to_cpu(bw_ets_config.credits[i]);
5205 		/* 3 bits out of 4 for each TC */
5206 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5207 	}
5208 
5209 	return 0;
5210 }
5211 
5212 /**
5213  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5214  * @vsi: the VSI being configured
5215  * @enabled_tc: TC bitmap
5216  * @bw_share: BW shared credits per TC
5217  *
5218  * Returns 0 on success, negative value on failure
5219  **/
5220 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5221 				       u8 *bw_share)
5222 {
5223 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5224 	struct i40e_pf *pf = vsi->back;
5225 	i40e_status ret;
5226 	int i;
5227 
5228 	/* There is no need to reset BW when mqprio mode is on.  */
5229 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5230 		return 0;
5231 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5232 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5233 		if (ret)
5234 			dev_info(&pf->pdev->dev,
5235 				 "Failed to reset tx rate for vsi->seid %u\n",
5236 				 vsi->seid);
5237 		return ret;
5238 	}
5239 	bw_data.tc_valid_bits = enabled_tc;
5240 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5241 		bw_data.tc_bw_credits[i] = bw_share[i];
5242 
5243 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5244 	if (ret) {
5245 		dev_info(&pf->pdev->dev,
5246 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5247 			 pf->hw.aq.asq_last_status);
5248 		return -EINVAL;
5249 	}
5250 
5251 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5252 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5253 
5254 	return 0;
5255 }
5256 
5257 /**
5258  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5259  * @vsi: the VSI being configured
5260  * @enabled_tc: TC map to be enabled
5261  *
5262  **/
5263 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5264 {
5265 	struct net_device *netdev = vsi->netdev;
5266 	struct i40e_pf *pf = vsi->back;
5267 	struct i40e_hw *hw = &pf->hw;
5268 	u8 netdev_tc = 0;
5269 	int i;
5270 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5271 
5272 	if (!netdev)
5273 		return;
5274 
5275 	if (!enabled_tc) {
5276 		netdev_reset_tc(netdev);
5277 		return;
5278 	}
5279 
5280 	/* Set up actual enabled TCs on the VSI */
5281 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5282 		return;
5283 
5284 	/* set per TC queues for the VSI */
5285 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5286 		/* Only set TC queues for enabled tcs
5287 		 *
5288 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5289 		 * enabled_tc bitmap would be 0x00001001; the driver
5290 		 * will set the numtc for netdev as 2 that will be
5291 		 * referenced by the netdev layer as TC 0 and 1.
5292 		 */
5293 		if (vsi->tc_config.enabled_tc & BIT(i))
5294 			netdev_set_tc_queue(netdev,
5295 					vsi->tc_config.tc_info[i].netdev_tc,
5296 					vsi->tc_config.tc_info[i].qcount,
5297 					vsi->tc_config.tc_info[i].qoffset);
5298 	}
5299 
5300 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5301 		return;
5302 
5303 	/* Assign UP2TC map for the VSI */
5304 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5305 		/* Get the actual TC# for the UP */
5306 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5307 		/* Get the mapped netdev TC# for the UP */
5308 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5309 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5310 	}
5311 }
5312 
5313 /**
5314  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5315  * @vsi: the VSI being configured
5316  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5317  **/
5318 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5319 				      struct i40e_vsi_context *ctxt)
5320 {
5321 	/* copy just the sections touched not the entire info
5322 	 * since not all sections are valid as returned by
5323 	 * update vsi params
5324 	 */
5325 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5326 	memcpy(&vsi->info.queue_mapping,
5327 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5328 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5329 	       sizeof(vsi->info.tc_mapping));
5330 }
5331 
5332 /**
5333  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5334  * @vsi: VSI to be configured
5335  * @enabled_tc: TC bitmap
5336  *
5337  * This configures a particular VSI for TCs that are mapped to the
5338  * given TC bitmap. It uses default bandwidth share for TCs across
5339  * VSIs to configure TC for a particular VSI.
5340  *
5341  * NOTE:
5342  * It is expected that the VSI queues have been quisced before calling
5343  * this function.
5344  **/
5345 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5346 {
5347 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5348 	struct i40e_pf *pf = vsi->back;
5349 	struct i40e_hw *hw = &pf->hw;
5350 	struct i40e_vsi_context ctxt;
5351 	int ret = 0;
5352 	int i;
5353 
5354 	/* Check if enabled_tc is same as existing or new TCs */
5355 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5356 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5357 		return ret;
5358 
5359 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5360 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5361 		if (enabled_tc & BIT(i))
5362 			bw_share[i] = 1;
5363 	}
5364 
5365 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5366 	if (ret) {
5367 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5368 
5369 		dev_info(&pf->pdev->dev,
5370 			 "Failed configuring TC map %d for VSI %d\n",
5371 			 enabled_tc, vsi->seid);
5372 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5373 						  &bw_config, NULL);
5374 		if (ret) {
5375 			dev_info(&pf->pdev->dev,
5376 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5377 				 i40e_stat_str(hw, ret),
5378 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5379 			goto out;
5380 		}
5381 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5382 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5383 
5384 			if (!valid_tc)
5385 				valid_tc = bw_config.tc_valid_bits;
5386 			/* Always enable TC0, no matter what */
5387 			valid_tc |= 1;
5388 			dev_info(&pf->pdev->dev,
5389 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5390 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5391 			enabled_tc = valid_tc;
5392 		}
5393 
5394 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5395 		if (ret) {
5396 			dev_err(&pf->pdev->dev,
5397 				"Unable to  configure TC map %d for VSI %d\n",
5398 				enabled_tc, vsi->seid);
5399 			goto out;
5400 		}
5401 	}
5402 
5403 	/* Update Queue Pairs Mapping for currently enabled UPs */
5404 	ctxt.seid = vsi->seid;
5405 	ctxt.pf_num = vsi->back->hw.pf_id;
5406 	ctxt.vf_num = 0;
5407 	ctxt.uplink_seid = vsi->uplink_seid;
5408 	ctxt.info = vsi->info;
5409 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5410 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5411 		if (ret)
5412 			goto out;
5413 	} else {
5414 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5415 	}
5416 
5417 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5418 	 * queues changed.
5419 	 */
5420 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5421 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5422 				      vsi->num_queue_pairs);
5423 		ret = i40e_vsi_config_rss(vsi);
5424 		if (ret) {
5425 			dev_info(&vsi->back->pdev->dev,
5426 				 "Failed to reconfig rss for num_queues\n");
5427 			return ret;
5428 		}
5429 		vsi->reconfig_rss = false;
5430 	}
5431 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5432 		ctxt.info.valid_sections |=
5433 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5434 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5435 	}
5436 
5437 	/* Update the VSI after updating the VSI queue-mapping
5438 	 * information
5439 	 */
5440 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5441 	if (ret) {
5442 		dev_info(&pf->pdev->dev,
5443 			 "Update vsi tc config failed, err %s aq_err %s\n",
5444 			 i40e_stat_str(hw, ret),
5445 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5446 		goto out;
5447 	}
5448 	/* update the local VSI info with updated queue map */
5449 	i40e_vsi_update_queue_map(vsi, &ctxt);
5450 	vsi->info.valid_sections = 0;
5451 
5452 	/* Update current VSI BW information */
5453 	ret = i40e_vsi_get_bw_info(vsi);
5454 	if (ret) {
5455 		dev_info(&pf->pdev->dev,
5456 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5457 			 i40e_stat_str(hw, ret),
5458 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5459 		goto out;
5460 	}
5461 
5462 	/* Update the netdev TC setup */
5463 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5464 out:
5465 	return ret;
5466 }
5467 
5468 /**
5469  * i40e_get_link_speed - Returns link speed for the interface
5470  * @vsi: VSI to be configured
5471  *
5472  **/
5473 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5474 {
5475 	struct i40e_pf *pf = vsi->back;
5476 
5477 	switch (pf->hw.phy.link_info.link_speed) {
5478 	case I40E_LINK_SPEED_40GB:
5479 		return 40000;
5480 	case I40E_LINK_SPEED_25GB:
5481 		return 25000;
5482 	case I40E_LINK_SPEED_20GB:
5483 		return 20000;
5484 	case I40E_LINK_SPEED_10GB:
5485 		return 10000;
5486 	case I40E_LINK_SPEED_1GB:
5487 		return 1000;
5488 	default:
5489 		return -EINVAL;
5490 	}
5491 }
5492 
5493 /**
5494  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5495  * @vsi: VSI to be configured
5496  * @seid: seid of the channel/VSI
5497  * @max_tx_rate: max TX rate to be configured as BW limit
5498  *
5499  * Helper function to set BW limit for a given VSI
5500  **/
5501 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5502 {
5503 	struct i40e_pf *pf = vsi->back;
5504 	u64 credits = 0;
5505 	int speed = 0;
5506 	int ret = 0;
5507 
5508 	speed = i40e_get_link_speed(vsi);
5509 	if (max_tx_rate > speed) {
5510 		dev_err(&pf->pdev->dev,
5511 			"Invalid max tx rate %llu specified for VSI seid %d.",
5512 			max_tx_rate, seid);
5513 		return -EINVAL;
5514 	}
5515 	if (max_tx_rate && max_tx_rate < 50) {
5516 		dev_warn(&pf->pdev->dev,
5517 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5518 		max_tx_rate = 50;
5519 	}
5520 
5521 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5522 	credits = max_tx_rate;
5523 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5524 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5525 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5526 	if (ret)
5527 		dev_err(&pf->pdev->dev,
5528 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5529 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5530 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5531 	return ret;
5532 }
5533 
5534 /**
5535  * i40e_remove_queue_channels - Remove queue channels for the TCs
5536  * @vsi: VSI to be configured
5537  *
5538  * Remove queue channels for the TCs
5539  **/
5540 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5541 {
5542 	enum i40e_admin_queue_err last_aq_status;
5543 	struct i40e_cloud_filter *cfilter;
5544 	struct i40e_channel *ch, *ch_tmp;
5545 	struct i40e_pf *pf = vsi->back;
5546 	struct hlist_node *node;
5547 	int ret, i;
5548 
5549 	/* Reset rss size that was stored when reconfiguring rss for
5550 	 * channel VSIs with non-power-of-2 queue count.
5551 	 */
5552 	vsi->current_rss_size = 0;
5553 
5554 	/* perform cleanup for channels if they exist */
5555 	if (list_empty(&vsi->ch_list))
5556 		return;
5557 
5558 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5559 		struct i40e_vsi *p_vsi;
5560 
5561 		list_del(&ch->list);
5562 		p_vsi = ch->parent_vsi;
5563 		if (!p_vsi || !ch->initialized) {
5564 			kfree(ch);
5565 			continue;
5566 		}
5567 		/* Reset queue contexts */
5568 		for (i = 0; i < ch->num_queue_pairs; i++) {
5569 			struct i40e_ring *tx_ring, *rx_ring;
5570 			u16 pf_q;
5571 
5572 			pf_q = ch->base_queue + i;
5573 			tx_ring = vsi->tx_rings[pf_q];
5574 			tx_ring->ch = NULL;
5575 
5576 			rx_ring = vsi->rx_rings[pf_q];
5577 			rx_ring->ch = NULL;
5578 		}
5579 
5580 		/* Reset BW configured for this VSI via mqprio */
5581 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5582 		if (ret)
5583 			dev_info(&vsi->back->pdev->dev,
5584 				 "Failed to reset tx rate for ch->seid %u\n",
5585 				 ch->seid);
5586 
5587 		/* delete cloud filters associated with this channel */
5588 		hlist_for_each_entry_safe(cfilter, node,
5589 					  &pf->cloud_filter_list, cloud_node) {
5590 			if (cfilter->seid != ch->seid)
5591 				continue;
5592 
5593 			hash_del(&cfilter->cloud_node);
5594 			if (cfilter->dst_port)
5595 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5596 									cfilter,
5597 									false);
5598 			else
5599 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5600 								false);
5601 			last_aq_status = pf->hw.aq.asq_last_status;
5602 			if (ret)
5603 				dev_info(&pf->pdev->dev,
5604 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5605 					 i40e_stat_str(&pf->hw, ret),
5606 					 i40e_aq_str(&pf->hw, last_aq_status));
5607 			kfree(cfilter);
5608 		}
5609 
5610 		/* delete VSI from FW */
5611 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5612 					     NULL);
5613 		if (ret)
5614 			dev_err(&vsi->back->pdev->dev,
5615 				"unable to remove channel (%d) for parent VSI(%d)\n",
5616 				ch->seid, p_vsi->seid);
5617 		kfree(ch);
5618 	}
5619 	INIT_LIST_HEAD(&vsi->ch_list);
5620 }
5621 
5622 /**
5623  * i40e_is_any_channel - channel exist or not
5624  * @vsi: ptr to VSI to which channels are associated with
5625  *
5626  * Returns true or false if channel(s) exist for associated VSI or not
5627  **/
5628 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5629 {
5630 	struct i40e_channel *ch, *ch_tmp;
5631 
5632 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5633 		if (ch->initialized)
5634 			return true;
5635 	}
5636 
5637 	return false;
5638 }
5639 
5640 /**
5641  * i40e_get_max_queues_for_channel
5642  * @vsi: ptr to VSI to which channels are associated with
5643  *
5644  * Helper function which returns max value among the queue counts set on the
5645  * channels/TCs created.
5646  **/
5647 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5648 {
5649 	struct i40e_channel *ch, *ch_tmp;
5650 	int max = 0;
5651 
5652 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5653 		if (!ch->initialized)
5654 			continue;
5655 		if (ch->num_queue_pairs > max)
5656 			max = ch->num_queue_pairs;
5657 	}
5658 
5659 	return max;
5660 }
5661 
5662 /**
5663  * i40e_validate_num_queues - validate num_queues w.r.t channel
5664  * @pf: ptr to PF device
5665  * @num_queues: number of queues
5666  * @vsi: the parent VSI
5667  * @reconfig_rss: indicates should the RSS be reconfigured or not
5668  *
5669  * This function validates number of queues in the context of new channel
5670  * which is being established and determines if RSS should be reconfigured
5671  * or not for parent VSI.
5672  **/
5673 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5674 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5675 {
5676 	int max_ch_queues;
5677 
5678 	if (!reconfig_rss)
5679 		return -EINVAL;
5680 
5681 	*reconfig_rss = false;
5682 	if (vsi->current_rss_size) {
5683 		if (num_queues > vsi->current_rss_size) {
5684 			dev_dbg(&pf->pdev->dev,
5685 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5686 				num_queues, vsi->current_rss_size);
5687 			return -EINVAL;
5688 		} else if ((num_queues < vsi->current_rss_size) &&
5689 			   (!is_power_of_2(num_queues))) {
5690 			dev_dbg(&pf->pdev->dev,
5691 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5692 				num_queues, vsi->current_rss_size);
5693 			return -EINVAL;
5694 		}
5695 	}
5696 
5697 	if (!is_power_of_2(num_queues)) {
5698 		/* Find the max num_queues configured for channel if channel
5699 		 * exist.
5700 		 * if channel exist, then enforce 'num_queues' to be more than
5701 		 * max ever queues configured for channel.
5702 		 */
5703 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5704 		if (num_queues < max_ch_queues) {
5705 			dev_dbg(&pf->pdev->dev,
5706 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5707 				num_queues, max_ch_queues);
5708 			return -EINVAL;
5709 		}
5710 		*reconfig_rss = true;
5711 	}
5712 
5713 	return 0;
5714 }
5715 
5716 /**
5717  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5718  * @vsi: the VSI being setup
5719  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5720  *
5721  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5722  **/
5723 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5724 {
5725 	struct i40e_pf *pf = vsi->back;
5726 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5727 	struct i40e_hw *hw = &pf->hw;
5728 	int local_rss_size;
5729 	u8 *lut;
5730 	int ret;
5731 
5732 	if (!vsi->rss_size)
5733 		return -EINVAL;
5734 
5735 	if (rss_size > vsi->rss_size)
5736 		return -EINVAL;
5737 
5738 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5739 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5740 	if (!lut)
5741 		return -ENOMEM;
5742 
5743 	/* Ignoring user configured lut if there is one */
5744 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5745 
5746 	/* Use user configured hash key if there is one, otherwise
5747 	 * use default.
5748 	 */
5749 	if (vsi->rss_hkey_user)
5750 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5751 	else
5752 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5753 
5754 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5755 	if (ret) {
5756 		dev_info(&pf->pdev->dev,
5757 			 "Cannot set RSS lut, err %s aq_err %s\n",
5758 			 i40e_stat_str(hw, ret),
5759 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5760 		kfree(lut);
5761 		return ret;
5762 	}
5763 	kfree(lut);
5764 
5765 	/* Do the update w.r.t. storing rss_size */
5766 	if (!vsi->orig_rss_size)
5767 		vsi->orig_rss_size = vsi->rss_size;
5768 	vsi->current_rss_size = local_rss_size;
5769 
5770 	return ret;
5771 }
5772 
5773 /**
5774  * i40e_channel_setup_queue_map - Setup a channel queue map
5775  * @pf: ptr to PF device
5776  * @vsi: the VSI being setup
5777  * @ctxt: VSI context structure
5778  * @ch: ptr to channel structure
5779  *
5780  * Setup queue map for a specific channel
5781  **/
5782 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5783 					 struct i40e_vsi_context *ctxt,
5784 					 struct i40e_channel *ch)
5785 {
5786 	u16 qcount, qmap, sections = 0;
5787 	u8 offset = 0;
5788 	int pow;
5789 
5790 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5791 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5792 
5793 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5794 	ch->num_queue_pairs = qcount;
5795 
5796 	/* find the next higher power-of-2 of num queue pairs */
5797 	pow = ilog2(qcount);
5798 	if (!is_power_of_2(qcount))
5799 		pow++;
5800 
5801 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5802 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5803 
5804 	/* Setup queue TC[0].qmap for given VSI context */
5805 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5806 
5807 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5808 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5809 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5810 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5811 }
5812 
5813 /**
5814  * i40e_add_channel - add a channel by adding VSI
5815  * @pf: ptr to PF device
5816  * @uplink_seid: underlying HW switching element (VEB) ID
5817  * @ch: ptr to channel structure
5818  *
5819  * Add a channel (VSI) using add_vsi and queue_map
5820  **/
5821 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5822 			    struct i40e_channel *ch)
5823 {
5824 	struct i40e_hw *hw = &pf->hw;
5825 	struct i40e_vsi_context ctxt;
5826 	u8 enabled_tc = 0x1; /* TC0 enabled */
5827 	int ret;
5828 
5829 	if (ch->type != I40E_VSI_VMDQ2) {
5830 		dev_info(&pf->pdev->dev,
5831 			 "add new vsi failed, ch->type %d\n", ch->type);
5832 		return -EINVAL;
5833 	}
5834 
5835 	memset(&ctxt, 0, sizeof(ctxt));
5836 	ctxt.pf_num = hw->pf_id;
5837 	ctxt.vf_num = 0;
5838 	ctxt.uplink_seid = uplink_seid;
5839 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5840 	if (ch->type == I40E_VSI_VMDQ2)
5841 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5842 
5843 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5844 		ctxt.info.valid_sections |=
5845 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5846 		ctxt.info.switch_id =
5847 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5848 	}
5849 
5850 	/* Set queue map for a given VSI context */
5851 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5852 
5853 	/* Now time to create VSI */
5854 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5855 	if (ret) {
5856 		dev_info(&pf->pdev->dev,
5857 			 "add new vsi failed, err %s aq_err %s\n",
5858 			 i40e_stat_str(&pf->hw, ret),
5859 			 i40e_aq_str(&pf->hw,
5860 				     pf->hw.aq.asq_last_status));
5861 		return -ENOENT;
5862 	}
5863 
5864 	/* Success, update channel, set enabled_tc only if the channel
5865 	 * is not a macvlan
5866 	 */
5867 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
5868 	ch->seid = ctxt.seid;
5869 	ch->vsi_number = ctxt.vsi_number;
5870 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5871 
5872 	/* copy just the sections touched not the entire info
5873 	 * since not all sections are valid as returned by
5874 	 * update vsi params
5875 	 */
5876 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5877 	memcpy(&ch->info.queue_mapping,
5878 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5879 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5880 	       sizeof(ctxt.info.tc_mapping));
5881 
5882 	return 0;
5883 }
5884 
5885 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5886 				  u8 *bw_share)
5887 {
5888 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5889 	i40e_status ret;
5890 	int i;
5891 
5892 	bw_data.tc_valid_bits = ch->enabled_tc;
5893 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5894 		bw_data.tc_bw_credits[i] = bw_share[i];
5895 
5896 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5897 				       &bw_data, NULL);
5898 	if (ret) {
5899 		dev_info(&vsi->back->pdev->dev,
5900 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5901 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5902 		return -EINVAL;
5903 	}
5904 
5905 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5906 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5907 
5908 	return 0;
5909 }
5910 
5911 /**
5912  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5913  * @pf: ptr to PF device
5914  * @vsi: the VSI being setup
5915  * @ch: ptr to channel structure
5916  *
5917  * Configure TX rings associated with channel (VSI) since queues are being
5918  * from parent VSI.
5919  **/
5920 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5921 				       struct i40e_vsi *vsi,
5922 				       struct i40e_channel *ch)
5923 {
5924 	i40e_status ret;
5925 	int i;
5926 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5927 
5928 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5929 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5930 		if (ch->enabled_tc & BIT(i))
5931 			bw_share[i] = 1;
5932 	}
5933 
5934 	/* configure BW for new VSI */
5935 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5936 	if (ret) {
5937 		dev_info(&vsi->back->pdev->dev,
5938 			 "Failed configuring TC map %d for channel (seid %u)\n",
5939 			 ch->enabled_tc, ch->seid);
5940 		return ret;
5941 	}
5942 
5943 	for (i = 0; i < ch->num_queue_pairs; i++) {
5944 		struct i40e_ring *tx_ring, *rx_ring;
5945 		u16 pf_q;
5946 
5947 		pf_q = ch->base_queue + i;
5948 
5949 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5950 		 * context
5951 		 */
5952 		tx_ring = vsi->tx_rings[pf_q];
5953 		tx_ring->ch = ch;
5954 
5955 		/* Get the RX ring ptr */
5956 		rx_ring = vsi->rx_rings[pf_q];
5957 		rx_ring->ch = ch;
5958 	}
5959 
5960 	return 0;
5961 }
5962 
5963 /**
5964  * i40e_setup_hw_channel - setup new channel
5965  * @pf: ptr to PF device
5966  * @vsi: the VSI being setup
5967  * @ch: ptr to channel structure
5968  * @uplink_seid: underlying HW switching element (VEB) ID
5969  * @type: type of channel to be created (VMDq2/VF)
5970  *
5971  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5972  * and configures TX rings accordingly
5973  **/
5974 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5975 					struct i40e_vsi *vsi,
5976 					struct i40e_channel *ch,
5977 					u16 uplink_seid, u8 type)
5978 {
5979 	int ret;
5980 
5981 	ch->initialized = false;
5982 	ch->base_queue = vsi->next_base_queue;
5983 	ch->type = type;
5984 
5985 	/* Proceed with creation of channel (VMDq2) VSI */
5986 	ret = i40e_add_channel(pf, uplink_seid, ch);
5987 	if (ret) {
5988 		dev_info(&pf->pdev->dev,
5989 			 "failed to add_channel using uplink_seid %u\n",
5990 			 uplink_seid);
5991 		return ret;
5992 	}
5993 
5994 	/* Mark the successful creation of channel */
5995 	ch->initialized = true;
5996 
5997 	/* Reconfigure TX queues using QTX_CTL register */
5998 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5999 	if (ret) {
6000 		dev_info(&pf->pdev->dev,
6001 			 "failed to configure TX rings for channel %u\n",
6002 			 ch->seid);
6003 		return ret;
6004 	}
6005 
6006 	/* update 'next_base_queue' */
6007 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6008 	dev_dbg(&pf->pdev->dev,
6009 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6010 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6011 		ch->num_queue_pairs,
6012 		vsi->next_base_queue);
6013 	return ret;
6014 }
6015 
6016 /**
6017  * i40e_setup_channel - setup new channel using uplink element
6018  * @pf: ptr to PF device
6019  * @type: type of channel to be created (VMDq2/VF)
6020  * @uplink_seid: underlying HW switching element (VEB) ID
6021  * @ch: ptr to channel structure
6022  *
6023  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6024  * and uplink switching element (uplink_seid)
6025  **/
6026 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6027 			       struct i40e_channel *ch)
6028 {
6029 	u8 vsi_type;
6030 	u16 seid;
6031 	int ret;
6032 
6033 	if (vsi->type == I40E_VSI_MAIN) {
6034 		vsi_type = I40E_VSI_VMDQ2;
6035 	} else {
6036 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6037 			vsi->type);
6038 		return false;
6039 	}
6040 
6041 	/* underlying switching element */
6042 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6043 
6044 	/* create channel (VSI), configure TX rings */
6045 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6046 	if (ret) {
6047 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6048 		return false;
6049 	}
6050 
6051 	return ch->initialized ? true : false;
6052 }
6053 
6054 /**
6055  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6056  * @vsi: ptr to VSI which has PF backing
6057  *
6058  * Sets up switch mode correctly if it needs to be changed and perform
6059  * what are allowed modes.
6060  **/
6061 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6062 {
6063 	u8 mode;
6064 	struct i40e_pf *pf = vsi->back;
6065 	struct i40e_hw *hw = &pf->hw;
6066 	int ret;
6067 
6068 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6069 	if (ret)
6070 		return -EINVAL;
6071 
6072 	if (hw->dev_caps.switch_mode) {
6073 		/* if switch mode is set, support mode2 (non-tunneled for
6074 		 * cloud filter) for now
6075 		 */
6076 		u32 switch_mode = hw->dev_caps.switch_mode &
6077 				  I40E_SWITCH_MODE_MASK;
6078 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6079 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6080 				return 0;
6081 			dev_err(&pf->pdev->dev,
6082 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6083 				hw->dev_caps.switch_mode);
6084 			return -EINVAL;
6085 		}
6086 	}
6087 
6088 	/* Set Bit 7 to be valid */
6089 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6090 
6091 	/* Set L4type for TCP support */
6092 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6093 
6094 	/* Set cloud filter mode */
6095 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6096 
6097 	/* Prep mode field for set_switch_config */
6098 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6099 					pf->last_sw_conf_valid_flags,
6100 					mode, NULL);
6101 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6102 		dev_err(&pf->pdev->dev,
6103 			"couldn't set switch config bits, err %s aq_err %s\n",
6104 			i40e_stat_str(hw, ret),
6105 			i40e_aq_str(hw,
6106 				    hw->aq.asq_last_status));
6107 
6108 	return ret;
6109 }
6110 
6111 /**
6112  * i40e_create_queue_channel - function to create channel
6113  * @vsi: VSI to be configured
6114  * @ch: ptr to channel (it contains channel specific params)
6115  *
6116  * This function creates channel (VSI) using num_queues specified by user,
6117  * reconfigs RSS if needed.
6118  **/
6119 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6120 			      struct i40e_channel *ch)
6121 {
6122 	struct i40e_pf *pf = vsi->back;
6123 	bool reconfig_rss;
6124 	int err;
6125 
6126 	if (!ch)
6127 		return -EINVAL;
6128 
6129 	if (!ch->num_queue_pairs) {
6130 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6131 			ch->num_queue_pairs);
6132 		return -EINVAL;
6133 	}
6134 
6135 	/* validate user requested num_queues for channel */
6136 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6137 				       &reconfig_rss);
6138 	if (err) {
6139 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6140 			 ch->num_queue_pairs);
6141 		return -EINVAL;
6142 	}
6143 
6144 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6145 	 * VSI to be added switch to VEB mode.
6146 	 */
6147 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6148 	    (!i40e_is_any_channel(vsi))) {
6149 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6150 			dev_dbg(&pf->pdev->dev,
6151 				"Failed to create channel. Override queues (%u) not power of 2\n",
6152 				vsi->tc_config.tc_info[0].qcount);
6153 			return -EINVAL;
6154 		}
6155 
6156 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6157 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6158 
6159 			if (vsi->type == I40E_VSI_MAIN) {
6160 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6161 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6162 						      true);
6163 				else
6164 					i40e_do_reset_safe(pf,
6165 							   I40E_PF_RESET_FLAG);
6166 			}
6167 		}
6168 		/* now onwards for main VSI, number of queues will be value
6169 		 * of TC0's queue count
6170 		 */
6171 	}
6172 
6173 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6174 	 * it should be more than num_queues
6175 	 */
6176 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6177 		dev_dbg(&pf->pdev->dev,
6178 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6179 			vsi->cnt_q_avail, ch->num_queue_pairs);
6180 		return -EINVAL;
6181 	}
6182 
6183 	/* reconfig_rss only if vsi type is MAIN_VSI */
6184 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6185 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6186 		if (err) {
6187 			dev_info(&pf->pdev->dev,
6188 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6189 				 ch->num_queue_pairs);
6190 			return -EINVAL;
6191 		}
6192 	}
6193 
6194 	if (!i40e_setup_channel(pf, vsi, ch)) {
6195 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6196 		return -EINVAL;
6197 	}
6198 
6199 	dev_info(&pf->pdev->dev,
6200 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6201 		 ch->seid, ch->num_queue_pairs);
6202 
6203 	/* configure VSI for BW limit */
6204 	if (ch->max_tx_rate) {
6205 		u64 credits = ch->max_tx_rate;
6206 
6207 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6208 			return -EINVAL;
6209 
6210 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6211 		dev_dbg(&pf->pdev->dev,
6212 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6213 			ch->max_tx_rate,
6214 			credits,
6215 			ch->seid);
6216 	}
6217 
6218 	/* in case of VF, this will be main SRIOV VSI */
6219 	ch->parent_vsi = vsi;
6220 
6221 	/* and update main_vsi's count for queue_available to use */
6222 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6223 
6224 	return 0;
6225 }
6226 
6227 /**
6228  * i40e_configure_queue_channels - Add queue channel for the given TCs
6229  * @vsi: VSI to be configured
6230  *
6231  * Configures queue channel mapping to the given TCs
6232  **/
6233 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6234 {
6235 	struct i40e_channel *ch;
6236 	u64 max_rate = 0;
6237 	int ret = 0, i;
6238 
6239 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6240 	vsi->tc_seid_map[0] = vsi->seid;
6241 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6242 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6243 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6244 			if (!ch) {
6245 				ret = -ENOMEM;
6246 				goto err_free;
6247 			}
6248 
6249 			INIT_LIST_HEAD(&ch->list);
6250 			ch->num_queue_pairs =
6251 				vsi->tc_config.tc_info[i].qcount;
6252 			ch->base_queue =
6253 				vsi->tc_config.tc_info[i].qoffset;
6254 
6255 			/* Bandwidth limit through tc interface is in bytes/s,
6256 			 * change to Mbit/s
6257 			 */
6258 			max_rate = vsi->mqprio_qopt.max_rate[i];
6259 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6260 			ch->max_tx_rate = max_rate;
6261 
6262 			list_add_tail(&ch->list, &vsi->ch_list);
6263 
6264 			ret = i40e_create_queue_channel(vsi, ch);
6265 			if (ret) {
6266 				dev_err(&vsi->back->pdev->dev,
6267 					"Failed creating queue channel with TC%d: queues %d\n",
6268 					i, ch->num_queue_pairs);
6269 				goto err_free;
6270 			}
6271 			vsi->tc_seid_map[i] = ch->seid;
6272 		}
6273 	}
6274 	return ret;
6275 
6276 err_free:
6277 	i40e_remove_queue_channels(vsi);
6278 	return ret;
6279 }
6280 
6281 /**
6282  * i40e_veb_config_tc - Configure TCs for given VEB
6283  * @veb: given VEB
6284  * @enabled_tc: TC bitmap
6285  *
6286  * Configures given TC bitmap for VEB (switching) element
6287  **/
6288 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6289 {
6290 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6291 	struct i40e_pf *pf = veb->pf;
6292 	int ret = 0;
6293 	int i;
6294 
6295 	/* No TCs or already enabled TCs just return */
6296 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6297 		return ret;
6298 
6299 	bw_data.tc_valid_bits = enabled_tc;
6300 	/* bw_data.absolute_credits is not set (relative) */
6301 
6302 	/* Enable ETS TCs with equal BW Share for now */
6303 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6304 		if (enabled_tc & BIT(i))
6305 			bw_data.tc_bw_share_credits[i] = 1;
6306 	}
6307 
6308 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6309 						   &bw_data, NULL);
6310 	if (ret) {
6311 		dev_info(&pf->pdev->dev,
6312 			 "VEB bw config failed, err %s aq_err %s\n",
6313 			 i40e_stat_str(&pf->hw, ret),
6314 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6315 		goto out;
6316 	}
6317 
6318 	/* Update the BW information */
6319 	ret = i40e_veb_get_bw_info(veb);
6320 	if (ret) {
6321 		dev_info(&pf->pdev->dev,
6322 			 "Failed getting veb bw config, err %s aq_err %s\n",
6323 			 i40e_stat_str(&pf->hw, ret),
6324 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6325 	}
6326 
6327 out:
6328 	return ret;
6329 }
6330 
6331 #ifdef CONFIG_I40E_DCB
6332 /**
6333  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6334  * @pf: PF struct
6335  *
6336  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6337  * the caller would've quiesce all the VSIs before calling
6338  * this function
6339  **/
6340 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6341 {
6342 	u8 tc_map = 0;
6343 	int ret;
6344 	u8 v;
6345 
6346 	/* Enable the TCs available on PF to all VEBs */
6347 	tc_map = i40e_pf_get_tc_map(pf);
6348 	for (v = 0; v < I40E_MAX_VEB; v++) {
6349 		if (!pf->veb[v])
6350 			continue;
6351 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6352 		if (ret) {
6353 			dev_info(&pf->pdev->dev,
6354 				 "Failed configuring TC for VEB seid=%d\n",
6355 				 pf->veb[v]->seid);
6356 			/* Will try to configure as many components */
6357 		}
6358 	}
6359 
6360 	/* Update each VSI */
6361 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6362 		if (!pf->vsi[v])
6363 			continue;
6364 
6365 		/* - Enable all TCs for the LAN VSI
6366 		 * - For all others keep them at TC0 for now
6367 		 */
6368 		if (v == pf->lan_vsi)
6369 			tc_map = i40e_pf_get_tc_map(pf);
6370 		else
6371 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6372 
6373 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6374 		if (ret) {
6375 			dev_info(&pf->pdev->dev,
6376 				 "Failed configuring TC for VSI seid=%d\n",
6377 				 pf->vsi[v]->seid);
6378 			/* Will try to configure as many components */
6379 		} else {
6380 			/* Re-configure VSI vectors based on updated TC map */
6381 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6382 			if (pf->vsi[v]->netdev)
6383 				i40e_dcbnl_set_all(pf->vsi[v]);
6384 		}
6385 	}
6386 }
6387 
6388 /**
6389  * i40e_resume_port_tx - Resume port Tx
6390  * @pf: PF struct
6391  *
6392  * Resume a port's Tx and issue a PF reset in case of failure to
6393  * resume.
6394  **/
6395 static int i40e_resume_port_tx(struct i40e_pf *pf)
6396 {
6397 	struct i40e_hw *hw = &pf->hw;
6398 	int ret;
6399 
6400 	ret = i40e_aq_resume_port_tx(hw, NULL);
6401 	if (ret) {
6402 		dev_info(&pf->pdev->dev,
6403 			 "Resume Port Tx failed, err %s aq_err %s\n",
6404 			  i40e_stat_str(&pf->hw, ret),
6405 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6406 		/* Schedule PF reset to recover */
6407 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6408 		i40e_service_event_schedule(pf);
6409 	}
6410 
6411 	return ret;
6412 }
6413 
6414 /**
6415  * i40e_update_dcb_config
6416  * @hw: pointer to the HW struct
6417  * @enable_mib_change: enable MIB change event
6418  *
6419  * Update DCB configuration from the firmware
6420  **/
6421 static enum i40e_status_code
6422 i40e_update_dcb_config(struct i40e_hw *hw, bool enable_mib_change)
6423 {
6424 	struct i40e_lldp_variables lldp_cfg;
6425 	i40e_status ret;
6426 
6427 	if (!hw->func_caps.dcb)
6428 		return I40E_NOT_SUPPORTED;
6429 
6430 	/* Read LLDP NVM area */
6431 	ret = i40e_read_lldp_cfg(hw, &lldp_cfg);
6432 	if (ret)
6433 		return I40E_ERR_NOT_READY;
6434 
6435 	/* Get DCBX status */
6436 	ret = i40e_get_dcbx_status(hw, &hw->dcbx_status);
6437 	if (ret)
6438 		return ret;
6439 
6440 	/* Check the DCBX Status */
6441 	if (hw->dcbx_status == I40E_DCBX_STATUS_DONE ||
6442 	    hw->dcbx_status == I40E_DCBX_STATUS_IN_PROGRESS) {
6443 		/* Get current DCBX configuration */
6444 		ret = i40e_get_dcb_config(hw);
6445 		if (ret)
6446 			return ret;
6447 	} else if (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED) {
6448 		return I40E_ERR_NOT_READY;
6449 	}
6450 
6451 	/* Configure the LLDP MIB change event */
6452 	if (enable_mib_change)
6453 		ret = i40e_aq_cfg_lldp_mib_change_event(hw, true, NULL);
6454 
6455 	return ret;
6456 }
6457 
6458 /**
6459  * i40e_init_pf_dcb - Initialize DCB configuration
6460  * @pf: PF being configured
6461  *
6462  * Query the current DCB configuration and cache it
6463  * in the hardware structure
6464  **/
6465 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6466 {
6467 	struct i40e_hw *hw = &pf->hw;
6468 	int err = 0;
6469 
6470 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6471 	 * Also do not enable DCBx if FW LLDP agent is disabled
6472 	 */
6473 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6474 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
6475 		dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
6476 		err = I40E_NOT_SUPPORTED;
6477 		goto out;
6478 	}
6479 
6480 	err = i40e_update_dcb_config(hw, true);
6481 	if (!err) {
6482 		/* Device/Function is not DCBX capable */
6483 		if ((!hw->func_caps.dcb) ||
6484 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6485 			dev_info(&pf->pdev->dev,
6486 				 "DCBX offload is not supported or is disabled for this PF.\n");
6487 		} else {
6488 			/* When status is not DISABLED then DCBX in FW */
6489 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6490 				       DCB_CAP_DCBX_VER_IEEE;
6491 
6492 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6493 			/* Enable DCB tagging only when more than one TC
6494 			 * or explicitly disable if only one TC
6495 			 */
6496 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6497 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6498 			else
6499 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6500 			dev_dbg(&pf->pdev->dev,
6501 				"DCBX offload is supported for this PF.\n");
6502 		}
6503 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6504 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6505 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6506 	} else {
6507 		dev_info(&pf->pdev->dev,
6508 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6509 			 i40e_stat_str(&pf->hw, err),
6510 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6511 	}
6512 
6513 out:
6514 	return err;
6515 }
6516 #endif /* CONFIG_I40E_DCB */
6517 #define SPEED_SIZE 14
6518 #define FC_SIZE 8
6519 /**
6520  * i40e_print_link_message - print link up or down
6521  * @vsi: the VSI for which link needs a message
6522  * @isup: true of link is up, false otherwise
6523  */
6524 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6525 {
6526 	enum i40e_aq_link_speed new_speed;
6527 	struct i40e_pf *pf = vsi->back;
6528 	char *speed = "Unknown";
6529 	char *fc = "Unknown";
6530 	char *fec = "";
6531 	char *req_fec = "";
6532 	char *an = "";
6533 
6534 	if (isup)
6535 		new_speed = pf->hw.phy.link_info.link_speed;
6536 	else
6537 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6538 
6539 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6540 		return;
6541 	vsi->current_isup = isup;
6542 	vsi->current_speed = new_speed;
6543 	if (!isup) {
6544 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6545 		return;
6546 	}
6547 
6548 	/* Warn user if link speed on NPAR enabled partition is not at
6549 	 * least 10GB
6550 	 */
6551 	if (pf->hw.func_caps.npar_enable &&
6552 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6553 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6554 		netdev_warn(vsi->netdev,
6555 			    "The partition detected link speed that is less than 10Gbps\n");
6556 
6557 	switch (pf->hw.phy.link_info.link_speed) {
6558 	case I40E_LINK_SPEED_40GB:
6559 		speed = "40 G";
6560 		break;
6561 	case I40E_LINK_SPEED_20GB:
6562 		speed = "20 G";
6563 		break;
6564 	case I40E_LINK_SPEED_25GB:
6565 		speed = "25 G";
6566 		break;
6567 	case I40E_LINK_SPEED_10GB:
6568 		speed = "10 G";
6569 		break;
6570 	case I40E_LINK_SPEED_5GB:
6571 		speed = "5 G";
6572 		break;
6573 	case I40E_LINK_SPEED_2_5GB:
6574 		speed = "2.5 G";
6575 		break;
6576 	case I40E_LINK_SPEED_1GB:
6577 		speed = "1000 M";
6578 		break;
6579 	case I40E_LINK_SPEED_100MB:
6580 		speed = "100 M";
6581 		break;
6582 	default:
6583 		break;
6584 	}
6585 
6586 	switch (pf->hw.fc.current_mode) {
6587 	case I40E_FC_FULL:
6588 		fc = "RX/TX";
6589 		break;
6590 	case I40E_FC_TX_PAUSE:
6591 		fc = "TX";
6592 		break;
6593 	case I40E_FC_RX_PAUSE:
6594 		fc = "RX";
6595 		break;
6596 	default:
6597 		fc = "None";
6598 		break;
6599 	}
6600 
6601 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6602 		req_fec = ", Requested FEC: None";
6603 		fec = ", FEC: None";
6604 		an = ", Autoneg: False";
6605 
6606 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6607 			an = ", Autoneg: True";
6608 
6609 		if (pf->hw.phy.link_info.fec_info &
6610 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6611 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6612 		else if (pf->hw.phy.link_info.fec_info &
6613 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6614 			fec = ", FEC: CL108 RS-FEC";
6615 
6616 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6617 		 * both RS and FC are requested
6618 		 */
6619 		if (vsi->back->hw.phy.link_info.req_fec_info &
6620 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6621 			if (vsi->back->hw.phy.link_info.req_fec_info &
6622 			    I40E_AQ_REQUEST_FEC_RS)
6623 				req_fec = ", Requested FEC: CL108 RS-FEC";
6624 			else
6625 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6626 		}
6627 	}
6628 
6629 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6630 		    speed, req_fec, fec, an, fc);
6631 }
6632 
6633 /**
6634  * i40e_up_complete - Finish the last steps of bringing up a connection
6635  * @vsi: the VSI being configured
6636  **/
6637 static int i40e_up_complete(struct i40e_vsi *vsi)
6638 {
6639 	struct i40e_pf *pf = vsi->back;
6640 	int err;
6641 
6642 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6643 		i40e_vsi_configure_msix(vsi);
6644 	else
6645 		i40e_configure_msi_and_legacy(vsi);
6646 
6647 	/* start rings */
6648 	err = i40e_vsi_start_rings(vsi);
6649 	if (err)
6650 		return err;
6651 
6652 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6653 	i40e_napi_enable_all(vsi);
6654 	i40e_vsi_enable_irq(vsi);
6655 
6656 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6657 	    (vsi->netdev)) {
6658 		i40e_print_link_message(vsi, true);
6659 		netif_tx_start_all_queues(vsi->netdev);
6660 		netif_carrier_on(vsi->netdev);
6661 	}
6662 
6663 	/* replay FDIR SB filters */
6664 	if (vsi->type == I40E_VSI_FDIR) {
6665 		/* reset fd counters */
6666 		pf->fd_add_err = 0;
6667 		pf->fd_atr_cnt = 0;
6668 		i40e_fdir_filter_restore(vsi);
6669 	}
6670 
6671 	/* On the next run of the service_task, notify any clients of the new
6672 	 * opened netdev
6673 	 */
6674 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6675 	i40e_service_event_schedule(pf);
6676 
6677 	return 0;
6678 }
6679 
6680 /**
6681  * i40e_vsi_reinit_locked - Reset the VSI
6682  * @vsi: the VSI being configured
6683  *
6684  * Rebuild the ring structs after some configuration
6685  * has changed, e.g. MTU size.
6686  **/
6687 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6688 {
6689 	struct i40e_pf *pf = vsi->back;
6690 
6691 	WARN_ON(in_interrupt());
6692 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6693 		usleep_range(1000, 2000);
6694 	i40e_down(vsi);
6695 
6696 	i40e_up(vsi);
6697 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6698 }
6699 
6700 /**
6701  * i40e_up - Bring the connection back up after being down
6702  * @vsi: the VSI being configured
6703  **/
6704 int i40e_up(struct i40e_vsi *vsi)
6705 {
6706 	int err;
6707 
6708 	err = i40e_vsi_configure(vsi);
6709 	if (!err)
6710 		err = i40e_up_complete(vsi);
6711 
6712 	return err;
6713 }
6714 
6715 /**
6716  * i40e_force_link_state - Force the link status
6717  * @pf: board private structure
6718  * @is_up: whether the link state should be forced up or down
6719  **/
6720 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6721 {
6722 	struct i40e_aq_get_phy_abilities_resp abilities;
6723 	struct i40e_aq_set_phy_config config = {0};
6724 	struct i40e_hw *hw = &pf->hw;
6725 	i40e_status err;
6726 	u64 mask;
6727 	u8 speed;
6728 
6729 	/* Card might've been put in an unstable state by other drivers
6730 	 * and applications, which causes incorrect speed values being
6731 	 * set on startup. In order to clear speed registers, we call
6732 	 * get_phy_capabilities twice, once to get initial state of
6733 	 * available speeds, and once to get current PHY config.
6734 	 */
6735 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6736 					   NULL);
6737 	if (err) {
6738 		dev_err(&pf->pdev->dev,
6739 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6740 			i40e_stat_str(hw, err),
6741 			i40e_aq_str(hw, hw->aq.asq_last_status));
6742 		return err;
6743 	}
6744 	speed = abilities.link_speed;
6745 
6746 	/* Get the current phy config */
6747 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6748 					   NULL);
6749 	if (err) {
6750 		dev_err(&pf->pdev->dev,
6751 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6752 			i40e_stat_str(hw, err),
6753 			i40e_aq_str(hw, hw->aq.asq_last_status));
6754 		return err;
6755 	}
6756 
6757 	/* If link needs to go up, but was not forced to go down,
6758 	 * and its speed values are OK, no need for a flap
6759 	 */
6760 	if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6761 		return I40E_SUCCESS;
6762 
6763 	/* To force link we need to set bits for all supported PHY types,
6764 	 * but there are now more than 32, so we need to split the bitmap
6765 	 * across two fields.
6766 	 */
6767 	mask = I40E_PHY_TYPES_BITMASK;
6768 	config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6769 	config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6770 	/* Copy the old settings, except of phy_type */
6771 	config.abilities = abilities.abilities;
6772 	if (abilities.link_speed != 0)
6773 		config.link_speed = abilities.link_speed;
6774 	else
6775 		config.link_speed = speed;
6776 	config.eee_capability = abilities.eee_capability;
6777 	config.eeer = abilities.eeer_val;
6778 	config.low_power_ctrl = abilities.d3_lpan;
6779 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6780 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6781 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6782 
6783 	if (err) {
6784 		dev_err(&pf->pdev->dev,
6785 			"set phy config ret =  %s last_status =  %s\n",
6786 			i40e_stat_str(&pf->hw, err),
6787 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6788 		return err;
6789 	}
6790 
6791 	/* Update the link info */
6792 	err = i40e_update_link_info(hw);
6793 	if (err) {
6794 		/* Wait a little bit (on 40G cards it sometimes takes a really
6795 		 * long time for link to come back from the atomic reset)
6796 		 * and try once more
6797 		 */
6798 		msleep(1000);
6799 		i40e_update_link_info(hw);
6800 	}
6801 
6802 	i40e_aq_set_link_restart_an(hw, true, NULL);
6803 
6804 	return I40E_SUCCESS;
6805 }
6806 
6807 /**
6808  * i40e_down - Shutdown the connection processing
6809  * @vsi: the VSI being stopped
6810  **/
6811 void i40e_down(struct i40e_vsi *vsi)
6812 {
6813 	int i;
6814 
6815 	/* It is assumed that the caller of this function
6816 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6817 	 */
6818 	if (vsi->netdev) {
6819 		netif_carrier_off(vsi->netdev);
6820 		netif_tx_disable(vsi->netdev);
6821 	}
6822 	i40e_vsi_disable_irq(vsi);
6823 	i40e_vsi_stop_rings(vsi);
6824 	if (vsi->type == I40E_VSI_MAIN &&
6825 	    vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6826 		i40e_force_link_state(vsi->back, false);
6827 	i40e_napi_disable_all(vsi);
6828 
6829 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6830 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6831 		if (i40e_enabled_xdp_vsi(vsi)) {
6832 			/* Make sure that in-progress ndo_xdp_xmit
6833 			 * calls are completed.
6834 			 */
6835 			synchronize_rcu();
6836 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6837 		}
6838 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6839 	}
6840 
6841 }
6842 
6843 /**
6844  * i40e_validate_mqprio_qopt- validate queue mapping info
6845  * @vsi: the VSI being configured
6846  * @mqprio_qopt: queue parametrs
6847  **/
6848 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6849 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6850 {
6851 	u64 sum_max_rate = 0;
6852 	u64 max_rate = 0;
6853 	int i;
6854 
6855 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6856 	    mqprio_qopt->qopt.num_tc < 1 ||
6857 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6858 		return -EINVAL;
6859 	for (i = 0; ; i++) {
6860 		if (!mqprio_qopt->qopt.count[i])
6861 			return -EINVAL;
6862 		if (mqprio_qopt->min_rate[i]) {
6863 			dev_err(&vsi->back->pdev->dev,
6864 				"Invalid min tx rate (greater than 0) specified\n");
6865 			return -EINVAL;
6866 		}
6867 		max_rate = mqprio_qopt->max_rate[i];
6868 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6869 		sum_max_rate += max_rate;
6870 
6871 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6872 			break;
6873 		if (mqprio_qopt->qopt.offset[i + 1] !=
6874 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6875 			return -EINVAL;
6876 	}
6877 	if (vsi->num_queue_pairs <
6878 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6879 		return -EINVAL;
6880 	}
6881 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6882 		dev_err(&vsi->back->pdev->dev,
6883 			"Invalid max tx rate specified\n");
6884 		return -EINVAL;
6885 	}
6886 	return 0;
6887 }
6888 
6889 /**
6890  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6891  * @vsi: the VSI being configured
6892  **/
6893 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6894 {
6895 	u16 qcount;
6896 	int i;
6897 
6898 	/* Only TC0 is enabled */
6899 	vsi->tc_config.numtc = 1;
6900 	vsi->tc_config.enabled_tc = 1;
6901 	qcount = min_t(int, vsi->alloc_queue_pairs,
6902 		       i40e_pf_get_max_q_per_tc(vsi->back));
6903 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6904 		/* For the TC that is not enabled set the offset to to default
6905 		 * queue and allocate one queue for the given TC.
6906 		 */
6907 		vsi->tc_config.tc_info[i].qoffset = 0;
6908 		if (i == 0)
6909 			vsi->tc_config.tc_info[i].qcount = qcount;
6910 		else
6911 			vsi->tc_config.tc_info[i].qcount = 1;
6912 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6913 	}
6914 }
6915 
6916 /**
6917  * i40e_del_macvlan_filter
6918  * @hw: pointer to the HW structure
6919  * @seid: seid of the channel VSI
6920  * @macaddr: the mac address to apply as a filter
6921  * @aq_err: store the admin Q error
6922  *
6923  * This function deletes a mac filter on the channel VSI which serves as the
6924  * macvlan. Returns 0 on success.
6925  **/
6926 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
6927 					   const u8 *macaddr, int *aq_err)
6928 {
6929 	struct i40e_aqc_remove_macvlan_element_data element;
6930 	i40e_status status;
6931 
6932 	memset(&element, 0, sizeof(element));
6933 	ether_addr_copy(element.mac_addr, macaddr);
6934 	element.vlan_tag = 0;
6935 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
6936 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
6937 	*aq_err = hw->aq.asq_last_status;
6938 
6939 	return status;
6940 }
6941 
6942 /**
6943  * i40e_add_macvlan_filter
6944  * @hw: pointer to the HW structure
6945  * @seid: seid of the channel VSI
6946  * @macaddr: the mac address to apply as a filter
6947  * @aq_err: store the admin Q error
6948  *
6949  * This function adds a mac filter on the channel VSI which serves as the
6950  * macvlan. Returns 0 on success.
6951  **/
6952 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
6953 					   const u8 *macaddr, int *aq_err)
6954 {
6955 	struct i40e_aqc_add_macvlan_element_data element;
6956 	i40e_status status;
6957 	u16 cmd_flags = 0;
6958 
6959 	ether_addr_copy(element.mac_addr, macaddr);
6960 	element.vlan_tag = 0;
6961 	element.queue_number = 0;
6962 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
6963 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
6964 	element.flags = cpu_to_le16(cmd_flags);
6965 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
6966 	*aq_err = hw->aq.asq_last_status;
6967 
6968 	return status;
6969 }
6970 
6971 /**
6972  * i40e_reset_ch_rings - Reset the queue contexts in a channel
6973  * @vsi: the VSI we want to access
6974  * @ch: the channel we want to access
6975  */
6976 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
6977 {
6978 	struct i40e_ring *tx_ring, *rx_ring;
6979 	u16 pf_q;
6980 	int i;
6981 
6982 	for (i = 0; i < ch->num_queue_pairs; i++) {
6983 		pf_q = ch->base_queue + i;
6984 		tx_ring = vsi->tx_rings[pf_q];
6985 		tx_ring->ch = NULL;
6986 		rx_ring = vsi->rx_rings[pf_q];
6987 		rx_ring->ch = NULL;
6988 	}
6989 }
6990 
6991 /**
6992  * i40e_free_macvlan_channels
6993  * @vsi: the VSI we want to access
6994  *
6995  * This function frees the Qs of the channel VSI from
6996  * the stack and also deletes the channel VSIs which
6997  * serve as macvlans.
6998  */
6999 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7000 {
7001 	struct i40e_channel *ch, *ch_tmp;
7002 	int ret;
7003 
7004 	if (list_empty(&vsi->macvlan_list))
7005 		return;
7006 
7007 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7008 		struct i40e_vsi *parent_vsi;
7009 
7010 		if (i40e_is_channel_macvlan(ch)) {
7011 			i40e_reset_ch_rings(vsi, ch);
7012 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7013 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7014 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7015 			kfree(ch->fwd);
7016 			ch->fwd = NULL;
7017 		}
7018 
7019 		list_del(&ch->list);
7020 		parent_vsi = ch->parent_vsi;
7021 		if (!parent_vsi || !ch->initialized) {
7022 			kfree(ch);
7023 			continue;
7024 		}
7025 
7026 		/* remove the VSI */
7027 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7028 					     NULL);
7029 		if (ret)
7030 			dev_err(&vsi->back->pdev->dev,
7031 				"unable to remove channel (%d) for parent VSI(%d)\n",
7032 				ch->seid, parent_vsi->seid);
7033 		kfree(ch);
7034 	}
7035 	vsi->macvlan_cnt = 0;
7036 }
7037 
7038 /**
7039  * i40e_fwd_ring_up - bring the macvlan device up
7040  * @vsi: the VSI we want to access
7041  * @vdev: macvlan netdevice
7042  * @fwd: the private fwd structure
7043  */
7044 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7045 			    struct i40e_fwd_adapter *fwd)
7046 {
7047 	int ret = 0, num_tc = 1,  i, aq_err;
7048 	struct i40e_channel *ch, *ch_tmp;
7049 	struct i40e_pf *pf = vsi->back;
7050 	struct i40e_hw *hw = &pf->hw;
7051 
7052 	if (list_empty(&vsi->macvlan_list))
7053 		return -EINVAL;
7054 
7055 	/* Go through the list and find an available channel */
7056 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7057 		if (!i40e_is_channel_macvlan(ch)) {
7058 			ch->fwd = fwd;
7059 			/* record configuration for macvlan interface in vdev */
7060 			for (i = 0; i < num_tc; i++)
7061 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7062 							     i,
7063 							     ch->num_queue_pairs,
7064 							     ch->base_queue);
7065 			for (i = 0; i < ch->num_queue_pairs; i++) {
7066 				struct i40e_ring *tx_ring, *rx_ring;
7067 				u16 pf_q;
7068 
7069 				pf_q = ch->base_queue + i;
7070 
7071 				/* Get to TX ring ptr */
7072 				tx_ring = vsi->tx_rings[pf_q];
7073 				tx_ring->ch = ch;
7074 
7075 				/* Get the RX ring ptr */
7076 				rx_ring = vsi->rx_rings[pf_q];
7077 				rx_ring->ch = ch;
7078 			}
7079 			break;
7080 		}
7081 	}
7082 
7083 	/* Guarantee all rings are updated before we update the
7084 	 * MAC address filter.
7085 	 */
7086 	wmb();
7087 
7088 	/* Add a mac filter */
7089 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7090 	if (ret) {
7091 		/* if we cannot add the MAC rule then disable the offload */
7092 		macvlan_release_l2fw_offload(vdev);
7093 		for (i = 0; i < ch->num_queue_pairs; i++) {
7094 			struct i40e_ring *rx_ring;
7095 			u16 pf_q;
7096 
7097 			pf_q = ch->base_queue + i;
7098 			rx_ring = vsi->rx_rings[pf_q];
7099 			rx_ring->netdev = NULL;
7100 		}
7101 		dev_info(&pf->pdev->dev,
7102 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7103 			  i40e_stat_str(hw, ret),
7104 			  i40e_aq_str(hw, aq_err));
7105 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7106 	}
7107 
7108 	return ret;
7109 }
7110 
7111 /**
7112  * i40e_setup_macvlans - create the channels which will be macvlans
7113  * @vsi: the VSI we want to access
7114  * @macvlan_cnt: no. of macvlans to be setup
7115  * @qcnt: no. of Qs per macvlan
7116  * @vdev: macvlan netdevice
7117  */
7118 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7119 			       struct net_device *vdev)
7120 {
7121 	struct i40e_pf *pf = vsi->back;
7122 	struct i40e_hw *hw = &pf->hw;
7123 	struct i40e_vsi_context ctxt;
7124 	u16 sections, qmap, num_qps;
7125 	struct i40e_channel *ch;
7126 	int i, pow, ret = 0;
7127 	u8 offset = 0;
7128 
7129 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7130 		return -EINVAL;
7131 
7132 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7133 
7134 	/* find the next higher power-of-2 of num queue pairs */
7135 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7136 
7137 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7138 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7139 
7140 	/* Setup context bits for the main VSI */
7141 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7142 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7143 	memset(&ctxt, 0, sizeof(ctxt));
7144 	ctxt.seid = vsi->seid;
7145 	ctxt.pf_num = vsi->back->hw.pf_id;
7146 	ctxt.vf_num = 0;
7147 	ctxt.uplink_seid = vsi->uplink_seid;
7148 	ctxt.info = vsi->info;
7149 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7150 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7151 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7152 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7153 
7154 	/* Reconfigure RSS for main VSI with new max queue count */
7155 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7156 	ret = i40e_vsi_config_rss(vsi);
7157 	if (ret) {
7158 		dev_info(&pf->pdev->dev,
7159 			 "Failed to reconfig RSS for num_queues (%u)\n",
7160 			 vsi->rss_size);
7161 		return ret;
7162 	}
7163 	vsi->reconfig_rss = true;
7164 	dev_dbg(&vsi->back->pdev->dev,
7165 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7166 	vsi->next_base_queue = num_qps;
7167 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7168 
7169 	/* Update the VSI after updating the VSI queue-mapping
7170 	 * information
7171 	 */
7172 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7173 	if (ret) {
7174 		dev_info(&pf->pdev->dev,
7175 			 "Update vsi tc config failed, err %s aq_err %s\n",
7176 			 i40e_stat_str(hw, ret),
7177 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7178 		return ret;
7179 	}
7180 	/* update the local VSI info with updated queue map */
7181 	i40e_vsi_update_queue_map(vsi, &ctxt);
7182 	vsi->info.valid_sections = 0;
7183 
7184 	/* Create channels for macvlans */
7185 	INIT_LIST_HEAD(&vsi->macvlan_list);
7186 	for (i = 0; i < macvlan_cnt; i++) {
7187 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7188 		if (!ch) {
7189 			ret = -ENOMEM;
7190 			goto err_free;
7191 		}
7192 		INIT_LIST_HEAD(&ch->list);
7193 		ch->num_queue_pairs = qcnt;
7194 		if (!i40e_setup_channel(pf, vsi, ch)) {
7195 			ret = -EINVAL;
7196 			goto err_free;
7197 		}
7198 		ch->parent_vsi = vsi;
7199 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7200 		vsi->macvlan_cnt++;
7201 		list_add_tail(&ch->list, &vsi->macvlan_list);
7202 	}
7203 
7204 	return ret;
7205 
7206 err_free:
7207 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7208 	i40e_free_macvlan_channels(vsi);
7209 
7210 	return ret;
7211 }
7212 
7213 /**
7214  * i40e_fwd_add - configure macvlans
7215  * @netdev: net device to configure
7216  * @vdev: macvlan netdevice
7217  **/
7218 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7219 {
7220 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7221 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7222 	struct i40e_vsi *vsi = np->vsi;
7223 	struct i40e_pf *pf = vsi->back;
7224 	struct i40e_fwd_adapter *fwd;
7225 	int avail_macvlan, ret;
7226 
7227 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7228 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7229 		return ERR_PTR(-EINVAL);
7230 	}
7231 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7232 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7233 		return ERR_PTR(-EINVAL);
7234 	}
7235 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7236 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7237 		return ERR_PTR(-EINVAL);
7238 	}
7239 
7240 	/* The macvlan device has to be a single Q device so that the
7241 	 * tc_to_txq field can be reused to pick the tx queue.
7242 	 */
7243 	if (netif_is_multiqueue(vdev))
7244 		return ERR_PTR(-ERANGE);
7245 
7246 	if (!vsi->macvlan_cnt) {
7247 		/* reserve bit 0 for the pf device */
7248 		set_bit(0, vsi->fwd_bitmask);
7249 
7250 		/* Try to reserve as many queues as possible for macvlans. First
7251 		 * reserve 3/4th of max vectors, then half, then quarter and
7252 		 * calculate Qs per macvlan as you go
7253 		 */
7254 		vectors = pf->num_lan_msix;
7255 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7256 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7257 			q_per_macvlan = 4;
7258 			macvlan_cnt = (vectors - 32) / 4;
7259 		} else if (vectors <= 64 && vectors > 32) {
7260 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7261 			q_per_macvlan = 2;
7262 			macvlan_cnt = (vectors - 16) / 2;
7263 		} else if (vectors <= 32 && vectors > 16) {
7264 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7265 			q_per_macvlan = 1;
7266 			macvlan_cnt = vectors - 16;
7267 		} else if (vectors <= 16 && vectors > 8) {
7268 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7269 			q_per_macvlan = 1;
7270 			macvlan_cnt = vectors - 8;
7271 		} else {
7272 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7273 			q_per_macvlan = 1;
7274 			macvlan_cnt = vectors - 1;
7275 		}
7276 
7277 		if (macvlan_cnt == 0)
7278 			return ERR_PTR(-EBUSY);
7279 
7280 		/* Quiesce VSI queues */
7281 		i40e_quiesce_vsi(vsi);
7282 
7283 		/* sets up the macvlans but does not "enable" them */
7284 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7285 					  vdev);
7286 		if (ret)
7287 			return ERR_PTR(ret);
7288 
7289 		/* Unquiesce VSI */
7290 		i40e_unquiesce_vsi(vsi);
7291 	}
7292 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7293 					    vsi->macvlan_cnt);
7294 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7295 		return ERR_PTR(-EBUSY);
7296 
7297 	/* create the fwd struct */
7298 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7299 	if (!fwd)
7300 		return ERR_PTR(-ENOMEM);
7301 
7302 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7303 	fwd->bit_no = avail_macvlan;
7304 	netdev_set_sb_channel(vdev, avail_macvlan);
7305 	fwd->netdev = vdev;
7306 
7307 	if (!netif_running(netdev))
7308 		return fwd;
7309 
7310 	/* Set fwd ring up */
7311 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7312 	if (ret) {
7313 		/* unbind the queues and drop the subordinate channel config */
7314 		netdev_unbind_sb_channel(netdev, vdev);
7315 		netdev_set_sb_channel(vdev, 0);
7316 
7317 		kfree(fwd);
7318 		return ERR_PTR(-EINVAL);
7319 	}
7320 
7321 	return fwd;
7322 }
7323 
7324 /**
7325  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7326  * @vsi: the VSI we want to access
7327  */
7328 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7329 {
7330 	struct i40e_channel *ch, *ch_tmp;
7331 	struct i40e_pf *pf = vsi->back;
7332 	struct i40e_hw *hw = &pf->hw;
7333 	int aq_err, ret = 0;
7334 
7335 	if (list_empty(&vsi->macvlan_list))
7336 		return;
7337 
7338 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7339 		if (i40e_is_channel_macvlan(ch)) {
7340 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7341 						      i40e_channel_mac(ch),
7342 						      &aq_err);
7343 			if (!ret) {
7344 				/* Reset queue contexts */
7345 				i40e_reset_ch_rings(vsi, ch);
7346 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7347 				netdev_unbind_sb_channel(vsi->netdev,
7348 							 ch->fwd->netdev);
7349 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7350 				kfree(ch->fwd);
7351 				ch->fwd = NULL;
7352 			}
7353 		}
7354 	}
7355 }
7356 
7357 /**
7358  * i40e_fwd_del - delete macvlan interfaces
7359  * @netdev: net device to configure
7360  * @vdev: macvlan netdevice
7361  */
7362 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7363 {
7364 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7365 	struct i40e_fwd_adapter *fwd = vdev;
7366 	struct i40e_channel *ch, *ch_tmp;
7367 	struct i40e_vsi *vsi = np->vsi;
7368 	struct i40e_pf *pf = vsi->back;
7369 	struct i40e_hw *hw = &pf->hw;
7370 	int aq_err, ret = 0;
7371 
7372 	/* Find the channel associated with the macvlan and del mac filter */
7373 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7374 		if (i40e_is_channel_macvlan(ch) &&
7375 		    ether_addr_equal(i40e_channel_mac(ch),
7376 				     fwd->netdev->dev_addr)) {
7377 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7378 						      i40e_channel_mac(ch),
7379 						      &aq_err);
7380 			if (!ret) {
7381 				/* Reset queue contexts */
7382 				i40e_reset_ch_rings(vsi, ch);
7383 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7384 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7385 				netdev_set_sb_channel(fwd->netdev, 0);
7386 				kfree(ch->fwd);
7387 				ch->fwd = NULL;
7388 			} else {
7389 				dev_info(&pf->pdev->dev,
7390 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7391 					  i40e_stat_str(hw, ret),
7392 					  i40e_aq_str(hw, aq_err));
7393 			}
7394 			break;
7395 		}
7396 	}
7397 }
7398 
7399 /**
7400  * i40e_setup_tc - configure multiple traffic classes
7401  * @netdev: net device to configure
7402  * @type_data: tc offload data
7403  **/
7404 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7405 {
7406 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7407 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7408 	struct i40e_vsi *vsi = np->vsi;
7409 	struct i40e_pf *pf = vsi->back;
7410 	u8 enabled_tc = 0, num_tc, hw;
7411 	bool need_reset = false;
7412 	int old_queue_pairs;
7413 	int ret = -EINVAL;
7414 	u16 mode;
7415 	int i;
7416 
7417 	old_queue_pairs = vsi->num_queue_pairs;
7418 	num_tc = mqprio_qopt->qopt.num_tc;
7419 	hw = mqprio_qopt->qopt.hw;
7420 	mode = mqprio_qopt->mode;
7421 	if (!hw) {
7422 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7423 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7424 		goto config_tc;
7425 	}
7426 
7427 	/* Check if MFP enabled */
7428 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7429 		netdev_info(netdev,
7430 			    "Configuring TC not supported in MFP mode\n");
7431 		return ret;
7432 	}
7433 	switch (mode) {
7434 	case TC_MQPRIO_MODE_DCB:
7435 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7436 
7437 		/* Check if DCB enabled to continue */
7438 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7439 			netdev_info(netdev,
7440 				    "DCB is not enabled for adapter\n");
7441 			return ret;
7442 		}
7443 
7444 		/* Check whether tc count is within enabled limit */
7445 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7446 			netdev_info(netdev,
7447 				    "TC count greater than enabled on link for adapter\n");
7448 			return ret;
7449 		}
7450 		break;
7451 	case TC_MQPRIO_MODE_CHANNEL:
7452 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7453 			netdev_info(netdev,
7454 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7455 			return ret;
7456 		}
7457 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7458 			return ret;
7459 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7460 		if (ret)
7461 			return ret;
7462 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7463 		       sizeof(*mqprio_qopt));
7464 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7465 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7466 		break;
7467 	default:
7468 		return -EINVAL;
7469 	}
7470 
7471 config_tc:
7472 	/* Generate TC map for number of tc requested */
7473 	for (i = 0; i < num_tc; i++)
7474 		enabled_tc |= BIT(i);
7475 
7476 	/* Requesting same TC configuration as already enabled */
7477 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7478 	    mode != TC_MQPRIO_MODE_CHANNEL)
7479 		return 0;
7480 
7481 	/* Quiesce VSI queues */
7482 	i40e_quiesce_vsi(vsi);
7483 
7484 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7485 		i40e_remove_queue_channels(vsi);
7486 
7487 	/* Configure VSI for enabled TCs */
7488 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7489 	if (ret) {
7490 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7491 			    vsi->seid);
7492 		need_reset = true;
7493 		goto exit;
7494 	} else {
7495 		dev_info(&vsi->back->pdev->dev,
7496 			 "Setup channel (id:%u) utilizing num_queues %d\n",
7497 			 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7498 	}
7499 
7500 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7501 		if (vsi->mqprio_qopt.max_rate[0]) {
7502 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7503 
7504 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7505 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7506 			if (!ret) {
7507 				u64 credits = max_tx_rate;
7508 
7509 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7510 				dev_dbg(&vsi->back->pdev->dev,
7511 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7512 					max_tx_rate,
7513 					credits,
7514 					vsi->seid);
7515 			} else {
7516 				need_reset = true;
7517 				goto exit;
7518 			}
7519 		}
7520 		ret = i40e_configure_queue_channels(vsi);
7521 		if (ret) {
7522 			vsi->num_queue_pairs = old_queue_pairs;
7523 			netdev_info(netdev,
7524 				    "Failed configuring queue channels\n");
7525 			need_reset = true;
7526 			goto exit;
7527 		}
7528 	}
7529 
7530 exit:
7531 	/* Reset the configuration data to defaults, only TC0 is enabled */
7532 	if (need_reset) {
7533 		i40e_vsi_set_default_tc_config(vsi);
7534 		need_reset = false;
7535 	}
7536 
7537 	/* Unquiesce VSI */
7538 	i40e_unquiesce_vsi(vsi);
7539 	return ret;
7540 }
7541 
7542 /**
7543  * i40e_set_cld_element - sets cloud filter element data
7544  * @filter: cloud filter rule
7545  * @cld: ptr to cloud filter element data
7546  *
7547  * This is helper function to copy data into cloud filter element
7548  **/
7549 static inline void
7550 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7551 		     struct i40e_aqc_cloud_filters_element_data *cld)
7552 {
7553 	int i, j;
7554 	u32 ipa;
7555 
7556 	memset(cld, 0, sizeof(*cld));
7557 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7558 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7559 
7560 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7561 		return;
7562 
7563 	if (filter->n_proto == ETH_P_IPV6) {
7564 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7565 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
7566 		     i++, j += 2) {
7567 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7568 			ipa = cpu_to_le32(ipa);
7569 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
7570 		}
7571 	} else {
7572 		ipa = be32_to_cpu(filter->dst_ipv4);
7573 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7574 	}
7575 
7576 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7577 
7578 	/* tenant_id is not supported by FW now, once the support is enabled
7579 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7580 	 */
7581 	if (filter->tenant_id)
7582 		return;
7583 }
7584 
7585 /**
7586  * i40e_add_del_cloud_filter - Add/del cloud filter
7587  * @vsi: pointer to VSI
7588  * @filter: cloud filter rule
7589  * @add: if true, add, if false, delete
7590  *
7591  * Add or delete a cloud filter for a specific flow spec.
7592  * Returns 0 if the filter were successfully added.
7593  **/
7594 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7595 			      struct i40e_cloud_filter *filter, bool add)
7596 {
7597 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7598 	struct i40e_pf *pf = vsi->back;
7599 	int ret;
7600 	static const u16 flag_table[128] = {
7601 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7602 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7603 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7604 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7605 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7606 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7607 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7608 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7609 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7610 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7611 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7612 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7613 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7614 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7615 	};
7616 
7617 	if (filter->flags >= ARRAY_SIZE(flag_table))
7618 		return I40E_ERR_CONFIG;
7619 
7620 	/* copy element needed to add cloud filter from filter */
7621 	i40e_set_cld_element(filter, &cld_filter);
7622 
7623 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7624 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7625 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7626 
7627 	if (filter->n_proto == ETH_P_IPV6)
7628 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7629 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7630 	else
7631 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7632 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7633 
7634 	if (add)
7635 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7636 						&cld_filter, 1);
7637 	else
7638 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7639 						&cld_filter, 1);
7640 	if (ret)
7641 		dev_dbg(&pf->pdev->dev,
7642 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7643 			add ? "add" : "delete", filter->dst_port, ret,
7644 			pf->hw.aq.asq_last_status);
7645 	else
7646 		dev_info(&pf->pdev->dev,
7647 			 "%s cloud filter for VSI: %d\n",
7648 			 add ? "Added" : "Deleted", filter->seid);
7649 	return ret;
7650 }
7651 
7652 /**
7653  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7654  * @vsi: pointer to VSI
7655  * @filter: cloud filter rule
7656  * @add: if true, add, if false, delete
7657  *
7658  * Add or delete a cloud filter for a specific flow spec using big buffer.
7659  * Returns 0 if the filter were successfully added.
7660  **/
7661 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7662 				      struct i40e_cloud_filter *filter,
7663 				      bool add)
7664 {
7665 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7666 	struct i40e_pf *pf = vsi->back;
7667 	int ret;
7668 
7669 	/* Both (src/dst) valid mac_addr are not supported */
7670 	if ((is_valid_ether_addr(filter->dst_mac) &&
7671 	     is_valid_ether_addr(filter->src_mac)) ||
7672 	    (is_multicast_ether_addr(filter->dst_mac) &&
7673 	     is_multicast_ether_addr(filter->src_mac)))
7674 		return -EOPNOTSUPP;
7675 
7676 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7677 	 * ports are not supported via big buffer now.
7678 	 */
7679 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7680 		return -EOPNOTSUPP;
7681 
7682 	/* adding filter using src_port/src_ip is not supported at this stage */
7683 	if (filter->src_port || filter->src_ipv4 ||
7684 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7685 		return -EOPNOTSUPP;
7686 
7687 	/* copy element needed to add cloud filter from filter */
7688 	i40e_set_cld_element(filter, &cld_filter.element);
7689 
7690 	if (is_valid_ether_addr(filter->dst_mac) ||
7691 	    is_valid_ether_addr(filter->src_mac) ||
7692 	    is_multicast_ether_addr(filter->dst_mac) ||
7693 	    is_multicast_ether_addr(filter->src_mac)) {
7694 		/* MAC + IP : unsupported mode */
7695 		if (filter->dst_ipv4)
7696 			return -EOPNOTSUPP;
7697 
7698 		/* since we validated that L4 port must be valid before
7699 		 * we get here, start with respective "flags" value
7700 		 * and update if vlan is present or not
7701 		 */
7702 		cld_filter.element.flags =
7703 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7704 
7705 		if (filter->vlan_id) {
7706 			cld_filter.element.flags =
7707 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7708 		}
7709 
7710 	} else if (filter->dst_ipv4 ||
7711 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7712 		cld_filter.element.flags =
7713 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7714 		if (filter->n_proto == ETH_P_IPV6)
7715 			cld_filter.element.flags |=
7716 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7717 		else
7718 			cld_filter.element.flags |=
7719 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7720 	} else {
7721 		dev_err(&pf->pdev->dev,
7722 			"either mac or ip has to be valid for cloud filter\n");
7723 		return -EINVAL;
7724 	}
7725 
7726 	/* Now copy L4 port in Byte 6..7 in general fields */
7727 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7728 						be16_to_cpu(filter->dst_port);
7729 
7730 	if (add) {
7731 		/* Validate current device switch mode, change if necessary */
7732 		ret = i40e_validate_and_set_switch_mode(vsi);
7733 		if (ret) {
7734 			dev_err(&pf->pdev->dev,
7735 				"failed to set switch mode, ret %d\n",
7736 				ret);
7737 			return ret;
7738 		}
7739 
7740 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7741 						   &cld_filter, 1);
7742 	} else {
7743 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7744 						   &cld_filter, 1);
7745 	}
7746 
7747 	if (ret)
7748 		dev_dbg(&pf->pdev->dev,
7749 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7750 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7751 	else
7752 		dev_info(&pf->pdev->dev,
7753 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7754 			 add ? "add" : "delete", filter->seid,
7755 			 ntohs(filter->dst_port));
7756 	return ret;
7757 }
7758 
7759 /**
7760  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7761  * @vsi: Pointer to VSI
7762  * @cls_flower: Pointer to struct tc_cls_flower_offload
7763  * @filter: Pointer to cloud filter structure
7764  *
7765  **/
7766 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7767 				 struct tc_cls_flower_offload *f,
7768 				 struct i40e_cloud_filter *filter)
7769 {
7770 	struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
7771 	struct flow_dissector *dissector = rule->match.dissector;
7772 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7773 	struct i40e_pf *pf = vsi->back;
7774 	u8 field_flags = 0;
7775 
7776 	if (dissector->used_keys &
7777 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7778 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7779 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7780 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7781 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7782 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7783 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7784 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7785 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7786 			dissector->used_keys);
7787 		return -EOPNOTSUPP;
7788 	}
7789 
7790 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7791 		struct flow_match_enc_keyid match;
7792 
7793 		flow_rule_match_enc_keyid(rule, &match);
7794 		if (match.mask->keyid != 0)
7795 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7796 
7797 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7798 	}
7799 
7800 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7801 		struct flow_match_basic match;
7802 
7803 		flow_rule_match_basic(rule, &match);
7804 		n_proto_key = ntohs(match.key->n_proto);
7805 		n_proto_mask = ntohs(match.mask->n_proto);
7806 
7807 		if (n_proto_key == ETH_P_ALL) {
7808 			n_proto_key = 0;
7809 			n_proto_mask = 0;
7810 		}
7811 		filter->n_proto = n_proto_key & n_proto_mask;
7812 		filter->ip_proto = match.key->ip_proto;
7813 	}
7814 
7815 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7816 		struct flow_match_eth_addrs match;
7817 
7818 		flow_rule_match_eth_addrs(rule, &match);
7819 
7820 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7821 		if (!is_zero_ether_addr(match.mask->dst)) {
7822 			if (is_broadcast_ether_addr(match.mask->dst)) {
7823 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7824 			} else {
7825 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7826 					match.mask->dst);
7827 				return I40E_ERR_CONFIG;
7828 			}
7829 		}
7830 
7831 		if (!is_zero_ether_addr(match.mask->src)) {
7832 			if (is_broadcast_ether_addr(match.mask->src)) {
7833 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7834 			} else {
7835 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7836 					match.mask->src);
7837 				return I40E_ERR_CONFIG;
7838 			}
7839 		}
7840 		ether_addr_copy(filter->dst_mac, match.key->dst);
7841 		ether_addr_copy(filter->src_mac, match.key->src);
7842 	}
7843 
7844 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7845 		struct flow_match_vlan match;
7846 
7847 		flow_rule_match_vlan(rule, &match);
7848 		if (match.mask->vlan_id) {
7849 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7850 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7851 
7852 			} else {
7853 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7854 					match.mask->vlan_id);
7855 				return I40E_ERR_CONFIG;
7856 			}
7857 		}
7858 
7859 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7860 	}
7861 
7862 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7863 		struct flow_match_control match;
7864 
7865 		flow_rule_match_control(rule, &match);
7866 		addr_type = match.key->addr_type;
7867 	}
7868 
7869 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7870 		struct flow_match_ipv4_addrs match;
7871 
7872 		flow_rule_match_ipv4_addrs(rule, &match);
7873 		if (match.mask->dst) {
7874 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7875 				field_flags |= I40E_CLOUD_FIELD_IIP;
7876 			} else {
7877 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7878 					&match.mask->dst);
7879 				return I40E_ERR_CONFIG;
7880 			}
7881 		}
7882 
7883 		if (match.mask->src) {
7884 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7885 				field_flags |= I40E_CLOUD_FIELD_IIP;
7886 			} else {
7887 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7888 					&match.mask->src);
7889 				return I40E_ERR_CONFIG;
7890 			}
7891 		}
7892 
7893 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7894 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7895 			return I40E_ERR_CONFIG;
7896 		}
7897 		filter->dst_ipv4 = match.key->dst;
7898 		filter->src_ipv4 = match.key->src;
7899 	}
7900 
7901 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7902 		struct flow_match_ipv6_addrs match;
7903 
7904 		flow_rule_match_ipv6_addrs(rule, &match);
7905 
7906 		/* src and dest IPV6 address should not be LOOPBACK
7907 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7908 		 */
7909 		if (ipv6_addr_loopback(&match.key->dst) ||
7910 		    ipv6_addr_loopback(&match.key->src)) {
7911 			dev_err(&pf->pdev->dev,
7912 				"Bad ipv6, addr is LOOPBACK\n");
7913 			return I40E_ERR_CONFIG;
7914 		}
7915 		if (!ipv6_addr_any(&match.mask->dst) ||
7916 		    !ipv6_addr_any(&match.mask->src))
7917 			field_flags |= I40E_CLOUD_FIELD_IIP;
7918 
7919 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7920 		       sizeof(filter->src_ipv6));
7921 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7922 		       sizeof(filter->dst_ipv6));
7923 	}
7924 
7925 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7926 		struct flow_match_ports match;
7927 
7928 		flow_rule_match_ports(rule, &match);
7929 		if (match.mask->src) {
7930 			if (match.mask->src == cpu_to_be16(0xffff)) {
7931 				field_flags |= I40E_CLOUD_FIELD_IIP;
7932 			} else {
7933 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7934 					be16_to_cpu(match.mask->src));
7935 				return I40E_ERR_CONFIG;
7936 			}
7937 		}
7938 
7939 		if (match.mask->dst) {
7940 			if (match.mask->dst == cpu_to_be16(0xffff)) {
7941 				field_flags |= I40E_CLOUD_FIELD_IIP;
7942 			} else {
7943 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7944 					be16_to_cpu(match.mask->dst));
7945 				return I40E_ERR_CONFIG;
7946 			}
7947 		}
7948 
7949 		filter->dst_port = match.key->dst;
7950 		filter->src_port = match.key->src;
7951 
7952 		switch (filter->ip_proto) {
7953 		case IPPROTO_TCP:
7954 		case IPPROTO_UDP:
7955 			break;
7956 		default:
7957 			dev_err(&pf->pdev->dev,
7958 				"Only UDP and TCP transport are supported\n");
7959 			return -EINVAL;
7960 		}
7961 	}
7962 	filter->flags = field_flags;
7963 	return 0;
7964 }
7965 
7966 /**
7967  * i40e_handle_tclass: Forward to a traffic class on the device
7968  * @vsi: Pointer to VSI
7969  * @tc: traffic class index on the device
7970  * @filter: Pointer to cloud filter structure
7971  *
7972  **/
7973 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7974 			      struct i40e_cloud_filter *filter)
7975 {
7976 	struct i40e_channel *ch, *ch_tmp;
7977 
7978 	/* direct to a traffic class on the same device */
7979 	if (tc == 0) {
7980 		filter->seid = vsi->seid;
7981 		return 0;
7982 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7983 		if (!filter->dst_port) {
7984 			dev_err(&vsi->back->pdev->dev,
7985 				"Specify destination port to direct to traffic class that is not default\n");
7986 			return -EINVAL;
7987 		}
7988 		if (list_empty(&vsi->ch_list))
7989 			return -EINVAL;
7990 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7991 					 list) {
7992 			if (ch->seid == vsi->tc_seid_map[tc])
7993 				filter->seid = ch->seid;
7994 		}
7995 		return 0;
7996 	}
7997 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7998 	return -EINVAL;
7999 }
8000 
8001 /**
8002  * i40e_configure_clsflower - Configure tc flower filters
8003  * @vsi: Pointer to VSI
8004  * @cls_flower: Pointer to struct tc_cls_flower_offload
8005  *
8006  **/
8007 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8008 				    struct tc_cls_flower_offload *cls_flower)
8009 {
8010 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8011 	struct i40e_cloud_filter *filter = NULL;
8012 	struct i40e_pf *pf = vsi->back;
8013 	int err = 0;
8014 
8015 	if (tc < 0) {
8016 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8017 		return -EOPNOTSUPP;
8018 	}
8019 
8020 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8021 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8022 		return -EBUSY;
8023 
8024 	if (pf->fdir_pf_active_filters ||
8025 	    (!hlist_empty(&pf->fdir_filter_list))) {
8026 		dev_err(&vsi->back->pdev->dev,
8027 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8028 		return -EINVAL;
8029 	}
8030 
8031 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8032 		dev_err(&vsi->back->pdev->dev,
8033 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8034 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8035 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8036 	}
8037 
8038 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8039 	if (!filter)
8040 		return -ENOMEM;
8041 
8042 	filter->cookie = cls_flower->cookie;
8043 
8044 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8045 	if (err < 0)
8046 		goto err;
8047 
8048 	err = i40e_handle_tclass(vsi, tc, filter);
8049 	if (err < 0)
8050 		goto err;
8051 
8052 	/* Add cloud filter */
8053 	if (filter->dst_port)
8054 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8055 	else
8056 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8057 
8058 	if (err) {
8059 		dev_err(&pf->pdev->dev,
8060 			"Failed to add cloud filter, err %s\n",
8061 			i40e_stat_str(&pf->hw, err));
8062 		goto err;
8063 	}
8064 
8065 	/* add filter to the ordered list */
8066 	INIT_HLIST_NODE(&filter->cloud_node);
8067 
8068 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8069 
8070 	pf->num_cloud_filters++;
8071 
8072 	return err;
8073 err:
8074 	kfree(filter);
8075 	return err;
8076 }
8077 
8078 /**
8079  * i40e_find_cloud_filter - Find the could filter in the list
8080  * @vsi: Pointer to VSI
8081  * @cookie: filter specific cookie
8082  *
8083  **/
8084 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8085 							unsigned long *cookie)
8086 {
8087 	struct i40e_cloud_filter *filter = NULL;
8088 	struct hlist_node *node2;
8089 
8090 	hlist_for_each_entry_safe(filter, node2,
8091 				  &vsi->back->cloud_filter_list, cloud_node)
8092 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8093 			return filter;
8094 	return NULL;
8095 }
8096 
8097 /**
8098  * i40e_delete_clsflower - Remove tc flower filters
8099  * @vsi: Pointer to VSI
8100  * @cls_flower: Pointer to struct tc_cls_flower_offload
8101  *
8102  **/
8103 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8104 				 struct tc_cls_flower_offload *cls_flower)
8105 {
8106 	struct i40e_cloud_filter *filter = NULL;
8107 	struct i40e_pf *pf = vsi->back;
8108 	int err = 0;
8109 
8110 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8111 
8112 	if (!filter)
8113 		return -EINVAL;
8114 
8115 	hash_del(&filter->cloud_node);
8116 
8117 	if (filter->dst_port)
8118 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8119 	else
8120 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8121 
8122 	kfree(filter);
8123 	if (err) {
8124 		dev_err(&pf->pdev->dev,
8125 			"Failed to delete cloud filter, err %s\n",
8126 			i40e_stat_str(&pf->hw, err));
8127 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8128 	}
8129 
8130 	pf->num_cloud_filters--;
8131 	if (!pf->num_cloud_filters)
8132 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8133 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8134 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8135 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8136 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8137 		}
8138 	return 0;
8139 }
8140 
8141 /**
8142  * i40e_setup_tc_cls_flower - flower classifier offloads
8143  * @netdev: net device to configure
8144  * @type_data: offload data
8145  **/
8146 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8147 				    struct tc_cls_flower_offload *cls_flower)
8148 {
8149 	struct i40e_vsi *vsi = np->vsi;
8150 
8151 	switch (cls_flower->command) {
8152 	case TC_CLSFLOWER_REPLACE:
8153 		return i40e_configure_clsflower(vsi, cls_flower);
8154 	case TC_CLSFLOWER_DESTROY:
8155 		return i40e_delete_clsflower(vsi, cls_flower);
8156 	case TC_CLSFLOWER_STATS:
8157 		return -EOPNOTSUPP;
8158 	default:
8159 		return -EOPNOTSUPP;
8160 	}
8161 }
8162 
8163 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8164 				  void *cb_priv)
8165 {
8166 	struct i40e_netdev_priv *np = cb_priv;
8167 
8168 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8169 		return -EOPNOTSUPP;
8170 
8171 	switch (type) {
8172 	case TC_SETUP_CLSFLOWER:
8173 		return i40e_setup_tc_cls_flower(np, type_data);
8174 
8175 	default:
8176 		return -EOPNOTSUPP;
8177 	}
8178 }
8179 
8180 static int i40e_setup_tc_block(struct net_device *dev,
8181 			       struct tc_block_offload *f)
8182 {
8183 	struct i40e_netdev_priv *np = netdev_priv(dev);
8184 
8185 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8186 		return -EOPNOTSUPP;
8187 
8188 	switch (f->command) {
8189 	case TC_BLOCK_BIND:
8190 		return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
8191 					     np, np, f->extack);
8192 	case TC_BLOCK_UNBIND:
8193 		tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
8194 		return 0;
8195 	default:
8196 		return -EOPNOTSUPP;
8197 	}
8198 }
8199 
8200 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8201 			   void *type_data)
8202 {
8203 	switch (type) {
8204 	case TC_SETUP_QDISC_MQPRIO:
8205 		return i40e_setup_tc(netdev, type_data);
8206 	case TC_SETUP_BLOCK:
8207 		return i40e_setup_tc_block(netdev, type_data);
8208 	default:
8209 		return -EOPNOTSUPP;
8210 	}
8211 }
8212 
8213 /**
8214  * i40e_open - Called when a network interface is made active
8215  * @netdev: network interface device structure
8216  *
8217  * The open entry point is called when a network interface is made
8218  * active by the system (IFF_UP).  At this point all resources needed
8219  * for transmit and receive operations are allocated, the interrupt
8220  * handler is registered with the OS, the netdev watchdog subtask is
8221  * enabled, and the stack is notified that the interface is ready.
8222  *
8223  * Returns 0 on success, negative value on failure
8224  **/
8225 int i40e_open(struct net_device *netdev)
8226 {
8227 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8228 	struct i40e_vsi *vsi = np->vsi;
8229 	struct i40e_pf *pf = vsi->back;
8230 	int err;
8231 
8232 	/* disallow open during test or if eeprom is broken */
8233 	if (test_bit(__I40E_TESTING, pf->state) ||
8234 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8235 		return -EBUSY;
8236 
8237 	netif_carrier_off(netdev);
8238 
8239 	if (i40e_force_link_state(pf, true))
8240 		return -EAGAIN;
8241 
8242 	err = i40e_vsi_open(vsi);
8243 	if (err)
8244 		return err;
8245 
8246 	/* configure global TSO hardware offload settings */
8247 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8248 						       TCP_FLAG_FIN) >> 16);
8249 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8250 						       TCP_FLAG_FIN |
8251 						       TCP_FLAG_CWR) >> 16);
8252 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8253 
8254 	udp_tunnel_get_rx_info(netdev);
8255 
8256 	return 0;
8257 }
8258 
8259 /**
8260  * i40e_vsi_open -
8261  * @vsi: the VSI to open
8262  *
8263  * Finish initialization of the VSI.
8264  *
8265  * Returns 0 on success, negative value on failure
8266  *
8267  * Note: expects to be called while under rtnl_lock()
8268  **/
8269 int i40e_vsi_open(struct i40e_vsi *vsi)
8270 {
8271 	struct i40e_pf *pf = vsi->back;
8272 	char int_name[I40E_INT_NAME_STR_LEN];
8273 	int err;
8274 
8275 	/* allocate descriptors */
8276 	err = i40e_vsi_setup_tx_resources(vsi);
8277 	if (err)
8278 		goto err_setup_tx;
8279 	err = i40e_vsi_setup_rx_resources(vsi);
8280 	if (err)
8281 		goto err_setup_rx;
8282 
8283 	err = i40e_vsi_configure(vsi);
8284 	if (err)
8285 		goto err_setup_rx;
8286 
8287 	if (vsi->netdev) {
8288 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8289 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8290 		err = i40e_vsi_request_irq(vsi, int_name);
8291 		if (err)
8292 			goto err_setup_rx;
8293 
8294 		/* Notify the stack of the actual queue counts. */
8295 		err = netif_set_real_num_tx_queues(vsi->netdev,
8296 						   vsi->num_queue_pairs);
8297 		if (err)
8298 			goto err_set_queues;
8299 
8300 		err = netif_set_real_num_rx_queues(vsi->netdev,
8301 						   vsi->num_queue_pairs);
8302 		if (err)
8303 			goto err_set_queues;
8304 
8305 	} else if (vsi->type == I40E_VSI_FDIR) {
8306 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8307 			 dev_driver_string(&pf->pdev->dev),
8308 			 dev_name(&pf->pdev->dev));
8309 		err = i40e_vsi_request_irq(vsi, int_name);
8310 
8311 	} else {
8312 		err = -EINVAL;
8313 		goto err_setup_rx;
8314 	}
8315 
8316 	err = i40e_up_complete(vsi);
8317 	if (err)
8318 		goto err_up_complete;
8319 
8320 	return 0;
8321 
8322 err_up_complete:
8323 	i40e_down(vsi);
8324 err_set_queues:
8325 	i40e_vsi_free_irq(vsi);
8326 err_setup_rx:
8327 	i40e_vsi_free_rx_resources(vsi);
8328 err_setup_tx:
8329 	i40e_vsi_free_tx_resources(vsi);
8330 	if (vsi == pf->vsi[pf->lan_vsi])
8331 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8332 
8333 	return err;
8334 }
8335 
8336 /**
8337  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8338  * @pf: Pointer to PF
8339  *
8340  * This function destroys the hlist where all the Flow Director
8341  * filters were saved.
8342  **/
8343 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8344 {
8345 	struct i40e_fdir_filter *filter;
8346 	struct i40e_flex_pit *pit_entry, *tmp;
8347 	struct hlist_node *node2;
8348 
8349 	hlist_for_each_entry_safe(filter, node2,
8350 				  &pf->fdir_filter_list, fdir_node) {
8351 		hlist_del(&filter->fdir_node);
8352 		kfree(filter);
8353 	}
8354 
8355 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8356 		list_del(&pit_entry->list);
8357 		kfree(pit_entry);
8358 	}
8359 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8360 
8361 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8362 		list_del(&pit_entry->list);
8363 		kfree(pit_entry);
8364 	}
8365 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8366 
8367 	pf->fdir_pf_active_filters = 0;
8368 	pf->fd_tcp4_filter_cnt = 0;
8369 	pf->fd_udp4_filter_cnt = 0;
8370 	pf->fd_sctp4_filter_cnt = 0;
8371 	pf->fd_ip4_filter_cnt = 0;
8372 
8373 	/* Reprogram the default input set for TCP/IPv4 */
8374 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8375 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8376 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8377 
8378 	/* Reprogram the default input set for UDP/IPv4 */
8379 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8380 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8381 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8382 
8383 	/* Reprogram the default input set for SCTP/IPv4 */
8384 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8385 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8386 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8387 
8388 	/* Reprogram the default input set for Other/IPv4 */
8389 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8390 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8391 
8392 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8393 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8394 }
8395 
8396 /**
8397  * i40e_cloud_filter_exit - Cleans up the cloud filters
8398  * @pf: Pointer to PF
8399  *
8400  * This function destroys the hlist where all the cloud filters
8401  * were saved.
8402  **/
8403 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8404 {
8405 	struct i40e_cloud_filter *cfilter;
8406 	struct hlist_node *node;
8407 
8408 	hlist_for_each_entry_safe(cfilter, node,
8409 				  &pf->cloud_filter_list, cloud_node) {
8410 		hlist_del(&cfilter->cloud_node);
8411 		kfree(cfilter);
8412 	}
8413 	pf->num_cloud_filters = 0;
8414 
8415 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8416 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8417 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8418 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8419 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8420 	}
8421 }
8422 
8423 /**
8424  * i40e_close - Disables a network interface
8425  * @netdev: network interface device structure
8426  *
8427  * The close entry point is called when an interface is de-activated
8428  * by the OS.  The hardware is still under the driver's control, but
8429  * this netdev interface is disabled.
8430  *
8431  * Returns 0, this is not allowed to fail
8432  **/
8433 int i40e_close(struct net_device *netdev)
8434 {
8435 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8436 	struct i40e_vsi *vsi = np->vsi;
8437 
8438 	i40e_vsi_close(vsi);
8439 
8440 	return 0;
8441 }
8442 
8443 /**
8444  * i40e_do_reset - Start a PF or Core Reset sequence
8445  * @pf: board private structure
8446  * @reset_flags: which reset is requested
8447  * @lock_acquired: indicates whether or not the lock has been acquired
8448  * before this function was called.
8449  *
8450  * The essential difference in resets is that the PF Reset
8451  * doesn't clear the packet buffers, doesn't reset the PE
8452  * firmware, and doesn't bother the other PFs on the chip.
8453  **/
8454 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8455 {
8456 	u32 val;
8457 
8458 	WARN_ON(in_interrupt());
8459 
8460 
8461 	/* do the biggest reset indicated */
8462 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8463 
8464 		/* Request a Global Reset
8465 		 *
8466 		 * This will start the chip's countdown to the actual full
8467 		 * chip reset event, and a warning interrupt to be sent
8468 		 * to all PFs, including the requestor.  Our handler
8469 		 * for the warning interrupt will deal with the shutdown
8470 		 * and recovery of the switch setup.
8471 		 */
8472 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8473 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8474 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8475 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8476 
8477 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8478 
8479 		/* Request a Core Reset
8480 		 *
8481 		 * Same as Global Reset, except does *not* include the MAC/PHY
8482 		 */
8483 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8484 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8485 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8486 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8487 		i40e_flush(&pf->hw);
8488 
8489 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8490 
8491 		/* Request a PF Reset
8492 		 *
8493 		 * Resets only the PF-specific registers
8494 		 *
8495 		 * This goes directly to the tear-down and rebuild of
8496 		 * the switch, since we need to do all the recovery as
8497 		 * for the Core Reset.
8498 		 */
8499 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8500 		i40e_handle_reset_warning(pf, lock_acquired);
8501 
8502 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8503 		int v;
8504 
8505 		/* Find the VSI(s) that requested a re-init */
8506 		dev_info(&pf->pdev->dev,
8507 			 "VSI reinit requested\n");
8508 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8509 			struct i40e_vsi *vsi = pf->vsi[v];
8510 
8511 			if (vsi != NULL &&
8512 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8513 					       vsi->state))
8514 				i40e_vsi_reinit_locked(pf->vsi[v]);
8515 		}
8516 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8517 		int v;
8518 
8519 		/* Find the VSI(s) that needs to be brought down */
8520 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8521 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8522 			struct i40e_vsi *vsi = pf->vsi[v];
8523 
8524 			if (vsi != NULL &&
8525 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8526 					       vsi->state)) {
8527 				set_bit(__I40E_VSI_DOWN, vsi->state);
8528 				i40e_down(vsi);
8529 			}
8530 		}
8531 	} else {
8532 		dev_info(&pf->pdev->dev,
8533 			 "bad reset request 0x%08x\n", reset_flags);
8534 	}
8535 }
8536 
8537 #ifdef CONFIG_I40E_DCB
8538 /**
8539  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8540  * @pf: board private structure
8541  * @old_cfg: current DCB config
8542  * @new_cfg: new DCB config
8543  **/
8544 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8545 			    struct i40e_dcbx_config *old_cfg,
8546 			    struct i40e_dcbx_config *new_cfg)
8547 {
8548 	bool need_reconfig = false;
8549 
8550 	/* Check if ETS configuration has changed */
8551 	if (memcmp(&new_cfg->etscfg,
8552 		   &old_cfg->etscfg,
8553 		   sizeof(new_cfg->etscfg))) {
8554 		/* If Priority Table has changed reconfig is needed */
8555 		if (memcmp(&new_cfg->etscfg.prioritytable,
8556 			   &old_cfg->etscfg.prioritytable,
8557 			   sizeof(new_cfg->etscfg.prioritytable))) {
8558 			need_reconfig = true;
8559 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8560 		}
8561 
8562 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8563 			   &old_cfg->etscfg.tcbwtable,
8564 			   sizeof(new_cfg->etscfg.tcbwtable)))
8565 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8566 
8567 		if (memcmp(&new_cfg->etscfg.tsatable,
8568 			   &old_cfg->etscfg.tsatable,
8569 			   sizeof(new_cfg->etscfg.tsatable)))
8570 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8571 	}
8572 
8573 	/* Check if PFC configuration has changed */
8574 	if (memcmp(&new_cfg->pfc,
8575 		   &old_cfg->pfc,
8576 		   sizeof(new_cfg->pfc))) {
8577 		need_reconfig = true;
8578 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8579 	}
8580 
8581 	/* Check if APP Table has changed */
8582 	if (memcmp(&new_cfg->app,
8583 		   &old_cfg->app,
8584 		   sizeof(new_cfg->app))) {
8585 		need_reconfig = true;
8586 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8587 	}
8588 
8589 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8590 	return need_reconfig;
8591 }
8592 
8593 /**
8594  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8595  * @pf: board private structure
8596  * @e: event info posted on ARQ
8597  **/
8598 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8599 				  struct i40e_arq_event_info *e)
8600 {
8601 	struct i40e_aqc_lldp_get_mib *mib =
8602 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8603 	struct i40e_hw *hw = &pf->hw;
8604 	struct i40e_dcbx_config tmp_dcbx_cfg;
8605 	bool need_reconfig = false;
8606 	int ret = 0;
8607 	u8 type;
8608 
8609 	/* Not DCB capable or capability disabled */
8610 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8611 		return ret;
8612 
8613 	/* Ignore if event is not for Nearest Bridge */
8614 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8615 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8616 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8617 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8618 		return ret;
8619 
8620 	/* Check MIB Type and return if event for Remote MIB update */
8621 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8622 	dev_dbg(&pf->pdev->dev,
8623 		"LLDP event mib type %s\n", type ? "remote" : "local");
8624 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8625 		/* Update the remote cached instance and return */
8626 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8627 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8628 				&hw->remote_dcbx_config);
8629 		goto exit;
8630 	}
8631 
8632 	/* Store the old configuration */
8633 	tmp_dcbx_cfg = hw->local_dcbx_config;
8634 
8635 	/* Reset the old DCBx configuration data */
8636 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8637 	/* Get updated DCBX data from firmware */
8638 	ret = i40e_get_dcb_config(&pf->hw);
8639 	if (ret) {
8640 		dev_info(&pf->pdev->dev,
8641 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8642 			 i40e_stat_str(&pf->hw, ret),
8643 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8644 		goto exit;
8645 	}
8646 
8647 	/* No change detected in DCBX configs */
8648 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8649 		    sizeof(tmp_dcbx_cfg))) {
8650 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8651 		goto exit;
8652 	}
8653 
8654 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8655 					       &hw->local_dcbx_config);
8656 
8657 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8658 
8659 	if (!need_reconfig)
8660 		goto exit;
8661 
8662 	/* Enable DCB tagging only when more than one TC */
8663 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8664 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8665 	else
8666 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8667 
8668 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8669 	/* Reconfiguration needed quiesce all VSIs */
8670 	i40e_pf_quiesce_all_vsi(pf);
8671 
8672 	/* Changes in configuration update VEB/VSI */
8673 	i40e_dcb_reconfigure(pf);
8674 
8675 	ret = i40e_resume_port_tx(pf);
8676 
8677 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8678 	/* In case of error no point in resuming VSIs */
8679 	if (ret)
8680 		goto exit;
8681 
8682 	/* Wait for the PF's queues to be disabled */
8683 	ret = i40e_pf_wait_queues_disabled(pf);
8684 	if (ret) {
8685 		/* Schedule PF reset to recover */
8686 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8687 		i40e_service_event_schedule(pf);
8688 	} else {
8689 		i40e_pf_unquiesce_all_vsi(pf);
8690 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8691 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8692 	}
8693 
8694 exit:
8695 	return ret;
8696 }
8697 #endif /* CONFIG_I40E_DCB */
8698 
8699 /**
8700  * i40e_do_reset_safe - Protected reset path for userland calls.
8701  * @pf: board private structure
8702  * @reset_flags: which reset is requested
8703  *
8704  **/
8705 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8706 {
8707 	rtnl_lock();
8708 	i40e_do_reset(pf, reset_flags, true);
8709 	rtnl_unlock();
8710 }
8711 
8712 /**
8713  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8714  * @pf: board private structure
8715  * @e: event info posted on ARQ
8716  *
8717  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8718  * and VF queues
8719  **/
8720 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8721 					   struct i40e_arq_event_info *e)
8722 {
8723 	struct i40e_aqc_lan_overflow *data =
8724 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8725 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8726 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8727 	struct i40e_hw *hw = &pf->hw;
8728 	struct i40e_vf *vf;
8729 	u16 vf_id;
8730 
8731 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8732 		queue, qtx_ctl);
8733 
8734 	/* Queue belongs to VF, find the VF and issue VF reset */
8735 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8736 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8737 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8738 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8739 		vf_id -= hw->func_caps.vf_base_id;
8740 		vf = &pf->vf[vf_id];
8741 		i40e_vc_notify_vf_reset(vf);
8742 		/* Allow VF to process pending reset notification */
8743 		msleep(20);
8744 		i40e_reset_vf(vf, false);
8745 	}
8746 }
8747 
8748 /**
8749  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8750  * @pf: board private structure
8751  **/
8752 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8753 {
8754 	u32 val, fcnt_prog;
8755 
8756 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8757 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8758 	return fcnt_prog;
8759 }
8760 
8761 /**
8762  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8763  * @pf: board private structure
8764  **/
8765 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8766 {
8767 	u32 val, fcnt_prog;
8768 
8769 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8770 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8771 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8772 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8773 	return fcnt_prog;
8774 }
8775 
8776 /**
8777  * i40e_get_global_fd_count - Get total FD filters programmed on device
8778  * @pf: board private structure
8779  **/
8780 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8781 {
8782 	u32 val, fcnt_prog;
8783 
8784 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8785 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8786 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8787 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8788 	return fcnt_prog;
8789 }
8790 
8791 /**
8792  * i40e_reenable_fdir_sb - Restore FDir SB capability
8793  * @pf: board private structure
8794  **/
8795 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8796 {
8797 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8798 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8799 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8800 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8801 }
8802 
8803 /**
8804  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8805  * @pf: board private structure
8806  **/
8807 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8808 {
8809 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8810 		/* ATR uses the same filtering logic as SB rules. It only
8811 		 * functions properly if the input set mask is at the default
8812 		 * settings. It is safe to restore the default input set
8813 		 * because there are no active TCPv4 filter rules.
8814 		 */
8815 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8816 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8817 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8818 
8819 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8820 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8821 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8822 	}
8823 }
8824 
8825 /**
8826  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8827  * @pf: board private structure
8828  * @filter: FDir filter to remove
8829  */
8830 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8831 				       struct i40e_fdir_filter *filter)
8832 {
8833 	/* Update counters */
8834 	pf->fdir_pf_active_filters--;
8835 	pf->fd_inv = 0;
8836 
8837 	switch (filter->flow_type) {
8838 	case TCP_V4_FLOW:
8839 		pf->fd_tcp4_filter_cnt--;
8840 		break;
8841 	case UDP_V4_FLOW:
8842 		pf->fd_udp4_filter_cnt--;
8843 		break;
8844 	case SCTP_V4_FLOW:
8845 		pf->fd_sctp4_filter_cnt--;
8846 		break;
8847 	case IP_USER_FLOW:
8848 		switch (filter->ip4_proto) {
8849 		case IPPROTO_TCP:
8850 			pf->fd_tcp4_filter_cnt--;
8851 			break;
8852 		case IPPROTO_UDP:
8853 			pf->fd_udp4_filter_cnt--;
8854 			break;
8855 		case IPPROTO_SCTP:
8856 			pf->fd_sctp4_filter_cnt--;
8857 			break;
8858 		case IPPROTO_IP:
8859 			pf->fd_ip4_filter_cnt--;
8860 			break;
8861 		}
8862 		break;
8863 	}
8864 
8865 	/* Remove the filter from the list and free memory */
8866 	hlist_del(&filter->fdir_node);
8867 	kfree(filter);
8868 }
8869 
8870 /**
8871  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8872  * @pf: board private structure
8873  **/
8874 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8875 {
8876 	struct i40e_fdir_filter *filter;
8877 	u32 fcnt_prog, fcnt_avail;
8878 	struct hlist_node *node;
8879 
8880 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8881 		return;
8882 
8883 	/* Check if we have enough room to re-enable FDir SB capability. */
8884 	fcnt_prog = i40e_get_global_fd_count(pf);
8885 	fcnt_avail = pf->fdir_pf_filter_count;
8886 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8887 	    (pf->fd_add_err == 0) ||
8888 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8889 		i40e_reenable_fdir_sb(pf);
8890 
8891 	/* We should wait for even more space before re-enabling ATR.
8892 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8893 	 * rules active.
8894 	 */
8895 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8896 	    (pf->fd_tcp4_filter_cnt == 0))
8897 		i40e_reenable_fdir_atr(pf);
8898 
8899 	/* if hw had a problem adding a filter, delete it */
8900 	if (pf->fd_inv > 0) {
8901 		hlist_for_each_entry_safe(filter, node,
8902 					  &pf->fdir_filter_list, fdir_node)
8903 			if (filter->fd_id == pf->fd_inv)
8904 				i40e_delete_invalid_filter(pf, filter);
8905 	}
8906 }
8907 
8908 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8909 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8910 /**
8911  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8912  * @pf: board private structure
8913  **/
8914 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8915 {
8916 	unsigned long min_flush_time;
8917 	int flush_wait_retry = 50;
8918 	bool disable_atr = false;
8919 	int fd_room;
8920 	int reg;
8921 
8922 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8923 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8924 		return;
8925 
8926 	/* If the flush is happening too quick and we have mostly SB rules we
8927 	 * should not re-enable ATR for some time.
8928 	 */
8929 	min_flush_time = pf->fd_flush_timestamp +
8930 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8931 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8932 
8933 	if (!(time_after(jiffies, min_flush_time)) &&
8934 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8935 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8936 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8937 		disable_atr = true;
8938 	}
8939 
8940 	pf->fd_flush_timestamp = jiffies;
8941 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8942 	/* flush all filters */
8943 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8944 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8945 	i40e_flush(&pf->hw);
8946 	pf->fd_flush_cnt++;
8947 	pf->fd_add_err = 0;
8948 	do {
8949 		/* Check FD flush status every 5-6msec */
8950 		usleep_range(5000, 6000);
8951 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8952 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8953 			break;
8954 	} while (flush_wait_retry--);
8955 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8956 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8957 	} else {
8958 		/* replay sideband filters */
8959 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8960 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8961 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8962 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8963 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8964 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8965 	}
8966 }
8967 
8968 /**
8969  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8970  * @pf: board private structure
8971  **/
8972 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8973 {
8974 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8975 }
8976 
8977 /* We can see up to 256 filter programming desc in transit if the filters are
8978  * being applied really fast; before we see the first
8979  * filter miss error on Rx queue 0. Accumulating enough error messages before
8980  * reacting will make sure we don't cause flush too often.
8981  */
8982 #define I40E_MAX_FD_PROGRAM_ERROR 256
8983 
8984 /**
8985  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8986  * @pf: board private structure
8987  **/
8988 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8989 {
8990 
8991 	/* if interface is down do nothing */
8992 	if (test_bit(__I40E_DOWN, pf->state))
8993 		return;
8994 
8995 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8996 		i40e_fdir_flush_and_replay(pf);
8997 
8998 	i40e_fdir_check_and_reenable(pf);
8999 
9000 }
9001 
9002 /**
9003  * i40e_vsi_link_event - notify VSI of a link event
9004  * @vsi: vsi to be notified
9005  * @link_up: link up or down
9006  **/
9007 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9008 {
9009 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9010 		return;
9011 
9012 	switch (vsi->type) {
9013 	case I40E_VSI_MAIN:
9014 		if (!vsi->netdev || !vsi->netdev_registered)
9015 			break;
9016 
9017 		if (link_up) {
9018 			netif_carrier_on(vsi->netdev);
9019 			netif_tx_wake_all_queues(vsi->netdev);
9020 		} else {
9021 			netif_carrier_off(vsi->netdev);
9022 			netif_tx_stop_all_queues(vsi->netdev);
9023 		}
9024 		break;
9025 
9026 	case I40E_VSI_SRIOV:
9027 	case I40E_VSI_VMDQ2:
9028 	case I40E_VSI_CTRL:
9029 	case I40E_VSI_IWARP:
9030 	case I40E_VSI_MIRROR:
9031 	default:
9032 		/* there is no notification for other VSIs */
9033 		break;
9034 	}
9035 }
9036 
9037 /**
9038  * i40e_veb_link_event - notify elements on the veb of a link event
9039  * @veb: veb to be notified
9040  * @link_up: link up or down
9041  **/
9042 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9043 {
9044 	struct i40e_pf *pf;
9045 	int i;
9046 
9047 	if (!veb || !veb->pf)
9048 		return;
9049 	pf = veb->pf;
9050 
9051 	/* depth first... */
9052 	for (i = 0; i < I40E_MAX_VEB; i++)
9053 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9054 			i40e_veb_link_event(pf->veb[i], link_up);
9055 
9056 	/* ... now the local VSIs */
9057 	for (i = 0; i < pf->num_alloc_vsi; i++)
9058 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9059 			i40e_vsi_link_event(pf->vsi[i], link_up);
9060 }
9061 
9062 /**
9063  * i40e_link_event - Update netif_carrier status
9064  * @pf: board private structure
9065  **/
9066 static void i40e_link_event(struct i40e_pf *pf)
9067 {
9068 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9069 	u8 new_link_speed, old_link_speed;
9070 	i40e_status status;
9071 	bool new_link, old_link;
9072 
9073 	/* set this to force the get_link_status call to refresh state */
9074 	pf->hw.phy.get_link_info = true;
9075 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9076 	status = i40e_get_link_status(&pf->hw, &new_link);
9077 
9078 	/* On success, disable temp link polling */
9079 	if (status == I40E_SUCCESS) {
9080 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9081 	} else {
9082 		/* Enable link polling temporarily until i40e_get_link_status
9083 		 * returns I40E_SUCCESS
9084 		 */
9085 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9086 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9087 			status);
9088 		return;
9089 	}
9090 
9091 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9092 	new_link_speed = pf->hw.phy.link_info.link_speed;
9093 
9094 	if (new_link == old_link &&
9095 	    new_link_speed == old_link_speed &&
9096 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9097 	     new_link == netif_carrier_ok(vsi->netdev)))
9098 		return;
9099 
9100 	i40e_print_link_message(vsi, new_link);
9101 
9102 	/* Notify the base of the switch tree connected to
9103 	 * the link.  Floating VEBs are not notified.
9104 	 */
9105 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9106 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9107 	else
9108 		i40e_vsi_link_event(vsi, new_link);
9109 
9110 	if (pf->vf)
9111 		i40e_vc_notify_link_state(pf);
9112 
9113 	if (pf->flags & I40E_FLAG_PTP)
9114 		i40e_ptp_set_increment(pf);
9115 }
9116 
9117 /**
9118  * i40e_watchdog_subtask - periodic checks not using event driven response
9119  * @pf: board private structure
9120  **/
9121 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9122 {
9123 	int i;
9124 
9125 	/* if interface is down do nothing */
9126 	if (test_bit(__I40E_DOWN, pf->state) ||
9127 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9128 		return;
9129 
9130 	/* make sure we don't do these things too often */
9131 	if (time_before(jiffies, (pf->service_timer_previous +
9132 				  pf->service_timer_period)))
9133 		return;
9134 	pf->service_timer_previous = jiffies;
9135 
9136 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9137 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9138 		i40e_link_event(pf);
9139 
9140 	/* Update the stats for active netdevs so the network stack
9141 	 * can look at updated numbers whenever it cares to
9142 	 */
9143 	for (i = 0; i < pf->num_alloc_vsi; i++)
9144 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9145 			i40e_update_stats(pf->vsi[i]);
9146 
9147 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9148 		/* Update the stats for the active switching components */
9149 		for (i = 0; i < I40E_MAX_VEB; i++)
9150 			if (pf->veb[i])
9151 				i40e_update_veb_stats(pf->veb[i]);
9152 	}
9153 
9154 	i40e_ptp_rx_hang(pf);
9155 	i40e_ptp_tx_hang(pf);
9156 }
9157 
9158 /**
9159  * i40e_reset_subtask - Set up for resetting the device and driver
9160  * @pf: board private structure
9161  **/
9162 static void i40e_reset_subtask(struct i40e_pf *pf)
9163 {
9164 	u32 reset_flags = 0;
9165 
9166 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9167 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9168 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9169 	}
9170 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9171 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9172 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9173 	}
9174 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9175 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9176 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9177 	}
9178 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9179 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9180 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9181 	}
9182 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9183 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9184 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9185 	}
9186 
9187 	/* If there's a recovery already waiting, it takes
9188 	 * precedence before starting a new reset sequence.
9189 	 */
9190 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9191 		i40e_prep_for_reset(pf, false);
9192 		i40e_reset(pf);
9193 		i40e_rebuild(pf, false, false);
9194 	}
9195 
9196 	/* If we're already down or resetting, just bail */
9197 	if (reset_flags &&
9198 	    !test_bit(__I40E_DOWN, pf->state) &&
9199 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9200 		i40e_do_reset(pf, reset_flags, false);
9201 	}
9202 }
9203 
9204 /**
9205  * i40e_handle_link_event - Handle link event
9206  * @pf: board private structure
9207  * @e: event info posted on ARQ
9208  **/
9209 static void i40e_handle_link_event(struct i40e_pf *pf,
9210 				   struct i40e_arq_event_info *e)
9211 {
9212 	struct i40e_aqc_get_link_status *status =
9213 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9214 
9215 	/* Do a new status request to re-enable LSE reporting
9216 	 * and load new status information into the hw struct
9217 	 * This completely ignores any state information
9218 	 * in the ARQ event info, instead choosing to always
9219 	 * issue the AQ update link status command.
9220 	 */
9221 	i40e_link_event(pf);
9222 
9223 	/* Check if module meets thermal requirements */
9224 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9225 		dev_err(&pf->pdev->dev,
9226 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9227 		dev_err(&pf->pdev->dev,
9228 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9229 	} else {
9230 		/* check for unqualified module, if link is down, suppress
9231 		 * the message if link was forced to be down.
9232 		 */
9233 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9234 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9235 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9236 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9237 			dev_err(&pf->pdev->dev,
9238 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9239 			dev_err(&pf->pdev->dev,
9240 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9241 		}
9242 	}
9243 }
9244 
9245 /**
9246  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9247  * @pf: board private structure
9248  **/
9249 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9250 {
9251 	struct i40e_arq_event_info event;
9252 	struct i40e_hw *hw = &pf->hw;
9253 	u16 pending, i = 0;
9254 	i40e_status ret;
9255 	u16 opcode;
9256 	u32 oldval;
9257 	u32 val;
9258 
9259 	/* Do not run clean AQ when PF reset fails */
9260 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9261 		return;
9262 
9263 	/* check for error indications */
9264 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9265 	oldval = val;
9266 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9267 		if (hw->debug_mask & I40E_DEBUG_AQ)
9268 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9269 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9270 	}
9271 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9272 		if (hw->debug_mask & I40E_DEBUG_AQ)
9273 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9274 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9275 		pf->arq_overflows++;
9276 	}
9277 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9278 		if (hw->debug_mask & I40E_DEBUG_AQ)
9279 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9280 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9281 	}
9282 	if (oldval != val)
9283 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9284 
9285 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9286 	oldval = val;
9287 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9288 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9289 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9290 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9291 	}
9292 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9293 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9294 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9295 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9296 	}
9297 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9298 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9299 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9300 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9301 	}
9302 	if (oldval != val)
9303 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9304 
9305 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9306 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9307 	if (!event.msg_buf)
9308 		return;
9309 
9310 	do {
9311 		ret = i40e_clean_arq_element(hw, &event, &pending);
9312 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9313 			break;
9314 		else if (ret) {
9315 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9316 			break;
9317 		}
9318 
9319 		opcode = le16_to_cpu(event.desc.opcode);
9320 		switch (opcode) {
9321 
9322 		case i40e_aqc_opc_get_link_status:
9323 			i40e_handle_link_event(pf, &event);
9324 			break;
9325 		case i40e_aqc_opc_send_msg_to_pf:
9326 			ret = i40e_vc_process_vf_msg(pf,
9327 					le16_to_cpu(event.desc.retval),
9328 					le32_to_cpu(event.desc.cookie_high),
9329 					le32_to_cpu(event.desc.cookie_low),
9330 					event.msg_buf,
9331 					event.msg_len);
9332 			break;
9333 		case i40e_aqc_opc_lldp_update_mib:
9334 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9335 #ifdef CONFIG_I40E_DCB
9336 			rtnl_lock();
9337 			ret = i40e_handle_lldp_event(pf, &event);
9338 			rtnl_unlock();
9339 #endif /* CONFIG_I40E_DCB */
9340 			break;
9341 		case i40e_aqc_opc_event_lan_overflow:
9342 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9343 			i40e_handle_lan_overflow_event(pf, &event);
9344 			break;
9345 		case i40e_aqc_opc_send_msg_to_peer:
9346 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9347 			break;
9348 		case i40e_aqc_opc_nvm_erase:
9349 		case i40e_aqc_opc_nvm_update:
9350 		case i40e_aqc_opc_oem_post_update:
9351 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9352 				   "ARQ NVM operation 0x%04x completed\n",
9353 				   opcode);
9354 			break;
9355 		default:
9356 			dev_info(&pf->pdev->dev,
9357 				 "ARQ: Unknown event 0x%04x ignored\n",
9358 				 opcode);
9359 			break;
9360 		}
9361 	} while (i++ < pf->adminq_work_limit);
9362 
9363 	if (i < pf->adminq_work_limit)
9364 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9365 
9366 	/* re-enable Admin queue interrupt cause */
9367 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9368 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9369 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9370 	i40e_flush(hw);
9371 
9372 	kfree(event.msg_buf);
9373 }
9374 
9375 /**
9376  * i40e_verify_eeprom - make sure eeprom is good to use
9377  * @pf: board private structure
9378  **/
9379 static void i40e_verify_eeprom(struct i40e_pf *pf)
9380 {
9381 	int err;
9382 
9383 	err = i40e_diag_eeprom_test(&pf->hw);
9384 	if (err) {
9385 		/* retry in case of garbage read */
9386 		err = i40e_diag_eeprom_test(&pf->hw);
9387 		if (err) {
9388 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9389 				 err);
9390 			set_bit(__I40E_BAD_EEPROM, pf->state);
9391 		}
9392 	}
9393 
9394 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9395 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9396 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9397 	}
9398 }
9399 
9400 /**
9401  * i40e_enable_pf_switch_lb
9402  * @pf: pointer to the PF structure
9403  *
9404  * enable switch loop back or die - no point in a return value
9405  **/
9406 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9407 {
9408 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9409 	struct i40e_vsi_context ctxt;
9410 	int ret;
9411 
9412 	ctxt.seid = pf->main_vsi_seid;
9413 	ctxt.pf_num = pf->hw.pf_id;
9414 	ctxt.vf_num = 0;
9415 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9416 	if (ret) {
9417 		dev_info(&pf->pdev->dev,
9418 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9419 			 i40e_stat_str(&pf->hw, ret),
9420 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9421 		return;
9422 	}
9423 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9424 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9425 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9426 
9427 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9428 	if (ret) {
9429 		dev_info(&pf->pdev->dev,
9430 			 "update vsi switch failed, err %s aq_err %s\n",
9431 			 i40e_stat_str(&pf->hw, ret),
9432 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9433 	}
9434 }
9435 
9436 /**
9437  * i40e_disable_pf_switch_lb
9438  * @pf: pointer to the PF structure
9439  *
9440  * disable switch loop back or die - no point in a return value
9441  **/
9442 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9443 {
9444 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9445 	struct i40e_vsi_context ctxt;
9446 	int ret;
9447 
9448 	ctxt.seid = pf->main_vsi_seid;
9449 	ctxt.pf_num = pf->hw.pf_id;
9450 	ctxt.vf_num = 0;
9451 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9452 	if (ret) {
9453 		dev_info(&pf->pdev->dev,
9454 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9455 			 i40e_stat_str(&pf->hw, ret),
9456 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9457 		return;
9458 	}
9459 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9460 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9461 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9462 
9463 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9464 	if (ret) {
9465 		dev_info(&pf->pdev->dev,
9466 			 "update vsi switch failed, err %s aq_err %s\n",
9467 			 i40e_stat_str(&pf->hw, ret),
9468 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9469 	}
9470 }
9471 
9472 /**
9473  * i40e_config_bridge_mode - Configure the HW bridge mode
9474  * @veb: pointer to the bridge instance
9475  *
9476  * Configure the loop back mode for the LAN VSI that is downlink to the
9477  * specified HW bridge instance. It is expected this function is called
9478  * when a new HW bridge is instantiated.
9479  **/
9480 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9481 {
9482 	struct i40e_pf *pf = veb->pf;
9483 
9484 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9485 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9486 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9487 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9488 		i40e_disable_pf_switch_lb(pf);
9489 	else
9490 		i40e_enable_pf_switch_lb(pf);
9491 }
9492 
9493 /**
9494  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9495  * @veb: pointer to the VEB instance
9496  *
9497  * This is a recursive function that first builds the attached VSIs then
9498  * recurses in to build the next layer of VEB.  We track the connections
9499  * through our own index numbers because the seid's from the HW could
9500  * change across the reset.
9501  **/
9502 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9503 {
9504 	struct i40e_vsi *ctl_vsi = NULL;
9505 	struct i40e_pf *pf = veb->pf;
9506 	int v, veb_idx;
9507 	int ret;
9508 
9509 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9510 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9511 		if (pf->vsi[v] &&
9512 		    pf->vsi[v]->veb_idx == veb->idx &&
9513 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9514 			ctl_vsi = pf->vsi[v];
9515 			break;
9516 		}
9517 	}
9518 	if (!ctl_vsi) {
9519 		dev_info(&pf->pdev->dev,
9520 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9521 		ret = -ENOENT;
9522 		goto end_reconstitute;
9523 	}
9524 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9525 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9526 	ret = i40e_add_vsi(ctl_vsi);
9527 	if (ret) {
9528 		dev_info(&pf->pdev->dev,
9529 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9530 			 veb->idx, ret);
9531 		goto end_reconstitute;
9532 	}
9533 	i40e_vsi_reset_stats(ctl_vsi);
9534 
9535 	/* create the VEB in the switch and move the VSI onto the VEB */
9536 	ret = i40e_add_veb(veb, ctl_vsi);
9537 	if (ret)
9538 		goto end_reconstitute;
9539 
9540 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9541 		veb->bridge_mode = BRIDGE_MODE_VEB;
9542 	else
9543 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9544 	i40e_config_bridge_mode(veb);
9545 
9546 	/* create the remaining VSIs attached to this VEB */
9547 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9548 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9549 			continue;
9550 
9551 		if (pf->vsi[v]->veb_idx == veb->idx) {
9552 			struct i40e_vsi *vsi = pf->vsi[v];
9553 
9554 			vsi->uplink_seid = veb->seid;
9555 			ret = i40e_add_vsi(vsi);
9556 			if (ret) {
9557 				dev_info(&pf->pdev->dev,
9558 					 "rebuild of vsi_idx %d failed: %d\n",
9559 					 v, ret);
9560 				goto end_reconstitute;
9561 			}
9562 			i40e_vsi_reset_stats(vsi);
9563 		}
9564 	}
9565 
9566 	/* create any VEBs attached to this VEB - RECURSION */
9567 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9568 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9569 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9570 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9571 			if (ret)
9572 				break;
9573 		}
9574 	}
9575 
9576 end_reconstitute:
9577 	return ret;
9578 }
9579 
9580 /**
9581  * i40e_get_capabilities - get info about the HW
9582  * @pf: the PF struct
9583  **/
9584 static int i40e_get_capabilities(struct i40e_pf *pf,
9585 				 enum i40e_admin_queue_opc list_type)
9586 {
9587 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9588 	u16 data_size;
9589 	int buf_len;
9590 	int err;
9591 
9592 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9593 	do {
9594 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9595 		if (!cap_buf)
9596 			return -ENOMEM;
9597 
9598 		/* this loads the data into the hw struct for us */
9599 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9600 						    &data_size, list_type,
9601 						    NULL);
9602 		/* data loaded, buffer no longer needed */
9603 		kfree(cap_buf);
9604 
9605 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9606 			/* retry with a larger buffer */
9607 			buf_len = data_size;
9608 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9609 			dev_info(&pf->pdev->dev,
9610 				 "capability discovery failed, err %s aq_err %s\n",
9611 				 i40e_stat_str(&pf->hw, err),
9612 				 i40e_aq_str(&pf->hw,
9613 					     pf->hw.aq.asq_last_status));
9614 			return -ENODEV;
9615 		}
9616 	} while (err);
9617 
9618 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9619 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9620 			dev_info(&pf->pdev->dev,
9621 				 "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",
9622 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9623 				 pf->hw.func_caps.num_msix_vectors,
9624 				 pf->hw.func_caps.num_msix_vectors_vf,
9625 				 pf->hw.func_caps.fd_filters_guaranteed,
9626 				 pf->hw.func_caps.fd_filters_best_effort,
9627 				 pf->hw.func_caps.num_tx_qp,
9628 				 pf->hw.func_caps.num_vsis);
9629 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9630 			dev_info(&pf->pdev->dev,
9631 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9632 				 pf->hw.dev_caps.switch_mode,
9633 				 pf->hw.dev_caps.valid_functions);
9634 			dev_info(&pf->pdev->dev,
9635 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9636 				 pf->hw.dev_caps.sr_iov_1_1,
9637 				 pf->hw.dev_caps.num_vfs);
9638 			dev_info(&pf->pdev->dev,
9639 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9640 				 pf->hw.dev_caps.num_vsis,
9641 				 pf->hw.dev_caps.num_rx_qp,
9642 				 pf->hw.dev_caps.num_tx_qp);
9643 		}
9644 	}
9645 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9646 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9647 		       + pf->hw.func_caps.num_vfs)
9648 		if (pf->hw.revision_id == 0 &&
9649 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9650 			dev_info(&pf->pdev->dev,
9651 				 "got num_vsis %d, setting num_vsis to %d\n",
9652 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9653 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9654 		}
9655 	}
9656 	return 0;
9657 }
9658 
9659 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9660 
9661 /**
9662  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9663  * @pf: board private structure
9664  **/
9665 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9666 {
9667 	struct i40e_vsi *vsi;
9668 
9669 	/* quick workaround for an NVM issue that leaves a critical register
9670 	 * uninitialized
9671 	 */
9672 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9673 		static const u32 hkey[] = {
9674 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9675 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9676 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9677 			0x95b3a76d};
9678 		int i;
9679 
9680 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9681 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9682 	}
9683 
9684 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9685 		return;
9686 
9687 	/* find existing VSI and see if it needs configuring */
9688 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9689 
9690 	/* create a new VSI if none exists */
9691 	if (!vsi) {
9692 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9693 				     pf->vsi[pf->lan_vsi]->seid, 0);
9694 		if (!vsi) {
9695 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9696 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9697 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9698 			return;
9699 		}
9700 	}
9701 
9702 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9703 }
9704 
9705 /**
9706  * i40e_fdir_teardown - release the Flow Director resources
9707  * @pf: board private structure
9708  **/
9709 static void i40e_fdir_teardown(struct i40e_pf *pf)
9710 {
9711 	struct i40e_vsi *vsi;
9712 
9713 	i40e_fdir_filter_exit(pf);
9714 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9715 	if (vsi)
9716 		i40e_vsi_release(vsi);
9717 }
9718 
9719 /**
9720  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9721  * @vsi: PF main vsi
9722  * @seid: seid of main or channel VSIs
9723  *
9724  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9725  * existed before reset
9726  **/
9727 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9728 {
9729 	struct i40e_cloud_filter *cfilter;
9730 	struct i40e_pf *pf = vsi->back;
9731 	struct hlist_node *node;
9732 	i40e_status ret;
9733 
9734 	/* Add cloud filters back if they exist */
9735 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9736 				  cloud_node) {
9737 		if (cfilter->seid != seid)
9738 			continue;
9739 
9740 		if (cfilter->dst_port)
9741 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9742 								true);
9743 		else
9744 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9745 
9746 		if (ret) {
9747 			dev_dbg(&pf->pdev->dev,
9748 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9749 				i40e_stat_str(&pf->hw, ret),
9750 				i40e_aq_str(&pf->hw,
9751 					    pf->hw.aq.asq_last_status));
9752 			return ret;
9753 		}
9754 	}
9755 	return 0;
9756 }
9757 
9758 /**
9759  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9760  * @vsi: PF main vsi
9761  *
9762  * Rebuilds channel VSIs if they existed before reset
9763  **/
9764 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9765 {
9766 	struct i40e_channel *ch, *ch_tmp;
9767 	i40e_status ret;
9768 
9769 	if (list_empty(&vsi->ch_list))
9770 		return 0;
9771 
9772 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9773 		if (!ch->initialized)
9774 			break;
9775 		/* Proceed with creation of channel (VMDq2) VSI */
9776 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9777 		if (ret) {
9778 			dev_info(&vsi->back->pdev->dev,
9779 				 "failed to rebuild channels using uplink_seid %u\n",
9780 				 vsi->uplink_seid);
9781 			return ret;
9782 		}
9783 		/* Reconfigure TX queues using QTX_CTL register */
9784 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9785 		if (ret) {
9786 			dev_info(&vsi->back->pdev->dev,
9787 				 "failed to configure TX rings for channel %u\n",
9788 				 ch->seid);
9789 			return ret;
9790 		}
9791 		/* update 'next_base_queue' */
9792 		vsi->next_base_queue = vsi->next_base_queue +
9793 							ch->num_queue_pairs;
9794 		if (ch->max_tx_rate) {
9795 			u64 credits = ch->max_tx_rate;
9796 
9797 			if (i40e_set_bw_limit(vsi, ch->seid,
9798 					      ch->max_tx_rate))
9799 				return -EINVAL;
9800 
9801 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9802 			dev_dbg(&vsi->back->pdev->dev,
9803 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9804 				ch->max_tx_rate,
9805 				credits,
9806 				ch->seid);
9807 		}
9808 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9809 		if (ret) {
9810 			dev_dbg(&vsi->back->pdev->dev,
9811 				"Failed to rebuild cloud filters for channel VSI %u\n",
9812 				ch->seid);
9813 			return ret;
9814 		}
9815 	}
9816 	return 0;
9817 }
9818 
9819 /**
9820  * i40e_prep_for_reset - prep for the core to reset
9821  * @pf: board private structure
9822  * @lock_acquired: indicates whether or not the lock has been acquired
9823  * before this function was called.
9824  *
9825  * Close up the VFs and other things in prep for PF Reset.
9826   **/
9827 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9828 {
9829 	struct i40e_hw *hw = &pf->hw;
9830 	i40e_status ret = 0;
9831 	u32 v;
9832 
9833 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9834 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9835 		return;
9836 	if (i40e_check_asq_alive(&pf->hw))
9837 		i40e_vc_notify_reset(pf);
9838 
9839 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9840 
9841 	/* quiesce the VSIs and their queues that are not already DOWN */
9842 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9843 	if (!lock_acquired)
9844 		rtnl_lock();
9845 	i40e_pf_quiesce_all_vsi(pf);
9846 	if (!lock_acquired)
9847 		rtnl_unlock();
9848 
9849 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9850 		if (pf->vsi[v])
9851 			pf->vsi[v]->seid = 0;
9852 	}
9853 
9854 	i40e_shutdown_adminq(&pf->hw);
9855 
9856 	/* call shutdown HMC */
9857 	if (hw->hmc.hmc_obj) {
9858 		ret = i40e_shutdown_lan_hmc(hw);
9859 		if (ret)
9860 			dev_warn(&pf->pdev->dev,
9861 				 "shutdown_lan_hmc failed: %d\n", ret);
9862 	}
9863 
9864 	/* Save the current PTP time so that we can restore the time after the
9865 	 * reset completes.
9866 	 */
9867 	i40e_ptp_save_hw_time(pf);
9868 }
9869 
9870 /**
9871  * i40e_send_version - update firmware with driver version
9872  * @pf: PF struct
9873  */
9874 static void i40e_send_version(struct i40e_pf *pf)
9875 {
9876 	struct i40e_driver_version dv;
9877 
9878 	dv.major_version = DRV_VERSION_MAJOR;
9879 	dv.minor_version = DRV_VERSION_MINOR;
9880 	dv.build_version = DRV_VERSION_BUILD;
9881 	dv.subbuild_version = 0;
9882 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9883 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9884 }
9885 
9886 /**
9887  * i40e_get_oem_version - get OEM specific version information
9888  * @hw: pointer to the hardware structure
9889  **/
9890 static void i40e_get_oem_version(struct i40e_hw *hw)
9891 {
9892 	u16 block_offset = 0xffff;
9893 	u16 block_length = 0;
9894 	u16 capabilities = 0;
9895 	u16 gen_snap = 0;
9896 	u16 release = 0;
9897 
9898 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9899 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9900 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9901 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9902 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9903 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9904 #define I40E_NVM_OEM_LENGTH			3
9905 
9906 	/* Check if pointer to OEM version block is valid. */
9907 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9908 	if (block_offset == 0xffff)
9909 		return;
9910 
9911 	/* Check if OEM version block has correct length. */
9912 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9913 			   &block_length);
9914 	if (block_length < I40E_NVM_OEM_LENGTH)
9915 		return;
9916 
9917 	/* Check if OEM version format is as expected. */
9918 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9919 			   &capabilities);
9920 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9921 		return;
9922 
9923 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9924 			   &gen_snap);
9925 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9926 			   &release);
9927 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9928 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9929 }
9930 
9931 /**
9932  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9933  * @pf: board private structure
9934  **/
9935 static int i40e_reset(struct i40e_pf *pf)
9936 {
9937 	struct i40e_hw *hw = &pf->hw;
9938 	i40e_status ret;
9939 
9940 	ret = i40e_pf_reset(hw);
9941 	if (ret) {
9942 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9943 		set_bit(__I40E_RESET_FAILED, pf->state);
9944 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9945 	} else {
9946 		pf->pfr_count++;
9947 	}
9948 	return ret;
9949 }
9950 
9951 /**
9952  * i40e_rebuild - rebuild using a saved config
9953  * @pf: board private structure
9954  * @reinit: if the Main VSI needs to re-initialized.
9955  * @lock_acquired: indicates whether or not the lock has been acquired
9956  * before this function was called.
9957  **/
9958 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9959 {
9960 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
9961 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9962 	struct i40e_hw *hw = &pf->hw;
9963 	u8 set_fc_aq_fail = 0;
9964 	i40e_status ret;
9965 	u32 val;
9966 	int v;
9967 
9968 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9969 	    i40e_check_recovery_mode(pf)) {
9970 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
9971 	}
9972 
9973 	if (test_bit(__I40E_DOWN, pf->state) &&
9974 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
9975 	    !old_recovery_mode_bit)
9976 		goto clear_recovery;
9977 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9978 
9979 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9980 	ret = i40e_init_adminq(&pf->hw);
9981 	if (ret) {
9982 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9983 			 i40e_stat_str(&pf->hw, ret),
9984 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9985 		goto clear_recovery;
9986 	}
9987 	i40e_get_oem_version(&pf->hw);
9988 
9989 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9990 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9991 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9992 		/* The following delay is necessary for 4.33 firmware and older
9993 		 * to recover after EMP reset. 200 ms should suffice but we
9994 		 * put here 300 ms to be sure that FW is ready to operate
9995 		 * after reset.
9996 		 */
9997 		mdelay(300);
9998 	}
9999 
10000 	/* re-verify the eeprom if we just had an EMP reset */
10001 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10002 		i40e_verify_eeprom(pf);
10003 
10004 	/* if we are going out of or into recovery mode we have to act
10005 	 * accordingly with regard to resources initialization
10006 	 * and deinitialization
10007 	 */
10008 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10009 	    old_recovery_mode_bit) {
10010 		if (i40e_get_capabilities(pf,
10011 					  i40e_aqc_opc_list_func_capabilities))
10012 			goto end_unlock;
10013 
10014 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10015 			/* we're staying in recovery mode so we'll reinitialize
10016 			 * misc vector here
10017 			 */
10018 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10019 				goto end_unlock;
10020 		} else {
10021 			if (!lock_acquired)
10022 				rtnl_lock();
10023 			/* we're going out of recovery mode so we'll free
10024 			 * the IRQ allocated specifically for recovery mode
10025 			 * and restore the interrupt scheme
10026 			 */
10027 			free_irq(pf->pdev->irq, pf);
10028 			i40e_clear_interrupt_scheme(pf);
10029 			if (i40e_restore_interrupt_scheme(pf))
10030 				goto end_unlock;
10031 		}
10032 
10033 		/* tell the firmware that we're starting */
10034 		i40e_send_version(pf);
10035 
10036 		/* bail out in case recovery mode was detected, as there is
10037 		 * no need for further configuration.
10038 		 */
10039 		goto end_unlock;
10040 	}
10041 
10042 	i40e_clear_pxe_mode(hw);
10043 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10044 	if (ret)
10045 		goto end_core_reset;
10046 
10047 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10048 				hw->func_caps.num_rx_qp, 0, 0);
10049 	if (ret) {
10050 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10051 		goto end_core_reset;
10052 	}
10053 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10054 	if (ret) {
10055 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10056 		goto end_core_reset;
10057 	}
10058 
10059 	/* Enable FW to write a default DCB config on link-up */
10060 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10061 
10062 #ifdef CONFIG_I40E_DCB
10063 	ret = i40e_init_pf_dcb(pf);
10064 	if (ret) {
10065 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10066 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10067 		/* Continue without DCB enabled */
10068 	}
10069 #endif /* CONFIG_I40E_DCB */
10070 	/* do basic switch setup */
10071 	if (!lock_acquired)
10072 		rtnl_lock();
10073 	ret = i40e_setup_pf_switch(pf, reinit);
10074 	if (ret)
10075 		goto end_unlock;
10076 
10077 	/* The driver only wants link up/down and module qualification
10078 	 * reports from firmware.  Note the negative logic.
10079 	 */
10080 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10081 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10082 					 I40E_AQ_EVENT_MEDIA_NA |
10083 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10084 	if (ret)
10085 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10086 			 i40e_stat_str(&pf->hw, ret),
10087 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10088 
10089 	/* make sure our flow control settings are restored */
10090 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
10091 	if (ret)
10092 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
10093 			i40e_stat_str(&pf->hw, ret),
10094 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10095 
10096 	/* Rebuild the VSIs and VEBs that existed before reset.
10097 	 * They are still in our local switch element arrays, so only
10098 	 * need to rebuild the switch model in the HW.
10099 	 *
10100 	 * If there were VEBs but the reconstitution failed, we'll try
10101 	 * try to recover minimal use by getting the basic PF VSI working.
10102 	 */
10103 	if (vsi->uplink_seid != pf->mac_seid) {
10104 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10105 		/* find the one VEB connected to the MAC, and find orphans */
10106 		for (v = 0; v < I40E_MAX_VEB; v++) {
10107 			if (!pf->veb[v])
10108 				continue;
10109 
10110 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10111 			    pf->veb[v]->uplink_seid == 0) {
10112 				ret = i40e_reconstitute_veb(pf->veb[v]);
10113 
10114 				if (!ret)
10115 					continue;
10116 
10117 				/* If Main VEB failed, we're in deep doodoo,
10118 				 * so give up rebuilding the switch and set up
10119 				 * for minimal rebuild of PF VSI.
10120 				 * If orphan failed, we'll report the error
10121 				 * but try to keep going.
10122 				 */
10123 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10124 					dev_info(&pf->pdev->dev,
10125 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10126 						 ret);
10127 					vsi->uplink_seid = pf->mac_seid;
10128 					break;
10129 				} else if (pf->veb[v]->uplink_seid == 0) {
10130 					dev_info(&pf->pdev->dev,
10131 						 "rebuild of orphan VEB failed: %d\n",
10132 						 ret);
10133 				}
10134 			}
10135 		}
10136 	}
10137 
10138 	if (vsi->uplink_seid == pf->mac_seid) {
10139 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10140 		/* no VEB, so rebuild only the Main VSI */
10141 		ret = i40e_add_vsi(vsi);
10142 		if (ret) {
10143 			dev_info(&pf->pdev->dev,
10144 				 "rebuild of Main VSI failed: %d\n", ret);
10145 			goto end_unlock;
10146 		}
10147 	}
10148 
10149 	if (vsi->mqprio_qopt.max_rate[0]) {
10150 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10151 		u64 credits = 0;
10152 
10153 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10154 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10155 		if (ret)
10156 			goto end_unlock;
10157 
10158 		credits = max_tx_rate;
10159 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10160 		dev_dbg(&vsi->back->pdev->dev,
10161 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10162 			max_tx_rate,
10163 			credits,
10164 			vsi->seid);
10165 	}
10166 
10167 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10168 	if (ret)
10169 		goto end_unlock;
10170 
10171 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10172 	 * for this main VSI if they exist
10173 	 */
10174 	ret = i40e_rebuild_channels(vsi);
10175 	if (ret)
10176 		goto end_unlock;
10177 
10178 	/* Reconfigure hardware for allowing smaller MSS in the case
10179 	 * of TSO, so that we avoid the MDD being fired and causing
10180 	 * a reset in the case of small MSS+TSO.
10181 	 */
10182 #define I40E_REG_MSS          0x000E64DC
10183 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10184 #define I40E_64BYTE_MSS       0x400000
10185 	val = rd32(hw, I40E_REG_MSS);
10186 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10187 		val &= ~I40E_REG_MSS_MIN_MASK;
10188 		val |= I40E_64BYTE_MSS;
10189 		wr32(hw, I40E_REG_MSS, val);
10190 	}
10191 
10192 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10193 		msleep(75);
10194 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10195 		if (ret)
10196 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10197 				 i40e_stat_str(&pf->hw, ret),
10198 				 i40e_aq_str(&pf->hw,
10199 					     pf->hw.aq.asq_last_status));
10200 	}
10201 	/* reinit the misc interrupt */
10202 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10203 		ret = i40e_setup_misc_vector(pf);
10204 
10205 	/* Add a filter to drop all Flow control frames from any VSI from being
10206 	 * transmitted. By doing so we stop a malicious VF from sending out
10207 	 * PAUSE or PFC frames and potentially controlling traffic for other
10208 	 * PF/VF VSIs.
10209 	 * The FW can still send Flow control frames if enabled.
10210 	 */
10211 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10212 						       pf->main_vsi_seid);
10213 
10214 	/* restart the VSIs that were rebuilt and running before the reset */
10215 	i40e_pf_unquiesce_all_vsi(pf);
10216 
10217 	/* Release the RTNL lock before we start resetting VFs */
10218 	if (!lock_acquired)
10219 		rtnl_unlock();
10220 
10221 	/* Restore promiscuous settings */
10222 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10223 	if (ret)
10224 		dev_warn(&pf->pdev->dev,
10225 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10226 			 pf->cur_promisc ? "on" : "off",
10227 			 i40e_stat_str(&pf->hw, ret),
10228 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10229 
10230 	i40e_reset_all_vfs(pf, true);
10231 
10232 	/* tell the firmware that we're starting */
10233 	i40e_send_version(pf);
10234 
10235 	/* We've already released the lock, so don't do it again */
10236 	goto end_core_reset;
10237 
10238 end_unlock:
10239 	if (!lock_acquired)
10240 		rtnl_unlock();
10241 end_core_reset:
10242 	clear_bit(__I40E_RESET_FAILED, pf->state);
10243 clear_recovery:
10244 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10245 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10246 }
10247 
10248 /**
10249  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10250  * @pf: board private structure
10251  * @reinit: if the Main VSI needs to re-initialized.
10252  * @lock_acquired: indicates whether or not the lock has been acquired
10253  * before this function was called.
10254  **/
10255 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10256 				   bool lock_acquired)
10257 {
10258 	int ret;
10259 	/* Now we wait for GRST to settle out.
10260 	 * We don't have to delete the VEBs or VSIs from the hw switch
10261 	 * because the reset will make them disappear.
10262 	 */
10263 	ret = i40e_reset(pf);
10264 	if (!ret)
10265 		i40e_rebuild(pf, reinit, lock_acquired);
10266 }
10267 
10268 /**
10269  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10270  * @pf: board private structure
10271  *
10272  * Close up the VFs and other things in prep for a Core Reset,
10273  * then get ready to rebuild the world.
10274  * @lock_acquired: indicates whether or not the lock has been acquired
10275  * before this function was called.
10276  **/
10277 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10278 {
10279 	i40e_prep_for_reset(pf, lock_acquired);
10280 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10281 }
10282 
10283 /**
10284  * i40e_handle_mdd_event
10285  * @pf: pointer to the PF structure
10286  *
10287  * Called from the MDD irq handler to identify possibly malicious vfs
10288  **/
10289 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10290 {
10291 	struct i40e_hw *hw = &pf->hw;
10292 	bool mdd_detected = false;
10293 	struct i40e_vf *vf;
10294 	u32 reg;
10295 	int i;
10296 
10297 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10298 		return;
10299 
10300 	/* find what triggered the MDD event */
10301 	reg = rd32(hw, I40E_GL_MDET_TX);
10302 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10303 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10304 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10305 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10306 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10307 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10308 				I40E_GL_MDET_TX_EVENT_SHIFT;
10309 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10310 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10311 				pf->hw.func_caps.base_queue;
10312 		if (netif_msg_tx_err(pf))
10313 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10314 				 event, queue, pf_num, vf_num);
10315 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10316 		mdd_detected = true;
10317 	}
10318 	reg = rd32(hw, I40E_GL_MDET_RX);
10319 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10320 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10321 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10322 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10323 				I40E_GL_MDET_RX_EVENT_SHIFT;
10324 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10325 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10326 				pf->hw.func_caps.base_queue;
10327 		if (netif_msg_rx_err(pf))
10328 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10329 				 event, queue, func);
10330 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10331 		mdd_detected = true;
10332 	}
10333 
10334 	if (mdd_detected) {
10335 		reg = rd32(hw, I40E_PF_MDET_TX);
10336 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10337 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10338 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10339 		}
10340 		reg = rd32(hw, I40E_PF_MDET_RX);
10341 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10342 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10343 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10344 		}
10345 	}
10346 
10347 	/* see if one of the VFs needs its hand slapped */
10348 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10349 		vf = &(pf->vf[i]);
10350 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10351 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10352 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10353 			vf->num_mdd_events++;
10354 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10355 				 i);
10356 			dev_info(&pf->pdev->dev,
10357 				 "Use PF Control I/F to re-enable the VF\n");
10358 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10359 		}
10360 
10361 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10362 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10363 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10364 			vf->num_mdd_events++;
10365 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10366 				 i);
10367 			dev_info(&pf->pdev->dev,
10368 				 "Use PF Control I/F to re-enable the VF\n");
10369 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10370 		}
10371 	}
10372 
10373 	/* re-enable mdd interrupt cause */
10374 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10375 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10376 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10377 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10378 	i40e_flush(hw);
10379 }
10380 
10381 static const char *i40e_tunnel_name(u8 type)
10382 {
10383 	switch (type) {
10384 	case UDP_TUNNEL_TYPE_VXLAN:
10385 		return "vxlan";
10386 	case UDP_TUNNEL_TYPE_GENEVE:
10387 		return "geneve";
10388 	default:
10389 		return "unknown";
10390 	}
10391 }
10392 
10393 /**
10394  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
10395  * @pf: board private structure
10396  **/
10397 static void i40e_sync_udp_filters(struct i40e_pf *pf)
10398 {
10399 	int i;
10400 
10401 	/* loop through and set pending bit for all active UDP filters */
10402 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
10403 		if (pf->udp_ports[i].port)
10404 			pf->pending_udp_bitmap |= BIT_ULL(i);
10405 	}
10406 
10407 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
10408 }
10409 
10410 /**
10411  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
10412  * @pf: board private structure
10413  **/
10414 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
10415 {
10416 	struct i40e_hw *hw = &pf->hw;
10417 	u8 filter_index, type;
10418 	u16 port;
10419 	int i;
10420 
10421 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
10422 		return;
10423 
10424 	/* acquire RTNL to maintain state of flags and port requests */
10425 	rtnl_lock();
10426 
10427 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
10428 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
10429 			struct i40e_udp_port_config *udp_port;
10430 			i40e_status ret = 0;
10431 
10432 			udp_port = &pf->udp_ports[i];
10433 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
10434 
10435 			port = READ_ONCE(udp_port->port);
10436 			type = READ_ONCE(udp_port->type);
10437 			filter_index = READ_ONCE(udp_port->filter_index);
10438 
10439 			/* release RTNL while we wait on AQ command */
10440 			rtnl_unlock();
10441 
10442 			if (port)
10443 				ret = i40e_aq_add_udp_tunnel(hw, port,
10444 							     type,
10445 							     &filter_index,
10446 							     NULL);
10447 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
10448 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
10449 							     NULL);
10450 
10451 			/* reacquire RTNL so we can update filter_index */
10452 			rtnl_lock();
10453 
10454 			if (ret) {
10455 				dev_info(&pf->pdev->dev,
10456 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
10457 					 i40e_tunnel_name(type),
10458 					 port ? "add" : "delete",
10459 					 port,
10460 					 filter_index,
10461 					 i40e_stat_str(&pf->hw, ret),
10462 					 i40e_aq_str(&pf->hw,
10463 						     pf->hw.aq.asq_last_status));
10464 				if (port) {
10465 					/* failed to add, just reset port,
10466 					 * drop pending bit for any deletion
10467 					 */
10468 					udp_port->port = 0;
10469 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
10470 				}
10471 			} else if (port) {
10472 				/* record filter index on success */
10473 				udp_port->filter_index = filter_index;
10474 			}
10475 		}
10476 	}
10477 
10478 	rtnl_unlock();
10479 }
10480 
10481 /**
10482  * i40e_service_task - Run the driver's async subtasks
10483  * @work: pointer to work_struct containing our data
10484  **/
10485 static void i40e_service_task(struct work_struct *work)
10486 {
10487 	struct i40e_pf *pf = container_of(work,
10488 					  struct i40e_pf,
10489 					  service_task);
10490 	unsigned long start_time = jiffies;
10491 
10492 	/* don't bother with service tasks if a reset is in progress */
10493 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10494 	    test_bit(__I40E_SUSPENDED, pf->state))
10495 		return;
10496 
10497 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10498 		return;
10499 
10500 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10501 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10502 		i40e_sync_filters_subtask(pf);
10503 		i40e_reset_subtask(pf);
10504 		i40e_handle_mdd_event(pf);
10505 		i40e_vc_process_vflr_event(pf);
10506 		i40e_watchdog_subtask(pf);
10507 		i40e_fdir_reinit_subtask(pf);
10508 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10509 			/* Client subtask will reopen next time through. */
10510 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10511 							   true);
10512 		} else {
10513 			i40e_client_subtask(pf);
10514 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10515 					       pf->state))
10516 				i40e_notify_client_of_l2_param_changes(
10517 								pf->vsi[pf->lan_vsi]);
10518 		}
10519 		i40e_sync_filters_subtask(pf);
10520 		i40e_sync_udp_filters_subtask(pf);
10521 	} else {
10522 		i40e_reset_subtask(pf);
10523 	}
10524 
10525 	i40e_clean_adminq_subtask(pf);
10526 
10527 	/* flush memory to make sure state is correct before next watchdog */
10528 	smp_mb__before_atomic();
10529 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10530 
10531 	/* If the tasks have taken longer than one timer cycle or there
10532 	 * is more work to be done, reschedule the service task now
10533 	 * rather than wait for the timer to tick again.
10534 	 */
10535 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10536 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10537 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10538 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10539 		i40e_service_event_schedule(pf);
10540 }
10541 
10542 /**
10543  * i40e_service_timer - timer callback
10544  * @data: pointer to PF struct
10545  **/
10546 static void i40e_service_timer(struct timer_list *t)
10547 {
10548 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10549 
10550 	mod_timer(&pf->service_timer,
10551 		  round_jiffies(jiffies + pf->service_timer_period));
10552 	i40e_service_event_schedule(pf);
10553 }
10554 
10555 /**
10556  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10557  * @vsi: the VSI being configured
10558  **/
10559 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10560 {
10561 	struct i40e_pf *pf = vsi->back;
10562 
10563 	switch (vsi->type) {
10564 	case I40E_VSI_MAIN:
10565 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10566 		if (!vsi->num_tx_desc)
10567 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10568 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10569 		if (!vsi->num_rx_desc)
10570 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10571 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10572 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10573 			vsi->num_q_vectors = pf->num_lan_msix;
10574 		else
10575 			vsi->num_q_vectors = 1;
10576 
10577 		break;
10578 
10579 	case I40E_VSI_FDIR:
10580 		vsi->alloc_queue_pairs = 1;
10581 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10582 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10583 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10584 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10585 		vsi->num_q_vectors = pf->num_fdsb_msix;
10586 		break;
10587 
10588 	case I40E_VSI_VMDQ2:
10589 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10590 		if (!vsi->num_tx_desc)
10591 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10592 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10593 		if (!vsi->num_rx_desc)
10594 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10595 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10596 		vsi->num_q_vectors = pf->num_vmdq_msix;
10597 		break;
10598 
10599 	case I40E_VSI_SRIOV:
10600 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10601 		if (!vsi->num_tx_desc)
10602 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10603 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10604 		if (!vsi->num_rx_desc)
10605 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10606 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10607 		break;
10608 
10609 	default:
10610 		WARN_ON(1);
10611 		return -ENODATA;
10612 	}
10613 
10614 	return 0;
10615 }
10616 
10617 /**
10618  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10619  * @vsi: VSI pointer
10620  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10621  *
10622  * On error: returns error code (negative)
10623  * On success: returns 0
10624  **/
10625 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10626 {
10627 	struct i40e_ring **next_rings;
10628 	int size;
10629 	int ret = 0;
10630 
10631 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10632 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10633 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10634 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10635 	if (!vsi->tx_rings)
10636 		return -ENOMEM;
10637 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10638 	if (i40e_enabled_xdp_vsi(vsi)) {
10639 		vsi->xdp_rings = next_rings;
10640 		next_rings += vsi->alloc_queue_pairs;
10641 	}
10642 	vsi->rx_rings = next_rings;
10643 
10644 	if (alloc_qvectors) {
10645 		/* allocate memory for q_vector pointers */
10646 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10647 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10648 		if (!vsi->q_vectors) {
10649 			ret = -ENOMEM;
10650 			goto err_vectors;
10651 		}
10652 	}
10653 	return ret;
10654 
10655 err_vectors:
10656 	kfree(vsi->tx_rings);
10657 	return ret;
10658 }
10659 
10660 /**
10661  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10662  * @pf: board private structure
10663  * @type: type of VSI
10664  *
10665  * On error: returns error code (negative)
10666  * On success: returns vsi index in PF (positive)
10667  **/
10668 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10669 {
10670 	int ret = -ENODEV;
10671 	struct i40e_vsi *vsi;
10672 	int vsi_idx;
10673 	int i;
10674 
10675 	/* Need to protect the allocation of the VSIs at the PF level */
10676 	mutex_lock(&pf->switch_mutex);
10677 
10678 	/* VSI list may be fragmented if VSI creation/destruction has
10679 	 * been happening.  We can afford to do a quick scan to look
10680 	 * for any free VSIs in the list.
10681 	 *
10682 	 * find next empty vsi slot, looping back around if necessary
10683 	 */
10684 	i = pf->next_vsi;
10685 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10686 		i++;
10687 	if (i >= pf->num_alloc_vsi) {
10688 		i = 0;
10689 		while (i < pf->next_vsi && pf->vsi[i])
10690 			i++;
10691 	}
10692 
10693 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10694 		vsi_idx = i;             /* Found one! */
10695 	} else {
10696 		ret = -ENODEV;
10697 		goto unlock_pf;  /* out of VSI slots! */
10698 	}
10699 	pf->next_vsi = ++i;
10700 
10701 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10702 	if (!vsi) {
10703 		ret = -ENOMEM;
10704 		goto unlock_pf;
10705 	}
10706 	vsi->type = type;
10707 	vsi->back = pf;
10708 	set_bit(__I40E_VSI_DOWN, vsi->state);
10709 	vsi->flags = 0;
10710 	vsi->idx = vsi_idx;
10711 	vsi->int_rate_limit = 0;
10712 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10713 				pf->rss_table_size : 64;
10714 	vsi->netdev_registered = false;
10715 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10716 	hash_init(vsi->mac_filter_hash);
10717 	vsi->irqs_ready = false;
10718 
10719 	if (type == I40E_VSI_MAIN) {
10720 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10721 		if (!vsi->af_xdp_zc_qps)
10722 			goto err_rings;
10723 	}
10724 
10725 	ret = i40e_set_num_rings_in_vsi(vsi);
10726 	if (ret)
10727 		goto err_rings;
10728 
10729 	ret = i40e_vsi_alloc_arrays(vsi, true);
10730 	if (ret)
10731 		goto err_rings;
10732 
10733 	/* Setup default MSIX irq handler for VSI */
10734 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10735 
10736 	/* Initialize VSI lock */
10737 	spin_lock_init(&vsi->mac_filter_hash_lock);
10738 	pf->vsi[vsi_idx] = vsi;
10739 	ret = vsi_idx;
10740 	goto unlock_pf;
10741 
10742 err_rings:
10743 	bitmap_free(vsi->af_xdp_zc_qps);
10744 	pf->next_vsi = i - 1;
10745 	kfree(vsi);
10746 unlock_pf:
10747 	mutex_unlock(&pf->switch_mutex);
10748 	return ret;
10749 }
10750 
10751 /**
10752  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10753  * @vsi: VSI pointer
10754  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10755  *
10756  * On error: returns error code (negative)
10757  * On success: returns 0
10758  **/
10759 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10760 {
10761 	/* free the ring and vector containers */
10762 	if (free_qvectors) {
10763 		kfree(vsi->q_vectors);
10764 		vsi->q_vectors = NULL;
10765 	}
10766 	kfree(vsi->tx_rings);
10767 	vsi->tx_rings = NULL;
10768 	vsi->rx_rings = NULL;
10769 	vsi->xdp_rings = NULL;
10770 }
10771 
10772 /**
10773  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10774  * and lookup table
10775  * @vsi: Pointer to VSI structure
10776  */
10777 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10778 {
10779 	if (!vsi)
10780 		return;
10781 
10782 	kfree(vsi->rss_hkey_user);
10783 	vsi->rss_hkey_user = NULL;
10784 
10785 	kfree(vsi->rss_lut_user);
10786 	vsi->rss_lut_user = NULL;
10787 }
10788 
10789 /**
10790  * i40e_vsi_clear - Deallocate the VSI provided
10791  * @vsi: the VSI being un-configured
10792  **/
10793 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10794 {
10795 	struct i40e_pf *pf;
10796 
10797 	if (!vsi)
10798 		return 0;
10799 
10800 	if (!vsi->back)
10801 		goto free_vsi;
10802 	pf = vsi->back;
10803 
10804 	mutex_lock(&pf->switch_mutex);
10805 	if (!pf->vsi[vsi->idx]) {
10806 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10807 			vsi->idx, vsi->idx, vsi->type);
10808 		goto unlock_vsi;
10809 	}
10810 
10811 	if (pf->vsi[vsi->idx] != vsi) {
10812 		dev_err(&pf->pdev->dev,
10813 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10814 			pf->vsi[vsi->idx]->idx,
10815 			pf->vsi[vsi->idx]->type,
10816 			vsi->idx, vsi->type);
10817 		goto unlock_vsi;
10818 	}
10819 
10820 	/* updates the PF for this cleared vsi */
10821 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10822 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10823 
10824 	bitmap_free(vsi->af_xdp_zc_qps);
10825 	i40e_vsi_free_arrays(vsi, true);
10826 	i40e_clear_rss_config_user(vsi);
10827 
10828 	pf->vsi[vsi->idx] = NULL;
10829 	if (vsi->idx < pf->next_vsi)
10830 		pf->next_vsi = vsi->idx;
10831 
10832 unlock_vsi:
10833 	mutex_unlock(&pf->switch_mutex);
10834 free_vsi:
10835 	kfree(vsi);
10836 
10837 	return 0;
10838 }
10839 
10840 /**
10841  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10842  * @vsi: the VSI being cleaned
10843  **/
10844 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10845 {
10846 	int i;
10847 
10848 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10849 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10850 			kfree_rcu(vsi->tx_rings[i], rcu);
10851 			vsi->tx_rings[i] = NULL;
10852 			vsi->rx_rings[i] = NULL;
10853 			if (vsi->xdp_rings)
10854 				vsi->xdp_rings[i] = NULL;
10855 		}
10856 	}
10857 }
10858 
10859 /**
10860  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10861  * @vsi: the VSI being configured
10862  **/
10863 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10864 {
10865 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10866 	struct i40e_pf *pf = vsi->back;
10867 	struct i40e_ring *ring;
10868 
10869 	/* Set basic values in the rings to be used later during open() */
10870 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10871 		/* allocate space for both Tx and Rx in one shot */
10872 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10873 		if (!ring)
10874 			goto err_out;
10875 
10876 		ring->queue_index = i;
10877 		ring->reg_idx = vsi->base_queue + i;
10878 		ring->ring_active = false;
10879 		ring->vsi = vsi;
10880 		ring->netdev = vsi->netdev;
10881 		ring->dev = &pf->pdev->dev;
10882 		ring->count = vsi->num_tx_desc;
10883 		ring->size = 0;
10884 		ring->dcb_tc = 0;
10885 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10886 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10887 		ring->itr_setting = pf->tx_itr_default;
10888 		vsi->tx_rings[i] = ring++;
10889 
10890 		if (!i40e_enabled_xdp_vsi(vsi))
10891 			goto setup_rx;
10892 
10893 		ring->queue_index = vsi->alloc_queue_pairs + i;
10894 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10895 		ring->ring_active = false;
10896 		ring->vsi = vsi;
10897 		ring->netdev = NULL;
10898 		ring->dev = &pf->pdev->dev;
10899 		ring->count = vsi->num_tx_desc;
10900 		ring->size = 0;
10901 		ring->dcb_tc = 0;
10902 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10903 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10904 		set_ring_xdp(ring);
10905 		ring->itr_setting = pf->tx_itr_default;
10906 		vsi->xdp_rings[i] = ring++;
10907 
10908 setup_rx:
10909 		ring->queue_index = i;
10910 		ring->reg_idx = vsi->base_queue + i;
10911 		ring->ring_active = false;
10912 		ring->vsi = vsi;
10913 		ring->netdev = vsi->netdev;
10914 		ring->dev = &pf->pdev->dev;
10915 		ring->count = vsi->num_rx_desc;
10916 		ring->size = 0;
10917 		ring->dcb_tc = 0;
10918 		ring->itr_setting = pf->rx_itr_default;
10919 		vsi->rx_rings[i] = ring;
10920 	}
10921 
10922 	return 0;
10923 
10924 err_out:
10925 	i40e_vsi_clear_rings(vsi);
10926 	return -ENOMEM;
10927 }
10928 
10929 /**
10930  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10931  * @pf: board private structure
10932  * @vectors: the number of MSI-X vectors to request
10933  *
10934  * Returns the number of vectors reserved, or error
10935  **/
10936 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10937 {
10938 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10939 					I40E_MIN_MSIX, vectors);
10940 	if (vectors < 0) {
10941 		dev_info(&pf->pdev->dev,
10942 			 "MSI-X vector reservation failed: %d\n", vectors);
10943 		vectors = 0;
10944 	}
10945 
10946 	return vectors;
10947 }
10948 
10949 /**
10950  * i40e_init_msix - Setup the MSIX capability
10951  * @pf: board private structure
10952  *
10953  * Work with the OS to set up the MSIX vectors needed.
10954  *
10955  * Returns the number of vectors reserved or negative on failure
10956  **/
10957 static int i40e_init_msix(struct i40e_pf *pf)
10958 {
10959 	struct i40e_hw *hw = &pf->hw;
10960 	int cpus, extra_vectors;
10961 	int vectors_left;
10962 	int v_budget, i;
10963 	int v_actual;
10964 	int iwarp_requested = 0;
10965 
10966 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10967 		return -ENODEV;
10968 
10969 	/* The number of vectors we'll request will be comprised of:
10970 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10971 	 *   - The number of LAN queue pairs
10972 	 *	- Queues being used for RSS.
10973 	 *		We don't need as many as max_rss_size vectors.
10974 	 *		use rss_size instead in the calculation since that
10975 	 *		is governed by number of cpus in the system.
10976 	 *	- assumes symmetric Tx/Rx pairing
10977 	 *   - The number of VMDq pairs
10978 	 *   - The CPU count within the NUMA node if iWARP is enabled
10979 	 * Once we count this up, try the request.
10980 	 *
10981 	 * If we can't get what we want, we'll simplify to nearly nothing
10982 	 * and try again.  If that still fails, we punt.
10983 	 */
10984 	vectors_left = hw->func_caps.num_msix_vectors;
10985 	v_budget = 0;
10986 
10987 	/* reserve one vector for miscellaneous handler */
10988 	if (vectors_left) {
10989 		v_budget++;
10990 		vectors_left--;
10991 	}
10992 
10993 	/* reserve some vectors for the main PF traffic queues. Initially we
10994 	 * only reserve at most 50% of the available vectors, in the case that
10995 	 * the number of online CPUs is large. This ensures that we can enable
10996 	 * extra features as well. Once we've enabled the other features, we
10997 	 * will use any remaining vectors to reach as close as we can to the
10998 	 * number of online CPUs.
10999 	 */
11000 	cpus = num_online_cpus();
11001 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11002 	vectors_left -= pf->num_lan_msix;
11003 
11004 	/* reserve one vector for sideband flow director */
11005 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11006 		if (vectors_left) {
11007 			pf->num_fdsb_msix = 1;
11008 			v_budget++;
11009 			vectors_left--;
11010 		} else {
11011 			pf->num_fdsb_msix = 0;
11012 		}
11013 	}
11014 
11015 	/* can we reserve enough for iWARP? */
11016 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11017 		iwarp_requested = pf->num_iwarp_msix;
11018 
11019 		if (!vectors_left)
11020 			pf->num_iwarp_msix = 0;
11021 		else if (vectors_left < pf->num_iwarp_msix)
11022 			pf->num_iwarp_msix = 1;
11023 		v_budget += pf->num_iwarp_msix;
11024 		vectors_left -= pf->num_iwarp_msix;
11025 	}
11026 
11027 	/* any vectors left over go for VMDq support */
11028 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11029 		if (!vectors_left) {
11030 			pf->num_vmdq_msix = 0;
11031 			pf->num_vmdq_qps = 0;
11032 		} else {
11033 			int vmdq_vecs_wanted =
11034 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11035 			int vmdq_vecs =
11036 				min_t(int, vectors_left, vmdq_vecs_wanted);
11037 
11038 			/* if we're short on vectors for what's desired, we limit
11039 			 * the queues per vmdq.  If this is still more than are
11040 			 * available, the user will need to change the number of
11041 			 * queues/vectors used by the PF later with the ethtool
11042 			 * channels command
11043 			 */
11044 			if (vectors_left < vmdq_vecs_wanted) {
11045 				pf->num_vmdq_qps = 1;
11046 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11047 				vmdq_vecs = min_t(int,
11048 						  vectors_left,
11049 						  vmdq_vecs_wanted);
11050 			}
11051 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11052 
11053 			v_budget += vmdq_vecs;
11054 			vectors_left -= vmdq_vecs;
11055 		}
11056 	}
11057 
11058 	/* On systems with a large number of SMP cores, we previously limited
11059 	 * the number of vectors for num_lan_msix to be at most 50% of the
11060 	 * available vectors, to allow for other features. Now, we add back
11061 	 * the remaining vectors. However, we ensure that the total
11062 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11063 	 * calculate the number of vectors we can add without going over the
11064 	 * cap of CPUs. For systems with a small number of CPUs this will be
11065 	 * zero.
11066 	 */
11067 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11068 	pf->num_lan_msix += extra_vectors;
11069 	vectors_left -= extra_vectors;
11070 
11071 	WARN(vectors_left < 0,
11072 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11073 
11074 	v_budget += pf->num_lan_msix;
11075 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11076 				   GFP_KERNEL);
11077 	if (!pf->msix_entries)
11078 		return -ENOMEM;
11079 
11080 	for (i = 0; i < v_budget; i++)
11081 		pf->msix_entries[i].entry = i;
11082 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11083 
11084 	if (v_actual < I40E_MIN_MSIX) {
11085 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11086 		kfree(pf->msix_entries);
11087 		pf->msix_entries = NULL;
11088 		pci_disable_msix(pf->pdev);
11089 		return -ENODEV;
11090 
11091 	} else if (v_actual == I40E_MIN_MSIX) {
11092 		/* Adjust for minimal MSIX use */
11093 		pf->num_vmdq_vsis = 0;
11094 		pf->num_vmdq_qps = 0;
11095 		pf->num_lan_qps = 1;
11096 		pf->num_lan_msix = 1;
11097 
11098 	} else if (v_actual != v_budget) {
11099 		/* If we have limited resources, we will start with no vectors
11100 		 * for the special features and then allocate vectors to some
11101 		 * of these features based on the policy and at the end disable
11102 		 * the features that did not get any vectors.
11103 		 */
11104 		int vec;
11105 
11106 		dev_info(&pf->pdev->dev,
11107 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11108 			 v_actual, v_budget);
11109 		/* reserve the misc vector */
11110 		vec = v_actual - 1;
11111 
11112 		/* Scale vector usage down */
11113 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11114 		pf->num_vmdq_vsis = 1;
11115 		pf->num_vmdq_qps = 1;
11116 
11117 		/* partition out the remaining vectors */
11118 		switch (vec) {
11119 		case 2:
11120 			pf->num_lan_msix = 1;
11121 			break;
11122 		case 3:
11123 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11124 				pf->num_lan_msix = 1;
11125 				pf->num_iwarp_msix = 1;
11126 			} else {
11127 				pf->num_lan_msix = 2;
11128 			}
11129 			break;
11130 		default:
11131 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11132 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11133 						 iwarp_requested);
11134 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11135 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11136 			} else {
11137 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11138 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11139 			}
11140 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11141 				pf->num_fdsb_msix = 1;
11142 				vec--;
11143 			}
11144 			pf->num_lan_msix = min_t(int,
11145 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11146 							      pf->num_lan_msix);
11147 			pf->num_lan_qps = pf->num_lan_msix;
11148 			break;
11149 		}
11150 	}
11151 
11152 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11153 	    (pf->num_fdsb_msix == 0)) {
11154 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11155 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11156 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11157 	}
11158 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11159 	    (pf->num_vmdq_msix == 0)) {
11160 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11161 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11162 	}
11163 
11164 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11165 	    (pf->num_iwarp_msix == 0)) {
11166 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11167 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11168 	}
11169 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11170 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11171 		   pf->num_lan_msix,
11172 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11173 		   pf->num_fdsb_msix,
11174 		   pf->num_iwarp_msix);
11175 
11176 	return v_actual;
11177 }
11178 
11179 /**
11180  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11181  * @vsi: the VSI being configured
11182  * @v_idx: index of the vector in the vsi struct
11183  * @cpu: cpu to be used on affinity_mask
11184  *
11185  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11186  **/
11187 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
11188 {
11189 	struct i40e_q_vector *q_vector;
11190 
11191 	/* allocate q_vector */
11192 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11193 	if (!q_vector)
11194 		return -ENOMEM;
11195 
11196 	q_vector->vsi = vsi;
11197 	q_vector->v_idx = v_idx;
11198 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11199 
11200 	if (vsi->netdev)
11201 		netif_napi_add(vsi->netdev, &q_vector->napi,
11202 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11203 
11204 	/* tie q_vector and vsi together */
11205 	vsi->q_vectors[v_idx] = q_vector;
11206 
11207 	return 0;
11208 }
11209 
11210 /**
11211  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11212  * @vsi: the VSI being configured
11213  *
11214  * We allocate one q_vector per queue interrupt.  If allocation fails we
11215  * return -ENOMEM.
11216  **/
11217 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11218 {
11219 	struct i40e_pf *pf = vsi->back;
11220 	int err, v_idx, num_q_vectors, current_cpu;
11221 
11222 	/* if not MSIX, give the one vector only to the LAN VSI */
11223 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11224 		num_q_vectors = vsi->num_q_vectors;
11225 	else if (vsi == pf->vsi[pf->lan_vsi])
11226 		num_q_vectors = 1;
11227 	else
11228 		return -EINVAL;
11229 
11230 	current_cpu = cpumask_first(cpu_online_mask);
11231 
11232 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11233 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
11234 		if (err)
11235 			goto err_out;
11236 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
11237 		if (unlikely(current_cpu >= nr_cpu_ids))
11238 			current_cpu = cpumask_first(cpu_online_mask);
11239 	}
11240 
11241 	return 0;
11242 
11243 err_out:
11244 	while (v_idx--)
11245 		i40e_free_q_vector(vsi, v_idx);
11246 
11247 	return err;
11248 }
11249 
11250 /**
11251  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11252  * @pf: board private structure to initialize
11253  **/
11254 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11255 {
11256 	int vectors = 0;
11257 	ssize_t size;
11258 
11259 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11260 		vectors = i40e_init_msix(pf);
11261 		if (vectors < 0) {
11262 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11263 				       I40E_FLAG_IWARP_ENABLED	|
11264 				       I40E_FLAG_RSS_ENABLED	|
11265 				       I40E_FLAG_DCB_CAPABLE	|
11266 				       I40E_FLAG_DCB_ENABLED	|
11267 				       I40E_FLAG_SRIOV_ENABLED	|
11268 				       I40E_FLAG_FD_SB_ENABLED	|
11269 				       I40E_FLAG_FD_ATR_ENABLED	|
11270 				       I40E_FLAG_VMDQ_ENABLED);
11271 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11272 
11273 			/* rework the queue expectations without MSIX */
11274 			i40e_determine_queue_usage(pf);
11275 		}
11276 	}
11277 
11278 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11279 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11280 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11281 		vectors = pci_enable_msi(pf->pdev);
11282 		if (vectors < 0) {
11283 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11284 				 vectors);
11285 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11286 		}
11287 		vectors = 1;  /* one MSI or Legacy vector */
11288 	}
11289 
11290 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11291 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11292 
11293 	/* set up vector assignment tracking */
11294 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11295 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11296 	if (!pf->irq_pile)
11297 		return -ENOMEM;
11298 
11299 	pf->irq_pile->num_entries = vectors;
11300 	pf->irq_pile->search_hint = 0;
11301 
11302 	/* track first vector for misc interrupts, ignore return */
11303 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11304 
11305 	return 0;
11306 }
11307 
11308 /**
11309  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11310  * @pf: private board data structure
11311  *
11312  * Restore the interrupt scheme that was cleared when we suspended the
11313  * device. This should be called during resume to re-allocate the q_vectors
11314  * and reacquire IRQs.
11315  */
11316 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11317 {
11318 	int err, i;
11319 
11320 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11321 	 * scheme. We need to re-enabled them here in order to attempt to
11322 	 * re-acquire the MSI or MSI-X vectors
11323 	 */
11324 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11325 
11326 	err = i40e_init_interrupt_scheme(pf);
11327 	if (err)
11328 		return err;
11329 
11330 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11331 	 * rings together again.
11332 	 */
11333 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11334 		if (pf->vsi[i]) {
11335 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11336 			if (err)
11337 				goto err_unwind;
11338 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11339 		}
11340 	}
11341 
11342 	err = i40e_setup_misc_vector(pf);
11343 	if (err)
11344 		goto err_unwind;
11345 
11346 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11347 		i40e_client_update_msix_info(pf);
11348 
11349 	return 0;
11350 
11351 err_unwind:
11352 	while (i--) {
11353 		if (pf->vsi[i])
11354 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11355 	}
11356 
11357 	return err;
11358 }
11359 
11360 /**
11361  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11362  * non queue events in recovery mode
11363  * @pf: board private structure
11364  *
11365  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11366  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11367  * This is handled differently than in recovery mode since no Tx/Rx resources
11368  * are being allocated.
11369  **/
11370 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11371 {
11372 	int err;
11373 
11374 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11375 		err = i40e_setup_misc_vector(pf);
11376 
11377 		if (err) {
11378 			dev_info(&pf->pdev->dev,
11379 				 "MSI-X misc vector request failed, error %d\n",
11380 				 err);
11381 			return err;
11382 		}
11383 	} else {
11384 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11385 
11386 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11387 				  pf->int_name, pf);
11388 
11389 		if (err) {
11390 			dev_info(&pf->pdev->dev,
11391 				 "MSI/legacy misc vector request failed, error %d\n",
11392 				 err);
11393 			return err;
11394 		}
11395 		i40e_enable_misc_int_causes(pf);
11396 		i40e_irq_dynamic_enable_icr0(pf);
11397 	}
11398 
11399 	return 0;
11400 }
11401 
11402 /**
11403  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11404  * @pf: board private structure
11405  *
11406  * This sets up the handler for MSIX 0, which is used to manage the
11407  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11408  * when in MSI or Legacy interrupt mode.
11409  **/
11410 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11411 {
11412 	struct i40e_hw *hw = &pf->hw;
11413 	int err = 0;
11414 
11415 	/* Only request the IRQ once, the first time through. */
11416 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11417 		err = request_irq(pf->msix_entries[0].vector,
11418 				  i40e_intr, 0, pf->int_name, pf);
11419 		if (err) {
11420 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11421 			dev_info(&pf->pdev->dev,
11422 				 "request_irq for %s failed: %d\n",
11423 				 pf->int_name, err);
11424 			return -EFAULT;
11425 		}
11426 	}
11427 
11428 	i40e_enable_misc_int_causes(pf);
11429 
11430 	/* associate no queues to the misc vector */
11431 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11432 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
11433 
11434 	i40e_flush(hw);
11435 
11436 	i40e_irq_dynamic_enable_icr0(pf);
11437 
11438 	return err;
11439 }
11440 
11441 /**
11442  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11443  * @vsi: Pointer to vsi structure
11444  * @seed: Buffter to store the hash keys
11445  * @lut: Buffer to store the lookup table entries
11446  * @lut_size: Size of buffer to store the lookup table entries
11447  *
11448  * Return 0 on success, negative on failure
11449  */
11450 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11451 			   u8 *lut, u16 lut_size)
11452 {
11453 	struct i40e_pf *pf = vsi->back;
11454 	struct i40e_hw *hw = &pf->hw;
11455 	int ret = 0;
11456 
11457 	if (seed) {
11458 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11459 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11460 		if (ret) {
11461 			dev_info(&pf->pdev->dev,
11462 				 "Cannot get RSS key, err %s aq_err %s\n",
11463 				 i40e_stat_str(&pf->hw, ret),
11464 				 i40e_aq_str(&pf->hw,
11465 					     pf->hw.aq.asq_last_status));
11466 			return ret;
11467 		}
11468 	}
11469 
11470 	if (lut) {
11471 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
11472 
11473 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11474 		if (ret) {
11475 			dev_info(&pf->pdev->dev,
11476 				 "Cannot get RSS lut, err %s aq_err %s\n",
11477 				 i40e_stat_str(&pf->hw, ret),
11478 				 i40e_aq_str(&pf->hw,
11479 					     pf->hw.aq.asq_last_status));
11480 			return ret;
11481 		}
11482 	}
11483 
11484 	return ret;
11485 }
11486 
11487 /**
11488  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11489  * @vsi: Pointer to vsi structure
11490  * @seed: RSS hash seed
11491  * @lut: Lookup table
11492  * @lut_size: Lookup table size
11493  *
11494  * Returns 0 on success, negative on failure
11495  **/
11496 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11497 			       const u8 *lut, u16 lut_size)
11498 {
11499 	struct i40e_pf *pf = vsi->back;
11500 	struct i40e_hw *hw = &pf->hw;
11501 	u16 vf_id = vsi->vf_id;
11502 	u8 i;
11503 
11504 	/* Fill out hash function seed */
11505 	if (seed) {
11506 		u32 *seed_dw = (u32 *)seed;
11507 
11508 		if (vsi->type == I40E_VSI_MAIN) {
11509 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11510 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11511 		} else if (vsi->type == I40E_VSI_SRIOV) {
11512 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11513 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11514 		} else {
11515 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11516 		}
11517 	}
11518 
11519 	if (lut) {
11520 		u32 *lut_dw = (u32 *)lut;
11521 
11522 		if (vsi->type == I40E_VSI_MAIN) {
11523 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11524 				return -EINVAL;
11525 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11526 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11527 		} else if (vsi->type == I40E_VSI_SRIOV) {
11528 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11529 				return -EINVAL;
11530 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11531 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11532 		} else {
11533 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11534 		}
11535 	}
11536 	i40e_flush(hw);
11537 
11538 	return 0;
11539 }
11540 
11541 /**
11542  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11543  * @vsi: Pointer to VSI structure
11544  * @seed: Buffer to store the keys
11545  * @lut: Buffer to store the lookup table entries
11546  * @lut_size: Size of buffer to store the lookup table entries
11547  *
11548  * Returns 0 on success, negative on failure
11549  */
11550 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11551 			    u8 *lut, u16 lut_size)
11552 {
11553 	struct i40e_pf *pf = vsi->back;
11554 	struct i40e_hw *hw = &pf->hw;
11555 	u16 i;
11556 
11557 	if (seed) {
11558 		u32 *seed_dw = (u32 *)seed;
11559 
11560 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11561 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11562 	}
11563 	if (lut) {
11564 		u32 *lut_dw = (u32 *)lut;
11565 
11566 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11567 			return -EINVAL;
11568 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11569 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11570 	}
11571 
11572 	return 0;
11573 }
11574 
11575 /**
11576  * i40e_config_rss - Configure RSS keys and lut
11577  * @vsi: Pointer to VSI structure
11578  * @seed: RSS hash seed
11579  * @lut: Lookup table
11580  * @lut_size: Lookup table size
11581  *
11582  * Returns 0 on success, negative on failure
11583  */
11584 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11585 {
11586 	struct i40e_pf *pf = vsi->back;
11587 
11588 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11589 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11590 	else
11591 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11592 }
11593 
11594 /**
11595  * i40e_get_rss - Get RSS keys and lut
11596  * @vsi: Pointer to VSI structure
11597  * @seed: Buffer to store the keys
11598  * @lut: Buffer to store the lookup table entries
11599  * @lut_size: Size of buffer to store the lookup table entries
11600  *
11601  * Returns 0 on success, negative on failure
11602  */
11603 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11604 {
11605 	struct i40e_pf *pf = vsi->back;
11606 
11607 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11608 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11609 	else
11610 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11611 }
11612 
11613 /**
11614  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11615  * @pf: Pointer to board private structure
11616  * @lut: Lookup table
11617  * @rss_table_size: Lookup table size
11618  * @rss_size: Range of queue number for hashing
11619  */
11620 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11621 		       u16 rss_table_size, u16 rss_size)
11622 {
11623 	u16 i;
11624 
11625 	for (i = 0; i < rss_table_size; i++)
11626 		lut[i] = i % rss_size;
11627 }
11628 
11629 /**
11630  * i40e_pf_config_rss - Prepare for RSS if used
11631  * @pf: board private structure
11632  **/
11633 static int i40e_pf_config_rss(struct i40e_pf *pf)
11634 {
11635 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11636 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11637 	u8 *lut;
11638 	struct i40e_hw *hw = &pf->hw;
11639 	u32 reg_val;
11640 	u64 hena;
11641 	int ret;
11642 
11643 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11644 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11645 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11646 	hena |= i40e_pf_get_default_rss_hena(pf);
11647 
11648 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11649 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11650 
11651 	/* Determine the RSS table size based on the hardware capabilities */
11652 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11653 	reg_val = (pf->rss_table_size == 512) ?
11654 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11655 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11656 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11657 
11658 	/* Determine the RSS size of the VSI */
11659 	if (!vsi->rss_size) {
11660 		u16 qcount;
11661 		/* If the firmware does something weird during VSI init, we
11662 		 * could end up with zero TCs. Check for that to avoid
11663 		 * divide-by-zero. It probably won't pass traffic, but it also
11664 		 * won't panic.
11665 		 */
11666 		qcount = vsi->num_queue_pairs /
11667 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11668 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11669 	}
11670 	if (!vsi->rss_size)
11671 		return -EINVAL;
11672 
11673 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11674 	if (!lut)
11675 		return -ENOMEM;
11676 
11677 	/* Use user configured lut if there is one, otherwise use default */
11678 	if (vsi->rss_lut_user)
11679 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11680 	else
11681 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11682 
11683 	/* Use user configured hash key if there is one, otherwise
11684 	 * use default.
11685 	 */
11686 	if (vsi->rss_hkey_user)
11687 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11688 	else
11689 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11690 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11691 	kfree(lut);
11692 
11693 	return ret;
11694 }
11695 
11696 /**
11697  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11698  * @pf: board private structure
11699  * @queue_count: the requested queue count for rss.
11700  *
11701  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11702  * count which may be different from the requested queue count.
11703  * Note: expects to be called while under rtnl_lock()
11704  **/
11705 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11706 {
11707 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11708 	int new_rss_size;
11709 
11710 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11711 		return 0;
11712 
11713 	queue_count = min_t(int, queue_count, num_online_cpus());
11714 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11715 
11716 	if (queue_count != vsi->num_queue_pairs) {
11717 		u16 qcount;
11718 
11719 		vsi->req_queue_pairs = queue_count;
11720 		i40e_prep_for_reset(pf, true);
11721 
11722 		pf->alloc_rss_size = new_rss_size;
11723 
11724 		i40e_reset_and_rebuild(pf, true, true);
11725 
11726 		/* Discard the user configured hash keys and lut, if less
11727 		 * queues are enabled.
11728 		 */
11729 		if (queue_count < vsi->rss_size) {
11730 			i40e_clear_rss_config_user(vsi);
11731 			dev_dbg(&pf->pdev->dev,
11732 				"discard user configured hash keys and lut\n");
11733 		}
11734 
11735 		/* Reset vsi->rss_size, as number of enabled queues changed */
11736 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11737 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11738 
11739 		i40e_pf_config_rss(pf);
11740 	}
11741 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11742 		 vsi->req_queue_pairs, pf->rss_size_max);
11743 	return pf->alloc_rss_size;
11744 }
11745 
11746 /**
11747  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11748  * @pf: board private structure
11749  **/
11750 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11751 {
11752 	i40e_status status;
11753 	bool min_valid, max_valid;
11754 	u32 max_bw, min_bw;
11755 
11756 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11757 					   &min_valid, &max_valid);
11758 
11759 	if (!status) {
11760 		if (min_valid)
11761 			pf->min_bw = min_bw;
11762 		if (max_valid)
11763 			pf->max_bw = max_bw;
11764 	}
11765 
11766 	return status;
11767 }
11768 
11769 /**
11770  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11771  * @pf: board private structure
11772  **/
11773 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11774 {
11775 	struct i40e_aqc_configure_partition_bw_data bw_data;
11776 	i40e_status status;
11777 
11778 	/* Set the valid bit for this PF */
11779 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11780 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11781 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11782 
11783 	/* Set the new bandwidths */
11784 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11785 
11786 	return status;
11787 }
11788 
11789 /**
11790  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11791  * @pf: board private structure
11792  **/
11793 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11794 {
11795 	/* Commit temporary BW setting to permanent NVM image */
11796 	enum i40e_admin_queue_err last_aq_status;
11797 	i40e_status ret;
11798 	u16 nvm_word;
11799 
11800 	if (pf->hw.partition_id != 1) {
11801 		dev_info(&pf->pdev->dev,
11802 			 "Commit BW only works on partition 1! This is partition %d",
11803 			 pf->hw.partition_id);
11804 		ret = I40E_NOT_SUPPORTED;
11805 		goto bw_commit_out;
11806 	}
11807 
11808 	/* Acquire NVM for read access */
11809 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11810 	last_aq_status = pf->hw.aq.asq_last_status;
11811 	if (ret) {
11812 		dev_info(&pf->pdev->dev,
11813 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11814 			 i40e_stat_str(&pf->hw, ret),
11815 			 i40e_aq_str(&pf->hw, last_aq_status));
11816 		goto bw_commit_out;
11817 	}
11818 
11819 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11820 	ret = i40e_aq_read_nvm(&pf->hw,
11821 			       I40E_SR_NVM_CONTROL_WORD,
11822 			       0x10, sizeof(nvm_word), &nvm_word,
11823 			       false, NULL);
11824 	/* Save off last admin queue command status before releasing
11825 	 * the NVM
11826 	 */
11827 	last_aq_status = pf->hw.aq.asq_last_status;
11828 	i40e_release_nvm(&pf->hw);
11829 	if (ret) {
11830 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11831 			 i40e_stat_str(&pf->hw, ret),
11832 			 i40e_aq_str(&pf->hw, last_aq_status));
11833 		goto bw_commit_out;
11834 	}
11835 
11836 	/* Wait a bit for NVM release to complete */
11837 	msleep(50);
11838 
11839 	/* Acquire NVM for write access */
11840 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11841 	last_aq_status = pf->hw.aq.asq_last_status;
11842 	if (ret) {
11843 		dev_info(&pf->pdev->dev,
11844 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11845 			 i40e_stat_str(&pf->hw, ret),
11846 			 i40e_aq_str(&pf->hw, last_aq_status));
11847 		goto bw_commit_out;
11848 	}
11849 	/* Write it back out unchanged to initiate update NVM,
11850 	 * which will force a write of the shadow (alt) RAM to
11851 	 * the NVM - thus storing the bandwidth values permanently.
11852 	 */
11853 	ret = i40e_aq_update_nvm(&pf->hw,
11854 				 I40E_SR_NVM_CONTROL_WORD,
11855 				 0x10, sizeof(nvm_word),
11856 				 &nvm_word, true, 0, NULL);
11857 	/* Save off last admin queue command status before releasing
11858 	 * the NVM
11859 	 */
11860 	last_aq_status = pf->hw.aq.asq_last_status;
11861 	i40e_release_nvm(&pf->hw);
11862 	if (ret)
11863 		dev_info(&pf->pdev->dev,
11864 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11865 			 i40e_stat_str(&pf->hw, ret),
11866 			 i40e_aq_str(&pf->hw, last_aq_status));
11867 bw_commit_out:
11868 
11869 	return ret;
11870 }
11871 
11872 /**
11873  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11874  * @pf: board private structure to initialize
11875  *
11876  * i40e_sw_init initializes the Adapter private data structure.
11877  * Fields are initialized based on PCI device information and
11878  * OS network device settings (MTU size).
11879  **/
11880 static int i40e_sw_init(struct i40e_pf *pf)
11881 {
11882 	int err = 0;
11883 	int size;
11884 
11885 	/* Set default capability flags */
11886 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11887 		    I40E_FLAG_MSI_ENABLED     |
11888 		    I40E_FLAG_MSIX_ENABLED;
11889 
11890 	/* Set default ITR */
11891 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11892 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11893 
11894 	/* Depending on PF configurations, it is possible that the RSS
11895 	 * maximum might end up larger than the available queues
11896 	 */
11897 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11898 	pf->alloc_rss_size = 1;
11899 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11900 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11901 				 pf->hw.func_caps.num_tx_qp);
11902 	if (pf->hw.func_caps.rss) {
11903 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11904 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11905 					   num_online_cpus());
11906 	}
11907 
11908 	/* MFP mode enabled */
11909 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11910 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11911 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11912 		if (i40e_get_partition_bw_setting(pf)) {
11913 			dev_warn(&pf->pdev->dev,
11914 				 "Could not get partition bw settings\n");
11915 		} else {
11916 			dev_info(&pf->pdev->dev,
11917 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11918 				 pf->min_bw, pf->max_bw);
11919 
11920 			/* nudge the Tx scheduler */
11921 			i40e_set_partition_bw_setting(pf);
11922 		}
11923 	}
11924 
11925 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11926 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11927 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11928 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11929 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11930 		    pf->hw.num_partitions > 1)
11931 			dev_info(&pf->pdev->dev,
11932 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11933 		else
11934 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11935 		pf->fdir_pf_filter_count =
11936 				 pf->hw.func_caps.fd_filters_guaranteed;
11937 		pf->hw.fdir_shared_filter_count =
11938 				 pf->hw.func_caps.fd_filters_best_effort;
11939 	}
11940 
11941 	if (pf->hw.mac.type == I40E_MAC_X722) {
11942 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11943 				    I40E_HW_128_QP_RSS_CAPABLE |
11944 				    I40E_HW_ATR_EVICT_CAPABLE |
11945 				    I40E_HW_WB_ON_ITR_CAPABLE |
11946 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11947 				    I40E_HW_NO_PCI_LINK_CHECK |
11948 				    I40E_HW_USE_SET_LLDP_MIB |
11949 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11950 				    I40E_HW_PTP_L4_CAPABLE |
11951 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11952 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11953 
11954 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11955 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11956 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11957 			dev_warn(&pf->pdev->dev,
11958 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11959 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11960 		}
11961 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11962 		   ((pf->hw.aq.api_maj_ver == 1) &&
11963 		    (pf->hw.aq.api_min_ver > 4))) {
11964 		/* Supported in FW API version higher than 1.4 */
11965 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11966 	}
11967 
11968 	/* Enable HW ATR eviction if possible */
11969 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11970 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11971 
11972 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11973 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11974 	    (pf->hw.aq.fw_maj_ver < 4))) {
11975 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11976 		/* No DCB support  for FW < v4.33 */
11977 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11978 	}
11979 
11980 	/* Disable FW LLDP if FW < v4.3 */
11981 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11982 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11983 	    (pf->hw.aq.fw_maj_ver < 4)))
11984 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11985 
11986 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11987 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11988 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11989 	    (pf->hw.aq.fw_maj_ver >= 5)))
11990 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11991 
11992 	/* Enable PTP L4 if FW > v6.0 */
11993 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11994 	    pf->hw.aq.fw_maj_ver >= 6)
11995 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11996 
11997 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11998 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11999 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12000 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12001 	}
12002 
12003 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12004 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12005 		/* IWARP needs one extra vector for CQP just like MISC.*/
12006 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12007 	}
12008 	/* Stopping FW LLDP engine is supported on XL710 and X722
12009 	 * starting from FW versions determined in i40e_init_adminq.
12010 	 * Stopping the FW LLDP engine is not supported on XL710
12011 	 * if NPAR is functioning so unset this hw flag in this case.
12012 	 */
12013 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12014 	    pf->hw.func_caps.npar_enable &&
12015 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12016 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12017 
12018 #ifdef CONFIG_PCI_IOV
12019 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12020 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12021 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12022 		pf->num_req_vfs = min_t(int,
12023 					pf->hw.func_caps.num_vfs,
12024 					I40E_MAX_VF_COUNT);
12025 	}
12026 #endif /* CONFIG_PCI_IOV */
12027 	pf->eeprom_version = 0xDEAD;
12028 	pf->lan_veb = I40E_NO_VEB;
12029 	pf->lan_vsi = I40E_NO_VSI;
12030 
12031 	/* By default FW has this off for performance reasons */
12032 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12033 
12034 	/* set up queue assignment tracking */
12035 	size = sizeof(struct i40e_lump_tracking)
12036 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12037 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12038 	if (!pf->qp_pile) {
12039 		err = -ENOMEM;
12040 		goto sw_init_done;
12041 	}
12042 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12043 	pf->qp_pile->search_hint = 0;
12044 
12045 	pf->tx_timeout_recovery_level = 1;
12046 
12047 	mutex_init(&pf->switch_mutex);
12048 
12049 sw_init_done:
12050 	return err;
12051 }
12052 
12053 /**
12054  * i40e_set_ntuple - set the ntuple feature flag and take action
12055  * @pf: board private structure to initialize
12056  * @features: the feature set that the stack is suggesting
12057  *
12058  * returns a bool to indicate if reset needs to happen
12059  **/
12060 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12061 {
12062 	bool need_reset = false;
12063 
12064 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12065 	 * the state changed, we need to reset.
12066 	 */
12067 	if (features & NETIF_F_NTUPLE) {
12068 		/* Enable filters and mark for reset */
12069 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12070 			need_reset = true;
12071 		/* enable FD_SB only if there is MSI-X vector and no cloud
12072 		 * filters exist
12073 		 */
12074 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12075 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12076 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12077 		}
12078 	} else {
12079 		/* turn off filters, mark for reset and clear SW filter list */
12080 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12081 			need_reset = true;
12082 			i40e_fdir_filter_exit(pf);
12083 		}
12084 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12085 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12086 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12087 
12088 		/* reset fd counters */
12089 		pf->fd_add_err = 0;
12090 		pf->fd_atr_cnt = 0;
12091 		/* if ATR was auto disabled it can be re-enabled. */
12092 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12093 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12094 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12095 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12096 	}
12097 	return need_reset;
12098 }
12099 
12100 /**
12101  * i40e_clear_rss_lut - clear the rx hash lookup table
12102  * @vsi: the VSI being configured
12103  **/
12104 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12105 {
12106 	struct i40e_pf *pf = vsi->back;
12107 	struct i40e_hw *hw = &pf->hw;
12108 	u16 vf_id = vsi->vf_id;
12109 	u8 i;
12110 
12111 	if (vsi->type == I40E_VSI_MAIN) {
12112 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12113 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12114 	} else if (vsi->type == I40E_VSI_SRIOV) {
12115 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12116 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12117 	} else {
12118 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12119 	}
12120 }
12121 
12122 /**
12123  * i40e_set_features - set the netdev feature flags
12124  * @netdev: ptr to the netdev being adjusted
12125  * @features: the feature set that the stack is suggesting
12126  * Note: expects to be called while under rtnl_lock()
12127  **/
12128 static int i40e_set_features(struct net_device *netdev,
12129 			     netdev_features_t features)
12130 {
12131 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12132 	struct i40e_vsi *vsi = np->vsi;
12133 	struct i40e_pf *pf = vsi->back;
12134 	bool need_reset;
12135 
12136 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12137 		i40e_pf_config_rss(pf);
12138 	else if (!(features & NETIF_F_RXHASH) &&
12139 		 netdev->features & NETIF_F_RXHASH)
12140 		i40e_clear_rss_lut(vsi);
12141 
12142 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12143 		i40e_vlan_stripping_enable(vsi);
12144 	else
12145 		i40e_vlan_stripping_disable(vsi);
12146 
12147 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12148 		dev_err(&pf->pdev->dev,
12149 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12150 		return -EINVAL;
12151 	}
12152 
12153 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12154 		i40e_del_all_macvlans(vsi);
12155 
12156 	need_reset = i40e_set_ntuple(pf, features);
12157 
12158 	if (need_reset)
12159 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12160 
12161 	return 0;
12162 }
12163 
12164 /**
12165  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
12166  * @pf: board private structure
12167  * @port: The UDP port to look up
12168  *
12169  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
12170  **/
12171 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
12172 {
12173 	u8 i;
12174 
12175 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
12176 		/* Do not report ports with pending deletions as
12177 		 * being available.
12178 		 */
12179 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
12180 			continue;
12181 		if (pf->udp_ports[i].port == port)
12182 			return i;
12183 	}
12184 
12185 	return i;
12186 }
12187 
12188 /**
12189  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
12190  * @netdev: This physical port's netdev
12191  * @ti: Tunnel endpoint information
12192  **/
12193 static void i40e_udp_tunnel_add(struct net_device *netdev,
12194 				struct udp_tunnel_info *ti)
12195 {
12196 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12197 	struct i40e_vsi *vsi = np->vsi;
12198 	struct i40e_pf *pf = vsi->back;
12199 	u16 port = ntohs(ti->port);
12200 	u8 next_idx;
12201 	u8 idx;
12202 
12203 	idx = i40e_get_udp_port_idx(pf, port);
12204 
12205 	/* Check if port already exists */
12206 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
12207 		netdev_info(netdev, "port %d already offloaded\n", port);
12208 		return;
12209 	}
12210 
12211 	/* Now check if there is space to add the new port */
12212 	next_idx = i40e_get_udp_port_idx(pf, 0);
12213 
12214 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
12215 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
12216 			    port);
12217 		return;
12218 	}
12219 
12220 	switch (ti->type) {
12221 	case UDP_TUNNEL_TYPE_VXLAN:
12222 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
12223 		break;
12224 	case UDP_TUNNEL_TYPE_GENEVE:
12225 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
12226 			return;
12227 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
12228 		break;
12229 	default:
12230 		return;
12231 	}
12232 
12233 	/* New port: add it and mark its index in the bitmap */
12234 	pf->udp_ports[next_idx].port = port;
12235 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
12236 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
12237 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
12238 }
12239 
12240 /**
12241  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
12242  * @netdev: This physical port's netdev
12243  * @ti: Tunnel endpoint information
12244  **/
12245 static void i40e_udp_tunnel_del(struct net_device *netdev,
12246 				struct udp_tunnel_info *ti)
12247 {
12248 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12249 	struct i40e_vsi *vsi = np->vsi;
12250 	struct i40e_pf *pf = vsi->back;
12251 	u16 port = ntohs(ti->port);
12252 	u8 idx;
12253 
12254 	idx = i40e_get_udp_port_idx(pf, port);
12255 
12256 	/* Check if port already exists */
12257 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
12258 		goto not_found;
12259 
12260 	switch (ti->type) {
12261 	case UDP_TUNNEL_TYPE_VXLAN:
12262 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
12263 			goto not_found;
12264 		break;
12265 	case UDP_TUNNEL_TYPE_GENEVE:
12266 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
12267 			goto not_found;
12268 		break;
12269 	default:
12270 		goto not_found;
12271 	}
12272 
12273 	/* if port exists, set it to 0 (mark for deletion)
12274 	 * and make it pending
12275 	 */
12276 	pf->udp_ports[idx].port = 0;
12277 
12278 	/* Toggle pending bit instead of setting it. This way if we are
12279 	 * deleting a port that has yet to be added we just clear the pending
12280 	 * bit and don't have to worry about it.
12281 	 */
12282 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
12283 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
12284 
12285 	return;
12286 not_found:
12287 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
12288 		    port);
12289 }
12290 
12291 static int i40e_get_phys_port_id(struct net_device *netdev,
12292 				 struct netdev_phys_item_id *ppid)
12293 {
12294 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12295 	struct i40e_pf *pf = np->vsi->back;
12296 	struct i40e_hw *hw = &pf->hw;
12297 
12298 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12299 		return -EOPNOTSUPP;
12300 
12301 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12302 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12303 
12304 	return 0;
12305 }
12306 
12307 /**
12308  * i40e_ndo_fdb_add - add an entry to the hardware database
12309  * @ndm: the input from the stack
12310  * @tb: pointer to array of nladdr (unused)
12311  * @dev: the net device pointer
12312  * @addr: the MAC address entry being added
12313  * @vid: VLAN ID
12314  * @flags: instructions from stack about fdb operation
12315  */
12316 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12317 			    struct net_device *dev,
12318 			    const unsigned char *addr, u16 vid,
12319 			    u16 flags,
12320 			    struct netlink_ext_ack *extack)
12321 {
12322 	struct i40e_netdev_priv *np = netdev_priv(dev);
12323 	struct i40e_pf *pf = np->vsi->back;
12324 	int err = 0;
12325 
12326 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12327 		return -EOPNOTSUPP;
12328 
12329 	if (vid) {
12330 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12331 		return -EINVAL;
12332 	}
12333 
12334 	/* Hardware does not support aging addresses so if a
12335 	 * ndm_state is given only allow permanent addresses
12336 	 */
12337 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12338 		netdev_info(dev, "FDB only supports static addresses\n");
12339 		return -EINVAL;
12340 	}
12341 
12342 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12343 		err = dev_uc_add_excl(dev, addr);
12344 	else if (is_multicast_ether_addr(addr))
12345 		err = dev_mc_add_excl(dev, addr);
12346 	else
12347 		err = -EINVAL;
12348 
12349 	/* Only return duplicate errors if NLM_F_EXCL is set */
12350 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12351 		err = 0;
12352 
12353 	return err;
12354 }
12355 
12356 /**
12357  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12358  * @dev: the netdev being configured
12359  * @nlh: RTNL message
12360  * @flags: bridge flags
12361  * @extack: netlink extended ack
12362  *
12363  * Inserts a new hardware bridge if not already created and
12364  * enables the bridging mode requested (VEB or VEPA). If the
12365  * hardware bridge has already been inserted and the request
12366  * is to change the mode then that requires a PF reset to
12367  * allow rebuild of the components with required hardware
12368  * bridge mode enabled.
12369  *
12370  * Note: expects to be called while under rtnl_lock()
12371  **/
12372 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12373 				   struct nlmsghdr *nlh,
12374 				   u16 flags,
12375 				   struct netlink_ext_ack *extack)
12376 {
12377 	struct i40e_netdev_priv *np = netdev_priv(dev);
12378 	struct i40e_vsi *vsi = np->vsi;
12379 	struct i40e_pf *pf = vsi->back;
12380 	struct i40e_veb *veb = NULL;
12381 	struct nlattr *attr, *br_spec;
12382 	int i, rem;
12383 
12384 	/* Only for PF VSI for now */
12385 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12386 		return -EOPNOTSUPP;
12387 
12388 	/* Find the HW bridge for PF VSI */
12389 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12390 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12391 			veb = pf->veb[i];
12392 	}
12393 
12394 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12395 
12396 	nla_for_each_nested(attr, br_spec, rem) {
12397 		__u16 mode;
12398 
12399 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12400 			continue;
12401 
12402 		mode = nla_get_u16(attr);
12403 		if ((mode != BRIDGE_MODE_VEPA) &&
12404 		    (mode != BRIDGE_MODE_VEB))
12405 			return -EINVAL;
12406 
12407 		/* Insert a new HW bridge */
12408 		if (!veb) {
12409 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12410 					     vsi->tc_config.enabled_tc);
12411 			if (veb) {
12412 				veb->bridge_mode = mode;
12413 				i40e_config_bridge_mode(veb);
12414 			} else {
12415 				/* No Bridge HW offload available */
12416 				return -ENOENT;
12417 			}
12418 			break;
12419 		} else if (mode != veb->bridge_mode) {
12420 			/* Existing HW bridge but different mode needs reset */
12421 			veb->bridge_mode = mode;
12422 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12423 			if (mode == BRIDGE_MODE_VEB)
12424 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12425 			else
12426 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12427 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12428 			break;
12429 		}
12430 	}
12431 
12432 	return 0;
12433 }
12434 
12435 /**
12436  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12437  * @skb: skb buff
12438  * @pid: process id
12439  * @seq: RTNL message seq #
12440  * @dev: the netdev being configured
12441  * @filter_mask: unused
12442  * @nlflags: netlink flags passed in
12443  *
12444  * Return the mode in which the hardware bridge is operating in
12445  * i.e VEB or VEPA.
12446  **/
12447 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12448 				   struct net_device *dev,
12449 				   u32 __always_unused filter_mask,
12450 				   int nlflags)
12451 {
12452 	struct i40e_netdev_priv *np = netdev_priv(dev);
12453 	struct i40e_vsi *vsi = np->vsi;
12454 	struct i40e_pf *pf = vsi->back;
12455 	struct i40e_veb *veb = NULL;
12456 	int i;
12457 
12458 	/* Only for PF VSI for now */
12459 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12460 		return -EOPNOTSUPP;
12461 
12462 	/* Find the HW bridge for the PF VSI */
12463 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12464 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12465 			veb = pf->veb[i];
12466 	}
12467 
12468 	if (!veb)
12469 		return 0;
12470 
12471 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12472 				       0, 0, nlflags, filter_mask, NULL);
12473 }
12474 
12475 /**
12476  * i40e_features_check - Validate encapsulated packet conforms to limits
12477  * @skb: skb buff
12478  * @dev: This physical port's netdev
12479  * @features: Offload features that the stack believes apply
12480  **/
12481 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12482 					     struct net_device *dev,
12483 					     netdev_features_t features)
12484 {
12485 	size_t len;
12486 
12487 	/* No point in doing any of this if neither checksum nor GSO are
12488 	 * being requested for this frame.  We can rule out both by just
12489 	 * checking for CHECKSUM_PARTIAL
12490 	 */
12491 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12492 		return features;
12493 
12494 	/* We cannot support GSO if the MSS is going to be less than
12495 	 * 64 bytes.  If it is then we need to drop support for GSO.
12496 	 */
12497 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12498 		features &= ~NETIF_F_GSO_MASK;
12499 
12500 	/* MACLEN can support at most 63 words */
12501 	len = skb_network_header(skb) - skb->data;
12502 	if (len & ~(63 * 2))
12503 		goto out_err;
12504 
12505 	/* IPLEN and EIPLEN can support at most 127 dwords */
12506 	len = skb_transport_header(skb) - skb_network_header(skb);
12507 	if (len & ~(127 * 4))
12508 		goto out_err;
12509 
12510 	if (skb->encapsulation) {
12511 		/* L4TUNLEN can support 127 words */
12512 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12513 		if (len & ~(127 * 2))
12514 			goto out_err;
12515 
12516 		/* IPLEN can support at most 127 dwords */
12517 		len = skb_inner_transport_header(skb) -
12518 		      skb_inner_network_header(skb);
12519 		if (len & ~(127 * 4))
12520 			goto out_err;
12521 	}
12522 
12523 	/* No need to validate L4LEN as TCP is the only protocol with a
12524 	 * a flexible value and we support all possible values supported
12525 	 * by TCP, which is at most 15 dwords
12526 	 */
12527 
12528 	return features;
12529 out_err:
12530 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12531 }
12532 
12533 /**
12534  * i40e_xdp_setup - add/remove an XDP program
12535  * @vsi: VSI to changed
12536  * @prog: XDP program
12537  **/
12538 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12539 			  struct bpf_prog *prog)
12540 {
12541 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12542 	struct i40e_pf *pf = vsi->back;
12543 	struct bpf_prog *old_prog;
12544 	bool need_reset;
12545 	int i;
12546 
12547 	/* Don't allow frames that span over multiple buffers */
12548 	if (frame_size > vsi->rx_buf_len)
12549 		return -EINVAL;
12550 
12551 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12552 		return 0;
12553 
12554 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12555 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12556 
12557 	if (need_reset)
12558 		i40e_prep_for_reset(pf, true);
12559 
12560 	old_prog = xchg(&vsi->xdp_prog, prog);
12561 
12562 	if (need_reset)
12563 		i40e_reset_and_rebuild(pf, true, true);
12564 
12565 	for (i = 0; i < vsi->num_queue_pairs; i++)
12566 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12567 
12568 	if (old_prog)
12569 		bpf_prog_put(old_prog);
12570 
12571 	/* Kick start the NAPI context if there is an AF_XDP socket open
12572 	 * on that queue id. This so that receiving will start.
12573 	 */
12574 	if (need_reset && prog)
12575 		for (i = 0; i < vsi->num_queue_pairs; i++)
12576 			if (vsi->xdp_rings[i]->xsk_umem)
12577 				(void)i40e_xsk_async_xmit(vsi->netdev, i);
12578 
12579 	return 0;
12580 }
12581 
12582 /**
12583  * i40e_enter_busy_conf - Enters busy config state
12584  * @vsi: vsi
12585  *
12586  * Returns 0 on success, <0 for failure.
12587  **/
12588 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12589 {
12590 	struct i40e_pf *pf = vsi->back;
12591 	int timeout = 50;
12592 
12593 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12594 		timeout--;
12595 		if (!timeout)
12596 			return -EBUSY;
12597 		usleep_range(1000, 2000);
12598 	}
12599 
12600 	return 0;
12601 }
12602 
12603 /**
12604  * i40e_exit_busy_conf - Exits busy config state
12605  * @vsi: vsi
12606  **/
12607 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12608 {
12609 	struct i40e_pf *pf = vsi->back;
12610 
12611 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12612 }
12613 
12614 /**
12615  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12616  * @vsi: vsi
12617  * @queue_pair: queue pair
12618  **/
12619 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12620 {
12621 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12622 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12623 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12624 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12625 	if (i40e_enabled_xdp_vsi(vsi)) {
12626 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12627 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12628 	}
12629 }
12630 
12631 /**
12632  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12633  * @vsi: vsi
12634  * @queue_pair: queue pair
12635  **/
12636 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12637 {
12638 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12639 	if (i40e_enabled_xdp_vsi(vsi)) {
12640 		/* Make sure that in-progress ndo_xdp_xmit calls are
12641 		 * completed.
12642 		 */
12643 		synchronize_rcu();
12644 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12645 	}
12646 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12647 }
12648 
12649 /**
12650  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12651  * @vsi: vsi
12652  * @queue_pair: queue pair
12653  * @enable: true for enable, false for disable
12654  **/
12655 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12656 					bool enable)
12657 {
12658 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12659 	struct i40e_q_vector *q_vector = rxr->q_vector;
12660 
12661 	if (!vsi->netdev)
12662 		return;
12663 
12664 	/* All rings in a qp belong to the same qvector. */
12665 	if (q_vector->rx.ring || q_vector->tx.ring) {
12666 		if (enable)
12667 			napi_enable(&q_vector->napi);
12668 		else
12669 			napi_disable(&q_vector->napi);
12670 	}
12671 }
12672 
12673 /**
12674  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12675  * @vsi: vsi
12676  * @queue_pair: queue pair
12677  * @enable: true for enable, false for disable
12678  *
12679  * Returns 0 on success, <0 on failure.
12680  **/
12681 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12682 					bool enable)
12683 {
12684 	struct i40e_pf *pf = vsi->back;
12685 	int pf_q, ret = 0;
12686 
12687 	pf_q = vsi->base_queue + queue_pair;
12688 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12689 				     false /*is xdp*/, enable);
12690 	if (ret) {
12691 		dev_info(&pf->pdev->dev,
12692 			 "VSI seid %d Tx ring %d %sable timeout\n",
12693 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12694 		return ret;
12695 	}
12696 
12697 	i40e_control_rx_q(pf, pf_q, enable);
12698 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12699 	if (ret) {
12700 		dev_info(&pf->pdev->dev,
12701 			 "VSI seid %d Rx ring %d %sable timeout\n",
12702 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12703 		return ret;
12704 	}
12705 
12706 	/* Due to HW errata, on Rx disable only, the register can
12707 	 * indicate done before it really is. Needs 50ms to be sure
12708 	 */
12709 	if (!enable)
12710 		mdelay(50);
12711 
12712 	if (!i40e_enabled_xdp_vsi(vsi))
12713 		return ret;
12714 
12715 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12716 				     pf_q + vsi->alloc_queue_pairs,
12717 				     true /*is xdp*/, enable);
12718 	if (ret) {
12719 		dev_info(&pf->pdev->dev,
12720 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12721 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12722 	}
12723 
12724 	return ret;
12725 }
12726 
12727 /**
12728  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12729  * @vsi: vsi
12730  * @queue_pair: queue_pair
12731  **/
12732 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12733 {
12734 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12735 	struct i40e_pf *pf = vsi->back;
12736 	struct i40e_hw *hw = &pf->hw;
12737 
12738 	/* All rings in a qp belong to the same qvector. */
12739 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12740 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12741 	else
12742 		i40e_irq_dynamic_enable_icr0(pf);
12743 
12744 	i40e_flush(hw);
12745 }
12746 
12747 /**
12748  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12749  * @vsi: vsi
12750  * @queue_pair: queue_pair
12751  **/
12752 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12753 {
12754 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12755 	struct i40e_pf *pf = vsi->back;
12756 	struct i40e_hw *hw = &pf->hw;
12757 
12758 	/* For simplicity, instead of removing the qp interrupt causes
12759 	 * from the interrupt linked list, we simply disable the interrupt, and
12760 	 * leave the list intact.
12761 	 *
12762 	 * All rings in a qp belong to the same qvector.
12763 	 */
12764 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12765 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12766 
12767 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12768 		i40e_flush(hw);
12769 		synchronize_irq(pf->msix_entries[intpf].vector);
12770 	} else {
12771 		/* Legacy and MSI mode - this stops all interrupt handling */
12772 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12773 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12774 		i40e_flush(hw);
12775 		synchronize_irq(pf->pdev->irq);
12776 	}
12777 }
12778 
12779 /**
12780  * i40e_queue_pair_disable - Disables a queue pair
12781  * @vsi: vsi
12782  * @queue_pair: queue pair
12783  *
12784  * Returns 0 on success, <0 on failure.
12785  **/
12786 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12787 {
12788 	int err;
12789 
12790 	err = i40e_enter_busy_conf(vsi);
12791 	if (err)
12792 		return err;
12793 
12794 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12795 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12796 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12797 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12798 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12799 
12800 	return err;
12801 }
12802 
12803 /**
12804  * i40e_queue_pair_enable - Enables a queue pair
12805  * @vsi: vsi
12806  * @queue_pair: queue pair
12807  *
12808  * Returns 0 on success, <0 on failure.
12809  **/
12810 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12811 {
12812 	int err;
12813 
12814 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12815 	if (err)
12816 		return err;
12817 
12818 	if (i40e_enabled_xdp_vsi(vsi)) {
12819 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12820 		if (err)
12821 			return err;
12822 	}
12823 
12824 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12825 	if (err)
12826 		return err;
12827 
12828 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12829 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12830 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12831 
12832 	i40e_exit_busy_conf(vsi);
12833 
12834 	return err;
12835 }
12836 
12837 /**
12838  * i40e_xdp - implements ndo_bpf for i40e
12839  * @dev: netdevice
12840  * @xdp: XDP command
12841  **/
12842 static int i40e_xdp(struct net_device *dev,
12843 		    struct netdev_bpf *xdp)
12844 {
12845 	struct i40e_netdev_priv *np = netdev_priv(dev);
12846 	struct i40e_vsi *vsi = np->vsi;
12847 
12848 	if (vsi->type != I40E_VSI_MAIN)
12849 		return -EINVAL;
12850 
12851 	switch (xdp->command) {
12852 	case XDP_SETUP_PROG:
12853 		return i40e_xdp_setup(vsi, xdp->prog);
12854 	case XDP_QUERY_PROG:
12855 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
12856 		return 0;
12857 	case XDP_SETUP_XSK_UMEM:
12858 		return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
12859 					   xdp->xsk.queue_id);
12860 	default:
12861 		return -EINVAL;
12862 	}
12863 }
12864 
12865 static const struct net_device_ops i40e_netdev_ops = {
12866 	.ndo_open		= i40e_open,
12867 	.ndo_stop		= i40e_close,
12868 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12869 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12870 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12871 	.ndo_validate_addr	= eth_validate_addr,
12872 	.ndo_set_mac_address	= i40e_set_mac,
12873 	.ndo_change_mtu		= i40e_change_mtu,
12874 	.ndo_do_ioctl		= i40e_ioctl,
12875 	.ndo_tx_timeout		= i40e_tx_timeout,
12876 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12877 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12878 #ifdef CONFIG_NET_POLL_CONTROLLER
12879 	.ndo_poll_controller	= i40e_netpoll,
12880 #endif
12881 	.ndo_setup_tc		= __i40e_setup_tc,
12882 	.ndo_set_features	= i40e_set_features,
12883 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12884 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12885 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12886 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12887 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12888 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12889 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12890 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
12891 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
12892 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12893 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12894 	.ndo_features_check	= i40e_features_check,
12895 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12896 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12897 	.ndo_bpf		= i40e_xdp,
12898 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12899 	.ndo_xsk_async_xmit	= i40e_xsk_async_xmit,
12900 	.ndo_dfwd_add_station	= i40e_fwd_add,
12901 	.ndo_dfwd_del_station	= i40e_fwd_del,
12902 };
12903 
12904 /**
12905  * i40e_config_netdev - Setup the netdev flags
12906  * @vsi: the VSI being configured
12907  *
12908  * Returns 0 on success, negative value on failure
12909  **/
12910 static int i40e_config_netdev(struct i40e_vsi *vsi)
12911 {
12912 	struct i40e_pf *pf = vsi->back;
12913 	struct i40e_hw *hw = &pf->hw;
12914 	struct i40e_netdev_priv *np;
12915 	struct net_device *netdev;
12916 	u8 broadcast[ETH_ALEN];
12917 	u8 mac_addr[ETH_ALEN];
12918 	int etherdev_size;
12919 	netdev_features_t hw_enc_features;
12920 	netdev_features_t hw_features;
12921 
12922 	etherdev_size = sizeof(struct i40e_netdev_priv);
12923 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12924 	if (!netdev)
12925 		return -ENOMEM;
12926 
12927 	vsi->netdev = netdev;
12928 	np = netdev_priv(netdev);
12929 	np->vsi = vsi;
12930 
12931 	hw_enc_features = NETIF_F_SG			|
12932 			  NETIF_F_IP_CSUM		|
12933 			  NETIF_F_IPV6_CSUM		|
12934 			  NETIF_F_HIGHDMA		|
12935 			  NETIF_F_SOFT_FEATURES		|
12936 			  NETIF_F_TSO			|
12937 			  NETIF_F_TSO_ECN		|
12938 			  NETIF_F_TSO6			|
12939 			  NETIF_F_GSO_GRE		|
12940 			  NETIF_F_GSO_GRE_CSUM		|
12941 			  NETIF_F_GSO_PARTIAL		|
12942 			  NETIF_F_GSO_IPXIP4		|
12943 			  NETIF_F_GSO_IPXIP6		|
12944 			  NETIF_F_GSO_UDP_TUNNEL	|
12945 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12946 			  NETIF_F_SCTP_CRC		|
12947 			  NETIF_F_RXHASH		|
12948 			  NETIF_F_RXCSUM		|
12949 			  0;
12950 
12951 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12952 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12953 
12954 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12955 
12956 	netdev->hw_enc_features |= hw_enc_features;
12957 
12958 	/* record features VLANs can make use of */
12959 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12960 
12961 	/* enable macvlan offloads */
12962 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
12963 
12964 	hw_features = hw_enc_features		|
12965 		      NETIF_F_HW_VLAN_CTAG_TX	|
12966 		      NETIF_F_HW_VLAN_CTAG_RX;
12967 
12968 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12969 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12970 
12971 	netdev->hw_features |= hw_features;
12972 
12973 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12974 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12975 
12976 	if (vsi->type == I40E_VSI_MAIN) {
12977 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12978 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12979 		/* The following steps are necessary for two reasons. First,
12980 		 * some older NVM configurations load a default MAC-VLAN
12981 		 * filter that will accept any tagged packet, and we want to
12982 		 * replace this with a normal filter. Additionally, it is
12983 		 * possible our MAC address was provided by the platform using
12984 		 * Open Firmware or similar.
12985 		 *
12986 		 * Thus, we need to remove the default filter and install one
12987 		 * specific to the MAC address.
12988 		 */
12989 		i40e_rm_default_mac_filter(vsi, mac_addr);
12990 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12991 		i40e_add_mac_filter(vsi, mac_addr);
12992 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12993 	} else {
12994 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12995 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12996 		 * the end, which is 4 bytes long, so force truncation of the
12997 		 * original name by IFNAMSIZ - 4
12998 		 */
12999 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13000 			 IFNAMSIZ - 4,
13001 			 pf->vsi[pf->lan_vsi]->netdev->name);
13002 		eth_random_addr(mac_addr);
13003 
13004 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13005 		i40e_add_mac_filter(vsi, mac_addr);
13006 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13007 	}
13008 
13009 	/* Add the broadcast filter so that we initially will receive
13010 	 * broadcast packets. Note that when a new VLAN is first added the
13011 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13012 	 * specific filters as part of transitioning into "vlan" operation.
13013 	 * When more VLANs are added, the driver will copy each existing MAC
13014 	 * filter and add it for the new VLAN.
13015 	 *
13016 	 * Broadcast filters are handled specially by
13017 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13018 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13019 	 * filter. The subtask will update the correct broadcast promiscuous
13020 	 * bits as VLANs become active or inactive.
13021 	 */
13022 	eth_broadcast_addr(broadcast);
13023 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13024 	i40e_add_mac_filter(vsi, broadcast);
13025 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13026 
13027 	ether_addr_copy(netdev->dev_addr, mac_addr);
13028 	ether_addr_copy(netdev->perm_addr, mac_addr);
13029 
13030 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13031 	netdev->neigh_priv_len = sizeof(u32) * 4;
13032 
13033 	netdev->priv_flags |= IFF_UNICAST_FLT;
13034 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13035 	/* Setup netdev TC information */
13036 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13037 
13038 	netdev->netdev_ops = &i40e_netdev_ops;
13039 	netdev->watchdog_timeo = 5 * HZ;
13040 	i40e_set_ethtool_ops(netdev);
13041 
13042 	/* MTU range: 68 - 9706 */
13043 	netdev->min_mtu = ETH_MIN_MTU;
13044 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13045 
13046 	return 0;
13047 }
13048 
13049 /**
13050  * i40e_vsi_delete - Delete a VSI from the switch
13051  * @vsi: the VSI being removed
13052  *
13053  * Returns 0 on success, negative value on failure
13054  **/
13055 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13056 {
13057 	/* remove default VSI is not allowed */
13058 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13059 		return;
13060 
13061 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13062 }
13063 
13064 /**
13065  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13066  * @vsi: the VSI being queried
13067  *
13068  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13069  **/
13070 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13071 {
13072 	struct i40e_veb *veb;
13073 	struct i40e_pf *pf = vsi->back;
13074 
13075 	/* Uplink is not a bridge so default to VEB */
13076 	if (vsi->veb_idx >= I40E_MAX_VEB)
13077 		return 1;
13078 
13079 	veb = pf->veb[vsi->veb_idx];
13080 	if (!veb) {
13081 		dev_info(&pf->pdev->dev,
13082 			 "There is no veb associated with the bridge\n");
13083 		return -ENOENT;
13084 	}
13085 
13086 	/* Uplink is a bridge in VEPA mode */
13087 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13088 		return 0;
13089 	} else {
13090 		/* Uplink is a bridge in VEB mode */
13091 		return 1;
13092 	}
13093 
13094 	/* VEPA is now default bridge, so return 0 */
13095 	return 0;
13096 }
13097 
13098 /**
13099  * i40e_add_vsi - Add a VSI to the switch
13100  * @vsi: the VSI being configured
13101  *
13102  * This initializes a VSI context depending on the VSI type to be added and
13103  * passes it down to the add_vsi aq command.
13104  **/
13105 static int i40e_add_vsi(struct i40e_vsi *vsi)
13106 {
13107 	int ret = -ENODEV;
13108 	struct i40e_pf *pf = vsi->back;
13109 	struct i40e_hw *hw = &pf->hw;
13110 	struct i40e_vsi_context ctxt;
13111 	struct i40e_mac_filter *f;
13112 	struct hlist_node *h;
13113 	int bkt;
13114 
13115 	u8 enabled_tc = 0x1; /* TC0 enabled */
13116 	int f_count = 0;
13117 
13118 	memset(&ctxt, 0, sizeof(ctxt));
13119 	switch (vsi->type) {
13120 	case I40E_VSI_MAIN:
13121 		/* The PF's main VSI is already setup as part of the
13122 		 * device initialization, so we'll not bother with
13123 		 * the add_vsi call, but we will retrieve the current
13124 		 * VSI context.
13125 		 */
13126 		ctxt.seid = pf->main_vsi_seid;
13127 		ctxt.pf_num = pf->hw.pf_id;
13128 		ctxt.vf_num = 0;
13129 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13130 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13131 		if (ret) {
13132 			dev_info(&pf->pdev->dev,
13133 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13134 				 i40e_stat_str(&pf->hw, ret),
13135 				 i40e_aq_str(&pf->hw,
13136 					     pf->hw.aq.asq_last_status));
13137 			return -ENOENT;
13138 		}
13139 		vsi->info = ctxt.info;
13140 		vsi->info.valid_sections = 0;
13141 
13142 		vsi->seid = ctxt.seid;
13143 		vsi->id = ctxt.vsi_number;
13144 
13145 		enabled_tc = i40e_pf_get_tc_map(pf);
13146 
13147 		/* Source pruning is enabled by default, so the flag is
13148 		 * negative logic - if it's set, we need to fiddle with
13149 		 * the VSI to disable source pruning.
13150 		 */
13151 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13152 			memset(&ctxt, 0, sizeof(ctxt));
13153 			ctxt.seid = pf->main_vsi_seid;
13154 			ctxt.pf_num = pf->hw.pf_id;
13155 			ctxt.vf_num = 0;
13156 			ctxt.info.valid_sections |=
13157 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13158 			ctxt.info.switch_id =
13159 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13160 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13161 			if (ret) {
13162 				dev_info(&pf->pdev->dev,
13163 					 "update vsi failed, err %s aq_err %s\n",
13164 					 i40e_stat_str(&pf->hw, ret),
13165 					 i40e_aq_str(&pf->hw,
13166 						     pf->hw.aq.asq_last_status));
13167 				ret = -ENOENT;
13168 				goto err;
13169 			}
13170 		}
13171 
13172 		/* MFP mode setup queue map and update VSI */
13173 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13174 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13175 			memset(&ctxt, 0, sizeof(ctxt));
13176 			ctxt.seid = pf->main_vsi_seid;
13177 			ctxt.pf_num = pf->hw.pf_id;
13178 			ctxt.vf_num = 0;
13179 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13180 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13181 			if (ret) {
13182 				dev_info(&pf->pdev->dev,
13183 					 "update vsi failed, err %s aq_err %s\n",
13184 					 i40e_stat_str(&pf->hw, ret),
13185 					 i40e_aq_str(&pf->hw,
13186 						    pf->hw.aq.asq_last_status));
13187 				ret = -ENOENT;
13188 				goto err;
13189 			}
13190 			/* update the local VSI info queue map */
13191 			i40e_vsi_update_queue_map(vsi, &ctxt);
13192 			vsi->info.valid_sections = 0;
13193 		} else {
13194 			/* Default/Main VSI is only enabled for TC0
13195 			 * reconfigure it to enable all TCs that are
13196 			 * available on the port in SFP mode.
13197 			 * For MFP case the iSCSI PF would use this
13198 			 * flow to enable LAN+iSCSI TC.
13199 			 */
13200 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13201 			if (ret) {
13202 				/* Single TC condition is not fatal,
13203 				 * message and continue
13204 				 */
13205 				dev_info(&pf->pdev->dev,
13206 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13207 					 enabled_tc,
13208 					 i40e_stat_str(&pf->hw, ret),
13209 					 i40e_aq_str(&pf->hw,
13210 						    pf->hw.aq.asq_last_status));
13211 			}
13212 		}
13213 		break;
13214 
13215 	case I40E_VSI_FDIR:
13216 		ctxt.pf_num = hw->pf_id;
13217 		ctxt.vf_num = 0;
13218 		ctxt.uplink_seid = vsi->uplink_seid;
13219 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13220 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13221 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13222 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13223 			ctxt.info.valid_sections |=
13224 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13225 			ctxt.info.switch_id =
13226 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13227 		}
13228 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13229 		break;
13230 
13231 	case I40E_VSI_VMDQ2:
13232 		ctxt.pf_num = hw->pf_id;
13233 		ctxt.vf_num = 0;
13234 		ctxt.uplink_seid = vsi->uplink_seid;
13235 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13236 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13237 
13238 		/* This VSI is connected to VEB so the switch_id
13239 		 * should be set to zero by default.
13240 		 */
13241 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13242 			ctxt.info.valid_sections |=
13243 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13244 			ctxt.info.switch_id =
13245 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13246 		}
13247 
13248 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13249 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13250 		break;
13251 
13252 	case I40E_VSI_SRIOV:
13253 		ctxt.pf_num = hw->pf_id;
13254 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13255 		ctxt.uplink_seid = vsi->uplink_seid;
13256 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13257 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13258 
13259 		/* This VSI is connected to VEB so the switch_id
13260 		 * should be set to zero by default.
13261 		 */
13262 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13263 			ctxt.info.valid_sections |=
13264 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13265 			ctxt.info.switch_id =
13266 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13267 		}
13268 
13269 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13270 			ctxt.info.valid_sections |=
13271 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13272 			ctxt.info.queueing_opt_flags |=
13273 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13274 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13275 		}
13276 
13277 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13278 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13279 		if (pf->vf[vsi->vf_id].spoofchk) {
13280 			ctxt.info.valid_sections |=
13281 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13282 			ctxt.info.sec_flags |=
13283 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13284 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13285 		}
13286 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13287 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13288 		break;
13289 
13290 	case I40E_VSI_IWARP:
13291 		/* send down message to iWARP */
13292 		break;
13293 
13294 	default:
13295 		return -ENODEV;
13296 	}
13297 
13298 	if (vsi->type != I40E_VSI_MAIN) {
13299 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13300 		if (ret) {
13301 			dev_info(&vsi->back->pdev->dev,
13302 				 "add vsi failed, err %s aq_err %s\n",
13303 				 i40e_stat_str(&pf->hw, ret),
13304 				 i40e_aq_str(&pf->hw,
13305 					     pf->hw.aq.asq_last_status));
13306 			ret = -ENOENT;
13307 			goto err;
13308 		}
13309 		vsi->info = ctxt.info;
13310 		vsi->info.valid_sections = 0;
13311 		vsi->seid = ctxt.seid;
13312 		vsi->id = ctxt.vsi_number;
13313 	}
13314 
13315 	vsi->active_filters = 0;
13316 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13317 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13318 	/* If macvlan filters already exist, force them to get loaded */
13319 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13320 		f->state = I40E_FILTER_NEW;
13321 		f_count++;
13322 	}
13323 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13324 
13325 	if (f_count) {
13326 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13327 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13328 	}
13329 
13330 	/* Update VSI BW information */
13331 	ret = i40e_vsi_get_bw_info(vsi);
13332 	if (ret) {
13333 		dev_info(&pf->pdev->dev,
13334 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13335 			 i40e_stat_str(&pf->hw, ret),
13336 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13337 		/* VSI is already added so not tearing that up */
13338 		ret = 0;
13339 	}
13340 
13341 err:
13342 	return ret;
13343 }
13344 
13345 /**
13346  * i40e_vsi_release - Delete a VSI and free its resources
13347  * @vsi: the VSI being removed
13348  *
13349  * Returns 0 on success or < 0 on error
13350  **/
13351 int i40e_vsi_release(struct i40e_vsi *vsi)
13352 {
13353 	struct i40e_mac_filter *f;
13354 	struct hlist_node *h;
13355 	struct i40e_veb *veb = NULL;
13356 	struct i40e_pf *pf;
13357 	u16 uplink_seid;
13358 	int i, n, bkt;
13359 
13360 	pf = vsi->back;
13361 
13362 	/* release of a VEB-owner or last VSI is not allowed */
13363 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13364 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13365 			 vsi->seid, vsi->uplink_seid);
13366 		return -ENODEV;
13367 	}
13368 	if (vsi == pf->vsi[pf->lan_vsi] &&
13369 	    !test_bit(__I40E_DOWN, pf->state)) {
13370 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13371 		return -ENODEV;
13372 	}
13373 
13374 	uplink_seid = vsi->uplink_seid;
13375 	if (vsi->type != I40E_VSI_SRIOV) {
13376 		if (vsi->netdev_registered) {
13377 			vsi->netdev_registered = false;
13378 			if (vsi->netdev) {
13379 				/* results in a call to i40e_close() */
13380 				unregister_netdev(vsi->netdev);
13381 			}
13382 		} else {
13383 			i40e_vsi_close(vsi);
13384 		}
13385 		i40e_vsi_disable_irq(vsi);
13386 	}
13387 
13388 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13389 
13390 	/* clear the sync flag on all filters */
13391 	if (vsi->netdev) {
13392 		__dev_uc_unsync(vsi->netdev, NULL);
13393 		__dev_mc_unsync(vsi->netdev, NULL);
13394 	}
13395 
13396 	/* make sure any remaining filters are marked for deletion */
13397 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13398 		__i40e_del_filter(vsi, f);
13399 
13400 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13401 
13402 	i40e_sync_vsi_filters(vsi);
13403 
13404 	i40e_vsi_delete(vsi);
13405 	i40e_vsi_free_q_vectors(vsi);
13406 	if (vsi->netdev) {
13407 		free_netdev(vsi->netdev);
13408 		vsi->netdev = NULL;
13409 	}
13410 	i40e_vsi_clear_rings(vsi);
13411 	i40e_vsi_clear(vsi);
13412 
13413 	/* If this was the last thing on the VEB, except for the
13414 	 * controlling VSI, remove the VEB, which puts the controlling
13415 	 * VSI onto the next level down in the switch.
13416 	 *
13417 	 * Well, okay, there's one more exception here: don't remove
13418 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13419 	 * from up the network stack.
13420 	 */
13421 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13422 		if (pf->vsi[i] &&
13423 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13424 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13425 			n++;      /* count the VSIs */
13426 		}
13427 	}
13428 	for (i = 0; i < I40E_MAX_VEB; i++) {
13429 		if (!pf->veb[i])
13430 			continue;
13431 		if (pf->veb[i]->uplink_seid == uplink_seid)
13432 			n++;     /* count the VEBs */
13433 		if (pf->veb[i]->seid == uplink_seid)
13434 			veb = pf->veb[i];
13435 	}
13436 	if (n == 0 && veb && veb->uplink_seid != 0)
13437 		i40e_veb_release(veb);
13438 
13439 	return 0;
13440 }
13441 
13442 /**
13443  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13444  * @vsi: ptr to the VSI
13445  *
13446  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13447  * corresponding SW VSI structure and initializes num_queue_pairs for the
13448  * newly allocated VSI.
13449  *
13450  * Returns 0 on success or negative on failure
13451  **/
13452 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13453 {
13454 	int ret = -ENOENT;
13455 	struct i40e_pf *pf = vsi->back;
13456 
13457 	if (vsi->q_vectors[0]) {
13458 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13459 			 vsi->seid);
13460 		return -EEXIST;
13461 	}
13462 
13463 	if (vsi->base_vector) {
13464 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13465 			 vsi->seid, vsi->base_vector);
13466 		return -EEXIST;
13467 	}
13468 
13469 	ret = i40e_vsi_alloc_q_vectors(vsi);
13470 	if (ret) {
13471 		dev_info(&pf->pdev->dev,
13472 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13473 			 vsi->num_q_vectors, vsi->seid, ret);
13474 		vsi->num_q_vectors = 0;
13475 		goto vector_setup_out;
13476 	}
13477 
13478 	/* In Legacy mode, we do not have to get any other vector since we
13479 	 * piggyback on the misc/ICR0 for queue interrupts.
13480 	*/
13481 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13482 		return ret;
13483 	if (vsi->num_q_vectors)
13484 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13485 						 vsi->num_q_vectors, vsi->idx);
13486 	if (vsi->base_vector < 0) {
13487 		dev_info(&pf->pdev->dev,
13488 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13489 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13490 		i40e_vsi_free_q_vectors(vsi);
13491 		ret = -ENOENT;
13492 		goto vector_setup_out;
13493 	}
13494 
13495 vector_setup_out:
13496 	return ret;
13497 }
13498 
13499 /**
13500  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13501  * @vsi: pointer to the vsi.
13502  *
13503  * This re-allocates a vsi's queue resources.
13504  *
13505  * Returns pointer to the successfully allocated and configured VSI sw struct
13506  * on success, otherwise returns NULL on failure.
13507  **/
13508 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13509 {
13510 	u16 alloc_queue_pairs;
13511 	struct i40e_pf *pf;
13512 	u8 enabled_tc;
13513 	int ret;
13514 
13515 	if (!vsi)
13516 		return NULL;
13517 
13518 	pf = vsi->back;
13519 
13520 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13521 	i40e_vsi_clear_rings(vsi);
13522 
13523 	i40e_vsi_free_arrays(vsi, false);
13524 	i40e_set_num_rings_in_vsi(vsi);
13525 	ret = i40e_vsi_alloc_arrays(vsi, false);
13526 	if (ret)
13527 		goto err_vsi;
13528 
13529 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13530 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13531 
13532 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13533 	if (ret < 0) {
13534 		dev_info(&pf->pdev->dev,
13535 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13536 			 alloc_queue_pairs, vsi->seid, ret);
13537 		goto err_vsi;
13538 	}
13539 	vsi->base_queue = ret;
13540 
13541 	/* Update the FW view of the VSI. Force a reset of TC and queue
13542 	 * layout configurations.
13543 	 */
13544 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13545 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13546 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13547 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13548 	if (vsi->type == I40E_VSI_MAIN)
13549 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13550 
13551 	/* assign it some queues */
13552 	ret = i40e_alloc_rings(vsi);
13553 	if (ret)
13554 		goto err_rings;
13555 
13556 	/* map all of the rings to the q_vectors */
13557 	i40e_vsi_map_rings_to_vectors(vsi);
13558 	return vsi;
13559 
13560 err_rings:
13561 	i40e_vsi_free_q_vectors(vsi);
13562 	if (vsi->netdev_registered) {
13563 		vsi->netdev_registered = false;
13564 		unregister_netdev(vsi->netdev);
13565 		free_netdev(vsi->netdev);
13566 		vsi->netdev = NULL;
13567 	}
13568 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13569 err_vsi:
13570 	i40e_vsi_clear(vsi);
13571 	return NULL;
13572 }
13573 
13574 /**
13575  * i40e_vsi_setup - Set up a VSI by a given type
13576  * @pf: board private structure
13577  * @type: VSI type
13578  * @uplink_seid: the switch element to link to
13579  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13580  *
13581  * This allocates the sw VSI structure and its queue resources, then add a VSI
13582  * to the identified VEB.
13583  *
13584  * Returns pointer to the successfully allocated and configure VSI sw struct on
13585  * success, otherwise returns NULL on failure.
13586  **/
13587 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13588 				u16 uplink_seid, u32 param1)
13589 {
13590 	struct i40e_vsi *vsi = NULL;
13591 	struct i40e_veb *veb = NULL;
13592 	u16 alloc_queue_pairs;
13593 	int ret, i;
13594 	int v_idx;
13595 
13596 	/* The requested uplink_seid must be either
13597 	 *     - the PF's port seid
13598 	 *              no VEB is needed because this is the PF
13599 	 *              or this is a Flow Director special case VSI
13600 	 *     - seid of an existing VEB
13601 	 *     - seid of a VSI that owns an existing VEB
13602 	 *     - seid of a VSI that doesn't own a VEB
13603 	 *              a new VEB is created and the VSI becomes the owner
13604 	 *     - seid of the PF VSI, which is what creates the first VEB
13605 	 *              this is a special case of the previous
13606 	 *
13607 	 * Find which uplink_seid we were given and create a new VEB if needed
13608 	 */
13609 	for (i = 0; i < I40E_MAX_VEB; i++) {
13610 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13611 			veb = pf->veb[i];
13612 			break;
13613 		}
13614 	}
13615 
13616 	if (!veb && uplink_seid != pf->mac_seid) {
13617 
13618 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13619 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13620 				vsi = pf->vsi[i];
13621 				break;
13622 			}
13623 		}
13624 		if (!vsi) {
13625 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13626 				 uplink_seid);
13627 			return NULL;
13628 		}
13629 
13630 		if (vsi->uplink_seid == pf->mac_seid)
13631 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13632 					     vsi->tc_config.enabled_tc);
13633 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13634 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13635 					     vsi->tc_config.enabled_tc);
13636 		if (veb) {
13637 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13638 				dev_info(&vsi->back->pdev->dev,
13639 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13640 				return NULL;
13641 			}
13642 			/* We come up by default in VEPA mode if SRIOV is not
13643 			 * already enabled, in which case we can't force VEPA
13644 			 * mode.
13645 			 */
13646 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13647 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13648 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13649 			}
13650 			i40e_config_bridge_mode(veb);
13651 		}
13652 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13653 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13654 				veb = pf->veb[i];
13655 		}
13656 		if (!veb) {
13657 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13658 			return NULL;
13659 		}
13660 
13661 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13662 		uplink_seid = veb->seid;
13663 	}
13664 
13665 	/* get vsi sw struct */
13666 	v_idx = i40e_vsi_mem_alloc(pf, type);
13667 	if (v_idx < 0)
13668 		goto err_alloc;
13669 	vsi = pf->vsi[v_idx];
13670 	if (!vsi)
13671 		goto err_alloc;
13672 	vsi->type = type;
13673 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13674 
13675 	if (type == I40E_VSI_MAIN)
13676 		pf->lan_vsi = v_idx;
13677 	else if (type == I40E_VSI_SRIOV)
13678 		vsi->vf_id = param1;
13679 	/* assign it some queues */
13680 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13681 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13682 
13683 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13684 	if (ret < 0) {
13685 		dev_info(&pf->pdev->dev,
13686 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13687 			 alloc_queue_pairs, vsi->seid, ret);
13688 		goto err_vsi;
13689 	}
13690 	vsi->base_queue = ret;
13691 
13692 	/* get a VSI from the hardware */
13693 	vsi->uplink_seid = uplink_seid;
13694 	ret = i40e_add_vsi(vsi);
13695 	if (ret)
13696 		goto err_vsi;
13697 
13698 	switch (vsi->type) {
13699 	/* setup the netdev if needed */
13700 	case I40E_VSI_MAIN:
13701 	case I40E_VSI_VMDQ2:
13702 		ret = i40e_config_netdev(vsi);
13703 		if (ret)
13704 			goto err_netdev;
13705 		ret = register_netdev(vsi->netdev);
13706 		if (ret)
13707 			goto err_netdev;
13708 		vsi->netdev_registered = true;
13709 		netif_carrier_off(vsi->netdev);
13710 #ifdef CONFIG_I40E_DCB
13711 		/* Setup DCB netlink interface */
13712 		i40e_dcbnl_setup(vsi);
13713 #endif /* CONFIG_I40E_DCB */
13714 		/* fall through */
13715 
13716 	case I40E_VSI_FDIR:
13717 		/* set up vectors and rings if needed */
13718 		ret = i40e_vsi_setup_vectors(vsi);
13719 		if (ret)
13720 			goto err_msix;
13721 
13722 		ret = i40e_alloc_rings(vsi);
13723 		if (ret)
13724 			goto err_rings;
13725 
13726 		/* map all of the rings to the q_vectors */
13727 		i40e_vsi_map_rings_to_vectors(vsi);
13728 
13729 		i40e_vsi_reset_stats(vsi);
13730 		break;
13731 
13732 	default:
13733 		/* no netdev or rings for the other VSI types */
13734 		break;
13735 	}
13736 
13737 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13738 	    (vsi->type == I40E_VSI_VMDQ2)) {
13739 		ret = i40e_vsi_config_rss(vsi);
13740 	}
13741 	return vsi;
13742 
13743 err_rings:
13744 	i40e_vsi_free_q_vectors(vsi);
13745 err_msix:
13746 	if (vsi->netdev_registered) {
13747 		vsi->netdev_registered = false;
13748 		unregister_netdev(vsi->netdev);
13749 		free_netdev(vsi->netdev);
13750 		vsi->netdev = NULL;
13751 	}
13752 err_netdev:
13753 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13754 err_vsi:
13755 	i40e_vsi_clear(vsi);
13756 err_alloc:
13757 	return NULL;
13758 }
13759 
13760 /**
13761  * i40e_veb_get_bw_info - Query VEB BW information
13762  * @veb: the veb to query
13763  *
13764  * Query the Tx scheduler BW configuration data for given VEB
13765  **/
13766 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13767 {
13768 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13769 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13770 	struct i40e_pf *pf = veb->pf;
13771 	struct i40e_hw *hw = &pf->hw;
13772 	u32 tc_bw_max;
13773 	int ret = 0;
13774 	int i;
13775 
13776 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13777 						  &bw_data, NULL);
13778 	if (ret) {
13779 		dev_info(&pf->pdev->dev,
13780 			 "query veb bw config failed, err %s aq_err %s\n",
13781 			 i40e_stat_str(&pf->hw, ret),
13782 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13783 		goto out;
13784 	}
13785 
13786 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13787 						   &ets_data, NULL);
13788 	if (ret) {
13789 		dev_info(&pf->pdev->dev,
13790 			 "query veb bw ets config failed, err %s aq_err %s\n",
13791 			 i40e_stat_str(&pf->hw, ret),
13792 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13793 		goto out;
13794 	}
13795 
13796 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13797 	veb->bw_max_quanta = ets_data.tc_bw_max;
13798 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13799 	veb->enabled_tc = ets_data.tc_valid_bits;
13800 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13801 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13802 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13803 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13804 		veb->bw_tc_limit_credits[i] =
13805 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13806 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13807 	}
13808 
13809 out:
13810 	return ret;
13811 }
13812 
13813 /**
13814  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13815  * @pf: board private structure
13816  *
13817  * On error: returns error code (negative)
13818  * On success: returns vsi index in PF (positive)
13819  **/
13820 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13821 {
13822 	int ret = -ENOENT;
13823 	struct i40e_veb *veb;
13824 	int i;
13825 
13826 	/* Need to protect the allocation of switch elements at the PF level */
13827 	mutex_lock(&pf->switch_mutex);
13828 
13829 	/* VEB list may be fragmented if VEB creation/destruction has
13830 	 * been happening.  We can afford to do a quick scan to look
13831 	 * for any free slots in the list.
13832 	 *
13833 	 * find next empty veb slot, looping back around if necessary
13834 	 */
13835 	i = 0;
13836 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13837 		i++;
13838 	if (i >= I40E_MAX_VEB) {
13839 		ret = -ENOMEM;
13840 		goto err_alloc_veb;  /* out of VEB slots! */
13841 	}
13842 
13843 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13844 	if (!veb) {
13845 		ret = -ENOMEM;
13846 		goto err_alloc_veb;
13847 	}
13848 	veb->pf = pf;
13849 	veb->idx = i;
13850 	veb->enabled_tc = 1;
13851 
13852 	pf->veb[i] = veb;
13853 	ret = i;
13854 err_alloc_veb:
13855 	mutex_unlock(&pf->switch_mutex);
13856 	return ret;
13857 }
13858 
13859 /**
13860  * i40e_switch_branch_release - Delete a branch of the switch tree
13861  * @branch: where to start deleting
13862  *
13863  * This uses recursion to find the tips of the branch to be
13864  * removed, deleting until we get back to and can delete this VEB.
13865  **/
13866 static void i40e_switch_branch_release(struct i40e_veb *branch)
13867 {
13868 	struct i40e_pf *pf = branch->pf;
13869 	u16 branch_seid = branch->seid;
13870 	u16 veb_idx = branch->idx;
13871 	int i;
13872 
13873 	/* release any VEBs on this VEB - RECURSION */
13874 	for (i = 0; i < I40E_MAX_VEB; i++) {
13875 		if (!pf->veb[i])
13876 			continue;
13877 		if (pf->veb[i]->uplink_seid == branch->seid)
13878 			i40e_switch_branch_release(pf->veb[i]);
13879 	}
13880 
13881 	/* Release the VSIs on this VEB, but not the owner VSI.
13882 	 *
13883 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13884 	 *       the VEB itself, so don't use (*branch) after this loop.
13885 	 */
13886 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13887 		if (!pf->vsi[i])
13888 			continue;
13889 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13890 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13891 			i40e_vsi_release(pf->vsi[i]);
13892 		}
13893 	}
13894 
13895 	/* There's one corner case where the VEB might not have been
13896 	 * removed, so double check it here and remove it if needed.
13897 	 * This case happens if the veb was created from the debugfs
13898 	 * commands and no VSIs were added to it.
13899 	 */
13900 	if (pf->veb[veb_idx])
13901 		i40e_veb_release(pf->veb[veb_idx]);
13902 }
13903 
13904 /**
13905  * i40e_veb_clear - remove veb struct
13906  * @veb: the veb to remove
13907  **/
13908 static void i40e_veb_clear(struct i40e_veb *veb)
13909 {
13910 	if (!veb)
13911 		return;
13912 
13913 	if (veb->pf) {
13914 		struct i40e_pf *pf = veb->pf;
13915 
13916 		mutex_lock(&pf->switch_mutex);
13917 		if (pf->veb[veb->idx] == veb)
13918 			pf->veb[veb->idx] = NULL;
13919 		mutex_unlock(&pf->switch_mutex);
13920 	}
13921 
13922 	kfree(veb);
13923 }
13924 
13925 /**
13926  * i40e_veb_release - Delete a VEB and free its resources
13927  * @veb: the VEB being removed
13928  **/
13929 void i40e_veb_release(struct i40e_veb *veb)
13930 {
13931 	struct i40e_vsi *vsi = NULL;
13932 	struct i40e_pf *pf;
13933 	int i, n = 0;
13934 
13935 	pf = veb->pf;
13936 
13937 	/* find the remaining VSI and check for extras */
13938 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13939 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13940 			n++;
13941 			vsi = pf->vsi[i];
13942 		}
13943 	}
13944 	if (n != 1) {
13945 		dev_info(&pf->pdev->dev,
13946 			 "can't remove VEB %d with %d VSIs left\n",
13947 			 veb->seid, n);
13948 		return;
13949 	}
13950 
13951 	/* move the remaining VSI to uplink veb */
13952 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13953 	if (veb->uplink_seid) {
13954 		vsi->uplink_seid = veb->uplink_seid;
13955 		if (veb->uplink_seid == pf->mac_seid)
13956 			vsi->veb_idx = I40E_NO_VEB;
13957 		else
13958 			vsi->veb_idx = veb->veb_idx;
13959 	} else {
13960 		/* floating VEB */
13961 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13962 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13963 	}
13964 
13965 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13966 	i40e_veb_clear(veb);
13967 }
13968 
13969 /**
13970  * i40e_add_veb - create the VEB in the switch
13971  * @veb: the VEB to be instantiated
13972  * @vsi: the controlling VSI
13973  **/
13974 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13975 {
13976 	struct i40e_pf *pf = veb->pf;
13977 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13978 	int ret;
13979 
13980 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13981 			      veb->enabled_tc, false,
13982 			      &veb->seid, enable_stats, NULL);
13983 
13984 	/* get a VEB from the hardware */
13985 	if (ret) {
13986 		dev_info(&pf->pdev->dev,
13987 			 "couldn't add VEB, err %s aq_err %s\n",
13988 			 i40e_stat_str(&pf->hw, ret),
13989 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13990 		return -EPERM;
13991 	}
13992 
13993 	/* get statistics counter */
13994 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13995 					 &veb->stats_idx, NULL, NULL, NULL);
13996 	if (ret) {
13997 		dev_info(&pf->pdev->dev,
13998 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13999 			 i40e_stat_str(&pf->hw, ret),
14000 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14001 		return -EPERM;
14002 	}
14003 	ret = i40e_veb_get_bw_info(veb);
14004 	if (ret) {
14005 		dev_info(&pf->pdev->dev,
14006 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14007 			 i40e_stat_str(&pf->hw, ret),
14008 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14009 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14010 		return -ENOENT;
14011 	}
14012 
14013 	vsi->uplink_seid = veb->seid;
14014 	vsi->veb_idx = veb->idx;
14015 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14016 
14017 	return 0;
14018 }
14019 
14020 /**
14021  * i40e_veb_setup - Set up a VEB
14022  * @pf: board private structure
14023  * @flags: VEB setup flags
14024  * @uplink_seid: the switch element to link to
14025  * @vsi_seid: the initial VSI seid
14026  * @enabled_tc: Enabled TC bit-map
14027  *
14028  * This allocates the sw VEB structure and links it into the switch
14029  * It is possible and legal for this to be a duplicate of an already
14030  * existing VEB.  It is also possible for both uplink and vsi seids
14031  * to be zero, in order to create a floating VEB.
14032  *
14033  * Returns pointer to the successfully allocated VEB sw struct on
14034  * success, otherwise returns NULL on failure.
14035  **/
14036 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14037 				u16 uplink_seid, u16 vsi_seid,
14038 				u8 enabled_tc)
14039 {
14040 	struct i40e_veb *veb, *uplink_veb = NULL;
14041 	int vsi_idx, veb_idx;
14042 	int ret;
14043 
14044 	/* if one seid is 0, the other must be 0 to create a floating relay */
14045 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14046 	    (uplink_seid + vsi_seid != 0)) {
14047 		dev_info(&pf->pdev->dev,
14048 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14049 			 uplink_seid, vsi_seid);
14050 		return NULL;
14051 	}
14052 
14053 	/* make sure there is such a vsi and uplink */
14054 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14055 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14056 			break;
14057 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14058 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14059 			 vsi_seid);
14060 		return NULL;
14061 	}
14062 
14063 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14064 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14065 			if (pf->veb[veb_idx] &&
14066 			    pf->veb[veb_idx]->seid == uplink_seid) {
14067 				uplink_veb = pf->veb[veb_idx];
14068 				break;
14069 			}
14070 		}
14071 		if (!uplink_veb) {
14072 			dev_info(&pf->pdev->dev,
14073 				 "uplink seid %d not found\n", uplink_seid);
14074 			return NULL;
14075 		}
14076 	}
14077 
14078 	/* get veb sw struct */
14079 	veb_idx = i40e_veb_mem_alloc(pf);
14080 	if (veb_idx < 0)
14081 		goto err_alloc;
14082 	veb = pf->veb[veb_idx];
14083 	veb->flags = flags;
14084 	veb->uplink_seid = uplink_seid;
14085 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14086 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14087 
14088 	/* create the VEB in the switch */
14089 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14090 	if (ret)
14091 		goto err_veb;
14092 	if (vsi_idx == pf->lan_vsi)
14093 		pf->lan_veb = veb->idx;
14094 
14095 	return veb;
14096 
14097 err_veb:
14098 	i40e_veb_clear(veb);
14099 err_alloc:
14100 	return NULL;
14101 }
14102 
14103 /**
14104  * i40e_setup_pf_switch_element - set PF vars based on switch type
14105  * @pf: board private structure
14106  * @ele: element we are building info from
14107  * @num_reported: total number of elements
14108  * @printconfig: should we print the contents
14109  *
14110  * helper function to assist in extracting a few useful SEID values.
14111  **/
14112 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14113 				struct i40e_aqc_switch_config_element_resp *ele,
14114 				u16 num_reported, bool printconfig)
14115 {
14116 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14117 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14118 	u8 element_type = ele->element_type;
14119 	u16 seid = le16_to_cpu(ele->seid);
14120 
14121 	if (printconfig)
14122 		dev_info(&pf->pdev->dev,
14123 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14124 			 element_type, seid, uplink_seid, downlink_seid);
14125 
14126 	switch (element_type) {
14127 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14128 		pf->mac_seid = seid;
14129 		break;
14130 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14131 		/* Main VEB? */
14132 		if (uplink_seid != pf->mac_seid)
14133 			break;
14134 		if (pf->lan_veb >= I40E_MAX_VEB) {
14135 			int v;
14136 
14137 			/* find existing or else empty VEB */
14138 			for (v = 0; v < I40E_MAX_VEB; v++) {
14139 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14140 					pf->lan_veb = v;
14141 					break;
14142 				}
14143 			}
14144 			if (pf->lan_veb >= I40E_MAX_VEB) {
14145 				v = i40e_veb_mem_alloc(pf);
14146 				if (v < 0)
14147 					break;
14148 				pf->lan_veb = v;
14149 			}
14150 		}
14151 		if (pf->lan_veb >= I40E_MAX_VEB)
14152 			break;
14153 
14154 		pf->veb[pf->lan_veb]->seid = seid;
14155 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14156 		pf->veb[pf->lan_veb]->pf = pf;
14157 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14158 		break;
14159 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14160 		if (num_reported != 1)
14161 			break;
14162 		/* This is immediately after a reset so we can assume this is
14163 		 * the PF's VSI
14164 		 */
14165 		pf->mac_seid = uplink_seid;
14166 		pf->pf_seid = downlink_seid;
14167 		pf->main_vsi_seid = seid;
14168 		if (printconfig)
14169 			dev_info(&pf->pdev->dev,
14170 				 "pf_seid=%d main_vsi_seid=%d\n",
14171 				 pf->pf_seid, pf->main_vsi_seid);
14172 		break;
14173 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14174 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14175 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14176 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14177 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14178 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14179 		/* ignore these for now */
14180 		break;
14181 	default:
14182 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14183 			 element_type, seid);
14184 		break;
14185 	}
14186 }
14187 
14188 /**
14189  * i40e_fetch_switch_configuration - Get switch config from firmware
14190  * @pf: board private structure
14191  * @printconfig: should we print the contents
14192  *
14193  * Get the current switch configuration from the device and
14194  * extract a few useful SEID values.
14195  **/
14196 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14197 {
14198 	struct i40e_aqc_get_switch_config_resp *sw_config;
14199 	u16 next_seid = 0;
14200 	int ret = 0;
14201 	u8 *aq_buf;
14202 	int i;
14203 
14204 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14205 	if (!aq_buf)
14206 		return -ENOMEM;
14207 
14208 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14209 	do {
14210 		u16 num_reported, num_total;
14211 
14212 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14213 						I40E_AQ_LARGE_BUF,
14214 						&next_seid, NULL);
14215 		if (ret) {
14216 			dev_info(&pf->pdev->dev,
14217 				 "get switch config failed err %s aq_err %s\n",
14218 				 i40e_stat_str(&pf->hw, ret),
14219 				 i40e_aq_str(&pf->hw,
14220 					     pf->hw.aq.asq_last_status));
14221 			kfree(aq_buf);
14222 			return -ENOENT;
14223 		}
14224 
14225 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14226 		num_total = le16_to_cpu(sw_config->header.num_total);
14227 
14228 		if (printconfig)
14229 			dev_info(&pf->pdev->dev,
14230 				 "header: %d reported %d total\n",
14231 				 num_reported, num_total);
14232 
14233 		for (i = 0; i < num_reported; i++) {
14234 			struct i40e_aqc_switch_config_element_resp *ele =
14235 				&sw_config->element[i];
14236 
14237 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14238 						     printconfig);
14239 		}
14240 	} while (next_seid != 0);
14241 
14242 	kfree(aq_buf);
14243 	return ret;
14244 }
14245 
14246 /**
14247  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14248  * @pf: board private structure
14249  * @reinit: if the Main VSI needs to re-initialized.
14250  *
14251  * Returns 0 on success, negative value on failure
14252  **/
14253 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
14254 {
14255 	u16 flags = 0;
14256 	int ret;
14257 
14258 	/* find out what's out there already */
14259 	ret = i40e_fetch_switch_configuration(pf, false);
14260 	if (ret) {
14261 		dev_info(&pf->pdev->dev,
14262 			 "couldn't fetch switch config, err %s aq_err %s\n",
14263 			 i40e_stat_str(&pf->hw, ret),
14264 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14265 		return ret;
14266 	}
14267 	i40e_pf_reset_stats(pf);
14268 
14269 	/* set the switch config bit for the whole device to
14270 	 * support limited promisc or true promisc
14271 	 * when user requests promisc. The default is limited
14272 	 * promisc.
14273 	*/
14274 
14275 	if ((pf->hw.pf_id == 0) &&
14276 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14277 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14278 		pf->last_sw_conf_flags = flags;
14279 	}
14280 
14281 	if (pf->hw.pf_id == 0) {
14282 		u16 valid_flags;
14283 
14284 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14285 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14286 						NULL);
14287 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14288 			dev_info(&pf->pdev->dev,
14289 				 "couldn't set switch config bits, err %s aq_err %s\n",
14290 				 i40e_stat_str(&pf->hw, ret),
14291 				 i40e_aq_str(&pf->hw,
14292 					     pf->hw.aq.asq_last_status));
14293 			/* not a fatal problem, just keep going */
14294 		}
14295 		pf->last_sw_conf_valid_flags = valid_flags;
14296 	}
14297 
14298 	/* first time setup */
14299 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14300 		struct i40e_vsi *vsi = NULL;
14301 		u16 uplink_seid;
14302 
14303 		/* Set up the PF VSI associated with the PF's main VSI
14304 		 * that is already in the HW switch
14305 		 */
14306 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14307 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14308 		else
14309 			uplink_seid = pf->mac_seid;
14310 		if (pf->lan_vsi == I40E_NO_VSI)
14311 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14312 		else if (reinit)
14313 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14314 		if (!vsi) {
14315 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14316 			i40e_cloud_filter_exit(pf);
14317 			i40e_fdir_teardown(pf);
14318 			return -EAGAIN;
14319 		}
14320 	} else {
14321 		/* force a reset of TC and queue layout configurations */
14322 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14323 
14324 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14325 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14326 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14327 	}
14328 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14329 
14330 	i40e_fdir_sb_setup(pf);
14331 
14332 	/* Setup static PF queue filter control settings */
14333 	ret = i40e_setup_pf_filter_control(pf);
14334 	if (ret) {
14335 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14336 			 ret);
14337 		/* Failure here should not stop continuing other steps */
14338 	}
14339 
14340 	/* enable RSS in the HW, even for only one queue, as the stack can use
14341 	 * the hash
14342 	 */
14343 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14344 		i40e_pf_config_rss(pf);
14345 
14346 	/* fill in link information and enable LSE reporting */
14347 	i40e_link_event(pf);
14348 
14349 	/* Initialize user-specific link properties */
14350 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14351 				  I40E_AQ_AN_COMPLETED) ? true : false);
14352 
14353 	i40e_ptp_init(pf);
14354 
14355 	/* repopulate tunnel port filters */
14356 	i40e_sync_udp_filters(pf);
14357 
14358 	return ret;
14359 }
14360 
14361 /**
14362  * i40e_determine_queue_usage - Work out queue distribution
14363  * @pf: board private structure
14364  **/
14365 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14366 {
14367 	int queues_left;
14368 	int q_max;
14369 
14370 	pf->num_lan_qps = 0;
14371 
14372 	/* Find the max queues to be put into basic use.  We'll always be
14373 	 * using TC0, whether or not DCB is running, and TC0 will get the
14374 	 * big RSS set.
14375 	 */
14376 	queues_left = pf->hw.func_caps.num_tx_qp;
14377 
14378 	if ((queues_left == 1) ||
14379 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14380 		/* one qp for PF, no queues for anything else */
14381 		queues_left = 0;
14382 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14383 
14384 		/* make sure all the fancies are disabled */
14385 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14386 			       I40E_FLAG_IWARP_ENABLED	|
14387 			       I40E_FLAG_FD_SB_ENABLED	|
14388 			       I40E_FLAG_FD_ATR_ENABLED	|
14389 			       I40E_FLAG_DCB_CAPABLE	|
14390 			       I40E_FLAG_DCB_ENABLED	|
14391 			       I40E_FLAG_SRIOV_ENABLED	|
14392 			       I40E_FLAG_VMDQ_ENABLED);
14393 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14394 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14395 				  I40E_FLAG_FD_SB_ENABLED |
14396 				  I40E_FLAG_FD_ATR_ENABLED |
14397 				  I40E_FLAG_DCB_CAPABLE))) {
14398 		/* one qp for PF */
14399 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14400 		queues_left -= pf->num_lan_qps;
14401 
14402 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14403 			       I40E_FLAG_IWARP_ENABLED	|
14404 			       I40E_FLAG_FD_SB_ENABLED	|
14405 			       I40E_FLAG_FD_ATR_ENABLED	|
14406 			       I40E_FLAG_DCB_ENABLED	|
14407 			       I40E_FLAG_VMDQ_ENABLED);
14408 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14409 	} else {
14410 		/* Not enough queues for all TCs */
14411 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14412 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14413 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14414 					I40E_FLAG_DCB_ENABLED);
14415 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14416 		}
14417 
14418 		/* limit lan qps to the smaller of qps, cpus or msix */
14419 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14420 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14421 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14422 		pf->num_lan_qps = q_max;
14423 
14424 		queues_left -= pf->num_lan_qps;
14425 	}
14426 
14427 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14428 		if (queues_left > 1) {
14429 			queues_left -= 1; /* save 1 queue for FD */
14430 		} else {
14431 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14432 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14433 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14434 		}
14435 	}
14436 
14437 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14438 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14439 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14440 					(queues_left / pf->num_vf_qps));
14441 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14442 	}
14443 
14444 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14445 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14446 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14447 					  (queues_left / pf->num_vmdq_qps));
14448 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14449 	}
14450 
14451 	pf->queues_left = queues_left;
14452 	dev_dbg(&pf->pdev->dev,
14453 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14454 		pf->hw.func_caps.num_tx_qp,
14455 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14456 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14457 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14458 		queues_left);
14459 }
14460 
14461 /**
14462  * i40e_setup_pf_filter_control - Setup PF static filter control
14463  * @pf: PF to be setup
14464  *
14465  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14466  * settings. If PE/FCoE are enabled then it will also set the per PF
14467  * based filter sizes required for them. It also enables Flow director,
14468  * ethertype and macvlan type filter settings for the pf.
14469  *
14470  * Returns 0 on success, negative on failure
14471  **/
14472 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14473 {
14474 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14475 
14476 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14477 
14478 	/* Flow Director is enabled */
14479 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14480 		settings->enable_fdir = true;
14481 
14482 	/* Ethtype and MACVLAN filters enabled for PF */
14483 	settings->enable_ethtype = true;
14484 	settings->enable_macvlan = true;
14485 
14486 	if (i40e_set_filter_control(&pf->hw, settings))
14487 		return -ENOENT;
14488 
14489 	return 0;
14490 }
14491 
14492 #define INFO_STRING_LEN 255
14493 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14494 static void i40e_print_features(struct i40e_pf *pf)
14495 {
14496 	struct i40e_hw *hw = &pf->hw;
14497 	char *buf;
14498 	int i;
14499 
14500 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14501 	if (!buf)
14502 		return;
14503 
14504 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14505 #ifdef CONFIG_PCI_IOV
14506 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14507 #endif
14508 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14509 		      pf->hw.func_caps.num_vsis,
14510 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14511 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14512 		i += snprintf(&buf[i], REMAIN(i), " RSS");
14513 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14514 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
14515 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14516 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
14517 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
14518 	}
14519 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14520 		i += snprintf(&buf[i], REMAIN(i), " DCB");
14521 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
14522 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
14523 	if (pf->flags & I40E_FLAG_PTP)
14524 		i += snprintf(&buf[i], REMAIN(i), " PTP");
14525 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14526 		i += snprintf(&buf[i], REMAIN(i), " VEB");
14527 	else
14528 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
14529 
14530 	dev_info(&pf->pdev->dev, "%s\n", buf);
14531 	kfree(buf);
14532 	WARN_ON(i > INFO_STRING_LEN);
14533 }
14534 
14535 /**
14536  * i40e_get_platform_mac_addr - get platform-specific MAC address
14537  * @pdev: PCI device information struct
14538  * @pf: board private structure
14539  *
14540  * Look up the MAC address for the device. First we'll try
14541  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14542  * specific fallback. Otherwise, we'll default to the stored value in
14543  * firmware.
14544  **/
14545 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14546 {
14547 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14548 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14549 }
14550 
14551 /**
14552  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14553  * @fec_cfg: FEC option to set in flags
14554  * @flags: ptr to flags in which we set FEC option
14555  **/
14556 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14557 {
14558 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14559 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14560 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14561 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14562 		*flags |= I40E_FLAG_RS_FEC;
14563 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14564 	}
14565 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14566 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14567 		*flags |= I40E_FLAG_BASE_R_FEC;
14568 		*flags &= ~I40E_FLAG_RS_FEC;
14569 	}
14570 	if (fec_cfg == 0)
14571 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14572 }
14573 
14574 /**
14575  * i40e_check_recovery_mode - check if we are running transition firmware
14576  * @pf: board private structure
14577  *
14578  * Check registers indicating the firmware runs in recovery mode. Sets the
14579  * appropriate driver state.
14580  *
14581  * Returns true if the recovery mode was detected, false otherwise
14582  **/
14583 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14584 {
14585 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14586 
14587 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14588 		dev_notice(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14589 		dev_notice(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14590 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14591 
14592 		return true;
14593 	}
14594 	if (test_and_clear_bit(__I40E_RECOVERY_MODE, pf->state))
14595 		dev_info(&pf->pdev->dev, "Reinitializing in normal mode with full functionality.\n");
14596 
14597 	return false;
14598 }
14599 
14600 /**
14601  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14602  * @pf: board private structure
14603  * @hw: ptr to the hardware info
14604  *
14605  * This function does a minimal setup of all subsystems needed for running
14606  * recovery mode.
14607  *
14608  * Returns 0 on success, negative on failure
14609  **/
14610 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14611 {
14612 	struct i40e_vsi *vsi;
14613 	int err;
14614 	int v_idx;
14615 
14616 	pci_save_state(pf->pdev);
14617 
14618 	/* set up periodic task facility */
14619 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14620 	pf->service_timer_period = HZ;
14621 
14622 	INIT_WORK(&pf->service_task, i40e_service_task);
14623 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14624 
14625 	err = i40e_init_interrupt_scheme(pf);
14626 	if (err)
14627 		goto err_switch_setup;
14628 
14629 	/* The number of VSIs reported by the FW is the minimum guaranteed
14630 	 * to us; HW supports far more and we share the remaining pool with
14631 	 * the other PFs. We allocate space for more than the guarantee with
14632 	 * the understanding that we might not get them all later.
14633 	 */
14634 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14635 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14636 	else
14637 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14638 
14639 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14640 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14641 			  GFP_KERNEL);
14642 	if (!pf->vsi) {
14643 		err = -ENOMEM;
14644 		goto err_switch_setup;
14645 	}
14646 
14647 	/* We allocate one VSI which is needed as absolute minimum
14648 	 * in order to register the netdev
14649 	 */
14650 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14651 	if (v_idx < 0)
14652 		goto err_switch_setup;
14653 	pf->lan_vsi = v_idx;
14654 	vsi = pf->vsi[v_idx];
14655 	if (!vsi)
14656 		goto err_switch_setup;
14657 	vsi->alloc_queue_pairs = 1;
14658 	err = i40e_config_netdev(vsi);
14659 	if (err)
14660 		goto err_switch_setup;
14661 	err = register_netdev(vsi->netdev);
14662 	if (err)
14663 		goto err_switch_setup;
14664 	vsi->netdev_registered = true;
14665 	i40e_dbg_pf_init(pf);
14666 
14667 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14668 	if (err)
14669 		goto err_switch_setup;
14670 
14671 	/* tell the firmware that we're starting */
14672 	i40e_send_version(pf);
14673 
14674 	/* since everything's happy, start the service_task timer */
14675 	mod_timer(&pf->service_timer,
14676 		  round_jiffies(jiffies + pf->service_timer_period));
14677 
14678 	return 0;
14679 
14680 err_switch_setup:
14681 	i40e_reset_interrupt_capability(pf);
14682 	del_timer_sync(&pf->service_timer);
14683 	i40e_shutdown_adminq(hw);
14684 	iounmap(hw->hw_addr);
14685 	pci_disable_pcie_error_reporting(pf->pdev);
14686 	pci_release_mem_regions(pf->pdev);
14687 	pci_disable_device(pf->pdev);
14688 	kfree(pf);
14689 
14690 	return err;
14691 }
14692 
14693 /**
14694  * i40e_probe - Device initialization routine
14695  * @pdev: PCI device information struct
14696  * @ent: entry in i40e_pci_tbl
14697  *
14698  * i40e_probe initializes a PF identified by a pci_dev structure.
14699  * The OS initialization, configuring of the PF private structure,
14700  * and a hardware reset occur.
14701  *
14702  * Returns 0 on success, negative on failure
14703  **/
14704 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14705 {
14706 	struct i40e_aq_get_phy_abilities_resp abilities;
14707 	struct i40e_pf *pf;
14708 	struct i40e_hw *hw;
14709 	static u16 pfs_found;
14710 	u16 wol_nvm_bits;
14711 	u16 link_status;
14712 	int err;
14713 	u32 val;
14714 	u32 i;
14715 	u8 set_fc_aq_fail;
14716 
14717 	err = pci_enable_device_mem(pdev);
14718 	if (err)
14719 		return err;
14720 
14721 	/* set up for high or low dma */
14722 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14723 	if (err) {
14724 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14725 		if (err) {
14726 			dev_err(&pdev->dev,
14727 				"DMA configuration failed: 0x%x\n", err);
14728 			goto err_dma;
14729 		}
14730 	}
14731 
14732 	/* set up pci connections */
14733 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14734 	if (err) {
14735 		dev_info(&pdev->dev,
14736 			 "pci_request_selected_regions failed %d\n", err);
14737 		goto err_pci_reg;
14738 	}
14739 
14740 	pci_enable_pcie_error_reporting(pdev);
14741 	pci_set_master(pdev);
14742 
14743 	/* Now that we have a PCI connection, we need to do the
14744 	 * low level device setup.  This is primarily setting up
14745 	 * the Admin Queue structures and then querying for the
14746 	 * device's current profile information.
14747 	 */
14748 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14749 	if (!pf) {
14750 		err = -ENOMEM;
14751 		goto err_pf_alloc;
14752 	}
14753 	pf->next_vsi = 0;
14754 	pf->pdev = pdev;
14755 	set_bit(__I40E_DOWN, pf->state);
14756 
14757 	hw = &pf->hw;
14758 	hw->back = pf;
14759 
14760 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14761 				I40E_MAX_CSR_SPACE);
14762 	/* We believe that the highest register to read is
14763 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14764 	 * is not less than that before mapping to prevent a
14765 	 * kernel panic.
14766 	 */
14767 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14768 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14769 			pf->ioremap_len);
14770 		err = -ENOMEM;
14771 		goto err_ioremap;
14772 	}
14773 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14774 	if (!hw->hw_addr) {
14775 		err = -EIO;
14776 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14777 			 (unsigned int)pci_resource_start(pdev, 0),
14778 			 pf->ioremap_len, err);
14779 		goto err_ioremap;
14780 	}
14781 	hw->vendor_id = pdev->vendor;
14782 	hw->device_id = pdev->device;
14783 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14784 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14785 	hw->subsystem_device_id = pdev->subsystem_device;
14786 	hw->bus.device = PCI_SLOT(pdev->devfn);
14787 	hw->bus.func = PCI_FUNC(pdev->devfn);
14788 	hw->bus.bus_id = pdev->bus->number;
14789 	pf->instance = pfs_found;
14790 
14791 	/* Select something other than the 802.1ad ethertype for the
14792 	 * switch to use internally and drop on ingress.
14793 	 */
14794 	hw->switch_tag = 0xffff;
14795 	hw->first_tag = ETH_P_8021AD;
14796 	hw->second_tag = ETH_P_8021Q;
14797 
14798 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14799 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14800 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14801 
14802 	/* set up the locks for the AQ, do this only once in probe
14803 	 * and destroy them only once in remove
14804 	 */
14805 	mutex_init(&hw->aq.asq_mutex);
14806 	mutex_init(&hw->aq.arq_mutex);
14807 
14808 	pf->msg_enable = netif_msg_init(debug,
14809 					NETIF_MSG_DRV |
14810 					NETIF_MSG_PROBE |
14811 					NETIF_MSG_LINK);
14812 	if (debug < -1)
14813 		pf->hw.debug_mask = debug;
14814 
14815 	/* do a special CORER for clearing PXE mode once at init */
14816 	if (hw->revision_id == 0 &&
14817 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14818 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14819 		i40e_flush(hw);
14820 		msleep(200);
14821 		pf->corer_count++;
14822 
14823 		i40e_clear_pxe_mode(hw);
14824 	}
14825 
14826 	/* Reset here to make sure all is clean and to define PF 'n' */
14827 	i40e_clear_hw(hw);
14828 	if (!i40e_check_recovery_mode(pf)) {
14829 		err = i40e_pf_reset(hw);
14830 		if (err) {
14831 			dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
14832 			goto err_pf_reset;
14833 		}
14834 		pf->pfr_count++;
14835 	}
14836 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14837 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14838 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14839 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14840 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14841 
14842 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14843 		 "%s-%s:misc",
14844 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14845 
14846 	err = i40e_init_shared_code(hw);
14847 	if (err) {
14848 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14849 			 err);
14850 		goto err_pf_reset;
14851 	}
14852 
14853 	/* set up a default setting for link flow control */
14854 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14855 
14856 	err = i40e_init_adminq(hw);
14857 	if (err) {
14858 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14859 			dev_info(&pdev->dev,
14860 				 "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",
14861 				 hw->aq.api_maj_ver,
14862 				 hw->aq.api_min_ver,
14863 				 I40E_FW_API_VERSION_MAJOR,
14864 				 I40E_FW_MINOR_VERSION(hw));
14865 		else
14866 			dev_info(&pdev->dev,
14867 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14868 
14869 		goto err_pf_reset;
14870 	}
14871 	i40e_get_oem_version(hw);
14872 
14873 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14874 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14875 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14876 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14877 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
14878 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
14879 
14880 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14881 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14882 		dev_info(&pdev->dev,
14883 			 "The driver for the device detected a newer version of the NVM image v%u.%u than expected v%u.%u. Please install the most recent version of the network driver.\n",
14884 			 hw->aq.api_maj_ver,
14885 			 hw->aq.api_min_ver,
14886 			 I40E_FW_API_VERSION_MAJOR,
14887 			 I40E_FW_MINOR_VERSION(hw));
14888 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14889 		dev_info(&pdev->dev,
14890 			 "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",
14891 			 hw->aq.api_maj_ver,
14892 			 hw->aq.api_min_ver,
14893 			 I40E_FW_API_VERSION_MAJOR,
14894 			 I40E_FW_MINOR_VERSION(hw));
14895 
14896 	i40e_verify_eeprom(pf);
14897 
14898 	/* Rev 0 hardware was never productized */
14899 	if (hw->revision_id < 1)
14900 		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");
14901 
14902 	i40e_clear_pxe_mode(hw);
14903 
14904 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14905 	if (err)
14906 		goto err_adminq_setup;
14907 
14908 	err = i40e_sw_init(pf);
14909 	if (err) {
14910 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14911 		goto err_sw_init;
14912 	}
14913 
14914 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14915 		return i40e_init_recovery_mode(pf, hw);
14916 
14917 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14918 				hw->func_caps.num_rx_qp, 0, 0);
14919 	if (err) {
14920 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14921 		goto err_init_lan_hmc;
14922 	}
14923 
14924 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14925 	if (err) {
14926 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14927 		err = -ENOENT;
14928 		goto err_configure_lan_hmc;
14929 	}
14930 
14931 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14932 	 * Ignore error return codes because if it was already disabled via
14933 	 * hardware settings this will fail
14934 	 */
14935 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14936 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14937 		i40e_aq_stop_lldp(hw, true, false, NULL);
14938 	}
14939 
14940 	/* allow a platform config to override the HW addr */
14941 	i40e_get_platform_mac_addr(pdev, pf);
14942 
14943 	if (!is_valid_ether_addr(hw->mac.addr)) {
14944 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14945 		err = -EIO;
14946 		goto err_mac_addr;
14947 	}
14948 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14949 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14950 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14951 	if (is_valid_ether_addr(hw->mac.port_addr))
14952 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14953 
14954 	pci_set_drvdata(pdev, pf);
14955 	pci_save_state(pdev);
14956 
14957 	dev_info(&pdev->dev,
14958 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
14959 			"FW LLDP is disabled\n" :
14960 			"FW LLDP is enabled\n");
14961 
14962 	/* Enable FW to write default DCB config on link-up */
14963 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14964 
14965 #ifdef CONFIG_I40E_DCB
14966 	err = i40e_init_pf_dcb(pf);
14967 	if (err) {
14968 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14969 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14970 		/* Continue without DCB enabled */
14971 	}
14972 #endif /* CONFIG_I40E_DCB */
14973 
14974 	/* set up periodic task facility */
14975 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14976 	pf->service_timer_period = HZ;
14977 
14978 	INIT_WORK(&pf->service_task, i40e_service_task);
14979 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14980 
14981 	/* NVM bit on means WoL disabled for the port */
14982 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14983 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14984 		pf->wol_en = false;
14985 	else
14986 		pf->wol_en = true;
14987 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14988 
14989 	/* set up the main switch operations */
14990 	i40e_determine_queue_usage(pf);
14991 	err = i40e_init_interrupt_scheme(pf);
14992 	if (err)
14993 		goto err_switch_setup;
14994 
14995 	/* The number of VSIs reported by the FW is the minimum guaranteed
14996 	 * to us; HW supports far more and we share the remaining pool with
14997 	 * the other PFs. We allocate space for more than the guarantee with
14998 	 * the understanding that we might not get them all later.
14999 	 */
15000 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15001 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15002 	else
15003 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15004 
15005 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15006 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15007 			  GFP_KERNEL);
15008 	if (!pf->vsi) {
15009 		err = -ENOMEM;
15010 		goto err_switch_setup;
15011 	}
15012 
15013 #ifdef CONFIG_PCI_IOV
15014 	/* prep for VF support */
15015 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15016 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15017 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15018 		if (pci_num_vf(pdev))
15019 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15020 	}
15021 #endif
15022 	err = i40e_setup_pf_switch(pf, false);
15023 	if (err) {
15024 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15025 		goto err_vsis;
15026 	}
15027 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15028 
15029 	/* Make sure flow control is set according to current settings */
15030 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
15031 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
15032 		dev_dbg(&pf->pdev->dev,
15033 			"Set fc with err %s aq_err %s on get_phy_cap\n",
15034 			i40e_stat_str(hw, err),
15035 			i40e_aq_str(hw, hw->aq.asq_last_status));
15036 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
15037 		dev_dbg(&pf->pdev->dev,
15038 			"Set fc with err %s aq_err %s on set_phy_config\n",
15039 			i40e_stat_str(hw, err),
15040 			i40e_aq_str(hw, hw->aq.asq_last_status));
15041 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
15042 		dev_dbg(&pf->pdev->dev,
15043 			"Set fc with err %s aq_err %s on get_link_info\n",
15044 			i40e_stat_str(hw, err),
15045 			i40e_aq_str(hw, hw->aq.asq_last_status));
15046 
15047 	/* if FDIR VSI was set up, start it now */
15048 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15049 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15050 			i40e_vsi_open(pf->vsi[i]);
15051 			break;
15052 		}
15053 	}
15054 
15055 	/* The driver only wants link up/down and module qualification
15056 	 * reports from firmware.  Note the negative logic.
15057 	 */
15058 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15059 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15060 					 I40E_AQ_EVENT_MEDIA_NA |
15061 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15062 	if (err)
15063 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15064 			 i40e_stat_str(&pf->hw, err),
15065 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15066 
15067 	/* Reconfigure hardware for allowing smaller MSS in the case
15068 	 * of TSO, so that we avoid the MDD being fired and causing
15069 	 * a reset in the case of small MSS+TSO.
15070 	 */
15071 	val = rd32(hw, I40E_REG_MSS);
15072 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15073 		val &= ~I40E_REG_MSS_MIN_MASK;
15074 		val |= I40E_64BYTE_MSS;
15075 		wr32(hw, I40E_REG_MSS, val);
15076 	}
15077 
15078 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15079 		msleep(75);
15080 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15081 		if (err)
15082 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15083 				 i40e_stat_str(&pf->hw, err),
15084 				 i40e_aq_str(&pf->hw,
15085 					     pf->hw.aq.asq_last_status));
15086 	}
15087 	/* The main driver is (mostly) up and happy. We need to set this state
15088 	 * before setting up the misc vector or we get a race and the vector
15089 	 * ends up disabled forever.
15090 	 */
15091 	clear_bit(__I40E_DOWN, pf->state);
15092 
15093 	/* In case of MSIX we are going to setup the misc vector right here
15094 	 * to handle admin queue events etc. In case of legacy and MSI
15095 	 * the misc functionality and queue processing is combined in
15096 	 * the same vector and that gets setup at open.
15097 	 */
15098 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15099 		err = i40e_setup_misc_vector(pf);
15100 		if (err) {
15101 			dev_info(&pdev->dev,
15102 				 "setup of misc vector failed: %d\n", err);
15103 			goto err_vsis;
15104 		}
15105 	}
15106 
15107 #ifdef CONFIG_PCI_IOV
15108 	/* prep for VF support */
15109 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15110 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15111 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15112 		/* disable link interrupts for VFs */
15113 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15114 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15115 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15116 		i40e_flush(hw);
15117 
15118 		if (pci_num_vf(pdev)) {
15119 			dev_info(&pdev->dev,
15120 				 "Active VFs found, allocating resources.\n");
15121 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15122 			if (err)
15123 				dev_info(&pdev->dev,
15124 					 "Error %d allocating resources for existing VFs\n",
15125 					 err);
15126 		}
15127 	}
15128 #endif /* CONFIG_PCI_IOV */
15129 
15130 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15131 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15132 						      pf->num_iwarp_msix,
15133 						      I40E_IWARP_IRQ_PILE_ID);
15134 		if (pf->iwarp_base_vector < 0) {
15135 			dev_info(&pdev->dev,
15136 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15137 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15138 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15139 		}
15140 	}
15141 
15142 	i40e_dbg_pf_init(pf);
15143 
15144 	/* tell the firmware that we're starting */
15145 	i40e_send_version(pf);
15146 
15147 	/* since everything's happy, start the service_task timer */
15148 	mod_timer(&pf->service_timer,
15149 		  round_jiffies(jiffies + pf->service_timer_period));
15150 
15151 	/* add this PF to client device list and launch a client service task */
15152 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15153 		err = i40e_lan_add_device(pf);
15154 		if (err)
15155 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15156 				 err);
15157 	}
15158 
15159 #define PCI_SPEED_SIZE 8
15160 #define PCI_WIDTH_SIZE 8
15161 	/* Devices on the IOSF bus do not have this information
15162 	 * and will report PCI Gen 1 x 1 by default so don't bother
15163 	 * checking them.
15164 	 */
15165 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15166 		char speed[PCI_SPEED_SIZE] = "Unknown";
15167 		char width[PCI_WIDTH_SIZE] = "Unknown";
15168 
15169 		/* Get the negotiated link width and speed from PCI config
15170 		 * space
15171 		 */
15172 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15173 					  &link_status);
15174 
15175 		i40e_set_pci_config_data(hw, link_status);
15176 
15177 		switch (hw->bus.speed) {
15178 		case i40e_bus_speed_8000:
15179 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15180 		case i40e_bus_speed_5000:
15181 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15182 		case i40e_bus_speed_2500:
15183 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15184 		default:
15185 			break;
15186 		}
15187 		switch (hw->bus.width) {
15188 		case i40e_bus_width_pcie_x8:
15189 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15190 		case i40e_bus_width_pcie_x4:
15191 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15192 		case i40e_bus_width_pcie_x2:
15193 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15194 		case i40e_bus_width_pcie_x1:
15195 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15196 		default:
15197 			break;
15198 		}
15199 
15200 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15201 			 speed, width);
15202 
15203 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15204 		    hw->bus.speed < i40e_bus_speed_8000) {
15205 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15206 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15207 		}
15208 	}
15209 
15210 	/* get the requested speeds from the fw */
15211 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15212 	if (err)
15213 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15214 			i40e_stat_str(&pf->hw, err),
15215 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15216 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15217 
15218 	/* set the FEC config due to the board capabilities */
15219 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15220 
15221 	/* get the supported phy types from the fw */
15222 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15223 	if (err)
15224 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15225 			i40e_stat_str(&pf->hw, err),
15226 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15227 
15228 	/* Add a filter to drop all Flow control frames from any VSI from being
15229 	 * transmitted. By doing so we stop a malicious VF from sending out
15230 	 * PAUSE or PFC frames and potentially controlling traffic for other
15231 	 * PF/VF VSIs.
15232 	 * The FW can still send Flow control frames if enabled.
15233 	 */
15234 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15235 						       pf->main_vsi_seid);
15236 
15237 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15238 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15239 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15240 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15241 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15242 	/* print a string summarizing features */
15243 	i40e_print_features(pf);
15244 
15245 	return 0;
15246 
15247 	/* Unwind what we've done if something failed in the setup */
15248 err_vsis:
15249 	set_bit(__I40E_DOWN, pf->state);
15250 	i40e_clear_interrupt_scheme(pf);
15251 	kfree(pf->vsi);
15252 err_switch_setup:
15253 	i40e_reset_interrupt_capability(pf);
15254 	del_timer_sync(&pf->service_timer);
15255 err_mac_addr:
15256 err_configure_lan_hmc:
15257 	(void)i40e_shutdown_lan_hmc(hw);
15258 err_init_lan_hmc:
15259 	kfree(pf->qp_pile);
15260 err_sw_init:
15261 err_adminq_setup:
15262 err_pf_reset:
15263 	iounmap(hw->hw_addr);
15264 err_ioremap:
15265 	kfree(pf);
15266 err_pf_alloc:
15267 	pci_disable_pcie_error_reporting(pdev);
15268 	pci_release_mem_regions(pdev);
15269 err_pci_reg:
15270 err_dma:
15271 	pci_disable_device(pdev);
15272 	return err;
15273 }
15274 
15275 /**
15276  * i40e_remove - Device removal routine
15277  * @pdev: PCI device information struct
15278  *
15279  * i40e_remove is called by the PCI subsystem to alert the driver
15280  * that is should release a PCI device.  This could be caused by a
15281  * Hot-Plug event, or because the driver is going to be removed from
15282  * memory.
15283  **/
15284 static void i40e_remove(struct pci_dev *pdev)
15285 {
15286 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15287 	struct i40e_hw *hw = &pf->hw;
15288 	i40e_status ret_code;
15289 	int i;
15290 
15291 	i40e_dbg_pf_exit(pf);
15292 
15293 	i40e_ptp_stop(pf);
15294 
15295 	/* Disable RSS in hw */
15296 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15297 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15298 
15299 	/* no more scheduling of any task */
15300 	set_bit(__I40E_SUSPENDED, pf->state);
15301 	set_bit(__I40E_DOWN, pf->state);
15302 	if (pf->service_timer.function)
15303 		del_timer_sync(&pf->service_timer);
15304 	if (pf->service_task.func)
15305 		cancel_work_sync(&pf->service_task);
15306 
15307 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15308 		struct i40e_vsi *vsi = pf->vsi[0];
15309 
15310 		/* We know that we have allocated only one vsi for this PF,
15311 		 * it was just for registering netdevice, so the interface
15312 		 * could be visible in the 'ifconfig' output
15313 		 */
15314 		unregister_netdev(vsi->netdev);
15315 		free_netdev(vsi->netdev);
15316 
15317 		goto unmap;
15318 	}
15319 
15320 	/* Client close must be called explicitly here because the timer
15321 	 * has been stopped.
15322 	 */
15323 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15324 
15325 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15326 		i40e_free_vfs(pf);
15327 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15328 	}
15329 
15330 	i40e_fdir_teardown(pf);
15331 
15332 	/* If there is a switch structure or any orphans, remove them.
15333 	 * This will leave only the PF's VSI remaining.
15334 	 */
15335 	for (i = 0; i < I40E_MAX_VEB; i++) {
15336 		if (!pf->veb[i])
15337 			continue;
15338 
15339 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15340 		    pf->veb[i]->uplink_seid == 0)
15341 			i40e_switch_branch_release(pf->veb[i]);
15342 	}
15343 
15344 	/* Now we can shutdown the PF's VSI, just before we kill
15345 	 * adminq and hmc.
15346 	 */
15347 	if (pf->vsi[pf->lan_vsi])
15348 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15349 
15350 	i40e_cloud_filter_exit(pf);
15351 
15352 	/* remove attached clients */
15353 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15354 		ret_code = i40e_lan_del_device(pf);
15355 		if (ret_code)
15356 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15357 				 ret_code);
15358 	}
15359 
15360 	/* shutdown and destroy the HMC */
15361 	if (hw->hmc.hmc_obj) {
15362 		ret_code = i40e_shutdown_lan_hmc(hw);
15363 		if (ret_code)
15364 			dev_warn(&pdev->dev,
15365 				 "Failed to destroy the HMC resources: %d\n",
15366 				 ret_code);
15367 	}
15368 
15369 unmap:
15370 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15371 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15372 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15373 		free_irq(pf->pdev->irq, pf);
15374 
15375 	/* shutdown the adminq */
15376 	i40e_shutdown_adminq(hw);
15377 
15378 	/* destroy the locks only once, here */
15379 	mutex_destroy(&hw->aq.arq_mutex);
15380 	mutex_destroy(&hw->aq.asq_mutex);
15381 
15382 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15383 	rtnl_lock();
15384 	i40e_clear_interrupt_scheme(pf);
15385 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15386 		if (pf->vsi[i]) {
15387 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15388 				i40e_vsi_clear_rings(pf->vsi[i]);
15389 			i40e_vsi_clear(pf->vsi[i]);
15390 			pf->vsi[i] = NULL;
15391 		}
15392 	}
15393 	rtnl_unlock();
15394 
15395 	for (i = 0; i < I40E_MAX_VEB; i++) {
15396 		kfree(pf->veb[i]);
15397 		pf->veb[i] = NULL;
15398 	}
15399 
15400 	kfree(pf->qp_pile);
15401 	kfree(pf->vsi);
15402 
15403 	iounmap(hw->hw_addr);
15404 	kfree(pf);
15405 	pci_release_mem_regions(pdev);
15406 
15407 	pci_disable_pcie_error_reporting(pdev);
15408 	pci_disable_device(pdev);
15409 }
15410 
15411 /**
15412  * i40e_pci_error_detected - warning that something funky happened in PCI land
15413  * @pdev: PCI device information struct
15414  * @error: the type of PCI error
15415  *
15416  * Called to warn that something happened and the error handling steps
15417  * are in progress.  Allows the driver to quiesce things, be ready for
15418  * remediation.
15419  **/
15420 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15421 						enum pci_channel_state error)
15422 {
15423 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15424 
15425 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15426 
15427 	if (!pf) {
15428 		dev_info(&pdev->dev,
15429 			 "Cannot recover - error happened during device probe\n");
15430 		return PCI_ERS_RESULT_DISCONNECT;
15431 	}
15432 
15433 	/* shutdown all operations */
15434 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15435 		i40e_prep_for_reset(pf, false);
15436 
15437 	/* Request a slot reset */
15438 	return PCI_ERS_RESULT_NEED_RESET;
15439 }
15440 
15441 /**
15442  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15443  * @pdev: PCI device information struct
15444  *
15445  * Called to find if the driver can work with the device now that
15446  * the pci slot has been reset.  If a basic connection seems good
15447  * (registers are readable and have sane content) then return a
15448  * happy little PCI_ERS_RESULT_xxx.
15449  **/
15450 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15451 {
15452 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15453 	pci_ers_result_t result;
15454 	u32 reg;
15455 
15456 	dev_dbg(&pdev->dev, "%s\n", __func__);
15457 	if (pci_enable_device_mem(pdev)) {
15458 		dev_info(&pdev->dev,
15459 			 "Cannot re-enable PCI device after reset.\n");
15460 		result = PCI_ERS_RESULT_DISCONNECT;
15461 	} else {
15462 		pci_set_master(pdev);
15463 		pci_restore_state(pdev);
15464 		pci_save_state(pdev);
15465 		pci_wake_from_d3(pdev, false);
15466 
15467 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15468 		if (reg == 0)
15469 			result = PCI_ERS_RESULT_RECOVERED;
15470 		else
15471 			result = PCI_ERS_RESULT_DISCONNECT;
15472 	}
15473 
15474 	return result;
15475 }
15476 
15477 /**
15478  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15479  * @pdev: PCI device information struct
15480  */
15481 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15482 {
15483 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15484 
15485 	i40e_prep_for_reset(pf, false);
15486 }
15487 
15488 /**
15489  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15490  * @pdev: PCI device information struct
15491  */
15492 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15493 {
15494 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15495 
15496 	i40e_reset_and_rebuild(pf, false, false);
15497 }
15498 
15499 /**
15500  * i40e_pci_error_resume - restart operations after PCI error recovery
15501  * @pdev: PCI device information struct
15502  *
15503  * Called to allow the driver to bring things back up after PCI error
15504  * and/or reset recovery has finished.
15505  **/
15506 static void i40e_pci_error_resume(struct pci_dev *pdev)
15507 {
15508 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15509 
15510 	dev_dbg(&pdev->dev, "%s\n", __func__);
15511 	if (test_bit(__I40E_SUSPENDED, pf->state))
15512 		return;
15513 
15514 	i40e_handle_reset_warning(pf, false);
15515 }
15516 
15517 /**
15518  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15519  * using the mac_address_write admin q function
15520  * @pf: pointer to i40e_pf struct
15521  **/
15522 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15523 {
15524 	struct i40e_hw *hw = &pf->hw;
15525 	i40e_status ret;
15526 	u8 mac_addr[6];
15527 	u16 flags = 0;
15528 
15529 	/* Get current MAC address in case it's an LAA */
15530 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15531 		ether_addr_copy(mac_addr,
15532 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15533 	} else {
15534 		dev_err(&pf->pdev->dev,
15535 			"Failed to retrieve MAC address; using default\n");
15536 		ether_addr_copy(mac_addr, hw->mac.addr);
15537 	}
15538 
15539 	/* The FW expects the mac address write cmd to first be called with
15540 	 * one of these flags before calling it again with the multicast
15541 	 * enable flags.
15542 	 */
15543 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15544 
15545 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15546 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15547 
15548 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15549 	if (ret) {
15550 		dev_err(&pf->pdev->dev,
15551 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15552 		return;
15553 	}
15554 
15555 	flags = I40E_AQC_MC_MAG_EN
15556 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15557 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15558 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15559 	if (ret)
15560 		dev_err(&pf->pdev->dev,
15561 			"Failed to enable Multicast Magic Packet wake up\n");
15562 }
15563 
15564 /**
15565  * i40e_shutdown - PCI callback for shutting down
15566  * @pdev: PCI device information struct
15567  **/
15568 static void i40e_shutdown(struct pci_dev *pdev)
15569 {
15570 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15571 	struct i40e_hw *hw = &pf->hw;
15572 
15573 	set_bit(__I40E_SUSPENDED, pf->state);
15574 	set_bit(__I40E_DOWN, pf->state);
15575 
15576 	del_timer_sync(&pf->service_timer);
15577 	cancel_work_sync(&pf->service_task);
15578 	i40e_cloud_filter_exit(pf);
15579 	i40e_fdir_teardown(pf);
15580 
15581 	/* Client close must be called explicitly here because the timer
15582 	 * has been stopped.
15583 	 */
15584 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15585 
15586 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15587 		i40e_enable_mc_magic_wake(pf);
15588 
15589 	i40e_prep_for_reset(pf, false);
15590 
15591 	wr32(hw, I40E_PFPM_APM,
15592 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15593 	wr32(hw, I40E_PFPM_WUFC,
15594 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15595 
15596 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15597 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15598 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15599 		free_irq(pf->pdev->irq, pf);
15600 
15601 	/* Since we're going to destroy queues during the
15602 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15603 	 * whole section
15604 	 */
15605 	rtnl_lock();
15606 	i40e_clear_interrupt_scheme(pf);
15607 	rtnl_unlock();
15608 
15609 	if (system_state == SYSTEM_POWER_OFF) {
15610 		pci_wake_from_d3(pdev, pf->wol_en);
15611 		pci_set_power_state(pdev, PCI_D3hot);
15612 	}
15613 }
15614 
15615 /**
15616  * i40e_suspend - PM callback for moving to D3
15617  * @dev: generic device information structure
15618  **/
15619 static int __maybe_unused i40e_suspend(struct device *dev)
15620 {
15621 	struct pci_dev *pdev = to_pci_dev(dev);
15622 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15623 	struct i40e_hw *hw = &pf->hw;
15624 
15625 	/* If we're already suspended, then there is nothing to do */
15626 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15627 		return 0;
15628 
15629 	set_bit(__I40E_DOWN, pf->state);
15630 
15631 	/* Ensure service task will not be running */
15632 	del_timer_sync(&pf->service_timer);
15633 	cancel_work_sync(&pf->service_task);
15634 
15635 	/* Client close must be called explicitly here because the timer
15636 	 * has been stopped.
15637 	 */
15638 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15639 
15640 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15641 		i40e_enable_mc_magic_wake(pf);
15642 
15643 	/* Since we're going to destroy queues during the
15644 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15645 	 * whole section
15646 	 */
15647 	rtnl_lock();
15648 
15649 	i40e_prep_for_reset(pf, true);
15650 
15651 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15652 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15653 
15654 	/* Clear the interrupt scheme and release our IRQs so that the system
15655 	 * can safely hibernate even when there are a large number of CPUs.
15656 	 * Otherwise hibernation might fail when mapping all the vectors back
15657 	 * to CPU0.
15658 	 */
15659 	i40e_clear_interrupt_scheme(pf);
15660 
15661 	rtnl_unlock();
15662 
15663 	return 0;
15664 }
15665 
15666 /**
15667  * i40e_resume - PM callback for waking up from D3
15668  * @dev: generic device information structure
15669  **/
15670 static int __maybe_unused i40e_resume(struct device *dev)
15671 {
15672 	struct pci_dev *pdev = to_pci_dev(dev);
15673 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15674 	int err;
15675 
15676 	/* If we're not suspended, then there is nothing to do */
15677 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15678 		return 0;
15679 
15680 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15681 	 * since we're going to be restoring queues
15682 	 */
15683 	rtnl_lock();
15684 
15685 	/* We cleared the interrupt scheme when we suspended, so we need to
15686 	 * restore it now to resume device functionality.
15687 	 */
15688 	err = i40e_restore_interrupt_scheme(pf);
15689 	if (err) {
15690 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
15691 			err);
15692 	}
15693 
15694 	clear_bit(__I40E_DOWN, pf->state);
15695 	i40e_reset_and_rebuild(pf, false, true);
15696 
15697 	rtnl_unlock();
15698 
15699 	/* Clear suspended state last after everything is recovered */
15700 	clear_bit(__I40E_SUSPENDED, pf->state);
15701 
15702 	/* Restart the service task */
15703 	mod_timer(&pf->service_timer,
15704 		  round_jiffies(jiffies + pf->service_timer_period));
15705 
15706 	return 0;
15707 }
15708 
15709 static const struct pci_error_handlers i40e_err_handler = {
15710 	.error_detected = i40e_pci_error_detected,
15711 	.slot_reset = i40e_pci_error_slot_reset,
15712 	.reset_prepare = i40e_pci_error_reset_prepare,
15713 	.reset_done = i40e_pci_error_reset_done,
15714 	.resume = i40e_pci_error_resume,
15715 };
15716 
15717 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15718 
15719 static struct pci_driver i40e_driver = {
15720 	.name     = i40e_driver_name,
15721 	.id_table = i40e_pci_tbl,
15722 	.probe    = i40e_probe,
15723 	.remove   = i40e_remove,
15724 	.driver   = {
15725 		.pm = &i40e_pm_ops,
15726 	},
15727 	.shutdown = i40e_shutdown,
15728 	.err_handler = &i40e_err_handler,
15729 	.sriov_configure = i40e_pci_sriov_configure,
15730 };
15731 
15732 /**
15733  * i40e_init_module - Driver registration routine
15734  *
15735  * i40e_init_module is the first routine called when the driver is
15736  * loaded. All it does is register with the PCI subsystem.
15737  **/
15738 static int __init i40e_init_module(void)
15739 {
15740 	pr_info("%s: %s - version %s\n", i40e_driver_name,
15741 		i40e_driver_string, i40e_driver_version_str);
15742 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15743 
15744 	/* There is no need to throttle the number of active tasks because
15745 	 * each device limits its own task using a state bit for scheduling
15746 	 * the service task, and the device tasks do not interfere with each
15747 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15748 	 * since we need to be able to guarantee forward progress even under
15749 	 * memory pressure.
15750 	 */
15751 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15752 	if (!i40e_wq) {
15753 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15754 		return -ENOMEM;
15755 	}
15756 
15757 	i40e_dbg_init();
15758 	return pci_register_driver(&i40e_driver);
15759 }
15760 module_init(i40e_init_module);
15761 
15762 /**
15763  * i40e_exit_module - Driver exit cleanup routine
15764  *
15765  * i40e_exit_module is called just before the driver is removed
15766  * from memory.
15767  **/
15768 static void __exit i40e_exit_module(void)
15769 {
15770 	pci_unregister_driver(&i40e_driver);
15771 	destroy_workqueue(i40e_wq);
15772 	i40e_dbg_exit();
15773 }
15774 module_exit(i40e_exit_module);
15775