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 10
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 - 2014 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 void i40e_fdir_sb_setup(struct i40e_pf *pf);
50 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
51 static int i40e_get_capabilities(struct i40e_pf *pf,
52 				 enum i40e_admin_queue_opc list_type);
53 
54 
55 /* i40e_pci_tbl - PCI Device ID Table
56  *
57  * Last entry must be all 0s
58  *
59  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
60  *   Class, Class Mask, private data (not used) }
61  */
62 static const struct pci_device_id i40e_pci_tbl[] = {
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
82 	/* required last entry */
83 	{0, }
84 };
85 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
86 
87 #define I40E_MAX_VF_COUNT 128
88 static int debug = -1;
89 module_param(debug, uint, 0);
90 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
91 
92 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
93 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
94 MODULE_LICENSE("GPL v2");
95 MODULE_VERSION(DRV_VERSION);
96 
97 static struct workqueue_struct *i40e_wq;
98 
99 /**
100  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
101  * @hw:   pointer to the HW structure
102  * @mem:  ptr to mem struct to fill out
103  * @size: size of memory requested
104  * @alignment: what to align the allocation to
105  **/
106 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
107 			    u64 size, u32 alignment)
108 {
109 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
110 
111 	mem->size = ALIGN(size, alignment);
112 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
113 				     GFP_KERNEL);
114 	if (!mem->va)
115 		return -ENOMEM;
116 
117 	return 0;
118 }
119 
120 /**
121  * i40e_free_dma_mem_d - OS specific memory free for shared code
122  * @hw:   pointer to the HW structure
123  * @mem:  ptr to mem struct to free
124  **/
125 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
126 {
127 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
128 
129 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
130 	mem->va = NULL;
131 	mem->pa = 0;
132 	mem->size = 0;
133 
134 	return 0;
135 }
136 
137 /**
138  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
139  * @hw:   pointer to the HW structure
140  * @mem:  ptr to mem struct to fill out
141  * @size: size of memory requested
142  **/
143 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
144 			     u32 size)
145 {
146 	mem->size = size;
147 	mem->va = kzalloc(size, GFP_KERNEL);
148 
149 	if (!mem->va)
150 		return -ENOMEM;
151 
152 	return 0;
153 }
154 
155 /**
156  * i40e_free_virt_mem_d - OS specific memory free for shared code
157  * @hw:   pointer to the HW structure
158  * @mem:  ptr to mem struct to free
159  **/
160 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
161 {
162 	/* it's ok to kfree a NULL pointer */
163 	kfree(mem->va);
164 	mem->va = NULL;
165 	mem->size = 0;
166 
167 	return 0;
168 }
169 
170 /**
171  * i40e_get_lump - find a lump of free generic resource
172  * @pf: board private structure
173  * @pile: the pile of resource to search
174  * @needed: the number of items needed
175  * @id: an owner id to stick on the items assigned
176  *
177  * Returns the base item index of the lump, or negative for error
178  *
179  * The search_hint trick and lack of advanced fit-finding only work
180  * because we're highly likely to have all the same size lump requests.
181  * Linear search time and any fragmentation should be minimal.
182  **/
183 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
184 			 u16 needed, u16 id)
185 {
186 	int ret = -ENOMEM;
187 	int i, j;
188 
189 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
190 		dev_info(&pf->pdev->dev,
191 			 "param err: pile=%s needed=%d id=0x%04x\n",
192 			 pile ? "<valid>" : "<null>", needed, id);
193 		return -EINVAL;
194 	}
195 
196 	/* start the linear search with an imperfect hint */
197 	i = pile->search_hint;
198 	while (i < pile->num_entries) {
199 		/* skip already allocated entries */
200 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
201 			i++;
202 			continue;
203 		}
204 
205 		/* do we have enough in this lump? */
206 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
207 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
208 				break;
209 		}
210 
211 		if (j == needed) {
212 			/* there was enough, so assign it to the requestor */
213 			for (j = 0; j < needed; j++)
214 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
215 			ret = i;
216 			pile->search_hint = i + j;
217 			break;
218 		}
219 
220 		/* not enough, so skip over it and continue looking */
221 		i += j;
222 	}
223 
224 	return ret;
225 }
226 
227 /**
228  * i40e_put_lump - return a lump of generic resource
229  * @pile: the pile of resource to search
230  * @index: the base item index
231  * @id: the owner id of the items assigned
232  *
233  * Returns the count of items in the lump
234  **/
235 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
236 {
237 	int valid_id = (id | I40E_PILE_VALID_BIT);
238 	int count = 0;
239 	int i;
240 
241 	if (!pile || index >= pile->num_entries)
242 		return -EINVAL;
243 
244 	for (i = index;
245 	     i < pile->num_entries && pile->list[i] == valid_id;
246 	     i++) {
247 		pile->list[i] = 0;
248 		count++;
249 	}
250 
251 	if (count && index < pile->search_hint)
252 		pile->search_hint = index;
253 
254 	return count;
255 }
256 
257 /**
258  * i40e_find_vsi_from_id - searches for the vsi with the given id
259  * @pf: the pf structure to search for the vsi
260  * @id: id of the vsi it is searching for
261  **/
262 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
263 {
264 	int i;
265 
266 	for (i = 0; i < pf->num_alloc_vsi; i++)
267 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
268 			return pf->vsi[i];
269 
270 	return NULL;
271 }
272 
273 /**
274  * i40e_service_event_schedule - Schedule the service task to wake up
275  * @pf: board private structure
276  *
277  * If not already scheduled, this puts the task into the work queue
278  **/
279 void i40e_service_event_schedule(struct i40e_pf *pf)
280 {
281 	if (!test_bit(__I40E_DOWN, pf->state) &&
282 	    !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
283 		queue_work(i40e_wq, &pf->service_task);
284 }
285 
286 /**
287  * i40e_tx_timeout - Respond to a Tx Hang
288  * @netdev: network interface device structure
289  *
290  * If any port has noticed a Tx timeout, it is likely that the whole
291  * device is munged, not just the one netdev port, so go for the full
292  * reset.
293  **/
294 static void i40e_tx_timeout(struct net_device *netdev)
295 {
296 	struct i40e_netdev_priv *np = netdev_priv(netdev);
297 	struct i40e_vsi *vsi = np->vsi;
298 	struct i40e_pf *pf = vsi->back;
299 	struct i40e_ring *tx_ring = NULL;
300 	unsigned int i, hung_queue = 0;
301 	u32 head, val;
302 
303 	pf->tx_timeout_count++;
304 
305 	/* find the stopped queue the same way the stack does */
306 	for (i = 0; i < netdev->num_tx_queues; i++) {
307 		struct netdev_queue *q;
308 		unsigned long trans_start;
309 
310 		q = netdev_get_tx_queue(netdev, i);
311 		trans_start = q->trans_start;
312 		if (netif_xmit_stopped(q) &&
313 		    time_after(jiffies,
314 			       (trans_start + netdev->watchdog_timeo))) {
315 			hung_queue = i;
316 			break;
317 		}
318 	}
319 
320 	if (i == netdev->num_tx_queues) {
321 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
322 	} else {
323 		/* now that we have an index, find the tx_ring struct */
324 		for (i = 0; i < vsi->num_queue_pairs; i++) {
325 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
326 				if (hung_queue ==
327 				    vsi->tx_rings[i]->queue_index) {
328 					tx_ring = vsi->tx_rings[i];
329 					break;
330 				}
331 			}
332 		}
333 	}
334 
335 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
336 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
337 	else if (time_before(jiffies,
338 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
339 		return;   /* don't do any new action before the next timeout */
340 
341 	/* don't kick off another recovery if one is already pending */
342 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
343 		return;
344 
345 	if (tx_ring) {
346 		head = i40e_get_head(tx_ring);
347 		/* Read interrupt register */
348 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
349 			val = rd32(&pf->hw,
350 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
351 						tx_ring->vsi->base_vector - 1));
352 		else
353 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
354 
355 		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",
356 			    vsi->seid, hung_queue, tx_ring->next_to_clean,
357 			    head, tx_ring->next_to_use,
358 			    readl(tx_ring->tail), val);
359 	}
360 
361 	pf->tx_timeout_last_recovery = jiffies;
362 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
363 		    pf->tx_timeout_recovery_level, hung_queue);
364 
365 	switch (pf->tx_timeout_recovery_level) {
366 	case 1:
367 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
368 		break;
369 	case 2:
370 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
371 		break;
372 	case 3:
373 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
374 		break;
375 	default:
376 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
377 		break;
378 	}
379 
380 	i40e_service_event_schedule(pf);
381 	pf->tx_timeout_recovery_level++;
382 }
383 
384 /**
385  * i40e_get_vsi_stats_struct - Get System Network Statistics
386  * @vsi: the VSI we care about
387  *
388  * Returns the address of the device statistics structure.
389  * The statistics are actually updated from the service task.
390  **/
391 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
392 {
393 	return &vsi->net_stats;
394 }
395 
396 /**
397  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
398  * @ring: Tx ring to get statistics from
399  * @stats: statistics entry to be updated
400  **/
401 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
402 					    struct rtnl_link_stats64 *stats)
403 {
404 	u64 bytes, packets;
405 	unsigned int start;
406 
407 	do {
408 		start = u64_stats_fetch_begin_irq(&ring->syncp);
409 		packets = ring->stats.packets;
410 		bytes   = ring->stats.bytes;
411 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
412 
413 	stats->tx_packets += packets;
414 	stats->tx_bytes   += bytes;
415 }
416 
417 /**
418  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
419  * @netdev: network interface device structure
420  * @stats: data structure to store statistics
421  *
422  * Returns the address of the device statistics structure.
423  * The statistics are actually updated from the service task.
424  **/
425 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
426 				  struct rtnl_link_stats64 *stats)
427 {
428 	struct i40e_netdev_priv *np = netdev_priv(netdev);
429 	struct i40e_vsi *vsi = np->vsi;
430 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
431 	struct i40e_ring *ring;
432 	int i;
433 
434 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
435 		return;
436 
437 	if (!vsi->tx_rings)
438 		return;
439 
440 	rcu_read_lock();
441 	for (i = 0; i < vsi->num_queue_pairs; i++) {
442 		u64 bytes, packets;
443 		unsigned int start;
444 
445 		ring = READ_ONCE(vsi->tx_rings[i]);
446 		if (!ring)
447 			continue;
448 		i40e_get_netdev_stats_struct_tx(ring, stats);
449 
450 		if (i40e_enabled_xdp_vsi(vsi)) {
451 			ring++;
452 			i40e_get_netdev_stats_struct_tx(ring, stats);
453 		}
454 
455 		ring++;
456 		do {
457 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
458 			packets = ring->stats.packets;
459 			bytes   = ring->stats.bytes;
460 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
461 
462 		stats->rx_packets += packets;
463 		stats->rx_bytes   += bytes;
464 
465 	}
466 	rcu_read_unlock();
467 
468 	/* following stats updated by i40e_watchdog_subtask() */
469 	stats->multicast	= vsi_stats->multicast;
470 	stats->tx_errors	= vsi_stats->tx_errors;
471 	stats->tx_dropped	= vsi_stats->tx_dropped;
472 	stats->rx_errors	= vsi_stats->rx_errors;
473 	stats->rx_dropped	= vsi_stats->rx_dropped;
474 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
475 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
476 }
477 
478 /**
479  * i40e_vsi_reset_stats - Resets all stats of the given vsi
480  * @vsi: the VSI to have its stats reset
481  **/
482 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
483 {
484 	struct rtnl_link_stats64 *ns;
485 	int i;
486 
487 	if (!vsi)
488 		return;
489 
490 	ns = i40e_get_vsi_stats_struct(vsi);
491 	memset(ns, 0, sizeof(*ns));
492 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
493 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
494 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
495 	if (vsi->rx_rings && vsi->rx_rings[0]) {
496 		for (i = 0; i < vsi->num_queue_pairs; i++) {
497 			memset(&vsi->rx_rings[i]->stats, 0,
498 			       sizeof(vsi->rx_rings[i]->stats));
499 			memset(&vsi->rx_rings[i]->rx_stats, 0,
500 			       sizeof(vsi->rx_rings[i]->rx_stats));
501 			memset(&vsi->tx_rings[i]->stats, 0,
502 			       sizeof(vsi->tx_rings[i]->stats));
503 			memset(&vsi->tx_rings[i]->tx_stats, 0,
504 			       sizeof(vsi->tx_rings[i]->tx_stats));
505 		}
506 	}
507 	vsi->stat_offsets_loaded = false;
508 }
509 
510 /**
511  * i40e_pf_reset_stats - Reset all of the stats for the given PF
512  * @pf: the PF to be reset
513  **/
514 void i40e_pf_reset_stats(struct i40e_pf *pf)
515 {
516 	int i;
517 
518 	memset(&pf->stats, 0, sizeof(pf->stats));
519 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
520 	pf->stat_offsets_loaded = false;
521 
522 	for (i = 0; i < I40E_MAX_VEB; i++) {
523 		if (pf->veb[i]) {
524 			memset(&pf->veb[i]->stats, 0,
525 			       sizeof(pf->veb[i]->stats));
526 			memset(&pf->veb[i]->stats_offsets, 0,
527 			       sizeof(pf->veb[i]->stats_offsets));
528 			pf->veb[i]->stat_offsets_loaded = false;
529 		}
530 	}
531 	pf->hw_csum_rx_error = 0;
532 }
533 
534 /**
535  * i40e_stat_update48 - read and update a 48 bit stat from the chip
536  * @hw: ptr to the hardware info
537  * @hireg: the high 32 bit reg to read
538  * @loreg: the low 32 bit reg to read
539  * @offset_loaded: has the initial offset been loaded yet
540  * @offset: ptr to current offset value
541  * @stat: ptr to the stat
542  *
543  * Since the device stats are not reset at PFReset, they likely will not
544  * be zeroed when the driver starts.  We'll save the first values read
545  * and use them as offsets to be subtracted from the raw values in order
546  * to report stats that count from zero.  In the process, we also manage
547  * the potential roll-over.
548  **/
549 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
550 			       bool offset_loaded, u64 *offset, u64 *stat)
551 {
552 	u64 new_data;
553 
554 	if (hw->device_id == I40E_DEV_ID_QEMU) {
555 		new_data = rd32(hw, loreg);
556 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
557 	} else {
558 		new_data = rd64(hw, loreg);
559 	}
560 	if (!offset_loaded)
561 		*offset = new_data;
562 	if (likely(new_data >= *offset))
563 		*stat = new_data - *offset;
564 	else
565 		*stat = (new_data + BIT_ULL(48)) - *offset;
566 	*stat &= 0xFFFFFFFFFFFFULL;
567 }
568 
569 /**
570  * i40e_stat_update32 - read and update a 32 bit stat from the chip
571  * @hw: ptr to the hardware info
572  * @reg: the hw reg to read
573  * @offset_loaded: has the initial offset been loaded yet
574  * @offset: ptr to current offset value
575  * @stat: ptr to the stat
576  **/
577 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
578 			       bool offset_loaded, u64 *offset, u64 *stat)
579 {
580 	u32 new_data;
581 
582 	new_data = rd32(hw, reg);
583 	if (!offset_loaded)
584 		*offset = new_data;
585 	if (likely(new_data >= *offset))
586 		*stat = (u32)(new_data - *offset);
587 	else
588 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
589 }
590 
591 /**
592  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
593  * @hw: ptr to the hardware info
594  * @reg: the hw reg to read and clear
595  * @stat: ptr to the stat
596  **/
597 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
598 {
599 	u32 new_data = rd32(hw, reg);
600 
601 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
602 	*stat += new_data;
603 }
604 
605 /**
606  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
607  * @vsi: the VSI to be updated
608  **/
609 void i40e_update_eth_stats(struct i40e_vsi *vsi)
610 {
611 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
612 	struct i40e_pf *pf = vsi->back;
613 	struct i40e_hw *hw = &pf->hw;
614 	struct i40e_eth_stats *oes;
615 	struct i40e_eth_stats *es;     /* device's eth stats */
616 
617 	es = &vsi->eth_stats;
618 	oes = &vsi->eth_stats_offsets;
619 
620 	/* Gather up the stats that the hw collects */
621 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
622 			   vsi->stat_offsets_loaded,
623 			   &oes->tx_errors, &es->tx_errors);
624 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
625 			   vsi->stat_offsets_loaded,
626 			   &oes->rx_discards, &es->rx_discards);
627 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
628 			   vsi->stat_offsets_loaded,
629 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
630 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
631 			   vsi->stat_offsets_loaded,
632 			   &oes->tx_errors, &es->tx_errors);
633 
634 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
635 			   I40E_GLV_GORCL(stat_idx),
636 			   vsi->stat_offsets_loaded,
637 			   &oes->rx_bytes, &es->rx_bytes);
638 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
639 			   I40E_GLV_UPRCL(stat_idx),
640 			   vsi->stat_offsets_loaded,
641 			   &oes->rx_unicast, &es->rx_unicast);
642 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
643 			   I40E_GLV_MPRCL(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->rx_multicast, &es->rx_multicast);
646 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
647 			   I40E_GLV_BPRCL(stat_idx),
648 			   vsi->stat_offsets_loaded,
649 			   &oes->rx_broadcast, &es->rx_broadcast);
650 
651 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
652 			   I40E_GLV_GOTCL(stat_idx),
653 			   vsi->stat_offsets_loaded,
654 			   &oes->tx_bytes, &es->tx_bytes);
655 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
656 			   I40E_GLV_UPTCL(stat_idx),
657 			   vsi->stat_offsets_loaded,
658 			   &oes->tx_unicast, &es->tx_unicast);
659 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
660 			   I40E_GLV_MPTCL(stat_idx),
661 			   vsi->stat_offsets_loaded,
662 			   &oes->tx_multicast, &es->tx_multicast);
663 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
664 			   I40E_GLV_BPTCL(stat_idx),
665 			   vsi->stat_offsets_loaded,
666 			   &oes->tx_broadcast, &es->tx_broadcast);
667 	vsi->stat_offsets_loaded = true;
668 }
669 
670 /**
671  * i40e_update_veb_stats - Update Switch component statistics
672  * @veb: the VEB being updated
673  **/
674 static void i40e_update_veb_stats(struct i40e_veb *veb)
675 {
676 	struct i40e_pf *pf = veb->pf;
677 	struct i40e_hw *hw = &pf->hw;
678 	struct i40e_eth_stats *oes;
679 	struct i40e_eth_stats *es;     /* device's eth stats */
680 	struct i40e_veb_tc_stats *veb_oes;
681 	struct i40e_veb_tc_stats *veb_es;
682 	int i, idx = 0;
683 
684 	idx = veb->stats_idx;
685 	es = &veb->stats;
686 	oes = &veb->stats_offsets;
687 	veb_es = &veb->tc_stats;
688 	veb_oes = &veb->tc_stats_offsets;
689 
690 	/* Gather up the stats that the hw collects */
691 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
692 			   veb->stat_offsets_loaded,
693 			   &oes->tx_discards, &es->tx_discards);
694 	if (hw->revision_id > 0)
695 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
696 				   veb->stat_offsets_loaded,
697 				   &oes->rx_unknown_protocol,
698 				   &es->rx_unknown_protocol);
699 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
700 			   veb->stat_offsets_loaded,
701 			   &oes->rx_bytes, &es->rx_bytes);
702 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
703 			   veb->stat_offsets_loaded,
704 			   &oes->rx_unicast, &es->rx_unicast);
705 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
706 			   veb->stat_offsets_loaded,
707 			   &oes->rx_multicast, &es->rx_multicast);
708 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
709 			   veb->stat_offsets_loaded,
710 			   &oes->rx_broadcast, &es->rx_broadcast);
711 
712 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
713 			   veb->stat_offsets_loaded,
714 			   &oes->tx_bytes, &es->tx_bytes);
715 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
716 			   veb->stat_offsets_loaded,
717 			   &oes->tx_unicast, &es->tx_unicast);
718 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->tx_multicast, &es->tx_multicast);
721 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->tx_broadcast, &es->tx_broadcast);
724 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
725 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
726 				   I40E_GLVEBTC_RPCL(i, idx),
727 				   veb->stat_offsets_loaded,
728 				   &veb_oes->tc_rx_packets[i],
729 				   &veb_es->tc_rx_packets[i]);
730 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
731 				   I40E_GLVEBTC_RBCL(i, idx),
732 				   veb->stat_offsets_loaded,
733 				   &veb_oes->tc_rx_bytes[i],
734 				   &veb_es->tc_rx_bytes[i]);
735 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
736 				   I40E_GLVEBTC_TPCL(i, idx),
737 				   veb->stat_offsets_loaded,
738 				   &veb_oes->tc_tx_packets[i],
739 				   &veb_es->tc_tx_packets[i]);
740 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
741 				   I40E_GLVEBTC_TBCL(i, idx),
742 				   veb->stat_offsets_loaded,
743 				   &veb_oes->tc_tx_bytes[i],
744 				   &veb_es->tc_tx_bytes[i]);
745 	}
746 	veb->stat_offsets_loaded = true;
747 }
748 
749 /**
750  * i40e_update_vsi_stats - Update the vsi statistics counters.
751  * @vsi: the VSI to be updated
752  *
753  * There are a few instances where we store the same stat in a
754  * couple of different structs.  This is partly because we have
755  * the netdev stats that need to be filled out, which is slightly
756  * different from the "eth_stats" defined by the chip and used in
757  * VF communications.  We sort it out here.
758  **/
759 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
760 {
761 	struct i40e_pf *pf = vsi->back;
762 	struct rtnl_link_stats64 *ons;
763 	struct rtnl_link_stats64 *ns;   /* netdev stats */
764 	struct i40e_eth_stats *oes;
765 	struct i40e_eth_stats *es;     /* device's eth stats */
766 	u32 tx_restart, tx_busy;
767 	struct i40e_ring *p;
768 	u32 rx_page, rx_buf;
769 	u64 bytes, packets;
770 	unsigned int start;
771 	u64 tx_linearize;
772 	u64 tx_force_wb;
773 	u64 rx_p, rx_b;
774 	u64 tx_p, tx_b;
775 	u16 q;
776 
777 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
778 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
779 		return;
780 
781 	ns = i40e_get_vsi_stats_struct(vsi);
782 	ons = &vsi->net_stats_offsets;
783 	es = &vsi->eth_stats;
784 	oes = &vsi->eth_stats_offsets;
785 
786 	/* Gather up the netdev and vsi stats that the driver collects
787 	 * on the fly during packet processing
788 	 */
789 	rx_b = rx_p = 0;
790 	tx_b = tx_p = 0;
791 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
792 	rx_page = 0;
793 	rx_buf = 0;
794 	rcu_read_lock();
795 	for (q = 0; q < vsi->num_queue_pairs; q++) {
796 		/* locate Tx ring */
797 		p = READ_ONCE(vsi->tx_rings[q]);
798 
799 		do {
800 			start = u64_stats_fetch_begin_irq(&p->syncp);
801 			packets = p->stats.packets;
802 			bytes = p->stats.bytes;
803 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
804 		tx_b += bytes;
805 		tx_p += packets;
806 		tx_restart += p->tx_stats.restart_queue;
807 		tx_busy += p->tx_stats.tx_busy;
808 		tx_linearize += p->tx_stats.tx_linearize;
809 		tx_force_wb += p->tx_stats.tx_force_wb;
810 
811 		/* Rx queue is part of the same block as Tx queue */
812 		p = &p[1];
813 		do {
814 			start = u64_stats_fetch_begin_irq(&p->syncp);
815 			packets = p->stats.packets;
816 			bytes = p->stats.bytes;
817 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
818 		rx_b += bytes;
819 		rx_p += packets;
820 		rx_buf += p->rx_stats.alloc_buff_failed;
821 		rx_page += p->rx_stats.alloc_page_failed;
822 	}
823 	rcu_read_unlock();
824 	vsi->tx_restart = tx_restart;
825 	vsi->tx_busy = tx_busy;
826 	vsi->tx_linearize = tx_linearize;
827 	vsi->tx_force_wb = tx_force_wb;
828 	vsi->rx_page_failed = rx_page;
829 	vsi->rx_buf_failed = rx_buf;
830 
831 	ns->rx_packets = rx_p;
832 	ns->rx_bytes = rx_b;
833 	ns->tx_packets = tx_p;
834 	ns->tx_bytes = tx_b;
835 
836 	/* update netdev stats from eth stats */
837 	i40e_update_eth_stats(vsi);
838 	ons->tx_errors = oes->tx_errors;
839 	ns->tx_errors = es->tx_errors;
840 	ons->multicast = oes->rx_multicast;
841 	ns->multicast = es->rx_multicast;
842 	ons->rx_dropped = oes->rx_discards;
843 	ns->rx_dropped = es->rx_discards;
844 	ons->tx_dropped = oes->tx_discards;
845 	ns->tx_dropped = es->tx_discards;
846 
847 	/* pull in a couple PF stats if this is the main vsi */
848 	if (vsi == pf->vsi[pf->lan_vsi]) {
849 		ns->rx_crc_errors = pf->stats.crc_errors;
850 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
851 		ns->rx_length_errors = pf->stats.rx_length_errors;
852 	}
853 }
854 
855 /**
856  * i40e_update_pf_stats - Update the PF statistics counters.
857  * @pf: the PF to be updated
858  **/
859 static void i40e_update_pf_stats(struct i40e_pf *pf)
860 {
861 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
862 	struct i40e_hw_port_stats *nsd = &pf->stats;
863 	struct i40e_hw *hw = &pf->hw;
864 	u32 val;
865 	int i;
866 
867 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
868 			   I40E_GLPRT_GORCL(hw->port),
869 			   pf->stat_offsets_loaded,
870 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
871 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
872 			   I40E_GLPRT_GOTCL(hw->port),
873 			   pf->stat_offsets_loaded,
874 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
875 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
876 			   pf->stat_offsets_loaded,
877 			   &osd->eth.rx_discards,
878 			   &nsd->eth.rx_discards);
879 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
880 			   I40E_GLPRT_UPRCL(hw->port),
881 			   pf->stat_offsets_loaded,
882 			   &osd->eth.rx_unicast,
883 			   &nsd->eth.rx_unicast);
884 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
885 			   I40E_GLPRT_MPRCL(hw->port),
886 			   pf->stat_offsets_loaded,
887 			   &osd->eth.rx_multicast,
888 			   &nsd->eth.rx_multicast);
889 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
890 			   I40E_GLPRT_BPRCL(hw->port),
891 			   pf->stat_offsets_loaded,
892 			   &osd->eth.rx_broadcast,
893 			   &nsd->eth.rx_broadcast);
894 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
895 			   I40E_GLPRT_UPTCL(hw->port),
896 			   pf->stat_offsets_loaded,
897 			   &osd->eth.tx_unicast,
898 			   &nsd->eth.tx_unicast);
899 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
900 			   I40E_GLPRT_MPTCL(hw->port),
901 			   pf->stat_offsets_loaded,
902 			   &osd->eth.tx_multicast,
903 			   &nsd->eth.tx_multicast);
904 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
905 			   I40E_GLPRT_BPTCL(hw->port),
906 			   pf->stat_offsets_loaded,
907 			   &osd->eth.tx_broadcast,
908 			   &nsd->eth.tx_broadcast);
909 
910 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
911 			   pf->stat_offsets_loaded,
912 			   &osd->tx_dropped_link_down,
913 			   &nsd->tx_dropped_link_down);
914 
915 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
916 			   pf->stat_offsets_loaded,
917 			   &osd->crc_errors, &nsd->crc_errors);
918 
919 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->illegal_bytes, &nsd->illegal_bytes);
922 
923 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
924 			   pf->stat_offsets_loaded,
925 			   &osd->mac_local_faults,
926 			   &nsd->mac_local_faults);
927 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
928 			   pf->stat_offsets_loaded,
929 			   &osd->mac_remote_faults,
930 			   &nsd->mac_remote_faults);
931 
932 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
933 			   pf->stat_offsets_loaded,
934 			   &osd->rx_length_errors,
935 			   &nsd->rx_length_errors);
936 
937 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
938 			   pf->stat_offsets_loaded,
939 			   &osd->link_xon_rx, &nsd->link_xon_rx);
940 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
941 			   pf->stat_offsets_loaded,
942 			   &osd->link_xon_tx, &nsd->link_xon_tx);
943 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
944 			   pf->stat_offsets_loaded,
945 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
946 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
949 
950 	for (i = 0; i < 8; i++) {
951 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
952 				   pf->stat_offsets_loaded,
953 				   &osd->priority_xoff_rx[i],
954 				   &nsd->priority_xoff_rx[i]);
955 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
956 				   pf->stat_offsets_loaded,
957 				   &osd->priority_xon_rx[i],
958 				   &nsd->priority_xon_rx[i]);
959 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
960 				   pf->stat_offsets_loaded,
961 				   &osd->priority_xon_tx[i],
962 				   &nsd->priority_xon_tx[i]);
963 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
964 				   pf->stat_offsets_loaded,
965 				   &osd->priority_xoff_tx[i],
966 				   &nsd->priority_xoff_tx[i]);
967 		i40e_stat_update32(hw,
968 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
969 				   pf->stat_offsets_loaded,
970 				   &osd->priority_xon_2_xoff[i],
971 				   &nsd->priority_xon_2_xoff[i]);
972 	}
973 
974 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
975 			   I40E_GLPRT_PRC64L(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->rx_size_64, &nsd->rx_size_64);
978 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
979 			   I40E_GLPRT_PRC127L(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->rx_size_127, &nsd->rx_size_127);
982 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
983 			   I40E_GLPRT_PRC255L(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->rx_size_255, &nsd->rx_size_255);
986 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
987 			   I40E_GLPRT_PRC511L(hw->port),
988 			   pf->stat_offsets_loaded,
989 			   &osd->rx_size_511, &nsd->rx_size_511);
990 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
991 			   I40E_GLPRT_PRC1023L(hw->port),
992 			   pf->stat_offsets_loaded,
993 			   &osd->rx_size_1023, &nsd->rx_size_1023);
994 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
995 			   I40E_GLPRT_PRC1522L(hw->port),
996 			   pf->stat_offsets_loaded,
997 			   &osd->rx_size_1522, &nsd->rx_size_1522);
998 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
999 			   I40E_GLPRT_PRC9522L(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->rx_size_big, &nsd->rx_size_big);
1002 
1003 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1004 			   I40E_GLPRT_PTC64L(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->tx_size_64, &nsd->tx_size_64);
1007 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1008 			   I40E_GLPRT_PTC127L(hw->port),
1009 			   pf->stat_offsets_loaded,
1010 			   &osd->tx_size_127, &nsd->tx_size_127);
1011 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1012 			   I40E_GLPRT_PTC255L(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->tx_size_255, &nsd->tx_size_255);
1015 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1016 			   I40E_GLPRT_PTC511L(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->tx_size_511, &nsd->tx_size_511);
1019 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1020 			   I40E_GLPRT_PTC1023L(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1023 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1024 			   I40E_GLPRT_PTC1522L(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1027 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1028 			   I40E_GLPRT_PTC9522L(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->tx_size_big, &nsd->tx_size_big);
1031 
1032 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->rx_undersize, &nsd->rx_undersize);
1035 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->rx_fragments, &nsd->rx_fragments);
1038 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_oversize, &nsd->rx_oversize);
1041 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->rx_jabber, &nsd->rx_jabber);
1044 
1045 	/* FDIR stats */
1046 	i40e_stat_update_and_clear32(hw,
1047 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1048 			&nsd->fd_atr_match);
1049 	i40e_stat_update_and_clear32(hw,
1050 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1051 			&nsd->fd_sb_match);
1052 	i40e_stat_update_and_clear32(hw,
1053 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1054 			&nsd->fd_atr_tunnel_match);
1055 
1056 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1057 	nsd->tx_lpi_status =
1058 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1059 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1060 	nsd->rx_lpi_status =
1061 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1062 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1063 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1066 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1067 			   pf->stat_offsets_loaded,
1068 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1069 
1070 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1071 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1072 		nsd->fd_sb_status = true;
1073 	else
1074 		nsd->fd_sb_status = false;
1075 
1076 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1077 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1078 		nsd->fd_atr_status = true;
1079 	else
1080 		nsd->fd_atr_status = false;
1081 
1082 	pf->stat_offsets_loaded = true;
1083 }
1084 
1085 /**
1086  * i40e_update_stats - Update the various statistics counters.
1087  * @vsi: the VSI to be updated
1088  *
1089  * Update the various stats for this VSI and its related entities.
1090  **/
1091 void i40e_update_stats(struct i40e_vsi *vsi)
1092 {
1093 	struct i40e_pf *pf = vsi->back;
1094 
1095 	if (vsi == pf->vsi[pf->lan_vsi])
1096 		i40e_update_pf_stats(pf);
1097 
1098 	i40e_update_vsi_stats(vsi);
1099 }
1100 
1101 /**
1102  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1103  * @vsi: the VSI to be searched
1104  * @macaddr: the MAC address
1105  * @vlan: the vlan
1106  *
1107  * Returns ptr to the filter object or NULL
1108  **/
1109 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1110 						const u8 *macaddr, s16 vlan)
1111 {
1112 	struct i40e_mac_filter *f;
1113 	u64 key;
1114 
1115 	if (!vsi || !macaddr)
1116 		return NULL;
1117 
1118 	key = i40e_addr_to_hkey(macaddr);
1119 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1120 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1121 		    (vlan == f->vlan))
1122 			return f;
1123 	}
1124 	return NULL;
1125 }
1126 
1127 /**
1128  * i40e_find_mac - Find a mac addr in the macvlan filters list
1129  * @vsi: the VSI to be searched
1130  * @macaddr: the MAC address we are searching for
1131  *
1132  * Returns the first filter with the provided MAC address or NULL if
1133  * MAC address was not found
1134  **/
1135 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1136 {
1137 	struct i40e_mac_filter *f;
1138 	u64 key;
1139 
1140 	if (!vsi || !macaddr)
1141 		return NULL;
1142 
1143 	key = i40e_addr_to_hkey(macaddr);
1144 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1145 		if ((ether_addr_equal(macaddr, f->macaddr)))
1146 			return f;
1147 	}
1148 	return NULL;
1149 }
1150 
1151 /**
1152  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1153  * @vsi: the VSI to be searched
1154  *
1155  * Returns true if VSI is in vlan mode or false otherwise
1156  **/
1157 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1158 {
1159 	/* If we have a PVID, always operate in VLAN mode */
1160 	if (vsi->info.pvid)
1161 		return true;
1162 
1163 	/* We need to operate in VLAN mode whenever we have any filters with
1164 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1165 	 * time, incurring search cost repeatedly. However, we can notice two
1166 	 * things:
1167 	 *
1168 	 * 1) the only place where we can gain a VLAN filter is in
1169 	 *    i40e_add_filter.
1170 	 *
1171 	 * 2) the only place where filters are actually removed is in
1172 	 *    i40e_sync_filters_subtask.
1173 	 *
1174 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1175 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1176 	 * we have to perform the full search after deleting filters in
1177 	 * i40e_sync_filters_subtask, but we already have to search
1178 	 * filters here and can perform the check at the same time. This
1179 	 * results in avoiding embedding a loop for VLAN mode inside another
1180 	 * loop over all the filters, and should maintain correctness as noted
1181 	 * above.
1182 	 */
1183 	return vsi->has_vlan_filter;
1184 }
1185 
1186 /**
1187  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1188  * @vsi: the VSI to configure
1189  * @tmp_add_list: list of filters ready to be added
1190  * @tmp_del_list: list of filters ready to be deleted
1191  * @vlan_filters: the number of active VLAN filters
1192  *
1193  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1194  * behave as expected. If we have any active VLAN filters remaining or about
1195  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1196  * so that they only match against untagged traffic. If we no longer have any
1197  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1198  * so that they match against both tagged and untagged traffic. In this way,
1199  * we ensure that we correctly receive the desired traffic. This ensures that
1200  * when we have an active VLAN we will receive only untagged traffic and
1201  * traffic matching active VLANs. If we have no active VLANs then we will
1202  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1203  *
1204  * Finally, in a similar fashion, this function also corrects filters when
1205  * there is an active PVID assigned to this VSI.
1206  *
1207  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1208  *
1209  * This function is only expected to be called from within
1210  * i40e_sync_vsi_filters.
1211  *
1212  * NOTE: This function expects to be called while under the
1213  * mac_filter_hash_lock
1214  */
1215 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1216 					 struct hlist_head *tmp_add_list,
1217 					 struct hlist_head *tmp_del_list,
1218 					 int vlan_filters)
1219 {
1220 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1221 	struct i40e_mac_filter *f, *add_head;
1222 	struct i40e_new_mac_filter *new;
1223 	struct hlist_node *h;
1224 	int bkt, new_vlan;
1225 
1226 	/* To determine if a particular filter needs to be replaced we
1227 	 * have the three following conditions:
1228 	 *
1229 	 * a) if we have a PVID assigned, then all filters which are
1230 	 *    not marked as VLAN=PVID must be replaced with filters that
1231 	 *    are.
1232 	 * b) otherwise, if we have any active VLANS, all filters
1233 	 *    which are marked as VLAN=-1 must be replaced with
1234 	 *    filters marked as VLAN=0
1235 	 * c) finally, if we do not have any active VLANS, all filters
1236 	 *    which are marked as VLAN=0 must be replaced with filters
1237 	 *    marked as VLAN=-1
1238 	 */
1239 
1240 	/* Update the filters about to be added in place */
1241 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1242 		if (pvid && new->f->vlan != pvid)
1243 			new->f->vlan = pvid;
1244 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1245 			new->f->vlan = 0;
1246 		else if (!vlan_filters && new->f->vlan == 0)
1247 			new->f->vlan = I40E_VLAN_ANY;
1248 	}
1249 
1250 	/* Update the remaining active filters */
1251 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1252 		/* Combine the checks for whether a filter needs to be changed
1253 		 * and then determine the new VLAN inside the if block, in
1254 		 * order to avoid duplicating code for adding the new filter
1255 		 * then deleting the old filter.
1256 		 */
1257 		if ((pvid && f->vlan != pvid) ||
1258 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1259 		    (!vlan_filters && f->vlan == 0)) {
1260 			/* Determine the new vlan we will be adding */
1261 			if (pvid)
1262 				new_vlan = pvid;
1263 			else if (vlan_filters)
1264 				new_vlan = 0;
1265 			else
1266 				new_vlan = I40E_VLAN_ANY;
1267 
1268 			/* Create the new filter */
1269 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1270 			if (!add_head)
1271 				return -ENOMEM;
1272 
1273 			/* Create a temporary i40e_new_mac_filter */
1274 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1275 			if (!new)
1276 				return -ENOMEM;
1277 
1278 			new->f = add_head;
1279 			new->state = add_head->state;
1280 
1281 			/* Add the new filter to the tmp list */
1282 			hlist_add_head(&new->hlist, tmp_add_list);
1283 
1284 			/* Put the original filter into the delete list */
1285 			f->state = I40E_FILTER_REMOVE;
1286 			hash_del(&f->hlist);
1287 			hlist_add_head(&f->hlist, tmp_del_list);
1288 		}
1289 	}
1290 
1291 	vsi->has_vlan_filter = !!vlan_filters;
1292 
1293 	return 0;
1294 }
1295 
1296 /**
1297  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1298  * @vsi: the PF Main VSI - inappropriate for any other VSI
1299  * @macaddr: the MAC address
1300  *
1301  * Remove whatever filter the firmware set up so the driver can manage
1302  * its own filtering intelligently.
1303  **/
1304 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1305 {
1306 	struct i40e_aqc_remove_macvlan_element_data element;
1307 	struct i40e_pf *pf = vsi->back;
1308 
1309 	/* Only appropriate for the PF main VSI */
1310 	if (vsi->type != I40E_VSI_MAIN)
1311 		return;
1312 
1313 	memset(&element, 0, sizeof(element));
1314 	ether_addr_copy(element.mac_addr, macaddr);
1315 	element.vlan_tag = 0;
1316 	/* Ignore error returns, some firmware does it this way... */
1317 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1318 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1319 
1320 	memset(&element, 0, sizeof(element));
1321 	ether_addr_copy(element.mac_addr, macaddr);
1322 	element.vlan_tag = 0;
1323 	/* ...and some firmware does it this way. */
1324 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1325 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1326 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1327 }
1328 
1329 /**
1330  * i40e_add_filter - Add a mac/vlan filter to the VSI
1331  * @vsi: the VSI to be searched
1332  * @macaddr: the MAC address
1333  * @vlan: the vlan
1334  *
1335  * Returns ptr to the filter object or NULL when no memory available.
1336  *
1337  * NOTE: This function is expected to be called with mac_filter_hash_lock
1338  * being held.
1339  **/
1340 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1341 					const u8 *macaddr, s16 vlan)
1342 {
1343 	struct i40e_mac_filter *f;
1344 	u64 key;
1345 
1346 	if (!vsi || !macaddr)
1347 		return NULL;
1348 
1349 	f = i40e_find_filter(vsi, macaddr, vlan);
1350 	if (!f) {
1351 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1352 		if (!f)
1353 			return NULL;
1354 
1355 		/* Update the boolean indicating if we need to function in
1356 		 * VLAN mode.
1357 		 */
1358 		if (vlan >= 0)
1359 			vsi->has_vlan_filter = true;
1360 
1361 		ether_addr_copy(f->macaddr, macaddr);
1362 		f->vlan = vlan;
1363 		f->state = I40E_FILTER_NEW;
1364 		INIT_HLIST_NODE(&f->hlist);
1365 
1366 		key = i40e_addr_to_hkey(macaddr);
1367 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1368 
1369 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1370 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1371 	}
1372 
1373 	/* If we're asked to add a filter that has been marked for removal, it
1374 	 * is safe to simply restore it to active state. __i40e_del_filter
1375 	 * will have simply deleted any filters which were previously marked
1376 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1377 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1378 	 * task, just restore this filter to the ACTIVE state so that the
1379 	 * sync task leaves it in place
1380 	 */
1381 	if (f->state == I40E_FILTER_REMOVE)
1382 		f->state = I40E_FILTER_ACTIVE;
1383 
1384 	return f;
1385 }
1386 
1387 /**
1388  * __i40e_del_filter - Remove a specific filter from the VSI
1389  * @vsi: VSI to remove from
1390  * @f: the filter to remove from the list
1391  *
1392  * This function should be called instead of i40e_del_filter only if you know
1393  * the exact filter you will remove already, such as via i40e_find_filter or
1394  * i40e_find_mac.
1395  *
1396  * NOTE: This function is expected to be called with mac_filter_hash_lock
1397  * being held.
1398  * ANOTHER NOTE: This function MUST be called from within the context of
1399  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1400  * instead of list_for_each_entry().
1401  **/
1402 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1403 {
1404 	if (!f)
1405 		return;
1406 
1407 	/* If the filter was never added to firmware then we can just delete it
1408 	 * directly and we don't want to set the status to remove or else an
1409 	 * admin queue command will unnecessarily fire.
1410 	 */
1411 	if ((f->state == I40E_FILTER_FAILED) ||
1412 	    (f->state == I40E_FILTER_NEW)) {
1413 		hash_del(&f->hlist);
1414 		kfree(f);
1415 	} else {
1416 		f->state = I40E_FILTER_REMOVE;
1417 	}
1418 
1419 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1420 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1421 }
1422 
1423 /**
1424  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1425  * @vsi: the VSI to be searched
1426  * @macaddr: the MAC address
1427  * @vlan: the VLAN
1428  *
1429  * NOTE: This function is expected to be called with mac_filter_hash_lock
1430  * being held.
1431  * ANOTHER NOTE: This function MUST be called from within the context of
1432  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1433  * instead of list_for_each_entry().
1434  **/
1435 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1436 {
1437 	struct i40e_mac_filter *f;
1438 
1439 	if (!vsi || !macaddr)
1440 		return;
1441 
1442 	f = i40e_find_filter(vsi, macaddr, vlan);
1443 	__i40e_del_filter(vsi, f);
1444 }
1445 
1446 /**
1447  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1448  * @vsi: the VSI to be searched
1449  * @macaddr: the mac address to be filtered
1450  *
1451  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1452  * go through all the macvlan filters and add a macvlan filter for each
1453  * unique vlan that already exists. If a PVID has been assigned, instead only
1454  * add the macaddr to that VLAN.
1455  *
1456  * Returns last filter added on success, else NULL
1457  **/
1458 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1459 					    const u8 *macaddr)
1460 {
1461 	struct i40e_mac_filter *f, *add = NULL;
1462 	struct hlist_node *h;
1463 	int bkt;
1464 
1465 	if (vsi->info.pvid)
1466 		return i40e_add_filter(vsi, macaddr,
1467 				       le16_to_cpu(vsi->info.pvid));
1468 
1469 	if (!i40e_is_vsi_in_vlan(vsi))
1470 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1471 
1472 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1473 		if (f->state == I40E_FILTER_REMOVE)
1474 			continue;
1475 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1476 		if (!add)
1477 			return NULL;
1478 	}
1479 
1480 	return add;
1481 }
1482 
1483 /**
1484  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1485  * @vsi: the VSI to be searched
1486  * @macaddr: the mac address to be removed
1487  *
1488  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1489  * associated with.
1490  *
1491  * Returns 0 for success, or error
1492  **/
1493 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1494 {
1495 	struct i40e_mac_filter *f;
1496 	struct hlist_node *h;
1497 	bool found = false;
1498 	int bkt;
1499 
1500 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1501 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1502 		if (ether_addr_equal(macaddr, f->macaddr)) {
1503 			__i40e_del_filter(vsi, f);
1504 			found = true;
1505 		}
1506 	}
1507 
1508 	if (found)
1509 		return 0;
1510 	else
1511 		return -ENOENT;
1512 }
1513 
1514 /**
1515  * i40e_set_mac - NDO callback to set mac address
1516  * @netdev: network interface device structure
1517  * @p: pointer to an address structure
1518  *
1519  * Returns 0 on success, negative on failure
1520  **/
1521 static int i40e_set_mac(struct net_device *netdev, void *p)
1522 {
1523 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1524 	struct i40e_vsi *vsi = np->vsi;
1525 	struct i40e_pf *pf = vsi->back;
1526 	struct i40e_hw *hw = &pf->hw;
1527 	struct sockaddr *addr = p;
1528 
1529 	if (!is_valid_ether_addr(addr->sa_data))
1530 		return -EADDRNOTAVAIL;
1531 
1532 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1533 		netdev_info(netdev, "already using mac address %pM\n",
1534 			    addr->sa_data);
1535 		return 0;
1536 	}
1537 
1538 	if (test_bit(__I40E_DOWN, pf->state) ||
1539 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1540 		return -EADDRNOTAVAIL;
1541 
1542 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1543 		netdev_info(netdev, "returning to hw mac address %pM\n",
1544 			    hw->mac.addr);
1545 	else
1546 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1547 
1548 	/* Copy the address first, so that we avoid a possible race with
1549 	 * .set_rx_mode().
1550 	 * - Remove old address from MAC filter
1551 	 * - Copy new address
1552 	 * - Add new address to MAC filter
1553 	 */
1554 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1555 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1556 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1557 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1558 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1559 
1560 	if (vsi->type == I40E_VSI_MAIN) {
1561 		i40e_status ret;
1562 
1563 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1564 						addr->sa_data, NULL);
1565 		if (ret)
1566 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1567 				    i40e_stat_str(hw, ret),
1568 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1569 	}
1570 
1571 	/* schedule our worker thread which will take care of
1572 	 * applying the new filter changes
1573 	 */
1574 	i40e_service_event_schedule(pf);
1575 	return 0;
1576 }
1577 
1578 /**
1579  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1580  * @vsi: vsi structure
1581  * @seed: RSS hash seed
1582  **/
1583 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1584 			      u8 *lut, u16 lut_size)
1585 {
1586 	struct i40e_pf *pf = vsi->back;
1587 	struct i40e_hw *hw = &pf->hw;
1588 	int ret = 0;
1589 
1590 	if (seed) {
1591 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1592 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1593 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1594 		if (ret) {
1595 			dev_info(&pf->pdev->dev,
1596 				 "Cannot set RSS key, err %s aq_err %s\n",
1597 				 i40e_stat_str(hw, ret),
1598 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1599 			return ret;
1600 		}
1601 	}
1602 	if (lut) {
1603 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1604 
1605 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1606 		if (ret) {
1607 			dev_info(&pf->pdev->dev,
1608 				 "Cannot set RSS lut, err %s aq_err %s\n",
1609 				 i40e_stat_str(hw, ret),
1610 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1611 			return ret;
1612 		}
1613 	}
1614 	return ret;
1615 }
1616 
1617 /**
1618  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1619  * @vsi: VSI structure
1620  **/
1621 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1622 {
1623 	struct i40e_pf *pf = vsi->back;
1624 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1625 	u8 *lut;
1626 	int ret;
1627 
1628 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1629 		return 0;
1630 	if (!vsi->rss_size)
1631 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1632 				      vsi->num_queue_pairs);
1633 	if (!vsi->rss_size)
1634 		return -EINVAL;
1635 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1636 	if (!lut)
1637 		return -ENOMEM;
1638 
1639 	/* Use the user configured hash keys and lookup table if there is one,
1640 	 * otherwise use default
1641 	 */
1642 	if (vsi->rss_lut_user)
1643 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1644 	else
1645 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1646 	if (vsi->rss_hkey_user)
1647 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1648 	else
1649 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1650 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1651 	kfree(lut);
1652 	return ret;
1653 }
1654 
1655 /**
1656  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1657  * @vsi: the VSI being configured,
1658  * @ctxt: VSI context structure
1659  * @enabled_tc: number of traffic classes to enable
1660  *
1661  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1662  **/
1663 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1664 					   struct i40e_vsi_context *ctxt,
1665 					   u8 enabled_tc)
1666 {
1667 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1668 	int i, override_q, pow, num_qps, ret;
1669 	u8 netdev_tc = 0, offset = 0;
1670 
1671 	if (vsi->type != I40E_VSI_MAIN)
1672 		return -EINVAL;
1673 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1674 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1675 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1676 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1677 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1678 
1679 	/* find the next higher power-of-2 of num queue pairs */
1680 	pow = ilog2(num_qps);
1681 	if (!is_power_of_2(num_qps))
1682 		pow++;
1683 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1684 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1685 
1686 	/* Setup queue offset/count for all TCs for given VSI */
1687 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1688 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1689 		/* See if the given TC is enabled for the given VSI */
1690 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1691 			offset = vsi->mqprio_qopt.qopt.offset[i];
1692 			qcount = vsi->mqprio_qopt.qopt.count[i];
1693 			if (qcount > max_qcount)
1694 				max_qcount = qcount;
1695 			vsi->tc_config.tc_info[i].qoffset = offset;
1696 			vsi->tc_config.tc_info[i].qcount = qcount;
1697 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1698 		} else {
1699 			/* TC is not enabled so set the offset to
1700 			 * default queue and allocate one queue
1701 			 * for the given TC.
1702 			 */
1703 			vsi->tc_config.tc_info[i].qoffset = 0;
1704 			vsi->tc_config.tc_info[i].qcount = 1;
1705 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1706 		}
1707 	}
1708 
1709 	/* Set actual Tx/Rx queue pairs */
1710 	vsi->num_queue_pairs = offset + qcount;
1711 
1712 	/* Setup queue TC[0].qmap for given VSI context */
1713 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1714 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1715 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1716 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1717 
1718 	/* Reconfigure RSS for main VSI with max queue count */
1719 	vsi->rss_size = max_qcount;
1720 	ret = i40e_vsi_config_rss(vsi);
1721 	if (ret) {
1722 		dev_info(&vsi->back->pdev->dev,
1723 			 "Failed to reconfig rss for num_queues (%u)\n",
1724 			 max_qcount);
1725 		return ret;
1726 	}
1727 	vsi->reconfig_rss = true;
1728 	dev_dbg(&vsi->back->pdev->dev,
1729 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1730 
1731 	/* Find queue count available for channel VSIs and starting offset
1732 	 * for channel VSIs
1733 	 */
1734 	override_q = vsi->mqprio_qopt.qopt.count[0];
1735 	if (override_q && override_q < vsi->num_queue_pairs) {
1736 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1737 		vsi->next_base_queue = override_q;
1738 	}
1739 	return 0;
1740 }
1741 
1742 /**
1743  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1744  * @vsi: the VSI being setup
1745  * @ctxt: VSI context structure
1746  * @enabled_tc: Enabled TCs bitmap
1747  * @is_add: True if called before Add VSI
1748  *
1749  * Setup VSI queue mapping for enabled traffic classes.
1750  **/
1751 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1752 				     struct i40e_vsi_context *ctxt,
1753 				     u8 enabled_tc,
1754 				     bool is_add)
1755 {
1756 	struct i40e_pf *pf = vsi->back;
1757 	u16 sections = 0;
1758 	u8 netdev_tc = 0;
1759 	u16 numtc = 1;
1760 	u16 qcount;
1761 	u8 offset;
1762 	u16 qmap;
1763 	int i;
1764 	u16 num_tc_qps = 0;
1765 
1766 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1767 	offset = 0;
1768 
1769 	/* Number of queues per enabled TC */
1770 	num_tc_qps = vsi->alloc_queue_pairs;
1771 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1772 		/* Find numtc from enabled TC bitmap */
1773 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1774 			if (enabled_tc & BIT(i)) /* TC is enabled */
1775 				numtc++;
1776 		}
1777 		if (!numtc) {
1778 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1779 			numtc = 1;
1780 		}
1781 		num_tc_qps = num_tc_qps / numtc;
1782 		num_tc_qps = min_t(int, num_tc_qps,
1783 				   i40e_pf_get_max_q_per_tc(pf));
1784 	}
1785 
1786 	vsi->tc_config.numtc = numtc;
1787 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1788 
1789 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1790 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1791 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1792 
1793 	/* Setup queue offset/count for all TCs for given VSI */
1794 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1795 		/* See if the given TC is enabled for the given VSI */
1796 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1797 			/* TC is enabled */
1798 			int pow, num_qps;
1799 
1800 			switch (vsi->type) {
1801 			case I40E_VSI_MAIN:
1802 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1803 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1804 				    vsi->tc_config.enabled_tc != 1) {
1805 					qcount = min_t(int, pf->alloc_rss_size,
1806 						       num_tc_qps);
1807 					break;
1808 				}
1809 				/* fall through */
1810 			case I40E_VSI_FDIR:
1811 			case I40E_VSI_SRIOV:
1812 			case I40E_VSI_VMDQ2:
1813 			default:
1814 				qcount = num_tc_qps;
1815 				WARN_ON(i != 0);
1816 				break;
1817 			}
1818 			vsi->tc_config.tc_info[i].qoffset = offset;
1819 			vsi->tc_config.tc_info[i].qcount = qcount;
1820 
1821 			/* find the next higher power-of-2 of num queue pairs */
1822 			num_qps = qcount;
1823 			pow = 0;
1824 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1825 				pow++;
1826 				num_qps >>= 1;
1827 			}
1828 
1829 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1830 			qmap =
1831 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1832 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1833 
1834 			offset += qcount;
1835 		} else {
1836 			/* TC is not enabled so set the offset to
1837 			 * default queue and allocate one queue
1838 			 * for the given TC.
1839 			 */
1840 			vsi->tc_config.tc_info[i].qoffset = 0;
1841 			vsi->tc_config.tc_info[i].qcount = 1;
1842 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1843 
1844 			qmap = 0;
1845 		}
1846 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1847 	}
1848 
1849 	/* Set actual Tx/Rx queue pairs */
1850 	vsi->num_queue_pairs = offset;
1851 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1852 		if (vsi->req_queue_pairs > 0)
1853 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1854 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1855 			vsi->num_queue_pairs = pf->num_lan_msix;
1856 	}
1857 
1858 	/* Scheduler section valid can only be set for ADD VSI */
1859 	if (is_add) {
1860 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1861 
1862 		ctxt->info.up_enable_bits = enabled_tc;
1863 	}
1864 	if (vsi->type == I40E_VSI_SRIOV) {
1865 		ctxt->info.mapping_flags |=
1866 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1867 		for (i = 0; i < vsi->num_queue_pairs; i++)
1868 			ctxt->info.queue_mapping[i] =
1869 					       cpu_to_le16(vsi->base_queue + i);
1870 	} else {
1871 		ctxt->info.mapping_flags |=
1872 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1873 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1874 	}
1875 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1876 }
1877 
1878 /**
1879  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1880  * @netdev: the netdevice
1881  * @addr: address to add
1882  *
1883  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1884  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1885  */
1886 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1887 {
1888 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1889 	struct i40e_vsi *vsi = np->vsi;
1890 
1891 	if (i40e_add_mac_filter(vsi, addr))
1892 		return 0;
1893 	else
1894 		return -ENOMEM;
1895 }
1896 
1897 /**
1898  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1899  * @netdev: the netdevice
1900  * @addr: address to add
1901  *
1902  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1903  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1904  */
1905 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1906 {
1907 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1908 	struct i40e_vsi *vsi = np->vsi;
1909 
1910 	/* Under some circumstances, we might receive a request to delete
1911 	 * our own device address from our uc list. Because we store the
1912 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1913 	 * such requests and not delete our device address from this list.
1914 	 */
1915 	if (ether_addr_equal(addr, netdev->dev_addr))
1916 		return 0;
1917 
1918 	i40e_del_mac_filter(vsi, addr);
1919 
1920 	return 0;
1921 }
1922 
1923 /**
1924  * i40e_set_rx_mode - NDO callback to set the netdev filters
1925  * @netdev: network interface device structure
1926  **/
1927 static void i40e_set_rx_mode(struct net_device *netdev)
1928 {
1929 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1930 	struct i40e_vsi *vsi = np->vsi;
1931 
1932 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1933 
1934 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1935 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1936 
1937 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1938 
1939 	/* check for other flag changes */
1940 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1941 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1942 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1943 	}
1944 }
1945 
1946 /**
1947  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1948  * @vsi: Pointer to VSI struct
1949  * @from: Pointer to list which contains MAC filter entries - changes to
1950  *        those entries needs to be undone.
1951  *
1952  * MAC filter entries from this list were slated for deletion.
1953  **/
1954 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1955 					 struct hlist_head *from)
1956 {
1957 	struct i40e_mac_filter *f;
1958 	struct hlist_node *h;
1959 
1960 	hlist_for_each_entry_safe(f, h, from, hlist) {
1961 		u64 key = i40e_addr_to_hkey(f->macaddr);
1962 
1963 		/* Move the element back into MAC filter list*/
1964 		hlist_del(&f->hlist);
1965 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1966 	}
1967 }
1968 
1969 /**
1970  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1971  * @vsi: Pointer to vsi struct
1972  * @from: Pointer to list which contains MAC filter entries - changes to
1973  *        those entries needs to be undone.
1974  *
1975  * MAC filter entries from this list were slated for addition.
1976  **/
1977 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1978 					 struct hlist_head *from)
1979 {
1980 	struct i40e_new_mac_filter *new;
1981 	struct hlist_node *h;
1982 
1983 	hlist_for_each_entry_safe(new, h, from, hlist) {
1984 		/* We can simply free the wrapper structure */
1985 		hlist_del(&new->hlist);
1986 		kfree(new);
1987 	}
1988 }
1989 
1990 /**
1991  * i40e_next_entry - Get the next non-broadcast filter from a list
1992  * @next: pointer to filter in list
1993  *
1994  * Returns the next non-broadcast filter in the list. Required so that we
1995  * ignore broadcast filters within the list, since these are not handled via
1996  * the normal firmware update path.
1997  */
1998 static
1999 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2000 {
2001 	hlist_for_each_entry_continue(next, hlist) {
2002 		if (!is_broadcast_ether_addr(next->f->macaddr))
2003 			return next;
2004 	}
2005 
2006 	return NULL;
2007 }
2008 
2009 /**
2010  * i40e_update_filter_state - Update filter state based on return data
2011  * from firmware
2012  * @count: Number of filters added
2013  * @add_list: return data from fw
2014  * @add_head: pointer to first filter in current batch
2015  *
2016  * MAC filter entries from list were slated to be added to device. Returns
2017  * number of successful filters. Note that 0 does NOT mean success!
2018  **/
2019 static int
2020 i40e_update_filter_state(int count,
2021 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2022 			 struct i40e_new_mac_filter *add_head)
2023 {
2024 	int retval = 0;
2025 	int i;
2026 
2027 	for (i = 0; i < count; i++) {
2028 		/* Always check status of each filter. We don't need to check
2029 		 * the firmware return status because we pre-set the filter
2030 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2031 		 * request to the adminq. Thus, if it no longer matches then
2032 		 * we know the filter is active.
2033 		 */
2034 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2035 			add_head->state = I40E_FILTER_FAILED;
2036 		} else {
2037 			add_head->state = I40E_FILTER_ACTIVE;
2038 			retval++;
2039 		}
2040 
2041 		add_head = i40e_next_filter(add_head);
2042 		if (!add_head)
2043 			break;
2044 	}
2045 
2046 	return retval;
2047 }
2048 
2049 /**
2050  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2051  * @vsi: ptr to the VSI
2052  * @vsi_name: name to display in messages
2053  * @list: the list of filters to send to firmware
2054  * @num_del: the number of filters to delete
2055  * @retval: Set to -EIO on failure to delete
2056  *
2057  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2058  * *retval instead of a return value so that success does not force ret_val to
2059  * be set to 0. This ensures that a sequence of calls to this function
2060  * preserve the previous value of *retval on successful delete.
2061  */
2062 static
2063 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2064 			  struct i40e_aqc_remove_macvlan_element_data *list,
2065 			  int num_del, int *retval)
2066 {
2067 	struct i40e_hw *hw = &vsi->back->hw;
2068 	i40e_status aq_ret;
2069 	int aq_err;
2070 
2071 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2072 	aq_err = hw->aq.asq_last_status;
2073 
2074 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2075 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2076 		*retval = -EIO;
2077 		dev_info(&vsi->back->pdev->dev,
2078 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2079 			 vsi_name, i40e_stat_str(hw, aq_ret),
2080 			 i40e_aq_str(hw, aq_err));
2081 	}
2082 }
2083 
2084 /**
2085  * i40e_aqc_add_filters - Request firmware to add a set of filters
2086  * @vsi: ptr to the VSI
2087  * @vsi_name: name to display in messages
2088  * @list: the list of filters to send to firmware
2089  * @add_head: Position in the add hlist
2090  * @num_add: the number of filters to add
2091  *
2092  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2093  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2094  * space for more filters.
2095  */
2096 static
2097 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2098 			  struct i40e_aqc_add_macvlan_element_data *list,
2099 			  struct i40e_new_mac_filter *add_head,
2100 			  int num_add)
2101 {
2102 	struct i40e_hw *hw = &vsi->back->hw;
2103 	int aq_err, fcnt;
2104 
2105 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2106 	aq_err = hw->aq.asq_last_status;
2107 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2108 
2109 	if (fcnt != num_add) {
2110 		if (vsi->type == I40E_VSI_MAIN) {
2111 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2112 			dev_warn(&vsi->back->pdev->dev,
2113 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2114 				 i40e_aq_str(hw, aq_err), vsi_name);
2115 		} else if (vsi->type == I40E_VSI_SRIOV ||
2116 			   vsi->type == I40E_VSI_VMDQ1 ||
2117 			   vsi->type == I40E_VSI_VMDQ2) {
2118 			dev_warn(&vsi->back->pdev->dev,
2119 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2120 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2121 		} else {
2122 			dev_warn(&vsi->back->pdev->dev,
2123 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2124 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2125 		}
2126 	}
2127 }
2128 
2129 /**
2130  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2131  * @vsi: pointer to the VSI
2132  * @vsi_name: the VSI name
2133  * @f: filter data
2134  *
2135  * This function sets or clears the promiscuous broadcast flags for VLAN
2136  * filters in order to properly receive broadcast frames. Assumes that only
2137  * broadcast filters are passed.
2138  *
2139  * Returns status indicating success or failure;
2140  **/
2141 static i40e_status
2142 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2143 			  struct i40e_mac_filter *f)
2144 {
2145 	bool enable = f->state == I40E_FILTER_NEW;
2146 	struct i40e_hw *hw = &vsi->back->hw;
2147 	i40e_status aq_ret;
2148 
2149 	if (f->vlan == I40E_VLAN_ANY) {
2150 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2151 						   vsi->seid,
2152 						   enable,
2153 						   NULL);
2154 	} else {
2155 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2156 							    vsi->seid,
2157 							    enable,
2158 							    f->vlan,
2159 							    NULL);
2160 	}
2161 
2162 	if (aq_ret) {
2163 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2164 		dev_warn(&vsi->back->pdev->dev,
2165 			 "Error %s, forcing overflow promiscuous on %s\n",
2166 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2167 			 vsi_name);
2168 	}
2169 
2170 	return aq_ret;
2171 }
2172 
2173 /**
2174  * i40e_set_promiscuous - set promiscuous mode
2175  * @pf: board private structure
2176  * @promisc: promisc on or off
2177  *
2178  * There are different ways of setting promiscuous mode on a PF depending on
2179  * what state/environment we're in.  This identifies and sets it appropriately.
2180  * Returns 0 on success.
2181  **/
2182 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2183 {
2184 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2185 	struct i40e_hw *hw = &pf->hw;
2186 	i40e_status aq_ret;
2187 
2188 	if (vsi->type == I40E_VSI_MAIN &&
2189 	    pf->lan_veb != I40E_NO_VEB &&
2190 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2191 		/* set defport ON for Main VSI instead of true promisc
2192 		 * this way we will get all unicast/multicast and VLAN
2193 		 * promisc behavior but will not get VF or VMDq traffic
2194 		 * replicated on the Main VSI.
2195 		 */
2196 		if (promisc)
2197 			aq_ret = i40e_aq_set_default_vsi(hw,
2198 							 vsi->seid,
2199 							 NULL);
2200 		else
2201 			aq_ret = i40e_aq_clear_default_vsi(hw,
2202 							   vsi->seid,
2203 							   NULL);
2204 		if (aq_ret) {
2205 			dev_info(&pf->pdev->dev,
2206 				 "Set default VSI failed, err %s, aq_err %s\n",
2207 				 i40e_stat_str(hw, aq_ret),
2208 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2209 		}
2210 	} else {
2211 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2212 						  hw,
2213 						  vsi->seid,
2214 						  promisc, NULL,
2215 						  true);
2216 		if (aq_ret) {
2217 			dev_info(&pf->pdev->dev,
2218 				 "set unicast promisc failed, err %s, aq_err %s\n",
2219 				 i40e_stat_str(hw, aq_ret),
2220 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2221 		}
2222 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2223 						  hw,
2224 						  vsi->seid,
2225 						  promisc, NULL);
2226 		if (aq_ret) {
2227 			dev_info(&pf->pdev->dev,
2228 				 "set multicast promisc failed, err %s, aq_err %s\n",
2229 				 i40e_stat_str(hw, aq_ret),
2230 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2231 		}
2232 	}
2233 
2234 	if (!aq_ret)
2235 		pf->cur_promisc = promisc;
2236 
2237 	return aq_ret;
2238 }
2239 
2240 /**
2241  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2242  * @vsi: ptr to the VSI
2243  *
2244  * Push any outstanding VSI filter changes through the AdminQ.
2245  *
2246  * Returns 0 or error value
2247  **/
2248 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2249 {
2250 	struct hlist_head tmp_add_list, tmp_del_list;
2251 	struct i40e_mac_filter *f;
2252 	struct i40e_new_mac_filter *new, *add_head = NULL;
2253 	struct i40e_hw *hw = &vsi->back->hw;
2254 	bool old_overflow, new_overflow;
2255 	unsigned int failed_filters = 0;
2256 	unsigned int vlan_filters = 0;
2257 	char vsi_name[16] = "PF";
2258 	int filter_list_len = 0;
2259 	i40e_status aq_ret = 0;
2260 	u32 changed_flags = 0;
2261 	struct hlist_node *h;
2262 	struct i40e_pf *pf;
2263 	int num_add = 0;
2264 	int num_del = 0;
2265 	int retval = 0;
2266 	u16 cmd_flags;
2267 	int list_size;
2268 	int bkt;
2269 
2270 	/* empty array typed pointers, kcalloc later */
2271 	struct i40e_aqc_add_macvlan_element_data *add_list;
2272 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2273 
2274 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2275 		usleep_range(1000, 2000);
2276 	pf = vsi->back;
2277 
2278 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2279 
2280 	if (vsi->netdev) {
2281 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2282 		vsi->current_netdev_flags = vsi->netdev->flags;
2283 	}
2284 
2285 	INIT_HLIST_HEAD(&tmp_add_list);
2286 	INIT_HLIST_HEAD(&tmp_del_list);
2287 
2288 	if (vsi->type == I40E_VSI_SRIOV)
2289 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2290 	else if (vsi->type != I40E_VSI_MAIN)
2291 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2292 
2293 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2294 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2295 
2296 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2297 		/* Create a list of filters to delete. */
2298 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2299 			if (f->state == I40E_FILTER_REMOVE) {
2300 				/* Move the element into temporary del_list */
2301 				hash_del(&f->hlist);
2302 				hlist_add_head(&f->hlist, &tmp_del_list);
2303 
2304 				/* Avoid counting removed filters */
2305 				continue;
2306 			}
2307 			if (f->state == I40E_FILTER_NEW) {
2308 				/* Create a temporary i40e_new_mac_filter */
2309 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2310 				if (!new)
2311 					goto err_no_memory_locked;
2312 
2313 				/* Store pointer to the real filter */
2314 				new->f = f;
2315 				new->state = f->state;
2316 
2317 				/* Add it to the hash list */
2318 				hlist_add_head(&new->hlist, &tmp_add_list);
2319 			}
2320 
2321 			/* Count the number of active (current and new) VLAN
2322 			 * filters we have now. Does not count filters which
2323 			 * are marked for deletion.
2324 			 */
2325 			if (f->vlan > 0)
2326 				vlan_filters++;
2327 		}
2328 
2329 		retval = i40e_correct_mac_vlan_filters(vsi,
2330 						       &tmp_add_list,
2331 						       &tmp_del_list,
2332 						       vlan_filters);
2333 		if (retval)
2334 			goto err_no_memory_locked;
2335 
2336 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2337 	}
2338 
2339 	/* Now process 'del_list' outside the lock */
2340 	if (!hlist_empty(&tmp_del_list)) {
2341 		filter_list_len = hw->aq.asq_buf_size /
2342 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2343 		list_size = filter_list_len *
2344 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2345 		del_list = kzalloc(list_size, GFP_ATOMIC);
2346 		if (!del_list)
2347 			goto err_no_memory;
2348 
2349 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2350 			cmd_flags = 0;
2351 
2352 			/* handle broadcast filters by updating the broadcast
2353 			 * promiscuous flag and release filter list.
2354 			 */
2355 			if (is_broadcast_ether_addr(f->macaddr)) {
2356 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2357 
2358 				hlist_del(&f->hlist);
2359 				kfree(f);
2360 				continue;
2361 			}
2362 
2363 			/* add to delete list */
2364 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2365 			if (f->vlan == I40E_VLAN_ANY) {
2366 				del_list[num_del].vlan_tag = 0;
2367 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2368 			} else {
2369 				del_list[num_del].vlan_tag =
2370 					cpu_to_le16((u16)(f->vlan));
2371 			}
2372 
2373 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2374 			del_list[num_del].flags = cmd_flags;
2375 			num_del++;
2376 
2377 			/* flush a full buffer */
2378 			if (num_del == filter_list_len) {
2379 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2380 						     num_del, &retval);
2381 				memset(del_list, 0, list_size);
2382 				num_del = 0;
2383 			}
2384 			/* Release memory for MAC filter entries which were
2385 			 * synced up with HW.
2386 			 */
2387 			hlist_del(&f->hlist);
2388 			kfree(f);
2389 		}
2390 
2391 		if (num_del) {
2392 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2393 					     num_del, &retval);
2394 		}
2395 
2396 		kfree(del_list);
2397 		del_list = NULL;
2398 	}
2399 
2400 	if (!hlist_empty(&tmp_add_list)) {
2401 		/* Do all the adds now. */
2402 		filter_list_len = hw->aq.asq_buf_size /
2403 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2404 		list_size = filter_list_len *
2405 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2406 		add_list = kzalloc(list_size, GFP_ATOMIC);
2407 		if (!add_list)
2408 			goto err_no_memory;
2409 
2410 		num_add = 0;
2411 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2412 			/* handle broadcast filters by updating the broadcast
2413 			 * promiscuous flag instead of adding a MAC filter.
2414 			 */
2415 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2416 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2417 							      new->f))
2418 					new->state = I40E_FILTER_FAILED;
2419 				else
2420 					new->state = I40E_FILTER_ACTIVE;
2421 				continue;
2422 			}
2423 
2424 			/* add to add array */
2425 			if (num_add == 0)
2426 				add_head = new;
2427 			cmd_flags = 0;
2428 			ether_addr_copy(add_list[num_add].mac_addr,
2429 					new->f->macaddr);
2430 			if (new->f->vlan == I40E_VLAN_ANY) {
2431 				add_list[num_add].vlan_tag = 0;
2432 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2433 			} else {
2434 				add_list[num_add].vlan_tag =
2435 					cpu_to_le16((u16)(new->f->vlan));
2436 			}
2437 			add_list[num_add].queue_number = 0;
2438 			/* set invalid match method for later detection */
2439 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2440 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2441 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2442 			num_add++;
2443 
2444 			/* flush a full buffer */
2445 			if (num_add == filter_list_len) {
2446 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2447 						     add_head, num_add);
2448 				memset(add_list, 0, list_size);
2449 				num_add = 0;
2450 			}
2451 		}
2452 		if (num_add) {
2453 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2454 					     num_add);
2455 		}
2456 		/* Now move all of the filters from the temp add list back to
2457 		 * the VSI's list.
2458 		 */
2459 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2460 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2461 			/* Only update the state if we're still NEW */
2462 			if (new->f->state == I40E_FILTER_NEW)
2463 				new->f->state = new->state;
2464 			hlist_del(&new->hlist);
2465 			kfree(new);
2466 		}
2467 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2468 		kfree(add_list);
2469 		add_list = NULL;
2470 	}
2471 
2472 	/* Determine the number of active and failed filters. */
2473 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2474 	vsi->active_filters = 0;
2475 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2476 		if (f->state == I40E_FILTER_ACTIVE)
2477 			vsi->active_filters++;
2478 		else if (f->state == I40E_FILTER_FAILED)
2479 			failed_filters++;
2480 	}
2481 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2482 
2483 	/* Check if we are able to exit overflow promiscuous mode. We can
2484 	 * safely exit if we didn't just enter, we no longer have any failed
2485 	 * filters, and we have reduced filters below the threshold value.
2486 	 */
2487 	if (old_overflow && !failed_filters &&
2488 	    vsi->active_filters < vsi->promisc_threshold) {
2489 		dev_info(&pf->pdev->dev,
2490 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2491 			 vsi_name);
2492 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2493 		vsi->promisc_threshold = 0;
2494 	}
2495 
2496 	/* if the VF is not trusted do not do promisc */
2497 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2498 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2499 		goto out;
2500 	}
2501 
2502 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2503 
2504 	/* If we are entering overflow promiscuous, we need to calculate a new
2505 	 * threshold for when we are safe to exit
2506 	 */
2507 	if (!old_overflow && new_overflow)
2508 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2509 
2510 	/* check for changes in promiscuous modes */
2511 	if (changed_flags & IFF_ALLMULTI) {
2512 		bool cur_multipromisc;
2513 
2514 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2515 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2516 							       vsi->seid,
2517 							       cur_multipromisc,
2518 							       NULL);
2519 		if (aq_ret) {
2520 			retval = i40e_aq_rc_to_posix(aq_ret,
2521 						     hw->aq.asq_last_status);
2522 			dev_info(&pf->pdev->dev,
2523 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2524 				 vsi_name,
2525 				 i40e_stat_str(hw, aq_ret),
2526 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2527 		}
2528 	}
2529 
2530 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2531 		bool cur_promisc;
2532 
2533 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2534 			       new_overflow);
2535 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2536 		if (aq_ret) {
2537 			retval = i40e_aq_rc_to_posix(aq_ret,
2538 						     hw->aq.asq_last_status);
2539 			dev_info(&pf->pdev->dev,
2540 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2541 				 cur_promisc ? "on" : "off",
2542 				 vsi_name,
2543 				 i40e_stat_str(hw, aq_ret),
2544 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2545 		}
2546 	}
2547 out:
2548 	/* if something went wrong then set the changed flag so we try again */
2549 	if (retval)
2550 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2551 
2552 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2553 	return retval;
2554 
2555 err_no_memory:
2556 	/* Restore elements on the temporary add and delete lists */
2557 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2558 err_no_memory_locked:
2559 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2560 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2561 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2562 
2563 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2564 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2565 	return -ENOMEM;
2566 }
2567 
2568 /**
2569  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2570  * @pf: board private structure
2571  **/
2572 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2573 {
2574 	int v;
2575 
2576 	if (!pf)
2577 		return;
2578 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2579 		return;
2580 
2581 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2582 		if (pf->vsi[v] &&
2583 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2584 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2585 
2586 			if (ret) {
2587 				/* come back and try again later */
2588 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2589 					pf->state);
2590 				break;
2591 			}
2592 		}
2593 	}
2594 }
2595 
2596 /**
2597  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2598  * @vsi: the vsi
2599  **/
2600 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2601 {
2602 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2603 		return I40E_RXBUFFER_2048;
2604 	else
2605 		return I40E_RXBUFFER_3072;
2606 }
2607 
2608 /**
2609  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2610  * @netdev: network interface device structure
2611  * @new_mtu: new value for maximum frame size
2612  *
2613  * Returns 0 on success, negative on failure
2614  **/
2615 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2616 {
2617 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2618 	struct i40e_vsi *vsi = np->vsi;
2619 	struct i40e_pf *pf = vsi->back;
2620 
2621 	if (i40e_enabled_xdp_vsi(vsi)) {
2622 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2623 
2624 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2625 			return -EINVAL;
2626 	}
2627 
2628 	netdev_info(netdev, "changing MTU from %d to %d\n",
2629 		    netdev->mtu, new_mtu);
2630 	netdev->mtu = new_mtu;
2631 	if (netif_running(netdev))
2632 		i40e_vsi_reinit_locked(vsi);
2633 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2634 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2635 	return 0;
2636 }
2637 
2638 /**
2639  * i40e_ioctl - Access the hwtstamp interface
2640  * @netdev: network interface device structure
2641  * @ifr: interface request data
2642  * @cmd: ioctl command
2643  **/
2644 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2645 {
2646 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2647 	struct i40e_pf *pf = np->vsi->back;
2648 
2649 	switch (cmd) {
2650 	case SIOCGHWTSTAMP:
2651 		return i40e_ptp_get_ts_config(pf, ifr);
2652 	case SIOCSHWTSTAMP:
2653 		return i40e_ptp_set_ts_config(pf, ifr);
2654 	default:
2655 		return -EOPNOTSUPP;
2656 	}
2657 }
2658 
2659 /**
2660  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2661  * @vsi: the vsi being adjusted
2662  **/
2663 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2664 {
2665 	struct i40e_vsi_context ctxt;
2666 	i40e_status ret;
2667 
2668 	/* Don't modify stripping options if a port VLAN is active */
2669 	if (vsi->info.pvid)
2670 		return;
2671 
2672 	if ((vsi->info.valid_sections &
2673 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2674 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2675 		return;  /* already enabled */
2676 
2677 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2678 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2679 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2680 
2681 	ctxt.seid = vsi->seid;
2682 	ctxt.info = vsi->info;
2683 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2684 	if (ret) {
2685 		dev_info(&vsi->back->pdev->dev,
2686 			 "update vlan stripping failed, err %s aq_err %s\n",
2687 			 i40e_stat_str(&vsi->back->hw, ret),
2688 			 i40e_aq_str(&vsi->back->hw,
2689 				     vsi->back->hw.aq.asq_last_status));
2690 	}
2691 }
2692 
2693 /**
2694  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2695  * @vsi: the vsi being adjusted
2696  **/
2697 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2698 {
2699 	struct i40e_vsi_context ctxt;
2700 	i40e_status ret;
2701 
2702 	/* Don't modify stripping options if a port VLAN is active */
2703 	if (vsi->info.pvid)
2704 		return;
2705 
2706 	if ((vsi->info.valid_sections &
2707 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2708 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2709 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2710 		return;  /* already disabled */
2711 
2712 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2713 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2714 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2715 
2716 	ctxt.seid = vsi->seid;
2717 	ctxt.info = vsi->info;
2718 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2719 	if (ret) {
2720 		dev_info(&vsi->back->pdev->dev,
2721 			 "update vlan stripping failed, err %s aq_err %s\n",
2722 			 i40e_stat_str(&vsi->back->hw, ret),
2723 			 i40e_aq_str(&vsi->back->hw,
2724 				     vsi->back->hw.aq.asq_last_status));
2725 	}
2726 }
2727 
2728 /**
2729  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2730  * @vsi: the vsi being configured
2731  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2732  *
2733  * This is a helper function for adding a new MAC/VLAN filter with the
2734  * specified VLAN for each existing MAC address already in the hash table.
2735  * This function does *not* perform any accounting to update filters based on
2736  * VLAN mode.
2737  *
2738  * NOTE: this function expects to be called while under the
2739  * mac_filter_hash_lock
2740  **/
2741 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2742 {
2743 	struct i40e_mac_filter *f, *add_f;
2744 	struct hlist_node *h;
2745 	int bkt;
2746 
2747 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2748 		if (f->state == I40E_FILTER_REMOVE)
2749 			continue;
2750 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2751 		if (!add_f) {
2752 			dev_info(&vsi->back->pdev->dev,
2753 				 "Could not add vlan filter %d for %pM\n",
2754 				 vid, f->macaddr);
2755 			return -ENOMEM;
2756 		}
2757 	}
2758 
2759 	return 0;
2760 }
2761 
2762 /**
2763  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2764  * @vsi: the VSI being configured
2765  * @vid: VLAN id to be added
2766  **/
2767 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2768 {
2769 	int err;
2770 
2771 	if (vsi->info.pvid)
2772 		return -EINVAL;
2773 
2774 	/* The network stack will attempt to add VID=0, with the intention to
2775 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2776 	 * these packets by default when configured to receive untagged
2777 	 * packets, so we don't need to add a filter for this case.
2778 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2779 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2780 	 * Thus, we do not want to actually add a filter for VID=0
2781 	 */
2782 	if (!vid)
2783 		return 0;
2784 
2785 	/* Locked once because all functions invoked below iterates list*/
2786 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2787 	err = i40e_add_vlan_all_mac(vsi, vid);
2788 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2789 	if (err)
2790 		return err;
2791 
2792 	/* schedule our worker thread which will take care of
2793 	 * applying the new filter changes
2794 	 */
2795 	i40e_service_event_schedule(vsi->back);
2796 	return 0;
2797 }
2798 
2799 /**
2800  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2801  * @vsi: the vsi being configured
2802  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2803  *
2804  * This function should be used to remove all VLAN filters which match the
2805  * given VID. It does not schedule the service event and does not take the
2806  * mac_filter_hash_lock so it may be combined with other operations under
2807  * a single invocation of the mac_filter_hash_lock.
2808  *
2809  * NOTE: this function expects to be called while under the
2810  * mac_filter_hash_lock
2811  */
2812 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2813 {
2814 	struct i40e_mac_filter *f;
2815 	struct hlist_node *h;
2816 	int bkt;
2817 
2818 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2819 		if (f->vlan == vid)
2820 			__i40e_del_filter(vsi, f);
2821 	}
2822 }
2823 
2824 /**
2825  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2826  * @vsi: the VSI being configured
2827  * @vid: VLAN id to be removed
2828  **/
2829 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2830 {
2831 	if (!vid || vsi->info.pvid)
2832 		return;
2833 
2834 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2835 	i40e_rm_vlan_all_mac(vsi, vid);
2836 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2837 
2838 	/* schedule our worker thread which will take care of
2839 	 * applying the new filter changes
2840 	 */
2841 	i40e_service_event_schedule(vsi->back);
2842 }
2843 
2844 /**
2845  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2846  * @netdev: network interface to be adjusted
2847  * @proto: unused protocol value
2848  * @vid: vlan id to be added
2849  *
2850  * net_device_ops implementation for adding vlan ids
2851  **/
2852 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2853 				__always_unused __be16 proto, u16 vid)
2854 {
2855 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2856 	struct i40e_vsi *vsi = np->vsi;
2857 	int ret = 0;
2858 
2859 	if (vid >= VLAN_N_VID)
2860 		return -EINVAL;
2861 
2862 	ret = i40e_vsi_add_vlan(vsi, vid);
2863 	if (!ret)
2864 		set_bit(vid, vsi->active_vlans);
2865 
2866 	return ret;
2867 }
2868 
2869 /**
2870  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2871  * @netdev: network interface to be adjusted
2872  * @proto: unused protocol value
2873  * @vid: vlan id to be added
2874  **/
2875 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2876 				    __always_unused __be16 proto, u16 vid)
2877 {
2878 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2879 	struct i40e_vsi *vsi = np->vsi;
2880 
2881 	if (vid >= VLAN_N_VID)
2882 		return;
2883 	set_bit(vid, vsi->active_vlans);
2884 }
2885 
2886 /**
2887  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2888  * @netdev: network interface to be adjusted
2889  * @proto: unused protocol value
2890  * @vid: vlan id to be removed
2891  *
2892  * net_device_ops implementation for removing vlan ids
2893  **/
2894 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2895 				 __always_unused __be16 proto, u16 vid)
2896 {
2897 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2898 	struct i40e_vsi *vsi = np->vsi;
2899 
2900 	/* return code is ignored as there is nothing a user
2901 	 * can do about failure to remove and a log message was
2902 	 * already printed from the other function
2903 	 */
2904 	i40e_vsi_kill_vlan(vsi, vid);
2905 
2906 	clear_bit(vid, vsi->active_vlans);
2907 
2908 	return 0;
2909 }
2910 
2911 /**
2912  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2913  * @vsi: the vsi being brought back up
2914  **/
2915 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2916 {
2917 	u16 vid;
2918 
2919 	if (!vsi->netdev)
2920 		return;
2921 
2922 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2923 		i40e_vlan_stripping_enable(vsi);
2924 	else
2925 		i40e_vlan_stripping_disable(vsi);
2926 
2927 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2928 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2929 					vid);
2930 }
2931 
2932 /**
2933  * i40e_vsi_add_pvid - Add pvid for the VSI
2934  * @vsi: the vsi being adjusted
2935  * @vid: the vlan id to set as a PVID
2936  **/
2937 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2938 {
2939 	struct i40e_vsi_context ctxt;
2940 	i40e_status ret;
2941 
2942 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2943 	vsi->info.pvid = cpu_to_le16(vid);
2944 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2945 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2946 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2947 
2948 	ctxt.seid = vsi->seid;
2949 	ctxt.info = vsi->info;
2950 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2951 	if (ret) {
2952 		dev_info(&vsi->back->pdev->dev,
2953 			 "add pvid failed, err %s aq_err %s\n",
2954 			 i40e_stat_str(&vsi->back->hw, ret),
2955 			 i40e_aq_str(&vsi->back->hw,
2956 				     vsi->back->hw.aq.asq_last_status));
2957 		return -ENOENT;
2958 	}
2959 
2960 	return 0;
2961 }
2962 
2963 /**
2964  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2965  * @vsi: the vsi being adjusted
2966  *
2967  * Just use the vlan_rx_register() service to put it back to normal
2968  **/
2969 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2970 {
2971 	i40e_vlan_stripping_disable(vsi);
2972 
2973 	vsi->info.pvid = 0;
2974 }
2975 
2976 /**
2977  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2978  * @vsi: ptr to the VSI
2979  *
2980  * If this function returns with an error, then it's possible one or
2981  * more of the rings is populated (while the rest are not).  It is the
2982  * callers duty to clean those orphaned rings.
2983  *
2984  * Return 0 on success, negative on failure
2985  **/
2986 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2987 {
2988 	int i, err = 0;
2989 
2990 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2991 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2992 
2993 	if (!i40e_enabled_xdp_vsi(vsi))
2994 		return err;
2995 
2996 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2997 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2998 
2999 	return err;
3000 }
3001 
3002 /**
3003  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3004  * @vsi: ptr to the VSI
3005  *
3006  * Free VSI's transmit software resources
3007  **/
3008 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3009 {
3010 	int i;
3011 
3012 	if (vsi->tx_rings) {
3013 		for (i = 0; i < vsi->num_queue_pairs; i++)
3014 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3015 				i40e_free_tx_resources(vsi->tx_rings[i]);
3016 	}
3017 
3018 	if (vsi->xdp_rings) {
3019 		for (i = 0; i < vsi->num_queue_pairs; i++)
3020 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3021 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3022 	}
3023 }
3024 
3025 /**
3026  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3027  * @vsi: ptr to the VSI
3028  *
3029  * If this function returns with an error, then it's possible one or
3030  * more of the rings is populated (while the rest are not).  It is the
3031  * callers duty to clean those orphaned rings.
3032  *
3033  * Return 0 on success, negative on failure
3034  **/
3035 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3036 {
3037 	int i, err = 0;
3038 
3039 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3040 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3041 	return err;
3042 }
3043 
3044 /**
3045  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3046  * @vsi: ptr to the VSI
3047  *
3048  * Free all receive software resources
3049  **/
3050 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3051 {
3052 	int i;
3053 
3054 	if (!vsi->rx_rings)
3055 		return;
3056 
3057 	for (i = 0; i < vsi->num_queue_pairs; i++)
3058 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3059 			i40e_free_rx_resources(vsi->rx_rings[i]);
3060 }
3061 
3062 /**
3063  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3064  * @ring: The Tx ring to configure
3065  *
3066  * This enables/disables XPS for a given Tx descriptor ring
3067  * based on the TCs enabled for the VSI that ring belongs to.
3068  **/
3069 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3070 {
3071 	int cpu;
3072 
3073 	if (!ring->q_vector || !ring->netdev || ring->ch)
3074 		return;
3075 
3076 	/* We only initialize XPS once, so as not to overwrite user settings */
3077 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3078 		return;
3079 
3080 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3081 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3082 			    ring->queue_index);
3083 }
3084 
3085 /**
3086  * i40e_xsk_umem - Retrieve the AF_XDP ZC if XDP and ZC is enabled
3087  * @ring: The Tx or Rx ring
3088  *
3089  * Returns the UMEM or NULL.
3090  **/
3091 static struct xdp_umem *i40e_xsk_umem(struct i40e_ring *ring)
3092 {
3093 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3094 	int qid = ring->queue_index;
3095 
3096 	if (ring_is_xdp(ring))
3097 		qid -= ring->vsi->alloc_queue_pairs;
3098 
3099 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3100 		return NULL;
3101 
3102 	return xdp_get_umem_from_qid(ring->vsi->netdev, qid);
3103 }
3104 
3105 /**
3106  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3107  * @ring: The Tx ring to configure
3108  *
3109  * Configure the Tx descriptor ring in the HMC context.
3110  **/
3111 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3112 {
3113 	struct i40e_vsi *vsi = ring->vsi;
3114 	u16 pf_q = vsi->base_queue + ring->queue_index;
3115 	struct i40e_hw *hw = &vsi->back->hw;
3116 	struct i40e_hmc_obj_txq tx_ctx;
3117 	i40e_status err = 0;
3118 	u32 qtx_ctl = 0;
3119 
3120 	if (ring_is_xdp(ring))
3121 		ring->xsk_umem = i40e_xsk_umem(ring);
3122 
3123 	/* some ATR related tx ring init */
3124 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3125 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3126 		ring->atr_count = 0;
3127 	} else {
3128 		ring->atr_sample_rate = 0;
3129 	}
3130 
3131 	/* configure XPS */
3132 	i40e_config_xps_tx_ring(ring);
3133 
3134 	/* clear the context structure first */
3135 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3136 
3137 	tx_ctx.new_context = 1;
3138 	tx_ctx.base = (ring->dma / 128);
3139 	tx_ctx.qlen = ring->count;
3140 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3141 					       I40E_FLAG_FD_ATR_ENABLED));
3142 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3143 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3144 	if (vsi->type != I40E_VSI_FDIR)
3145 		tx_ctx.head_wb_ena = 1;
3146 	tx_ctx.head_wb_addr = ring->dma +
3147 			      (ring->count * sizeof(struct i40e_tx_desc));
3148 
3149 	/* As part of VSI creation/update, FW allocates certain
3150 	 * Tx arbitration queue sets for each TC enabled for
3151 	 * the VSI. The FW returns the handles to these queue
3152 	 * sets as part of the response buffer to Add VSI,
3153 	 * Update VSI, etc. AQ commands. It is expected that
3154 	 * these queue set handles be associated with the Tx
3155 	 * queues by the driver as part of the TX queue context
3156 	 * initialization. This has to be done regardless of
3157 	 * DCB as by default everything is mapped to TC0.
3158 	 */
3159 
3160 	if (ring->ch)
3161 		tx_ctx.rdylist =
3162 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3163 
3164 	else
3165 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3166 
3167 	tx_ctx.rdylist_act = 0;
3168 
3169 	/* clear the context in the HMC */
3170 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3171 	if (err) {
3172 		dev_info(&vsi->back->pdev->dev,
3173 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3174 			 ring->queue_index, pf_q, err);
3175 		return -ENOMEM;
3176 	}
3177 
3178 	/* set the context in the HMC */
3179 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3180 	if (err) {
3181 		dev_info(&vsi->back->pdev->dev,
3182 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3183 			 ring->queue_index, pf_q, err);
3184 		return -ENOMEM;
3185 	}
3186 
3187 	/* Now associate this queue with this PCI function */
3188 	if (ring->ch) {
3189 		if (ring->ch->type == I40E_VSI_VMDQ2)
3190 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3191 		else
3192 			return -EINVAL;
3193 
3194 		qtx_ctl |= (ring->ch->vsi_number <<
3195 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3196 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3197 	} else {
3198 		if (vsi->type == I40E_VSI_VMDQ2) {
3199 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3200 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3201 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3202 		} else {
3203 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3204 		}
3205 	}
3206 
3207 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3208 		    I40E_QTX_CTL_PF_INDX_MASK);
3209 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3210 	i40e_flush(hw);
3211 
3212 	/* cache tail off for easier writes later */
3213 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3214 
3215 	return 0;
3216 }
3217 
3218 /**
3219  * i40e_configure_rx_ring - Configure a receive ring context
3220  * @ring: The Rx ring to configure
3221  *
3222  * Configure the Rx descriptor ring in the HMC context.
3223  **/
3224 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3225 {
3226 	struct i40e_vsi *vsi = ring->vsi;
3227 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3228 	u16 pf_q = vsi->base_queue + ring->queue_index;
3229 	struct i40e_hw *hw = &vsi->back->hw;
3230 	struct i40e_hmc_obj_rxq rx_ctx;
3231 	i40e_status err = 0;
3232 	bool ok;
3233 	int ret;
3234 
3235 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3236 
3237 	/* clear the context structure first */
3238 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3239 
3240 	if (ring->vsi->type == I40E_VSI_MAIN)
3241 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3242 
3243 	ring->xsk_umem = i40e_xsk_umem(ring);
3244 	if (ring->xsk_umem) {
3245 		ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
3246 				   XDP_PACKET_HEADROOM;
3247 		/* For AF_XDP ZC, we disallow packets to span on
3248 		 * multiple buffers, thus letting us skip that
3249 		 * handling in the fast-path.
3250 		 */
3251 		chain_len = 1;
3252 		ring->zca.free = i40e_zca_free;
3253 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3254 						 MEM_TYPE_ZERO_COPY,
3255 						 &ring->zca);
3256 		if (ret)
3257 			return ret;
3258 		dev_info(&vsi->back->pdev->dev,
3259 			 "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
3260 			 ring->queue_index);
3261 
3262 	} else {
3263 		ring->rx_buf_len = vsi->rx_buf_len;
3264 		if (ring->vsi->type == I40E_VSI_MAIN) {
3265 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3266 							 MEM_TYPE_PAGE_SHARED,
3267 							 NULL);
3268 			if (ret)
3269 				return ret;
3270 		}
3271 	}
3272 
3273 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3274 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3275 
3276 	rx_ctx.base = (ring->dma / 128);
3277 	rx_ctx.qlen = ring->count;
3278 
3279 	/* use 32 byte descriptors */
3280 	rx_ctx.dsize = 1;
3281 
3282 	/* descriptor type is always zero
3283 	 * rx_ctx.dtype = 0;
3284 	 */
3285 	rx_ctx.hsplit_0 = 0;
3286 
3287 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3288 	if (hw->revision_id == 0)
3289 		rx_ctx.lrxqthresh = 0;
3290 	else
3291 		rx_ctx.lrxqthresh = 1;
3292 	rx_ctx.crcstrip = 1;
3293 	rx_ctx.l2tsel = 1;
3294 	/* this controls whether VLAN is stripped from inner headers */
3295 	rx_ctx.showiv = 0;
3296 	/* set the prefena field to 1 because the manual says to */
3297 	rx_ctx.prefena = 1;
3298 
3299 	/* clear the context in the HMC */
3300 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3301 	if (err) {
3302 		dev_info(&vsi->back->pdev->dev,
3303 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3304 			 ring->queue_index, pf_q, err);
3305 		return -ENOMEM;
3306 	}
3307 
3308 	/* set the context in the HMC */
3309 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3310 	if (err) {
3311 		dev_info(&vsi->back->pdev->dev,
3312 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3313 			 ring->queue_index, pf_q, err);
3314 		return -ENOMEM;
3315 	}
3316 
3317 	/* configure Rx buffer alignment */
3318 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3319 		clear_ring_build_skb_enabled(ring);
3320 	else
3321 		set_ring_build_skb_enabled(ring);
3322 
3323 	/* cache tail for quicker writes, and clear the reg before use */
3324 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3325 	writel(0, ring->tail);
3326 
3327 	ok = ring->xsk_umem ?
3328 	     i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
3329 	     !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3330 	if (!ok) {
3331 		/* Log this in case the user has forgotten to give the kernel
3332 		 * any buffers, even later in the application.
3333 		 */
3334 		dev_info(&vsi->back->pdev->dev,
3335 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3336 			 ring->xsk_umem ? "UMEM enabled " : "",
3337 			 ring->queue_index, pf_q);
3338 	}
3339 
3340 	return 0;
3341 }
3342 
3343 /**
3344  * i40e_vsi_configure_tx - Configure the VSI for Tx
3345  * @vsi: VSI structure describing this set of rings and resources
3346  *
3347  * Configure the Tx VSI for operation.
3348  **/
3349 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3350 {
3351 	int err = 0;
3352 	u16 i;
3353 
3354 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3355 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3356 
3357 	if (!i40e_enabled_xdp_vsi(vsi))
3358 		return err;
3359 
3360 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3361 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3362 
3363 	return err;
3364 }
3365 
3366 /**
3367  * i40e_vsi_configure_rx - Configure the VSI for Rx
3368  * @vsi: the VSI being configured
3369  *
3370  * Configure the Rx VSI for operation.
3371  **/
3372 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3373 {
3374 	int err = 0;
3375 	u16 i;
3376 
3377 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3378 		vsi->max_frame = I40E_MAX_RXBUFFER;
3379 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3380 #if (PAGE_SIZE < 8192)
3381 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3382 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3383 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3384 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3385 #endif
3386 	} else {
3387 		vsi->max_frame = I40E_MAX_RXBUFFER;
3388 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3389 						       I40E_RXBUFFER_2048;
3390 	}
3391 
3392 	/* set up individual rings */
3393 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3394 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3395 
3396 	return err;
3397 }
3398 
3399 /**
3400  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3401  * @vsi: ptr to the VSI
3402  **/
3403 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3404 {
3405 	struct i40e_ring *tx_ring, *rx_ring;
3406 	u16 qoffset, qcount;
3407 	int i, n;
3408 
3409 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3410 		/* Reset the TC information */
3411 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3412 			rx_ring = vsi->rx_rings[i];
3413 			tx_ring = vsi->tx_rings[i];
3414 			rx_ring->dcb_tc = 0;
3415 			tx_ring->dcb_tc = 0;
3416 		}
3417 		return;
3418 	}
3419 
3420 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3421 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3422 			continue;
3423 
3424 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3425 		qcount = vsi->tc_config.tc_info[n].qcount;
3426 		for (i = qoffset; i < (qoffset + qcount); i++) {
3427 			rx_ring = vsi->rx_rings[i];
3428 			tx_ring = vsi->tx_rings[i];
3429 			rx_ring->dcb_tc = n;
3430 			tx_ring->dcb_tc = n;
3431 		}
3432 	}
3433 }
3434 
3435 /**
3436  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3437  * @vsi: ptr to the VSI
3438  **/
3439 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3440 {
3441 	if (vsi->netdev)
3442 		i40e_set_rx_mode(vsi->netdev);
3443 }
3444 
3445 /**
3446  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3447  * @vsi: Pointer to the targeted VSI
3448  *
3449  * This function replays the hlist on the hw where all the SB Flow Director
3450  * filters were saved.
3451  **/
3452 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3453 {
3454 	struct i40e_fdir_filter *filter;
3455 	struct i40e_pf *pf = vsi->back;
3456 	struct hlist_node *node;
3457 
3458 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3459 		return;
3460 
3461 	/* Reset FDir counters as we're replaying all existing filters */
3462 	pf->fd_tcp4_filter_cnt = 0;
3463 	pf->fd_udp4_filter_cnt = 0;
3464 	pf->fd_sctp4_filter_cnt = 0;
3465 	pf->fd_ip4_filter_cnt = 0;
3466 
3467 	hlist_for_each_entry_safe(filter, node,
3468 				  &pf->fdir_filter_list, fdir_node) {
3469 		i40e_add_del_fdir(vsi, filter, true);
3470 	}
3471 }
3472 
3473 /**
3474  * i40e_vsi_configure - Set up the VSI for action
3475  * @vsi: the VSI being configured
3476  **/
3477 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3478 {
3479 	int err;
3480 
3481 	i40e_set_vsi_rx_mode(vsi);
3482 	i40e_restore_vlan(vsi);
3483 	i40e_vsi_config_dcb_rings(vsi);
3484 	err = i40e_vsi_configure_tx(vsi);
3485 	if (!err)
3486 		err = i40e_vsi_configure_rx(vsi);
3487 
3488 	return err;
3489 }
3490 
3491 /**
3492  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3493  * @vsi: the VSI being configured
3494  **/
3495 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3496 {
3497 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3498 	struct i40e_pf *pf = vsi->back;
3499 	struct i40e_hw *hw = &pf->hw;
3500 	u16 vector;
3501 	int i, q;
3502 	u32 qp;
3503 
3504 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3505 	 * and PFINT_LNKLSTn registers, e.g.:
3506 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3507 	 */
3508 	qp = vsi->base_queue;
3509 	vector = vsi->base_vector;
3510 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3511 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3512 
3513 		q_vector->rx.next_update = jiffies + 1;
3514 		q_vector->rx.target_itr =
3515 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3516 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3517 		     q_vector->rx.target_itr);
3518 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3519 
3520 		q_vector->tx.next_update = jiffies + 1;
3521 		q_vector->tx.target_itr =
3522 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3523 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3524 		     q_vector->tx.target_itr);
3525 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3526 
3527 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3528 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3529 
3530 		/* Linked list for the queuepairs assigned to this vector */
3531 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3532 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3533 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3534 			u32 val;
3535 
3536 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3537 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3538 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3539 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3540 			      (I40E_QUEUE_TYPE_TX <<
3541 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3542 
3543 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3544 
3545 			if (has_xdp) {
3546 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3547 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3548 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3549 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3550 				      (I40E_QUEUE_TYPE_TX <<
3551 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3552 
3553 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3554 			}
3555 
3556 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3557 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3558 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3559 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3560 			      (I40E_QUEUE_TYPE_RX <<
3561 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3562 
3563 			/* Terminate the linked list */
3564 			if (q == (q_vector->num_ringpairs - 1))
3565 				val |= (I40E_QUEUE_END_OF_LIST <<
3566 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3567 
3568 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3569 			qp++;
3570 		}
3571 	}
3572 
3573 	i40e_flush(hw);
3574 }
3575 
3576 /**
3577  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3578  * @pf: pointer to private device data structure
3579  **/
3580 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3581 {
3582 	struct i40e_hw *hw = &pf->hw;
3583 	u32 val;
3584 
3585 	/* clear things first */
3586 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3587 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3588 
3589 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3590 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3591 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3592 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3593 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3594 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3595 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3596 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3597 
3598 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3599 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3600 
3601 	if (pf->flags & I40E_FLAG_PTP)
3602 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3603 
3604 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3605 
3606 	/* SW_ITR_IDX = 0, but don't change INTENA */
3607 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3608 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3609 
3610 	/* OTHER_ITR_IDX = 0 */
3611 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3612 }
3613 
3614 /**
3615  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3616  * @vsi: the VSI being configured
3617  **/
3618 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3619 {
3620 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3621 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3622 	struct i40e_pf *pf = vsi->back;
3623 	struct i40e_hw *hw = &pf->hw;
3624 	u32 val;
3625 
3626 	/* set the ITR configuration */
3627 	q_vector->rx.next_update = jiffies + 1;
3628 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3629 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3630 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3631 	q_vector->tx.next_update = jiffies + 1;
3632 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3633 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3634 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3635 
3636 	i40e_enable_misc_int_causes(pf);
3637 
3638 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3639 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3640 
3641 	/* Associate the queue pair to the vector and enable the queue int */
3642 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3643 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3644 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3645 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3646 
3647 	wr32(hw, I40E_QINT_RQCTL(0), val);
3648 
3649 	if (i40e_enabled_xdp_vsi(vsi)) {
3650 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3651 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3652 		      (I40E_QUEUE_TYPE_TX
3653 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3654 
3655 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3656 	}
3657 
3658 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3659 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3660 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3661 
3662 	wr32(hw, I40E_QINT_TQCTL(0), val);
3663 	i40e_flush(hw);
3664 }
3665 
3666 /**
3667  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3668  * @pf: board private structure
3669  **/
3670 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3671 {
3672 	struct i40e_hw *hw = &pf->hw;
3673 
3674 	wr32(hw, I40E_PFINT_DYN_CTL0,
3675 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3676 	i40e_flush(hw);
3677 }
3678 
3679 /**
3680  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3681  * @pf: board private structure
3682  **/
3683 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3684 {
3685 	struct i40e_hw *hw = &pf->hw;
3686 	u32 val;
3687 
3688 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3689 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3690 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3691 
3692 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3693 	i40e_flush(hw);
3694 }
3695 
3696 /**
3697  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3698  * @irq: interrupt number
3699  * @data: pointer to a q_vector
3700  **/
3701 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3702 {
3703 	struct i40e_q_vector *q_vector = data;
3704 
3705 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3706 		return IRQ_HANDLED;
3707 
3708 	napi_schedule_irqoff(&q_vector->napi);
3709 
3710 	return IRQ_HANDLED;
3711 }
3712 
3713 /**
3714  * i40e_irq_affinity_notify - Callback for affinity changes
3715  * @notify: context as to what irq was changed
3716  * @mask: the new affinity mask
3717  *
3718  * This is a callback function used by the irq_set_affinity_notifier function
3719  * so that we may register to receive changes to the irq affinity masks.
3720  **/
3721 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3722 				     const cpumask_t *mask)
3723 {
3724 	struct i40e_q_vector *q_vector =
3725 		container_of(notify, struct i40e_q_vector, affinity_notify);
3726 
3727 	cpumask_copy(&q_vector->affinity_mask, mask);
3728 }
3729 
3730 /**
3731  * i40e_irq_affinity_release - Callback for affinity notifier release
3732  * @ref: internal core kernel usage
3733  *
3734  * This is a callback function used by the irq_set_affinity_notifier function
3735  * to inform the current notification subscriber that they will no longer
3736  * receive notifications.
3737  **/
3738 static void i40e_irq_affinity_release(struct kref *ref) {}
3739 
3740 /**
3741  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3742  * @vsi: the VSI being configured
3743  * @basename: name for the vector
3744  *
3745  * Allocates MSI-X vectors and requests interrupts from the kernel.
3746  **/
3747 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3748 {
3749 	int q_vectors = vsi->num_q_vectors;
3750 	struct i40e_pf *pf = vsi->back;
3751 	int base = vsi->base_vector;
3752 	int rx_int_idx = 0;
3753 	int tx_int_idx = 0;
3754 	int vector, err;
3755 	int irq_num;
3756 	int cpu;
3757 
3758 	for (vector = 0; vector < q_vectors; vector++) {
3759 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3760 
3761 		irq_num = pf->msix_entries[base + vector].vector;
3762 
3763 		if (q_vector->tx.ring && q_vector->rx.ring) {
3764 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3765 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3766 			tx_int_idx++;
3767 		} else if (q_vector->rx.ring) {
3768 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3769 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3770 		} else if (q_vector->tx.ring) {
3771 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3772 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3773 		} else {
3774 			/* skip this unused q_vector */
3775 			continue;
3776 		}
3777 		err = request_irq(irq_num,
3778 				  vsi->irq_handler,
3779 				  0,
3780 				  q_vector->name,
3781 				  q_vector);
3782 		if (err) {
3783 			dev_info(&pf->pdev->dev,
3784 				 "MSIX request_irq failed, error: %d\n", err);
3785 			goto free_queue_irqs;
3786 		}
3787 
3788 		/* register for affinity change notifications */
3789 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3790 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3791 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3792 		/* Spread affinity hints out across online CPUs.
3793 		 *
3794 		 * get_cpu_mask returns a static constant mask with
3795 		 * a permanent lifetime so it's ok to pass to
3796 		 * irq_set_affinity_hint without making a copy.
3797 		 */
3798 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3799 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3800 	}
3801 
3802 	vsi->irqs_ready = true;
3803 	return 0;
3804 
3805 free_queue_irqs:
3806 	while (vector) {
3807 		vector--;
3808 		irq_num = pf->msix_entries[base + vector].vector;
3809 		irq_set_affinity_notifier(irq_num, NULL);
3810 		irq_set_affinity_hint(irq_num, NULL);
3811 		free_irq(irq_num, &vsi->q_vectors[vector]);
3812 	}
3813 	return err;
3814 }
3815 
3816 /**
3817  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3818  * @vsi: the VSI being un-configured
3819  **/
3820 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3821 {
3822 	struct i40e_pf *pf = vsi->back;
3823 	struct i40e_hw *hw = &pf->hw;
3824 	int base = vsi->base_vector;
3825 	int i;
3826 
3827 	/* disable interrupt causation from each queue */
3828 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3829 		u32 val;
3830 
3831 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3832 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3833 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3834 
3835 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3836 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3837 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3838 
3839 		if (!i40e_enabled_xdp_vsi(vsi))
3840 			continue;
3841 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3842 	}
3843 
3844 	/* disable each interrupt */
3845 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3846 		for (i = vsi->base_vector;
3847 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3848 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3849 
3850 		i40e_flush(hw);
3851 		for (i = 0; i < vsi->num_q_vectors; i++)
3852 			synchronize_irq(pf->msix_entries[i + base].vector);
3853 	} else {
3854 		/* Legacy and MSI mode - this stops all interrupt handling */
3855 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3856 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3857 		i40e_flush(hw);
3858 		synchronize_irq(pf->pdev->irq);
3859 	}
3860 }
3861 
3862 /**
3863  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3864  * @vsi: the VSI being configured
3865  **/
3866 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3867 {
3868 	struct i40e_pf *pf = vsi->back;
3869 	int i;
3870 
3871 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3872 		for (i = 0; i < vsi->num_q_vectors; i++)
3873 			i40e_irq_dynamic_enable(vsi, i);
3874 	} else {
3875 		i40e_irq_dynamic_enable_icr0(pf);
3876 	}
3877 
3878 	i40e_flush(&pf->hw);
3879 	return 0;
3880 }
3881 
3882 /**
3883  * i40e_free_misc_vector - Free the vector that handles non-queue events
3884  * @pf: board private structure
3885  **/
3886 static void i40e_free_misc_vector(struct i40e_pf *pf)
3887 {
3888 	/* Disable ICR 0 */
3889 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3890 	i40e_flush(&pf->hw);
3891 
3892 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3893 		synchronize_irq(pf->msix_entries[0].vector);
3894 		free_irq(pf->msix_entries[0].vector, pf);
3895 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3896 	}
3897 }
3898 
3899 /**
3900  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3901  * @irq: interrupt number
3902  * @data: pointer to a q_vector
3903  *
3904  * This is the handler used for all MSI/Legacy interrupts, and deals
3905  * with both queue and non-queue interrupts.  This is also used in
3906  * MSIX mode to handle the non-queue interrupts.
3907  **/
3908 static irqreturn_t i40e_intr(int irq, void *data)
3909 {
3910 	struct i40e_pf *pf = (struct i40e_pf *)data;
3911 	struct i40e_hw *hw = &pf->hw;
3912 	irqreturn_t ret = IRQ_NONE;
3913 	u32 icr0, icr0_remaining;
3914 	u32 val, ena_mask;
3915 
3916 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3917 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3918 
3919 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3920 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3921 		goto enable_intr;
3922 
3923 	/* if interrupt but no bits showing, must be SWINT */
3924 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3925 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3926 		pf->sw_int_count++;
3927 
3928 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3929 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3930 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3931 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3932 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3933 	}
3934 
3935 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3936 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3937 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3938 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3939 
3940 		/* We do not have a way to disarm Queue causes while leaving
3941 		 * interrupt enabled for all other causes, ideally
3942 		 * interrupt should be disabled while we are in NAPI but
3943 		 * this is not a performance path and napi_schedule()
3944 		 * can deal with rescheduling.
3945 		 */
3946 		if (!test_bit(__I40E_DOWN, pf->state))
3947 			napi_schedule_irqoff(&q_vector->napi);
3948 	}
3949 
3950 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3951 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3952 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3953 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3954 	}
3955 
3956 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3957 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3958 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3959 	}
3960 
3961 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3962 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3963 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3964 	}
3965 
3966 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3967 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3968 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3969 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3970 		val = rd32(hw, I40E_GLGEN_RSTAT);
3971 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3972 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3973 		if (val == I40E_RESET_CORER) {
3974 			pf->corer_count++;
3975 		} else if (val == I40E_RESET_GLOBR) {
3976 			pf->globr_count++;
3977 		} else if (val == I40E_RESET_EMPR) {
3978 			pf->empr_count++;
3979 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3980 		}
3981 	}
3982 
3983 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3984 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3985 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3986 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3987 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3988 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3989 	}
3990 
3991 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3992 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3993 
3994 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
3995 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3996 			i40e_ptp_tx_hwtstamp(pf);
3997 		}
3998 	}
3999 
4000 	/* If a critical error is pending we have no choice but to reset the
4001 	 * device.
4002 	 * Report and mask out any remaining unexpected interrupts.
4003 	 */
4004 	icr0_remaining = icr0 & ena_mask;
4005 	if (icr0_remaining) {
4006 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4007 			 icr0_remaining);
4008 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4009 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4010 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4011 			dev_info(&pf->pdev->dev, "device will be reset\n");
4012 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4013 			i40e_service_event_schedule(pf);
4014 		}
4015 		ena_mask &= ~icr0_remaining;
4016 	}
4017 	ret = IRQ_HANDLED;
4018 
4019 enable_intr:
4020 	/* re-enable interrupt causes */
4021 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4022 	if (!test_bit(__I40E_DOWN, pf->state)) {
4023 		i40e_service_event_schedule(pf);
4024 		i40e_irq_dynamic_enable_icr0(pf);
4025 	}
4026 
4027 	return ret;
4028 }
4029 
4030 /**
4031  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4032  * @tx_ring:  tx ring to clean
4033  * @budget:   how many cleans we're allowed
4034  *
4035  * Returns true if there's any budget left (e.g. the clean is finished)
4036  **/
4037 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4038 {
4039 	struct i40e_vsi *vsi = tx_ring->vsi;
4040 	u16 i = tx_ring->next_to_clean;
4041 	struct i40e_tx_buffer *tx_buf;
4042 	struct i40e_tx_desc *tx_desc;
4043 
4044 	tx_buf = &tx_ring->tx_bi[i];
4045 	tx_desc = I40E_TX_DESC(tx_ring, i);
4046 	i -= tx_ring->count;
4047 
4048 	do {
4049 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4050 
4051 		/* if next_to_watch is not set then there is no work pending */
4052 		if (!eop_desc)
4053 			break;
4054 
4055 		/* prevent any other reads prior to eop_desc */
4056 		smp_rmb();
4057 
4058 		/* if the descriptor isn't done, no work yet to do */
4059 		if (!(eop_desc->cmd_type_offset_bsz &
4060 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4061 			break;
4062 
4063 		/* clear next_to_watch to prevent false hangs */
4064 		tx_buf->next_to_watch = NULL;
4065 
4066 		tx_desc->buffer_addr = 0;
4067 		tx_desc->cmd_type_offset_bsz = 0;
4068 		/* move past filter desc */
4069 		tx_buf++;
4070 		tx_desc++;
4071 		i++;
4072 		if (unlikely(!i)) {
4073 			i -= tx_ring->count;
4074 			tx_buf = tx_ring->tx_bi;
4075 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4076 		}
4077 		/* unmap skb header data */
4078 		dma_unmap_single(tx_ring->dev,
4079 				 dma_unmap_addr(tx_buf, dma),
4080 				 dma_unmap_len(tx_buf, len),
4081 				 DMA_TO_DEVICE);
4082 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4083 			kfree(tx_buf->raw_buf);
4084 
4085 		tx_buf->raw_buf = NULL;
4086 		tx_buf->tx_flags = 0;
4087 		tx_buf->next_to_watch = NULL;
4088 		dma_unmap_len_set(tx_buf, len, 0);
4089 		tx_desc->buffer_addr = 0;
4090 		tx_desc->cmd_type_offset_bsz = 0;
4091 
4092 		/* move us past the eop_desc for start of next FD desc */
4093 		tx_buf++;
4094 		tx_desc++;
4095 		i++;
4096 		if (unlikely(!i)) {
4097 			i -= tx_ring->count;
4098 			tx_buf = tx_ring->tx_bi;
4099 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4100 		}
4101 
4102 		/* update budget accounting */
4103 		budget--;
4104 	} while (likely(budget));
4105 
4106 	i += tx_ring->count;
4107 	tx_ring->next_to_clean = i;
4108 
4109 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4110 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4111 
4112 	return budget > 0;
4113 }
4114 
4115 /**
4116  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4117  * @irq: interrupt number
4118  * @data: pointer to a q_vector
4119  **/
4120 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4121 {
4122 	struct i40e_q_vector *q_vector = data;
4123 	struct i40e_vsi *vsi;
4124 
4125 	if (!q_vector->tx.ring)
4126 		return IRQ_HANDLED;
4127 
4128 	vsi = q_vector->tx.ring->vsi;
4129 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4130 
4131 	return IRQ_HANDLED;
4132 }
4133 
4134 /**
4135  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4136  * @vsi: the VSI being configured
4137  * @v_idx: vector index
4138  * @qp_idx: queue pair index
4139  **/
4140 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4141 {
4142 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4143 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4144 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4145 
4146 	tx_ring->q_vector = q_vector;
4147 	tx_ring->next = q_vector->tx.ring;
4148 	q_vector->tx.ring = tx_ring;
4149 	q_vector->tx.count++;
4150 
4151 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4152 	if (i40e_enabled_xdp_vsi(vsi)) {
4153 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4154 
4155 		xdp_ring->q_vector = q_vector;
4156 		xdp_ring->next = q_vector->tx.ring;
4157 		q_vector->tx.ring = xdp_ring;
4158 		q_vector->tx.count++;
4159 	}
4160 
4161 	rx_ring->q_vector = q_vector;
4162 	rx_ring->next = q_vector->rx.ring;
4163 	q_vector->rx.ring = rx_ring;
4164 	q_vector->rx.count++;
4165 }
4166 
4167 /**
4168  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4169  * @vsi: the VSI being configured
4170  *
4171  * This function maps descriptor rings to the queue-specific vectors
4172  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4173  * one vector per queue pair, but on a constrained vector budget, we
4174  * group the queue pairs as "efficiently" as possible.
4175  **/
4176 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4177 {
4178 	int qp_remaining = vsi->num_queue_pairs;
4179 	int q_vectors = vsi->num_q_vectors;
4180 	int num_ringpairs;
4181 	int v_start = 0;
4182 	int qp_idx = 0;
4183 
4184 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4185 	 * group them so there are multiple queues per vector.
4186 	 * It is also important to go through all the vectors available to be
4187 	 * sure that if we don't use all the vectors, that the remaining vectors
4188 	 * are cleared. This is especially important when decreasing the
4189 	 * number of queues in use.
4190 	 */
4191 	for (; v_start < q_vectors; v_start++) {
4192 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4193 
4194 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4195 
4196 		q_vector->num_ringpairs = num_ringpairs;
4197 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4198 
4199 		q_vector->rx.count = 0;
4200 		q_vector->tx.count = 0;
4201 		q_vector->rx.ring = NULL;
4202 		q_vector->tx.ring = NULL;
4203 
4204 		while (num_ringpairs--) {
4205 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4206 			qp_idx++;
4207 			qp_remaining--;
4208 		}
4209 	}
4210 }
4211 
4212 /**
4213  * i40e_vsi_request_irq - Request IRQ from the OS
4214  * @vsi: the VSI being configured
4215  * @basename: name for the vector
4216  **/
4217 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4218 {
4219 	struct i40e_pf *pf = vsi->back;
4220 	int err;
4221 
4222 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4223 		err = i40e_vsi_request_irq_msix(vsi, basename);
4224 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4225 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4226 				  pf->int_name, pf);
4227 	else
4228 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4229 				  pf->int_name, pf);
4230 
4231 	if (err)
4232 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4233 
4234 	return err;
4235 }
4236 
4237 #ifdef CONFIG_NET_POLL_CONTROLLER
4238 /**
4239  * i40e_netpoll - A Polling 'interrupt' handler
4240  * @netdev: network interface device structure
4241  *
4242  * This is used by netconsole to send skbs without having to re-enable
4243  * interrupts.  It's not called while the normal interrupt routine is executing.
4244  **/
4245 static void i40e_netpoll(struct net_device *netdev)
4246 {
4247 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4248 	struct i40e_vsi *vsi = np->vsi;
4249 	struct i40e_pf *pf = vsi->back;
4250 	int i;
4251 
4252 	/* if interface is down do nothing */
4253 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4254 		return;
4255 
4256 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4257 		for (i = 0; i < vsi->num_q_vectors; i++)
4258 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4259 	} else {
4260 		i40e_intr(pf->pdev->irq, netdev);
4261 	}
4262 }
4263 #endif
4264 
4265 #define I40E_QTX_ENA_WAIT_COUNT 50
4266 
4267 /**
4268  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4269  * @pf: the PF being configured
4270  * @pf_q: the PF queue
4271  * @enable: enable or disable state of the queue
4272  *
4273  * This routine will wait for the given Tx queue of the PF to reach the
4274  * enabled or disabled state.
4275  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4276  * multiple retries; else will return 0 in case of success.
4277  **/
4278 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4279 {
4280 	int i;
4281 	u32 tx_reg;
4282 
4283 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4284 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4285 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4286 			break;
4287 
4288 		usleep_range(10, 20);
4289 	}
4290 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4291 		return -ETIMEDOUT;
4292 
4293 	return 0;
4294 }
4295 
4296 /**
4297  * i40e_control_tx_q - Start or stop a particular Tx queue
4298  * @pf: the PF structure
4299  * @pf_q: the PF queue to configure
4300  * @enable: start or stop the queue
4301  *
4302  * This function enables or disables a single queue. Note that any delay
4303  * required after the operation is expected to be handled by the caller of
4304  * this function.
4305  **/
4306 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4307 {
4308 	struct i40e_hw *hw = &pf->hw;
4309 	u32 tx_reg;
4310 	int i;
4311 
4312 	/* warn the TX unit of coming changes */
4313 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4314 	if (!enable)
4315 		usleep_range(10, 20);
4316 
4317 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4318 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4319 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4320 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4321 			break;
4322 		usleep_range(1000, 2000);
4323 	}
4324 
4325 	/* Skip if the queue is already in the requested state */
4326 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4327 		return;
4328 
4329 	/* turn on/off the queue */
4330 	if (enable) {
4331 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4332 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4333 	} else {
4334 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4335 	}
4336 
4337 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4338 }
4339 
4340 /**
4341  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4342  * @seid: VSI SEID
4343  * @pf: the PF structure
4344  * @pf_q: the PF queue to configure
4345  * @is_xdp: true if the queue is used for XDP
4346  * @enable: start or stop the queue
4347  **/
4348 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4349 			   bool is_xdp, bool enable)
4350 {
4351 	int ret;
4352 
4353 	i40e_control_tx_q(pf, pf_q, enable);
4354 
4355 	/* wait for the change to finish */
4356 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4357 	if (ret) {
4358 		dev_info(&pf->pdev->dev,
4359 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4360 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4361 			 (enable ? "en" : "dis"));
4362 	}
4363 
4364 	return ret;
4365 }
4366 
4367 /**
4368  * i40e_vsi_control_tx - Start or stop a VSI's rings
4369  * @vsi: the VSI being configured
4370  * @enable: start or stop the rings
4371  **/
4372 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4373 {
4374 	struct i40e_pf *pf = vsi->back;
4375 	int i, pf_q, ret = 0;
4376 
4377 	pf_q = vsi->base_queue;
4378 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4379 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4380 					     pf_q,
4381 					     false /*is xdp*/, enable);
4382 		if (ret)
4383 			break;
4384 
4385 		if (!i40e_enabled_xdp_vsi(vsi))
4386 			continue;
4387 
4388 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4389 					     pf_q + vsi->alloc_queue_pairs,
4390 					     true /*is xdp*/, enable);
4391 		if (ret)
4392 			break;
4393 	}
4394 	return ret;
4395 }
4396 
4397 /**
4398  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4399  * @pf: the PF being configured
4400  * @pf_q: the PF queue
4401  * @enable: enable or disable state of the queue
4402  *
4403  * This routine will wait for the given Rx queue of the PF to reach the
4404  * enabled or disabled state.
4405  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4406  * multiple retries; else will return 0 in case of success.
4407  **/
4408 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4409 {
4410 	int i;
4411 	u32 rx_reg;
4412 
4413 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4414 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4415 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4416 			break;
4417 
4418 		usleep_range(10, 20);
4419 	}
4420 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4421 		return -ETIMEDOUT;
4422 
4423 	return 0;
4424 }
4425 
4426 /**
4427  * i40e_control_rx_q - Start or stop a particular Rx queue
4428  * @pf: the PF structure
4429  * @pf_q: the PF queue to configure
4430  * @enable: start or stop the queue
4431  *
4432  * This function enables or disables a single queue. Note that
4433  * any delay required after the operation is expected to be
4434  * handled by the caller of this function.
4435  **/
4436 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4437 {
4438 	struct i40e_hw *hw = &pf->hw;
4439 	u32 rx_reg;
4440 	int i;
4441 
4442 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4443 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4444 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4445 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4446 			break;
4447 		usleep_range(1000, 2000);
4448 	}
4449 
4450 	/* Skip if the queue is already in the requested state */
4451 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4452 		return;
4453 
4454 	/* turn on/off the queue */
4455 	if (enable)
4456 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4457 	else
4458 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4459 
4460 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4461 }
4462 
4463 /**
4464  * i40e_control_wait_rx_q
4465  * @pf: the PF structure
4466  * @pf_q: queue being configured
4467  * @enable: start or stop the rings
4468  *
4469  * This function enables or disables a single queue along with waiting
4470  * for the change to finish. The caller of this function should handle
4471  * the delays needed in the case of disabling queues.
4472  **/
4473 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4474 {
4475 	int ret = 0;
4476 
4477 	i40e_control_rx_q(pf, pf_q, enable);
4478 
4479 	/* wait for the change to finish */
4480 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4481 	if (ret)
4482 		return ret;
4483 
4484 	return ret;
4485 }
4486 
4487 /**
4488  * i40e_vsi_control_rx - Start or stop a VSI's rings
4489  * @vsi: the VSI being configured
4490  * @enable: start or stop the rings
4491  **/
4492 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4493 {
4494 	struct i40e_pf *pf = vsi->back;
4495 	int i, pf_q, ret = 0;
4496 
4497 	pf_q = vsi->base_queue;
4498 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4499 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4500 		if (ret) {
4501 			dev_info(&pf->pdev->dev,
4502 				 "VSI seid %d Rx ring %d %sable timeout\n",
4503 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4504 			break;
4505 		}
4506 	}
4507 
4508 	/* Due to HW errata, on Rx disable only, the register can indicate done
4509 	 * before it really is. Needs 50ms to be sure
4510 	 */
4511 	if (!enable)
4512 		mdelay(50);
4513 
4514 	return ret;
4515 }
4516 
4517 /**
4518  * i40e_vsi_start_rings - Start a VSI's rings
4519  * @vsi: the VSI being configured
4520  **/
4521 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4522 {
4523 	int ret = 0;
4524 
4525 	/* do rx first for enable and last for disable */
4526 	ret = i40e_vsi_control_rx(vsi, true);
4527 	if (ret)
4528 		return ret;
4529 	ret = i40e_vsi_control_tx(vsi, true);
4530 
4531 	return ret;
4532 }
4533 
4534 /**
4535  * i40e_vsi_stop_rings - Stop a VSI's rings
4536  * @vsi: the VSI being configured
4537  **/
4538 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4539 {
4540 	/* When port TX is suspended, don't wait */
4541 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4542 		return i40e_vsi_stop_rings_no_wait(vsi);
4543 
4544 	/* do rx first for enable and last for disable
4545 	 * Ignore return value, we need to shutdown whatever we can
4546 	 */
4547 	i40e_vsi_control_tx(vsi, false);
4548 	i40e_vsi_control_rx(vsi, false);
4549 }
4550 
4551 /**
4552  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4553  * @vsi: the VSI being shutdown
4554  *
4555  * This function stops all the rings for a VSI but does not delay to verify
4556  * that rings have been disabled. It is expected that the caller is shutting
4557  * down multiple VSIs at once and will delay together for all the VSIs after
4558  * initiating the shutdown. This is particularly useful for shutting down lots
4559  * of VFs together. Otherwise, a large delay can be incurred while configuring
4560  * each VSI in serial.
4561  **/
4562 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4563 {
4564 	struct i40e_pf *pf = vsi->back;
4565 	int i, pf_q;
4566 
4567 	pf_q = vsi->base_queue;
4568 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4569 		i40e_control_tx_q(pf, pf_q, false);
4570 		i40e_control_rx_q(pf, pf_q, false);
4571 	}
4572 }
4573 
4574 /**
4575  * i40e_vsi_free_irq - Free the irq association with the OS
4576  * @vsi: the VSI being configured
4577  **/
4578 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4579 {
4580 	struct i40e_pf *pf = vsi->back;
4581 	struct i40e_hw *hw = &pf->hw;
4582 	int base = vsi->base_vector;
4583 	u32 val, qp;
4584 	int i;
4585 
4586 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4587 		if (!vsi->q_vectors)
4588 			return;
4589 
4590 		if (!vsi->irqs_ready)
4591 			return;
4592 
4593 		vsi->irqs_ready = false;
4594 		for (i = 0; i < vsi->num_q_vectors; i++) {
4595 			int irq_num;
4596 			u16 vector;
4597 
4598 			vector = i + base;
4599 			irq_num = pf->msix_entries[vector].vector;
4600 
4601 			/* free only the irqs that were actually requested */
4602 			if (!vsi->q_vectors[i] ||
4603 			    !vsi->q_vectors[i]->num_ringpairs)
4604 				continue;
4605 
4606 			/* clear the affinity notifier in the IRQ descriptor */
4607 			irq_set_affinity_notifier(irq_num, NULL);
4608 			/* remove our suggested affinity mask for this IRQ */
4609 			irq_set_affinity_hint(irq_num, NULL);
4610 			synchronize_irq(irq_num);
4611 			free_irq(irq_num, vsi->q_vectors[i]);
4612 
4613 			/* Tear down the interrupt queue link list
4614 			 *
4615 			 * We know that they come in pairs and always
4616 			 * the Rx first, then the Tx.  To clear the
4617 			 * link list, stick the EOL value into the
4618 			 * next_q field of the registers.
4619 			 */
4620 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4621 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4622 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4623 			val |= I40E_QUEUE_END_OF_LIST
4624 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4625 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4626 
4627 			while (qp != I40E_QUEUE_END_OF_LIST) {
4628 				u32 next;
4629 
4630 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4631 
4632 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4633 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4634 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4635 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4636 
4637 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4638 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4639 
4640 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4641 
4642 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4643 
4644 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4645 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4646 
4647 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4648 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4649 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4650 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4651 
4652 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4653 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4654 
4655 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4656 				qp = next;
4657 			}
4658 		}
4659 	} else {
4660 		free_irq(pf->pdev->irq, pf);
4661 
4662 		val = rd32(hw, I40E_PFINT_LNKLST0);
4663 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4664 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4665 		val |= I40E_QUEUE_END_OF_LIST
4666 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4667 		wr32(hw, I40E_PFINT_LNKLST0, val);
4668 
4669 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4670 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4671 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4672 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4673 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4674 
4675 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4676 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4677 
4678 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4679 
4680 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4681 
4682 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4683 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4684 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4685 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4686 
4687 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4688 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4689 
4690 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4691 	}
4692 }
4693 
4694 /**
4695  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4696  * @vsi: the VSI being configured
4697  * @v_idx: Index of vector to be freed
4698  *
4699  * This function frees the memory allocated to the q_vector.  In addition if
4700  * NAPI is enabled it will delete any references to the NAPI struct prior
4701  * to freeing the q_vector.
4702  **/
4703 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4704 {
4705 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4706 	struct i40e_ring *ring;
4707 
4708 	if (!q_vector)
4709 		return;
4710 
4711 	/* disassociate q_vector from rings */
4712 	i40e_for_each_ring(ring, q_vector->tx)
4713 		ring->q_vector = NULL;
4714 
4715 	i40e_for_each_ring(ring, q_vector->rx)
4716 		ring->q_vector = NULL;
4717 
4718 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4719 	if (vsi->netdev)
4720 		netif_napi_del(&q_vector->napi);
4721 
4722 	vsi->q_vectors[v_idx] = NULL;
4723 
4724 	kfree_rcu(q_vector, rcu);
4725 }
4726 
4727 /**
4728  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4729  * @vsi: the VSI being un-configured
4730  *
4731  * This frees the memory allocated to the q_vectors and
4732  * deletes references to the NAPI struct.
4733  **/
4734 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4735 {
4736 	int v_idx;
4737 
4738 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4739 		i40e_free_q_vector(vsi, v_idx);
4740 }
4741 
4742 /**
4743  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4744  * @pf: board private structure
4745  **/
4746 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4747 {
4748 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4749 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4750 		pci_disable_msix(pf->pdev);
4751 		kfree(pf->msix_entries);
4752 		pf->msix_entries = NULL;
4753 		kfree(pf->irq_pile);
4754 		pf->irq_pile = NULL;
4755 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4756 		pci_disable_msi(pf->pdev);
4757 	}
4758 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4759 }
4760 
4761 /**
4762  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4763  * @pf: board private structure
4764  *
4765  * We go through and clear interrupt specific resources and reset the structure
4766  * to pre-load conditions
4767  **/
4768 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4769 {
4770 	int i;
4771 
4772 	i40e_free_misc_vector(pf);
4773 
4774 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4775 		      I40E_IWARP_IRQ_PILE_ID);
4776 
4777 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4778 	for (i = 0; i < pf->num_alloc_vsi; i++)
4779 		if (pf->vsi[i])
4780 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4781 	i40e_reset_interrupt_capability(pf);
4782 }
4783 
4784 /**
4785  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4786  * @vsi: the VSI being configured
4787  **/
4788 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4789 {
4790 	int q_idx;
4791 
4792 	if (!vsi->netdev)
4793 		return;
4794 
4795 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4796 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4797 
4798 		if (q_vector->rx.ring || q_vector->tx.ring)
4799 			napi_enable(&q_vector->napi);
4800 	}
4801 }
4802 
4803 /**
4804  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4805  * @vsi: the VSI being configured
4806  **/
4807 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4808 {
4809 	int q_idx;
4810 
4811 	if (!vsi->netdev)
4812 		return;
4813 
4814 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4815 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4816 
4817 		if (q_vector->rx.ring || q_vector->tx.ring)
4818 			napi_disable(&q_vector->napi);
4819 	}
4820 }
4821 
4822 /**
4823  * i40e_vsi_close - Shut down a VSI
4824  * @vsi: the vsi to be quelled
4825  **/
4826 static void i40e_vsi_close(struct i40e_vsi *vsi)
4827 {
4828 	struct i40e_pf *pf = vsi->back;
4829 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4830 		i40e_down(vsi);
4831 	i40e_vsi_free_irq(vsi);
4832 	i40e_vsi_free_tx_resources(vsi);
4833 	i40e_vsi_free_rx_resources(vsi);
4834 	vsi->current_netdev_flags = 0;
4835 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4836 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4837 		set_bit(__I40E_CLIENT_RESET, pf->state);
4838 }
4839 
4840 /**
4841  * i40e_quiesce_vsi - Pause a given VSI
4842  * @vsi: the VSI being paused
4843  **/
4844 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4845 {
4846 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4847 		return;
4848 
4849 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4850 	if (vsi->netdev && netif_running(vsi->netdev))
4851 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4852 	else
4853 		i40e_vsi_close(vsi);
4854 }
4855 
4856 /**
4857  * i40e_unquiesce_vsi - Resume a given VSI
4858  * @vsi: the VSI being resumed
4859  **/
4860 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4861 {
4862 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4863 		return;
4864 
4865 	if (vsi->netdev && netif_running(vsi->netdev))
4866 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4867 	else
4868 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4869 }
4870 
4871 /**
4872  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4873  * @pf: the PF
4874  **/
4875 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4876 {
4877 	int v;
4878 
4879 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4880 		if (pf->vsi[v])
4881 			i40e_quiesce_vsi(pf->vsi[v]);
4882 	}
4883 }
4884 
4885 /**
4886  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4887  * @pf: the PF
4888  **/
4889 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4890 {
4891 	int v;
4892 
4893 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4894 		if (pf->vsi[v])
4895 			i40e_unquiesce_vsi(pf->vsi[v]);
4896 	}
4897 }
4898 
4899 /**
4900  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4901  * @vsi: the VSI being configured
4902  *
4903  * Wait until all queues on a given VSI have been disabled.
4904  **/
4905 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4906 {
4907 	struct i40e_pf *pf = vsi->back;
4908 	int i, pf_q, ret;
4909 
4910 	pf_q = vsi->base_queue;
4911 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4912 		/* Check and wait for the Tx queue */
4913 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4914 		if (ret) {
4915 			dev_info(&pf->pdev->dev,
4916 				 "VSI seid %d Tx ring %d disable timeout\n",
4917 				 vsi->seid, pf_q);
4918 			return ret;
4919 		}
4920 
4921 		if (!i40e_enabled_xdp_vsi(vsi))
4922 			goto wait_rx;
4923 
4924 		/* Check and wait for the XDP Tx queue */
4925 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4926 				       false);
4927 		if (ret) {
4928 			dev_info(&pf->pdev->dev,
4929 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4930 				 vsi->seid, pf_q);
4931 			return ret;
4932 		}
4933 wait_rx:
4934 		/* Check and wait for the Rx queue */
4935 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4936 		if (ret) {
4937 			dev_info(&pf->pdev->dev,
4938 				 "VSI seid %d Rx ring %d disable timeout\n",
4939 				 vsi->seid, pf_q);
4940 			return ret;
4941 		}
4942 	}
4943 
4944 	return 0;
4945 }
4946 
4947 #ifdef CONFIG_I40E_DCB
4948 /**
4949  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4950  * @pf: the PF
4951  *
4952  * This function waits for the queues to be in disabled state for all the
4953  * VSIs that are managed by this PF.
4954  **/
4955 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4956 {
4957 	int v, ret = 0;
4958 
4959 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4960 		if (pf->vsi[v]) {
4961 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4962 			if (ret)
4963 				break;
4964 		}
4965 	}
4966 
4967 	return ret;
4968 }
4969 
4970 #endif
4971 
4972 /**
4973  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4974  * @pf: pointer to PF
4975  *
4976  * Get TC map for ISCSI PF type that will include iSCSI TC
4977  * and LAN TC.
4978  **/
4979 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4980 {
4981 	struct i40e_dcb_app_priority_table app;
4982 	struct i40e_hw *hw = &pf->hw;
4983 	u8 enabled_tc = 1; /* TC0 is always enabled */
4984 	u8 tc, i;
4985 	/* Get the iSCSI APP TLV */
4986 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4987 
4988 	for (i = 0; i < dcbcfg->numapps; i++) {
4989 		app = dcbcfg->app[i];
4990 		if (app.selector == I40E_APP_SEL_TCPIP &&
4991 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
4992 			tc = dcbcfg->etscfg.prioritytable[app.priority];
4993 			enabled_tc |= BIT(tc);
4994 			break;
4995 		}
4996 	}
4997 
4998 	return enabled_tc;
4999 }
5000 
5001 /**
5002  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5003  * @dcbcfg: the corresponding DCBx configuration structure
5004  *
5005  * Return the number of TCs from given DCBx configuration
5006  **/
5007 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5008 {
5009 	int i, tc_unused = 0;
5010 	u8 num_tc = 0;
5011 	u8 ret = 0;
5012 
5013 	/* Scan the ETS Config Priority Table to find
5014 	 * traffic class enabled for a given priority
5015 	 * and create a bitmask of enabled TCs
5016 	 */
5017 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5018 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5019 
5020 	/* Now scan the bitmask to check for
5021 	 * contiguous TCs starting with TC0
5022 	 */
5023 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5024 		if (num_tc & BIT(i)) {
5025 			if (!tc_unused) {
5026 				ret++;
5027 			} else {
5028 				pr_err("Non-contiguous TC - Disabling DCB\n");
5029 				return 1;
5030 			}
5031 		} else {
5032 			tc_unused = 1;
5033 		}
5034 	}
5035 
5036 	/* There is always at least TC0 */
5037 	if (!ret)
5038 		ret = 1;
5039 
5040 	return ret;
5041 }
5042 
5043 /**
5044  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5045  * @dcbcfg: the corresponding DCBx configuration structure
5046  *
5047  * Query the current DCB configuration and return the number of
5048  * traffic classes enabled from the given DCBX config
5049  **/
5050 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5051 {
5052 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5053 	u8 enabled_tc = 1;
5054 	u8 i;
5055 
5056 	for (i = 0; i < num_tc; i++)
5057 		enabled_tc |= BIT(i);
5058 
5059 	return enabled_tc;
5060 }
5061 
5062 /**
5063  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5064  * @pf: PF being queried
5065  *
5066  * Query the current MQPRIO configuration and return the number of
5067  * traffic classes enabled.
5068  **/
5069 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5070 {
5071 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5072 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5073 	u8 enabled_tc = 1, i;
5074 
5075 	for (i = 1; i < num_tc; i++)
5076 		enabled_tc |= BIT(i);
5077 	return enabled_tc;
5078 }
5079 
5080 /**
5081  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5082  * @pf: PF being queried
5083  *
5084  * Return number of traffic classes enabled for the given PF
5085  **/
5086 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5087 {
5088 	struct i40e_hw *hw = &pf->hw;
5089 	u8 i, enabled_tc = 1;
5090 	u8 num_tc = 0;
5091 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5092 
5093 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5094 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5095 
5096 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5097 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5098 		return 1;
5099 
5100 	/* SFP mode will be enabled for all TCs on port */
5101 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5102 		return i40e_dcb_get_num_tc(dcbcfg);
5103 
5104 	/* MFP mode return count of enabled TCs for this PF */
5105 	if (pf->hw.func_caps.iscsi)
5106 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5107 	else
5108 		return 1; /* Only TC0 */
5109 
5110 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5111 		if (enabled_tc & BIT(i))
5112 			num_tc++;
5113 	}
5114 	return num_tc;
5115 }
5116 
5117 /**
5118  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5119  * @pf: PF being queried
5120  *
5121  * Return a bitmap for enabled traffic classes for this PF.
5122  **/
5123 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5124 {
5125 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5126 		return i40e_mqprio_get_enabled_tc(pf);
5127 
5128 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5129 	 * default TC
5130 	 */
5131 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5132 		return I40E_DEFAULT_TRAFFIC_CLASS;
5133 
5134 	/* SFP mode we want PF to be enabled for all TCs */
5135 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5136 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5137 
5138 	/* MFP enabled and iSCSI PF type */
5139 	if (pf->hw.func_caps.iscsi)
5140 		return i40e_get_iscsi_tc_map(pf);
5141 	else
5142 		return I40E_DEFAULT_TRAFFIC_CLASS;
5143 }
5144 
5145 /**
5146  * i40e_vsi_get_bw_info - Query VSI BW Information
5147  * @vsi: the VSI being queried
5148  *
5149  * Returns 0 on success, negative value on failure
5150  **/
5151 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5152 {
5153 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5154 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5155 	struct i40e_pf *pf = vsi->back;
5156 	struct i40e_hw *hw = &pf->hw;
5157 	i40e_status ret;
5158 	u32 tc_bw_max;
5159 	int i;
5160 
5161 	/* Get the VSI level BW configuration */
5162 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5163 	if (ret) {
5164 		dev_info(&pf->pdev->dev,
5165 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5166 			 i40e_stat_str(&pf->hw, ret),
5167 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5168 		return -EINVAL;
5169 	}
5170 
5171 	/* Get the VSI level BW configuration per TC */
5172 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5173 					       NULL);
5174 	if (ret) {
5175 		dev_info(&pf->pdev->dev,
5176 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5177 			 i40e_stat_str(&pf->hw, ret),
5178 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5179 		return -EINVAL;
5180 	}
5181 
5182 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5183 		dev_info(&pf->pdev->dev,
5184 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5185 			 bw_config.tc_valid_bits,
5186 			 bw_ets_config.tc_valid_bits);
5187 		/* Still continuing */
5188 	}
5189 
5190 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5191 	vsi->bw_max_quanta = bw_config.max_bw;
5192 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5193 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5194 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5195 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5196 		vsi->bw_ets_limit_credits[i] =
5197 					le16_to_cpu(bw_ets_config.credits[i]);
5198 		/* 3 bits out of 4 for each TC */
5199 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5200 	}
5201 
5202 	return 0;
5203 }
5204 
5205 /**
5206  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5207  * @vsi: the VSI being configured
5208  * @enabled_tc: TC bitmap
5209  * @bw_share: BW shared credits per TC
5210  *
5211  * Returns 0 on success, negative value on failure
5212  **/
5213 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5214 				       u8 *bw_share)
5215 {
5216 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5217 	struct i40e_pf *pf = vsi->back;
5218 	i40e_status ret;
5219 	int i;
5220 
5221 	/* There is no need to reset BW when mqprio mode is on.  */
5222 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5223 		return 0;
5224 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5225 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5226 		if (ret)
5227 			dev_info(&pf->pdev->dev,
5228 				 "Failed to reset tx rate for vsi->seid %u\n",
5229 				 vsi->seid);
5230 		return ret;
5231 	}
5232 	bw_data.tc_valid_bits = enabled_tc;
5233 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5234 		bw_data.tc_bw_credits[i] = bw_share[i];
5235 
5236 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5237 	if (ret) {
5238 		dev_info(&pf->pdev->dev,
5239 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5240 			 pf->hw.aq.asq_last_status);
5241 		return -EINVAL;
5242 	}
5243 
5244 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5245 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5246 
5247 	return 0;
5248 }
5249 
5250 /**
5251  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5252  * @vsi: the VSI being configured
5253  * @enabled_tc: TC map to be enabled
5254  *
5255  **/
5256 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5257 {
5258 	struct net_device *netdev = vsi->netdev;
5259 	struct i40e_pf *pf = vsi->back;
5260 	struct i40e_hw *hw = &pf->hw;
5261 	u8 netdev_tc = 0;
5262 	int i;
5263 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5264 
5265 	if (!netdev)
5266 		return;
5267 
5268 	if (!enabled_tc) {
5269 		netdev_reset_tc(netdev);
5270 		return;
5271 	}
5272 
5273 	/* Set up actual enabled TCs on the VSI */
5274 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5275 		return;
5276 
5277 	/* set per TC queues for the VSI */
5278 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5279 		/* Only set TC queues for enabled tcs
5280 		 *
5281 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5282 		 * enabled_tc bitmap would be 0x00001001; the driver
5283 		 * will set the numtc for netdev as 2 that will be
5284 		 * referenced by the netdev layer as TC 0 and 1.
5285 		 */
5286 		if (vsi->tc_config.enabled_tc & BIT(i))
5287 			netdev_set_tc_queue(netdev,
5288 					vsi->tc_config.tc_info[i].netdev_tc,
5289 					vsi->tc_config.tc_info[i].qcount,
5290 					vsi->tc_config.tc_info[i].qoffset);
5291 	}
5292 
5293 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5294 		return;
5295 
5296 	/* Assign UP2TC map for the VSI */
5297 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5298 		/* Get the actual TC# for the UP */
5299 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5300 		/* Get the mapped netdev TC# for the UP */
5301 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5302 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5303 	}
5304 }
5305 
5306 /**
5307  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5308  * @vsi: the VSI being configured
5309  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5310  **/
5311 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5312 				      struct i40e_vsi_context *ctxt)
5313 {
5314 	/* copy just the sections touched not the entire info
5315 	 * since not all sections are valid as returned by
5316 	 * update vsi params
5317 	 */
5318 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5319 	memcpy(&vsi->info.queue_mapping,
5320 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5321 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5322 	       sizeof(vsi->info.tc_mapping));
5323 }
5324 
5325 /**
5326  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5327  * @vsi: VSI to be configured
5328  * @enabled_tc: TC bitmap
5329  *
5330  * This configures a particular VSI for TCs that are mapped to the
5331  * given TC bitmap. It uses default bandwidth share for TCs across
5332  * VSIs to configure TC for a particular VSI.
5333  *
5334  * NOTE:
5335  * It is expected that the VSI queues have been quisced before calling
5336  * this function.
5337  **/
5338 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5339 {
5340 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5341 	struct i40e_pf *pf = vsi->back;
5342 	struct i40e_hw *hw = &pf->hw;
5343 	struct i40e_vsi_context ctxt;
5344 	int ret = 0;
5345 	int i;
5346 
5347 	/* Check if enabled_tc is same as existing or new TCs */
5348 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5349 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5350 		return ret;
5351 
5352 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5353 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5354 		if (enabled_tc & BIT(i))
5355 			bw_share[i] = 1;
5356 	}
5357 
5358 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5359 	if (ret) {
5360 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5361 
5362 		dev_info(&pf->pdev->dev,
5363 			 "Failed configuring TC map %d for VSI %d\n",
5364 			 enabled_tc, vsi->seid);
5365 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5366 						  &bw_config, NULL);
5367 		if (ret) {
5368 			dev_info(&pf->pdev->dev,
5369 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5370 				 i40e_stat_str(hw, ret),
5371 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5372 			goto out;
5373 		}
5374 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5375 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5376 
5377 			if (!valid_tc)
5378 				valid_tc = bw_config.tc_valid_bits;
5379 			/* Always enable TC0, no matter what */
5380 			valid_tc |= 1;
5381 			dev_info(&pf->pdev->dev,
5382 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5383 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5384 			enabled_tc = valid_tc;
5385 		}
5386 
5387 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5388 		if (ret) {
5389 			dev_err(&pf->pdev->dev,
5390 				"Unable to  configure TC map %d for VSI %d\n",
5391 				enabled_tc, vsi->seid);
5392 			goto out;
5393 		}
5394 	}
5395 
5396 	/* Update Queue Pairs Mapping for currently enabled UPs */
5397 	ctxt.seid = vsi->seid;
5398 	ctxt.pf_num = vsi->back->hw.pf_id;
5399 	ctxt.vf_num = 0;
5400 	ctxt.uplink_seid = vsi->uplink_seid;
5401 	ctxt.info = vsi->info;
5402 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5403 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5404 		if (ret)
5405 			goto out;
5406 	} else {
5407 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5408 	}
5409 
5410 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5411 	 * queues changed.
5412 	 */
5413 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5414 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5415 				      vsi->num_queue_pairs);
5416 		ret = i40e_vsi_config_rss(vsi);
5417 		if (ret) {
5418 			dev_info(&vsi->back->pdev->dev,
5419 				 "Failed to reconfig rss for num_queues\n");
5420 			return ret;
5421 		}
5422 		vsi->reconfig_rss = false;
5423 	}
5424 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5425 		ctxt.info.valid_sections |=
5426 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5427 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5428 	}
5429 
5430 	/* Update the VSI after updating the VSI queue-mapping
5431 	 * information
5432 	 */
5433 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5434 	if (ret) {
5435 		dev_info(&pf->pdev->dev,
5436 			 "Update vsi tc config failed, err %s aq_err %s\n",
5437 			 i40e_stat_str(hw, ret),
5438 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5439 		goto out;
5440 	}
5441 	/* update the local VSI info with updated queue map */
5442 	i40e_vsi_update_queue_map(vsi, &ctxt);
5443 	vsi->info.valid_sections = 0;
5444 
5445 	/* Update current VSI BW information */
5446 	ret = i40e_vsi_get_bw_info(vsi);
5447 	if (ret) {
5448 		dev_info(&pf->pdev->dev,
5449 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5450 			 i40e_stat_str(hw, ret),
5451 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5452 		goto out;
5453 	}
5454 
5455 	/* Update the netdev TC setup */
5456 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5457 out:
5458 	return ret;
5459 }
5460 
5461 /**
5462  * i40e_get_link_speed - Returns link speed for the interface
5463  * @vsi: VSI to be configured
5464  *
5465  **/
5466 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5467 {
5468 	struct i40e_pf *pf = vsi->back;
5469 
5470 	switch (pf->hw.phy.link_info.link_speed) {
5471 	case I40E_LINK_SPEED_40GB:
5472 		return 40000;
5473 	case I40E_LINK_SPEED_25GB:
5474 		return 25000;
5475 	case I40E_LINK_SPEED_20GB:
5476 		return 20000;
5477 	case I40E_LINK_SPEED_10GB:
5478 		return 10000;
5479 	case I40E_LINK_SPEED_1GB:
5480 		return 1000;
5481 	default:
5482 		return -EINVAL;
5483 	}
5484 }
5485 
5486 /**
5487  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5488  * @vsi: VSI to be configured
5489  * @seid: seid of the channel/VSI
5490  * @max_tx_rate: max TX rate to be configured as BW limit
5491  *
5492  * Helper function to set BW limit for a given VSI
5493  **/
5494 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5495 {
5496 	struct i40e_pf *pf = vsi->back;
5497 	u64 credits = 0;
5498 	int speed = 0;
5499 	int ret = 0;
5500 
5501 	speed = i40e_get_link_speed(vsi);
5502 	if (max_tx_rate > speed) {
5503 		dev_err(&pf->pdev->dev,
5504 			"Invalid max tx rate %llu specified for VSI seid %d.",
5505 			max_tx_rate, seid);
5506 		return -EINVAL;
5507 	}
5508 	if (max_tx_rate && max_tx_rate < 50) {
5509 		dev_warn(&pf->pdev->dev,
5510 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5511 		max_tx_rate = 50;
5512 	}
5513 
5514 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5515 	credits = max_tx_rate;
5516 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5517 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5518 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5519 	if (ret)
5520 		dev_err(&pf->pdev->dev,
5521 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5522 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5523 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5524 	return ret;
5525 }
5526 
5527 /**
5528  * i40e_remove_queue_channels - Remove queue channels for the TCs
5529  * @vsi: VSI to be configured
5530  *
5531  * Remove queue channels for the TCs
5532  **/
5533 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5534 {
5535 	enum i40e_admin_queue_err last_aq_status;
5536 	struct i40e_cloud_filter *cfilter;
5537 	struct i40e_channel *ch, *ch_tmp;
5538 	struct i40e_pf *pf = vsi->back;
5539 	struct hlist_node *node;
5540 	int ret, i;
5541 
5542 	/* Reset rss size that was stored when reconfiguring rss for
5543 	 * channel VSIs with non-power-of-2 queue count.
5544 	 */
5545 	vsi->current_rss_size = 0;
5546 
5547 	/* perform cleanup for channels if they exist */
5548 	if (list_empty(&vsi->ch_list))
5549 		return;
5550 
5551 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5552 		struct i40e_vsi *p_vsi;
5553 
5554 		list_del(&ch->list);
5555 		p_vsi = ch->parent_vsi;
5556 		if (!p_vsi || !ch->initialized) {
5557 			kfree(ch);
5558 			continue;
5559 		}
5560 		/* Reset queue contexts */
5561 		for (i = 0; i < ch->num_queue_pairs; i++) {
5562 			struct i40e_ring *tx_ring, *rx_ring;
5563 			u16 pf_q;
5564 
5565 			pf_q = ch->base_queue + i;
5566 			tx_ring = vsi->tx_rings[pf_q];
5567 			tx_ring->ch = NULL;
5568 
5569 			rx_ring = vsi->rx_rings[pf_q];
5570 			rx_ring->ch = NULL;
5571 		}
5572 
5573 		/* Reset BW configured for this VSI via mqprio */
5574 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5575 		if (ret)
5576 			dev_info(&vsi->back->pdev->dev,
5577 				 "Failed to reset tx rate for ch->seid %u\n",
5578 				 ch->seid);
5579 
5580 		/* delete cloud filters associated with this channel */
5581 		hlist_for_each_entry_safe(cfilter, node,
5582 					  &pf->cloud_filter_list, cloud_node) {
5583 			if (cfilter->seid != ch->seid)
5584 				continue;
5585 
5586 			hash_del(&cfilter->cloud_node);
5587 			if (cfilter->dst_port)
5588 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5589 									cfilter,
5590 									false);
5591 			else
5592 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5593 								false);
5594 			last_aq_status = pf->hw.aq.asq_last_status;
5595 			if (ret)
5596 				dev_info(&pf->pdev->dev,
5597 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5598 					 i40e_stat_str(&pf->hw, ret),
5599 					 i40e_aq_str(&pf->hw, last_aq_status));
5600 			kfree(cfilter);
5601 		}
5602 
5603 		/* delete VSI from FW */
5604 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5605 					     NULL);
5606 		if (ret)
5607 			dev_err(&vsi->back->pdev->dev,
5608 				"unable to remove channel (%d) for parent VSI(%d)\n",
5609 				ch->seid, p_vsi->seid);
5610 		kfree(ch);
5611 	}
5612 	INIT_LIST_HEAD(&vsi->ch_list);
5613 }
5614 
5615 /**
5616  * i40e_is_any_channel - channel exist or not
5617  * @vsi: ptr to VSI to which channels are associated with
5618  *
5619  * Returns true or false if channel(s) exist for associated VSI or not
5620  **/
5621 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5622 {
5623 	struct i40e_channel *ch, *ch_tmp;
5624 
5625 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5626 		if (ch->initialized)
5627 			return true;
5628 	}
5629 
5630 	return false;
5631 }
5632 
5633 /**
5634  * i40e_get_max_queues_for_channel
5635  * @vsi: ptr to VSI to which channels are associated with
5636  *
5637  * Helper function which returns max value among the queue counts set on the
5638  * channels/TCs created.
5639  **/
5640 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5641 {
5642 	struct i40e_channel *ch, *ch_tmp;
5643 	int max = 0;
5644 
5645 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5646 		if (!ch->initialized)
5647 			continue;
5648 		if (ch->num_queue_pairs > max)
5649 			max = ch->num_queue_pairs;
5650 	}
5651 
5652 	return max;
5653 }
5654 
5655 /**
5656  * i40e_validate_num_queues - validate num_queues w.r.t channel
5657  * @pf: ptr to PF device
5658  * @num_queues: number of queues
5659  * @vsi: the parent VSI
5660  * @reconfig_rss: indicates should the RSS be reconfigured or not
5661  *
5662  * This function validates number of queues in the context of new channel
5663  * which is being established and determines if RSS should be reconfigured
5664  * or not for parent VSI.
5665  **/
5666 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5667 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5668 {
5669 	int max_ch_queues;
5670 
5671 	if (!reconfig_rss)
5672 		return -EINVAL;
5673 
5674 	*reconfig_rss = false;
5675 	if (vsi->current_rss_size) {
5676 		if (num_queues > vsi->current_rss_size) {
5677 			dev_dbg(&pf->pdev->dev,
5678 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5679 				num_queues, vsi->current_rss_size);
5680 			return -EINVAL;
5681 		} else if ((num_queues < vsi->current_rss_size) &&
5682 			   (!is_power_of_2(num_queues))) {
5683 			dev_dbg(&pf->pdev->dev,
5684 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5685 				num_queues, vsi->current_rss_size);
5686 			return -EINVAL;
5687 		}
5688 	}
5689 
5690 	if (!is_power_of_2(num_queues)) {
5691 		/* Find the max num_queues configured for channel if channel
5692 		 * exist.
5693 		 * if channel exist, then enforce 'num_queues' to be more than
5694 		 * max ever queues configured for channel.
5695 		 */
5696 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5697 		if (num_queues < max_ch_queues) {
5698 			dev_dbg(&pf->pdev->dev,
5699 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5700 				num_queues, max_ch_queues);
5701 			return -EINVAL;
5702 		}
5703 		*reconfig_rss = true;
5704 	}
5705 
5706 	return 0;
5707 }
5708 
5709 /**
5710  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5711  * @vsi: the VSI being setup
5712  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5713  *
5714  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5715  **/
5716 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5717 {
5718 	struct i40e_pf *pf = vsi->back;
5719 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5720 	struct i40e_hw *hw = &pf->hw;
5721 	int local_rss_size;
5722 	u8 *lut;
5723 	int ret;
5724 
5725 	if (!vsi->rss_size)
5726 		return -EINVAL;
5727 
5728 	if (rss_size > vsi->rss_size)
5729 		return -EINVAL;
5730 
5731 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5732 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5733 	if (!lut)
5734 		return -ENOMEM;
5735 
5736 	/* Ignoring user configured lut if there is one */
5737 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5738 
5739 	/* Use user configured hash key if there is one, otherwise
5740 	 * use default.
5741 	 */
5742 	if (vsi->rss_hkey_user)
5743 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5744 	else
5745 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5746 
5747 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5748 	if (ret) {
5749 		dev_info(&pf->pdev->dev,
5750 			 "Cannot set RSS lut, err %s aq_err %s\n",
5751 			 i40e_stat_str(hw, ret),
5752 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5753 		kfree(lut);
5754 		return ret;
5755 	}
5756 	kfree(lut);
5757 
5758 	/* Do the update w.r.t. storing rss_size */
5759 	if (!vsi->orig_rss_size)
5760 		vsi->orig_rss_size = vsi->rss_size;
5761 	vsi->current_rss_size = local_rss_size;
5762 
5763 	return ret;
5764 }
5765 
5766 /**
5767  * i40e_channel_setup_queue_map - Setup a channel queue map
5768  * @pf: ptr to PF device
5769  * @vsi: the VSI being setup
5770  * @ctxt: VSI context structure
5771  * @ch: ptr to channel structure
5772  *
5773  * Setup queue map for a specific channel
5774  **/
5775 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5776 					 struct i40e_vsi_context *ctxt,
5777 					 struct i40e_channel *ch)
5778 {
5779 	u16 qcount, qmap, sections = 0;
5780 	u8 offset = 0;
5781 	int pow;
5782 
5783 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5784 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5785 
5786 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5787 	ch->num_queue_pairs = qcount;
5788 
5789 	/* find the next higher power-of-2 of num queue pairs */
5790 	pow = ilog2(qcount);
5791 	if (!is_power_of_2(qcount))
5792 		pow++;
5793 
5794 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5795 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5796 
5797 	/* Setup queue TC[0].qmap for given VSI context */
5798 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5799 
5800 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5801 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5802 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5803 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5804 }
5805 
5806 /**
5807  * i40e_add_channel - add a channel by adding VSI
5808  * @pf: ptr to PF device
5809  * @uplink_seid: underlying HW switching element (VEB) ID
5810  * @ch: ptr to channel structure
5811  *
5812  * Add a channel (VSI) using add_vsi and queue_map
5813  **/
5814 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5815 			    struct i40e_channel *ch)
5816 {
5817 	struct i40e_hw *hw = &pf->hw;
5818 	struct i40e_vsi_context ctxt;
5819 	u8 enabled_tc = 0x1; /* TC0 enabled */
5820 	int ret;
5821 
5822 	if (ch->type != I40E_VSI_VMDQ2) {
5823 		dev_info(&pf->pdev->dev,
5824 			 "add new vsi failed, ch->type %d\n", ch->type);
5825 		return -EINVAL;
5826 	}
5827 
5828 	memset(&ctxt, 0, sizeof(ctxt));
5829 	ctxt.pf_num = hw->pf_id;
5830 	ctxt.vf_num = 0;
5831 	ctxt.uplink_seid = uplink_seid;
5832 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5833 	if (ch->type == I40E_VSI_VMDQ2)
5834 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5835 
5836 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5837 		ctxt.info.valid_sections |=
5838 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5839 		ctxt.info.switch_id =
5840 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5841 	}
5842 
5843 	/* Set queue map for a given VSI context */
5844 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5845 
5846 	/* Now time to create VSI */
5847 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5848 	if (ret) {
5849 		dev_info(&pf->pdev->dev,
5850 			 "add new vsi failed, err %s aq_err %s\n",
5851 			 i40e_stat_str(&pf->hw, ret),
5852 			 i40e_aq_str(&pf->hw,
5853 				     pf->hw.aq.asq_last_status));
5854 		return -ENOENT;
5855 	}
5856 
5857 	/* Success, update channel */
5858 	ch->enabled_tc = enabled_tc;
5859 	ch->seid = ctxt.seid;
5860 	ch->vsi_number = ctxt.vsi_number;
5861 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5862 
5863 	/* copy just the sections touched not the entire info
5864 	 * since not all sections are valid as returned by
5865 	 * update vsi params
5866 	 */
5867 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5868 	memcpy(&ch->info.queue_mapping,
5869 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5870 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5871 	       sizeof(ctxt.info.tc_mapping));
5872 
5873 	return 0;
5874 }
5875 
5876 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5877 				  u8 *bw_share)
5878 {
5879 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5880 	i40e_status ret;
5881 	int i;
5882 
5883 	bw_data.tc_valid_bits = ch->enabled_tc;
5884 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5885 		bw_data.tc_bw_credits[i] = bw_share[i];
5886 
5887 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5888 				       &bw_data, NULL);
5889 	if (ret) {
5890 		dev_info(&vsi->back->pdev->dev,
5891 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5892 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5893 		return -EINVAL;
5894 	}
5895 
5896 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5897 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5898 
5899 	return 0;
5900 }
5901 
5902 /**
5903  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5904  * @pf: ptr to PF device
5905  * @vsi: the VSI being setup
5906  * @ch: ptr to channel structure
5907  *
5908  * Configure TX rings associated with channel (VSI) since queues are being
5909  * from parent VSI.
5910  **/
5911 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5912 				       struct i40e_vsi *vsi,
5913 				       struct i40e_channel *ch)
5914 {
5915 	i40e_status ret;
5916 	int i;
5917 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5918 
5919 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5920 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5921 		if (ch->enabled_tc & BIT(i))
5922 			bw_share[i] = 1;
5923 	}
5924 
5925 	/* configure BW for new VSI */
5926 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5927 	if (ret) {
5928 		dev_info(&vsi->back->pdev->dev,
5929 			 "Failed configuring TC map %d for channel (seid %u)\n",
5930 			 ch->enabled_tc, ch->seid);
5931 		return ret;
5932 	}
5933 
5934 	for (i = 0; i < ch->num_queue_pairs; i++) {
5935 		struct i40e_ring *tx_ring, *rx_ring;
5936 		u16 pf_q;
5937 
5938 		pf_q = ch->base_queue + i;
5939 
5940 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5941 		 * context
5942 		 */
5943 		tx_ring = vsi->tx_rings[pf_q];
5944 		tx_ring->ch = ch;
5945 
5946 		/* Get the RX ring ptr */
5947 		rx_ring = vsi->rx_rings[pf_q];
5948 		rx_ring->ch = ch;
5949 	}
5950 
5951 	return 0;
5952 }
5953 
5954 /**
5955  * i40e_setup_hw_channel - setup new channel
5956  * @pf: ptr to PF device
5957  * @vsi: the VSI being setup
5958  * @ch: ptr to channel structure
5959  * @uplink_seid: underlying HW switching element (VEB) ID
5960  * @type: type of channel to be created (VMDq2/VF)
5961  *
5962  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5963  * and configures TX rings accordingly
5964  **/
5965 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5966 					struct i40e_vsi *vsi,
5967 					struct i40e_channel *ch,
5968 					u16 uplink_seid, u8 type)
5969 {
5970 	int ret;
5971 
5972 	ch->initialized = false;
5973 	ch->base_queue = vsi->next_base_queue;
5974 	ch->type = type;
5975 
5976 	/* Proceed with creation of channel (VMDq2) VSI */
5977 	ret = i40e_add_channel(pf, uplink_seid, ch);
5978 	if (ret) {
5979 		dev_info(&pf->pdev->dev,
5980 			 "failed to add_channel using uplink_seid %u\n",
5981 			 uplink_seid);
5982 		return ret;
5983 	}
5984 
5985 	/* Mark the successful creation of channel */
5986 	ch->initialized = true;
5987 
5988 	/* Reconfigure TX queues using QTX_CTL register */
5989 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5990 	if (ret) {
5991 		dev_info(&pf->pdev->dev,
5992 			 "failed to configure TX rings for channel %u\n",
5993 			 ch->seid);
5994 		return ret;
5995 	}
5996 
5997 	/* update 'next_base_queue' */
5998 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5999 	dev_dbg(&pf->pdev->dev,
6000 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6001 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6002 		ch->num_queue_pairs,
6003 		vsi->next_base_queue);
6004 	return ret;
6005 }
6006 
6007 /**
6008  * i40e_setup_channel - setup new channel using uplink element
6009  * @pf: ptr to PF device
6010  * @type: type of channel to be created (VMDq2/VF)
6011  * @uplink_seid: underlying HW switching element (VEB) ID
6012  * @ch: ptr to channel structure
6013  *
6014  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6015  * and uplink switching element (uplink_seid)
6016  **/
6017 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6018 			       struct i40e_channel *ch)
6019 {
6020 	u8 vsi_type;
6021 	u16 seid;
6022 	int ret;
6023 
6024 	if (vsi->type == I40E_VSI_MAIN) {
6025 		vsi_type = I40E_VSI_VMDQ2;
6026 	} else {
6027 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6028 			vsi->type);
6029 		return false;
6030 	}
6031 
6032 	/* underlying switching element */
6033 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6034 
6035 	/* create channel (VSI), configure TX rings */
6036 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6037 	if (ret) {
6038 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6039 		return false;
6040 	}
6041 
6042 	return ch->initialized ? true : false;
6043 }
6044 
6045 /**
6046  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6047  * @vsi: ptr to VSI which has PF backing
6048  *
6049  * Sets up switch mode correctly if it needs to be changed and perform
6050  * what are allowed modes.
6051  **/
6052 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6053 {
6054 	u8 mode;
6055 	struct i40e_pf *pf = vsi->back;
6056 	struct i40e_hw *hw = &pf->hw;
6057 	int ret;
6058 
6059 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6060 	if (ret)
6061 		return -EINVAL;
6062 
6063 	if (hw->dev_caps.switch_mode) {
6064 		/* if switch mode is set, support mode2 (non-tunneled for
6065 		 * cloud filter) for now
6066 		 */
6067 		u32 switch_mode = hw->dev_caps.switch_mode &
6068 				  I40E_SWITCH_MODE_MASK;
6069 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6070 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6071 				return 0;
6072 			dev_err(&pf->pdev->dev,
6073 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6074 				hw->dev_caps.switch_mode);
6075 			return -EINVAL;
6076 		}
6077 	}
6078 
6079 	/* Set Bit 7 to be valid */
6080 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6081 
6082 	/* Set L4type for TCP support */
6083 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6084 
6085 	/* Set cloud filter mode */
6086 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6087 
6088 	/* Prep mode field for set_switch_config */
6089 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6090 					pf->last_sw_conf_valid_flags,
6091 					mode, NULL);
6092 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6093 		dev_err(&pf->pdev->dev,
6094 			"couldn't set switch config bits, err %s aq_err %s\n",
6095 			i40e_stat_str(hw, ret),
6096 			i40e_aq_str(hw,
6097 				    hw->aq.asq_last_status));
6098 
6099 	return ret;
6100 }
6101 
6102 /**
6103  * i40e_create_queue_channel - function to create channel
6104  * @vsi: VSI to be configured
6105  * @ch: ptr to channel (it contains channel specific params)
6106  *
6107  * This function creates channel (VSI) using num_queues specified by user,
6108  * reconfigs RSS if needed.
6109  **/
6110 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6111 			      struct i40e_channel *ch)
6112 {
6113 	struct i40e_pf *pf = vsi->back;
6114 	bool reconfig_rss;
6115 	int err;
6116 
6117 	if (!ch)
6118 		return -EINVAL;
6119 
6120 	if (!ch->num_queue_pairs) {
6121 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6122 			ch->num_queue_pairs);
6123 		return -EINVAL;
6124 	}
6125 
6126 	/* validate user requested num_queues for channel */
6127 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6128 				       &reconfig_rss);
6129 	if (err) {
6130 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6131 			 ch->num_queue_pairs);
6132 		return -EINVAL;
6133 	}
6134 
6135 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6136 	 * VSI to be added switch to VEB mode.
6137 	 */
6138 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6139 	    (!i40e_is_any_channel(vsi))) {
6140 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6141 			dev_dbg(&pf->pdev->dev,
6142 				"Failed to create channel. Override queues (%u) not power of 2\n",
6143 				vsi->tc_config.tc_info[0].qcount);
6144 			return -EINVAL;
6145 		}
6146 
6147 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6148 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6149 
6150 			if (vsi->type == I40E_VSI_MAIN) {
6151 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6152 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6153 						      true);
6154 				else
6155 					i40e_do_reset_safe(pf,
6156 							   I40E_PF_RESET_FLAG);
6157 			}
6158 		}
6159 		/* now onwards for main VSI, number of queues will be value
6160 		 * of TC0's queue count
6161 		 */
6162 	}
6163 
6164 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6165 	 * it should be more than num_queues
6166 	 */
6167 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6168 		dev_dbg(&pf->pdev->dev,
6169 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6170 			vsi->cnt_q_avail, ch->num_queue_pairs);
6171 		return -EINVAL;
6172 	}
6173 
6174 	/* reconfig_rss only if vsi type is MAIN_VSI */
6175 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6176 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6177 		if (err) {
6178 			dev_info(&pf->pdev->dev,
6179 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6180 				 ch->num_queue_pairs);
6181 			return -EINVAL;
6182 		}
6183 	}
6184 
6185 	if (!i40e_setup_channel(pf, vsi, ch)) {
6186 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6187 		return -EINVAL;
6188 	}
6189 
6190 	dev_info(&pf->pdev->dev,
6191 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6192 		 ch->seid, ch->num_queue_pairs);
6193 
6194 	/* configure VSI for BW limit */
6195 	if (ch->max_tx_rate) {
6196 		u64 credits = ch->max_tx_rate;
6197 
6198 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6199 			return -EINVAL;
6200 
6201 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6202 		dev_dbg(&pf->pdev->dev,
6203 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6204 			ch->max_tx_rate,
6205 			credits,
6206 			ch->seid);
6207 	}
6208 
6209 	/* in case of VF, this will be main SRIOV VSI */
6210 	ch->parent_vsi = vsi;
6211 
6212 	/* and update main_vsi's count for queue_available to use */
6213 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6214 
6215 	return 0;
6216 }
6217 
6218 /**
6219  * i40e_configure_queue_channels - Add queue channel for the given TCs
6220  * @vsi: VSI to be configured
6221  *
6222  * Configures queue channel mapping to the given TCs
6223  **/
6224 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6225 {
6226 	struct i40e_channel *ch;
6227 	u64 max_rate = 0;
6228 	int ret = 0, i;
6229 
6230 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6231 	vsi->tc_seid_map[0] = vsi->seid;
6232 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6233 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6234 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6235 			if (!ch) {
6236 				ret = -ENOMEM;
6237 				goto err_free;
6238 			}
6239 
6240 			INIT_LIST_HEAD(&ch->list);
6241 			ch->num_queue_pairs =
6242 				vsi->tc_config.tc_info[i].qcount;
6243 			ch->base_queue =
6244 				vsi->tc_config.tc_info[i].qoffset;
6245 
6246 			/* Bandwidth limit through tc interface is in bytes/s,
6247 			 * change to Mbit/s
6248 			 */
6249 			max_rate = vsi->mqprio_qopt.max_rate[i];
6250 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6251 			ch->max_tx_rate = max_rate;
6252 
6253 			list_add_tail(&ch->list, &vsi->ch_list);
6254 
6255 			ret = i40e_create_queue_channel(vsi, ch);
6256 			if (ret) {
6257 				dev_err(&vsi->back->pdev->dev,
6258 					"Failed creating queue channel with TC%d: queues %d\n",
6259 					i, ch->num_queue_pairs);
6260 				goto err_free;
6261 			}
6262 			vsi->tc_seid_map[i] = ch->seid;
6263 		}
6264 	}
6265 	return ret;
6266 
6267 err_free:
6268 	i40e_remove_queue_channels(vsi);
6269 	return ret;
6270 }
6271 
6272 /**
6273  * i40e_veb_config_tc - Configure TCs for given VEB
6274  * @veb: given VEB
6275  * @enabled_tc: TC bitmap
6276  *
6277  * Configures given TC bitmap for VEB (switching) element
6278  **/
6279 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6280 {
6281 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6282 	struct i40e_pf *pf = veb->pf;
6283 	int ret = 0;
6284 	int i;
6285 
6286 	/* No TCs or already enabled TCs just return */
6287 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6288 		return ret;
6289 
6290 	bw_data.tc_valid_bits = enabled_tc;
6291 	/* bw_data.absolute_credits is not set (relative) */
6292 
6293 	/* Enable ETS TCs with equal BW Share for now */
6294 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6295 		if (enabled_tc & BIT(i))
6296 			bw_data.tc_bw_share_credits[i] = 1;
6297 	}
6298 
6299 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6300 						   &bw_data, NULL);
6301 	if (ret) {
6302 		dev_info(&pf->pdev->dev,
6303 			 "VEB bw config failed, err %s aq_err %s\n",
6304 			 i40e_stat_str(&pf->hw, ret),
6305 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6306 		goto out;
6307 	}
6308 
6309 	/* Update the BW information */
6310 	ret = i40e_veb_get_bw_info(veb);
6311 	if (ret) {
6312 		dev_info(&pf->pdev->dev,
6313 			 "Failed getting veb bw config, err %s aq_err %s\n",
6314 			 i40e_stat_str(&pf->hw, ret),
6315 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6316 	}
6317 
6318 out:
6319 	return ret;
6320 }
6321 
6322 #ifdef CONFIG_I40E_DCB
6323 /**
6324  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6325  * @pf: PF struct
6326  *
6327  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6328  * the caller would've quiesce all the VSIs before calling
6329  * this function
6330  **/
6331 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6332 {
6333 	u8 tc_map = 0;
6334 	int ret;
6335 	u8 v;
6336 
6337 	/* Enable the TCs available on PF to all VEBs */
6338 	tc_map = i40e_pf_get_tc_map(pf);
6339 	for (v = 0; v < I40E_MAX_VEB; v++) {
6340 		if (!pf->veb[v])
6341 			continue;
6342 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6343 		if (ret) {
6344 			dev_info(&pf->pdev->dev,
6345 				 "Failed configuring TC for VEB seid=%d\n",
6346 				 pf->veb[v]->seid);
6347 			/* Will try to configure as many components */
6348 		}
6349 	}
6350 
6351 	/* Update each VSI */
6352 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6353 		if (!pf->vsi[v])
6354 			continue;
6355 
6356 		/* - Enable all TCs for the LAN VSI
6357 		 * - For all others keep them at TC0 for now
6358 		 */
6359 		if (v == pf->lan_vsi)
6360 			tc_map = i40e_pf_get_tc_map(pf);
6361 		else
6362 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6363 
6364 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6365 		if (ret) {
6366 			dev_info(&pf->pdev->dev,
6367 				 "Failed configuring TC for VSI seid=%d\n",
6368 				 pf->vsi[v]->seid);
6369 			/* Will try to configure as many components */
6370 		} else {
6371 			/* Re-configure VSI vectors based on updated TC map */
6372 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6373 			if (pf->vsi[v]->netdev)
6374 				i40e_dcbnl_set_all(pf->vsi[v]);
6375 		}
6376 	}
6377 }
6378 
6379 /**
6380  * i40e_resume_port_tx - Resume port Tx
6381  * @pf: PF struct
6382  *
6383  * Resume a port's Tx and issue a PF reset in case of failure to
6384  * resume.
6385  **/
6386 static int i40e_resume_port_tx(struct i40e_pf *pf)
6387 {
6388 	struct i40e_hw *hw = &pf->hw;
6389 	int ret;
6390 
6391 	ret = i40e_aq_resume_port_tx(hw, NULL);
6392 	if (ret) {
6393 		dev_info(&pf->pdev->dev,
6394 			 "Resume Port Tx failed, err %s aq_err %s\n",
6395 			  i40e_stat_str(&pf->hw, ret),
6396 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6397 		/* Schedule PF reset to recover */
6398 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6399 		i40e_service_event_schedule(pf);
6400 	}
6401 
6402 	return ret;
6403 }
6404 
6405 /**
6406  * i40e_init_pf_dcb - Initialize DCB configuration
6407  * @pf: PF being configured
6408  *
6409  * Query the current DCB configuration and cache it
6410  * in the hardware structure
6411  **/
6412 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6413 {
6414 	struct i40e_hw *hw = &pf->hw;
6415 	int err = 0;
6416 
6417 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6418 	 * Also do not enable DCBx if FW LLDP agent is disabled
6419 	 */
6420 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6421 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
6422 		goto out;
6423 
6424 	/* Get the initial DCB configuration */
6425 	err = i40e_init_dcb(hw, true);
6426 	if (!err) {
6427 		/* Device/Function is not DCBX capable */
6428 		if ((!hw->func_caps.dcb) ||
6429 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6430 			dev_info(&pf->pdev->dev,
6431 				 "DCBX offload is not supported or is disabled for this PF.\n");
6432 		} else {
6433 			/* When status is not DISABLED then DCBX in FW */
6434 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6435 				       DCB_CAP_DCBX_VER_IEEE;
6436 
6437 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6438 			/* Enable DCB tagging only when more than one TC
6439 			 * or explicitly disable if only one TC
6440 			 */
6441 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6442 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6443 			else
6444 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6445 			dev_dbg(&pf->pdev->dev,
6446 				"DCBX offload is supported for this PF.\n");
6447 		}
6448 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6449 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6450 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6451 	} else {
6452 		dev_info(&pf->pdev->dev,
6453 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6454 			 i40e_stat_str(&pf->hw, err),
6455 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6456 	}
6457 
6458 out:
6459 	return err;
6460 }
6461 #endif /* CONFIG_I40E_DCB */
6462 #define SPEED_SIZE 14
6463 #define FC_SIZE 8
6464 /**
6465  * i40e_print_link_message - print link up or down
6466  * @vsi: the VSI for which link needs a message
6467  * @isup: true of link is up, false otherwise
6468  */
6469 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6470 {
6471 	enum i40e_aq_link_speed new_speed;
6472 	struct i40e_pf *pf = vsi->back;
6473 	char *speed = "Unknown";
6474 	char *fc = "Unknown";
6475 	char *fec = "";
6476 	char *req_fec = "";
6477 	char *an = "";
6478 
6479 	if (isup)
6480 		new_speed = pf->hw.phy.link_info.link_speed;
6481 	else
6482 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6483 
6484 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6485 		return;
6486 	vsi->current_isup = isup;
6487 	vsi->current_speed = new_speed;
6488 	if (!isup) {
6489 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6490 		return;
6491 	}
6492 
6493 	/* Warn user if link speed on NPAR enabled partition is not at
6494 	 * least 10GB
6495 	 */
6496 	if (pf->hw.func_caps.npar_enable &&
6497 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6498 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6499 		netdev_warn(vsi->netdev,
6500 			    "The partition detected link speed that is less than 10Gbps\n");
6501 
6502 	switch (pf->hw.phy.link_info.link_speed) {
6503 	case I40E_LINK_SPEED_40GB:
6504 		speed = "40 G";
6505 		break;
6506 	case I40E_LINK_SPEED_20GB:
6507 		speed = "20 G";
6508 		break;
6509 	case I40E_LINK_SPEED_25GB:
6510 		speed = "25 G";
6511 		break;
6512 	case I40E_LINK_SPEED_10GB:
6513 		speed = "10 G";
6514 		break;
6515 	case I40E_LINK_SPEED_1GB:
6516 		speed = "1000 M";
6517 		break;
6518 	case I40E_LINK_SPEED_100MB:
6519 		speed = "100 M";
6520 		break;
6521 	default:
6522 		break;
6523 	}
6524 
6525 	switch (pf->hw.fc.current_mode) {
6526 	case I40E_FC_FULL:
6527 		fc = "RX/TX";
6528 		break;
6529 	case I40E_FC_TX_PAUSE:
6530 		fc = "TX";
6531 		break;
6532 	case I40E_FC_RX_PAUSE:
6533 		fc = "RX";
6534 		break;
6535 	default:
6536 		fc = "None";
6537 		break;
6538 	}
6539 
6540 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6541 		req_fec = ", Requested FEC: None";
6542 		fec = ", FEC: None";
6543 		an = ", Autoneg: False";
6544 
6545 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6546 			an = ", Autoneg: True";
6547 
6548 		if (pf->hw.phy.link_info.fec_info &
6549 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6550 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6551 		else if (pf->hw.phy.link_info.fec_info &
6552 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6553 			fec = ", FEC: CL108 RS-FEC";
6554 
6555 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6556 		 * both RS and FC are requested
6557 		 */
6558 		if (vsi->back->hw.phy.link_info.req_fec_info &
6559 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6560 			if (vsi->back->hw.phy.link_info.req_fec_info &
6561 			    I40E_AQ_REQUEST_FEC_RS)
6562 				req_fec = ", Requested FEC: CL108 RS-FEC";
6563 			else
6564 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6565 		}
6566 	}
6567 
6568 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6569 		    speed, req_fec, fec, an, fc);
6570 }
6571 
6572 /**
6573  * i40e_up_complete - Finish the last steps of bringing up a connection
6574  * @vsi: the VSI being configured
6575  **/
6576 static int i40e_up_complete(struct i40e_vsi *vsi)
6577 {
6578 	struct i40e_pf *pf = vsi->back;
6579 	int err;
6580 
6581 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6582 		i40e_vsi_configure_msix(vsi);
6583 	else
6584 		i40e_configure_msi_and_legacy(vsi);
6585 
6586 	/* start rings */
6587 	err = i40e_vsi_start_rings(vsi);
6588 	if (err)
6589 		return err;
6590 
6591 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6592 	i40e_napi_enable_all(vsi);
6593 	i40e_vsi_enable_irq(vsi);
6594 
6595 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6596 	    (vsi->netdev)) {
6597 		i40e_print_link_message(vsi, true);
6598 		netif_tx_start_all_queues(vsi->netdev);
6599 		netif_carrier_on(vsi->netdev);
6600 	}
6601 
6602 	/* replay FDIR SB filters */
6603 	if (vsi->type == I40E_VSI_FDIR) {
6604 		/* reset fd counters */
6605 		pf->fd_add_err = 0;
6606 		pf->fd_atr_cnt = 0;
6607 		i40e_fdir_filter_restore(vsi);
6608 	}
6609 
6610 	/* On the next run of the service_task, notify any clients of the new
6611 	 * opened netdev
6612 	 */
6613 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6614 	i40e_service_event_schedule(pf);
6615 
6616 	return 0;
6617 }
6618 
6619 /**
6620  * i40e_vsi_reinit_locked - Reset the VSI
6621  * @vsi: the VSI being configured
6622  *
6623  * Rebuild the ring structs after some configuration
6624  * has changed, e.g. MTU size.
6625  **/
6626 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6627 {
6628 	struct i40e_pf *pf = vsi->back;
6629 
6630 	WARN_ON(in_interrupt());
6631 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6632 		usleep_range(1000, 2000);
6633 	i40e_down(vsi);
6634 
6635 	i40e_up(vsi);
6636 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6637 }
6638 
6639 /**
6640  * i40e_up - Bring the connection back up after being down
6641  * @vsi: the VSI being configured
6642  **/
6643 int i40e_up(struct i40e_vsi *vsi)
6644 {
6645 	int err;
6646 
6647 	err = i40e_vsi_configure(vsi);
6648 	if (!err)
6649 		err = i40e_up_complete(vsi);
6650 
6651 	return err;
6652 }
6653 
6654 /**
6655  * i40e_force_link_state - Force the link status
6656  * @pf: board private structure
6657  * @is_up: whether the link state should be forced up or down
6658  **/
6659 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6660 {
6661 	struct i40e_aq_get_phy_abilities_resp abilities;
6662 	struct i40e_aq_set_phy_config config = {0};
6663 	struct i40e_hw *hw = &pf->hw;
6664 	i40e_status err;
6665 	u64 mask;
6666 	u8 speed;
6667 
6668 	/* Card might've been put in an unstable state by other drivers
6669 	 * and applications, which causes incorrect speed values being
6670 	 * set on startup. In order to clear speed registers, we call
6671 	 * get_phy_capabilities twice, once to get initial state of
6672 	 * available speeds, and once to get current PHY config.
6673 	 */
6674 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6675 					   NULL);
6676 	if (err) {
6677 		dev_err(&pf->pdev->dev,
6678 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6679 			i40e_stat_str(hw, err),
6680 			i40e_aq_str(hw, hw->aq.asq_last_status));
6681 		return err;
6682 	}
6683 	speed = abilities.link_speed;
6684 
6685 	/* Get the current phy config */
6686 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6687 					   NULL);
6688 	if (err) {
6689 		dev_err(&pf->pdev->dev,
6690 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6691 			i40e_stat_str(hw, err),
6692 			i40e_aq_str(hw, hw->aq.asq_last_status));
6693 		return err;
6694 	}
6695 
6696 	/* If link needs to go up, but was not forced to go down,
6697 	 * and its speed values are OK, no need for a flap
6698 	 */
6699 	if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6700 		return I40E_SUCCESS;
6701 
6702 	/* To force link we need to set bits for all supported PHY types,
6703 	 * but there are now more than 32, so we need to split the bitmap
6704 	 * across two fields.
6705 	 */
6706 	mask = I40E_PHY_TYPES_BITMASK;
6707 	config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6708 	config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6709 	/* Copy the old settings, except of phy_type */
6710 	config.abilities = abilities.abilities;
6711 	if (abilities.link_speed != 0)
6712 		config.link_speed = abilities.link_speed;
6713 	else
6714 		config.link_speed = speed;
6715 	config.eee_capability = abilities.eee_capability;
6716 	config.eeer = abilities.eeer_val;
6717 	config.low_power_ctrl = abilities.d3_lpan;
6718 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6719 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6720 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6721 
6722 	if (err) {
6723 		dev_err(&pf->pdev->dev,
6724 			"set phy config ret =  %s last_status =  %s\n",
6725 			i40e_stat_str(&pf->hw, err),
6726 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6727 		return err;
6728 	}
6729 
6730 	/* Update the link info */
6731 	err = i40e_update_link_info(hw);
6732 	if (err) {
6733 		/* Wait a little bit (on 40G cards it sometimes takes a really
6734 		 * long time for link to come back from the atomic reset)
6735 		 * and try once more
6736 		 */
6737 		msleep(1000);
6738 		i40e_update_link_info(hw);
6739 	}
6740 
6741 	i40e_aq_set_link_restart_an(hw, true, NULL);
6742 
6743 	return I40E_SUCCESS;
6744 }
6745 
6746 /**
6747  * i40e_down - Shutdown the connection processing
6748  * @vsi: the VSI being stopped
6749  **/
6750 void i40e_down(struct i40e_vsi *vsi)
6751 {
6752 	int i;
6753 
6754 	/* It is assumed that the caller of this function
6755 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6756 	 */
6757 	if (vsi->netdev) {
6758 		netif_carrier_off(vsi->netdev);
6759 		netif_tx_disable(vsi->netdev);
6760 	}
6761 	i40e_vsi_disable_irq(vsi);
6762 	i40e_vsi_stop_rings(vsi);
6763 	if (vsi->type == I40E_VSI_MAIN &&
6764 	    vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6765 		i40e_force_link_state(vsi->back, false);
6766 	i40e_napi_disable_all(vsi);
6767 
6768 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6769 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6770 		if (i40e_enabled_xdp_vsi(vsi)) {
6771 			/* Make sure that in-progress ndo_xdp_xmit
6772 			 * calls are completed.
6773 			 */
6774 			synchronize_rcu();
6775 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6776 		}
6777 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6778 	}
6779 
6780 }
6781 
6782 /**
6783  * i40e_validate_mqprio_qopt- validate queue mapping info
6784  * @vsi: the VSI being configured
6785  * @mqprio_qopt: queue parametrs
6786  **/
6787 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6788 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6789 {
6790 	u64 sum_max_rate = 0;
6791 	u64 max_rate = 0;
6792 	int i;
6793 
6794 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6795 	    mqprio_qopt->qopt.num_tc < 1 ||
6796 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6797 		return -EINVAL;
6798 	for (i = 0; ; i++) {
6799 		if (!mqprio_qopt->qopt.count[i])
6800 			return -EINVAL;
6801 		if (mqprio_qopt->min_rate[i]) {
6802 			dev_err(&vsi->back->pdev->dev,
6803 				"Invalid min tx rate (greater than 0) specified\n");
6804 			return -EINVAL;
6805 		}
6806 		max_rate = mqprio_qopt->max_rate[i];
6807 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6808 		sum_max_rate += max_rate;
6809 
6810 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6811 			break;
6812 		if (mqprio_qopt->qopt.offset[i + 1] !=
6813 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6814 			return -EINVAL;
6815 	}
6816 	if (vsi->num_queue_pairs <
6817 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6818 		return -EINVAL;
6819 	}
6820 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6821 		dev_err(&vsi->back->pdev->dev,
6822 			"Invalid max tx rate specified\n");
6823 		return -EINVAL;
6824 	}
6825 	return 0;
6826 }
6827 
6828 /**
6829  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6830  * @vsi: the VSI being configured
6831  **/
6832 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6833 {
6834 	u16 qcount;
6835 	int i;
6836 
6837 	/* Only TC0 is enabled */
6838 	vsi->tc_config.numtc = 1;
6839 	vsi->tc_config.enabled_tc = 1;
6840 	qcount = min_t(int, vsi->alloc_queue_pairs,
6841 		       i40e_pf_get_max_q_per_tc(vsi->back));
6842 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6843 		/* For the TC that is not enabled set the offset to to default
6844 		 * queue and allocate one queue for the given TC.
6845 		 */
6846 		vsi->tc_config.tc_info[i].qoffset = 0;
6847 		if (i == 0)
6848 			vsi->tc_config.tc_info[i].qcount = qcount;
6849 		else
6850 			vsi->tc_config.tc_info[i].qcount = 1;
6851 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6852 	}
6853 }
6854 
6855 /**
6856  * i40e_setup_tc - configure multiple traffic classes
6857  * @netdev: net device to configure
6858  * @type_data: tc offload data
6859  **/
6860 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6861 {
6862 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6863 	struct i40e_netdev_priv *np = netdev_priv(netdev);
6864 	struct i40e_vsi *vsi = np->vsi;
6865 	struct i40e_pf *pf = vsi->back;
6866 	u8 enabled_tc = 0, num_tc, hw;
6867 	bool need_reset = false;
6868 	int old_queue_pairs;
6869 	int ret = -EINVAL;
6870 	u16 mode;
6871 	int i;
6872 
6873 	old_queue_pairs = vsi->num_queue_pairs;
6874 	num_tc = mqprio_qopt->qopt.num_tc;
6875 	hw = mqprio_qopt->qopt.hw;
6876 	mode = mqprio_qopt->mode;
6877 	if (!hw) {
6878 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6879 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6880 		goto config_tc;
6881 	}
6882 
6883 	/* Check if MFP enabled */
6884 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6885 		netdev_info(netdev,
6886 			    "Configuring TC not supported in MFP mode\n");
6887 		return ret;
6888 	}
6889 	switch (mode) {
6890 	case TC_MQPRIO_MODE_DCB:
6891 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6892 
6893 		/* Check if DCB enabled to continue */
6894 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6895 			netdev_info(netdev,
6896 				    "DCB is not enabled for adapter\n");
6897 			return ret;
6898 		}
6899 
6900 		/* Check whether tc count is within enabled limit */
6901 		if (num_tc > i40e_pf_get_num_tc(pf)) {
6902 			netdev_info(netdev,
6903 				    "TC count greater than enabled on link for adapter\n");
6904 			return ret;
6905 		}
6906 		break;
6907 	case TC_MQPRIO_MODE_CHANNEL:
6908 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6909 			netdev_info(netdev,
6910 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6911 			return ret;
6912 		}
6913 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6914 			return ret;
6915 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6916 		if (ret)
6917 			return ret;
6918 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6919 		       sizeof(*mqprio_qopt));
6920 		pf->flags |= I40E_FLAG_TC_MQPRIO;
6921 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6922 		break;
6923 	default:
6924 		return -EINVAL;
6925 	}
6926 
6927 config_tc:
6928 	/* Generate TC map for number of tc requested */
6929 	for (i = 0; i < num_tc; i++)
6930 		enabled_tc |= BIT(i);
6931 
6932 	/* Requesting same TC configuration as already enabled */
6933 	if (enabled_tc == vsi->tc_config.enabled_tc &&
6934 	    mode != TC_MQPRIO_MODE_CHANNEL)
6935 		return 0;
6936 
6937 	/* Quiesce VSI queues */
6938 	i40e_quiesce_vsi(vsi);
6939 
6940 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6941 		i40e_remove_queue_channels(vsi);
6942 
6943 	/* Configure VSI for enabled TCs */
6944 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
6945 	if (ret) {
6946 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6947 			    vsi->seid);
6948 		need_reset = true;
6949 		goto exit;
6950 	}
6951 
6952 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6953 		if (vsi->mqprio_qopt.max_rate[0]) {
6954 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6955 
6956 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6957 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6958 			if (!ret) {
6959 				u64 credits = max_tx_rate;
6960 
6961 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
6962 				dev_dbg(&vsi->back->pdev->dev,
6963 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6964 					max_tx_rate,
6965 					credits,
6966 					vsi->seid);
6967 			} else {
6968 				need_reset = true;
6969 				goto exit;
6970 			}
6971 		}
6972 		ret = i40e_configure_queue_channels(vsi);
6973 		if (ret) {
6974 			vsi->num_queue_pairs = old_queue_pairs;
6975 			netdev_info(netdev,
6976 				    "Failed configuring queue channels\n");
6977 			need_reset = true;
6978 			goto exit;
6979 		}
6980 	}
6981 
6982 exit:
6983 	/* Reset the configuration data to defaults, only TC0 is enabled */
6984 	if (need_reset) {
6985 		i40e_vsi_set_default_tc_config(vsi);
6986 		need_reset = false;
6987 	}
6988 
6989 	/* Unquiesce VSI */
6990 	i40e_unquiesce_vsi(vsi);
6991 	return ret;
6992 }
6993 
6994 /**
6995  * i40e_set_cld_element - sets cloud filter element data
6996  * @filter: cloud filter rule
6997  * @cld: ptr to cloud filter element data
6998  *
6999  * This is helper function to copy data into cloud filter element
7000  **/
7001 static inline void
7002 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7003 		     struct i40e_aqc_cloud_filters_element_data *cld)
7004 {
7005 	int i, j;
7006 	u32 ipa;
7007 
7008 	memset(cld, 0, sizeof(*cld));
7009 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7010 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7011 
7012 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7013 		return;
7014 
7015 	if (filter->n_proto == ETH_P_IPV6) {
7016 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7017 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
7018 		     i++, j += 2) {
7019 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7020 			ipa = cpu_to_le32(ipa);
7021 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
7022 		}
7023 	} else {
7024 		ipa = be32_to_cpu(filter->dst_ipv4);
7025 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7026 	}
7027 
7028 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7029 
7030 	/* tenant_id is not supported by FW now, once the support is enabled
7031 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7032 	 */
7033 	if (filter->tenant_id)
7034 		return;
7035 }
7036 
7037 /**
7038  * i40e_add_del_cloud_filter - Add/del cloud filter
7039  * @vsi: pointer to VSI
7040  * @filter: cloud filter rule
7041  * @add: if true, add, if false, delete
7042  *
7043  * Add or delete a cloud filter for a specific flow spec.
7044  * Returns 0 if the filter were successfully added.
7045  **/
7046 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7047 			      struct i40e_cloud_filter *filter, bool add)
7048 {
7049 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7050 	struct i40e_pf *pf = vsi->back;
7051 	int ret;
7052 	static const u16 flag_table[128] = {
7053 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7054 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7055 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7056 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7057 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7058 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7059 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7060 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7061 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7062 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7063 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7064 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7065 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7066 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7067 	};
7068 
7069 	if (filter->flags >= ARRAY_SIZE(flag_table))
7070 		return I40E_ERR_CONFIG;
7071 
7072 	/* copy element needed to add cloud filter from filter */
7073 	i40e_set_cld_element(filter, &cld_filter);
7074 
7075 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7076 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7077 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7078 
7079 	if (filter->n_proto == ETH_P_IPV6)
7080 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7081 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7082 	else
7083 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7084 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7085 
7086 	if (add)
7087 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7088 						&cld_filter, 1);
7089 	else
7090 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7091 						&cld_filter, 1);
7092 	if (ret)
7093 		dev_dbg(&pf->pdev->dev,
7094 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7095 			add ? "add" : "delete", filter->dst_port, ret,
7096 			pf->hw.aq.asq_last_status);
7097 	else
7098 		dev_info(&pf->pdev->dev,
7099 			 "%s cloud filter for VSI: %d\n",
7100 			 add ? "Added" : "Deleted", filter->seid);
7101 	return ret;
7102 }
7103 
7104 /**
7105  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7106  * @vsi: pointer to VSI
7107  * @filter: cloud filter rule
7108  * @add: if true, add, if false, delete
7109  *
7110  * Add or delete a cloud filter for a specific flow spec using big buffer.
7111  * Returns 0 if the filter were successfully added.
7112  **/
7113 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7114 				      struct i40e_cloud_filter *filter,
7115 				      bool add)
7116 {
7117 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7118 	struct i40e_pf *pf = vsi->back;
7119 	int ret;
7120 
7121 	/* Both (src/dst) valid mac_addr are not supported */
7122 	if ((is_valid_ether_addr(filter->dst_mac) &&
7123 	     is_valid_ether_addr(filter->src_mac)) ||
7124 	    (is_multicast_ether_addr(filter->dst_mac) &&
7125 	     is_multicast_ether_addr(filter->src_mac)))
7126 		return -EOPNOTSUPP;
7127 
7128 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7129 	 * ports are not supported via big buffer now.
7130 	 */
7131 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7132 		return -EOPNOTSUPP;
7133 
7134 	/* adding filter using src_port/src_ip is not supported at this stage */
7135 	if (filter->src_port || filter->src_ipv4 ||
7136 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7137 		return -EOPNOTSUPP;
7138 
7139 	/* copy element needed to add cloud filter from filter */
7140 	i40e_set_cld_element(filter, &cld_filter.element);
7141 
7142 	if (is_valid_ether_addr(filter->dst_mac) ||
7143 	    is_valid_ether_addr(filter->src_mac) ||
7144 	    is_multicast_ether_addr(filter->dst_mac) ||
7145 	    is_multicast_ether_addr(filter->src_mac)) {
7146 		/* MAC + IP : unsupported mode */
7147 		if (filter->dst_ipv4)
7148 			return -EOPNOTSUPP;
7149 
7150 		/* since we validated that L4 port must be valid before
7151 		 * we get here, start with respective "flags" value
7152 		 * and update if vlan is present or not
7153 		 */
7154 		cld_filter.element.flags =
7155 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7156 
7157 		if (filter->vlan_id) {
7158 			cld_filter.element.flags =
7159 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7160 		}
7161 
7162 	} else if (filter->dst_ipv4 ||
7163 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7164 		cld_filter.element.flags =
7165 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7166 		if (filter->n_proto == ETH_P_IPV6)
7167 			cld_filter.element.flags |=
7168 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7169 		else
7170 			cld_filter.element.flags |=
7171 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7172 	} else {
7173 		dev_err(&pf->pdev->dev,
7174 			"either mac or ip has to be valid for cloud filter\n");
7175 		return -EINVAL;
7176 	}
7177 
7178 	/* Now copy L4 port in Byte 6..7 in general fields */
7179 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7180 						be16_to_cpu(filter->dst_port);
7181 
7182 	if (add) {
7183 		/* Validate current device switch mode, change if necessary */
7184 		ret = i40e_validate_and_set_switch_mode(vsi);
7185 		if (ret) {
7186 			dev_err(&pf->pdev->dev,
7187 				"failed to set switch mode, ret %d\n",
7188 				ret);
7189 			return ret;
7190 		}
7191 
7192 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7193 						   &cld_filter, 1);
7194 	} else {
7195 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7196 						   &cld_filter, 1);
7197 	}
7198 
7199 	if (ret)
7200 		dev_dbg(&pf->pdev->dev,
7201 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7202 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7203 	else
7204 		dev_info(&pf->pdev->dev,
7205 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7206 			 add ? "add" : "delete", filter->seid,
7207 			 ntohs(filter->dst_port));
7208 	return ret;
7209 }
7210 
7211 /**
7212  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7213  * @vsi: Pointer to VSI
7214  * @cls_flower: Pointer to struct tc_cls_flower_offload
7215  * @filter: Pointer to cloud filter structure
7216  *
7217  **/
7218 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7219 				 struct tc_cls_flower_offload *f,
7220 				 struct i40e_cloud_filter *filter)
7221 {
7222 	struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
7223 	struct flow_dissector *dissector = rule->match.dissector;
7224 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7225 	struct i40e_pf *pf = vsi->back;
7226 	u8 field_flags = 0;
7227 
7228 	if (dissector->used_keys &
7229 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7230 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7231 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7232 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7233 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7234 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7235 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7236 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7237 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7238 			dissector->used_keys);
7239 		return -EOPNOTSUPP;
7240 	}
7241 
7242 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7243 		struct flow_match_enc_keyid match;
7244 
7245 		flow_rule_match_enc_keyid(rule, &match);
7246 		if (match.mask->keyid != 0)
7247 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7248 
7249 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7250 	}
7251 
7252 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7253 		struct flow_match_basic match;
7254 
7255 		flow_rule_match_basic(rule, &match);
7256 		n_proto_key = ntohs(match.key->n_proto);
7257 		n_proto_mask = ntohs(match.mask->n_proto);
7258 
7259 		if (n_proto_key == ETH_P_ALL) {
7260 			n_proto_key = 0;
7261 			n_proto_mask = 0;
7262 		}
7263 		filter->n_proto = n_proto_key & n_proto_mask;
7264 		filter->ip_proto = match.key->ip_proto;
7265 	}
7266 
7267 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7268 		struct flow_match_eth_addrs match;
7269 
7270 		flow_rule_match_eth_addrs(rule, &match);
7271 
7272 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7273 		if (!is_zero_ether_addr(match.mask->dst)) {
7274 			if (is_broadcast_ether_addr(match.mask->dst)) {
7275 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7276 			} else {
7277 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7278 					match.mask->dst);
7279 				return I40E_ERR_CONFIG;
7280 			}
7281 		}
7282 
7283 		if (!is_zero_ether_addr(match.mask->src)) {
7284 			if (is_broadcast_ether_addr(match.mask->src)) {
7285 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7286 			} else {
7287 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7288 					match.mask->src);
7289 				return I40E_ERR_CONFIG;
7290 			}
7291 		}
7292 		ether_addr_copy(filter->dst_mac, match.key->dst);
7293 		ether_addr_copy(filter->src_mac, match.key->src);
7294 	}
7295 
7296 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7297 		struct flow_match_vlan match;
7298 
7299 		flow_rule_match_vlan(rule, &match);
7300 		if (match.mask->vlan_id) {
7301 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7302 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7303 
7304 			} else {
7305 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7306 					match.mask->vlan_id);
7307 				return I40E_ERR_CONFIG;
7308 			}
7309 		}
7310 
7311 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7312 	}
7313 
7314 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7315 		struct flow_match_control match;
7316 
7317 		flow_rule_match_control(rule, &match);
7318 		addr_type = match.key->addr_type;
7319 	}
7320 
7321 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7322 		struct flow_match_ipv4_addrs match;
7323 
7324 		flow_rule_match_ipv4_addrs(rule, &match);
7325 		if (match.mask->dst) {
7326 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7327 				field_flags |= I40E_CLOUD_FIELD_IIP;
7328 			} else {
7329 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7330 					&match.mask->dst);
7331 				return I40E_ERR_CONFIG;
7332 			}
7333 		}
7334 
7335 		if (match.mask->src) {
7336 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7337 				field_flags |= I40E_CLOUD_FIELD_IIP;
7338 			} else {
7339 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7340 					&match.mask->src);
7341 				return I40E_ERR_CONFIG;
7342 			}
7343 		}
7344 
7345 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7346 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7347 			return I40E_ERR_CONFIG;
7348 		}
7349 		filter->dst_ipv4 = match.key->dst;
7350 		filter->src_ipv4 = match.key->src;
7351 	}
7352 
7353 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7354 		struct flow_match_ipv6_addrs match;
7355 
7356 		flow_rule_match_ipv6_addrs(rule, &match);
7357 
7358 		/* src and dest IPV6 address should not be LOOPBACK
7359 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7360 		 */
7361 		if (ipv6_addr_loopback(&match.key->dst) ||
7362 		    ipv6_addr_loopback(&match.key->src)) {
7363 			dev_err(&pf->pdev->dev,
7364 				"Bad ipv6, addr is LOOPBACK\n");
7365 			return I40E_ERR_CONFIG;
7366 		}
7367 		if (!ipv6_addr_any(&match.mask->dst) ||
7368 		    !ipv6_addr_any(&match.mask->src))
7369 			field_flags |= I40E_CLOUD_FIELD_IIP;
7370 
7371 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7372 		       sizeof(filter->src_ipv6));
7373 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7374 		       sizeof(filter->dst_ipv6));
7375 	}
7376 
7377 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7378 		struct flow_match_ports match;
7379 
7380 		flow_rule_match_ports(rule, &match);
7381 		if (match.mask->src) {
7382 			if (match.mask->src == cpu_to_be16(0xffff)) {
7383 				field_flags |= I40E_CLOUD_FIELD_IIP;
7384 			} else {
7385 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7386 					be16_to_cpu(match.mask->src));
7387 				return I40E_ERR_CONFIG;
7388 			}
7389 		}
7390 
7391 		if (match.mask->dst) {
7392 			if (match.mask->dst == cpu_to_be16(0xffff)) {
7393 				field_flags |= I40E_CLOUD_FIELD_IIP;
7394 			} else {
7395 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7396 					be16_to_cpu(match.mask->dst));
7397 				return I40E_ERR_CONFIG;
7398 			}
7399 		}
7400 
7401 		filter->dst_port = match.key->dst;
7402 		filter->src_port = match.key->src;
7403 
7404 		switch (filter->ip_proto) {
7405 		case IPPROTO_TCP:
7406 		case IPPROTO_UDP:
7407 			break;
7408 		default:
7409 			dev_err(&pf->pdev->dev,
7410 				"Only UDP and TCP transport are supported\n");
7411 			return -EINVAL;
7412 		}
7413 	}
7414 	filter->flags = field_flags;
7415 	return 0;
7416 }
7417 
7418 /**
7419  * i40e_handle_tclass: Forward to a traffic class on the device
7420  * @vsi: Pointer to VSI
7421  * @tc: traffic class index on the device
7422  * @filter: Pointer to cloud filter structure
7423  *
7424  **/
7425 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7426 			      struct i40e_cloud_filter *filter)
7427 {
7428 	struct i40e_channel *ch, *ch_tmp;
7429 
7430 	/* direct to a traffic class on the same device */
7431 	if (tc == 0) {
7432 		filter->seid = vsi->seid;
7433 		return 0;
7434 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7435 		if (!filter->dst_port) {
7436 			dev_err(&vsi->back->pdev->dev,
7437 				"Specify destination port to direct to traffic class that is not default\n");
7438 			return -EINVAL;
7439 		}
7440 		if (list_empty(&vsi->ch_list))
7441 			return -EINVAL;
7442 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7443 					 list) {
7444 			if (ch->seid == vsi->tc_seid_map[tc])
7445 				filter->seid = ch->seid;
7446 		}
7447 		return 0;
7448 	}
7449 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7450 	return -EINVAL;
7451 }
7452 
7453 /**
7454  * i40e_configure_clsflower - Configure tc flower filters
7455  * @vsi: Pointer to VSI
7456  * @cls_flower: Pointer to struct tc_cls_flower_offload
7457  *
7458  **/
7459 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7460 				    struct tc_cls_flower_offload *cls_flower)
7461 {
7462 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7463 	struct i40e_cloud_filter *filter = NULL;
7464 	struct i40e_pf *pf = vsi->back;
7465 	int err = 0;
7466 
7467 	if (tc < 0) {
7468 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7469 		return -EOPNOTSUPP;
7470 	}
7471 
7472 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7473 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7474 		return -EBUSY;
7475 
7476 	if (pf->fdir_pf_active_filters ||
7477 	    (!hlist_empty(&pf->fdir_filter_list))) {
7478 		dev_err(&vsi->back->pdev->dev,
7479 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7480 		return -EINVAL;
7481 	}
7482 
7483 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7484 		dev_err(&vsi->back->pdev->dev,
7485 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7486 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7487 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7488 	}
7489 
7490 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7491 	if (!filter)
7492 		return -ENOMEM;
7493 
7494 	filter->cookie = cls_flower->cookie;
7495 
7496 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7497 	if (err < 0)
7498 		goto err;
7499 
7500 	err = i40e_handle_tclass(vsi, tc, filter);
7501 	if (err < 0)
7502 		goto err;
7503 
7504 	/* Add cloud filter */
7505 	if (filter->dst_port)
7506 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7507 	else
7508 		err = i40e_add_del_cloud_filter(vsi, filter, true);
7509 
7510 	if (err) {
7511 		dev_err(&pf->pdev->dev,
7512 			"Failed to add cloud filter, err %s\n",
7513 			i40e_stat_str(&pf->hw, err));
7514 		goto err;
7515 	}
7516 
7517 	/* add filter to the ordered list */
7518 	INIT_HLIST_NODE(&filter->cloud_node);
7519 
7520 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7521 
7522 	pf->num_cloud_filters++;
7523 
7524 	return err;
7525 err:
7526 	kfree(filter);
7527 	return err;
7528 }
7529 
7530 /**
7531  * i40e_find_cloud_filter - Find the could filter in the list
7532  * @vsi: Pointer to VSI
7533  * @cookie: filter specific cookie
7534  *
7535  **/
7536 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7537 							unsigned long *cookie)
7538 {
7539 	struct i40e_cloud_filter *filter = NULL;
7540 	struct hlist_node *node2;
7541 
7542 	hlist_for_each_entry_safe(filter, node2,
7543 				  &vsi->back->cloud_filter_list, cloud_node)
7544 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7545 			return filter;
7546 	return NULL;
7547 }
7548 
7549 /**
7550  * i40e_delete_clsflower - Remove tc flower filters
7551  * @vsi: Pointer to VSI
7552  * @cls_flower: Pointer to struct tc_cls_flower_offload
7553  *
7554  **/
7555 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7556 				 struct tc_cls_flower_offload *cls_flower)
7557 {
7558 	struct i40e_cloud_filter *filter = NULL;
7559 	struct i40e_pf *pf = vsi->back;
7560 	int err = 0;
7561 
7562 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7563 
7564 	if (!filter)
7565 		return -EINVAL;
7566 
7567 	hash_del(&filter->cloud_node);
7568 
7569 	if (filter->dst_port)
7570 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7571 	else
7572 		err = i40e_add_del_cloud_filter(vsi, filter, false);
7573 
7574 	kfree(filter);
7575 	if (err) {
7576 		dev_err(&pf->pdev->dev,
7577 			"Failed to delete cloud filter, err %s\n",
7578 			i40e_stat_str(&pf->hw, err));
7579 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7580 	}
7581 
7582 	pf->num_cloud_filters--;
7583 	if (!pf->num_cloud_filters)
7584 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7585 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7586 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7587 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7588 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7589 		}
7590 	return 0;
7591 }
7592 
7593 /**
7594  * i40e_setup_tc_cls_flower - flower classifier offloads
7595  * @netdev: net device to configure
7596  * @type_data: offload data
7597  **/
7598 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7599 				    struct tc_cls_flower_offload *cls_flower)
7600 {
7601 	struct i40e_vsi *vsi = np->vsi;
7602 
7603 	switch (cls_flower->command) {
7604 	case TC_CLSFLOWER_REPLACE:
7605 		return i40e_configure_clsflower(vsi, cls_flower);
7606 	case TC_CLSFLOWER_DESTROY:
7607 		return i40e_delete_clsflower(vsi, cls_flower);
7608 	case TC_CLSFLOWER_STATS:
7609 		return -EOPNOTSUPP;
7610 	default:
7611 		return -EOPNOTSUPP;
7612 	}
7613 }
7614 
7615 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7616 				  void *cb_priv)
7617 {
7618 	struct i40e_netdev_priv *np = cb_priv;
7619 
7620 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
7621 		return -EOPNOTSUPP;
7622 
7623 	switch (type) {
7624 	case TC_SETUP_CLSFLOWER:
7625 		return i40e_setup_tc_cls_flower(np, type_data);
7626 
7627 	default:
7628 		return -EOPNOTSUPP;
7629 	}
7630 }
7631 
7632 static int i40e_setup_tc_block(struct net_device *dev,
7633 			       struct tc_block_offload *f)
7634 {
7635 	struct i40e_netdev_priv *np = netdev_priv(dev);
7636 
7637 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7638 		return -EOPNOTSUPP;
7639 
7640 	switch (f->command) {
7641 	case TC_BLOCK_BIND:
7642 		return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7643 					     np, np, f->extack);
7644 	case TC_BLOCK_UNBIND:
7645 		tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7646 		return 0;
7647 	default:
7648 		return -EOPNOTSUPP;
7649 	}
7650 }
7651 
7652 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7653 			   void *type_data)
7654 {
7655 	switch (type) {
7656 	case TC_SETUP_QDISC_MQPRIO:
7657 		return i40e_setup_tc(netdev, type_data);
7658 	case TC_SETUP_BLOCK:
7659 		return i40e_setup_tc_block(netdev, type_data);
7660 	default:
7661 		return -EOPNOTSUPP;
7662 	}
7663 }
7664 
7665 /**
7666  * i40e_open - Called when a network interface is made active
7667  * @netdev: network interface device structure
7668  *
7669  * The open entry point is called when a network interface is made
7670  * active by the system (IFF_UP).  At this point all resources needed
7671  * for transmit and receive operations are allocated, the interrupt
7672  * handler is registered with the OS, the netdev watchdog subtask is
7673  * enabled, and the stack is notified that the interface is ready.
7674  *
7675  * Returns 0 on success, negative value on failure
7676  **/
7677 int i40e_open(struct net_device *netdev)
7678 {
7679 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7680 	struct i40e_vsi *vsi = np->vsi;
7681 	struct i40e_pf *pf = vsi->back;
7682 	int err;
7683 
7684 	/* disallow open during test or if eeprom is broken */
7685 	if (test_bit(__I40E_TESTING, pf->state) ||
7686 	    test_bit(__I40E_BAD_EEPROM, pf->state))
7687 		return -EBUSY;
7688 
7689 	netif_carrier_off(netdev);
7690 
7691 	if (i40e_force_link_state(pf, true))
7692 		return -EAGAIN;
7693 
7694 	err = i40e_vsi_open(vsi);
7695 	if (err)
7696 		return err;
7697 
7698 	/* configure global TSO hardware offload settings */
7699 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7700 						       TCP_FLAG_FIN) >> 16);
7701 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7702 						       TCP_FLAG_FIN |
7703 						       TCP_FLAG_CWR) >> 16);
7704 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7705 
7706 	udp_tunnel_get_rx_info(netdev);
7707 
7708 	return 0;
7709 }
7710 
7711 /**
7712  * i40e_vsi_open -
7713  * @vsi: the VSI to open
7714  *
7715  * Finish initialization of the VSI.
7716  *
7717  * Returns 0 on success, negative value on failure
7718  *
7719  * Note: expects to be called while under rtnl_lock()
7720  **/
7721 int i40e_vsi_open(struct i40e_vsi *vsi)
7722 {
7723 	struct i40e_pf *pf = vsi->back;
7724 	char int_name[I40E_INT_NAME_STR_LEN];
7725 	int err;
7726 
7727 	/* allocate descriptors */
7728 	err = i40e_vsi_setup_tx_resources(vsi);
7729 	if (err)
7730 		goto err_setup_tx;
7731 	err = i40e_vsi_setup_rx_resources(vsi);
7732 	if (err)
7733 		goto err_setup_rx;
7734 
7735 	err = i40e_vsi_configure(vsi);
7736 	if (err)
7737 		goto err_setup_rx;
7738 
7739 	if (vsi->netdev) {
7740 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7741 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7742 		err = i40e_vsi_request_irq(vsi, int_name);
7743 		if (err)
7744 			goto err_setup_rx;
7745 
7746 		/* Notify the stack of the actual queue counts. */
7747 		err = netif_set_real_num_tx_queues(vsi->netdev,
7748 						   vsi->num_queue_pairs);
7749 		if (err)
7750 			goto err_set_queues;
7751 
7752 		err = netif_set_real_num_rx_queues(vsi->netdev,
7753 						   vsi->num_queue_pairs);
7754 		if (err)
7755 			goto err_set_queues;
7756 
7757 	} else if (vsi->type == I40E_VSI_FDIR) {
7758 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7759 			 dev_driver_string(&pf->pdev->dev),
7760 			 dev_name(&pf->pdev->dev));
7761 		err = i40e_vsi_request_irq(vsi, int_name);
7762 
7763 	} else {
7764 		err = -EINVAL;
7765 		goto err_setup_rx;
7766 	}
7767 
7768 	err = i40e_up_complete(vsi);
7769 	if (err)
7770 		goto err_up_complete;
7771 
7772 	return 0;
7773 
7774 err_up_complete:
7775 	i40e_down(vsi);
7776 err_set_queues:
7777 	i40e_vsi_free_irq(vsi);
7778 err_setup_rx:
7779 	i40e_vsi_free_rx_resources(vsi);
7780 err_setup_tx:
7781 	i40e_vsi_free_tx_resources(vsi);
7782 	if (vsi == pf->vsi[pf->lan_vsi])
7783 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7784 
7785 	return err;
7786 }
7787 
7788 /**
7789  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7790  * @pf: Pointer to PF
7791  *
7792  * This function destroys the hlist where all the Flow Director
7793  * filters were saved.
7794  **/
7795 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7796 {
7797 	struct i40e_fdir_filter *filter;
7798 	struct i40e_flex_pit *pit_entry, *tmp;
7799 	struct hlist_node *node2;
7800 
7801 	hlist_for_each_entry_safe(filter, node2,
7802 				  &pf->fdir_filter_list, fdir_node) {
7803 		hlist_del(&filter->fdir_node);
7804 		kfree(filter);
7805 	}
7806 
7807 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7808 		list_del(&pit_entry->list);
7809 		kfree(pit_entry);
7810 	}
7811 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7812 
7813 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7814 		list_del(&pit_entry->list);
7815 		kfree(pit_entry);
7816 	}
7817 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7818 
7819 	pf->fdir_pf_active_filters = 0;
7820 	pf->fd_tcp4_filter_cnt = 0;
7821 	pf->fd_udp4_filter_cnt = 0;
7822 	pf->fd_sctp4_filter_cnt = 0;
7823 	pf->fd_ip4_filter_cnt = 0;
7824 
7825 	/* Reprogram the default input set for TCP/IPv4 */
7826 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7827 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7828 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7829 
7830 	/* Reprogram the default input set for UDP/IPv4 */
7831 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7832 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7833 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7834 
7835 	/* Reprogram the default input set for SCTP/IPv4 */
7836 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7837 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7838 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7839 
7840 	/* Reprogram the default input set for Other/IPv4 */
7841 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7842 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7843 
7844 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7845 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7846 }
7847 
7848 /**
7849  * i40e_cloud_filter_exit - Cleans up the cloud filters
7850  * @pf: Pointer to PF
7851  *
7852  * This function destroys the hlist where all the cloud filters
7853  * were saved.
7854  **/
7855 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7856 {
7857 	struct i40e_cloud_filter *cfilter;
7858 	struct hlist_node *node;
7859 
7860 	hlist_for_each_entry_safe(cfilter, node,
7861 				  &pf->cloud_filter_list, cloud_node) {
7862 		hlist_del(&cfilter->cloud_node);
7863 		kfree(cfilter);
7864 	}
7865 	pf->num_cloud_filters = 0;
7866 
7867 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7868 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7869 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7870 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7871 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7872 	}
7873 }
7874 
7875 /**
7876  * i40e_close - Disables a network interface
7877  * @netdev: network interface device structure
7878  *
7879  * The close entry point is called when an interface is de-activated
7880  * by the OS.  The hardware is still under the driver's control, but
7881  * this netdev interface is disabled.
7882  *
7883  * Returns 0, this is not allowed to fail
7884  **/
7885 int i40e_close(struct net_device *netdev)
7886 {
7887 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7888 	struct i40e_vsi *vsi = np->vsi;
7889 
7890 	i40e_vsi_close(vsi);
7891 
7892 	return 0;
7893 }
7894 
7895 /**
7896  * i40e_do_reset - Start a PF or Core Reset sequence
7897  * @pf: board private structure
7898  * @reset_flags: which reset is requested
7899  * @lock_acquired: indicates whether or not the lock has been acquired
7900  * before this function was called.
7901  *
7902  * The essential difference in resets is that the PF Reset
7903  * doesn't clear the packet buffers, doesn't reset the PE
7904  * firmware, and doesn't bother the other PFs on the chip.
7905  **/
7906 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7907 {
7908 	u32 val;
7909 
7910 	WARN_ON(in_interrupt());
7911 
7912 
7913 	/* do the biggest reset indicated */
7914 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7915 
7916 		/* Request a Global Reset
7917 		 *
7918 		 * This will start the chip's countdown to the actual full
7919 		 * chip reset event, and a warning interrupt to be sent
7920 		 * to all PFs, including the requestor.  Our handler
7921 		 * for the warning interrupt will deal with the shutdown
7922 		 * and recovery of the switch setup.
7923 		 */
7924 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7925 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7926 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7927 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7928 
7929 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7930 
7931 		/* Request a Core Reset
7932 		 *
7933 		 * Same as Global Reset, except does *not* include the MAC/PHY
7934 		 */
7935 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7936 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7937 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
7938 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7939 		i40e_flush(&pf->hw);
7940 
7941 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
7942 
7943 		/* Request a PF Reset
7944 		 *
7945 		 * Resets only the PF-specific registers
7946 		 *
7947 		 * This goes directly to the tear-down and rebuild of
7948 		 * the switch, since we need to do all the recovery as
7949 		 * for the Core Reset.
7950 		 */
7951 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
7952 		i40e_handle_reset_warning(pf, lock_acquired);
7953 
7954 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7955 		int v;
7956 
7957 		/* Find the VSI(s) that requested a re-init */
7958 		dev_info(&pf->pdev->dev,
7959 			 "VSI reinit requested\n");
7960 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7961 			struct i40e_vsi *vsi = pf->vsi[v];
7962 
7963 			if (vsi != NULL &&
7964 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7965 					       vsi->state))
7966 				i40e_vsi_reinit_locked(pf->vsi[v]);
7967 		}
7968 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7969 		int v;
7970 
7971 		/* Find the VSI(s) that needs to be brought down */
7972 		dev_info(&pf->pdev->dev, "VSI down requested\n");
7973 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7974 			struct i40e_vsi *vsi = pf->vsi[v];
7975 
7976 			if (vsi != NULL &&
7977 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7978 					       vsi->state)) {
7979 				set_bit(__I40E_VSI_DOWN, vsi->state);
7980 				i40e_down(vsi);
7981 			}
7982 		}
7983 	} else {
7984 		dev_info(&pf->pdev->dev,
7985 			 "bad reset request 0x%08x\n", reset_flags);
7986 	}
7987 }
7988 
7989 #ifdef CONFIG_I40E_DCB
7990 /**
7991  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7992  * @pf: board private structure
7993  * @old_cfg: current DCB config
7994  * @new_cfg: new DCB config
7995  **/
7996 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7997 			    struct i40e_dcbx_config *old_cfg,
7998 			    struct i40e_dcbx_config *new_cfg)
7999 {
8000 	bool need_reconfig = false;
8001 
8002 	/* Check if ETS configuration has changed */
8003 	if (memcmp(&new_cfg->etscfg,
8004 		   &old_cfg->etscfg,
8005 		   sizeof(new_cfg->etscfg))) {
8006 		/* If Priority Table has changed reconfig is needed */
8007 		if (memcmp(&new_cfg->etscfg.prioritytable,
8008 			   &old_cfg->etscfg.prioritytable,
8009 			   sizeof(new_cfg->etscfg.prioritytable))) {
8010 			need_reconfig = true;
8011 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8012 		}
8013 
8014 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8015 			   &old_cfg->etscfg.tcbwtable,
8016 			   sizeof(new_cfg->etscfg.tcbwtable)))
8017 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8018 
8019 		if (memcmp(&new_cfg->etscfg.tsatable,
8020 			   &old_cfg->etscfg.tsatable,
8021 			   sizeof(new_cfg->etscfg.tsatable)))
8022 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8023 	}
8024 
8025 	/* Check if PFC configuration has changed */
8026 	if (memcmp(&new_cfg->pfc,
8027 		   &old_cfg->pfc,
8028 		   sizeof(new_cfg->pfc))) {
8029 		need_reconfig = true;
8030 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8031 	}
8032 
8033 	/* Check if APP Table has changed */
8034 	if (memcmp(&new_cfg->app,
8035 		   &old_cfg->app,
8036 		   sizeof(new_cfg->app))) {
8037 		need_reconfig = true;
8038 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8039 	}
8040 
8041 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8042 	return need_reconfig;
8043 }
8044 
8045 /**
8046  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8047  * @pf: board private structure
8048  * @e: event info posted on ARQ
8049  **/
8050 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8051 				  struct i40e_arq_event_info *e)
8052 {
8053 	struct i40e_aqc_lldp_get_mib *mib =
8054 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8055 	struct i40e_hw *hw = &pf->hw;
8056 	struct i40e_dcbx_config tmp_dcbx_cfg;
8057 	bool need_reconfig = false;
8058 	int ret = 0;
8059 	u8 type;
8060 
8061 	/* Not DCB capable or capability disabled */
8062 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8063 		return ret;
8064 
8065 	/* Ignore if event is not for Nearest Bridge */
8066 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8067 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8068 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8069 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8070 		return ret;
8071 
8072 	/* Check MIB Type and return if event for Remote MIB update */
8073 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8074 	dev_dbg(&pf->pdev->dev,
8075 		"LLDP event mib type %s\n", type ? "remote" : "local");
8076 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8077 		/* Update the remote cached instance and return */
8078 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8079 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8080 				&hw->remote_dcbx_config);
8081 		goto exit;
8082 	}
8083 
8084 	/* Store the old configuration */
8085 	tmp_dcbx_cfg = hw->local_dcbx_config;
8086 
8087 	/* Reset the old DCBx configuration data */
8088 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8089 	/* Get updated DCBX data from firmware */
8090 	ret = i40e_get_dcb_config(&pf->hw);
8091 	if (ret) {
8092 		dev_info(&pf->pdev->dev,
8093 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8094 			 i40e_stat_str(&pf->hw, ret),
8095 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8096 		goto exit;
8097 	}
8098 
8099 	/* No change detected in DCBX configs */
8100 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8101 		    sizeof(tmp_dcbx_cfg))) {
8102 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8103 		goto exit;
8104 	}
8105 
8106 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8107 					       &hw->local_dcbx_config);
8108 
8109 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8110 
8111 	if (!need_reconfig)
8112 		goto exit;
8113 
8114 	/* Enable DCB tagging only when more than one TC */
8115 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8116 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8117 	else
8118 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8119 
8120 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8121 	/* Reconfiguration needed quiesce all VSIs */
8122 	i40e_pf_quiesce_all_vsi(pf);
8123 
8124 	/* Changes in configuration update VEB/VSI */
8125 	i40e_dcb_reconfigure(pf);
8126 
8127 	ret = i40e_resume_port_tx(pf);
8128 
8129 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8130 	/* In case of error no point in resuming VSIs */
8131 	if (ret)
8132 		goto exit;
8133 
8134 	/* Wait for the PF's queues to be disabled */
8135 	ret = i40e_pf_wait_queues_disabled(pf);
8136 	if (ret) {
8137 		/* Schedule PF reset to recover */
8138 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8139 		i40e_service_event_schedule(pf);
8140 	} else {
8141 		i40e_pf_unquiesce_all_vsi(pf);
8142 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8143 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8144 	}
8145 
8146 exit:
8147 	return ret;
8148 }
8149 #endif /* CONFIG_I40E_DCB */
8150 
8151 /**
8152  * i40e_do_reset_safe - Protected reset path for userland calls.
8153  * @pf: board private structure
8154  * @reset_flags: which reset is requested
8155  *
8156  **/
8157 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8158 {
8159 	rtnl_lock();
8160 	i40e_do_reset(pf, reset_flags, true);
8161 	rtnl_unlock();
8162 }
8163 
8164 /**
8165  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8166  * @pf: board private structure
8167  * @e: event info posted on ARQ
8168  *
8169  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8170  * and VF queues
8171  **/
8172 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8173 					   struct i40e_arq_event_info *e)
8174 {
8175 	struct i40e_aqc_lan_overflow *data =
8176 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8177 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8178 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8179 	struct i40e_hw *hw = &pf->hw;
8180 	struct i40e_vf *vf;
8181 	u16 vf_id;
8182 
8183 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8184 		queue, qtx_ctl);
8185 
8186 	/* Queue belongs to VF, find the VF and issue VF reset */
8187 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8188 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8189 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8190 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8191 		vf_id -= hw->func_caps.vf_base_id;
8192 		vf = &pf->vf[vf_id];
8193 		i40e_vc_notify_vf_reset(vf);
8194 		/* Allow VF to process pending reset notification */
8195 		msleep(20);
8196 		i40e_reset_vf(vf, false);
8197 	}
8198 }
8199 
8200 /**
8201  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8202  * @pf: board private structure
8203  **/
8204 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8205 {
8206 	u32 val, fcnt_prog;
8207 
8208 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8209 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8210 	return fcnt_prog;
8211 }
8212 
8213 /**
8214  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8215  * @pf: board private structure
8216  **/
8217 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8218 {
8219 	u32 val, fcnt_prog;
8220 
8221 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8222 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8223 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8224 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8225 	return fcnt_prog;
8226 }
8227 
8228 /**
8229  * i40e_get_global_fd_count - Get total FD filters programmed on device
8230  * @pf: board private structure
8231  **/
8232 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8233 {
8234 	u32 val, fcnt_prog;
8235 
8236 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8237 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8238 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8239 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8240 	return fcnt_prog;
8241 }
8242 
8243 /**
8244  * i40e_reenable_fdir_sb - Restore FDir SB capability
8245  * @pf: board private structure
8246  **/
8247 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8248 {
8249 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8250 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8251 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8252 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8253 }
8254 
8255 /**
8256  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8257  * @pf: board private structure
8258  **/
8259 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8260 {
8261 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8262 		/* ATR uses the same filtering logic as SB rules. It only
8263 		 * functions properly if the input set mask is at the default
8264 		 * settings. It is safe to restore the default input set
8265 		 * because there are no active TCPv4 filter rules.
8266 		 */
8267 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8268 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8269 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8270 
8271 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8272 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8273 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8274 	}
8275 }
8276 
8277 /**
8278  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8279  * @pf: board private structure
8280  * @filter: FDir filter to remove
8281  */
8282 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8283 				       struct i40e_fdir_filter *filter)
8284 {
8285 	/* Update counters */
8286 	pf->fdir_pf_active_filters--;
8287 	pf->fd_inv = 0;
8288 
8289 	switch (filter->flow_type) {
8290 	case TCP_V4_FLOW:
8291 		pf->fd_tcp4_filter_cnt--;
8292 		break;
8293 	case UDP_V4_FLOW:
8294 		pf->fd_udp4_filter_cnt--;
8295 		break;
8296 	case SCTP_V4_FLOW:
8297 		pf->fd_sctp4_filter_cnt--;
8298 		break;
8299 	case IP_USER_FLOW:
8300 		switch (filter->ip4_proto) {
8301 		case IPPROTO_TCP:
8302 			pf->fd_tcp4_filter_cnt--;
8303 			break;
8304 		case IPPROTO_UDP:
8305 			pf->fd_udp4_filter_cnt--;
8306 			break;
8307 		case IPPROTO_SCTP:
8308 			pf->fd_sctp4_filter_cnt--;
8309 			break;
8310 		case IPPROTO_IP:
8311 			pf->fd_ip4_filter_cnt--;
8312 			break;
8313 		}
8314 		break;
8315 	}
8316 
8317 	/* Remove the filter from the list and free memory */
8318 	hlist_del(&filter->fdir_node);
8319 	kfree(filter);
8320 }
8321 
8322 /**
8323  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8324  * @pf: board private structure
8325  **/
8326 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8327 {
8328 	struct i40e_fdir_filter *filter;
8329 	u32 fcnt_prog, fcnt_avail;
8330 	struct hlist_node *node;
8331 
8332 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8333 		return;
8334 
8335 	/* Check if we have enough room to re-enable FDir SB capability. */
8336 	fcnt_prog = i40e_get_global_fd_count(pf);
8337 	fcnt_avail = pf->fdir_pf_filter_count;
8338 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8339 	    (pf->fd_add_err == 0) ||
8340 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8341 		i40e_reenable_fdir_sb(pf);
8342 
8343 	/* We should wait for even more space before re-enabling ATR.
8344 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8345 	 * rules active.
8346 	 */
8347 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8348 	    (pf->fd_tcp4_filter_cnt == 0))
8349 		i40e_reenable_fdir_atr(pf);
8350 
8351 	/* if hw had a problem adding a filter, delete it */
8352 	if (pf->fd_inv > 0) {
8353 		hlist_for_each_entry_safe(filter, node,
8354 					  &pf->fdir_filter_list, fdir_node)
8355 			if (filter->fd_id == pf->fd_inv)
8356 				i40e_delete_invalid_filter(pf, filter);
8357 	}
8358 }
8359 
8360 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8361 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8362 /**
8363  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8364  * @pf: board private structure
8365  **/
8366 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8367 {
8368 	unsigned long min_flush_time;
8369 	int flush_wait_retry = 50;
8370 	bool disable_atr = false;
8371 	int fd_room;
8372 	int reg;
8373 
8374 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8375 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8376 		return;
8377 
8378 	/* If the flush is happening too quick and we have mostly SB rules we
8379 	 * should not re-enable ATR for some time.
8380 	 */
8381 	min_flush_time = pf->fd_flush_timestamp +
8382 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8383 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8384 
8385 	if (!(time_after(jiffies, min_flush_time)) &&
8386 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8387 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8388 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8389 		disable_atr = true;
8390 	}
8391 
8392 	pf->fd_flush_timestamp = jiffies;
8393 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8394 	/* flush all filters */
8395 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8396 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8397 	i40e_flush(&pf->hw);
8398 	pf->fd_flush_cnt++;
8399 	pf->fd_add_err = 0;
8400 	do {
8401 		/* Check FD flush status every 5-6msec */
8402 		usleep_range(5000, 6000);
8403 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8404 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8405 			break;
8406 	} while (flush_wait_retry--);
8407 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8408 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8409 	} else {
8410 		/* replay sideband filters */
8411 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8412 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8413 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8414 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8415 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8416 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8417 	}
8418 }
8419 
8420 /**
8421  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8422  * @pf: board private structure
8423  **/
8424 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8425 {
8426 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8427 }
8428 
8429 /* We can see up to 256 filter programming desc in transit if the filters are
8430  * being applied really fast; before we see the first
8431  * filter miss error on Rx queue 0. Accumulating enough error messages before
8432  * reacting will make sure we don't cause flush too often.
8433  */
8434 #define I40E_MAX_FD_PROGRAM_ERROR 256
8435 
8436 /**
8437  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8438  * @pf: board private structure
8439  **/
8440 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8441 {
8442 
8443 	/* if interface is down do nothing */
8444 	if (test_bit(__I40E_DOWN, pf->state))
8445 		return;
8446 
8447 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8448 		i40e_fdir_flush_and_replay(pf);
8449 
8450 	i40e_fdir_check_and_reenable(pf);
8451 
8452 }
8453 
8454 /**
8455  * i40e_vsi_link_event - notify VSI of a link event
8456  * @vsi: vsi to be notified
8457  * @link_up: link up or down
8458  **/
8459 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8460 {
8461 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8462 		return;
8463 
8464 	switch (vsi->type) {
8465 	case I40E_VSI_MAIN:
8466 		if (!vsi->netdev || !vsi->netdev_registered)
8467 			break;
8468 
8469 		if (link_up) {
8470 			netif_carrier_on(vsi->netdev);
8471 			netif_tx_wake_all_queues(vsi->netdev);
8472 		} else {
8473 			netif_carrier_off(vsi->netdev);
8474 			netif_tx_stop_all_queues(vsi->netdev);
8475 		}
8476 		break;
8477 
8478 	case I40E_VSI_SRIOV:
8479 	case I40E_VSI_VMDQ2:
8480 	case I40E_VSI_CTRL:
8481 	case I40E_VSI_IWARP:
8482 	case I40E_VSI_MIRROR:
8483 	default:
8484 		/* there is no notification for other VSIs */
8485 		break;
8486 	}
8487 }
8488 
8489 /**
8490  * i40e_veb_link_event - notify elements on the veb of a link event
8491  * @veb: veb to be notified
8492  * @link_up: link up or down
8493  **/
8494 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8495 {
8496 	struct i40e_pf *pf;
8497 	int i;
8498 
8499 	if (!veb || !veb->pf)
8500 		return;
8501 	pf = veb->pf;
8502 
8503 	/* depth first... */
8504 	for (i = 0; i < I40E_MAX_VEB; i++)
8505 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8506 			i40e_veb_link_event(pf->veb[i], link_up);
8507 
8508 	/* ... now the local VSIs */
8509 	for (i = 0; i < pf->num_alloc_vsi; i++)
8510 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8511 			i40e_vsi_link_event(pf->vsi[i], link_up);
8512 }
8513 
8514 /**
8515  * i40e_link_event - Update netif_carrier status
8516  * @pf: board private structure
8517  **/
8518 static void i40e_link_event(struct i40e_pf *pf)
8519 {
8520 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8521 	u8 new_link_speed, old_link_speed;
8522 	i40e_status status;
8523 	bool new_link, old_link;
8524 
8525 	/* set this to force the get_link_status call to refresh state */
8526 	pf->hw.phy.get_link_info = true;
8527 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8528 	status = i40e_get_link_status(&pf->hw, &new_link);
8529 
8530 	/* On success, disable temp link polling */
8531 	if (status == I40E_SUCCESS) {
8532 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8533 	} else {
8534 		/* Enable link polling temporarily until i40e_get_link_status
8535 		 * returns I40E_SUCCESS
8536 		 */
8537 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8538 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8539 			status);
8540 		return;
8541 	}
8542 
8543 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
8544 	new_link_speed = pf->hw.phy.link_info.link_speed;
8545 
8546 	if (new_link == old_link &&
8547 	    new_link_speed == old_link_speed &&
8548 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8549 	     new_link == netif_carrier_ok(vsi->netdev)))
8550 		return;
8551 
8552 	i40e_print_link_message(vsi, new_link);
8553 
8554 	/* Notify the base of the switch tree connected to
8555 	 * the link.  Floating VEBs are not notified.
8556 	 */
8557 	if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8558 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8559 	else
8560 		i40e_vsi_link_event(vsi, new_link);
8561 
8562 	if (pf->vf)
8563 		i40e_vc_notify_link_state(pf);
8564 
8565 	if (pf->flags & I40E_FLAG_PTP)
8566 		i40e_ptp_set_increment(pf);
8567 }
8568 
8569 /**
8570  * i40e_watchdog_subtask - periodic checks not using event driven response
8571  * @pf: board private structure
8572  **/
8573 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8574 {
8575 	int i;
8576 
8577 	/* if interface is down do nothing */
8578 	if (test_bit(__I40E_DOWN, pf->state) ||
8579 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
8580 		return;
8581 
8582 	/* make sure we don't do these things too often */
8583 	if (time_before(jiffies, (pf->service_timer_previous +
8584 				  pf->service_timer_period)))
8585 		return;
8586 	pf->service_timer_previous = jiffies;
8587 
8588 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8589 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
8590 		i40e_link_event(pf);
8591 
8592 	/* Update the stats for active netdevs so the network stack
8593 	 * can look at updated numbers whenever it cares to
8594 	 */
8595 	for (i = 0; i < pf->num_alloc_vsi; i++)
8596 		if (pf->vsi[i] && pf->vsi[i]->netdev)
8597 			i40e_update_stats(pf->vsi[i]);
8598 
8599 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8600 		/* Update the stats for the active switching components */
8601 		for (i = 0; i < I40E_MAX_VEB; i++)
8602 			if (pf->veb[i])
8603 				i40e_update_veb_stats(pf->veb[i]);
8604 	}
8605 
8606 	i40e_ptp_rx_hang(pf);
8607 	i40e_ptp_tx_hang(pf);
8608 }
8609 
8610 /**
8611  * i40e_reset_subtask - Set up for resetting the device and driver
8612  * @pf: board private structure
8613  **/
8614 static void i40e_reset_subtask(struct i40e_pf *pf)
8615 {
8616 	u32 reset_flags = 0;
8617 
8618 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8619 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8620 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8621 	}
8622 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8623 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8624 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8625 	}
8626 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8627 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8628 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8629 	}
8630 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8631 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8632 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8633 	}
8634 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8635 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8636 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8637 	}
8638 
8639 	/* If there's a recovery already waiting, it takes
8640 	 * precedence before starting a new reset sequence.
8641 	 */
8642 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8643 		i40e_prep_for_reset(pf, false);
8644 		i40e_reset(pf);
8645 		i40e_rebuild(pf, false, false);
8646 	}
8647 
8648 	/* If we're already down or resetting, just bail */
8649 	if (reset_flags &&
8650 	    !test_bit(__I40E_DOWN, pf->state) &&
8651 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8652 		i40e_do_reset(pf, reset_flags, false);
8653 	}
8654 }
8655 
8656 /**
8657  * i40e_handle_link_event - Handle link event
8658  * @pf: board private structure
8659  * @e: event info posted on ARQ
8660  **/
8661 static void i40e_handle_link_event(struct i40e_pf *pf,
8662 				   struct i40e_arq_event_info *e)
8663 {
8664 	struct i40e_aqc_get_link_status *status =
8665 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8666 
8667 	/* Do a new status request to re-enable LSE reporting
8668 	 * and load new status information into the hw struct
8669 	 * This completely ignores any state information
8670 	 * in the ARQ event info, instead choosing to always
8671 	 * issue the AQ update link status command.
8672 	 */
8673 	i40e_link_event(pf);
8674 
8675 	/* Check if module meets thermal requirements */
8676 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8677 		dev_err(&pf->pdev->dev,
8678 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8679 		dev_err(&pf->pdev->dev,
8680 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8681 	} else {
8682 		/* check for unqualified module, if link is down, suppress
8683 		 * the message if link was forced to be down.
8684 		 */
8685 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8686 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8687 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
8688 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8689 			dev_err(&pf->pdev->dev,
8690 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8691 			dev_err(&pf->pdev->dev,
8692 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8693 		}
8694 	}
8695 }
8696 
8697 /**
8698  * i40e_clean_adminq_subtask - Clean the AdminQ rings
8699  * @pf: board private structure
8700  **/
8701 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8702 {
8703 	struct i40e_arq_event_info event;
8704 	struct i40e_hw *hw = &pf->hw;
8705 	u16 pending, i = 0;
8706 	i40e_status ret;
8707 	u16 opcode;
8708 	u32 oldval;
8709 	u32 val;
8710 
8711 	/* Do not run clean AQ when PF reset fails */
8712 	if (test_bit(__I40E_RESET_FAILED, pf->state))
8713 		return;
8714 
8715 	/* check for error indications */
8716 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
8717 	oldval = val;
8718 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8719 		if (hw->debug_mask & I40E_DEBUG_AQ)
8720 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8721 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8722 	}
8723 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8724 		if (hw->debug_mask & I40E_DEBUG_AQ)
8725 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8726 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8727 		pf->arq_overflows++;
8728 	}
8729 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8730 		if (hw->debug_mask & I40E_DEBUG_AQ)
8731 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8732 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8733 	}
8734 	if (oldval != val)
8735 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
8736 
8737 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
8738 	oldval = val;
8739 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8740 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8741 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8742 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8743 	}
8744 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8745 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8746 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8747 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8748 	}
8749 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8750 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8751 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8752 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8753 	}
8754 	if (oldval != val)
8755 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
8756 
8757 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8758 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8759 	if (!event.msg_buf)
8760 		return;
8761 
8762 	do {
8763 		ret = i40e_clean_arq_element(hw, &event, &pending);
8764 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8765 			break;
8766 		else if (ret) {
8767 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8768 			break;
8769 		}
8770 
8771 		opcode = le16_to_cpu(event.desc.opcode);
8772 		switch (opcode) {
8773 
8774 		case i40e_aqc_opc_get_link_status:
8775 			i40e_handle_link_event(pf, &event);
8776 			break;
8777 		case i40e_aqc_opc_send_msg_to_pf:
8778 			ret = i40e_vc_process_vf_msg(pf,
8779 					le16_to_cpu(event.desc.retval),
8780 					le32_to_cpu(event.desc.cookie_high),
8781 					le32_to_cpu(event.desc.cookie_low),
8782 					event.msg_buf,
8783 					event.msg_len);
8784 			break;
8785 		case i40e_aqc_opc_lldp_update_mib:
8786 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8787 #ifdef CONFIG_I40E_DCB
8788 			rtnl_lock();
8789 			ret = i40e_handle_lldp_event(pf, &event);
8790 			rtnl_unlock();
8791 #endif /* CONFIG_I40E_DCB */
8792 			break;
8793 		case i40e_aqc_opc_event_lan_overflow:
8794 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8795 			i40e_handle_lan_overflow_event(pf, &event);
8796 			break;
8797 		case i40e_aqc_opc_send_msg_to_peer:
8798 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8799 			break;
8800 		case i40e_aqc_opc_nvm_erase:
8801 		case i40e_aqc_opc_nvm_update:
8802 		case i40e_aqc_opc_oem_post_update:
8803 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8804 				   "ARQ NVM operation 0x%04x completed\n",
8805 				   opcode);
8806 			break;
8807 		default:
8808 			dev_info(&pf->pdev->dev,
8809 				 "ARQ: Unknown event 0x%04x ignored\n",
8810 				 opcode);
8811 			break;
8812 		}
8813 	} while (i++ < pf->adminq_work_limit);
8814 
8815 	if (i < pf->adminq_work_limit)
8816 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8817 
8818 	/* re-enable Admin queue interrupt cause */
8819 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
8820 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8821 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
8822 	i40e_flush(hw);
8823 
8824 	kfree(event.msg_buf);
8825 }
8826 
8827 /**
8828  * i40e_verify_eeprom - make sure eeprom is good to use
8829  * @pf: board private structure
8830  **/
8831 static void i40e_verify_eeprom(struct i40e_pf *pf)
8832 {
8833 	int err;
8834 
8835 	err = i40e_diag_eeprom_test(&pf->hw);
8836 	if (err) {
8837 		/* retry in case of garbage read */
8838 		err = i40e_diag_eeprom_test(&pf->hw);
8839 		if (err) {
8840 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8841 				 err);
8842 			set_bit(__I40E_BAD_EEPROM, pf->state);
8843 		}
8844 	}
8845 
8846 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8847 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8848 		clear_bit(__I40E_BAD_EEPROM, pf->state);
8849 	}
8850 }
8851 
8852 /**
8853  * i40e_enable_pf_switch_lb
8854  * @pf: pointer to the PF structure
8855  *
8856  * enable switch loop back or die - no point in a return value
8857  **/
8858 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8859 {
8860 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8861 	struct i40e_vsi_context ctxt;
8862 	int ret;
8863 
8864 	ctxt.seid = pf->main_vsi_seid;
8865 	ctxt.pf_num = pf->hw.pf_id;
8866 	ctxt.vf_num = 0;
8867 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8868 	if (ret) {
8869 		dev_info(&pf->pdev->dev,
8870 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8871 			 i40e_stat_str(&pf->hw, ret),
8872 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8873 		return;
8874 	}
8875 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8876 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8877 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8878 
8879 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8880 	if (ret) {
8881 		dev_info(&pf->pdev->dev,
8882 			 "update vsi switch failed, err %s aq_err %s\n",
8883 			 i40e_stat_str(&pf->hw, ret),
8884 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8885 	}
8886 }
8887 
8888 /**
8889  * i40e_disable_pf_switch_lb
8890  * @pf: pointer to the PF structure
8891  *
8892  * disable switch loop back or die - no point in a return value
8893  **/
8894 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8895 {
8896 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8897 	struct i40e_vsi_context ctxt;
8898 	int ret;
8899 
8900 	ctxt.seid = pf->main_vsi_seid;
8901 	ctxt.pf_num = pf->hw.pf_id;
8902 	ctxt.vf_num = 0;
8903 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8904 	if (ret) {
8905 		dev_info(&pf->pdev->dev,
8906 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8907 			 i40e_stat_str(&pf->hw, ret),
8908 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8909 		return;
8910 	}
8911 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8912 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8913 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8914 
8915 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8916 	if (ret) {
8917 		dev_info(&pf->pdev->dev,
8918 			 "update vsi switch failed, err %s aq_err %s\n",
8919 			 i40e_stat_str(&pf->hw, ret),
8920 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8921 	}
8922 }
8923 
8924 /**
8925  * i40e_config_bridge_mode - Configure the HW bridge mode
8926  * @veb: pointer to the bridge instance
8927  *
8928  * Configure the loop back mode for the LAN VSI that is downlink to the
8929  * specified HW bridge instance. It is expected this function is called
8930  * when a new HW bridge is instantiated.
8931  **/
8932 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8933 {
8934 	struct i40e_pf *pf = veb->pf;
8935 
8936 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8937 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8938 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8939 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8940 		i40e_disable_pf_switch_lb(pf);
8941 	else
8942 		i40e_enable_pf_switch_lb(pf);
8943 }
8944 
8945 /**
8946  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8947  * @veb: pointer to the VEB instance
8948  *
8949  * This is a recursive function that first builds the attached VSIs then
8950  * recurses in to build the next layer of VEB.  We track the connections
8951  * through our own index numbers because the seid's from the HW could
8952  * change across the reset.
8953  **/
8954 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8955 {
8956 	struct i40e_vsi *ctl_vsi = NULL;
8957 	struct i40e_pf *pf = veb->pf;
8958 	int v, veb_idx;
8959 	int ret;
8960 
8961 	/* build VSI that owns this VEB, temporarily attached to base VEB */
8962 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8963 		if (pf->vsi[v] &&
8964 		    pf->vsi[v]->veb_idx == veb->idx &&
8965 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8966 			ctl_vsi = pf->vsi[v];
8967 			break;
8968 		}
8969 	}
8970 	if (!ctl_vsi) {
8971 		dev_info(&pf->pdev->dev,
8972 			 "missing owner VSI for veb_idx %d\n", veb->idx);
8973 		ret = -ENOENT;
8974 		goto end_reconstitute;
8975 	}
8976 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
8977 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8978 	ret = i40e_add_vsi(ctl_vsi);
8979 	if (ret) {
8980 		dev_info(&pf->pdev->dev,
8981 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
8982 			 veb->idx, ret);
8983 		goto end_reconstitute;
8984 	}
8985 	i40e_vsi_reset_stats(ctl_vsi);
8986 
8987 	/* create the VEB in the switch and move the VSI onto the VEB */
8988 	ret = i40e_add_veb(veb, ctl_vsi);
8989 	if (ret)
8990 		goto end_reconstitute;
8991 
8992 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8993 		veb->bridge_mode = BRIDGE_MODE_VEB;
8994 	else
8995 		veb->bridge_mode = BRIDGE_MODE_VEPA;
8996 	i40e_config_bridge_mode(veb);
8997 
8998 	/* create the remaining VSIs attached to this VEB */
8999 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9000 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9001 			continue;
9002 
9003 		if (pf->vsi[v]->veb_idx == veb->idx) {
9004 			struct i40e_vsi *vsi = pf->vsi[v];
9005 
9006 			vsi->uplink_seid = veb->seid;
9007 			ret = i40e_add_vsi(vsi);
9008 			if (ret) {
9009 				dev_info(&pf->pdev->dev,
9010 					 "rebuild of vsi_idx %d failed: %d\n",
9011 					 v, ret);
9012 				goto end_reconstitute;
9013 			}
9014 			i40e_vsi_reset_stats(vsi);
9015 		}
9016 	}
9017 
9018 	/* create any VEBs attached to this VEB - RECURSION */
9019 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9020 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9021 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9022 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9023 			if (ret)
9024 				break;
9025 		}
9026 	}
9027 
9028 end_reconstitute:
9029 	return ret;
9030 }
9031 
9032 /**
9033  * i40e_get_capabilities - get info about the HW
9034  * @pf: the PF struct
9035  **/
9036 static int i40e_get_capabilities(struct i40e_pf *pf,
9037 				 enum i40e_admin_queue_opc list_type)
9038 {
9039 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9040 	u16 data_size;
9041 	int buf_len;
9042 	int err;
9043 
9044 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9045 	do {
9046 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9047 		if (!cap_buf)
9048 			return -ENOMEM;
9049 
9050 		/* this loads the data into the hw struct for us */
9051 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9052 						    &data_size, list_type,
9053 						    NULL);
9054 		/* data loaded, buffer no longer needed */
9055 		kfree(cap_buf);
9056 
9057 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9058 			/* retry with a larger buffer */
9059 			buf_len = data_size;
9060 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9061 			dev_info(&pf->pdev->dev,
9062 				 "capability discovery failed, err %s aq_err %s\n",
9063 				 i40e_stat_str(&pf->hw, err),
9064 				 i40e_aq_str(&pf->hw,
9065 					     pf->hw.aq.asq_last_status));
9066 			return -ENODEV;
9067 		}
9068 	} while (err);
9069 
9070 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9071 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9072 			dev_info(&pf->pdev->dev,
9073 				 "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",
9074 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9075 				 pf->hw.func_caps.num_msix_vectors,
9076 				 pf->hw.func_caps.num_msix_vectors_vf,
9077 				 pf->hw.func_caps.fd_filters_guaranteed,
9078 				 pf->hw.func_caps.fd_filters_best_effort,
9079 				 pf->hw.func_caps.num_tx_qp,
9080 				 pf->hw.func_caps.num_vsis);
9081 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9082 			dev_info(&pf->pdev->dev,
9083 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9084 				 pf->hw.dev_caps.switch_mode,
9085 				 pf->hw.dev_caps.valid_functions);
9086 			dev_info(&pf->pdev->dev,
9087 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9088 				 pf->hw.dev_caps.sr_iov_1_1,
9089 				 pf->hw.dev_caps.num_vfs);
9090 			dev_info(&pf->pdev->dev,
9091 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9092 				 pf->hw.dev_caps.num_vsis,
9093 				 pf->hw.dev_caps.num_rx_qp,
9094 				 pf->hw.dev_caps.num_tx_qp);
9095 		}
9096 	}
9097 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9098 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9099 		       + pf->hw.func_caps.num_vfs)
9100 		if (pf->hw.revision_id == 0 &&
9101 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9102 			dev_info(&pf->pdev->dev,
9103 				 "got num_vsis %d, setting num_vsis to %d\n",
9104 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9105 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9106 		}
9107 	}
9108 	return 0;
9109 }
9110 
9111 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9112 
9113 /**
9114  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9115  * @pf: board private structure
9116  **/
9117 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9118 {
9119 	struct i40e_vsi *vsi;
9120 
9121 	/* quick workaround for an NVM issue that leaves a critical register
9122 	 * uninitialized
9123 	 */
9124 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9125 		static const u32 hkey[] = {
9126 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9127 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9128 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9129 			0x95b3a76d};
9130 		int i;
9131 
9132 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9133 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9134 	}
9135 
9136 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9137 		return;
9138 
9139 	/* find existing VSI and see if it needs configuring */
9140 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9141 
9142 	/* create a new VSI if none exists */
9143 	if (!vsi) {
9144 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9145 				     pf->vsi[pf->lan_vsi]->seid, 0);
9146 		if (!vsi) {
9147 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9148 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9149 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9150 			return;
9151 		}
9152 	}
9153 
9154 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9155 }
9156 
9157 /**
9158  * i40e_fdir_teardown - release the Flow Director resources
9159  * @pf: board private structure
9160  **/
9161 static void i40e_fdir_teardown(struct i40e_pf *pf)
9162 {
9163 	struct i40e_vsi *vsi;
9164 
9165 	i40e_fdir_filter_exit(pf);
9166 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9167 	if (vsi)
9168 		i40e_vsi_release(vsi);
9169 }
9170 
9171 /**
9172  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9173  * @vsi: PF main vsi
9174  * @seid: seid of main or channel VSIs
9175  *
9176  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9177  * existed before reset
9178  **/
9179 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9180 {
9181 	struct i40e_cloud_filter *cfilter;
9182 	struct i40e_pf *pf = vsi->back;
9183 	struct hlist_node *node;
9184 	i40e_status ret;
9185 
9186 	/* Add cloud filters back if they exist */
9187 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9188 				  cloud_node) {
9189 		if (cfilter->seid != seid)
9190 			continue;
9191 
9192 		if (cfilter->dst_port)
9193 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9194 								true);
9195 		else
9196 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9197 
9198 		if (ret) {
9199 			dev_dbg(&pf->pdev->dev,
9200 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9201 				i40e_stat_str(&pf->hw, ret),
9202 				i40e_aq_str(&pf->hw,
9203 					    pf->hw.aq.asq_last_status));
9204 			return ret;
9205 		}
9206 	}
9207 	return 0;
9208 }
9209 
9210 /**
9211  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9212  * @vsi: PF main vsi
9213  *
9214  * Rebuilds channel VSIs if they existed before reset
9215  **/
9216 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9217 {
9218 	struct i40e_channel *ch, *ch_tmp;
9219 	i40e_status ret;
9220 
9221 	if (list_empty(&vsi->ch_list))
9222 		return 0;
9223 
9224 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9225 		if (!ch->initialized)
9226 			break;
9227 		/* Proceed with creation of channel (VMDq2) VSI */
9228 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9229 		if (ret) {
9230 			dev_info(&vsi->back->pdev->dev,
9231 				 "failed to rebuild channels using uplink_seid %u\n",
9232 				 vsi->uplink_seid);
9233 			return ret;
9234 		}
9235 		/* Reconfigure TX queues using QTX_CTL register */
9236 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9237 		if (ret) {
9238 			dev_info(&vsi->back->pdev->dev,
9239 				 "failed to configure TX rings for channel %u\n",
9240 				 ch->seid);
9241 			return ret;
9242 		}
9243 		/* update 'next_base_queue' */
9244 		vsi->next_base_queue = vsi->next_base_queue +
9245 							ch->num_queue_pairs;
9246 		if (ch->max_tx_rate) {
9247 			u64 credits = ch->max_tx_rate;
9248 
9249 			if (i40e_set_bw_limit(vsi, ch->seid,
9250 					      ch->max_tx_rate))
9251 				return -EINVAL;
9252 
9253 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9254 			dev_dbg(&vsi->back->pdev->dev,
9255 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9256 				ch->max_tx_rate,
9257 				credits,
9258 				ch->seid);
9259 		}
9260 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9261 		if (ret) {
9262 			dev_dbg(&vsi->back->pdev->dev,
9263 				"Failed to rebuild cloud filters for channel VSI %u\n",
9264 				ch->seid);
9265 			return ret;
9266 		}
9267 	}
9268 	return 0;
9269 }
9270 
9271 /**
9272  * i40e_prep_for_reset - prep for the core to reset
9273  * @pf: board private structure
9274  * @lock_acquired: indicates whether or not the lock has been acquired
9275  * before this function was called.
9276  *
9277  * Close up the VFs and other things in prep for PF Reset.
9278   **/
9279 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9280 {
9281 	struct i40e_hw *hw = &pf->hw;
9282 	i40e_status ret = 0;
9283 	u32 v;
9284 
9285 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9286 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9287 		return;
9288 	if (i40e_check_asq_alive(&pf->hw))
9289 		i40e_vc_notify_reset(pf);
9290 
9291 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9292 
9293 	/* quiesce the VSIs and their queues that are not already DOWN */
9294 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9295 	if (!lock_acquired)
9296 		rtnl_lock();
9297 	i40e_pf_quiesce_all_vsi(pf);
9298 	if (!lock_acquired)
9299 		rtnl_unlock();
9300 
9301 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9302 		if (pf->vsi[v])
9303 			pf->vsi[v]->seid = 0;
9304 	}
9305 
9306 	i40e_shutdown_adminq(&pf->hw);
9307 
9308 	/* call shutdown HMC */
9309 	if (hw->hmc.hmc_obj) {
9310 		ret = i40e_shutdown_lan_hmc(hw);
9311 		if (ret)
9312 			dev_warn(&pf->pdev->dev,
9313 				 "shutdown_lan_hmc failed: %d\n", ret);
9314 	}
9315 
9316 	/* Save the current PTP time so that we can restore the time after the
9317 	 * reset completes.
9318 	 */
9319 	i40e_ptp_save_hw_time(pf);
9320 }
9321 
9322 /**
9323  * i40e_send_version - update firmware with driver version
9324  * @pf: PF struct
9325  */
9326 static void i40e_send_version(struct i40e_pf *pf)
9327 {
9328 	struct i40e_driver_version dv;
9329 
9330 	dv.major_version = DRV_VERSION_MAJOR;
9331 	dv.minor_version = DRV_VERSION_MINOR;
9332 	dv.build_version = DRV_VERSION_BUILD;
9333 	dv.subbuild_version = 0;
9334 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9335 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9336 }
9337 
9338 /**
9339  * i40e_get_oem_version - get OEM specific version information
9340  * @hw: pointer to the hardware structure
9341  **/
9342 static void i40e_get_oem_version(struct i40e_hw *hw)
9343 {
9344 	u16 block_offset = 0xffff;
9345 	u16 block_length = 0;
9346 	u16 capabilities = 0;
9347 	u16 gen_snap = 0;
9348 	u16 release = 0;
9349 
9350 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9351 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9352 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9353 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9354 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9355 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9356 #define I40E_NVM_OEM_LENGTH			3
9357 
9358 	/* Check if pointer to OEM version block is valid. */
9359 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9360 	if (block_offset == 0xffff)
9361 		return;
9362 
9363 	/* Check if OEM version block has correct length. */
9364 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9365 			   &block_length);
9366 	if (block_length < I40E_NVM_OEM_LENGTH)
9367 		return;
9368 
9369 	/* Check if OEM version format is as expected. */
9370 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9371 			   &capabilities);
9372 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9373 		return;
9374 
9375 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9376 			   &gen_snap);
9377 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9378 			   &release);
9379 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9380 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9381 }
9382 
9383 /**
9384  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9385  * @pf: board private structure
9386  **/
9387 static int i40e_reset(struct i40e_pf *pf)
9388 {
9389 	struct i40e_hw *hw = &pf->hw;
9390 	i40e_status ret;
9391 
9392 	ret = i40e_pf_reset(hw);
9393 	if (ret) {
9394 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9395 		set_bit(__I40E_RESET_FAILED, pf->state);
9396 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9397 	} else {
9398 		pf->pfr_count++;
9399 	}
9400 	return ret;
9401 }
9402 
9403 /**
9404  * i40e_rebuild - rebuild using a saved config
9405  * @pf: board private structure
9406  * @reinit: if the Main VSI needs to re-initialized.
9407  * @lock_acquired: indicates whether or not the lock has been acquired
9408  * before this function was called.
9409  **/
9410 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9411 {
9412 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9413 	struct i40e_hw *hw = &pf->hw;
9414 	u8 set_fc_aq_fail = 0;
9415 	i40e_status ret;
9416 	u32 val;
9417 	int v;
9418 
9419 	if (test_bit(__I40E_DOWN, pf->state))
9420 		goto clear_recovery;
9421 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9422 
9423 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9424 	ret = i40e_init_adminq(&pf->hw);
9425 	if (ret) {
9426 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9427 			 i40e_stat_str(&pf->hw, ret),
9428 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9429 		goto clear_recovery;
9430 	}
9431 	i40e_get_oem_version(&pf->hw);
9432 
9433 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9434 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9435 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9436 		/* The following delay is necessary for 4.33 firmware and older
9437 		 * to recover after EMP reset. 200 ms should suffice but we
9438 		 * put here 300 ms to be sure that FW is ready to operate
9439 		 * after reset.
9440 		 */
9441 		mdelay(300);
9442 	}
9443 
9444 	/* re-verify the eeprom if we just had an EMP reset */
9445 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9446 		i40e_verify_eeprom(pf);
9447 
9448 	i40e_clear_pxe_mode(hw);
9449 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9450 	if (ret)
9451 		goto end_core_reset;
9452 
9453 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9454 				hw->func_caps.num_rx_qp, 0, 0);
9455 	if (ret) {
9456 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9457 		goto end_core_reset;
9458 	}
9459 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9460 	if (ret) {
9461 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9462 		goto end_core_reset;
9463 	}
9464 
9465 	/* Enable FW to write a default DCB config on link-up */
9466 	i40e_aq_set_dcb_parameters(hw, true, NULL);
9467 
9468 #ifdef CONFIG_I40E_DCB
9469 	ret = i40e_init_pf_dcb(pf);
9470 	if (ret) {
9471 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9472 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9473 		/* Continue without DCB enabled */
9474 	}
9475 #endif /* CONFIG_I40E_DCB */
9476 	/* do basic switch setup */
9477 	if (!lock_acquired)
9478 		rtnl_lock();
9479 	ret = i40e_setup_pf_switch(pf, reinit);
9480 	if (ret)
9481 		goto end_unlock;
9482 
9483 	/* The driver only wants link up/down and module qualification
9484 	 * reports from firmware.  Note the negative logic.
9485 	 */
9486 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
9487 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
9488 					 I40E_AQ_EVENT_MEDIA_NA |
9489 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9490 	if (ret)
9491 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9492 			 i40e_stat_str(&pf->hw, ret),
9493 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9494 
9495 	/* make sure our flow control settings are restored */
9496 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9497 	if (ret)
9498 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9499 			i40e_stat_str(&pf->hw, ret),
9500 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9501 
9502 	/* Rebuild the VSIs and VEBs that existed before reset.
9503 	 * They are still in our local switch element arrays, so only
9504 	 * need to rebuild the switch model in the HW.
9505 	 *
9506 	 * If there were VEBs but the reconstitution failed, we'll try
9507 	 * try to recover minimal use by getting the basic PF VSI working.
9508 	 */
9509 	if (vsi->uplink_seid != pf->mac_seid) {
9510 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9511 		/* find the one VEB connected to the MAC, and find orphans */
9512 		for (v = 0; v < I40E_MAX_VEB; v++) {
9513 			if (!pf->veb[v])
9514 				continue;
9515 
9516 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9517 			    pf->veb[v]->uplink_seid == 0) {
9518 				ret = i40e_reconstitute_veb(pf->veb[v]);
9519 
9520 				if (!ret)
9521 					continue;
9522 
9523 				/* If Main VEB failed, we're in deep doodoo,
9524 				 * so give up rebuilding the switch and set up
9525 				 * for minimal rebuild of PF VSI.
9526 				 * If orphan failed, we'll report the error
9527 				 * but try to keep going.
9528 				 */
9529 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9530 					dev_info(&pf->pdev->dev,
9531 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9532 						 ret);
9533 					vsi->uplink_seid = pf->mac_seid;
9534 					break;
9535 				} else if (pf->veb[v]->uplink_seid == 0) {
9536 					dev_info(&pf->pdev->dev,
9537 						 "rebuild of orphan VEB failed: %d\n",
9538 						 ret);
9539 				}
9540 			}
9541 		}
9542 	}
9543 
9544 	if (vsi->uplink_seid == pf->mac_seid) {
9545 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9546 		/* no VEB, so rebuild only the Main VSI */
9547 		ret = i40e_add_vsi(vsi);
9548 		if (ret) {
9549 			dev_info(&pf->pdev->dev,
9550 				 "rebuild of Main VSI failed: %d\n", ret);
9551 			goto end_unlock;
9552 		}
9553 	}
9554 
9555 	if (vsi->mqprio_qopt.max_rate[0]) {
9556 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9557 		u64 credits = 0;
9558 
9559 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9560 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9561 		if (ret)
9562 			goto end_unlock;
9563 
9564 		credits = max_tx_rate;
9565 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
9566 		dev_dbg(&vsi->back->pdev->dev,
9567 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9568 			max_tx_rate,
9569 			credits,
9570 			vsi->seid);
9571 	}
9572 
9573 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9574 	if (ret)
9575 		goto end_unlock;
9576 
9577 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9578 	 * for this main VSI if they exist
9579 	 */
9580 	ret = i40e_rebuild_channels(vsi);
9581 	if (ret)
9582 		goto end_unlock;
9583 
9584 	/* Reconfigure hardware for allowing smaller MSS in the case
9585 	 * of TSO, so that we avoid the MDD being fired and causing
9586 	 * a reset in the case of small MSS+TSO.
9587 	 */
9588 #define I40E_REG_MSS          0x000E64DC
9589 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9590 #define I40E_64BYTE_MSS       0x400000
9591 	val = rd32(hw, I40E_REG_MSS);
9592 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9593 		val &= ~I40E_REG_MSS_MIN_MASK;
9594 		val |= I40E_64BYTE_MSS;
9595 		wr32(hw, I40E_REG_MSS, val);
9596 	}
9597 
9598 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9599 		msleep(75);
9600 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9601 		if (ret)
9602 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9603 				 i40e_stat_str(&pf->hw, ret),
9604 				 i40e_aq_str(&pf->hw,
9605 					     pf->hw.aq.asq_last_status));
9606 	}
9607 	/* reinit the misc interrupt */
9608 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9609 		ret = i40e_setup_misc_vector(pf);
9610 
9611 	/* Add a filter to drop all Flow control frames from any VSI from being
9612 	 * transmitted. By doing so we stop a malicious VF from sending out
9613 	 * PAUSE or PFC frames and potentially controlling traffic for other
9614 	 * PF/VF VSIs.
9615 	 * The FW can still send Flow control frames if enabled.
9616 	 */
9617 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9618 						       pf->main_vsi_seid);
9619 
9620 	/* restart the VSIs that were rebuilt and running before the reset */
9621 	i40e_pf_unquiesce_all_vsi(pf);
9622 
9623 	/* Release the RTNL lock before we start resetting VFs */
9624 	if (!lock_acquired)
9625 		rtnl_unlock();
9626 
9627 	/* Restore promiscuous settings */
9628 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9629 	if (ret)
9630 		dev_warn(&pf->pdev->dev,
9631 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9632 			 pf->cur_promisc ? "on" : "off",
9633 			 i40e_stat_str(&pf->hw, ret),
9634 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9635 
9636 	i40e_reset_all_vfs(pf, true);
9637 
9638 	/* tell the firmware that we're starting */
9639 	i40e_send_version(pf);
9640 
9641 	/* We've already released the lock, so don't do it again */
9642 	goto end_core_reset;
9643 
9644 end_unlock:
9645 	if (!lock_acquired)
9646 		rtnl_unlock();
9647 end_core_reset:
9648 	clear_bit(__I40E_RESET_FAILED, pf->state);
9649 clear_recovery:
9650 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9651 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
9652 }
9653 
9654 /**
9655  * i40e_reset_and_rebuild - reset and rebuild using a saved config
9656  * @pf: board private structure
9657  * @reinit: if the Main VSI needs to re-initialized.
9658  * @lock_acquired: indicates whether or not the lock has been acquired
9659  * before this function was called.
9660  **/
9661 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9662 				   bool lock_acquired)
9663 {
9664 	int ret;
9665 	/* Now we wait for GRST to settle out.
9666 	 * We don't have to delete the VEBs or VSIs from the hw switch
9667 	 * because the reset will make them disappear.
9668 	 */
9669 	ret = i40e_reset(pf);
9670 	if (!ret)
9671 		i40e_rebuild(pf, reinit, lock_acquired);
9672 }
9673 
9674 /**
9675  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9676  * @pf: board private structure
9677  *
9678  * Close up the VFs and other things in prep for a Core Reset,
9679  * then get ready to rebuild the world.
9680  * @lock_acquired: indicates whether or not the lock has been acquired
9681  * before this function was called.
9682  **/
9683 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9684 {
9685 	i40e_prep_for_reset(pf, lock_acquired);
9686 	i40e_reset_and_rebuild(pf, false, lock_acquired);
9687 }
9688 
9689 /**
9690  * i40e_handle_mdd_event
9691  * @pf: pointer to the PF structure
9692  *
9693  * Called from the MDD irq handler to identify possibly malicious vfs
9694  **/
9695 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9696 {
9697 	struct i40e_hw *hw = &pf->hw;
9698 	bool mdd_detected = false;
9699 	bool pf_mdd_detected = false;
9700 	struct i40e_vf *vf;
9701 	u32 reg;
9702 	int i;
9703 
9704 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9705 		return;
9706 
9707 	/* find what triggered the MDD event */
9708 	reg = rd32(hw, I40E_GL_MDET_TX);
9709 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9710 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9711 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
9712 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9713 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
9714 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9715 				I40E_GL_MDET_TX_EVENT_SHIFT;
9716 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9717 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
9718 				pf->hw.func_caps.base_queue;
9719 		if (netif_msg_tx_err(pf))
9720 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9721 				 event, queue, pf_num, vf_num);
9722 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9723 		mdd_detected = true;
9724 	}
9725 	reg = rd32(hw, I40E_GL_MDET_RX);
9726 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9727 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9728 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
9729 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9730 				I40E_GL_MDET_RX_EVENT_SHIFT;
9731 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9732 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
9733 				pf->hw.func_caps.base_queue;
9734 		if (netif_msg_rx_err(pf))
9735 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9736 				 event, queue, func);
9737 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9738 		mdd_detected = true;
9739 	}
9740 
9741 	if (mdd_detected) {
9742 		reg = rd32(hw, I40E_PF_MDET_TX);
9743 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9744 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9745 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
9746 			pf_mdd_detected = true;
9747 		}
9748 		reg = rd32(hw, I40E_PF_MDET_RX);
9749 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9750 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9751 			dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
9752 			pf_mdd_detected = true;
9753 		}
9754 		/* Queue belongs to the PF, initiate a reset */
9755 		if (pf_mdd_detected) {
9756 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9757 			i40e_service_event_schedule(pf);
9758 		}
9759 	}
9760 
9761 	/* see if one of the VFs needs its hand slapped */
9762 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9763 		vf = &(pf->vf[i]);
9764 		reg = rd32(hw, I40E_VP_MDET_TX(i));
9765 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9766 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9767 			vf->num_mdd_events++;
9768 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9769 				 i);
9770 		}
9771 
9772 		reg = rd32(hw, I40E_VP_MDET_RX(i));
9773 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9774 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9775 			vf->num_mdd_events++;
9776 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9777 				 i);
9778 		}
9779 
9780 		if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9781 			dev_info(&pf->pdev->dev,
9782 				 "Too many MDD events on VF %d, disabled\n", i);
9783 			dev_info(&pf->pdev->dev,
9784 				 "Use PF Control I/F to re-enable the VF\n");
9785 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9786 		}
9787 	}
9788 
9789 	/* re-enable mdd interrupt cause */
9790 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9791 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9792 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9793 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9794 	i40e_flush(hw);
9795 }
9796 
9797 static const char *i40e_tunnel_name(u8 type)
9798 {
9799 	switch (type) {
9800 	case UDP_TUNNEL_TYPE_VXLAN:
9801 		return "vxlan";
9802 	case UDP_TUNNEL_TYPE_GENEVE:
9803 		return "geneve";
9804 	default:
9805 		return "unknown";
9806 	}
9807 }
9808 
9809 /**
9810  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9811  * @pf: board private structure
9812  **/
9813 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9814 {
9815 	int i;
9816 
9817 	/* loop through and set pending bit for all active UDP filters */
9818 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9819 		if (pf->udp_ports[i].port)
9820 			pf->pending_udp_bitmap |= BIT_ULL(i);
9821 	}
9822 
9823 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
9824 }
9825 
9826 /**
9827  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9828  * @pf: board private structure
9829  **/
9830 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9831 {
9832 	struct i40e_hw *hw = &pf->hw;
9833 	u8 filter_index, type;
9834 	u16 port;
9835 	int i;
9836 
9837 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
9838 		return;
9839 
9840 	/* acquire RTNL to maintain state of flags and port requests */
9841 	rtnl_lock();
9842 
9843 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9844 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9845 			struct i40e_udp_port_config *udp_port;
9846 			i40e_status ret = 0;
9847 
9848 			udp_port = &pf->udp_ports[i];
9849 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
9850 
9851 			port = READ_ONCE(udp_port->port);
9852 			type = READ_ONCE(udp_port->type);
9853 			filter_index = READ_ONCE(udp_port->filter_index);
9854 
9855 			/* release RTNL while we wait on AQ command */
9856 			rtnl_unlock();
9857 
9858 			if (port)
9859 				ret = i40e_aq_add_udp_tunnel(hw, port,
9860 							     type,
9861 							     &filter_index,
9862 							     NULL);
9863 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
9864 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
9865 							     NULL);
9866 
9867 			/* reacquire RTNL so we can update filter_index */
9868 			rtnl_lock();
9869 
9870 			if (ret) {
9871 				dev_info(&pf->pdev->dev,
9872 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9873 					 i40e_tunnel_name(type),
9874 					 port ? "add" : "delete",
9875 					 port,
9876 					 filter_index,
9877 					 i40e_stat_str(&pf->hw, ret),
9878 					 i40e_aq_str(&pf->hw,
9879 						     pf->hw.aq.asq_last_status));
9880 				if (port) {
9881 					/* failed to add, just reset port,
9882 					 * drop pending bit for any deletion
9883 					 */
9884 					udp_port->port = 0;
9885 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
9886 				}
9887 			} else if (port) {
9888 				/* record filter index on success */
9889 				udp_port->filter_index = filter_index;
9890 			}
9891 		}
9892 	}
9893 
9894 	rtnl_unlock();
9895 }
9896 
9897 /**
9898  * i40e_service_task - Run the driver's async subtasks
9899  * @work: pointer to work_struct containing our data
9900  **/
9901 static void i40e_service_task(struct work_struct *work)
9902 {
9903 	struct i40e_pf *pf = container_of(work,
9904 					  struct i40e_pf,
9905 					  service_task);
9906 	unsigned long start_time = jiffies;
9907 
9908 	/* don't bother with service tasks if a reset is in progress */
9909 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9910 		return;
9911 
9912 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9913 		return;
9914 
9915 	i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
9916 	i40e_sync_filters_subtask(pf);
9917 	i40e_reset_subtask(pf);
9918 	i40e_handle_mdd_event(pf);
9919 	i40e_vc_process_vflr_event(pf);
9920 	i40e_watchdog_subtask(pf);
9921 	i40e_fdir_reinit_subtask(pf);
9922 	if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
9923 		/* Client subtask will reopen next time through. */
9924 		i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9925 	} else {
9926 		i40e_client_subtask(pf);
9927 		if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
9928 				       pf->state))
9929 			i40e_notify_client_of_l2_param_changes(
9930 							pf->vsi[pf->lan_vsi]);
9931 	}
9932 	i40e_sync_filters_subtask(pf);
9933 	i40e_sync_udp_filters_subtask(pf);
9934 	i40e_clean_adminq_subtask(pf);
9935 
9936 	/* flush memory to make sure state is correct before next watchdog */
9937 	smp_mb__before_atomic();
9938 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
9939 
9940 	/* If the tasks have taken longer than one timer cycle or there
9941 	 * is more work to be done, reschedule the service task now
9942 	 * rather than wait for the timer to tick again.
9943 	 */
9944 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
9945 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
9946 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
9947 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
9948 		i40e_service_event_schedule(pf);
9949 }
9950 
9951 /**
9952  * i40e_service_timer - timer callback
9953  * @data: pointer to PF struct
9954  **/
9955 static void i40e_service_timer(struct timer_list *t)
9956 {
9957 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
9958 
9959 	mod_timer(&pf->service_timer,
9960 		  round_jiffies(jiffies + pf->service_timer_period));
9961 	i40e_service_event_schedule(pf);
9962 }
9963 
9964 /**
9965  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9966  * @vsi: the VSI being configured
9967  **/
9968 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9969 {
9970 	struct i40e_pf *pf = vsi->back;
9971 
9972 	switch (vsi->type) {
9973 	case I40E_VSI_MAIN:
9974 		vsi->alloc_queue_pairs = pf->num_lan_qps;
9975 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9976 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9977 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9978 			vsi->num_q_vectors = pf->num_lan_msix;
9979 		else
9980 			vsi->num_q_vectors = 1;
9981 
9982 		break;
9983 
9984 	case I40E_VSI_FDIR:
9985 		vsi->alloc_queue_pairs = 1;
9986 		vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9987 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9988 		vsi->num_q_vectors = pf->num_fdsb_msix;
9989 		break;
9990 
9991 	case I40E_VSI_VMDQ2:
9992 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9993 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9994 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9995 		vsi->num_q_vectors = pf->num_vmdq_msix;
9996 		break;
9997 
9998 	case I40E_VSI_SRIOV:
9999 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10000 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10001 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
10002 		break;
10003 
10004 	default:
10005 		WARN_ON(1);
10006 		return -ENODATA;
10007 	}
10008 
10009 	return 0;
10010 }
10011 
10012 /**
10013  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10014  * @vsi: VSI pointer
10015  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10016  *
10017  * On error: returns error code (negative)
10018  * On success: returns 0
10019  **/
10020 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10021 {
10022 	struct i40e_ring **next_rings;
10023 	int size;
10024 	int ret = 0;
10025 
10026 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10027 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10028 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10029 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10030 	if (!vsi->tx_rings)
10031 		return -ENOMEM;
10032 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10033 	if (i40e_enabled_xdp_vsi(vsi)) {
10034 		vsi->xdp_rings = next_rings;
10035 		next_rings += vsi->alloc_queue_pairs;
10036 	}
10037 	vsi->rx_rings = next_rings;
10038 
10039 	if (alloc_qvectors) {
10040 		/* allocate memory for q_vector pointers */
10041 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10042 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10043 		if (!vsi->q_vectors) {
10044 			ret = -ENOMEM;
10045 			goto err_vectors;
10046 		}
10047 	}
10048 	return ret;
10049 
10050 err_vectors:
10051 	kfree(vsi->tx_rings);
10052 	return ret;
10053 }
10054 
10055 /**
10056  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10057  * @pf: board private structure
10058  * @type: type of VSI
10059  *
10060  * On error: returns error code (negative)
10061  * On success: returns vsi index in PF (positive)
10062  **/
10063 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10064 {
10065 	int ret = -ENODEV;
10066 	struct i40e_vsi *vsi;
10067 	int vsi_idx;
10068 	int i;
10069 
10070 	/* Need to protect the allocation of the VSIs at the PF level */
10071 	mutex_lock(&pf->switch_mutex);
10072 
10073 	/* VSI list may be fragmented if VSI creation/destruction has
10074 	 * been happening.  We can afford to do a quick scan to look
10075 	 * for any free VSIs in the list.
10076 	 *
10077 	 * find next empty vsi slot, looping back around if necessary
10078 	 */
10079 	i = pf->next_vsi;
10080 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10081 		i++;
10082 	if (i >= pf->num_alloc_vsi) {
10083 		i = 0;
10084 		while (i < pf->next_vsi && pf->vsi[i])
10085 			i++;
10086 	}
10087 
10088 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10089 		vsi_idx = i;             /* Found one! */
10090 	} else {
10091 		ret = -ENODEV;
10092 		goto unlock_pf;  /* out of VSI slots! */
10093 	}
10094 	pf->next_vsi = ++i;
10095 
10096 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10097 	if (!vsi) {
10098 		ret = -ENOMEM;
10099 		goto unlock_pf;
10100 	}
10101 	vsi->type = type;
10102 	vsi->back = pf;
10103 	set_bit(__I40E_VSI_DOWN, vsi->state);
10104 	vsi->flags = 0;
10105 	vsi->idx = vsi_idx;
10106 	vsi->int_rate_limit = 0;
10107 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10108 				pf->rss_table_size : 64;
10109 	vsi->netdev_registered = false;
10110 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10111 	hash_init(vsi->mac_filter_hash);
10112 	vsi->irqs_ready = false;
10113 
10114 	if (type == I40E_VSI_MAIN) {
10115 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10116 		if (!vsi->af_xdp_zc_qps)
10117 			goto err_rings;
10118 	}
10119 
10120 	ret = i40e_set_num_rings_in_vsi(vsi);
10121 	if (ret)
10122 		goto err_rings;
10123 
10124 	ret = i40e_vsi_alloc_arrays(vsi, true);
10125 	if (ret)
10126 		goto err_rings;
10127 
10128 	/* Setup default MSIX irq handler for VSI */
10129 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10130 
10131 	/* Initialize VSI lock */
10132 	spin_lock_init(&vsi->mac_filter_hash_lock);
10133 	pf->vsi[vsi_idx] = vsi;
10134 	ret = vsi_idx;
10135 	goto unlock_pf;
10136 
10137 err_rings:
10138 	bitmap_free(vsi->af_xdp_zc_qps);
10139 	pf->next_vsi = i - 1;
10140 	kfree(vsi);
10141 unlock_pf:
10142 	mutex_unlock(&pf->switch_mutex);
10143 	return ret;
10144 }
10145 
10146 /**
10147  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10148  * @vsi: VSI pointer
10149  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10150  *
10151  * On error: returns error code (negative)
10152  * On success: returns 0
10153  **/
10154 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10155 {
10156 	/* free the ring and vector containers */
10157 	if (free_qvectors) {
10158 		kfree(vsi->q_vectors);
10159 		vsi->q_vectors = NULL;
10160 	}
10161 	kfree(vsi->tx_rings);
10162 	vsi->tx_rings = NULL;
10163 	vsi->rx_rings = NULL;
10164 	vsi->xdp_rings = NULL;
10165 }
10166 
10167 /**
10168  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10169  * and lookup table
10170  * @vsi: Pointer to VSI structure
10171  */
10172 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10173 {
10174 	if (!vsi)
10175 		return;
10176 
10177 	kfree(vsi->rss_hkey_user);
10178 	vsi->rss_hkey_user = NULL;
10179 
10180 	kfree(vsi->rss_lut_user);
10181 	vsi->rss_lut_user = NULL;
10182 }
10183 
10184 /**
10185  * i40e_vsi_clear - Deallocate the VSI provided
10186  * @vsi: the VSI being un-configured
10187  **/
10188 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10189 {
10190 	struct i40e_pf *pf;
10191 
10192 	if (!vsi)
10193 		return 0;
10194 
10195 	if (!vsi->back)
10196 		goto free_vsi;
10197 	pf = vsi->back;
10198 
10199 	mutex_lock(&pf->switch_mutex);
10200 	if (!pf->vsi[vsi->idx]) {
10201 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10202 			vsi->idx, vsi->idx, vsi->type);
10203 		goto unlock_vsi;
10204 	}
10205 
10206 	if (pf->vsi[vsi->idx] != vsi) {
10207 		dev_err(&pf->pdev->dev,
10208 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10209 			pf->vsi[vsi->idx]->idx,
10210 			pf->vsi[vsi->idx]->type,
10211 			vsi->idx, vsi->type);
10212 		goto unlock_vsi;
10213 	}
10214 
10215 	/* updates the PF for this cleared vsi */
10216 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10217 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10218 
10219 	bitmap_free(vsi->af_xdp_zc_qps);
10220 	i40e_vsi_free_arrays(vsi, true);
10221 	i40e_clear_rss_config_user(vsi);
10222 
10223 	pf->vsi[vsi->idx] = NULL;
10224 	if (vsi->idx < pf->next_vsi)
10225 		pf->next_vsi = vsi->idx;
10226 
10227 unlock_vsi:
10228 	mutex_unlock(&pf->switch_mutex);
10229 free_vsi:
10230 	kfree(vsi);
10231 
10232 	return 0;
10233 }
10234 
10235 /**
10236  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10237  * @vsi: the VSI being cleaned
10238  **/
10239 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10240 {
10241 	int i;
10242 
10243 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10244 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10245 			kfree_rcu(vsi->tx_rings[i], rcu);
10246 			vsi->tx_rings[i] = NULL;
10247 			vsi->rx_rings[i] = NULL;
10248 			if (vsi->xdp_rings)
10249 				vsi->xdp_rings[i] = NULL;
10250 		}
10251 	}
10252 }
10253 
10254 /**
10255  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10256  * @vsi: the VSI being configured
10257  **/
10258 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10259 {
10260 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10261 	struct i40e_pf *pf = vsi->back;
10262 	struct i40e_ring *ring;
10263 
10264 	/* Set basic values in the rings to be used later during open() */
10265 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10266 		/* allocate space for both Tx and Rx in one shot */
10267 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10268 		if (!ring)
10269 			goto err_out;
10270 
10271 		ring->queue_index = i;
10272 		ring->reg_idx = vsi->base_queue + i;
10273 		ring->ring_active = false;
10274 		ring->vsi = vsi;
10275 		ring->netdev = vsi->netdev;
10276 		ring->dev = &pf->pdev->dev;
10277 		ring->count = vsi->num_desc;
10278 		ring->size = 0;
10279 		ring->dcb_tc = 0;
10280 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10281 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10282 		ring->itr_setting = pf->tx_itr_default;
10283 		vsi->tx_rings[i] = ring++;
10284 
10285 		if (!i40e_enabled_xdp_vsi(vsi))
10286 			goto setup_rx;
10287 
10288 		ring->queue_index = vsi->alloc_queue_pairs + i;
10289 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10290 		ring->ring_active = false;
10291 		ring->vsi = vsi;
10292 		ring->netdev = NULL;
10293 		ring->dev = &pf->pdev->dev;
10294 		ring->count = vsi->num_desc;
10295 		ring->size = 0;
10296 		ring->dcb_tc = 0;
10297 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10298 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10299 		set_ring_xdp(ring);
10300 		ring->itr_setting = pf->tx_itr_default;
10301 		vsi->xdp_rings[i] = ring++;
10302 
10303 setup_rx:
10304 		ring->queue_index = i;
10305 		ring->reg_idx = vsi->base_queue + i;
10306 		ring->ring_active = false;
10307 		ring->vsi = vsi;
10308 		ring->netdev = vsi->netdev;
10309 		ring->dev = &pf->pdev->dev;
10310 		ring->count = vsi->num_desc;
10311 		ring->size = 0;
10312 		ring->dcb_tc = 0;
10313 		ring->itr_setting = pf->rx_itr_default;
10314 		vsi->rx_rings[i] = ring;
10315 	}
10316 
10317 	return 0;
10318 
10319 err_out:
10320 	i40e_vsi_clear_rings(vsi);
10321 	return -ENOMEM;
10322 }
10323 
10324 /**
10325  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10326  * @pf: board private structure
10327  * @vectors: the number of MSI-X vectors to request
10328  *
10329  * Returns the number of vectors reserved, or error
10330  **/
10331 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10332 {
10333 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10334 					I40E_MIN_MSIX, vectors);
10335 	if (vectors < 0) {
10336 		dev_info(&pf->pdev->dev,
10337 			 "MSI-X vector reservation failed: %d\n", vectors);
10338 		vectors = 0;
10339 	}
10340 
10341 	return vectors;
10342 }
10343 
10344 /**
10345  * i40e_init_msix - Setup the MSIX capability
10346  * @pf: board private structure
10347  *
10348  * Work with the OS to set up the MSIX vectors needed.
10349  *
10350  * Returns the number of vectors reserved or negative on failure
10351  **/
10352 static int i40e_init_msix(struct i40e_pf *pf)
10353 {
10354 	struct i40e_hw *hw = &pf->hw;
10355 	int cpus, extra_vectors;
10356 	int vectors_left;
10357 	int v_budget, i;
10358 	int v_actual;
10359 	int iwarp_requested = 0;
10360 
10361 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10362 		return -ENODEV;
10363 
10364 	/* The number of vectors we'll request will be comprised of:
10365 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10366 	 *   - The number of LAN queue pairs
10367 	 *	- Queues being used for RSS.
10368 	 *		We don't need as many as max_rss_size vectors.
10369 	 *		use rss_size instead in the calculation since that
10370 	 *		is governed by number of cpus in the system.
10371 	 *	- assumes symmetric Tx/Rx pairing
10372 	 *   - The number of VMDq pairs
10373 	 *   - The CPU count within the NUMA node if iWARP is enabled
10374 	 * Once we count this up, try the request.
10375 	 *
10376 	 * If we can't get what we want, we'll simplify to nearly nothing
10377 	 * and try again.  If that still fails, we punt.
10378 	 */
10379 	vectors_left = hw->func_caps.num_msix_vectors;
10380 	v_budget = 0;
10381 
10382 	/* reserve one vector for miscellaneous handler */
10383 	if (vectors_left) {
10384 		v_budget++;
10385 		vectors_left--;
10386 	}
10387 
10388 	/* reserve some vectors for the main PF traffic queues. Initially we
10389 	 * only reserve at most 50% of the available vectors, in the case that
10390 	 * the number of online CPUs is large. This ensures that we can enable
10391 	 * extra features as well. Once we've enabled the other features, we
10392 	 * will use any remaining vectors to reach as close as we can to the
10393 	 * number of online CPUs.
10394 	 */
10395 	cpus = num_online_cpus();
10396 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10397 	vectors_left -= pf->num_lan_msix;
10398 
10399 	/* reserve one vector for sideband flow director */
10400 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10401 		if (vectors_left) {
10402 			pf->num_fdsb_msix = 1;
10403 			v_budget++;
10404 			vectors_left--;
10405 		} else {
10406 			pf->num_fdsb_msix = 0;
10407 		}
10408 	}
10409 
10410 	/* can we reserve enough for iWARP? */
10411 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10412 		iwarp_requested = pf->num_iwarp_msix;
10413 
10414 		if (!vectors_left)
10415 			pf->num_iwarp_msix = 0;
10416 		else if (vectors_left < pf->num_iwarp_msix)
10417 			pf->num_iwarp_msix = 1;
10418 		v_budget += pf->num_iwarp_msix;
10419 		vectors_left -= pf->num_iwarp_msix;
10420 	}
10421 
10422 	/* any vectors left over go for VMDq support */
10423 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10424 		if (!vectors_left) {
10425 			pf->num_vmdq_msix = 0;
10426 			pf->num_vmdq_qps = 0;
10427 		} else {
10428 			int vmdq_vecs_wanted =
10429 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
10430 			int vmdq_vecs =
10431 				min_t(int, vectors_left, vmdq_vecs_wanted);
10432 
10433 			/* if we're short on vectors for what's desired, we limit
10434 			 * the queues per vmdq.  If this is still more than are
10435 			 * available, the user will need to change the number of
10436 			 * queues/vectors used by the PF later with the ethtool
10437 			 * channels command
10438 			 */
10439 			if (vectors_left < vmdq_vecs_wanted) {
10440 				pf->num_vmdq_qps = 1;
10441 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
10442 				vmdq_vecs = min_t(int,
10443 						  vectors_left,
10444 						  vmdq_vecs_wanted);
10445 			}
10446 			pf->num_vmdq_msix = pf->num_vmdq_qps;
10447 
10448 			v_budget += vmdq_vecs;
10449 			vectors_left -= vmdq_vecs;
10450 		}
10451 	}
10452 
10453 	/* On systems with a large number of SMP cores, we previously limited
10454 	 * the number of vectors for num_lan_msix to be at most 50% of the
10455 	 * available vectors, to allow for other features. Now, we add back
10456 	 * the remaining vectors. However, we ensure that the total
10457 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10458 	 * calculate the number of vectors we can add without going over the
10459 	 * cap of CPUs. For systems with a small number of CPUs this will be
10460 	 * zero.
10461 	 */
10462 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10463 	pf->num_lan_msix += extra_vectors;
10464 	vectors_left -= extra_vectors;
10465 
10466 	WARN(vectors_left < 0,
10467 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10468 
10469 	v_budget += pf->num_lan_msix;
10470 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10471 				   GFP_KERNEL);
10472 	if (!pf->msix_entries)
10473 		return -ENOMEM;
10474 
10475 	for (i = 0; i < v_budget; i++)
10476 		pf->msix_entries[i].entry = i;
10477 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10478 
10479 	if (v_actual < I40E_MIN_MSIX) {
10480 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10481 		kfree(pf->msix_entries);
10482 		pf->msix_entries = NULL;
10483 		pci_disable_msix(pf->pdev);
10484 		return -ENODEV;
10485 
10486 	} else if (v_actual == I40E_MIN_MSIX) {
10487 		/* Adjust for minimal MSIX use */
10488 		pf->num_vmdq_vsis = 0;
10489 		pf->num_vmdq_qps = 0;
10490 		pf->num_lan_qps = 1;
10491 		pf->num_lan_msix = 1;
10492 
10493 	} else if (v_actual != v_budget) {
10494 		/* If we have limited resources, we will start with no vectors
10495 		 * for the special features and then allocate vectors to some
10496 		 * of these features based on the policy and at the end disable
10497 		 * the features that did not get any vectors.
10498 		 */
10499 		int vec;
10500 
10501 		dev_info(&pf->pdev->dev,
10502 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10503 			 v_actual, v_budget);
10504 		/* reserve the misc vector */
10505 		vec = v_actual - 1;
10506 
10507 		/* Scale vector usage down */
10508 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
10509 		pf->num_vmdq_vsis = 1;
10510 		pf->num_vmdq_qps = 1;
10511 
10512 		/* partition out the remaining vectors */
10513 		switch (vec) {
10514 		case 2:
10515 			pf->num_lan_msix = 1;
10516 			break;
10517 		case 3:
10518 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10519 				pf->num_lan_msix = 1;
10520 				pf->num_iwarp_msix = 1;
10521 			} else {
10522 				pf->num_lan_msix = 2;
10523 			}
10524 			break;
10525 		default:
10526 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10527 				pf->num_iwarp_msix = min_t(int, (vec / 3),
10528 						 iwarp_requested);
10529 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
10530 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10531 			} else {
10532 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
10533 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10534 			}
10535 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10536 				pf->num_fdsb_msix = 1;
10537 				vec--;
10538 			}
10539 			pf->num_lan_msix = min_t(int,
10540 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10541 							      pf->num_lan_msix);
10542 			pf->num_lan_qps = pf->num_lan_msix;
10543 			break;
10544 		}
10545 	}
10546 
10547 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10548 	    (pf->num_fdsb_msix == 0)) {
10549 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10550 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10551 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10552 	}
10553 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10554 	    (pf->num_vmdq_msix == 0)) {
10555 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10556 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10557 	}
10558 
10559 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10560 	    (pf->num_iwarp_msix == 0)) {
10561 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10562 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10563 	}
10564 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10565 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10566 		   pf->num_lan_msix,
10567 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
10568 		   pf->num_fdsb_msix,
10569 		   pf->num_iwarp_msix);
10570 
10571 	return v_actual;
10572 }
10573 
10574 /**
10575  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10576  * @vsi: the VSI being configured
10577  * @v_idx: index of the vector in the vsi struct
10578  * @cpu: cpu to be used on affinity_mask
10579  *
10580  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
10581  **/
10582 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10583 {
10584 	struct i40e_q_vector *q_vector;
10585 
10586 	/* allocate q_vector */
10587 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10588 	if (!q_vector)
10589 		return -ENOMEM;
10590 
10591 	q_vector->vsi = vsi;
10592 	q_vector->v_idx = v_idx;
10593 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10594 
10595 	if (vsi->netdev)
10596 		netif_napi_add(vsi->netdev, &q_vector->napi,
10597 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
10598 
10599 	/* tie q_vector and vsi together */
10600 	vsi->q_vectors[v_idx] = q_vector;
10601 
10602 	return 0;
10603 }
10604 
10605 /**
10606  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10607  * @vsi: the VSI being configured
10608  *
10609  * We allocate one q_vector per queue interrupt.  If allocation fails we
10610  * return -ENOMEM.
10611  **/
10612 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10613 {
10614 	struct i40e_pf *pf = vsi->back;
10615 	int err, v_idx, num_q_vectors, current_cpu;
10616 
10617 	/* if not MSIX, give the one vector only to the LAN VSI */
10618 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10619 		num_q_vectors = vsi->num_q_vectors;
10620 	else if (vsi == pf->vsi[pf->lan_vsi])
10621 		num_q_vectors = 1;
10622 	else
10623 		return -EINVAL;
10624 
10625 	current_cpu = cpumask_first(cpu_online_mask);
10626 
10627 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10628 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10629 		if (err)
10630 			goto err_out;
10631 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10632 		if (unlikely(current_cpu >= nr_cpu_ids))
10633 			current_cpu = cpumask_first(cpu_online_mask);
10634 	}
10635 
10636 	return 0;
10637 
10638 err_out:
10639 	while (v_idx--)
10640 		i40e_free_q_vector(vsi, v_idx);
10641 
10642 	return err;
10643 }
10644 
10645 /**
10646  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10647  * @pf: board private structure to initialize
10648  **/
10649 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10650 {
10651 	int vectors = 0;
10652 	ssize_t size;
10653 
10654 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10655 		vectors = i40e_init_msix(pf);
10656 		if (vectors < 0) {
10657 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
10658 				       I40E_FLAG_IWARP_ENABLED	|
10659 				       I40E_FLAG_RSS_ENABLED	|
10660 				       I40E_FLAG_DCB_CAPABLE	|
10661 				       I40E_FLAG_DCB_ENABLED	|
10662 				       I40E_FLAG_SRIOV_ENABLED	|
10663 				       I40E_FLAG_FD_SB_ENABLED	|
10664 				       I40E_FLAG_FD_ATR_ENABLED	|
10665 				       I40E_FLAG_VMDQ_ENABLED);
10666 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10667 
10668 			/* rework the queue expectations without MSIX */
10669 			i40e_determine_queue_usage(pf);
10670 		}
10671 	}
10672 
10673 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10674 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10675 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10676 		vectors = pci_enable_msi(pf->pdev);
10677 		if (vectors < 0) {
10678 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10679 				 vectors);
10680 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10681 		}
10682 		vectors = 1;  /* one MSI or Legacy vector */
10683 	}
10684 
10685 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10686 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10687 
10688 	/* set up vector assignment tracking */
10689 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10690 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
10691 	if (!pf->irq_pile)
10692 		return -ENOMEM;
10693 
10694 	pf->irq_pile->num_entries = vectors;
10695 	pf->irq_pile->search_hint = 0;
10696 
10697 	/* track first vector for misc interrupts, ignore return */
10698 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10699 
10700 	return 0;
10701 }
10702 
10703 /**
10704  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10705  * @pf: private board data structure
10706  *
10707  * Restore the interrupt scheme that was cleared when we suspended the
10708  * device. This should be called during resume to re-allocate the q_vectors
10709  * and reacquire IRQs.
10710  */
10711 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10712 {
10713 	int err, i;
10714 
10715 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
10716 	 * scheme. We need to re-enabled them here in order to attempt to
10717 	 * re-acquire the MSI or MSI-X vectors
10718 	 */
10719 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10720 
10721 	err = i40e_init_interrupt_scheme(pf);
10722 	if (err)
10723 		return err;
10724 
10725 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
10726 	 * rings together again.
10727 	 */
10728 	for (i = 0; i < pf->num_alloc_vsi; i++) {
10729 		if (pf->vsi[i]) {
10730 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10731 			if (err)
10732 				goto err_unwind;
10733 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10734 		}
10735 	}
10736 
10737 	err = i40e_setup_misc_vector(pf);
10738 	if (err)
10739 		goto err_unwind;
10740 
10741 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
10742 		i40e_client_update_msix_info(pf);
10743 
10744 	return 0;
10745 
10746 err_unwind:
10747 	while (i--) {
10748 		if (pf->vsi[i])
10749 			i40e_vsi_free_q_vectors(pf->vsi[i]);
10750 	}
10751 
10752 	return err;
10753 }
10754 
10755 /**
10756  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10757  * @pf: board private structure
10758  *
10759  * This sets up the handler for MSIX 0, which is used to manage the
10760  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
10761  * when in MSI or Legacy interrupt mode.
10762  **/
10763 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10764 {
10765 	struct i40e_hw *hw = &pf->hw;
10766 	int err = 0;
10767 
10768 	/* Only request the IRQ once, the first time through. */
10769 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10770 		err = request_irq(pf->msix_entries[0].vector,
10771 				  i40e_intr, 0, pf->int_name, pf);
10772 		if (err) {
10773 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10774 			dev_info(&pf->pdev->dev,
10775 				 "request_irq for %s failed: %d\n",
10776 				 pf->int_name, err);
10777 			return -EFAULT;
10778 		}
10779 	}
10780 
10781 	i40e_enable_misc_int_causes(pf);
10782 
10783 	/* associate no queues to the misc vector */
10784 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10785 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10786 
10787 	i40e_flush(hw);
10788 
10789 	i40e_irq_dynamic_enable_icr0(pf);
10790 
10791 	return err;
10792 }
10793 
10794 /**
10795  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10796  * @vsi: Pointer to vsi structure
10797  * @seed: Buffter to store the hash keys
10798  * @lut: Buffer to store the lookup table entries
10799  * @lut_size: Size of buffer to store the lookup table entries
10800  *
10801  * Return 0 on success, negative on failure
10802  */
10803 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10804 			   u8 *lut, u16 lut_size)
10805 {
10806 	struct i40e_pf *pf = vsi->back;
10807 	struct i40e_hw *hw = &pf->hw;
10808 	int ret = 0;
10809 
10810 	if (seed) {
10811 		ret = i40e_aq_get_rss_key(hw, vsi->id,
10812 			(struct i40e_aqc_get_set_rss_key_data *)seed);
10813 		if (ret) {
10814 			dev_info(&pf->pdev->dev,
10815 				 "Cannot get RSS key, err %s aq_err %s\n",
10816 				 i40e_stat_str(&pf->hw, ret),
10817 				 i40e_aq_str(&pf->hw,
10818 					     pf->hw.aq.asq_last_status));
10819 			return ret;
10820 		}
10821 	}
10822 
10823 	if (lut) {
10824 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10825 
10826 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10827 		if (ret) {
10828 			dev_info(&pf->pdev->dev,
10829 				 "Cannot get RSS lut, err %s aq_err %s\n",
10830 				 i40e_stat_str(&pf->hw, ret),
10831 				 i40e_aq_str(&pf->hw,
10832 					     pf->hw.aq.asq_last_status));
10833 			return ret;
10834 		}
10835 	}
10836 
10837 	return ret;
10838 }
10839 
10840 /**
10841  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10842  * @vsi: Pointer to vsi structure
10843  * @seed: RSS hash seed
10844  * @lut: Lookup table
10845  * @lut_size: Lookup table size
10846  *
10847  * Returns 0 on success, negative on failure
10848  **/
10849 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10850 			       const u8 *lut, u16 lut_size)
10851 {
10852 	struct i40e_pf *pf = vsi->back;
10853 	struct i40e_hw *hw = &pf->hw;
10854 	u16 vf_id = vsi->vf_id;
10855 	u8 i;
10856 
10857 	/* Fill out hash function seed */
10858 	if (seed) {
10859 		u32 *seed_dw = (u32 *)seed;
10860 
10861 		if (vsi->type == I40E_VSI_MAIN) {
10862 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10863 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10864 		} else if (vsi->type == I40E_VSI_SRIOV) {
10865 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10866 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10867 		} else {
10868 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10869 		}
10870 	}
10871 
10872 	if (lut) {
10873 		u32 *lut_dw = (u32 *)lut;
10874 
10875 		if (vsi->type == I40E_VSI_MAIN) {
10876 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
10877 				return -EINVAL;
10878 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10879 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10880 		} else if (vsi->type == I40E_VSI_SRIOV) {
10881 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10882 				return -EINVAL;
10883 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10884 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10885 		} else {
10886 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10887 		}
10888 	}
10889 	i40e_flush(hw);
10890 
10891 	return 0;
10892 }
10893 
10894 /**
10895  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10896  * @vsi: Pointer to VSI structure
10897  * @seed: Buffer to store the keys
10898  * @lut: Buffer to store the lookup table entries
10899  * @lut_size: Size of buffer to store the lookup table entries
10900  *
10901  * Returns 0 on success, negative on failure
10902  */
10903 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10904 			    u8 *lut, u16 lut_size)
10905 {
10906 	struct i40e_pf *pf = vsi->back;
10907 	struct i40e_hw *hw = &pf->hw;
10908 	u16 i;
10909 
10910 	if (seed) {
10911 		u32 *seed_dw = (u32 *)seed;
10912 
10913 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10914 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
10915 	}
10916 	if (lut) {
10917 		u32 *lut_dw = (u32 *)lut;
10918 
10919 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
10920 			return -EINVAL;
10921 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10922 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10923 	}
10924 
10925 	return 0;
10926 }
10927 
10928 /**
10929  * i40e_config_rss - Configure RSS keys and lut
10930  * @vsi: Pointer to VSI structure
10931  * @seed: RSS hash seed
10932  * @lut: Lookup table
10933  * @lut_size: Lookup table size
10934  *
10935  * Returns 0 on success, negative on failure
10936  */
10937 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10938 {
10939 	struct i40e_pf *pf = vsi->back;
10940 
10941 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10942 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10943 	else
10944 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10945 }
10946 
10947 /**
10948  * i40e_get_rss - Get RSS keys and lut
10949  * @vsi: Pointer to VSI structure
10950  * @seed: Buffer to store the keys
10951  * @lut: Buffer to store the lookup table entries
10952  * @lut_size: Size of buffer to store the lookup table entries
10953  *
10954  * Returns 0 on success, negative on failure
10955  */
10956 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10957 {
10958 	struct i40e_pf *pf = vsi->back;
10959 
10960 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10961 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10962 	else
10963 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
10964 }
10965 
10966 /**
10967  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10968  * @pf: Pointer to board private structure
10969  * @lut: Lookup table
10970  * @rss_table_size: Lookup table size
10971  * @rss_size: Range of queue number for hashing
10972  */
10973 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10974 		       u16 rss_table_size, u16 rss_size)
10975 {
10976 	u16 i;
10977 
10978 	for (i = 0; i < rss_table_size; i++)
10979 		lut[i] = i % rss_size;
10980 }
10981 
10982 /**
10983  * i40e_pf_config_rss - Prepare for RSS if used
10984  * @pf: board private structure
10985  **/
10986 static int i40e_pf_config_rss(struct i40e_pf *pf)
10987 {
10988 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10989 	u8 seed[I40E_HKEY_ARRAY_SIZE];
10990 	u8 *lut;
10991 	struct i40e_hw *hw = &pf->hw;
10992 	u32 reg_val;
10993 	u64 hena;
10994 	int ret;
10995 
10996 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
10997 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10998 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
10999 	hena |= i40e_pf_get_default_rss_hena(pf);
11000 
11001 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11002 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11003 
11004 	/* Determine the RSS table size based on the hardware capabilities */
11005 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11006 	reg_val = (pf->rss_table_size == 512) ?
11007 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11008 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11009 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11010 
11011 	/* Determine the RSS size of the VSI */
11012 	if (!vsi->rss_size) {
11013 		u16 qcount;
11014 		/* If the firmware does something weird during VSI init, we
11015 		 * could end up with zero TCs. Check for that to avoid
11016 		 * divide-by-zero. It probably won't pass traffic, but it also
11017 		 * won't panic.
11018 		 */
11019 		qcount = vsi->num_queue_pairs /
11020 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11021 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11022 	}
11023 	if (!vsi->rss_size)
11024 		return -EINVAL;
11025 
11026 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11027 	if (!lut)
11028 		return -ENOMEM;
11029 
11030 	/* Use user configured lut if there is one, otherwise use default */
11031 	if (vsi->rss_lut_user)
11032 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11033 	else
11034 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11035 
11036 	/* Use user configured hash key if there is one, otherwise
11037 	 * use default.
11038 	 */
11039 	if (vsi->rss_hkey_user)
11040 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11041 	else
11042 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11043 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11044 	kfree(lut);
11045 
11046 	return ret;
11047 }
11048 
11049 /**
11050  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11051  * @pf: board private structure
11052  * @queue_count: the requested queue count for rss.
11053  *
11054  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11055  * count which may be different from the requested queue count.
11056  * Note: expects to be called while under rtnl_lock()
11057  **/
11058 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11059 {
11060 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11061 	int new_rss_size;
11062 
11063 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11064 		return 0;
11065 
11066 	queue_count = min_t(int, queue_count, num_online_cpus());
11067 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11068 
11069 	if (queue_count != vsi->num_queue_pairs) {
11070 		u16 qcount;
11071 
11072 		vsi->req_queue_pairs = queue_count;
11073 		i40e_prep_for_reset(pf, true);
11074 
11075 		pf->alloc_rss_size = new_rss_size;
11076 
11077 		i40e_reset_and_rebuild(pf, true, true);
11078 
11079 		/* Discard the user configured hash keys and lut, if less
11080 		 * queues are enabled.
11081 		 */
11082 		if (queue_count < vsi->rss_size) {
11083 			i40e_clear_rss_config_user(vsi);
11084 			dev_dbg(&pf->pdev->dev,
11085 				"discard user configured hash keys and lut\n");
11086 		}
11087 
11088 		/* Reset vsi->rss_size, as number of enabled queues changed */
11089 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11090 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11091 
11092 		i40e_pf_config_rss(pf);
11093 	}
11094 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11095 		 vsi->req_queue_pairs, pf->rss_size_max);
11096 	return pf->alloc_rss_size;
11097 }
11098 
11099 /**
11100  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11101  * @pf: board private structure
11102  **/
11103 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11104 {
11105 	i40e_status status;
11106 	bool min_valid, max_valid;
11107 	u32 max_bw, min_bw;
11108 
11109 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11110 					   &min_valid, &max_valid);
11111 
11112 	if (!status) {
11113 		if (min_valid)
11114 			pf->min_bw = min_bw;
11115 		if (max_valid)
11116 			pf->max_bw = max_bw;
11117 	}
11118 
11119 	return status;
11120 }
11121 
11122 /**
11123  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11124  * @pf: board private structure
11125  **/
11126 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11127 {
11128 	struct i40e_aqc_configure_partition_bw_data bw_data;
11129 	i40e_status status;
11130 
11131 	/* Set the valid bit for this PF */
11132 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11133 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11134 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11135 
11136 	/* Set the new bandwidths */
11137 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11138 
11139 	return status;
11140 }
11141 
11142 /**
11143  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11144  * @pf: board private structure
11145  **/
11146 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11147 {
11148 	/* Commit temporary BW setting to permanent NVM image */
11149 	enum i40e_admin_queue_err last_aq_status;
11150 	i40e_status ret;
11151 	u16 nvm_word;
11152 
11153 	if (pf->hw.partition_id != 1) {
11154 		dev_info(&pf->pdev->dev,
11155 			 "Commit BW only works on partition 1! This is partition %d",
11156 			 pf->hw.partition_id);
11157 		ret = I40E_NOT_SUPPORTED;
11158 		goto bw_commit_out;
11159 	}
11160 
11161 	/* Acquire NVM for read access */
11162 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11163 	last_aq_status = pf->hw.aq.asq_last_status;
11164 	if (ret) {
11165 		dev_info(&pf->pdev->dev,
11166 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11167 			 i40e_stat_str(&pf->hw, ret),
11168 			 i40e_aq_str(&pf->hw, last_aq_status));
11169 		goto bw_commit_out;
11170 	}
11171 
11172 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11173 	ret = i40e_aq_read_nvm(&pf->hw,
11174 			       I40E_SR_NVM_CONTROL_WORD,
11175 			       0x10, sizeof(nvm_word), &nvm_word,
11176 			       false, NULL);
11177 	/* Save off last admin queue command status before releasing
11178 	 * the NVM
11179 	 */
11180 	last_aq_status = pf->hw.aq.asq_last_status;
11181 	i40e_release_nvm(&pf->hw);
11182 	if (ret) {
11183 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11184 			 i40e_stat_str(&pf->hw, ret),
11185 			 i40e_aq_str(&pf->hw, last_aq_status));
11186 		goto bw_commit_out;
11187 	}
11188 
11189 	/* Wait a bit for NVM release to complete */
11190 	msleep(50);
11191 
11192 	/* Acquire NVM for write access */
11193 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11194 	last_aq_status = pf->hw.aq.asq_last_status;
11195 	if (ret) {
11196 		dev_info(&pf->pdev->dev,
11197 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11198 			 i40e_stat_str(&pf->hw, ret),
11199 			 i40e_aq_str(&pf->hw, last_aq_status));
11200 		goto bw_commit_out;
11201 	}
11202 	/* Write it back out unchanged to initiate update NVM,
11203 	 * which will force a write of the shadow (alt) RAM to
11204 	 * the NVM - thus storing the bandwidth values permanently.
11205 	 */
11206 	ret = i40e_aq_update_nvm(&pf->hw,
11207 				 I40E_SR_NVM_CONTROL_WORD,
11208 				 0x10, sizeof(nvm_word),
11209 				 &nvm_word, true, 0, NULL);
11210 	/* Save off last admin queue command status before releasing
11211 	 * the NVM
11212 	 */
11213 	last_aq_status = pf->hw.aq.asq_last_status;
11214 	i40e_release_nvm(&pf->hw);
11215 	if (ret)
11216 		dev_info(&pf->pdev->dev,
11217 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11218 			 i40e_stat_str(&pf->hw, ret),
11219 			 i40e_aq_str(&pf->hw, last_aq_status));
11220 bw_commit_out:
11221 
11222 	return ret;
11223 }
11224 
11225 /**
11226  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11227  * @pf: board private structure to initialize
11228  *
11229  * i40e_sw_init initializes the Adapter private data structure.
11230  * Fields are initialized based on PCI device information and
11231  * OS network device settings (MTU size).
11232  **/
11233 static int i40e_sw_init(struct i40e_pf *pf)
11234 {
11235 	int err = 0;
11236 	int size;
11237 
11238 	/* Set default capability flags */
11239 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11240 		    I40E_FLAG_MSI_ENABLED     |
11241 		    I40E_FLAG_MSIX_ENABLED;
11242 
11243 	/* Set default ITR */
11244 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11245 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11246 
11247 	/* Depending on PF configurations, it is possible that the RSS
11248 	 * maximum might end up larger than the available queues
11249 	 */
11250 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11251 	pf->alloc_rss_size = 1;
11252 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11253 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11254 				 pf->hw.func_caps.num_tx_qp);
11255 	if (pf->hw.func_caps.rss) {
11256 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11257 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11258 					   num_online_cpus());
11259 	}
11260 
11261 	/* MFP mode enabled */
11262 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11263 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11264 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11265 		if (i40e_get_partition_bw_setting(pf)) {
11266 			dev_warn(&pf->pdev->dev,
11267 				 "Could not get partition bw settings\n");
11268 		} else {
11269 			dev_info(&pf->pdev->dev,
11270 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11271 				 pf->min_bw, pf->max_bw);
11272 
11273 			/* nudge the Tx scheduler */
11274 			i40e_set_partition_bw_setting(pf);
11275 		}
11276 	}
11277 
11278 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11279 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11280 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11281 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11282 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11283 		    pf->hw.num_partitions > 1)
11284 			dev_info(&pf->pdev->dev,
11285 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11286 		else
11287 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11288 		pf->fdir_pf_filter_count =
11289 				 pf->hw.func_caps.fd_filters_guaranteed;
11290 		pf->hw.fdir_shared_filter_count =
11291 				 pf->hw.func_caps.fd_filters_best_effort;
11292 	}
11293 
11294 	if (pf->hw.mac.type == I40E_MAC_X722) {
11295 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11296 				    I40E_HW_128_QP_RSS_CAPABLE |
11297 				    I40E_HW_ATR_EVICT_CAPABLE |
11298 				    I40E_HW_WB_ON_ITR_CAPABLE |
11299 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11300 				    I40E_HW_NO_PCI_LINK_CHECK |
11301 				    I40E_HW_USE_SET_LLDP_MIB |
11302 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11303 				    I40E_HW_PTP_L4_CAPABLE |
11304 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11305 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11306 
11307 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11308 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11309 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11310 			dev_warn(&pf->pdev->dev,
11311 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11312 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11313 		}
11314 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11315 		   ((pf->hw.aq.api_maj_ver == 1) &&
11316 		    (pf->hw.aq.api_min_ver > 4))) {
11317 		/* Supported in FW API version higher than 1.4 */
11318 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11319 	}
11320 
11321 	/* Enable HW ATR eviction if possible */
11322 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11323 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11324 
11325 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11326 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11327 	    (pf->hw.aq.fw_maj_ver < 4))) {
11328 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11329 		/* No DCB support  for FW < v4.33 */
11330 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11331 	}
11332 
11333 	/* Disable FW LLDP if FW < v4.3 */
11334 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11335 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11336 	    (pf->hw.aq.fw_maj_ver < 4)))
11337 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11338 
11339 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11340 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11341 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11342 	    (pf->hw.aq.fw_maj_ver >= 5)))
11343 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11344 
11345 	/* Enable PTP L4 if FW > v6.0 */
11346 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11347 	    pf->hw.aq.fw_maj_ver >= 6)
11348 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11349 
11350 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11351 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11352 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11353 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11354 	}
11355 
11356 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11357 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11358 		/* IWARP needs one extra vector for CQP just like MISC.*/
11359 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11360 	}
11361 	/* Stopping FW LLDP engine is supported on XL710 and X722
11362 	 * starting from FW versions determined in i40e_init_adminq.
11363 	 * Stopping the FW LLDP engine is not supported on XL710
11364 	 * if NPAR is functioning so unset this hw flag in this case.
11365 	 */
11366 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11367 	    pf->hw.func_caps.npar_enable &&
11368 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
11369 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
11370 
11371 #ifdef CONFIG_PCI_IOV
11372 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11373 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11374 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11375 		pf->num_req_vfs = min_t(int,
11376 					pf->hw.func_caps.num_vfs,
11377 					I40E_MAX_VF_COUNT);
11378 	}
11379 #endif /* CONFIG_PCI_IOV */
11380 	pf->eeprom_version = 0xDEAD;
11381 	pf->lan_veb = I40E_NO_VEB;
11382 	pf->lan_vsi = I40E_NO_VSI;
11383 
11384 	/* By default FW has this off for performance reasons */
11385 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11386 
11387 	/* set up queue assignment tracking */
11388 	size = sizeof(struct i40e_lump_tracking)
11389 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11390 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
11391 	if (!pf->qp_pile) {
11392 		err = -ENOMEM;
11393 		goto sw_init_done;
11394 	}
11395 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11396 	pf->qp_pile->search_hint = 0;
11397 
11398 	pf->tx_timeout_recovery_level = 1;
11399 
11400 	mutex_init(&pf->switch_mutex);
11401 
11402 sw_init_done:
11403 	return err;
11404 }
11405 
11406 /**
11407  * i40e_set_ntuple - set the ntuple feature flag and take action
11408  * @pf: board private structure to initialize
11409  * @features: the feature set that the stack is suggesting
11410  *
11411  * returns a bool to indicate if reset needs to happen
11412  **/
11413 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11414 {
11415 	bool need_reset = false;
11416 
11417 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
11418 	 * the state changed, we need to reset.
11419 	 */
11420 	if (features & NETIF_F_NTUPLE) {
11421 		/* Enable filters and mark for reset */
11422 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11423 			need_reset = true;
11424 		/* enable FD_SB only if there is MSI-X vector and no cloud
11425 		 * filters exist
11426 		 */
11427 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11428 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11429 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11430 		}
11431 	} else {
11432 		/* turn off filters, mark for reset and clear SW filter list */
11433 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11434 			need_reset = true;
11435 			i40e_fdir_filter_exit(pf);
11436 		}
11437 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11438 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
11439 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11440 
11441 		/* reset fd counters */
11442 		pf->fd_add_err = 0;
11443 		pf->fd_atr_cnt = 0;
11444 		/* if ATR was auto disabled it can be re-enabled. */
11445 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
11446 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11447 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
11448 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11449 	}
11450 	return need_reset;
11451 }
11452 
11453 /**
11454  * i40e_clear_rss_lut - clear the rx hash lookup table
11455  * @vsi: the VSI being configured
11456  **/
11457 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11458 {
11459 	struct i40e_pf *pf = vsi->back;
11460 	struct i40e_hw *hw = &pf->hw;
11461 	u16 vf_id = vsi->vf_id;
11462 	u8 i;
11463 
11464 	if (vsi->type == I40E_VSI_MAIN) {
11465 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11466 			wr32(hw, I40E_PFQF_HLUT(i), 0);
11467 	} else if (vsi->type == I40E_VSI_SRIOV) {
11468 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11469 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11470 	} else {
11471 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11472 	}
11473 }
11474 
11475 /**
11476  * i40e_set_features - set the netdev feature flags
11477  * @netdev: ptr to the netdev being adjusted
11478  * @features: the feature set that the stack is suggesting
11479  * Note: expects to be called while under rtnl_lock()
11480  **/
11481 static int i40e_set_features(struct net_device *netdev,
11482 			     netdev_features_t features)
11483 {
11484 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11485 	struct i40e_vsi *vsi = np->vsi;
11486 	struct i40e_pf *pf = vsi->back;
11487 	bool need_reset;
11488 
11489 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11490 		i40e_pf_config_rss(pf);
11491 	else if (!(features & NETIF_F_RXHASH) &&
11492 		 netdev->features & NETIF_F_RXHASH)
11493 		i40e_clear_rss_lut(vsi);
11494 
11495 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
11496 		i40e_vlan_stripping_enable(vsi);
11497 	else
11498 		i40e_vlan_stripping_disable(vsi);
11499 
11500 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11501 		dev_err(&pf->pdev->dev,
11502 			"Offloaded tc filters active, can't turn hw_tc_offload off");
11503 		return -EINVAL;
11504 	}
11505 
11506 	need_reset = i40e_set_ntuple(pf, features);
11507 
11508 	if (need_reset)
11509 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11510 
11511 	return 0;
11512 }
11513 
11514 /**
11515  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11516  * @pf: board private structure
11517  * @port: The UDP port to look up
11518  *
11519  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11520  **/
11521 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11522 {
11523 	u8 i;
11524 
11525 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11526 		/* Do not report ports with pending deletions as
11527 		 * being available.
11528 		 */
11529 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
11530 			continue;
11531 		if (pf->udp_ports[i].port == port)
11532 			return i;
11533 	}
11534 
11535 	return i;
11536 }
11537 
11538 /**
11539  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11540  * @netdev: This physical port's netdev
11541  * @ti: Tunnel endpoint information
11542  **/
11543 static void i40e_udp_tunnel_add(struct net_device *netdev,
11544 				struct udp_tunnel_info *ti)
11545 {
11546 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11547 	struct i40e_vsi *vsi = np->vsi;
11548 	struct i40e_pf *pf = vsi->back;
11549 	u16 port = ntohs(ti->port);
11550 	u8 next_idx;
11551 	u8 idx;
11552 
11553 	idx = i40e_get_udp_port_idx(pf, port);
11554 
11555 	/* Check if port already exists */
11556 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11557 		netdev_info(netdev, "port %d already offloaded\n", port);
11558 		return;
11559 	}
11560 
11561 	/* Now check if there is space to add the new port */
11562 	next_idx = i40e_get_udp_port_idx(pf, 0);
11563 
11564 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11565 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11566 			    port);
11567 		return;
11568 	}
11569 
11570 	switch (ti->type) {
11571 	case UDP_TUNNEL_TYPE_VXLAN:
11572 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11573 		break;
11574 	case UDP_TUNNEL_TYPE_GENEVE:
11575 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11576 			return;
11577 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11578 		break;
11579 	default:
11580 		return;
11581 	}
11582 
11583 	/* New port: add it and mark its index in the bitmap */
11584 	pf->udp_ports[next_idx].port = port;
11585 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
11586 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11587 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11588 }
11589 
11590 /**
11591  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11592  * @netdev: This physical port's netdev
11593  * @ti: Tunnel endpoint information
11594  **/
11595 static void i40e_udp_tunnel_del(struct net_device *netdev,
11596 				struct udp_tunnel_info *ti)
11597 {
11598 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11599 	struct i40e_vsi *vsi = np->vsi;
11600 	struct i40e_pf *pf = vsi->back;
11601 	u16 port = ntohs(ti->port);
11602 	u8 idx;
11603 
11604 	idx = i40e_get_udp_port_idx(pf, port);
11605 
11606 	/* Check if port already exists */
11607 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11608 		goto not_found;
11609 
11610 	switch (ti->type) {
11611 	case UDP_TUNNEL_TYPE_VXLAN:
11612 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11613 			goto not_found;
11614 		break;
11615 	case UDP_TUNNEL_TYPE_GENEVE:
11616 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11617 			goto not_found;
11618 		break;
11619 	default:
11620 		goto not_found;
11621 	}
11622 
11623 	/* if port exists, set it to 0 (mark for deletion)
11624 	 * and make it pending
11625 	 */
11626 	pf->udp_ports[idx].port = 0;
11627 
11628 	/* Toggle pending bit instead of setting it. This way if we are
11629 	 * deleting a port that has yet to be added we just clear the pending
11630 	 * bit and don't have to worry about it.
11631 	 */
11632 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
11633 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11634 
11635 	return;
11636 not_found:
11637 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11638 		    port);
11639 }
11640 
11641 static int i40e_get_phys_port_id(struct net_device *netdev,
11642 				 struct netdev_phys_item_id *ppid)
11643 {
11644 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11645 	struct i40e_pf *pf = np->vsi->back;
11646 	struct i40e_hw *hw = &pf->hw;
11647 
11648 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11649 		return -EOPNOTSUPP;
11650 
11651 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11652 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11653 
11654 	return 0;
11655 }
11656 
11657 /**
11658  * i40e_ndo_fdb_add - add an entry to the hardware database
11659  * @ndm: the input from the stack
11660  * @tb: pointer to array of nladdr (unused)
11661  * @dev: the net device pointer
11662  * @addr: the MAC address entry being added
11663  * @vid: VLAN ID
11664  * @flags: instructions from stack about fdb operation
11665  */
11666 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11667 			    struct net_device *dev,
11668 			    const unsigned char *addr, u16 vid,
11669 			    u16 flags,
11670 			    struct netlink_ext_ack *extack)
11671 {
11672 	struct i40e_netdev_priv *np = netdev_priv(dev);
11673 	struct i40e_pf *pf = np->vsi->back;
11674 	int err = 0;
11675 
11676 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11677 		return -EOPNOTSUPP;
11678 
11679 	if (vid) {
11680 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11681 		return -EINVAL;
11682 	}
11683 
11684 	/* Hardware does not support aging addresses so if a
11685 	 * ndm_state is given only allow permanent addresses
11686 	 */
11687 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11688 		netdev_info(dev, "FDB only supports static addresses\n");
11689 		return -EINVAL;
11690 	}
11691 
11692 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11693 		err = dev_uc_add_excl(dev, addr);
11694 	else if (is_multicast_ether_addr(addr))
11695 		err = dev_mc_add_excl(dev, addr);
11696 	else
11697 		err = -EINVAL;
11698 
11699 	/* Only return duplicate errors if NLM_F_EXCL is set */
11700 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
11701 		err = 0;
11702 
11703 	return err;
11704 }
11705 
11706 /**
11707  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11708  * @dev: the netdev being configured
11709  * @nlh: RTNL message
11710  * @flags: bridge flags
11711  * @extack: netlink extended ack
11712  *
11713  * Inserts a new hardware bridge if not already created and
11714  * enables the bridging mode requested (VEB or VEPA). If the
11715  * hardware bridge has already been inserted and the request
11716  * is to change the mode then that requires a PF reset to
11717  * allow rebuild of the components with required hardware
11718  * bridge mode enabled.
11719  *
11720  * Note: expects to be called while under rtnl_lock()
11721  **/
11722 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11723 				   struct nlmsghdr *nlh,
11724 				   u16 flags,
11725 				   struct netlink_ext_ack *extack)
11726 {
11727 	struct i40e_netdev_priv *np = netdev_priv(dev);
11728 	struct i40e_vsi *vsi = np->vsi;
11729 	struct i40e_pf *pf = vsi->back;
11730 	struct i40e_veb *veb = NULL;
11731 	struct nlattr *attr, *br_spec;
11732 	int i, rem;
11733 
11734 	/* Only for PF VSI for now */
11735 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11736 		return -EOPNOTSUPP;
11737 
11738 	/* Find the HW bridge for PF VSI */
11739 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11740 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11741 			veb = pf->veb[i];
11742 	}
11743 
11744 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11745 
11746 	nla_for_each_nested(attr, br_spec, rem) {
11747 		__u16 mode;
11748 
11749 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
11750 			continue;
11751 
11752 		mode = nla_get_u16(attr);
11753 		if ((mode != BRIDGE_MODE_VEPA) &&
11754 		    (mode != BRIDGE_MODE_VEB))
11755 			return -EINVAL;
11756 
11757 		/* Insert a new HW bridge */
11758 		if (!veb) {
11759 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11760 					     vsi->tc_config.enabled_tc);
11761 			if (veb) {
11762 				veb->bridge_mode = mode;
11763 				i40e_config_bridge_mode(veb);
11764 			} else {
11765 				/* No Bridge HW offload available */
11766 				return -ENOENT;
11767 			}
11768 			break;
11769 		} else if (mode != veb->bridge_mode) {
11770 			/* Existing HW bridge but different mode needs reset */
11771 			veb->bridge_mode = mode;
11772 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11773 			if (mode == BRIDGE_MODE_VEB)
11774 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11775 			else
11776 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11777 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11778 			break;
11779 		}
11780 	}
11781 
11782 	return 0;
11783 }
11784 
11785 /**
11786  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11787  * @skb: skb buff
11788  * @pid: process id
11789  * @seq: RTNL message seq #
11790  * @dev: the netdev being configured
11791  * @filter_mask: unused
11792  * @nlflags: netlink flags passed in
11793  *
11794  * Return the mode in which the hardware bridge is operating in
11795  * i.e VEB or VEPA.
11796  **/
11797 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11798 				   struct net_device *dev,
11799 				   u32 __always_unused filter_mask,
11800 				   int nlflags)
11801 {
11802 	struct i40e_netdev_priv *np = netdev_priv(dev);
11803 	struct i40e_vsi *vsi = np->vsi;
11804 	struct i40e_pf *pf = vsi->back;
11805 	struct i40e_veb *veb = NULL;
11806 	int i;
11807 
11808 	/* Only for PF VSI for now */
11809 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11810 		return -EOPNOTSUPP;
11811 
11812 	/* Find the HW bridge for the PF VSI */
11813 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11814 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11815 			veb = pf->veb[i];
11816 	}
11817 
11818 	if (!veb)
11819 		return 0;
11820 
11821 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11822 				       0, 0, nlflags, filter_mask, NULL);
11823 }
11824 
11825 /**
11826  * i40e_features_check - Validate encapsulated packet conforms to limits
11827  * @skb: skb buff
11828  * @dev: This physical port's netdev
11829  * @features: Offload features that the stack believes apply
11830  **/
11831 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11832 					     struct net_device *dev,
11833 					     netdev_features_t features)
11834 {
11835 	size_t len;
11836 
11837 	/* No point in doing any of this if neither checksum nor GSO are
11838 	 * being requested for this frame.  We can rule out both by just
11839 	 * checking for CHECKSUM_PARTIAL
11840 	 */
11841 	if (skb->ip_summed != CHECKSUM_PARTIAL)
11842 		return features;
11843 
11844 	/* We cannot support GSO if the MSS is going to be less than
11845 	 * 64 bytes.  If it is then we need to drop support for GSO.
11846 	 */
11847 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11848 		features &= ~NETIF_F_GSO_MASK;
11849 
11850 	/* MACLEN can support at most 63 words */
11851 	len = skb_network_header(skb) - skb->data;
11852 	if (len & ~(63 * 2))
11853 		goto out_err;
11854 
11855 	/* IPLEN and EIPLEN can support at most 127 dwords */
11856 	len = skb_transport_header(skb) - skb_network_header(skb);
11857 	if (len & ~(127 * 4))
11858 		goto out_err;
11859 
11860 	if (skb->encapsulation) {
11861 		/* L4TUNLEN can support 127 words */
11862 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
11863 		if (len & ~(127 * 2))
11864 			goto out_err;
11865 
11866 		/* IPLEN can support at most 127 dwords */
11867 		len = skb_inner_transport_header(skb) -
11868 		      skb_inner_network_header(skb);
11869 		if (len & ~(127 * 4))
11870 			goto out_err;
11871 	}
11872 
11873 	/* No need to validate L4LEN as TCP is the only protocol with a
11874 	 * a flexible value and we support all possible values supported
11875 	 * by TCP, which is at most 15 dwords
11876 	 */
11877 
11878 	return features;
11879 out_err:
11880 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11881 }
11882 
11883 /**
11884  * i40e_xdp_setup - add/remove an XDP program
11885  * @vsi: VSI to changed
11886  * @prog: XDP program
11887  **/
11888 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11889 			  struct bpf_prog *prog)
11890 {
11891 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11892 	struct i40e_pf *pf = vsi->back;
11893 	struct bpf_prog *old_prog;
11894 	bool need_reset;
11895 	int i;
11896 
11897 	/* Don't allow frames that span over multiple buffers */
11898 	if (frame_size > vsi->rx_buf_len)
11899 		return -EINVAL;
11900 
11901 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11902 		return 0;
11903 
11904 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
11905 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11906 
11907 	if (need_reset)
11908 		i40e_prep_for_reset(pf, true);
11909 
11910 	old_prog = xchg(&vsi->xdp_prog, prog);
11911 
11912 	if (need_reset)
11913 		i40e_reset_and_rebuild(pf, true, true);
11914 
11915 	for (i = 0; i < vsi->num_queue_pairs; i++)
11916 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11917 
11918 	if (old_prog)
11919 		bpf_prog_put(old_prog);
11920 
11921 	/* Kick start the NAPI context if there is an AF_XDP socket open
11922 	 * on that queue id. This so that receiving will start.
11923 	 */
11924 	if (need_reset && prog)
11925 		for (i = 0; i < vsi->num_queue_pairs; i++)
11926 			if (vsi->xdp_rings[i]->xsk_umem)
11927 				(void)i40e_xsk_async_xmit(vsi->netdev, i);
11928 
11929 	return 0;
11930 }
11931 
11932 /**
11933  * i40e_enter_busy_conf - Enters busy config state
11934  * @vsi: vsi
11935  *
11936  * Returns 0 on success, <0 for failure.
11937  **/
11938 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
11939 {
11940 	struct i40e_pf *pf = vsi->back;
11941 	int timeout = 50;
11942 
11943 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
11944 		timeout--;
11945 		if (!timeout)
11946 			return -EBUSY;
11947 		usleep_range(1000, 2000);
11948 	}
11949 
11950 	return 0;
11951 }
11952 
11953 /**
11954  * i40e_exit_busy_conf - Exits busy config state
11955  * @vsi: vsi
11956  **/
11957 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
11958 {
11959 	struct i40e_pf *pf = vsi->back;
11960 
11961 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
11962 }
11963 
11964 /**
11965  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
11966  * @vsi: vsi
11967  * @queue_pair: queue pair
11968  **/
11969 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
11970 {
11971 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
11972 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
11973 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
11974 	       sizeof(vsi->tx_rings[queue_pair]->stats));
11975 	if (i40e_enabled_xdp_vsi(vsi)) {
11976 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
11977 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
11978 	}
11979 }
11980 
11981 /**
11982  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
11983  * @vsi: vsi
11984  * @queue_pair: queue pair
11985  **/
11986 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
11987 {
11988 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
11989 	if (i40e_enabled_xdp_vsi(vsi)) {
11990 		/* Make sure that in-progress ndo_xdp_xmit calls are
11991 		 * completed.
11992 		 */
11993 		synchronize_rcu();
11994 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
11995 	}
11996 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
11997 }
11998 
11999 /**
12000  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12001  * @vsi: vsi
12002  * @queue_pair: queue pair
12003  * @enable: true for enable, false for disable
12004  **/
12005 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12006 					bool enable)
12007 {
12008 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12009 	struct i40e_q_vector *q_vector = rxr->q_vector;
12010 
12011 	if (!vsi->netdev)
12012 		return;
12013 
12014 	/* All rings in a qp belong to the same qvector. */
12015 	if (q_vector->rx.ring || q_vector->tx.ring) {
12016 		if (enable)
12017 			napi_enable(&q_vector->napi);
12018 		else
12019 			napi_disable(&q_vector->napi);
12020 	}
12021 }
12022 
12023 /**
12024  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12025  * @vsi: vsi
12026  * @queue_pair: queue pair
12027  * @enable: true for enable, false for disable
12028  *
12029  * Returns 0 on success, <0 on failure.
12030  **/
12031 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12032 					bool enable)
12033 {
12034 	struct i40e_pf *pf = vsi->back;
12035 	int pf_q, ret = 0;
12036 
12037 	pf_q = vsi->base_queue + queue_pair;
12038 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12039 				     false /*is xdp*/, enable);
12040 	if (ret) {
12041 		dev_info(&pf->pdev->dev,
12042 			 "VSI seid %d Tx ring %d %sable timeout\n",
12043 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12044 		return ret;
12045 	}
12046 
12047 	i40e_control_rx_q(pf, pf_q, enable);
12048 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12049 	if (ret) {
12050 		dev_info(&pf->pdev->dev,
12051 			 "VSI seid %d Rx ring %d %sable timeout\n",
12052 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12053 		return ret;
12054 	}
12055 
12056 	/* Due to HW errata, on Rx disable only, the register can
12057 	 * indicate done before it really is. Needs 50ms to be sure
12058 	 */
12059 	if (!enable)
12060 		mdelay(50);
12061 
12062 	if (!i40e_enabled_xdp_vsi(vsi))
12063 		return ret;
12064 
12065 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12066 				     pf_q + vsi->alloc_queue_pairs,
12067 				     true /*is xdp*/, enable);
12068 	if (ret) {
12069 		dev_info(&pf->pdev->dev,
12070 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12071 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12072 	}
12073 
12074 	return ret;
12075 }
12076 
12077 /**
12078  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12079  * @vsi: vsi
12080  * @queue_pair: queue_pair
12081  **/
12082 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12083 {
12084 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12085 	struct i40e_pf *pf = vsi->back;
12086 	struct i40e_hw *hw = &pf->hw;
12087 
12088 	/* All rings in a qp belong to the same qvector. */
12089 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12090 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12091 	else
12092 		i40e_irq_dynamic_enable_icr0(pf);
12093 
12094 	i40e_flush(hw);
12095 }
12096 
12097 /**
12098  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12099  * @vsi: vsi
12100  * @queue_pair: queue_pair
12101  **/
12102 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12103 {
12104 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12105 	struct i40e_pf *pf = vsi->back;
12106 	struct i40e_hw *hw = &pf->hw;
12107 
12108 	/* For simplicity, instead of removing the qp interrupt causes
12109 	 * from the interrupt linked list, we simply disable the interrupt, and
12110 	 * leave the list intact.
12111 	 *
12112 	 * All rings in a qp belong to the same qvector.
12113 	 */
12114 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12115 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12116 
12117 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12118 		i40e_flush(hw);
12119 		synchronize_irq(pf->msix_entries[intpf].vector);
12120 	} else {
12121 		/* Legacy and MSI mode - this stops all interrupt handling */
12122 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12123 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12124 		i40e_flush(hw);
12125 		synchronize_irq(pf->pdev->irq);
12126 	}
12127 }
12128 
12129 /**
12130  * i40e_queue_pair_disable - Disables a queue pair
12131  * @vsi: vsi
12132  * @queue_pair: queue pair
12133  *
12134  * Returns 0 on success, <0 on failure.
12135  **/
12136 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12137 {
12138 	int err;
12139 
12140 	err = i40e_enter_busy_conf(vsi);
12141 	if (err)
12142 		return err;
12143 
12144 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12145 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12146 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12147 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12148 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12149 
12150 	return err;
12151 }
12152 
12153 /**
12154  * i40e_queue_pair_enable - Enables a queue pair
12155  * @vsi: vsi
12156  * @queue_pair: queue pair
12157  *
12158  * Returns 0 on success, <0 on failure.
12159  **/
12160 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12161 {
12162 	int err;
12163 
12164 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12165 	if (err)
12166 		return err;
12167 
12168 	if (i40e_enabled_xdp_vsi(vsi)) {
12169 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12170 		if (err)
12171 			return err;
12172 	}
12173 
12174 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12175 	if (err)
12176 		return err;
12177 
12178 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12179 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12180 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12181 
12182 	i40e_exit_busy_conf(vsi);
12183 
12184 	return err;
12185 }
12186 
12187 /**
12188  * i40e_xdp - implements ndo_bpf for i40e
12189  * @dev: netdevice
12190  * @xdp: XDP command
12191  **/
12192 static int i40e_xdp(struct net_device *dev,
12193 		    struct netdev_bpf *xdp)
12194 {
12195 	struct i40e_netdev_priv *np = netdev_priv(dev);
12196 	struct i40e_vsi *vsi = np->vsi;
12197 
12198 	if (vsi->type != I40E_VSI_MAIN)
12199 		return -EINVAL;
12200 
12201 	switch (xdp->command) {
12202 	case XDP_SETUP_PROG:
12203 		return i40e_xdp_setup(vsi, xdp->prog);
12204 	case XDP_QUERY_PROG:
12205 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
12206 		return 0;
12207 	case XDP_SETUP_XSK_UMEM:
12208 		return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
12209 					   xdp->xsk.queue_id);
12210 	default:
12211 		return -EINVAL;
12212 	}
12213 }
12214 
12215 static const struct net_device_ops i40e_netdev_ops = {
12216 	.ndo_open		= i40e_open,
12217 	.ndo_stop		= i40e_close,
12218 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12219 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12220 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12221 	.ndo_validate_addr	= eth_validate_addr,
12222 	.ndo_set_mac_address	= i40e_set_mac,
12223 	.ndo_change_mtu		= i40e_change_mtu,
12224 	.ndo_do_ioctl		= i40e_ioctl,
12225 	.ndo_tx_timeout		= i40e_tx_timeout,
12226 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12227 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12228 #ifdef CONFIG_NET_POLL_CONTROLLER
12229 	.ndo_poll_controller	= i40e_netpoll,
12230 #endif
12231 	.ndo_setup_tc		= __i40e_setup_tc,
12232 	.ndo_set_features	= i40e_set_features,
12233 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12234 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12235 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12236 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12237 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12238 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12239 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12240 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
12241 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
12242 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12243 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12244 	.ndo_features_check	= i40e_features_check,
12245 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12246 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12247 	.ndo_bpf		= i40e_xdp,
12248 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12249 	.ndo_xsk_async_xmit	= i40e_xsk_async_xmit,
12250 };
12251 
12252 /**
12253  * i40e_config_netdev - Setup the netdev flags
12254  * @vsi: the VSI being configured
12255  *
12256  * Returns 0 on success, negative value on failure
12257  **/
12258 static int i40e_config_netdev(struct i40e_vsi *vsi)
12259 {
12260 	struct i40e_pf *pf = vsi->back;
12261 	struct i40e_hw *hw = &pf->hw;
12262 	struct i40e_netdev_priv *np;
12263 	struct net_device *netdev;
12264 	u8 broadcast[ETH_ALEN];
12265 	u8 mac_addr[ETH_ALEN];
12266 	int etherdev_size;
12267 	netdev_features_t hw_enc_features;
12268 	netdev_features_t hw_features;
12269 
12270 	etherdev_size = sizeof(struct i40e_netdev_priv);
12271 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12272 	if (!netdev)
12273 		return -ENOMEM;
12274 
12275 	vsi->netdev = netdev;
12276 	np = netdev_priv(netdev);
12277 	np->vsi = vsi;
12278 
12279 	hw_enc_features = NETIF_F_SG			|
12280 			  NETIF_F_IP_CSUM		|
12281 			  NETIF_F_IPV6_CSUM		|
12282 			  NETIF_F_HIGHDMA		|
12283 			  NETIF_F_SOFT_FEATURES		|
12284 			  NETIF_F_TSO			|
12285 			  NETIF_F_TSO_ECN		|
12286 			  NETIF_F_TSO6			|
12287 			  NETIF_F_GSO_GRE		|
12288 			  NETIF_F_GSO_GRE_CSUM		|
12289 			  NETIF_F_GSO_PARTIAL		|
12290 			  NETIF_F_GSO_IPXIP4		|
12291 			  NETIF_F_GSO_IPXIP6		|
12292 			  NETIF_F_GSO_UDP_TUNNEL	|
12293 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12294 			  NETIF_F_SCTP_CRC		|
12295 			  NETIF_F_RXHASH		|
12296 			  NETIF_F_RXCSUM		|
12297 			  0;
12298 
12299 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12300 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12301 
12302 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12303 
12304 	netdev->hw_enc_features |= hw_enc_features;
12305 
12306 	/* record features VLANs can make use of */
12307 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12308 
12309 	hw_features = hw_enc_features		|
12310 		      NETIF_F_HW_VLAN_CTAG_TX	|
12311 		      NETIF_F_HW_VLAN_CTAG_RX;
12312 
12313 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12314 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12315 
12316 	netdev->hw_features |= hw_features;
12317 
12318 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12319 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12320 
12321 	if (vsi->type == I40E_VSI_MAIN) {
12322 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12323 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12324 		/* The following steps are necessary for two reasons. First,
12325 		 * some older NVM configurations load a default MAC-VLAN
12326 		 * filter that will accept any tagged packet, and we want to
12327 		 * replace this with a normal filter. Additionally, it is
12328 		 * possible our MAC address was provided by the platform using
12329 		 * Open Firmware or similar.
12330 		 *
12331 		 * Thus, we need to remove the default filter and install one
12332 		 * specific to the MAC address.
12333 		 */
12334 		i40e_rm_default_mac_filter(vsi, mac_addr);
12335 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12336 		i40e_add_mac_filter(vsi, mac_addr);
12337 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12338 	} else {
12339 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12340 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12341 		 * the end, which is 4 bytes long, so force truncation of the
12342 		 * original name by IFNAMSIZ - 4
12343 		 */
12344 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12345 			 IFNAMSIZ - 4,
12346 			 pf->vsi[pf->lan_vsi]->netdev->name);
12347 		eth_random_addr(mac_addr);
12348 
12349 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12350 		i40e_add_mac_filter(vsi, mac_addr);
12351 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12352 	}
12353 
12354 	/* Add the broadcast filter so that we initially will receive
12355 	 * broadcast packets. Note that when a new VLAN is first added the
12356 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12357 	 * specific filters as part of transitioning into "vlan" operation.
12358 	 * When more VLANs are added, the driver will copy each existing MAC
12359 	 * filter and add it for the new VLAN.
12360 	 *
12361 	 * Broadcast filters are handled specially by
12362 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
12363 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
12364 	 * filter. The subtask will update the correct broadcast promiscuous
12365 	 * bits as VLANs become active or inactive.
12366 	 */
12367 	eth_broadcast_addr(broadcast);
12368 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12369 	i40e_add_mac_filter(vsi, broadcast);
12370 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12371 
12372 	ether_addr_copy(netdev->dev_addr, mac_addr);
12373 	ether_addr_copy(netdev->perm_addr, mac_addr);
12374 
12375 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
12376 	netdev->neigh_priv_len = sizeof(u32) * 4;
12377 
12378 	netdev->priv_flags |= IFF_UNICAST_FLT;
12379 	netdev->priv_flags |= IFF_SUPP_NOFCS;
12380 	/* Setup netdev TC information */
12381 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12382 
12383 	netdev->netdev_ops = &i40e_netdev_ops;
12384 	netdev->watchdog_timeo = 5 * HZ;
12385 	i40e_set_ethtool_ops(netdev);
12386 
12387 	/* MTU range: 68 - 9706 */
12388 	netdev->min_mtu = ETH_MIN_MTU;
12389 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12390 
12391 	return 0;
12392 }
12393 
12394 /**
12395  * i40e_vsi_delete - Delete a VSI from the switch
12396  * @vsi: the VSI being removed
12397  *
12398  * Returns 0 on success, negative value on failure
12399  **/
12400 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12401 {
12402 	/* remove default VSI is not allowed */
12403 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12404 		return;
12405 
12406 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
12407 }
12408 
12409 /**
12410  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
12411  * @vsi: the VSI being queried
12412  *
12413  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
12414  **/
12415 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
12416 {
12417 	struct i40e_veb *veb;
12418 	struct i40e_pf *pf = vsi->back;
12419 
12420 	/* Uplink is not a bridge so default to VEB */
12421 	if (vsi->veb_idx == I40E_NO_VEB)
12422 		return 1;
12423 
12424 	veb = pf->veb[vsi->veb_idx];
12425 	if (!veb) {
12426 		dev_info(&pf->pdev->dev,
12427 			 "There is no veb associated with the bridge\n");
12428 		return -ENOENT;
12429 	}
12430 
12431 	/* Uplink is a bridge in VEPA mode */
12432 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
12433 		return 0;
12434 	} else {
12435 		/* Uplink is a bridge in VEB mode */
12436 		return 1;
12437 	}
12438 
12439 	/* VEPA is now default bridge, so return 0 */
12440 	return 0;
12441 }
12442 
12443 /**
12444  * i40e_add_vsi - Add a VSI to the switch
12445  * @vsi: the VSI being configured
12446  *
12447  * This initializes a VSI context depending on the VSI type to be added and
12448  * passes it down to the add_vsi aq command.
12449  **/
12450 static int i40e_add_vsi(struct i40e_vsi *vsi)
12451 {
12452 	int ret = -ENODEV;
12453 	struct i40e_pf *pf = vsi->back;
12454 	struct i40e_hw *hw = &pf->hw;
12455 	struct i40e_vsi_context ctxt;
12456 	struct i40e_mac_filter *f;
12457 	struct hlist_node *h;
12458 	int bkt;
12459 
12460 	u8 enabled_tc = 0x1; /* TC0 enabled */
12461 	int f_count = 0;
12462 
12463 	memset(&ctxt, 0, sizeof(ctxt));
12464 	switch (vsi->type) {
12465 	case I40E_VSI_MAIN:
12466 		/* The PF's main VSI is already setup as part of the
12467 		 * device initialization, so we'll not bother with
12468 		 * the add_vsi call, but we will retrieve the current
12469 		 * VSI context.
12470 		 */
12471 		ctxt.seid = pf->main_vsi_seid;
12472 		ctxt.pf_num = pf->hw.pf_id;
12473 		ctxt.vf_num = 0;
12474 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
12475 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12476 		if (ret) {
12477 			dev_info(&pf->pdev->dev,
12478 				 "couldn't get PF vsi config, err %s aq_err %s\n",
12479 				 i40e_stat_str(&pf->hw, ret),
12480 				 i40e_aq_str(&pf->hw,
12481 					     pf->hw.aq.asq_last_status));
12482 			return -ENOENT;
12483 		}
12484 		vsi->info = ctxt.info;
12485 		vsi->info.valid_sections = 0;
12486 
12487 		vsi->seid = ctxt.seid;
12488 		vsi->id = ctxt.vsi_number;
12489 
12490 		enabled_tc = i40e_pf_get_tc_map(pf);
12491 
12492 		/* Source pruning is enabled by default, so the flag is
12493 		 * negative logic - if it's set, we need to fiddle with
12494 		 * the VSI to disable source pruning.
12495 		 */
12496 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
12497 			memset(&ctxt, 0, sizeof(ctxt));
12498 			ctxt.seid = pf->main_vsi_seid;
12499 			ctxt.pf_num = pf->hw.pf_id;
12500 			ctxt.vf_num = 0;
12501 			ctxt.info.valid_sections |=
12502 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12503 			ctxt.info.switch_id =
12504 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
12505 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12506 			if (ret) {
12507 				dev_info(&pf->pdev->dev,
12508 					 "update vsi failed, err %s aq_err %s\n",
12509 					 i40e_stat_str(&pf->hw, ret),
12510 					 i40e_aq_str(&pf->hw,
12511 						     pf->hw.aq.asq_last_status));
12512 				ret = -ENOENT;
12513 				goto err;
12514 			}
12515 		}
12516 
12517 		/* MFP mode setup queue map and update VSI */
12518 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
12519 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
12520 			memset(&ctxt, 0, sizeof(ctxt));
12521 			ctxt.seid = pf->main_vsi_seid;
12522 			ctxt.pf_num = pf->hw.pf_id;
12523 			ctxt.vf_num = 0;
12524 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
12525 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12526 			if (ret) {
12527 				dev_info(&pf->pdev->dev,
12528 					 "update vsi failed, err %s aq_err %s\n",
12529 					 i40e_stat_str(&pf->hw, ret),
12530 					 i40e_aq_str(&pf->hw,
12531 						    pf->hw.aq.asq_last_status));
12532 				ret = -ENOENT;
12533 				goto err;
12534 			}
12535 			/* update the local VSI info queue map */
12536 			i40e_vsi_update_queue_map(vsi, &ctxt);
12537 			vsi->info.valid_sections = 0;
12538 		} else {
12539 			/* Default/Main VSI is only enabled for TC0
12540 			 * reconfigure it to enable all TCs that are
12541 			 * available on the port in SFP mode.
12542 			 * For MFP case the iSCSI PF would use this
12543 			 * flow to enable LAN+iSCSI TC.
12544 			 */
12545 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
12546 			if (ret) {
12547 				/* Single TC condition is not fatal,
12548 				 * message and continue
12549 				 */
12550 				dev_info(&pf->pdev->dev,
12551 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12552 					 enabled_tc,
12553 					 i40e_stat_str(&pf->hw, ret),
12554 					 i40e_aq_str(&pf->hw,
12555 						    pf->hw.aq.asq_last_status));
12556 			}
12557 		}
12558 		break;
12559 
12560 	case I40E_VSI_FDIR:
12561 		ctxt.pf_num = hw->pf_id;
12562 		ctxt.vf_num = 0;
12563 		ctxt.uplink_seid = vsi->uplink_seid;
12564 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12565 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12566 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12567 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
12568 			ctxt.info.valid_sections |=
12569 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12570 			ctxt.info.switch_id =
12571 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12572 		}
12573 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12574 		break;
12575 
12576 	case I40E_VSI_VMDQ2:
12577 		ctxt.pf_num = hw->pf_id;
12578 		ctxt.vf_num = 0;
12579 		ctxt.uplink_seid = vsi->uplink_seid;
12580 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12581 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12582 
12583 		/* This VSI is connected to VEB so the switch_id
12584 		 * should be set to zero by default.
12585 		 */
12586 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12587 			ctxt.info.valid_sections |=
12588 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12589 			ctxt.info.switch_id =
12590 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12591 		}
12592 
12593 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12594 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12595 		break;
12596 
12597 	case I40E_VSI_SRIOV:
12598 		ctxt.pf_num = hw->pf_id;
12599 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12600 		ctxt.uplink_seid = vsi->uplink_seid;
12601 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12602 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12603 
12604 		/* This VSI is connected to VEB so the switch_id
12605 		 * should be set to zero by default.
12606 		 */
12607 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12608 			ctxt.info.valid_sections |=
12609 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12610 			ctxt.info.switch_id =
12611 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12612 		}
12613 
12614 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12615 			ctxt.info.valid_sections |=
12616 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12617 			ctxt.info.queueing_opt_flags |=
12618 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12619 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12620 		}
12621 
12622 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12623 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12624 		if (pf->vf[vsi->vf_id].spoofchk) {
12625 			ctxt.info.valid_sections |=
12626 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12627 			ctxt.info.sec_flags |=
12628 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12629 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12630 		}
12631 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12632 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12633 		break;
12634 
12635 	case I40E_VSI_IWARP:
12636 		/* send down message to iWARP */
12637 		break;
12638 
12639 	default:
12640 		return -ENODEV;
12641 	}
12642 
12643 	if (vsi->type != I40E_VSI_MAIN) {
12644 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12645 		if (ret) {
12646 			dev_info(&vsi->back->pdev->dev,
12647 				 "add vsi failed, err %s aq_err %s\n",
12648 				 i40e_stat_str(&pf->hw, ret),
12649 				 i40e_aq_str(&pf->hw,
12650 					     pf->hw.aq.asq_last_status));
12651 			ret = -ENOENT;
12652 			goto err;
12653 		}
12654 		vsi->info = ctxt.info;
12655 		vsi->info.valid_sections = 0;
12656 		vsi->seid = ctxt.seid;
12657 		vsi->id = ctxt.vsi_number;
12658 	}
12659 
12660 	vsi->active_filters = 0;
12661 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12662 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12663 	/* If macvlan filters already exist, force them to get loaded */
12664 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12665 		f->state = I40E_FILTER_NEW;
12666 		f_count++;
12667 	}
12668 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12669 
12670 	if (f_count) {
12671 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12672 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
12673 	}
12674 
12675 	/* Update VSI BW information */
12676 	ret = i40e_vsi_get_bw_info(vsi);
12677 	if (ret) {
12678 		dev_info(&pf->pdev->dev,
12679 			 "couldn't get vsi bw info, err %s aq_err %s\n",
12680 			 i40e_stat_str(&pf->hw, ret),
12681 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12682 		/* VSI is already added so not tearing that up */
12683 		ret = 0;
12684 	}
12685 
12686 err:
12687 	return ret;
12688 }
12689 
12690 /**
12691  * i40e_vsi_release - Delete a VSI and free its resources
12692  * @vsi: the VSI being removed
12693  *
12694  * Returns 0 on success or < 0 on error
12695  **/
12696 int i40e_vsi_release(struct i40e_vsi *vsi)
12697 {
12698 	struct i40e_mac_filter *f;
12699 	struct hlist_node *h;
12700 	struct i40e_veb *veb = NULL;
12701 	struct i40e_pf *pf;
12702 	u16 uplink_seid;
12703 	int i, n, bkt;
12704 
12705 	pf = vsi->back;
12706 
12707 	/* release of a VEB-owner or last VSI is not allowed */
12708 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12709 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12710 			 vsi->seid, vsi->uplink_seid);
12711 		return -ENODEV;
12712 	}
12713 	if (vsi == pf->vsi[pf->lan_vsi] &&
12714 	    !test_bit(__I40E_DOWN, pf->state)) {
12715 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12716 		return -ENODEV;
12717 	}
12718 
12719 	uplink_seid = vsi->uplink_seid;
12720 	if (vsi->type != I40E_VSI_SRIOV) {
12721 		if (vsi->netdev_registered) {
12722 			vsi->netdev_registered = false;
12723 			if (vsi->netdev) {
12724 				/* results in a call to i40e_close() */
12725 				unregister_netdev(vsi->netdev);
12726 			}
12727 		} else {
12728 			i40e_vsi_close(vsi);
12729 		}
12730 		i40e_vsi_disable_irq(vsi);
12731 	}
12732 
12733 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12734 
12735 	/* clear the sync flag on all filters */
12736 	if (vsi->netdev) {
12737 		__dev_uc_unsync(vsi->netdev, NULL);
12738 		__dev_mc_unsync(vsi->netdev, NULL);
12739 	}
12740 
12741 	/* make sure any remaining filters are marked for deletion */
12742 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12743 		__i40e_del_filter(vsi, f);
12744 
12745 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12746 
12747 	i40e_sync_vsi_filters(vsi);
12748 
12749 	i40e_vsi_delete(vsi);
12750 	i40e_vsi_free_q_vectors(vsi);
12751 	if (vsi->netdev) {
12752 		free_netdev(vsi->netdev);
12753 		vsi->netdev = NULL;
12754 	}
12755 	i40e_vsi_clear_rings(vsi);
12756 	i40e_vsi_clear(vsi);
12757 
12758 	/* If this was the last thing on the VEB, except for the
12759 	 * controlling VSI, remove the VEB, which puts the controlling
12760 	 * VSI onto the next level down in the switch.
12761 	 *
12762 	 * Well, okay, there's one more exception here: don't remove
12763 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
12764 	 * from up the network stack.
12765 	 */
12766 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12767 		if (pf->vsi[i] &&
12768 		    pf->vsi[i]->uplink_seid == uplink_seid &&
12769 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12770 			n++;      /* count the VSIs */
12771 		}
12772 	}
12773 	for (i = 0; i < I40E_MAX_VEB; i++) {
12774 		if (!pf->veb[i])
12775 			continue;
12776 		if (pf->veb[i]->uplink_seid == uplink_seid)
12777 			n++;     /* count the VEBs */
12778 		if (pf->veb[i]->seid == uplink_seid)
12779 			veb = pf->veb[i];
12780 	}
12781 	if (n == 0 && veb && veb->uplink_seid != 0)
12782 		i40e_veb_release(veb);
12783 
12784 	return 0;
12785 }
12786 
12787 /**
12788  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12789  * @vsi: ptr to the VSI
12790  *
12791  * This should only be called after i40e_vsi_mem_alloc() which allocates the
12792  * corresponding SW VSI structure and initializes num_queue_pairs for the
12793  * newly allocated VSI.
12794  *
12795  * Returns 0 on success or negative on failure
12796  **/
12797 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12798 {
12799 	int ret = -ENOENT;
12800 	struct i40e_pf *pf = vsi->back;
12801 
12802 	if (vsi->q_vectors[0]) {
12803 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12804 			 vsi->seid);
12805 		return -EEXIST;
12806 	}
12807 
12808 	if (vsi->base_vector) {
12809 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12810 			 vsi->seid, vsi->base_vector);
12811 		return -EEXIST;
12812 	}
12813 
12814 	ret = i40e_vsi_alloc_q_vectors(vsi);
12815 	if (ret) {
12816 		dev_info(&pf->pdev->dev,
12817 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12818 			 vsi->num_q_vectors, vsi->seid, ret);
12819 		vsi->num_q_vectors = 0;
12820 		goto vector_setup_out;
12821 	}
12822 
12823 	/* In Legacy mode, we do not have to get any other vector since we
12824 	 * piggyback on the misc/ICR0 for queue interrupts.
12825 	*/
12826 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12827 		return ret;
12828 	if (vsi->num_q_vectors)
12829 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12830 						 vsi->num_q_vectors, vsi->idx);
12831 	if (vsi->base_vector < 0) {
12832 		dev_info(&pf->pdev->dev,
12833 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12834 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12835 		i40e_vsi_free_q_vectors(vsi);
12836 		ret = -ENOENT;
12837 		goto vector_setup_out;
12838 	}
12839 
12840 vector_setup_out:
12841 	return ret;
12842 }
12843 
12844 /**
12845  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12846  * @vsi: pointer to the vsi.
12847  *
12848  * This re-allocates a vsi's queue resources.
12849  *
12850  * Returns pointer to the successfully allocated and configured VSI sw struct
12851  * on success, otherwise returns NULL on failure.
12852  **/
12853 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12854 {
12855 	u16 alloc_queue_pairs;
12856 	struct i40e_pf *pf;
12857 	u8 enabled_tc;
12858 	int ret;
12859 
12860 	if (!vsi)
12861 		return NULL;
12862 
12863 	pf = vsi->back;
12864 
12865 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12866 	i40e_vsi_clear_rings(vsi);
12867 
12868 	i40e_vsi_free_arrays(vsi, false);
12869 	i40e_set_num_rings_in_vsi(vsi);
12870 	ret = i40e_vsi_alloc_arrays(vsi, false);
12871 	if (ret)
12872 		goto err_vsi;
12873 
12874 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12875 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12876 
12877 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12878 	if (ret < 0) {
12879 		dev_info(&pf->pdev->dev,
12880 			 "failed to get tracking for %d queues for VSI %d err %d\n",
12881 			 alloc_queue_pairs, vsi->seid, ret);
12882 		goto err_vsi;
12883 	}
12884 	vsi->base_queue = ret;
12885 
12886 	/* Update the FW view of the VSI. Force a reset of TC and queue
12887 	 * layout configurations.
12888 	 */
12889 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12890 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12891 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12892 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12893 	if (vsi->type == I40E_VSI_MAIN)
12894 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12895 
12896 	/* assign it some queues */
12897 	ret = i40e_alloc_rings(vsi);
12898 	if (ret)
12899 		goto err_rings;
12900 
12901 	/* map all of the rings to the q_vectors */
12902 	i40e_vsi_map_rings_to_vectors(vsi);
12903 	return vsi;
12904 
12905 err_rings:
12906 	i40e_vsi_free_q_vectors(vsi);
12907 	if (vsi->netdev_registered) {
12908 		vsi->netdev_registered = false;
12909 		unregister_netdev(vsi->netdev);
12910 		free_netdev(vsi->netdev);
12911 		vsi->netdev = NULL;
12912 	}
12913 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12914 err_vsi:
12915 	i40e_vsi_clear(vsi);
12916 	return NULL;
12917 }
12918 
12919 /**
12920  * i40e_vsi_setup - Set up a VSI by a given type
12921  * @pf: board private structure
12922  * @type: VSI type
12923  * @uplink_seid: the switch element to link to
12924  * @param1: usage depends upon VSI type. For VF types, indicates VF id
12925  *
12926  * This allocates the sw VSI structure and its queue resources, then add a VSI
12927  * to the identified VEB.
12928  *
12929  * Returns pointer to the successfully allocated and configure VSI sw struct on
12930  * success, otherwise returns NULL on failure.
12931  **/
12932 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12933 				u16 uplink_seid, u32 param1)
12934 {
12935 	struct i40e_vsi *vsi = NULL;
12936 	struct i40e_veb *veb = NULL;
12937 	u16 alloc_queue_pairs;
12938 	int ret, i;
12939 	int v_idx;
12940 
12941 	/* The requested uplink_seid must be either
12942 	 *     - the PF's port seid
12943 	 *              no VEB is needed because this is the PF
12944 	 *              or this is a Flow Director special case VSI
12945 	 *     - seid of an existing VEB
12946 	 *     - seid of a VSI that owns an existing VEB
12947 	 *     - seid of a VSI that doesn't own a VEB
12948 	 *              a new VEB is created and the VSI becomes the owner
12949 	 *     - seid of the PF VSI, which is what creates the first VEB
12950 	 *              this is a special case of the previous
12951 	 *
12952 	 * Find which uplink_seid we were given and create a new VEB if needed
12953 	 */
12954 	for (i = 0; i < I40E_MAX_VEB; i++) {
12955 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12956 			veb = pf->veb[i];
12957 			break;
12958 		}
12959 	}
12960 
12961 	if (!veb && uplink_seid != pf->mac_seid) {
12962 
12963 		for (i = 0; i < pf->num_alloc_vsi; i++) {
12964 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12965 				vsi = pf->vsi[i];
12966 				break;
12967 			}
12968 		}
12969 		if (!vsi) {
12970 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12971 				 uplink_seid);
12972 			return NULL;
12973 		}
12974 
12975 		if (vsi->uplink_seid == pf->mac_seid)
12976 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12977 					     vsi->tc_config.enabled_tc);
12978 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12979 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12980 					     vsi->tc_config.enabled_tc);
12981 		if (veb) {
12982 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12983 				dev_info(&vsi->back->pdev->dev,
12984 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
12985 				return NULL;
12986 			}
12987 			/* We come up by default in VEPA mode if SRIOV is not
12988 			 * already enabled, in which case we can't force VEPA
12989 			 * mode.
12990 			 */
12991 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12992 				veb->bridge_mode = BRIDGE_MODE_VEPA;
12993 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12994 			}
12995 			i40e_config_bridge_mode(veb);
12996 		}
12997 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12998 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12999 				veb = pf->veb[i];
13000 		}
13001 		if (!veb) {
13002 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13003 			return NULL;
13004 		}
13005 
13006 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13007 		uplink_seid = veb->seid;
13008 	}
13009 
13010 	/* get vsi sw struct */
13011 	v_idx = i40e_vsi_mem_alloc(pf, type);
13012 	if (v_idx < 0)
13013 		goto err_alloc;
13014 	vsi = pf->vsi[v_idx];
13015 	if (!vsi)
13016 		goto err_alloc;
13017 	vsi->type = type;
13018 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13019 
13020 	if (type == I40E_VSI_MAIN)
13021 		pf->lan_vsi = v_idx;
13022 	else if (type == I40E_VSI_SRIOV)
13023 		vsi->vf_id = param1;
13024 	/* assign it some queues */
13025 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13026 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13027 
13028 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13029 	if (ret < 0) {
13030 		dev_info(&pf->pdev->dev,
13031 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13032 			 alloc_queue_pairs, vsi->seid, ret);
13033 		goto err_vsi;
13034 	}
13035 	vsi->base_queue = ret;
13036 
13037 	/* get a VSI from the hardware */
13038 	vsi->uplink_seid = uplink_seid;
13039 	ret = i40e_add_vsi(vsi);
13040 	if (ret)
13041 		goto err_vsi;
13042 
13043 	switch (vsi->type) {
13044 	/* setup the netdev if needed */
13045 	case I40E_VSI_MAIN:
13046 	case I40E_VSI_VMDQ2:
13047 		ret = i40e_config_netdev(vsi);
13048 		if (ret)
13049 			goto err_netdev;
13050 		ret = register_netdev(vsi->netdev);
13051 		if (ret)
13052 			goto err_netdev;
13053 		vsi->netdev_registered = true;
13054 		netif_carrier_off(vsi->netdev);
13055 #ifdef CONFIG_I40E_DCB
13056 		/* Setup DCB netlink interface */
13057 		i40e_dcbnl_setup(vsi);
13058 #endif /* CONFIG_I40E_DCB */
13059 		/* fall through */
13060 
13061 	case I40E_VSI_FDIR:
13062 		/* set up vectors and rings if needed */
13063 		ret = i40e_vsi_setup_vectors(vsi);
13064 		if (ret)
13065 			goto err_msix;
13066 
13067 		ret = i40e_alloc_rings(vsi);
13068 		if (ret)
13069 			goto err_rings;
13070 
13071 		/* map all of the rings to the q_vectors */
13072 		i40e_vsi_map_rings_to_vectors(vsi);
13073 
13074 		i40e_vsi_reset_stats(vsi);
13075 		break;
13076 
13077 	default:
13078 		/* no netdev or rings for the other VSI types */
13079 		break;
13080 	}
13081 
13082 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13083 	    (vsi->type == I40E_VSI_VMDQ2)) {
13084 		ret = i40e_vsi_config_rss(vsi);
13085 	}
13086 	return vsi;
13087 
13088 err_rings:
13089 	i40e_vsi_free_q_vectors(vsi);
13090 err_msix:
13091 	if (vsi->netdev_registered) {
13092 		vsi->netdev_registered = false;
13093 		unregister_netdev(vsi->netdev);
13094 		free_netdev(vsi->netdev);
13095 		vsi->netdev = NULL;
13096 	}
13097 err_netdev:
13098 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13099 err_vsi:
13100 	i40e_vsi_clear(vsi);
13101 err_alloc:
13102 	return NULL;
13103 }
13104 
13105 /**
13106  * i40e_veb_get_bw_info - Query VEB BW information
13107  * @veb: the veb to query
13108  *
13109  * Query the Tx scheduler BW configuration data for given VEB
13110  **/
13111 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13112 {
13113 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13114 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13115 	struct i40e_pf *pf = veb->pf;
13116 	struct i40e_hw *hw = &pf->hw;
13117 	u32 tc_bw_max;
13118 	int ret = 0;
13119 	int i;
13120 
13121 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13122 						  &bw_data, NULL);
13123 	if (ret) {
13124 		dev_info(&pf->pdev->dev,
13125 			 "query veb bw config failed, err %s aq_err %s\n",
13126 			 i40e_stat_str(&pf->hw, ret),
13127 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13128 		goto out;
13129 	}
13130 
13131 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13132 						   &ets_data, NULL);
13133 	if (ret) {
13134 		dev_info(&pf->pdev->dev,
13135 			 "query veb bw ets config failed, err %s aq_err %s\n",
13136 			 i40e_stat_str(&pf->hw, ret),
13137 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13138 		goto out;
13139 	}
13140 
13141 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13142 	veb->bw_max_quanta = ets_data.tc_bw_max;
13143 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13144 	veb->enabled_tc = ets_data.tc_valid_bits;
13145 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13146 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13147 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13148 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13149 		veb->bw_tc_limit_credits[i] =
13150 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13151 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13152 	}
13153 
13154 out:
13155 	return ret;
13156 }
13157 
13158 /**
13159  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13160  * @pf: board private structure
13161  *
13162  * On error: returns error code (negative)
13163  * On success: returns vsi index in PF (positive)
13164  **/
13165 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13166 {
13167 	int ret = -ENOENT;
13168 	struct i40e_veb *veb;
13169 	int i;
13170 
13171 	/* Need to protect the allocation of switch elements at the PF level */
13172 	mutex_lock(&pf->switch_mutex);
13173 
13174 	/* VEB list may be fragmented if VEB creation/destruction has
13175 	 * been happening.  We can afford to do a quick scan to look
13176 	 * for any free slots in the list.
13177 	 *
13178 	 * find next empty veb slot, looping back around if necessary
13179 	 */
13180 	i = 0;
13181 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13182 		i++;
13183 	if (i >= I40E_MAX_VEB) {
13184 		ret = -ENOMEM;
13185 		goto err_alloc_veb;  /* out of VEB slots! */
13186 	}
13187 
13188 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13189 	if (!veb) {
13190 		ret = -ENOMEM;
13191 		goto err_alloc_veb;
13192 	}
13193 	veb->pf = pf;
13194 	veb->idx = i;
13195 	veb->enabled_tc = 1;
13196 
13197 	pf->veb[i] = veb;
13198 	ret = i;
13199 err_alloc_veb:
13200 	mutex_unlock(&pf->switch_mutex);
13201 	return ret;
13202 }
13203 
13204 /**
13205  * i40e_switch_branch_release - Delete a branch of the switch tree
13206  * @branch: where to start deleting
13207  *
13208  * This uses recursion to find the tips of the branch to be
13209  * removed, deleting until we get back to and can delete this VEB.
13210  **/
13211 static void i40e_switch_branch_release(struct i40e_veb *branch)
13212 {
13213 	struct i40e_pf *pf = branch->pf;
13214 	u16 branch_seid = branch->seid;
13215 	u16 veb_idx = branch->idx;
13216 	int i;
13217 
13218 	/* release any VEBs on this VEB - RECURSION */
13219 	for (i = 0; i < I40E_MAX_VEB; i++) {
13220 		if (!pf->veb[i])
13221 			continue;
13222 		if (pf->veb[i]->uplink_seid == branch->seid)
13223 			i40e_switch_branch_release(pf->veb[i]);
13224 	}
13225 
13226 	/* Release the VSIs on this VEB, but not the owner VSI.
13227 	 *
13228 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13229 	 *       the VEB itself, so don't use (*branch) after this loop.
13230 	 */
13231 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13232 		if (!pf->vsi[i])
13233 			continue;
13234 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13235 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13236 			i40e_vsi_release(pf->vsi[i]);
13237 		}
13238 	}
13239 
13240 	/* There's one corner case where the VEB might not have been
13241 	 * removed, so double check it here and remove it if needed.
13242 	 * This case happens if the veb was created from the debugfs
13243 	 * commands and no VSIs were added to it.
13244 	 */
13245 	if (pf->veb[veb_idx])
13246 		i40e_veb_release(pf->veb[veb_idx]);
13247 }
13248 
13249 /**
13250  * i40e_veb_clear - remove veb struct
13251  * @veb: the veb to remove
13252  **/
13253 static void i40e_veb_clear(struct i40e_veb *veb)
13254 {
13255 	if (!veb)
13256 		return;
13257 
13258 	if (veb->pf) {
13259 		struct i40e_pf *pf = veb->pf;
13260 
13261 		mutex_lock(&pf->switch_mutex);
13262 		if (pf->veb[veb->idx] == veb)
13263 			pf->veb[veb->idx] = NULL;
13264 		mutex_unlock(&pf->switch_mutex);
13265 	}
13266 
13267 	kfree(veb);
13268 }
13269 
13270 /**
13271  * i40e_veb_release - Delete a VEB and free its resources
13272  * @veb: the VEB being removed
13273  **/
13274 void i40e_veb_release(struct i40e_veb *veb)
13275 {
13276 	struct i40e_vsi *vsi = NULL;
13277 	struct i40e_pf *pf;
13278 	int i, n = 0;
13279 
13280 	pf = veb->pf;
13281 
13282 	/* find the remaining VSI and check for extras */
13283 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13284 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13285 			n++;
13286 			vsi = pf->vsi[i];
13287 		}
13288 	}
13289 	if (n != 1) {
13290 		dev_info(&pf->pdev->dev,
13291 			 "can't remove VEB %d with %d VSIs left\n",
13292 			 veb->seid, n);
13293 		return;
13294 	}
13295 
13296 	/* move the remaining VSI to uplink veb */
13297 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13298 	if (veb->uplink_seid) {
13299 		vsi->uplink_seid = veb->uplink_seid;
13300 		if (veb->uplink_seid == pf->mac_seid)
13301 			vsi->veb_idx = I40E_NO_VEB;
13302 		else
13303 			vsi->veb_idx = veb->veb_idx;
13304 	} else {
13305 		/* floating VEB */
13306 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13307 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13308 	}
13309 
13310 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13311 	i40e_veb_clear(veb);
13312 }
13313 
13314 /**
13315  * i40e_add_veb - create the VEB in the switch
13316  * @veb: the VEB to be instantiated
13317  * @vsi: the controlling VSI
13318  **/
13319 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13320 {
13321 	struct i40e_pf *pf = veb->pf;
13322 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13323 	int ret;
13324 
13325 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13326 			      veb->enabled_tc, false,
13327 			      &veb->seid, enable_stats, NULL);
13328 
13329 	/* get a VEB from the hardware */
13330 	if (ret) {
13331 		dev_info(&pf->pdev->dev,
13332 			 "couldn't add VEB, err %s aq_err %s\n",
13333 			 i40e_stat_str(&pf->hw, ret),
13334 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13335 		return -EPERM;
13336 	}
13337 
13338 	/* get statistics counter */
13339 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13340 					 &veb->stats_idx, NULL, NULL, NULL);
13341 	if (ret) {
13342 		dev_info(&pf->pdev->dev,
13343 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13344 			 i40e_stat_str(&pf->hw, ret),
13345 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13346 		return -EPERM;
13347 	}
13348 	ret = i40e_veb_get_bw_info(veb);
13349 	if (ret) {
13350 		dev_info(&pf->pdev->dev,
13351 			 "couldn't get VEB bw info, err %s aq_err %s\n",
13352 			 i40e_stat_str(&pf->hw, ret),
13353 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13354 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13355 		return -ENOENT;
13356 	}
13357 
13358 	vsi->uplink_seid = veb->seid;
13359 	vsi->veb_idx = veb->idx;
13360 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13361 
13362 	return 0;
13363 }
13364 
13365 /**
13366  * i40e_veb_setup - Set up a VEB
13367  * @pf: board private structure
13368  * @flags: VEB setup flags
13369  * @uplink_seid: the switch element to link to
13370  * @vsi_seid: the initial VSI seid
13371  * @enabled_tc: Enabled TC bit-map
13372  *
13373  * This allocates the sw VEB structure and links it into the switch
13374  * It is possible and legal for this to be a duplicate of an already
13375  * existing VEB.  It is also possible for both uplink and vsi seids
13376  * to be zero, in order to create a floating VEB.
13377  *
13378  * Returns pointer to the successfully allocated VEB sw struct on
13379  * success, otherwise returns NULL on failure.
13380  **/
13381 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13382 				u16 uplink_seid, u16 vsi_seid,
13383 				u8 enabled_tc)
13384 {
13385 	struct i40e_veb *veb, *uplink_veb = NULL;
13386 	int vsi_idx, veb_idx;
13387 	int ret;
13388 
13389 	/* if one seid is 0, the other must be 0 to create a floating relay */
13390 	if ((uplink_seid == 0 || vsi_seid == 0) &&
13391 	    (uplink_seid + vsi_seid != 0)) {
13392 		dev_info(&pf->pdev->dev,
13393 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13394 			 uplink_seid, vsi_seid);
13395 		return NULL;
13396 	}
13397 
13398 	/* make sure there is such a vsi and uplink */
13399 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13400 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13401 			break;
13402 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
13403 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13404 			 vsi_seid);
13405 		return NULL;
13406 	}
13407 
13408 	if (uplink_seid && uplink_seid != pf->mac_seid) {
13409 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
13410 			if (pf->veb[veb_idx] &&
13411 			    pf->veb[veb_idx]->seid == uplink_seid) {
13412 				uplink_veb = pf->veb[veb_idx];
13413 				break;
13414 			}
13415 		}
13416 		if (!uplink_veb) {
13417 			dev_info(&pf->pdev->dev,
13418 				 "uplink seid %d not found\n", uplink_seid);
13419 			return NULL;
13420 		}
13421 	}
13422 
13423 	/* get veb sw struct */
13424 	veb_idx = i40e_veb_mem_alloc(pf);
13425 	if (veb_idx < 0)
13426 		goto err_alloc;
13427 	veb = pf->veb[veb_idx];
13428 	veb->flags = flags;
13429 	veb->uplink_seid = uplink_seid;
13430 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
13431 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
13432 
13433 	/* create the VEB in the switch */
13434 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
13435 	if (ret)
13436 		goto err_veb;
13437 	if (vsi_idx == pf->lan_vsi)
13438 		pf->lan_veb = veb->idx;
13439 
13440 	return veb;
13441 
13442 err_veb:
13443 	i40e_veb_clear(veb);
13444 err_alloc:
13445 	return NULL;
13446 }
13447 
13448 /**
13449  * i40e_setup_pf_switch_element - set PF vars based on switch type
13450  * @pf: board private structure
13451  * @ele: element we are building info from
13452  * @num_reported: total number of elements
13453  * @printconfig: should we print the contents
13454  *
13455  * helper function to assist in extracting a few useful SEID values.
13456  **/
13457 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
13458 				struct i40e_aqc_switch_config_element_resp *ele,
13459 				u16 num_reported, bool printconfig)
13460 {
13461 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
13462 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
13463 	u8 element_type = ele->element_type;
13464 	u16 seid = le16_to_cpu(ele->seid);
13465 
13466 	if (printconfig)
13467 		dev_info(&pf->pdev->dev,
13468 			 "type=%d seid=%d uplink=%d downlink=%d\n",
13469 			 element_type, seid, uplink_seid, downlink_seid);
13470 
13471 	switch (element_type) {
13472 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
13473 		pf->mac_seid = seid;
13474 		break;
13475 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
13476 		/* Main VEB? */
13477 		if (uplink_seid != pf->mac_seid)
13478 			break;
13479 		if (pf->lan_veb == I40E_NO_VEB) {
13480 			int v;
13481 
13482 			/* find existing or else empty VEB */
13483 			for (v = 0; v < I40E_MAX_VEB; v++) {
13484 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
13485 					pf->lan_veb = v;
13486 					break;
13487 				}
13488 			}
13489 			if (pf->lan_veb == I40E_NO_VEB) {
13490 				v = i40e_veb_mem_alloc(pf);
13491 				if (v < 0)
13492 					break;
13493 				pf->lan_veb = v;
13494 			}
13495 		}
13496 
13497 		pf->veb[pf->lan_veb]->seid = seid;
13498 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
13499 		pf->veb[pf->lan_veb]->pf = pf;
13500 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
13501 		break;
13502 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
13503 		if (num_reported != 1)
13504 			break;
13505 		/* This is immediately after a reset so we can assume this is
13506 		 * the PF's VSI
13507 		 */
13508 		pf->mac_seid = uplink_seid;
13509 		pf->pf_seid = downlink_seid;
13510 		pf->main_vsi_seid = seid;
13511 		if (printconfig)
13512 			dev_info(&pf->pdev->dev,
13513 				 "pf_seid=%d main_vsi_seid=%d\n",
13514 				 pf->pf_seid, pf->main_vsi_seid);
13515 		break;
13516 	case I40E_SWITCH_ELEMENT_TYPE_PF:
13517 	case I40E_SWITCH_ELEMENT_TYPE_VF:
13518 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
13519 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
13520 	case I40E_SWITCH_ELEMENT_TYPE_PE:
13521 	case I40E_SWITCH_ELEMENT_TYPE_PA:
13522 		/* ignore these for now */
13523 		break;
13524 	default:
13525 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
13526 			 element_type, seid);
13527 		break;
13528 	}
13529 }
13530 
13531 /**
13532  * i40e_fetch_switch_configuration - Get switch config from firmware
13533  * @pf: board private structure
13534  * @printconfig: should we print the contents
13535  *
13536  * Get the current switch configuration from the device and
13537  * extract a few useful SEID values.
13538  **/
13539 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
13540 {
13541 	struct i40e_aqc_get_switch_config_resp *sw_config;
13542 	u16 next_seid = 0;
13543 	int ret = 0;
13544 	u8 *aq_buf;
13545 	int i;
13546 
13547 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13548 	if (!aq_buf)
13549 		return -ENOMEM;
13550 
13551 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13552 	do {
13553 		u16 num_reported, num_total;
13554 
13555 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13556 						I40E_AQ_LARGE_BUF,
13557 						&next_seid, NULL);
13558 		if (ret) {
13559 			dev_info(&pf->pdev->dev,
13560 				 "get switch config failed err %s aq_err %s\n",
13561 				 i40e_stat_str(&pf->hw, ret),
13562 				 i40e_aq_str(&pf->hw,
13563 					     pf->hw.aq.asq_last_status));
13564 			kfree(aq_buf);
13565 			return -ENOENT;
13566 		}
13567 
13568 		num_reported = le16_to_cpu(sw_config->header.num_reported);
13569 		num_total = le16_to_cpu(sw_config->header.num_total);
13570 
13571 		if (printconfig)
13572 			dev_info(&pf->pdev->dev,
13573 				 "header: %d reported %d total\n",
13574 				 num_reported, num_total);
13575 
13576 		for (i = 0; i < num_reported; i++) {
13577 			struct i40e_aqc_switch_config_element_resp *ele =
13578 				&sw_config->element[i];
13579 
13580 			i40e_setup_pf_switch_element(pf, ele, num_reported,
13581 						     printconfig);
13582 		}
13583 	} while (next_seid != 0);
13584 
13585 	kfree(aq_buf);
13586 	return ret;
13587 }
13588 
13589 /**
13590  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13591  * @pf: board private structure
13592  * @reinit: if the Main VSI needs to re-initialized.
13593  *
13594  * Returns 0 on success, negative value on failure
13595  **/
13596 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13597 {
13598 	u16 flags = 0;
13599 	int ret;
13600 
13601 	/* find out what's out there already */
13602 	ret = i40e_fetch_switch_configuration(pf, false);
13603 	if (ret) {
13604 		dev_info(&pf->pdev->dev,
13605 			 "couldn't fetch switch config, err %s aq_err %s\n",
13606 			 i40e_stat_str(&pf->hw, ret),
13607 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13608 		return ret;
13609 	}
13610 	i40e_pf_reset_stats(pf);
13611 
13612 	/* set the switch config bit for the whole device to
13613 	 * support limited promisc or true promisc
13614 	 * when user requests promisc. The default is limited
13615 	 * promisc.
13616 	*/
13617 
13618 	if ((pf->hw.pf_id == 0) &&
13619 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13620 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13621 		pf->last_sw_conf_flags = flags;
13622 	}
13623 
13624 	if (pf->hw.pf_id == 0) {
13625 		u16 valid_flags;
13626 
13627 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13628 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13629 						NULL);
13630 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13631 			dev_info(&pf->pdev->dev,
13632 				 "couldn't set switch config bits, err %s aq_err %s\n",
13633 				 i40e_stat_str(&pf->hw, ret),
13634 				 i40e_aq_str(&pf->hw,
13635 					     pf->hw.aq.asq_last_status));
13636 			/* not a fatal problem, just keep going */
13637 		}
13638 		pf->last_sw_conf_valid_flags = valid_flags;
13639 	}
13640 
13641 	/* first time setup */
13642 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13643 		struct i40e_vsi *vsi = NULL;
13644 		u16 uplink_seid;
13645 
13646 		/* Set up the PF VSI associated with the PF's main VSI
13647 		 * that is already in the HW switch
13648 		 */
13649 		if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13650 			uplink_seid = pf->veb[pf->lan_veb]->seid;
13651 		else
13652 			uplink_seid = pf->mac_seid;
13653 		if (pf->lan_vsi == I40E_NO_VSI)
13654 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13655 		else if (reinit)
13656 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13657 		if (!vsi) {
13658 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13659 			i40e_cloud_filter_exit(pf);
13660 			i40e_fdir_teardown(pf);
13661 			return -EAGAIN;
13662 		}
13663 	} else {
13664 		/* force a reset of TC and queue layout configurations */
13665 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13666 
13667 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13668 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13669 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13670 	}
13671 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13672 
13673 	i40e_fdir_sb_setup(pf);
13674 
13675 	/* Setup static PF queue filter control settings */
13676 	ret = i40e_setup_pf_filter_control(pf);
13677 	if (ret) {
13678 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13679 			 ret);
13680 		/* Failure here should not stop continuing other steps */
13681 	}
13682 
13683 	/* enable RSS in the HW, even for only one queue, as the stack can use
13684 	 * the hash
13685 	 */
13686 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13687 		i40e_pf_config_rss(pf);
13688 
13689 	/* fill in link information and enable LSE reporting */
13690 	i40e_link_event(pf);
13691 
13692 	/* Initialize user-specific link properties */
13693 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13694 				  I40E_AQ_AN_COMPLETED) ? true : false);
13695 
13696 	i40e_ptp_init(pf);
13697 
13698 	/* repopulate tunnel port filters */
13699 	i40e_sync_udp_filters(pf);
13700 
13701 	return ret;
13702 }
13703 
13704 /**
13705  * i40e_determine_queue_usage - Work out queue distribution
13706  * @pf: board private structure
13707  **/
13708 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13709 {
13710 	int queues_left;
13711 	int q_max;
13712 
13713 	pf->num_lan_qps = 0;
13714 
13715 	/* Find the max queues to be put into basic use.  We'll always be
13716 	 * using TC0, whether or not DCB is running, and TC0 will get the
13717 	 * big RSS set.
13718 	 */
13719 	queues_left = pf->hw.func_caps.num_tx_qp;
13720 
13721 	if ((queues_left == 1) ||
13722 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13723 		/* one qp for PF, no queues for anything else */
13724 		queues_left = 0;
13725 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13726 
13727 		/* make sure all the fancies are disabled */
13728 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13729 			       I40E_FLAG_IWARP_ENABLED	|
13730 			       I40E_FLAG_FD_SB_ENABLED	|
13731 			       I40E_FLAG_FD_ATR_ENABLED	|
13732 			       I40E_FLAG_DCB_CAPABLE	|
13733 			       I40E_FLAG_DCB_ENABLED	|
13734 			       I40E_FLAG_SRIOV_ENABLED	|
13735 			       I40E_FLAG_VMDQ_ENABLED);
13736 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13737 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13738 				  I40E_FLAG_FD_SB_ENABLED |
13739 				  I40E_FLAG_FD_ATR_ENABLED |
13740 				  I40E_FLAG_DCB_CAPABLE))) {
13741 		/* one qp for PF */
13742 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13743 		queues_left -= pf->num_lan_qps;
13744 
13745 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13746 			       I40E_FLAG_IWARP_ENABLED	|
13747 			       I40E_FLAG_FD_SB_ENABLED	|
13748 			       I40E_FLAG_FD_ATR_ENABLED	|
13749 			       I40E_FLAG_DCB_ENABLED	|
13750 			       I40E_FLAG_VMDQ_ENABLED);
13751 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13752 	} else {
13753 		/* Not enough queues for all TCs */
13754 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13755 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13756 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13757 					I40E_FLAG_DCB_ENABLED);
13758 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13759 		}
13760 
13761 		/* limit lan qps to the smaller of qps, cpus or msix */
13762 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13763 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13764 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13765 		pf->num_lan_qps = q_max;
13766 
13767 		queues_left -= pf->num_lan_qps;
13768 	}
13769 
13770 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13771 		if (queues_left > 1) {
13772 			queues_left -= 1; /* save 1 queue for FD */
13773 		} else {
13774 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13775 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13776 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13777 		}
13778 	}
13779 
13780 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13781 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13782 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13783 					(queues_left / pf->num_vf_qps));
13784 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13785 	}
13786 
13787 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13788 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13789 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13790 					  (queues_left / pf->num_vmdq_qps));
13791 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13792 	}
13793 
13794 	pf->queues_left = queues_left;
13795 	dev_dbg(&pf->pdev->dev,
13796 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13797 		pf->hw.func_caps.num_tx_qp,
13798 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13799 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13800 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13801 		queues_left);
13802 }
13803 
13804 /**
13805  * i40e_setup_pf_filter_control - Setup PF static filter control
13806  * @pf: PF to be setup
13807  *
13808  * i40e_setup_pf_filter_control sets up a PF's initial filter control
13809  * settings. If PE/FCoE are enabled then it will also set the per PF
13810  * based filter sizes required for them. It also enables Flow director,
13811  * ethertype and macvlan type filter settings for the pf.
13812  *
13813  * Returns 0 on success, negative on failure
13814  **/
13815 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13816 {
13817 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
13818 
13819 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13820 
13821 	/* Flow Director is enabled */
13822 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13823 		settings->enable_fdir = true;
13824 
13825 	/* Ethtype and MACVLAN filters enabled for PF */
13826 	settings->enable_ethtype = true;
13827 	settings->enable_macvlan = true;
13828 
13829 	if (i40e_set_filter_control(&pf->hw, settings))
13830 		return -ENOENT;
13831 
13832 	return 0;
13833 }
13834 
13835 #define INFO_STRING_LEN 255
13836 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13837 static void i40e_print_features(struct i40e_pf *pf)
13838 {
13839 	struct i40e_hw *hw = &pf->hw;
13840 	char *buf;
13841 	int i;
13842 
13843 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13844 	if (!buf)
13845 		return;
13846 
13847 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13848 #ifdef CONFIG_PCI_IOV
13849 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13850 #endif
13851 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13852 		      pf->hw.func_caps.num_vsis,
13853 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
13854 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
13855 		i += snprintf(&buf[i], REMAIN(i), " RSS");
13856 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13857 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13858 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13859 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13860 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13861 	}
13862 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13863 		i += snprintf(&buf[i], REMAIN(i), " DCB");
13864 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13865 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
13866 	if (pf->flags & I40E_FLAG_PTP)
13867 		i += snprintf(&buf[i], REMAIN(i), " PTP");
13868 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13869 		i += snprintf(&buf[i], REMAIN(i), " VEB");
13870 	else
13871 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
13872 
13873 	dev_info(&pf->pdev->dev, "%s\n", buf);
13874 	kfree(buf);
13875 	WARN_ON(i > INFO_STRING_LEN);
13876 }
13877 
13878 /**
13879  * i40e_get_platform_mac_addr - get platform-specific MAC address
13880  * @pdev: PCI device information struct
13881  * @pf: board private structure
13882  *
13883  * Look up the MAC address for the device. First we'll try
13884  * eth_platform_get_mac_address, which will check Open Firmware, or arch
13885  * specific fallback. Otherwise, we'll default to the stored value in
13886  * firmware.
13887  **/
13888 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13889 {
13890 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13891 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13892 }
13893 
13894 /**
13895  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
13896  * @fec_cfg: FEC option to set in flags
13897  * @flags: ptr to flags in which we set FEC option
13898  **/
13899 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
13900 {
13901 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
13902 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
13903 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
13904 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
13905 		*flags |= I40E_FLAG_RS_FEC;
13906 		*flags &= ~I40E_FLAG_BASE_R_FEC;
13907 	}
13908 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
13909 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
13910 		*flags |= I40E_FLAG_BASE_R_FEC;
13911 		*flags &= ~I40E_FLAG_RS_FEC;
13912 	}
13913 	if (fec_cfg == 0)
13914 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
13915 }
13916 
13917 /**
13918  * i40e_probe - Device initialization routine
13919  * @pdev: PCI device information struct
13920  * @ent: entry in i40e_pci_tbl
13921  *
13922  * i40e_probe initializes a PF identified by a pci_dev structure.
13923  * The OS initialization, configuring of the PF private structure,
13924  * and a hardware reset occur.
13925  *
13926  * Returns 0 on success, negative on failure
13927  **/
13928 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13929 {
13930 	struct i40e_aq_get_phy_abilities_resp abilities;
13931 	struct i40e_pf *pf;
13932 	struct i40e_hw *hw;
13933 	static u16 pfs_found;
13934 	u16 wol_nvm_bits;
13935 	u16 link_status;
13936 	int err;
13937 	u32 val;
13938 	u32 i;
13939 	u8 set_fc_aq_fail;
13940 
13941 	err = pci_enable_device_mem(pdev);
13942 	if (err)
13943 		return err;
13944 
13945 	/* set up for high or low dma */
13946 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13947 	if (err) {
13948 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13949 		if (err) {
13950 			dev_err(&pdev->dev,
13951 				"DMA configuration failed: 0x%x\n", err);
13952 			goto err_dma;
13953 		}
13954 	}
13955 
13956 	/* set up pci connections */
13957 	err = pci_request_mem_regions(pdev, i40e_driver_name);
13958 	if (err) {
13959 		dev_info(&pdev->dev,
13960 			 "pci_request_selected_regions failed %d\n", err);
13961 		goto err_pci_reg;
13962 	}
13963 
13964 	pci_enable_pcie_error_reporting(pdev);
13965 	pci_set_master(pdev);
13966 
13967 	/* Now that we have a PCI connection, we need to do the
13968 	 * low level device setup.  This is primarily setting up
13969 	 * the Admin Queue structures and then querying for the
13970 	 * device's current profile information.
13971 	 */
13972 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13973 	if (!pf) {
13974 		err = -ENOMEM;
13975 		goto err_pf_alloc;
13976 	}
13977 	pf->next_vsi = 0;
13978 	pf->pdev = pdev;
13979 	set_bit(__I40E_DOWN, pf->state);
13980 
13981 	hw = &pf->hw;
13982 	hw->back = pf;
13983 
13984 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13985 				I40E_MAX_CSR_SPACE);
13986 
13987 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
13988 	if (!hw->hw_addr) {
13989 		err = -EIO;
13990 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13991 			 (unsigned int)pci_resource_start(pdev, 0),
13992 			 pf->ioremap_len, err);
13993 		goto err_ioremap;
13994 	}
13995 	hw->vendor_id = pdev->vendor;
13996 	hw->device_id = pdev->device;
13997 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13998 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
13999 	hw->subsystem_device_id = pdev->subsystem_device;
14000 	hw->bus.device = PCI_SLOT(pdev->devfn);
14001 	hw->bus.func = PCI_FUNC(pdev->devfn);
14002 	hw->bus.bus_id = pdev->bus->number;
14003 	pf->instance = pfs_found;
14004 
14005 	/* Select something other than the 802.1ad ethertype for the
14006 	 * switch to use internally and drop on ingress.
14007 	 */
14008 	hw->switch_tag = 0xffff;
14009 	hw->first_tag = ETH_P_8021AD;
14010 	hw->second_tag = ETH_P_8021Q;
14011 
14012 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14013 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14014 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14015 
14016 	/* set up the locks for the AQ, do this only once in probe
14017 	 * and destroy them only once in remove
14018 	 */
14019 	mutex_init(&hw->aq.asq_mutex);
14020 	mutex_init(&hw->aq.arq_mutex);
14021 
14022 	pf->msg_enable = netif_msg_init(debug,
14023 					NETIF_MSG_DRV |
14024 					NETIF_MSG_PROBE |
14025 					NETIF_MSG_LINK);
14026 	if (debug < -1)
14027 		pf->hw.debug_mask = debug;
14028 
14029 	/* do a special CORER for clearing PXE mode once at init */
14030 	if (hw->revision_id == 0 &&
14031 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14032 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14033 		i40e_flush(hw);
14034 		msleep(200);
14035 		pf->corer_count++;
14036 
14037 		i40e_clear_pxe_mode(hw);
14038 	}
14039 
14040 	/* Reset here to make sure all is clean and to define PF 'n' */
14041 	i40e_clear_hw(hw);
14042 	err = i40e_pf_reset(hw);
14043 	if (err) {
14044 		dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
14045 		goto err_pf_reset;
14046 	}
14047 	pf->pfr_count++;
14048 
14049 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14050 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14051 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14052 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14053 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14054 
14055 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14056 		 "%s-%s:misc",
14057 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14058 
14059 	err = i40e_init_shared_code(hw);
14060 	if (err) {
14061 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14062 			 err);
14063 		goto err_pf_reset;
14064 	}
14065 
14066 	/* set up a default setting for link flow control */
14067 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14068 
14069 	err = i40e_init_adminq(hw);
14070 	if (err) {
14071 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14072 			dev_info(&pdev->dev,
14073 				 "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",
14074 				 hw->aq.api_maj_ver,
14075 				 hw->aq.api_min_ver,
14076 				 I40E_FW_API_VERSION_MAJOR,
14077 				 I40E_FW_MINOR_VERSION(hw));
14078 		else
14079 			dev_info(&pdev->dev,
14080 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14081 
14082 		goto err_pf_reset;
14083 	}
14084 	i40e_get_oem_version(hw);
14085 
14086 	/* provide nvm, fw, api versions */
14087 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
14088 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14089 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14090 		 i40e_nvm_version_str(hw));
14091 
14092 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14093 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14094 		dev_info(&pdev->dev,
14095 			 "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",
14096 			 hw->aq.api_maj_ver,
14097 			 hw->aq.api_min_ver,
14098 			 I40E_FW_API_VERSION_MAJOR,
14099 			 I40E_FW_MINOR_VERSION(hw));
14100 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14101 		dev_info(&pdev->dev,
14102 			 "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",
14103 			 hw->aq.api_maj_ver,
14104 			 hw->aq.api_min_ver,
14105 			 I40E_FW_API_VERSION_MAJOR,
14106 			 I40E_FW_MINOR_VERSION(hw));
14107 
14108 	i40e_verify_eeprom(pf);
14109 
14110 	/* Rev 0 hardware was never productized */
14111 	if (hw->revision_id < 1)
14112 		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");
14113 
14114 	i40e_clear_pxe_mode(hw);
14115 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14116 	if (err)
14117 		goto err_adminq_setup;
14118 
14119 	err = i40e_sw_init(pf);
14120 	if (err) {
14121 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14122 		goto err_sw_init;
14123 	}
14124 
14125 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14126 				hw->func_caps.num_rx_qp, 0, 0);
14127 	if (err) {
14128 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14129 		goto err_init_lan_hmc;
14130 	}
14131 
14132 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14133 	if (err) {
14134 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14135 		err = -ENOENT;
14136 		goto err_configure_lan_hmc;
14137 	}
14138 
14139 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14140 	 * Ignore error return codes because if it was already disabled via
14141 	 * hardware settings this will fail
14142 	 */
14143 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14144 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14145 		i40e_aq_stop_lldp(hw, true, NULL);
14146 	}
14147 
14148 	/* allow a platform config to override the HW addr */
14149 	i40e_get_platform_mac_addr(pdev, pf);
14150 
14151 	if (!is_valid_ether_addr(hw->mac.addr)) {
14152 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14153 		err = -EIO;
14154 		goto err_mac_addr;
14155 	}
14156 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14157 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14158 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14159 	if (is_valid_ether_addr(hw->mac.port_addr))
14160 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14161 
14162 	pci_set_drvdata(pdev, pf);
14163 	pci_save_state(pdev);
14164 
14165 	/* Enable FW to write default DCB config on link-up */
14166 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14167 
14168 #ifdef CONFIG_I40E_DCB
14169 	err = i40e_init_pf_dcb(pf);
14170 	if (err) {
14171 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14172 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14173 		/* Continue without DCB enabled */
14174 	}
14175 #endif /* CONFIG_I40E_DCB */
14176 
14177 	/* set up periodic task facility */
14178 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14179 	pf->service_timer_period = HZ;
14180 
14181 	INIT_WORK(&pf->service_task, i40e_service_task);
14182 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14183 
14184 	/* NVM bit on means WoL disabled for the port */
14185 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14186 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14187 		pf->wol_en = false;
14188 	else
14189 		pf->wol_en = true;
14190 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14191 
14192 	/* set up the main switch operations */
14193 	i40e_determine_queue_usage(pf);
14194 	err = i40e_init_interrupt_scheme(pf);
14195 	if (err)
14196 		goto err_switch_setup;
14197 
14198 	/* The number of VSIs reported by the FW is the minimum guaranteed
14199 	 * to us; HW supports far more and we share the remaining pool with
14200 	 * the other PFs. We allocate space for more than the guarantee with
14201 	 * the understanding that we might not get them all later.
14202 	 */
14203 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14204 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14205 	else
14206 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14207 
14208 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
14209 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14210 			  GFP_KERNEL);
14211 	if (!pf->vsi) {
14212 		err = -ENOMEM;
14213 		goto err_switch_setup;
14214 	}
14215 
14216 #ifdef CONFIG_PCI_IOV
14217 	/* prep for VF support */
14218 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14219 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
14220 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
14221 		if (pci_num_vf(pdev))
14222 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
14223 	}
14224 #endif
14225 	err = i40e_setup_pf_switch(pf, false);
14226 	if (err) {
14227 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
14228 		goto err_vsis;
14229 	}
14230 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
14231 
14232 	/* Make sure flow control is set according to current settings */
14233 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
14234 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
14235 		dev_dbg(&pf->pdev->dev,
14236 			"Set fc with err %s aq_err %s on get_phy_cap\n",
14237 			i40e_stat_str(hw, err),
14238 			i40e_aq_str(hw, hw->aq.asq_last_status));
14239 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
14240 		dev_dbg(&pf->pdev->dev,
14241 			"Set fc with err %s aq_err %s on set_phy_config\n",
14242 			i40e_stat_str(hw, err),
14243 			i40e_aq_str(hw, hw->aq.asq_last_status));
14244 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
14245 		dev_dbg(&pf->pdev->dev,
14246 			"Set fc with err %s aq_err %s on get_link_info\n",
14247 			i40e_stat_str(hw, err),
14248 			i40e_aq_str(hw, hw->aq.asq_last_status));
14249 
14250 	/* if FDIR VSI was set up, start it now */
14251 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14252 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
14253 			i40e_vsi_open(pf->vsi[i]);
14254 			break;
14255 		}
14256 	}
14257 
14258 	/* The driver only wants link up/down and module qualification
14259 	 * reports from firmware.  Note the negative logic.
14260 	 */
14261 	err = i40e_aq_set_phy_int_mask(&pf->hw,
14262 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
14263 					 I40E_AQ_EVENT_MEDIA_NA |
14264 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
14265 	if (err)
14266 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
14267 			 i40e_stat_str(&pf->hw, err),
14268 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14269 
14270 	/* Reconfigure hardware for allowing smaller MSS in the case
14271 	 * of TSO, so that we avoid the MDD being fired and causing
14272 	 * a reset in the case of small MSS+TSO.
14273 	 */
14274 	val = rd32(hw, I40E_REG_MSS);
14275 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
14276 		val &= ~I40E_REG_MSS_MIN_MASK;
14277 		val |= I40E_64BYTE_MSS;
14278 		wr32(hw, I40E_REG_MSS, val);
14279 	}
14280 
14281 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
14282 		msleep(75);
14283 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
14284 		if (err)
14285 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
14286 				 i40e_stat_str(&pf->hw, err),
14287 				 i40e_aq_str(&pf->hw,
14288 					     pf->hw.aq.asq_last_status));
14289 	}
14290 	/* The main driver is (mostly) up and happy. We need to set this state
14291 	 * before setting up the misc vector or we get a race and the vector
14292 	 * ends up disabled forever.
14293 	 */
14294 	clear_bit(__I40E_DOWN, pf->state);
14295 
14296 	/* In case of MSIX we are going to setup the misc vector right here
14297 	 * to handle admin queue events etc. In case of legacy and MSI
14298 	 * the misc functionality and queue processing is combined in
14299 	 * the same vector and that gets setup at open.
14300 	 */
14301 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
14302 		err = i40e_setup_misc_vector(pf);
14303 		if (err) {
14304 			dev_info(&pdev->dev,
14305 				 "setup of misc vector failed: %d\n", err);
14306 			goto err_vsis;
14307 		}
14308 	}
14309 
14310 #ifdef CONFIG_PCI_IOV
14311 	/* prep for VF support */
14312 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14313 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
14314 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
14315 		/* disable link interrupts for VFs */
14316 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
14317 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
14318 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
14319 		i40e_flush(hw);
14320 
14321 		if (pci_num_vf(pdev)) {
14322 			dev_info(&pdev->dev,
14323 				 "Active VFs found, allocating resources.\n");
14324 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
14325 			if (err)
14326 				dev_info(&pdev->dev,
14327 					 "Error %d allocating resources for existing VFs\n",
14328 					 err);
14329 		}
14330 	}
14331 #endif /* CONFIG_PCI_IOV */
14332 
14333 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14334 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
14335 						      pf->num_iwarp_msix,
14336 						      I40E_IWARP_IRQ_PILE_ID);
14337 		if (pf->iwarp_base_vector < 0) {
14338 			dev_info(&pdev->dev,
14339 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
14340 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
14341 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
14342 		}
14343 	}
14344 
14345 	i40e_dbg_pf_init(pf);
14346 
14347 	/* tell the firmware that we're starting */
14348 	i40e_send_version(pf);
14349 
14350 	/* since everything's happy, start the service_task timer */
14351 	mod_timer(&pf->service_timer,
14352 		  round_jiffies(jiffies + pf->service_timer_period));
14353 
14354 	/* add this PF to client device list and launch a client service task */
14355 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14356 		err = i40e_lan_add_device(pf);
14357 		if (err)
14358 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
14359 				 err);
14360 	}
14361 
14362 #define PCI_SPEED_SIZE 8
14363 #define PCI_WIDTH_SIZE 8
14364 	/* Devices on the IOSF bus do not have this information
14365 	 * and will report PCI Gen 1 x 1 by default so don't bother
14366 	 * checking them.
14367 	 */
14368 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
14369 		char speed[PCI_SPEED_SIZE] = "Unknown";
14370 		char width[PCI_WIDTH_SIZE] = "Unknown";
14371 
14372 		/* Get the negotiated link width and speed from PCI config
14373 		 * space
14374 		 */
14375 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
14376 					  &link_status);
14377 
14378 		i40e_set_pci_config_data(hw, link_status);
14379 
14380 		switch (hw->bus.speed) {
14381 		case i40e_bus_speed_8000:
14382 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
14383 		case i40e_bus_speed_5000:
14384 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
14385 		case i40e_bus_speed_2500:
14386 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
14387 		default:
14388 			break;
14389 		}
14390 		switch (hw->bus.width) {
14391 		case i40e_bus_width_pcie_x8:
14392 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
14393 		case i40e_bus_width_pcie_x4:
14394 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
14395 		case i40e_bus_width_pcie_x2:
14396 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
14397 		case i40e_bus_width_pcie_x1:
14398 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
14399 		default:
14400 			break;
14401 		}
14402 
14403 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
14404 			 speed, width);
14405 
14406 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
14407 		    hw->bus.speed < i40e_bus_speed_8000) {
14408 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
14409 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
14410 		}
14411 	}
14412 
14413 	/* get the requested speeds from the fw */
14414 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
14415 	if (err)
14416 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
14417 			i40e_stat_str(&pf->hw, err),
14418 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14419 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
14420 
14421 	/* set the FEC config due to the board capabilities */
14422 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
14423 
14424 	/* get the supported phy types from the fw */
14425 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
14426 	if (err)
14427 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
14428 			i40e_stat_str(&pf->hw, err),
14429 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14430 
14431 	/* Add a filter to drop all Flow control frames from any VSI from being
14432 	 * transmitted. By doing so we stop a malicious VF from sending out
14433 	 * PAUSE or PFC frames and potentially controlling traffic for other
14434 	 * PF/VF VSIs.
14435 	 * The FW can still send Flow control frames if enabled.
14436 	 */
14437 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
14438 						       pf->main_vsi_seid);
14439 
14440 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
14441 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
14442 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
14443 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
14444 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
14445 	/* print a string summarizing features */
14446 	i40e_print_features(pf);
14447 
14448 	return 0;
14449 
14450 	/* Unwind what we've done if something failed in the setup */
14451 err_vsis:
14452 	set_bit(__I40E_DOWN, pf->state);
14453 	i40e_clear_interrupt_scheme(pf);
14454 	kfree(pf->vsi);
14455 err_switch_setup:
14456 	i40e_reset_interrupt_capability(pf);
14457 	del_timer_sync(&pf->service_timer);
14458 err_mac_addr:
14459 err_configure_lan_hmc:
14460 	(void)i40e_shutdown_lan_hmc(hw);
14461 err_init_lan_hmc:
14462 	kfree(pf->qp_pile);
14463 err_sw_init:
14464 err_adminq_setup:
14465 err_pf_reset:
14466 	iounmap(hw->hw_addr);
14467 err_ioremap:
14468 	kfree(pf);
14469 err_pf_alloc:
14470 	pci_disable_pcie_error_reporting(pdev);
14471 	pci_release_mem_regions(pdev);
14472 err_pci_reg:
14473 err_dma:
14474 	pci_disable_device(pdev);
14475 	return err;
14476 }
14477 
14478 /**
14479  * i40e_remove - Device removal routine
14480  * @pdev: PCI device information struct
14481  *
14482  * i40e_remove is called by the PCI subsystem to alert the driver
14483  * that is should release a PCI device.  This could be caused by a
14484  * Hot-Plug event, or because the driver is going to be removed from
14485  * memory.
14486  **/
14487 static void i40e_remove(struct pci_dev *pdev)
14488 {
14489 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14490 	struct i40e_hw *hw = &pf->hw;
14491 	i40e_status ret_code;
14492 	int i;
14493 
14494 	i40e_dbg_pf_exit(pf);
14495 
14496 	i40e_ptp_stop(pf);
14497 
14498 	/* Disable RSS in hw */
14499 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
14500 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
14501 
14502 	/* no more scheduling of any task */
14503 	set_bit(__I40E_SUSPENDED, pf->state);
14504 	set_bit(__I40E_DOWN, pf->state);
14505 	if (pf->service_timer.function)
14506 		del_timer_sync(&pf->service_timer);
14507 	if (pf->service_task.func)
14508 		cancel_work_sync(&pf->service_task);
14509 
14510 	/* Client close must be called explicitly here because the timer
14511 	 * has been stopped.
14512 	 */
14513 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14514 
14515 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
14516 		i40e_free_vfs(pf);
14517 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
14518 	}
14519 
14520 	i40e_fdir_teardown(pf);
14521 
14522 	/* If there is a switch structure or any orphans, remove them.
14523 	 * This will leave only the PF's VSI remaining.
14524 	 */
14525 	for (i = 0; i < I40E_MAX_VEB; i++) {
14526 		if (!pf->veb[i])
14527 			continue;
14528 
14529 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
14530 		    pf->veb[i]->uplink_seid == 0)
14531 			i40e_switch_branch_release(pf->veb[i]);
14532 	}
14533 
14534 	/* Now we can shutdown the PF's VSI, just before we kill
14535 	 * adminq and hmc.
14536 	 */
14537 	if (pf->vsi[pf->lan_vsi])
14538 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
14539 
14540 	i40e_cloud_filter_exit(pf);
14541 
14542 	/* remove attached clients */
14543 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14544 		ret_code = i40e_lan_del_device(pf);
14545 		if (ret_code)
14546 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
14547 				 ret_code);
14548 	}
14549 
14550 	/* shutdown and destroy the HMC */
14551 	if (hw->hmc.hmc_obj) {
14552 		ret_code = i40e_shutdown_lan_hmc(hw);
14553 		if (ret_code)
14554 			dev_warn(&pdev->dev,
14555 				 "Failed to destroy the HMC resources: %d\n",
14556 				 ret_code);
14557 	}
14558 
14559 	/* shutdown the adminq */
14560 	i40e_shutdown_adminq(hw);
14561 
14562 	/* destroy the locks only once, here */
14563 	mutex_destroy(&hw->aq.arq_mutex);
14564 	mutex_destroy(&hw->aq.asq_mutex);
14565 
14566 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
14567 	rtnl_lock();
14568 	i40e_clear_interrupt_scheme(pf);
14569 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14570 		if (pf->vsi[i]) {
14571 			i40e_vsi_clear_rings(pf->vsi[i]);
14572 			i40e_vsi_clear(pf->vsi[i]);
14573 			pf->vsi[i] = NULL;
14574 		}
14575 	}
14576 	rtnl_unlock();
14577 
14578 	for (i = 0; i < I40E_MAX_VEB; i++) {
14579 		kfree(pf->veb[i]);
14580 		pf->veb[i] = NULL;
14581 	}
14582 
14583 	kfree(pf->qp_pile);
14584 	kfree(pf->vsi);
14585 
14586 	iounmap(hw->hw_addr);
14587 	kfree(pf);
14588 	pci_release_mem_regions(pdev);
14589 
14590 	pci_disable_pcie_error_reporting(pdev);
14591 	pci_disable_device(pdev);
14592 }
14593 
14594 /**
14595  * i40e_pci_error_detected - warning that something funky happened in PCI land
14596  * @pdev: PCI device information struct
14597  * @error: the type of PCI error
14598  *
14599  * Called to warn that something happened and the error handling steps
14600  * are in progress.  Allows the driver to quiesce things, be ready for
14601  * remediation.
14602  **/
14603 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14604 						enum pci_channel_state error)
14605 {
14606 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14607 
14608 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14609 
14610 	if (!pf) {
14611 		dev_info(&pdev->dev,
14612 			 "Cannot recover - error happened during device probe\n");
14613 		return PCI_ERS_RESULT_DISCONNECT;
14614 	}
14615 
14616 	/* shutdown all operations */
14617 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14618 		i40e_prep_for_reset(pf, false);
14619 
14620 	/* Request a slot reset */
14621 	return PCI_ERS_RESULT_NEED_RESET;
14622 }
14623 
14624 /**
14625  * i40e_pci_error_slot_reset - a PCI slot reset just happened
14626  * @pdev: PCI device information struct
14627  *
14628  * Called to find if the driver can work with the device now that
14629  * the pci slot has been reset.  If a basic connection seems good
14630  * (registers are readable and have sane content) then return a
14631  * happy little PCI_ERS_RESULT_xxx.
14632  **/
14633 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14634 {
14635 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14636 	pci_ers_result_t result;
14637 	u32 reg;
14638 
14639 	dev_dbg(&pdev->dev, "%s\n", __func__);
14640 	if (pci_enable_device_mem(pdev)) {
14641 		dev_info(&pdev->dev,
14642 			 "Cannot re-enable PCI device after reset.\n");
14643 		result = PCI_ERS_RESULT_DISCONNECT;
14644 	} else {
14645 		pci_set_master(pdev);
14646 		pci_restore_state(pdev);
14647 		pci_save_state(pdev);
14648 		pci_wake_from_d3(pdev, false);
14649 
14650 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14651 		if (reg == 0)
14652 			result = PCI_ERS_RESULT_RECOVERED;
14653 		else
14654 			result = PCI_ERS_RESULT_DISCONNECT;
14655 	}
14656 
14657 	return result;
14658 }
14659 
14660 /**
14661  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14662  * @pdev: PCI device information struct
14663  */
14664 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14665 {
14666 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14667 
14668 	i40e_prep_for_reset(pf, false);
14669 }
14670 
14671 /**
14672  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14673  * @pdev: PCI device information struct
14674  */
14675 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14676 {
14677 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14678 
14679 	i40e_reset_and_rebuild(pf, false, false);
14680 }
14681 
14682 /**
14683  * i40e_pci_error_resume - restart operations after PCI error recovery
14684  * @pdev: PCI device information struct
14685  *
14686  * Called to allow the driver to bring things back up after PCI error
14687  * and/or reset recovery has finished.
14688  **/
14689 static void i40e_pci_error_resume(struct pci_dev *pdev)
14690 {
14691 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14692 
14693 	dev_dbg(&pdev->dev, "%s\n", __func__);
14694 	if (test_bit(__I40E_SUSPENDED, pf->state))
14695 		return;
14696 
14697 	i40e_handle_reset_warning(pf, false);
14698 }
14699 
14700 /**
14701  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14702  * using the mac_address_write admin q function
14703  * @pf: pointer to i40e_pf struct
14704  **/
14705 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14706 {
14707 	struct i40e_hw *hw = &pf->hw;
14708 	i40e_status ret;
14709 	u8 mac_addr[6];
14710 	u16 flags = 0;
14711 
14712 	/* Get current MAC address in case it's an LAA */
14713 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14714 		ether_addr_copy(mac_addr,
14715 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14716 	} else {
14717 		dev_err(&pf->pdev->dev,
14718 			"Failed to retrieve MAC address; using default\n");
14719 		ether_addr_copy(mac_addr, hw->mac.addr);
14720 	}
14721 
14722 	/* The FW expects the mac address write cmd to first be called with
14723 	 * one of these flags before calling it again with the multicast
14724 	 * enable flags.
14725 	 */
14726 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14727 
14728 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14729 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14730 
14731 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14732 	if (ret) {
14733 		dev_err(&pf->pdev->dev,
14734 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14735 		return;
14736 	}
14737 
14738 	flags = I40E_AQC_MC_MAG_EN
14739 			| I40E_AQC_WOL_PRESERVE_ON_PFR
14740 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14741 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14742 	if (ret)
14743 		dev_err(&pf->pdev->dev,
14744 			"Failed to enable Multicast Magic Packet wake up\n");
14745 }
14746 
14747 /**
14748  * i40e_shutdown - PCI callback for shutting down
14749  * @pdev: PCI device information struct
14750  **/
14751 static void i40e_shutdown(struct pci_dev *pdev)
14752 {
14753 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14754 	struct i40e_hw *hw = &pf->hw;
14755 
14756 	set_bit(__I40E_SUSPENDED, pf->state);
14757 	set_bit(__I40E_DOWN, pf->state);
14758 
14759 	del_timer_sync(&pf->service_timer);
14760 	cancel_work_sync(&pf->service_task);
14761 	i40e_cloud_filter_exit(pf);
14762 	i40e_fdir_teardown(pf);
14763 
14764 	/* Client close must be called explicitly here because the timer
14765 	 * has been stopped.
14766 	 */
14767 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14768 
14769 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14770 		i40e_enable_mc_magic_wake(pf);
14771 
14772 	i40e_prep_for_reset(pf, false);
14773 
14774 	wr32(hw, I40E_PFPM_APM,
14775 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14776 	wr32(hw, I40E_PFPM_WUFC,
14777 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14778 
14779 	/* Since we're going to destroy queues during the
14780 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14781 	 * whole section
14782 	 */
14783 	rtnl_lock();
14784 	i40e_clear_interrupt_scheme(pf);
14785 	rtnl_unlock();
14786 
14787 	if (system_state == SYSTEM_POWER_OFF) {
14788 		pci_wake_from_d3(pdev, pf->wol_en);
14789 		pci_set_power_state(pdev, PCI_D3hot);
14790 	}
14791 }
14792 
14793 /**
14794  * i40e_suspend - PM callback for moving to D3
14795  * @dev: generic device information structure
14796  **/
14797 static int __maybe_unused i40e_suspend(struct device *dev)
14798 {
14799 	struct pci_dev *pdev = to_pci_dev(dev);
14800 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14801 	struct i40e_hw *hw = &pf->hw;
14802 
14803 	/* If we're already suspended, then there is nothing to do */
14804 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14805 		return 0;
14806 
14807 	set_bit(__I40E_DOWN, pf->state);
14808 
14809 	/* Ensure service task will not be running */
14810 	del_timer_sync(&pf->service_timer);
14811 	cancel_work_sync(&pf->service_task);
14812 
14813 	/* Client close must be called explicitly here because the timer
14814 	 * has been stopped.
14815 	 */
14816 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14817 
14818 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14819 		i40e_enable_mc_magic_wake(pf);
14820 
14821 	/* Since we're going to destroy queues during the
14822 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14823 	 * whole section
14824 	 */
14825 	rtnl_lock();
14826 
14827 	i40e_prep_for_reset(pf, true);
14828 
14829 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14830 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14831 
14832 	/* Clear the interrupt scheme and release our IRQs so that the system
14833 	 * can safely hibernate even when there are a large number of CPUs.
14834 	 * Otherwise hibernation might fail when mapping all the vectors back
14835 	 * to CPU0.
14836 	 */
14837 	i40e_clear_interrupt_scheme(pf);
14838 
14839 	rtnl_unlock();
14840 
14841 	return 0;
14842 }
14843 
14844 /**
14845  * i40e_resume - PM callback for waking up from D3
14846  * @dev: generic device information structure
14847  **/
14848 static int __maybe_unused i40e_resume(struct device *dev)
14849 {
14850 	struct pci_dev *pdev = to_pci_dev(dev);
14851 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14852 	int err;
14853 
14854 	/* If we're not suspended, then there is nothing to do */
14855 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14856 		return 0;
14857 
14858 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
14859 	 * since we're going to be restoring queues
14860 	 */
14861 	rtnl_lock();
14862 
14863 	/* We cleared the interrupt scheme when we suspended, so we need to
14864 	 * restore it now to resume device functionality.
14865 	 */
14866 	err = i40e_restore_interrupt_scheme(pf);
14867 	if (err) {
14868 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14869 			err);
14870 	}
14871 
14872 	clear_bit(__I40E_DOWN, pf->state);
14873 	i40e_reset_and_rebuild(pf, false, true);
14874 
14875 	rtnl_unlock();
14876 
14877 	/* Clear suspended state last after everything is recovered */
14878 	clear_bit(__I40E_SUSPENDED, pf->state);
14879 
14880 	/* Restart the service task */
14881 	mod_timer(&pf->service_timer,
14882 		  round_jiffies(jiffies + pf->service_timer_period));
14883 
14884 	return 0;
14885 }
14886 
14887 static const struct pci_error_handlers i40e_err_handler = {
14888 	.error_detected = i40e_pci_error_detected,
14889 	.slot_reset = i40e_pci_error_slot_reset,
14890 	.reset_prepare = i40e_pci_error_reset_prepare,
14891 	.reset_done = i40e_pci_error_reset_done,
14892 	.resume = i40e_pci_error_resume,
14893 };
14894 
14895 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14896 
14897 static struct pci_driver i40e_driver = {
14898 	.name     = i40e_driver_name,
14899 	.id_table = i40e_pci_tbl,
14900 	.probe    = i40e_probe,
14901 	.remove   = i40e_remove,
14902 	.driver   = {
14903 		.pm = &i40e_pm_ops,
14904 	},
14905 	.shutdown = i40e_shutdown,
14906 	.err_handler = &i40e_err_handler,
14907 	.sriov_configure = i40e_pci_sriov_configure,
14908 };
14909 
14910 /**
14911  * i40e_init_module - Driver registration routine
14912  *
14913  * i40e_init_module is the first routine called when the driver is
14914  * loaded. All it does is register with the PCI subsystem.
14915  **/
14916 static int __init i40e_init_module(void)
14917 {
14918 	pr_info("%s: %s - version %s\n", i40e_driver_name,
14919 		i40e_driver_string, i40e_driver_version_str);
14920 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
14921 
14922 	/* There is no need to throttle the number of active tasks because
14923 	 * each device limits its own task using a state bit for scheduling
14924 	 * the service task, and the device tasks do not interfere with each
14925 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14926 	 * since we need to be able to guarantee forward progress even under
14927 	 * memory pressure.
14928 	 */
14929 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
14930 	if (!i40e_wq) {
14931 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14932 		return -ENOMEM;
14933 	}
14934 
14935 	i40e_dbg_init();
14936 	return pci_register_driver(&i40e_driver);
14937 }
14938 module_init(i40e_init_module);
14939 
14940 /**
14941  * i40e_exit_module - Driver exit cleanup routine
14942  *
14943  * i40e_exit_module is called just before the driver is removed
14944  * from memory.
14945  **/
14946 static void __exit i40e_exit_module(void)
14947 {
14948 	pci_unregister_driver(&i40e_driver);
14949 	destroy_workqueue(i40e_wq);
14950 	i40e_dbg_exit();
14951 }
14952 module_exit(i40e_exit_module);
14953