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 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2111 		dev_warn(&vsi->back->pdev->dev,
2112 			 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2113 			 i40e_aq_str(hw, aq_err),
2114 			 vsi_name);
2115 	}
2116 }
2117 
2118 /**
2119  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2120  * @vsi: pointer to the VSI
2121  * @vsi_name: the VSI name
2122  * @f: filter data
2123  *
2124  * This function sets or clears the promiscuous broadcast flags for VLAN
2125  * filters in order to properly receive broadcast frames. Assumes that only
2126  * broadcast filters are passed.
2127  *
2128  * Returns status indicating success or failure;
2129  **/
2130 static i40e_status
2131 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2132 			  struct i40e_mac_filter *f)
2133 {
2134 	bool enable = f->state == I40E_FILTER_NEW;
2135 	struct i40e_hw *hw = &vsi->back->hw;
2136 	i40e_status aq_ret;
2137 
2138 	if (f->vlan == I40E_VLAN_ANY) {
2139 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2140 						   vsi->seid,
2141 						   enable,
2142 						   NULL);
2143 	} else {
2144 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2145 							    vsi->seid,
2146 							    enable,
2147 							    f->vlan,
2148 							    NULL);
2149 	}
2150 
2151 	if (aq_ret) {
2152 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2153 		dev_warn(&vsi->back->pdev->dev,
2154 			 "Error %s, forcing overflow promiscuous on %s\n",
2155 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2156 			 vsi_name);
2157 	}
2158 
2159 	return aq_ret;
2160 }
2161 
2162 /**
2163  * i40e_set_promiscuous - set promiscuous mode
2164  * @pf: board private structure
2165  * @promisc: promisc on or off
2166  *
2167  * There are different ways of setting promiscuous mode on a PF depending on
2168  * what state/environment we're in.  This identifies and sets it appropriately.
2169  * Returns 0 on success.
2170  **/
2171 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2172 {
2173 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2174 	struct i40e_hw *hw = &pf->hw;
2175 	i40e_status aq_ret;
2176 
2177 	if (vsi->type == I40E_VSI_MAIN &&
2178 	    pf->lan_veb != I40E_NO_VEB &&
2179 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2180 		/* set defport ON for Main VSI instead of true promisc
2181 		 * this way we will get all unicast/multicast and VLAN
2182 		 * promisc behavior but will not get VF or VMDq traffic
2183 		 * replicated on the Main VSI.
2184 		 */
2185 		if (promisc)
2186 			aq_ret = i40e_aq_set_default_vsi(hw,
2187 							 vsi->seid,
2188 							 NULL);
2189 		else
2190 			aq_ret = i40e_aq_clear_default_vsi(hw,
2191 							   vsi->seid,
2192 							   NULL);
2193 		if (aq_ret) {
2194 			dev_info(&pf->pdev->dev,
2195 				 "Set default VSI failed, err %s, aq_err %s\n",
2196 				 i40e_stat_str(hw, aq_ret),
2197 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2198 		}
2199 	} else {
2200 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2201 						  hw,
2202 						  vsi->seid,
2203 						  promisc, NULL,
2204 						  true);
2205 		if (aq_ret) {
2206 			dev_info(&pf->pdev->dev,
2207 				 "set unicast promisc failed, err %s, aq_err %s\n",
2208 				 i40e_stat_str(hw, aq_ret),
2209 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2210 		}
2211 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2212 						  hw,
2213 						  vsi->seid,
2214 						  promisc, NULL);
2215 		if (aq_ret) {
2216 			dev_info(&pf->pdev->dev,
2217 				 "set multicast promisc failed, err %s, aq_err %s\n",
2218 				 i40e_stat_str(hw, aq_ret),
2219 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2220 		}
2221 	}
2222 
2223 	if (!aq_ret)
2224 		pf->cur_promisc = promisc;
2225 
2226 	return aq_ret;
2227 }
2228 
2229 /**
2230  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2231  * @vsi: ptr to the VSI
2232  *
2233  * Push any outstanding VSI filter changes through the AdminQ.
2234  *
2235  * Returns 0 or error value
2236  **/
2237 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2238 {
2239 	struct hlist_head tmp_add_list, tmp_del_list;
2240 	struct i40e_mac_filter *f;
2241 	struct i40e_new_mac_filter *new, *add_head = NULL;
2242 	struct i40e_hw *hw = &vsi->back->hw;
2243 	bool old_overflow, new_overflow;
2244 	unsigned int failed_filters = 0;
2245 	unsigned int vlan_filters = 0;
2246 	char vsi_name[16] = "PF";
2247 	int filter_list_len = 0;
2248 	i40e_status aq_ret = 0;
2249 	u32 changed_flags = 0;
2250 	struct hlist_node *h;
2251 	struct i40e_pf *pf;
2252 	int num_add = 0;
2253 	int num_del = 0;
2254 	int retval = 0;
2255 	u16 cmd_flags;
2256 	int list_size;
2257 	int bkt;
2258 
2259 	/* empty array typed pointers, kcalloc later */
2260 	struct i40e_aqc_add_macvlan_element_data *add_list;
2261 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2262 
2263 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2264 		usleep_range(1000, 2000);
2265 	pf = vsi->back;
2266 
2267 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2268 
2269 	if (vsi->netdev) {
2270 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2271 		vsi->current_netdev_flags = vsi->netdev->flags;
2272 	}
2273 
2274 	INIT_HLIST_HEAD(&tmp_add_list);
2275 	INIT_HLIST_HEAD(&tmp_del_list);
2276 
2277 	if (vsi->type == I40E_VSI_SRIOV)
2278 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2279 	else if (vsi->type != I40E_VSI_MAIN)
2280 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2281 
2282 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2283 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2284 
2285 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2286 		/* Create a list of filters to delete. */
2287 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2288 			if (f->state == I40E_FILTER_REMOVE) {
2289 				/* Move the element into temporary del_list */
2290 				hash_del(&f->hlist);
2291 				hlist_add_head(&f->hlist, &tmp_del_list);
2292 
2293 				/* Avoid counting removed filters */
2294 				continue;
2295 			}
2296 			if (f->state == I40E_FILTER_NEW) {
2297 				/* Create a temporary i40e_new_mac_filter */
2298 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2299 				if (!new)
2300 					goto err_no_memory_locked;
2301 
2302 				/* Store pointer to the real filter */
2303 				new->f = f;
2304 				new->state = f->state;
2305 
2306 				/* Add it to the hash list */
2307 				hlist_add_head(&new->hlist, &tmp_add_list);
2308 			}
2309 
2310 			/* Count the number of active (current and new) VLAN
2311 			 * filters we have now. Does not count filters which
2312 			 * are marked for deletion.
2313 			 */
2314 			if (f->vlan > 0)
2315 				vlan_filters++;
2316 		}
2317 
2318 		retval = i40e_correct_mac_vlan_filters(vsi,
2319 						       &tmp_add_list,
2320 						       &tmp_del_list,
2321 						       vlan_filters);
2322 		if (retval)
2323 			goto err_no_memory_locked;
2324 
2325 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2326 	}
2327 
2328 	/* Now process 'del_list' outside the lock */
2329 	if (!hlist_empty(&tmp_del_list)) {
2330 		filter_list_len = hw->aq.asq_buf_size /
2331 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2332 		list_size = filter_list_len *
2333 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2334 		del_list = kzalloc(list_size, GFP_ATOMIC);
2335 		if (!del_list)
2336 			goto err_no_memory;
2337 
2338 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2339 			cmd_flags = 0;
2340 
2341 			/* handle broadcast filters by updating the broadcast
2342 			 * promiscuous flag and release filter list.
2343 			 */
2344 			if (is_broadcast_ether_addr(f->macaddr)) {
2345 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2346 
2347 				hlist_del(&f->hlist);
2348 				kfree(f);
2349 				continue;
2350 			}
2351 
2352 			/* add to delete list */
2353 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2354 			if (f->vlan == I40E_VLAN_ANY) {
2355 				del_list[num_del].vlan_tag = 0;
2356 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2357 			} else {
2358 				del_list[num_del].vlan_tag =
2359 					cpu_to_le16((u16)(f->vlan));
2360 			}
2361 
2362 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2363 			del_list[num_del].flags = cmd_flags;
2364 			num_del++;
2365 
2366 			/* flush a full buffer */
2367 			if (num_del == filter_list_len) {
2368 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2369 						     num_del, &retval);
2370 				memset(del_list, 0, list_size);
2371 				num_del = 0;
2372 			}
2373 			/* Release memory for MAC filter entries which were
2374 			 * synced up with HW.
2375 			 */
2376 			hlist_del(&f->hlist);
2377 			kfree(f);
2378 		}
2379 
2380 		if (num_del) {
2381 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2382 					     num_del, &retval);
2383 		}
2384 
2385 		kfree(del_list);
2386 		del_list = NULL;
2387 	}
2388 
2389 	if (!hlist_empty(&tmp_add_list)) {
2390 		/* Do all the adds now. */
2391 		filter_list_len = hw->aq.asq_buf_size /
2392 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2393 		list_size = filter_list_len *
2394 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2395 		add_list = kzalloc(list_size, GFP_ATOMIC);
2396 		if (!add_list)
2397 			goto err_no_memory;
2398 
2399 		num_add = 0;
2400 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2401 			/* handle broadcast filters by updating the broadcast
2402 			 * promiscuous flag instead of adding a MAC filter.
2403 			 */
2404 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2405 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2406 							      new->f))
2407 					new->state = I40E_FILTER_FAILED;
2408 				else
2409 					new->state = I40E_FILTER_ACTIVE;
2410 				continue;
2411 			}
2412 
2413 			/* add to add array */
2414 			if (num_add == 0)
2415 				add_head = new;
2416 			cmd_flags = 0;
2417 			ether_addr_copy(add_list[num_add].mac_addr,
2418 					new->f->macaddr);
2419 			if (new->f->vlan == I40E_VLAN_ANY) {
2420 				add_list[num_add].vlan_tag = 0;
2421 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2422 			} else {
2423 				add_list[num_add].vlan_tag =
2424 					cpu_to_le16((u16)(new->f->vlan));
2425 			}
2426 			add_list[num_add].queue_number = 0;
2427 			/* set invalid match method for later detection */
2428 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2429 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2430 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2431 			num_add++;
2432 
2433 			/* flush a full buffer */
2434 			if (num_add == filter_list_len) {
2435 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2436 						     add_head, num_add);
2437 				memset(add_list, 0, list_size);
2438 				num_add = 0;
2439 			}
2440 		}
2441 		if (num_add) {
2442 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2443 					     num_add);
2444 		}
2445 		/* Now move all of the filters from the temp add list back to
2446 		 * the VSI's list.
2447 		 */
2448 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2449 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2450 			/* Only update the state if we're still NEW */
2451 			if (new->f->state == I40E_FILTER_NEW)
2452 				new->f->state = new->state;
2453 			hlist_del(&new->hlist);
2454 			kfree(new);
2455 		}
2456 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2457 		kfree(add_list);
2458 		add_list = NULL;
2459 	}
2460 
2461 	/* Determine the number of active and failed filters. */
2462 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2463 	vsi->active_filters = 0;
2464 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2465 		if (f->state == I40E_FILTER_ACTIVE)
2466 			vsi->active_filters++;
2467 		else if (f->state == I40E_FILTER_FAILED)
2468 			failed_filters++;
2469 	}
2470 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2471 
2472 	/* Check if we are able to exit overflow promiscuous mode. We can
2473 	 * safely exit if we didn't just enter, we no longer have any failed
2474 	 * filters, and we have reduced filters below the threshold value.
2475 	 */
2476 	if (old_overflow && !failed_filters &&
2477 	    vsi->active_filters < vsi->promisc_threshold) {
2478 		dev_info(&pf->pdev->dev,
2479 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2480 			 vsi_name);
2481 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2482 		vsi->promisc_threshold = 0;
2483 	}
2484 
2485 	/* if the VF is not trusted do not do promisc */
2486 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2487 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2488 		goto out;
2489 	}
2490 
2491 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2492 
2493 	/* If we are entering overflow promiscuous, we need to calculate a new
2494 	 * threshold for when we are safe to exit
2495 	 */
2496 	if (!old_overflow && new_overflow)
2497 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2498 
2499 	/* check for changes in promiscuous modes */
2500 	if (changed_flags & IFF_ALLMULTI) {
2501 		bool cur_multipromisc;
2502 
2503 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2504 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2505 							       vsi->seid,
2506 							       cur_multipromisc,
2507 							       NULL);
2508 		if (aq_ret) {
2509 			retval = i40e_aq_rc_to_posix(aq_ret,
2510 						     hw->aq.asq_last_status);
2511 			dev_info(&pf->pdev->dev,
2512 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2513 				 vsi_name,
2514 				 i40e_stat_str(hw, aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 	}
2518 
2519 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2520 		bool cur_promisc;
2521 
2522 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2523 			       new_overflow);
2524 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2525 		if (aq_ret) {
2526 			retval = i40e_aq_rc_to_posix(aq_ret,
2527 						     hw->aq.asq_last_status);
2528 			dev_info(&pf->pdev->dev,
2529 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2530 				 cur_promisc ? "on" : "off",
2531 				 vsi_name,
2532 				 i40e_stat_str(hw, aq_ret),
2533 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2534 		}
2535 	}
2536 out:
2537 	/* if something went wrong then set the changed flag so we try again */
2538 	if (retval)
2539 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2540 
2541 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2542 	return retval;
2543 
2544 err_no_memory:
2545 	/* Restore elements on the temporary add and delete lists */
2546 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2547 err_no_memory_locked:
2548 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2549 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2550 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2551 
2552 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2553 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2554 	return -ENOMEM;
2555 }
2556 
2557 /**
2558  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2559  * @pf: board private structure
2560  **/
2561 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2562 {
2563 	int v;
2564 
2565 	if (!pf)
2566 		return;
2567 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2568 		return;
2569 
2570 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2571 		if (pf->vsi[v] &&
2572 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2573 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2574 
2575 			if (ret) {
2576 				/* come back and try again later */
2577 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2578 					pf->state);
2579 				break;
2580 			}
2581 		}
2582 	}
2583 }
2584 
2585 /**
2586  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2587  * @vsi: the vsi
2588  **/
2589 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2590 {
2591 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2592 		return I40E_RXBUFFER_2048;
2593 	else
2594 		return I40E_RXBUFFER_3072;
2595 }
2596 
2597 /**
2598  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2599  * @netdev: network interface device structure
2600  * @new_mtu: new value for maximum frame size
2601  *
2602  * Returns 0 on success, negative on failure
2603  **/
2604 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2605 {
2606 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2607 	struct i40e_vsi *vsi = np->vsi;
2608 	struct i40e_pf *pf = vsi->back;
2609 
2610 	if (i40e_enabled_xdp_vsi(vsi)) {
2611 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2612 
2613 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2614 			return -EINVAL;
2615 	}
2616 
2617 	netdev_info(netdev, "changing MTU from %d to %d\n",
2618 		    netdev->mtu, new_mtu);
2619 	netdev->mtu = new_mtu;
2620 	if (netif_running(netdev))
2621 		i40e_vsi_reinit_locked(vsi);
2622 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2623 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2624 	return 0;
2625 }
2626 
2627 /**
2628  * i40e_ioctl - Access the hwtstamp interface
2629  * @netdev: network interface device structure
2630  * @ifr: interface request data
2631  * @cmd: ioctl command
2632  **/
2633 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2634 {
2635 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2636 	struct i40e_pf *pf = np->vsi->back;
2637 
2638 	switch (cmd) {
2639 	case SIOCGHWTSTAMP:
2640 		return i40e_ptp_get_ts_config(pf, ifr);
2641 	case SIOCSHWTSTAMP:
2642 		return i40e_ptp_set_ts_config(pf, ifr);
2643 	default:
2644 		return -EOPNOTSUPP;
2645 	}
2646 }
2647 
2648 /**
2649  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2650  * @vsi: the vsi being adjusted
2651  **/
2652 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2653 {
2654 	struct i40e_vsi_context ctxt;
2655 	i40e_status ret;
2656 
2657 	if ((vsi->info.valid_sections &
2658 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2659 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2660 		return;  /* already enabled */
2661 
2662 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2663 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2664 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2665 
2666 	ctxt.seid = vsi->seid;
2667 	ctxt.info = vsi->info;
2668 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2669 	if (ret) {
2670 		dev_info(&vsi->back->pdev->dev,
2671 			 "update vlan stripping failed, err %s aq_err %s\n",
2672 			 i40e_stat_str(&vsi->back->hw, ret),
2673 			 i40e_aq_str(&vsi->back->hw,
2674 				     vsi->back->hw.aq.asq_last_status));
2675 	}
2676 }
2677 
2678 /**
2679  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2680  * @vsi: the vsi being adjusted
2681  **/
2682 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2683 {
2684 	struct i40e_vsi_context ctxt;
2685 	i40e_status ret;
2686 
2687 	if ((vsi->info.valid_sections &
2688 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2689 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2690 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2691 		return;  /* already disabled */
2692 
2693 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2694 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2695 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2696 
2697 	ctxt.seid = vsi->seid;
2698 	ctxt.info = vsi->info;
2699 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2700 	if (ret) {
2701 		dev_info(&vsi->back->pdev->dev,
2702 			 "update vlan stripping failed, err %s aq_err %s\n",
2703 			 i40e_stat_str(&vsi->back->hw, ret),
2704 			 i40e_aq_str(&vsi->back->hw,
2705 				     vsi->back->hw.aq.asq_last_status));
2706 	}
2707 }
2708 
2709 /**
2710  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2711  * @vsi: the vsi being configured
2712  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2713  *
2714  * This is a helper function for adding a new MAC/VLAN filter with the
2715  * specified VLAN for each existing MAC address already in the hash table.
2716  * This function does *not* perform any accounting to update filters based on
2717  * VLAN mode.
2718  *
2719  * NOTE: this function expects to be called while under the
2720  * mac_filter_hash_lock
2721  **/
2722 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2723 {
2724 	struct i40e_mac_filter *f, *add_f;
2725 	struct hlist_node *h;
2726 	int bkt;
2727 
2728 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2729 		if (f->state == I40E_FILTER_REMOVE)
2730 			continue;
2731 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2732 		if (!add_f) {
2733 			dev_info(&vsi->back->pdev->dev,
2734 				 "Could not add vlan filter %d for %pM\n",
2735 				 vid, f->macaddr);
2736 			return -ENOMEM;
2737 		}
2738 	}
2739 
2740 	return 0;
2741 }
2742 
2743 /**
2744  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2745  * @vsi: the VSI being configured
2746  * @vid: VLAN id to be added
2747  **/
2748 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2749 {
2750 	int err;
2751 
2752 	if (vsi->info.pvid)
2753 		return -EINVAL;
2754 
2755 	/* The network stack will attempt to add VID=0, with the intention to
2756 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2757 	 * these packets by default when configured to receive untagged
2758 	 * packets, so we don't need to add a filter for this case.
2759 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2760 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2761 	 * Thus, we do not want to actually add a filter for VID=0
2762 	 */
2763 	if (!vid)
2764 		return 0;
2765 
2766 	/* Locked once because all functions invoked below iterates list*/
2767 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2768 	err = i40e_add_vlan_all_mac(vsi, vid);
2769 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2770 	if (err)
2771 		return err;
2772 
2773 	/* schedule our worker thread which will take care of
2774 	 * applying the new filter changes
2775 	 */
2776 	i40e_service_event_schedule(vsi->back);
2777 	return 0;
2778 }
2779 
2780 /**
2781  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2782  * @vsi: the vsi being configured
2783  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2784  *
2785  * This function should be used to remove all VLAN filters which match the
2786  * given VID. It does not schedule the service event and does not take the
2787  * mac_filter_hash_lock so it may be combined with other operations under
2788  * a single invocation of the mac_filter_hash_lock.
2789  *
2790  * NOTE: this function expects to be called while under the
2791  * mac_filter_hash_lock
2792  */
2793 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2794 {
2795 	struct i40e_mac_filter *f;
2796 	struct hlist_node *h;
2797 	int bkt;
2798 
2799 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2800 		if (f->vlan == vid)
2801 			__i40e_del_filter(vsi, f);
2802 	}
2803 }
2804 
2805 /**
2806  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2807  * @vsi: the VSI being configured
2808  * @vid: VLAN id to be removed
2809  **/
2810 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2811 {
2812 	if (!vid || vsi->info.pvid)
2813 		return;
2814 
2815 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2816 	i40e_rm_vlan_all_mac(vsi, vid);
2817 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2818 
2819 	/* schedule our worker thread which will take care of
2820 	 * applying the new filter changes
2821 	 */
2822 	i40e_service_event_schedule(vsi->back);
2823 }
2824 
2825 /**
2826  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2827  * @netdev: network interface to be adjusted
2828  * @proto: unused protocol value
2829  * @vid: vlan id to be added
2830  *
2831  * net_device_ops implementation for adding vlan ids
2832  **/
2833 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2834 				__always_unused __be16 proto, u16 vid)
2835 {
2836 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2837 	struct i40e_vsi *vsi = np->vsi;
2838 	int ret = 0;
2839 
2840 	if (vid >= VLAN_N_VID)
2841 		return -EINVAL;
2842 
2843 	ret = i40e_vsi_add_vlan(vsi, vid);
2844 	if (!ret)
2845 		set_bit(vid, vsi->active_vlans);
2846 
2847 	return ret;
2848 }
2849 
2850 /**
2851  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2852  * @netdev: network interface to be adjusted
2853  * @proto: unused protocol value
2854  * @vid: vlan id to be added
2855  **/
2856 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2857 				    __always_unused __be16 proto, u16 vid)
2858 {
2859 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2860 	struct i40e_vsi *vsi = np->vsi;
2861 
2862 	if (vid >= VLAN_N_VID)
2863 		return;
2864 	set_bit(vid, vsi->active_vlans);
2865 }
2866 
2867 /**
2868  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2869  * @netdev: network interface to be adjusted
2870  * @proto: unused protocol value
2871  * @vid: vlan id to be removed
2872  *
2873  * net_device_ops implementation for removing vlan ids
2874  **/
2875 static int i40e_vlan_rx_kill_vid(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 	/* return code is ignored as there is nothing a user
2882 	 * can do about failure to remove and a log message was
2883 	 * already printed from the other function
2884 	 */
2885 	i40e_vsi_kill_vlan(vsi, vid);
2886 
2887 	clear_bit(vid, vsi->active_vlans);
2888 
2889 	return 0;
2890 }
2891 
2892 /**
2893  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2894  * @vsi: the vsi being brought back up
2895  **/
2896 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2897 {
2898 	u16 vid;
2899 
2900 	if (!vsi->netdev)
2901 		return;
2902 
2903 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2904 		i40e_vlan_stripping_enable(vsi);
2905 	else
2906 		i40e_vlan_stripping_disable(vsi);
2907 
2908 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2909 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2910 					vid);
2911 }
2912 
2913 /**
2914  * i40e_vsi_add_pvid - Add pvid for the VSI
2915  * @vsi: the vsi being adjusted
2916  * @vid: the vlan id to set as a PVID
2917  **/
2918 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2919 {
2920 	struct i40e_vsi_context ctxt;
2921 	i40e_status ret;
2922 
2923 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2924 	vsi->info.pvid = cpu_to_le16(vid);
2925 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2926 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2927 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2928 
2929 	ctxt.seid = vsi->seid;
2930 	ctxt.info = vsi->info;
2931 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2932 	if (ret) {
2933 		dev_info(&vsi->back->pdev->dev,
2934 			 "add pvid failed, err %s aq_err %s\n",
2935 			 i40e_stat_str(&vsi->back->hw, ret),
2936 			 i40e_aq_str(&vsi->back->hw,
2937 				     vsi->back->hw.aq.asq_last_status));
2938 		return -ENOENT;
2939 	}
2940 
2941 	return 0;
2942 }
2943 
2944 /**
2945  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2946  * @vsi: the vsi being adjusted
2947  *
2948  * Just use the vlan_rx_register() service to put it back to normal
2949  **/
2950 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2951 {
2952 	i40e_vlan_stripping_disable(vsi);
2953 
2954 	vsi->info.pvid = 0;
2955 }
2956 
2957 /**
2958  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2959  * @vsi: ptr to the VSI
2960  *
2961  * If this function returns with an error, then it's possible one or
2962  * more of the rings is populated (while the rest are not).  It is the
2963  * callers duty to clean those orphaned rings.
2964  *
2965  * Return 0 on success, negative on failure
2966  **/
2967 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2968 {
2969 	int i, err = 0;
2970 
2971 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2972 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2973 
2974 	if (!i40e_enabled_xdp_vsi(vsi))
2975 		return err;
2976 
2977 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2978 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2979 
2980 	return err;
2981 }
2982 
2983 /**
2984  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2985  * @vsi: ptr to the VSI
2986  *
2987  * Free VSI's transmit software resources
2988  **/
2989 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2990 {
2991 	int i;
2992 
2993 	if (vsi->tx_rings) {
2994 		for (i = 0; i < vsi->num_queue_pairs; i++)
2995 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2996 				i40e_free_tx_resources(vsi->tx_rings[i]);
2997 	}
2998 
2999 	if (vsi->xdp_rings) {
3000 		for (i = 0; i < vsi->num_queue_pairs; i++)
3001 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3002 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3003 	}
3004 }
3005 
3006 /**
3007  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3008  * @vsi: ptr to the VSI
3009  *
3010  * If this function returns with an error, then it's possible one or
3011  * more of the rings is populated (while the rest are not).  It is the
3012  * callers duty to clean those orphaned rings.
3013  *
3014  * Return 0 on success, negative on failure
3015  **/
3016 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3017 {
3018 	int i, err = 0;
3019 
3020 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3021 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3022 	return err;
3023 }
3024 
3025 /**
3026  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3027  * @vsi: ptr to the VSI
3028  *
3029  * Free all receive software resources
3030  **/
3031 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3032 {
3033 	int i;
3034 
3035 	if (!vsi->rx_rings)
3036 		return;
3037 
3038 	for (i = 0; i < vsi->num_queue_pairs; i++)
3039 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3040 			i40e_free_rx_resources(vsi->rx_rings[i]);
3041 }
3042 
3043 /**
3044  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3045  * @ring: The Tx ring to configure
3046  *
3047  * This enables/disables XPS for a given Tx descriptor ring
3048  * based on the TCs enabled for the VSI that ring belongs to.
3049  **/
3050 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3051 {
3052 	int cpu;
3053 
3054 	if (!ring->q_vector || !ring->netdev || ring->ch)
3055 		return;
3056 
3057 	/* We only initialize XPS once, so as not to overwrite user settings */
3058 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3059 		return;
3060 
3061 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3062 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3063 			    ring->queue_index);
3064 }
3065 
3066 /**
3067  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3068  * @ring: The Tx ring to configure
3069  *
3070  * Configure the Tx descriptor ring in the HMC context.
3071  **/
3072 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3073 {
3074 	struct i40e_vsi *vsi = ring->vsi;
3075 	u16 pf_q = vsi->base_queue + ring->queue_index;
3076 	struct i40e_hw *hw = &vsi->back->hw;
3077 	struct i40e_hmc_obj_txq tx_ctx;
3078 	i40e_status err = 0;
3079 	u32 qtx_ctl = 0;
3080 
3081 	if (ring_is_xdp(ring))
3082 		ring->xsk_umem = i40e_xsk_umem(ring);
3083 
3084 	/* some ATR related tx ring init */
3085 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3086 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3087 		ring->atr_count = 0;
3088 	} else {
3089 		ring->atr_sample_rate = 0;
3090 	}
3091 
3092 	/* configure XPS */
3093 	i40e_config_xps_tx_ring(ring);
3094 
3095 	/* clear the context structure first */
3096 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3097 
3098 	tx_ctx.new_context = 1;
3099 	tx_ctx.base = (ring->dma / 128);
3100 	tx_ctx.qlen = ring->count;
3101 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3102 					       I40E_FLAG_FD_ATR_ENABLED));
3103 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3104 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3105 	if (vsi->type != I40E_VSI_FDIR)
3106 		tx_ctx.head_wb_ena = 1;
3107 	tx_ctx.head_wb_addr = ring->dma +
3108 			      (ring->count * sizeof(struct i40e_tx_desc));
3109 
3110 	/* As part of VSI creation/update, FW allocates certain
3111 	 * Tx arbitration queue sets for each TC enabled for
3112 	 * the VSI. The FW returns the handles to these queue
3113 	 * sets as part of the response buffer to Add VSI,
3114 	 * Update VSI, etc. AQ commands. It is expected that
3115 	 * these queue set handles be associated with the Tx
3116 	 * queues by the driver as part of the TX queue context
3117 	 * initialization. This has to be done regardless of
3118 	 * DCB as by default everything is mapped to TC0.
3119 	 */
3120 
3121 	if (ring->ch)
3122 		tx_ctx.rdylist =
3123 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3124 
3125 	else
3126 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3127 
3128 	tx_ctx.rdylist_act = 0;
3129 
3130 	/* clear the context in the HMC */
3131 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3132 	if (err) {
3133 		dev_info(&vsi->back->pdev->dev,
3134 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3135 			 ring->queue_index, pf_q, err);
3136 		return -ENOMEM;
3137 	}
3138 
3139 	/* set the context in the HMC */
3140 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3141 	if (err) {
3142 		dev_info(&vsi->back->pdev->dev,
3143 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3144 			 ring->queue_index, pf_q, err);
3145 		return -ENOMEM;
3146 	}
3147 
3148 	/* Now associate this queue with this PCI function */
3149 	if (ring->ch) {
3150 		if (ring->ch->type == I40E_VSI_VMDQ2)
3151 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3152 		else
3153 			return -EINVAL;
3154 
3155 		qtx_ctl |= (ring->ch->vsi_number <<
3156 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3157 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3158 	} else {
3159 		if (vsi->type == I40E_VSI_VMDQ2) {
3160 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3161 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3162 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3163 		} else {
3164 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3165 		}
3166 	}
3167 
3168 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3169 		    I40E_QTX_CTL_PF_INDX_MASK);
3170 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3171 	i40e_flush(hw);
3172 
3173 	/* cache tail off for easier writes later */
3174 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3175 
3176 	return 0;
3177 }
3178 
3179 /**
3180  * i40e_configure_rx_ring - Configure a receive ring context
3181  * @ring: The Rx ring to configure
3182  *
3183  * Configure the Rx descriptor ring in the HMC context.
3184  **/
3185 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3186 {
3187 	struct i40e_vsi *vsi = ring->vsi;
3188 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3189 	u16 pf_q = vsi->base_queue + ring->queue_index;
3190 	struct i40e_hw *hw = &vsi->back->hw;
3191 	struct i40e_hmc_obj_rxq rx_ctx;
3192 	i40e_status err = 0;
3193 	bool ok;
3194 	int ret;
3195 
3196 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3197 
3198 	/* clear the context structure first */
3199 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3200 
3201 	if (ring->vsi->type == I40E_VSI_MAIN)
3202 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3203 
3204 	ring->xsk_umem = i40e_xsk_umem(ring);
3205 	if (ring->xsk_umem) {
3206 		ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
3207 				   XDP_PACKET_HEADROOM;
3208 		/* For AF_XDP ZC, we disallow packets to span on
3209 		 * multiple buffers, thus letting us skip that
3210 		 * handling in the fast-path.
3211 		 */
3212 		chain_len = 1;
3213 		ring->zca.free = i40e_zca_free;
3214 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3215 						 MEM_TYPE_ZERO_COPY,
3216 						 &ring->zca);
3217 		if (ret)
3218 			return ret;
3219 		dev_info(&vsi->back->pdev->dev,
3220 			 "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
3221 			 ring->queue_index);
3222 
3223 	} else {
3224 		ring->rx_buf_len = vsi->rx_buf_len;
3225 		if (ring->vsi->type == I40E_VSI_MAIN) {
3226 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3227 							 MEM_TYPE_PAGE_SHARED,
3228 							 NULL);
3229 			if (ret)
3230 				return ret;
3231 		}
3232 	}
3233 
3234 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3235 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3236 
3237 	rx_ctx.base = (ring->dma / 128);
3238 	rx_ctx.qlen = ring->count;
3239 
3240 	/* use 32 byte descriptors */
3241 	rx_ctx.dsize = 1;
3242 
3243 	/* descriptor type is always zero
3244 	 * rx_ctx.dtype = 0;
3245 	 */
3246 	rx_ctx.hsplit_0 = 0;
3247 
3248 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3249 	if (hw->revision_id == 0)
3250 		rx_ctx.lrxqthresh = 0;
3251 	else
3252 		rx_ctx.lrxqthresh = 1;
3253 	rx_ctx.crcstrip = 1;
3254 	rx_ctx.l2tsel = 1;
3255 	/* this controls whether VLAN is stripped from inner headers */
3256 	rx_ctx.showiv = 0;
3257 	/* set the prefena field to 1 because the manual says to */
3258 	rx_ctx.prefena = 1;
3259 
3260 	/* clear the context in the HMC */
3261 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3262 	if (err) {
3263 		dev_info(&vsi->back->pdev->dev,
3264 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3265 			 ring->queue_index, pf_q, err);
3266 		return -ENOMEM;
3267 	}
3268 
3269 	/* set the context in the HMC */
3270 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3271 	if (err) {
3272 		dev_info(&vsi->back->pdev->dev,
3273 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3274 			 ring->queue_index, pf_q, err);
3275 		return -ENOMEM;
3276 	}
3277 
3278 	/* configure Rx buffer alignment */
3279 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3280 		clear_ring_build_skb_enabled(ring);
3281 	else
3282 		set_ring_build_skb_enabled(ring);
3283 
3284 	/* cache tail for quicker writes, and clear the reg before use */
3285 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3286 	writel(0, ring->tail);
3287 
3288 	ok = ring->xsk_umem ?
3289 	     i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
3290 	     !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3291 	if (!ok) {
3292 		/* Log this in case the user has forgotten to give the kernel
3293 		 * any buffers, even later in the application.
3294 		 */
3295 		dev_info(&vsi->back->pdev->dev,
3296 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3297 			 ring->xsk_umem ? "UMEM enabled " : "",
3298 			 ring->queue_index, pf_q);
3299 	}
3300 
3301 	return 0;
3302 }
3303 
3304 /**
3305  * i40e_vsi_configure_tx - Configure the VSI for Tx
3306  * @vsi: VSI structure describing this set of rings and resources
3307  *
3308  * Configure the Tx VSI for operation.
3309  **/
3310 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3311 {
3312 	int err = 0;
3313 	u16 i;
3314 
3315 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3316 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3317 
3318 	if (!i40e_enabled_xdp_vsi(vsi))
3319 		return err;
3320 
3321 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3322 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3323 
3324 	return err;
3325 }
3326 
3327 /**
3328  * i40e_vsi_configure_rx - Configure the VSI for Rx
3329  * @vsi: the VSI being configured
3330  *
3331  * Configure the Rx VSI for operation.
3332  **/
3333 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3334 {
3335 	int err = 0;
3336 	u16 i;
3337 
3338 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3339 		vsi->max_frame = I40E_MAX_RXBUFFER;
3340 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3341 #if (PAGE_SIZE < 8192)
3342 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3343 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3344 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3345 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3346 #endif
3347 	} else {
3348 		vsi->max_frame = I40E_MAX_RXBUFFER;
3349 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3350 						       I40E_RXBUFFER_2048;
3351 	}
3352 
3353 	/* set up individual rings */
3354 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3355 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3356 
3357 	return err;
3358 }
3359 
3360 /**
3361  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3362  * @vsi: ptr to the VSI
3363  **/
3364 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3365 {
3366 	struct i40e_ring *tx_ring, *rx_ring;
3367 	u16 qoffset, qcount;
3368 	int i, n;
3369 
3370 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3371 		/* Reset the TC information */
3372 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3373 			rx_ring = vsi->rx_rings[i];
3374 			tx_ring = vsi->tx_rings[i];
3375 			rx_ring->dcb_tc = 0;
3376 			tx_ring->dcb_tc = 0;
3377 		}
3378 		return;
3379 	}
3380 
3381 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3382 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3383 			continue;
3384 
3385 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3386 		qcount = vsi->tc_config.tc_info[n].qcount;
3387 		for (i = qoffset; i < (qoffset + qcount); i++) {
3388 			rx_ring = vsi->rx_rings[i];
3389 			tx_ring = vsi->tx_rings[i];
3390 			rx_ring->dcb_tc = n;
3391 			tx_ring->dcb_tc = n;
3392 		}
3393 	}
3394 }
3395 
3396 /**
3397  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3398  * @vsi: ptr to the VSI
3399  **/
3400 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3401 {
3402 	if (vsi->netdev)
3403 		i40e_set_rx_mode(vsi->netdev);
3404 }
3405 
3406 /**
3407  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3408  * @vsi: Pointer to the targeted VSI
3409  *
3410  * This function replays the hlist on the hw where all the SB Flow Director
3411  * filters were saved.
3412  **/
3413 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3414 {
3415 	struct i40e_fdir_filter *filter;
3416 	struct i40e_pf *pf = vsi->back;
3417 	struct hlist_node *node;
3418 
3419 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3420 		return;
3421 
3422 	/* Reset FDir counters as we're replaying all existing filters */
3423 	pf->fd_tcp4_filter_cnt = 0;
3424 	pf->fd_udp4_filter_cnt = 0;
3425 	pf->fd_sctp4_filter_cnt = 0;
3426 	pf->fd_ip4_filter_cnt = 0;
3427 
3428 	hlist_for_each_entry_safe(filter, node,
3429 				  &pf->fdir_filter_list, fdir_node) {
3430 		i40e_add_del_fdir(vsi, filter, true);
3431 	}
3432 }
3433 
3434 /**
3435  * i40e_vsi_configure - Set up the VSI for action
3436  * @vsi: the VSI being configured
3437  **/
3438 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3439 {
3440 	int err;
3441 
3442 	i40e_set_vsi_rx_mode(vsi);
3443 	i40e_restore_vlan(vsi);
3444 	i40e_vsi_config_dcb_rings(vsi);
3445 	err = i40e_vsi_configure_tx(vsi);
3446 	if (!err)
3447 		err = i40e_vsi_configure_rx(vsi);
3448 
3449 	return err;
3450 }
3451 
3452 /**
3453  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3454  * @vsi: the VSI being configured
3455  **/
3456 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3457 {
3458 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3459 	struct i40e_pf *pf = vsi->back;
3460 	struct i40e_hw *hw = &pf->hw;
3461 	u16 vector;
3462 	int i, q;
3463 	u32 qp;
3464 
3465 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3466 	 * and PFINT_LNKLSTn registers, e.g.:
3467 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3468 	 */
3469 	qp = vsi->base_queue;
3470 	vector = vsi->base_vector;
3471 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3472 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3473 
3474 		q_vector->rx.next_update = jiffies + 1;
3475 		q_vector->rx.target_itr =
3476 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3477 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3478 		     q_vector->rx.target_itr);
3479 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3480 
3481 		q_vector->tx.next_update = jiffies + 1;
3482 		q_vector->tx.target_itr =
3483 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3484 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3485 		     q_vector->tx.target_itr);
3486 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3487 
3488 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3489 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3490 
3491 		/* Linked list for the queuepairs assigned to this vector */
3492 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3493 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3494 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3495 			u32 val;
3496 
3497 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3498 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3499 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3500 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3501 			      (I40E_QUEUE_TYPE_TX <<
3502 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3503 
3504 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3505 
3506 			if (has_xdp) {
3507 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3508 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3509 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3510 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3511 				      (I40E_QUEUE_TYPE_TX <<
3512 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3513 
3514 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3515 			}
3516 
3517 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3518 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3519 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3520 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3521 			      (I40E_QUEUE_TYPE_RX <<
3522 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3523 
3524 			/* Terminate the linked list */
3525 			if (q == (q_vector->num_ringpairs - 1))
3526 				val |= (I40E_QUEUE_END_OF_LIST <<
3527 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3528 
3529 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3530 			qp++;
3531 		}
3532 	}
3533 
3534 	i40e_flush(hw);
3535 }
3536 
3537 /**
3538  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3539  * @pf: pointer to private device data structure
3540  **/
3541 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3542 {
3543 	struct i40e_hw *hw = &pf->hw;
3544 	u32 val;
3545 
3546 	/* clear things first */
3547 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3548 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3549 
3550 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3551 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3552 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3553 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3554 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3555 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3556 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3557 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3558 
3559 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3560 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3561 
3562 	if (pf->flags & I40E_FLAG_PTP)
3563 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3564 
3565 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3566 
3567 	/* SW_ITR_IDX = 0, but don't change INTENA */
3568 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3569 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3570 
3571 	/* OTHER_ITR_IDX = 0 */
3572 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3573 }
3574 
3575 /**
3576  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3577  * @vsi: the VSI being configured
3578  **/
3579 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3580 {
3581 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3582 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3583 	struct i40e_pf *pf = vsi->back;
3584 	struct i40e_hw *hw = &pf->hw;
3585 	u32 val;
3586 
3587 	/* set the ITR configuration */
3588 	q_vector->rx.next_update = jiffies + 1;
3589 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3590 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3591 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3592 	q_vector->tx.next_update = jiffies + 1;
3593 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3594 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3595 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3596 
3597 	i40e_enable_misc_int_causes(pf);
3598 
3599 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3600 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3601 
3602 	/* Associate the queue pair to the vector and enable the queue int */
3603 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3604 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3605 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3606 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3607 
3608 	wr32(hw, I40E_QINT_RQCTL(0), val);
3609 
3610 	if (i40e_enabled_xdp_vsi(vsi)) {
3611 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3612 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3613 		      (I40E_QUEUE_TYPE_TX
3614 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3615 
3616 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3617 	}
3618 
3619 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3620 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3621 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3622 
3623 	wr32(hw, I40E_QINT_TQCTL(0), val);
3624 	i40e_flush(hw);
3625 }
3626 
3627 /**
3628  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3629  * @pf: board private structure
3630  **/
3631 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3632 {
3633 	struct i40e_hw *hw = &pf->hw;
3634 
3635 	wr32(hw, I40E_PFINT_DYN_CTL0,
3636 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3637 	i40e_flush(hw);
3638 }
3639 
3640 /**
3641  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3642  * @pf: board private structure
3643  **/
3644 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3645 {
3646 	struct i40e_hw *hw = &pf->hw;
3647 	u32 val;
3648 
3649 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3650 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3651 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3652 
3653 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3654 	i40e_flush(hw);
3655 }
3656 
3657 /**
3658  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3659  * @irq: interrupt number
3660  * @data: pointer to a q_vector
3661  **/
3662 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3663 {
3664 	struct i40e_q_vector *q_vector = data;
3665 
3666 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3667 		return IRQ_HANDLED;
3668 
3669 	napi_schedule_irqoff(&q_vector->napi);
3670 
3671 	return IRQ_HANDLED;
3672 }
3673 
3674 /**
3675  * i40e_irq_affinity_notify - Callback for affinity changes
3676  * @notify: context as to what irq was changed
3677  * @mask: the new affinity mask
3678  *
3679  * This is a callback function used by the irq_set_affinity_notifier function
3680  * so that we may register to receive changes to the irq affinity masks.
3681  **/
3682 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3683 				     const cpumask_t *mask)
3684 {
3685 	struct i40e_q_vector *q_vector =
3686 		container_of(notify, struct i40e_q_vector, affinity_notify);
3687 
3688 	cpumask_copy(&q_vector->affinity_mask, mask);
3689 }
3690 
3691 /**
3692  * i40e_irq_affinity_release - Callback for affinity notifier release
3693  * @ref: internal core kernel usage
3694  *
3695  * This is a callback function used by the irq_set_affinity_notifier function
3696  * to inform the current notification subscriber that they will no longer
3697  * receive notifications.
3698  **/
3699 static void i40e_irq_affinity_release(struct kref *ref) {}
3700 
3701 /**
3702  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3703  * @vsi: the VSI being configured
3704  * @basename: name for the vector
3705  *
3706  * Allocates MSI-X vectors and requests interrupts from the kernel.
3707  **/
3708 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3709 {
3710 	int q_vectors = vsi->num_q_vectors;
3711 	struct i40e_pf *pf = vsi->back;
3712 	int base = vsi->base_vector;
3713 	int rx_int_idx = 0;
3714 	int tx_int_idx = 0;
3715 	int vector, err;
3716 	int irq_num;
3717 	int cpu;
3718 
3719 	for (vector = 0; vector < q_vectors; vector++) {
3720 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3721 
3722 		irq_num = pf->msix_entries[base + vector].vector;
3723 
3724 		if (q_vector->tx.ring && q_vector->rx.ring) {
3725 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3726 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3727 			tx_int_idx++;
3728 		} else if (q_vector->rx.ring) {
3729 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3730 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3731 		} else if (q_vector->tx.ring) {
3732 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3733 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3734 		} else {
3735 			/* skip this unused q_vector */
3736 			continue;
3737 		}
3738 		err = request_irq(irq_num,
3739 				  vsi->irq_handler,
3740 				  0,
3741 				  q_vector->name,
3742 				  q_vector);
3743 		if (err) {
3744 			dev_info(&pf->pdev->dev,
3745 				 "MSIX request_irq failed, error: %d\n", err);
3746 			goto free_queue_irqs;
3747 		}
3748 
3749 		/* register for affinity change notifications */
3750 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3751 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3752 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3753 		/* Spread affinity hints out across online CPUs.
3754 		 *
3755 		 * get_cpu_mask returns a static constant mask with
3756 		 * a permanent lifetime so it's ok to pass to
3757 		 * irq_set_affinity_hint without making a copy.
3758 		 */
3759 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3760 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3761 	}
3762 
3763 	vsi->irqs_ready = true;
3764 	return 0;
3765 
3766 free_queue_irqs:
3767 	while (vector) {
3768 		vector--;
3769 		irq_num = pf->msix_entries[base + vector].vector;
3770 		irq_set_affinity_notifier(irq_num, NULL);
3771 		irq_set_affinity_hint(irq_num, NULL);
3772 		free_irq(irq_num, &vsi->q_vectors[vector]);
3773 	}
3774 	return err;
3775 }
3776 
3777 /**
3778  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3779  * @vsi: the VSI being un-configured
3780  **/
3781 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3782 {
3783 	struct i40e_pf *pf = vsi->back;
3784 	struct i40e_hw *hw = &pf->hw;
3785 	int base = vsi->base_vector;
3786 	int i;
3787 
3788 	/* disable interrupt causation from each queue */
3789 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3790 		u32 val;
3791 
3792 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3793 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3794 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3795 
3796 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3797 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3798 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3799 
3800 		if (!i40e_enabled_xdp_vsi(vsi))
3801 			continue;
3802 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3803 	}
3804 
3805 	/* disable each interrupt */
3806 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3807 		for (i = vsi->base_vector;
3808 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3809 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3810 
3811 		i40e_flush(hw);
3812 		for (i = 0; i < vsi->num_q_vectors; i++)
3813 			synchronize_irq(pf->msix_entries[i + base].vector);
3814 	} else {
3815 		/* Legacy and MSI mode - this stops all interrupt handling */
3816 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3817 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3818 		i40e_flush(hw);
3819 		synchronize_irq(pf->pdev->irq);
3820 	}
3821 }
3822 
3823 /**
3824  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3825  * @vsi: the VSI being configured
3826  **/
3827 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3828 {
3829 	struct i40e_pf *pf = vsi->back;
3830 	int i;
3831 
3832 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3833 		for (i = 0; i < vsi->num_q_vectors; i++)
3834 			i40e_irq_dynamic_enable(vsi, i);
3835 	} else {
3836 		i40e_irq_dynamic_enable_icr0(pf);
3837 	}
3838 
3839 	i40e_flush(&pf->hw);
3840 	return 0;
3841 }
3842 
3843 /**
3844  * i40e_free_misc_vector - Free the vector that handles non-queue events
3845  * @pf: board private structure
3846  **/
3847 static void i40e_free_misc_vector(struct i40e_pf *pf)
3848 {
3849 	/* Disable ICR 0 */
3850 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3851 	i40e_flush(&pf->hw);
3852 
3853 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3854 		synchronize_irq(pf->msix_entries[0].vector);
3855 		free_irq(pf->msix_entries[0].vector, pf);
3856 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3857 	}
3858 }
3859 
3860 /**
3861  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3862  * @irq: interrupt number
3863  * @data: pointer to a q_vector
3864  *
3865  * This is the handler used for all MSI/Legacy interrupts, and deals
3866  * with both queue and non-queue interrupts.  This is also used in
3867  * MSIX mode to handle the non-queue interrupts.
3868  **/
3869 static irqreturn_t i40e_intr(int irq, void *data)
3870 {
3871 	struct i40e_pf *pf = (struct i40e_pf *)data;
3872 	struct i40e_hw *hw = &pf->hw;
3873 	irqreturn_t ret = IRQ_NONE;
3874 	u32 icr0, icr0_remaining;
3875 	u32 val, ena_mask;
3876 
3877 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3878 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3879 
3880 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3881 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3882 		goto enable_intr;
3883 
3884 	/* if interrupt but no bits showing, must be SWINT */
3885 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3886 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3887 		pf->sw_int_count++;
3888 
3889 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3890 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3891 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3892 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3893 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3894 	}
3895 
3896 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3897 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3898 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3899 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3900 
3901 		/* We do not have a way to disarm Queue causes while leaving
3902 		 * interrupt enabled for all other causes, ideally
3903 		 * interrupt should be disabled while we are in NAPI but
3904 		 * this is not a performance path and napi_schedule()
3905 		 * can deal with rescheduling.
3906 		 */
3907 		if (!test_bit(__I40E_DOWN, pf->state))
3908 			napi_schedule_irqoff(&q_vector->napi);
3909 	}
3910 
3911 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3912 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3913 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3914 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3915 	}
3916 
3917 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3918 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3919 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3920 	}
3921 
3922 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3923 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3924 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3925 	}
3926 
3927 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3928 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3929 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3930 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3931 		val = rd32(hw, I40E_GLGEN_RSTAT);
3932 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3933 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3934 		if (val == I40E_RESET_CORER) {
3935 			pf->corer_count++;
3936 		} else if (val == I40E_RESET_GLOBR) {
3937 			pf->globr_count++;
3938 		} else if (val == I40E_RESET_EMPR) {
3939 			pf->empr_count++;
3940 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3941 		}
3942 	}
3943 
3944 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3945 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3946 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3947 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3948 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3949 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3950 	}
3951 
3952 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3953 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3954 
3955 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
3956 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3957 			i40e_ptp_tx_hwtstamp(pf);
3958 		}
3959 	}
3960 
3961 	/* If a critical error is pending we have no choice but to reset the
3962 	 * device.
3963 	 * Report and mask out any remaining unexpected interrupts.
3964 	 */
3965 	icr0_remaining = icr0 & ena_mask;
3966 	if (icr0_remaining) {
3967 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3968 			 icr0_remaining);
3969 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
3970 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
3971 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
3972 			dev_info(&pf->pdev->dev, "device will be reset\n");
3973 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
3974 			i40e_service_event_schedule(pf);
3975 		}
3976 		ena_mask &= ~icr0_remaining;
3977 	}
3978 	ret = IRQ_HANDLED;
3979 
3980 enable_intr:
3981 	/* re-enable interrupt causes */
3982 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
3983 	if (!test_bit(__I40E_DOWN, pf->state)) {
3984 		i40e_service_event_schedule(pf);
3985 		i40e_irq_dynamic_enable_icr0(pf);
3986 	}
3987 
3988 	return ret;
3989 }
3990 
3991 /**
3992  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3993  * @tx_ring:  tx ring to clean
3994  * @budget:   how many cleans we're allowed
3995  *
3996  * Returns true if there's any budget left (e.g. the clean is finished)
3997  **/
3998 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3999 {
4000 	struct i40e_vsi *vsi = tx_ring->vsi;
4001 	u16 i = tx_ring->next_to_clean;
4002 	struct i40e_tx_buffer *tx_buf;
4003 	struct i40e_tx_desc *tx_desc;
4004 
4005 	tx_buf = &tx_ring->tx_bi[i];
4006 	tx_desc = I40E_TX_DESC(tx_ring, i);
4007 	i -= tx_ring->count;
4008 
4009 	do {
4010 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4011 
4012 		/* if next_to_watch is not set then there is no work pending */
4013 		if (!eop_desc)
4014 			break;
4015 
4016 		/* prevent any other reads prior to eop_desc */
4017 		smp_rmb();
4018 
4019 		/* if the descriptor isn't done, no work yet to do */
4020 		if (!(eop_desc->cmd_type_offset_bsz &
4021 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4022 			break;
4023 
4024 		/* clear next_to_watch to prevent false hangs */
4025 		tx_buf->next_to_watch = NULL;
4026 
4027 		tx_desc->buffer_addr = 0;
4028 		tx_desc->cmd_type_offset_bsz = 0;
4029 		/* move past filter desc */
4030 		tx_buf++;
4031 		tx_desc++;
4032 		i++;
4033 		if (unlikely(!i)) {
4034 			i -= tx_ring->count;
4035 			tx_buf = tx_ring->tx_bi;
4036 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4037 		}
4038 		/* unmap skb header data */
4039 		dma_unmap_single(tx_ring->dev,
4040 				 dma_unmap_addr(tx_buf, dma),
4041 				 dma_unmap_len(tx_buf, len),
4042 				 DMA_TO_DEVICE);
4043 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4044 			kfree(tx_buf->raw_buf);
4045 
4046 		tx_buf->raw_buf = NULL;
4047 		tx_buf->tx_flags = 0;
4048 		tx_buf->next_to_watch = NULL;
4049 		dma_unmap_len_set(tx_buf, len, 0);
4050 		tx_desc->buffer_addr = 0;
4051 		tx_desc->cmd_type_offset_bsz = 0;
4052 
4053 		/* move us past the eop_desc for start of next FD desc */
4054 		tx_buf++;
4055 		tx_desc++;
4056 		i++;
4057 		if (unlikely(!i)) {
4058 			i -= tx_ring->count;
4059 			tx_buf = tx_ring->tx_bi;
4060 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4061 		}
4062 
4063 		/* update budget accounting */
4064 		budget--;
4065 	} while (likely(budget));
4066 
4067 	i += tx_ring->count;
4068 	tx_ring->next_to_clean = i;
4069 
4070 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4071 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4072 
4073 	return budget > 0;
4074 }
4075 
4076 /**
4077  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4078  * @irq: interrupt number
4079  * @data: pointer to a q_vector
4080  **/
4081 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4082 {
4083 	struct i40e_q_vector *q_vector = data;
4084 	struct i40e_vsi *vsi;
4085 
4086 	if (!q_vector->tx.ring)
4087 		return IRQ_HANDLED;
4088 
4089 	vsi = q_vector->tx.ring->vsi;
4090 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4091 
4092 	return IRQ_HANDLED;
4093 }
4094 
4095 /**
4096  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4097  * @vsi: the VSI being configured
4098  * @v_idx: vector index
4099  * @qp_idx: queue pair index
4100  **/
4101 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4102 {
4103 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4104 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4105 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4106 
4107 	tx_ring->q_vector = q_vector;
4108 	tx_ring->next = q_vector->tx.ring;
4109 	q_vector->tx.ring = tx_ring;
4110 	q_vector->tx.count++;
4111 
4112 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4113 	if (i40e_enabled_xdp_vsi(vsi)) {
4114 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4115 
4116 		xdp_ring->q_vector = q_vector;
4117 		xdp_ring->next = q_vector->tx.ring;
4118 		q_vector->tx.ring = xdp_ring;
4119 		q_vector->tx.count++;
4120 	}
4121 
4122 	rx_ring->q_vector = q_vector;
4123 	rx_ring->next = q_vector->rx.ring;
4124 	q_vector->rx.ring = rx_ring;
4125 	q_vector->rx.count++;
4126 }
4127 
4128 /**
4129  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4130  * @vsi: the VSI being configured
4131  *
4132  * This function maps descriptor rings to the queue-specific vectors
4133  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4134  * one vector per queue pair, but on a constrained vector budget, we
4135  * group the queue pairs as "efficiently" as possible.
4136  **/
4137 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4138 {
4139 	int qp_remaining = vsi->num_queue_pairs;
4140 	int q_vectors = vsi->num_q_vectors;
4141 	int num_ringpairs;
4142 	int v_start = 0;
4143 	int qp_idx = 0;
4144 
4145 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4146 	 * group them so there are multiple queues per vector.
4147 	 * It is also important to go through all the vectors available to be
4148 	 * sure that if we don't use all the vectors, that the remaining vectors
4149 	 * are cleared. This is especially important when decreasing the
4150 	 * number of queues in use.
4151 	 */
4152 	for (; v_start < q_vectors; v_start++) {
4153 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4154 
4155 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4156 
4157 		q_vector->num_ringpairs = num_ringpairs;
4158 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4159 
4160 		q_vector->rx.count = 0;
4161 		q_vector->tx.count = 0;
4162 		q_vector->rx.ring = NULL;
4163 		q_vector->tx.ring = NULL;
4164 
4165 		while (num_ringpairs--) {
4166 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4167 			qp_idx++;
4168 			qp_remaining--;
4169 		}
4170 	}
4171 }
4172 
4173 /**
4174  * i40e_vsi_request_irq - Request IRQ from the OS
4175  * @vsi: the VSI being configured
4176  * @basename: name for the vector
4177  **/
4178 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4179 {
4180 	struct i40e_pf *pf = vsi->back;
4181 	int err;
4182 
4183 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4184 		err = i40e_vsi_request_irq_msix(vsi, basename);
4185 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4186 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4187 				  pf->int_name, pf);
4188 	else
4189 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4190 				  pf->int_name, pf);
4191 
4192 	if (err)
4193 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4194 
4195 	return err;
4196 }
4197 
4198 #ifdef CONFIG_NET_POLL_CONTROLLER
4199 /**
4200  * i40e_netpoll - A Polling 'interrupt' handler
4201  * @netdev: network interface device structure
4202  *
4203  * This is used by netconsole to send skbs without having to re-enable
4204  * interrupts.  It's not called while the normal interrupt routine is executing.
4205  **/
4206 static void i40e_netpoll(struct net_device *netdev)
4207 {
4208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4209 	struct i40e_vsi *vsi = np->vsi;
4210 	struct i40e_pf *pf = vsi->back;
4211 	int i;
4212 
4213 	/* if interface is down do nothing */
4214 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4215 		return;
4216 
4217 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4218 		for (i = 0; i < vsi->num_q_vectors; i++)
4219 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4220 	} else {
4221 		i40e_intr(pf->pdev->irq, netdev);
4222 	}
4223 }
4224 #endif
4225 
4226 #define I40E_QTX_ENA_WAIT_COUNT 50
4227 
4228 /**
4229  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4230  * @pf: the PF being configured
4231  * @pf_q: the PF queue
4232  * @enable: enable or disable state of the queue
4233  *
4234  * This routine will wait for the given Tx queue of the PF to reach the
4235  * enabled or disabled state.
4236  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4237  * multiple retries; else will return 0 in case of success.
4238  **/
4239 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4240 {
4241 	int i;
4242 	u32 tx_reg;
4243 
4244 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4245 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4246 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4247 			break;
4248 
4249 		usleep_range(10, 20);
4250 	}
4251 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4252 		return -ETIMEDOUT;
4253 
4254 	return 0;
4255 }
4256 
4257 /**
4258  * i40e_control_tx_q - Start or stop a particular Tx queue
4259  * @pf: the PF structure
4260  * @pf_q: the PF queue to configure
4261  * @enable: start or stop the queue
4262  *
4263  * This function enables or disables a single queue. Note that any delay
4264  * required after the operation is expected to be handled by the caller of
4265  * this function.
4266  **/
4267 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4268 {
4269 	struct i40e_hw *hw = &pf->hw;
4270 	u32 tx_reg;
4271 	int i;
4272 
4273 	/* warn the TX unit of coming changes */
4274 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4275 	if (!enable)
4276 		usleep_range(10, 20);
4277 
4278 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4279 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4280 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4281 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4282 			break;
4283 		usleep_range(1000, 2000);
4284 	}
4285 
4286 	/* Skip if the queue is already in the requested state */
4287 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4288 		return;
4289 
4290 	/* turn on/off the queue */
4291 	if (enable) {
4292 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4293 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4294 	} else {
4295 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4296 	}
4297 
4298 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4299 }
4300 
4301 /**
4302  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4303  * @seid: VSI SEID
4304  * @pf: the PF structure
4305  * @pf_q: the PF queue to configure
4306  * @is_xdp: true if the queue is used for XDP
4307  * @enable: start or stop the queue
4308  **/
4309 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4310 			   bool is_xdp, bool enable)
4311 {
4312 	int ret;
4313 
4314 	i40e_control_tx_q(pf, pf_q, enable);
4315 
4316 	/* wait for the change to finish */
4317 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4318 	if (ret) {
4319 		dev_info(&pf->pdev->dev,
4320 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4321 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4322 			 (enable ? "en" : "dis"));
4323 	}
4324 
4325 	return ret;
4326 }
4327 
4328 /**
4329  * i40e_vsi_control_tx - Start or stop a VSI's rings
4330  * @vsi: the VSI being configured
4331  * @enable: start or stop the rings
4332  **/
4333 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4334 {
4335 	struct i40e_pf *pf = vsi->back;
4336 	int i, pf_q, ret = 0;
4337 
4338 	pf_q = vsi->base_queue;
4339 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4340 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4341 					     pf_q,
4342 					     false /*is xdp*/, enable);
4343 		if (ret)
4344 			break;
4345 
4346 		if (!i40e_enabled_xdp_vsi(vsi))
4347 			continue;
4348 
4349 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4350 					     pf_q + vsi->alloc_queue_pairs,
4351 					     true /*is xdp*/, enable);
4352 		if (ret)
4353 			break;
4354 	}
4355 	return ret;
4356 }
4357 
4358 /**
4359  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4360  * @pf: the PF being configured
4361  * @pf_q: the PF queue
4362  * @enable: enable or disable state of the queue
4363  *
4364  * This routine will wait for the given Rx queue of the PF to reach the
4365  * enabled or disabled state.
4366  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4367  * multiple retries; else will return 0 in case of success.
4368  **/
4369 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4370 {
4371 	int i;
4372 	u32 rx_reg;
4373 
4374 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4375 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4376 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4377 			break;
4378 
4379 		usleep_range(10, 20);
4380 	}
4381 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4382 		return -ETIMEDOUT;
4383 
4384 	return 0;
4385 }
4386 
4387 /**
4388  * i40e_control_rx_q - Start or stop a particular Rx queue
4389  * @pf: the PF structure
4390  * @pf_q: the PF queue to configure
4391  * @enable: start or stop the queue
4392  *
4393  * This function enables or disables a single queue. Note that
4394  * any delay required after the operation is expected to be
4395  * handled by the caller of this function.
4396  **/
4397 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4398 {
4399 	struct i40e_hw *hw = &pf->hw;
4400 	u32 rx_reg;
4401 	int i;
4402 
4403 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4404 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4405 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4406 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4407 			break;
4408 		usleep_range(1000, 2000);
4409 	}
4410 
4411 	/* Skip if the queue is already in the requested state */
4412 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4413 		return;
4414 
4415 	/* turn on/off the queue */
4416 	if (enable)
4417 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4418 	else
4419 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4420 
4421 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4422 }
4423 
4424 /**
4425  * i40e_control_wait_rx_q
4426  * @pf: the PF structure
4427  * @pf_q: queue being configured
4428  * @enable: start or stop the rings
4429  *
4430  * This function enables or disables a single queue along with waiting
4431  * for the change to finish. The caller of this function should handle
4432  * the delays needed in the case of disabling queues.
4433  **/
4434 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4435 {
4436 	int ret = 0;
4437 
4438 	i40e_control_rx_q(pf, pf_q, enable);
4439 
4440 	/* wait for the change to finish */
4441 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4442 	if (ret)
4443 		return ret;
4444 
4445 	return ret;
4446 }
4447 
4448 /**
4449  * i40e_vsi_control_rx - Start or stop a VSI's rings
4450  * @vsi: the VSI being configured
4451  * @enable: start or stop the rings
4452  **/
4453 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4454 {
4455 	struct i40e_pf *pf = vsi->back;
4456 	int i, pf_q, ret = 0;
4457 
4458 	pf_q = vsi->base_queue;
4459 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4460 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4461 		if (ret) {
4462 			dev_info(&pf->pdev->dev,
4463 				 "VSI seid %d Rx ring %d %sable timeout\n",
4464 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4465 			break;
4466 		}
4467 	}
4468 
4469 	/* Due to HW errata, on Rx disable only, the register can indicate done
4470 	 * before it really is. Needs 50ms to be sure
4471 	 */
4472 	if (!enable)
4473 		mdelay(50);
4474 
4475 	return ret;
4476 }
4477 
4478 /**
4479  * i40e_vsi_start_rings - Start a VSI's rings
4480  * @vsi: the VSI being configured
4481  **/
4482 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4483 {
4484 	int ret = 0;
4485 
4486 	/* do rx first for enable and last for disable */
4487 	ret = i40e_vsi_control_rx(vsi, true);
4488 	if (ret)
4489 		return ret;
4490 	ret = i40e_vsi_control_tx(vsi, true);
4491 
4492 	return ret;
4493 }
4494 
4495 /**
4496  * i40e_vsi_stop_rings - Stop a VSI's rings
4497  * @vsi: the VSI being configured
4498  **/
4499 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4500 {
4501 	/* When port TX is suspended, don't wait */
4502 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4503 		return i40e_vsi_stop_rings_no_wait(vsi);
4504 
4505 	/* do rx first for enable and last for disable
4506 	 * Ignore return value, we need to shutdown whatever we can
4507 	 */
4508 	i40e_vsi_control_tx(vsi, false);
4509 	i40e_vsi_control_rx(vsi, false);
4510 }
4511 
4512 /**
4513  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4514  * @vsi: the VSI being shutdown
4515  *
4516  * This function stops all the rings for a VSI but does not delay to verify
4517  * that rings have been disabled. It is expected that the caller is shutting
4518  * down multiple VSIs at once and will delay together for all the VSIs after
4519  * initiating the shutdown. This is particularly useful for shutting down lots
4520  * of VFs together. Otherwise, a large delay can be incurred while configuring
4521  * each VSI in serial.
4522  **/
4523 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4524 {
4525 	struct i40e_pf *pf = vsi->back;
4526 	int i, pf_q;
4527 
4528 	pf_q = vsi->base_queue;
4529 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4530 		i40e_control_tx_q(pf, pf_q, false);
4531 		i40e_control_rx_q(pf, pf_q, false);
4532 	}
4533 }
4534 
4535 /**
4536  * i40e_vsi_free_irq - Free the irq association with the OS
4537  * @vsi: the VSI being configured
4538  **/
4539 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4540 {
4541 	struct i40e_pf *pf = vsi->back;
4542 	struct i40e_hw *hw = &pf->hw;
4543 	int base = vsi->base_vector;
4544 	u32 val, qp;
4545 	int i;
4546 
4547 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4548 		if (!vsi->q_vectors)
4549 			return;
4550 
4551 		if (!vsi->irqs_ready)
4552 			return;
4553 
4554 		vsi->irqs_ready = false;
4555 		for (i = 0; i < vsi->num_q_vectors; i++) {
4556 			int irq_num;
4557 			u16 vector;
4558 
4559 			vector = i + base;
4560 			irq_num = pf->msix_entries[vector].vector;
4561 
4562 			/* free only the irqs that were actually requested */
4563 			if (!vsi->q_vectors[i] ||
4564 			    !vsi->q_vectors[i]->num_ringpairs)
4565 				continue;
4566 
4567 			/* clear the affinity notifier in the IRQ descriptor */
4568 			irq_set_affinity_notifier(irq_num, NULL);
4569 			/* remove our suggested affinity mask for this IRQ */
4570 			irq_set_affinity_hint(irq_num, NULL);
4571 			synchronize_irq(irq_num);
4572 			free_irq(irq_num, vsi->q_vectors[i]);
4573 
4574 			/* Tear down the interrupt queue link list
4575 			 *
4576 			 * We know that they come in pairs and always
4577 			 * the Rx first, then the Tx.  To clear the
4578 			 * link list, stick the EOL value into the
4579 			 * next_q field of the registers.
4580 			 */
4581 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4582 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4583 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4584 			val |= I40E_QUEUE_END_OF_LIST
4585 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4586 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4587 
4588 			while (qp != I40E_QUEUE_END_OF_LIST) {
4589 				u32 next;
4590 
4591 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4592 
4593 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4594 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4595 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4596 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4597 
4598 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4599 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4600 
4601 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4602 
4603 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4604 
4605 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4606 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4607 
4608 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4609 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4610 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4611 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4612 
4613 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4614 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4615 
4616 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4617 				qp = next;
4618 			}
4619 		}
4620 	} else {
4621 		free_irq(pf->pdev->irq, pf);
4622 
4623 		val = rd32(hw, I40E_PFINT_LNKLST0);
4624 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4625 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4626 		val |= I40E_QUEUE_END_OF_LIST
4627 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4628 		wr32(hw, I40E_PFINT_LNKLST0, val);
4629 
4630 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4631 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4632 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4633 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4634 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4635 
4636 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4637 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4638 
4639 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4640 
4641 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4642 
4643 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4644 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4645 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4646 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4647 
4648 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4649 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4650 
4651 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4652 	}
4653 }
4654 
4655 /**
4656  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4657  * @vsi: the VSI being configured
4658  * @v_idx: Index of vector to be freed
4659  *
4660  * This function frees the memory allocated to the q_vector.  In addition if
4661  * NAPI is enabled it will delete any references to the NAPI struct prior
4662  * to freeing the q_vector.
4663  **/
4664 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4665 {
4666 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4667 	struct i40e_ring *ring;
4668 
4669 	if (!q_vector)
4670 		return;
4671 
4672 	/* disassociate q_vector from rings */
4673 	i40e_for_each_ring(ring, q_vector->tx)
4674 		ring->q_vector = NULL;
4675 
4676 	i40e_for_each_ring(ring, q_vector->rx)
4677 		ring->q_vector = NULL;
4678 
4679 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4680 	if (vsi->netdev)
4681 		netif_napi_del(&q_vector->napi);
4682 
4683 	vsi->q_vectors[v_idx] = NULL;
4684 
4685 	kfree_rcu(q_vector, rcu);
4686 }
4687 
4688 /**
4689  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4690  * @vsi: the VSI being un-configured
4691  *
4692  * This frees the memory allocated to the q_vectors and
4693  * deletes references to the NAPI struct.
4694  **/
4695 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4696 {
4697 	int v_idx;
4698 
4699 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4700 		i40e_free_q_vector(vsi, v_idx);
4701 }
4702 
4703 /**
4704  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4705  * @pf: board private structure
4706  **/
4707 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4708 {
4709 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4710 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4711 		pci_disable_msix(pf->pdev);
4712 		kfree(pf->msix_entries);
4713 		pf->msix_entries = NULL;
4714 		kfree(pf->irq_pile);
4715 		pf->irq_pile = NULL;
4716 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4717 		pci_disable_msi(pf->pdev);
4718 	}
4719 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4720 }
4721 
4722 /**
4723  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4724  * @pf: board private structure
4725  *
4726  * We go through and clear interrupt specific resources and reset the structure
4727  * to pre-load conditions
4728  **/
4729 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4730 {
4731 	int i;
4732 
4733 	i40e_free_misc_vector(pf);
4734 
4735 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4736 		      I40E_IWARP_IRQ_PILE_ID);
4737 
4738 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4739 	for (i = 0; i < pf->num_alloc_vsi; i++)
4740 		if (pf->vsi[i])
4741 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4742 	i40e_reset_interrupt_capability(pf);
4743 }
4744 
4745 /**
4746  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4747  * @vsi: the VSI being configured
4748  **/
4749 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4750 {
4751 	int q_idx;
4752 
4753 	if (!vsi->netdev)
4754 		return;
4755 
4756 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4757 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4758 
4759 		if (q_vector->rx.ring || q_vector->tx.ring)
4760 			napi_enable(&q_vector->napi);
4761 	}
4762 }
4763 
4764 /**
4765  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4766  * @vsi: the VSI being configured
4767  **/
4768 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4769 {
4770 	int q_idx;
4771 
4772 	if (!vsi->netdev)
4773 		return;
4774 
4775 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4776 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4777 
4778 		if (q_vector->rx.ring || q_vector->tx.ring)
4779 			napi_disable(&q_vector->napi);
4780 	}
4781 }
4782 
4783 /**
4784  * i40e_vsi_close - Shut down a VSI
4785  * @vsi: the vsi to be quelled
4786  **/
4787 static void i40e_vsi_close(struct i40e_vsi *vsi)
4788 {
4789 	struct i40e_pf *pf = vsi->back;
4790 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4791 		i40e_down(vsi);
4792 	i40e_vsi_free_irq(vsi);
4793 	i40e_vsi_free_tx_resources(vsi);
4794 	i40e_vsi_free_rx_resources(vsi);
4795 	vsi->current_netdev_flags = 0;
4796 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4797 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4798 		set_bit(__I40E_CLIENT_RESET, pf->state);
4799 }
4800 
4801 /**
4802  * i40e_quiesce_vsi - Pause a given VSI
4803  * @vsi: the VSI being paused
4804  **/
4805 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4806 {
4807 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4808 		return;
4809 
4810 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4811 	if (vsi->netdev && netif_running(vsi->netdev))
4812 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4813 	else
4814 		i40e_vsi_close(vsi);
4815 }
4816 
4817 /**
4818  * i40e_unquiesce_vsi - Resume a given VSI
4819  * @vsi: the VSI being resumed
4820  **/
4821 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4822 {
4823 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4824 		return;
4825 
4826 	if (vsi->netdev && netif_running(vsi->netdev))
4827 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4828 	else
4829 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4830 }
4831 
4832 /**
4833  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4834  * @pf: the PF
4835  **/
4836 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4837 {
4838 	int v;
4839 
4840 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4841 		if (pf->vsi[v])
4842 			i40e_quiesce_vsi(pf->vsi[v]);
4843 	}
4844 }
4845 
4846 /**
4847  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4848  * @pf: the PF
4849  **/
4850 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4851 {
4852 	int v;
4853 
4854 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4855 		if (pf->vsi[v])
4856 			i40e_unquiesce_vsi(pf->vsi[v]);
4857 	}
4858 }
4859 
4860 /**
4861  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4862  * @vsi: the VSI being configured
4863  *
4864  * Wait until all queues on a given VSI have been disabled.
4865  **/
4866 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4867 {
4868 	struct i40e_pf *pf = vsi->back;
4869 	int i, pf_q, ret;
4870 
4871 	pf_q = vsi->base_queue;
4872 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4873 		/* Check and wait for the Tx queue */
4874 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4875 		if (ret) {
4876 			dev_info(&pf->pdev->dev,
4877 				 "VSI seid %d Tx ring %d disable timeout\n",
4878 				 vsi->seid, pf_q);
4879 			return ret;
4880 		}
4881 
4882 		if (!i40e_enabled_xdp_vsi(vsi))
4883 			goto wait_rx;
4884 
4885 		/* Check and wait for the XDP Tx queue */
4886 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4887 				       false);
4888 		if (ret) {
4889 			dev_info(&pf->pdev->dev,
4890 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4891 				 vsi->seid, pf_q);
4892 			return ret;
4893 		}
4894 wait_rx:
4895 		/* Check and wait for the Rx queue */
4896 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4897 		if (ret) {
4898 			dev_info(&pf->pdev->dev,
4899 				 "VSI seid %d Rx ring %d disable timeout\n",
4900 				 vsi->seid, pf_q);
4901 			return ret;
4902 		}
4903 	}
4904 
4905 	return 0;
4906 }
4907 
4908 #ifdef CONFIG_I40E_DCB
4909 /**
4910  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4911  * @pf: the PF
4912  *
4913  * This function waits for the queues to be in disabled state for all the
4914  * VSIs that are managed by this PF.
4915  **/
4916 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4917 {
4918 	int v, ret = 0;
4919 
4920 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4921 		if (pf->vsi[v]) {
4922 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4923 			if (ret)
4924 				break;
4925 		}
4926 	}
4927 
4928 	return ret;
4929 }
4930 
4931 #endif
4932 
4933 /**
4934  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4935  * @pf: pointer to PF
4936  *
4937  * Get TC map for ISCSI PF type that will include iSCSI TC
4938  * and LAN TC.
4939  **/
4940 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4941 {
4942 	struct i40e_dcb_app_priority_table app;
4943 	struct i40e_hw *hw = &pf->hw;
4944 	u8 enabled_tc = 1; /* TC0 is always enabled */
4945 	u8 tc, i;
4946 	/* Get the iSCSI APP TLV */
4947 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4948 
4949 	for (i = 0; i < dcbcfg->numapps; i++) {
4950 		app = dcbcfg->app[i];
4951 		if (app.selector == I40E_APP_SEL_TCPIP &&
4952 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
4953 			tc = dcbcfg->etscfg.prioritytable[app.priority];
4954 			enabled_tc |= BIT(tc);
4955 			break;
4956 		}
4957 	}
4958 
4959 	return enabled_tc;
4960 }
4961 
4962 /**
4963  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
4964  * @dcbcfg: the corresponding DCBx configuration structure
4965  *
4966  * Return the number of TCs from given DCBx configuration
4967  **/
4968 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4969 {
4970 	int i, tc_unused = 0;
4971 	u8 num_tc = 0;
4972 	u8 ret = 0;
4973 
4974 	/* Scan the ETS Config Priority Table to find
4975 	 * traffic class enabled for a given priority
4976 	 * and create a bitmask of enabled TCs
4977 	 */
4978 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4979 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4980 
4981 	/* Now scan the bitmask to check for
4982 	 * contiguous TCs starting with TC0
4983 	 */
4984 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4985 		if (num_tc & BIT(i)) {
4986 			if (!tc_unused) {
4987 				ret++;
4988 			} else {
4989 				pr_err("Non-contiguous TC - Disabling DCB\n");
4990 				return 1;
4991 			}
4992 		} else {
4993 			tc_unused = 1;
4994 		}
4995 	}
4996 
4997 	/* There is always at least TC0 */
4998 	if (!ret)
4999 		ret = 1;
5000 
5001 	return ret;
5002 }
5003 
5004 /**
5005  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5006  * @dcbcfg: the corresponding DCBx configuration structure
5007  *
5008  * Query the current DCB configuration and return the number of
5009  * traffic classes enabled from the given DCBX config
5010  **/
5011 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5012 {
5013 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5014 	u8 enabled_tc = 1;
5015 	u8 i;
5016 
5017 	for (i = 0; i < num_tc; i++)
5018 		enabled_tc |= BIT(i);
5019 
5020 	return enabled_tc;
5021 }
5022 
5023 /**
5024  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5025  * @pf: PF being queried
5026  *
5027  * Query the current MQPRIO configuration and return the number of
5028  * traffic classes enabled.
5029  **/
5030 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5031 {
5032 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5033 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5034 	u8 enabled_tc = 1, i;
5035 
5036 	for (i = 1; i < num_tc; i++)
5037 		enabled_tc |= BIT(i);
5038 	return enabled_tc;
5039 }
5040 
5041 /**
5042  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5043  * @pf: PF being queried
5044  *
5045  * Return number of traffic classes enabled for the given PF
5046  **/
5047 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5048 {
5049 	struct i40e_hw *hw = &pf->hw;
5050 	u8 i, enabled_tc = 1;
5051 	u8 num_tc = 0;
5052 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5053 
5054 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5055 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5056 
5057 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5058 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5059 		return 1;
5060 
5061 	/* SFP mode will be enabled for all TCs on port */
5062 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5063 		return i40e_dcb_get_num_tc(dcbcfg);
5064 
5065 	/* MFP mode return count of enabled TCs for this PF */
5066 	if (pf->hw.func_caps.iscsi)
5067 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5068 	else
5069 		return 1; /* Only TC0 */
5070 
5071 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5072 		if (enabled_tc & BIT(i))
5073 			num_tc++;
5074 	}
5075 	return num_tc;
5076 }
5077 
5078 /**
5079  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5080  * @pf: PF being queried
5081  *
5082  * Return a bitmap for enabled traffic classes for this PF.
5083  **/
5084 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5085 {
5086 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5087 		return i40e_mqprio_get_enabled_tc(pf);
5088 
5089 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5090 	 * default TC
5091 	 */
5092 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5093 		return I40E_DEFAULT_TRAFFIC_CLASS;
5094 
5095 	/* SFP mode we want PF to be enabled for all TCs */
5096 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5097 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5098 
5099 	/* MFP enabled and iSCSI PF type */
5100 	if (pf->hw.func_caps.iscsi)
5101 		return i40e_get_iscsi_tc_map(pf);
5102 	else
5103 		return I40E_DEFAULT_TRAFFIC_CLASS;
5104 }
5105 
5106 /**
5107  * i40e_vsi_get_bw_info - Query VSI BW Information
5108  * @vsi: the VSI being queried
5109  *
5110  * Returns 0 on success, negative value on failure
5111  **/
5112 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5113 {
5114 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5115 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5116 	struct i40e_pf *pf = vsi->back;
5117 	struct i40e_hw *hw = &pf->hw;
5118 	i40e_status ret;
5119 	u32 tc_bw_max;
5120 	int i;
5121 
5122 	/* Get the VSI level BW configuration */
5123 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5124 	if (ret) {
5125 		dev_info(&pf->pdev->dev,
5126 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5127 			 i40e_stat_str(&pf->hw, ret),
5128 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5129 		return -EINVAL;
5130 	}
5131 
5132 	/* Get the VSI level BW configuration per TC */
5133 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5134 					       NULL);
5135 	if (ret) {
5136 		dev_info(&pf->pdev->dev,
5137 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5138 			 i40e_stat_str(&pf->hw, ret),
5139 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5140 		return -EINVAL;
5141 	}
5142 
5143 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5144 		dev_info(&pf->pdev->dev,
5145 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5146 			 bw_config.tc_valid_bits,
5147 			 bw_ets_config.tc_valid_bits);
5148 		/* Still continuing */
5149 	}
5150 
5151 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5152 	vsi->bw_max_quanta = bw_config.max_bw;
5153 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5154 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5155 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5156 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5157 		vsi->bw_ets_limit_credits[i] =
5158 					le16_to_cpu(bw_ets_config.credits[i]);
5159 		/* 3 bits out of 4 for each TC */
5160 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5161 	}
5162 
5163 	return 0;
5164 }
5165 
5166 /**
5167  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5168  * @vsi: the VSI being configured
5169  * @enabled_tc: TC bitmap
5170  * @bw_share: BW shared credits per TC
5171  *
5172  * Returns 0 on success, negative value on failure
5173  **/
5174 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5175 				       u8 *bw_share)
5176 {
5177 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5178 	struct i40e_pf *pf = vsi->back;
5179 	i40e_status ret;
5180 	int i;
5181 
5182 	/* There is no need to reset BW when mqprio mode is on.  */
5183 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5184 		return 0;
5185 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5186 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5187 		if (ret)
5188 			dev_info(&pf->pdev->dev,
5189 				 "Failed to reset tx rate for vsi->seid %u\n",
5190 				 vsi->seid);
5191 		return ret;
5192 	}
5193 	bw_data.tc_valid_bits = enabled_tc;
5194 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5195 		bw_data.tc_bw_credits[i] = bw_share[i];
5196 
5197 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5198 	if (ret) {
5199 		dev_info(&pf->pdev->dev,
5200 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5201 			 pf->hw.aq.asq_last_status);
5202 		return -EINVAL;
5203 	}
5204 
5205 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5206 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5207 
5208 	return 0;
5209 }
5210 
5211 /**
5212  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5213  * @vsi: the VSI being configured
5214  * @enabled_tc: TC map to be enabled
5215  *
5216  **/
5217 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5218 {
5219 	struct net_device *netdev = vsi->netdev;
5220 	struct i40e_pf *pf = vsi->back;
5221 	struct i40e_hw *hw = &pf->hw;
5222 	u8 netdev_tc = 0;
5223 	int i;
5224 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5225 
5226 	if (!netdev)
5227 		return;
5228 
5229 	if (!enabled_tc) {
5230 		netdev_reset_tc(netdev);
5231 		return;
5232 	}
5233 
5234 	/* Set up actual enabled TCs on the VSI */
5235 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5236 		return;
5237 
5238 	/* set per TC queues for the VSI */
5239 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5240 		/* Only set TC queues for enabled tcs
5241 		 *
5242 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5243 		 * enabled_tc bitmap would be 0x00001001; the driver
5244 		 * will set the numtc for netdev as 2 that will be
5245 		 * referenced by the netdev layer as TC 0 and 1.
5246 		 */
5247 		if (vsi->tc_config.enabled_tc & BIT(i))
5248 			netdev_set_tc_queue(netdev,
5249 					vsi->tc_config.tc_info[i].netdev_tc,
5250 					vsi->tc_config.tc_info[i].qcount,
5251 					vsi->tc_config.tc_info[i].qoffset);
5252 	}
5253 
5254 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5255 		return;
5256 
5257 	/* Assign UP2TC map for the VSI */
5258 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5259 		/* Get the actual TC# for the UP */
5260 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5261 		/* Get the mapped netdev TC# for the UP */
5262 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5263 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5264 	}
5265 }
5266 
5267 /**
5268  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5269  * @vsi: the VSI being configured
5270  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5271  **/
5272 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5273 				      struct i40e_vsi_context *ctxt)
5274 {
5275 	/* copy just the sections touched not the entire info
5276 	 * since not all sections are valid as returned by
5277 	 * update vsi params
5278 	 */
5279 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5280 	memcpy(&vsi->info.queue_mapping,
5281 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5282 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5283 	       sizeof(vsi->info.tc_mapping));
5284 }
5285 
5286 /**
5287  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5288  * @vsi: VSI to be configured
5289  * @enabled_tc: TC bitmap
5290  *
5291  * This configures a particular VSI for TCs that are mapped to the
5292  * given TC bitmap. It uses default bandwidth share for TCs across
5293  * VSIs to configure TC for a particular VSI.
5294  *
5295  * NOTE:
5296  * It is expected that the VSI queues have been quisced before calling
5297  * this function.
5298  **/
5299 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5300 {
5301 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5302 	struct i40e_pf *pf = vsi->back;
5303 	struct i40e_hw *hw = &pf->hw;
5304 	struct i40e_vsi_context ctxt;
5305 	int ret = 0;
5306 	int i;
5307 
5308 	/* Check if enabled_tc is same as existing or new TCs */
5309 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5310 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5311 		return ret;
5312 
5313 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5314 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5315 		if (enabled_tc & BIT(i))
5316 			bw_share[i] = 1;
5317 	}
5318 
5319 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5320 	if (ret) {
5321 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5322 
5323 		dev_info(&pf->pdev->dev,
5324 			 "Failed configuring TC map %d for VSI %d\n",
5325 			 enabled_tc, vsi->seid);
5326 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5327 						  &bw_config, NULL);
5328 		if (ret) {
5329 			dev_info(&pf->pdev->dev,
5330 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5331 				 i40e_stat_str(hw, ret),
5332 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5333 			goto out;
5334 		}
5335 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5336 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5337 
5338 			if (!valid_tc)
5339 				valid_tc = bw_config.tc_valid_bits;
5340 			/* Always enable TC0, no matter what */
5341 			valid_tc |= 1;
5342 			dev_info(&pf->pdev->dev,
5343 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5344 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5345 			enabled_tc = valid_tc;
5346 		}
5347 
5348 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5349 		if (ret) {
5350 			dev_err(&pf->pdev->dev,
5351 				"Unable to  configure TC map %d for VSI %d\n",
5352 				enabled_tc, vsi->seid);
5353 			goto out;
5354 		}
5355 	}
5356 
5357 	/* Update Queue Pairs Mapping for currently enabled UPs */
5358 	ctxt.seid = vsi->seid;
5359 	ctxt.pf_num = vsi->back->hw.pf_id;
5360 	ctxt.vf_num = 0;
5361 	ctxt.uplink_seid = vsi->uplink_seid;
5362 	ctxt.info = vsi->info;
5363 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5364 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5365 		if (ret)
5366 			goto out;
5367 	} else {
5368 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5369 	}
5370 
5371 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5372 	 * queues changed.
5373 	 */
5374 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5375 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5376 				      vsi->num_queue_pairs);
5377 		ret = i40e_vsi_config_rss(vsi);
5378 		if (ret) {
5379 			dev_info(&vsi->back->pdev->dev,
5380 				 "Failed to reconfig rss for num_queues\n");
5381 			return ret;
5382 		}
5383 		vsi->reconfig_rss = false;
5384 	}
5385 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5386 		ctxt.info.valid_sections |=
5387 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5388 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5389 	}
5390 
5391 	/* Update the VSI after updating the VSI queue-mapping
5392 	 * information
5393 	 */
5394 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5395 	if (ret) {
5396 		dev_info(&pf->pdev->dev,
5397 			 "Update vsi tc config failed, err %s aq_err %s\n",
5398 			 i40e_stat_str(hw, ret),
5399 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5400 		goto out;
5401 	}
5402 	/* update the local VSI info with updated queue map */
5403 	i40e_vsi_update_queue_map(vsi, &ctxt);
5404 	vsi->info.valid_sections = 0;
5405 
5406 	/* Update current VSI BW information */
5407 	ret = i40e_vsi_get_bw_info(vsi);
5408 	if (ret) {
5409 		dev_info(&pf->pdev->dev,
5410 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5411 			 i40e_stat_str(hw, ret),
5412 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5413 		goto out;
5414 	}
5415 
5416 	/* Update the netdev TC setup */
5417 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5418 out:
5419 	return ret;
5420 }
5421 
5422 /**
5423  * i40e_get_link_speed - Returns link speed for the interface
5424  * @vsi: VSI to be configured
5425  *
5426  **/
5427 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5428 {
5429 	struct i40e_pf *pf = vsi->back;
5430 
5431 	switch (pf->hw.phy.link_info.link_speed) {
5432 	case I40E_LINK_SPEED_40GB:
5433 		return 40000;
5434 	case I40E_LINK_SPEED_25GB:
5435 		return 25000;
5436 	case I40E_LINK_SPEED_20GB:
5437 		return 20000;
5438 	case I40E_LINK_SPEED_10GB:
5439 		return 10000;
5440 	case I40E_LINK_SPEED_1GB:
5441 		return 1000;
5442 	default:
5443 		return -EINVAL;
5444 	}
5445 }
5446 
5447 /**
5448  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5449  * @vsi: VSI to be configured
5450  * @seid: seid of the channel/VSI
5451  * @max_tx_rate: max TX rate to be configured as BW limit
5452  *
5453  * Helper function to set BW limit for a given VSI
5454  **/
5455 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5456 {
5457 	struct i40e_pf *pf = vsi->back;
5458 	u64 credits = 0;
5459 	int speed = 0;
5460 	int ret = 0;
5461 
5462 	speed = i40e_get_link_speed(vsi);
5463 	if (max_tx_rate > speed) {
5464 		dev_err(&pf->pdev->dev,
5465 			"Invalid max tx rate %llu specified for VSI seid %d.",
5466 			max_tx_rate, seid);
5467 		return -EINVAL;
5468 	}
5469 	if (max_tx_rate && max_tx_rate < 50) {
5470 		dev_warn(&pf->pdev->dev,
5471 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5472 		max_tx_rate = 50;
5473 	}
5474 
5475 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5476 	credits = max_tx_rate;
5477 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5478 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5479 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5480 	if (ret)
5481 		dev_err(&pf->pdev->dev,
5482 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5483 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5484 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5485 	return ret;
5486 }
5487 
5488 /**
5489  * i40e_remove_queue_channels - Remove queue channels for the TCs
5490  * @vsi: VSI to be configured
5491  *
5492  * Remove queue channels for the TCs
5493  **/
5494 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5495 {
5496 	enum i40e_admin_queue_err last_aq_status;
5497 	struct i40e_cloud_filter *cfilter;
5498 	struct i40e_channel *ch, *ch_tmp;
5499 	struct i40e_pf *pf = vsi->back;
5500 	struct hlist_node *node;
5501 	int ret, i;
5502 
5503 	/* Reset rss size that was stored when reconfiguring rss for
5504 	 * channel VSIs with non-power-of-2 queue count.
5505 	 */
5506 	vsi->current_rss_size = 0;
5507 
5508 	/* perform cleanup for channels if they exist */
5509 	if (list_empty(&vsi->ch_list))
5510 		return;
5511 
5512 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5513 		struct i40e_vsi *p_vsi;
5514 
5515 		list_del(&ch->list);
5516 		p_vsi = ch->parent_vsi;
5517 		if (!p_vsi || !ch->initialized) {
5518 			kfree(ch);
5519 			continue;
5520 		}
5521 		/* Reset queue contexts */
5522 		for (i = 0; i < ch->num_queue_pairs; i++) {
5523 			struct i40e_ring *tx_ring, *rx_ring;
5524 			u16 pf_q;
5525 
5526 			pf_q = ch->base_queue + i;
5527 			tx_ring = vsi->tx_rings[pf_q];
5528 			tx_ring->ch = NULL;
5529 
5530 			rx_ring = vsi->rx_rings[pf_q];
5531 			rx_ring->ch = NULL;
5532 		}
5533 
5534 		/* Reset BW configured for this VSI via mqprio */
5535 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5536 		if (ret)
5537 			dev_info(&vsi->back->pdev->dev,
5538 				 "Failed to reset tx rate for ch->seid %u\n",
5539 				 ch->seid);
5540 
5541 		/* delete cloud filters associated with this channel */
5542 		hlist_for_each_entry_safe(cfilter, node,
5543 					  &pf->cloud_filter_list, cloud_node) {
5544 			if (cfilter->seid != ch->seid)
5545 				continue;
5546 
5547 			hash_del(&cfilter->cloud_node);
5548 			if (cfilter->dst_port)
5549 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5550 									cfilter,
5551 									false);
5552 			else
5553 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5554 								false);
5555 			last_aq_status = pf->hw.aq.asq_last_status;
5556 			if (ret)
5557 				dev_info(&pf->pdev->dev,
5558 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5559 					 i40e_stat_str(&pf->hw, ret),
5560 					 i40e_aq_str(&pf->hw, last_aq_status));
5561 			kfree(cfilter);
5562 		}
5563 
5564 		/* delete VSI from FW */
5565 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5566 					     NULL);
5567 		if (ret)
5568 			dev_err(&vsi->back->pdev->dev,
5569 				"unable to remove channel (%d) for parent VSI(%d)\n",
5570 				ch->seid, p_vsi->seid);
5571 		kfree(ch);
5572 	}
5573 	INIT_LIST_HEAD(&vsi->ch_list);
5574 }
5575 
5576 /**
5577  * i40e_is_any_channel - channel exist or not
5578  * @vsi: ptr to VSI to which channels are associated with
5579  *
5580  * Returns true or false if channel(s) exist for associated VSI or not
5581  **/
5582 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5583 {
5584 	struct i40e_channel *ch, *ch_tmp;
5585 
5586 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5587 		if (ch->initialized)
5588 			return true;
5589 	}
5590 
5591 	return false;
5592 }
5593 
5594 /**
5595  * i40e_get_max_queues_for_channel
5596  * @vsi: ptr to VSI to which channels are associated with
5597  *
5598  * Helper function which returns max value among the queue counts set on the
5599  * channels/TCs created.
5600  **/
5601 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5602 {
5603 	struct i40e_channel *ch, *ch_tmp;
5604 	int max = 0;
5605 
5606 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5607 		if (!ch->initialized)
5608 			continue;
5609 		if (ch->num_queue_pairs > max)
5610 			max = ch->num_queue_pairs;
5611 	}
5612 
5613 	return max;
5614 }
5615 
5616 /**
5617  * i40e_validate_num_queues - validate num_queues w.r.t channel
5618  * @pf: ptr to PF device
5619  * @num_queues: number of queues
5620  * @vsi: the parent VSI
5621  * @reconfig_rss: indicates should the RSS be reconfigured or not
5622  *
5623  * This function validates number of queues in the context of new channel
5624  * which is being established and determines if RSS should be reconfigured
5625  * or not for parent VSI.
5626  **/
5627 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5628 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5629 {
5630 	int max_ch_queues;
5631 
5632 	if (!reconfig_rss)
5633 		return -EINVAL;
5634 
5635 	*reconfig_rss = false;
5636 	if (vsi->current_rss_size) {
5637 		if (num_queues > vsi->current_rss_size) {
5638 			dev_dbg(&pf->pdev->dev,
5639 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5640 				num_queues, vsi->current_rss_size);
5641 			return -EINVAL;
5642 		} else if ((num_queues < vsi->current_rss_size) &&
5643 			   (!is_power_of_2(num_queues))) {
5644 			dev_dbg(&pf->pdev->dev,
5645 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5646 				num_queues, vsi->current_rss_size);
5647 			return -EINVAL;
5648 		}
5649 	}
5650 
5651 	if (!is_power_of_2(num_queues)) {
5652 		/* Find the max num_queues configured for channel if channel
5653 		 * exist.
5654 		 * if channel exist, then enforce 'num_queues' to be more than
5655 		 * max ever queues configured for channel.
5656 		 */
5657 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5658 		if (num_queues < max_ch_queues) {
5659 			dev_dbg(&pf->pdev->dev,
5660 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5661 				num_queues, max_ch_queues);
5662 			return -EINVAL;
5663 		}
5664 		*reconfig_rss = true;
5665 	}
5666 
5667 	return 0;
5668 }
5669 
5670 /**
5671  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5672  * @vsi: the VSI being setup
5673  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5674  *
5675  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5676  **/
5677 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5678 {
5679 	struct i40e_pf *pf = vsi->back;
5680 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5681 	struct i40e_hw *hw = &pf->hw;
5682 	int local_rss_size;
5683 	u8 *lut;
5684 	int ret;
5685 
5686 	if (!vsi->rss_size)
5687 		return -EINVAL;
5688 
5689 	if (rss_size > vsi->rss_size)
5690 		return -EINVAL;
5691 
5692 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5693 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5694 	if (!lut)
5695 		return -ENOMEM;
5696 
5697 	/* Ignoring user configured lut if there is one */
5698 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5699 
5700 	/* Use user configured hash key if there is one, otherwise
5701 	 * use default.
5702 	 */
5703 	if (vsi->rss_hkey_user)
5704 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5705 	else
5706 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5707 
5708 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5709 	if (ret) {
5710 		dev_info(&pf->pdev->dev,
5711 			 "Cannot set RSS lut, err %s aq_err %s\n",
5712 			 i40e_stat_str(hw, ret),
5713 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5714 		kfree(lut);
5715 		return ret;
5716 	}
5717 	kfree(lut);
5718 
5719 	/* Do the update w.r.t. storing rss_size */
5720 	if (!vsi->orig_rss_size)
5721 		vsi->orig_rss_size = vsi->rss_size;
5722 	vsi->current_rss_size = local_rss_size;
5723 
5724 	return ret;
5725 }
5726 
5727 /**
5728  * i40e_channel_setup_queue_map - Setup a channel queue map
5729  * @pf: ptr to PF device
5730  * @vsi: the VSI being setup
5731  * @ctxt: VSI context structure
5732  * @ch: ptr to channel structure
5733  *
5734  * Setup queue map for a specific channel
5735  **/
5736 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5737 					 struct i40e_vsi_context *ctxt,
5738 					 struct i40e_channel *ch)
5739 {
5740 	u16 qcount, qmap, sections = 0;
5741 	u8 offset = 0;
5742 	int pow;
5743 
5744 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5745 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5746 
5747 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5748 	ch->num_queue_pairs = qcount;
5749 
5750 	/* find the next higher power-of-2 of num queue pairs */
5751 	pow = ilog2(qcount);
5752 	if (!is_power_of_2(qcount))
5753 		pow++;
5754 
5755 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5756 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5757 
5758 	/* Setup queue TC[0].qmap for given VSI context */
5759 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5760 
5761 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5762 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5763 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5764 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5765 }
5766 
5767 /**
5768  * i40e_add_channel - add a channel by adding VSI
5769  * @pf: ptr to PF device
5770  * @uplink_seid: underlying HW switching element (VEB) ID
5771  * @ch: ptr to channel structure
5772  *
5773  * Add a channel (VSI) using add_vsi and queue_map
5774  **/
5775 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5776 			    struct i40e_channel *ch)
5777 {
5778 	struct i40e_hw *hw = &pf->hw;
5779 	struct i40e_vsi_context ctxt;
5780 	u8 enabled_tc = 0x1; /* TC0 enabled */
5781 	int ret;
5782 
5783 	if (ch->type != I40E_VSI_VMDQ2) {
5784 		dev_info(&pf->pdev->dev,
5785 			 "add new vsi failed, ch->type %d\n", ch->type);
5786 		return -EINVAL;
5787 	}
5788 
5789 	memset(&ctxt, 0, sizeof(ctxt));
5790 	ctxt.pf_num = hw->pf_id;
5791 	ctxt.vf_num = 0;
5792 	ctxt.uplink_seid = uplink_seid;
5793 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5794 	if (ch->type == I40E_VSI_VMDQ2)
5795 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5796 
5797 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5798 		ctxt.info.valid_sections |=
5799 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5800 		ctxt.info.switch_id =
5801 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5802 	}
5803 
5804 	/* Set queue map for a given VSI context */
5805 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5806 
5807 	/* Now time to create VSI */
5808 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5809 	if (ret) {
5810 		dev_info(&pf->pdev->dev,
5811 			 "add new vsi failed, err %s aq_err %s\n",
5812 			 i40e_stat_str(&pf->hw, ret),
5813 			 i40e_aq_str(&pf->hw,
5814 				     pf->hw.aq.asq_last_status));
5815 		return -ENOENT;
5816 	}
5817 
5818 	/* Success, update channel */
5819 	ch->enabled_tc = enabled_tc;
5820 	ch->seid = ctxt.seid;
5821 	ch->vsi_number = ctxt.vsi_number;
5822 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5823 
5824 	/* copy just the sections touched not the entire info
5825 	 * since not all sections are valid as returned by
5826 	 * update vsi params
5827 	 */
5828 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5829 	memcpy(&ch->info.queue_mapping,
5830 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5831 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5832 	       sizeof(ctxt.info.tc_mapping));
5833 
5834 	return 0;
5835 }
5836 
5837 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5838 				  u8 *bw_share)
5839 {
5840 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5841 	i40e_status ret;
5842 	int i;
5843 
5844 	bw_data.tc_valid_bits = ch->enabled_tc;
5845 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5846 		bw_data.tc_bw_credits[i] = bw_share[i];
5847 
5848 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5849 				       &bw_data, NULL);
5850 	if (ret) {
5851 		dev_info(&vsi->back->pdev->dev,
5852 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5853 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5854 		return -EINVAL;
5855 	}
5856 
5857 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5858 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5859 
5860 	return 0;
5861 }
5862 
5863 /**
5864  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5865  * @pf: ptr to PF device
5866  * @vsi: the VSI being setup
5867  * @ch: ptr to channel structure
5868  *
5869  * Configure TX rings associated with channel (VSI) since queues are being
5870  * from parent VSI.
5871  **/
5872 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5873 				       struct i40e_vsi *vsi,
5874 				       struct i40e_channel *ch)
5875 {
5876 	i40e_status ret;
5877 	int i;
5878 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5879 
5880 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5881 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5882 		if (ch->enabled_tc & BIT(i))
5883 			bw_share[i] = 1;
5884 	}
5885 
5886 	/* configure BW for new VSI */
5887 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5888 	if (ret) {
5889 		dev_info(&vsi->back->pdev->dev,
5890 			 "Failed configuring TC map %d for channel (seid %u)\n",
5891 			 ch->enabled_tc, ch->seid);
5892 		return ret;
5893 	}
5894 
5895 	for (i = 0; i < ch->num_queue_pairs; i++) {
5896 		struct i40e_ring *tx_ring, *rx_ring;
5897 		u16 pf_q;
5898 
5899 		pf_q = ch->base_queue + i;
5900 
5901 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5902 		 * context
5903 		 */
5904 		tx_ring = vsi->tx_rings[pf_q];
5905 		tx_ring->ch = ch;
5906 
5907 		/* Get the RX ring ptr */
5908 		rx_ring = vsi->rx_rings[pf_q];
5909 		rx_ring->ch = ch;
5910 	}
5911 
5912 	return 0;
5913 }
5914 
5915 /**
5916  * i40e_setup_hw_channel - setup new channel
5917  * @pf: ptr to PF device
5918  * @vsi: the VSI being setup
5919  * @ch: ptr to channel structure
5920  * @uplink_seid: underlying HW switching element (VEB) ID
5921  * @type: type of channel to be created (VMDq2/VF)
5922  *
5923  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5924  * and configures TX rings accordingly
5925  **/
5926 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5927 					struct i40e_vsi *vsi,
5928 					struct i40e_channel *ch,
5929 					u16 uplink_seid, u8 type)
5930 {
5931 	int ret;
5932 
5933 	ch->initialized = false;
5934 	ch->base_queue = vsi->next_base_queue;
5935 	ch->type = type;
5936 
5937 	/* Proceed with creation of channel (VMDq2) VSI */
5938 	ret = i40e_add_channel(pf, uplink_seid, ch);
5939 	if (ret) {
5940 		dev_info(&pf->pdev->dev,
5941 			 "failed to add_channel using uplink_seid %u\n",
5942 			 uplink_seid);
5943 		return ret;
5944 	}
5945 
5946 	/* Mark the successful creation of channel */
5947 	ch->initialized = true;
5948 
5949 	/* Reconfigure TX queues using QTX_CTL register */
5950 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5951 	if (ret) {
5952 		dev_info(&pf->pdev->dev,
5953 			 "failed to configure TX rings for channel %u\n",
5954 			 ch->seid);
5955 		return ret;
5956 	}
5957 
5958 	/* update 'next_base_queue' */
5959 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5960 	dev_dbg(&pf->pdev->dev,
5961 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5962 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
5963 		ch->num_queue_pairs,
5964 		vsi->next_base_queue);
5965 	return ret;
5966 }
5967 
5968 /**
5969  * i40e_setup_channel - setup new channel using uplink element
5970  * @pf: ptr to PF device
5971  * @type: type of channel to be created (VMDq2/VF)
5972  * @uplink_seid: underlying HW switching element (VEB) ID
5973  * @ch: ptr to channel structure
5974  *
5975  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5976  * and uplink switching element (uplink_seid)
5977  **/
5978 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5979 			       struct i40e_channel *ch)
5980 {
5981 	u8 vsi_type;
5982 	u16 seid;
5983 	int ret;
5984 
5985 	if (vsi->type == I40E_VSI_MAIN) {
5986 		vsi_type = I40E_VSI_VMDQ2;
5987 	} else {
5988 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5989 			vsi->type);
5990 		return false;
5991 	}
5992 
5993 	/* underlying switching element */
5994 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5995 
5996 	/* create channel (VSI), configure TX rings */
5997 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5998 	if (ret) {
5999 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6000 		return false;
6001 	}
6002 
6003 	return ch->initialized ? true : false;
6004 }
6005 
6006 /**
6007  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6008  * @vsi: ptr to VSI which has PF backing
6009  *
6010  * Sets up switch mode correctly if it needs to be changed and perform
6011  * what are allowed modes.
6012  **/
6013 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6014 {
6015 	u8 mode;
6016 	struct i40e_pf *pf = vsi->back;
6017 	struct i40e_hw *hw = &pf->hw;
6018 	int ret;
6019 
6020 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6021 	if (ret)
6022 		return -EINVAL;
6023 
6024 	if (hw->dev_caps.switch_mode) {
6025 		/* if switch mode is set, support mode2 (non-tunneled for
6026 		 * cloud filter) for now
6027 		 */
6028 		u32 switch_mode = hw->dev_caps.switch_mode &
6029 				  I40E_SWITCH_MODE_MASK;
6030 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6031 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6032 				return 0;
6033 			dev_err(&pf->pdev->dev,
6034 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6035 				hw->dev_caps.switch_mode);
6036 			return -EINVAL;
6037 		}
6038 	}
6039 
6040 	/* Set Bit 7 to be valid */
6041 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6042 
6043 	/* Set L4type for TCP support */
6044 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6045 
6046 	/* Set cloud filter mode */
6047 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6048 
6049 	/* Prep mode field for set_switch_config */
6050 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6051 					pf->last_sw_conf_valid_flags,
6052 					mode, NULL);
6053 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6054 		dev_err(&pf->pdev->dev,
6055 			"couldn't set switch config bits, err %s aq_err %s\n",
6056 			i40e_stat_str(hw, ret),
6057 			i40e_aq_str(hw,
6058 				    hw->aq.asq_last_status));
6059 
6060 	return ret;
6061 }
6062 
6063 /**
6064  * i40e_create_queue_channel - function to create channel
6065  * @vsi: VSI to be configured
6066  * @ch: ptr to channel (it contains channel specific params)
6067  *
6068  * This function creates channel (VSI) using num_queues specified by user,
6069  * reconfigs RSS if needed.
6070  **/
6071 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6072 			      struct i40e_channel *ch)
6073 {
6074 	struct i40e_pf *pf = vsi->back;
6075 	bool reconfig_rss;
6076 	int err;
6077 
6078 	if (!ch)
6079 		return -EINVAL;
6080 
6081 	if (!ch->num_queue_pairs) {
6082 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6083 			ch->num_queue_pairs);
6084 		return -EINVAL;
6085 	}
6086 
6087 	/* validate user requested num_queues for channel */
6088 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6089 				       &reconfig_rss);
6090 	if (err) {
6091 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6092 			 ch->num_queue_pairs);
6093 		return -EINVAL;
6094 	}
6095 
6096 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6097 	 * VSI to be added switch to VEB mode.
6098 	 */
6099 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6100 	    (!i40e_is_any_channel(vsi))) {
6101 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6102 			dev_dbg(&pf->pdev->dev,
6103 				"Failed to create channel. Override queues (%u) not power of 2\n",
6104 				vsi->tc_config.tc_info[0].qcount);
6105 			return -EINVAL;
6106 		}
6107 
6108 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6109 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6110 
6111 			if (vsi->type == I40E_VSI_MAIN) {
6112 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6113 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6114 						      true);
6115 				else
6116 					i40e_do_reset_safe(pf,
6117 							   I40E_PF_RESET_FLAG);
6118 			}
6119 		}
6120 		/* now onwards for main VSI, number of queues will be value
6121 		 * of TC0's queue count
6122 		 */
6123 	}
6124 
6125 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6126 	 * it should be more than num_queues
6127 	 */
6128 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6129 		dev_dbg(&pf->pdev->dev,
6130 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6131 			vsi->cnt_q_avail, ch->num_queue_pairs);
6132 		return -EINVAL;
6133 	}
6134 
6135 	/* reconfig_rss only if vsi type is MAIN_VSI */
6136 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6137 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6138 		if (err) {
6139 			dev_info(&pf->pdev->dev,
6140 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6141 				 ch->num_queue_pairs);
6142 			return -EINVAL;
6143 		}
6144 	}
6145 
6146 	if (!i40e_setup_channel(pf, vsi, ch)) {
6147 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6148 		return -EINVAL;
6149 	}
6150 
6151 	dev_info(&pf->pdev->dev,
6152 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6153 		 ch->seid, ch->num_queue_pairs);
6154 
6155 	/* configure VSI for BW limit */
6156 	if (ch->max_tx_rate) {
6157 		u64 credits = ch->max_tx_rate;
6158 
6159 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6160 			return -EINVAL;
6161 
6162 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6163 		dev_dbg(&pf->pdev->dev,
6164 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6165 			ch->max_tx_rate,
6166 			credits,
6167 			ch->seid);
6168 	}
6169 
6170 	/* in case of VF, this will be main SRIOV VSI */
6171 	ch->parent_vsi = vsi;
6172 
6173 	/* and update main_vsi's count for queue_available to use */
6174 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6175 
6176 	return 0;
6177 }
6178 
6179 /**
6180  * i40e_configure_queue_channels - Add queue channel for the given TCs
6181  * @vsi: VSI to be configured
6182  *
6183  * Configures queue channel mapping to the given TCs
6184  **/
6185 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6186 {
6187 	struct i40e_channel *ch;
6188 	u64 max_rate = 0;
6189 	int ret = 0, i;
6190 
6191 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6192 	vsi->tc_seid_map[0] = vsi->seid;
6193 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6194 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6195 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6196 			if (!ch) {
6197 				ret = -ENOMEM;
6198 				goto err_free;
6199 			}
6200 
6201 			INIT_LIST_HEAD(&ch->list);
6202 			ch->num_queue_pairs =
6203 				vsi->tc_config.tc_info[i].qcount;
6204 			ch->base_queue =
6205 				vsi->tc_config.tc_info[i].qoffset;
6206 
6207 			/* Bandwidth limit through tc interface is in bytes/s,
6208 			 * change to Mbit/s
6209 			 */
6210 			max_rate = vsi->mqprio_qopt.max_rate[i];
6211 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6212 			ch->max_tx_rate = max_rate;
6213 
6214 			list_add_tail(&ch->list, &vsi->ch_list);
6215 
6216 			ret = i40e_create_queue_channel(vsi, ch);
6217 			if (ret) {
6218 				dev_err(&vsi->back->pdev->dev,
6219 					"Failed creating queue channel with TC%d: queues %d\n",
6220 					i, ch->num_queue_pairs);
6221 				goto err_free;
6222 			}
6223 			vsi->tc_seid_map[i] = ch->seid;
6224 		}
6225 	}
6226 	return ret;
6227 
6228 err_free:
6229 	i40e_remove_queue_channels(vsi);
6230 	return ret;
6231 }
6232 
6233 /**
6234  * i40e_veb_config_tc - Configure TCs for given VEB
6235  * @veb: given VEB
6236  * @enabled_tc: TC bitmap
6237  *
6238  * Configures given TC bitmap for VEB (switching) element
6239  **/
6240 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6241 {
6242 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6243 	struct i40e_pf *pf = veb->pf;
6244 	int ret = 0;
6245 	int i;
6246 
6247 	/* No TCs or already enabled TCs just return */
6248 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6249 		return ret;
6250 
6251 	bw_data.tc_valid_bits = enabled_tc;
6252 	/* bw_data.absolute_credits is not set (relative) */
6253 
6254 	/* Enable ETS TCs with equal BW Share for now */
6255 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6256 		if (enabled_tc & BIT(i))
6257 			bw_data.tc_bw_share_credits[i] = 1;
6258 	}
6259 
6260 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6261 						   &bw_data, NULL);
6262 	if (ret) {
6263 		dev_info(&pf->pdev->dev,
6264 			 "VEB bw config failed, err %s aq_err %s\n",
6265 			 i40e_stat_str(&pf->hw, ret),
6266 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6267 		goto out;
6268 	}
6269 
6270 	/* Update the BW information */
6271 	ret = i40e_veb_get_bw_info(veb);
6272 	if (ret) {
6273 		dev_info(&pf->pdev->dev,
6274 			 "Failed getting veb bw config, err %s aq_err %s\n",
6275 			 i40e_stat_str(&pf->hw, ret),
6276 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6277 	}
6278 
6279 out:
6280 	return ret;
6281 }
6282 
6283 #ifdef CONFIG_I40E_DCB
6284 /**
6285  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6286  * @pf: PF struct
6287  *
6288  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6289  * the caller would've quiesce all the VSIs before calling
6290  * this function
6291  **/
6292 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6293 {
6294 	u8 tc_map = 0;
6295 	int ret;
6296 	u8 v;
6297 
6298 	/* Enable the TCs available on PF to all VEBs */
6299 	tc_map = i40e_pf_get_tc_map(pf);
6300 	for (v = 0; v < I40E_MAX_VEB; v++) {
6301 		if (!pf->veb[v])
6302 			continue;
6303 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6304 		if (ret) {
6305 			dev_info(&pf->pdev->dev,
6306 				 "Failed configuring TC for VEB seid=%d\n",
6307 				 pf->veb[v]->seid);
6308 			/* Will try to configure as many components */
6309 		}
6310 	}
6311 
6312 	/* Update each VSI */
6313 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6314 		if (!pf->vsi[v])
6315 			continue;
6316 
6317 		/* - Enable all TCs for the LAN VSI
6318 		 * - For all others keep them at TC0 for now
6319 		 */
6320 		if (v == pf->lan_vsi)
6321 			tc_map = i40e_pf_get_tc_map(pf);
6322 		else
6323 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6324 
6325 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6326 		if (ret) {
6327 			dev_info(&pf->pdev->dev,
6328 				 "Failed configuring TC for VSI seid=%d\n",
6329 				 pf->vsi[v]->seid);
6330 			/* Will try to configure as many components */
6331 		} else {
6332 			/* Re-configure VSI vectors based on updated TC map */
6333 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6334 			if (pf->vsi[v]->netdev)
6335 				i40e_dcbnl_set_all(pf->vsi[v]);
6336 		}
6337 	}
6338 }
6339 
6340 /**
6341  * i40e_resume_port_tx - Resume port Tx
6342  * @pf: PF struct
6343  *
6344  * Resume a port's Tx and issue a PF reset in case of failure to
6345  * resume.
6346  **/
6347 static int i40e_resume_port_tx(struct i40e_pf *pf)
6348 {
6349 	struct i40e_hw *hw = &pf->hw;
6350 	int ret;
6351 
6352 	ret = i40e_aq_resume_port_tx(hw, NULL);
6353 	if (ret) {
6354 		dev_info(&pf->pdev->dev,
6355 			 "Resume Port Tx failed, err %s aq_err %s\n",
6356 			  i40e_stat_str(&pf->hw, ret),
6357 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6358 		/* Schedule PF reset to recover */
6359 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6360 		i40e_service_event_schedule(pf);
6361 	}
6362 
6363 	return ret;
6364 }
6365 
6366 /**
6367  * i40e_init_pf_dcb - Initialize DCB configuration
6368  * @pf: PF being configured
6369  *
6370  * Query the current DCB configuration and cache it
6371  * in the hardware structure
6372  **/
6373 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6374 {
6375 	struct i40e_hw *hw = &pf->hw;
6376 	int err = 0;
6377 
6378 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6379 	 * Also do not enable DCBx if FW LLDP agent is disabled
6380 	 */
6381 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6382 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
6383 		goto out;
6384 
6385 	/* Get the initial DCB configuration */
6386 	err = i40e_init_dcb(hw);
6387 	if (!err) {
6388 		/* Device/Function is not DCBX capable */
6389 		if ((!hw->func_caps.dcb) ||
6390 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6391 			dev_info(&pf->pdev->dev,
6392 				 "DCBX offload is not supported or is disabled for this PF.\n");
6393 		} else {
6394 			/* When status is not DISABLED then DCBX in FW */
6395 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6396 				       DCB_CAP_DCBX_VER_IEEE;
6397 
6398 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6399 			/* Enable DCB tagging only when more than one TC
6400 			 * or explicitly disable if only one TC
6401 			 */
6402 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6403 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6404 			else
6405 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6406 			dev_dbg(&pf->pdev->dev,
6407 				"DCBX offload is supported for this PF.\n");
6408 		}
6409 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6410 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6411 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6412 	} else {
6413 		dev_info(&pf->pdev->dev,
6414 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6415 			 i40e_stat_str(&pf->hw, err),
6416 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6417 	}
6418 
6419 out:
6420 	return err;
6421 }
6422 #endif /* CONFIG_I40E_DCB */
6423 #define SPEED_SIZE 14
6424 #define FC_SIZE 8
6425 /**
6426  * i40e_print_link_message - print link up or down
6427  * @vsi: the VSI for which link needs a message
6428  * @isup: true of link is up, false otherwise
6429  */
6430 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6431 {
6432 	enum i40e_aq_link_speed new_speed;
6433 	struct i40e_pf *pf = vsi->back;
6434 	char *speed = "Unknown";
6435 	char *fc = "Unknown";
6436 	char *fec = "";
6437 	char *req_fec = "";
6438 	char *an = "";
6439 
6440 	if (isup)
6441 		new_speed = pf->hw.phy.link_info.link_speed;
6442 	else
6443 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6444 
6445 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6446 		return;
6447 	vsi->current_isup = isup;
6448 	vsi->current_speed = new_speed;
6449 	if (!isup) {
6450 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6451 		return;
6452 	}
6453 
6454 	/* Warn user if link speed on NPAR enabled partition is not at
6455 	 * least 10GB
6456 	 */
6457 	if (pf->hw.func_caps.npar_enable &&
6458 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6459 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6460 		netdev_warn(vsi->netdev,
6461 			    "The partition detected link speed that is less than 10Gbps\n");
6462 
6463 	switch (pf->hw.phy.link_info.link_speed) {
6464 	case I40E_LINK_SPEED_40GB:
6465 		speed = "40 G";
6466 		break;
6467 	case I40E_LINK_SPEED_20GB:
6468 		speed = "20 G";
6469 		break;
6470 	case I40E_LINK_SPEED_25GB:
6471 		speed = "25 G";
6472 		break;
6473 	case I40E_LINK_SPEED_10GB:
6474 		speed = "10 G";
6475 		break;
6476 	case I40E_LINK_SPEED_1GB:
6477 		speed = "1000 M";
6478 		break;
6479 	case I40E_LINK_SPEED_100MB:
6480 		speed = "100 M";
6481 		break;
6482 	default:
6483 		break;
6484 	}
6485 
6486 	switch (pf->hw.fc.current_mode) {
6487 	case I40E_FC_FULL:
6488 		fc = "RX/TX";
6489 		break;
6490 	case I40E_FC_TX_PAUSE:
6491 		fc = "TX";
6492 		break;
6493 	case I40E_FC_RX_PAUSE:
6494 		fc = "RX";
6495 		break;
6496 	default:
6497 		fc = "None";
6498 		break;
6499 	}
6500 
6501 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6502 		req_fec = ", Requested FEC: None";
6503 		fec = ", FEC: None";
6504 		an = ", Autoneg: False";
6505 
6506 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6507 			an = ", Autoneg: True";
6508 
6509 		if (pf->hw.phy.link_info.fec_info &
6510 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6511 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6512 		else if (pf->hw.phy.link_info.fec_info &
6513 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6514 			fec = ", FEC: CL108 RS-FEC";
6515 
6516 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6517 		 * both RS and FC are requested
6518 		 */
6519 		if (vsi->back->hw.phy.link_info.req_fec_info &
6520 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6521 			if (vsi->back->hw.phy.link_info.req_fec_info &
6522 			    I40E_AQ_REQUEST_FEC_RS)
6523 				req_fec = ", Requested FEC: CL108 RS-FEC";
6524 			else
6525 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6526 		}
6527 	}
6528 
6529 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6530 		    speed, req_fec, fec, an, fc);
6531 }
6532 
6533 /**
6534  * i40e_up_complete - Finish the last steps of bringing up a connection
6535  * @vsi: the VSI being configured
6536  **/
6537 static int i40e_up_complete(struct i40e_vsi *vsi)
6538 {
6539 	struct i40e_pf *pf = vsi->back;
6540 	int err;
6541 
6542 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6543 		i40e_vsi_configure_msix(vsi);
6544 	else
6545 		i40e_configure_msi_and_legacy(vsi);
6546 
6547 	/* start rings */
6548 	err = i40e_vsi_start_rings(vsi);
6549 	if (err)
6550 		return err;
6551 
6552 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6553 	i40e_napi_enable_all(vsi);
6554 	i40e_vsi_enable_irq(vsi);
6555 
6556 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6557 	    (vsi->netdev)) {
6558 		i40e_print_link_message(vsi, true);
6559 		netif_tx_start_all_queues(vsi->netdev);
6560 		netif_carrier_on(vsi->netdev);
6561 	}
6562 
6563 	/* replay FDIR SB filters */
6564 	if (vsi->type == I40E_VSI_FDIR) {
6565 		/* reset fd counters */
6566 		pf->fd_add_err = 0;
6567 		pf->fd_atr_cnt = 0;
6568 		i40e_fdir_filter_restore(vsi);
6569 	}
6570 
6571 	/* On the next run of the service_task, notify any clients of the new
6572 	 * opened netdev
6573 	 */
6574 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6575 	i40e_service_event_schedule(pf);
6576 
6577 	return 0;
6578 }
6579 
6580 /**
6581  * i40e_vsi_reinit_locked - Reset the VSI
6582  * @vsi: the VSI being configured
6583  *
6584  * Rebuild the ring structs after some configuration
6585  * has changed, e.g. MTU size.
6586  **/
6587 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6588 {
6589 	struct i40e_pf *pf = vsi->back;
6590 
6591 	WARN_ON(in_interrupt());
6592 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6593 		usleep_range(1000, 2000);
6594 	i40e_down(vsi);
6595 
6596 	i40e_up(vsi);
6597 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6598 }
6599 
6600 /**
6601  * i40e_up - Bring the connection back up after being down
6602  * @vsi: the VSI being configured
6603  **/
6604 int i40e_up(struct i40e_vsi *vsi)
6605 {
6606 	int err;
6607 
6608 	err = i40e_vsi_configure(vsi);
6609 	if (!err)
6610 		err = i40e_up_complete(vsi);
6611 
6612 	return err;
6613 }
6614 
6615 /**
6616  * i40e_force_link_state - Force the link status
6617  * @pf: board private structure
6618  * @is_up: whether the link state should be forced up or down
6619  **/
6620 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6621 {
6622 	struct i40e_aq_get_phy_abilities_resp abilities;
6623 	struct i40e_aq_set_phy_config config = {0};
6624 	struct i40e_hw *hw = &pf->hw;
6625 	i40e_status err;
6626 	u64 mask;
6627 	u8 speed;
6628 
6629 	/* Card might've been put in an unstable state by other drivers
6630 	 * and applications, which causes incorrect speed values being
6631 	 * set on startup. In order to clear speed registers, we call
6632 	 * get_phy_capabilities twice, once to get initial state of
6633 	 * available speeds, and once to get current PHY config.
6634 	 */
6635 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6636 					   NULL);
6637 	if (err) {
6638 		dev_err(&pf->pdev->dev,
6639 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6640 			i40e_stat_str(hw, err),
6641 			i40e_aq_str(hw, hw->aq.asq_last_status));
6642 		return err;
6643 	}
6644 	speed = abilities.link_speed;
6645 
6646 	/* Get the current phy config */
6647 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6648 					   NULL);
6649 	if (err) {
6650 		dev_err(&pf->pdev->dev,
6651 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6652 			i40e_stat_str(hw, err),
6653 			i40e_aq_str(hw, hw->aq.asq_last_status));
6654 		return err;
6655 	}
6656 
6657 	/* If link needs to go up, but was not forced to go down,
6658 	 * and its speed values are OK, no need for a flap
6659 	 */
6660 	if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6661 		return I40E_SUCCESS;
6662 
6663 	/* To force link we need to set bits for all supported PHY types,
6664 	 * but there are now more than 32, so we need to split the bitmap
6665 	 * across two fields.
6666 	 */
6667 	mask = I40E_PHY_TYPES_BITMASK;
6668 	config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6669 	config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6670 	/* Copy the old settings, except of phy_type */
6671 	config.abilities = abilities.abilities;
6672 	if (abilities.link_speed != 0)
6673 		config.link_speed = abilities.link_speed;
6674 	else
6675 		config.link_speed = speed;
6676 	config.eee_capability = abilities.eee_capability;
6677 	config.eeer = abilities.eeer_val;
6678 	config.low_power_ctrl = abilities.d3_lpan;
6679 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6680 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6681 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6682 
6683 	if (err) {
6684 		dev_err(&pf->pdev->dev,
6685 			"set phy config ret =  %s last_status =  %s\n",
6686 			i40e_stat_str(&pf->hw, err),
6687 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6688 		return err;
6689 	}
6690 
6691 	/* Update the link info */
6692 	err = i40e_update_link_info(hw);
6693 	if (err) {
6694 		/* Wait a little bit (on 40G cards it sometimes takes a really
6695 		 * long time for link to come back from the atomic reset)
6696 		 * and try once more
6697 		 */
6698 		msleep(1000);
6699 		i40e_update_link_info(hw);
6700 	}
6701 
6702 	i40e_aq_set_link_restart_an(hw, true, NULL);
6703 
6704 	return I40E_SUCCESS;
6705 }
6706 
6707 /**
6708  * i40e_down - Shutdown the connection processing
6709  * @vsi: the VSI being stopped
6710  **/
6711 void i40e_down(struct i40e_vsi *vsi)
6712 {
6713 	int i;
6714 
6715 	/* It is assumed that the caller of this function
6716 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6717 	 */
6718 	if (vsi->netdev) {
6719 		netif_carrier_off(vsi->netdev);
6720 		netif_tx_disable(vsi->netdev);
6721 	}
6722 	i40e_vsi_disable_irq(vsi);
6723 	i40e_vsi_stop_rings(vsi);
6724 	if (vsi->type == I40E_VSI_MAIN &&
6725 	    vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6726 		i40e_force_link_state(vsi->back, false);
6727 	i40e_napi_disable_all(vsi);
6728 
6729 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6730 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6731 		if (i40e_enabled_xdp_vsi(vsi)) {
6732 			/* Make sure that in-progress ndo_xdp_xmit
6733 			 * calls are completed.
6734 			 */
6735 			synchronize_rcu();
6736 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6737 		}
6738 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6739 	}
6740 
6741 }
6742 
6743 /**
6744  * i40e_validate_mqprio_qopt- validate queue mapping info
6745  * @vsi: the VSI being configured
6746  * @mqprio_qopt: queue parametrs
6747  **/
6748 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6749 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6750 {
6751 	u64 sum_max_rate = 0;
6752 	u64 max_rate = 0;
6753 	int i;
6754 
6755 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6756 	    mqprio_qopt->qopt.num_tc < 1 ||
6757 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6758 		return -EINVAL;
6759 	for (i = 0; ; i++) {
6760 		if (!mqprio_qopt->qopt.count[i])
6761 			return -EINVAL;
6762 		if (mqprio_qopt->min_rate[i]) {
6763 			dev_err(&vsi->back->pdev->dev,
6764 				"Invalid min tx rate (greater than 0) specified\n");
6765 			return -EINVAL;
6766 		}
6767 		max_rate = mqprio_qopt->max_rate[i];
6768 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6769 		sum_max_rate += max_rate;
6770 
6771 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6772 			break;
6773 		if (mqprio_qopt->qopt.offset[i + 1] !=
6774 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6775 			return -EINVAL;
6776 	}
6777 	if (vsi->num_queue_pairs <
6778 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6779 		return -EINVAL;
6780 	}
6781 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6782 		dev_err(&vsi->back->pdev->dev,
6783 			"Invalid max tx rate specified\n");
6784 		return -EINVAL;
6785 	}
6786 	return 0;
6787 }
6788 
6789 /**
6790  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6791  * @vsi: the VSI being configured
6792  **/
6793 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6794 {
6795 	u16 qcount;
6796 	int i;
6797 
6798 	/* Only TC0 is enabled */
6799 	vsi->tc_config.numtc = 1;
6800 	vsi->tc_config.enabled_tc = 1;
6801 	qcount = min_t(int, vsi->alloc_queue_pairs,
6802 		       i40e_pf_get_max_q_per_tc(vsi->back));
6803 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6804 		/* For the TC that is not enabled set the offset to to default
6805 		 * queue and allocate one queue for the given TC.
6806 		 */
6807 		vsi->tc_config.tc_info[i].qoffset = 0;
6808 		if (i == 0)
6809 			vsi->tc_config.tc_info[i].qcount = qcount;
6810 		else
6811 			vsi->tc_config.tc_info[i].qcount = 1;
6812 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6813 	}
6814 }
6815 
6816 /**
6817  * i40e_setup_tc - configure multiple traffic classes
6818  * @netdev: net device to configure
6819  * @type_data: tc offload data
6820  **/
6821 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6822 {
6823 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6824 	struct i40e_netdev_priv *np = netdev_priv(netdev);
6825 	struct i40e_vsi *vsi = np->vsi;
6826 	struct i40e_pf *pf = vsi->back;
6827 	u8 enabled_tc = 0, num_tc, hw;
6828 	bool need_reset = false;
6829 	int ret = -EINVAL;
6830 	u16 mode;
6831 	int i;
6832 
6833 	num_tc = mqprio_qopt->qopt.num_tc;
6834 	hw = mqprio_qopt->qopt.hw;
6835 	mode = mqprio_qopt->mode;
6836 	if (!hw) {
6837 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6838 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6839 		goto config_tc;
6840 	}
6841 
6842 	/* Check if MFP enabled */
6843 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6844 		netdev_info(netdev,
6845 			    "Configuring TC not supported in MFP mode\n");
6846 		return ret;
6847 	}
6848 	switch (mode) {
6849 	case TC_MQPRIO_MODE_DCB:
6850 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6851 
6852 		/* Check if DCB enabled to continue */
6853 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6854 			netdev_info(netdev,
6855 				    "DCB is not enabled for adapter\n");
6856 			return ret;
6857 		}
6858 
6859 		/* Check whether tc count is within enabled limit */
6860 		if (num_tc > i40e_pf_get_num_tc(pf)) {
6861 			netdev_info(netdev,
6862 				    "TC count greater than enabled on link for adapter\n");
6863 			return ret;
6864 		}
6865 		break;
6866 	case TC_MQPRIO_MODE_CHANNEL:
6867 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6868 			netdev_info(netdev,
6869 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6870 			return ret;
6871 		}
6872 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6873 			return ret;
6874 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6875 		if (ret)
6876 			return ret;
6877 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6878 		       sizeof(*mqprio_qopt));
6879 		pf->flags |= I40E_FLAG_TC_MQPRIO;
6880 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6881 		break;
6882 	default:
6883 		return -EINVAL;
6884 	}
6885 
6886 config_tc:
6887 	/* Generate TC map for number of tc requested */
6888 	for (i = 0; i < num_tc; i++)
6889 		enabled_tc |= BIT(i);
6890 
6891 	/* Requesting same TC configuration as already enabled */
6892 	if (enabled_tc == vsi->tc_config.enabled_tc &&
6893 	    mode != TC_MQPRIO_MODE_CHANNEL)
6894 		return 0;
6895 
6896 	/* Quiesce VSI queues */
6897 	i40e_quiesce_vsi(vsi);
6898 
6899 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6900 		i40e_remove_queue_channels(vsi);
6901 
6902 	/* Configure VSI for enabled TCs */
6903 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
6904 	if (ret) {
6905 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6906 			    vsi->seid);
6907 		need_reset = true;
6908 		goto exit;
6909 	}
6910 
6911 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6912 		if (vsi->mqprio_qopt.max_rate[0]) {
6913 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6914 
6915 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6916 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6917 			if (!ret) {
6918 				u64 credits = max_tx_rate;
6919 
6920 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
6921 				dev_dbg(&vsi->back->pdev->dev,
6922 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6923 					max_tx_rate,
6924 					credits,
6925 					vsi->seid);
6926 			} else {
6927 				need_reset = true;
6928 				goto exit;
6929 			}
6930 		}
6931 		ret = i40e_configure_queue_channels(vsi);
6932 		if (ret) {
6933 			netdev_info(netdev,
6934 				    "Failed configuring queue channels\n");
6935 			need_reset = true;
6936 			goto exit;
6937 		}
6938 	}
6939 
6940 exit:
6941 	/* Reset the configuration data to defaults, only TC0 is enabled */
6942 	if (need_reset) {
6943 		i40e_vsi_set_default_tc_config(vsi);
6944 		need_reset = false;
6945 	}
6946 
6947 	/* Unquiesce VSI */
6948 	i40e_unquiesce_vsi(vsi);
6949 	return ret;
6950 }
6951 
6952 /**
6953  * i40e_set_cld_element - sets cloud filter element data
6954  * @filter: cloud filter rule
6955  * @cld: ptr to cloud filter element data
6956  *
6957  * This is helper function to copy data into cloud filter element
6958  **/
6959 static inline void
6960 i40e_set_cld_element(struct i40e_cloud_filter *filter,
6961 		     struct i40e_aqc_cloud_filters_element_data *cld)
6962 {
6963 	int i, j;
6964 	u32 ipa;
6965 
6966 	memset(cld, 0, sizeof(*cld));
6967 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
6968 	ether_addr_copy(cld->inner_mac, filter->src_mac);
6969 
6970 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6971 		return;
6972 
6973 	if (filter->n_proto == ETH_P_IPV6) {
6974 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
6975 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6976 		     i++, j += 2) {
6977 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6978 			ipa = cpu_to_le32(ipa);
6979 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6980 		}
6981 	} else {
6982 		ipa = be32_to_cpu(filter->dst_ipv4);
6983 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6984 	}
6985 
6986 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6987 
6988 	/* tenant_id is not supported by FW now, once the support is enabled
6989 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6990 	 */
6991 	if (filter->tenant_id)
6992 		return;
6993 }
6994 
6995 /**
6996  * i40e_add_del_cloud_filter - Add/del cloud filter
6997  * @vsi: pointer to VSI
6998  * @filter: cloud filter rule
6999  * @add: if true, add, if false, delete
7000  *
7001  * Add or delete a cloud filter for a specific flow spec.
7002  * Returns 0 if the filter were successfully added.
7003  **/
7004 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7005 			      struct i40e_cloud_filter *filter, bool add)
7006 {
7007 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7008 	struct i40e_pf *pf = vsi->back;
7009 	int ret;
7010 	static const u16 flag_table[128] = {
7011 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7012 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7013 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7014 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7015 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7016 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7017 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7018 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7019 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7020 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7021 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7022 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7023 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7024 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7025 	};
7026 
7027 	if (filter->flags >= ARRAY_SIZE(flag_table))
7028 		return I40E_ERR_CONFIG;
7029 
7030 	/* copy element needed to add cloud filter from filter */
7031 	i40e_set_cld_element(filter, &cld_filter);
7032 
7033 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7034 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7035 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7036 
7037 	if (filter->n_proto == ETH_P_IPV6)
7038 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7039 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7040 	else
7041 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7042 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7043 
7044 	if (add)
7045 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7046 						&cld_filter, 1);
7047 	else
7048 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7049 						&cld_filter, 1);
7050 	if (ret)
7051 		dev_dbg(&pf->pdev->dev,
7052 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7053 			add ? "add" : "delete", filter->dst_port, ret,
7054 			pf->hw.aq.asq_last_status);
7055 	else
7056 		dev_info(&pf->pdev->dev,
7057 			 "%s cloud filter for VSI: %d\n",
7058 			 add ? "Added" : "Deleted", filter->seid);
7059 	return ret;
7060 }
7061 
7062 /**
7063  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7064  * @vsi: pointer to VSI
7065  * @filter: cloud filter rule
7066  * @add: if true, add, if false, delete
7067  *
7068  * Add or delete a cloud filter for a specific flow spec using big buffer.
7069  * Returns 0 if the filter were successfully added.
7070  **/
7071 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7072 				      struct i40e_cloud_filter *filter,
7073 				      bool add)
7074 {
7075 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7076 	struct i40e_pf *pf = vsi->back;
7077 	int ret;
7078 
7079 	/* Both (src/dst) valid mac_addr are not supported */
7080 	if ((is_valid_ether_addr(filter->dst_mac) &&
7081 	     is_valid_ether_addr(filter->src_mac)) ||
7082 	    (is_multicast_ether_addr(filter->dst_mac) &&
7083 	     is_multicast_ether_addr(filter->src_mac)))
7084 		return -EOPNOTSUPP;
7085 
7086 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7087 	 * ports are not supported via big buffer now.
7088 	 */
7089 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7090 		return -EOPNOTSUPP;
7091 
7092 	/* adding filter using src_port/src_ip is not supported at this stage */
7093 	if (filter->src_port || filter->src_ipv4 ||
7094 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7095 		return -EOPNOTSUPP;
7096 
7097 	/* copy element needed to add cloud filter from filter */
7098 	i40e_set_cld_element(filter, &cld_filter.element);
7099 
7100 	if (is_valid_ether_addr(filter->dst_mac) ||
7101 	    is_valid_ether_addr(filter->src_mac) ||
7102 	    is_multicast_ether_addr(filter->dst_mac) ||
7103 	    is_multicast_ether_addr(filter->src_mac)) {
7104 		/* MAC + IP : unsupported mode */
7105 		if (filter->dst_ipv4)
7106 			return -EOPNOTSUPP;
7107 
7108 		/* since we validated that L4 port must be valid before
7109 		 * we get here, start with respective "flags" value
7110 		 * and update if vlan is present or not
7111 		 */
7112 		cld_filter.element.flags =
7113 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7114 
7115 		if (filter->vlan_id) {
7116 			cld_filter.element.flags =
7117 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7118 		}
7119 
7120 	} else if (filter->dst_ipv4 ||
7121 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7122 		cld_filter.element.flags =
7123 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7124 		if (filter->n_proto == ETH_P_IPV6)
7125 			cld_filter.element.flags |=
7126 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7127 		else
7128 			cld_filter.element.flags |=
7129 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7130 	} else {
7131 		dev_err(&pf->pdev->dev,
7132 			"either mac or ip has to be valid for cloud filter\n");
7133 		return -EINVAL;
7134 	}
7135 
7136 	/* Now copy L4 port in Byte 6..7 in general fields */
7137 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7138 						be16_to_cpu(filter->dst_port);
7139 
7140 	if (add) {
7141 		/* Validate current device switch mode, change if necessary */
7142 		ret = i40e_validate_and_set_switch_mode(vsi);
7143 		if (ret) {
7144 			dev_err(&pf->pdev->dev,
7145 				"failed to set switch mode, ret %d\n",
7146 				ret);
7147 			return ret;
7148 		}
7149 
7150 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7151 						   &cld_filter, 1);
7152 	} else {
7153 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7154 						   &cld_filter, 1);
7155 	}
7156 
7157 	if (ret)
7158 		dev_dbg(&pf->pdev->dev,
7159 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7160 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7161 	else
7162 		dev_info(&pf->pdev->dev,
7163 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7164 			 add ? "add" : "delete", filter->seid,
7165 			 ntohs(filter->dst_port));
7166 	return ret;
7167 }
7168 
7169 /**
7170  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7171  * @vsi: Pointer to VSI
7172  * @cls_flower: Pointer to struct tc_cls_flower_offload
7173  * @filter: Pointer to cloud filter structure
7174  *
7175  **/
7176 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7177 				 struct tc_cls_flower_offload *f,
7178 				 struct i40e_cloud_filter *filter)
7179 {
7180 	struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
7181 	struct flow_dissector *dissector = rule->match.dissector;
7182 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7183 	struct i40e_pf *pf = vsi->back;
7184 	u8 field_flags = 0;
7185 
7186 	if (dissector->used_keys &
7187 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7188 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7189 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7190 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7191 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7192 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7193 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7194 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7195 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7196 			dissector->used_keys);
7197 		return -EOPNOTSUPP;
7198 	}
7199 
7200 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7201 		struct flow_match_enc_keyid match;
7202 
7203 		flow_rule_match_enc_keyid(rule, &match);
7204 		if (match.mask->keyid != 0)
7205 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7206 
7207 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7208 	}
7209 
7210 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7211 		struct flow_match_basic match;
7212 
7213 		flow_rule_match_basic(rule, &match);
7214 		n_proto_key = ntohs(match.key->n_proto);
7215 		n_proto_mask = ntohs(match.mask->n_proto);
7216 
7217 		if (n_proto_key == ETH_P_ALL) {
7218 			n_proto_key = 0;
7219 			n_proto_mask = 0;
7220 		}
7221 		filter->n_proto = n_proto_key & n_proto_mask;
7222 		filter->ip_proto = match.key->ip_proto;
7223 	}
7224 
7225 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7226 		struct flow_match_eth_addrs match;
7227 
7228 		flow_rule_match_eth_addrs(rule, &match);
7229 
7230 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7231 		if (!is_zero_ether_addr(match.mask->dst)) {
7232 			if (is_broadcast_ether_addr(match.mask->dst)) {
7233 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7234 			} else {
7235 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7236 					match.mask->dst);
7237 				return I40E_ERR_CONFIG;
7238 			}
7239 		}
7240 
7241 		if (!is_zero_ether_addr(match.mask->src)) {
7242 			if (is_broadcast_ether_addr(match.mask->src)) {
7243 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7244 			} else {
7245 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7246 					match.mask->src);
7247 				return I40E_ERR_CONFIG;
7248 			}
7249 		}
7250 		ether_addr_copy(filter->dst_mac, match.key->dst);
7251 		ether_addr_copy(filter->src_mac, match.key->src);
7252 	}
7253 
7254 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7255 		struct flow_match_vlan match;
7256 
7257 		flow_rule_match_vlan(rule, &match);
7258 		if (match.mask->vlan_id) {
7259 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7260 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7261 
7262 			} else {
7263 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7264 					match.mask->vlan_id);
7265 				return I40E_ERR_CONFIG;
7266 			}
7267 		}
7268 
7269 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
7270 	}
7271 
7272 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
7273 		struct flow_match_control match;
7274 
7275 		flow_rule_match_control(rule, &match);
7276 		addr_type = match.key->addr_type;
7277 	}
7278 
7279 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7280 		struct flow_match_ipv4_addrs match;
7281 
7282 		flow_rule_match_ipv4_addrs(rule, &match);
7283 		if (match.mask->dst) {
7284 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
7285 				field_flags |= I40E_CLOUD_FIELD_IIP;
7286 			} else {
7287 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7288 					&match.mask->dst);
7289 				return I40E_ERR_CONFIG;
7290 			}
7291 		}
7292 
7293 		if (match.mask->src) {
7294 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
7295 				field_flags |= I40E_CLOUD_FIELD_IIP;
7296 			} else {
7297 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7298 					&match.mask->src);
7299 				return I40E_ERR_CONFIG;
7300 			}
7301 		}
7302 
7303 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7304 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7305 			return I40E_ERR_CONFIG;
7306 		}
7307 		filter->dst_ipv4 = match.key->dst;
7308 		filter->src_ipv4 = match.key->src;
7309 	}
7310 
7311 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7312 		struct flow_match_ipv6_addrs match;
7313 
7314 		flow_rule_match_ipv6_addrs(rule, &match);
7315 
7316 		/* src and dest IPV6 address should not be LOOPBACK
7317 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7318 		 */
7319 		if (ipv6_addr_loopback(&match.key->dst) ||
7320 		    ipv6_addr_loopback(&match.key->src)) {
7321 			dev_err(&pf->pdev->dev,
7322 				"Bad ipv6, addr is LOOPBACK\n");
7323 			return I40E_ERR_CONFIG;
7324 		}
7325 		if (!ipv6_addr_any(&match.mask->dst) ||
7326 		    !ipv6_addr_any(&match.mask->src))
7327 			field_flags |= I40E_CLOUD_FIELD_IIP;
7328 
7329 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
7330 		       sizeof(filter->src_ipv6));
7331 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
7332 		       sizeof(filter->dst_ipv6));
7333 	}
7334 
7335 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
7336 		struct flow_match_ports match;
7337 
7338 		flow_rule_match_ports(rule, &match);
7339 		if (match.mask->src) {
7340 			if (match.mask->src == cpu_to_be16(0xffff)) {
7341 				field_flags |= I40E_CLOUD_FIELD_IIP;
7342 			} else {
7343 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7344 					be16_to_cpu(match.mask->src));
7345 				return I40E_ERR_CONFIG;
7346 			}
7347 		}
7348 
7349 		if (match.mask->dst) {
7350 			if (match.mask->dst == cpu_to_be16(0xffff)) {
7351 				field_flags |= I40E_CLOUD_FIELD_IIP;
7352 			} else {
7353 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7354 					be16_to_cpu(match.mask->dst));
7355 				return I40E_ERR_CONFIG;
7356 			}
7357 		}
7358 
7359 		filter->dst_port = match.key->dst;
7360 		filter->src_port = match.key->src;
7361 
7362 		switch (filter->ip_proto) {
7363 		case IPPROTO_TCP:
7364 		case IPPROTO_UDP:
7365 			break;
7366 		default:
7367 			dev_err(&pf->pdev->dev,
7368 				"Only UDP and TCP transport are supported\n");
7369 			return -EINVAL;
7370 		}
7371 	}
7372 	filter->flags = field_flags;
7373 	return 0;
7374 }
7375 
7376 /**
7377  * i40e_handle_tclass: Forward to a traffic class on the device
7378  * @vsi: Pointer to VSI
7379  * @tc: traffic class index on the device
7380  * @filter: Pointer to cloud filter structure
7381  *
7382  **/
7383 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7384 			      struct i40e_cloud_filter *filter)
7385 {
7386 	struct i40e_channel *ch, *ch_tmp;
7387 
7388 	/* direct to a traffic class on the same device */
7389 	if (tc == 0) {
7390 		filter->seid = vsi->seid;
7391 		return 0;
7392 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7393 		if (!filter->dst_port) {
7394 			dev_err(&vsi->back->pdev->dev,
7395 				"Specify destination port to direct to traffic class that is not default\n");
7396 			return -EINVAL;
7397 		}
7398 		if (list_empty(&vsi->ch_list))
7399 			return -EINVAL;
7400 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7401 					 list) {
7402 			if (ch->seid == vsi->tc_seid_map[tc])
7403 				filter->seid = ch->seid;
7404 		}
7405 		return 0;
7406 	}
7407 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7408 	return -EINVAL;
7409 }
7410 
7411 /**
7412  * i40e_configure_clsflower - Configure tc flower filters
7413  * @vsi: Pointer to VSI
7414  * @cls_flower: Pointer to struct tc_cls_flower_offload
7415  *
7416  **/
7417 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7418 				    struct tc_cls_flower_offload *cls_flower)
7419 {
7420 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7421 	struct i40e_cloud_filter *filter = NULL;
7422 	struct i40e_pf *pf = vsi->back;
7423 	int err = 0;
7424 
7425 	if (tc < 0) {
7426 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7427 		return -EOPNOTSUPP;
7428 	}
7429 
7430 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7431 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7432 		return -EBUSY;
7433 
7434 	if (pf->fdir_pf_active_filters ||
7435 	    (!hlist_empty(&pf->fdir_filter_list))) {
7436 		dev_err(&vsi->back->pdev->dev,
7437 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7438 		return -EINVAL;
7439 	}
7440 
7441 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7442 		dev_err(&vsi->back->pdev->dev,
7443 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7444 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7445 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7446 	}
7447 
7448 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7449 	if (!filter)
7450 		return -ENOMEM;
7451 
7452 	filter->cookie = cls_flower->cookie;
7453 
7454 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7455 	if (err < 0)
7456 		goto err;
7457 
7458 	err = i40e_handle_tclass(vsi, tc, filter);
7459 	if (err < 0)
7460 		goto err;
7461 
7462 	/* Add cloud filter */
7463 	if (filter->dst_port)
7464 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7465 	else
7466 		err = i40e_add_del_cloud_filter(vsi, filter, true);
7467 
7468 	if (err) {
7469 		dev_err(&pf->pdev->dev,
7470 			"Failed to add cloud filter, err %s\n",
7471 			i40e_stat_str(&pf->hw, err));
7472 		goto err;
7473 	}
7474 
7475 	/* add filter to the ordered list */
7476 	INIT_HLIST_NODE(&filter->cloud_node);
7477 
7478 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7479 
7480 	pf->num_cloud_filters++;
7481 
7482 	return err;
7483 err:
7484 	kfree(filter);
7485 	return err;
7486 }
7487 
7488 /**
7489  * i40e_find_cloud_filter - Find the could filter in the list
7490  * @vsi: Pointer to VSI
7491  * @cookie: filter specific cookie
7492  *
7493  **/
7494 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7495 							unsigned long *cookie)
7496 {
7497 	struct i40e_cloud_filter *filter = NULL;
7498 	struct hlist_node *node2;
7499 
7500 	hlist_for_each_entry_safe(filter, node2,
7501 				  &vsi->back->cloud_filter_list, cloud_node)
7502 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7503 			return filter;
7504 	return NULL;
7505 }
7506 
7507 /**
7508  * i40e_delete_clsflower - Remove tc flower filters
7509  * @vsi: Pointer to VSI
7510  * @cls_flower: Pointer to struct tc_cls_flower_offload
7511  *
7512  **/
7513 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7514 				 struct tc_cls_flower_offload *cls_flower)
7515 {
7516 	struct i40e_cloud_filter *filter = NULL;
7517 	struct i40e_pf *pf = vsi->back;
7518 	int err = 0;
7519 
7520 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7521 
7522 	if (!filter)
7523 		return -EINVAL;
7524 
7525 	hash_del(&filter->cloud_node);
7526 
7527 	if (filter->dst_port)
7528 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7529 	else
7530 		err = i40e_add_del_cloud_filter(vsi, filter, false);
7531 
7532 	kfree(filter);
7533 	if (err) {
7534 		dev_err(&pf->pdev->dev,
7535 			"Failed to delete cloud filter, err %s\n",
7536 			i40e_stat_str(&pf->hw, err));
7537 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7538 	}
7539 
7540 	pf->num_cloud_filters--;
7541 	if (!pf->num_cloud_filters)
7542 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7543 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7544 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7545 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7546 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7547 		}
7548 	return 0;
7549 }
7550 
7551 /**
7552  * i40e_setup_tc_cls_flower - flower classifier offloads
7553  * @netdev: net device to configure
7554  * @type_data: offload data
7555  **/
7556 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7557 				    struct tc_cls_flower_offload *cls_flower)
7558 {
7559 	struct i40e_vsi *vsi = np->vsi;
7560 
7561 	switch (cls_flower->command) {
7562 	case TC_CLSFLOWER_REPLACE:
7563 		return i40e_configure_clsflower(vsi, cls_flower);
7564 	case TC_CLSFLOWER_DESTROY:
7565 		return i40e_delete_clsflower(vsi, cls_flower);
7566 	case TC_CLSFLOWER_STATS:
7567 		return -EOPNOTSUPP;
7568 	default:
7569 		return -EOPNOTSUPP;
7570 	}
7571 }
7572 
7573 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7574 				  void *cb_priv)
7575 {
7576 	struct i40e_netdev_priv *np = cb_priv;
7577 
7578 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
7579 		return -EOPNOTSUPP;
7580 
7581 	switch (type) {
7582 	case TC_SETUP_CLSFLOWER:
7583 		return i40e_setup_tc_cls_flower(np, type_data);
7584 
7585 	default:
7586 		return -EOPNOTSUPP;
7587 	}
7588 }
7589 
7590 static int i40e_setup_tc_block(struct net_device *dev,
7591 			       struct tc_block_offload *f)
7592 {
7593 	struct i40e_netdev_priv *np = netdev_priv(dev);
7594 
7595 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7596 		return -EOPNOTSUPP;
7597 
7598 	switch (f->command) {
7599 	case TC_BLOCK_BIND:
7600 		return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7601 					     np, np, f->extack);
7602 	case TC_BLOCK_UNBIND:
7603 		tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7604 		return 0;
7605 	default:
7606 		return -EOPNOTSUPP;
7607 	}
7608 }
7609 
7610 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7611 			   void *type_data)
7612 {
7613 	switch (type) {
7614 	case TC_SETUP_QDISC_MQPRIO:
7615 		return i40e_setup_tc(netdev, type_data);
7616 	case TC_SETUP_BLOCK:
7617 		return i40e_setup_tc_block(netdev, type_data);
7618 	default:
7619 		return -EOPNOTSUPP;
7620 	}
7621 }
7622 
7623 /**
7624  * i40e_open - Called when a network interface is made active
7625  * @netdev: network interface device structure
7626  *
7627  * The open entry point is called when a network interface is made
7628  * active by the system (IFF_UP).  At this point all resources needed
7629  * for transmit and receive operations are allocated, the interrupt
7630  * handler is registered with the OS, the netdev watchdog subtask is
7631  * enabled, and the stack is notified that the interface is ready.
7632  *
7633  * Returns 0 on success, negative value on failure
7634  **/
7635 int i40e_open(struct net_device *netdev)
7636 {
7637 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7638 	struct i40e_vsi *vsi = np->vsi;
7639 	struct i40e_pf *pf = vsi->back;
7640 	int err;
7641 
7642 	/* disallow open during test or if eeprom is broken */
7643 	if (test_bit(__I40E_TESTING, pf->state) ||
7644 	    test_bit(__I40E_BAD_EEPROM, pf->state))
7645 		return -EBUSY;
7646 
7647 	netif_carrier_off(netdev);
7648 
7649 	if (i40e_force_link_state(pf, true))
7650 		return -EAGAIN;
7651 
7652 	err = i40e_vsi_open(vsi);
7653 	if (err)
7654 		return err;
7655 
7656 	/* configure global TSO hardware offload settings */
7657 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7658 						       TCP_FLAG_FIN) >> 16);
7659 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7660 						       TCP_FLAG_FIN |
7661 						       TCP_FLAG_CWR) >> 16);
7662 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7663 
7664 	udp_tunnel_get_rx_info(netdev);
7665 
7666 	return 0;
7667 }
7668 
7669 /**
7670  * i40e_vsi_open -
7671  * @vsi: the VSI to open
7672  *
7673  * Finish initialization of the VSI.
7674  *
7675  * Returns 0 on success, negative value on failure
7676  *
7677  * Note: expects to be called while under rtnl_lock()
7678  **/
7679 int i40e_vsi_open(struct i40e_vsi *vsi)
7680 {
7681 	struct i40e_pf *pf = vsi->back;
7682 	char int_name[I40E_INT_NAME_STR_LEN];
7683 	int err;
7684 
7685 	/* allocate descriptors */
7686 	err = i40e_vsi_setup_tx_resources(vsi);
7687 	if (err)
7688 		goto err_setup_tx;
7689 	err = i40e_vsi_setup_rx_resources(vsi);
7690 	if (err)
7691 		goto err_setup_rx;
7692 
7693 	err = i40e_vsi_configure(vsi);
7694 	if (err)
7695 		goto err_setup_rx;
7696 
7697 	if (vsi->netdev) {
7698 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7699 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7700 		err = i40e_vsi_request_irq(vsi, int_name);
7701 		if (err)
7702 			goto err_setup_rx;
7703 
7704 		/* Notify the stack of the actual queue counts. */
7705 		err = netif_set_real_num_tx_queues(vsi->netdev,
7706 						   vsi->num_queue_pairs);
7707 		if (err)
7708 			goto err_set_queues;
7709 
7710 		err = netif_set_real_num_rx_queues(vsi->netdev,
7711 						   vsi->num_queue_pairs);
7712 		if (err)
7713 			goto err_set_queues;
7714 
7715 	} else if (vsi->type == I40E_VSI_FDIR) {
7716 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7717 			 dev_driver_string(&pf->pdev->dev),
7718 			 dev_name(&pf->pdev->dev));
7719 		err = i40e_vsi_request_irq(vsi, int_name);
7720 
7721 	} else {
7722 		err = -EINVAL;
7723 		goto err_setup_rx;
7724 	}
7725 
7726 	err = i40e_up_complete(vsi);
7727 	if (err)
7728 		goto err_up_complete;
7729 
7730 	return 0;
7731 
7732 err_up_complete:
7733 	i40e_down(vsi);
7734 err_set_queues:
7735 	i40e_vsi_free_irq(vsi);
7736 err_setup_rx:
7737 	i40e_vsi_free_rx_resources(vsi);
7738 err_setup_tx:
7739 	i40e_vsi_free_tx_resources(vsi);
7740 	if (vsi == pf->vsi[pf->lan_vsi])
7741 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7742 
7743 	return err;
7744 }
7745 
7746 /**
7747  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7748  * @pf: Pointer to PF
7749  *
7750  * This function destroys the hlist where all the Flow Director
7751  * filters were saved.
7752  **/
7753 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7754 {
7755 	struct i40e_fdir_filter *filter;
7756 	struct i40e_flex_pit *pit_entry, *tmp;
7757 	struct hlist_node *node2;
7758 
7759 	hlist_for_each_entry_safe(filter, node2,
7760 				  &pf->fdir_filter_list, fdir_node) {
7761 		hlist_del(&filter->fdir_node);
7762 		kfree(filter);
7763 	}
7764 
7765 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7766 		list_del(&pit_entry->list);
7767 		kfree(pit_entry);
7768 	}
7769 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7770 
7771 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7772 		list_del(&pit_entry->list);
7773 		kfree(pit_entry);
7774 	}
7775 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7776 
7777 	pf->fdir_pf_active_filters = 0;
7778 	pf->fd_tcp4_filter_cnt = 0;
7779 	pf->fd_udp4_filter_cnt = 0;
7780 	pf->fd_sctp4_filter_cnt = 0;
7781 	pf->fd_ip4_filter_cnt = 0;
7782 
7783 	/* Reprogram the default input set for TCP/IPv4 */
7784 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7785 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7786 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7787 
7788 	/* Reprogram the default input set for UDP/IPv4 */
7789 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7790 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7791 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7792 
7793 	/* Reprogram the default input set for SCTP/IPv4 */
7794 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7795 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7796 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7797 
7798 	/* Reprogram the default input set for Other/IPv4 */
7799 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7800 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7801 
7802 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7803 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7804 }
7805 
7806 /**
7807  * i40e_cloud_filter_exit - Cleans up the cloud filters
7808  * @pf: Pointer to PF
7809  *
7810  * This function destroys the hlist where all the cloud filters
7811  * were saved.
7812  **/
7813 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7814 {
7815 	struct i40e_cloud_filter *cfilter;
7816 	struct hlist_node *node;
7817 
7818 	hlist_for_each_entry_safe(cfilter, node,
7819 				  &pf->cloud_filter_list, cloud_node) {
7820 		hlist_del(&cfilter->cloud_node);
7821 		kfree(cfilter);
7822 	}
7823 	pf->num_cloud_filters = 0;
7824 
7825 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7826 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7827 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7828 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7829 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7830 	}
7831 }
7832 
7833 /**
7834  * i40e_close - Disables a network interface
7835  * @netdev: network interface device structure
7836  *
7837  * The close entry point is called when an interface is de-activated
7838  * by the OS.  The hardware is still under the driver's control, but
7839  * this netdev interface is disabled.
7840  *
7841  * Returns 0, this is not allowed to fail
7842  **/
7843 int i40e_close(struct net_device *netdev)
7844 {
7845 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7846 	struct i40e_vsi *vsi = np->vsi;
7847 
7848 	i40e_vsi_close(vsi);
7849 
7850 	return 0;
7851 }
7852 
7853 /**
7854  * i40e_do_reset - Start a PF or Core Reset sequence
7855  * @pf: board private structure
7856  * @reset_flags: which reset is requested
7857  * @lock_acquired: indicates whether or not the lock has been acquired
7858  * before this function was called.
7859  *
7860  * The essential difference in resets is that the PF Reset
7861  * doesn't clear the packet buffers, doesn't reset the PE
7862  * firmware, and doesn't bother the other PFs on the chip.
7863  **/
7864 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7865 {
7866 	u32 val;
7867 
7868 	WARN_ON(in_interrupt());
7869 
7870 
7871 	/* do the biggest reset indicated */
7872 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7873 
7874 		/* Request a Global Reset
7875 		 *
7876 		 * This will start the chip's countdown to the actual full
7877 		 * chip reset event, and a warning interrupt to be sent
7878 		 * to all PFs, including the requestor.  Our handler
7879 		 * for the warning interrupt will deal with the shutdown
7880 		 * and recovery of the switch setup.
7881 		 */
7882 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7883 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7884 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7885 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7886 
7887 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7888 
7889 		/* Request a Core Reset
7890 		 *
7891 		 * Same as Global Reset, except does *not* include the MAC/PHY
7892 		 */
7893 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7894 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7895 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
7896 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7897 		i40e_flush(&pf->hw);
7898 
7899 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
7900 
7901 		/* Request a PF Reset
7902 		 *
7903 		 * Resets only the PF-specific registers
7904 		 *
7905 		 * This goes directly to the tear-down and rebuild of
7906 		 * the switch, since we need to do all the recovery as
7907 		 * for the Core Reset.
7908 		 */
7909 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
7910 		i40e_handle_reset_warning(pf, lock_acquired);
7911 
7912 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7913 		int v;
7914 
7915 		/* Find the VSI(s) that requested a re-init */
7916 		dev_info(&pf->pdev->dev,
7917 			 "VSI reinit requested\n");
7918 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7919 			struct i40e_vsi *vsi = pf->vsi[v];
7920 
7921 			if (vsi != NULL &&
7922 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7923 					       vsi->state))
7924 				i40e_vsi_reinit_locked(pf->vsi[v]);
7925 		}
7926 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7927 		int v;
7928 
7929 		/* Find the VSI(s) that needs to be brought down */
7930 		dev_info(&pf->pdev->dev, "VSI down requested\n");
7931 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7932 			struct i40e_vsi *vsi = pf->vsi[v];
7933 
7934 			if (vsi != NULL &&
7935 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7936 					       vsi->state)) {
7937 				set_bit(__I40E_VSI_DOWN, vsi->state);
7938 				i40e_down(vsi);
7939 			}
7940 		}
7941 	} else {
7942 		dev_info(&pf->pdev->dev,
7943 			 "bad reset request 0x%08x\n", reset_flags);
7944 	}
7945 }
7946 
7947 #ifdef CONFIG_I40E_DCB
7948 /**
7949  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7950  * @pf: board private structure
7951  * @old_cfg: current DCB config
7952  * @new_cfg: new DCB config
7953  **/
7954 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7955 			    struct i40e_dcbx_config *old_cfg,
7956 			    struct i40e_dcbx_config *new_cfg)
7957 {
7958 	bool need_reconfig = false;
7959 
7960 	/* Check if ETS configuration has changed */
7961 	if (memcmp(&new_cfg->etscfg,
7962 		   &old_cfg->etscfg,
7963 		   sizeof(new_cfg->etscfg))) {
7964 		/* If Priority Table has changed reconfig is needed */
7965 		if (memcmp(&new_cfg->etscfg.prioritytable,
7966 			   &old_cfg->etscfg.prioritytable,
7967 			   sizeof(new_cfg->etscfg.prioritytable))) {
7968 			need_reconfig = true;
7969 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
7970 		}
7971 
7972 		if (memcmp(&new_cfg->etscfg.tcbwtable,
7973 			   &old_cfg->etscfg.tcbwtable,
7974 			   sizeof(new_cfg->etscfg.tcbwtable)))
7975 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
7976 
7977 		if (memcmp(&new_cfg->etscfg.tsatable,
7978 			   &old_cfg->etscfg.tsatable,
7979 			   sizeof(new_cfg->etscfg.tsatable)))
7980 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
7981 	}
7982 
7983 	/* Check if PFC configuration has changed */
7984 	if (memcmp(&new_cfg->pfc,
7985 		   &old_cfg->pfc,
7986 		   sizeof(new_cfg->pfc))) {
7987 		need_reconfig = true;
7988 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
7989 	}
7990 
7991 	/* Check if APP Table has changed */
7992 	if (memcmp(&new_cfg->app,
7993 		   &old_cfg->app,
7994 		   sizeof(new_cfg->app))) {
7995 		need_reconfig = true;
7996 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
7997 	}
7998 
7999 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8000 	return need_reconfig;
8001 }
8002 
8003 /**
8004  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8005  * @pf: board private structure
8006  * @e: event info posted on ARQ
8007  **/
8008 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8009 				  struct i40e_arq_event_info *e)
8010 {
8011 	struct i40e_aqc_lldp_get_mib *mib =
8012 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8013 	struct i40e_hw *hw = &pf->hw;
8014 	struct i40e_dcbx_config tmp_dcbx_cfg;
8015 	bool need_reconfig = false;
8016 	int ret = 0;
8017 	u8 type;
8018 
8019 	/* Not DCB capable or capability disabled */
8020 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8021 		return ret;
8022 
8023 	/* Ignore if event is not for Nearest Bridge */
8024 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8025 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8026 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8027 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8028 		return ret;
8029 
8030 	/* Check MIB Type and return if event for Remote MIB update */
8031 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8032 	dev_dbg(&pf->pdev->dev,
8033 		"LLDP event mib type %s\n", type ? "remote" : "local");
8034 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8035 		/* Update the remote cached instance and return */
8036 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8037 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8038 				&hw->remote_dcbx_config);
8039 		goto exit;
8040 	}
8041 
8042 	/* Store the old configuration */
8043 	tmp_dcbx_cfg = hw->local_dcbx_config;
8044 
8045 	/* Reset the old DCBx configuration data */
8046 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8047 	/* Get updated DCBX data from firmware */
8048 	ret = i40e_get_dcb_config(&pf->hw);
8049 	if (ret) {
8050 		dev_info(&pf->pdev->dev,
8051 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8052 			 i40e_stat_str(&pf->hw, ret),
8053 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8054 		goto exit;
8055 	}
8056 
8057 	/* No change detected in DCBX configs */
8058 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8059 		    sizeof(tmp_dcbx_cfg))) {
8060 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8061 		goto exit;
8062 	}
8063 
8064 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8065 					       &hw->local_dcbx_config);
8066 
8067 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8068 
8069 	if (!need_reconfig)
8070 		goto exit;
8071 
8072 	/* Enable DCB tagging only when more than one TC */
8073 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8074 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8075 	else
8076 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8077 
8078 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8079 	/* Reconfiguration needed quiesce all VSIs */
8080 	i40e_pf_quiesce_all_vsi(pf);
8081 
8082 	/* Changes in configuration update VEB/VSI */
8083 	i40e_dcb_reconfigure(pf);
8084 
8085 	ret = i40e_resume_port_tx(pf);
8086 
8087 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8088 	/* In case of error no point in resuming VSIs */
8089 	if (ret)
8090 		goto exit;
8091 
8092 	/* Wait for the PF's queues to be disabled */
8093 	ret = i40e_pf_wait_queues_disabled(pf);
8094 	if (ret) {
8095 		/* Schedule PF reset to recover */
8096 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8097 		i40e_service_event_schedule(pf);
8098 	} else {
8099 		i40e_pf_unquiesce_all_vsi(pf);
8100 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8101 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8102 	}
8103 
8104 exit:
8105 	return ret;
8106 }
8107 #endif /* CONFIG_I40E_DCB */
8108 
8109 /**
8110  * i40e_do_reset_safe - Protected reset path for userland calls.
8111  * @pf: board private structure
8112  * @reset_flags: which reset is requested
8113  *
8114  **/
8115 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8116 {
8117 	rtnl_lock();
8118 	i40e_do_reset(pf, reset_flags, true);
8119 	rtnl_unlock();
8120 }
8121 
8122 /**
8123  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8124  * @pf: board private structure
8125  * @e: event info posted on ARQ
8126  *
8127  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8128  * and VF queues
8129  **/
8130 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8131 					   struct i40e_arq_event_info *e)
8132 {
8133 	struct i40e_aqc_lan_overflow *data =
8134 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8135 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8136 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8137 	struct i40e_hw *hw = &pf->hw;
8138 	struct i40e_vf *vf;
8139 	u16 vf_id;
8140 
8141 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8142 		queue, qtx_ctl);
8143 
8144 	/* Queue belongs to VF, find the VF and issue VF reset */
8145 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8146 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8147 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8148 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8149 		vf_id -= hw->func_caps.vf_base_id;
8150 		vf = &pf->vf[vf_id];
8151 		i40e_vc_notify_vf_reset(vf);
8152 		/* Allow VF to process pending reset notification */
8153 		msleep(20);
8154 		i40e_reset_vf(vf, false);
8155 	}
8156 }
8157 
8158 /**
8159  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8160  * @pf: board private structure
8161  **/
8162 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8163 {
8164 	u32 val, fcnt_prog;
8165 
8166 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8167 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8168 	return fcnt_prog;
8169 }
8170 
8171 /**
8172  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8173  * @pf: board private structure
8174  **/
8175 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8176 {
8177 	u32 val, fcnt_prog;
8178 
8179 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8180 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8181 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8182 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8183 	return fcnt_prog;
8184 }
8185 
8186 /**
8187  * i40e_get_global_fd_count - Get total FD filters programmed on device
8188  * @pf: board private structure
8189  **/
8190 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8191 {
8192 	u32 val, fcnt_prog;
8193 
8194 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8195 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8196 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8197 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8198 	return fcnt_prog;
8199 }
8200 
8201 /**
8202  * i40e_reenable_fdir_sb - Restore FDir SB capability
8203  * @pf: board private structure
8204  **/
8205 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8206 {
8207 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8208 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8209 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8210 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8211 }
8212 
8213 /**
8214  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8215  * @pf: board private structure
8216  **/
8217 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8218 {
8219 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8220 		/* ATR uses the same filtering logic as SB rules. It only
8221 		 * functions properly if the input set mask is at the default
8222 		 * settings. It is safe to restore the default input set
8223 		 * because there are no active TCPv4 filter rules.
8224 		 */
8225 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8226 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8227 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8228 
8229 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8230 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8231 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8232 	}
8233 }
8234 
8235 /**
8236  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8237  * @pf: board private structure
8238  * @filter: FDir filter to remove
8239  */
8240 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8241 				       struct i40e_fdir_filter *filter)
8242 {
8243 	/* Update counters */
8244 	pf->fdir_pf_active_filters--;
8245 	pf->fd_inv = 0;
8246 
8247 	switch (filter->flow_type) {
8248 	case TCP_V4_FLOW:
8249 		pf->fd_tcp4_filter_cnt--;
8250 		break;
8251 	case UDP_V4_FLOW:
8252 		pf->fd_udp4_filter_cnt--;
8253 		break;
8254 	case SCTP_V4_FLOW:
8255 		pf->fd_sctp4_filter_cnt--;
8256 		break;
8257 	case IP_USER_FLOW:
8258 		switch (filter->ip4_proto) {
8259 		case IPPROTO_TCP:
8260 			pf->fd_tcp4_filter_cnt--;
8261 			break;
8262 		case IPPROTO_UDP:
8263 			pf->fd_udp4_filter_cnt--;
8264 			break;
8265 		case IPPROTO_SCTP:
8266 			pf->fd_sctp4_filter_cnt--;
8267 			break;
8268 		case IPPROTO_IP:
8269 			pf->fd_ip4_filter_cnt--;
8270 			break;
8271 		}
8272 		break;
8273 	}
8274 
8275 	/* Remove the filter from the list and free memory */
8276 	hlist_del(&filter->fdir_node);
8277 	kfree(filter);
8278 }
8279 
8280 /**
8281  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8282  * @pf: board private structure
8283  **/
8284 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8285 {
8286 	struct i40e_fdir_filter *filter;
8287 	u32 fcnt_prog, fcnt_avail;
8288 	struct hlist_node *node;
8289 
8290 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8291 		return;
8292 
8293 	/* Check if we have enough room to re-enable FDir SB capability. */
8294 	fcnt_prog = i40e_get_global_fd_count(pf);
8295 	fcnt_avail = pf->fdir_pf_filter_count;
8296 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8297 	    (pf->fd_add_err == 0) ||
8298 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8299 		i40e_reenable_fdir_sb(pf);
8300 
8301 	/* We should wait for even more space before re-enabling ATR.
8302 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8303 	 * rules active.
8304 	 */
8305 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8306 	    (pf->fd_tcp4_filter_cnt == 0))
8307 		i40e_reenable_fdir_atr(pf);
8308 
8309 	/* if hw had a problem adding a filter, delete it */
8310 	if (pf->fd_inv > 0) {
8311 		hlist_for_each_entry_safe(filter, node,
8312 					  &pf->fdir_filter_list, fdir_node)
8313 			if (filter->fd_id == pf->fd_inv)
8314 				i40e_delete_invalid_filter(pf, filter);
8315 	}
8316 }
8317 
8318 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8319 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8320 /**
8321  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8322  * @pf: board private structure
8323  **/
8324 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8325 {
8326 	unsigned long min_flush_time;
8327 	int flush_wait_retry = 50;
8328 	bool disable_atr = false;
8329 	int fd_room;
8330 	int reg;
8331 
8332 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8333 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8334 		return;
8335 
8336 	/* If the flush is happening too quick and we have mostly SB rules we
8337 	 * should not re-enable ATR for some time.
8338 	 */
8339 	min_flush_time = pf->fd_flush_timestamp +
8340 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8341 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8342 
8343 	if (!(time_after(jiffies, min_flush_time)) &&
8344 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8345 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8346 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8347 		disable_atr = true;
8348 	}
8349 
8350 	pf->fd_flush_timestamp = jiffies;
8351 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8352 	/* flush all filters */
8353 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8354 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8355 	i40e_flush(&pf->hw);
8356 	pf->fd_flush_cnt++;
8357 	pf->fd_add_err = 0;
8358 	do {
8359 		/* Check FD flush status every 5-6msec */
8360 		usleep_range(5000, 6000);
8361 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8362 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8363 			break;
8364 	} while (flush_wait_retry--);
8365 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8366 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8367 	} else {
8368 		/* replay sideband filters */
8369 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8370 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8371 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8372 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8373 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8374 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8375 	}
8376 }
8377 
8378 /**
8379  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8380  * @pf: board private structure
8381  **/
8382 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8383 {
8384 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8385 }
8386 
8387 /* We can see up to 256 filter programming desc in transit if the filters are
8388  * being applied really fast; before we see the first
8389  * filter miss error on Rx queue 0. Accumulating enough error messages before
8390  * reacting will make sure we don't cause flush too often.
8391  */
8392 #define I40E_MAX_FD_PROGRAM_ERROR 256
8393 
8394 /**
8395  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8396  * @pf: board private structure
8397  **/
8398 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8399 {
8400 
8401 	/* if interface is down do nothing */
8402 	if (test_bit(__I40E_DOWN, pf->state))
8403 		return;
8404 
8405 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8406 		i40e_fdir_flush_and_replay(pf);
8407 
8408 	i40e_fdir_check_and_reenable(pf);
8409 
8410 }
8411 
8412 /**
8413  * i40e_vsi_link_event - notify VSI of a link event
8414  * @vsi: vsi to be notified
8415  * @link_up: link up or down
8416  **/
8417 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8418 {
8419 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8420 		return;
8421 
8422 	switch (vsi->type) {
8423 	case I40E_VSI_MAIN:
8424 		if (!vsi->netdev || !vsi->netdev_registered)
8425 			break;
8426 
8427 		if (link_up) {
8428 			netif_carrier_on(vsi->netdev);
8429 			netif_tx_wake_all_queues(vsi->netdev);
8430 		} else {
8431 			netif_carrier_off(vsi->netdev);
8432 			netif_tx_stop_all_queues(vsi->netdev);
8433 		}
8434 		break;
8435 
8436 	case I40E_VSI_SRIOV:
8437 	case I40E_VSI_VMDQ2:
8438 	case I40E_VSI_CTRL:
8439 	case I40E_VSI_IWARP:
8440 	case I40E_VSI_MIRROR:
8441 	default:
8442 		/* there is no notification for other VSIs */
8443 		break;
8444 	}
8445 }
8446 
8447 /**
8448  * i40e_veb_link_event - notify elements on the veb of a link event
8449  * @veb: veb to be notified
8450  * @link_up: link up or down
8451  **/
8452 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8453 {
8454 	struct i40e_pf *pf;
8455 	int i;
8456 
8457 	if (!veb || !veb->pf)
8458 		return;
8459 	pf = veb->pf;
8460 
8461 	/* depth first... */
8462 	for (i = 0; i < I40E_MAX_VEB; i++)
8463 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8464 			i40e_veb_link_event(pf->veb[i], link_up);
8465 
8466 	/* ... now the local VSIs */
8467 	for (i = 0; i < pf->num_alloc_vsi; i++)
8468 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8469 			i40e_vsi_link_event(pf->vsi[i], link_up);
8470 }
8471 
8472 /**
8473  * i40e_link_event - Update netif_carrier status
8474  * @pf: board private structure
8475  **/
8476 static void i40e_link_event(struct i40e_pf *pf)
8477 {
8478 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8479 	u8 new_link_speed, old_link_speed;
8480 	i40e_status status;
8481 	bool new_link, old_link;
8482 
8483 	/* set this to force the get_link_status call to refresh state */
8484 	pf->hw.phy.get_link_info = true;
8485 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8486 	status = i40e_get_link_status(&pf->hw, &new_link);
8487 
8488 	/* On success, disable temp link polling */
8489 	if (status == I40E_SUCCESS) {
8490 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8491 	} else {
8492 		/* Enable link polling temporarily until i40e_get_link_status
8493 		 * returns I40E_SUCCESS
8494 		 */
8495 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8496 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8497 			status);
8498 		return;
8499 	}
8500 
8501 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
8502 	new_link_speed = pf->hw.phy.link_info.link_speed;
8503 
8504 	if (new_link == old_link &&
8505 	    new_link_speed == old_link_speed &&
8506 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8507 	     new_link == netif_carrier_ok(vsi->netdev)))
8508 		return;
8509 
8510 	i40e_print_link_message(vsi, new_link);
8511 
8512 	/* Notify the base of the switch tree connected to
8513 	 * the link.  Floating VEBs are not notified.
8514 	 */
8515 	if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8516 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8517 	else
8518 		i40e_vsi_link_event(vsi, new_link);
8519 
8520 	if (pf->vf)
8521 		i40e_vc_notify_link_state(pf);
8522 
8523 	if (pf->flags & I40E_FLAG_PTP)
8524 		i40e_ptp_set_increment(pf);
8525 }
8526 
8527 /**
8528  * i40e_watchdog_subtask - periodic checks not using event driven response
8529  * @pf: board private structure
8530  **/
8531 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8532 {
8533 	int i;
8534 
8535 	/* if interface is down do nothing */
8536 	if (test_bit(__I40E_DOWN, pf->state) ||
8537 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
8538 		return;
8539 
8540 	/* make sure we don't do these things too often */
8541 	if (time_before(jiffies, (pf->service_timer_previous +
8542 				  pf->service_timer_period)))
8543 		return;
8544 	pf->service_timer_previous = jiffies;
8545 
8546 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8547 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
8548 		i40e_link_event(pf);
8549 
8550 	/* Update the stats for active netdevs so the network stack
8551 	 * can look at updated numbers whenever it cares to
8552 	 */
8553 	for (i = 0; i < pf->num_alloc_vsi; i++)
8554 		if (pf->vsi[i] && pf->vsi[i]->netdev)
8555 			i40e_update_stats(pf->vsi[i]);
8556 
8557 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8558 		/* Update the stats for the active switching components */
8559 		for (i = 0; i < I40E_MAX_VEB; i++)
8560 			if (pf->veb[i])
8561 				i40e_update_veb_stats(pf->veb[i]);
8562 	}
8563 
8564 	i40e_ptp_rx_hang(pf);
8565 	i40e_ptp_tx_hang(pf);
8566 }
8567 
8568 /**
8569  * i40e_reset_subtask - Set up for resetting the device and driver
8570  * @pf: board private structure
8571  **/
8572 static void i40e_reset_subtask(struct i40e_pf *pf)
8573 {
8574 	u32 reset_flags = 0;
8575 
8576 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8577 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8578 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8579 	}
8580 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8581 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8582 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8583 	}
8584 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8585 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8586 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8587 	}
8588 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8589 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8590 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8591 	}
8592 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8593 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8594 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8595 	}
8596 
8597 	/* If there's a recovery already waiting, it takes
8598 	 * precedence before starting a new reset sequence.
8599 	 */
8600 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8601 		i40e_prep_for_reset(pf, false);
8602 		i40e_reset(pf);
8603 		i40e_rebuild(pf, false, false);
8604 	}
8605 
8606 	/* If we're already down or resetting, just bail */
8607 	if (reset_flags &&
8608 	    !test_bit(__I40E_DOWN, pf->state) &&
8609 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8610 		i40e_do_reset(pf, reset_flags, false);
8611 	}
8612 }
8613 
8614 /**
8615  * i40e_handle_link_event - Handle link event
8616  * @pf: board private structure
8617  * @e: event info posted on ARQ
8618  **/
8619 static void i40e_handle_link_event(struct i40e_pf *pf,
8620 				   struct i40e_arq_event_info *e)
8621 {
8622 	struct i40e_aqc_get_link_status *status =
8623 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8624 
8625 	/* Do a new status request to re-enable LSE reporting
8626 	 * and load new status information into the hw struct
8627 	 * This completely ignores any state information
8628 	 * in the ARQ event info, instead choosing to always
8629 	 * issue the AQ update link status command.
8630 	 */
8631 	i40e_link_event(pf);
8632 
8633 	/* Check if module meets thermal requirements */
8634 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8635 		dev_err(&pf->pdev->dev,
8636 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8637 		dev_err(&pf->pdev->dev,
8638 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8639 	} else {
8640 		/* check for unqualified module, if link is down, suppress
8641 		 * the message if link was forced to be down.
8642 		 */
8643 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8644 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8645 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
8646 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8647 			dev_err(&pf->pdev->dev,
8648 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8649 			dev_err(&pf->pdev->dev,
8650 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8651 		}
8652 	}
8653 }
8654 
8655 /**
8656  * i40e_clean_adminq_subtask - Clean the AdminQ rings
8657  * @pf: board private structure
8658  **/
8659 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8660 {
8661 	struct i40e_arq_event_info event;
8662 	struct i40e_hw *hw = &pf->hw;
8663 	u16 pending, i = 0;
8664 	i40e_status ret;
8665 	u16 opcode;
8666 	u32 oldval;
8667 	u32 val;
8668 
8669 	/* Do not run clean AQ when PF reset fails */
8670 	if (test_bit(__I40E_RESET_FAILED, pf->state))
8671 		return;
8672 
8673 	/* check for error indications */
8674 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
8675 	oldval = val;
8676 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8677 		if (hw->debug_mask & I40E_DEBUG_AQ)
8678 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8679 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8680 	}
8681 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8682 		if (hw->debug_mask & I40E_DEBUG_AQ)
8683 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8684 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8685 		pf->arq_overflows++;
8686 	}
8687 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8688 		if (hw->debug_mask & I40E_DEBUG_AQ)
8689 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8690 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8691 	}
8692 	if (oldval != val)
8693 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
8694 
8695 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
8696 	oldval = val;
8697 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8698 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8699 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8700 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8701 	}
8702 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8703 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8704 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8705 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8706 	}
8707 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8708 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8709 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8710 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8711 	}
8712 	if (oldval != val)
8713 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
8714 
8715 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8716 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8717 	if (!event.msg_buf)
8718 		return;
8719 
8720 	do {
8721 		ret = i40e_clean_arq_element(hw, &event, &pending);
8722 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8723 			break;
8724 		else if (ret) {
8725 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8726 			break;
8727 		}
8728 
8729 		opcode = le16_to_cpu(event.desc.opcode);
8730 		switch (opcode) {
8731 
8732 		case i40e_aqc_opc_get_link_status:
8733 			i40e_handle_link_event(pf, &event);
8734 			break;
8735 		case i40e_aqc_opc_send_msg_to_pf:
8736 			ret = i40e_vc_process_vf_msg(pf,
8737 					le16_to_cpu(event.desc.retval),
8738 					le32_to_cpu(event.desc.cookie_high),
8739 					le32_to_cpu(event.desc.cookie_low),
8740 					event.msg_buf,
8741 					event.msg_len);
8742 			break;
8743 		case i40e_aqc_opc_lldp_update_mib:
8744 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8745 #ifdef CONFIG_I40E_DCB
8746 			rtnl_lock();
8747 			ret = i40e_handle_lldp_event(pf, &event);
8748 			rtnl_unlock();
8749 #endif /* CONFIG_I40E_DCB */
8750 			break;
8751 		case i40e_aqc_opc_event_lan_overflow:
8752 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8753 			i40e_handle_lan_overflow_event(pf, &event);
8754 			break;
8755 		case i40e_aqc_opc_send_msg_to_peer:
8756 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8757 			break;
8758 		case i40e_aqc_opc_nvm_erase:
8759 		case i40e_aqc_opc_nvm_update:
8760 		case i40e_aqc_opc_oem_post_update:
8761 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8762 				   "ARQ NVM operation 0x%04x completed\n",
8763 				   opcode);
8764 			break;
8765 		default:
8766 			dev_info(&pf->pdev->dev,
8767 				 "ARQ: Unknown event 0x%04x ignored\n",
8768 				 opcode);
8769 			break;
8770 		}
8771 	} while (i++ < pf->adminq_work_limit);
8772 
8773 	if (i < pf->adminq_work_limit)
8774 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8775 
8776 	/* re-enable Admin queue interrupt cause */
8777 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
8778 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8779 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
8780 	i40e_flush(hw);
8781 
8782 	kfree(event.msg_buf);
8783 }
8784 
8785 /**
8786  * i40e_verify_eeprom - make sure eeprom is good to use
8787  * @pf: board private structure
8788  **/
8789 static void i40e_verify_eeprom(struct i40e_pf *pf)
8790 {
8791 	int err;
8792 
8793 	err = i40e_diag_eeprom_test(&pf->hw);
8794 	if (err) {
8795 		/* retry in case of garbage read */
8796 		err = i40e_diag_eeprom_test(&pf->hw);
8797 		if (err) {
8798 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8799 				 err);
8800 			set_bit(__I40E_BAD_EEPROM, pf->state);
8801 		}
8802 	}
8803 
8804 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8805 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8806 		clear_bit(__I40E_BAD_EEPROM, pf->state);
8807 	}
8808 }
8809 
8810 /**
8811  * i40e_enable_pf_switch_lb
8812  * @pf: pointer to the PF structure
8813  *
8814  * enable switch loop back or die - no point in a return value
8815  **/
8816 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8817 {
8818 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8819 	struct i40e_vsi_context ctxt;
8820 	int ret;
8821 
8822 	ctxt.seid = pf->main_vsi_seid;
8823 	ctxt.pf_num = pf->hw.pf_id;
8824 	ctxt.vf_num = 0;
8825 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8826 	if (ret) {
8827 		dev_info(&pf->pdev->dev,
8828 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8829 			 i40e_stat_str(&pf->hw, ret),
8830 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8831 		return;
8832 	}
8833 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8834 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8835 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8836 
8837 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8838 	if (ret) {
8839 		dev_info(&pf->pdev->dev,
8840 			 "update vsi switch failed, err %s aq_err %s\n",
8841 			 i40e_stat_str(&pf->hw, ret),
8842 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8843 	}
8844 }
8845 
8846 /**
8847  * i40e_disable_pf_switch_lb
8848  * @pf: pointer to the PF structure
8849  *
8850  * disable switch loop back or die - no point in a return value
8851  **/
8852 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8853 {
8854 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8855 	struct i40e_vsi_context ctxt;
8856 	int ret;
8857 
8858 	ctxt.seid = pf->main_vsi_seid;
8859 	ctxt.pf_num = pf->hw.pf_id;
8860 	ctxt.vf_num = 0;
8861 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8862 	if (ret) {
8863 		dev_info(&pf->pdev->dev,
8864 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8865 			 i40e_stat_str(&pf->hw, ret),
8866 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8867 		return;
8868 	}
8869 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8870 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8871 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8872 
8873 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8874 	if (ret) {
8875 		dev_info(&pf->pdev->dev,
8876 			 "update vsi switch failed, err %s aq_err %s\n",
8877 			 i40e_stat_str(&pf->hw, ret),
8878 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8879 	}
8880 }
8881 
8882 /**
8883  * i40e_config_bridge_mode - Configure the HW bridge mode
8884  * @veb: pointer to the bridge instance
8885  *
8886  * Configure the loop back mode for the LAN VSI that is downlink to the
8887  * specified HW bridge instance. It is expected this function is called
8888  * when a new HW bridge is instantiated.
8889  **/
8890 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8891 {
8892 	struct i40e_pf *pf = veb->pf;
8893 
8894 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8895 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8896 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8897 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8898 		i40e_disable_pf_switch_lb(pf);
8899 	else
8900 		i40e_enable_pf_switch_lb(pf);
8901 }
8902 
8903 /**
8904  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8905  * @veb: pointer to the VEB instance
8906  *
8907  * This is a recursive function that first builds the attached VSIs then
8908  * recurses in to build the next layer of VEB.  We track the connections
8909  * through our own index numbers because the seid's from the HW could
8910  * change across the reset.
8911  **/
8912 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8913 {
8914 	struct i40e_vsi *ctl_vsi = NULL;
8915 	struct i40e_pf *pf = veb->pf;
8916 	int v, veb_idx;
8917 	int ret;
8918 
8919 	/* build VSI that owns this VEB, temporarily attached to base VEB */
8920 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8921 		if (pf->vsi[v] &&
8922 		    pf->vsi[v]->veb_idx == veb->idx &&
8923 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8924 			ctl_vsi = pf->vsi[v];
8925 			break;
8926 		}
8927 	}
8928 	if (!ctl_vsi) {
8929 		dev_info(&pf->pdev->dev,
8930 			 "missing owner VSI for veb_idx %d\n", veb->idx);
8931 		ret = -ENOENT;
8932 		goto end_reconstitute;
8933 	}
8934 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
8935 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8936 	ret = i40e_add_vsi(ctl_vsi);
8937 	if (ret) {
8938 		dev_info(&pf->pdev->dev,
8939 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
8940 			 veb->idx, ret);
8941 		goto end_reconstitute;
8942 	}
8943 	i40e_vsi_reset_stats(ctl_vsi);
8944 
8945 	/* create the VEB in the switch and move the VSI onto the VEB */
8946 	ret = i40e_add_veb(veb, ctl_vsi);
8947 	if (ret)
8948 		goto end_reconstitute;
8949 
8950 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8951 		veb->bridge_mode = BRIDGE_MODE_VEB;
8952 	else
8953 		veb->bridge_mode = BRIDGE_MODE_VEPA;
8954 	i40e_config_bridge_mode(veb);
8955 
8956 	/* create the remaining VSIs attached to this VEB */
8957 	for (v = 0; v < pf->num_alloc_vsi; v++) {
8958 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8959 			continue;
8960 
8961 		if (pf->vsi[v]->veb_idx == veb->idx) {
8962 			struct i40e_vsi *vsi = pf->vsi[v];
8963 
8964 			vsi->uplink_seid = veb->seid;
8965 			ret = i40e_add_vsi(vsi);
8966 			if (ret) {
8967 				dev_info(&pf->pdev->dev,
8968 					 "rebuild of vsi_idx %d failed: %d\n",
8969 					 v, ret);
8970 				goto end_reconstitute;
8971 			}
8972 			i40e_vsi_reset_stats(vsi);
8973 		}
8974 	}
8975 
8976 	/* create any VEBs attached to this VEB - RECURSION */
8977 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8978 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8979 			pf->veb[veb_idx]->uplink_seid = veb->seid;
8980 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8981 			if (ret)
8982 				break;
8983 		}
8984 	}
8985 
8986 end_reconstitute:
8987 	return ret;
8988 }
8989 
8990 /**
8991  * i40e_get_capabilities - get info about the HW
8992  * @pf: the PF struct
8993  **/
8994 static int i40e_get_capabilities(struct i40e_pf *pf,
8995 				 enum i40e_admin_queue_opc list_type)
8996 {
8997 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8998 	u16 data_size;
8999 	int buf_len;
9000 	int err;
9001 
9002 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9003 	do {
9004 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9005 		if (!cap_buf)
9006 			return -ENOMEM;
9007 
9008 		/* this loads the data into the hw struct for us */
9009 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9010 						    &data_size, list_type,
9011 						    NULL);
9012 		/* data loaded, buffer no longer needed */
9013 		kfree(cap_buf);
9014 
9015 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9016 			/* retry with a larger buffer */
9017 			buf_len = data_size;
9018 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
9019 			dev_info(&pf->pdev->dev,
9020 				 "capability discovery failed, err %s aq_err %s\n",
9021 				 i40e_stat_str(&pf->hw, err),
9022 				 i40e_aq_str(&pf->hw,
9023 					     pf->hw.aq.asq_last_status));
9024 			return -ENODEV;
9025 		}
9026 	} while (err);
9027 
9028 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9029 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9030 			dev_info(&pf->pdev->dev,
9031 				 "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",
9032 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9033 				 pf->hw.func_caps.num_msix_vectors,
9034 				 pf->hw.func_caps.num_msix_vectors_vf,
9035 				 pf->hw.func_caps.fd_filters_guaranteed,
9036 				 pf->hw.func_caps.fd_filters_best_effort,
9037 				 pf->hw.func_caps.num_tx_qp,
9038 				 pf->hw.func_caps.num_vsis);
9039 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9040 			dev_info(&pf->pdev->dev,
9041 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9042 				 pf->hw.dev_caps.switch_mode,
9043 				 pf->hw.dev_caps.valid_functions);
9044 			dev_info(&pf->pdev->dev,
9045 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9046 				 pf->hw.dev_caps.sr_iov_1_1,
9047 				 pf->hw.dev_caps.num_vfs);
9048 			dev_info(&pf->pdev->dev,
9049 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9050 				 pf->hw.dev_caps.num_vsis,
9051 				 pf->hw.dev_caps.num_rx_qp,
9052 				 pf->hw.dev_caps.num_tx_qp);
9053 		}
9054 	}
9055 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9056 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9057 		       + pf->hw.func_caps.num_vfs)
9058 		if (pf->hw.revision_id == 0 &&
9059 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9060 			dev_info(&pf->pdev->dev,
9061 				 "got num_vsis %d, setting num_vsis to %d\n",
9062 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9063 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9064 		}
9065 	}
9066 	return 0;
9067 }
9068 
9069 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9070 
9071 /**
9072  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9073  * @pf: board private structure
9074  **/
9075 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9076 {
9077 	struct i40e_vsi *vsi;
9078 
9079 	/* quick workaround for an NVM issue that leaves a critical register
9080 	 * uninitialized
9081 	 */
9082 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9083 		static const u32 hkey[] = {
9084 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9085 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9086 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9087 			0x95b3a76d};
9088 		int i;
9089 
9090 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9091 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9092 	}
9093 
9094 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9095 		return;
9096 
9097 	/* find existing VSI and see if it needs configuring */
9098 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9099 
9100 	/* create a new VSI if none exists */
9101 	if (!vsi) {
9102 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9103 				     pf->vsi[pf->lan_vsi]->seid, 0);
9104 		if (!vsi) {
9105 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9106 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9107 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9108 			return;
9109 		}
9110 	}
9111 
9112 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9113 }
9114 
9115 /**
9116  * i40e_fdir_teardown - release the Flow Director resources
9117  * @pf: board private structure
9118  **/
9119 static void i40e_fdir_teardown(struct i40e_pf *pf)
9120 {
9121 	struct i40e_vsi *vsi;
9122 
9123 	i40e_fdir_filter_exit(pf);
9124 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9125 	if (vsi)
9126 		i40e_vsi_release(vsi);
9127 }
9128 
9129 /**
9130  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9131  * @vsi: PF main vsi
9132  * @seid: seid of main or channel VSIs
9133  *
9134  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9135  * existed before reset
9136  **/
9137 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9138 {
9139 	struct i40e_cloud_filter *cfilter;
9140 	struct i40e_pf *pf = vsi->back;
9141 	struct hlist_node *node;
9142 	i40e_status ret;
9143 
9144 	/* Add cloud filters back if they exist */
9145 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9146 				  cloud_node) {
9147 		if (cfilter->seid != seid)
9148 			continue;
9149 
9150 		if (cfilter->dst_port)
9151 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9152 								true);
9153 		else
9154 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9155 
9156 		if (ret) {
9157 			dev_dbg(&pf->pdev->dev,
9158 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9159 				i40e_stat_str(&pf->hw, ret),
9160 				i40e_aq_str(&pf->hw,
9161 					    pf->hw.aq.asq_last_status));
9162 			return ret;
9163 		}
9164 	}
9165 	return 0;
9166 }
9167 
9168 /**
9169  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9170  * @vsi: PF main vsi
9171  *
9172  * Rebuilds channel VSIs if they existed before reset
9173  **/
9174 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9175 {
9176 	struct i40e_channel *ch, *ch_tmp;
9177 	i40e_status ret;
9178 
9179 	if (list_empty(&vsi->ch_list))
9180 		return 0;
9181 
9182 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9183 		if (!ch->initialized)
9184 			break;
9185 		/* Proceed with creation of channel (VMDq2) VSI */
9186 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9187 		if (ret) {
9188 			dev_info(&vsi->back->pdev->dev,
9189 				 "failed to rebuild channels using uplink_seid %u\n",
9190 				 vsi->uplink_seid);
9191 			return ret;
9192 		}
9193 		/* Reconfigure TX queues using QTX_CTL register */
9194 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9195 		if (ret) {
9196 			dev_info(&vsi->back->pdev->dev,
9197 				 "failed to configure TX rings for channel %u\n",
9198 				 ch->seid);
9199 			return ret;
9200 		}
9201 		/* update 'next_base_queue' */
9202 		vsi->next_base_queue = vsi->next_base_queue +
9203 							ch->num_queue_pairs;
9204 		if (ch->max_tx_rate) {
9205 			u64 credits = ch->max_tx_rate;
9206 
9207 			if (i40e_set_bw_limit(vsi, ch->seid,
9208 					      ch->max_tx_rate))
9209 				return -EINVAL;
9210 
9211 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9212 			dev_dbg(&vsi->back->pdev->dev,
9213 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9214 				ch->max_tx_rate,
9215 				credits,
9216 				ch->seid);
9217 		}
9218 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9219 		if (ret) {
9220 			dev_dbg(&vsi->back->pdev->dev,
9221 				"Failed to rebuild cloud filters for channel VSI %u\n",
9222 				ch->seid);
9223 			return ret;
9224 		}
9225 	}
9226 	return 0;
9227 }
9228 
9229 /**
9230  * i40e_prep_for_reset - prep for the core to reset
9231  * @pf: board private structure
9232  * @lock_acquired: indicates whether or not the lock has been acquired
9233  * before this function was called.
9234  *
9235  * Close up the VFs and other things in prep for PF Reset.
9236   **/
9237 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9238 {
9239 	struct i40e_hw *hw = &pf->hw;
9240 	i40e_status ret = 0;
9241 	u32 v;
9242 
9243 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9244 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9245 		return;
9246 	if (i40e_check_asq_alive(&pf->hw))
9247 		i40e_vc_notify_reset(pf);
9248 
9249 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9250 
9251 	/* quiesce the VSIs and their queues that are not already DOWN */
9252 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9253 	if (!lock_acquired)
9254 		rtnl_lock();
9255 	i40e_pf_quiesce_all_vsi(pf);
9256 	if (!lock_acquired)
9257 		rtnl_unlock();
9258 
9259 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9260 		if (pf->vsi[v])
9261 			pf->vsi[v]->seid = 0;
9262 	}
9263 
9264 	i40e_shutdown_adminq(&pf->hw);
9265 
9266 	/* call shutdown HMC */
9267 	if (hw->hmc.hmc_obj) {
9268 		ret = i40e_shutdown_lan_hmc(hw);
9269 		if (ret)
9270 			dev_warn(&pf->pdev->dev,
9271 				 "shutdown_lan_hmc failed: %d\n", ret);
9272 	}
9273 }
9274 
9275 /**
9276  * i40e_send_version - update firmware with driver version
9277  * @pf: PF struct
9278  */
9279 static void i40e_send_version(struct i40e_pf *pf)
9280 {
9281 	struct i40e_driver_version dv;
9282 
9283 	dv.major_version = DRV_VERSION_MAJOR;
9284 	dv.minor_version = DRV_VERSION_MINOR;
9285 	dv.build_version = DRV_VERSION_BUILD;
9286 	dv.subbuild_version = 0;
9287 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9288 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9289 }
9290 
9291 /**
9292  * i40e_get_oem_version - get OEM specific version information
9293  * @hw: pointer to the hardware structure
9294  **/
9295 static void i40e_get_oem_version(struct i40e_hw *hw)
9296 {
9297 	u16 block_offset = 0xffff;
9298 	u16 block_length = 0;
9299 	u16 capabilities = 0;
9300 	u16 gen_snap = 0;
9301 	u16 release = 0;
9302 
9303 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9304 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9305 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9306 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9307 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9308 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9309 #define I40E_NVM_OEM_LENGTH			3
9310 
9311 	/* Check if pointer to OEM version block is valid. */
9312 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9313 	if (block_offset == 0xffff)
9314 		return;
9315 
9316 	/* Check if OEM version block has correct length. */
9317 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9318 			   &block_length);
9319 	if (block_length < I40E_NVM_OEM_LENGTH)
9320 		return;
9321 
9322 	/* Check if OEM version format is as expected. */
9323 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9324 			   &capabilities);
9325 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9326 		return;
9327 
9328 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9329 			   &gen_snap);
9330 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9331 			   &release);
9332 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9333 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9334 }
9335 
9336 /**
9337  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9338  * @pf: board private structure
9339  **/
9340 static int i40e_reset(struct i40e_pf *pf)
9341 {
9342 	struct i40e_hw *hw = &pf->hw;
9343 	i40e_status ret;
9344 
9345 	ret = i40e_pf_reset(hw);
9346 	if (ret) {
9347 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9348 		set_bit(__I40E_RESET_FAILED, pf->state);
9349 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9350 	} else {
9351 		pf->pfr_count++;
9352 	}
9353 	return ret;
9354 }
9355 
9356 /**
9357  * i40e_rebuild - rebuild using a saved config
9358  * @pf: board private structure
9359  * @reinit: if the Main VSI needs to re-initialized.
9360  * @lock_acquired: indicates whether or not the lock has been acquired
9361  * before this function was called.
9362  **/
9363 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9364 {
9365 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9366 	struct i40e_hw *hw = &pf->hw;
9367 	u8 set_fc_aq_fail = 0;
9368 	i40e_status ret;
9369 	u32 val;
9370 	int v;
9371 
9372 	if (test_bit(__I40E_DOWN, pf->state))
9373 		goto clear_recovery;
9374 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9375 
9376 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9377 	ret = i40e_init_adminq(&pf->hw);
9378 	if (ret) {
9379 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9380 			 i40e_stat_str(&pf->hw, ret),
9381 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9382 		goto clear_recovery;
9383 	}
9384 	i40e_get_oem_version(&pf->hw);
9385 
9386 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9387 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9388 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9389 		/* The following delay is necessary for 4.33 firmware and older
9390 		 * to recover after EMP reset. 200 ms should suffice but we
9391 		 * put here 300 ms to be sure that FW is ready to operate
9392 		 * after reset.
9393 		 */
9394 		mdelay(300);
9395 	}
9396 
9397 	/* re-verify the eeprom if we just had an EMP reset */
9398 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9399 		i40e_verify_eeprom(pf);
9400 
9401 	i40e_clear_pxe_mode(hw);
9402 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9403 	if (ret)
9404 		goto end_core_reset;
9405 
9406 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9407 				hw->func_caps.num_rx_qp, 0, 0);
9408 	if (ret) {
9409 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9410 		goto end_core_reset;
9411 	}
9412 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9413 	if (ret) {
9414 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9415 		goto end_core_reset;
9416 	}
9417 
9418 	/* Enable FW to write a default DCB config on link-up */
9419 	i40e_aq_set_dcb_parameters(hw, true, NULL);
9420 
9421 #ifdef CONFIG_I40E_DCB
9422 	ret = i40e_init_pf_dcb(pf);
9423 	if (ret) {
9424 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9425 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9426 		/* Continue without DCB enabled */
9427 	}
9428 #endif /* CONFIG_I40E_DCB */
9429 	/* do basic switch setup */
9430 	if (!lock_acquired)
9431 		rtnl_lock();
9432 	ret = i40e_setup_pf_switch(pf, reinit);
9433 	if (ret)
9434 		goto end_unlock;
9435 
9436 	/* The driver only wants link up/down and module qualification
9437 	 * reports from firmware.  Note the negative logic.
9438 	 */
9439 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
9440 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
9441 					 I40E_AQ_EVENT_MEDIA_NA |
9442 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9443 	if (ret)
9444 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9445 			 i40e_stat_str(&pf->hw, ret),
9446 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9447 
9448 	/* make sure our flow control settings are restored */
9449 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9450 	if (ret)
9451 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9452 			i40e_stat_str(&pf->hw, ret),
9453 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9454 
9455 	/* Rebuild the VSIs and VEBs that existed before reset.
9456 	 * They are still in our local switch element arrays, so only
9457 	 * need to rebuild the switch model in the HW.
9458 	 *
9459 	 * If there were VEBs but the reconstitution failed, we'll try
9460 	 * try to recover minimal use by getting the basic PF VSI working.
9461 	 */
9462 	if (vsi->uplink_seid != pf->mac_seid) {
9463 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9464 		/* find the one VEB connected to the MAC, and find orphans */
9465 		for (v = 0; v < I40E_MAX_VEB; v++) {
9466 			if (!pf->veb[v])
9467 				continue;
9468 
9469 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9470 			    pf->veb[v]->uplink_seid == 0) {
9471 				ret = i40e_reconstitute_veb(pf->veb[v]);
9472 
9473 				if (!ret)
9474 					continue;
9475 
9476 				/* If Main VEB failed, we're in deep doodoo,
9477 				 * so give up rebuilding the switch and set up
9478 				 * for minimal rebuild of PF VSI.
9479 				 * If orphan failed, we'll report the error
9480 				 * but try to keep going.
9481 				 */
9482 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9483 					dev_info(&pf->pdev->dev,
9484 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9485 						 ret);
9486 					vsi->uplink_seid = pf->mac_seid;
9487 					break;
9488 				} else if (pf->veb[v]->uplink_seid == 0) {
9489 					dev_info(&pf->pdev->dev,
9490 						 "rebuild of orphan VEB failed: %d\n",
9491 						 ret);
9492 				}
9493 			}
9494 		}
9495 	}
9496 
9497 	if (vsi->uplink_seid == pf->mac_seid) {
9498 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9499 		/* no VEB, so rebuild only the Main VSI */
9500 		ret = i40e_add_vsi(vsi);
9501 		if (ret) {
9502 			dev_info(&pf->pdev->dev,
9503 				 "rebuild of Main VSI failed: %d\n", ret);
9504 			goto end_unlock;
9505 		}
9506 	}
9507 
9508 	if (vsi->mqprio_qopt.max_rate[0]) {
9509 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9510 		u64 credits = 0;
9511 
9512 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9513 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9514 		if (ret)
9515 			goto end_unlock;
9516 
9517 		credits = max_tx_rate;
9518 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
9519 		dev_dbg(&vsi->back->pdev->dev,
9520 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9521 			max_tx_rate,
9522 			credits,
9523 			vsi->seid);
9524 	}
9525 
9526 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9527 	if (ret)
9528 		goto end_unlock;
9529 
9530 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9531 	 * for this main VSI if they exist
9532 	 */
9533 	ret = i40e_rebuild_channels(vsi);
9534 	if (ret)
9535 		goto end_unlock;
9536 
9537 	/* Reconfigure hardware for allowing smaller MSS in the case
9538 	 * of TSO, so that we avoid the MDD being fired and causing
9539 	 * a reset in the case of small MSS+TSO.
9540 	 */
9541 #define I40E_REG_MSS          0x000E64DC
9542 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9543 #define I40E_64BYTE_MSS       0x400000
9544 	val = rd32(hw, I40E_REG_MSS);
9545 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9546 		val &= ~I40E_REG_MSS_MIN_MASK;
9547 		val |= I40E_64BYTE_MSS;
9548 		wr32(hw, I40E_REG_MSS, val);
9549 	}
9550 
9551 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9552 		msleep(75);
9553 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9554 		if (ret)
9555 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9556 				 i40e_stat_str(&pf->hw, ret),
9557 				 i40e_aq_str(&pf->hw,
9558 					     pf->hw.aq.asq_last_status));
9559 	}
9560 	/* reinit the misc interrupt */
9561 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9562 		ret = i40e_setup_misc_vector(pf);
9563 
9564 	/* Add a filter to drop all Flow control frames from any VSI from being
9565 	 * transmitted. By doing so we stop a malicious VF from sending out
9566 	 * PAUSE or PFC frames and potentially controlling traffic for other
9567 	 * PF/VF VSIs.
9568 	 * The FW can still send Flow control frames if enabled.
9569 	 */
9570 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9571 						       pf->main_vsi_seid);
9572 
9573 	/* restart the VSIs that were rebuilt and running before the reset */
9574 	i40e_pf_unquiesce_all_vsi(pf);
9575 
9576 	/* Release the RTNL lock before we start resetting VFs */
9577 	if (!lock_acquired)
9578 		rtnl_unlock();
9579 
9580 	/* Restore promiscuous settings */
9581 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9582 	if (ret)
9583 		dev_warn(&pf->pdev->dev,
9584 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9585 			 pf->cur_promisc ? "on" : "off",
9586 			 i40e_stat_str(&pf->hw, ret),
9587 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9588 
9589 	i40e_reset_all_vfs(pf, true);
9590 
9591 	/* tell the firmware that we're starting */
9592 	i40e_send_version(pf);
9593 
9594 	/* We've already released the lock, so don't do it again */
9595 	goto end_core_reset;
9596 
9597 end_unlock:
9598 	if (!lock_acquired)
9599 		rtnl_unlock();
9600 end_core_reset:
9601 	clear_bit(__I40E_RESET_FAILED, pf->state);
9602 clear_recovery:
9603 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9604 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
9605 }
9606 
9607 /**
9608  * i40e_reset_and_rebuild - reset and rebuild using a saved config
9609  * @pf: board private structure
9610  * @reinit: if the Main VSI needs to re-initialized.
9611  * @lock_acquired: indicates whether or not the lock has been acquired
9612  * before this function was called.
9613  **/
9614 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9615 				   bool lock_acquired)
9616 {
9617 	int ret;
9618 	/* Now we wait for GRST to settle out.
9619 	 * We don't have to delete the VEBs or VSIs from the hw switch
9620 	 * because the reset will make them disappear.
9621 	 */
9622 	ret = i40e_reset(pf);
9623 	if (!ret)
9624 		i40e_rebuild(pf, reinit, lock_acquired);
9625 }
9626 
9627 /**
9628  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9629  * @pf: board private structure
9630  *
9631  * Close up the VFs and other things in prep for a Core Reset,
9632  * then get ready to rebuild the world.
9633  * @lock_acquired: indicates whether or not the lock has been acquired
9634  * before this function was called.
9635  **/
9636 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9637 {
9638 	i40e_prep_for_reset(pf, lock_acquired);
9639 	i40e_reset_and_rebuild(pf, false, lock_acquired);
9640 }
9641 
9642 /**
9643  * i40e_handle_mdd_event
9644  * @pf: pointer to the PF structure
9645  *
9646  * Called from the MDD irq handler to identify possibly malicious vfs
9647  **/
9648 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9649 {
9650 	struct i40e_hw *hw = &pf->hw;
9651 	bool mdd_detected = false;
9652 	bool pf_mdd_detected = false;
9653 	struct i40e_vf *vf;
9654 	u32 reg;
9655 	int i;
9656 
9657 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9658 		return;
9659 
9660 	/* find what triggered the MDD event */
9661 	reg = rd32(hw, I40E_GL_MDET_TX);
9662 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9663 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9664 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
9665 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9666 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
9667 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9668 				I40E_GL_MDET_TX_EVENT_SHIFT;
9669 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9670 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
9671 				pf->hw.func_caps.base_queue;
9672 		if (netif_msg_tx_err(pf))
9673 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9674 				 event, queue, pf_num, vf_num);
9675 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9676 		mdd_detected = true;
9677 	}
9678 	reg = rd32(hw, I40E_GL_MDET_RX);
9679 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9680 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9681 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
9682 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9683 				I40E_GL_MDET_RX_EVENT_SHIFT;
9684 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9685 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
9686 				pf->hw.func_caps.base_queue;
9687 		if (netif_msg_rx_err(pf))
9688 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9689 				 event, queue, func);
9690 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9691 		mdd_detected = true;
9692 	}
9693 
9694 	if (mdd_detected) {
9695 		reg = rd32(hw, I40E_PF_MDET_TX);
9696 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9697 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9698 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
9699 			pf_mdd_detected = true;
9700 		}
9701 		reg = rd32(hw, I40E_PF_MDET_RX);
9702 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9703 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9704 			dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
9705 			pf_mdd_detected = true;
9706 		}
9707 		/* Queue belongs to the PF, initiate a reset */
9708 		if (pf_mdd_detected) {
9709 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9710 			i40e_service_event_schedule(pf);
9711 		}
9712 	}
9713 
9714 	/* see if one of the VFs needs its hand slapped */
9715 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9716 		vf = &(pf->vf[i]);
9717 		reg = rd32(hw, I40E_VP_MDET_TX(i));
9718 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9719 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9720 			vf->num_mdd_events++;
9721 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9722 				 i);
9723 		}
9724 
9725 		reg = rd32(hw, I40E_VP_MDET_RX(i));
9726 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9727 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9728 			vf->num_mdd_events++;
9729 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9730 				 i);
9731 		}
9732 
9733 		if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9734 			dev_info(&pf->pdev->dev,
9735 				 "Too many MDD events on VF %d, disabled\n", i);
9736 			dev_info(&pf->pdev->dev,
9737 				 "Use PF Control I/F to re-enable the VF\n");
9738 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9739 		}
9740 	}
9741 
9742 	/* re-enable mdd interrupt cause */
9743 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9744 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9745 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9746 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9747 	i40e_flush(hw);
9748 }
9749 
9750 static const char *i40e_tunnel_name(u8 type)
9751 {
9752 	switch (type) {
9753 	case UDP_TUNNEL_TYPE_VXLAN:
9754 		return "vxlan";
9755 	case UDP_TUNNEL_TYPE_GENEVE:
9756 		return "geneve";
9757 	default:
9758 		return "unknown";
9759 	}
9760 }
9761 
9762 /**
9763  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9764  * @pf: board private structure
9765  **/
9766 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9767 {
9768 	int i;
9769 
9770 	/* loop through and set pending bit for all active UDP filters */
9771 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9772 		if (pf->udp_ports[i].port)
9773 			pf->pending_udp_bitmap |= BIT_ULL(i);
9774 	}
9775 
9776 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
9777 }
9778 
9779 /**
9780  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9781  * @pf: board private structure
9782  **/
9783 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9784 {
9785 	struct i40e_hw *hw = &pf->hw;
9786 	u8 filter_index, type;
9787 	u16 port;
9788 	int i;
9789 
9790 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
9791 		return;
9792 
9793 	/* acquire RTNL to maintain state of flags and port requests */
9794 	rtnl_lock();
9795 
9796 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9797 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9798 			struct i40e_udp_port_config *udp_port;
9799 			i40e_status ret = 0;
9800 
9801 			udp_port = &pf->udp_ports[i];
9802 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
9803 
9804 			port = READ_ONCE(udp_port->port);
9805 			type = READ_ONCE(udp_port->type);
9806 			filter_index = READ_ONCE(udp_port->filter_index);
9807 
9808 			/* release RTNL while we wait on AQ command */
9809 			rtnl_unlock();
9810 
9811 			if (port)
9812 				ret = i40e_aq_add_udp_tunnel(hw, port,
9813 							     type,
9814 							     &filter_index,
9815 							     NULL);
9816 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
9817 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
9818 							     NULL);
9819 
9820 			/* reacquire RTNL so we can update filter_index */
9821 			rtnl_lock();
9822 
9823 			if (ret) {
9824 				dev_info(&pf->pdev->dev,
9825 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9826 					 i40e_tunnel_name(type),
9827 					 port ? "add" : "delete",
9828 					 port,
9829 					 filter_index,
9830 					 i40e_stat_str(&pf->hw, ret),
9831 					 i40e_aq_str(&pf->hw,
9832 						     pf->hw.aq.asq_last_status));
9833 				if (port) {
9834 					/* failed to add, just reset port,
9835 					 * drop pending bit for any deletion
9836 					 */
9837 					udp_port->port = 0;
9838 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
9839 				}
9840 			} else if (port) {
9841 				/* record filter index on success */
9842 				udp_port->filter_index = filter_index;
9843 			}
9844 		}
9845 	}
9846 
9847 	rtnl_unlock();
9848 }
9849 
9850 /**
9851  * i40e_service_task - Run the driver's async subtasks
9852  * @work: pointer to work_struct containing our data
9853  **/
9854 static void i40e_service_task(struct work_struct *work)
9855 {
9856 	struct i40e_pf *pf = container_of(work,
9857 					  struct i40e_pf,
9858 					  service_task);
9859 	unsigned long start_time = jiffies;
9860 
9861 	/* don't bother with service tasks if a reset is in progress */
9862 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9863 		return;
9864 
9865 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9866 		return;
9867 
9868 	i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
9869 	i40e_sync_filters_subtask(pf);
9870 	i40e_reset_subtask(pf);
9871 	i40e_handle_mdd_event(pf);
9872 	i40e_vc_process_vflr_event(pf);
9873 	i40e_watchdog_subtask(pf);
9874 	i40e_fdir_reinit_subtask(pf);
9875 	if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
9876 		/* Client subtask will reopen next time through. */
9877 		i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9878 	} else {
9879 		i40e_client_subtask(pf);
9880 		if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
9881 				       pf->state))
9882 			i40e_notify_client_of_l2_param_changes(
9883 							pf->vsi[pf->lan_vsi]);
9884 	}
9885 	i40e_sync_filters_subtask(pf);
9886 	i40e_sync_udp_filters_subtask(pf);
9887 	i40e_clean_adminq_subtask(pf);
9888 
9889 	/* flush memory to make sure state is correct before next watchdog */
9890 	smp_mb__before_atomic();
9891 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
9892 
9893 	/* If the tasks have taken longer than one timer cycle or there
9894 	 * is more work to be done, reschedule the service task now
9895 	 * rather than wait for the timer to tick again.
9896 	 */
9897 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
9898 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
9899 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
9900 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
9901 		i40e_service_event_schedule(pf);
9902 }
9903 
9904 /**
9905  * i40e_service_timer - timer callback
9906  * @data: pointer to PF struct
9907  **/
9908 static void i40e_service_timer(struct timer_list *t)
9909 {
9910 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
9911 
9912 	mod_timer(&pf->service_timer,
9913 		  round_jiffies(jiffies + pf->service_timer_period));
9914 	i40e_service_event_schedule(pf);
9915 }
9916 
9917 /**
9918  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9919  * @vsi: the VSI being configured
9920  **/
9921 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9922 {
9923 	struct i40e_pf *pf = vsi->back;
9924 
9925 	switch (vsi->type) {
9926 	case I40E_VSI_MAIN:
9927 		vsi->alloc_queue_pairs = pf->num_lan_qps;
9928 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9929 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9930 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9931 			vsi->num_q_vectors = pf->num_lan_msix;
9932 		else
9933 			vsi->num_q_vectors = 1;
9934 
9935 		break;
9936 
9937 	case I40E_VSI_FDIR:
9938 		vsi->alloc_queue_pairs = 1;
9939 		vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9940 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9941 		vsi->num_q_vectors = pf->num_fdsb_msix;
9942 		break;
9943 
9944 	case I40E_VSI_VMDQ2:
9945 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9946 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9947 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9948 		vsi->num_q_vectors = pf->num_vmdq_msix;
9949 		break;
9950 
9951 	case I40E_VSI_SRIOV:
9952 		vsi->alloc_queue_pairs = pf->num_vf_qps;
9953 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9954 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9955 		break;
9956 
9957 	default:
9958 		WARN_ON(1);
9959 		return -ENODATA;
9960 	}
9961 
9962 	return 0;
9963 }
9964 
9965 /**
9966  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
9967  * @vsi: VSI pointer
9968  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
9969  *
9970  * On error: returns error code (negative)
9971  * On success: returns 0
9972  **/
9973 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
9974 {
9975 	struct i40e_ring **next_rings;
9976 	int size;
9977 	int ret = 0;
9978 
9979 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9980 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9981 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
9982 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9983 	if (!vsi->tx_rings)
9984 		return -ENOMEM;
9985 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9986 	if (i40e_enabled_xdp_vsi(vsi)) {
9987 		vsi->xdp_rings = next_rings;
9988 		next_rings += vsi->alloc_queue_pairs;
9989 	}
9990 	vsi->rx_rings = next_rings;
9991 
9992 	if (alloc_qvectors) {
9993 		/* allocate memory for q_vector pointers */
9994 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
9995 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9996 		if (!vsi->q_vectors) {
9997 			ret = -ENOMEM;
9998 			goto err_vectors;
9999 		}
10000 	}
10001 	return ret;
10002 
10003 err_vectors:
10004 	kfree(vsi->tx_rings);
10005 	return ret;
10006 }
10007 
10008 /**
10009  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10010  * @pf: board private structure
10011  * @type: type of VSI
10012  *
10013  * On error: returns error code (negative)
10014  * On success: returns vsi index in PF (positive)
10015  **/
10016 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10017 {
10018 	int ret = -ENODEV;
10019 	struct i40e_vsi *vsi;
10020 	int vsi_idx;
10021 	int i;
10022 
10023 	/* Need to protect the allocation of the VSIs at the PF level */
10024 	mutex_lock(&pf->switch_mutex);
10025 
10026 	/* VSI list may be fragmented if VSI creation/destruction has
10027 	 * been happening.  We can afford to do a quick scan to look
10028 	 * for any free VSIs in the list.
10029 	 *
10030 	 * find next empty vsi slot, looping back around if necessary
10031 	 */
10032 	i = pf->next_vsi;
10033 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10034 		i++;
10035 	if (i >= pf->num_alloc_vsi) {
10036 		i = 0;
10037 		while (i < pf->next_vsi && pf->vsi[i])
10038 			i++;
10039 	}
10040 
10041 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10042 		vsi_idx = i;             /* Found one! */
10043 	} else {
10044 		ret = -ENODEV;
10045 		goto unlock_pf;  /* out of VSI slots! */
10046 	}
10047 	pf->next_vsi = ++i;
10048 
10049 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10050 	if (!vsi) {
10051 		ret = -ENOMEM;
10052 		goto unlock_pf;
10053 	}
10054 	vsi->type = type;
10055 	vsi->back = pf;
10056 	set_bit(__I40E_VSI_DOWN, vsi->state);
10057 	vsi->flags = 0;
10058 	vsi->idx = vsi_idx;
10059 	vsi->int_rate_limit = 0;
10060 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10061 				pf->rss_table_size : 64;
10062 	vsi->netdev_registered = false;
10063 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10064 	hash_init(vsi->mac_filter_hash);
10065 	vsi->irqs_ready = false;
10066 
10067 	ret = i40e_set_num_rings_in_vsi(vsi);
10068 	if (ret)
10069 		goto err_rings;
10070 
10071 	ret = i40e_vsi_alloc_arrays(vsi, true);
10072 	if (ret)
10073 		goto err_rings;
10074 
10075 	/* Setup default MSIX irq handler for VSI */
10076 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10077 
10078 	/* Initialize VSI lock */
10079 	spin_lock_init(&vsi->mac_filter_hash_lock);
10080 	pf->vsi[vsi_idx] = vsi;
10081 	ret = vsi_idx;
10082 	goto unlock_pf;
10083 
10084 err_rings:
10085 	pf->next_vsi = i - 1;
10086 	kfree(vsi);
10087 unlock_pf:
10088 	mutex_unlock(&pf->switch_mutex);
10089 	return ret;
10090 }
10091 
10092 /**
10093  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10094  * @vsi: VSI pointer
10095  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10096  *
10097  * On error: returns error code (negative)
10098  * On success: returns 0
10099  **/
10100 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10101 {
10102 	/* free the ring and vector containers */
10103 	if (free_qvectors) {
10104 		kfree(vsi->q_vectors);
10105 		vsi->q_vectors = NULL;
10106 	}
10107 	kfree(vsi->tx_rings);
10108 	vsi->tx_rings = NULL;
10109 	vsi->rx_rings = NULL;
10110 	vsi->xdp_rings = NULL;
10111 }
10112 
10113 /**
10114  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10115  * and lookup table
10116  * @vsi: Pointer to VSI structure
10117  */
10118 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10119 {
10120 	if (!vsi)
10121 		return;
10122 
10123 	kfree(vsi->rss_hkey_user);
10124 	vsi->rss_hkey_user = NULL;
10125 
10126 	kfree(vsi->rss_lut_user);
10127 	vsi->rss_lut_user = NULL;
10128 }
10129 
10130 /**
10131  * i40e_vsi_clear - Deallocate the VSI provided
10132  * @vsi: the VSI being un-configured
10133  **/
10134 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10135 {
10136 	struct i40e_pf *pf;
10137 
10138 	if (!vsi)
10139 		return 0;
10140 
10141 	if (!vsi->back)
10142 		goto free_vsi;
10143 	pf = vsi->back;
10144 
10145 	mutex_lock(&pf->switch_mutex);
10146 	if (!pf->vsi[vsi->idx]) {
10147 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10148 			vsi->idx, vsi->idx, vsi->type);
10149 		goto unlock_vsi;
10150 	}
10151 
10152 	if (pf->vsi[vsi->idx] != vsi) {
10153 		dev_err(&pf->pdev->dev,
10154 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10155 			pf->vsi[vsi->idx]->idx,
10156 			pf->vsi[vsi->idx]->type,
10157 			vsi->idx, vsi->type);
10158 		goto unlock_vsi;
10159 	}
10160 
10161 	/* updates the PF for this cleared vsi */
10162 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10163 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10164 
10165 	i40e_vsi_free_arrays(vsi, true);
10166 	i40e_clear_rss_config_user(vsi);
10167 
10168 	pf->vsi[vsi->idx] = NULL;
10169 	if (vsi->idx < pf->next_vsi)
10170 		pf->next_vsi = vsi->idx;
10171 
10172 unlock_vsi:
10173 	mutex_unlock(&pf->switch_mutex);
10174 free_vsi:
10175 	kfree(vsi);
10176 
10177 	return 0;
10178 }
10179 
10180 /**
10181  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10182  * @vsi: the VSI being cleaned
10183  **/
10184 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10185 {
10186 	int i;
10187 
10188 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10189 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10190 			kfree_rcu(vsi->tx_rings[i], rcu);
10191 			vsi->tx_rings[i] = NULL;
10192 			vsi->rx_rings[i] = NULL;
10193 			if (vsi->xdp_rings)
10194 				vsi->xdp_rings[i] = NULL;
10195 		}
10196 	}
10197 }
10198 
10199 /**
10200  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10201  * @vsi: the VSI being configured
10202  **/
10203 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10204 {
10205 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10206 	struct i40e_pf *pf = vsi->back;
10207 	struct i40e_ring *ring;
10208 
10209 	/* Set basic values in the rings to be used later during open() */
10210 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10211 		/* allocate space for both Tx and Rx in one shot */
10212 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10213 		if (!ring)
10214 			goto err_out;
10215 
10216 		ring->queue_index = i;
10217 		ring->reg_idx = vsi->base_queue + i;
10218 		ring->ring_active = false;
10219 		ring->vsi = vsi;
10220 		ring->netdev = vsi->netdev;
10221 		ring->dev = &pf->pdev->dev;
10222 		ring->count = vsi->num_desc;
10223 		ring->size = 0;
10224 		ring->dcb_tc = 0;
10225 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10226 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10227 		ring->itr_setting = pf->tx_itr_default;
10228 		vsi->tx_rings[i] = ring++;
10229 
10230 		if (!i40e_enabled_xdp_vsi(vsi))
10231 			goto setup_rx;
10232 
10233 		ring->queue_index = vsi->alloc_queue_pairs + i;
10234 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10235 		ring->ring_active = false;
10236 		ring->vsi = vsi;
10237 		ring->netdev = NULL;
10238 		ring->dev = &pf->pdev->dev;
10239 		ring->count = vsi->num_desc;
10240 		ring->size = 0;
10241 		ring->dcb_tc = 0;
10242 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10243 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10244 		set_ring_xdp(ring);
10245 		ring->itr_setting = pf->tx_itr_default;
10246 		vsi->xdp_rings[i] = ring++;
10247 
10248 setup_rx:
10249 		ring->queue_index = i;
10250 		ring->reg_idx = vsi->base_queue + i;
10251 		ring->ring_active = false;
10252 		ring->vsi = vsi;
10253 		ring->netdev = vsi->netdev;
10254 		ring->dev = &pf->pdev->dev;
10255 		ring->count = vsi->num_desc;
10256 		ring->size = 0;
10257 		ring->dcb_tc = 0;
10258 		ring->itr_setting = pf->rx_itr_default;
10259 		vsi->rx_rings[i] = ring;
10260 	}
10261 
10262 	return 0;
10263 
10264 err_out:
10265 	i40e_vsi_clear_rings(vsi);
10266 	return -ENOMEM;
10267 }
10268 
10269 /**
10270  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10271  * @pf: board private structure
10272  * @vectors: the number of MSI-X vectors to request
10273  *
10274  * Returns the number of vectors reserved, or error
10275  **/
10276 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10277 {
10278 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10279 					I40E_MIN_MSIX, vectors);
10280 	if (vectors < 0) {
10281 		dev_info(&pf->pdev->dev,
10282 			 "MSI-X vector reservation failed: %d\n", vectors);
10283 		vectors = 0;
10284 	}
10285 
10286 	return vectors;
10287 }
10288 
10289 /**
10290  * i40e_init_msix - Setup the MSIX capability
10291  * @pf: board private structure
10292  *
10293  * Work with the OS to set up the MSIX vectors needed.
10294  *
10295  * Returns the number of vectors reserved or negative on failure
10296  **/
10297 static int i40e_init_msix(struct i40e_pf *pf)
10298 {
10299 	struct i40e_hw *hw = &pf->hw;
10300 	int cpus, extra_vectors;
10301 	int vectors_left;
10302 	int v_budget, i;
10303 	int v_actual;
10304 	int iwarp_requested = 0;
10305 
10306 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10307 		return -ENODEV;
10308 
10309 	/* The number of vectors we'll request will be comprised of:
10310 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10311 	 *   - The number of LAN queue pairs
10312 	 *	- Queues being used for RSS.
10313 	 *		We don't need as many as max_rss_size vectors.
10314 	 *		use rss_size instead in the calculation since that
10315 	 *		is governed by number of cpus in the system.
10316 	 *	- assumes symmetric Tx/Rx pairing
10317 	 *   - The number of VMDq pairs
10318 	 *   - The CPU count within the NUMA node if iWARP is enabled
10319 	 * Once we count this up, try the request.
10320 	 *
10321 	 * If we can't get what we want, we'll simplify to nearly nothing
10322 	 * and try again.  If that still fails, we punt.
10323 	 */
10324 	vectors_left = hw->func_caps.num_msix_vectors;
10325 	v_budget = 0;
10326 
10327 	/* reserve one vector for miscellaneous handler */
10328 	if (vectors_left) {
10329 		v_budget++;
10330 		vectors_left--;
10331 	}
10332 
10333 	/* reserve some vectors for the main PF traffic queues. Initially we
10334 	 * only reserve at most 50% of the available vectors, in the case that
10335 	 * the number of online CPUs is large. This ensures that we can enable
10336 	 * extra features as well. Once we've enabled the other features, we
10337 	 * will use any remaining vectors to reach as close as we can to the
10338 	 * number of online CPUs.
10339 	 */
10340 	cpus = num_online_cpus();
10341 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10342 	vectors_left -= pf->num_lan_msix;
10343 
10344 	/* reserve one vector for sideband flow director */
10345 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10346 		if (vectors_left) {
10347 			pf->num_fdsb_msix = 1;
10348 			v_budget++;
10349 			vectors_left--;
10350 		} else {
10351 			pf->num_fdsb_msix = 0;
10352 		}
10353 	}
10354 
10355 	/* can we reserve enough for iWARP? */
10356 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10357 		iwarp_requested = pf->num_iwarp_msix;
10358 
10359 		if (!vectors_left)
10360 			pf->num_iwarp_msix = 0;
10361 		else if (vectors_left < pf->num_iwarp_msix)
10362 			pf->num_iwarp_msix = 1;
10363 		v_budget += pf->num_iwarp_msix;
10364 		vectors_left -= pf->num_iwarp_msix;
10365 	}
10366 
10367 	/* any vectors left over go for VMDq support */
10368 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10369 		if (!vectors_left) {
10370 			pf->num_vmdq_msix = 0;
10371 			pf->num_vmdq_qps = 0;
10372 		} else {
10373 			int vmdq_vecs_wanted =
10374 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
10375 			int vmdq_vecs =
10376 				min_t(int, vectors_left, vmdq_vecs_wanted);
10377 
10378 			/* if we're short on vectors for what's desired, we limit
10379 			 * the queues per vmdq.  If this is still more than are
10380 			 * available, the user will need to change the number of
10381 			 * queues/vectors used by the PF later with the ethtool
10382 			 * channels command
10383 			 */
10384 			if (vectors_left < vmdq_vecs_wanted) {
10385 				pf->num_vmdq_qps = 1;
10386 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
10387 				vmdq_vecs = min_t(int,
10388 						  vectors_left,
10389 						  vmdq_vecs_wanted);
10390 			}
10391 			pf->num_vmdq_msix = pf->num_vmdq_qps;
10392 
10393 			v_budget += vmdq_vecs;
10394 			vectors_left -= vmdq_vecs;
10395 		}
10396 	}
10397 
10398 	/* On systems with a large number of SMP cores, we previously limited
10399 	 * the number of vectors for num_lan_msix to be at most 50% of the
10400 	 * available vectors, to allow for other features. Now, we add back
10401 	 * the remaining vectors. However, we ensure that the total
10402 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10403 	 * calculate the number of vectors we can add without going over the
10404 	 * cap of CPUs. For systems with a small number of CPUs this will be
10405 	 * zero.
10406 	 */
10407 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10408 	pf->num_lan_msix += extra_vectors;
10409 	vectors_left -= extra_vectors;
10410 
10411 	WARN(vectors_left < 0,
10412 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10413 
10414 	v_budget += pf->num_lan_msix;
10415 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10416 				   GFP_KERNEL);
10417 	if (!pf->msix_entries)
10418 		return -ENOMEM;
10419 
10420 	for (i = 0; i < v_budget; i++)
10421 		pf->msix_entries[i].entry = i;
10422 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10423 
10424 	if (v_actual < I40E_MIN_MSIX) {
10425 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10426 		kfree(pf->msix_entries);
10427 		pf->msix_entries = NULL;
10428 		pci_disable_msix(pf->pdev);
10429 		return -ENODEV;
10430 
10431 	} else if (v_actual == I40E_MIN_MSIX) {
10432 		/* Adjust for minimal MSIX use */
10433 		pf->num_vmdq_vsis = 0;
10434 		pf->num_vmdq_qps = 0;
10435 		pf->num_lan_qps = 1;
10436 		pf->num_lan_msix = 1;
10437 
10438 	} else if (v_actual != v_budget) {
10439 		/* If we have limited resources, we will start with no vectors
10440 		 * for the special features and then allocate vectors to some
10441 		 * of these features based on the policy and at the end disable
10442 		 * the features that did not get any vectors.
10443 		 */
10444 		int vec;
10445 
10446 		dev_info(&pf->pdev->dev,
10447 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10448 			 v_actual, v_budget);
10449 		/* reserve the misc vector */
10450 		vec = v_actual - 1;
10451 
10452 		/* Scale vector usage down */
10453 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
10454 		pf->num_vmdq_vsis = 1;
10455 		pf->num_vmdq_qps = 1;
10456 
10457 		/* partition out the remaining vectors */
10458 		switch (vec) {
10459 		case 2:
10460 			pf->num_lan_msix = 1;
10461 			break;
10462 		case 3:
10463 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10464 				pf->num_lan_msix = 1;
10465 				pf->num_iwarp_msix = 1;
10466 			} else {
10467 				pf->num_lan_msix = 2;
10468 			}
10469 			break;
10470 		default:
10471 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10472 				pf->num_iwarp_msix = min_t(int, (vec / 3),
10473 						 iwarp_requested);
10474 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
10475 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10476 			} else {
10477 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
10478 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10479 			}
10480 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10481 				pf->num_fdsb_msix = 1;
10482 				vec--;
10483 			}
10484 			pf->num_lan_msix = min_t(int,
10485 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10486 							      pf->num_lan_msix);
10487 			pf->num_lan_qps = pf->num_lan_msix;
10488 			break;
10489 		}
10490 	}
10491 
10492 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10493 	    (pf->num_fdsb_msix == 0)) {
10494 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10495 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10496 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10497 	}
10498 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10499 	    (pf->num_vmdq_msix == 0)) {
10500 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10501 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10502 	}
10503 
10504 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10505 	    (pf->num_iwarp_msix == 0)) {
10506 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10507 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10508 	}
10509 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10510 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10511 		   pf->num_lan_msix,
10512 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
10513 		   pf->num_fdsb_msix,
10514 		   pf->num_iwarp_msix);
10515 
10516 	return v_actual;
10517 }
10518 
10519 /**
10520  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10521  * @vsi: the VSI being configured
10522  * @v_idx: index of the vector in the vsi struct
10523  * @cpu: cpu to be used on affinity_mask
10524  *
10525  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
10526  **/
10527 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10528 {
10529 	struct i40e_q_vector *q_vector;
10530 
10531 	/* allocate q_vector */
10532 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10533 	if (!q_vector)
10534 		return -ENOMEM;
10535 
10536 	q_vector->vsi = vsi;
10537 	q_vector->v_idx = v_idx;
10538 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10539 
10540 	if (vsi->netdev)
10541 		netif_napi_add(vsi->netdev, &q_vector->napi,
10542 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
10543 
10544 	/* tie q_vector and vsi together */
10545 	vsi->q_vectors[v_idx] = q_vector;
10546 
10547 	return 0;
10548 }
10549 
10550 /**
10551  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10552  * @vsi: the VSI being configured
10553  *
10554  * We allocate one q_vector per queue interrupt.  If allocation fails we
10555  * return -ENOMEM.
10556  **/
10557 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10558 {
10559 	struct i40e_pf *pf = vsi->back;
10560 	int err, v_idx, num_q_vectors, current_cpu;
10561 
10562 	/* if not MSIX, give the one vector only to the LAN VSI */
10563 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10564 		num_q_vectors = vsi->num_q_vectors;
10565 	else if (vsi == pf->vsi[pf->lan_vsi])
10566 		num_q_vectors = 1;
10567 	else
10568 		return -EINVAL;
10569 
10570 	current_cpu = cpumask_first(cpu_online_mask);
10571 
10572 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10573 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10574 		if (err)
10575 			goto err_out;
10576 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10577 		if (unlikely(current_cpu >= nr_cpu_ids))
10578 			current_cpu = cpumask_first(cpu_online_mask);
10579 	}
10580 
10581 	return 0;
10582 
10583 err_out:
10584 	while (v_idx--)
10585 		i40e_free_q_vector(vsi, v_idx);
10586 
10587 	return err;
10588 }
10589 
10590 /**
10591  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10592  * @pf: board private structure to initialize
10593  **/
10594 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10595 {
10596 	int vectors = 0;
10597 	ssize_t size;
10598 
10599 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10600 		vectors = i40e_init_msix(pf);
10601 		if (vectors < 0) {
10602 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
10603 				       I40E_FLAG_IWARP_ENABLED	|
10604 				       I40E_FLAG_RSS_ENABLED	|
10605 				       I40E_FLAG_DCB_CAPABLE	|
10606 				       I40E_FLAG_DCB_ENABLED	|
10607 				       I40E_FLAG_SRIOV_ENABLED	|
10608 				       I40E_FLAG_FD_SB_ENABLED	|
10609 				       I40E_FLAG_FD_ATR_ENABLED	|
10610 				       I40E_FLAG_VMDQ_ENABLED);
10611 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10612 
10613 			/* rework the queue expectations without MSIX */
10614 			i40e_determine_queue_usage(pf);
10615 		}
10616 	}
10617 
10618 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10619 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10620 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10621 		vectors = pci_enable_msi(pf->pdev);
10622 		if (vectors < 0) {
10623 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10624 				 vectors);
10625 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10626 		}
10627 		vectors = 1;  /* one MSI or Legacy vector */
10628 	}
10629 
10630 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10631 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10632 
10633 	/* set up vector assignment tracking */
10634 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10635 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
10636 	if (!pf->irq_pile)
10637 		return -ENOMEM;
10638 
10639 	pf->irq_pile->num_entries = vectors;
10640 	pf->irq_pile->search_hint = 0;
10641 
10642 	/* track first vector for misc interrupts, ignore return */
10643 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10644 
10645 	return 0;
10646 }
10647 
10648 /**
10649  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10650  * @pf: private board data structure
10651  *
10652  * Restore the interrupt scheme that was cleared when we suspended the
10653  * device. This should be called during resume to re-allocate the q_vectors
10654  * and reacquire IRQs.
10655  */
10656 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10657 {
10658 	int err, i;
10659 
10660 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
10661 	 * scheme. We need to re-enabled them here in order to attempt to
10662 	 * re-acquire the MSI or MSI-X vectors
10663 	 */
10664 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10665 
10666 	err = i40e_init_interrupt_scheme(pf);
10667 	if (err)
10668 		return err;
10669 
10670 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
10671 	 * rings together again.
10672 	 */
10673 	for (i = 0; i < pf->num_alloc_vsi; i++) {
10674 		if (pf->vsi[i]) {
10675 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10676 			if (err)
10677 				goto err_unwind;
10678 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10679 		}
10680 	}
10681 
10682 	err = i40e_setup_misc_vector(pf);
10683 	if (err)
10684 		goto err_unwind;
10685 
10686 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
10687 		i40e_client_update_msix_info(pf);
10688 
10689 	return 0;
10690 
10691 err_unwind:
10692 	while (i--) {
10693 		if (pf->vsi[i])
10694 			i40e_vsi_free_q_vectors(pf->vsi[i]);
10695 	}
10696 
10697 	return err;
10698 }
10699 
10700 /**
10701  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10702  * @pf: board private structure
10703  *
10704  * This sets up the handler for MSIX 0, which is used to manage the
10705  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
10706  * when in MSI or Legacy interrupt mode.
10707  **/
10708 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10709 {
10710 	struct i40e_hw *hw = &pf->hw;
10711 	int err = 0;
10712 
10713 	/* Only request the IRQ once, the first time through. */
10714 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10715 		err = request_irq(pf->msix_entries[0].vector,
10716 				  i40e_intr, 0, pf->int_name, pf);
10717 		if (err) {
10718 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10719 			dev_info(&pf->pdev->dev,
10720 				 "request_irq for %s failed: %d\n",
10721 				 pf->int_name, err);
10722 			return -EFAULT;
10723 		}
10724 	}
10725 
10726 	i40e_enable_misc_int_causes(pf);
10727 
10728 	/* associate no queues to the misc vector */
10729 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10730 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10731 
10732 	i40e_flush(hw);
10733 
10734 	i40e_irq_dynamic_enable_icr0(pf);
10735 
10736 	return err;
10737 }
10738 
10739 /**
10740  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10741  * @vsi: Pointer to vsi structure
10742  * @seed: Buffter to store the hash keys
10743  * @lut: Buffer to store the lookup table entries
10744  * @lut_size: Size of buffer to store the lookup table entries
10745  *
10746  * Return 0 on success, negative on failure
10747  */
10748 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10749 			   u8 *lut, u16 lut_size)
10750 {
10751 	struct i40e_pf *pf = vsi->back;
10752 	struct i40e_hw *hw = &pf->hw;
10753 	int ret = 0;
10754 
10755 	if (seed) {
10756 		ret = i40e_aq_get_rss_key(hw, vsi->id,
10757 			(struct i40e_aqc_get_set_rss_key_data *)seed);
10758 		if (ret) {
10759 			dev_info(&pf->pdev->dev,
10760 				 "Cannot get RSS key, err %s aq_err %s\n",
10761 				 i40e_stat_str(&pf->hw, ret),
10762 				 i40e_aq_str(&pf->hw,
10763 					     pf->hw.aq.asq_last_status));
10764 			return ret;
10765 		}
10766 	}
10767 
10768 	if (lut) {
10769 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10770 
10771 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10772 		if (ret) {
10773 			dev_info(&pf->pdev->dev,
10774 				 "Cannot get RSS lut, err %s aq_err %s\n",
10775 				 i40e_stat_str(&pf->hw, ret),
10776 				 i40e_aq_str(&pf->hw,
10777 					     pf->hw.aq.asq_last_status));
10778 			return ret;
10779 		}
10780 	}
10781 
10782 	return ret;
10783 }
10784 
10785 /**
10786  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10787  * @vsi: Pointer to vsi structure
10788  * @seed: RSS hash seed
10789  * @lut: Lookup table
10790  * @lut_size: Lookup table size
10791  *
10792  * Returns 0 on success, negative on failure
10793  **/
10794 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10795 			       const u8 *lut, u16 lut_size)
10796 {
10797 	struct i40e_pf *pf = vsi->back;
10798 	struct i40e_hw *hw = &pf->hw;
10799 	u16 vf_id = vsi->vf_id;
10800 	u8 i;
10801 
10802 	/* Fill out hash function seed */
10803 	if (seed) {
10804 		u32 *seed_dw = (u32 *)seed;
10805 
10806 		if (vsi->type == I40E_VSI_MAIN) {
10807 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10808 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10809 		} else if (vsi->type == I40E_VSI_SRIOV) {
10810 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10811 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10812 		} else {
10813 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10814 		}
10815 	}
10816 
10817 	if (lut) {
10818 		u32 *lut_dw = (u32 *)lut;
10819 
10820 		if (vsi->type == I40E_VSI_MAIN) {
10821 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
10822 				return -EINVAL;
10823 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10824 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10825 		} else if (vsi->type == I40E_VSI_SRIOV) {
10826 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10827 				return -EINVAL;
10828 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10829 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10830 		} else {
10831 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10832 		}
10833 	}
10834 	i40e_flush(hw);
10835 
10836 	return 0;
10837 }
10838 
10839 /**
10840  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10841  * @vsi: Pointer to VSI structure
10842  * @seed: Buffer to store the keys
10843  * @lut: Buffer to store the lookup table entries
10844  * @lut_size: Size of buffer to store the lookup table entries
10845  *
10846  * Returns 0 on success, negative on failure
10847  */
10848 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10849 			    u8 *lut, u16 lut_size)
10850 {
10851 	struct i40e_pf *pf = vsi->back;
10852 	struct i40e_hw *hw = &pf->hw;
10853 	u16 i;
10854 
10855 	if (seed) {
10856 		u32 *seed_dw = (u32 *)seed;
10857 
10858 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10859 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
10860 	}
10861 	if (lut) {
10862 		u32 *lut_dw = (u32 *)lut;
10863 
10864 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
10865 			return -EINVAL;
10866 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10867 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10868 	}
10869 
10870 	return 0;
10871 }
10872 
10873 /**
10874  * i40e_config_rss - Configure RSS keys and lut
10875  * @vsi: Pointer to VSI structure
10876  * @seed: RSS hash seed
10877  * @lut: Lookup table
10878  * @lut_size: Lookup table size
10879  *
10880  * Returns 0 on success, negative on failure
10881  */
10882 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10883 {
10884 	struct i40e_pf *pf = vsi->back;
10885 
10886 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10887 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10888 	else
10889 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10890 }
10891 
10892 /**
10893  * i40e_get_rss - Get RSS keys and lut
10894  * @vsi: Pointer to VSI structure
10895  * @seed: Buffer to store the keys
10896  * @lut: Buffer to store the lookup table entries
10897  * @lut_size: Size of buffer to store the lookup table entries
10898  *
10899  * Returns 0 on success, negative on failure
10900  */
10901 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10902 {
10903 	struct i40e_pf *pf = vsi->back;
10904 
10905 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10906 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10907 	else
10908 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
10909 }
10910 
10911 /**
10912  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10913  * @pf: Pointer to board private structure
10914  * @lut: Lookup table
10915  * @rss_table_size: Lookup table size
10916  * @rss_size: Range of queue number for hashing
10917  */
10918 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10919 		       u16 rss_table_size, u16 rss_size)
10920 {
10921 	u16 i;
10922 
10923 	for (i = 0; i < rss_table_size; i++)
10924 		lut[i] = i % rss_size;
10925 }
10926 
10927 /**
10928  * i40e_pf_config_rss - Prepare for RSS if used
10929  * @pf: board private structure
10930  **/
10931 static int i40e_pf_config_rss(struct i40e_pf *pf)
10932 {
10933 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10934 	u8 seed[I40E_HKEY_ARRAY_SIZE];
10935 	u8 *lut;
10936 	struct i40e_hw *hw = &pf->hw;
10937 	u32 reg_val;
10938 	u64 hena;
10939 	int ret;
10940 
10941 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
10942 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10943 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
10944 	hena |= i40e_pf_get_default_rss_hena(pf);
10945 
10946 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10947 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
10948 
10949 	/* Determine the RSS table size based on the hardware capabilities */
10950 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
10951 	reg_val = (pf->rss_table_size == 512) ?
10952 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10953 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
10954 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
10955 
10956 	/* Determine the RSS size of the VSI */
10957 	if (!vsi->rss_size) {
10958 		u16 qcount;
10959 		/* If the firmware does something weird during VSI init, we
10960 		 * could end up with zero TCs. Check for that to avoid
10961 		 * divide-by-zero. It probably won't pass traffic, but it also
10962 		 * won't panic.
10963 		 */
10964 		qcount = vsi->num_queue_pairs /
10965 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
10966 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10967 	}
10968 	if (!vsi->rss_size)
10969 		return -EINVAL;
10970 
10971 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10972 	if (!lut)
10973 		return -ENOMEM;
10974 
10975 	/* Use user configured lut if there is one, otherwise use default */
10976 	if (vsi->rss_lut_user)
10977 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10978 	else
10979 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
10980 
10981 	/* Use user configured hash key if there is one, otherwise
10982 	 * use default.
10983 	 */
10984 	if (vsi->rss_hkey_user)
10985 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10986 	else
10987 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
10988 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
10989 	kfree(lut);
10990 
10991 	return ret;
10992 }
10993 
10994 /**
10995  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10996  * @pf: board private structure
10997  * @queue_count: the requested queue count for rss.
10998  *
10999  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11000  * count which may be different from the requested queue count.
11001  * Note: expects to be called while under rtnl_lock()
11002  **/
11003 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11004 {
11005 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11006 	int new_rss_size;
11007 
11008 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11009 		return 0;
11010 
11011 	queue_count = min_t(int, queue_count, num_online_cpus());
11012 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11013 
11014 	if (queue_count != vsi->num_queue_pairs) {
11015 		u16 qcount;
11016 
11017 		vsi->req_queue_pairs = queue_count;
11018 		i40e_prep_for_reset(pf, true);
11019 
11020 		pf->alloc_rss_size = new_rss_size;
11021 
11022 		i40e_reset_and_rebuild(pf, true, true);
11023 
11024 		/* Discard the user configured hash keys and lut, if less
11025 		 * queues are enabled.
11026 		 */
11027 		if (queue_count < vsi->rss_size) {
11028 			i40e_clear_rss_config_user(vsi);
11029 			dev_dbg(&pf->pdev->dev,
11030 				"discard user configured hash keys and lut\n");
11031 		}
11032 
11033 		/* Reset vsi->rss_size, as number of enabled queues changed */
11034 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11035 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11036 
11037 		i40e_pf_config_rss(pf);
11038 	}
11039 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11040 		 vsi->req_queue_pairs, pf->rss_size_max);
11041 	return pf->alloc_rss_size;
11042 }
11043 
11044 /**
11045  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11046  * @pf: board private structure
11047  **/
11048 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11049 {
11050 	i40e_status status;
11051 	bool min_valid, max_valid;
11052 	u32 max_bw, min_bw;
11053 
11054 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11055 					   &min_valid, &max_valid);
11056 
11057 	if (!status) {
11058 		if (min_valid)
11059 			pf->min_bw = min_bw;
11060 		if (max_valid)
11061 			pf->max_bw = max_bw;
11062 	}
11063 
11064 	return status;
11065 }
11066 
11067 /**
11068  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11069  * @pf: board private structure
11070  **/
11071 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11072 {
11073 	struct i40e_aqc_configure_partition_bw_data bw_data;
11074 	i40e_status status;
11075 
11076 	/* Set the valid bit for this PF */
11077 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11078 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11079 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11080 
11081 	/* Set the new bandwidths */
11082 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11083 
11084 	return status;
11085 }
11086 
11087 /**
11088  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11089  * @pf: board private structure
11090  **/
11091 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11092 {
11093 	/* Commit temporary BW setting to permanent NVM image */
11094 	enum i40e_admin_queue_err last_aq_status;
11095 	i40e_status ret;
11096 	u16 nvm_word;
11097 
11098 	if (pf->hw.partition_id != 1) {
11099 		dev_info(&pf->pdev->dev,
11100 			 "Commit BW only works on partition 1! This is partition %d",
11101 			 pf->hw.partition_id);
11102 		ret = I40E_NOT_SUPPORTED;
11103 		goto bw_commit_out;
11104 	}
11105 
11106 	/* Acquire NVM for read access */
11107 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11108 	last_aq_status = pf->hw.aq.asq_last_status;
11109 	if (ret) {
11110 		dev_info(&pf->pdev->dev,
11111 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11112 			 i40e_stat_str(&pf->hw, ret),
11113 			 i40e_aq_str(&pf->hw, last_aq_status));
11114 		goto bw_commit_out;
11115 	}
11116 
11117 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11118 	ret = i40e_aq_read_nvm(&pf->hw,
11119 			       I40E_SR_NVM_CONTROL_WORD,
11120 			       0x10, sizeof(nvm_word), &nvm_word,
11121 			       false, NULL);
11122 	/* Save off last admin queue command status before releasing
11123 	 * the NVM
11124 	 */
11125 	last_aq_status = pf->hw.aq.asq_last_status;
11126 	i40e_release_nvm(&pf->hw);
11127 	if (ret) {
11128 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11129 			 i40e_stat_str(&pf->hw, ret),
11130 			 i40e_aq_str(&pf->hw, last_aq_status));
11131 		goto bw_commit_out;
11132 	}
11133 
11134 	/* Wait a bit for NVM release to complete */
11135 	msleep(50);
11136 
11137 	/* Acquire NVM for write access */
11138 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11139 	last_aq_status = pf->hw.aq.asq_last_status;
11140 	if (ret) {
11141 		dev_info(&pf->pdev->dev,
11142 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11143 			 i40e_stat_str(&pf->hw, ret),
11144 			 i40e_aq_str(&pf->hw, last_aq_status));
11145 		goto bw_commit_out;
11146 	}
11147 	/* Write it back out unchanged to initiate update NVM,
11148 	 * which will force a write of the shadow (alt) RAM to
11149 	 * the NVM - thus storing the bandwidth values permanently.
11150 	 */
11151 	ret = i40e_aq_update_nvm(&pf->hw,
11152 				 I40E_SR_NVM_CONTROL_WORD,
11153 				 0x10, sizeof(nvm_word),
11154 				 &nvm_word, true, 0, NULL);
11155 	/* Save off last admin queue command status before releasing
11156 	 * the NVM
11157 	 */
11158 	last_aq_status = pf->hw.aq.asq_last_status;
11159 	i40e_release_nvm(&pf->hw);
11160 	if (ret)
11161 		dev_info(&pf->pdev->dev,
11162 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11163 			 i40e_stat_str(&pf->hw, ret),
11164 			 i40e_aq_str(&pf->hw, last_aq_status));
11165 bw_commit_out:
11166 
11167 	return ret;
11168 }
11169 
11170 /**
11171  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11172  * @pf: board private structure to initialize
11173  *
11174  * i40e_sw_init initializes the Adapter private data structure.
11175  * Fields are initialized based on PCI device information and
11176  * OS network device settings (MTU size).
11177  **/
11178 static int i40e_sw_init(struct i40e_pf *pf)
11179 {
11180 	int err = 0;
11181 	int size;
11182 
11183 	/* Set default capability flags */
11184 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11185 		    I40E_FLAG_MSI_ENABLED     |
11186 		    I40E_FLAG_MSIX_ENABLED;
11187 
11188 	/* Set default ITR */
11189 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11190 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11191 
11192 	/* Depending on PF configurations, it is possible that the RSS
11193 	 * maximum might end up larger than the available queues
11194 	 */
11195 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11196 	pf->alloc_rss_size = 1;
11197 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11198 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11199 				 pf->hw.func_caps.num_tx_qp);
11200 	if (pf->hw.func_caps.rss) {
11201 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11202 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11203 					   num_online_cpus());
11204 	}
11205 
11206 	/* MFP mode enabled */
11207 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11208 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11209 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11210 		if (i40e_get_partition_bw_setting(pf)) {
11211 			dev_warn(&pf->pdev->dev,
11212 				 "Could not get partition bw settings\n");
11213 		} else {
11214 			dev_info(&pf->pdev->dev,
11215 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11216 				 pf->min_bw, pf->max_bw);
11217 
11218 			/* nudge the Tx scheduler */
11219 			i40e_set_partition_bw_setting(pf);
11220 		}
11221 	}
11222 
11223 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11224 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11225 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11226 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11227 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11228 		    pf->hw.num_partitions > 1)
11229 			dev_info(&pf->pdev->dev,
11230 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11231 		else
11232 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11233 		pf->fdir_pf_filter_count =
11234 				 pf->hw.func_caps.fd_filters_guaranteed;
11235 		pf->hw.fdir_shared_filter_count =
11236 				 pf->hw.func_caps.fd_filters_best_effort;
11237 	}
11238 
11239 	if (pf->hw.mac.type == I40E_MAC_X722) {
11240 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11241 				    I40E_HW_128_QP_RSS_CAPABLE |
11242 				    I40E_HW_ATR_EVICT_CAPABLE |
11243 				    I40E_HW_WB_ON_ITR_CAPABLE |
11244 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11245 				    I40E_HW_NO_PCI_LINK_CHECK |
11246 				    I40E_HW_USE_SET_LLDP_MIB |
11247 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11248 				    I40E_HW_PTP_L4_CAPABLE |
11249 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11250 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11251 
11252 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11253 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11254 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11255 			dev_warn(&pf->pdev->dev,
11256 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11257 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11258 		}
11259 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11260 		   ((pf->hw.aq.api_maj_ver == 1) &&
11261 		    (pf->hw.aq.api_min_ver > 4))) {
11262 		/* Supported in FW API version higher than 1.4 */
11263 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11264 	}
11265 
11266 	/* Enable HW ATR eviction if possible */
11267 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11268 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11269 
11270 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11271 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11272 	    (pf->hw.aq.fw_maj_ver < 4))) {
11273 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11274 		/* No DCB support  for FW < v4.33 */
11275 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11276 	}
11277 
11278 	/* Disable FW LLDP if FW < v4.3 */
11279 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11280 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11281 	    (pf->hw.aq.fw_maj_ver < 4)))
11282 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11283 
11284 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11285 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11286 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11287 	    (pf->hw.aq.fw_maj_ver >= 5)))
11288 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11289 
11290 	/* Enable PTP L4 if FW > v6.0 */
11291 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11292 	    pf->hw.aq.fw_maj_ver >= 6)
11293 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11294 
11295 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11296 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11297 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11298 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11299 	}
11300 
11301 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11302 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11303 		/* IWARP needs one extra vector for CQP just like MISC.*/
11304 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11305 	}
11306 	/* Stopping FW LLDP engine is supported on XL710 and X722
11307 	 * starting from FW versions determined in i40e_init_adminq.
11308 	 * Stopping the FW LLDP engine is not supported on XL710
11309 	 * if NPAR is functioning so unset this hw flag in this case.
11310 	 */
11311 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11312 	    pf->hw.func_caps.npar_enable &&
11313 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
11314 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
11315 
11316 #ifdef CONFIG_PCI_IOV
11317 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11318 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11319 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11320 		pf->num_req_vfs = min_t(int,
11321 					pf->hw.func_caps.num_vfs,
11322 					I40E_MAX_VF_COUNT);
11323 	}
11324 #endif /* CONFIG_PCI_IOV */
11325 	pf->eeprom_version = 0xDEAD;
11326 	pf->lan_veb = I40E_NO_VEB;
11327 	pf->lan_vsi = I40E_NO_VSI;
11328 
11329 	/* By default FW has this off for performance reasons */
11330 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11331 
11332 	/* set up queue assignment tracking */
11333 	size = sizeof(struct i40e_lump_tracking)
11334 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11335 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
11336 	if (!pf->qp_pile) {
11337 		err = -ENOMEM;
11338 		goto sw_init_done;
11339 	}
11340 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11341 	pf->qp_pile->search_hint = 0;
11342 
11343 	pf->tx_timeout_recovery_level = 1;
11344 
11345 	mutex_init(&pf->switch_mutex);
11346 
11347 sw_init_done:
11348 	return err;
11349 }
11350 
11351 /**
11352  * i40e_set_ntuple - set the ntuple feature flag and take action
11353  * @pf: board private structure to initialize
11354  * @features: the feature set that the stack is suggesting
11355  *
11356  * returns a bool to indicate if reset needs to happen
11357  **/
11358 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11359 {
11360 	bool need_reset = false;
11361 
11362 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
11363 	 * the state changed, we need to reset.
11364 	 */
11365 	if (features & NETIF_F_NTUPLE) {
11366 		/* Enable filters and mark for reset */
11367 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11368 			need_reset = true;
11369 		/* enable FD_SB only if there is MSI-X vector and no cloud
11370 		 * filters exist
11371 		 */
11372 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11373 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11374 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11375 		}
11376 	} else {
11377 		/* turn off filters, mark for reset and clear SW filter list */
11378 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11379 			need_reset = true;
11380 			i40e_fdir_filter_exit(pf);
11381 		}
11382 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11383 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
11384 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11385 
11386 		/* reset fd counters */
11387 		pf->fd_add_err = 0;
11388 		pf->fd_atr_cnt = 0;
11389 		/* if ATR was auto disabled it can be re-enabled. */
11390 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
11391 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11392 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
11393 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11394 	}
11395 	return need_reset;
11396 }
11397 
11398 /**
11399  * i40e_clear_rss_lut - clear the rx hash lookup table
11400  * @vsi: the VSI being configured
11401  **/
11402 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11403 {
11404 	struct i40e_pf *pf = vsi->back;
11405 	struct i40e_hw *hw = &pf->hw;
11406 	u16 vf_id = vsi->vf_id;
11407 	u8 i;
11408 
11409 	if (vsi->type == I40E_VSI_MAIN) {
11410 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11411 			wr32(hw, I40E_PFQF_HLUT(i), 0);
11412 	} else if (vsi->type == I40E_VSI_SRIOV) {
11413 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11414 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11415 	} else {
11416 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11417 	}
11418 }
11419 
11420 /**
11421  * i40e_set_features - set the netdev feature flags
11422  * @netdev: ptr to the netdev being adjusted
11423  * @features: the feature set that the stack is suggesting
11424  * Note: expects to be called while under rtnl_lock()
11425  **/
11426 static int i40e_set_features(struct net_device *netdev,
11427 			     netdev_features_t features)
11428 {
11429 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11430 	struct i40e_vsi *vsi = np->vsi;
11431 	struct i40e_pf *pf = vsi->back;
11432 	bool need_reset;
11433 
11434 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11435 		i40e_pf_config_rss(pf);
11436 	else if (!(features & NETIF_F_RXHASH) &&
11437 		 netdev->features & NETIF_F_RXHASH)
11438 		i40e_clear_rss_lut(vsi);
11439 
11440 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
11441 		i40e_vlan_stripping_enable(vsi);
11442 	else
11443 		i40e_vlan_stripping_disable(vsi);
11444 
11445 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11446 		dev_err(&pf->pdev->dev,
11447 			"Offloaded tc filters active, can't turn hw_tc_offload off");
11448 		return -EINVAL;
11449 	}
11450 
11451 	need_reset = i40e_set_ntuple(pf, features);
11452 
11453 	if (need_reset)
11454 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11455 
11456 	return 0;
11457 }
11458 
11459 /**
11460  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11461  * @pf: board private structure
11462  * @port: The UDP port to look up
11463  *
11464  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11465  **/
11466 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11467 {
11468 	u8 i;
11469 
11470 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11471 		/* Do not report ports with pending deletions as
11472 		 * being available.
11473 		 */
11474 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
11475 			continue;
11476 		if (pf->udp_ports[i].port == port)
11477 			return i;
11478 	}
11479 
11480 	return i;
11481 }
11482 
11483 /**
11484  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11485  * @netdev: This physical port's netdev
11486  * @ti: Tunnel endpoint information
11487  **/
11488 static void i40e_udp_tunnel_add(struct net_device *netdev,
11489 				struct udp_tunnel_info *ti)
11490 {
11491 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11492 	struct i40e_vsi *vsi = np->vsi;
11493 	struct i40e_pf *pf = vsi->back;
11494 	u16 port = ntohs(ti->port);
11495 	u8 next_idx;
11496 	u8 idx;
11497 
11498 	idx = i40e_get_udp_port_idx(pf, port);
11499 
11500 	/* Check if port already exists */
11501 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11502 		netdev_info(netdev, "port %d already offloaded\n", port);
11503 		return;
11504 	}
11505 
11506 	/* Now check if there is space to add the new port */
11507 	next_idx = i40e_get_udp_port_idx(pf, 0);
11508 
11509 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11510 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11511 			    port);
11512 		return;
11513 	}
11514 
11515 	switch (ti->type) {
11516 	case UDP_TUNNEL_TYPE_VXLAN:
11517 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11518 		break;
11519 	case UDP_TUNNEL_TYPE_GENEVE:
11520 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11521 			return;
11522 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11523 		break;
11524 	default:
11525 		return;
11526 	}
11527 
11528 	/* New port: add it and mark its index in the bitmap */
11529 	pf->udp_ports[next_idx].port = port;
11530 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
11531 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11532 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11533 }
11534 
11535 /**
11536  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11537  * @netdev: This physical port's netdev
11538  * @ti: Tunnel endpoint information
11539  **/
11540 static void i40e_udp_tunnel_del(struct net_device *netdev,
11541 				struct udp_tunnel_info *ti)
11542 {
11543 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11544 	struct i40e_vsi *vsi = np->vsi;
11545 	struct i40e_pf *pf = vsi->back;
11546 	u16 port = ntohs(ti->port);
11547 	u8 idx;
11548 
11549 	idx = i40e_get_udp_port_idx(pf, port);
11550 
11551 	/* Check if port already exists */
11552 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11553 		goto not_found;
11554 
11555 	switch (ti->type) {
11556 	case UDP_TUNNEL_TYPE_VXLAN:
11557 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11558 			goto not_found;
11559 		break;
11560 	case UDP_TUNNEL_TYPE_GENEVE:
11561 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11562 			goto not_found;
11563 		break;
11564 	default:
11565 		goto not_found;
11566 	}
11567 
11568 	/* if port exists, set it to 0 (mark for deletion)
11569 	 * and make it pending
11570 	 */
11571 	pf->udp_ports[idx].port = 0;
11572 
11573 	/* Toggle pending bit instead of setting it. This way if we are
11574 	 * deleting a port that has yet to be added we just clear the pending
11575 	 * bit and don't have to worry about it.
11576 	 */
11577 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
11578 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11579 
11580 	return;
11581 not_found:
11582 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11583 		    port);
11584 }
11585 
11586 static int i40e_get_phys_port_id(struct net_device *netdev,
11587 				 struct netdev_phys_item_id *ppid)
11588 {
11589 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11590 	struct i40e_pf *pf = np->vsi->back;
11591 	struct i40e_hw *hw = &pf->hw;
11592 
11593 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11594 		return -EOPNOTSUPP;
11595 
11596 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11597 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11598 
11599 	return 0;
11600 }
11601 
11602 /**
11603  * i40e_ndo_fdb_add - add an entry to the hardware database
11604  * @ndm: the input from the stack
11605  * @tb: pointer to array of nladdr (unused)
11606  * @dev: the net device pointer
11607  * @addr: the MAC address entry being added
11608  * @vid: VLAN ID
11609  * @flags: instructions from stack about fdb operation
11610  */
11611 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11612 			    struct net_device *dev,
11613 			    const unsigned char *addr, u16 vid,
11614 			    u16 flags,
11615 			    struct netlink_ext_ack *extack)
11616 {
11617 	struct i40e_netdev_priv *np = netdev_priv(dev);
11618 	struct i40e_pf *pf = np->vsi->back;
11619 	int err = 0;
11620 
11621 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11622 		return -EOPNOTSUPP;
11623 
11624 	if (vid) {
11625 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11626 		return -EINVAL;
11627 	}
11628 
11629 	/* Hardware does not support aging addresses so if a
11630 	 * ndm_state is given only allow permanent addresses
11631 	 */
11632 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11633 		netdev_info(dev, "FDB only supports static addresses\n");
11634 		return -EINVAL;
11635 	}
11636 
11637 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11638 		err = dev_uc_add_excl(dev, addr);
11639 	else if (is_multicast_ether_addr(addr))
11640 		err = dev_mc_add_excl(dev, addr);
11641 	else
11642 		err = -EINVAL;
11643 
11644 	/* Only return duplicate errors if NLM_F_EXCL is set */
11645 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
11646 		err = 0;
11647 
11648 	return err;
11649 }
11650 
11651 /**
11652  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11653  * @dev: the netdev being configured
11654  * @nlh: RTNL message
11655  * @flags: bridge flags
11656  * @extack: netlink extended ack
11657  *
11658  * Inserts a new hardware bridge if not already created and
11659  * enables the bridging mode requested (VEB or VEPA). If the
11660  * hardware bridge has already been inserted and the request
11661  * is to change the mode then that requires a PF reset to
11662  * allow rebuild of the components with required hardware
11663  * bridge mode enabled.
11664  *
11665  * Note: expects to be called while under rtnl_lock()
11666  **/
11667 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11668 				   struct nlmsghdr *nlh,
11669 				   u16 flags,
11670 				   struct netlink_ext_ack *extack)
11671 {
11672 	struct i40e_netdev_priv *np = netdev_priv(dev);
11673 	struct i40e_vsi *vsi = np->vsi;
11674 	struct i40e_pf *pf = vsi->back;
11675 	struct i40e_veb *veb = NULL;
11676 	struct nlattr *attr, *br_spec;
11677 	int i, rem;
11678 
11679 	/* Only for PF VSI for now */
11680 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11681 		return -EOPNOTSUPP;
11682 
11683 	/* Find the HW bridge for PF VSI */
11684 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11685 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11686 			veb = pf->veb[i];
11687 	}
11688 
11689 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11690 
11691 	nla_for_each_nested(attr, br_spec, rem) {
11692 		__u16 mode;
11693 
11694 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
11695 			continue;
11696 
11697 		mode = nla_get_u16(attr);
11698 		if ((mode != BRIDGE_MODE_VEPA) &&
11699 		    (mode != BRIDGE_MODE_VEB))
11700 			return -EINVAL;
11701 
11702 		/* Insert a new HW bridge */
11703 		if (!veb) {
11704 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11705 					     vsi->tc_config.enabled_tc);
11706 			if (veb) {
11707 				veb->bridge_mode = mode;
11708 				i40e_config_bridge_mode(veb);
11709 			} else {
11710 				/* No Bridge HW offload available */
11711 				return -ENOENT;
11712 			}
11713 			break;
11714 		} else if (mode != veb->bridge_mode) {
11715 			/* Existing HW bridge but different mode needs reset */
11716 			veb->bridge_mode = mode;
11717 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11718 			if (mode == BRIDGE_MODE_VEB)
11719 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11720 			else
11721 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11722 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11723 			break;
11724 		}
11725 	}
11726 
11727 	return 0;
11728 }
11729 
11730 /**
11731  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11732  * @skb: skb buff
11733  * @pid: process id
11734  * @seq: RTNL message seq #
11735  * @dev: the netdev being configured
11736  * @filter_mask: unused
11737  * @nlflags: netlink flags passed in
11738  *
11739  * Return the mode in which the hardware bridge is operating in
11740  * i.e VEB or VEPA.
11741  **/
11742 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11743 				   struct net_device *dev,
11744 				   u32 __always_unused filter_mask,
11745 				   int nlflags)
11746 {
11747 	struct i40e_netdev_priv *np = netdev_priv(dev);
11748 	struct i40e_vsi *vsi = np->vsi;
11749 	struct i40e_pf *pf = vsi->back;
11750 	struct i40e_veb *veb = NULL;
11751 	int i;
11752 
11753 	/* Only for PF VSI for now */
11754 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11755 		return -EOPNOTSUPP;
11756 
11757 	/* Find the HW bridge for the PF VSI */
11758 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11759 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11760 			veb = pf->veb[i];
11761 	}
11762 
11763 	if (!veb)
11764 		return 0;
11765 
11766 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11767 				       0, 0, nlflags, filter_mask, NULL);
11768 }
11769 
11770 /**
11771  * i40e_features_check - Validate encapsulated packet conforms to limits
11772  * @skb: skb buff
11773  * @dev: This physical port's netdev
11774  * @features: Offload features that the stack believes apply
11775  **/
11776 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11777 					     struct net_device *dev,
11778 					     netdev_features_t features)
11779 {
11780 	size_t len;
11781 
11782 	/* No point in doing any of this if neither checksum nor GSO are
11783 	 * being requested for this frame.  We can rule out both by just
11784 	 * checking for CHECKSUM_PARTIAL
11785 	 */
11786 	if (skb->ip_summed != CHECKSUM_PARTIAL)
11787 		return features;
11788 
11789 	/* We cannot support GSO if the MSS is going to be less than
11790 	 * 64 bytes.  If it is then we need to drop support for GSO.
11791 	 */
11792 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11793 		features &= ~NETIF_F_GSO_MASK;
11794 
11795 	/* MACLEN can support at most 63 words */
11796 	len = skb_network_header(skb) - skb->data;
11797 	if (len & ~(63 * 2))
11798 		goto out_err;
11799 
11800 	/* IPLEN and EIPLEN can support at most 127 dwords */
11801 	len = skb_transport_header(skb) - skb_network_header(skb);
11802 	if (len & ~(127 * 4))
11803 		goto out_err;
11804 
11805 	if (skb->encapsulation) {
11806 		/* L4TUNLEN can support 127 words */
11807 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
11808 		if (len & ~(127 * 2))
11809 			goto out_err;
11810 
11811 		/* IPLEN can support at most 127 dwords */
11812 		len = skb_inner_transport_header(skb) -
11813 		      skb_inner_network_header(skb);
11814 		if (len & ~(127 * 4))
11815 			goto out_err;
11816 	}
11817 
11818 	/* No need to validate L4LEN as TCP is the only protocol with a
11819 	 * a flexible value and we support all possible values supported
11820 	 * by TCP, which is at most 15 dwords
11821 	 */
11822 
11823 	return features;
11824 out_err:
11825 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11826 }
11827 
11828 /**
11829  * i40e_xdp_setup - add/remove an XDP program
11830  * @vsi: VSI to changed
11831  * @prog: XDP program
11832  **/
11833 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11834 			  struct bpf_prog *prog)
11835 {
11836 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11837 	struct i40e_pf *pf = vsi->back;
11838 	struct bpf_prog *old_prog;
11839 	bool need_reset;
11840 	int i;
11841 
11842 	/* Don't allow frames that span over multiple buffers */
11843 	if (frame_size > vsi->rx_buf_len)
11844 		return -EINVAL;
11845 
11846 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11847 		return 0;
11848 
11849 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
11850 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11851 
11852 	if (need_reset)
11853 		i40e_prep_for_reset(pf, true);
11854 
11855 	old_prog = xchg(&vsi->xdp_prog, prog);
11856 
11857 	if (need_reset)
11858 		i40e_reset_and_rebuild(pf, true, true);
11859 
11860 	for (i = 0; i < vsi->num_queue_pairs; i++)
11861 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11862 
11863 	if (old_prog)
11864 		bpf_prog_put(old_prog);
11865 
11866 	/* Kick start the NAPI context if there is an AF_XDP socket open
11867 	 * on that queue id. This so that receiving will start.
11868 	 */
11869 	if (need_reset && prog)
11870 		for (i = 0; i < vsi->num_queue_pairs; i++)
11871 			if (vsi->xdp_rings[i]->xsk_umem)
11872 				(void)i40e_xsk_async_xmit(vsi->netdev, i);
11873 
11874 	return 0;
11875 }
11876 
11877 /**
11878  * i40e_enter_busy_conf - Enters busy config state
11879  * @vsi: vsi
11880  *
11881  * Returns 0 on success, <0 for failure.
11882  **/
11883 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
11884 {
11885 	struct i40e_pf *pf = vsi->back;
11886 	int timeout = 50;
11887 
11888 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
11889 		timeout--;
11890 		if (!timeout)
11891 			return -EBUSY;
11892 		usleep_range(1000, 2000);
11893 	}
11894 
11895 	return 0;
11896 }
11897 
11898 /**
11899  * i40e_exit_busy_conf - Exits busy config state
11900  * @vsi: vsi
11901  **/
11902 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
11903 {
11904 	struct i40e_pf *pf = vsi->back;
11905 
11906 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
11907 }
11908 
11909 /**
11910  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
11911  * @vsi: vsi
11912  * @queue_pair: queue pair
11913  **/
11914 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
11915 {
11916 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
11917 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
11918 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
11919 	       sizeof(vsi->tx_rings[queue_pair]->stats));
11920 	if (i40e_enabled_xdp_vsi(vsi)) {
11921 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
11922 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
11923 	}
11924 }
11925 
11926 /**
11927  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
11928  * @vsi: vsi
11929  * @queue_pair: queue pair
11930  **/
11931 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
11932 {
11933 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
11934 	if (i40e_enabled_xdp_vsi(vsi)) {
11935 		/* Make sure that in-progress ndo_xdp_xmit calls are
11936 		 * completed.
11937 		 */
11938 		synchronize_rcu();
11939 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
11940 	}
11941 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
11942 }
11943 
11944 /**
11945  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
11946  * @vsi: vsi
11947  * @queue_pair: queue pair
11948  * @enable: true for enable, false for disable
11949  **/
11950 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
11951 					bool enable)
11952 {
11953 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
11954 	struct i40e_q_vector *q_vector = rxr->q_vector;
11955 
11956 	if (!vsi->netdev)
11957 		return;
11958 
11959 	/* All rings in a qp belong to the same qvector. */
11960 	if (q_vector->rx.ring || q_vector->tx.ring) {
11961 		if (enable)
11962 			napi_enable(&q_vector->napi);
11963 		else
11964 			napi_disable(&q_vector->napi);
11965 	}
11966 }
11967 
11968 /**
11969  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
11970  * @vsi: vsi
11971  * @queue_pair: queue pair
11972  * @enable: true for enable, false for disable
11973  *
11974  * Returns 0 on success, <0 on failure.
11975  **/
11976 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
11977 					bool enable)
11978 {
11979 	struct i40e_pf *pf = vsi->back;
11980 	int pf_q, ret = 0;
11981 
11982 	pf_q = vsi->base_queue + queue_pair;
11983 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
11984 				     false /*is xdp*/, enable);
11985 	if (ret) {
11986 		dev_info(&pf->pdev->dev,
11987 			 "VSI seid %d Tx ring %d %sable timeout\n",
11988 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
11989 		return ret;
11990 	}
11991 
11992 	i40e_control_rx_q(pf, pf_q, enable);
11993 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
11994 	if (ret) {
11995 		dev_info(&pf->pdev->dev,
11996 			 "VSI seid %d Rx ring %d %sable timeout\n",
11997 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
11998 		return ret;
11999 	}
12000 
12001 	/* Due to HW errata, on Rx disable only, the register can
12002 	 * indicate done before it really is. Needs 50ms to be sure
12003 	 */
12004 	if (!enable)
12005 		mdelay(50);
12006 
12007 	if (!i40e_enabled_xdp_vsi(vsi))
12008 		return ret;
12009 
12010 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12011 				     pf_q + vsi->alloc_queue_pairs,
12012 				     true /*is xdp*/, enable);
12013 	if (ret) {
12014 		dev_info(&pf->pdev->dev,
12015 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12016 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12017 	}
12018 
12019 	return ret;
12020 }
12021 
12022 /**
12023  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12024  * @vsi: vsi
12025  * @queue_pair: queue_pair
12026  **/
12027 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12028 {
12029 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12030 	struct i40e_pf *pf = vsi->back;
12031 	struct i40e_hw *hw = &pf->hw;
12032 
12033 	/* All rings in a qp belong to the same qvector. */
12034 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12035 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12036 	else
12037 		i40e_irq_dynamic_enable_icr0(pf);
12038 
12039 	i40e_flush(hw);
12040 }
12041 
12042 /**
12043  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12044  * @vsi: vsi
12045  * @queue_pair: queue_pair
12046  **/
12047 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12048 {
12049 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12050 	struct i40e_pf *pf = vsi->back;
12051 	struct i40e_hw *hw = &pf->hw;
12052 
12053 	/* For simplicity, instead of removing the qp interrupt causes
12054 	 * from the interrupt linked list, we simply disable the interrupt, and
12055 	 * leave the list intact.
12056 	 *
12057 	 * All rings in a qp belong to the same qvector.
12058 	 */
12059 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12060 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12061 
12062 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12063 		i40e_flush(hw);
12064 		synchronize_irq(pf->msix_entries[intpf].vector);
12065 	} else {
12066 		/* Legacy and MSI mode - this stops all interrupt handling */
12067 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12068 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12069 		i40e_flush(hw);
12070 		synchronize_irq(pf->pdev->irq);
12071 	}
12072 }
12073 
12074 /**
12075  * i40e_queue_pair_disable - Disables a queue pair
12076  * @vsi: vsi
12077  * @queue_pair: queue pair
12078  *
12079  * Returns 0 on success, <0 on failure.
12080  **/
12081 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12082 {
12083 	int err;
12084 
12085 	err = i40e_enter_busy_conf(vsi);
12086 	if (err)
12087 		return err;
12088 
12089 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12090 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12091 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12092 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12093 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12094 
12095 	return err;
12096 }
12097 
12098 /**
12099  * i40e_queue_pair_enable - Enables a queue pair
12100  * @vsi: vsi
12101  * @queue_pair: queue pair
12102  *
12103  * Returns 0 on success, <0 on failure.
12104  **/
12105 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12106 {
12107 	int err;
12108 
12109 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12110 	if (err)
12111 		return err;
12112 
12113 	if (i40e_enabled_xdp_vsi(vsi)) {
12114 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12115 		if (err)
12116 			return err;
12117 	}
12118 
12119 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12120 	if (err)
12121 		return err;
12122 
12123 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12124 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12125 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12126 
12127 	i40e_exit_busy_conf(vsi);
12128 
12129 	return err;
12130 }
12131 
12132 /**
12133  * i40e_xdp - implements ndo_bpf for i40e
12134  * @dev: netdevice
12135  * @xdp: XDP command
12136  **/
12137 static int i40e_xdp(struct net_device *dev,
12138 		    struct netdev_bpf *xdp)
12139 {
12140 	struct i40e_netdev_priv *np = netdev_priv(dev);
12141 	struct i40e_vsi *vsi = np->vsi;
12142 
12143 	if (vsi->type != I40E_VSI_MAIN)
12144 		return -EINVAL;
12145 
12146 	switch (xdp->command) {
12147 	case XDP_SETUP_PROG:
12148 		return i40e_xdp_setup(vsi, xdp->prog);
12149 	case XDP_QUERY_PROG:
12150 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
12151 		return 0;
12152 	case XDP_SETUP_XSK_UMEM:
12153 		return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
12154 					   xdp->xsk.queue_id);
12155 	default:
12156 		return -EINVAL;
12157 	}
12158 }
12159 
12160 static const struct net_device_ops i40e_netdev_ops = {
12161 	.ndo_open		= i40e_open,
12162 	.ndo_stop		= i40e_close,
12163 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12164 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12165 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12166 	.ndo_validate_addr	= eth_validate_addr,
12167 	.ndo_set_mac_address	= i40e_set_mac,
12168 	.ndo_change_mtu		= i40e_change_mtu,
12169 	.ndo_do_ioctl		= i40e_ioctl,
12170 	.ndo_tx_timeout		= i40e_tx_timeout,
12171 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12172 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12173 #ifdef CONFIG_NET_POLL_CONTROLLER
12174 	.ndo_poll_controller	= i40e_netpoll,
12175 #endif
12176 	.ndo_setup_tc		= __i40e_setup_tc,
12177 	.ndo_set_features	= i40e_set_features,
12178 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12179 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12180 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12181 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12182 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12183 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12184 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12185 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
12186 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
12187 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12188 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12189 	.ndo_features_check	= i40e_features_check,
12190 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12191 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12192 	.ndo_bpf		= i40e_xdp,
12193 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12194 	.ndo_xsk_async_xmit	= i40e_xsk_async_xmit,
12195 };
12196 
12197 /**
12198  * i40e_config_netdev - Setup the netdev flags
12199  * @vsi: the VSI being configured
12200  *
12201  * Returns 0 on success, negative value on failure
12202  **/
12203 static int i40e_config_netdev(struct i40e_vsi *vsi)
12204 {
12205 	struct i40e_pf *pf = vsi->back;
12206 	struct i40e_hw *hw = &pf->hw;
12207 	struct i40e_netdev_priv *np;
12208 	struct net_device *netdev;
12209 	u8 broadcast[ETH_ALEN];
12210 	u8 mac_addr[ETH_ALEN];
12211 	int etherdev_size;
12212 	netdev_features_t hw_enc_features;
12213 	netdev_features_t hw_features;
12214 
12215 	etherdev_size = sizeof(struct i40e_netdev_priv);
12216 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12217 	if (!netdev)
12218 		return -ENOMEM;
12219 
12220 	vsi->netdev = netdev;
12221 	np = netdev_priv(netdev);
12222 	np->vsi = vsi;
12223 
12224 	hw_enc_features = NETIF_F_SG			|
12225 			  NETIF_F_IP_CSUM		|
12226 			  NETIF_F_IPV6_CSUM		|
12227 			  NETIF_F_HIGHDMA		|
12228 			  NETIF_F_SOFT_FEATURES		|
12229 			  NETIF_F_TSO			|
12230 			  NETIF_F_TSO_ECN		|
12231 			  NETIF_F_TSO6			|
12232 			  NETIF_F_GSO_GRE		|
12233 			  NETIF_F_GSO_GRE_CSUM		|
12234 			  NETIF_F_GSO_PARTIAL		|
12235 			  NETIF_F_GSO_IPXIP4		|
12236 			  NETIF_F_GSO_IPXIP6		|
12237 			  NETIF_F_GSO_UDP_TUNNEL	|
12238 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12239 			  NETIF_F_SCTP_CRC		|
12240 			  NETIF_F_RXHASH		|
12241 			  NETIF_F_RXCSUM		|
12242 			  0;
12243 
12244 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12245 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12246 
12247 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12248 
12249 	netdev->hw_enc_features |= hw_enc_features;
12250 
12251 	/* record features VLANs can make use of */
12252 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12253 
12254 	hw_features = hw_enc_features		|
12255 		      NETIF_F_HW_VLAN_CTAG_TX	|
12256 		      NETIF_F_HW_VLAN_CTAG_RX;
12257 
12258 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12259 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12260 
12261 	netdev->hw_features |= hw_features;
12262 
12263 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12264 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12265 
12266 	if (vsi->type == I40E_VSI_MAIN) {
12267 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
12268 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
12269 		/* The following steps are necessary for two reasons. First,
12270 		 * some older NVM configurations load a default MAC-VLAN
12271 		 * filter that will accept any tagged packet, and we want to
12272 		 * replace this with a normal filter. Additionally, it is
12273 		 * possible our MAC address was provided by the platform using
12274 		 * Open Firmware or similar.
12275 		 *
12276 		 * Thus, we need to remove the default filter and install one
12277 		 * specific to the MAC address.
12278 		 */
12279 		i40e_rm_default_mac_filter(vsi, mac_addr);
12280 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12281 		i40e_add_mac_filter(vsi, mac_addr);
12282 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12283 	} else {
12284 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
12285 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
12286 		 * the end, which is 4 bytes long, so force truncation of the
12287 		 * original name by IFNAMSIZ - 4
12288 		 */
12289 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
12290 			 IFNAMSIZ - 4,
12291 			 pf->vsi[pf->lan_vsi]->netdev->name);
12292 		eth_random_addr(mac_addr);
12293 
12294 		spin_lock_bh(&vsi->mac_filter_hash_lock);
12295 		i40e_add_mac_filter(vsi, mac_addr);
12296 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
12297 	}
12298 
12299 	/* Add the broadcast filter so that we initially will receive
12300 	 * broadcast packets. Note that when a new VLAN is first added the
12301 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12302 	 * specific filters as part of transitioning into "vlan" operation.
12303 	 * When more VLANs are added, the driver will copy each existing MAC
12304 	 * filter and add it for the new VLAN.
12305 	 *
12306 	 * Broadcast filters are handled specially by
12307 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
12308 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
12309 	 * filter. The subtask will update the correct broadcast promiscuous
12310 	 * bits as VLANs become active or inactive.
12311 	 */
12312 	eth_broadcast_addr(broadcast);
12313 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12314 	i40e_add_mac_filter(vsi, broadcast);
12315 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12316 
12317 	ether_addr_copy(netdev->dev_addr, mac_addr);
12318 	ether_addr_copy(netdev->perm_addr, mac_addr);
12319 
12320 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
12321 	netdev->neigh_priv_len = sizeof(u32) * 4;
12322 
12323 	netdev->priv_flags |= IFF_UNICAST_FLT;
12324 	netdev->priv_flags |= IFF_SUPP_NOFCS;
12325 	/* Setup netdev TC information */
12326 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12327 
12328 	netdev->netdev_ops = &i40e_netdev_ops;
12329 	netdev->watchdog_timeo = 5 * HZ;
12330 	i40e_set_ethtool_ops(netdev);
12331 
12332 	/* MTU range: 68 - 9706 */
12333 	netdev->min_mtu = ETH_MIN_MTU;
12334 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12335 
12336 	return 0;
12337 }
12338 
12339 /**
12340  * i40e_vsi_delete - Delete a VSI from the switch
12341  * @vsi: the VSI being removed
12342  *
12343  * Returns 0 on success, negative value on failure
12344  **/
12345 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12346 {
12347 	/* remove default VSI is not allowed */
12348 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12349 		return;
12350 
12351 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
12352 }
12353 
12354 /**
12355  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
12356  * @vsi: the VSI being queried
12357  *
12358  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
12359  **/
12360 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
12361 {
12362 	struct i40e_veb *veb;
12363 	struct i40e_pf *pf = vsi->back;
12364 
12365 	/* Uplink is not a bridge so default to VEB */
12366 	if (vsi->veb_idx == I40E_NO_VEB)
12367 		return 1;
12368 
12369 	veb = pf->veb[vsi->veb_idx];
12370 	if (!veb) {
12371 		dev_info(&pf->pdev->dev,
12372 			 "There is no veb associated with the bridge\n");
12373 		return -ENOENT;
12374 	}
12375 
12376 	/* Uplink is a bridge in VEPA mode */
12377 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
12378 		return 0;
12379 	} else {
12380 		/* Uplink is a bridge in VEB mode */
12381 		return 1;
12382 	}
12383 
12384 	/* VEPA is now default bridge, so return 0 */
12385 	return 0;
12386 }
12387 
12388 /**
12389  * i40e_add_vsi - Add a VSI to the switch
12390  * @vsi: the VSI being configured
12391  *
12392  * This initializes a VSI context depending on the VSI type to be added and
12393  * passes it down to the add_vsi aq command.
12394  **/
12395 static int i40e_add_vsi(struct i40e_vsi *vsi)
12396 {
12397 	int ret = -ENODEV;
12398 	struct i40e_pf *pf = vsi->back;
12399 	struct i40e_hw *hw = &pf->hw;
12400 	struct i40e_vsi_context ctxt;
12401 	struct i40e_mac_filter *f;
12402 	struct hlist_node *h;
12403 	int bkt;
12404 
12405 	u8 enabled_tc = 0x1; /* TC0 enabled */
12406 	int f_count = 0;
12407 
12408 	memset(&ctxt, 0, sizeof(ctxt));
12409 	switch (vsi->type) {
12410 	case I40E_VSI_MAIN:
12411 		/* The PF's main VSI is already setup as part of the
12412 		 * device initialization, so we'll not bother with
12413 		 * the add_vsi call, but we will retrieve the current
12414 		 * VSI context.
12415 		 */
12416 		ctxt.seid = pf->main_vsi_seid;
12417 		ctxt.pf_num = pf->hw.pf_id;
12418 		ctxt.vf_num = 0;
12419 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
12420 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12421 		if (ret) {
12422 			dev_info(&pf->pdev->dev,
12423 				 "couldn't get PF vsi config, err %s aq_err %s\n",
12424 				 i40e_stat_str(&pf->hw, ret),
12425 				 i40e_aq_str(&pf->hw,
12426 					     pf->hw.aq.asq_last_status));
12427 			return -ENOENT;
12428 		}
12429 		vsi->info = ctxt.info;
12430 		vsi->info.valid_sections = 0;
12431 
12432 		vsi->seid = ctxt.seid;
12433 		vsi->id = ctxt.vsi_number;
12434 
12435 		enabled_tc = i40e_pf_get_tc_map(pf);
12436 
12437 		/* Source pruning is enabled by default, so the flag is
12438 		 * negative logic - if it's set, we need to fiddle with
12439 		 * the VSI to disable source pruning.
12440 		 */
12441 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
12442 			memset(&ctxt, 0, sizeof(ctxt));
12443 			ctxt.seid = pf->main_vsi_seid;
12444 			ctxt.pf_num = pf->hw.pf_id;
12445 			ctxt.vf_num = 0;
12446 			ctxt.info.valid_sections |=
12447 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12448 			ctxt.info.switch_id =
12449 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
12450 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12451 			if (ret) {
12452 				dev_info(&pf->pdev->dev,
12453 					 "update vsi failed, err %s aq_err %s\n",
12454 					 i40e_stat_str(&pf->hw, ret),
12455 					 i40e_aq_str(&pf->hw,
12456 						     pf->hw.aq.asq_last_status));
12457 				ret = -ENOENT;
12458 				goto err;
12459 			}
12460 		}
12461 
12462 		/* MFP mode setup queue map and update VSI */
12463 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
12464 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
12465 			memset(&ctxt, 0, sizeof(ctxt));
12466 			ctxt.seid = pf->main_vsi_seid;
12467 			ctxt.pf_num = pf->hw.pf_id;
12468 			ctxt.vf_num = 0;
12469 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
12470 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12471 			if (ret) {
12472 				dev_info(&pf->pdev->dev,
12473 					 "update vsi failed, err %s aq_err %s\n",
12474 					 i40e_stat_str(&pf->hw, ret),
12475 					 i40e_aq_str(&pf->hw,
12476 						    pf->hw.aq.asq_last_status));
12477 				ret = -ENOENT;
12478 				goto err;
12479 			}
12480 			/* update the local VSI info queue map */
12481 			i40e_vsi_update_queue_map(vsi, &ctxt);
12482 			vsi->info.valid_sections = 0;
12483 		} else {
12484 			/* Default/Main VSI is only enabled for TC0
12485 			 * reconfigure it to enable all TCs that are
12486 			 * available on the port in SFP mode.
12487 			 * For MFP case the iSCSI PF would use this
12488 			 * flow to enable LAN+iSCSI TC.
12489 			 */
12490 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
12491 			if (ret) {
12492 				/* Single TC condition is not fatal,
12493 				 * message and continue
12494 				 */
12495 				dev_info(&pf->pdev->dev,
12496 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12497 					 enabled_tc,
12498 					 i40e_stat_str(&pf->hw, ret),
12499 					 i40e_aq_str(&pf->hw,
12500 						    pf->hw.aq.asq_last_status));
12501 			}
12502 		}
12503 		break;
12504 
12505 	case I40E_VSI_FDIR:
12506 		ctxt.pf_num = hw->pf_id;
12507 		ctxt.vf_num = 0;
12508 		ctxt.uplink_seid = vsi->uplink_seid;
12509 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12510 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12511 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12512 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
12513 			ctxt.info.valid_sections |=
12514 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12515 			ctxt.info.switch_id =
12516 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12517 		}
12518 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12519 		break;
12520 
12521 	case I40E_VSI_VMDQ2:
12522 		ctxt.pf_num = hw->pf_id;
12523 		ctxt.vf_num = 0;
12524 		ctxt.uplink_seid = vsi->uplink_seid;
12525 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12526 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12527 
12528 		/* This VSI is connected to VEB so the switch_id
12529 		 * should be set to zero by default.
12530 		 */
12531 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12532 			ctxt.info.valid_sections |=
12533 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12534 			ctxt.info.switch_id =
12535 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12536 		}
12537 
12538 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12539 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12540 		break;
12541 
12542 	case I40E_VSI_SRIOV:
12543 		ctxt.pf_num = hw->pf_id;
12544 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12545 		ctxt.uplink_seid = vsi->uplink_seid;
12546 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12547 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12548 
12549 		/* This VSI is connected to VEB so the switch_id
12550 		 * should be set to zero by default.
12551 		 */
12552 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12553 			ctxt.info.valid_sections |=
12554 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12555 			ctxt.info.switch_id =
12556 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12557 		}
12558 
12559 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12560 			ctxt.info.valid_sections |=
12561 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12562 			ctxt.info.queueing_opt_flags |=
12563 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12564 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12565 		}
12566 
12567 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12568 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12569 		if (pf->vf[vsi->vf_id].spoofchk) {
12570 			ctxt.info.valid_sections |=
12571 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12572 			ctxt.info.sec_flags |=
12573 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12574 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12575 		}
12576 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12577 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12578 		break;
12579 
12580 	case I40E_VSI_IWARP:
12581 		/* send down message to iWARP */
12582 		break;
12583 
12584 	default:
12585 		return -ENODEV;
12586 	}
12587 
12588 	if (vsi->type != I40E_VSI_MAIN) {
12589 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12590 		if (ret) {
12591 			dev_info(&vsi->back->pdev->dev,
12592 				 "add vsi failed, err %s aq_err %s\n",
12593 				 i40e_stat_str(&pf->hw, ret),
12594 				 i40e_aq_str(&pf->hw,
12595 					     pf->hw.aq.asq_last_status));
12596 			ret = -ENOENT;
12597 			goto err;
12598 		}
12599 		vsi->info = ctxt.info;
12600 		vsi->info.valid_sections = 0;
12601 		vsi->seid = ctxt.seid;
12602 		vsi->id = ctxt.vsi_number;
12603 	}
12604 
12605 	vsi->active_filters = 0;
12606 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12607 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12608 	/* If macvlan filters already exist, force them to get loaded */
12609 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12610 		f->state = I40E_FILTER_NEW;
12611 		f_count++;
12612 	}
12613 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12614 
12615 	if (f_count) {
12616 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12617 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
12618 	}
12619 
12620 	/* Update VSI BW information */
12621 	ret = i40e_vsi_get_bw_info(vsi);
12622 	if (ret) {
12623 		dev_info(&pf->pdev->dev,
12624 			 "couldn't get vsi bw info, err %s aq_err %s\n",
12625 			 i40e_stat_str(&pf->hw, ret),
12626 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12627 		/* VSI is already added so not tearing that up */
12628 		ret = 0;
12629 	}
12630 
12631 err:
12632 	return ret;
12633 }
12634 
12635 /**
12636  * i40e_vsi_release - Delete a VSI and free its resources
12637  * @vsi: the VSI being removed
12638  *
12639  * Returns 0 on success or < 0 on error
12640  **/
12641 int i40e_vsi_release(struct i40e_vsi *vsi)
12642 {
12643 	struct i40e_mac_filter *f;
12644 	struct hlist_node *h;
12645 	struct i40e_veb *veb = NULL;
12646 	struct i40e_pf *pf;
12647 	u16 uplink_seid;
12648 	int i, n, bkt;
12649 
12650 	pf = vsi->back;
12651 
12652 	/* release of a VEB-owner or last VSI is not allowed */
12653 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12654 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12655 			 vsi->seid, vsi->uplink_seid);
12656 		return -ENODEV;
12657 	}
12658 	if (vsi == pf->vsi[pf->lan_vsi] &&
12659 	    !test_bit(__I40E_DOWN, pf->state)) {
12660 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12661 		return -ENODEV;
12662 	}
12663 
12664 	uplink_seid = vsi->uplink_seid;
12665 	if (vsi->type != I40E_VSI_SRIOV) {
12666 		if (vsi->netdev_registered) {
12667 			vsi->netdev_registered = false;
12668 			if (vsi->netdev) {
12669 				/* results in a call to i40e_close() */
12670 				unregister_netdev(vsi->netdev);
12671 			}
12672 		} else {
12673 			i40e_vsi_close(vsi);
12674 		}
12675 		i40e_vsi_disable_irq(vsi);
12676 	}
12677 
12678 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12679 
12680 	/* clear the sync flag on all filters */
12681 	if (vsi->netdev) {
12682 		__dev_uc_unsync(vsi->netdev, NULL);
12683 		__dev_mc_unsync(vsi->netdev, NULL);
12684 	}
12685 
12686 	/* make sure any remaining filters are marked for deletion */
12687 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12688 		__i40e_del_filter(vsi, f);
12689 
12690 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12691 
12692 	i40e_sync_vsi_filters(vsi);
12693 
12694 	i40e_vsi_delete(vsi);
12695 	i40e_vsi_free_q_vectors(vsi);
12696 	if (vsi->netdev) {
12697 		free_netdev(vsi->netdev);
12698 		vsi->netdev = NULL;
12699 	}
12700 	i40e_vsi_clear_rings(vsi);
12701 	i40e_vsi_clear(vsi);
12702 
12703 	/* If this was the last thing on the VEB, except for the
12704 	 * controlling VSI, remove the VEB, which puts the controlling
12705 	 * VSI onto the next level down in the switch.
12706 	 *
12707 	 * Well, okay, there's one more exception here: don't remove
12708 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
12709 	 * from up the network stack.
12710 	 */
12711 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12712 		if (pf->vsi[i] &&
12713 		    pf->vsi[i]->uplink_seid == uplink_seid &&
12714 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12715 			n++;      /* count the VSIs */
12716 		}
12717 	}
12718 	for (i = 0; i < I40E_MAX_VEB; i++) {
12719 		if (!pf->veb[i])
12720 			continue;
12721 		if (pf->veb[i]->uplink_seid == uplink_seid)
12722 			n++;     /* count the VEBs */
12723 		if (pf->veb[i]->seid == uplink_seid)
12724 			veb = pf->veb[i];
12725 	}
12726 	if (n == 0 && veb && veb->uplink_seid != 0)
12727 		i40e_veb_release(veb);
12728 
12729 	return 0;
12730 }
12731 
12732 /**
12733  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12734  * @vsi: ptr to the VSI
12735  *
12736  * This should only be called after i40e_vsi_mem_alloc() which allocates the
12737  * corresponding SW VSI structure and initializes num_queue_pairs for the
12738  * newly allocated VSI.
12739  *
12740  * Returns 0 on success or negative on failure
12741  **/
12742 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12743 {
12744 	int ret = -ENOENT;
12745 	struct i40e_pf *pf = vsi->back;
12746 
12747 	if (vsi->q_vectors[0]) {
12748 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12749 			 vsi->seid);
12750 		return -EEXIST;
12751 	}
12752 
12753 	if (vsi->base_vector) {
12754 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12755 			 vsi->seid, vsi->base_vector);
12756 		return -EEXIST;
12757 	}
12758 
12759 	ret = i40e_vsi_alloc_q_vectors(vsi);
12760 	if (ret) {
12761 		dev_info(&pf->pdev->dev,
12762 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12763 			 vsi->num_q_vectors, vsi->seid, ret);
12764 		vsi->num_q_vectors = 0;
12765 		goto vector_setup_out;
12766 	}
12767 
12768 	/* In Legacy mode, we do not have to get any other vector since we
12769 	 * piggyback on the misc/ICR0 for queue interrupts.
12770 	*/
12771 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12772 		return ret;
12773 	if (vsi->num_q_vectors)
12774 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12775 						 vsi->num_q_vectors, vsi->idx);
12776 	if (vsi->base_vector < 0) {
12777 		dev_info(&pf->pdev->dev,
12778 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12779 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12780 		i40e_vsi_free_q_vectors(vsi);
12781 		ret = -ENOENT;
12782 		goto vector_setup_out;
12783 	}
12784 
12785 vector_setup_out:
12786 	return ret;
12787 }
12788 
12789 /**
12790  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12791  * @vsi: pointer to the vsi.
12792  *
12793  * This re-allocates a vsi's queue resources.
12794  *
12795  * Returns pointer to the successfully allocated and configured VSI sw struct
12796  * on success, otherwise returns NULL on failure.
12797  **/
12798 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12799 {
12800 	u16 alloc_queue_pairs;
12801 	struct i40e_pf *pf;
12802 	u8 enabled_tc;
12803 	int ret;
12804 
12805 	if (!vsi)
12806 		return NULL;
12807 
12808 	pf = vsi->back;
12809 
12810 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12811 	i40e_vsi_clear_rings(vsi);
12812 
12813 	i40e_vsi_free_arrays(vsi, false);
12814 	i40e_set_num_rings_in_vsi(vsi);
12815 	ret = i40e_vsi_alloc_arrays(vsi, false);
12816 	if (ret)
12817 		goto err_vsi;
12818 
12819 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12820 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12821 
12822 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12823 	if (ret < 0) {
12824 		dev_info(&pf->pdev->dev,
12825 			 "failed to get tracking for %d queues for VSI %d err %d\n",
12826 			 alloc_queue_pairs, vsi->seid, ret);
12827 		goto err_vsi;
12828 	}
12829 	vsi->base_queue = ret;
12830 
12831 	/* Update the FW view of the VSI. Force a reset of TC and queue
12832 	 * layout configurations.
12833 	 */
12834 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12835 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12836 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12837 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12838 	if (vsi->type == I40E_VSI_MAIN)
12839 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12840 
12841 	/* assign it some queues */
12842 	ret = i40e_alloc_rings(vsi);
12843 	if (ret)
12844 		goto err_rings;
12845 
12846 	/* map all of the rings to the q_vectors */
12847 	i40e_vsi_map_rings_to_vectors(vsi);
12848 	return vsi;
12849 
12850 err_rings:
12851 	i40e_vsi_free_q_vectors(vsi);
12852 	if (vsi->netdev_registered) {
12853 		vsi->netdev_registered = false;
12854 		unregister_netdev(vsi->netdev);
12855 		free_netdev(vsi->netdev);
12856 		vsi->netdev = NULL;
12857 	}
12858 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12859 err_vsi:
12860 	i40e_vsi_clear(vsi);
12861 	return NULL;
12862 }
12863 
12864 /**
12865  * i40e_vsi_setup - Set up a VSI by a given type
12866  * @pf: board private structure
12867  * @type: VSI type
12868  * @uplink_seid: the switch element to link to
12869  * @param1: usage depends upon VSI type. For VF types, indicates VF id
12870  *
12871  * This allocates the sw VSI structure and its queue resources, then add a VSI
12872  * to the identified VEB.
12873  *
12874  * Returns pointer to the successfully allocated and configure VSI sw struct on
12875  * success, otherwise returns NULL on failure.
12876  **/
12877 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12878 				u16 uplink_seid, u32 param1)
12879 {
12880 	struct i40e_vsi *vsi = NULL;
12881 	struct i40e_veb *veb = NULL;
12882 	u16 alloc_queue_pairs;
12883 	int ret, i;
12884 	int v_idx;
12885 
12886 	/* The requested uplink_seid must be either
12887 	 *     - the PF's port seid
12888 	 *              no VEB is needed because this is the PF
12889 	 *              or this is a Flow Director special case VSI
12890 	 *     - seid of an existing VEB
12891 	 *     - seid of a VSI that owns an existing VEB
12892 	 *     - seid of a VSI that doesn't own a VEB
12893 	 *              a new VEB is created and the VSI becomes the owner
12894 	 *     - seid of the PF VSI, which is what creates the first VEB
12895 	 *              this is a special case of the previous
12896 	 *
12897 	 * Find which uplink_seid we were given and create a new VEB if needed
12898 	 */
12899 	for (i = 0; i < I40E_MAX_VEB; i++) {
12900 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12901 			veb = pf->veb[i];
12902 			break;
12903 		}
12904 	}
12905 
12906 	if (!veb && uplink_seid != pf->mac_seid) {
12907 
12908 		for (i = 0; i < pf->num_alloc_vsi; i++) {
12909 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12910 				vsi = pf->vsi[i];
12911 				break;
12912 			}
12913 		}
12914 		if (!vsi) {
12915 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12916 				 uplink_seid);
12917 			return NULL;
12918 		}
12919 
12920 		if (vsi->uplink_seid == pf->mac_seid)
12921 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12922 					     vsi->tc_config.enabled_tc);
12923 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12924 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12925 					     vsi->tc_config.enabled_tc);
12926 		if (veb) {
12927 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12928 				dev_info(&vsi->back->pdev->dev,
12929 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
12930 				return NULL;
12931 			}
12932 			/* We come up by default in VEPA mode if SRIOV is not
12933 			 * already enabled, in which case we can't force VEPA
12934 			 * mode.
12935 			 */
12936 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12937 				veb->bridge_mode = BRIDGE_MODE_VEPA;
12938 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12939 			}
12940 			i40e_config_bridge_mode(veb);
12941 		}
12942 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12943 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12944 				veb = pf->veb[i];
12945 		}
12946 		if (!veb) {
12947 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12948 			return NULL;
12949 		}
12950 
12951 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12952 		uplink_seid = veb->seid;
12953 	}
12954 
12955 	/* get vsi sw struct */
12956 	v_idx = i40e_vsi_mem_alloc(pf, type);
12957 	if (v_idx < 0)
12958 		goto err_alloc;
12959 	vsi = pf->vsi[v_idx];
12960 	if (!vsi)
12961 		goto err_alloc;
12962 	vsi->type = type;
12963 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12964 
12965 	if (type == I40E_VSI_MAIN)
12966 		pf->lan_vsi = v_idx;
12967 	else if (type == I40E_VSI_SRIOV)
12968 		vsi->vf_id = param1;
12969 	/* assign it some queues */
12970 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12971 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12972 
12973 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12974 	if (ret < 0) {
12975 		dev_info(&pf->pdev->dev,
12976 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
12977 			 alloc_queue_pairs, vsi->seid, ret);
12978 		goto err_vsi;
12979 	}
12980 	vsi->base_queue = ret;
12981 
12982 	/* get a VSI from the hardware */
12983 	vsi->uplink_seid = uplink_seid;
12984 	ret = i40e_add_vsi(vsi);
12985 	if (ret)
12986 		goto err_vsi;
12987 
12988 	switch (vsi->type) {
12989 	/* setup the netdev if needed */
12990 	case I40E_VSI_MAIN:
12991 	case I40E_VSI_VMDQ2:
12992 		ret = i40e_config_netdev(vsi);
12993 		if (ret)
12994 			goto err_netdev;
12995 		ret = register_netdev(vsi->netdev);
12996 		if (ret)
12997 			goto err_netdev;
12998 		vsi->netdev_registered = true;
12999 		netif_carrier_off(vsi->netdev);
13000 #ifdef CONFIG_I40E_DCB
13001 		/* Setup DCB netlink interface */
13002 		i40e_dcbnl_setup(vsi);
13003 #endif /* CONFIG_I40E_DCB */
13004 		/* fall through */
13005 
13006 	case I40E_VSI_FDIR:
13007 		/* set up vectors and rings if needed */
13008 		ret = i40e_vsi_setup_vectors(vsi);
13009 		if (ret)
13010 			goto err_msix;
13011 
13012 		ret = i40e_alloc_rings(vsi);
13013 		if (ret)
13014 			goto err_rings;
13015 
13016 		/* map all of the rings to the q_vectors */
13017 		i40e_vsi_map_rings_to_vectors(vsi);
13018 
13019 		i40e_vsi_reset_stats(vsi);
13020 		break;
13021 
13022 	default:
13023 		/* no netdev or rings for the other VSI types */
13024 		break;
13025 	}
13026 
13027 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13028 	    (vsi->type == I40E_VSI_VMDQ2)) {
13029 		ret = i40e_vsi_config_rss(vsi);
13030 	}
13031 	return vsi;
13032 
13033 err_rings:
13034 	i40e_vsi_free_q_vectors(vsi);
13035 err_msix:
13036 	if (vsi->netdev_registered) {
13037 		vsi->netdev_registered = false;
13038 		unregister_netdev(vsi->netdev);
13039 		free_netdev(vsi->netdev);
13040 		vsi->netdev = NULL;
13041 	}
13042 err_netdev:
13043 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13044 err_vsi:
13045 	i40e_vsi_clear(vsi);
13046 err_alloc:
13047 	return NULL;
13048 }
13049 
13050 /**
13051  * i40e_veb_get_bw_info - Query VEB BW information
13052  * @veb: the veb to query
13053  *
13054  * Query the Tx scheduler BW configuration data for given VEB
13055  **/
13056 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13057 {
13058 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13059 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13060 	struct i40e_pf *pf = veb->pf;
13061 	struct i40e_hw *hw = &pf->hw;
13062 	u32 tc_bw_max;
13063 	int ret = 0;
13064 	int i;
13065 
13066 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13067 						  &bw_data, NULL);
13068 	if (ret) {
13069 		dev_info(&pf->pdev->dev,
13070 			 "query veb bw config failed, err %s aq_err %s\n",
13071 			 i40e_stat_str(&pf->hw, ret),
13072 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13073 		goto out;
13074 	}
13075 
13076 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13077 						   &ets_data, NULL);
13078 	if (ret) {
13079 		dev_info(&pf->pdev->dev,
13080 			 "query veb bw ets config failed, err %s aq_err %s\n",
13081 			 i40e_stat_str(&pf->hw, ret),
13082 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13083 		goto out;
13084 	}
13085 
13086 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13087 	veb->bw_max_quanta = ets_data.tc_bw_max;
13088 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13089 	veb->enabled_tc = ets_data.tc_valid_bits;
13090 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13091 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13092 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13093 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13094 		veb->bw_tc_limit_credits[i] =
13095 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13096 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13097 	}
13098 
13099 out:
13100 	return ret;
13101 }
13102 
13103 /**
13104  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13105  * @pf: board private structure
13106  *
13107  * On error: returns error code (negative)
13108  * On success: returns vsi index in PF (positive)
13109  **/
13110 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13111 {
13112 	int ret = -ENOENT;
13113 	struct i40e_veb *veb;
13114 	int i;
13115 
13116 	/* Need to protect the allocation of switch elements at the PF level */
13117 	mutex_lock(&pf->switch_mutex);
13118 
13119 	/* VEB list may be fragmented if VEB creation/destruction has
13120 	 * been happening.  We can afford to do a quick scan to look
13121 	 * for any free slots in the list.
13122 	 *
13123 	 * find next empty veb slot, looping back around if necessary
13124 	 */
13125 	i = 0;
13126 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13127 		i++;
13128 	if (i >= I40E_MAX_VEB) {
13129 		ret = -ENOMEM;
13130 		goto err_alloc_veb;  /* out of VEB slots! */
13131 	}
13132 
13133 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13134 	if (!veb) {
13135 		ret = -ENOMEM;
13136 		goto err_alloc_veb;
13137 	}
13138 	veb->pf = pf;
13139 	veb->idx = i;
13140 	veb->enabled_tc = 1;
13141 
13142 	pf->veb[i] = veb;
13143 	ret = i;
13144 err_alloc_veb:
13145 	mutex_unlock(&pf->switch_mutex);
13146 	return ret;
13147 }
13148 
13149 /**
13150  * i40e_switch_branch_release - Delete a branch of the switch tree
13151  * @branch: where to start deleting
13152  *
13153  * This uses recursion to find the tips of the branch to be
13154  * removed, deleting until we get back to and can delete this VEB.
13155  **/
13156 static void i40e_switch_branch_release(struct i40e_veb *branch)
13157 {
13158 	struct i40e_pf *pf = branch->pf;
13159 	u16 branch_seid = branch->seid;
13160 	u16 veb_idx = branch->idx;
13161 	int i;
13162 
13163 	/* release any VEBs on this VEB - RECURSION */
13164 	for (i = 0; i < I40E_MAX_VEB; i++) {
13165 		if (!pf->veb[i])
13166 			continue;
13167 		if (pf->veb[i]->uplink_seid == branch->seid)
13168 			i40e_switch_branch_release(pf->veb[i]);
13169 	}
13170 
13171 	/* Release the VSIs on this VEB, but not the owner VSI.
13172 	 *
13173 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13174 	 *       the VEB itself, so don't use (*branch) after this loop.
13175 	 */
13176 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13177 		if (!pf->vsi[i])
13178 			continue;
13179 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13180 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13181 			i40e_vsi_release(pf->vsi[i]);
13182 		}
13183 	}
13184 
13185 	/* There's one corner case where the VEB might not have been
13186 	 * removed, so double check it here and remove it if needed.
13187 	 * This case happens if the veb was created from the debugfs
13188 	 * commands and no VSIs were added to it.
13189 	 */
13190 	if (pf->veb[veb_idx])
13191 		i40e_veb_release(pf->veb[veb_idx]);
13192 }
13193 
13194 /**
13195  * i40e_veb_clear - remove veb struct
13196  * @veb: the veb to remove
13197  **/
13198 static void i40e_veb_clear(struct i40e_veb *veb)
13199 {
13200 	if (!veb)
13201 		return;
13202 
13203 	if (veb->pf) {
13204 		struct i40e_pf *pf = veb->pf;
13205 
13206 		mutex_lock(&pf->switch_mutex);
13207 		if (pf->veb[veb->idx] == veb)
13208 			pf->veb[veb->idx] = NULL;
13209 		mutex_unlock(&pf->switch_mutex);
13210 	}
13211 
13212 	kfree(veb);
13213 }
13214 
13215 /**
13216  * i40e_veb_release - Delete a VEB and free its resources
13217  * @veb: the VEB being removed
13218  **/
13219 void i40e_veb_release(struct i40e_veb *veb)
13220 {
13221 	struct i40e_vsi *vsi = NULL;
13222 	struct i40e_pf *pf;
13223 	int i, n = 0;
13224 
13225 	pf = veb->pf;
13226 
13227 	/* find the remaining VSI and check for extras */
13228 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13229 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13230 			n++;
13231 			vsi = pf->vsi[i];
13232 		}
13233 	}
13234 	if (n != 1) {
13235 		dev_info(&pf->pdev->dev,
13236 			 "can't remove VEB %d with %d VSIs left\n",
13237 			 veb->seid, n);
13238 		return;
13239 	}
13240 
13241 	/* move the remaining VSI to uplink veb */
13242 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13243 	if (veb->uplink_seid) {
13244 		vsi->uplink_seid = veb->uplink_seid;
13245 		if (veb->uplink_seid == pf->mac_seid)
13246 			vsi->veb_idx = I40E_NO_VEB;
13247 		else
13248 			vsi->veb_idx = veb->veb_idx;
13249 	} else {
13250 		/* floating VEB */
13251 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13252 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13253 	}
13254 
13255 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13256 	i40e_veb_clear(veb);
13257 }
13258 
13259 /**
13260  * i40e_add_veb - create the VEB in the switch
13261  * @veb: the VEB to be instantiated
13262  * @vsi: the controlling VSI
13263  **/
13264 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13265 {
13266 	struct i40e_pf *pf = veb->pf;
13267 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
13268 	int ret;
13269 
13270 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
13271 			      veb->enabled_tc, false,
13272 			      &veb->seid, enable_stats, NULL);
13273 
13274 	/* get a VEB from the hardware */
13275 	if (ret) {
13276 		dev_info(&pf->pdev->dev,
13277 			 "couldn't add VEB, err %s aq_err %s\n",
13278 			 i40e_stat_str(&pf->hw, ret),
13279 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13280 		return -EPERM;
13281 	}
13282 
13283 	/* get statistics counter */
13284 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
13285 					 &veb->stats_idx, NULL, NULL, NULL);
13286 	if (ret) {
13287 		dev_info(&pf->pdev->dev,
13288 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
13289 			 i40e_stat_str(&pf->hw, ret),
13290 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13291 		return -EPERM;
13292 	}
13293 	ret = i40e_veb_get_bw_info(veb);
13294 	if (ret) {
13295 		dev_info(&pf->pdev->dev,
13296 			 "couldn't get VEB bw info, err %s aq_err %s\n",
13297 			 i40e_stat_str(&pf->hw, ret),
13298 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13299 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13300 		return -ENOENT;
13301 	}
13302 
13303 	vsi->uplink_seid = veb->seid;
13304 	vsi->veb_idx = veb->idx;
13305 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13306 
13307 	return 0;
13308 }
13309 
13310 /**
13311  * i40e_veb_setup - Set up a VEB
13312  * @pf: board private structure
13313  * @flags: VEB setup flags
13314  * @uplink_seid: the switch element to link to
13315  * @vsi_seid: the initial VSI seid
13316  * @enabled_tc: Enabled TC bit-map
13317  *
13318  * This allocates the sw VEB structure and links it into the switch
13319  * It is possible and legal for this to be a duplicate of an already
13320  * existing VEB.  It is also possible for both uplink and vsi seids
13321  * to be zero, in order to create a floating VEB.
13322  *
13323  * Returns pointer to the successfully allocated VEB sw struct on
13324  * success, otherwise returns NULL on failure.
13325  **/
13326 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13327 				u16 uplink_seid, u16 vsi_seid,
13328 				u8 enabled_tc)
13329 {
13330 	struct i40e_veb *veb, *uplink_veb = NULL;
13331 	int vsi_idx, veb_idx;
13332 	int ret;
13333 
13334 	/* if one seid is 0, the other must be 0 to create a floating relay */
13335 	if ((uplink_seid == 0 || vsi_seid == 0) &&
13336 	    (uplink_seid + vsi_seid != 0)) {
13337 		dev_info(&pf->pdev->dev,
13338 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13339 			 uplink_seid, vsi_seid);
13340 		return NULL;
13341 	}
13342 
13343 	/* make sure there is such a vsi and uplink */
13344 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13345 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13346 			break;
13347 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
13348 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13349 			 vsi_seid);
13350 		return NULL;
13351 	}
13352 
13353 	if (uplink_seid && uplink_seid != pf->mac_seid) {
13354 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
13355 			if (pf->veb[veb_idx] &&
13356 			    pf->veb[veb_idx]->seid == uplink_seid) {
13357 				uplink_veb = pf->veb[veb_idx];
13358 				break;
13359 			}
13360 		}
13361 		if (!uplink_veb) {
13362 			dev_info(&pf->pdev->dev,
13363 				 "uplink seid %d not found\n", uplink_seid);
13364 			return NULL;
13365 		}
13366 	}
13367 
13368 	/* get veb sw struct */
13369 	veb_idx = i40e_veb_mem_alloc(pf);
13370 	if (veb_idx < 0)
13371 		goto err_alloc;
13372 	veb = pf->veb[veb_idx];
13373 	veb->flags = flags;
13374 	veb->uplink_seid = uplink_seid;
13375 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
13376 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
13377 
13378 	/* create the VEB in the switch */
13379 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
13380 	if (ret)
13381 		goto err_veb;
13382 	if (vsi_idx == pf->lan_vsi)
13383 		pf->lan_veb = veb->idx;
13384 
13385 	return veb;
13386 
13387 err_veb:
13388 	i40e_veb_clear(veb);
13389 err_alloc:
13390 	return NULL;
13391 }
13392 
13393 /**
13394  * i40e_setup_pf_switch_element - set PF vars based on switch type
13395  * @pf: board private structure
13396  * @ele: element we are building info from
13397  * @num_reported: total number of elements
13398  * @printconfig: should we print the contents
13399  *
13400  * helper function to assist in extracting a few useful SEID values.
13401  **/
13402 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
13403 				struct i40e_aqc_switch_config_element_resp *ele,
13404 				u16 num_reported, bool printconfig)
13405 {
13406 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
13407 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
13408 	u8 element_type = ele->element_type;
13409 	u16 seid = le16_to_cpu(ele->seid);
13410 
13411 	if (printconfig)
13412 		dev_info(&pf->pdev->dev,
13413 			 "type=%d seid=%d uplink=%d downlink=%d\n",
13414 			 element_type, seid, uplink_seid, downlink_seid);
13415 
13416 	switch (element_type) {
13417 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
13418 		pf->mac_seid = seid;
13419 		break;
13420 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
13421 		/* Main VEB? */
13422 		if (uplink_seid != pf->mac_seid)
13423 			break;
13424 		if (pf->lan_veb == I40E_NO_VEB) {
13425 			int v;
13426 
13427 			/* find existing or else empty VEB */
13428 			for (v = 0; v < I40E_MAX_VEB; v++) {
13429 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
13430 					pf->lan_veb = v;
13431 					break;
13432 				}
13433 			}
13434 			if (pf->lan_veb == I40E_NO_VEB) {
13435 				v = i40e_veb_mem_alloc(pf);
13436 				if (v < 0)
13437 					break;
13438 				pf->lan_veb = v;
13439 			}
13440 		}
13441 
13442 		pf->veb[pf->lan_veb]->seid = seid;
13443 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
13444 		pf->veb[pf->lan_veb]->pf = pf;
13445 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
13446 		break;
13447 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
13448 		if (num_reported != 1)
13449 			break;
13450 		/* This is immediately after a reset so we can assume this is
13451 		 * the PF's VSI
13452 		 */
13453 		pf->mac_seid = uplink_seid;
13454 		pf->pf_seid = downlink_seid;
13455 		pf->main_vsi_seid = seid;
13456 		if (printconfig)
13457 			dev_info(&pf->pdev->dev,
13458 				 "pf_seid=%d main_vsi_seid=%d\n",
13459 				 pf->pf_seid, pf->main_vsi_seid);
13460 		break;
13461 	case I40E_SWITCH_ELEMENT_TYPE_PF:
13462 	case I40E_SWITCH_ELEMENT_TYPE_VF:
13463 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
13464 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
13465 	case I40E_SWITCH_ELEMENT_TYPE_PE:
13466 	case I40E_SWITCH_ELEMENT_TYPE_PA:
13467 		/* ignore these for now */
13468 		break;
13469 	default:
13470 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
13471 			 element_type, seid);
13472 		break;
13473 	}
13474 }
13475 
13476 /**
13477  * i40e_fetch_switch_configuration - Get switch config from firmware
13478  * @pf: board private structure
13479  * @printconfig: should we print the contents
13480  *
13481  * Get the current switch configuration from the device and
13482  * extract a few useful SEID values.
13483  **/
13484 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
13485 {
13486 	struct i40e_aqc_get_switch_config_resp *sw_config;
13487 	u16 next_seid = 0;
13488 	int ret = 0;
13489 	u8 *aq_buf;
13490 	int i;
13491 
13492 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13493 	if (!aq_buf)
13494 		return -ENOMEM;
13495 
13496 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13497 	do {
13498 		u16 num_reported, num_total;
13499 
13500 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13501 						I40E_AQ_LARGE_BUF,
13502 						&next_seid, NULL);
13503 		if (ret) {
13504 			dev_info(&pf->pdev->dev,
13505 				 "get switch config failed err %s aq_err %s\n",
13506 				 i40e_stat_str(&pf->hw, ret),
13507 				 i40e_aq_str(&pf->hw,
13508 					     pf->hw.aq.asq_last_status));
13509 			kfree(aq_buf);
13510 			return -ENOENT;
13511 		}
13512 
13513 		num_reported = le16_to_cpu(sw_config->header.num_reported);
13514 		num_total = le16_to_cpu(sw_config->header.num_total);
13515 
13516 		if (printconfig)
13517 			dev_info(&pf->pdev->dev,
13518 				 "header: %d reported %d total\n",
13519 				 num_reported, num_total);
13520 
13521 		for (i = 0; i < num_reported; i++) {
13522 			struct i40e_aqc_switch_config_element_resp *ele =
13523 				&sw_config->element[i];
13524 
13525 			i40e_setup_pf_switch_element(pf, ele, num_reported,
13526 						     printconfig);
13527 		}
13528 	} while (next_seid != 0);
13529 
13530 	kfree(aq_buf);
13531 	return ret;
13532 }
13533 
13534 /**
13535  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13536  * @pf: board private structure
13537  * @reinit: if the Main VSI needs to re-initialized.
13538  *
13539  * Returns 0 on success, negative value on failure
13540  **/
13541 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13542 {
13543 	u16 flags = 0;
13544 	int ret;
13545 
13546 	/* find out what's out there already */
13547 	ret = i40e_fetch_switch_configuration(pf, false);
13548 	if (ret) {
13549 		dev_info(&pf->pdev->dev,
13550 			 "couldn't fetch switch config, err %s aq_err %s\n",
13551 			 i40e_stat_str(&pf->hw, ret),
13552 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13553 		return ret;
13554 	}
13555 	i40e_pf_reset_stats(pf);
13556 
13557 	/* set the switch config bit for the whole device to
13558 	 * support limited promisc or true promisc
13559 	 * when user requests promisc. The default is limited
13560 	 * promisc.
13561 	*/
13562 
13563 	if ((pf->hw.pf_id == 0) &&
13564 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13565 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13566 		pf->last_sw_conf_flags = flags;
13567 	}
13568 
13569 	if (pf->hw.pf_id == 0) {
13570 		u16 valid_flags;
13571 
13572 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13573 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13574 						NULL);
13575 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13576 			dev_info(&pf->pdev->dev,
13577 				 "couldn't set switch config bits, err %s aq_err %s\n",
13578 				 i40e_stat_str(&pf->hw, ret),
13579 				 i40e_aq_str(&pf->hw,
13580 					     pf->hw.aq.asq_last_status));
13581 			/* not a fatal problem, just keep going */
13582 		}
13583 		pf->last_sw_conf_valid_flags = valid_flags;
13584 	}
13585 
13586 	/* first time setup */
13587 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13588 		struct i40e_vsi *vsi = NULL;
13589 		u16 uplink_seid;
13590 
13591 		/* Set up the PF VSI associated with the PF's main VSI
13592 		 * that is already in the HW switch
13593 		 */
13594 		if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13595 			uplink_seid = pf->veb[pf->lan_veb]->seid;
13596 		else
13597 			uplink_seid = pf->mac_seid;
13598 		if (pf->lan_vsi == I40E_NO_VSI)
13599 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13600 		else if (reinit)
13601 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13602 		if (!vsi) {
13603 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13604 			i40e_cloud_filter_exit(pf);
13605 			i40e_fdir_teardown(pf);
13606 			return -EAGAIN;
13607 		}
13608 	} else {
13609 		/* force a reset of TC and queue layout configurations */
13610 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13611 
13612 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13613 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13614 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13615 	}
13616 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13617 
13618 	i40e_fdir_sb_setup(pf);
13619 
13620 	/* Setup static PF queue filter control settings */
13621 	ret = i40e_setup_pf_filter_control(pf);
13622 	if (ret) {
13623 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13624 			 ret);
13625 		/* Failure here should not stop continuing other steps */
13626 	}
13627 
13628 	/* enable RSS in the HW, even for only one queue, as the stack can use
13629 	 * the hash
13630 	 */
13631 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13632 		i40e_pf_config_rss(pf);
13633 
13634 	/* fill in link information and enable LSE reporting */
13635 	i40e_link_event(pf);
13636 
13637 	/* Initialize user-specific link properties */
13638 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13639 				  I40E_AQ_AN_COMPLETED) ? true : false);
13640 
13641 	i40e_ptp_init(pf);
13642 
13643 	/* repopulate tunnel port filters */
13644 	i40e_sync_udp_filters(pf);
13645 
13646 	return ret;
13647 }
13648 
13649 /**
13650  * i40e_determine_queue_usage - Work out queue distribution
13651  * @pf: board private structure
13652  **/
13653 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13654 {
13655 	int queues_left;
13656 	int q_max;
13657 
13658 	pf->num_lan_qps = 0;
13659 
13660 	/* Find the max queues to be put into basic use.  We'll always be
13661 	 * using TC0, whether or not DCB is running, and TC0 will get the
13662 	 * big RSS set.
13663 	 */
13664 	queues_left = pf->hw.func_caps.num_tx_qp;
13665 
13666 	if ((queues_left == 1) ||
13667 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13668 		/* one qp for PF, no queues for anything else */
13669 		queues_left = 0;
13670 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13671 
13672 		/* make sure all the fancies are disabled */
13673 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13674 			       I40E_FLAG_IWARP_ENABLED	|
13675 			       I40E_FLAG_FD_SB_ENABLED	|
13676 			       I40E_FLAG_FD_ATR_ENABLED	|
13677 			       I40E_FLAG_DCB_CAPABLE	|
13678 			       I40E_FLAG_DCB_ENABLED	|
13679 			       I40E_FLAG_SRIOV_ENABLED	|
13680 			       I40E_FLAG_VMDQ_ENABLED);
13681 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13682 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13683 				  I40E_FLAG_FD_SB_ENABLED |
13684 				  I40E_FLAG_FD_ATR_ENABLED |
13685 				  I40E_FLAG_DCB_CAPABLE))) {
13686 		/* one qp for PF */
13687 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13688 		queues_left -= pf->num_lan_qps;
13689 
13690 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13691 			       I40E_FLAG_IWARP_ENABLED	|
13692 			       I40E_FLAG_FD_SB_ENABLED	|
13693 			       I40E_FLAG_FD_ATR_ENABLED	|
13694 			       I40E_FLAG_DCB_ENABLED	|
13695 			       I40E_FLAG_VMDQ_ENABLED);
13696 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13697 	} else {
13698 		/* Not enough queues for all TCs */
13699 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13700 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13701 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13702 					I40E_FLAG_DCB_ENABLED);
13703 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13704 		}
13705 
13706 		/* limit lan qps to the smaller of qps, cpus or msix */
13707 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13708 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13709 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13710 		pf->num_lan_qps = q_max;
13711 
13712 		queues_left -= pf->num_lan_qps;
13713 	}
13714 
13715 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13716 		if (queues_left > 1) {
13717 			queues_left -= 1; /* save 1 queue for FD */
13718 		} else {
13719 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13720 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13721 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13722 		}
13723 	}
13724 
13725 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13726 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13727 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13728 					(queues_left / pf->num_vf_qps));
13729 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13730 	}
13731 
13732 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13733 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13734 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13735 					  (queues_left / pf->num_vmdq_qps));
13736 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13737 	}
13738 
13739 	pf->queues_left = queues_left;
13740 	dev_dbg(&pf->pdev->dev,
13741 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13742 		pf->hw.func_caps.num_tx_qp,
13743 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13744 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13745 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13746 		queues_left);
13747 }
13748 
13749 /**
13750  * i40e_setup_pf_filter_control - Setup PF static filter control
13751  * @pf: PF to be setup
13752  *
13753  * i40e_setup_pf_filter_control sets up a PF's initial filter control
13754  * settings. If PE/FCoE are enabled then it will also set the per PF
13755  * based filter sizes required for them. It also enables Flow director,
13756  * ethertype and macvlan type filter settings for the pf.
13757  *
13758  * Returns 0 on success, negative on failure
13759  **/
13760 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13761 {
13762 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
13763 
13764 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13765 
13766 	/* Flow Director is enabled */
13767 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13768 		settings->enable_fdir = true;
13769 
13770 	/* Ethtype and MACVLAN filters enabled for PF */
13771 	settings->enable_ethtype = true;
13772 	settings->enable_macvlan = true;
13773 
13774 	if (i40e_set_filter_control(&pf->hw, settings))
13775 		return -ENOENT;
13776 
13777 	return 0;
13778 }
13779 
13780 #define INFO_STRING_LEN 255
13781 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13782 static void i40e_print_features(struct i40e_pf *pf)
13783 {
13784 	struct i40e_hw *hw = &pf->hw;
13785 	char *buf;
13786 	int i;
13787 
13788 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13789 	if (!buf)
13790 		return;
13791 
13792 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13793 #ifdef CONFIG_PCI_IOV
13794 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13795 #endif
13796 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13797 		      pf->hw.func_caps.num_vsis,
13798 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
13799 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
13800 		i += snprintf(&buf[i], REMAIN(i), " RSS");
13801 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13802 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13803 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13804 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13805 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13806 	}
13807 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13808 		i += snprintf(&buf[i], REMAIN(i), " DCB");
13809 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13810 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
13811 	if (pf->flags & I40E_FLAG_PTP)
13812 		i += snprintf(&buf[i], REMAIN(i), " PTP");
13813 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13814 		i += snprintf(&buf[i], REMAIN(i), " VEB");
13815 	else
13816 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
13817 
13818 	dev_info(&pf->pdev->dev, "%s\n", buf);
13819 	kfree(buf);
13820 	WARN_ON(i > INFO_STRING_LEN);
13821 }
13822 
13823 /**
13824  * i40e_get_platform_mac_addr - get platform-specific MAC address
13825  * @pdev: PCI device information struct
13826  * @pf: board private structure
13827  *
13828  * Look up the MAC address for the device. First we'll try
13829  * eth_platform_get_mac_address, which will check Open Firmware, or arch
13830  * specific fallback. Otherwise, we'll default to the stored value in
13831  * firmware.
13832  **/
13833 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13834 {
13835 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13836 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13837 }
13838 
13839 /**
13840  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
13841  * @fec_cfg: FEC option to set in flags
13842  * @flags: ptr to flags in which we set FEC option
13843  **/
13844 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
13845 {
13846 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
13847 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
13848 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
13849 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
13850 		*flags |= I40E_FLAG_RS_FEC;
13851 		*flags &= ~I40E_FLAG_BASE_R_FEC;
13852 	}
13853 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
13854 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
13855 		*flags |= I40E_FLAG_BASE_R_FEC;
13856 		*flags &= ~I40E_FLAG_RS_FEC;
13857 	}
13858 	if (fec_cfg == 0)
13859 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
13860 }
13861 
13862 /**
13863  * i40e_probe - Device initialization routine
13864  * @pdev: PCI device information struct
13865  * @ent: entry in i40e_pci_tbl
13866  *
13867  * i40e_probe initializes a PF identified by a pci_dev structure.
13868  * The OS initialization, configuring of the PF private structure,
13869  * and a hardware reset occur.
13870  *
13871  * Returns 0 on success, negative on failure
13872  **/
13873 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13874 {
13875 	struct i40e_aq_get_phy_abilities_resp abilities;
13876 	struct i40e_pf *pf;
13877 	struct i40e_hw *hw;
13878 	static u16 pfs_found;
13879 	u16 wol_nvm_bits;
13880 	u16 link_status;
13881 	int err;
13882 	u32 val;
13883 	u32 i;
13884 	u8 set_fc_aq_fail;
13885 
13886 	err = pci_enable_device_mem(pdev);
13887 	if (err)
13888 		return err;
13889 
13890 	/* set up for high or low dma */
13891 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13892 	if (err) {
13893 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13894 		if (err) {
13895 			dev_err(&pdev->dev,
13896 				"DMA configuration failed: 0x%x\n", err);
13897 			goto err_dma;
13898 		}
13899 	}
13900 
13901 	/* set up pci connections */
13902 	err = pci_request_mem_regions(pdev, i40e_driver_name);
13903 	if (err) {
13904 		dev_info(&pdev->dev,
13905 			 "pci_request_selected_regions failed %d\n", err);
13906 		goto err_pci_reg;
13907 	}
13908 
13909 	pci_enable_pcie_error_reporting(pdev);
13910 	pci_set_master(pdev);
13911 
13912 	/* Now that we have a PCI connection, we need to do the
13913 	 * low level device setup.  This is primarily setting up
13914 	 * the Admin Queue structures and then querying for the
13915 	 * device's current profile information.
13916 	 */
13917 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13918 	if (!pf) {
13919 		err = -ENOMEM;
13920 		goto err_pf_alloc;
13921 	}
13922 	pf->next_vsi = 0;
13923 	pf->pdev = pdev;
13924 	set_bit(__I40E_DOWN, pf->state);
13925 
13926 	hw = &pf->hw;
13927 	hw->back = pf;
13928 
13929 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13930 				I40E_MAX_CSR_SPACE);
13931 
13932 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
13933 	if (!hw->hw_addr) {
13934 		err = -EIO;
13935 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13936 			 (unsigned int)pci_resource_start(pdev, 0),
13937 			 pf->ioremap_len, err);
13938 		goto err_ioremap;
13939 	}
13940 	hw->vendor_id = pdev->vendor;
13941 	hw->device_id = pdev->device;
13942 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13943 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
13944 	hw->subsystem_device_id = pdev->subsystem_device;
13945 	hw->bus.device = PCI_SLOT(pdev->devfn);
13946 	hw->bus.func = PCI_FUNC(pdev->devfn);
13947 	hw->bus.bus_id = pdev->bus->number;
13948 	pf->instance = pfs_found;
13949 
13950 	/* Select something other than the 802.1ad ethertype for the
13951 	 * switch to use internally and drop on ingress.
13952 	 */
13953 	hw->switch_tag = 0xffff;
13954 	hw->first_tag = ETH_P_8021AD;
13955 	hw->second_tag = ETH_P_8021Q;
13956 
13957 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13958 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13959 
13960 	/* set up the locks for the AQ, do this only once in probe
13961 	 * and destroy them only once in remove
13962 	 */
13963 	mutex_init(&hw->aq.asq_mutex);
13964 	mutex_init(&hw->aq.arq_mutex);
13965 
13966 	pf->msg_enable = netif_msg_init(debug,
13967 					NETIF_MSG_DRV |
13968 					NETIF_MSG_PROBE |
13969 					NETIF_MSG_LINK);
13970 	if (debug < -1)
13971 		pf->hw.debug_mask = debug;
13972 
13973 	/* do a special CORER for clearing PXE mode once at init */
13974 	if (hw->revision_id == 0 &&
13975 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13976 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13977 		i40e_flush(hw);
13978 		msleep(200);
13979 		pf->corer_count++;
13980 
13981 		i40e_clear_pxe_mode(hw);
13982 	}
13983 
13984 	/* Reset here to make sure all is clean and to define PF 'n' */
13985 	i40e_clear_hw(hw);
13986 	err = i40e_pf_reset(hw);
13987 	if (err) {
13988 		dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13989 		goto err_pf_reset;
13990 	}
13991 	pf->pfr_count++;
13992 
13993 	hw->aq.num_arq_entries = I40E_AQ_LEN;
13994 	hw->aq.num_asq_entries = I40E_AQ_LEN;
13995 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13996 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13997 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
13998 
13999 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14000 		 "%s-%s:misc",
14001 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14002 
14003 	err = i40e_init_shared_code(hw);
14004 	if (err) {
14005 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14006 			 err);
14007 		goto err_pf_reset;
14008 	}
14009 
14010 	/* set up a default setting for link flow control */
14011 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14012 
14013 	err = i40e_init_adminq(hw);
14014 	if (err) {
14015 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14016 			dev_info(&pdev->dev,
14017 				 "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
14018 		else
14019 			dev_info(&pdev->dev,
14020 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14021 
14022 		goto err_pf_reset;
14023 	}
14024 	i40e_get_oem_version(hw);
14025 
14026 	/* provide nvm, fw, api versions */
14027 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
14028 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
14029 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
14030 		 i40e_nvm_version_str(hw));
14031 
14032 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
14033 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
14034 		dev_info(&pdev->dev,
14035 			 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
14036 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
14037 		dev_info(&pdev->dev,
14038 			 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
14039 
14040 	i40e_verify_eeprom(pf);
14041 
14042 	/* Rev 0 hardware was never productized */
14043 	if (hw->revision_id < 1)
14044 		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");
14045 
14046 	i40e_clear_pxe_mode(hw);
14047 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
14048 	if (err)
14049 		goto err_adminq_setup;
14050 
14051 	err = i40e_sw_init(pf);
14052 	if (err) {
14053 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
14054 		goto err_sw_init;
14055 	}
14056 
14057 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
14058 				hw->func_caps.num_rx_qp, 0, 0);
14059 	if (err) {
14060 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
14061 		goto err_init_lan_hmc;
14062 	}
14063 
14064 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
14065 	if (err) {
14066 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
14067 		err = -ENOENT;
14068 		goto err_configure_lan_hmc;
14069 	}
14070 
14071 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
14072 	 * Ignore error return codes because if it was already disabled via
14073 	 * hardware settings this will fail
14074 	 */
14075 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
14076 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
14077 		i40e_aq_stop_lldp(hw, true, NULL);
14078 	}
14079 
14080 	/* allow a platform config to override the HW addr */
14081 	i40e_get_platform_mac_addr(pdev, pf);
14082 
14083 	if (!is_valid_ether_addr(hw->mac.addr)) {
14084 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
14085 		err = -EIO;
14086 		goto err_mac_addr;
14087 	}
14088 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
14089 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
14090 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
14091 	if (is_valid_ether_addr(hw->mac.port_addr))
14092 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
14093 
14094 	pci_set_drvdata(pdev, pf);
14095 	pci_save_state(pdev);
14096 
14097 	/* Enable FW to write default DCB config on link-up */
14098 	i40e_aq_set_dcb_parameters(hw, true, NULL);
14099 
14100 #ifdef CONFIG_I40E_DCB
14101 	err = i40e_init_pf_dcb(pf);
14102 	if (err) {
14103 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
14104 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
14105 		/* Continue without DCB enabled */
14106 	}
14107 #endif /* CONFIG_I40E_DCB */
14108 
14109 	/* set up periodic task facility */
14110 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14111 	pf->service_timer_period = HZ;
14112 
14113 	INIT_WORK(&pf->service_task, i40e_service_task);
14114 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14115 
14116 	/* NVM bit on means WoL disabled for the port */
14117 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
14118 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
14119 		pf->wol_en = false;
14120 	else
14121 		pf->wol_en = true;
14122 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
14123 
14124 	/* set up the main switch operations */
14125 	i40e_determine_queue_usage(pf);
14126 	err = i40e_init_interrupt_scheme(pf);
14127 	if (err)
14128 		goto err_switch_setup;
14129 
14130 	/* The number of VSIs reported by the FW is the minimum guaranteed
14131 	 * to us; HW supports far more and we share the remaining pool with
14132 	 * the other PFs. We allocate space for more than the guarantee with
14133 	 * the understanding that we might not get them all later.
14134 	 */
14135 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14136 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14137 	else
14138 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14139 
14140 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
14141 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14142 			  GFP_KERNEL);
14143 	if (!pf->vsi) {
14144 		err = -ENOMEM;
14145 		goto err_switch_setup;
14146 	}
14147 
14148 #ifdef CONFIG_PCI_IOV
14149 	/* prep for VF support */
14150 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14151 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
14152 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
14153 		if (pci_num_vf(pdev))
14154 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
14155 	}
14156 #endif
14157 	err = i40e_setup_pf_switch(pf, false);
14158 	if (err) {
14159 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
14160 		goto err_vsis;
14161 	}
14162 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
14163 
14164 	/* Make sure flow control is set according to current settings */
14165 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
14166 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
14167 		dev_dbg(&pf->pdev->dev,
14168 			"Set fc with err %s aq_err %s on get_phy_cap\n",
14169 			i40e_stat_str(hw, err),
14170 			i40e_aq_str(hw, hw->aq.asq_last_status));
14171 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
14172 		dev_dbg(&pf->pdev->dev,
14173 			"Set fc with err %s aq_err %s on set_phy_config\n",
14174 			i40e_stat_str(hw, err),
14175 			i40e_aq_str(hw, hw->aq.asq_last_status));
14176 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
14177 		dev_dbg(&pf->pdev->dev,
14178 			"Set fc with err %s aq_err %s on get_link_info\n",
14179 			i40e_stat_str(hw, err),
14180 			i40e_aq_str(hw, hw->aq.asq_last_status));
14181 
14182 	/* if FDIR VSI was set up, start it now */
14183 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14184 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
14185 			i40e_vsi_open(pf->vsi[i]);
14186 			break;
14187 		}
14188 	}
14189 
14190 	/* The driver only wants link up/down and module qualification
14191 	 * reports from firmware.  Note the negative logic.
14192 	 */
14193 	err = i40e_aq_set_phy_int_mask(&pf->hw,
14194 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
14195 					 I40E_AQ_EVENT_MEDIA_NA |
14196 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
14197 	if (err)
14198 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
14199 			 i40e_stat_str(&pf->hw, err),
14200 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14201 
14202 	/* Reconfigure hardware for allowing smaller MSS in the case
14203 	 * of TSO, so that we avoid the MDD being fired and causing
14204 	 * a reset in the case of small MSS+TSO.
14205 	 */
14206 	val = rd32(hw, I40E_REG_MSS);
14207 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
14208 		val &= ~I40E_REG_MSS_MIN_MASK;
14209 		val |= I40E_64BYTE_MSS;
14210 		wr32(hw, I40E_REG_MSS, val);
14211 	}
14212 
14213 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
14214 		msleep(75);
14215 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
14216 		if (err)
14217 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
14218 				 i40e_stat_str(&pf->hw, err),
14219 				 i40e_aq_str(&pf->hw,
14220 					     pf->hw.aq.asq_last_status));
14221 	}
14222 	/* The main driver is (mostly) up and happy. We need to set this state
14223 	 * before setting up the misc vector or we get a race and the vector
14224 	 * ends up disabled forever.
14225 	 */
14226 	clear_bit(__I40E_DOWN, pf->state);
14227 
14228 	/* In case of MSIX we are going to setup the misc vector right here
14229 	 * to handle admin queue events etc. In case of legacy and MSI
14230 	 * the misc functionality and queue processing is combined in
14231 	 * the same vector and that gets setup at open.
14232 	 */
14233 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
14234 		err = i40e_setup_misc_vector(pf);
14235 		if (err) {
14236 			dev_info(&pdev->dev,
14237 				 "setup of misc vector failed: %d\n", err);
14238 			goto err_vsis;
14239 		}
14240 	}
14241 
14242 #ifdef CONFIG_PCI_IOV
14243 	/* prep for VF support */
14244 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14245 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
14246 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
14247 		/* disable link interrupts for VFs */
14248 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
14249 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
14250 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
14251 		i40e_flush(hw);
14252 
14253 		if (pci_num_vf(pdev)) {
14254 			dev_info(&pdev->dev,
14255 				 "Active VFs found, allocating resources.\n");
14256 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
14257 			if (err)
14258 				dev_info(&pdev->dev,
14259 					 "Error %d allocating resources for existing VFs\n",
14260 					 err);
14261 		}
14262 	}
14263 #endif /* CONFIG_PCI_IOV */
14264 
14265 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14266 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
14267 						      pf->num_iwarp_msix,
14268 						      I40E_IWARP_IRQ_PILE_ID);
14269 		if (pf->iwarp_base_vector < 0) {
14270 			dev_info(&pdev->dev,
14271 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
14272 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
14273 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
14274 		}
14275 	}
14276 
14277 	i40e_dbg_pf_init(pf);
14278 
14279 	/* tell the firmware that we're starting */
14280 	i40e_send_version(pf);
14281 
14282 	/* since everything's happy, start the service_task timer */
14283 	mod_timer(&pf->service_timer,
14284 		  round_jiffies(jiffies + pf->service_timer_period));
14285 
14286 	/* add this PF to client device list and launch a client service task */
14287 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14288 		err = i40e_lan_add_device(pf);
14289 		if (err)
14290 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
14291 				 err);
14292 	}
14293 
14294 #define PCI_SPEED_SIZE 8
14295 #define PCI_WIDTH_SIZE 8
14296 	/* Devices on the IOSF bus do not have this information
14297 	 * and will report PCI Gen 1 x 1 by default so don't bother
14298 	 * checking them.
14299 	 */
14300 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
14301 		char speed[PCI_SPEED_SIZE] = "Unknown";
14302 		char width[PCI_WIDTH_SIZE] = "Unknown";
14303 
14304 		/* Get the negotiated link width and speed from PCI config
14305 		 * space
14306 		 */
14307 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
14308 					  &link_status);
14309 
14310 		i40e_set_pci_config_data(hw, link_status);
14311 
14312 		switch (hw->bus.speed) {
14313 		case i40e_bus_speed_8000:
14314 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
14315 		case i40e_bus_speed_5000:
14316 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
14317 		case i40e_bus_speed_2500:
14318 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
14319 		default:
14320 			break;
14321 		}
14322 		switch (hw->bus.width) {
14323 		case i40e_bus_width_pcie_x8:
14324 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
14325 		case i40e_bus_width_pcie_x4:
14326 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
14327 		case i40e_bus_width_pcie_x2:
14328 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
14329 		case i40e_bus_width_pcie_x1:
14330 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
14331 		default:
14332 			break;
14333 		}
14334 
14335 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
14336 			 speed, width);
14337 
14338 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
14339 		    hw->bus.speed < i40e_bus_speed_8000) {
14340 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
14341 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
14342 		}
14343 	}
14344 
14345 	/* get the requested speeds from the fw */
14346 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
14347 	if (err)
14348 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
14349 			i40e_stat_str(&pf->hw, err),
14350 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14351 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
14352 
14353 	/* set the FEC config due to the board capabilities */
14354 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
14355 
14356 	/* get the supported phy types from the fw */
14357 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
14358 	if (err)
14359 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
14360 			i40e_stat_str(&pf->hw, err),
14361 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14362 
14363 	/* Add a filter to drop all Flow control frames from any VSI from being
14364 	 * transmitted. By doing so we stop a malicious VF from sending out
14365 	 * PAUSE or PFC frames and potentially controlling traffic for other
14366 	 * PF/VF VSIs.
14367 	 * The FW can still send Flow control frames if enabled.
14368 	 */
14369 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
14370 						       pf->main_vsi_seid);
14371 
14372 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
14373 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
14374 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
14375 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
14376 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
14377 	/* print a string summarizing features */
14378 	i40e_print_features(pf);
14379 
14380 	return 0;
14381 
14382 	/* Unwind what we've done if something failed in the setup */
14383 err_vsis:
14384 	set_bit(__I40E_DOWN, pf->state);
14385 	i40e_clear_interrupt_scheme(pf);
14386 	kfree(pf->vsi);
14387 err_switch_setup:
14388 	i40e_reset_interrupt_capability(pf);
14389 	del_timer_sync(&pf->service_timer);
14390 err_mac_addr:
14391 err_configure_lan_hmc:
14392 	(void)i40e_shutdown_lan_hmc(hw);
14393 err_init_lan_hmc:
14394 	kfree(pf->qp_pile);
14395 err_sw_init:
14396 err_adminq_setup:
14397 err_pf_reset:
14398 	iounmap(hw->hw_addr);
14399 err_ioremap:
14400 	kfree(pf);
14401 err_pf_alloc:
14402 	pci_disable_pcie_error_reporting(pdev);
14403 	pci_release_mem_regions(pdev);
14404 err_pci_reg:
14405 err_dma:
14406 	pci_disable_device(pdev);
14407 	return err;
14408 }
14409 
14410 /**
14411  * i40e_remove - Device removal routine
14412  * @pdev: PCI device information struct
14413  *
14414  * i40e_remove is called by the PCI subsystem to alert the driver
14415  * that is should release a PCI device.  This could be caused by a
14416  * Hot-Plug event, or because the driver is going to be removed from
14417  * memory.
14418  **/
14419 static void i40e_remove(struct pci_dev *pdev)
14420 {
14421 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14422 	struct i40e_hw *hw = &pf->hw;
14423 	i40e_status ret_code;
14424 	int i;
14425 
14426 	i40e_dbg_pf_exit(pf);
14427 
14428 	i40e_ptp_stop(pf);
14429 
14430 	/* Disable RSS in hw */
14431 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
14432 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
14433 
14434 	/* no more scheduling of any task */
14435 	set_bit(__I40E_SUSPENDED, pf->state);
14436 	set_bit(__I40E_DOWN, pf->state);
14437 	if (pf->service_timer.function)
14438 		del_timer_sync(&pf->service_timer);
14439 	if (pf->service_task.func)
14440 		cancel_work_sync(&pf->service_task);
14441 
14442 	/* Client close must be called explicitly here because the timer
14443 	 * has been stopped.
14444 	 */
14445 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14446 
14447 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
14448 		i40e_free_vfs(pf);
14449 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
14450 	}
14451 
14452 	i40e_fdir_teardown(pf);
14453 
14454 	/* If there is a switch structure or any orphans, remove them.
14455 	 * This will leave only the PF's VSI remaining.
14456 	 */
14457 	for (i = 0; i < I40E_MAX_VEB; i++) {
14458 		if (!pf->veb[i])
14459 			continue;
14460 
14461 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
14462 		    pf->veb[i]->uplink_seid == 0)
14463 			i40e_switch_branch_release(pf->veb[i]);
14464 	}
14465 
14466 	/* Now we can shutdown the PF's VSI, just before we kill
14467 	 * adminq and hmc.
14468 	 */
14469 	if (pf->vsi[pf->lan_vsi])
14470 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
14471 
14472 	i40e_cloud_filter_exit(pf);
14473 
14474 	/* remove attached clients */
14475 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14476 		ret_code = i40e_lan_del_device(pf);
14477 		if (ret_code)
14478 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
14479 				 ret_code);
14480 	}
14481 
14482 	/* shutdown and destroy the HMC */
14483 	if (hw->hmc.hmc_obj) {
14484 		ret_code = i40e_shutdown_lan_hmc(hw);
14485 		if (ret_code)
14486 			dev_warn(&pdev->dev,
14487 				 "Failed to destroy the HMC resources: %d\n",
14488 				 ret_code);
14489 	}
14490 
14491 	/* shutdown the adminq */
14492 	i40e_shutdown_adminq(hw);
14493 
14494 	/* destroy the locks only once, here */
14495 	mutex_destroy(&hw->aq.arq_mutex);
14496 	mutex_destroy(&hw->aq.asq_mutex);
14497 
14498 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
14499 	rtnl_lock();
14500 	i40e_clear_interrupt_scheme(pf);
14501 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14502 		if (pf->vsi[i]) {
14503 			i40e_vsi_clear_rings(pf->vsi[i]);
14504 			i40e_vsi_clear(pf->vsi[i]);
14505 			pf->vsi[i] = NULL;
14506 		}
14507 	}
14508 	rtnl_unlock();
14509 
14510 	for (i = 0; i < I40E_MAX_VEB; i++) {
14511 		kfree(pf->veb[i]);
14512 		pf->veb[i] = NULL;
14513 	}
14514 
14515 	kfree(pf->qp_pile);
14516 	kfree(pf->vsi);
14517 
14518 	iounmap(hw->hw_addr);
14519 	kfree(pf);
14520 	pci_release_mem_regions(pdev);
14521 
14522 	pci_disable_pcie_error_reporting(pdev);
14523 	pci_disable_device(pdev);
14524 }
14525 
14526 /**
14527  * i40e_pci_error_detected - warning that something funky happened in PCI land
14528  * @pdev: PCI device information struct
14529  * @error: the type of PCI error
14530  *
14531  * Called to warn that something happened and the error handling steps
14532  * are in progress.  Allows the driver to quiesce things, be ready for
14533  * remediation.
14534  **/
14535 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14536 						enum pci_channel_state error)
14537 {
14538 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14539 
14540 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14541 
14542 	if (!pf) {
14543 		dev_info(&pdev->dev,
14544 			 "Cannot recover - error happened during device probe\n");
14545 		return PCI_ERS_RESULT_DISCONNECT;
14546 	}
14547 
14548 	/* shutdown all operations */
14549 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14550 		i40e_prep_for_reset(pf, false);
14551 
14552 	/* Request a slot reset */
14553 	return PCI_ERS_RESULT_NEED_RESET;
14554 }
14555 
14556 /**
14557  * i40e_pci_error_slot_reset - a PCI slot reset just happened
14558  * @pdev: PCI device information struct
14559  *
14560  * Called to find if the driver can work with the device now that
14561  * the pci slot has been reset.  If a basic connection seems good
14562  * (registers are readable and have sane content) then return a
14563  * happy little PCI_ERS_RESULT_xxx.
14564  **/
14565 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14566 {
14567 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14568 	pci_ers_result_t result;
14569 	u32 reg;
14570 
14571 	dev_dbg(&pdev->dev, "%s\n", __func__);
14572 	if (pci_enable_device_mem(pdev)) {
14573 		dev_info(&pdev->dev,
14574 			 "Cannot re-enable PCI device after reset.\n");
14575 		result = PCI_ERS_RESULT_DISCONNECT;
14576 	} else {
14577 		pci_set_master(pdev);
14578 		pci_restore_state(pdev);
14579 		pci_save_state(pdev);
14580 		pci_wake_from_d3(pdev, false);
14581 
14582 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14583 		if (reg == 0)
14584 			result = PCI_ERS_RESULT_RECOVERED;
14585 		else
14586 			result = PCI_ERS_RESULT_DISCONNECT;
14587 	}
14588 
14589 	return result;
14590 }
14591 
14592 /**
14593  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14594  * @pdev: PCI device information struct
14595  */
14596 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14597 {
14598 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14599 
14600 	i40e_prep_for_reset(pf, false);
14601 }
14602 
14603 /**
14604  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14605  * @pdev: PCI device information struct
14606  */
14607 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14608 {
14609 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14610 
14611 	i40e_reset_and_rebuild(pf, false, false);
14612 }
14613 
14614 /**
14615  * i40e_pci_error_resume - restart operations after PCI error recovery
14616  * @pdev: PCI device information struct
14617  *
14618  * Called to allow the driver to bring things back up after PCI error
14619  * and/or reset recovery has finished.
14620  **/
14621 static void i40e_pci_error_resume(struct pci_dev *pdev)
14622 {
14623 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14624 
14625 	dev_dbg(&pdev->dev, "%s\n", __func__);
14626 	if (test_bit(__I40E_SUSPENDED, pf->state))
14627 		return;
14628 
14629 	i40e_handle_reset_warning(pf, false);
14630 }
14631 
14632 /**
14633  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14634  * using the mac_address_write admin q function
14635  * @pf: pointer to i40e_pf struct
14636  **/
14637 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14638 {
14639 	struct i40e_hw *hw = &pf->hw;
14640 	i40e_status ret;
14641 	u8 mac_addr[6];
14642 	u16 flags = 0;
14643 
14644 	/* Get current MAC address in case it's an LAA */
14645 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14646 		ether_addr_copy(mac_addr,
14647 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14648 	} else {
14649 		dev_err(&pf->pdev->dev,
14650 			"Failed to retrieve MAC address; using default\n");
14651 		ether_addr_copy(mac_addr, hw->mac.addr);
14652 	}
14653 
14654 	/* The FW expects the mac address write cmd to first be called with
14655 	 * one of these flags before calling it again with the multicast
14656 	 * enable flags.
14657 	 */
14658 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14659 
14660 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14661 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14662 
14663 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14664 	if (ret) {
14665 		dev_err(&pf->pdev->dev,
14666 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14667 		return;
14668 	}
14669 
14670 	flags = I40E_AQC_MC_MAG_EN
14671 			| I40E_AQC_WOL_PRESERVE_ON_PFR
14672 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14673 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14674 	if (ret)
14675 		dev_err(&pf->pdev->dev,
14676 			"Failed to enable Multicast Magic Packet wake up\n");
14677 }
14678 
14679 /**
14680  * i40e_shutdown - PCI callback for shutting down
14681  * @pdev: PCI device information struct
14682  **/
14683 static void i40e_shutdown(struct pci_dev *pdev)
14684 {
14685 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14686 	struct i40e_hw *hw = &pf->hw;
14687 
14688 	set_bit(__I40E_SUSPENDED, pf->state);
14689 	set_bit(__I40E_DOWN, pf->state);
14690 
14691 	del_timer_sync(&pf->service_timer);
14692 	cancel_work_sync(&pf->service_task);
14693 	i40e_cloud_filter_exit(pf);
14694 	i40e_fdir_teardown(pf);
14695 
14696 	/* Client close must be called explicitly here because the timer
14697 	 * has been stopped.
14698 	 */
14699 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14700 
14701 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14702 		i40e_enable_mc_magic_wake(pf);
14703 
14704 	i40e_prep_for_reset(pf, false);
14705 
14706 	wr32(hw, I40E_PFPM_APM,
14707 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14708 	wr32(hw, I40E_PFPM_WUFC,
14709 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14710 
14711 	/* Since we're going to destroy queues during the
14712 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14713 	 * whole section
14714 	 */
14715 	rtnl_lock();
14716 	i40e_clear_interrupt_scheme(pf);
14717 	rtnl_unlock();
14718 
14719 	if (system_state == SYSTEM_POWER_OFF) {
14720 		pci_wake_from_d3(pdev, pf->wol_en);
14721 		pci_set_power_state(pdev, PCI_D3hot);
14722 	}
14723 }
14724 
14725 /**
14726  * i40e_suspend - PM callback for moving to D3
14727  * @dev: generic device information structure
14728  **/
14729 static int __maybe_unused i40e_suspend(struct device *dev)
14730 {
14731 	struct pci_dev *pdev = to_pci_dev(dev);
14732 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14733 	struct i40e_hw *hw = &pf->hw;
14734 
14735 	/* If we're already suspended, then there is nothing to do */
14736 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14737 		return 0;
14738 
14739 	set_bit(__I40E_DOWN, pf->state);
14740 
14741 	/* Ensure service task will not be running */
14742 	del_timer_sync(&pf->service_timer);
14743 	cancel_work_sync(&pf->service_task);
14744 
14745 	/* Client close must be called explicitly here because the timer
14746 	 * has been stopped.
14747 	 */
14748 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14749 
14750 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14751 		i40e_enable_mc_magic_wake(pf);
14752 
14753 	/* Since we're going to destroy queues during the
14754 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14755 	 * whole section
14756 	 */
14757 	rtnl_lock();
14758 
14759 	i40e_prep_for_reset(pf, true);
14760 
14761 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14762 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14763 
14764 	/* Clear the interrupt scheme and release our IRQs so that the system
14765 	 * can safely hibernate even when there are a large number of CPUs.
14766 	 * Otherwise hibernation might fail when mapping all the vectors back
14767 	 * to CPU0.
14768 	 */
14769 	i40e_clear_interrupt_scheme(pf);
14770 
14771 	rtnl_unlock();
14772 
14773 	return 0;
14774 }
14775 
14776 /**
14777  * i40e_resume - PM callback for waking up from D3
14778  * @dev: generic device information structure
14779  **/
14780 static int __maybe_unused i40e_resume(struct device *dev)
14781 {
14782 	struct pci_dev *pdev = to_pci_dev(dev);
14783 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14784 	int err;
14785 
14786 	/* If we're not suspended, then there is nothing to do */
14787 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14788 		return 0;
14789 
14790 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
14791 	 * since we're going to be restoring queues
14792 	 */
14793 	rtnl_lock();
14794 
14795 	/* We cleared the interrupt scheme when we suspended, so we need to
14796 	 * restore it now to resume device functionality.
14797 	 */
14798 	err = i40e_restore_interrupt_scheme(pf);
14799 	if (err) {
14800 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14801 			err);
14802 	}
14803 
14804 	clear_bit(__I40E_DOWN, pf->state);
14805 	i40e_reset_and_rebuild(pf, false, true);
14806 
14807 	rtnl_unlock();
14808 
14809 	/* Clear suspended state last after everything is recovered */
14810 	clear_bit(__I40E_SUSPENDED, pf->state);
14811 
14812 	/* Restart the service task */
14813 	mod_timer(&pf->service_timer,
14814 		  round_jiffies(jiffies + pf->service_timer_period));
14815 
14816 	return 0;
14817 }
14818 
14819 static const struct pci_error_handlers i40e_err_handler = {
14820 	.error_detected = i40e_pci_error_detected,
14821 	.slot_reset = i40e_pci_error_slot_reset,
14822 	.reset_prepare = i40e_pci_error_reset_prepare,
14823 	.reset_done = i40e_pci_error_reset_done,
14824 	.resume = i40e_pci_error_resume,
14825 };
14826 
14827 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14828 
14829 static struct pci_driver i40e_driver = {
14830 	.name     = i40e_driver_name,
14831 	.id_table = i40e_pci_tbl,
14832 	.probe    = i40e_probe,
14833 	.remove   = i40e_remove,
14834 	.driver   = {
14835 		.pm = &i40e_pm_ops,
14836 	},
14837 	.shutdown = i40e_shutdown,
14838 	.err_handler = &i40e_err_handler,
14839 	.sriov_configure = i40e_pci_sriov_configure,
14840 };
14841 
14842 /**
14843  * i40e_init_module - Driver registration routine
14844  *
14845  * i40e_init_module is the first routine called when the driver is
14846  * loaded. All it does is register with the PCI subsystem.
14847  **/
14848 static int __init i40e_init_module(void)
14849 {
14850 	pr_info("%s: %s - version %s\n", i40e_driver_name,
14851 		i40e_driver_string, i40e_driver_version_str);
14852 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
14853 
14854 	/* There is no need to throttle the number of active tasks because
14855 	 * each device limits its own task using a state bit for scheduling
14856 	 * the service task, and the device tasks do not interfere with each
14857 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14858 	 * since we need to be able to guarantee forward progress even under
14859 	 * memory pressure.
14860 	 */
14861 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
14862 	if (!i40e_wq) {
14863 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14864 		return -ENOMEM;
14865 	}
14866 
14867 	i40e_dbg_init();
14868 	return pci_register_driver(&i40e_driver);
14869 }
14870 module_init(i40e_init_module);
14871 
14872 /**
14873  * i40e_exit_module - Driver exit cleanup routine
14874  *
14875  * i40e_exit_module is called just before the driver is removed
14876  * from memory.
14877  **/
14878 static void __exit i40e_exit_module(void)
14879 {
14880 	pci_unregister_driver(&i40e_driver);
14881 	destroy_workqueue(i40e_wq);
14882 	i40e_dbg_exit();
14883 }
14884 module_exit(i40e_exit_module);
14885