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(struct net_device *netdev, enum tc_setup_type type,
8181 			   void *type_data)
8182 {
8183 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8184 
8185 	switch (type) {
8186 	case TC_SETUP_QDISC_MQPRIO:
8187 		return i40e_setup_tc(netdev, type_data);
8188 	case TC_SETUP_BLOCK:
8189 		return flow_block_cb_setup_simple(type_data, NULL,
8190 						  i40e_setup_tc_block_cb,
8191 						  np, np, true);
8192 	default:
8193 		return -EOPNOTSUPP;
8194 	}
8195 }
8196 
8197 /**
8198  * i40e_open - Called when a network interface is made active
8199  * @netdev: network interface device structure
8200  *
8201  * The open entry point is called when a network interface is made
8202  * active by the system (IFF_UP).  At this point all resources needed
8203  * for transmit and receive operations are allocated, the interrupt
8204  * handler is registered with the OS, the netdev watchdog subtask is
8205  * enabled, and the stack is notified that the interface is ready.
8206  *
8207  * Returns 0 on success, negative value on failure
8208  **/
8209 int i40e_open(struct net_device *netdev)
8210 {
8211 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8212 	struct i40e_vsi *vsi = np->vsi;
8213 	struct i40e_pf *pf = vsi->back;
8214 	int err;
8215 
8216 	/* disallow open during test or if eeprom is broken */
8217 	if (test_bit(__I40E_TESTING, pf->state) ||
8218 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8219 		return -EBUSY;
8220 
8221 	netif_carrier_off(netdev);
8222 
8223 	if (i40e_force_link_state(pf, true))
8224 		return -EAGAIN;
8225 
8226 	err = i40e_vsi_open(vsi);
8227 	if (err)
8228 		return err;
8229 
8230 	/* configure global TSO hardware offload settings */
8231 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8232 						       TCP_FLAG_FIN) >> 16);
8233 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8234 						       TCP_FLAG_FIN |
8235 						       TCP_FLAG_CWR) >> 16);
8236 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8237 
8238 	udp_tunnel_get_rx_info(netdev);
8239 
8240 	return 0;
8241 }
8242 
8243 /**
8244  * i40e_vsi_open -
8245  * @vsi: the VSI to open
8246  *
8247  * Finish initialization of the VSI.
8248  *
8249  * Returns 0 on success, negative value on failure
8250  *
8251  * Note: expects to be called while under rtnl_lock()
8252  **/
8253 int i40e_vsi_open(struct i40e_vsi *vsi)
8254 {
8255 	struct i40e_pf *pf = vsi->back;
8256 	char int_name[I40E_INT_NAME_STR_LEN];
8257 	int err;
8258 
8259 	/* allocate descriptors */
8260 	err = i40e_vsi_setup_tx_resources(vsi);
8261 	if (err)
8262 		goto err_setup_tx;
8263 	err = i40e_vsi_setup_rx_resources(vsi);
8264 	if (err)
8265 		goto err_setup_rx;
8266 
8267 	err = i40e_vsi_configure(vsi);
8268 	if (err)
8269 		goto err_setup_rx;
8270 
8271 	if (vsi->netdev) {
8272 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8273 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8274 		err = i40e_vsi_request_irq(vsi, int_name);
8275 		if (err)
8276 			goto err_setup_rx;
8277 
8278 		/* Notify the stack of the actual queue counts. */
8279 		err = netif_set_real_num_tx_queues(vsi->netdev,
8280 						   vsi->num_queue_pairs);
8281 		if (err)
8282 			goto err_set_queues;
8283 
8284 		err = netif_set_real_num_rx_queues(vsi->netdev,
8285 						   vsi->num_queue_pairs);
8286 		if (err)
8287 			goto err_set_queues;
8288 
8289 	} else if (vsi->type == I40E_VSI_FDIR) {
8290 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8291 			 dev_driver_string(&pf->pdev->dev),
8292 			 dev_name(&pf->pdev->dev));
8293 		err = i40e_vsi_request_irq(vsi, int_name);
8294 
8295 	} else {
8296 		err = -EINVAL;
8297 		goto err_setup_rx;
8298 	}
8299 
8300 	err = i40e_up_complete(vsi);
8301 	if (err)
8302 		goto err_up_complete;
8303 
8304 	return 0;
8305 
8306 err_up_complete:
8307 	i40e_down(vsi);
8308 err_set_queues:
8309 	i40e_vsi_free_irq(vsi);
8310 err_setup_rx:
8311 	i40e_vsi_free_rx_resources(vsi);
8312 err_setup_tx:
8313 	i40e_vsi_free_tx_resources(vsi);
8314 	if (vsi == pf->vsi[pf->lan_vsi])
8315 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8316 
8317 	return err;
8318 }
8319 
8320 /**
8321  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8322  * @pf: Pointer to PF
8323  *
8324  * This function destroys the hlist where all the Flow Director
8325  * filters were saved.
8326  **/
8327 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8328 {
8329 	struct i40e_fdir_filter *filter;
8330 	struct i40e_flex_pit *pit_entry, *tmp;
8331 	struct hlist_node *node2;
8332 
8333 	hlist_for_each_entry_safe(filter, node2,
8334 				  &pf->fdir_filter_list, fdir_node) {
8335 		hlist_del(&filter->fdir_node);
8336 		kfree(filter);
8337 	}
8338 
8339 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8340 		list_del(&pit_entry->list);
8341 		kfree(pit_entry);
8342 	}
8343 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8344 
8345 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8346 		list_del(&pit_entry->list);
8347 		kfree(pit_entry);
8348 	}
8349 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8350 
8351 	pf->fdir_pf_active_filters = 0;
8352 	pf->fd_tcp4_filter_cnt = 0;
8353 	pf->fd_udp4_filter_cnt = 0;
8354 	pf->fd_sctp4_filter_cnt = 0;
8355 	pf->fd_ip4_filter_cnt = 0;
8356 
8357 	/* Reprogram the default input set for TCP/IPv4 */
8358 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8359 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8360 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8361 
8362 	/* Reprogram the default input set for UDP/IPv4 */
8363 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8364 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8365 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8366 
8367 	/* Reprogram the default input set for SCTP/IPv4 */
8368 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8369 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8370 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8371 
8372 	/* Reprogram the default input set for Other/IPv4 */
8373 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8374 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8375 
8376 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8377 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8378 }
8379 
8380 /**
8381  * i40e_cloud_filter_exit - Cleans up the cloud filters
8382  * @pf: Pointer to PF
8383  *
8384  * This function destroys the hlist where all the cloud filters
8385  * were saved.
8386  **/
8387 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8388 {
8389 	struct i40e_cloud_filter *cfilter;
8390 	struct hlist_node *node;
8391 
8392 	hlist_for_each_entry_safe(cfilter, node,
8393 				  &pf->cloud_filter_list, cloud_node) {
8394 		hlist_del(&cfilter->cloud_node);
8395 		kfree(cfilter);
8396 	}
8397 	pf->num_cloud_filters = 0;
8398 
8399 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8400 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8401 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8402 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8403 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8404 	}
8405 }
8406 
8407 /**
8408  * i40e_close - Disables a network interface
8409  * @netdev: network interface device structure
8410  *
8411  * The close entry point is called when an interface is de-activated
8412  * by the OS.  The hardware is still under the driver's control, but
8413  * this netdev interface is disabled.
8414  *
8415  * Returns 0, this is not allowed to fail
8416  **/
8417 int i40e_close(struct net_device *netdev)
8418 {
8419 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8420 	struct i40e_vsi *vsi = np->vsi;
8421 
8422 	i40e_vsi_close(vsi);
8423 
8424 	return 0;
8425 }
8426 
8427 /**
8428  * i40e_do_reset - Start a PF or Core Reset sequence
8429  * @pf: board private structure
8430  * @reset_flags: which reset is requested
8431  * @lock_acquired: indicates whether or not the lock has been acquired
8432  * before this function was called.
8433  *
8434  * The essential difference in resets is that the PF Reset
8435  * doesn't clear the packet buffers, doesn't reset the PE
8436  * firmware, and doesn't bother the other PFs on the chip.
8437  **/
8438 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8439 {
8440 	u32 val;
8441 
8442 	WARN_ON(in_interrupt());
8443 
8444 
8445 	/* do the biggest reset indicated */
8446 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8447 
8448 		/* Request a Global Reset
8449 		 *
8450 		 * This will start the chip's countdown to the actual full
8451 		 * chip reset event, and a warning interrupt to be sent
8452 		 * to all PFs, including the requestor.  Our handler
8453 		 * for the warning interrupt will deal with the shutdown
8454 		 * and recovery of the switch setup.
8455 		 */
8456 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8457 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8458 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8459 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8460 
8461 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8462 
8463 		/* Request a Core Reset
8464 		 *
8465 		 * Same as Global Reset, except does *not* include the MAC/PHY
8466 		 */
8467 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8468 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8469 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8470 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8471 		i40e_flush(&pf->hw);
8472 
8473 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8474 
8475 		/* Request a PF Reset
8476 		 *
8477 		 * Resets only the PF-specific registers
8478 		 *
8479 		 * This goes directly to the tear-down and rebuild of
8480 		 * the switch, since we need to do all the recovery as
8481 		 * for the Core Reset.
8482 		 */
8483 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8484 		i40e_handle_reset_warning(pf, lock_acquired);
8485 
8486 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8487 		int v;
8488 
8489 		/* Find the VSI(s) that requested a re-init */
8490 		dev_info(&pf->pdev->dev,
8491 			 "VSI reinit requested\n");
8492 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8493 			struct i40e_vsi *vsi = pf->vsi[v];
8494 
8495 			if (vsi != NULL &&
8496 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8497 					       vsi->state))
8498 				i40e_vsi_reinit_locked(pf->vsi[v]);
8499 		}
8500 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8501 		int v;
8502 
8503 		/* Find the VSI(s) that needs to be brought down */
8504 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8505 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8506 			struct i40e_vsi *vsi = pf->vsi[v];
8507 
8508 			if (vsi != NULL &&
8509 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8510 					       vsi->state)) {
8511 				set_bit(__I40E_VSI_DOWN, vsi->state);
8512 				i40e_down(vsi);
8513 			}
8514 		}
8515 	} else {
8516 		dev_info(&pf->pdev->dev,
8517 			 "bad reset request 0x%08x\n", reset_flags);
8518 	}
8519 }
8520 
8521 #ifdef CONFIG_I40E_DCB
8522 /**
8523  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8524  * @pf: board private structure
8525  * @old_cfg: current DCB config
8526  * @new_cfg: new DCB config
8527  **/
8528 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8529 			    struct i40e_dcbx_config *old_cfg,
8530 			    struct i40e_dcbx_config *new_cfg)
8531 {
8532 	bool need_reconfig = false;
8533 
8534 	/* Check if ETS configuration has changed */
8535 	if (memcmp(&new_cfg->etscfg,
8536 		   &old_cfg->etscfg,
8537 		   sizeof(new_cfg->etscfg))) {
8538 		/* If Priority Table has changed reconfig is needed */
8539 		if (memcmp(&new_cfg->etscfg.prioritytable,
8540 			   &old_cfg->etscfg.prioritytable,
8541 			   sizeof(new_cfg->etscfg.prioritytable))) {
8542 			need_reconfig = true;
8543 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8544 		}
8545 
8546 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8547 			   &old_cfg->etscfg.tcbwtable,
8548 			   sizeof(new_cfg->etscfg.tcbwtable)))
8549 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8550 
8551 		if (memcmp(&new_cfg->etscfg.tsatable,
8552 			   &old_cfg->etscfg.tsatable,
8553 			   sizeof(new_cfg->etscfg.tsatable)))
8554 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8555 	}
8556 
8557 	/* Check if PFC configuration has changed */
8558 	if (memcmp(&new_cfg->pfc,
8559 		   &old_cfg->pfc,
8560 		   sizeof(new_cfg->pfc))) {
8561 		need_reconfig = true;
8562 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8563 	}
8564 
8565 	/* Check if APP Table has changed */
8566 	if (memcmp(&new_cfg->app,
8567 		   &old_cfg->app,
8568 		   sizeof(new_cfg->app))) {
8569 		need_reconfig = true;
8570 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8571 	}
8572 
8573 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8574 	return need_reconfig;
8575 }
8576 
8577 /**
8578  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8579  * @pf: board private structure
8580  * @e: event info posted on ARQ
8581  **/
8582 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8583 				  struct i40e_arq_event_info *e)
8584 {
8585 	struct i40e_aqc_lldp_get_mib *mib =
8586 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8587 	struct i40e_hw *hw = &pf->hw;
8588 	struct i40e_dcbx_config tmp_dcbx_cfg;
8589 	bool need_reconfig = false;
8590 	int ret = 0;
8591 	u8 type;
8592 
8593 	/* Not DCB capable or capability disabled */
8594 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8595 		return ret;
8596 
8597 	/* Ignore if event is not for Nearest Bridge */
8598 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8599 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8600 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8601 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8602 		return ret;
8603 
8604 	/* Check MIB Type and return if event for Remote MIB update */
8605 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8606 	dev_dbg(&pf->pdev->dev,
8607 		"LLDP event mib type %s\n", type ? "remote" : "local");
8608 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8609 		/* Update the remote cached instance and return */
8610 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8611 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8612 				&hw->remote_dcbx_config);
8613 		goto exit;
8614 	}
8615 
8616 	/* Store the old configuration */
8617 	tmp_dcbx_cfg = hw->local_dcbx_config;
8618 
8619 	/* Reset the old DCBx configuration data */
8620 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8621 	/* Get updated DCBX data from firmware */
8622 	ret = i40e_get_dcb_config(&pf->hw);
8623 	if (ret) {
8624 		dev_info(&pf->pdev->dev,
8625 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8626 			 i40e_stat_str(&pf->hw, ret),
8627 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8628 		goto exit;
8629 	}
8630 
8631 	/* No change detected in DCBX configs */
8632 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8633 		    sizeof(tmp_dcbx_cfg))) {
8634 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8635 		goto exit;
8636 	}
8637 
8638 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8639 					       &hw->local_dcbx_config);
8640 
8641 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8642 
8643 	if (!need_reconfig)
8644 		goto exit;
8645 
8646 	/* Enable DCB tagging only when more than one TC */
8647 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8648 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8649 	else
8650 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8651 
8652 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8653 	/* Reconfiguration needed quiesce all VSIs */
8654 	i40e_pf_quiesce_all_vsi(pf);
8655 
8656 	/* Changes in configuration update VEB/VSI */
8657 	i40e_dcb_reconfigure(pf);
8658 
8659 	ret = i40e_resume_port_tx(pf);
8660 
8661 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8662 	/* In case of error no point in resuming VSIs */
8663 	if (ret)
8664 		goto exit;
8665 
8666 	/* Wait for the PF's queues to be disabled */
8667 	ret = i40e_pf_wait_queues_disabled(pf);
8668 	if (ret) {
8669 		/* Schedule PF reset to recover */
8670 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8671 		i40e_service_event_schedule(pf);
8672 	} else {
8673 		i40e_pf_unquiesce_all_vsi(pf);
8674 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8675 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8676 	}
8677 
8678 exit:
8679 	return ret;
8680 }
8681 #endif /* CONFIG_I40E_DCB */
8682 
8683 /**
8684  * i40e_do_reset_safe - Protected reset path for userland calls.
8685  * @pf: board private structure
8686  * @reset_flags: which reset is requested
8687  *
8688  **/
8689 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8690 {
8691 	rtnl_lock();
8692 	i40e_do_reset(pf, reset_flags, true);
8693 	rtnl_unlock();
8694 }
8695 
8696 /**
8697  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8698  * @pf: board private structure
8699  * @e: event info posted on ARQ
8700  *
8701  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8702  * and VF queues
8703  **/
8704 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8705 					   struct i40e_arq_event_info *e)
8706 {
8707 	struct i40e_aqc_lan_overflow *data =
8708 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8709 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8710 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8711 	struct i40e_hw *hw = &pf->hw;
8712 	struct i40e_vf *vf;
8713 	u16 vf_id;
8714 
8715 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8716 		queue, qtx_ctl);
8717 
8718 	/* Queue belongs to VF, find the VF and issue VF reset */
8719 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8720 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8721 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8722 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8723 		vf_id -= hw->func_caps.vf_base_id;
8724 		vf = &pf->vf[vf_id];
8725 		i40e_vc_notify_vf_reset(vf);
8726 		/* Allow VF to process pending reset notification */
8727 		msleep(20);
8728 		i40e_reset_vf(vf, false);
8729 	}
8730 }
8731 
8732 /**
8733  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8734  * @pf: board private structure
8735  **/
8736 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8737 {
8738 	u32 val, fcnt_prog;
8739 
8740 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8741 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8742 	return fcnt_prog;
8743 }
8744 
8745 /**
8746  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8747  * @pf: board private structure
8748  **/
8749 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8750 {
8751 	u32 val, fcnt_prog;
8752 
8753 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8754 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8755 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8756 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8757 	return fcnt_prog;
8758 }
8759 
8760 /**
8761  * i40e_get_global_fd_count - Get total FD filters programmed on device
8762  * @pf: board private structure
8763  **/
8764 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8765 {
8766 	u32 val, fcnt_prog;
8767 
8768 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8769 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8770 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8771 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8772 	return fcnt_prog;
8773 }
8774 
8775 /**
8776  * i40e_reenable_fdir_sb - Restore FDir SB capability
8777  * @pf: board private structure
8778  **/
8779 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8780 {
8781 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8782 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8783 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8784 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8785 }
8786 
8787 /**
8788  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8789  * @pf: board private structure
8790  **/
8791 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8792 {
8793 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8794 		/* ATR uses the same filtering logic as SB rules. It only
8795 		 * functions properly if the input set mask is at the default
8796 		 * settings. It is safe to restore the default input set
8797 		 * because there are no active TCPv4 filter rules.
8798 		 */
8799 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8800 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8801 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8802 
8803 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8804 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8805 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8806 	}
8807 }
8808 
8809 /**
8810  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8811  * @pf: board private structure
8812  * @filter: FDir filter to remove
8813  */
8814 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8815 				       struct i40e_fdir_filter *filter)
8816 {
8817 	/* Update counters */
8818 	pf->fdir_pf_active_filters--;
8819 	pf->fd_inv = 0;
8820 
8821 	switch (filter->flow_type) {
8822 	case TCP_V4_FLOW:
8823 		pf->fd_tcp4_filter_cnt--;
8824 		break;
8825 	case UDP_V4_FLOW:
8826 		pf->fd_udp4_filter_cnt--;
8827 		break;
8828 	case SCTP_V4_FLOW:
8829 		pf->fd_sctp4_filter_cnt--;
8830 		break;
8831 	case IP_USER_FLOW:
8832 		switch (filter->ip4_proto) {
8833 		case IPPROTO_TCP:
8834 			pf->fd_tcp4_filter_cnt--;
8835 			break;
8836 		case IPPROTO_UDP:
8837 			pf->fd_udp4_filter_cnt--;
8838 			break;
8839 		case IPPROTO_SCTP:
8840 			pf->fd_sctp4_filter_cnt--;
8841 			break;
8842 		case IPPROTO_IP:
8843 			pf->fd_ip4_filter_cnt--;
8844 			break;
8845 		}
8846 		break;
8847 	}
8848 
8849 	/* Remove the filter from the list and free memory */
8850 	hlist_del(&filter->fdir_node);
8851 	kfree(filter);
8852 }
8853 
8854 /**
8855  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8856  * @pf: board private structure
8857  **/
8858 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8859 {
8860 	struct i40e_fdir_filter *filter;
8861 	u32 fcnt_prog, fcnt_avail;
8862 	struct hlist_node *node;
8863 
8864 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8865 		return;
8866 
8867 	/* Check if we have enough room to re-enable FDir SB capability. */
8868 	fcnt_prog = i40e_get_global_fd_count(pf);
8869 	fcnt_avail = pf->fdir_pf_filter_count;
8870 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8871 	    (pf->fd_add_err == 0) ||
8872 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8873 		i40e_reenable_fdir_sb(pf);
8874 
8875 	/* We should wait for even more space before re-enabling ATR.
8876 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8877 	 * rules active.
8878 	 */
8879 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8880 	    (pf->fd_tcp4_filter_cnt == 0))
8881 		i40e_reenable_fdir_atr(pf);
8882 
8883 	/* if hw had a problem adding a filter, delete it */
8884 	if (pf->fd_inv > 0) {
8885 		hlist_for_each_entry_safe(filter, node,
8886 					  &pf->fdir_filter_list, fdir_node)
8887 			if (filter->fd_id == pf->fd_inv)
8888 				i40e_delete_invalid_filter(pf, filter);
8889 	}
8890 }
8891 
8892 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8893 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8894 /**
8895  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8896  * @pf: board private structure
8897  **/
8898 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8899 {
8900 	unsigned long min_flush_time;
8901 	int flush_wait_retry = 50;
8902 	bool disable_atr = false;
8903 	int fd_room;
8904 	int reg;
8905 
8906 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8907 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8908 		return;
8909 
8910 	/* If the flush is happening too quick and we have mostly SB rules we
8911 	 * should not re-enable ATR for some time.
8912 	 */
8913 	min_flush_time = pf->fd_flush_timestamp +
8914 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8915 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8916 
8917 	if (!(time_after(jiffies, min_flush_time)) &&
8918 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8919 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8920 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8921 		disable_atr = true;
8922 	}
8923 
8924 	pf->fd_flush_timestamp = jiffies;
8925 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8926 	/* flush all filters */
8927 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8928 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8929 	i40e_flush(&pf->hw);
8930 	pf->fd_flush_cnt++;
8931 	pf->fd_add_err = 0;
8932 	do {
8933 		/* Check FD flush status every 5-6msec */
8934 		usleep_range(5000, 6000);
8935 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8936 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8937 			break;
8938 	} while (flush_wait_retry--);
8939 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8940 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8941 	} else {
8942 		/* replay sideband filters */
8943 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8944 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8945 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8946 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8947 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8948 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8949 	}
8950 }
8951 
8952 /**
8953  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8954  * @pf: board private structure
8955  **/
8956 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8957 {
8958 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8959 }
8960 
8961 /* We can see up to 256 filter programming desc in transit if the filters are
8962  * being applied really fast; before we see the first
8963  * filter miss error on Rx queue 0. Accumulating enough error messages before
8964  * reacting will make sure we don't cause flush too often.
8965  */
8966 #define I40E_MAX_FD_PROGRAM_ERROR 256
8967 
8968 /**
8969  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8970  * @pf: board private structure
8971  **/
8972 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8973 {
8974 
8975 	/* if interface is down do nothing */
8976 	if (test_bit(__I40E_DOWN, pf->state))
8977 		return;
8978 
8979 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8980 		i40e_fdir_flush_and_replay(pf);
8981 
8982 	i40e_fdir_check_and_reenable(pf);
8983 
8984 }
8985 
8986 /**
8987  * i40e_vsi_link_event - notify VSI of a link event
8988  * @vsi: vsi to be notified
8989  * @link_up: link up or down
8990  **/
8991 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8992 {
8993 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8994 		return;
8995 
8996 	switch (vsi->type) {
8997 	case I40E_VSI_MAIN:
8998 		if (!vsi->netdev || !vsi->netdev_registered)
8999 			break;
9000 
9001 		if (link_up) {
9002 			netif_carrier_on(vsi->netdev);
9003 			netif_tx_wake_all_queues(vsi->netdev);
9004 		} else {
9005 			netif_carrier_off(vsi->netdev);
9006 			netif_tx_stop_all_queues(vsi->netdev);
9007 		}
9008 		break;
9009 
9010 	case I40E_VSI_SRIOV:
9011 	case I40E_VSI_VMDQ2:
9012 	case I40E_VSI_CTRL:
9013 	case I40E_VSI_IWARP:
9014 	case I40E_VSI_MIRROR:
9015 	default:
9016 		/* there is no notification for other VSIs */
9017 		break;
9018 	}
9019 }
9020 
9021 /**
9022  * i40e_veb_link_event - notify elements on the veb of a link event
9023  * @veb: veb to be notified
9024  * @link_up: link up or down
9025  **/
9026 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9027 {
9028 	struct i40e_pf *pf;
9029 	int i;
9030 
9031 	if (!veb || !veb->pf)
9032 		return;
9033 	pf = veb->pf;
9034 
9035 	/* depth first... */
9036 	for (i = 0; i < I40E_MAX_VEB; i++)
9037 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9038 			i40e_veb_link_event(pf->veb[i], link_up);
9039 
9040 	/* ... now the local VSIs */
9041 	for (i = 0; i < pf->num_alloc_vsi; i++)
9042 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9043 			i40e_vsi_link_event(pf->vsi[i], link_up);
9044 }
9045 
9046 /**
9047  * i40e_link_event - Update netif_carrier status
9048  * @pf: board private structure
9049  **/
9050 static void i40e_link_event(struct i40e_pf *pf)
9051 {
9052 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9053 	u8 new_link_speed, old_link_speed;
9054 	i40e_status status;
9055 	bool new_link, old_link;
9056 
9057 	/* set this to force the get_link_status call to refresh state */
9058 	pf->hw.phy.get_link_info = true;
9059 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9060 	status = i40e_get_link_status(&pf->hw, &new_link);
9061 
9062 	/* On success, disable temp link polling */
9063 	if (status == I40E_SUCCESS) {
9064 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9065 	} else {
9066 		/* Enable link polling temporarily until i40e_get_link_status
9067 		 * returns I40E_SUCCESS
9068 		 */
9069 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9070 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9071 			status);
9072 		return;
9073 	}
9074 
9075 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9076 	new_link_speed = pf->hw.phy.link_info.link_speed;
9077 
9078 	if (new_link == old_link &&
9079 	    new_link_speed == old_link_speed &&
9080 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9081 	     new_link == netif_carrier_ok(vsi->netdev)))
9082 		return;
9083 
9084 	i40e_print_link_message(vsi, new_link);
9085 
9086 	/* Notify the base of the switch tree connected to
9087 	 * the link.  Floating VEBs are not notified.
9088 	 */
9089 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9090 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9091 	else
9092 		i40e_vsi_link_event(vsi, new_link);
9093 
9094 	if (pf->vf)
9095 		i40e_vc_notify_link_state(pf);
9096 
9097 	if (pf->flags & I40E_FLAG_PTP)
9098 		i40e_ptp_set_increment(pf);
9099 }
9100 
9101 /**
9102  * i40e_watchdog_subtask - periodic checks not using event driven response
9103  * @pf: board private structure
9104  **/
9105 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9106 {
9107 	int i;
9108 
9109 	/* if interface is down do nothing */
9110 	if (test_bit(__I40E_DOWN, pf->state) ||
9111 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9112 		return;
9113 
9114 	/* make sure we don't do these things too often */
9115 	if (time_before(jiffies, (pf->service_timer_previous +
9116 				  pf->service_timer_period)))
9117 		return;
9118 	pf->service_timer_previous = jiffies;
9119 
9120 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9121 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9122 		i40e_link_event(pf);
9123 
9124 	/* Update the stats for active netdevs so the network stack
9125 	 * can look at updated numbers whenever it cares to
9126 	 */
9127 	for (i = 0; i < pf->num_alloc_vsi; i++)
9128 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9129 			i40e_update_stats(pf->vsi[i]);
9130 
9131 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9132 		/* Update the stats for the active switching components */
9133 		for (i = 0; i < I40E_MAX_VEB; i++)
9134 			if (pf->veb[i])
9135 				i40e_update_veb_stats(pf->veb[i]);
9136 	}
9137 
9138 	i40e_ptp_rx_hang(pf);
9139 	i40e_ptp_tx_hang(pf);
9140 }
9141 
9142 /**
9143  * i40e_reset_subtask - Set up for resetting the device and driver
9144  * @pf: board private structure
9145  **/
9146 static void i40e_reset_subtask(struct i40e_pf *pf)
9147 {
9148 	u32 reset_flags = 0;
9149 
9150 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9151 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9152 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9153 	}
9154 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9155 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9156 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9157 	}
9158 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9159 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9160 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9161 	}
9162 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9163 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9164 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9165 	}
9166 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9167 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9168 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9169 	}
9170 
9171 	/* If there's a recovery already waiting, it takes
9172 	 * precedence before starting a new reset sequence.
9173 	 */
9174 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9175 		i40e_prep_for_reset(pf, false);
9176 		i40e_reset(pf);
9177 		i40e_rebuild(pf, false, false);
9178 	}
9179 
9180 	/* If we're already down or resetting, just bail */
9181 	if (reset_flags &&
9182 	    !test_bit(__I40E_DOWN, pf->state) &&
9183 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9184 		i40e_do_reset(pf, reset_flags, false);
9185 	}
9186 }
9187 
9188 /**
9189  * i40e_handle_link_event - Handle link event
9190  * @pf: board private structure
9191  * @e: event info posted on ARQ
9192  **/
9193 static void i40e_handle_link_event(struct i40e_pf *pf,
9194 				   struct i40e_arq_event_info *e)
9195 {
9196 	struct i40e_aqc_get_link_status *status =
9197 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9198 
9199 	/* Do a new status request to re-enable LSE reporting
9200 	 * and load new status information into the hw struct
9201 	 * This completely ignores any state information
9202 	 * in the ARQ event info, instead choosing to always
9203 	 * issue the AQ update link status command.
9204 	 */
9205 	i40e_link_event(pf);
9206 
9207 	/* Check if module meets thermal requirements */
9208 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9209 		dev_err(&pf->pdev->dev,
9210 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9211 		dev_err(&pf->pdev->dev,
9212 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9213 	} else {
9214 		/* check for unqualified module, if link is down, suppress
9215 		 * the message if link was forced to be down.
9216 		 */
9217 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9218 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9219 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9220 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9221 			dev_err(&pf->pdev->dev,
9222 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9223 			dev_err(&pf->pdev->dev,
9224 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9225 		}
9226 	}
9227 }
9228 
9229 /**
9230  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9231  * @pf: board private structure
9232  **/
9233 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9234 {
9235 	struct i40e_arq_event_info event;
9236 	struct i40e_hw *hw = &pf->hw;
9237 	u16 pending, i = 0;
9238 	i40e_status ret;
9239 	u16 opcode;
9240 	u32 oldval;
9241 	u32 val;
9242 
9243 	/* Do not run clean AQ when PF reset fails */
9244 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9245 		return;
9246 
9247 	/* check for error indications */
9248 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9249 	oldval = val;
9250 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9251 		if (hw->debug_mask & I40E_DEBUG_AQ)
9252 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9253 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9254 	}
9255 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9256 		if (hw->debug_mask & I40E_DEBUG_AQ)
9257 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9258 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9259 		pf->arq_overflows++;
9260 	}
9261 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9262 		if (hw->debug_mask & I40E_DEBUG_AQ)
9263 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9264 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9265 	}
9266 	if (oldval != val)
9267 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9268 
9269 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9270 	oldval = val;
9271 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9272 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9273 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9274 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9275 	}
9276 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9277 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9278 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9279 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9280 	}
9281 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9282 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9283 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9284 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9285 	}
9286 	if (oldval != val)
9287 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9288 
9289 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9290 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9291 	if (!event.msg_buf)
9292 		return;
9293 
9294 	do {
9295 		ret = i40e_clean_arq_element(hw, &event, &pending);
9296 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9297 			break;
9298 		else if (ret) {
9299 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9300 			break;
9301 		}
9302 
9303 		opcode = le16_to_cpu(event.desc.opcode);
9304 		switch (opcode) {
9305 
9306 		case i40e_aqc_opc_get_link_status:
9307 			i40e_handle_link_event(pf, &event);
9308 			break;
9309 		case i40e_aqc_opc_send_msg_to_pf:
9310 			ret = i40e_vc_process_vf_msg(pf,
9311 					le16_to_cpu(event.desc.retval),
9312 					le32_to_cpu(event.desc.cookie_high),
9313 					le32_to_cpu(event.desc.cookie_low),
9314 					event.msg_buf,
9315 					event.msg_len);
9316 			break;
9317 		case i40e_aqc_opc_lldp_update_mib:
9318 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9319 #ifdef CONFIG_I40E_DCB
9320 			rtnl_lock();
9321 			ret = i40e_handle_lldp_event(pf, &event);
9322 			rtnl_unlock();
9323 #endif /* CONFIG_I40E_DCB */
9324 			break;
9325 		case i40e_aqc_opc_event_lan_overflow:
9326 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9327 			i40e_handle_lan_overflow_event(pf, &event);
9328 			break;
9329 		case i40e_aqc_opc_send_msg_to_peer:
9330 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9331 			break;
9332 		case i40e_aqc_opc_nvm_erase:
9333 		case i40e_aqc_opc_nvm_update:
9334 		case i40e_aqc_opc_oem_post_update:
9335 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9336 				   "ARQ NVM operation 0x%04x completed\n",
9337 				   opcode);
9338 			break;
9339 		default:
9340 			dev_info(&pf->pdev->dev,
9341 				 "ARQ: Unknown event 0x%04x ignored\n",
9342 				 opcode);
9343 			break;
9344 		}
9345 	} while (i++ < pf->adminq_work_limit);
9346 
9347 	if (i < pf->adminq_work_limit)
9348 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9349 
9350 	/* re-enable Admin queue interrupt cause */
9351 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9352 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9353 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9354 	i40e_flush(hw);
9355 
9356 	kfree(event.msg_buf);
9357 }
9358 
9359 /**
9360  * i40e_verify_eeprom - make sure eeprom is good to use
9361  * @pf: board private structure
9362  **/
9363 static void i40e_verify_eeprom(struct i40e_pf *pf)
9364 {
9365 	int err;
9366 
9367 	err = i40e_diag_eeprom_test(&pf->hw);
9368 	if (err) {
9369 		/* retry in case of garbage read */
9370 		err = i40e_diag_eeprom_test(&pf->hw);
9371 		if (err) {
9372 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9373 				 err);
9374 			set_bit(__I40E_BAD_EEPROM, pf->state);
9375 		}
9376 	}
9377 
9378 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9379 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9380 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9381 	}
9382 }
9383 
9384 /**
9385  * i40e_enable_pf_switch_lb
9386  * @pf: pointer to the PF structure
9387  *
9388  * enable switch loop back or die - no point in a return value
9389  **/
9390 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9391 {
9392 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9393 	struct i40e_vsi_context ctxt;
9394 	int ret;
9395 
9396 	ctxt.seid = pf->main_vsi_seid;
9397 	ctxt.pf_num = pf->hw.pf_id;
9398 	ctxt.vf_num = 0;
9399 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9400 	if (ret) {
9401 		dev_info(&pf->pdev->dev,
9402 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9403 			 i40e_stat_str(&pf->hw, ret),
9404 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9405 		return;
9406 	}
9407 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9408 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9409 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9410 
9411 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9412 	if (ret) {
9413 		dev_info(&pf->pdev->dev,
9414 			 "update vsi switch failed, err %s aq_err %s\n",
9415 			 i40e_stat_str(&pf->hw, ret),
9416 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9417 	}
9418 }
9419 
9420 /**
9421  * i40e_disable_pf_switch_lb
9422  * @pf: pointer to the PF structure
9423  *
9424  * disable switch loop back or die - no point in a return value
9425  **/
9426 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9427 {
9428 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9429 	struct i40e_vsi_context ctxt;
9430 	int ret;
9431 
9432 	ctxt.seid = pf->main_vsi_seid;
9433 	ctxt.pf_num = pf->hw.pf_id;
9434 	ctxt.vf_num = 0;
9435 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9436 	if (ret) {
9437 		dev_info(&pf->pdev->dev,
9438 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9439 			 i40e_stat_str(&pf->hw, ret),
9440 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9441 		return;
9442 	}
9443 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9444 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9445 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9446 
9447 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9448 	if (ret) {
9449 		dev_info(&pf->pdev->dev,
9450 			 "update vsi switch failed, err %s aq_err %s\n",
9451 			 i40e_stat_str(&pf->hw, ret),
9452 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9453 	}
9454 }
9455 
9456 /**
9457  * i40e_config_bridge_mode - Configure the HW bridge mode
9458  * @veb: pointer to the bridge instance
9459  *
9460  * Configure the loop back mode for the LAN VSI that is downlink to the
9461  * specified HW bridge instance. It is expected this function is called
9462  * when a new HW bridge is instantiated.
9463  **/
9464 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9465 {
9466 	struct i40e_pf *pf = veb->pf;
9467 
9468 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9469 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9470 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9471 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9472 		i40e_disable_pf_switch_lb(pf);
9473 	else
9474 		i40e_enable_pf_switch_lb(pf);
9475 }
9476 
9477 /**
9478  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9479  * @veb: pointer to the VEB instance
9480  *
9481  * This is a recursive function that first builds the attached VSIs then
9482  * recurses in to build the next layer of VEB.  We track the connections
9483  * through our own index numbers because the seid's from the HW could
9484  * change across the reset.
9485  **/
9486 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9487 {
9488 	struct i40e_vsi *ctl_vsi = NULL;
9489 	struct i40e_pf *pf = veb->pf;
9490 	int v, veb_idx;
9491 	int ret;
9492 
9493 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9494 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9495 		if (pf->vsi[v] &&
9496 		    pf->vsi[v]->veb_idx == veb->idx &&
9497 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9498 			ctl_vsi = pf->vsi[v];
9499 			break;
9500 		}
9501 	}
9502 	if (!ctl_vsi) {
9503 		dev_info(&pf->pdev->dev,
9504 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9505 		ret = -ENOENT;
9506 		goto end_reconstitute;
9507 	}
9508 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9509 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9510 	ret = i40e_add_vsi(ctl_vsi);
9511 	if (ret) {
9512 		dev_info(&pf->pdev->dev,
9513 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9514 			 veb->idx, ret);
9515 		goto end_reconstitute;
9516 	}
9517 	i40e_vsi_reset_stats(ctl_vsi);
9518 
9519 	/* create the VEB in the switch and move the VSI onto the VEB */
9520 	ret = i40e_add_veb(veb, ctl_vsi);
9521 	if (ret)
9522 		goto end_reconstitute;
9523 
9524 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9525 		veb->bridge_mode = BRIDGE_MODE_VEB;
9526 	else
9527 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9528 	i40e_config_bridge_mode(veb);
9529 
9530 	/* create the remaining VSIs attached to this VEB */
9531 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9532 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9533 			continue;
9534 
9535 		if (pf->vsi[v]->veb_idx == veb->idx) {
9536 			struct i40e_vsi *vsi = pf->vsi[v];
9537 
9538 			vsi->uplink_seid = veb->seid;
9539 			ret = i40e_add_vsi(vsi);
9540 			if (ret) {
9541 				dev_info(&pf->pdev->dev,
9542 					 "rebuild of vsi_idx %d failed: %d\n",
9543 					 v, ret);
9544 				goto end_reconstitute;
9545 			}
9546 			i40e_vsi_reset_stats(vsi);
9547 		}
9548 	}
9549 
9550 	/* create any VEBs attached to this VEB - RECURSION */
9551 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9552 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9553 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9554 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9555 			if (ret)
9556 				break;
9557 		}
9558 	}
9559 
9560 end_reconstitute:
9561 	return ret;
9562 }
9563 
9564 /**
9565  * i40e_get_capabilities - get info about the HW
9566  * @pf: the PF struct
9567  **/
9568 static int i40e_get_capabilities(struct i40e_pf *pf,
9569 				 enum i40e_admin_queue_opc list_type)
9570 {
9571 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9572 	u16 data_size;
9573 	int buf_len;
9574 	int err;
9575 
9576 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9577 	do {
9578 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9579 		if (!cap_buf)
9580 			return -ENOMEM;
9581 
9582 		/* this loads the data into the hw struct for us */
9583 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9584 						    &data_size, list_type,
9585 						    NULL);
9586 		/* data loaded, buffer no longer needed */
9587 		kfree(cap_buf);
9588 
9589 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9590 			/* retry with a larger buffer */
9591 			buf_len = data_size;
9592 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9593 			dev_info(&pf->pdev->dev,
9594 				 "capability discovery failed, err %s aq_err %s\n",
9595 				 i40e_stat_str(&pf->hw, err),
9596 				 i40e_aq_str(&pf->hw,
9597 					     pf->hw.aq.asq_last_status));
9598 			return -ENODEV;
9599 		}
9600 	} while (err);
9601 
9602 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9603 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9604 			dev_info(&pf->pdev->dev,
9605 				 "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",
9606 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9607 				 pf->hw.func_caps.num_msix_vectors,
9608 				 pf->hw.func_caps.num_msix_vectors_vf,
9609 				 pf->hw.func_caps.fd_filters_guaranteed,
9610 				 pf->hw.func_caps.fd_filters_best_effort,
9611 				 pf->hw.func_caps.num_tx_qp,
9612 				 pf->hw.func_caps.num_vsis);
9613 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9614 			dev_info(&pf->pdev->dev,
9615 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9616 				 pf->hw.dev_caps.switch_mode,
9617 				 pf->hw.dev_caps.valid_functions);
9618 			dev_info(&pf->pdev->dev,
9619 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9620 				 pf->hw.dev_caps.sr_iov_1_1,
9621 				 pf->hw.dev_caps.num_vfs);
9622 			dev_info(&pf->pdev->dev,
9623 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9624 				 pf->hw.dev_caps.num_vsis,
9625 				 pf->hw.dev_caps.num_rx_qp,
9626 				 pf->hw.dev_caps.num_tx_qp);
9627 		}
9628 	}
9629 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9630 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9631 		       + pf->hw.func_caps.num_vfs)
9632 		if (pf->hw.revision_id == 0 &&
9633 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9634 			dev_info(&pf->pdev->dev,
9635 				 "got num_vsis %d, setting num_vsis to %d\n",
9636 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9637 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9638 		}
9639 	}
9640 	return 0;
9641 }
9642 
9643 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9644 
9645 /**
9646  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9647  * @pf: board private structure
9648  **/
9649 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9650 {
9651 	struct i40e_vsi *vsi;
9652 
9653 	/* quick workaround for an NVM issue that leaves a critical register
9654 	 * uninitialized
9655 	 */
9656 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9657 		static const u32 hkey[] = {
9658 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9659 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9660 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9661 			0x95b3a76d};
9662 		int i;
9663 
9664 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9665 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9666 	}
9667 
9668 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9669 		return;
9670 
9671 	/* find existing VSI and see if it needs configuring */
9672 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9673 
9674 	/* create a new VSI if none exists */
9675 	if (!vsi) {
9676 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9677 				     pf->vsi[pf->lan_vsi]->seid, 0);
9678 		if (!vsi) {
9679 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9680 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9681 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9682 			return;
9683 		}
9684 	}
9685 
9686 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9687 }
9688 
9689 /**
9690  * i40e_fdir_teardown - release the Flow Director resources
9691  * @pf: board private structure
9692  **/
9693 static void i40e_fdir_teardown(struct i40e_pf *pf)
9694 {
9695 	struct i40e_vsi *vsi;
9696 
9697 	i40e_fdir_filter_exit(pf);
9698 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9699 	if (vsi)
9700 		i40e_vsi_release(vsi);
9701 }
9702 
9703 /**
9704  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9705  * @vsi: PF main vsi
9706  * @seid: seid of main or channel VSIs
9707  *
9708  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9709  * existed before reset
9710  **/
9711 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9712 {
9713 	struct i40e_cloud_filter *cfilter;
9714 	struct i40e_pf *pf = vsi->back;
9715 	struct hlist_node *node;
9716 	i40e_status ret;
9717 
9718 	/* Add cloud filters back if they exist */
9719 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9720 				  cloud_node) {
9721 		if (cfilter->seid != seid)
9722 			continue;
9723 
9724 		if (cfilter->dst_port)
9725 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9726 								true);
9727 		else
9728 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9729 
9730 		if (ret) {
9731 			dev_dbg(&pf->pdev->dev,
9732 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9733 				i40e_stat_str(&pf->hw, ret),
9734 				i40e_aq_str(&pf->hw,
9735 					    pf->hw.aq.asq_last_status));
9736 			return ret;
9737 		}
9738 	}
9739 	return 0;
9740 }
9741 
9742 /**
9743  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9744  * @vsi: PF main vsi
9745  *
9746  * Rebuilds channel VSIs if they existed before reset
9747  **/
9748 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9749 {
9750 	struct i40e_channel *ch, *ch_tmp;
9751 	i40e_status ret;
9752 
9753 	if (list_empty(&vsi->ch_list))
9754 		return 0;
9755 
9756 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9757 		if (!ch->initialized)
9758 			break;
9759 		/* Proceed with creation of channel (VMDq2) VSI */
9760 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9761 		if (ret) {
9762 			dev_info(&vsi->back->pdev->dev,
9763 				 "failed to rebuild channels using uplink_seid %u\n",
9764 				 vsi->uplink_seid);
9765 			return ret;
9766 		}
9767 		/* Reconfigure TX queues using QTX_CTL register */
9768 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9769 		if (ret) {
9770 			dev_info(&vsi->back->pdev->dev,
9771 				 "failed to configure TX rings for channel %u\n",
9772 				 ch->seid);
9773 			return ret;
9774 		}
9775 		/* update 'next_base_queue' */
9776 		vsi->next_base_queue = vsi->next_base_queue +
9777 							ch->num_queue_pairs;
9778 		if (ch->max_tx_rate) {
9779 			u64 credits = ch->max_tx_rate;
9780 
9781 			if (i40e_set_bw_limit(vsi, ch->seid,
9782 					      ch->max_tx_rate))
9783 				return -EINVAL;
9784 
9785 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9786 			dev_dbg(&vsi->back->pdev->dev,
9787 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9788 				ch->max_tx_rate,
9789 				credits,
9790 				ch->seid);
9791 		}
9792 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9793 		if (ret) {
9794 			dev_dbg(&vsi->back->pdev->dev,
9795 				"Failed to rebuild cloud filters for channel VSI %u\n",
9796 				ch->seid);
9797 			return ret;
9798 		}
9799 	}
9800 	return 0;
9801 }
9802 
9803 /**
9804  * i40e_prep_for_reset - prep for the core to reset
9805  * @pf: board private structure
9806  * @lock_acquired: indicates whether or not the lock has been acquired
9807  * before this function was called.
9808  *
9809  * Close up the VFs and other things in prep for PF Reset.
9810   **/
9811 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9812 {
9813 	struct i40e_hw *hw = &pf->hw;
9814 	i40e_status ret = 0;
9815 	u32 v;
9816 
9817 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9818 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9819 		return;
9820 	if (i40e_check_asq_alive(&pf->hw))
9821 		i40e_vc_notify_reset(pf);
9822 
9823 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9824 
9825 	/* quiesce the VSIs and their queues that are not already DOWN */
9826 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9827 	if (!lock_acquired)
9828 		rtnl_lock();
9829 	i40e_pf_quiesce_all_vsi(pf);
9830 	if (!lock_acquired)
9831 		rtnl_unlock();
9832 
9833 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9834 		if (pf->vsi[v])
9835 			pf->vsi[v]->seid = 0;
9836 	}
9837 
9838 	i40e_shutdown_adminq(&pf->hw);
9839 
9840 	/* call shutdown HMC */
9841 	if (hw->hmc.hmc_obj) {
9842 		ret = i40e_shutdown_lan_hmc(hw);
9843 		if (ret)
9844 			dev_warn(&pf->pdev->dev,
9845 				 "shutdown_lan_hmc failed: %d\n", ret);
9846 	}
9847 
9848 	/* Save the current PTP time so that we can restore the time after the
9849 	 * reset completes.
9850 	 */
9851 	i40e_ptp_save_hw_time(pf);
9852 }
9853 
9854 /**
9855  * i40e_send_version - update firmware with driver version
9856  * @pf: PF struct
9857  */
9858 static void i40e_send_version(struct i40e_pf *pf)
9859 {
9860 	struct i40e_driver_version dv;
9861 
9862 	dv.major_version = DRV_VERSION_MAJOR;
9863 	dv.minor_version = DRV_VERSION_MINOR;
9864 	dv.build_version = DRV_VERSION_BUILD;
9865 	dv.subbuild_version = 0;
9866 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9867 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9868 }
9869 
9870 /**
9871  * i40e_get_oem_version - get OEM specific version information
9872  * @hw: pointer to the hardware structure
9873  **/
9874 static void i40e_get_oem_version(struct i40e_hw *hw)
9875 {
9876 	u16 block_offset = 0xffff;
9877 	u16 block_length = 0;
9878 	u16 capabilities = 0;
9879 	u16 gen_snap = 0;
9880 	u16 release = 0;
9881 
9882 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9883 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9884 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9885 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9886 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9887 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9888 #define I40E_NVM_OEM_LENGTH			3
9889 
9890 	/* Check if pointer to OEM version block is valid. */
9891 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9892 	if (block_offset == 0xffff)
9893 		return;
9894 
9895 	/* Check if OEM version block has correct length. */
9896 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9897 			   &block_length);
9898 	if (block_length < I40E_NVM_OEM_LENGTH)
9899 		return;
9900 
9901 	/* Check if OEM version format is as expected. */
9902 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9903 			   &capabilities);
9904 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9905 		return;
9906 
9907 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9908 			   &gen_snap);
9909 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9910 			   &release);
9911 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9912 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9913 }
9914 
9915 /**
9916  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9917  * @pf: board private structure
9918  **/
9919 static int i40e_reset(struct i40e_pf *pf)
9920 {
9921 	struct i40e_hw *hw = &pf->hw;
9922 	i40e_status ret;
9923 
9924 	ret = i40e_pf_reset(hw);
9925 	if (ret) {
9926 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9927 		set_bit(__I40E_RESET_FAILED, pf->state);
9928 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9929 	} else {
9930 		pf->pfr_count++;
9931 	}
9932 	return ret;
9933 }
9934 
9935 /**
9936  * i40e_rebuild - rebuild using a saved config
9937  * @pf: board private structure
9938  * @reinit: if the Main VSI needs to re-initialized.
9939  * @lock_acquired: indicates whether or not the lock has been acquired
9940  * before this function was called.
9941  **/
9942 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9943 {
9944 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
9945 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9946 	struct i40e_hw *hw = &pf->hw;
9947 	u8 set_fc_aq_fail = 0;
9948 	i40e_status ret;
9949 	u32 val;
9950 	int v;
9951 
9952 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9953 	    i40e_check_recovery_mode(pf)) {
9954 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
9955 	}
9956 
9957 	if (test_bit(__I40E_DOWN, pf->state) &&
9958 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
9959 	    !old_recovery_mode_bit)
9960 		goto clear_recovery;
9961 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9962 
9963 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9964 	ret = i40e_init_adminq(&pf->hw);
9965 	if (ret) {
9966 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9967 			 i40e_stat_str(&pf->hw, ret),
9968 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9969 		goto clear_recovery;
9970 	}
9971 	i40e_get_oem_version(&pf->hw);
9972 
9973 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9974 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9975 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9976 		/* The following delay is necessary for 4.33 firmware and older
9977 		 * to recover after EMP reset. 200 ms should suffice but we
9978 		 * put here 300 ms to be sure that FW is ready to operate
9979 		 * after reset.
9980 		 */
9981 		mdelay(300);
9982 	}
9983 
9984 	/* re-verify the eeprom if we just had an EMP reset */
9985 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9986 		i40e_verify_eeprom(pf);
9987 
9988 	/* if we are going out of or into recovery mode we have to act
9989 	 * accordingly with regard to resources initialization
9990 	 * and deinitialization
9991 	 */
9992 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
9993 	    old_recovery_mode_bit) {
9994 		if (i40e_get_capabilities(pf,
9995 					  i40e_aqc_opc_list_func_capabilities))
9996 			goto end_unlock;
9997 
9998 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
9999 			/* we're staying in recovery mode so we'll reinitialize
10000 			 * misc vector here
10001 			 */
10002 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10003 				goto end_unlock;
10004 		} else {
10005 			if (!lock_acquired)
10006 				rtnl_lock();
10007 			/* we're going out of recovery mode so we'll free
10008 			 * the IRQ allocated specifically for recovery mode
10009 			 * and restore the interrupt scheme
10010 			 */
10011 			free_irq(pf->pdev->irq, pf);
10012 			i40e_clear_interrupt_scheme(pf);
10013 			if (i40e_restore_interrupt_scheme(pf))
10014 				goto end_unlock;
10015 		}
10016 
10017 		/* tell the firmware that we're starting */
10018 		i40e_send_version(pf);
10019 
10020 		/* bail out in case recovery mode was detected, as there is
10021 		 * no need for further configuration.
10022 		 */
10023 		goto end_unlock;
10024 	}
10025 
10026 	i40e_clear_pxe_mode(hw);
10027 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10028 	if (ret)
10029 		goto end_core_reset;
10030 
10031 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10032 				hw->func_caps.num_rx_qp, 0, 0);
10033 	if (ret) {
10034 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10035 		goto end_core_reset;
10036 	}
10037 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10038 	if (ret) {
10039 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10040 		goto end_core_reset;
10041 	}
10042 
10043 	/* Enable FW to write a default DCB config on link-up */
10044 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10045 
10046 #ifdef CONFIG_I40E_DCB
10047 	ret = i40e_init_pf_dcb(pf);
10048 	if (ret) {
10049 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10050 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10051 		/* Continue without DCB enabled */
10052 	}
10053 #endif /* CONFIG_I40E_DCB */
10054 	/* do basic switch setup */
10055 	if (!lock_acquired)
10056 		rtnl_lock();
10057 	ret = i40e_setup_pf_switch(pf, reinit);
10058 	if (ret)
10059 		goto end_unlock;
10060 
10061 	/* The driver only wants link up/down and module qualification
10062 	 * reports from firmware.  Note the negative logic.
10063 	 */
10064 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10065 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10066 					 I40E_AQ_EVENT_MEDIA_NA |
10067 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10068 	if (ret)
10069 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10070 			 i40e_stat_str(&pf->hw, ret),
10071 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10072 
10073 	/* make sure our flow control settings are restored */
10074 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
10075 	if (ret)
10076 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
10077 			i40e_stat_str(&pf->hw, ret),
10078 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10079 
10080 	/* Rebuild the VSIs and VEBs that existed before reset.
10081 	 * They are still in our local switch element arrays, so only
10082 	 * need to rebuild the switch model in the HW.
10083 	 *
10084 	 * If there were VEBs but the reconstitution failed, we'll try
10085 	 * try to recover minimal use by getting the basic PF VSI working.
10086 	 */
10087 	if (vsi->uplink_seid != pf->mac_seid) {
10088 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10089 		/* find the one VEB connected to the MAC, and find orphans */
10090 		for (v = 0; v < I40E_MAX_VEB; v++) {
10091 			if (!pf->veb[v])
10092 				continue;
10093 
10094 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10095 			    pf->veb[v]->uplink_seid == 0) {
10096 				ret = i40e_reconstitute_veb(pf->veb[v]);
10097 
10098 				if (!ret)
10099 					continue;
10100 
10101 				/* If Main VEB failed, we're in deep doodoo,
10102 				 * so give up rebuilding the switch and set up
10103 				 * for minimal rebuild of PF VSI.
10104 				 * If orphan failed, we'll report the error
10105 				 * but try to keep going.
10106 				 */
10107 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10108 					dev_info(&pf->pdev->dev,
10109 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10110 						 ret);
10111 					vsi->uplink_seid = pf->mac_seid;
10112 					break;
10113 				} else if (pf->veb[v]->uplink_seid == 0) {
10114 					dev_info(&pf->pdev->dev,
10115 						 "rebuild of orphan VEB failed: %d\n",
10116 						 ret);
10117 				}
10118 			}
10119 		}
10120 	}
10121 
10122 	if (vsi->uplink_seid == pf->mac_seid) {
10123 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10124 		/* no VEB, so rebuild only the Main VSI */
10125 		ret = i40e_add_vsi(vsi);
10126 		if (ret) {
10127 			dev_info(&pf->pdev->dev,
10128 				 "rebuild of Main VSI failed: %d\n", ret);
10129 			goto end_unlock;
10130 		}
10131 	}
10132 
10133 	if (vsi->mqprio_qopt.max_rate[0]) {
10134 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10135 		u64 credits = 0;
10136 
10137 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10138 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10139 		if (ret)
10140 			goto end_unlock;
10141 
10142 		credits = max_tx_rate;
10143 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10144 		dev_dbg(&vsi->back->pdev->dev,
10145 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10146 			max_tx_rate,
10147 			credits,
10148 			vsi->seid);
10149 	}
10150 
10151 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10152 	if (ret)
10153 		goto end_unlock;
10154 
10155 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10156 	 * for this main VSI if they exist
10157 	 */
10158 	ret = i40e_rebuild_channels(vsi);
10159 	if (ret)
10160 		goto end_unlock;
10161 
10162 	/* Reconfigure hardware for allowing smaller MSS in the case
10163 	 * of TSO, so that we avoid the MDD being fired and causing
10164 	 * a reset in the case of small MSS+TSO.
10165 	 */
10166 #define I40E_REG_MSS          0x000E64DC
10167 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10168 #define I40E_64BYTE_MSS       0x400000
10169 	val = rd32(hw, I40E_REG_MSS);
10170 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10171 		val &= ~I40E_REG_MSS_MIN_MASK;
10172 		val |= I40E_64BYTE_MSS;
10173 		wr32(hw, I40E_REG_MSS, val);
10174 	}
10175 
10176 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10177 		msleep(75);
10178 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10179 		if (ret)
10180 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10181 				 i40e_stat_str(&pf->hw, ret),
10182 				 i40e_aq_str(&pf->hw,
10183 					     pf->hw.aq.asq_last_status));
10184 	}
10185 	/* reinit the misc interrupt */
10186 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10187 		ret = i40e_setup_misc_vector(pf);
10188 
10189 	/* Add a filter to drop all Flow control frames from any VSI from being
10190 	 * transmitted. By doing so we stop a malicious VF from sending out
10191 	 * PAUSE or PFC frames and potentially controlling traffic for other
10192 	 * PF/VF VSIs.
10193 	 * The FW can still send Flow control frames if enabled.
10194 	 */
10195 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10196 						       pf->main_vsi_seid);
10197 
10198 	/* restart the VSIs that were rebuilt and running before the reset */
10199 	i40e_pf_unquiesce_all_vsi(pf);
10200 
10201 	/* Release the RTNL lock before we start resetting VFs */
10202 	if (!lock_acquired)
10203 		rtnl_unlock();
10204 
10205 	/* Restore promiscuous settings */
10206 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10207 	if (ret)
10208 		dev_warn(&pf->pdev->dev,
10209 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10210 			 pf->cur_promisc ? "on" : "off",
10211 			 i40e_stat_str(&pf->hw, ret),
10212 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10213 
10214 	i40e_reset_all_vfs(pf, true);
10215 
10216 	/* tell the firmware that we're starting */
10217 	i40e_send_version(pf);
10218 
10219 	/* We've already released the lock, so don't do it again */
10220 	goto end_core_reset;
10221 
10222 end_unlock:
10223 	if (!lock_acquired)
10224 		rtnl_unlock();
10225 end_core_reset:
10226 	clear_bit(__I40E_RESET_FAILED, pf->state);
10227 clear_recovery:
10228 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10229 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10230 }
10231 
10232 /**
10233  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10234  * @pf: board private structure
10235  * @reinit: if the Main VSI needs to re-initialized.
10236  * @lock_acquired: indicates whether or not the lock has been acquired
10237  * before this function was called.
10238  **/
10239 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10240 				   bool lock_acquired)
10241 {
10242 	int ret;
10243 	/* Now we wait for GRST to settle out.
10244 	 * We don't have to delete the VEBs or VSIs from the hw switch
10245 	 * because the reset will make them disappear.
10246 	 */
10247 	ret = i40e_reset(pf);
10248 	if (!ret)
10249 		i40e_rebuild(pf, reinit, lock_acquired);
10250 }
10251 
10252 /**
10253  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10254  * @pf: board private structure
10255  *
10256  * Close up the VFs and other things in prep for a Core Reset,
10257  * then get ready to rebuild the world.
10258  * @lock_acquired: indicates whether or not the lock has been acquired
10259  * before this function was called.
10260  **/
10261 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10262 {
10263 	i40e_prep_for_reset(pf, lock_acquired);
10264 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10265 }
10266 
10267 /**
10268  * i40e_handle_mdd_event
10269  * @pf: pointer to the PF structure
10270  *
10271  * Called from the MDD irq handler to identify possibly malicious vfs
10272  **/
10273 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10274 {
10275 	struct i40e_hw *hw = &pf->hw;
10276 	bool mdd_detected = false;
10277 	struct i40e_vf *vf;
10278 	u32 reg;
10279 	int i;
10280 
10281 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10282 		return;
10283 
10284 	/* find what triggered the MDD event */
10285 	reg = rd32(hw, I40E_GL_MDET_TX);
10286 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10287 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10288 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10289 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10290 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10291 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10292 				I40E_GL_MDET_TX_EVENT_SHIFT;
10293 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10294 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10295 				pf->hw.func_caps.base_queue;
10296 		if (netif_msg_tx_err(pf))
10297 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10298 				 event, queue, pf_num, vf_num);
10299 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10300 		mdd_detected = true;
10301 	}
10302 	reg = rd32(hw, I40E_GL_MDET_RX);
10303 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10304 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10305 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10306 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10307 				I40E_GL_MDET_RX_EVENT_SHIFT;
10308 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10309 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10310 				pf->hw.func_caps.base_queue;
10311 		if (netif_msg_rx_err(pf))
10312 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10313 				 event, queue, func);
10314 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10315 		mdd_detected = true;
10316 	}
10317 
10318 	if (mdd_detected) {
10319 		reg = rd32(hw, I40E_PF_MDET_TX);
10320 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10321 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10322 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10323 		}
10324 		reg = rd32(hw, I40E_PF_MDET_RX);
10325 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10326 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10327 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10328 		}
10329 	}
10330 
10331 	/* see if one of the VFs needs its hand slapped */
10332 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10333 		vf = &(pf->vf[i]);
10334 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10335 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10336 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10337 			vf->num_mdd_events++;
10338 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10339 				 i);
10340 			dev_info(&pf->pdev->dev,
10341 				 "Use PF Control I/F to re-enable the VF\n");
10342 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10343 		}
10344 
10345 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10346 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10347 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10348 			vf->num_mdd_events++;
10349 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10350 				 i);
10351 			dev_info(&pf->pdev->dev,
10352 				 "Use PF Control I/F to re-enable the VF\n");
10353 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10354 		}
10355 	}
10356 
10357 	/* re-enable mdd interrupt cause */
10358 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10359 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10360 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10361 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10362 	i40e_flush(hw);
10363 }
10364 
10365 static const char *i40e_tunnel_name(u8 type)
10366 {
10367 	switch (type) {
10368 	case UDP_TUNNEL_TYPE_VXLAN:
10369 		return "vxlan";
10370 	case UDP_TUNNEL_TYPE_GENEVE:
10371 		return "geneve";
10372 	default:
10373 		return "unknown";
10374 	}
10375 }
10376 
10377 /**
10378  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
10379  * @pf: board private structure
10380  **/
10381 static void i40e_sync_udp_filters(struct i40e_pf *pf)
10382 {
10383 	int i;
10384 
10385 	/* loop through and set pending bit for all active UDP filters */
10386 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
10387 		if (pf->udp_ports[i].port)
10388 			pf->pending_udp_bitmap |= BIT_ULL(i);
10389 	}
10390 
10391 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
10392 }
10393 
10394 /**
10395  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
10396  * @pf: board private structure
10397  **/
10398 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
10399 {
10400 	struct i40e_hw *hw = &pf->hw;
10401 	u8 filter_index, type;
10402 	u16 port;
10403 	int i;
10404 
10405 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
10406 		return;
10407 
10408 	/* acquire RTNL to maintain state of flags and port requests */
10409 	rtnl_lock();
10410 
10411 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
10412 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
10413 			struct i40e_udp_port_config *udp_port;
10414 			i40e_status ret = 0;
10415 
10416 			udp_port = &pf->udp_ports[i];
10417 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
10418 
10419 			port = READ_ONCE(udp_port->port);
10420 			type = READ_ONCE(udp_port->type);
10421 			filter_index = READ_ONCE(udp_port->filter_index);
10422 
10423 			/* release RTNL while we wait on AQ command */
10424 			rtnl_unlock();
10425 
10426 			if (port)
10427 				ret = i40e_aq_add_udp_tunnel(hw, port,
10428 							     type,
10429 							     &filter_index,
10430 							     NULL);
10431 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
10432 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
10433 							     NULL);
10434 
10435 			/* reacquire RTNL so we can update filter_index */
10436 			rtnl_lock();
10437 
10438 			if (ret) {
10439 				dev_info(&pf->pdev->dev,
10440 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
10441 					 i40e_tunnel_name(type),
10442 					 port ? "add" : "delete",
10443 					 port,
10444 					 filter_index,
10445 					 i40e_stat_str(&pf->hw, ret),
10446 					 i40e_aq_str(&pf->hw,
10447 						     pf->hw.aq.asq_last_status));
10448 				if (port) {
10449 					/* failed to add, just reset port,
10450 					 * drop pending bit for any deletion
10451 					 */
10452 					udp_port->port = 0;
10453 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
10454 				}
10455 			} else if (port) {
10456 				/* record filter index on success */
10457 				udp_port->filter_index = filter_index;
10458 			}
10459 		}
10460 	}
10461 
10462 	rtnl_unlock();
10463 }
10464 
10465 /**
10466  * i40e_service_task - Run the driver's async subtasks
10467  * @work: pointer to work_struct containing our data
10468  **/
10469 static void i40e_service_task(struct work_struct *work)
10470 {
10471 	struct i40e_pf *pf = container_of(work,
10472 					  struct i40e_pf,
10473 					  service_task);
10474 	unsigned long start_time = jiffies;
10475 
10476 	/* don't bother with service tasks if a reset is in progress */
10477 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10478 	    test_bit(__I40E_SUSPENDED, pf->state))
10479 		return;
10480 
10481 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10482 		return;
10483 
10484 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10485 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10486 		i40e_sync_filters_subtask(pf);
10487 		i40e_reset_subtask(pf);
10488 		i40e_handle_mdd_event(pf);
10489 		i40e_vc_process_vflr_event(pf);
10490 		i40e_watchdog_subtask(pf);
10491 		i40e_fdir_reinit_subtask(pf);
10492 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10493 			/* Client subtask will reopen next time through. */
10494 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10495 							   true);
10496 		} else {
10497 			i40e_client_subtask(pf);
10498 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10499 					       pf->state))
10500 				i40e_notify_client_of_l2_param_changes(
10501 								pf->vsi[pf->lan_vsi]);
10502 		}
10503 		i40e_sync_filters_subtask(pf);
10504 		i40e_sync_udp_filters_subtask(pf);
10505 	} else {
10506 		i40e_reset_subtask(pf);
10507 	}
10508 
10509 	i40e_clean_adminq_subtask(pf);
10510 
10511 	/* flush memory to make sure state is correct before next watchdog */
10512 	smp_mb__before_atomic();
10513 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10514 
10515 	/* If the tasks have taken longer than one timer cycle or there
10516 	 * is more work to be done, reschedule the service task now
10517 	 * rather than wait for the timer to tick again.
10518 	 */
10519 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10520 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10521 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10522 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10523 		i40e_service_event_schedule(pf);
10524 }
10525 
10526 /**
10527  * i40e_service_timer - timer callback
10528  * @data: pointer to PF struct
10529  **/
10530 static void i40e_service_timer(struct timer_list *t)
10531 {
10532 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10533 
10534 	mod_timer(&pf->service_timer,
10535 		  round_jiffies(jiffies + pf->service_timer_period));
10536 	i40e_service_event_schedule(pf);
10537 }
10538 
10539 /**
10540  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10541  * @vsi: the VSI being configured
10542  **/
10543 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10544 {
10545 	struct i40e_pf *pf = vsi->back;
10546 
10547 	switch (vsi->type) {
10548 	case I40E_VSI_MAIN:
10549 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10550 		if (!vsi->num_tx_desc)
10551 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10552 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10553 		if (!vsi->num_rx_desc)
10554 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10555 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10556 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10557 			vsi->num_q_vectors = pf->num_lan_msix;
10558 		else
10559 			vsi->num_q_vectors = 1;
10560 
10561 		break;
10562 
10563 	case I40E_VSI_FDIR:
10564 		vsi->alloc_queue_pairs = 1;
10565 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10566 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10567 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10568 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10569 		vsi->num_q_vectors = pf->num_fdsb_msix;
10570 		break;
10571 
10572 	case I40E_VSI_VMDQ2:
10573 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10574 		if (!vsi->num_tx_desc)
10575 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10576 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10577 		if (!vsi->num_rx_desc)
10578 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10579 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10580 		vsi->num_q_vectors = pf->num_vmdq_msix;
10581 		break;
10582 
10583 	case I40E_VSI_SRIOV:
10584 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10585 		if (!vsi->num_tx_desc)
10586 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10587 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10588 		if (!vsi->num_rx_desc)
10589 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10590 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10591 		break;
10592 
10593 	default:
10594 		WARN_ON(1);
10595 		return -ENODATA;
10596 	}
10597 
10598 	return 0;
10599 }
10600 
10601 /**
10602  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10603  * @vsi: VSI pointer
10604  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10605  *
10606  * On error: returns error code (negative)
10607  * On success: returns 0
10608  **/
10609 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10610 {
10611 	struct i40e_ring **next_rings;
10612 	int size;
10613 	int ret = 0;
10614 
10615 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10616 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10617 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10618 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10619 	if (!vsi->tx_rings)
10620 		return -ENOMEM;
10621 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10622 	if (i40e_enabled_xdp_vsi(vsi)) {
10623 		vsi->xdp_rings = next_rings;
10624 		next_rings += vsi->alloc_queue_pairs;
10625 	}
10626 	vsi->rx_rings = next_rings;
10627 
10628 	if (alloc_qvectors) {
10629 		/* allocate memory for q_vector pointers */
10630 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10631 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10632 		if (!vsi->q_vectors) {
10633 			ret = -ENOMEM;
10634 			goto err_vectors;
10635 		}
10636 	}
10637 	return ret;
10638 
10639 err_vectors:
10640 	kfree(vsi->tx_rings);
10641 	return ret;
10642 }
10643 
10644 /**
10645  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10646  * @pf: board private structure
10647  * @type: type of VSI
10648  *
10649  * On error: returns error code (negative)
10650  * On success: returns vsi index in PF (positive)
10651  **/
10652 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10653 {
10654 	int ret = -ENODEV;
10655 	struct i40e_vsi *vsi;
10656 	int vsi_idx;
10657 	int i;
10658 
10659 	/* Need to protect the allocation of the VSIs at the PF level */
10660 	mutex_lock(&pf->switch_mutex);
10661 
10662 	/* VSI list may be fragmented if VSI creation/destruction has
10663 	 * been happening.  We can afford to do a quick scan to look
10664 	 * for any free VSIs in the list.
10665 	 *
10666 	 * find next empty vsi slot, looping back around if necessary
10667 	 */
10668 	i = pf->next_vsi;
10669 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10670 		i++;
10671 	if (i >= pf->num_alloc_vsi) {
10672 		i = 0;
10673 		while (i < pf->next_vsi && pf->vsi[i])
10674 			i++;
10675 	}
10676 
10677 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10678 		vsi_idx = i;             /* Found one! */
10679 	} else {
10680 		ret = -ENODEV;
10681 		goto unlock_pf;  /* out of VSI slots! */
10682 	}
10683 	pf->next_vsi = ++i;
10684 
10685 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10686 	if (!vsi) {
10687 		ret = -ENOMEM;
10688 		goto unlock_pf;
10689 	}
10690 	vsi->type = type;
10691 	vsi->back = pf;
10692 	set_bit(__I40E_VSI_DOWN, vsi->state);
10693 	vsi->flags = 0;
10694 	vsi->idx = vsi_idx;
10695 	vsi->int_rate_limit = 0;
10696 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10697 				pf->rss_table_size : 64;
10698 	vsi->netdev_registered = false;
10699 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10700 	hash_init(vsi->mac_filter_hash);
10701 	vsi->irqs_ready = false;
10702 
10703 	if (type == I40E_VSI_MAIN) {
10704 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10705 		if (!vsi->af_xdp_zc_qps)
10706 			goto err_rings;
10707 	}
10708 
10709 	ret = i40e_set_num_rings_in_vsi(vsi);
10710 	if (ret)
10711 		goto err_rings;
10712 
10713 	ret = i40e_vsi_alloc_arrays(vsi, true);
10714 	if (ret)
10715 		goto err_rings;
10716 
10717 	/* Setup default MSIX irq handler for VSI */
10718 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10719 
10720 	/* Initialize VSI lock */
10721 	spin_lock_init(&vsi->mac_filter_hash_lock);
10722 	pf->vsi[vsi_idx] = vsi;
10723 	ret = vsi_idx;
10724 	goto unlock_pf;
10725 
10726 err_rings:
10727 	bitmap_free(vsi->af_xdp_zc_qps);
10728 	pf->next_vsi = i - 1;
10729 	kfree(vsi);
10730 unlock_pf:
10731 	mutex_unlock(&pf->switch_mutex);
10732 	return ret;
10733 }
10734 
10735 /**
10736  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10737  * @vsi: VSI pointer
10738  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10739  *
10740  * On error: returns error code (negative)
10741  * On success: returns 0
10742  **/
10743 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10744 {
10745 	/* free the ring and vector containers */
10746 	if (free_qvectors) {
10747 		kfree(vsi->q_vectors);
10748 		vsi->q_vectors = NULL;
10749 	}
10750 	kfree(vsi->tx_rings);
10751 	vsi->tx_rings = NULL;
10752 	vsi->rx_rings = NULL;
10753 	vsi->xdp_rings = NULL;
10754 }
10755 
10756 /**
10757  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10758  * and lookup table
10759  * @vsi: Pointer to VSI structure
10760  */
10761 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10762 {
10763 	if (!vsi)
10764 		return;
10765 
10766 	kfree(vsi->rss_hkey_user);
10767 	vsi->rss_hkey_user = NULL;
10768 
10769 	kfree(vsi->rss_lut_user);
10770 	vsi->rss_lut_user = NULL;
10771 }
10772 
10773 /**
10774  * i40e_vsi_clear - Deallocate the VSI provided
10775  * @vsi: the VSI being un-configured
10776  **/
10777 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10778 {
10779 	struct i40e_pf *pf;
10780 
10781 	if (!vsi)
10782 		return 0;
10783 
10784 	if (!vsi->back)
10785 		goto free_vsi;
10786 	pf = vsi->back;
10787 
10788 	mutex_lock(&pf->switch_mutex);
10789 	if (!pf->vsi[vsi->idx]) {
10790 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10791 			vsi->idx, vsi->idx, vsi->type);
10792 		goto unlock_vsi;
10793 	}
10794 
10795 	if (pf->vsi[vsi->idx] != vsi) {
10796 		dev_err(&pf->pdev->dev,
10797 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10798 			pf->vsi[vsi->idx]->idx,
10799 			pf->vsi[vsi->idx]->type,
10800 			vsi->idx, vsi->type);
10801 		goto unlock_vsi;
10802 	}
10803 
10804 	/* updates the PF for this cleared vsi */
10805 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10806 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10807 
10808 	bitmap_free(vsi->af_xdp_zc_qps);
10809 	i40e_vsi_free_arrays(vsi, true);
10810 	i40e_clear_rss_config_user(vsi);
10811 
10812 	pf->vsi[vsi->idx] = NULL;
10813 	if (vsi->idx < pf->next_vsi)
10814 		pf->next_vsi = vsi->idx;
10815 
10816 unlock_vsi:
10817 	mutex_unlock(&pf->switch_mutex);
10818 free_vsi:
10819 	kfree(vsi);
10820 
10821 	return 0;
10822 }
10823 
10824 /**
10825  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10826  * @vsi: the VSI being cleaned
10827  **/
10828 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10829 {
10830 	int i;
10831 
10832 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10833 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10834 			kfree_rcu(vsi->tx_rings[i], rcu);
10835 			vsi->tx_rings[i] = NULL;
10836 			vsi->rx_rings[i] = NULL;
10837 			if (vsi->xdp_rings)
10838 				vsi->xdp_rings[i] = NULL;
10839 		}
10840 	}
10841 }
10842 
10843 /**
10844  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10845  * @vsi: the VSI being configured
10846  **/
10847 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10848 {
10849 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10850 	struct i40e_pf *pf = vsi->back;
10851 	struct i40e_ring *ring;
10852 
10853 	/* Set basic values in the rings to be used later during open() */
10854 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10855 		/* allocate space for both Tx and Rx in one shot */
10856 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10857 		if (!ring)
10858 			goto err_out;
10859 
10860 		ring->queue_index = i;
10861 		ring->reg_idx = vsi->base_queue + i;
10862 		ring->ring_active = false;
10863 		ring->vsi = vsi;
10864 		ring->netdev = vsi->netdev;
10865 		ring->dev = &pf->pdev->dev;
10866 		ring->count = vsi->num_tx_desc;
10867 		ring->size = 0;
10868 		ring->dcb_tc = 0;
10869 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10870 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10871 		ring->itr_setting = pf->tx_itr_default;
10872 		vsi->tx_rings[i] = ring++;
10873 
10874 		if (!i40e_enabled_xdp_vsi(vsi))
10875 			goto setup_rx;
10876 
10877 		ring->queue_index = vsi->alloc_queue_pairs + i;
10878 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10879 		ring->ring_active = false;
10880 		ring->vsi = vsi;
10881 		ring->netdev = NULL;
10882 		ring->dev = &pf->pdev->dev;
10883 		ring->count = vsi->num_tx_desc;
10884 		ring->size = 0;
10885 		ring->dcb_tc = 0;
10886 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10887 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10888 		set_ring_xdp(ring);
10889 		ring->itr_setting = pf->tx_itr_default;
10890 		vsi->xdp_rings[i] = ring++;
10891 
10892 setup_rx:
10893 		ring->queue_index = i;
10894 		ring->reg_idx = vsi->base_queue + i;
10895 		ring->ring_active = false;
10896 		ring->vsi = vsi;
10897 		ring->netdev = vsi->netdev;
10898 		ring->dev = &pf->pdev->dev;
10899 		ring->count = vsi->num_rx_desc;
10900 		ring->size = 0;
10901 		ring->dcb_tc = 0;
10902 		ring->itr_setting = pf->rx_itr_default;
10903 		vsi->rx_rings[i] = ring;
10904 	}
10905 
10906 	return 0;
10907 
10908 err_out:
10909 	i40e_vsi_clear_rings(vsi);
10910 	return -ENOMEM;
10911 }
10912 
10913 /**
10914  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10915  * @pf: board private structure
10916  * @vectors: the number of MSI-X vectors to request
10917  *
10918  * Returns the number of vectors reserved, or error
10919  **/
10920 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10921 {
10922 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10923 					I40E_MIN_MSIX, vectors);
10924 	if (vectors < 0) {
10925 		dev_info(&pf->pdev->dev,
10926 			 "MSI-X vector reservation failed: %d\n", vectors);
10927 		vectors = 0;
10928 	}
10929 
10930 	return vectors;
10931 }
10932 
10933 /**
10934  * i40e_init_msix - Setup the MSIX capability
10935  * @pf: board private structure
10936  *
10937  * Work with the OS to set up the MSIX vectors needed.
10938  *
10939  * Returns the number of vectors reserved or negative on failure
10940  **/
10941 static int i40e_init_msix(struct i40e_pf *pf)
10942 {
10943 	struct i40e_hw *hw = &pf->hw;
10944 	int cpus, extra_vectors;
10945 	int vectors_left;
10946 	int v_budget, i;
10947 	int v_actual;
10948 	int iwarp_requested = 0;
10949 
10950 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10951 		return -ENODEV;
10952 
10953 	/* The number of vectors we'll request will be comprised of:
10954 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10955 	 *   - The number of LAN queue pairs
10956 	 *	- Queues being used for RSS.
10957 	 *		We don't need as many as max_rss_size vectors.
10958 	 *		use rss_size instead in the calculation since that
10959 	 *		is governed by number of cpus in the system.
10960 	 *	- assumes symmetric Tx/Rx pairing
10961 	 *   - The number of VMDq pairs
10962 	 *   - The CPU count within the NUMA node if iWARP is enabled
10963 	 * Once we count this up, try the request.
10964 	 *
10965 	 * If we can't get what we want, we'll simplify to nearly nothing
10966 	 * and try again.  If that still fails, we punt.
10967 	 */
10968 	vectors_left = hw->func_caps.num_msix_vectors;
10969 	v_budget = 0;
10970 
10971 	/* reserve one vector for miscellaneous handler */
10972 	if (vectors_left) {
10973 		v_budget++;
10974 		vectors_left--;
10975 	}
10976 
10977 	/* reserve some vectors for the main PF traffic queues. Initially we
10978 	 * only reserve at most 50% of the available vectors, in the case that
10979 	 * the number of online CPUs is large. This ensures that we can enable
10980 	 * extra features as well. Once we've enabled the other features, we
10981 	 * will use any remaining vectors to reach as close as we can to the
10982 	 * number of online CPUs.
10983 	 */
10984 	cpus = num_online_cpus();
10985 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10986 	vectors_left -= pf->num_lan_msix;
10987 
10988 	/* reserve one vector for sideband flow director */
10989 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10990 		if (vectors_left) {
10991 			pf->num_fdsb_msix = 1;
10992 			v_budget++;
10993 			vectors_left--;
10994 		} else {
10995 			pf->num_fdsb_msix = 0;
10996 		}
10997 	}
10998 
10999 	/* can we reserve enough for iWARP? */
11000 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11001 		iwarp_requested = pf->num_iwarp_msix;
11002 
11003 		if (!vectors_left)
11004 			pf->num_iwarp_msix = 0;
11005 		else if (vectors_left < pf->num_iwarp_msix)
11006 			pf->num_iwarp_msix = 1;
11007 		v_budget += pf->num_iwarp_msix;
11008 		vectors_left -= pf->num_iwarp_msix;
11009 	}
11010 
11011 	/* any vectors left over go for VMDq support */
11012 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11013 		if (!vectors_left) {
11014 			pf->num_vmdq_msix = 0;
11015 			pf->num_vmdq_qps = 0;
11016 		} else {
11017 			int vmdq_vecs_wanted =
11018 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11019 			int vmdq_vecs =
11020 				min_t(int, vectors_left, vmdq_vecs_wanted);
11021 
11022 			/* if we're short on vectors for what's desired, we limit
11023 			 * the queues per vmdq.  If this is still more than are
11024 			 * available, the user will need to change the number of
11025 			 * queues/vectors used by the PF later with the ethtool
11026 			 * channels command
11027 			 */
11028 			if (vectors_left < vmdq_vecs_wanted) {
11029 				pf->num_vmdq_qps = 1;
11030 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11031 				vmdq_vecs = min_t(int,
11032 						  vectors_left,
11033 						  vmdq_vecs_wanted);
11034 			}
11035 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11036 
11037 			v_budget += vmdq_vecs;
11038 			vectors_left -= vmdq_vecs;
11039 		}
11040 	}
11041 
11042 	/* On systems with a large number of SMP cores, we previously limited
11043 	 * the number of vectors for num_lan_msix to be at most 50% of the
11044 	 * available vectors, to allow for other features. Now, we add back
11045 	 * the remaining vectors. However, we ensure that the total
11046 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11047 	 * calculate the number of vectors we can add without going over the
11048 	 * cap of CPUs. For systems with a small number of CPUs this will be
11049 	 * zero.
11050 	 */
11051 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11052 	pf->num_lan_msix += extra_vectors;
11053 	vectors_left -= extra_vectors;
11054 
11055 	WARN(vectors_left < 0,
11056 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11057 
11058 	v_budget += pf->num_lan_msix;
11059 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11060 				   GFP_KERNEL);
11061 	if (!pf->msix_entries)
11062 		return -ENOMEM;
11063 
11064 	for (i = 0; i < v_budget; i++)
11065 		pf->msix_entries[i].entry = i;
11066 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11067 
11068 	if (v_actual < I40E_MIN_MSIX) {
11069 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11070 		kfree(pf->msix_entries);
11071 		pf->msix_entries = NULL;
11072 		pci_disable_msix(pf->pdev);
11073 		return -ENODEV;
11074 
11075 	} else if (v_actual == I40E_MIN_MSIX) {
11076 		/* Adjust for minimal MSIX use */
11077 		pf->num_vmdq_vsis = 0;
11078 		pf->num_vmdq_qps = 0;
11079 		pf->num_lan_qps = 1;
11080 		pf->num_lan_msix = 1;
11081 
11082 	} else if (v_actual != v_budget) {
11083 		/* If we have limited resources, we will start with no vectors
11084 		 * for the special features and then allocate vectors to some
11085 		 * of these features based on the policy and at the end disable
11086 		 * the features that did not get any vectors.
11087 		 */
11088 		int vec;
11089 
11090 		dev_info(&pf->pdev->dev,
11091 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11092 			 v_actual, v_budget);
11093 		/* reserve the misc vector */
11094 		vec = v_actual - 1;
11095 
11096 		/* Scale vector usage down */
11097 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11098 		pf->num_vmdq_vsis = 1;
11099 		pf->num_vmdq_qps = 1;
11100 
11101 		/* partition out the remaining vectors */
11102 		switch (vec) {
11103 		case 2:
11104 			pf->num_lan_msix = 1;
11105 			break;
11106 		case 3:
11107 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11108 				pf->num_lan_msix = 1;
11109 				pf->num_iwarp_msix = 1;
11110 			} else {
11111 				pf->num_lan_msix = 2;
11112 			}
11113 			break;
11114 		default:
11115 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11116 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11117 						 iwarp_requested);
11118 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11119 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11120 			} else {
11121 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11122 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11123 			}
11124 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11125 				pf->num_fdsb_msix = 1;
11126 				vec--;
11127 			}
11128 			pf->num_lan_msix = min_t(int,
11129 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11130 							      pf->num_lan_msix);
11131 			pf->num_lan_qps = pf->num_lan_msix;
11132 			break;
11133 		}
11134 	}
11135 
11136 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11137 	    (pf->num_fdsb_msix == 0)) {
11138 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11139 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11140 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11141 	}
11142 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11143 	    (pf->num_vmdq_msix == 0)) {
11144 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11145 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11146 	}
11147 
11148 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11149 	    (pf->num_iwarp_msix == 0)) {
11150 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11151 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11152 	}
11153 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11154 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11155 		   pf->num_lan_msix,
11156 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11157 		   pf->num_fdsb_msix,
11158 		   pf->num_iwarp_msix);
11159 
11160 	return v_actual;
11161 }
11162 
11163 /**
11164  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11165  * @vsi: the VSI being configured
11166  * @v_idx: index of the vector in the vsi struct
11167  * @cpu: cpu to be used on affinity_mask
11168  *
11169  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11170  **/
11171 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
11172 {
11173 	struct i40e_q_vector *q_vector;
11174 
11175 	/* allocate q_vector */
11176 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11177 	if (!q_vector)
11178 		return -ENOMEM;
11179 
11180 	q_vector->vsi = vsi;
11181 	q_vector->v_idx = v_idx;
11182 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11183 
11184 	if (vsi->netdev)
11185 		netif_napi_add(vsi->netdev, &q_vector->napi,
11186 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11187 
11188 	/* tie q_vector and vsi together */
11189 	vsi->q_vectors[v_idx] = q_vector;
11190 
11191 	return 0;
11192 }
11193 
11194 /**
11195  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11196  * @vsi: the VSI being configured
11197  *
11198  * We allocate one q_vector per queue interrupt.  If allocation fails we
11199  * return -ENOMEM.
11200  **/
11201 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11202 {
11203 	struct i40e_pf *pf = vsi->back;
11204 	int err, v_idx, num_q_vectors, current_cpu;
11205 
11206 	/* if not MSIX, give the one vector only to the LAN VSI */
11207 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11208 		num_q_vectors = vsi->num_q_vectors;
11209 	else if (vsi == pf->vsi[pf->lan_vsi])
11210 		num_q_vectors = 1;
11211 	else
11212 		return -EINVAL;
11213 
11214 	current_cpu = cpumask_first(cpu_online_mask);
11215 
11216 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11217 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
11218 		if (err)
11219 			goto err_out;
11220 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
11221 		if (unlikely(current_cpu >= nr_cpu_ids))
11222 			current_cpu = cpumask_first(cpu_online_mask);
11223 	}
11224 
11225 	return 0;
11226 
11227 err_out:
11228 	while (v_idx--)
11229 		i40e_free_q_vector(vsi, v_idx);
11230 
11231 	return err;
11232 }
11233 
11234 /**
11235  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11236  * @pf: board private structure to initialize
11237  **/
11238 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11239 {
11240 	int vectors = 0;
11241 	ssize_t size;
11242 
11243 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11244 		vectors = i40e_init_msix(pf);
11245 		if (vectors < 0) {
11246 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11247 				       I40E_FLAG_IWARP_ENABLED	|
11248 				       I40E_FLAG_RSS_ENABLED	|
11249 				       I40E_FLAG_DCB_CAPABLE	|
11250 				       I40E_FLAG_DCB_ENABLED	|
11251 				       I40E_FLAG_SRIOV_ENABLED	|
11252 				       I40E_FLAG_FD_SB_ENABLED	|
11253 				       I40E_FLAG_FD_ATR_ENABLED	|
11254 				       I40E_FLAG_VMDQ_ENABLED);
11255 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11256 
11257 			/* rework the queue expectations without MSIX */
11258 			i40e_determine_queue_usage(pf);
11259 		}
11260 	}
11261 
11262 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11263 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11264 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11265 		vectors = pci_enable_msi(pf->pdev);
11266 		if (vectors < 0) {
11267 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11268 				 vectors);
11269 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11270 		}
11271 		vectors = 1;  /* one MSI or Legacy vector */
11272 	}
11273 
11274 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11275 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11276 
11277 	/* set up vector assignment tracking */
11278 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11279 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11280 	if (!pf->irq_pile)
11281 		return -ENOMEM;
11282 
11283 	pf->irq_pile->num_entries = vectors;
11284 	pf->irq_pile->search_hint = 0;
11285 
11286 	/* track first vector for misc interrupts, ignore return */
11287 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11288 
11289 	return 0;
11290 }
11291 
11292 /**
11293  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11294  * @pf: private board data structure
11295  *
11296  * Restore the interrupt scheme that was cleared when we suspended the
11297  * device. This should be called during resume to re-allocate the q_vectors
11298  * and reacquire IRQs.
11299  */
11300 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11301 {
11302 	int err, i;
11303 
11304 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11305 	 * scheme. We need to re-enabled them here in order to attempt to
11306 	 * re-acquire the MSI or MSI-X vectors
11307 	 */
11308 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11309 
11310 	err = i40e_init_interrupt_scheme(pf);
11311 	if (err)
11312 		return err;
11313 
11314 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11315 	 * rings together again.
11316 	 */
11317 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11318 		if (pf->vsi[i]) {
11319 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11320 			if (err)
11321 				goto err_unwind;
11322 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11323 		}
11324 	}
11325 
11326 	err = i40e_setup_misc_vector(pf);
11327 	if (err)
11328 		goto err_unwind;
11329 
11330 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11331 		i40e_client_update_msix_info(pf);
11332 
11333 	return 0;
11334 
11335 err_unwind:
11336 	while (i--) {
11337 		if (pf->vsi[i])
11338 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11339 	}
11340 
11341 	return err;
11342 }
11343 
11344 /**
11345  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11346  * non queue events in recovery mode
11347  * @pf: board private structure
11348  *
11349  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11350  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11351  * This is handled differently than in recovery mode since no Tx/Rx resources
11352  * are being allocated.
11353  **/
11354 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11355 {
11356 	int err;
11357 
11358 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11359 		err = i40e_setup_misc_vector(pf);
11360 
11361 		if (err) {
11362 			dev_info(&pf->pdev->dev,
11363 				 "MSI-X misc vector request failed, error %d\n",
11364 				 err);
11365 			return err;
11366 		}
11367 	} else {
11368 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11369 
11370 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11371 				  pf->int_name, pf);
11372 
11373 		if (err) {
11374 			dev_info(&pf->pdev->dev,
11375 				 "MSI/legacy misc vector request failed, error %d\n",
11376 				 err);
11377 			return err;
11378 		}
11379 		i40e_enable_misc_int_causes(pf);
11380 		i40e_irq_dynamic_enable_icr0(pf);
11381 	}
11382 
11383 	return 0;
11384 }
11385 
11386 /**
11387  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11388  * @pf: board private structure
11389  *
11390  * This sets up the handler for MSIX 0, which is used to manage the
11391  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11392  * when in MSI or Legacy interrupt mode.
11393  **/
11394 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11395 {
11396 	struct i40e_hw *hw = &pf->hw;
11397 	int err = 0;
11398 
11399 	/* Only request the IRQ once, the first time through. */
11400 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11401 		err = request_irq(pf->msix_entries[0].vector,
11402 				  i40e_intr, 0, pf->int_name, pf);
11403 		if (err) {
11404 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11405 			dev_info(&pf->pdev->dev,
11406 				 "request_irq for %s failed: %d\n",
11407 				 pf->int_name, err);
11408 			return -EFAULT;
11409 		}
11410 	}
11411 
11412 	i40e_enable_misc_int_causes(pf);
11413 
11414 	/* associate no queues to the misc vector */
11415 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11416 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
11417 
11418 	i40e_flush(hw);
11419 
11420 	i40e_irq_dynamic_enable_icr0(pf);
11421 
11422 	return err;
11423 }
11424 
11425 /**
11426  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11427  * @vsi: Pointer to vsi structure
11428  * @seed: Buffter to store the hash keys
11429  * @lut: Buffer to store the lookup table entries
11430  * @lut_size: Size of buffer to store the lookup table entries
11431  *
11432  * Return 0 on success, negative on failure
11433  */
11434 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11435 			   u8 *lut, u16 lut_size)
11436 {
11437 	struct i40e_pf *pf = vsi->back;
11438 	struct i40e_hw *hw = &pf->hw;
11439 	int ret = 0;
11440 
11441 	if (seed) {
11442 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11443 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11444 		if (ret) {
11445 			dev_info(&pf->pdev->dev,
11446 				 "Cannot get RSS key, err %s aq_err %s\n",
11447 				 i40e_stat_str(&pf->hw, ret),
11448 				 i40e_aq_str(&pf->hw,
11449 					     pf->hw.aq.asq_last_status));
11450 			return ret;
11451 		}
11452 	}
11453 
11454 	if (lut) {
11455 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
11456 
11457 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11458 		if (ret) {
11459 			dev_info(&pf->pdev->dev,
11460 				 "Cannot get RSS lut, err %s aq_err %s\n",
11461 				 i40e_stat_str(&pf->hw, ret),
11462 				 i40e_aq_str(&pf->hw,
11463 					     pf->hw.aq.asq_last_status));
11464 			return ret;
11465 		}
11466 	}
11467 
11468 	return ret;
11469 }
11470 
11471 /**
11472  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11473  * @vsi: Pointer to vsi structure
11474  * @seed: RSS hash seed
11475  * @lut: Lookup table
11476  * @lut_size: Lookup table size
11477  *
11478  * Returns 0 on success, negative on failure
11479  **/
11480 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11481 			       const u8 *lut, u16 lut_size)
11482 {
11483 	struct i40e_pf *pf = vsi->back;
11484 	struct i40e_hw *hw = &pf->hw;
11485 	u16 vf_id = vsi->vf_id;
11486 	u8 i;
11487 
11488 	/* Fill out hash function seed */
11489 	if (seed) {
11490 		u32 *seed_dw = (u32 *)seed;
11491 
11492 		if (vsi->type == I40E_VSI_MAIN) {
11493 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11494 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11495 		} else if (vsi->type == I40E_VSI_SRIOV) {
11496 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11497 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11498 		} else {
11499 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11500 		}
11501 	}
11502 
11503 	if (lut) {
11504 		u32 *lut_dw = (u32 *)lut;
11505 
11506 		if (vsi->type == I40E_VSI_MAIN) {
11507 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11508 				return -EINVAL;
11509 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11510 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11511 		} else if (vsi->type == I40E_VSI_SRIOV) {
11512 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11513 				return -EINVAL;
11514 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11515 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11516 		} else {
11517 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11518 		}
11519 	}
11520 	i40e_flush(hw);
11521 
11522 	return 0;
11523 }
11524 
11525 /**
11526  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11527  * @vsi: Pointer to VSI structure
11528  * @seed: Buffer to store the keys
11529  * @lut: Buffer to store the lookup table entries
11530  * @lut_size: Size of buffer to store the lookup table entries
11531  *
11532  * Returns 0 on success, negative on failure
11533  */
11534 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11535 			    u8 *lut, u16 lut_size)
11536 {
11537 	struct i40e_pf *pf = vsi->back;
11538 	struct i40e_hw *hw = &pf->hw;
11539 	u16 i;
11540 
11541 	if (seed) {
11542 		u32 *seed_dw = (u32 *)seed;
11543 
11544 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11545 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11546 	}
11547 	if (lut) {
11548 		u32 *lut_dw = (u32 *)lut;
11549 
11550 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11551 			return -EINVAL;
11552 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11553 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11554 	}
11555 
11556 	return 0;
11557 }
11558 
11559 /**
11560  * i40e_config_rss - Configure RSS keys and lut
11561  * @vsi: Pointer to VSI structure
11562  * @seed: RSS hash seed
11563  * @lut: Lookup table
11564  * @lut_size: Lookup table size
11565  *
11566  * Returns 0 on success, negative on failure
11567  */
11568 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11569 {
11570 	struct i40e_pf *pf = vsi->back;
11571 
11572 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11573 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11574 	else
11575 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11576 }
11577 
11578 /**
11579  * i40e_get_rss - Get RSS keys and lut
11580  * @vsi: Pointer to VSI structure
11581  * @seed: Buffer to store the keys
11582  * @lut: Buffer to store the lookup table entries
11583  * @lut_size: Size of buffer to store the lookup table entries
11584  *
11585  * Returns 0 on success, negative on failure
11586  */
11587 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11588 {
11589 	struct i40e_pf *pf = vsi->back;
11590 
11591 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11592 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11593 	else
11594 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11595 }
11596 
11597 /**
11598  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11599  * @pf: Pointer to board private structure
11600  * @lut: Lookup table
11601  * @rss_table_size: Lookup table size
11602  * @rss_size: Range of queue number for hashing
11603  */
11604 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11605 		       u16 rss_table_size, u16 rss_size)
11606 {
11607 	u16 i;
11608 
11609 	for (i = 0; i < rss_table_size; i++)
11610 		lut[i] = i % rss_size;
11611 }
11612 
11613 /**
11614  * i40e_pf_config_rss - Prepare for RSS if used
11615  * @pf: board private structure
11616  **/
11617 static int i40e_pf_config_rss(struct i40e_pf *pf)
11618 {
11619 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11620 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11621 	u8 *lut;
11622 	struct i40e_hw *hw = &pf->hw;
11623 	u32 reg_val;
11624 	u64 hena;
11625 	int ret;
11626 
11627 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11628 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11629 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11630 	hena |= i40e_pf_get_default_rss_hena(pf);
11631 
11632 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11633 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11634 
11635 	/* Determine the RSS table size based on the hardware capabilities */
11636 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11637 	reg_val = (pf->rss_table_size == 512) ?
11638 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11639 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11640 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11641 
11642 	/* Determine the RSS size of the VSI */
11643 	if (!vsi->rss_size) {
11644 		u16 qcount;
11645 		/* If the firmware does something weird during VSI init, we
11646 		 * could end up with zero TCs. Check for that to avoid
11647 		 * divide-by-zero. It probably won't pass traffic, but it also
11648 		 * won't panic.
11649 		 */
11650 		qcount = vsi->num_queue_pairs /
11651 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11652 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11653 	}
11654 	if (!vsi->rss_size)
11655 		return -EINVAL;
11656 
11657 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11658 	if (!lut)
11659 		return -ENOMEM;
11660 
11661 	/* Use user configured lut if there is one, otherwise use default */
11662 	if (vsi->rss_lut_user)
11663 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11664 	else
11665 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11666 
11667 	/* Use user configured hash key if there is one, otherwise
11668 	 * use default.
11669 	 */
11670 	if (vsi->rss_hkey_user)
11671 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11672 	else
11673 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11674 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11675 	kfree(lut);
11676 
11677 	return ret;
11678 }
11679 
11680 /**
11681  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11682  * @pf: board private structure
11683  * @queue_count: the requested queue count for rss.
11684  *
11685  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11686  * count which may be different from the requested queue count.
11687  * Note: expects to be called while under rtnl_lock()
11688  **/
11689 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11690 {
11691 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11692 	int new_rss_size;
11693 
11694 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11695 		return 0;
11696 
11697 	queue_count = min_t(int, queue_count, num_online_cpus());
11698 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11699 
11700 	if (queue_count != vsi->num_queue_pairs) {
11701 		u16 qcount;
11702 
11703 		vsi->req_queue_pairs = queue_count;
11704 		i40e_prep_for_reset(pf, true);
11705 
11706 		pf->alloc_rss_size = new_rss_size;
11707 
11708 		i40e_reset_and_rebuild(pf, true, true);
11709 
11710 		/* Discard the user configured hash keys and lut, if less
11711 		 * queues are enabled.
11712 		 */
11713 		if (queue_count < vsi->rss_size) {
11714 			i40e_clear_rss_config_user(vsi);
11715 			dev_dbg(&pf->pdev->dev,
11716 				"discard user configured hash keys and lut\n");
11717 		}
11718 
11719 		/* Reset vsi->rss_size, as number of enabled queues changed */
11720 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11721 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11722 
11723 		i40e_pf_config_rss(pf);
11724 	}
11725 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11726 		 vsi->req_queue_pairs, pf->rss_size_max);
11727 	return pf->alloc_rss_size;
11728 }
11729 
11730 /**
11731  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11732  * @pf: board private structure
11733  **/
11734 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11735 {
11736 	i40e_status status;
11737 	bool min_valid, max_valid;
11738 	u32 max_bw, min_bw;
11739 
11740 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11741 					   &min_valid, &max_valid);
11742 
11743 	if (!status) {
11744 		if (min_valid)
11745 			pf->min_bw = min_bw;
11746 		if (max_valid)
11747 			pf->max_bw = max_bw;
11748 	}
11749 
11750 	return status;
11751 }
11752 
11753 /**
11754  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11755  * @pf: board private structure
11756  **/
11757 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11758 {
11759 	struct i40e_aqc_configure_partition_bw_data bw_data;
11760 	i40e_status status;
11761 
11762 	/* Set the valid bit for this PF */
11763 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11764 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11765 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11766 
11767 	/* Set the new bandwidths */
11768 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11769 
11770 	return status;
11771 }
11772 
11773 /**
11774  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11775  * @pf: board private structure
11776  **/
11777 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11778 {
11779 	/* Commit temporary BW setting to permanent NVM image */
11780 	enum i40e_admin_queue_err last_aq_status;
11781 	i40e_status ret;
11782 	u16 nvm_word;
11783 
11784 	if (pf->hw.partition_id != 1) {
11785 		dev_info(&pf->pdev->dev,
11786 			 "Commit BW only works on partition 1! This is partition %d",
11787 			 pf->hw.partition_id);
11788 		ret = I40E_NOT_SUPPORTED;
11789 		goto bw_commit_out;
11790 	}
11791 
11792 	/* Acquire NVM for read access */
11793 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11794 	last_aq_status = pf->hw.aq.asq_last_status;
11795 	if (ret) {
11796 		dev_info(&pf->pdev->dev,
11797 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11798 			 i40e_stat_str(&pf->hw, ret),
11799 			 i40e_aq_str(&pf->hw, last_aq_status));
11800 		goto bw_commit_out;
11801 	}
11802 
11803 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11804 	ret = i40e_aq_read_nvm(&pf->hw,
11805 			       I40E_SR_NVM_CONTROL_WORD,
11806 			       0x10, sizeof(nvm_word), &nvm_word,
11807 			       false, NULL);
11808 	/* Save off last admin queue command status before releasing
11809 	 * the NVM
11810 	 */
11811 	last_aq_status = pf->hw.aq.asq_last_status;
11812 	i40e_release_nvm(&pf->hw);
11813 	if (ret) {
11814 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11815 			 i40e_stat_str(&pf->hw, ret),
11816 			 i40e_aq_str(&pf->hw, last_aq_status));
11817 		goto bw_commit_out;
11818 	}
11819 
11820 	/* Wait a bit for NVM release to complete */
11821 	msleep(50);
11822 
11823 	/* Acquire NVM for write access */
11824 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11825 	last_aq_status = pf->hw.aq.asq_last_status;
11826 	if (ret) {
11827 		dev_info(&pf->pdev->dev,
11828 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11829 			 i40e_stat_str(&pf->hw, ret),
11830 			 i40e_aq_str(&pf->hw, last_aq_status));
11831 		goto bw_commit_out;
11832 	}
11833 	/* Write it back out unchanged to initiate update NVM,
11834 	 * which will force a write of the shadow (alt) RAM to
11835 	 * the NVM - thus storing the bandwidth values permanently.
11836 	 */
11837 	ret = i40e_aq_update_nvm(&pf->hw,
11838 				 I40E_SR_NVM_CONTROL_WORD,
11839 				 0x10, sizeof(nvm_word),
11840 				 &nvm_word, true, 0, NULL);
11841 	/* Save off last admin queue command status before releasing
11842 	 * the NVM
11843 	 */
11844 	last_aq_status = pf->hw.aq.asq_last_status;
11845 	i40e_release_nvm(&pf->hw);
11846 	if (ret)
11847 		dev_info(&pf->pdev->dev,
11848 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11849 			 i40e_stat_str(&pf->hw, ret),
11850 			 i40e_aq_str(&pf->hw, last_aq_status));
11851 bw_commit_out:
11852 
11853 	return ret;
11854 }
11855 
11856 /**
11857  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11858  * @pf: board private structure to initialize
11859  *
11860  * i40e_sw_init initializes the Adapter private data structure.
11861  * Fields are initialized based on PCI device information and
11862  * OS network device settings (MTU size).
11863  **/
11864 static int i40e_sw_init(struct i40e_pf *pf)
11865 {
11866 	int err = 0;
11867 	int size;
11868 
11869 	/* Set default capability flags */
11870 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11871 		    I40E_FLAG_MSI_ENABLED     |
11872 		    I40E_FLAG_MSIX_ENABLED;
11873 
11874 	/* Set default ITR */
11875 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11876 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11877 
11878 	/* Depending on PF configurations, it is possible that the RSS
11879 	 * maximum might end up larger than the available queues
11880 	 */
11881 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11882 	pf->alloc_rss_size = 1;
11883 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11884 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11885 				 pf->hw.func_caps.num_tx_qp);
11886 	if (pf->hw.func_caps.rss) {
11887 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11888 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11889 					   num_online_cpus());
11890 	}
11891 
11892 	/* MFP mode enabled */
11893 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11894 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11895 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11896 		if (i40e_get_partition_bw_setting(pf)) {
11897 			dev_warn(&pf->pdev->dev,
11898 				 "Could not get partition bw settings\n");
11899 		} else {
11900 			dev_info(&pf->pdev->dev,
11901 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11902 				 pf->min_bw, pf->max_bw);
11903 
11904 			/* nudge the Tx scheduler */
11905 			i40e_set_partition_bw_setting(pf);
11906 		}
11907 	}
11908 
11909 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11910 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11911 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11912 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11913 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11914 		    pf->hw.num_partitions > 1)
11915 			dev_info(&pf->pdev->dev,
11916 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11917 		else
11918 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11919 		pf->fdir_pf_filter_count =
11920 				 pf->hw.func_caps.fd_filters_guaranteed;
11921 		pf->hw.fdir_shared_filter_count =
11922 				 pf->hw.func_caps.fd_filters_best_effort;
11923 	}
11924 
11925 	if (pf->hw.mac.type == I40E_MAC_X722) {
11926 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11927 				    I40E_HW_128_QP_RSS_CAPABLE |
11928 				    I40E_HW_ATR_EVICT_CAPABLE |
11929 				    I40E_HW_WB_ON_ITR_CAPABLE |
11930 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11931 				    I40E_HW_NO_PCI_LINK_CHECK |
11932 				    I40E_HW_USE_SET_LLDP_MIB |
11933 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11934 				    I40E_HW_PTP_L4_CAPABLE |
11935 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11936 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11937 
11938 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11939 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11940 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11941 			dev_warn(&pf->pdev->dev,
11942 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11943 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11944 		}
11945 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11946 		   ((pf->hw.aq.api_maj_ver == 1) &&
11947 		    (pf->hw.aq.api_min_ver > 4))) {
11948 		/* Supported in FW API version higher than 1.4 */
11949 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11950 	}
11951 
11952 	/* Enable HW ATR eviction if possible */
11953 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11954 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11955 
11956 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11957 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11958 	    (pf->hw.aq.fw_maj_ver < 4))) {
11959 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11960 		/* No DCB support  for FW < v4.33 */
11961 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11962 	}
11963 
11964 	/* Disable FW LLDP if FW < v4.3 */
11965 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11966 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11967 	    (pf->hw.aq.fw_maj_ver < 4)))
11968 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11969 
11970 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11971 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11972 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11973 	    (pf->hw.aq.fw_maj_ver >= 5)))
11974 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11975 
11976 	/* Enable PTP L4 if FW > v6.0 */
11977 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11978 	    pf->hw.aq.fw_maj_ver >= 6)
11979 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11980 
11981 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11982 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11983 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11984 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11985 	}
11986 
11987 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11988 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11989 		/* IWARP needs one extra vector for CQP just like MISC.*/
11990 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11991 	}
11992 	/* Stopping FW LLDP engine is supported on XL710 and X722
11993 	 * starting from FW versions determined in i40e_init_adminq.
11994 	 * Stopping the FW LLDP engine is not supported on XL710
11995 	 * if NPAR is functioning so unset this hw flag in this case.
11996 	 */
11997 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11998 	    pf->hw.func_caps.npar_enable &&
11999 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12000 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12001 
12002 #ifdef CONFIG_PCI_IOV
12003 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12004 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12005 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12006 		pf->num_req_vfs = min_t(int,
12007 					pf->hw.func_caps.num_vfs,
12008 					I40E_MAX_VF_COUNT);
12009 	}
12010 #endif /* CONFIG_PCI_IOV */
12011 	pf->eeprom_version = 0xDEAD;
12012 	pf->lan_veb = I40E_NO_VEB;
12013 	pf->lan_vsi = I40E_NO_VSI;
12014 
12015 	/* By default FW has this off for performance reasons */
12016 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12017 
12018 	/* set up queue assignment tracking */
12019 	size = sizeof(struct i40e_lump_tracking)
12020 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12021 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12022 	if (!pf->qp_pile) {
12023 		err = -ENOMEM;
12024 		goto sw_init_done;
12025 	}
12026 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12027 	pf->qp_pile->search_hint = 0;
12028 
12029 	pf->tx_timeout_recovery_level = 1;
12030 
12031 	mutex_init(&pf->switch_mutex);
12032 
12033 sw_init_done:
12034 	return err;
12035 }
12036 
12037 /**
12038  * i40e_set_ntuple - set the ntuple feature flag and take action
12039  * @pf: board private structure to initialize
12040  * @features: the feature set that the stack is suggesting
12041  *
12042  * returns a bool to indicate if reset needs to happen
12043  **/
12044 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12045 {
12046 	bool need_reset = false;
12047 
12048 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12049 	 * the state changed, we need to reset.
12050 	 */
12051 	if (features & NETIF_F_NTUPLE) {
12052 		/* Enable filters and mark for reset */
12053 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12054 			need_reset = true;
12055 		/* enable FD_SB only if there is MSI-X vector and no cloud
12056 		 * filters exist
12057 		 */
12058 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12059 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12060 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12061 		}
12062 	} else {
12063 		/* turn off filters, mark for reset and clear SW filter list */
12064 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12065 			need_reset = true;
12066 			i40e_fdir_filter_exit(pf);
12067 		}
12068 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12069 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12070 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12071 
12072 		/* reset fd counters */
12073 		pf->fd_add_err = 0;
12074 		pf->fd_atr_cnt = 0;
12075 		/* if ATR was auto disabled it can be re-enabled. */
12076 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12077 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12078 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12079 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12080 	}
12081 	return need_reset;
12082 }
12083 
12084 /**
12085  * i40e_clear_rss_lut - clear the rx hash lookup table
12086  * @vsi: the VSI being configured
12087  **/
12088 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12089 {
12090 	struct i40e_pf *pf = vsi->back;
12091 	struct i40e_hw *hw = &pf->hw;
12092 	u16 vf_id = vsi->vf_id;
12093 	u8 i;
12094 
12095 	if (vsi->type == I40E_VSI_MAIN) {
12096 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12097 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12098 	} else if (vsi->type == I40E_VSI_SRIOV) {
12099 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12100 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12101 	} else {
12102 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12103 	}
12104 }
12105 
12106 /**
12107  * i40e_set_features - set the netdev feature flags
12108  * @netdev: ptr to the netdev being adjusted
12109  * @features: the feature set that the stack is suggesting
12110  * Note: expects to be called while under rtnl_lock()
12111  **/
12112 static int i40e_set_features(struct net_device *netdev,
12113 			     netdev_features_t features)
12114 {
12115 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12116 	struct i40e_vsi *vsi = np->vsi;
12117 	struct i40e_pf *pf = vsi->back;
12118 	bool need_reset;
12119 
12120 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12121 		i40e_pf_config_rss(pf);
12122 	else if (!(features & NETIF_F_RXHASH) &&
12123 		 netdev->features & NETIF_F_RXHASH)
12124 		i40e_clear_rss_lut(vsi);
12125 
12126 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12127 		i40e_vlan_stripping_enable(vsi);
12128 	else
12129 		i40e_vlan_stripping_disable(vsi);
12130 
12131 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12132 		dev_err(&pf->pdev->dev,
12133 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12134 		return -EINVAL;
12135 	}
12136 
12137 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12138 		i40e_del_all_macvlans(vsi);
12139 
12140 	need_reset = i40e_set_ntuple(pf, features);
12141 
12142 	if (need_reset)
12143 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12144 
12145 	return 0;
12146 }
12147 
12148 /**
12149  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
12150  * @pf: board private structure
12151  * @port: The UDP port to look up
12152  *
12153  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
12154  **/
12155 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
12156 {
12157 	u8 i;
12158 
12159 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
12160 		/* Do not report ports with pending deletions as
12161 		 * being available.
12162 		 */
12163 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
12164 			continue;
12165 		if (pf->udp_ports[i].port == port)
12166 			return i;
12167 	}
12168 
12169 	return i;
12170 }
12171 
12172 /**
12173  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
12174  * @netdev: This physical port's netdev
12175  * @ti: Tunnel endpoint information
12176  **/
12177 static void i40e_udp_tunnel_add(struct net_device *netdev,
12178 				struct udp_tunnel_info *ti)
12179 {
12180 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12181 	struct i40e_vsi *vsi = np->vsi;
12182 	struct i40e_pf *pf = vsi->back;
12183 	u16 port = ntohs(ti->port);
12184 	u8 next_idx;
12185 	u8 idx;
12186 
12187 	idx = i40e_get_udp_port_idx(pf, port);
12188 
12189 	/* Check if port already exists */
12190 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
12191 		netdev_info(netdev, "port %d already offloaded\n", port);
12192 		return;
12193 	}
12194 
12195 	/* Now check if there is space to add the new port */
12196 	next_idx = i40e_get_udp_port_idx(pf, 0);
12197 
12198 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
12199 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
12200 			    port);
12201 		return;
12202 	}
12203 
12204 	switch (ti->type) {
12205 	case UDP_TUNNEL_TYPE_VXLAN:
12206 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
12207 		break;
12208 	case UDP_TUNNEL_TYPE_GENEVE:
12209 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
12210 			return;
12211 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
12212 		break;
12213 	default:
12214 		return;
12215 	}
12216 
12217 	/* New port: add it and mark its index in the bitmap */
12218 	pf->udp_ports[next_idx].port = port;
12219 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
12220 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
12221 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
12222 }
12223 
12224 /**
12225  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
12226  * @netdev: This physical port's netdev
12227  * @ti: Tunnel endpoint information
12228  **/
12229 static void i40e_udp_tunnel_del(struct net_device *netdev,
12230 				struct udp_tunnel_info *ti)
12231 {
12232 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12233 	struct i40e_vsi *vsi = np->vsi;
12234 	struct i40e_pf *pf = vsi->back;
12235 	u16 port = ntohs(ti->port);
12236 	u8 idx;
12237 
12238 	idx = i40e_get_udp_port_idx(pf, port);
12239 
12240 	/* Check if port already exists */
12241 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
12242 		goto not_found;
12243 
12244 	switch (ti->type) {
12245 	case UDP_TUNNEL_TYPE_VXLAN:
12246 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
12247 			goto not_found;
12248 		break;
12249 	case UDP_TUNNEL_TYPE_GENEVE:
12250 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
12251 			goto not_found;
12252 		break;
12253 	default:
12254 		goto not_found;
12255 	}
12256 
12257 	/* if port exists, set it to 0 (mark for deletion)
12258 	 * and make it pending
12259 	 */
12260 	pf->udp_ports[idx].port = 0;
12261 
12262 	/* Toggle pending bit instead of setting it. This way if we are
12263 	 * deleting a port that has yet to be added we just clear the pending
12264 	 * bit and don't have to worry about it.
12265 	 */
12266 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
12267 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
12268 
12269 	return;
12270 not_found:
12271 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
12272 		    port);
12273 }
12274 
12275 static int i40e_get_phys_port_id(struct net_device *netdev,
12276 				 struct netdev_phys_item_id *ppid)
12277 {
12278 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12279 	struct i40e_pf *pf = np->vsi->back;
12280 	struct i40e_hw *hw = &pf->hw;
12281 
12282 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12283 		return -EOPNOTSUPP;
12284 
12285 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12286 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12287 
12288 	return 0;
12289 }
12290 
12291 /**
12292  * i40e_ndo_fdb_add - add an entry to the hardware database
12293  * @ndm: the input from the stack
12294  * @tb: pointer to array of nladdr (unused)
12295  * @dev: the net device pointer
12296  * @addr: the MAC address entry being added
12297  * @vid: VLAN ID
12298  * @flags: instructions from stack about fdb operation
12299  */
12300 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12301 			    struct net_device *dev,
12302 			    const unsigned char *addr, u16 vid,
12303 			    u16 flags,
12304 			    struct netlink_ext_ack *extack)
12305 {
12306 	struct i40e_netdev_priv *np = netdev_priv(dev);
12307 	struct i40e_pf *pf = np->vsi->back;
12308 	int err = 0;
12309 
12310 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12311 		return -EOPNOTSUPP;
12312 
12313 	if (vid) {
12314 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12315 		return -EINVAL;
12316 	}
12317 
12318 	/* Hardware does not support aging addresses so if a
12319 	 * ndm_state is given only allow permanent addresses
12320 	 */
12321 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12322 		netdev_info(dev, "FDB only supports static addresses\n");
12323 		return -EINVAL;
12324 	}
12325 
12326 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12327 		err = dev_uc_add_excl(dev, addr);
12328 	else if (is_multicast_ether_addr(addr))
12329 		err = dev_mc_add_excl(dev, addr);
12330 	else
12331 		err = -EINVAL;
12332 
12333 	/* Only return duplicate errors if NLM_F_EXCL is set */
12334 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12335 		err = 0;
12336 
12337 	return err;
12338 }
12339 
12340 /**
12341  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12342  * @dev: the netdev being configured
12343  * @nlh: RTNL message
12344  * @flags: bridge flags
12345  * @extack: netlink extended ack
12346  *
12347  * Inserts a new hardware bridge if not already created and
12348  * enables the bridging mode requested (VEB or VEPA). If the
12349  * hardware bridge has already been inserted and the request
12350  * is to change the mode then that requires a PF reset to
12351  * allow rebuild of the components with required hardware
12352  * bridge mode enabled.
12353  *
12354  * Note: expects to be called while under rtnl_lock()
12355  **/
12356 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12357 				   struct nlmsghdr *nlh,
12358 				   u16 flags,
12359 				   struct netlink_ext_ack *extack)
12360 {
12361 	struct i40e_netdev_priv *np = netdev_priv(dev);
12362 	struct i40e_vsi *vsi = np->vsi;
12363 	struct i40e_pf *pf = vsi->back;
12364 	struct i40e_veb *veb = NULL;
12365 	struct nlattr *attr, *br_spec;
12366 	int i, rem;
12367 
12368 	/* Only for PF VSI for now */
12369 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12370 		return -EOPNOTSUPP;
12371 
12372 	/* Find the HW bridge for PF VSI */
12373 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12374 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12375 			veb = pf->veb[i];
12376 	}
12377 
12378 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12379 
12380 	nla_for_each_nested(attr, br_spec, rem) {
12381 		__u16 mode;
12382 
12383 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12384 			continue;
12385 
12386 		mode = nla_get_u16(attr);
12387 		if ((mode != BRIDGE_MODE_VEPA) &&
12388 		    (mode != BRIDGE_MODE_VEB))
12389 			return -EINVAL;
12390 
12391 		/* Insert a new HW bridge */
12392 		if (!veb) {
12393 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12394 					     vsi->tc_config.enabled_tc);
12395 			if (veb) {
12396 				veb->bridge_mode = mode;
12397 				i40e_config_bridge_mode(veb);
12398 			} else {
12399 				/* No Bridge HW offload available */
12400 				return -ENOENT;
12401 			}
12402 			break;
12403 		} else if (mode != veb->bridge_mode) {
12404 			/* Existing HW bridge but different mode needs reset */
12405 			veb->bridge_mode = mode;
12406 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12407 			if (mode == BRIDGE_MODE_VEB)
12408 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12409 			else
12410 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12411 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12412 			break;
12413 		}
12414 	}
12415 
12416 	return 0;
12417 }
12418 
12419 /**
12420  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12421  * @skb: skb buff
12422  * @pid: process id
12423  * @seq: RTNL message seq #
12424  * @dev: the netdev being configured
12425  * @filter_mask: unused
12426  * @nlflags: netlink flags passed in
12427  *
12428  * Return the mode in which the hardware bridge is operating in
12429  * i.e VEB or VEPA.
12430  **/
12431 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12432 				   struct net_device *dev,
12433 				   u32 __always_unused filter_mask,
12434 				   int nlflags)
12435 {
12436 	struct i40e_netdev_priv *np = netdev_priv(dev);
12437 	struct i40e_vsi *vsi = np->vsi;
12438 	struct i40e_pf *pf = vsi->back;
12439 	struct i40e_veb *veb = NULL;
12440 	int i;
12441 
12442 	/* Only for PF VSI for now */
12443 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12444 		return -EOPNOTSUPP;
12445 
12446 	/* Find the HW bridge for the PF VSI */
12447 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12448 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12449 			veb = pf->veb[i];
12450 	}
12451 
12452 	if (!veb)
12453 		return 0;
12454 
12455 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12456 				       0, 0, nlflags, filter_mask, NULL);
12457 }
12458 
12459 /**
12460  * i40e_features_check - Validate encapsulated packet conforms to limits
12461  * @skb: skb buff
12462  * @dev: This physical port's netdev
12463  * @features: Offload features that the stack believes apply
12464  **/
12465 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12466 					     struct net_device *dev,
12467 					     netdev_features_t features)
12468 {
12469 	size_t len;
12470 
12471 	/* No point in doing any of this if neither checksum nor GSO are
12472 	 * being requested for this frame.  We can rule out both by just
12473 	 * checking for CHECKSUM_PARTIAL
12474 	 */
12475 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12476 		return features;
12477 
12478 	/* We cannot support GSO if the MSS is going to be less than
12479 	 * 64 bytes.  If it is then we need to drop support for GSO.
12480 	 */
12481 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12482 		features &= ~NETIF_F_GSO_MASK;
12483 
12484 	/* MACLEN can support at most 63 words */
12485 	len = skb_network_header(skb) - skb->data;
12486 	if (len & ~(63 * 2))
12487 		goto out_err;
12488 
12489 	/* IPLEN and EIPLEN can support at most 127 dwords */
12490 	len = skb_transport_header(skb) - skb_network_header(skb);
12491 	if (len & ~(127 * 4))
12492 		goto out_err;
12493 
12494 	if (skb->encapsulation) {
12495 		/* L4TUNLEN can support 127 words */
12496 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12497 		if (len & ~(127 * 2))
12498 			goto out_err;
12499 
12500 		/* IPLEN can support at most 127 dwords */
12501 		len = skb_inner_transport_header(skb) -
12502 		      skb_inner_network_header(skb);
12503 		if (len & ~(127 * 4))
12504 			goto out_err;
12505 	}
12506 
12507 	/* No need to validate L4LEN as TCP is the only protocol with a
12508 	 * a flexible value and we support all possible values supported
12509 	 * by TCP, which is at most 15 dwords
12510 	 */
12511 
12512 	return features;
12513 out_err:
12514 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12515 }
12516 
12517 /**
12518  * i40e_xdp_setup - add/remove an XDP program
12519  * @vsi: VSI to changed
12520  * @prog: XDP program
12521  **/
12522 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12523 			  struct bpf_prog *prog)
12524 {
12525 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12526 	struct i40e_pf *pf = vsi->back;
12527 	struct bpf_prog *old_prog;
12528 	bool need_reset;
12529 	int i;
12530 
12531 	/* Don't allow frames that span over multiple buffers */
12532 	if (frame_size > vsi->rx_buf_len)
12533 		return -EINVAL;
12534 
12535 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12536 		return 0;
12537 
12538 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12539 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12540 
12541 	if (need_reset)
12542 		i40e_prep_for_reset(pf, true);
12543 
12544 	old_prog = xchg(&vsi->xdp_prog, prog);
12545 
12546 	if (need_reset)
12547 		i40e_reset_and_rebuild(pf, true, true);
12548 
12549 	for (i = 0; i < vsi->num_queue_pairs; i++)
12550 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12551 
12552 	if (old_prog)
12553 		bpf_prog_put(old_prog);
12554 
12555 	/* Kick start the NAPI context if there is an AF_XDP socket open
12556 	 * on that queue id. This so that receiving will start.
12557 	 */
12558 	if (need_reset && prog)
12559 		for (i = 0; i < vsi->num_queue_pairs; i++)
12560 			if (vsi->xdp_rings[i]->xsk_umem)
12561 				(void)i40e_xsk_async_xmit(vsi->netdev, i);
12562 
12563 	return 0;
12564 }
12565 
12566 /**
12567  * i40e_enter_busy_conf - Enters busy config state
12568  * @vsi: vsi
12569  *
12570  * Returns 0 on success, <0 for failure.
12571  **/
12572 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12573 {
12574 	struct i40e_pf *pf = vsi->back;
12575 	int timeout = 50;
12576 
12577 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12578 		timeout--;
12579 		if (!timeout)
12580 			return -EBUSY;
12581 		usleep_range(1000, 2000);
12582 	}
12583 
12584 	return 0;
12585 }
12586 
12587 /**
12588  * i40e_exit_busy_conf - Exits busy config state
12589  * @vsi: vsi
12590  **/
12591 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12592 {
12593 	struct i40e_pf *pf = vsi->back;
12594 
12595 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12596 }
12597 
12598 /**
12599  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12600  * @vsi: vsi
12601  * @queue_pair: queue pair
12602  **/
12603 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12604 {
12605 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12606 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12607 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12608 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12609 	if (i40e_enabled_xdp_vsi(vsi)) {
12610 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12611 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12612 	}
12613 }
12614 
12615 /**
12616  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12617  * @vsi: vsi
12618  * @queue_pair: queue pair
12619  **/
12620 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12621 {
12622 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12623 	if (i40e_enabled_xdp_vsi(vsi)) {
12624 		/* Make sure that in-progress ndo_xdp_xmit calls are
12625 		 * completed.
12626 		 */
12627 		synchronize_rcu();
12628 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12629 	}
12630 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12631 }
12632 
12633 /**
12634  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12635  * @vsi: vsi
12636  * @queue_pair: queue pair
12637  * @enable: true for enable, false for disable
12638  **/
12639 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12640 					bool enable)
12641 {
12642 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12643 	struct i40e_q_vector *q_vector = rxr->q_vector;
12644 
12645 	if (!vsi->netdev)
12646 		return;
12647 
12648 	/* All rings in a qp belong to the same qvector. */
12649 	if (q_vector->rx.ring || q_vector->tx.ring) {
12650 		if (enable)
12651 			napi_enable(&q_vector->napi);
12652 		else
12653 			napi_disable(&q_vector->napi);
12654 	}
12655 }
12656 
12657 /**
12658  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12659  * @vsi: vsi
12660  * @queue_pair: queue pair
12661  * @enable: true for enable, false for disable
12662  *
12663  * Returns 0 on success, <0 on failure.
12664  **/
12665 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12666 					bool enable)
12667 {
12668 	struct i40e_pf *pf = vsi->back;
12669 	int pf_q, ret = 0;
12670 
12671 	pf_q = vsi->base_queue + queue_pair;
12672 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12673 				     false /*is xdp*/, enable);
12674 	if (ret) {
12675 		dev_info(&pf->pdev->dev,
12676 			 "VSI seid %d Tx ring %d %sable timeout\n",
12677 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12678 		return ret;
12679 	}
12680 
12681 	i40e_control_rx_q(pf, pf_q, enable);
12682 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12683 	if (ret) {
12684 		dev_info(&pf->pdev->dev,
12685 			 "VSI seid %d Rx ring %d %sable timeout\n",
12686 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12687 		return ret;
12688 	}
12689 
12690 	/* Due to HW errata, on Rx disable only, the register can
12691 	 * indicate done before it really is. Needs 50ms to be sure
12692 	 */
12693 	if (!enable)
12694 		mdelay(50);
12695 
12696 	if (!i40e_enabled_xdp_vsi(vsi))
12697 		return ret;
12698 
12699 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12700 				     pf_q + vsi->alloc_queue_pairs,
12701 				     true /*is xdp*/, enable);
12702 	if (ret) {
12703 		dev_info(&pf->pdev->dev,
12704 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12705 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12706 	}
12707 
12708 	return ret;
12709 }
12710 
12711 /**
12712  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12713  * @vsi: vsi
12714  * @queue_pair: queue_pair
12715  **/
12716 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12717 {
12718 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12719 	struct i40e_pf *pf = vsi->back;
12720 	struct i40e_hw *hw = &pf->hw;
12721 
12722 	/* All rings in a qp belong to the same qvector. */
12723 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12724 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12725 	else
12726 		i40e_irq_dynamic_enable_icr0(pf);
12727 
12728 	i40e_flush(hw);
12729 }
12730 
12731 /**
12732  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12733  * @vsi: vsi
12734  * @queue_pair: queue_pair
12735  **/
12736 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12737 {
12738 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12739 	struct i40e_pf *pf = vsi->back;
12740 	struct i40e_hw *hw = &pf->hw;
12741 
12742 	/* For simplicity, instead of removing the qp interrupt causes
12743 	 * from the interrupt linked list, we simply disable the interrupt, and
12744 	 * leave the list intact.
12745 	 *
12746 	 * All rings in a qp belong to the same qvector.
12747 	 */
12748 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12749 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12750 
12751 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12752 		i40e_flush(hw);
12753 		synchronize_irq(pf->msix_entries[intpf].vector);
12754 	} else {
12755 		/* Legacy and MSI mode - this stops all interrupt handling */
12756 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12757 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12758 		i40e_flush(hw);
12759 		synchronize_irq(pf->pdev->irq);
12760 	}
12761 }
12762 
12763 /**
12764  * i40e_queue_pair_disable - Disables a queue pair
12765  * @vsi: vsi
12766  * @queue_pair: queue pair
12767  *
12768  * Returns 0 on success, <0 on failure.
12769  **/
12770 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12771 {
12772 	int err;
12773 
12774 	err = i40e_enter_busy_conf(vsi);
12775 	if (err)
12776 		return err;
12777 
12778 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12779 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12780 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12781 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12782 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12783 
12784 	return err;
12785 }
12786 
12787 /**
12788  * i40e_queue_pair_enable - Enables a queue pair
12789  * @vsi: vsi
12790  * @queue_pair: queue pair
12791  *
12792  * Returns 0 on success, <0 on failure.
12793  **/
12794 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12795 {
12796 	int err;
12797 
12798 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12799 	if (err)
12800 		return err;
12801 
12802 	if (i40e_enabled_xdp_vsi(vsi)) {
12803 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12804 		if (err)
12805 			return err;
12806 	}
12807 
12808 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12809 	if (err)
12810 		return err;
12811 
12812 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12813 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12814 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12815 
12816 	i40e_exit_busy_conf(vsi);
12817 
12818 	return err;
12819 }
12820 
12821 /**
12822  * i40e_xdp - implements ndo_bpf for i40e
12823  * @dev: netdevice
12824  * @xdp: XDP command
12825  **/
12826 static int i40e_xdp(struct net_device *dev,
12827 		    struct netdev_bpf *xdp)
12828 {
12829 	struct i40e_netdev_priv *np = netdev_priv(dev);
12830 	struct i40e_vsi *vsi = np->vsi;
12831 
12832 	if (vsi->type != I40E_VSI_MAIN)
12833 		return -EINVAL;
12834 
12835 	switch (xdp->command) {
12836 	case XDP_SETUP_PROG:
12837 		return i40e_xdp_setup(vsi, xdp->prog);
12838 	case XDP_QUERY_PROG:
12839 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
12840 		return 0;
12841 	case XDP_SETUP_XSK_UMEM:
12842 		return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
12843 					   xdp->xsk.queue_id);
12844 	default:
12845 		return -EINVAL;
12846 	}
12847 }
12848 
12849 static const struct net_device_ops i40e_netdev_ops = {
12850 	.ndo_open		= i40e_open,
12851 	.ndo_stop		= i40e_close,
12852 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12853 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12854 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12855 	.ndo_validate_addr	= eth_validate_addr,
12856 	.ndo_set_mac_address	= i40e_set_mac,
12857 	.ndo_change_mtu		= i40e_change_mtu,
12858 	.ndo_do_ioctl		= i40e_ioctl,
12859 	.ndo_tx_timeout		= i40e_tx_timeout,
12860 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12861 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12862 #ifdef CONFIG_NET_POLL_CONTROLLER
12863 	.ndo_poll_controller	= i40e_netpoll,
12864 #endif
12865 	.ndo_setup_tc		= __i40e_setup_tc,
12866 	.ndo_set_features	= i40e_set_features,
12867 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12868 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12869 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12870 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12871 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12872 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12873 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12874 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
12875 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
12876 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12877 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12878 	.ndo_features_check	= i40e_features_check,
12879 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12880 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12881 	.ndo_bpf		= i40e_xdp,
12882 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12883 	.ndo_xsk_async_xmit	= i40e_xsk_async_xmit,
12884 	.ndo_dfwd_add_station	= i40e_fwd_add,
12885 	.ndo_dfwd_del_station	= i40e_fwd_del,
12886 };
12887 
12888 /**
12889  * i40e_config_netdev - Setup the netdev flags
12890  * @vsi: the VSI being configured
12891  *
12892  * Returns 0 on success, negative value on failure
12893  **/
12894 static int i40e_config_netdev(struct i40e_vsi *vsi)
12895 {
12896 	struct i40e_pf *pf = vsi->back;
12897 	struct i40e_hw *hw = &pf->hw;
12898 	struct i40e_netdev_priv *np;
12899 	struct net_device *netdev;
12900 	u8 broadcast[ETH_ALEN];
12901 	u8 mac_addr[ETH_ALEN];
12902 	int etherdev_size;
12903 	netdev_features_t hw_enc_features;
12904 	netdev_features_t hw_features;
12905 
12906 	etherdev_size = sizeof(struct i40e_netdev_priv);
12907 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12908 	if (!netdev)
12909 		return -ENOMEM;
12910 
12911 	vsi->netdev = netdev;
12912 	np = netdev_priv(netdev);
12913 	np->vsi = vsi;
12914 
12915 	hw_enc_features = NETIF_F_SG			|
12916 			  NETIF_F_IP_CSUM		|
12917 			  NETIF_F_IPV6_CSUM		|
12918 			  NETIF_F_HIGHDMA		|
12919 			  NETIF_F_SOFT_FEATURES		|
12920 			  NETIF_F_TSO			|
12921 			  NETIF_F_TSO_ECN		|
12922 			  NETIF_F_TSO6			|
12923 			  NETIF_F_GSO_GRE		|
12924 			  NETIF_F_GSO_GRE_CSUM		|
12925 			  NETIF_F_GSO_PARTIAL		|
12926 			  NETIF_F_GSO_IPXIP4		|
12927 			  NETIF_F_GSO_IPXIP6		|
12928 			  NETIF_F_GSO_UDP_TUNNEL	|
12929 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12930 			  NETIF_F_SCTP_CRC		|
12931 			  NETIF_F_RXHASH		|
12932 			  NETIF_F_RXCSUM		|
12933 			  0;
12934 
12935 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12936 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12937 
12938 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12939 
12940 	netdev->hw_enc_features |= hw_enc_features;
12941 
12942 	/* record features VLANs can make use of */
12943 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12944 
12945 	/* enable macvlan offloads */
12946 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
12947 
12948 	hw_features = hw_enc_features		|
12949 		      NETIF_F_HW_VLAN_CTAG_TX	|
12950 		      NETIF_F_HW_VLAN_CTAG_RX;
12951 
12952 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12953 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12954 
12955 	netdev->hw_features |= hw_features;
12956 
12957 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12958 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12959 
12960 	if (vsi->type == I40E_VSI_MAIN) {
12961 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12962 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12963 		/* The following steps are necessary for two reasons. First,
12964 		 * some older NVM configurations load a default MAC-VLAN
12965 		 * filter that will accept any tagged packet, and we want to
12966 		 * replace this with a normal filter. Additionally, it is
12967 		 * possible our MAC address was provided by the platform using
12968 		 * Open Firmware or similar.
12969 		 *
12970 		 * Thus, we need to remove the default filter and install one
12971 		 * specific to the MAC address.
12972 		 */
12973 		i40e_rm_default_mac_filter(vsi, mac_addr);
12974 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12975 		i40e_add_mac_filter(vsi, mac_addr);
12976 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12977 	} else {
12978 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12979 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12980 		 * the end, which is 4 bytes long, so force truncation of the
12981 		 * original name by IFNAMSIZ - 4
12982 		 */
12983 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12984 			 IFNAMSIZ - 4,
12985 			 pf->vsi[pf->lan_vsi]->netdev->name);
12986 		eth_random_addr(mac_addr);
12987 
12988 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12989 		i40e_add_mac_filter(vsi, mac_addr);
12990 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12991 	}
12992 
12993 	/* Add the broadcast filter so that we initially will receive
12994 	 * broadcast packets. Note that when a new VLAN is first added the
12995 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12996 	 * specific filters as part of transitioning into "vlan" operation.
12997 	 * When more VLANs are added, the driver will copy each existing MAC
12998 	 * filter and add it for the new VLAN.
12999 	 *
13000 	 * Broadcast filters are handled specially by
13001 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13002 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13003 	 * filter. The subtask will update the correct broadcast promiscuous
13004 	 * bits as VLANs become active or inactive.
13005 	 */
13006 	eth_broadcast_addr(broadcast);
13007 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13008 	i40e_add_mac_filter(vsi, broadcast);
13009 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13010 
13011 	ether_addr_copy(netdev->dev_addr, mac_addr);
13012 	ether_addr_copy(netdev->perm_addr, mac_addr);
13013 
13014 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13015 	netdev->neigh_priv_len = sizeof(u32) * 4;
13016 
13017 	netdev->priv_flags |= IFF_UNICAST_FLT;
13018 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13019 	/* Setup netdev TC information */
13020 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13021 
13022 	netdev->netdev_ops = &i40e_netdev_ops;
13023 	netdev->watchdog_timeo = 5 * HZ;
13024 	i40e_set_ethtool_ops(netdev);
13025 
13026 	/* MTU range: 68 - 9706 */
13027 	netdev->min_mtu = ETH_MIN_MTU;
13028 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13029 
13030 	return 0;
13031 }
13032 
13033 /**
13034  * i40e_vsi_delete - Delete a VSI from the switch
13035  * @vsi: the VSI being removed
13036  *
13037  * Returns 0 on success, negative value on failure
13038  **/
13039 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13040 {
13041 	/* remove default VSI is not allowed */
13042 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13043 		return;
13044 
13045 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13046 }
13047 
13048 /**
13049  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13050  * @vsi: the VSI being queried
13051  *
13052  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13053  **/
13054 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13055 {
13056 	struct i40e_veb *veb;
13057 	struct i40e_pf *pf = vsi->back;
13058 
13059 	/* Uplink is not a bridge so default to VEB */
13060 	if (vsi->veb_idx >= I40E_MAX_VEB)
13061 		return 1;
13062 
13063 	veb = pf->veb[vsi->veb_idx];
13064 	if (!veb) {
13065 		dev_info(&pf->pdev->dev,
13066 			 "There is no veb associated with the bridge\n");
13067 		return -ENOENT;
13068 	}
13069 
13070 	/* Uplink is a bridge in VEPA mode */
13071 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13072 		return 0;
13073 	} else {
13074 		/* Uplink is a bridge in VEB mode */
13075 		return 1;
13076 	}
13077 
13078 	/* VEPA is now default bridge, so return 0 */
13079 	return 0;
13080 }
13081 
13082 /**
13083  * i40e_add_vsi - Add a VSI to the switch
13084  * @vsi: the VSI being configured
13085  *
13086  * This initializes a VSI context depending on the VSI type to be added and
13087  * passes it down to the add_vsi aq command.
13088  **/
13089 static int i40e_add_vsi(struct i40e_vsi *vsi)
13090 {
13091 	int ret = -ENODEV;
13092 	struct i40e_pf *pf = vsi->back;
13093 	struct i40e_hw *hw = &pf->hw;
13094 	struct i40e_vsi_context ctxt;
13095 	struct i40e_mac_filter *f;
13096 	struct hlist_node *h;
13097 	int bkt;
13098 
13099 	u8 enabled_tc = 0x1; /* TC0 enabled */
13100 	int f_count = 0;
13101 
13102 	memset(&ctxt, 0, sizeof(ctxt));
13103 	switch (vsi->type) {
13104 	case I40E_VSI_MAIN:
13105 		/* The PF's main VSI is already setup as part of the
13106 		 * device initialization, so we'll not bother with
13107 		 * the add_vsi call, but we will retrieve the current
13108 		 * VSI context.
13109 		 */
13110 		ctxt.seid = pf->main_vsi_seid;
13111 		ctxt.pf_num = pf->hw.pf_id;
13112 		ctxt.vf_num = 0;
13113 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13114 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13115 		if (ret) {
13116 			dev_info(&pf->pdev->dev,
13117 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13118 				 i40e_stat_str(&pf->hw, ret),
13119 				 i40e_aq_str(&pf->hw,
13120 					     pf->hw.aq.asq_last_status));
13121 			return -ENOENT;
13122 		}
13123 		vsi->info = ctxt.info;
13124 		vsi->info.valid_sections = 0;
13125 
13126 		vsi->seid = ctxt.seid;
13127 		vsi->id = ctxt.vsi_number;
13128 
13129 		enabled_tc = i40e_pf_get_tc_map(pf);
13130 
13131 		/* Source pruning is enabled by default, so the flag is
13132 		 * negative logic - if it's set, we need to fiddle with
13133 		 * the VSI to disable source pruning.
13134 		 */
13135 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13136 			memset(&ctxt, 0, sizeof(ctxt));
13137 			ctxt.seid = pf->main_vsi_seid;
13138 			ctxt.pf_num = pf->hw.pf_id;
13139 			ctxt.vf_num = 0;
13140 			ctxt.info.valid_sections |=
13141 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13142 			ctxt.info.switch_id =
13143 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13144 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13145 			if (ret) {
13146 				dev_info(&pf->pdev->dev,
13147 					 "update vsi failed, err %s aq_err %s\n",
13148 					 i40e_stat_str(&pf->hw, ret),
13149 					 i40e_aq_str(&pf->hw,
13150 						     pf->hw.aq.asq_last_status));
13151 				ret = -ENOENT;
13152 				goto err;
13153 			}
13154 		}
13155 
13156 		/* MFP mode setup queue map and update VSI */
13157 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13158 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13159 			memset(&ctxt, 0, sizeof(ctxt));
13160 			ctxt.seid = pf->main_vsi_seid;
13161 			ctxt.pf_num = pf->hw.pf_id;
13162 			ctxt.vf_num = 0;
13163 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13164 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13165 			if (ret) {
13166 				dev_info(&pf->pdev->dev,
13167 					 "update vsi failed, err %s aq_err %s\n",
13168 					 i40e_stat_str(&pf->hw, ret),
13169 					 i40e_aq_str(&pf->hw,
13170 						    pf->hw.aq.asq_last_status));
13171 				ret = -ENOENT;
13172 				goto err;
13173 			}
13174 			/* update the local VSI info queue map */
13175 			i40e_vsi_update_queue_map(vsi, &ctxt);
13176 			vsi->info.valid_sections = 0;
13177 		} else {
13178 			/* Default/Main VSI is only enabled for TC0
13179 			 * reconfigure it to enable all TCs that are
13180 			 * available on the port in SFP mode.
13181 			 * For MFP case the iSCSI PF would use this
13182 			 * flow to enable LAN+iSCSI TC.
13183 			 */
13184 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13185 			if (ret) {
13186 				/* Single TC condition is not fatal,
13187 				 * message and continue
13188 				 */
13189 				dev_info(&pf->pdev->dev,
13190 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13191 					 enabled_tc,
13192 					 i40e_stat_str(&pf->hw, ret),
13193 					 i40e_aq_str(&pf->hw,
13194 						    pf->hw.aq.asq_last_status));
13195 			}
13196 		}
13197 		break;
13198 
13199 	case I40E_VSI_FDIR:
13200 		ctxt.pf_num = hw->pf_id;
13201 		ctxt.vf_num = 0;
13202 		ctxt.uplink_seid = vsi->uplink_seid;
13203 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13204 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13205 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13206 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13207 			ctxt.info.valid_sections |=
13208 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13209 			ctxt.info.switch_id =
13210 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13211 		}
13212 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13213 		break;
13214 
13215 	case I40E_VSI_VMDQ2:
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_VMDQ2;
13221 
13222 		/* This VSI is connected to VEB so the switch_id
13223 		 * should be set to zero by default.
13224 		 */
13225 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13226 			ctxt.info.valid_sections |=
13227 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13228 			ctxt.info.switch_id =
13229 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13230 		}
13231 
13232 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13233 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13234 		break;
13235 
13236 	case I40E_VSI_SRIOV:
13237 		ctxt.pf_num = hw->pf_id;
13238 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13239 		ctxt.uplink_seid = vsi->uplink_seid;
13240 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13241 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13242 
13243 		/* This VSI is connected to VEB so the switch_id
13244 		 * should be set to zero by default.
13245 		 */
13246 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13247 			ctxt.info.valid_sections |=
13248 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13249 			ctxt.info.switch_id =
13250 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13251 		}
13252 
13253 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13254 			ctxt.info.valid_sections |=
13255 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13256 			ctxt.info.queueing_opt_flags |=
13257 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13258 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13259 		}
13260 
13261 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13262 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13263 		if (pf->vf[vsi->vf_id].spoofchk) {
13264 			ctxt.info.valid_sections |=
13265 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13266 			ctxt.info.sec_flags |=
13267 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13268 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13269 		}
13270 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13271 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13272 		break;
13273 
13274 	case I40E_VSI_IWARP:
13275 		/* send down message to iWARP */
13276 		break;
13277 
13278 	default:
13279 		return -ENODEV;
13280 	}
13281 
13282 	if (vsi->type != I40E_VSI_MAIN) {
13283 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13284 		if (ret) {
13285 			dev_info(&vsi->back->pdev->dev,
13286 				 "add vsi failed, err %s aq_err %s\n",
13287 				 i40e_stat_str(&pf->hw, ret),
13288 				 i40e_aq_str(&pf->hw,
13289 					     pf->hw.aq.asq_last_status));
13290 			ret = -ENOENT;
13291 			goto err;
13292 		}
13293 		vsi->info = ctxt.info;
13294 		vsi->info.valid_sections = 0;
13295 		vsi->seid = ctxt.seid;
13296 		vsi->id = ctxt.vsi_number;
13297 	}
13298 
13299 	vsi->active_filters = 0;
13300 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13301 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13302 	/* If macvlan filters already exist, force them to get loaded */
13303 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13304 		f->state = I40E_FILTER_NEW;
13305 		f_count++;
13306 	}
13307 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13308 
13309 	if (f_count) {
13310 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13311 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13312 	}
13313 
13314 	/* Update VSI BW information */
13315 	ret = i40e_vsi_get_bw_info(vsi);
13316 	if (ret) {
13317 		dev_info(&pf->pdev->dev,
13318 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13319 			 i40e_stat_str(&pf->hw, ret),
13320 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13321 		/* VSI is already added so not tearing that up */
13322 		ret = 0;
13323 	}
13324 
13325 err:
13326 	return ret;
13327 }
13328 
13329 /**
13330  * i40e_vsi_release - Delete a VSI and free its resources
13331  * @vsi: the VSI being removed
13332  *
13333  * Returns 0 on success or < 0 on error
13334  **/
13335 int i40e_vsi_release(struct i40e_vsi *vsi)
13336 {
13337 	struct i40e_mac_filter *f;
13338 	struct hlist_node *h;
13339 	struct i40e_veb *veb = NULL;
13340 	struct i40e_pf *pf;
13341 	u16 uplink_seid;
13342 	int i, n, bkt;
13343 
13344 	pf = vsi->back;
13345 
13346 	/* release of a VEB-owner or last VSI is not allowed */
13347 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13348 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13349 			 vsi->seid, vsi->uplink_seid);
13350 		return -ENODEV;
13351 	}
13352 	if (vsi == pf->vsi[pf->lan_vsi] &&
13353 	    !test_bit(__I40E_DOWN, pf->state)) {
13354 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13355 		return -ENODEV;
13356 	}
13357 
13358 	uplink_seid = vsi->uplink_seid;
13359 	if (vsi->type != I40E_VSI_SRIOV) {
13360 		if (vsi->netdev_registered) {
13361 			vsi->netdev_registered = false;
13362 			if (vsi->netdev) {
13363 				/* results in a call to i40e_close() */
13364 				unregister_netdev(vsi->netdev);
13365 			}
13366 		} else {
13367 			i40e_vsi_close(vsi);
13368 		}
13369 		i40e_vsi_disable_irq(vsi);
13370 	}
13371 
13372 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13373 
13374 	/* clear the sync flag on all filters */
13375 	if (vsi->netdev) {
13376 		__dev_uc_unsync(vsi->netdev, NULL);
13377 		__dev_mc_unsync(vsi->netdev, NULL);
13378 	}
13379 
13380 	/* make sure any remaining filters are marked for deletion */
13381 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13382 		__i40e_del_filter(vsi, f);
13383 
13384 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13385 
13386 	i40e_sync_vsi_filters(vsi);
13387 
13388 	i40e_vsi_delete(vsi);
13389 	i40e_vsi_free_q_vectors(vsi);
13390 	if (vsi->netdev) {
13391 		free_netdev(vsi->netdev);
13392 		vsi->netdev = NULL;
13393 	}
13394 	i40e_vsi_clear_rings(vsi);
13395 	i40e_vsi_clear(vsi);
13396 
13397 	/* If this was the last thing on the VEB, except for the
13398 	 * controlling VSI, remove the VEB, which puts the controlling
13399 	 * VSI onto the next level down in the switch.
13400 	 *
13401 	 * Well, okay, there's one more exception here: don't remove
13402 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13403 	 * from up the network stack.
13404 	 */
13405 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13406 		if (pf->vsi[i] &&
13407 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13408 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13409 			n++;      /* count the VSIs */
13410 		}
13411 	}
13412 	for (i = 0; i < I40E_MAX_VEB; i++) {
13413 		if (!pf->veb[i])
13414 			continue;
13415 		if (pf->veb[i]->uplink_seid == uplink_seid)
13416 			n++;     /* count the VEBs */
13417 		if (pf->veb[i]->seid == uplink_seid)
13418 			veb = pf->veb[i];
13419 	}
13420 	if (n == 0 && veb && veb->uplink_seid != 0)
13421 		i40e_veb_release(veb);
13422 
13423 	return 0;
13424 }
13425 
13426 /**
13427  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13428  * @vsi: ptr to the VSI
13429  *
13430  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13431  * corresponding SW VSI structure and initializes num_queue_pairs for the
13432  * newly allocated VSI.
13433  *
13434  * Returns 0 on success or negative on failure
13435  **/
13436 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13437 {
13438 	int ret = -ENOENT;
13439 	struct i40e_pf *pf = vsi->back;
13440 
13441 	if (vsi->q_vectors[0]) {
13442 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13443 			 vsi->seid);
13444 		return -EEXIST;
13445 	}
13446 
13447 	if (vsi->base_vector) {
13448 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13449 			 vsi->seid, vsi->base_vector);
13450 		return -EEXIST;
13451 	}
13452 
13453 	ret = i40e_vsi_alloc_q_vectors(vsi);
13454 	if (ret) {
13455 		dev_info(&pf->pdev->dev,
13456 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13457 			 vsi->num_q_vectors, vsi->seid, ret);
13458 		vsi->num_q_vectors = 0;
13459 		goto vector_setup_out;
13460 	}
13461 
13462 	/* In Legacy mode, we do not have to get any other vector since we
13463 	 * piggyback on the misc/ICR0 for queue interrupts.
13464 	*/
13465 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13466 		return ret;
13467 	if (vsi->num_q_vectors)
13468 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13469 						 vsi->num_q_vectors, vsi->idx);
13470 	if (vsi->base_vector < 0) {
13471 		dev_info(&pf->pdev->dev,
13472 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13473 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13474 		i40e_vsi_free_q_vectors(vsi);
13475 		ret = -ENOENT;
13476 		goto vector_setup_out;
13477 	}
13478 
13479 vector_setup_out:
13480 	return ret;
13481 }
13482 
13483 /**
13484  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13485  * @vsi: pointer to the vsi.
13486  *
13487  * This re-allocates a vsi's queue resources.
13488  *
13489  * Returns pointer to the successfully allocated and configured VSI sw struct
13490  * on success, otherwise returns NULL on failure.
13491  **/
13492 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13493 {
13494 	u16 alloc_queue_pairs;
13495 	struct i40e_pf *pf;
13496 	u8 enabled_tc;
13497 	int ret;
13498 
13499 	if (!vsi)
13500 		return NULL;
13501 
13502 	pf = vsi->back;
13503 
13504 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13505 	i40e_vsi_clear_rings(vsi);
13506 
13507 	i40e_vsi_free_arrays(vsi, false);
13508 	i40e_set_num_rings_in_vsi(vsi);
13509 	ret = i40e_vsi_alloc_arrays(vsi, false);
13510 	if (ret)
13511 		goto err_vsi;
13512 
13513 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13514 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13515 
13516 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13517 	if (ret < 0) {
13518 		dev_info(&pf->pdev->dev,
13519 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13520 			 alloc_queue_pairs, vsi->seid, ret);
13521 		goto err_vsi;
13522 	}
13523 	vsi->base_queue = ret;
13524 
13525 	/* Update the FW view of the VSI. Force a reset of TC and queue
13526 	 * layout configurations.
13527 	 */
13528 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13529 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13530 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13531 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13532 	if (vsi->type == I40E_VSI_MAIN)
13533 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13534 
13535 	/* assign it some queues */
13536 	ret = i40e_alloc_rings(vsi);
13537 	if (ret)
13538 		goto err_rings;
13539 
13540 	/* map all of the rings to the q_vectors */
13541 	i40e_vsi_map_rings_to_vectors(vsi);
13542 	return vsi;
13543 
13544 err_rings:
13545 	i40e_vsi_free_q_vectors(vsi);
13546 	if (vsi->netdev_registered) {
13547 		vsi->netdev_registered = false;
13548 		unregister_netdev(vsi->netdev);
13549 		free_netdev(vsi->netdev);
13550 		vsi->netdev = NULL;
13551 	}
13552 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13553 err_vsi:
13554 	i40e_vsi_clear(vsi);
13555 	return NULL;
13556 }
13557 
13558 /**
13559  * i40e_vsi_setup - Set up a VSI by a given type
13560  * @pf: board private structure
13561  * @type: VSI type
13562  * @uplink_seid: the switch element to link to
13563  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13564  *
13565  * This allocates the sw VSI structure and its queue resources, then add a VSI
13566  * to the identified VEB.
13567  *
13568  * Returns pointer to the successfully allocated and configure VSI sw struct on
13569  * success, otherwise returns NULL on failure.
13570  **/
13571 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13572 				u16 uplink_seid, u32 param1)
13573 {
13574 	struct i40e_vsi *vsi = NULL;
13575 	struct i40e_veb *veb = NULL;
13576 	u16 alloc_queue_pairs;
13577 	int ret, i;
13578 	int v_idx;
13579 
13580 	/* The requested uplink_seid must be either
13581 	 *     - the PF's port seid
13582 	 *              no VEB is needed because this is the PF
13583 	 *              or this is a Flow Director special case VSI
13584 	 *     - seid of an existing VEB
13585 	 *     - seid of a VSI that owns an existing VEB
13586 	 *     - seid of a VSI that doesn't own a VEB
13587 	 *              a new VEB is created and the VSI becomes the owner
13588 	 *     - seid of the PF VSI, which is what creates the first VEB
13589 	 *              this is a special case of the previous
13590 	 *
13591 	 * Find which uplink_seid we were given and create a new VEB if needed
13592 	 */
13593 	for (i = 0; i < I40E_MAX_VEB; i++) {
13594 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13595 			veb = pf->veb[i];
13596 			break;
13597 		}
13598 	}
13599 
13600 	if (!veb && uplink_seid != pf->mac_seid) {
13601 
13602 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13603 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13604 				vsi = pf->vsi[i];
13605 				break;
13606 			}
13607 		}
13608 		if (!vsi) {
13609 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13610 				 uplink_seid);
13611 			return NULL;
13612 		}
13613 
13614 		if (vsi->uplink_seid == pf->mac_seid)
13615 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13616 					     vsi->tc_config.enabled_tc);
13617 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13618 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13619 					     vsi->tc_config.enabled_tc);
13620 		if (veb) {
13621 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13622 				dev_info(&vsi->back->pdev->dev,
13623 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13624 				return NULL;
13625 			}
13626 			/* We come up by default in VEPA mode if SRIOV is not
13627 			 * already enabled, in which case we can't force VEPA
13628 			 * mode.
13629 			 */
13630 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13631 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13632 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13633 			}
13634 			i40e_config_bridge_mode(veb);
13635 		}
13636 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13637 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13638 				veb = pf->veb[i];
13639 		}
13640 		if (!veb) {
13641 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13642 			return NULL;
13643 		}
13644 
13645 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13646 		uplink_seid = veb->seid;
13647 	}
13648 
13649 	/* get vsi sw struct */
13650 	v_idx = i40e_vsi_mem_alloc(pf, type);
13651 	if (v_idx < 0)
13652 		goto err_alloc;
13653 	vsi = pf->vsi[v_idx];
13654 	if (!vsi)
13655 		goto err_alloc;
13656 	vsi->type = type;
13657 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13658 
13659 	if (type == I40E_VSI_MAIN)
13660 		pf->lan_vsi = v_idx;
13661 	else if (type == I40E_VSI_SRIOV)
13662 		vsi->vf_id = param1;
13663 	/* assign it some queues */
13664 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13665 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13666 
13667 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13668 	if (ret < 0) {
13669 		dev_info(&pf->pdev->dev,
13670 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13671 			 alloc_queue_pairs, vsi->seid, ret);
13672 		goto err_vsi;
13673 	}
13674 	vsi->base_queue = ret;
13675 
13676 	/* get a VSI from the hardware */
13677 	vsi->uplink_seid = uplink_seid;
13678 	ret = i40e_add_vsi(vsi);
13679 	if (ret)
13680 		goto err_vsi;
13681 
13682 	switch (vsi->type) {
13683 	/* setup the netdev if needed */
13684 	case I40E_VSI_MAIN:
13685 	case I40E_VSI_VMDQ2:
13686 		ret = i40e_config_netdev(vsi);
13687 		if (ret)
13688 			goto err_netdev;
13689 		ret = register_netdev(vsi->netdev);
13690 		if (ret)
13691 			goto err_netdev;
13692 		vsi->netdev_registered = true;
13693 		netif_carrier_off(vsi->netdev);
13694 #ifdef CONFIG_I40E_DCB
13695 		/* Setup DCB netlink interface */
13696 		i40e_dcbnl_setup(vsi);
13697 #endif /* CONFIG_I40E_DCB */
13698 		/* fall through */
13699 
13700 	case I40E_VSI_FDIR:
13701 		/* set up vectors and rings if needed */
13702 		ret = i40e_vsi_setup_vectors(vsi);
13703 		if (ret)
13704 			goto err_msix;
13705 
13706 		ret = i40e_alloc_rings(vsi);
13707 		if (ret)
13708 			goto err_rings;
13709 
13710 		/* map all of the rings to the q_vectors */
13711 		i40e_vsi_map_rings_to_vectors(vsi);
13712 
13713 		i40e_vsi_reset_stats(vsi);
13714 		break;
13715 
13716 	default:
13717 		/* no netdev or rings for the other VSI types */
13718 		break;
13719 	}
13720 
13721 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13722 	    (vsi->type == I40E_VSI_VMDQ2)) {
13723 		ret = i40e_vsi_config_rss(vsi);
13724 	}
13725 	return vsi;
13726 
13727 err_rings:
13728 	i40e_vsi_free_q_vectors(vsi);
13729 err_msix:
13730 	if (vsi->netdev_registered) {
13731 		vsi->netdev_registered = false;
13732 		unregister_netdev(vsi->netdev);
13733 		free_netdev(vsi->netdev);
13734 		vsi->netdev = NULL;
13735 	}
13736 err_netdev:
13737 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13738 err_vsi:
13739 	i40e_vsi_clear(vsi);
13740 err_alloc:
13741 	return NULL;
13742 }
13743 
13744 /**
13745  * i40e_veb_get_bw_info - Query VEB BW information
13746  * @veb: the veb to query
13747  *
13748  * Query the Tx scheduler BW configuration data for given VEB
13749  **/
13750 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13751 {
13752 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13753 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13754 	struct i40e_pf *pf = veb->pf;
13755 	struct i40e_hw *hw = &pf->hw;
13756 	u32 tc_bw_max;
13757 	int ret = 0;
13758 	int i;
13759 
13760 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13761 						  &bw_data, NULL);
13762 	if (ret) {
13763 		dev_info(&pf->pdev->dev,
13764 			 "query veb bw config failed, err %s aq_err %s\n",
13765 			 i40e_stat_str(&pf->hw, ret),
13766 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13767 		goto out;
13768 	}
13769 
13770 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13771 						   &ets_data, NULL);
13772 	if (ret) {
13773 		dev_info(&pf->pdev->dev,
13774 			 "query veb bw ets config failed, err %s aq_err %s\n",
13775 			 i40e_stat_str(&pf->hw, ret),
13776 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13777 		goto out;
13778 	}
13779 
13780 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13781 	veb->bw_max_quanta = ets_data.tc_bw_max;
13782 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13783 	veb->enabled_tc = ets_data.tc_valid_bits;
13784 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13785 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13786 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13787 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13788 		veb->bw_tc_limit_credits[i] =
13789 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13790 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13791 	}
13792 
13793 out:
13794 	return ret;
13795 }
13796 
13797 /**
13798  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13799  * @pf: board private structure
13800  *
13801  * On error: returns error code (negative)
13802  * On success: returns vsi index in PF (positive)
13803  **/
13804 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13805 {
13806 	int ret = -ENOENT;
13807 	struct i40e_veb *veb;
13808 	int i;
13809 
13810 	/* Need to protect the allocation of switch elements at the PF level */
13811 	mutex_lock(&pf->switch_mutex);
13812 
13813 	/* VEB list may be fragmented if VEB creation/destruction has
13814 	 * been happening.  We can afford to do a quick scan to look
13815 	 * for any free slots in the list.
13816 	 *
13817 	 * find next empty veb slot, looping back around if necessary
13818 	 */
13819 	i = 0;
13820 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13821 		i++;
13822 	if (i >= I40E_MAX_VEB) {
13823 		ret = -ENOMEM;
13824 		goto err_alloc_veb;  /* out of VEB slots! */
13825 	}
13826 
13827 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13828 	if (!veb) {
13829 		ret = -ENOMEM;
13830 		goto err_alloc_veb;
13831 	}
13832 	veb->pf = pf;
13833 	veb->idx = i;
13834 	veb->enabled_tc = 1;
13835 
13836 	pf->veb[i] = veb;
13837 	ret = i;
13838 err_alloc_veb:
13839 	mutex_unlock(&pf->switch_mutex);
13840 	return ret;
13841 }
13842 
13843 /**
13844  * i40e_switch_branch_release - Delete a branch of the switch tree
13845  * @branch: where to start deleting
13846  *
13847  * This uses recursion to find the tips of the branch to be
13848  * removed, deleting until we get back to and can delete this VEB.
13849  **/
13850 static void i40e_switch_branch_release(struct i40e_veb *branch)
13851 {
13852 	struct i40e_pf *pf = branch->pf;
13853 	u16 branch_seid = branch->seid;
13854 	u16 veb_idx = branch->idx;
13855 	int i;
13856 
13857 	/* release any VEBs on this VEB - RECURSION */
13858 	for (i = 0; i < I40E_MAX_VEB; i++) {
13859 		if (!pf->veb[i])
13860 			continue;
13861 		if (pf->veb[i]->uplink_seid == branch->seid)
13862 			i40e_switch_branch_release(pf->veb[i]);
13863 	}
13864 
13865 	/* Release the VSIs on this VEB, but not the owner VSI.
13866 	 *
13867 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13868 	 *       the VEB itself, so don't use (*branch) after this loop.
13869 	 */
13870 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13871 		if (!pf->vsi[i])
13872 			continue;
13873 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13874 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13875 			i40e_vsi_release(pf->vsi[i]);
13876 		}
13877 	}
13878 
13879 	/* There's one corner case where the VEB might not have been
13880 	 * removed, so double check it here and remove it if needed.
13881 	 * This case happens if the veb was created from the debugfs
13882 	 * commands and no VSIs were added to it.
13883 	 */
13884 	if (pf->veb[veb_idx])
13885 		i40e_veb_release(pf->veb[veb_idx]);
13886 }
13887 
13888 /**
13889  * i40e_veb_clear - remove veb struct
13890  * @veb: the veb to remove
13891  **/
13892 static void i40e_veb_clear(struct i40e_veb *veb)
13893 {
13894 	if (!veb)
13895 		return;
13896 
13897 	if (veb->pf) {
13898 		struct i40e_pf *pf = veb->pf;
13899 
13900 		mutex_lock(&pf->switch_mutex);
13901 		if (pf->veb[veb->idx] == veb)
13902 			pf->veb[veb->idx] = NULL;
13903 		mutex_unlock(&pf->switch_mutex);
13904 	}
13905 
13906 	kfree(veb);
13907 }
13908 
13909 /**
13910  * i40e_veb_release - Delete a VEB and free its resources
13911  * @veb: the VEB being removed
13912  **/
13913 void i40e_veb_release(struct i40e_veb *veb)
13914 {
13915 	struct i40e_vsi *vsi = NULL;
13916 	struct i40e_pf *pf;
13917 	int i, n = 0;
13918 
13919 	pf = veb->pf;
13920 
13921 	/* find the remaining VSI and check for extras */
13922 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13923 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13924 			n++;
13925 			vsi = pf->vsi[i];
13926 		}
13927 	}
13928 	if (n != 1) {
13929 		dev_info(&pf->pdev->dev,
13930 			 "can't remove VEB %d with %d VSIs left\n",
13931 			 veb->seid, n);
13932 		return;
13933 	}
13934 
13935 	/* move the remaining VSI to uplink veb */
13936 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13937 	if (veb->uplink_seid) {
13938 		vsi->uplink_seid = veb->uplink_seid;
13939 		if (veb->uplink_seid == pf->mac_seid)
13940 			vsi->veb_idx = I40E_NO_VEB;
13941 		else
13942 			vsi->veb_idx = veb->veb_idx;
13943 	} else {
13944 		/* floating VEB */
13945 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13946 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13947 	}
13948 
13949 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13950 	i40e_veb_clear(veb);
13951 }
13952 
13953 /**
13954  * i40e_add_veb - create the VEB in the switch
13955  * @veb: the VEB to be instantiated
13956  * @vsi: the controlling VSI
13957  **/
13958 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13959 {
13960 	struct i40e_pf *pf = veb->pf;
13961 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13962 	int ret;
13963 
13964 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13965 			      veb->enabled_tc, false,
13966 			      &veb->seid, enable_stats, NULL);
13967 
13968 	/* get a VEB from the hardware */
13969 	if (ret) {
13970 		dev_info(&pf->pdev->dev,
13971 			 "couldn't add VEB, err %s aq_err %s\n",
13972 			 i40e_stat_str(&pf->hw, ret),
13973 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13974 		return -EPERM;
13975 	}
13976 
13977 	/* get statistics counter */
13978 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13979 					 &veb->stats_idx, NULL, NULL, NULL);
13980 	if (ret) {
13981 		dev_info(&pf->pdev->dev,
13982 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13983 			 i40e_stat_str(&pf->hw, ret),
13984 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13985 		return -EPERM;
13986 	}
13987 	ret = i40e_veb_get_bw_info(veb);
13988 	if (ret) {
13989 		dev_info(&pf->pdev->dev,
13990 			 "couldn't get VEB bw info, err %s aq_err %s\n",
13991 			 i40e_stat_str(&pf->hw, ret),
13992 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13993 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13994 		return -ENOENT;
13995 	}
13996 
13997 	vsi->uplink_seid = veb->seid;
13998 	vsi->veb_idx = veb->idx;
13999 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14000 
14001 	return 0;
14002 }
14003 
14004 /**
14005  * i40e_veb_setup - Set up a VEB
14006  * @pf: board private structure
14007  * @flags: VEB setup flags
14008  * @uplink_seid: the switch element to link to
14009  * @vsi_seid: the initial VSI seid
14010  * @enabled_tc: Enabled TC bit-map
14011  *
14012  * This allocates the sw VEB structure and links it into the switch
14013  * It is possible and legal for this to be a duplicate of an already
14014  * existing VEB.  It is also possible for both uplink and vsi seids
14015  * to be zero, in order to create a floating VEB.
14016  *
14017  * Returns pointer to the successfully allocated VEB sw struct on
14018  * success, otherwise returns NULL on failure.
14019  **/
14020 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14021 				u16 uplink_seid, u16 vsi_seid,
14022 				u8 enabled_tc)
14023 {
14024 	struct i40e_veb *veb, *uplink_veb = NULL;
14025 	int vsi_idx, veb_idx;
14026 	int ret;
14027 
14028 	/* if one seid is 0, the other must be 0 to create a floating relay */
14029 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14030 	    (uplink_seid + vsi_seid != 0)) {
14031 		dev_info(&pf->pdev->dev,
14032 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14033 			 uplink_seid, vsi_seid);
14034 		return NULL;
14035 	}
14036 
14037 	/* make sure there is such a vsi and uplink */
14038 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14039 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14040 			break;
14041 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14042 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14043 			 vsi_seid);
14044 		return NULL;
14045 	}
14046 
14047 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14048 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14049 			if (pf->veb[veb_idx] &&
14050 			    pf->veb[veb_idx]->seid == uplink_seid) {
14051 				uplink_veb = pf->veb[veb_idx];
14052 				break;
14053 			}
14054 		}
14055 		if (!uplink_veb) {
14056 			dev_info(&pf->pdev->dev,
14057 				 "uplink seid %d not found\n", uplink_seid);
14058 			return NULL;
14059 		}
14060 	}
14061 
14062 	/* get veb sw struct */
14063 	veb_idx = i40e_veb_mem_alloc(pf);
14064 	if (veb_idx < 0)
14065 		goto err_alloc;
14066 	veb = pf->veb[veb_idx];
14067 	veb->flags = flags;
14068 	veb->uplink_seid = uplink_seid;
14069 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14070 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14071 
14072 	/* create the VEB in the switch */
14073 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14074 	if (ret)
14075 		goto err_veb;
14076 	if (vsi_idx == pf->lan_vsi)
14077 		pf->lan_veb = veb->idx;
14078 
14079 	return veb;
14080 
14081 err_veb:
14082 	i40e_veb_clear(veb);
14083 err_alloc:
14084 	return NULL;
14085 }
14086 
14087 /**
14088  * i40e_setup_pf_switch_element - set PF vars based on switch type
14089  * @pf: board private structure
14090  * @ele: element we are building info from
14091  * @num_reported: total number of elements
14092  * @printconfig: should we print the contents
14093  *
14094  * helper function to assist in extracting a few useful SEID values.
14095  **/
14096 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14097 				struct i40e_aqc_switch_config_element_resp *ele,
14098 				u16 num_reported, bool printconfig)
14099 {
14100 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14101 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14102 	u8 element_type = ele->element_type;
14103 	u16 seid = le16_to_cpu(ele->seid);
14104 
14105 	if (printconfig)
14106 		dev_info(&pf->pdev->dev,
14107 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14108 			 element_type, seid, uplink_seid, downlink_seid);
14109 
14110 	switch (element_type) {
14111 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14112 		pf->mac_seid = seid;
14113 		break;
14114 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14115 		/* Main VEB? */
14116 		if (uplink_seid != pf->mac_seid)
14117 			break;
14118 		if (pf->lan_veb >= I40E_MAX_VEB) {
14119 			int v;
14120 
14121 			/* find existing or else empty VEB */
14122 			for (v = 0; v < I40E_MAX_VEB; v++) {
14123 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14124 					pf->lan_veb = v;
14125 					break;
14126 				}
14127 			}
14128 			if (pf->lan_veb >= I40E_MAX_VEB) {
14129 				v = i40e_veb_mem_alloc(pf);
14130 				if (v < 0)
14131 					break;
14132 				pf->lan_veb = v;
14133 			}
14134 		}
14135 		if (pf->lan_veb >= I40E_MAX_VEB)
14136 			break;
14137 
14138 		pf->veb[pf->lan_veb]->seid = seid;
14139 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14140 		pf->veb[pf->lan_veb]->pf = pf;
14141 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14142 		break;
14143 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14144 		if (num_reported != 1)
14145 			break;
14146 		/* This is immediately after a reset so we can assume this is
14147 		 * the PF's VSI
14148 		 */
14149 		pf->mac_seid = uplink_seid;
14150 		pf->pf_seid = downlink_seid;
14151 		pf->main_vsi_seid = seid;
14152 		if (printconfig)
14153 			dev_info(&pf->pdev->dev,
14154 				 "pf_seid=%d main_vsi_seid=%d\n",
14155 				 pf->pf_seid, pf->main_vsi_seid);
14156 		break;
14157 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14158 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14159 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14160 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14161 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14162 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14163 		/* ignore these for now */
14164 		break;
14165 	default:
14166 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14167 			 element_type, seid);
14168 		break;
14169 	}
14170 }
14171 
14172 /**
14173  * i40e_fetch_switch_configuration - Get switch config from firmware
14174  * @pf: board private structure
14175  * @printconfig: should we print the contents
14176  *
14177  * Get the current switch configuration from the device and
14178  * extract a few useful SEID values.
14179  **/
14180 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14181 {
14182 	struct i40e_aqc_get_switch_config_resp *sw_config;
14183 	u16 next_seid = 0;
14184 	int ret = 0;
14185 	u8 *aq_buf;
14186 	int i;
14187 
14188 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14189 	if (!aq_buf)
14190 		return -ENOMEM;
14191 
14192 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14193 	do {
14194 		u16 num_reported, num_total;
14195 
14196 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14197 						I40E_AQ_LARGE_BUF,
14198 						&next_seid, NULL);
14199 		if (ret) {
14200 			dev_info(&pf->pdev->dev,
14201 				 "get switch config failed err %s aq_err %s\n",
14202 				 i40e_stat_str(&pf->hw, ret),
14203 				 i40e_aq_str(&pf->hw,
14204 					     pf->hw.aq.asq_last_status));
14205 			kfree(aq_buf);
14206 			return -ENOENT;
14207 		}
14208 
14209 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14210 		num_total = le16_to_cpu(sw_config->header.num_total);
14211 
14212 		if (printconfig)
14213 			dev_info(&pf->pdev->dev,
14214 				 "header: %d reported %d total\n",
14215 				 num_reported, num_total);
14216 
14217 		for (i = 0; i < num_reported; i++) {
14218 			struct i40e_aqc_switch_config_element_resp *ele =
14219 				&sw_config->element[i];
14220 
14221 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14222 						     printconfig);
14223 		}
14224 	} while (next_seid != 0);
14225 
14226 	kfree(aq_buf);
14227 	return ret;
14228 }
14229 
14230 /**
14231  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14232  * @pf: board private structure
14233  * @reinit: if the Main VSI needs to re-initialized.
14234  *
14235  * Returns 0 on success, negative value on failure
14236  **/
14237 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
14238 {
14239 	u16 flags = 0;
14240 	int ret;
14241 
14242 	/* find out what's out there already */
14243 	ret = i40e_fetch_switch_configuration(pf, false);
14244 	if (ret) {
14245 		dev_info(&pf->pdev->dev,
14246 			 "couldn't fetch switch config, err %s aq_err %s\n",
14247 			 i40e_stat_str(&pf->hw, ret),
14248 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14249 		return ret;
14250 	}
14251 	i40e_pf_reset_stats(pf);
14252 
14253 	/* set the switch config bit for the whole device to
14254 	 * support limited promisc or true promisc
14255 	 * when user requests promisc. The default is limited
14256 	 * promisc.
14257 	*/
14258 
14259 	if ((pf->hw.pf_id == 0) &&
14260 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14261 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14262 		pf->last_sw_conf_flags = flags;
14263 	}
14264 
14265 	if (pf->hw.pf_id == 0) {
14266 		u16 valid_flags;
14267 
14268 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14269 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14270 						NULL);
14271 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14272 			dev_info(&pf->pdev->dev,
14273 				 "couldn't set switch config bits, err %s aq_err %s\n",
14274 				 i40e_stat_str(&pf->hw, ret),
14275 				 i40e_aq_str(&pf->hw,
14276 					     pf->hw.aq.asq_last_status));
14277 			/* not a fatal problem, just keep going */
14278 		}
14279 		pf->last_sw_conf_valid_flags = valid_flags;
14280 	}
14281 
14282 	/* first time setup */
14283 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14284 		struct i40e_vsi *vsi = NULL;
14285 		u16 uplink_seid;
14286 
14287 		/* Set up the PF VSI associated with the PF's main VSI
14288 		 * that is already in the HW switch
14289 		 */
14290 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14291 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14292 		else
14293 			uplink_seid = pf->mac_seid;
14294 		if (pf->lan_vsi == I40E_NO_VSI)
14295 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14296 		else if (reinit)
14297 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14298 		if (!vsi) {
14299 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14300 			i40e_cloud_filter_exit(pf);
14301 			i40e_fdir_teardown(pf);
14302 			return -EAGAIN;
14303 		}
14304 	} else {
14305 		/* force a reset of TC and queue layout configurations */
14306 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14307 
14308 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14309 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14310 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14311 	}
14312 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14313 
14314 	i40e_fdir_sb_setup(pf);
14315 
14316 	/* Setup static PF queue filter control settings */
14317 	ret = i40e_setup_pf_filter_control(pf);
14318 	if (ret) {
14319 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14320 			 ret);
14321 		/* Failure here should not stop continuing other steps */
14322 	}
14323 
14324 	/* enable RSS in the HW, even for only one queue, as the stack can use
14325 	 * the hash
14326 	 */
14327 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14328 		i40e_pf_config_rss(pf);
14329 
14330 	/* fill in link information and enable LSE reporting */
14331 	i40e_link_event(pf);
14332 
14333 	/* Initialize user-specific link properties */
14334 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14335 				  I40E_AQ_AN_COMPLETED) ? true : false);
14336 
14337 	i40e_ptp_init(pf);
14338 
14339 	/* repopulate tunnel port filters */
14340 	i40e_sync_udp_filters(pf);
14341 
14342 	return ret;
14343 }
14344 
14345 /**
14346  * i40e_determine_queue_usage - Work out queue distribution
14347  * @pf: board private structure
14348  **/
14349 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14350 {
14351 	int queues_left;
14352 	int q_max;
14353 
14354 	pf->num_lan_qps = 0;
14355 
14356 	/* Find the max queues to be put into basic use.  We'll always be
14357 	 * using TC0, whether or not DCB is running, and TC0 will get the
14358 	 * big RSS set.
14359 	 */
14360 	queues_left = pf->hw.func_caps.num_tx_qp;
14361 
14362 	if ((queues_left == 1) ||
14363 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14364 		/* one qp for PF, no queues for anything else */
14365 		queues_left = 0;
14366 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14367 
14368 		/* make sure all the fancies are disabled */
14369 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14370 			       I40E_FLAG_IWARP_ENABLED	|
14371 			       I40E_FLAG_FD_SB_ENABLED	|
14372 			       I40E_FLAG_FD_ATR_ENABLED	|
14373 			       I40E_FLAG_DCB_CAPABLE	|
14374 			       I40E_FLAG_DCB_ENABLED	|
14375 			       I40E_FLAG_SRIOV_ENABLED	|
14376 			       I40E_FLAG_VMDQ_ENABLED);
14377 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14378 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14379 				  I40E_FLAG_FD_SB_ENABLED |
14380 				  I40E_FLAG_FD_ATR_ENABLED |
14381 				  I40E_FLAG_DCB_CAPABLE))) {
14382 		/* one qp for PF */
14383 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14384 		queues_left -= pf->num_lan_qps;
14385 
14386 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14387 			       I40E_FLAG_IWARP_ENABLED	|
14388 			       I40E_FLAG_FD_SB_ENABLED	|
14389 			       I40E_FLAG_FD_ATR_ENABLED	|
14390 			       I40E_FLAG_DCB_ENABLED	|
14391 			       I40E_FLAG_VMDQ_ENABLED);
14392 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14393 	} else {
14394 		/* Not enough queues for all TCs */
14395 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14396 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14397 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14398 					I40E_FLAG_DCB_ENABLED);
14399 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14400 		}
14401 
14402 		/* limit lan qps to the smaller of qps, cpus or msix */
14403 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14404 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14405 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14406 		pf->num_lan_qps = q_max;
14407 
14408 		queues_left -= pf->num_lan_qps;
14409 	}
14410 
14411 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14412 		if (queues_left > 1) {
14413 			queues_left -= 1; /* save 1 queue for FD */
14414 		} else {
14415 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14416 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14417 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14418 		}
14419 	}
14420 
14421 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14422 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14423 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14424 					(queues_left / pf->num_vf_qps));
14425 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14426 	}
14427 
14428 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14429 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14430 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14431 					  (queues_left / pf->num_vmdq_qps));
14432 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14433 	}
14434 
14435 	pf->queues_left = queues_left;
14436 	dev_dbg(&pf->pdev->dev,
14437 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14438 		pf->hw.func_caps.num_tx_qp,
14439 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14440 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14441 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14442 		queues_left);
14443 }
14444 
14445 /**
14446  * i40e_setup_pf_filter_control - Setup PF static filter control
14447  * @pf: PF to be setup
14448  *
14449  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14450  * settings. If PE/FCoE are enabled then it will also set the per PF
14451  * based filter sizes required for them. It also enables Flow director,
14452  * ethertype and macvlan type filter settings for the pf.
14453  *
14454  * Returns 0 on success, negative on failure
14455  **/
14456 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14457 {
14458 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14459 
14460 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14461 
14462 	/* Flow Director is enabled */
14463 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14464 		settings->enable_fdir = true;
14465 
14466 	/* Ethtype and MACVLAN filters enabled for PF */
14467 	settings->enable_ethtype = true;
14468 	settings->enable_macvlan = true;
14469 
14470 	if (i40e_set_filter_control(&pf->hw, settings))
14471 		return -ENOENT;
14472 
14473 	return 0;
14474 }
14475 
14476 #define INFO_STRING_LEN 255
14477 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14478 static void i40e_print_features(struct i40e_pf *pf)
14479 {
14480 	struct i40e_hw *hw = &pf->hw;
14481 	char *buf;
14482 	int i;
14483 
14484 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14485 	if (!buf)
14486 		return;
14487 
14488 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14489 #ifdef CONFIG_PCI_IOV
14490 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14491 #endif
14492 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14493 		      pf->hw.func_caps.num_vsis,
14494 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14495 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14496 		i += snprintf(&buf[i], REMAIN(i), " RSS");
14497 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14498 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
14499 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14500 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
14501 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
14502 	}
14503 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14504 		i += snprintf(&buf[i], REMAIN(i), " DCB");
14505 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
14506 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
14507 	if (pf->flags & I40E_FLAG_PTP)
14508 		i += snprintf(&buf[i], REMAIN(i), " PTP");
14509 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14510 		i += snprintf(&buf[i], REMAIN(i), " VEB");
14511 	else
14512 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
14513 
14514 	dev_info(&pf->pdev->dev, "%s\n", buf);
14515 	kfree(buf);
14516 	WARN_ON(i > INFO_STRING_LEN);
14517 }
14518 
14519 /**
14520  * i40e_get_platform_mac_addr - get platform-specific MAC address
14521  * @pdev: PCI device information struct
14522  * @pf: board private structure
14523  *
14524  * Look up the MAC address for the device. First we'll try
14525  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14526  * specific fallback. Otherwise, we'll default to the stored value in
14527  * firmware.
14528  **/
14529 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14530 {
14531 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14532 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14533 }
14534 
14535 /**
14536  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14537  * @fec_cfg: FEC option to set in flags
14538  * @flags: ptr to flags in which we set FEC option
14539  **/
14540 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14541 {
14542 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14543 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14544 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14545 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14546 		*flags |= I40E_FLAG_RS_FEC;
14547 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14548 	}
14549 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14550 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14551 		*flags |= I40E_FLAG_BASE_R_FEC;
14552 		*flags &= ~I40E_FLAG_RS_FEC;
14553 	}
14554 	if (fec_cfg == 0)
14555 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14556 }
14557 
14558 /**
14559  * i40e_check_recovery_mode - check if we are running transition firmware
14560  * @pf: board private structure
14561  *
14562  * Check registers indicating the firmware runs in recovery mode. Sets the
14563  * appropriate driver state.
14564  *
14565  * Returns true if the recovery mode was detected, false otherwise
14566  **/
14567 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14568 {
14569 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14570 
14571 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14572 		dev_notice(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14573 		dev_notice(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14574 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14575 
14576 		return true;
14577 	}
14578 	if (test_and_clear_bit(__I40E_RECOVERY_MODE, pf->state))
14579 		dev_info(&pf->pdev->dev, "Reinitializing in normal mode with full functionality.\n");
14580 
14581 	return false;
14582 }
14583 
14584 /**
14585  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14586  * @pf: board private structure
14587  * @hw: ptr to the hardware info
14588  *
14589  * This function does a minimal setup of all subsystems needed for running
14590  * recovery mode.
14591  *
14592  * Returns 0 on success, negative on failure
14593  **/
14594 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14595 {
14596 	struct i40e_vsi *vsi;
14597 	int err;
14598 	int v_idx;
14599 
14600 	pci_save_state(pf->pdev);
14601 
14602 	/* set up periodic task facility */
14603 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14604 	pf->service_timer_period = HZ;
14605 
14606 	INIT_WORK(&pf->service_task, i40e_service_task);
14607 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14608 
14609 	err = i40e_init_interrupt_scheme(pf);
14610 	if (err)
14611 		goto err_switch_setup;
14612 
14613 	/* The number of VSIs reported by the FW is the minimum guaranteed
14614 	 * to us; HW supports far more and we share the remaining pool with
14615 	 * the other PFs. We allocate space for more than the guarantee with
14616 	 * the understanding that we might not get them all later.
14617 	 */
14618 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14619 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14620 	else
14621 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14622 
14623 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14624 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14625 			  GFP_KERNEL);
14626 	if (!pf->vsi) {
14627 		err = -ENOMEM;
14628 		goto err_switch_setup;
14629 	}
14630 
14631 	/* We allocate one VSI which is needed as absolute minimum
14632 	 * in order to register the netdev
14633 	 */
14634 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14635 	if (v_idx < 0)
14636 		goto err_switch_setup;
14637 	pf->lan_vsi = v_idx;
14638 	vsi = pf->vsi[v_idx];
14639 	if (!vsi)
14640 		goto err_switch_setup;
14641 	vsi->alloc_queue_pairs = 1;
14642 	err = i40e_config_netdev(vsi);
14643 	if (err)
14644 		goto err_switch_setup;
14645 	err = register_netdev(vsi->netdev);
14646 	if (err)
14647 		goto err_switch_setup;
14648 	vsi->netdev_registered = true;
14649 	i40e_dbg_pf_init(pf);
14650 
14651 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14652 	if (err)
14653 		goto err_switch_setup;
14654 
14655 	/* tell the firmware that we're starting */
14656 	i40e_send_version(pf);
14657 
14658 	/* since everything's happy, start the service_task timer */
14659 	mod_timer(&pf->service_timer,
14660 		  round_jiffies(jiffies + pf->service_timer_period));
14661 
14662 	return 0;
14663 
14664 err_switch_setup:
14665 	i40e_reset_interrupt_capability(pf);
14666 	del_timer_sync(&pf->service_timer);
14667 	i40e_shutdown_adminq(hw);
14668 	iounmap(hw->hw_addr);
14669 	pci_disable_pcie_error_reporting(pf->pdev);
14670 	pci_release_mem_regions(pf->pdev);
14671 	pci_disable_device(pf->pdev);
14672 	kfree(pf);
14673 
14674 	return err;
14675 }
14676 
14677 /**
14678  * i40e_probe - Device initialization routine
14679  * @pdev: PCI device information struct
14680  * @ent: entry in i40e_pci_tbl
14681  *
14682  * i40e_probe initializes a PF identified by a pci_dev structure.
14683  * The OS initialization, configuring of the PF private structure,
14684  * and a hardware reset occur.
14685  *
14686  * Returns 0 on success, negative on failure
14687  **/
14688 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14689 {
14690 	struct i40e_aq_get_phy_abilities_resp abilities;
14691 	struct i40e_pf *pf;
14692 	struct i40e_hw *hw;
14693 	static u16 pfs_found;
14694 	u16 wol_nvm_bits;
14695 	u16 link_status;
14696 	int err;
14697 	u32 val;
14698 	u32 i;
14699 	u8 set_fc_aq_fail;
14700 
14701 	err = pci_enable_device_mem(pdev);
14702 	if (err)
14703 		return err;
14704 
14705 	/* set up for high or low dma */
14706 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14707 	if (err) {
14708 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14709 		if (err) {
14710 			dev_err(&pdev->dev,
14711 				"DMA configuration failed: 0x%x\n", err);
14712 			goto err_dma;
14713 		}
14714 	}
14715 
14716 	/* set up pci connections */
14717 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14718 	if (err) {
14719 		dev_info(&pdev->dev,
14720 			 "pci_request_selected_regions failed %d\n", err);
14721 		goto err_pci_reg;
14722 	}
14723 
14724 	pci_enable_pcie_error_reporting(pdev);
14725 	pci_set_master(pdev);
14726 
14727 	/* Now that we have a PCI connection, we need to do the
14728 	 * low level device setup.  This is primarily setting up
14729 	 * the Admin Queue structures and then querying for the
14730 	 * device's current profile information.
14731 	 */
14732 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14733 	if (!pf) {
14734 		err = -ENOMEM;
14735 		goto err_pf_alloc;
14736 	}
14737 	pf->next_vsi = 0;
14738 	pf->pdev = pdev;
14739 	set_bit(__I40E_DOWN, pf->state);
14740 
14741 	hw = &pf->hw;
14742 	hw->back = pf;
14743 
14744 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14745 				I40E_MAX_CSR_SPACE);
14746 	/* We believe that the highest register to read is
14747 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14748 	 * is not less than that before mapping to prevent a
14749 	 * kernel panic.
14750 	 */
14751 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14752 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14753 			pf->ioremap_len);
14754 		err = -ENOMEM;
14755 		goto err_ioremap;
14756 	}
14757 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14758 	if (!hw->hw_addr) {
14759 		err = -EIO;
14760 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14761 			 (unsigned int)pci_resource_start(pdev, 0),
14762 			 pf->ioremap_len, err);
14763 		goto err_ioremap;
14764 	}
14765 	hw->vendor_id = pdev->vendor;
14766 	hw->device_id = pdev->device;
14767 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14768 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14769 	hw->subsystem_device_id = pdev->subsystem_device;
14770 	hw->bus.device = PCI_SLOT(pdev->devfn);
14771 	hw->bus.func = PCI_FUNC(pdev->devfn);
14772 	hw->bus.bus_id = pdev->bus->number;
14773 	pf->instance = pfs_found;
14774 
14775 	/* Select something other than the 802.1ad ethertype for the
14776 	 * switch to use internally and drop on ingress.
14777 	 */
14778 	hw->switch_tag = 0xffff;
14779 	hw->first_tag = ETH_P_8021AD;
14780 	hw->second_tag = ETH_P_8021Q;
14781 
14782 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14783 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14784 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14785 
14786 	/* set up the locks for the AQ, do this only once in probe
14787 	 * and destroy them only once in remove
14788 	 */
14789 	mutex_init(&hw->aq.asq_mutex);
14790 	mutex_init(&hw->aq.arq_mutex);
14791 
14792 	pf->msg_enable = netif_msg_init(debug,
14793 					NETIF_MSG_DRV |
14794 					NETIF_MSG_PROBE |
14795 					NETIF_MSG_LINK);
14796 	if (debug < -1)
14797 		pf->hw.debug_mask = debug;
14798 
14799 	/* do a special CORER for clearing PXE mode once at init */
14800 	if (hw->revision_id == 0 &&
14801 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14802 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14803 		i40e_flush(hw);
14804 		msleep(200);
14805 		pf->corer_count++;
14806 
14807 		i40e_clear_pxe_mode(hw);
14808 	}
14809 
14810 	/* Reset here to make sure all is clean and to define PF 'n' */
14811 	i40e_clear_hw(hw);
14812 	if (!i40e_check_recovery_mode(pf)) {
14813 		err = i40e_pf_reset(hw);
14814 		if (err) {
14815 			dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
14816 			goto err_pf_reset;
14817 		}
14818 		pf->pfr_count++;
14819 	}
14820 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14821 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14822 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14823 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14824 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14825 
14826 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14827 		 "%s-%s:misc",
14828 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14829 
14830 	err = i40e_init_shared_code(hw);
14831 	if (err) {
14832 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14833 			 err);
14834 		goto err_pf_reset;
14835 	}
14836 
14837 	/* set up a default setting for link flow control */
14838 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14839 
14840 	err = i40e_init_adminq(hw);
14841 	if (err) {
14842 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14843 			dev_info(&pdev->dev,
14844 				 "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",
14845 				 hw->aq.api_maj_ver,
14846 				 hw->aq.api_min_ver,
14847 				 I40E_FW_API_VERSION_MAJOR,
14848 				 I40E_FW_MINOR_VERSION(hw));
14849 		else
14850 			dev_info(&pdev->dev,
14851 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14852 
14853 		goto err_pf_reset;
14854 	}
14855 	i40e_get_oem_version(hw);
14856 
14857 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14858 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14859 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14860 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14861 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
14862 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
14863 
14864 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14865 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14866 		dev_info(&pdev->dev,
14867 			 "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",
14868 			 hw->aq.api_maj_ver,
14869 			 hw->aq.api_min_ver,
14870 			 I40E_FW_API_VERSION_MAJOR,
14871 			 I40E_FW_MINOR_VERSION(hw));
14872 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14873 		dev_info(&pdev->dev,
14874 			 "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",
14875 			 hw->aq.api_maj_ver,
14876 			 hw->aq.api_min_ver,
14877 			 I40E_FW_API_VERSION_MAJOR,
14878 			 I40E_FW_MINOR_VERSION(hw));
14879 
14880 	i40e_verify_eeprom(pf);
14881 
14882 	/* Rev 0 hardware was never productized */
14883 	if (hw->revision_id < 1)
14884 		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");
14885 
14886 	i40e_clear_pxe_mode(hw);
14887 
14888 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14889 	if (err)
14890 		goto err_adminq_setup;
14891 
14892 	err = i40e_sw_init(pf);
14893 	if (err) {
14894 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14895 		goto err_sw_init;
14896 	}
14897 
14898 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14899 		return i40e_init_recovery_mode(pf, hw);
14900 
14901 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14902 				hw->func_caps.num_rx_qp, 0, 0);
14903 	if (err) {
14904 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14905 		goto err_init_lan_hmc;
14906 	}
14907 
14908 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14909 	if (err) {
14910 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14911 		err = -ENOENT;
14912 		goto err_configure_lan_hmc;
14913 	}
14914 
14915 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14916 	 * Ignore error return codes because if it was already disabled via
14917 	 * hardware settings this will fail
14918 	 */
14919 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14920 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14921 		i40e_aq_stop_lldp(hw, true, false, NULL);
14922 	}
14923 
14924 	/* allow a platform config to override the HW addr */
14925 	i40e_get_platform_mac_addr(pdev, pf);
14926 
14927 	if (!is_valid_ether_addr(hw->mac.addr)) {
14928 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14929 		err = -EIO;
14930 		goto err_mac_addr;
14931 	}
14932 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14933 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14934 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14935 	if (is_valid_ether_addr(hw->mac.port_addr))
14936 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14937 
14938 	pci_set_drvdata(pdev, pf);
14939 	pci_save_state(pdev);
14940 
14941 	dev_info(&pdev->dev,
14942 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
14943 			"FW LLDP is disabled\n" :
14944 			"FW LLDP is enabled\n");
14945 
14946 	/* Enable FW to write default DCB config on link-up */
14947 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14948 
14949 #ifdef CONFIG_I40E_DCB
14950 	err = i40e_init_pf_dcb(pf);
14951 	if (err) {
14952 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14953 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14954 		/* Continue without DCB enabled */
14955 	}
14956 #endif /* CONFIG_I40E_DCB */
14957 
14958 	/* set up periodic task facility */
14959 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14960 	pf->service_timer_period = HZ;
14961 
14962 	INIT_WORK(&pf->service_task, i40e_service_task);
14963 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14964 
14965 	/* NVM bit on means WoL disabled for the port */
14966 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14967 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14968 		pf->wol_en = false;
14969 	else
14970 		pf->wol_en = true;
14971 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14972 
14973 	/* set up the main switch operations */
14974 	i40e_determine_queue_usage(pf);
14975 	err = i40e_init_interrupt_scheme(pf);
14976 	if (err)
14977 		goto err_switch_setup;
14978 
14979 	/* The number of VSIs reported by the FW is the minimum guaranteed
14980 	 * to us; HW supports far more and we share the remaining pool with
14981 	 * the other PFs. We allocate space for more than the guarantee with
14982 	 * the understanding that we might not get them all later.
14983 	 */
14984 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14985 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14986 	else
14987 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14988 
14989 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
14990 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14991 			  GFP_KERNEL);
14992 	if (!pf->vsi) {
14993 		err = -ENOMEM;
14994 		goto err_switch_setup;
14995 	}
14996 
14997 #ifdef CONFIG_PCI_IOV
14998 	/* prep for VF support */
14999 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15000 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15001 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15002 		if (pci_num_vf(pdev))
15003 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15004 	}
15005 #endif
15006 	err = i40e_setup_pf_switch(pf, false);
15007 	if (err) {
15008 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15009 		goto err_vsis;
15010 	}
15011 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15012 
15013 	/* Make sure flow control is set according to current settings */
15014 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
15015 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
15016 		dev_dbg(&pf->pdev->dev,
15017 			"Set fc with err %s aq_err %s on get_phy_cap\n",
15018 			i40e_stat_str(hw, err),
15019 			i40e_aq_str(hw, hw->aq.asq_last_status));
15020 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
15021 		dev_dbg(&pf->pdev->dev,
15022 			"Set fc with err %s aq_err %s on set_phy_config\n",
15023 			i40e_stat_str(hw, err),
15024 			i40e_aq_str(hw, hw->aq.asq_last_status));
15025 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
15026 		dev_dbg(&pf->pdev->dev,
15027 			"Set fc with err %s aq_err %s on get_link_info\n",
15028 			i40e_stat_str(hw, err),
15029 			i40e_aq_str(hw, hw->aq.asq_last_status));
15030 
15031 	/* if FDIR VSI was set up, start it now */
15032 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15033 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15034 			i40e_vsi_open(pf->vsi[i]);
15035 			break;
15036 		}
15037 	}
15038 
15039 	/* The driver only wants link up/down and module qualification
15040 	 * reports from firmware.  Note the negative logic.
15041 	 */
15042 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15043 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15044 					 I40E_AQ_EVENT_MEDIA_NA |
15045 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15046 	if (err)
15047 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15048 			 i40e_stat_str(&pf->hw, err),
15049 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15050 
15051 	/* Reconfigure hardware for allowing smaller MSS in the case
15052 	 * of TSO, so that we avoid the MDD being fired and causing
15053 	 * a reset in the case of small MSS+TSO.
15054 	 */
15055 	val = rd32(hw, I40E_REG_MSS);
15056 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15057 		val &= ~I40E_REG_MSS_MIN_MASK;
15058 		val |= I40E_64BYTE_MSS;
15059 		wr32(hw, I40E_REG_MSS, val);
15060 	}
15061 
15062 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15063 		msleep(75);
15064 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15065 		if (err)
15066 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15067 				 i40e_stat_str(&pf->hw, err),
15068 				 i40e_aq_str(&pf->hw,
15069 					     pf->hw.aq.asq_last_status));
15070 	}
15071 	/* The main driver is (mostly) up and happy. We need to set this state
15072 	 * before setting up the misc vector or we get a race and the vector
15073 	 * ends up disabled forever.
15074 	 */
15075 	clear_bit(__I40E_DOWN, pf->state);
15076 
15077 	/* In case of MSIX we are going to setup the misc vector right here
15078 	 * to handle admin queue events etc. In case of legacy and MSI
15079 	 * the misc functionality and queue processing is combined in
15080 	 * the same vector and that gets setup at open.
15081 	 */
15082 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15083 		err = i40e_setup_misc_vector(pf);
15084 		if (err) {
15085 			dev_info(&pdev->dev,
15086 				 "setup of misc vector failed: %d\n", err);
15087 			goto err_vsis;
15088 		}
15089 	}
15090 
15091 #ifdef CONFIG_PCI_IOV
15092 	/* prep for VF support */
15093 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15094 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15095 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15096 		/* disable link interrupts for VFs */
15097 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15098 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15099 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15100 		i40e_flush(hw);
15101 
15102 		if (pci_num_vf(pdev)) {
15103 			dev_info(&pdev->dev,
15104 				 "Active VFs found, allocating resources.\n");
15105 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15106 			if (err)
15107 				dev_info(&pdev->dev,
15108 					 "Error %d allocating resources for existing VFs\n",
15109 					 err);
15110 		}
15111 	}
15112 #endif /* CONFIG_PCI_IOV */
15113 
15114 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15115 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15116 						      pf->num_iwarp_msix,
15117 						      I40E_IWARP_IRQ_PILE_ID);
15118 		if (pf->iwarp_base_vector < 0) {
15119 			dev_info(&pdev->dev,
15120 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15121 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15122 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15123 		}
15124 	}
15125 
15126 	i40e_dbg_pf_init(pf);
15127 
15128 	/* tell the firmware that we're starting */
15129 	i40e_send_version(pf);
15130 
15131 	/* since everything's happy, start the service_task timer */
15132 	mod_timer(&pf->service_timer,
15133 		  round_jiffies(jiffies + pf->service_timer_period));
15134 
15135 	/* add this PF to client device list and launch a client service task */
15136 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15137 		err = i40e_lan_add_device(pf);
15138 		if (err)
15139 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15140 				 err);
15141 	}
15142 
15143 #define PCI_SPEED_SIZE 8
15144 #define PCI_WIDTH_SIZE 8
15145 	/* Devices on the IOSF bus do not have this information
15146 	 * and will report PCI Gen 1 x 1 by default so don't bother
15147 	 * checking them.
15148 	 */
15149 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15150 		char speed[PCI_SPEED_SIZE] = "Unknown";
15151 		char width[PCI_WIDTH_SIZE] = "Unknown";
15152 
15153 		/* Get the negotiated link width and speed from PCI config
15154 		 * space
15155 		 */
15156 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15157 					  &link_status);
15158 
15159 		i40e_set_pci_config_data(hw, link_status);
15160 
15161 		switch (hw->bus.speed) {
15162 		case i40e_bus_speed_8000:
15163 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15164 		case i40e_bus_speed_5000:
15165 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15166 		case i40e_bus_speed_2500:
15167 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15168 		default:
15169 			break;
15170 		}
15171 		switch (hw->bus.width) {
15172 		case i40e_bus_width_pcie_x8:
15173 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15174 		case i40e_bus_width_pcie_x4:
15175 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15176 		case i40e_bus_width_pcie_x2:
15177 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15178 		case i40e_bus_width_pcie_x1:
15179 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15180 		default:
15181 			break;
15182 		}
15183 
15184 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15185 			 speed, width);
15186 
15187 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15188 		    hw->bus.speed < i40e_bus_speed_8000) {
15189 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15190 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15191 		}
15192 	}
15193 
15194 	/* get the requested speeds from the fw */
15195 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15196 	if (err)
15197 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15198 			i40e_stat_str(&pf->hw, err),
15199 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15200 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15201 
15202 	/* set the FEC config due to the board capabilities */
15203 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15204 
15205 	/* get the supported phy types from the fw */
15206 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15207 	if (err)
15208 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15209 			i40e_stat_str(&pf->hw, err),
15210 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15211 
15212 	/* Add a filter to drop all Flow control frames from any VSI from being
15213 	 * transmitted. By doing so we stop a malicious VF from sending out
15214 	 * PAUSE or PFC frames and potentially controlling traffic for other
15215 	 * PF/VF VSIs.
15216 	 * The FW can still send Flow control frames if enabled.
15217 	 */
15218 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15219 						       pf->main_vsi_seid);
15220 
15221 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15222 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15223 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15224 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15225 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15226 	/* print a string summarizing features */
15227 	i40e_print_features(pf);
15228 
15229 	return 0;
15230 
15231 	/* Unwind what we've done if something failed in the setup */
15232 err_vsis:
15233 	set_bit(__I40E_DOWN, pf->state);
15234 	i40e_clear_interrupt_scheme(pf);
15235 	kfree(pf->vsi);
15236 err_switch_setup:
15237 	i40e_reset_interrupt_capability(pf);
15238 	del_timer_sync(&pf->service_timer);
15239 err_mac_addr:
15240 err_configure_lan_hmc:
15241 	(void)i40e_shutdown_lan_hmc(hw);
15242 err_init_lan_hmc:
15243 	kfree(pf->qp_pile);
15244 err_sw_init:
15245 err_adminq_setup:
15246 err_pf_reset:
15247 	iounmap(hw->hw_addr);
15248 err_ioremap:
15249 	kfree(pf);
15250 err_pf_alloc:
15251 	pci_disable_pcie_error_reporting(pdev);
15252 	pci_release_mem_regions(pdev);
15253 err_pci_reg:
15254 err_dma:
15255 	pci_disable_device(pdev);
15256 	return err;
15257 }
15258 
15259 /**
15260  * i40e_remove - Device removal routine
15261  * @pdev: PCI device information struct
15262  *
15263  * i40e_remove is called by the PCI subsystem to alert the driver
15264  * that is should release a PCI device.  This could be caused by a
15265  * Hot-Plug event, or because the driver is going to be removed from
15266  * memory.
15267  **/
15268 static void i40e_remove(struct pci_dev *pdev)
15269 {
15270 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15271 	struct i40e_hw *hw = &pf->hw;
15272 	i40e_status ret_code;
15273 	int i;
15274 
15275 	i40e_dbg_pf_exit(pf);
15276 
15277 	i40e_ptp_stop(pf);
15278 
15279 	/* Disable RSS in hw */
15280 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15281 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15282 
15283 	/* no more scheduling of any task */
15284 	set_bit(__I40E_SUSPENDED, pf->state);
15285 	set_bit(__I40E_DOWN, pf->state);
15286 	if (pf->service_timer.function)
15287 		del_timer_sync(&pf->service_timer);
15288 	if (pf->service_task.func)
15289 		cancel_work_sync(&pf->service_task);
15290 
15291 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15292 		struct i40e_vsi *vsi = pf->vsi[0];
15293 
15294 		/* We know that we have allocated only one vsi for this PF,
15295 		 * it was just for registering netdevice, so the interface
15296 		 * could be visible in the 'ifconfig' output
15297 		 */
15298 		unregister_netdev(vsi->netdev);
15299 		free_netdev(vsi->netdev);
15300 
15301 		goto unmap;
15302 	}
15303 
15304 	/* Client close must be called explicitly here because the timer
15305 	 * has been stopped.
15306 	 */
15307 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15308 
15309 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15310 		i40e_free_vfs(pf);
15311 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15312 	}
15313 
15314 	i40e_fdir_teardown(pf);
15315 
15316 	/* If there is a switch structure or any orphans, remove them.
15317 	 * This will leave only the PF's VSI remaining.
15318 	 */
15319 	for (i = 0; i < I40E_MAX_VEB; i++) {
15320 		if (!pf->veb[i])
15321 			continue;
15322 
15323 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15324 		    pf->veb[i]->uplink_seid == 0)
15325 			i40e_switch_branch_release(pf->veb[i]);
15326 	}
15327 
15328 	/* Now we can shutdown the PF's VSI, just before we kill
15329 	 * adminq and hmc.
15330 	 */
15331 	if (pf->vsi[pf->lan_vsi])
15332 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15333 
15334 	i40e_cloud_filter_exit(pf);
15335 
15336 	/* remove attached clients */
15337 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15338 		ret_code = i40e_lan_del_device(pf);
15339 		if (ret_code)
15340 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15341 				 ret_code);
15342 	}
15343 
15344 	/* shutdown and destroy the HMC */
15345 	if (hw->hmc.hmc_obj) {
15346 		ret_code = i40e_shutdown_lan_hmc(hw);
15347 		if (ret_code)
15348 			dev_warn(&pdev->dev,
15349 				 "Failed to destroy the HMC resources: %d\n",
15350 				 ret_code);
15351 	}
15352 
15353 unmap:
15354 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15355 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15356 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15357 		free_irq(pf->pdev->irq, pf);
15358 
15359 	/* shutdown the adminq */
15360 	i40e_shutdown_adminq(hw);
15361 
15362 	/* destroy the locks only once, here */
15363 	mutex_destroy(&hw->aq.arq_mutex);
15364 	mutex_destroy(&hw->aq.asq_mutex);
15365 
15366 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15367 	rtnl_lock();
15368 	i40e_clear_interrupt_scheme(pf);
15369 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15370 		if (pf->vsi[i]) {
15371 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15372 				i40e_vsi_clear_rings(pf->vsi[i]);
15373 			i40e_vsi_clear(pf->vsi[i]);
15374 			pf->vsi[i] = NULL;
15375 		}
15376 	}
15377 	rtnl_unlock();
15378 
15379 	for (i = 0; i < I40E_MAX_VEB; i++) {
15380 		kfree(pf->veb[i]);
15381 		pf->veb[i] = NULL;
15382 	}
15383 
15384 	kfree(pf->qp_pile);
15385 	kfree(pf->vsi);
15386 
15387 	iounmap(hw->hw_addr);
15388 	kfree(pf);
15389 	pci_release_mem_regions(pdev);
15390 
15391 	pci_disable_pcie_error_reporting(pdev);
15392 	pci_disable_device(pdev);
15393 }
15394 
15395 /**
15396  * i40e_pci_error_detected - warning that something funky happened in PCI land
15397  * @pdev: PCI device information struct
15398  * @error: the type of PCI error
15399  *
15400  * Called to warn that something happened and the error handling steps
15401  * are in progress.  Allows the driver to quiesce things, be ready for
15402  * remediation.
15403  **/
15404 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15405 						enum pci_channel_state error)
15406 {
15407 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15408 
15409 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15410 
15411 	if (!pf) {
15412 		dev_info(&pdev->dev,
15413 			 "Cannot recover - error happened during device probe\n");
15414 		return PCI_ERS_RESULT_DISCONNECT;
15415 	}
15416 
15417 	/* shutdown all operations */
15418 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15419 		i40e_prep_for_reset(pf, false);
15420 
15421 	/* Request a slot reset */
15422 	return PCI_ERS_RESULT_NEED_RESET;
15423 }
15424 
15425 /**
15426  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15427  * @pdev: PCI device information struct
15428  *
15429  * Called to find if the driver can work with the device now that
15430  * the pci slot has been reset.  If a basic connection seems good
15431  * (registers are readable and have sane content) then return a
15432  * happy little PCI_ERS_RESULT_xxx.
15433  **/
15434 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15435 {
15436 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15437 	pci_ers_result_t result;
15438 	u32 reg;
15439 
15440 	dev_dbg(&pdev->dev, "%s\n", __func__);
15441 	if (pci_enable_device_mem(pdev)) {
15442 		dev_info(&pdev->dev,
15443 			 "Cannot re-enable PCI device after reset.\n");
15444 		result = PCI_ERS_RESULT_DISCONNECT;
15445 	} else {
15446 		pci_set_master(pdev);
15447 		pci_restore_state(pdev);
15448 		pci_save_state(pdev);
15449 		pci_wake_from_d3(pdev, false);
15450 
15451 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15452 		if (reg == 0)
15453 			result = PCI_ERS_RESULT_RECOVERED;
15454 		else
15455 			result = PCI_ERS_RESULT_DISCONNECT;
15456 	}
15457 
15458 	return result;
15459 }
15460 
15461 /**
15462  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15463  * @pdev: PCI device information struct
15464  */
15465 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15466 {
15467 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15468 
15469 	i40e_prep_for_reset(pf, false);
15470 }
15471 
15472 /**
15473  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15474  * @pdev: PCI device information struct
15475  */
15476 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15477 {
15478 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15479 
15480 	i40e_reset_and_rebuild(pf, false, false);
15481 }
15482 
15483 /**
15484  * i40e_pci_error_resume - restart operations after PCI error recovery
15485  * @pdev: PCI device information struct
15486  *
15487  * Called to allow the driver to bring things back up after PCI error
15488  * and/or reset recovery has finished.
15489  **/
15490 static void i40e_pci_error_resume(struct pci_dev *pdev)
15491 {
15492 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15493 
15494 	dev_dbg(&pdev->dev, "%s\n", __func__);
15495 	if (test_bit(__I40E_SUSPENDED, pf->state))
15496 		return;
15497 
15498 	i40e_handle_reset_warning(pf, false);
15499 }
15500 
15501 /**
15502  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15503  * using the mac_address_write admin q function
15504  * @pf: pointer to i40e_pf struct
15505  **/
15506 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15507 {
15508 	struct i40e_hw *hw = &pf->hw;
15509 	i40e_status ret;
15510 	u8 mac_addr[6];
15511 	u16 flags = 0;
15512 
15513 	/* Get current MAC address in case it's an LAA */
15514 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15515 		ether_addr_copy(mac_addr,
15516 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15517 	} else {
15518 		dev_err(&pf->pdev->dev,
15519 			"Failed to retrieve MAC address; using default\n");
15520 		ether_addr_copy(mac_addr, hw->mac.addr);
15521 	}
15522 
15523 	/* The FW expects the mac address write cmd to first be called with
15524 	 * one of these flags before calling it again with the multicast
15525 	 * enable flags.
15526 	 */
15527 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15528 
15529 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15530 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15531 
15532 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15533 	if (ret) {
15534 		dev_err(&pf->pdev->dev,
15535 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15536 		return;
15537 	}
15538 
15539 	flags = I40E_AQC_MC_MAG_EN
15540 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15541 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15542 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15543 	if (ret)
15544 		dev_err(&pf->pdev->dev,
15545 			"Failed to enable Multicast Magic Packet wake up\n");
15546 }
15547 
15548 /**
15549  * i40e_shutdown - PCI callback for shutting down
15550  * @pdev: PCI device information struct
15551  **/
15552 static void i40e_shutdown(struct pci_dev *pdev)
15553 {
15554 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15555 	struct i40e_hw *hw = &pf->hw;
15556 
15557 	set_bit(__I40E_SUSPENDED, pf->state);
15558 	set_bit(__I40E_DOWN, pf->state);
15559 
15560 	del_timer_sync(&pf->service_timer);
15561 	cancel_work_sync(&pf->service_task);
15562 	i40e_cloud_filter_exit(pf);
15563 	i40e_fdir_teardown(pf);
15564 
15565 	/* Client close must be called explicitly here because the timer
15566 	 * has been stopped.
15567 	 */
15568 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15569 
15570 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15571 		i40e_enable_mc_magic_wake(pf);
15572 
15573 	i40e_prep_for_reset(pf, false);
15574 
15575 	wr32(hw, I40E_PFPM_APM,
15576 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15577 	wr32(hw, I40E_PFPM_WUFC,
15578 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15579 
15580 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15581 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15582 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15583 		free_irq(pf->pdev->irq, pf);
15584 
15585 	/* Since we're going to destroy queues during the
15586 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15587 	 * whole section
15588 	 */
15589 	rtnl_lock();
15590 	i40e_clear_interrupt_scheme(pf);
15591 	rtnl_unlock();
15592 
15593 	if (system_state == SYSTEM_POWER_OFF) {
15594 		pci_wake_from_d3(pdev, pf->wol_en);
15595 		pci_set_power_state(pdev, PCI_D3hot);
15596 	}
15597 }
15598 
15599 /**
15600  * i40e_suspend - PM callback for moving to D3
15601  * @dev: generic device information structure
15602  **/
15603 static int __maybe_unused i40e_suspend(struct device *dev)
15604 {
15605 	struct pci_dev *pdev = to_pci_dev(dev);
15606 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15607 	struct i40e_hw *hw = &pf->hw;
15608 
15609 	/* If we're already suspended, then there is nothing to do */
15610 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15611 		return 0;
15612 
15613 	set_bit(__I40E_DOWN, pf->state);
15614 
15615 	/* Ensure service task will not be running */
15616 	del_timer_sync(&pf->service_timer);
15617 	cancel_work_sync(&pf->service_task);
15618 
15619 	/* Client close must be called explicitly here because the timer
15620 	 * has been stopped.
15621 	 */
15622 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15623 
15624 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15625 		i40e_enable_mc_magic_wake(pf);
15626 
15627 	/* Since we're going to destroy queues during the
15628 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15629 	 * whole section
15630 	 */
15631 	rtnl_lock();
15632 
15633 	i40e_prep_for_reset(pf, true);
15634 
15635 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15636 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15637 
15638 	/* Clear the interrupt scheme and release our IRQs so that the system
15639 	 * can safely hibernate even when there are a large number of CPUs.
15640 	 * Otherwise hibernation might fail when mapping all the vectors back
15641 	 * to CPU0.
15642 	 */
15643 	i40e_clear_interrupt_scheme(pf);
15644 
15645 	rtnl_unlock();
15646 
15647 	return 0;
15648 }
15649 
15650 /**
15651  * i40e_resume - PM callback for waking up from D3
15652  * @dev: generic device information structure
15653  **/
15654 static int __maybe_unused i40e_resume(struct device *dev)
15655 {
15656 	struct pci_dev *pdev = to_pci_dev(dev);
15657 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15658 	int err;
15659 
15660 	/* If we're not suspended, then there is nothing to do */
15661 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15662 		return 0;
15663 
15664 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15665 	 * since we're going to be restoring queues
15666 	 */
15667 	rtnl_lock();
15668 
15669 	/* We cleared the interrupt scheme when we suspended, so we need to
15670 	 * restore it now to resume device functionality.
15671 	 */
15672 	err = i40e_restore_interrupt_scheme(pf);
15673 	if (err) {
15674 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
15675 			err);
15676 	}
15677 
15678 	clear_bit(__I40E_DOWN, pf->state);
15679 	i40e_reset_and_rebuild(pf, false, true);
15680 
15681 	rtnl_unlock();
15682 
15683 	/* Clear suspended state last after everything is recovered */
15684 	clear_bit(__I40E_SUSPENDED, pf->state);
15685 
15686 	/* Restart the service task */
15687 	mod_timer(&pf->service_timer,
15688 		  round_jiffies(jiffies + pf->service_timer_period));
15689 
15690 	return 0;
15691 }
15692 
15693 static const struct pci_error_handlers i40e_err_handler = {
15694 	.error_detected = i40e_pci_error_detected,
15695 	.slot_reset = i40e_pci_error_slot_reset,
15696 	.reset_prepare = i40e_pci_error_reset_prepare,
15697 	.reset_done = i40e_pci_error_reset_done,
15698 	.resume = i40e_pci_error_resume,
15699 };
15700 
15701 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15702 
15703 static struct pci_driver i40e_driver = {
15704 	.name     = i40e_driver_name,
15705 	.id_table = i40e_pci_tbl,
15706 	.probe    = i40e_probe,
15707 	.remove   = i40e_remove,
15708 	.driver   = {
15709 		.pm = &i40e_pm_ops,
15710 	},
15711 	.shutdown = i40e_shutdown,
15712 	.err_handler = &i40e_err_handler,
15713 	.sriov_configure = i40e_pci_sriov_configure,
15714 };
15715 
15716 /**
15717  * i40e_init_module - Driver registration routine
15718  *
15719  * i40e_init_module is the first routine called when the driver is
15720  * loaded. All it does is register with the PCI subsystem.
15721  **/
15722 static int __init i40e_init_module(void)
15723 {
15724 	pr_info("%s: %s - version %s\n", i40e_driver_name,
15725 		i40e_driver_string, i40e_driver_version_str);
15726 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15727 
15728 	/* There is no need to throttle the number of active tasks because
15729 	 * each device limits its own task using a state bit for scheduling
15730 	 * the service task, and the device tasks do not interfere with each
15731 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15732 	 * since we need to be able to guarantee forward progress even under
15733 	 * memory pressure.
15734 	 */
15735 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15736 	if (!i40e_wq) {
15737 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15738 		return -ENOMEM;
15739 	}
15740 
15741 	i40e_dbg_init();
15742 	return pci_register_driver(&i40e_driver);
15743 }
15744 module_init(i40e_init_module);
15745 
15746 /**
15747  * i40e_exit_module - Driver exit cleanup routine
15748  *
15749  * i40e_exit_module is called just before the driver is removed
15750  * from memory.
15751  **/
15752 static void __exit i40e_exit_module(void)
15753 {
15754 	pci_unregister_driver(&i40e_driver);
15755 	destroy_workqueue(i40e_wq);
15756 	i40e_dbg_exit();
15757 }
15758 module_exit(i40e_exit_module);
15759