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