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 <net/udp_tunnel.h>
13 /* All i40e tracepoints are defined by the include below, which
14  * must be included exactly once across the whole kernel with
15  * CREATE_TRACE_POINTS defined
16  */
17 #define CREATE_TRACE_POINTS
18 #include "i40e_trace.h"
19 
20 const char i40e_driver_name[] = "i40e";
21 static const char i40e_driver_string[] =
22 			"Intel(R) Ethernet Connection XL710 Network Driver";
23 
24 #define DRV_KERN "-k"
25 
26 #define DRV_VERSION_MAJOR 2
27 #define DRV_VERSION_MINOR 3
28 #define DRV_VERSION_BUILD 2
29 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
30 	     __stringify(DRV_VERSION_MINOR) "." \
31 	     __stringify(DRV_VERSION_BUILD)    DRV_KERN
32 const char i40e_driver_version_str[] = DRV_VERSION;
33 static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
34 
35 /* a bit of forward declarations */
36 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
37 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
38 static int i40e_add_vsi(struct i40e_vsi *vsi);
39 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
40 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
41 static int i40e_setup_misc_vector(struct i40e_pf *pf);
42 static void i40e_determine_queue_usage(struct i40e_pf *pf);
43 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
44 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
48 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
49 static int i40e_get_capabilities(struct i40e_pf *pf,
50 				 enum i40e_admin_queue_opc list_type);
51 
52 
53 /* i40e_pci_tbl - PCI Device ID Table
54  *
55  * Last entry must be all 0s
56  *
57  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
58  *   Class, Class Mask, private data (not used) }
59  */
60 static const struct pci_device_id i40e_pci_tbl[] = {
61 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
80 	/* required last entry */
81 	{0, }
82 };
83 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
84 
85 #define I40E_MAX_VF_COUNT 128
86 static int debug = -1;
87 module_param(debug, uint, 0);
88 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
89 
90 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
91 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_VERSION(DRV_VERSION);
94 
95 static struct workqueue_struct *i40e_wq;
96 
97 /**
98  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
99  * @hw:   pointer to the HW structure
100  * @mem:  ptr to mem struct to fill out
101  * @size: size of memory requested
102  * @alignment: what to align the allocation to
103  **/
104 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
105 			    u64 size, u32 alignment)
106 {
107 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
108 
109 	mem->size = ALIGN(size, alignment);
110 	mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
111 				      &mem->pa, GFP_KERNEL);
112 	if (!mem->va)
113 		return -ENOMEM;
114 
115 	return 0;
116 }
117 
118 /**
119  * i40e_free_dma_mem_d - OS specific memory free for shared code
120  * @hw:   pointer to the HW structure
121  * @mem:  ptr to mem struct to free
122  **/
123 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
124 {
125 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
126 
127 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
128 	mem->va = NULL;
129 	mem->pa = 0;
130 	mem->size = 0;
131 
132 	return 0;
133 }
134 
135 /**
136  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
137  * @hw:   pointer to the HW structure
138  * @mem:  ptr to mem struct to fill out
139  * @size: size of memory requested
140  **/
141 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
142 			     u32 size)
143 {
144 	mem->size = size;
145 	mem->va = kzalloc(size, GFP_KERNEL);
146 
147 	if (!mem->va)
148 		return -ENOMEM;
149 
150 	return 0;
151 }
152 
153 /**
154  * i40e_free_virt_mem_d - OS specific memory free for shared code
155  * @hw:   pointer to the HW structure
156  * @mem:  ptr to mem struct to free
157  **/
158 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
159 {
160 	/* it's ok to kfree a NULL pointer */
161 	kfree(mem->va);
162 	mem->va = NULL;
163 	mem->size = 0;
164 
165 	return 0;
166 }
167 
168 /**
169  * i40e_get_lump - find a lump of free generic resource
170  * @pf: board private structure
171  * @pile: the pile of resource to search
172  * @needed: the number of items needed
173  * @id: an owner id to stick on the items assigned
174  *
175  * Returns the base item index of the lump, or negative for error
176  *
177  * The search_hint trick and lack of advanced fit-finding only work
178  * because we're highly likely to have all the same size lump requests.
179  * Linear search time and any fragmentation should be minimal.
180  **/
181 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
182 			 u16 needed, u16 id)
183 {
184 	int ret = -ENOMEM;
185 	int i, j;
186 
187 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
188 		dev_info(&pf->pdev->dev,
189 			 "param err: pile=%s needed=%d id=0x%04x\n",
190 			 pile ? "<valid>" : "<null>", needed, id);
191 		return -EINVAL;
192 	}
193 
194 	/* start the linear search with an imperfect hint */
195 	i = pile->search_hint;
196 	while (i < pile->num_entries) {
197 		/* skip already allocated entries */
198 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
199 			i++;
200 			continue;
201 		}
202 
203 		/* do we have enough in this lump? */
204 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
205 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
206 				break;
207 		}
208 
209 		if (j == needed) {
210 			/* there was enough, so assign it to the requestor */
211 			for (j = 0; j < needed; j++)
212 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
213 			ret = i;
214 			pile->search_hint = i + j;
215 			break;
216 		}
217 
218 		/* not enough, so skip over it and continue looking */
219 		i += j;
220 	}
221 
222 	return ret;
223 }
224 
225 /**
226  * i40e_put_lump - return a lump of generic resource
227  * @pile: the pile of resource to search
228  * @index: the base item index
229  * @id: the owner id of the items assigned
230  *
231  * Returns the count of items in the lump
232  **/
233 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
234 {
235 	int valid_id = (id | I40E_PILE_VALID_BIT);
236 	int count = 0;
237 	int i;
238 
239 	if (!pile || index >= pile->num_entries)
240 		return -EINVAL;
241 
242 	for (i = index;
243 	     i < pile->num_entries && pile->list[i] == valid_id;
244 	     i++) {
245 		pile->list[i] = 0;
246 		count++;
247 	}
248 
249 	if (count && index < pile->search_hint)
250 		pile->search_hint = index;
251 
252 	return count;
253 }
254 
255 /**
256  * i40e_find_vsi_from_id - searches for the vsi with the given id
257  * @pf: the pf structure to search for the vsi
258  * @id: id of the vsi it is searching for
259  **/
260 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
261 {
262 	int i;
263 
264 	for (i = 0; i < pf->num_alloc_vsi; i++)
265 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
266 			return pf->vsi[i];
267 
268 	return NULL;
269 }
270 
271 /**
272  * i40e_service_event_schedule - Schedule the service task to wake up
273  * @pf: board private structure
274  *
275  * If not already scheduled, this puts the task into the work queue
276  **/
277 void i40e_service_event_schedule(struct i40e_pf *pf)
278 {
279 	if (!test_bit(__I40E_DOWN, pf->state) &&
280 	    !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
281 		queue_work(i40e_wq, &pf->service_task);
282 }
283 
284 /**
285  * i40e_tx_timeout - Respond to a Tx Hang
286  * @netdev: network interface device structure
287  *
288  * If any port has noticed a Tx timeout, it is likely that the whole
289  * device is munged, not just the one netdev port, so go for the full
290  * reset.
291  **/
292 static void i40e_tx_timeout(struct net_device *netdev)
293 {
294 	struct i40e_netdev_priv *np = netdev_priv(netdev);
295 	struct i40e_vsi *vsi = np->vsi;
296 	struct i40e_pf *pf = vsi->back;
297 	struct i40e_ring *tx_ring = NULL;
298 	unsigned int i, hung_queue = 0;
299 	u32 head, val;
300 
301 	pf->tx_timeout_count++;
302 
303 	/* find the stopped queue the same way the stack does */
304 	for (i = 0; i < netdev->num_tx_queues; i++) {
305 		struct netdev_queue *q;
306 		unsigned long trans_start;
307 
308 		q = netdev_get_tx_queue(netdev, i);
309 		trans_start = q->trans_start;
310 		if (netif_xmit_stopped(q) &&
311 		    time_after(jiffies,
312 			       (trans_start + netdev->watchdog_timeo))) {
313 			hung_queue = i;
314 			break;
315 		}
316 	}
317 
318 	if (i == netdev->num_tx_queues) {
319 		netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
320 	} else {
321 		/* now that we have an index, find the tx_ring struct */
322 		for (i = 0; i < vsi->num_queue_pairs; i++) {
323 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
324 				if (hung_queue ==
325 				    vsi->tx_rings[i]->queue_index) {
326 					tx_ring = vsi->tx_rings[i];
327 					break;
328 				}
329 			}
330 		}
331 	}
332 
333 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
334 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
335 	else if (time_before(jiffies,
336 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
337 		return;   /* don't do any new action before the next timeout */
338 
339 	if (tx_ring) {
340 		head = i40e_get_head(tx_ring);
341 		/* Read interrupt register */
342 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
343 			val = rd32(&pf->hw,
344 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
345 						tx_ring->vsi->base_vector - 1));
346 		else
347 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
348 
349 		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",
350 			    vsi->seid, hung_queue, tx_ring->next_to_clean,
351 			    head, tx_ring->next_to_use,
352 			    readl(tx_ring->tail), val);
353 	}
354 
355 	pf->tx_timeout_last_recovery = jiffies;
356 	netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
357 		    pf->tx_timeout_recovery_level, hung_queue);
358 
359 	switch (pf->tx_timeout_recovery_level) {
360 	case 1:
361 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
362 		break;
363 	case 2:
364 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
365 		break;
366 	case 3:
367 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
368 		break;
369 	default:
370 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
371 		break;
372 	}
373 
374 	i40e_service_event_schedule(pf);
375 	pf->tx_timeout_recovery_level++;
376 }
377 
378 /**
379  * i40e_get_vsi_stats_struct - Get System Network Statistics
380  * @vsi: the VSI we care about
381  *
382  * Returns the address of the device statistics structure.
383  * The statistics are actually updated from the service task.
384  **/
385 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
386 {
387 	return &vsi->net_stats;
388 }
389 
390 /**
391  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
392  * @ring: Tx ring to get statistics from
393  * @stats: statistics entry to be updated
394  **/
395 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
396 					    struct rtnl_link_stats64 *stats)
397 {
398 	u64 bytes, packets;
399 	unsigned int start;
400 
401 	do {
402 		start = u64_stats_fetch_begin_irq(&ring->syncp);
403 		packets = ring->stats.packets;
404 		bytes   = ring->stats.bytes;
405 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
406 
407 	stats->tx_packets += packets;
408 	stats->tx_bytes   += bytes;
409 }
410 
411 /**
412  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
413  * @netdev: network interface device structure
414  * @stats: data structure to store statistics
415  *
416  * Returns the address of the device statistics structure.
417  * The statistics are actually updated from the service task.
418  **/
419 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
420 				  struct rtnl_link_stats64 *stats)
421 {
422 	struct i40e_netdev_priv *np = netdev_priv(netdev);
423 	struct i40e_ring *tx_ring, *rx_ring;
424 	struct i40e_vsi *vsi = np->vsi;
425 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
426 	int i;
427 
428 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
429 		return;
430 
431 	if (!vsi->tx_rings)
432 		return;
433 
434 	rcu_read_lock();
435 	for (i = 0; i < vsi->num_queue_pairs; i++) {
436 		u64 bytes, packets;
437 		unsigned int start;
438 
439 		tx_ring = READ_ONCE(vsi->tx_rings[i]);
440 		if (!tx_ring)
441 			continue;
442 		i40e_get_netdev_stats_struct_tx(tx_ring, stats);
443 
444 		rx_ring = &tx_ring[1];
445 
446 		do {
447 			start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
448 			packets = rx_ring->stats.packets;
449 			bytes   = rx_ring->stats.bytes;
450 		} while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
451 
452 		stats->rx_packets += packets;
453 		stats->rx_bytes   += bytes;
454 
455 		if (i40e_enabled_xdp_vsi(vsi))
456 			i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
457 	}
458 	rcu_read_unlock();
459 
460 	/* following stats updated by i40e_watchdog_subtask() */
461 	stats->multicast	= vsi_stats->multicast;
462 	stats->tx_errors	= vsi_stats->tx_errors;
463 	stats->tx_dropped	= vsi_stats->tx_dropped;
464 	stats->rx_errors	= vsi_stats->rx_errors;
465 	stats->rx_dropped	= vsi_stats->rx_dropped;
466 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
467 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
468 }
469 
470 /**
471  * i40e_vsi_reset_stats - Resets all stats of the given vsi
472  * @vsi: the VSI to have its stats reset
473  **/
474 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
475 {
476 	struct rtnl_link_stats64 *ns;
477 	int i;
478 
479 	if (!vsi)
480 		return;
481 
482 	ns = i40e_get_vsi_stats_struct(vsi);
483 	memset(ns, 0, sizeof(*ns));
484 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
485 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
486 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
487 	if (vsi->rx_rings && vsi->rx_rings[0]) {
488 		for (i = 0; i < vsi->num_queue_pairs; i++) {
489 			memset(&vsi->rx_rings[i]->stats, 0,
490 			       sizeof(vsi->rx_rings[i]->stats));
491 			memset(&vsi->rx_rings[i]->rx_stats, 0,
492 			       sizeof(vsi->rx_rings[i]->rx_stats));
493 			memset(&vsi->tx_rings[i]->stats, 0,
494 			       sizeof(vsi->tx_rings[i]->stats));
495 			memset(&vsi->tx_rings[i]->tx_stats, 0,
496 			       sizeof(vsi->tx_rings[i]->tx_stats));
497 		}
498 	}
499 	vsi->stat_offsets_loaded = false;
500 }
501 
502 /**
503  * i40e_pf_reset_stats - Reset all of the stats for the given PF
504  * @pf: the PF to be reset
505  **/
506 void i40e_pf_reset_stats(struct i40e_pf *pf)
507 {
508 	int i;
509 
510 	memset(&pf->stats, 0, sizeof(pf->stats));
511 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
512 	pf->stat_offsets_loaded = false;
513 
514 	for (i = 0; i < I40E_MAX_VEB; i++) {
515 		if (pf->veb[i]) {
516 			memset(&pf->veb[i]->stats, 0,
517 			       sizeof(pf->veb[i]->stats));
518 			memset(&pf->veb[i]->stats_offsets, 0,
519 			       sizeof(pf->veb[i]->stats_offsets));
520 			pf->veb[i]->stat_offsets_loaded = false;
521 		}
522 	}
523 	pf->hw_csum_rx_error = 0;
524 }
525 
526 /**
527  * i40e_stat_update48 - read and update a 48 bit stat from the chip
528  * @hw: ptr to the hardware info
529  * @hireg: the high 32 bit reg to read
530  * @loreg: the low 32 bit reg to read
531  * @offset_loaded: has the initial offset been loaded yet
532  * @offset: ptr to current offset value
533  * @stat: ptr to the stat
534  *
535  * Since the device stats are not reset at PFReset, they likely will not
536  * be zeroed when the driver starts.  We'll save the first values read
537  * and use them as offsets to be subtracted from the raw values in order
538  * to report stats that count from zero.  In the process, we also manage
539  * the potential roll-over.
540  **/
541 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
542 			       bool offset_loaded, u64 *offset, u64 *stat)
543 {
544 	u64 new_data;
545 
546 	if (hw->device_id == I40E_DEV_ID_QEMU) {
547 		new_data = rd32(hw, loreg);
548 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
549 	} else {
550 		new_data = rd64(hw, loreg);
551 	}
552 	if (!offset_loaded)
553 		*offset = new_data;
554 	if (likely(new_data >= *offset))
555 		*stat = new_data - *offset;
556 	else
557 		*stat = (new_data + BIT_ULL(48)) - *offset;
558 	*stat &= 0xFFFFFFFFFFFFULL;
559 }
560 
561 /**
562  * i40e_stat_update32 - read and update a 32 bit stat from the chip
563  * @hw: ptr to the hardware info
564  * @reg: the hw reg to read
565  * @offset_loaded: has the initial offset been loaded yet
566  * @offset: ptr to current offset value
567  * @stat: ptr to the stat
568  **/
569 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
570 			       bool offset_loaded, u64 *offset, u64 *stat)
571 {
572 	u32 new_data;
573 
574 	new_data = rd32(hw, reg);
575 	if (!offset_loaded)
576 		*offset = new_data;
577 	if (likely(new_data >= *offset))
578 		*stat = (u32)(new_data - *offset);
579 	else
580 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
581 }
582 
583 /**
584  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
585  * @hw: ptr to the hardware info
586  * @reg: the hw reg to read and clear
587  * @stat: ptr to the stat
588  **/
589 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
590 {
591 	u32 new_data = rd32(hw, reg);
592 
593 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
594 	*stat += new_data;
595 }
596 
597 /**
598  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
599  * @vsi: the VSI to be updated
600  **/
601 void i40e_update_eth_stats(struct i40e_vsi *vsi)
602 {
603 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
604 	struct i40e_pf *pf = vsi->back;
605 	struct i40e_hw *hw = &pf->hw;
606 	struct i40e_eth_stats *oes;
607 	struct i40e_eth_stats *es;     /* device's eth stats */
608 
609 	es = &vsi->eth_stats;
610 	oes = &vsi->eth_stats_offsets;
611 
612 	/* Gather up the stats that the hw collects */
613 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
614 			   vsi->stat_offsets_loaded,
615 			   &oes->tx_errors, &es->tx_errors);
616 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
617 			   vsi->stat_offsets_loaded,
618 			   &oes->rx_discards, &es->rx_discards);
619 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
620 			   vsi->stat_offsets_loaded,
621 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
622 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
623 			   vsi->stat_offsets_loaded,
624 			   &oes->tx_errors, &es->tx_errors);
625 
626 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
627 			   I40E_GLV_GORCL(stat_idx),
628 			   vsi->stat_offsets_loaded,
629 			   &oes->rx_bytes, &es->rx_bytes);
630 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
631 			   I40E_GLV_UPRCL(stat_idx),
632 			   vsi->stat_offsets_loaded,
633 			   &oes->rx_unicast, &es->rx_unicast);
634 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
635 			   I40E_GLV_MPRCL(stat_idx),
636 			   vsi->stat_offsets_loaded,
637 			   &oes->rx_multicast, &es->rx_multicast);
638 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
639 			   I40E_GLV_BPRCL(stat_idx),
640 			   vsi->stat_offsets_loaded,
641 			   &oes->rx_broadcast, &es->rx_broadcast);
642 
643 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
644 			   I40E_GLV_GOTCL(stat_idx),
645 			   vsi->stat_offsets_loaded,
646 			   &oes->tx_bytes, &es->tx_bytes);
647 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
648 			   I40E_GLV_UPTCL(stat_idx),
649 			   vsi->stat_offsets_loaded,
650 			   &oes->tx_unicast, &es->tx_unicast);
651 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
652 			   I40E_GLV_MPTCL(stat_idx),
653 			   vsi->stat_offsets_loaded,
654 			   &oes->tx_multicast, &es->tx_multicast);
655 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
656 			   I40E_GLV_BPTCL(stat_idx),
657 			   vsi->stat_offsets_loaded,
658 			   &oes->tx_broadcast, &es->tx_broadcast);
659 	vsi->stat_offsets_loaded = true;
660 }
661 
662 /**
663  * i40e_update_veb_stats - Update Switch component statistics
664  * @veb: the VEB being updated
665  **/
666 static void i40e_update_veb_stats(struct i40e_veb *veb)
667 {
668 	struct i40e_pf *pf = veb->pf;
669 	struct i40e_hw *hw = &pf->hw;
670 	struct i40e_eth_stats *oes;
671 	struct i40e_eth_stats *es;     /* device's eth stats */
672 	struct i40e_veb_tc_stats *veb_oes;
673 	struct i40e_veb_tc_stats *veb_es;
674 	int i, idx = 0;
675 
676 	idx = veb->stats_idx;
677 	es = &veb->stats;
678 	oes = &veb->stats_offsets;
679 	veb_es = &veb->tc_stats;
680 	veb_oes = &veb->tc_stats_offsets;
681 
682 	/* Gather up the stats that the hw collects */
683 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
684 			   veb->stat_offsets_loaded,
685 			   &oes->tx_discards, &es->tx_discards);
686 	if (hw->revision_id > 0)
687 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
688 				   veb->stat_offsets_loaded,
689 				   &oes->rx_unknown_protocol,
690 				   &es->rx_unknown_protocol);
691 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
692 			   veb->stat_offsets_loaded,
693 			   &oes->rx_bytes, &es->rx_bytes);
694 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
695 			   veb->stat_offsets_loaded,
696 			   &oes->rx_unicast, &es->rx_unicast);
697 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
698 			   veb->stat_offsets_loaded,
699 			   &oes->rx_multicast, &es->rx_multicast);
700 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
701 			   veb->stat_offsets_loaded,
702 			   &oes->rx_broadcast, &es->rx_broadcast);
703 
704 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
705 			   veb->stat_offsets_loaded,
706 			   &oes->tx_bytes, &es->tx_bytes);
707 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
708 			   veb->stat_offsets_loaded,
709 			   &oes->tx_unicast, &es->tx_unicast);
710 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
711 			   veb->stat_offsets_loaded,
712 			   &oes->tx_multicast, &es->tx_multicast);
713 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
714 			   veb->stat_offsets_loaded,
715 			   &oes->tx_broadcast, &es->tx_broadcast);
716 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
717 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
718 				   I40E_GLVEBTC_RPCL(i, idx),
719 				   veb->stat_offsets_loaded,
720 				   &veb_oes->tc_rx_packets[i],
721 				   &veb_es->tc_rx_packets[i]);
722 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
723 				   I40E_GLVEBTC_RBCL(i, idx),
724 				   veb->stat_offsets_loaded,
725 				   &veb_oes->tc_rx_bytes[i],
726 				   &veb_es->tc_rx_bytes[i]);
727 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
728 				   I40E_GLVEBTC_TPCL(i, idx),
729 				   veb->stat_offsets_loaded,
730 				   &veb_oes->tc_tx_packets[i],
731 				   &veb_es->tc_tx_packets[i]);
732 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
733 				   I40E_GLVEBTC_TBCL(i, idx),
734 				   veb->stat_offsets_loaded,
735 				   &veb_oes->tc_tx_bytes[i],
736 				   &veb_es->tc_tx_bytes[i]);
737 	}
738 	veb->stat_offsets_loaded = true;
739 }
740 
741 /**
742  * i40e_update_vsi_stats - Update the vsi statistics counters.
743  * @vsi: the VSI to be updated
744  *
745  * There are a few instances where we store the same stat in a
746  * couple of different structs.  This is partly because we have
747  * the netdev stats that need to be filled out, which is slightly
748  * different from the "eth_stats" defined by the chip and used in
749  * VF communications.  We sort it out here.
750  **/
751 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
752 {
753 	struct i40e_pf *pf = vsi->back;
754 	struct rtnl_link_stats64 *ons;
755 	struct rtnl_link_stats64 *ns;   /* netdev stats */
756 	struct i40e_eth_stats *oes;
757 	struct i40e_eth_stats *es;     /* device's eth stats */
758 	u32 tx_restart, tx_busy;
759 	struct i40e_ring *p;
760 	u32 rx_page, rx_buf;
761 	u64 bytes, packets;
762 	unsigned int start;
763 	u64 tx_linearize;
764 	u64 tx_force_wb;
765 	u64 rx_p, rx_b;
766 	u64 tx_p, tx_b;
767 	u16 q;
768 
769 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
770 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
771 		return;
772 
773 	ns = i40e_get_vsi_stats_struct(vsi);
774 	ons = &vsi->net_stats_offsets;
775 	es = &vsi->eth_stats;
776 	oes = &vsi->eth_stats_offsets;
777 
778 	/* Gather up the netdev and vsi stats that the driver collects
779 	 * on the fly during packet processing
780 	 */
781 	rx_b = rx_p = 0;
782 	tx_b = tx_p = 0;
783 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
784 	rx_page = 0;
785 	rx_buf = 0;
786 	rcu_read_lock();
787 	for (q = 0; q < vsi->num_queue_pairs; q++) {
788 		/* locate Tx ring */
789 		p = READ_ONCE(vsi->tx_rings[q]);
790 
791 		do {
792 			start = u64_stats_fetch_begin_irq(&p->syncp);
793 			packets = p->stats.packets;
794 			bytes = p->stats.bytes;
795 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
796 		tx_b += bytes;
797 		tx_p += packets;
798 		tx_restart += p->tx_stats.restart_queue;
799 		tx_busy += p->tx_stats.tx_busy;
800 		tx_linearize += p->tx_stats.tx_linearize;
801 		tx_force_wb += p->tx_stats.tx_force_wb;
802 
803 		/* Rx queue is part of the same block as Tx queue */
804 		p = &p[1];
805 		do {
806 			start = u64_stats_fetch_begin_irq(&p->syncp);
807 			packets = p->stats.packets;
808 			bytes = p->stats.bytes;
809 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
810 		rx_b += bytes;
811 		rx_p += packets;
812 		rx_buf += p->rx_stats.alloc_buff_failed;
813 		rx_page += p->rx_stats.alloc_page_failed;
814 	}
815 	rcu_read_unlock();
816 	vsi->tx_restart = tx_restart;
817 	vsi->tx_busy = tx_busy;
818 	vsi->tx_linearize = tx_linearize;
819 	vsi->tx_force_wb = tx_force_wb;
820 	vsi->rx_page_failed = rx_page;
821 	vsi->rx_buf_failed = rx_buf;
822 
823 	ns->rx_packets = rx_p;
824 	ns->rx_bytes = rx_b;
825 	ns->tx_packets = tx_p;
826 	ns->tx_bytes = tx_b;
827 
828 	/* update netdev stats from eth stats */
829 	i40e_update_eth_stats(vsi);
830 	ons->tx_errors = oes->tx_errors;
831 	ns->tx_errors = es->tx_errors;
832 	ons->multicast = oes->rx_multicast;
833 	ns->multicast = es->rx_multicast;
834 	ons->rx_dropped = oes->rx_discards;
835 	ns->rx_dropped = es->rx_discards;
836 	ons->tx_dropped = oes->tx_discards;
837 	ns->tx_dropped = es->tx_discards;
838 
839 	/* pull in a couple PF stats if this is the main vsi */
840 	if (vsi == pf->vsi[pf->lan_vsi]) {
841 		ns->rx_crc_errors = pf->stats.crc_errors;
842 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
843 		ns->rx_length_errors = pf->stats.rx_length_errors;
844 	}
845 }
846 
847 /**
848  * i40e_update_pf_stats - Update the PF statistics counters.
849  * @pf: the PF to be updated
850  **/
851 static void i40e_update_pf_stats(struct i40e_pf *pf)
852 {
853 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
854 	struct i40e_hw_port_stats *nsd = &pf->stats;
855 	struct i40e_hw *hw = &pf->hw;
856 	u32 val;
857 	int i;
858 
859 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
860 			   I40E_GLPRT_GORCL(hw->port),
861 			   pf->stat_offsets_loaded,
862 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
863 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
864 			   I40E_GLPRT_GOTCL(hw->port),
865 			   pf->stat_offsets_loaded,
866 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
867 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
868 			   pf->stat_offsets_loaded,
869 			   &osd->eth.rx_discards,
870 			   &nsd->eth.rx_discards);
871 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
872 			   I40E_GLPRT_UPRCL(hw->port),
873 			   pf->stat_offsets_loaded,
874 			   &osd->eth.rx_unicast,
875 			   &nsd->eth.rx_unicast);
876 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
877 			   I40E_GLPRT_MPRCL(hw->port),
878 			   pf->stat_offsets_loaded,
879 			   &osd->eth.rx_multicast,
880 			   &nsd->eth.rx_multicast);
881 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
882 			   I40E_GLPRT_BPRCL(hw->port),
883 			   pf->stat_offsets_loaded,
884 			   &osd->eth.rx_broadcast,
885 			   &nsd->eth.rx_broadcast);
886 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
887 			   I40E_GLPRT_UPTCL(hw->port),
888 			   pf->stat_offsets_loaded,
889 			   &osd->eth.tx_unicast,
890 			   &nsd->eth.tx_unicast);
891 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
892 			   I40E_GLPRT_MPTCL(hw->port),
893 			   pf->stat_offsets_loaded,
894 			   &osd->eth.tx_multicast,
895 			   &nsd->eth.tx_multicast);
896 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
897 			   I40E_GLPRT_BPTCL(hw->port),
898 			   pf->stat_offsets_loaded,
899 			   &osd->eth.tx_broadcast,
900 			   &nsd->eth.tx_broadcast);
901 
902 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
903 			   pf->stat_offsets_loaded,
904 			   &osd->tx_dropped_link_down,
905 			   &nsd->tx_dropped_link_down);
906 
907 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
908 			   pf->stat_offsets_loaded,
909 			   &osd->crc_errors, &nsd->crc_errors);
910 
911 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->illegal_bytes, &nsd->illegal_bytes);
914 
915 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
916 			   pf->stat_offsets_loaded,
917 			   &osd->mac_local_faults,
918 			   &nsd->mac_local_faults);
919 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->mac_remote_faults,
922 			   &nsd->mac_remote_faults);
923 
924 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->rx_length_errors,
927 			   &nsd->rx_length_errors);
928 
929 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->link_xon_rx, &nsd->link_xon_rx);
932 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
933 			   pf->stat_offsets_loaded,
934 			   &osd->link_xon_tx, &nsd->link_xon_tx);
935 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
936 			   pf->stat_offsets_loaded,
937 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
938 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
941 
942 	for (i = 0; i < 8; i++) {
943 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
944 				   pf->stat_offsets_loaded,
945 				   &osd->priority_xoff_rx[i],
946 				   &nsd->priority_xoff_rx[i]);
947 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
948 				   pf->stat_offsets_loaded,
949 				   &osd->priority_xon_rx[i],
950 				   &nsd->priority_xon_rx[i]);
951 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
952 				   pf->stat_offsets_loaded,
953 				   &osd->priority_xon_tx[i],
954 				   &nsd->priority_xon_tx[i]);
955 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
956 				   pf->stat_offsets_loaded,
957 				   &osd->priority_xoff_tx[i],
958 				   &nsd->priority_xoff_tx[i]);
959 		i40e_stat_update32(hw,
960 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
961 				   pf->stat_offsets_loaded,
962 				   &osd->priority_xon_2_xoff[i],
963 				   &nsd->priority_xon_2_xoff[i]);
964 	}
965 
966 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
967 			   I40E_GLPRT_PRC64L(hw->port),
968 			   pf->stat_offsets_loaded,
969 			   &osd->rx_size_64, &nsd->rx_size_64);
970 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
971 			   I40E_GLPRT_PRC127L(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->rx_size_127, &nsd->rx_size_127);
974 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
975 			   I40E_GLPRT_PRC255L(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->rx_size_255, &nsd->rx_size_255);
978 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
979 			   I40E_GLPRT_PRC511L(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->rx_size_511, &nsd->rx_size_511);
982 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
983 			   I40E_GLPRT_PRC1023L(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->rx_size_1023, &nsd->rx_size_1023);
986 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
987 			   I40E_GLPRT_PRC1522L(hw->port),
988 			   pf->stat_offsets_loaded,
989 			   &osd->rx_size_1522, &nsd->rx_size_1522);
990 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
991 			   I40E_GLPRT_PRC9522L(hw->port),
992 			   pf->stat_offsets_loaded,
993 			   &osd->rx_size_big, &nsd->rx_size_big);
994 
995 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
996 			   I40E_GLPRT_PTC64L(hw->port),
997 			   pf->stat_offsets_loaded,
998 			   &osd->tx_size_64, &nsd->tx_size_64);
999 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1000 			   I40E_GLPRT_PTC127L(hw->port),
1001 			   pf->stat_offsets_loaded,
1002 			   &osd->tx_size_127, &nsd->tx_size_127);
1003 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1004 			   I40E_GLPRT_PTC255L(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->tx_size_255, &nsd->tx_size_255);
1007 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1008 			   I40E_GLPRT_PTC511L(hw->port),
1009 			   pf->stat_offsets_loaded,
1010 			   &osd->tx_size_511, &nsd->tx_size_511);
1011 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1012 			   I40E_GLPRT_PTC1023L(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1015 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1016 			   I40E_GLPRT_PTC1522L(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1019 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1020 			   I40E_GLPRT_PTC9522L(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->tx_size_big, &nsd->tx_size_big);
1023 
1024 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->rx_undersize, &nsd->rx_undersize);
1027 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->rx_fragments, &nsd->rx_fragments);
1030 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->rx_oversize, &nsd->rx_oversize);
1033 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->rx_jabber, &nsd->rx_jabber);
1036 
1037 	/* FDIR stats */
1038 	i40e_stat_update_and_clear32(hw,
1039 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1040 			&nsd->fd_atr_match);
1041 	i40e_stat_update_and_clear32(hw,
1042 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1043 			&nsd->fd_sb_match);
1044 	i40e_stat_update_and_clear32(hw,
1045 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1046 			&nsd->fd_atr_tunnel_match);
1047 
1048 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1049 	nsd->tx_lpi_status =
1050 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1051 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1052 	nsd->rx_lpi_status =
1053 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1054 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1055 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1058 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1061 
1062 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1063 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1064 		nsd->fd_sb_status = true;
1065 	else
1066 		nsd->fd_sb_status = false;
1067 
1068 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1069 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1070 		nsd->fd_atr_status = true;
1071 	else
1072 		nsd->fd_atr_status = false;
1073 
1074 	pf->stat_offsets_loaded = true;
1075 }
1076 
1077 /**
1078  * i40e_update_stats - Update the various statistics counters.
1079  * @vsi: the VSI to be updated
1080  *
1081  * Update the various stats for this VSI and its related entities.
1082  **/
1083 void i40e_update_stats(struct i40e_vsi *vsi)
1084 {
1085 	struct i40e_pf *pf = vsi->back;
1086 
1087 	if (vsi == pf->vsi[pf->lan_vsi])
1088 		i40e_update_pf_stats(pf);
1089 
1090 	i40e_update_vsi_stats(vsi);
1091 }
1092 
1093 /**
1094  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1095  * @vsi: the VSI to be searched
1096  * @macaddr: the MAC address
1097  * @vlan: the vlan
1098  *
1099  * Returns ptr to the filter object or NULL
1100  **/
1101 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1102 						const u8 *macaddr, s16 vlan)
1103 {
1104 	struct i40e_mac_filter *f;
1105 	u64 key;
1106 
1107 	if (!vsi || !macaddr)
1108 		return NULL;
1109 
1110 	key = i40e_addr_to_hkey(macaddr);
1111 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1112 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1113 		    (vlan == f->vlan))
1114 			return f;
1115 	}
1116 	return NULL;
1117 }
1118 
1119 /**
1120  * i40e_find_mac - Find a mac addr in the macvlan filters list
1121  * @vsi: the VSI to be searched
1122  * @macaddr: the MAC address we are searching for
1123  *
1124  * Returns the first filter with the provided MAC address or NULL if
1125  * MAC address was not found
1126  **/
1127 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1128 {
1129 	struct i40e_mac_filter *f;
1130 	u64 key;
1131 
1132 	if (!vsi || !macaddr)
1133 		return NULL;
1134 
1135 	key = i40e_addr_to_hkey(macaddr);
1136 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1137 		if ((ether_addr_equal(macaddr, f->macaddr)))
1138 			return f;
1139 	}
1140 	return NULL;
1141 }
1142 
1143 /**
1144  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1145  * @vsi: the VSI to be searched
1146  *
1147  * Returns true if VSI is in vlan mode or false otherwise
1148  **/
1149 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1150 {
1151 	/* If we have a PVID, always operate in VLAN mode */
1152 	if (vsi->info.pvid)
1153 		return true;
1154 
1155 	/* We need to operate in VLAN mode whenever we have any filters with
1156 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1157 	 * time, incurring search cost repeatedly. However, we can notice two
1158 	 * things:
1159 	 *
1160 	 * 1) the only place where we can gain a VLAN filter is in
1161 	 *    i40e_add_filter.
1162 	 *
1163 	 * 2) the only place where filters are actually removed is in
1164 	 *    i40e_sync_filters_subtask.
1165 	 *
1166 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1167 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1168 	 * we have to perform the full search after deleting filters in
1169 	 * i40e_sync_filters_subtask, but we already have to search
1170 	 * filters here and can perform the check at the same time. This
1171 	 * results in avoiding embedding a loop for VLAN mode inside another
1172 	 * loop over all the filters, and should maintain correctness as noted
1173 	 * above.
1174 	 */
1175 	return vsi->has_vlan_filter;
1176 }
1177 
1178 /**
1179  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1180  * @vsi: the VSI to configure
1181  * @tmp_add_list: list of filters ready to be added
1182  * @tmp_del_list: list of filters ready to be deleted
1183  * @vlan_filters: the number of active VLAN filters
1184  *
1185  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1186  * behave as expected. If we have any active VLAN filters remaining or about
1187  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1188  * so that they only match against untagged traffic. If we no longer have any
1189  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1190  * so that they match against both tagged and untagged traffic. In this way,
1191  * we ensure that we correctly receive the desired traffic. This ensures that
1192  * when we have an active VLAN we will receive only untagged traffic and
1193  * traffic matching active VLANs. If we have no active VLANs then we will
1194  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1195  *
1196  * Finally, in a similar fashion, this function also corrects filters when
1197  * there is an active PVID assigned to this VSI.
1198  *
1199  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1200  *
1201  * This function is only expected to be called from within
1202  * i40e_sync_vsi_filters.
1203  *
1204  * NOTE: This function expects to be called while under the
1205  * mac_filter_hash_lock
1206  */
1207 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1208 					 struct hlist_head *tmp_add_list,
1209 					 struct hlist_head *tmp_del_list,
1210 					 int vlan_filters)
1211 {
1212 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1213 	struct i40e_mac_filter *f, *add_head;
1214 	struct i40e_new_mac_filter *new;
1215 	struct hlist_node *h;
1216 	int bkt, new_vlan;
1217 
1218 	/* To determine if a particular filter needs to be replaced we
1219 	 * have the three following conditions:
1220 	 *
1221 	 * a) if we have a PVID assigned, then all filters which are
1222 	 *    not marked as VLAN=PVID must be replaced with filters that
1223 	 *    are.
1224 	 * b) otherwise, if we have any active VLANS, all filters
1225 	 *    which are marked as VLAN=-1 must be replaced with
1226 	 *    filters marked as VLAN=0
1227 	 * c) finally, if we do not have any active VLANS, all filters
1228 	 *    which are marked as VLAN=0 must be replaced with filters
1229 	 *    marked as VLAN=-1
1230 	 */
1231 
1232 	/* Update the filters about to be added in place */
1233 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1234 		if (pvid && new->f->vlan != pvid)
1235 			new->f->vlan = pvid;
1236 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1237 			new->f->vlan = 0;
1238 		else if (!vlan_filters && new->f->vlan == 0)
1239 			new->f->vlan = I40E_VLAN_ANY;
1240 	}
1241 
1242 	/* Update the remaining active filters */
1243 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1244 		/* Combine the checks for whether a filter needs to be changed
1245 		 * and then determine the new VLAN inside the if block, in
1246 		 * order to avoid duplicating code for adding the new filter
1247 		 * then deleting the old filter.
1248 		 */
1249 		if ((pvid && f->vlan != pvid) ||
1250 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1251 		    (!vlan_filters && f->vlan == 0)) {
1252 			/* Determine the new vlan we will be adding */
1253 			if (pvid)
1254 				new_vlan = pvid;
1255 			else if (vlan_filters)
1256 				new_vlan = 0;
1257 			else
1258 				new_vlan = I40E_VLAN_ANY;
1259 
1260 			/* Create the new filter */
1261 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1262 			if (!add_head)
1263 				return -ENOMEM;
1264 
1265 			/* Create a temporary i40e_new_mac_filter */
1266 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1267 			if (!new)
1268 				return -ENOMEM;
1269 
1270 			new->f = add_head;
1271 			new->state = add_head->state;
1272 
1273 			/* Add the new filter to the tmp list */
1274 			hlist_add_head(&new->hlist, tmp_add_list);
1275 
1276 			/* Put the original filter into the delete list */
1277 			f->state = I40E_FILTER_REMOVE;
1278 			hash_del(&f->hlist);
1279 			hlist_add_head(&f->hlist, tmp_del_list);
1280 		}
1281 	}
1282 
1283 	vsi->has_vlan_filter = !!vlan_filters;
1284 
1285 	return 0;
1286 }
1287 
1288 /**
1289  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1290  * @vsi: the PF Main VSI - inappropriate for any other VSI
1291  * @macaddr: the MAC address
1292  *
1293  * Remove whatever filter the firmware set up so the driver can manage
1294  * its own filtering intelligently.
1295  **/
1296 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1297 {
1298 	struct i40e_aqc_remove_macvlan_element_data element;
1299 	struct i40e_pf *pf = vsi->back;
1300 
1301 	/* Only appropriate for the PF main VSI */
1302 	if (vsi->type != I40E_VSI_MAIN)
1303 		return;
1304 
1305 	memset(&element, 0, sizeof(element));
1306 	ether_addr_copy(element.mac_addr, macaddr);
1307 	element.vlan_tag = 0;
1308 	/* Ignore error returns, some firmware does it this way... */
1309 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1310 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1311 
1312 	memset(&element, 0, sizeof(element));
1313 	ether_addr_copy(element.mac_addr, macaddr);
1314 	element.vlan_tag = 0;
1315 	/* ...and some firmware does it this way. */
1316 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1317 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1318 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1319 }
1320 
1321 /**
1322  * i40e_add_filter - Add a mac/vlan filter to the VSI
1323  * @vsi: the VSI to be searched
1324  * @macaddr: the MAC address
1325  * @vlan: the vlan
1326  *
1327  * Returns ptr to the filter object or NULL when no memory available.
1328  *
1329  * NOTE: This function is expected to be called with mac_filter_hash_lock
1330  * being held.
1331  **/
1332 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1333 					const u8 *macaddr, s16 vlan)
1334 {
1335 	struct i40e_mac_filter *f;
1336 	u64 key;
1337 
1338 	if (!vsi || !macaddr)
1339 		return NULL;
1340 
1341 	f = i40e_find_filter(vsi, macaddr, vlan);
1342 	if (!f) {
1343 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1344 		if (!f)
1345 			return NULL;
1346 
1347 		/* Update the boolean indicating if we need to function in
1348 		 * VLAN mode.
1349 		 */
1350 		if (vlan >= 0)
1351 			vsi->has_vlan_filter = true;
1352 
1353 		ether_addr_copy(f->macaddr, macaddr);
1354 		f->vlan = vlan;
1355 		f->state = I40E_FILTER_NEW;
1356 		INIT_HLIST_NODE(&f->hlist);
1357 
1358 		key = i40e_addr_to_hkey(macaddr);
1359 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1360 
1361 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1362 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1363 	}
1364 
1365 	/* If we're asked to add a filter that has been marked for removal, it
1366 	 * is safe to simply restore it to active state. __i40e_del_filter
1367 	 * will have simply deleted any filters which were previously marked
1368 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1369 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1370 	 * task, just restore this filter to the ACTIVE state so that the
1371 	 * sync task leaves it in place
1372 	 */
1373 	if (f->state == I40E_FILTER_REMOVE)
1374 		f->state = I40E_FILTER_ACTIVE;
1375 
1376 	return f;
1377 }
1378 
1379 /**
1380  * __i40e_del_filter - Remove a specific filter from the VSI
1381  * @vsi: VSI to remove from
1382  * @f: the filter to remove from the list
1383  *
1384  * This function should be called instead of i40e_del_filter only if you know
1385  * the exact filter you will remove already, such as via i40e_find_filter or
1386  * i40e_find_mac.
1387  *
1388  * NOTE: This function is expected to be called with mac_filter_hash_lock
1389  * being held.
1390  * ANOTHER NOTE: This function MUST be called from within the context of
1391  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1392  * instead of list_for_each_entry().
1393  **/
1394 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1395 {
1396 	if (!f)
1397 		return;
1398 
1399 	/* If the filter was never added to firmware then we can just delete it
1400 	 * directly and we don't want to set the status to remove or else an
1401 	 * admin queue command will unnecessarily fire.
1402 	 */
1403 	if ((f->state == I40E_FILTER_FAILED) ||
1404 	    (f->state == I40E_FILTER_NEW)) {
1405 		hash_del(&f->hlist);
1406 		kfree(f);
1407 	} else {
1408 		f->state = I40E_FILTER_REMOVE;
1409 	}
1410 
1411 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1412 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->state);
1413 }
1414 
1415 /**
1416  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1417  * @vsi: the VSI to be searched
1418  * @macaddr: the MAC address
1419  * @vlan: the VLAN
1420  *
1421  * NOTE: This function is expected to be called with mac_filter_hash_lock
1422  * being held.
1423  * ANOTHER NOTE: This function MUST be called from within the context of
1424  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1425  * instead of list_for_each_entry().
1426  **/
1427 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1428 {
1429 	struct i40e_mac_filter *f;
1430 
1431 	if (!vsi || !macaddr)
1432 		return;
1433 
1434 	f = i40e_find_filter(vsi, macaddr, vlan);
1435 	__i40e_del_filter(vsi, f);
1436 }
1437 
1438 /**
1439  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1440  * @vsi: the VSI to be searched
1441  * @macaddr: the mac address to be filtered
1442  *
1443  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1444  * go through all the macvlan filters and add a macvlan filter for each
1445  * unique vlan that already exists. If a PVID has been assigned, instead only
1446  * add the macaddr to that VLAN.
1447  *
1448  * Returns last filter added on success, else NULL
1449  **/
1450 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1451 					    const u8 *macaddr)
1452 {
1453 	struct i40e_mac_filter *f, *add = NULL;
1454 	struct hlist_node *h;
1455 	int bkt;
1456 
1457 	if (vsi->info.pvid)
1458 		return i40e_add_filter(vsi, macaddr,
1459 				       le16_to_cpu(vsi->info.pvid));
1460 
1461 	if (!i40e_is_vsi_in_vlan(vsi))
1462 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1463 
1464 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1465 		if (f->state == I40E_FILTER_REMOVE)
1466 			continue;
1467 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1468 		if (!add)
1469 			return NULL;
1470 	}
1471 
1472 	return add;
1473 }
1474 
1475 /**
1476  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1477  * @vsi: the VSI to be searched
1478  * @macaddr: the mac address to be removed
1479  *
1480  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1481  * associated with.
1482  *
1483  * Returns 0 for success, or error
1484  **/
1485 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1486 {
1487 	struct i40e_mac_filter *f;
1488 	struct hlist_node *h;
1489 	bool found = false;
1490 	int bkt;
1491 
1492 	WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1493 	     "Missing mac_filter_hash_lock\n");
1494 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1495 		if (ether_addr_equal(macaddr, f->macaddr)) {
1496 			__i40e_del_filter(vsi, f);
1497 			found = true;
1498 		}
1499 	}
1500 
1501 	if (found)
1502 		return 0;
1503 	else
1504 		return -ENOENT;
1505 }
1506 
1507 /**
1508  * i40e_set_mac - NDO callback to set mac address
1509  * @netdev: network interface device structure
1510  * @p: pointer to an address structure
1511  *
1512  * Returns 0 on success, negative on failure
1513  **/
1514 static int i40e_set_mac(struct net_device *netdev, void *p)
1515 {
1516 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1517 	struct i40e_vsi *vsi = np->vsi;
1518 	struct i40e_pf *pf = vsi->back;
1519 	struct i40e_hw *hw = &pf->hw;
1520 	struct sockaddr *addr = p;
1521 
1522 	if (!is_valid_ether_addr(addr->sa_data))
1523 		return -EADDRNOTAVAIL;
1524 
1525 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1526 		netdev_info(netdev, "already using mac address %pM\n",
1527 			    addr->sa_data);
1528 		return 0;
1529 	}
1530 
1531 	if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1532 	    test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
1533 		return -EADDRNOTAVAIL;
1534 
1535 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1536 		netdev_info(netdev, "returning to hw mac address %pM\n",
1537 			    hw->mac.addr);
1538 	else
1539 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1540 
1541 	/* Copy the address first, so that we avoid a possible race with
1542 	 * .set_rx_mode(). If we copy after changing the address in the filter
1543 	 * list, we might open ourselves to a narrow race window where
1544 	 * .set_rx_mode could delete our dev_addr filter and prevent traffic
1545 	 * from passing.
1546 	 */
1547 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1548 
1549 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1550 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1551 	i40e_add_mac_filter(vsi, addr->sa_data);
1552 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1553 	if (vsi->type == I40E_VSI_MAIN) {
1554 		i40e_status ret;
1555 
1556 		ret = i40e_aq_mac_address_write(&vsi->back->hw,
1557 						I40E_AQC_WRITE_TYPE_LAA_WOL,
1558 						addr->sa_data, NULL);
1559 		if (ret)
1560 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1561 				    i40e_stat_str(hw, ret),
1562 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1563 	}
1564 
1565 	/* schedule our worker thread which will take care of
1566 	 * applying the new filter changes
1567 	 */
1568 	i40e_service_event_schedule(vsi->back);
1569 	return 0;
1570 }
1571 
1572 /**
1573  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1574  * @vsi: vsi structure
1575  * @seed: RSS hash seed
1576  **/
1577 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1578 			      u8 *lut, u16 lut_size)
1579 {
1580 	struct i40e_pf *pf = vsi->back;
1581 	struct i40e_hw *hw = &pf->hw;
1582 	int ret = 0;
1583 
1584 	if (seed) {
1585 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1586 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1587 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1588 		if (ret) {
1589 			dev_info(&pf->pdev->dev,
1590 				 "Cannot set RSS key, err %s aq_err %s\n",
1591 				 i40e_stat_str(hw, ret),
1592 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1593 			return ret;
1594 		}
1595 	}
1596 	if (lut) {
1597 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1598 
1599 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1600 		if (ret) {
1601 			dev_info(&pf->pdev->dev,
1602 				 "Cannot set RSS lut, err %s aq_err %s\n",
1603 				 i40e_stat_str(hw, ret),
1604 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1605 			return ret;
1606 		}
1607 	}
1608 	return ret;
1609 }
1610 
1611 /**
1612  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1613  * @vsi: VSI structure
1614  **/
1615 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1616 {
1617 	struct i40e_pf *pf = vsi->back;
1618 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1619 	u8 *lut;
1620 	int ret;
1621 
1622 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1623 		return 0;
1624 	if (!vsi->rss_size)
1625 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1626 				      vsi->num_queue_pairs);
1627 	if (!vsi->rss_size)
1628 		return -EINVAL;
1629 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1630 	if (!lut)
1631 		return -ENOMEM;
1632 
1633 	/* Use the user configured hash keys and lookup table if there is one,
1634 	 * otherwise use default
1635 	 */
1636 	if (vsi->rss_lut_user)
1637 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1638 	else
1639 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1640 	if (vsi->rss_hkey_user)
1641 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1642 	else
1643 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1644 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1645 	kfree(lut);
1646 	return ret;
1647 }
1648 
1649 /**
1650  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1651  * @vsi: the VSI being configured,
1652  * @ctxt: VSI context structure
1653  * @enabled_tc: number of traffic classes to enable
1654  *
1655  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1656  **/
1657 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1658 					   struct i40e_vsi_context *ctxt,
1659 					   u8 enabled_tc)
1660 {
1661 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1662 	int i, override_q, pow, num_qps, ret;
1663 	u8 netdev_tc = 0, offset = 0;
1664 
1665 	if (vsi->type != I40E_VSI_MAIN)
1666 		return -EINVAL;
1667 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1668 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1669 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1670 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1671 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1672 
1673 	/* find the next higher power-of-2 of num queue pairs */
1674 	pow = ilog2(num_qps);
1675 	if (!is_power_of_2(num_qps))
1676 		pow++;
1677 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1678 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1679 
1680 	/* Setup queue offset/count for all TCs for given VSI */
1681 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1682 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1683 		/* See if the given TC is enabled for the given VSI */
1684 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1685 			offset = vsi->mqprio_qopt.qopt.offset[i];
1686 			qcount = vsi->mqprio_qopt.qopt.count[i];
1687 			if (qcount > max_qcount)
1688 				max_qcount = qcount;
1689 			vsi->tc_config.tc_info[i].qoffset = offset;
1690 			vsi->tc_config.tc_info[i].qcount = qcount;
1691 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1692 		} else {
1693 			/* TC is not enabled so set the offset to
1694 			 * default queue and allocate one queue
1695 			 * for the given TC.
1696 			 */
1697 			vsi->tc_config.tc_info[i].qoffset = 0;
1698 			vsi->tc_config.tc_info[i].qcount = 1;
1699 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1700 		}
1701 	}
1702 
1703 	/* Set actual Tx/Rx queue pairs */
1704 	vsi->num_queue_pairs = offset + qcount;
1705 
1706 	/* Setup queue TC[0].qmap for given VSI context */
1707 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1708 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1709 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1710 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1711 
1712 	/* Reconfigure RSS for main VSI with max queue count */
1713 	vsi->rss_size = max_qcount;
1714 	ret = i40e_vsi_config_rss(vsi);
1715 	if (ret) {
1716 		dev_info(&vsi->back->pdev->dev,
1717 			 "Failed to reconfig rss for num_queues (%u)\n",
1718 			 max_qcount);
1719 		return ret;
1720 	}
1721 	vsi->reconfig_rss = true;
1722 	dev_dbg(&vsi->back->pdev->dev,
1723 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1724 
1725 	/* Find queue count available for channel VSIs and starting offset
1726 	 * for channel VSIs
1727 	 */
1728 	override_q = vsi->mqprio_qopt.qopt.count[0];
1729 	if (override_q && override_q < vsi->num_queue_pairs) {
1730 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1731 		vsi->next_base_queue = override_q;
1732 	}
1733 	return 0;
1734 }
1735 
1736 /**
1737  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1738  * @vsi: the VSI being setup
1739  * @ctxt: VSI context structure
1740  * @enabled_tc: Enabled TCs bitmap
1741  * @is_add: True if called before Add VSI
1742  *
1743  * Setup VSI queue mapping for enabled traffic classes.
1744  **/
1745 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1746 				     struct i40e_vsi_context *ctxt,
1747 				     u8 enabled_tc,
1748 				     bool is_add)
1749 {
1750 	struct i40e_pf *pf = vsi->back;
1751 	u16 sections = 0;
1752 	u8 netdev_tc = 0;
1753 	u16 numtc = 1;
1754 	u16 qcount;
1755 	u8 offset;
1756 	u16 qmap;
1757 	int i;
1758 	u16 num_tc_qps = 0;
1759 
1760 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1761 	offset = 0;
1762 
1763 	/* Number of queues per enabled TC */
1764 	num_tc_qps = vsi->alloc_queue_pairs;
1765 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1766 		/* Find numtc from enabled TC bitmap */
1767 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1768 			if (enabled_tc & BIT(i)) /* TC is enabled */
1769 				numtc++;
1770 		}
1771 		if (!numtc) {
1772 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1773 			numtc = 1;
1774 		}
1775 		num_tc_qps = num_tc_qps / numtc;
1776 		num_tc_qps = min_t(int, num_tc_qps,
1777 				   i40e_pf_get_max_q_per_tc(pf));
1778 	}
1779 
1780 	vsi->tc_config.numtc = numtc;
1781 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1782 
1783 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1784 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1785 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1786 
1787 	/* Setup queue offset/count for all TCs for given VSI */
1788 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1789 		/* See if the given TC is enabled for the given VSI */
1790 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1791 			/* TC is enabled */
1792 			int pow, num_qps;
1793 
1794 			switch (vsi->type) {
1795 			case I40E_VSI_MAIN:
1796 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1797 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1798 				    vsi->tc_config.enabled_tc != 1) {
1799 					qcount = min_t(int, pf->alloc_rss_size,
1800 						       num_tc_qps);
1801 					break;
1802 				}
1803 				/* fall through */
1804 			case I40E_VSI_FDIR:
1805 			case I40E_VSI_SRIOV:
1806 			case I40E_VSI_VMDQ2:
1807 			default:
1808 				qcount = num_tc_qps;
1809 				WARN_ON(i != 0);
1810 				break;
1811 			}
1812 			vsi->tc_config.tc_info[i].qoffset = offset;
1813 			vsi->tc_config.tc_info[i].qcount = qcount;
1814 
1815 			/* find the next higher power-of-2 of num queue pairs */
1816 			num_qps = qcount;
1817 			pow = 0;
1818 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1819 				pow++;
1820 				num_qps >>= 1;
1821 			}
1822 
1823 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1824 			qmap =
1825 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1826 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1827 
1828 			offset += qcount;
1829 		} else {
1830 			/* TC is not enabled so set the offset to
1831 			 * default queue and allocate one queue
1832 			 * for the given TC.
1833 			 */
1834 			vsi->tc_config.tc_info[i].qoffset = 0;
1835 			vsi->tc_config.tc_info[i].qcount = 1;
1836 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1837 
1838 			qmap = 0;
1839 		}
1840 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1841 	}
1842 
1843 	/* Set actual Tx/Rx queue pairs */
1844 	vsi->num_queue_pairs = offset;
1845 	if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1846 		if (vsi->req_queue_pairs > 0)
1847 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1848 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1849 			vsi->num_queue_pairs = pf->num_lan_msix;
1850 	}
1851 
1852 	/* Scheduler section valid can only be set for ADD VSI */
1853 	if (is_add) {
1854 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1855 
1856 		ctxt->info.up_enable_bits = enabled_tc;
1857 	}
1858 	if (vsi->type == I40E_VSI_SRIOV) {
1859 		ctxt->info.mapping_flags |=
1860 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1861 		for (i = 0; i < vsi->num_queue_pairs; i++)
1862 			ctxt->info.queue_mapping[i] =
1863 					       cpu_to_le16(vsi->base_queue + i);
1864 	} else {
1865 		ctxt->info.mapping_flags |=
1866 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1867 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1868 	}
1869 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1870 }
1871 
1872 /**
1873  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1874  * @netdev: the netdevice
1875  * @addr: address to add
1876  *
1877  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1878  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1879  */
1880 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1881 {
1882 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1883 	struct i40e_vsi *vsi = np->vsi;
1884 
1885 	if (i40e_add_mac_filter(vsi, addr))
1886 		return 0;
1887 	else
1888 		return -ENOMEM;
1889 }
1890 
1891 /**
1892  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1893  * @netdev: the netdevice
1894  * @addr: address to add
1895  *
1896  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1897  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1898  */
1899 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1900 {
1901 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1902 	struct i40e_vsi *vsi = np->vsi;
1903 
1904 	/* Under some circumstances, we might receive a request to delete
1905 	 * our own device address from our uc list. Because we store the
1906 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1907 	 * such requests and not delete our device address from this list.
1908 	 */
1909 	if (ether_addr_equal(addr, netdev->dev_addr))
1910 		return 0;
1911 
1912 	i40e_del_mac_filter(vsi, addr);
1913 
1914 	return 0;
1915 }
1916 
1917 /**
1918  * i40e_set_rx_mode - NDO callback to set the netdev filters
1919  * @netdev: network interface device structure
1920  **/
1921 static void i40e_set_rx_mode(struct net_device *netdev)
1922 {
1923 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1924 	struct i40e_vsi *vsi = np->vsi;
1925 
1926 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1927 
1928 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1929 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1930 
1931 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1932 
1933 	/* check for other flag changes */
1934 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
1935 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1936 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1937 	}
1938 }
1939 
1940 /**
1941  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1942  * @vsi: Pointer to VSI struct
1943  * @from: Pointer to list which contains MAC filter entries - changes to
1944  *        those entries needs to be undone.
1945  *
1946  * MAC filter entries from this list were slated for deletion.
1947  **/
1948 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1949 					 struct hlist_head *from)
1950 {
1951 	struct i40e_mac_filter *f;
1952 	struct hlist_node *h;
1953 
1954 	hlist_for_each_entry_safe(f, h, from, hlist) {
1955 		u64 key = i40e_addr_to_hkey(f->macaddr);
1956 
1957 		/* Move the element back into MAC filter list*/
1958 		hlist_del(&f->hlist);
1959 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1960 	}
1961 }
1962 
1963 /**
1964  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1965  * @vsi: Pointer to vsi struct
1966  * @from: Pointer to list which contains MAC filter entries - changes to
1967  *        those entries needs to be undone.
1968  *
1969  * MAC filter entries from this list were slated for addition.
1970  **/
1971 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1972 					 struct hlist_head *from)
1973 {
1974 	struct i40e_new_mac_filter *new;
1975 	struct hlist_node *h;
1976 
1977 	hlist_for_each_entry_safe(new, h, from, hlist) {
1978 		/* We can simply free the wrapper structure */
1979 		hlist_del(&new->hlist);
1980 		kfree(new);
1981 	}
1982 }
1983 
1984 /**
1985  * i40e_next_entry - Get the next non-broadcast filter from a list
1986  * @next: pointer to filter in list
1987  *
1988  * Returns the next non-broadcast filter in the list. Required so that we
1989  * ignore broadcast filters within the list, since these are not handled via
1990  * the normal firmware update path.
1991  */
1992 static
1993 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
1994 {
1995 	hlist_for_each_entry_continue(next, hlist) {
1996 		if (!is_broadcast_ether_addr(next->f->macaddr))
1997 			return next;
1998 	}
1999 
2000 	return NULL;
2001 }
2002 
2003 /**
2004  * i40e_update_filter_state - Update filter state based on return data
2005  * from firmware
2006  * @count: Number of filters added
2007  * @add_list: return data from fw
2008  * @add_head: pointer to first filter in current batch
2009  *
2010  * MAC filter entries from list were slated to be added to device. Returns
2011  * number of successful filters. Note that 0 does NOT mean success!
2012  **/
2013 static int
2014 i40e_update_filter_state(int count,
2015 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2016 			 struct i40e_new_mac_filter *add_head)
2017 {
2018 	int retval = 0;
2019 	int i;
2020 
2021 	for (i = 0; i < count; i++) {
2022 		/* Always check status of each filter. We don't need to check
2023 		 * the firmware return status because we pre-set the filter
2024 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2025 		 * request to the adminq. Thus, if it no longer matches then
2026 		 * we know the filter is active.
2027 		 */
2028 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2029 			add_head->state = I40E_FILTER_FAILED;
2030 		} else {
2031 			add_head->state = I40E_FILTER_ACTIVE;
2032 			retval++;
2033 		}
2034 
2035 		add_head = i40e_next_filter(add_head);
2036 		if (!add_head)
2037 			break;
2038 	}
2039 
2040 	return retval;
2041 }
2042 
2043 /**
2044  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2045  * @vsi: ptr to the VSI
2046  * @vsi_name: name to display in messages
2047  * @list: the list of filters to send to firmware
2048  * @num_del: the number of filters to delete
2049  * @retval: Set to -EIO on failure to delete
2050  *
2051  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2052  * *retval instead of a return value so that success does not force ret_val to
2053  * be set to 0. This ensures that a sequence of calls to this function
2054  * preserve the previous value of *retval on successful delete.
2055  */
2056 static
2057 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2058 			  struct i40e_aqc_remove_macvlan_element_data *list,
2059 			  int num_del, int *retval)
2060 {
2061 	struct i40e_hw *hw = &vsi->back->hw;
2062 	i40e_status aq_ret;
2063 	int aq_err;
2064 
2065 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2066 	aq_err = hw->aq.asq_last_status;
2067 
2068 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2069 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2070 		*retval = -EIO;
2071 		dev_info(&vsi->back->pdev->dev,
2072 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2073 			 vsi_name, i40e_stat_str(hw, aq_ret),
2074 			 i40e_aq_str(hw, aq_err));
2075 	}
2076 }
2077 
2078 /**
2079  * i40e_aqc_add_filters - Request firmware to add a set of filters
2080  * @vsi: ptr to the VSI
2081  * @vsi_name: name to display in messages
2082  * @list: the list of filters to send to firmware
2083  * @add_head: Position in the add hlist
2084  * @num_add: the number of filters to add
2085  *
2086  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2087  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2088  * space for more filters.
2089  */
2090 static
2091 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2092 			  struct i40e_aqc_add_macvlan_element_data *list,
2093 			  struct i40e_new_mac_filter *add_head,
2094 			  int num_add)
2095 {
2096 	struct i40e_hw *hw = &vsi->back->hw;
2097 	int aq_err, fcnt;
2098 
2099 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2100 	aq_err = hw->aq.asq_last_status;
2101 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2102 
2103 	if (fcnt != num_add) {
2104 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2105 		dev_warn(&vsi->back->pdev->dev,
2106 			 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2107 			 i40e_aq_str(hw, aq_err),
2108 			 vsi_name);
2109 	}
2110 }
2111 
2112 /**
2113  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2114  * @vsi: pointer to the VSI
2115  * @vsi_name: the VSI name
2116  * @f: filter data
2117  *
2118  * This function sets or clears the promiscuous broadcast flags for VLAN
2119  * filters in order to properly receive broadcast frames. Assumes that only
2120  * broadcast filters are passed.
2121  *
2122  * Returns status indicating success or failure;
2123  **/
2124 static i40e_status
2125 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2126 			  struct i40e_mac_filter *f)
2127 {
2128 	bool enable = f->state == I40E_FILTER_NEW;
2129 	struct i40e_hw *hw = &vsi->back->hw;
2130 	i40e_status aq_ret;
2131 
2132 	if (f->vlan == I40E_VLAN_ANY) {
2133 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2134 						   vsi->seid,
2135 						   enable,
2136 						   NULL);
2137 	} else {
2138 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2139 							    vsi->seid,
2140 							    enable,
2141 							    f->vlan,
2142 							    NULL);
2143 	}
2144 
2145 	if (aq_ret) {
2146 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2147 		dev_warn(&vsi->back->pdev->dev,
2148 			 "Error %s, forcing overflow promiscuous on %s\n",
2149 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2150 			 vsi_name);
2151 	}
2152 
2153 	return aq_ret;
2154 }
2155 
2156 /**
2157  * i40e_set_promiscuous - set promiscuous mode
2158  * @pf: board private structure
2159  * @promisc: promisc on or off
2160  *
2161  * There are different ways of setting promiscuous mode on a PF depending on
2162  * what state/environment we're in.  This identifies and sets it appropriately.
2163  * Returns 0 on success.
2164  **/
2165 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2166 {
2167 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2168 	struct i40e_hw *hw = &pf->hw;
2169 	i40e_status aq_ret;
2170 
2171 	if (vsi->type == I40E_VSI_MAIN &&
2172 	    pf->lan_veb != I40E_NO_VEB &&
2173 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2174 		/* set defport ON for Main VSI instead of true promisc
2175 		 * this way we will get all unicast/multicast and VLAN
2176 		 * promisc behavior but will not get VF or VMDq traffic
2177 		 * replicated on the Main VSI.
2178 		 */
2179 		if (promisc)
2180 			aq_ret = i40e_aq_set_default_vsi(hw,
2181 							 vsi->seid,
2182 							 NULL);
2183 		else
2184 			aq_ret = i40e_aq_clear_default_vsi(hw,
2185 							   vsi->seid,
2186 							   NULL);
2187 		if (aq_ret) {
2188 			dev_info(&pf->pdev->dev,
2189 				 "Set default VSI failed, err %s, aq_err %s\n",
2190 				 i40e_stat_str(hw, aq_ret),
2191 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2192 		}
2193 	} else {
2194 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2195 						  hw,
2196 						  vsi->seid,
2197 						  promisc, NULL,
2198 						  true);
2199 		if (aq_ret) {
2200 			dev_info(&pf->pdev->dev,
2201 				 "set unicast promisc failed, err %s, aq_err %s\n",
2202 				 i40e_stat_str(hw, aq_ret),
2203 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2204 		}
2205 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2206 						  hw,
2207 						  vsi->seid,
2208 						  promisc, NULL);
2209 		if (aq_ret) {
2210 			dev_info(&pf->pdev->dev,
2211 				 "set multicast promisc failed, err %s, aq_err %s\n",
2212 				 i40e_stat_str(hw, aq_ret),
2213 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2214 		}
2215 	}
2216 
2217 	if (!aq_ret)
2218 		pf->cur_promisc = promisc;
2219 
2220 	return aq_ret;
2221 }
2222 
2223 /**
2224  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2225  * @vsi: ptr to the VSI
2226  *
2227  * Push any outstanding VSI filter changes through the AdminQ.
2228  *
2229  * Returns 0 or error value
2230  **/
2231 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2232 {
2233 	struct hlist_head tmp_add_list, tmp_del_list;
2234 	struct i40e_mac_filter *f;
2235 	struct i40e_new_mac_filter *new, *add_head = NULL;
2236 	struct i40e_hw *hw = &vsi->back->hw;
2237 	bool old_overflow, new_overflow;
2238 	unsigned int failed_filters = 0;
2239 	unsigned int vlan_filters = 0;
2240 	char vsi_name[16] = "PF";
2241 	int filter_list_len = 0;
2242 	i40e_status aq_ret = 0;
2243 	u32 changed_flags = 0;
2244 	struct hlist_node *h;
2245 	struct i40e_pf *pf;
2246 	int num_add = 0;
2247 	int num_del = 0;
2248 	int retval = 0;
2249 	u16 cmd_flags;
2250 	int list_size;
2251 	int bkt;
2252 
2253 	/* empty array typed pointers, kcalloc later */
2254 	struct i40e_aqc_add_macvlan_element_data *add_list;
2255 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2256 
2257 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2258 		usleep_range(1000, 2000);
2259 	pf = vsi->back;
2260 
2261 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2262 
2263 	if (vsi->netdev) {
2264 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2265 		vsi->current_netdev_flags = vsi->netdev->flags;
2266 	}
2267 
2268 	INIT_HLIST_HEAD(&tmp_add_list);
2269 	INIT_HLIST_HEAD(&tmp_del_list);
2270 
2271 	if (vsi->type == I40E_VSI_SRIOV)
2272 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2273 	else if (vsi->type != I40E_VSI_MAIN)
2274 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2275 
2276 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2277 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2278 
2279 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2280 		/* Create a list of filters to delete. */
2281 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2282 			if (f->state == I40E_FILTER_REMOVE) {
2283 				/* Move the element into temporary del_list */
2284 				hash_del(&f->hlist);
2285 				hlist_add_head(&f->hlist, &tmp_del_list);
2286 
2287 				/* Avoid counting removed filters */
2288 				continue;
2289 			}
2290 			if (f->state == I40E_FILTER_NEW) {
2291 				/* Create a temporary i40e_new_mac_filter */
2292 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2293 				if (!new)
2294 					goto err_no_memory_locked;
2295 
2296 				/* Store pointer to the real filter */
2297 				new->f = f;
2298 				new->state = f->state;
2299 
2300 				/* Add it to the hash list */
2301 				hlist_add_head(&new->hlist, &tmp_add_list);
2302 			}
2303 
2304 			/* Count the number of active (current and new) VLAN
2305 			 * filters we have now. Does not count filters which
2306 			 * are marked for deletion.
2307 			 */
2308 			if (f->vlan > 0)
2309 				vlan_filters++;
2310 		}
2311 
2312 		retval = i40e_correct_mac_vlan_filters(vsi,
2313 						       &tmp_add_list,
2314 						       &tmp_del_list,
2315 						       vlan_filters);
2316 		if (retval)
2317 			goto err_no_memory_locked;
2318 
2319 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2320 	}
2321 
2322 	/* Now process 'del_list' outside the lock */
2323 	if (!hlist_empty(&tmp_del_list)) {
2324 		filter_list_len = hw->aq.asq_buf_size /
2325 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2326 		list_size = filter_list_len *
2327 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2328 		del_list = kzalloc(list_size, GFP_ATOMIC);
2329 		if (!del_list)
2330 			goto err_no_memory;
2331 
2332 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2333 			cmd_flags = 0;
2334 
2335 			/* handle broadcast filters by updating the broadcast
2336 			 * promiscuous flag and release filter list.
2337 			 */
2338 			if (is_broadcast_ether_addr(f->macaddr)) {
2339 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2340 
2341 				hlist_del(&f->hlist);
2342 				kfree(f);
2343 				continue;
2344 			}
2345 
2346 			/* add to delete list */
2347 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2348 			if (f->vlan == I40E_VLAN_ANY) {
2349 				del_list[num_del].vlan_tag = 0;
2350 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2351 			} else {
2352 				del_list[num_del].vlan_tag =
2353 					cpu_to_le16((u16)(f->vlan));
2354 			}
2355 
2356 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2357 			del_list[num_del].flags = cmd_flags;
2358 			num_del++;
2359 
2360 			/* flush a full buffer */
2361 			if (num_del == filter_list_len) {
2362 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2363 						     num_del, &retval);
2364 				memset(del_list, 0, list_size);
2365 				num_del = 0;
2366 			}
2367 			/* Release memory for MAC filter entries which were
2368 			 * synced up with HW.
2369 			 */
2370 			hlist_del(&f->hlist);
2371 			kfree(f);
2372 		}
2373 
2374 		if (num_del) {
2375 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2376 					     num_del, &retval);
2377 		}
2378 
2379 		kfree(del_list);
2380 		del_list = NULL;
2381 	}
2382 
2383 	if (!hlist_empty(&tmp_add_list)) {
2384 		/* Do all the adds now. */
2385 		filter_list_len = hw->aq.asq_buf_size /
2386 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2387 		list_size = filter_list_len *
2388 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2389 		add_list = kzalloc(list_size, GFP_ATOMIC);
2390 		if (!add_list)
2391 			goto err_no_memory;
2392 
2393 		num_add = 0;
2394 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2395 			/* handle broadcast filters by updating the broadcast
2396 			 * promiscuous flag instead of adding a MAC filter.
2397 			 */
2398 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2399 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2400 							      new->f))
2401 					new->state = I40E_FILTER_FAILED;
2402 				else
2403 					new->state = I40E_FILTER_ACTIVE;
2404 				continue;
2405 			}
2406 
2407 			/* add to add array */
2408 			if (num_add == 0)
2409 				add_head = new;
2410 			cmd_flags = 0;
2411 			ether_addr_copy(add_list[num_add].mac_addr,
2412 					new->f->macaddr);
2413 			if (new->f->vlan == I40E_VLAN_ANY) {
2414 				add_list[num_add].vlan_tag = 0;
2415 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2416 			} else {
2417 				add_list[num_add].vlan_tag =
2418 					cpu_to_le16((u16)(new->f->vlan));
2419 			}
2420 			add_list[num_add].queue_number = 0;
2421 			/* set invalid match method for later detection */
2422 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2423 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2424 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2425 			num_add++;
2426 
2427 			/* flush a full buffer */
2428 			if (num_add == filter_list_len) {
2429 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2430 						     add_head, num_add);
2431 				memset(add_list, 0, list_size);
2432 				num_add = 0;
2433 			}
2434 		}
2435 		if (num_add) {
2436 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2437 					     num_add);
2438 		}
2439 		/* Now move all of the filters from the temp add list back to
2440 		 * the VSI's list.
2441 		 */
2442 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2443 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2444 			/* Only update the state if we're still NEW */
2445 			if (new->f->state == I40E_FILTER_NEW)
2446 				new->f->state = new->state;
2447 			hlist_del(&new->hlist);
2448 			kfree(new);
2449 		}
2450 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2451 		kfree(add_list);
2452 		add_list = NULL;
2453 	}
2454 
2455 	/* Determine the number of active and failed filters. */
2456 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2457 	vsi->active_filters = 0;
2458 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2459 		if (f->state == I40E_FILTER_ACTIVE)
2460 			vsi->active_filters++;
2461 		else if (f->state == I40E_FILTER_FAILED)
2462 			failed_filters++;
2463 	}
2464 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2465 
2466 	/* Check if we are able to exit overflow promiscuous mode. We can
2467 	 * safely exit if we didn't just enter, we no longer have any failed
2468 	 * filters, and we have reduced filters below the threshold value.
2469 	 */
2470 	if (old_overflow && !failed_filters &&
2471 	    vsi->active_filters < vsi->promisc_threshold) {
2472 		dev_info(&pf->pdev->dev,
2473 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2474 			 vsi_name);
2475 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2476 		vsi->promisc_threshold = 0;
2477 	}
2478 
2479 	/* if the VF is not trusted do not do promisc */
2480 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2481 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2482 		goto out;
2483 	}
2484 
2485 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2486 
2487 	/* If we are entering overflow promiscuous, we need to calculate a new
2488 	 * threshold for when we are safe to exit
2489 	 */
2490 	if (!old_overflow && new_overflow)
2491 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2492 
2493 	/* check for changes in promiscuous modes */
2494 	if (changed_flags & IFF_ALLMULTI) {
2495 		bool cur_multipromisc;
2496 
2497 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2498 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2499 							       vsi->seid,
2500 							       cur_multipromisc,
2501 							       NULL);
2502 		if (aq_ret) {
2503 			retval = i40e_aq_rc_to_posix(aq_ret,
2504 						     hw->aq.asq_last_status);
2505 			dev_info(&pf->pdev->dev,
2506 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2507 				 vsi_name,
2508 				 i40e_stat_str(hw, aq_ret),
2509 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2510 		}
2511 	}
2512 
2513 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2514 		bool cur_promisc;
2515 
2516 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2517 			       new_overflow);
2518 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2519 		if (aq_ret) {
2520 			retval = i40e_aq_rc_to_posix(aq_ret,
2521 						     hw->aq.asq_last_status);
2522 			dev_info(&pf->pdev->dev,
2523 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2524 				 cur_promisc ? "on" : "off",
2525 				 vsi_name,
2526 				 i40e_stat_str(hw, aq_ret),
2527 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2528 		}
2529 	}
2530 out:
2531 	/* if something went wrong then set the changed flag so we try again */
2532 	if (retval)
2533 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2534 
2535 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2536 	return retval;
2537 
2538 err_no_memory:
2539 	/* Restore elements on the temporary add and delete lists */
2540 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2541 err_no_memory_locked:
2542 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2543 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2544 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2545 
2546 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2547 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2548 	return -ENOMEM;
2549 }
2550 
2551 /**
2552  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2553  * @pf: board private structure
2554  **/
2555 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2556 {
2557 	int v;
2558 
2559 	if (!pf)
2560 		return;
2561 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2562 		return;
2563 
2564 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2565 		if (pf->vsi[v] &&
2566 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2567 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2568 
2569 			if (ret) {
2570 				/* come back and try again later */
2571 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2572 					pf->state);
2573 				break;
2574 			}
2575 		}
2576 	}
2577 }
2578 
2579 /**
2580  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2581  * @vsi: the vsi
2582  **/
2583 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2584 {
2585 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2586 		return I40E_RXBUFFER_2048;
2587 	else
2588 		return I40E_RXBUFFER_3072;
2589 }
2590 
2591 /**
2592  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2593  * @netdev: network interface device structure
2594  * @new_mtu: new value for maximum frame size
2595  *
2596  * Returns 0 on success, negative on failure
2597  **/
2598 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2599 {
2600 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2601 	struct i40e_vsi *vsi = np->vsi;
2602 	struct i40e_pf *pf = vsi->back;
2603 
2604 	if (i40e_enabled_xdp_vsi(vsi)) {
2605 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2606 
2607 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2608 			return -EINVAL;
2609 	}
2610 
2611 	netdev_info(netdev, "changing MTU from %d to %d\n",
2612 		    netdev->mtu, new_mtu);
2613 	netdev->mtu = new_mtu;
2614 	if (netif_running(netdev))
2615 		i40e_vsi_reinit_locked(vsi);
2616 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2617 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2618 	return 0;
2619 }
2620 
2621 /**
2622  * i40e_ioctl - Access the hwtstamp interface
2623  * @netdev: network interface device structure
2624  * @ifr: interface request data
2625  * @cmd: ioctl command
2626  **/
2627 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2628 {
2629 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2630 	struct i40e_pf *pf = np->vsi->back;
2631 
2632 	switch (cmd) {
2633 	case SIOCGHWTSTAMP:
2634 		return i40e_ptp_get_ts_config(pf, ifr);
2635 	case SIOCSHWTSTAMP:
2636 		return i40e_ptp_set_ts_config(pf, ifr);
2637 	default:
2638 		return -EOPNOTSUPP;
2639 	}
2640 }
2641 
2642 /**
2643  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2644  * @vsi: the vsi being adjusted
2645  **/
2646 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2647 {
2648 	struct i40e_vsi_context ctxt;
2649 	i40e_status ret;
2650 
2651 	if ((vsi->info.valid_sections &
2652 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2653 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2654 		return;  /* already enabled */
2655 
2656 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2657 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2658 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2659 
2660 	ctxt.seid = vsi->seid;
2661 	ctxt.info = vsi->info;
2662 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2663 	if (ret) {
2664 		dev_info(&vsi->back->pdev->dev,
2665 			 "update vlan stripping failed, err %s aq_err %s\n",
2666 			 i40e_stat_str(&vsi->back->hw, ret),
2667 			 i40e_aq_str(&vsi->back->hw,
2668 				     vsi->back->hw.aq.asq_last_status));
2669 	}
2670 }
2671 
2672 /**
2673  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2674  * @vsi: the vsi being adjusted
2675  **/
2676 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2677 {
2678 	struct i40e_vsi_context ctxt;
2679 	i40e_status ret;
2680 
2681 	if ((vsi->info.valid_sections &
2682 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2683 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2684 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2685 		return;  /* already disabled */
2686 
2687 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2688 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2689 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2690 
2691 	ctxt.seid = vsi->seid;
2692 	ctxt.info = vsi->info;
2693 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2694 	if (ret) {
2695 		dev_info(&vsi->back->pdev->dev,
2696 			 "update vlan stripping failed, err %s aq_err %s\n",
2697 			 i40e_stat_str(&vsi->back->hw, ret),
2698 			 i40e_aq_str(&vsi->back->hw,
2699 				     vsi->back->hw.aq.asq_last_status));
2700 	}
2701 }
2702 
2703 /**
2704  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2705  * @vsi: the vsi being configured
2706  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2707  *
2708  * This is a helper function for adding a new MAC/VLAN filter with the
2709  * specified VLAN for each existing MAC address already in the hash table.
2710  * This function does *not* perform any accounting to update filters based on
2711  * VLAN mode.
2712  *
2713  * NOTE: this function expects to be called while under the
2714  * mac_filter_hash_lock
2715  **/
2716 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2717 {
2718 	struct i40e_mac_filter *f, *add_f;
2719 	struct hlist_node *h;
2720 	int bkt;
2721 
2722 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2723 		if (f->state == I40E_FILTER_REMOVE)
2724 			continue;
2725 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2726 		if (!add_f) {
2727 			dev_info(&vsi->back->pdev->dev,
2728 				 "Could not add vlan filter %d for %pM\n",
2729 				 vid, f->macaddr);
2730 			return -ENOMEM;
2731 		}
2732 	}
2733 
2734 	return 0;
2735 }
2736 
2737 /**
2738  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2739  * @vsi: the VSI being configured
2740  * @vid: VLAN id to be added
2741  **/
2742 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2743 {
2744 	int err;
2745 
2746 	if (vsi->info.pvid)
2747 		return -EINVAL;
2748 
2749 	/* The network stack will attempt to add VID=0, with the intention to
2750 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2751 	 * these packets by default when configured to receive untagged
2752 	 * packets, so we don't need to add a filter for this case.
2753 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2754 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2755 	 * Thus, we do not want to actually add a filter for VID=0
2756 	 */
2757 	if (!vid)
2758 		return 0;
2759 
2760 	/* Locked once because all functions invoked below iterates list*/
2761 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2762 	err = i40e_add_vlan_all_mac(vsi, vid);
2763 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2764 	if (err)
2765 		return err;
2766 
2767 	/* schedule our worker thread which will take care of
2768 	 * applying the new filter changes
2769 	 */
2770 	i40e_service_event_schedule(vsi->back);
2771 	return 0;
2772 }
2773 
2774 /**
2775  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2776  * @vsi: the vsi being configured
2777  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2778  *
2779  * This function should be used to remove all VLAN filters which match the
2780  * given VID. It does not schedule the service event and does not take the
2781  * mac_filter_hash_lock so it may be combined with other operations under
2782  * a single invocation of the mac_filter_hash_lock.
2783  *
2784  * NOTE: this function expects to be called while under the
2785  * mac_filter_hash_lock
2786  */
2787 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2788 {
2789 	struct i40e_mac_filter *f;
2790 	struct hlist_node *h;
2791 	int bkt;
2792 
2793 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2794 		if (f->vlan == vid)
2795 			__i40e_del_filter(vsi, f);
2796 	}
2797 }
2798 
2799 /**
2800  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2801  * @vsi: the VSI being configured
2802  * @vid: VLAN id to be removed
2803  **/
2804 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2805 {
2806 	if (!vid || vsi->info.pvid)
2807 		return;
2808 
2809 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2810 	i40e_rm_vlan_all_mac(vsi, vid);
2811 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2812 
2813 	/* schedule our worker thread which will take care of
2814 	 * applying the new filter changes
2815 	 */
2816 	i40e_service_event_schedule(vsi->back);
2817 }
2818 
2819 /**
2820  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2821  * @netdev: network interface to be adjusted
2822  * @proto: unused protocol value
2823  * @vid: vlan id to be added
2824  *
2825  * net_device_ops implementation for adding vlan ids
2826  **/
2827 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2828 				__always_unused __be16 proto, u16 vid)
2829 {
2830 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2831 	struct i40e_vsi *vsi = np->vsi;
2832 	int ret = 0;
2833 
2834 	if (vid >= VLAN_N_VID)
2835 		return -EINVAL;
2836 
2837 	ret = i40e_vsi_add_vlan(vsi, vid);
2838 	if (!ret)
2839 		set_bit(vid, vsi->active_vlans);
2840 
2841 	return ret;
2842 }
2843 
2844 /**
2845  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2846  * @netdev: network interface to be adjusted
2847  * @proto: unused protocol value
2848  * @vid: vlan id to be added
2849  **/
2850 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2851 				    __always_unused __be16 proto, u16 vid)
2852 {
2853 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2854 	struct i40e_vsi *vsi = np->vsi;
2855 
2856 	if (vid >= VLAN_N_VID)
2857 		return;
2858 	set_bit(vid, vsi->active_vlans);
2859 }
2860 
2861 /**
2862  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2863  * @netdev: network interface to be adjusted
2864  * @proto: unused protocol value
2865  * @vid: vlan id to be removed
2866  *
2867  * net_device_ops implementation for removing vlan ids
2868  **/
2869 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2870 				 __always_unused __be16 proto, u16 vid)
2871 {
2872 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2873 	struct i40e_vsi *vsi = np->vsi;
2874 
2875 	/* return code is ignored as there is nothing a user
2876 	 * can do about failure to remove and a log message was
2877 	 * already printed from the other function
2878 	 */
2879 	i40e_vsi_kill_vlan(vsi, vid);
2880 
2881 	clear_bit(vid, vsi->active_vlans);
2882 
2883 	return 0;
2884 }
2885 
2886 /**
2887  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2888  * @vsi: the vsi being brought back up
2889  **/
2890 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2891 {
2892 	u16 vid;
2893 
2894 	if (!vsi->netdev)
2895 		return;
2896 
2897 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2898 		i40e_vlan_stripping_enable(vsi);
2899 	else
2900 		i40e_vlan_stripping_disable(vsi);
2901 
2902 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2903 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2904 					vid);
2905 }
2906 
2907 /**
2908  * i40e_vsi_add_pvid - Add pvid for the VSI
2909  * @vsi: the vsi being adjusted
2910  * @vid: the vlan id to set as a PVID
2911  **/
2912 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2913 {
2914 	struct i40e_vsi_context ctxt;
2915 	i40e_status ret;
2916 
2917 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2918 	vsi->info.pvid = cpu_to_le16(vid);
2919 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2920 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
2921 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
2922 
2923 	ctxt.seid = vsi->seid;
2924 	ctxt.info = vsi->info;
2925 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2926 	if (ret) {
2927 		dev_info(&vsi->back->pdev->dev,
2928 			 "add pvid failed, err %s aq_err %s\n",
2929 			 i40e_stat_str(&vsi->back->hw, ret),
2930 			 i40e_aq_str(&vsi->back->hw,
2931 				     vsi->back->hw.aq.asq_last_status));
2932 		return -ENOENT;
2933 	}
2934 
2935 	return 0;
2936 }
2937 
2938 /**
2939  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2940  * @vsi: the vsi being adjusted
2941  *
2942  * Just use the vlan_rx_register() service to put it back to normal
2943  **/
2944 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2945 {
2946 	i40e_vlan_stripping_disable(vsi);
2947 
2948 	vsi->info.pvid = 0;
2949 }
2950 
2951 /**
2952  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2953  * @vsi: ptr to the VSI
2954  *
2955  * If this function returns with an error, then it's possible one or
2956  * more of the rings is populated (while the rest are not).  It is the
2957  * callers duty to clean those orphaned rings.
2958  *
2959  * Return 0 on success, negative on failure
2960  **/
2961 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2962 {
2963 	int i, err = 0;
2964 
2965 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2966 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2967 
2968 	if (!i40e_enabled_xdp_vsi(vsi))
2969 		return err;
2970 
2971 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2972 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2973 
2974 	return err;
2975 }
2976 
2977 /**
2978  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2979  * @vsi: ptr to the VSI
2980  *
2981  * Free VSI's transmit software resources
2982  **/
2983 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2984 {
2985 	int i;
2986 
2987 	if (vsi->tx_rings) {
2988 		for (i = 0; i < vsi->num_queue_pairs; i++)
2989 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2990 				i40e_free_tx_resources(vsi->tx_rings[i]);
2991 	}
2992 
2993 	if (vsi->xdp_rings) {
2994 		for (i = 0; i < vsi->num_queue_pairs; i++)
2995 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2996 				i40e_free_tx_resources(vsi->xdp_rings[i]);
2997 	}
2998 }
2999 
3000 /**
3001  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3002  * @vsi: ptr to the VSI
3003  *
3004  * If this function returns with an error, then it's possible one or
3005  * more of the rings is populated (while the rest are not).  It is the
3006  * callers duty to clean those orphaned rings.
3007  *
3008  * Return 0 on success, negative on failure
3009  **/
3010 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3011 {
3012 	int i, err = 0;
3013 
3014 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3015 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3016 	return err;
3017 }
3018 
3019 /**
3020  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3021  * @vsi: ptr to the VSI
3022  *
3023  * Free all receive software resources
3024  **/
3025 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3026 {
3027 	int i;
3028 
3029 	if (!vsi->rx_rings)
3030 		return;
3031 
3032 	for (i = 0; i < vsi->num_queue_pairs; i++)
3033 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3034 			i40e_free_rx_resources(vsi->rx_rings[i]);
3035 }
3036 
3037 /**
3038  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3039  * @ring: The Tx ring to configure
3040  *
3041  * This enables/disables XPS for a given Tx descriptor ring
3042  * based on the TCs enabled for the VSI that ring belongs to.
3043  **/
3044 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3045 {
3046 	int cpu;
3047 
3048 	if (!ring->q_vector || !ring->netdev || ring->ch)
3049 		return;
3050 
3051 	/* We only initialize XPS once, so as not to overwrite user settings */
3052 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3053 		return;
3054 
3055 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3056 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3057 			    ring->queue_index);
3058 }
3059 
3060 /**
3061  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3062  * @ring: The Tx ring to configure
3063  *
3064  * Configure the Tx descriptor ring in the HMC context.
3065  **/
3066 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3067 {
3068 	struct i40e_vsi *vsi = ring->vsi;
3069 	u16 pf_q = vsi->base_queue + ring->queue_index;
3070 	struct i40e_hw *hw = &vsi->back->hw;
3071 	struct i40e_hmc_obj_txq tx_ctx;
3072 	i40e_status err = 0;
3073 	u32 qtx_ctl = 0;
3074 
3075 	/* some ATR related tx ring init */
3076 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3077 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3078 		ring->atr_count = 0;
3079 	} else {
3080 		ring->atr_sample_rate = 0;
3081 	}
3082 
3083 	/* configure XPS */
3084 	i40e_config_xps_tx_ring(ring);
3085 
3086 	/* clear the context structure first */
3087 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3088 
3089 	tx_ctx.new_context = 1;
3090 	tx_ctx.base = (ring->dma / 128);
3091 	tx_ctx.qlen = ring->count;
3092 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3093 					       I40E_FLAG_FD_ATR_ENABLED));
3094 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3095 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3096 	if (vsi->type != I40E_VSI_FDIR)
3097 		tx_ctx.head_wb_ena = 1;
3098 	tx_ctx.head_wb_addr = ring->dma +
3099 			      (ring->count * sizeof(struct i40e_tx_desc));
3100 
3101 	/* As part of VSI creation/update, FW allocates certain
3102 	 * Tx arbitration queue sets for each TC enabled for
3103 	 * the VSI. The FW returns the handles to these queue
3104 	 * sets as part of the response buffer to Add VSI,
3105 	 * Update VSI, etc. AQ commands. It is expected that
3106 	 * these queue set handles be associated with the Tx
3107 	 * queues by the driver as part of the TX queue context
3108 	 * initialization. This has to be done regardless of
3109 	 * DCB as by default everything is mapped to TC0.
3110 	 */
3111 
3112 	if (ring->ch)
3113 		tx_ctx.rdylist =
3114 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3115 
3116 	else
3117 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3118 
3119 	tx_ctx.rdylist_act = 0;
3120 
3121 	/* clear the context in the HMC */
3122 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3123 	if (err) {
3124 		dev_info(&vsi->back->pdev->dev,
3125 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3126 			 ring->queue_index, pf_q, err);
3127 		return -ENOMEM;
3128 	}
3129 
3130 	/* set the context in the HMC */
3131 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3132 	if (err) {
3133 		dev_info(&vsi->back->pdev->dev,
3134 			 "Failed to set 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 	/* Now associate this queue with this PCI function */
3140 	if (ring->ch) {
3141 		if (ring->ch->type == I40E_VSI_VMDQ2)
3142 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3143 		else
3144 			return -EINVAL;
3145 
3146 		qtx_ctl |= (ring->ch->vsi_number <<
3147 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3148 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3149 	} else {
3150 		if (vsi->type == I40E_VSI_VMDQ2) {
3151 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3152 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3153 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3154 		} else {
3155 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3156 		}
3157 	}
3158 
3159 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3160 		    I40E_QTX_CTL_PF_INDX_MASK);
3161 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3162 	i40e_flush(hw);
3163 
3164 	/* cache tail off for easier writes later */
3165 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3166 
3167 	return 0;
3168 }
3169 
3170 /**
3171  * i40e_configure_rx_ring - Configure a receive ring context
3172  * @ring: The Rx ring to configure
3173  *
3174  * Configure the Rx descriptor ring in the HMC context.
3175  **/
3176 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3177 {
3178 	struct i40e_vsi *vsi = ring->vsi;
3179 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3180 	u16 pf_q = vsi->base_queue + ring->queue_index;
3181 	struct i40e_hw *hw = &vsi->back->hw;
3182 	struct i40e_hmc_obj_rxq rx_ctx;
3183 	i40e_status err = 0;
3184 
3185 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3186 
3187 	/* clear the context structure first */
3188 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3189 
3190 	ring->rx_buf_len = vsi->rx_buf_len;
3191 
3192 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3193 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3194 
3195 	rx_ctx.base = (ring->dma / 128);
3196 	rx_ctx.qlen = ring->count;
3197 
3198 	/* use 32 byte descriptors */
3199 	rx_ctx.dsize = 1;
3200 
3201 	/* descriptor type is always zero
3202 	 * rx_ctx.dtype = 0;
3203 	 */
3204 	rx_ctx.hsplit_0 = 0;
3205 
3206 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3207 	if (hw->revision_id == 0)
3208 		rx_ctx.lrxqthresh = 0;
3209 	else
3210 		rx_ctx.lrxqthresh = 1;
3211 	rx_ctx.crcstrip = 1;
3212 	rx_ctx.l2tsel = 1;
3213 	/* this controls whether VLAN is stripped from inner headers */
3214 	rx_ctx.showiv = 0;
3215 	/* set the prefena field to 1 because the manual says to */
3216 	rx_ctx.prefena = 1;
3217 
3218 	/* clear the context in the HMC */
3219 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3220 	if (err) {
3221 		dev_info(&vsi->back->pdev->dev,
3222 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3223 			 ring->queue_index, pf_q, err);
3224 		return -ENOMEM;
3225 	}
3226 
3227 	/* set the context in the HMC */
3228 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3229 	if (err) {
3230 		dev_info(&vsi->back->pdev->dev,
3231 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3232 			 ring->queue_index, pf_q, err);
3233 		return -ENOMEM;
3234 	}
3235 
3236 	/* configure Rx buffer alignment */
3237 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3238 		clear_ring_build_skb_enabled(ring);
3239 	else
3240 		set_ring_build_skb_enabled(ring);
3241 
3242 	/* cache tail for quicker writes, and clear the reg before use */
3243 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3244 	writel(0, ring->tail);
3245 
3246 	i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3247 
3248 	return 0;
3249 }
3250 
3251 /**
3252  * i40e_vsi_configure_tx - Configure the VSI for Tx
3253  * @vsi: VSI structure describing this set of rings and resources
3254  *
3255  * Configure the Tx VSI for operation.
3256  **/
3257 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3258 {
3259 	int err = 0;
3260 	u16 i;
3261 
3262 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3263 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3264 
3265 	if (!i40e_enabled_xdp_vsi(vsi))
3266 		return err;
3267 
3268 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3269 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3270 
3271 	return err;
3272 }
3273 
3274 /**
3275  * i40e_vsi_configure_rx - Configure the VSI for Rx
3276  * @vsi: the VSI being configured
3277  *
3278  * Configure the Rx VSI for operation.
3279  **/
3280 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3281 {
3282 	int err = 0;
3283 	u16 i;
3284 
3285 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3286 		vsi->max_frame = I40E_MAX_RXBUFFER;
3287 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3288 #if (PAGE_SIZE < 8192)
3289 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3290 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3291 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3292 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3293 #endif
3294 	} else {
3295 		vsi->max_frame = I40E_MAX_RXBUFFER;
3296 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3297 						       I40E_RXBUFFER_2048;
3298 	}
3299 
3300 	/* set up individual rings */
3301 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3302 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3303 
3304 	return err;
3305 }
3306 
3307 /**
3308  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3309  * @vsi: ptr to the VSI
3310  **/
3311 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3312 {
3313 	struct i40e_ring *tx_ring, *rx_ring;
3314 	u16 qoffset, qcount;
3315 	int i, n;
3316 
3317 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3318 		/* Reset the TC information */
3319 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3320 			rx_ring = vsi->rx_rings[i];
3321 			tx_ring = vsi->tx_rings[i];
3322 			rx_ring->dcb_tc = 0;
3323 			tx_ring->dcb_tc = 0;
3324 		}
3325 		return;
3326 	}
3327 
3328 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3329 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3330 			continue;
3331 
3332 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3333 		qcount = vsi->tc_config.tc_info[n].qcount;
3334 		for (i = qoffset; i < (qoffset + qcount); i++) {
3335 			rx_ring = vsi->rx_rings[i];
3336 			tx_ring = vsi->tx_rings[i];
3337 			rx_ring->dcb_tc = n;
3338 			tx_ring->dcb_tc = n;
3339 		}
3340 	}
3341 }
3342 
3343 /**
3344  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3345  * @vsi: ptr to the VSI
3346  **/
3347 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3348 {
3349 	if (vsi->netdev)
3350 		i40e_set_rx_mode(vsi->netdev);
3351 }
3352 
3353 /**
3354  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3355  * @vsi: Pointer to the targeted VSI
3356  *
3357  * This function replays the hlist on the hw where all the SB Flow Director
3358  * filters were saved.
3359  **/
3360 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3361 {
3362 	struct i40e_fdir_filter *filter;
3363 	struct i40e_pf *pf = vsi->back;
3364 	struct hlist_node *node;
3365 
3366 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3367 		return;
3368 
3369 	/* Reset FDir counters as we're replaying all existing filters */
3370 	pf->fd_tcp4_filter_cnt = 0;
3371 	pf->fd_udp4_filter_cnt = 0;
3372 	pf->fd_sctp4_filter_cnt = 0;
3373 	pf->fd_ip4_filter_cnt = 0;
3374 
3375 	hlist_for_each_entry_safe(filter, node,
3376 				  &pf->fdir_filter_list, fdir_node) {
3377 		i40e_add_del_fdir(vsi, filter, true);
3378 	}
3379 }
3380 
3381 /**
3382  * i40e_vsi_configure - Set up the VSI for action
3383  * @vsi: the VSI being configured
3384  **/
3385 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3386 {
3387 	int err;
3388 
3389 	i40e_set_vsi_rx_mode(vsi);
3390 	i40e_restore_vlan(vsi);
3391 	i40e_vsi_config_dcb_rings(vsi);
3392 	err = i40e_vsi_configure_tx(vsi);
3393 	if (!err)
3394 		err = i40e_vsi_configure_rx(vsi);
3395 
3396 	return err;
3397 }
3398 
3399 /**
3400  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3401  * @vsi: the VSI being configured
3402  **/
3403 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3404 {
3405 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3406 	struct i40e_pf *pf = vsi->back;
3407 	struct i40e_hw *hw = &pf->hw;
3408 	u16 vector;
3409 	int i, q;
3410 	u32 qp;
3411 
3412 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3413 	 * and PFINT_LNKLSTn registers, e.g.:
3414 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3415 	 */
3416 	qp = vsi->base_queue;
3417 	vector = vsi->base_vector;
3418 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3419 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3420 
3421 		q_vector->rx.next_update = jiffies + 1;
3422 		q_vector->rx.target_itr =
3423 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3424 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3425 		     q_vector->rx.target_itr);
3426 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3427 
3428 		q_vector->tx.next_update = jiffies + 1;
3429 		q_vector->tx.target_itr =
3430 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3431 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3432 		     q_vector->tx.target_itr);
3433 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3434 
3435 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3436 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3437 
3438 		/* Linked list for the queuepairs assigned to this vector */
3439 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3440 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3441 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3442 			u32 val;
3443 
3444 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3445 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3446 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3447 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3448 			      (I40E_QUEUE_TYPE_TX <<
3449 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3450 
3451 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3452 
3453 			if (has_xdp) {
3454 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3455 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3456 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3457 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3458 				      (I40E_QUEUE_TYPE_TX <<
3459 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3460 
3461 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3462 			}
3463 
3464 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3465 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3466 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3467 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3468 			      (I40E_QUEUE_TYPE_RX <<
3469 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3470 
3471 			/* Terminate the linked list */
3472 			if (q == (q_vector->num_ringpairs - 1))
3473 				val |= (I40E_QUEUE_END_OF_LIST <<
3474 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3475 
3476 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3477 			qp++;
3478 		}
3479 	}
3480 
3481 	i40e_flush(hw);
3482 }
3483 
3484 /**
3485  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3486  * @pf: pointer to private device data structure
3487  **/
3488 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3489 {
3490 	struct i40e_hw *hw = &pf->hw;
3491 	u32 val;
3492 
3493 	/* clear things first */
3494 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3495 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3496 
3497 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3498 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3499 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3500 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3501 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3502 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3503 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3504 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3505 
3506 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3507 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3508 
3509 	if (pf->flags & I40E_FLAG_PTP)
3510 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3511 
3512 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3513 
3514 	/* SW_ITR_IDX = 0, but don't change INTENA */
3515 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3516 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3517 
3518 	/* OTHER_ITR_IDX = 0 */
3519 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3520 }
3521 
3522 /**
3523  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3524  * @vsi: the VSI being configured
3525  **/
3526 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3527 {
3528 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3529 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3530 	struct i40e_pf *pf = vsi->back;
3531 	struct i40e_hw *hw = &pf->hw;
3532 	u32 val;
3533 
3534 	/* set the ITR configuration */
3535 	q_vector->rx.next_update = jiffies + 1;
3536 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3537 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3538 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3539 	q_vector->tx.next_update = jiffies + 1;
3540 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3541 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3542 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3543 
3544 	i40e_enable_misc_int_causes(pf);
3545 
3546 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3547 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3548 
3549 	/* Associate the queue pair to the vector and enable the queue int */
3550 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3551 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3552 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3553 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3554 
3555 	wr32(hw, I40E_QINT_RQCTL(0), val);
3556 
3557 	if (i40e_enabled_xdp_vsi(vsi)) {
3558 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3559 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3560 		      (I40E_QUEUE_TYPE_TX
3561 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3562 
3563 	       wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3564 	}
3565 
3566 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3567 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3568 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3569 
3570 	wr32(hw, I40E_QINT_TQCTL(0), val);
3571 	i40e_flush(hw);
3572 }
3573 
3574 /**
3575  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3576  * @pf: board private structure
3577  **/
3578 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3579 {
3580 	struct i40e_hw *hw = &pf->hw;
3581 
3582 	wr32(hw, I40E_PFINT_DYN_CTL0,
3583 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3584 	i40e_flush(hw);
3585 }
3586 
3587 /**
3588  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3589  * @pf: board private structure
3590  **/
3591 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3592 {
3593 	struct i40e_hw *hw = &pf->hw;
3594 	u32 val;
3595 
3596 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3597 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3598 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3599 
3600 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3601 	i40e_flush(hw);
3602 }
3603 
3604 /**
3605  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3606  * @irq: interrupt number
3607  * @data: pointer to a q_vector
3608  **/
3609 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3610 {
3611 	struct i40e_q_vector *q_vector = data;
3612 
3613 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3614 		return IRQ_HANDLED;
3615 
3616 	napi_schedule_irqoff(&q_vector->napi);
3617 
3618 	return IRQ_HANDLED;
3619 }
3620 
3621 /**
3622  * i40e_irq_affinity_notify - Callback for affinity changes
3623  * @notify: context as to what irq was changed
3624  * @mask: the new affinity mask
3625  *
3626  * This is a callback function used by the irq_set_affinity_notifier function
3627  * so that we may register to receive changes to the irq affinity masks.
3628  **/
3629 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3630 				     const cpumask_t *mask)
3631 {
3632 	struct i40e_q_vector *q_vector =
3633 		container_of(notify, struct i40e_q_vector, affinity_notify);
3634 
3635 	cpumask_copy(&q_vector->affinity_mask, mask);
3636 }
3637 
3638 /**
3639  * i40e_irq_affinity_release - Callback for affinity notifier release
3640  * @ref: internal core kernel usage
3641  *
3642  * This is a callback function used by the irq_set_affinity_notifier function
3643  * to inform the current notification subscriber that they will no longer
3644  * receive notifications.
3645  **/
3646 static void i40e_irq_affinity_release(struct kref *ref) {}
3647 
3648 /**
3649  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3650  * @vsi: the VSI being configured
3651  * @basename: name for the vector
3652  *
3653  * Allocates MSI-X vectors and requests interrupts from the kernel.
3654  **/
3655 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3656 {
3657 	int q_vectors = vsi->num_q_vectors;
3658 	struct i40e_pf *pf = vsi->back;
3659 	int base = vsi->base_vector;
3660 	int rx_int_idx = 0;
3661 	int tx_int_idx = 0;
3662 	int vector, err;
3663 	int irq_num;
3664 	int cpu;
3665 
3666 	for (vector = 0; vector < q_vectors; vector++) {
3667 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3668 
3669 		irq_num = pf->msix_entries[base + vector].vector;
3670 
3671 		if (q_vector->tx.ring && q_vector->rx.ring) {
3672 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3673 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3674 			tx_int_idx++;
3675 		} else if (q_vector->rx.ring) {
3676 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3677 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3678 		} else if (q_vector->tx.ring) {
3679 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3680 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3681 		} else {
3682 			/* skip this unused q_vector */
3683 			continue;
3684 		}
3685 		err = request_irq(irq_num,
3686 				  vsi->irq_handler,
3687 				  0,
3688 				  q_vector->name,
3689 				  q_vector);
3690 		if (err) {
3691 			dev_info(&pf->pdev->dev,
3692 				 "MSIX request_irq failed, error: %d\n", err);
3693 			goto free_queue_irqs;
3694 		}
3695 
3696 		/* register for affinity change notifications */
3697 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3698 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3699 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3700 		/* Spread affinity hints out across online CPUs.
3701 		 *
3702 		 * get_cpu_mask returns a static constant mask with
3703 		 * a permanent lifetime so it's ok to pass to
3704 		 * irq_set_affinity_hint without making a copy.
3705 		 */
3706 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3707 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3708 	}
3709 
3710 	vsi->irqs_ready = true;
3711 	return 0;
3712 
3713 free_queue_irqs:
3714 	while (vector) {
3715 		vector--;
3716 		irq_num = pf->msix_entries[base + vector].vector;
3717 		irq_set_affinity_notifier(irq_num, NULL);
3718 		irq_set_affinity_hint(irq_num, NULL);
3719 		free_irq(irq_num, &vsi->q_vectors[vector]);
3720 	}
3721 	return err;
3722 }
3723 
3724 /**
3725  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3726  * @vsi: the VSI being un-configured
3727  **/
3728 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3729 {
3730 	struct i40e_pf *pf = vsi->back;
3731 	struct i40e_hw *hw = &pf->hw;
3732 	int base = vsi->base_vector;
3733 	int i;
3734 
3735 	/* disable interrupt causation from each queue */
3736 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3737 		u32 val;
3738 
3739 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3740 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3741 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3742 
3743 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3744 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3745 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3746 
3747 		if (!i40e_enabled_xdp_vsi(vsi))
3748 			continue;
3749 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3750 	}
3751 
3752 	/* disable each interrupt */
3753 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3754 		for (i = vsi->base_vector;
3755 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3756 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3757 
3758 		i40e_flush(hw);
3759 		for (i = 0; i < vsi->num_q_vectors; i++)
3760 			synchronize_irq(pf->msix_entries[i + base].vector);
3761 	} else {
3762 		/* Legacy and MSI mode - this stops all interrupt handling */
3763 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3764 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3765 		i40e_flush(hw);
3766 		synchronize_irq(pf->pdev->irq);
3767 	}
3768 }
3769 
3770 /**
3771  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3772  * @vsi: the VSI being configured
3773  **/
3774 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3775 {
3776 	struct i40e_pf *pf = vsi->back;
3777 	int i;
3778 
3779 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3780 		for (i = 0; i < vsi->num_q_vectors; i++)
3781 			i40e_irq_dynamic_enable(vsi, i);
3782 	} else {
3783 		i40e_irq_dynamic_enable_icr0(pf);
3784 	}
3785 
3786 	i40e_flush(&pf->hw);
3787 	return 0;
3788 }
3789 
3790 /**
3791  * i40e_free_misc_vector - Free the vector that handles non-queue events
3792  * @pf: board private structure
3793  **/
3794 static void i40e_free_misc_vector(struct i40e_pf *pf)
3795 {
3796 	/* Disable ICR 0 */
3797 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3798 	i40e_flush(&pf->hw);
3799 
3800 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3801 		synchronize_irq(pf->msix_entries[0].vector);
3802 		free_irq(pf->msix_entries[0].vector, pf);
3803 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3804 	}
3805 }
3806 
3807 /**
3808  * i40e_intr - MSI/Legacy and non-queue interrupt handler
3809  * @irq: interrupt number
3810  * @data: pointer to a q_vector
3811  *
3812  * This is the handler used for all MSI/Legacy interrupts, and deals
3813  * with both queue and non-queue interrupts.  This is also used in
3814  * MSIX mode to handle the non-queue interrupts.
3815  **/
3816 static irqreturn_t i40e_intr(int irq, void *data)
3817 {
3818 	struct i40e_pf *pf = (struct i40e_pf *)data;
3819 	struct i40e_hw *hw = &pf->hw;
3820 	irqreturn_t ret = IRQ_NONE;
3821 	u32 icr0, icr0_remaining;
3822 	u32 val, ena_mask;
3823 
3824 	icr0 = rd32(hw, I40E_PFINT_ICR0);
3825 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3826 
3827 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
3828 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3829 		goto enable_intr;
3830 
3831 	/* if interrupt but no bits showing, must be SWINT */
3832 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3833 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3834 		pf->sw_int_count++;
3835 
3836 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3837 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3838 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3839 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3840 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3841 	}
3842 
3843 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3844 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3845 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3846 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3847 
3848 		/* We do not have a way to disarm Queue causes while leaving
3849 		 * interrupt enabled for all other causes, ideally
3850 		 * interrupt should be disabled while we are in NAPI but
3851 		 * this is not a performance path and napi_schedule()
3852 		 * can deal with rescheduling.
3853 		 */
3854 		if (!test_bit(__I40E_DOWN, pf->state))
3855 			napi_schedule_irqoff(&q_vector->napi);
3856 	}
3857 
3858 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3859 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3860 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3861 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3862 	}
3863 
3864 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3865 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3866 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3867 	}
3868 
3869 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3870 		ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3871 		set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3872 	}
3873 
3874 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3875 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3876 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3877 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3878 		val = rd32(hw, I40E_GLGEN_RSTAT);
3879 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3880 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3881 		if (val == I40E_RESET_CORER) {
3882 			pf->corer_count++;
3883 		} else if (val == I40E_RESET_GLOBR) {
3884 			pf->globr_count++;
3885 		} else if (val == I40E_RESET_EMPR) {
3886 			pf->empr_count++;
3887 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3888 		}
3889 	}
3890 
3891 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3892 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3893 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3894 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3895 			 rd32(hw, I40E_PFHMC_ERRORINFO),
3896 			 rd32(hw, I40E_PFHMC_ERRORDATA));
3897 	}
3898 
3899 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3900 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3901 
3902 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
3903 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3904 			i40e_ptp_tx_hwtstamp(pf);
3905 		}
3906 	}
3907 
3908 	/* If a critical error is pending we have no choice but to reset the
3909 	 * device.
3910 	 * Report and mask out any remaining unexpected interrupts.
3911 	 */
3912 	icr0_remaining = icr0 & ena_mask;
3913 	if (icr0_remaining) {
3914 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3915 			 icr0_remaining);
3916 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
3917 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
3918 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
3919 			dev_info(&pf->pdev->dev, "device will be reset\n");
3920 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
3921 			i40e_service_event_schedule(pf);
3922 		}
3923 		ena_mask &= ~icr0_remaining;
3924 	}
3925 	ret = IRQ_HANDLED;
3926 
3927 enable_intr:
3928 	/* re-enable interrupt causes */
3929 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
3930 	if (!test_bit(__I40E_DOWN, pf->state)) {
3931 		i40e_service_event_schedule(pf);
3932 		i40e_irq_dynamic_enable_icr0(pf);
3933 	}
3934 
3935 	return ret;
3936 }
3937 
3938 /**
3939  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3940  * @tx_ring:  tx ring to clean
3941  * @budget:   how many cleans we're allowed
3942  *
3943  * Returns true if there's any budget left (e.g. the clean is finished)
3944  **/
3945 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3946 {
3947 	struct i40e_vsi *vsi = tx_ring->vsi;
3948 	u16 i = tx_ring->next_to_clean;
3949 	struct i40e_tx_buffer *tx_buf;
3950 	struct i40e_tx_desc *tx_desc;
3951 
3952 	tx_buf = &tx_ring->tx_bi[i];
3953 	tx_desc = I40E_TX_DESC(tx_ring, i);
3954 	i -= tx_ring->count;
3955 
3956 	do {
3957 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3958 
3959 		/* if next_to_watch is not set then there is no work pending */
3960 		if (!eop_desc)
3961 			break;
3962 
3963 		/* prevent any other reads prior to eop_desc */
3964 		smp_rmb();
3965 
3966 		/* if the descriptor isn't done, no work yet to do */
3967 		if (!(eop_desc->cmd_type_offset_bsz &
3968 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3969 			break;
3970 
3971 		/* clear next_to_watch to prevent false hangs */
3972 		tx_buf->next_to_watch = NULL;
3973 
3974 		tx_desc->buffer_addr = 0;
3975 		tx_desc->cmd_type_offset_bsz = 0;
3976 		/* move past filter desc */
3977 		tx_buf++;
3978 		tx_desc++;
3979 		i++;
3980 		if (unlikely(!i)) {
3981 			i -= tx_ring->count;
3982 			tx_buf = tx_ring->tx_bi;
3983 			tx_desc = I40E_TX_DESC(tx_ring, 0);
3984 		}
3985 		/* unmap skb header data */
3986 		dma_unmap_single(tx_ring->dev,
3987 				 dma_unmap_addr(tx_buf, dma),
3988 				 dma_unmap_len(tx_buf, len),
3989 				 DMA_TO_DEVICE);
3990 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3991 			kfree(tx_buf->raw_buf);
3992 
3993 		tx_buf->raw_buf = NULL;
3994 		tx_buf->tx_flags = 0;
3995 		tx_buf->next_to_watch = NULL;
3996 		dma_unmap_len_set(tx_buf, len, 0);
3997 		tx_desc->buffer_addr = 0;
3998 		tx_desc->cmd_type_offset_bsz = 0;
3999 
4000 		/* move us past the eop_desc for start of next FD desc */
4001 		tx_buf++;
4002 		tx_desc++;
4003 		i++;
4004 		if (unlikely(!i)) {
4005 			i -= tx_ring->count;
4006 			tx_buf = tx_ring->tx_bi;
4007 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4008 		}
4009 
4010 		/* update budget accounting */
4011 		budget--;
4012 	} while (likely(budget));
4013 
4014 	i += tx_ring->count;
4015 	tx_ring->next_to_clean = i;
4016 
4017 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4018 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4019 
4020 	return budget > 0;
4021 }
4022 
4023 /**
4024  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4025  * @irq: interrupt number
4026  * @data: pointer to a q_vector
4027  **/
4028 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4029 {
4030 	struct i40e_q_vector *q_vector = data;
4031 	struct i40e_vsi *vsi;
4032 
4033 	if (!q_vector->tx.ring)
4034 		return IRQ_HANDLED;
4035 
4036 	vsi = q_vector->tx.ring->vsi;
4037 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4038 
4039 	return IRQ_HANDLED;
4040 }
4041 
4042 /**
4043  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4044  * @vsi: the VSI being configured
4045  * @v_idx: vector index
4046  * @qp_idx: queue pair index
4047  **/
4048 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4049 {
4050 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4051 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4052 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4053 
4054 	tx_ring->q_vector = q_vector;
4055 	tx_ring->next = q_vector->tx.ring;
4056 	q_vector->tx.ring = tx_ring;
4057 	q_vector->tx.count++;
4058 
4059 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4060 	if (i40e_enabled_xdp_vsi(vsi)) {
4061 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4062 
4063 		xdp_ring->q_vector = q_vector;
4064 		xdp_ring->next = q_vector->tx.ring;
4065 		q_vector->tx.ring = xdp_ring;
4066 		q_vector->tx.count++;
4067 	}
4068 
4069 	rx_ring->q_vector = q_vector;
4070 	rx_ring->next = q_vector->rx.ring;
4071 	q_vector->rx.ring = rx_ring;
4072 	q_vector->rx.count++;
4073 }
4074 
4075 /**
4076  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4077  * @vsi: the VSI being configured
4078  *
4079  * This function maps descriptor rings to the queue-specific vectors
4080  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4081  * one vector per queue pair, but on a constrained vector budget, we
4082  * group the queue pairs as "efficiently" as possible.
4083  **/
4084 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4085 {
4086 	int qp_remaining = vsi->num_queue_pairs;
4087 	int q_vectors = vsi->num_q_vectors;
4088 	int num_ringpairs;
4089 	int v_start = 0;
4090 	int qp_idx = 0;
4091 
4092 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4093 	 * group them so there are multiple queues per vector.
4094 	 * It is also important to go through all the vectors available to be
4095 	 * sure that if we don't use all the vectors, that the remaining vectors
4096 	 * are cleared. This is especially important when decreasing the
4097 	 * number of queues in use.
4098 	 */
4099 	for (; v_start < q_vectors; v_start++) {
4100 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4101 
4102 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4103 
4104 		q_vector->num_ringpairs = num_ringpairs;
4105 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4106 
4107 		q_vector->rx.count = 0;
4108 		q_vector->tx.count = 0;
4109 		q_vector->rx.ring = NULL;
4110 		q_vector->tx.ring = NULL;
4111 
4112 		while (num_ringpairs--) {
4113 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4114 			qp_idx++;
4115 			qp_remaining--;
4116 		}
4117 	}
4118 }
4119 
4120 /**
4121  * i40e_vsi_request_irq - Request IRQ from the OS
4122  * @vsi: the VSI being configured
4123  * @basename: name for the vector
4124  **/
4125 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4126 {
4127 	struct i40e_pf *pf = vsi->back;
4128 	int err;
4129 
4130 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4131 		err = i40e_vsi_request_irq_msix(vsi, basename);
4132 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4133 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4134 				  pf->int_name, pf);
4135 	else
4136 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4137 				  pf->int_name, pf);
4138 
4139 	if (err)
4140 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4141 
4142 	return err;
4143 }
4144 
4145 #ifdef CONFIG_NET_POLL_CONTROLLER
4146 /**
4147  * i40e_netpoll - A Polling 'interrupt' handler
4148  * @netdev: network interface device structure
4149  *
4150  * This is used by netconsole to send skbs without having to re-enable
4151  * interrupts.  It's not called while the normal interrupt routine is executing.
4152  **/
4153 static void i40e_netpoll(struct net_device *netdev)
4154 {
4155 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4156 	struct i40e_vsi *vsi = np->vsi;
4157 	struct i40e_pf *pf = vsi->back;
4158 	int i;
4159 
4160 	/* if interface is down do nothing */
4161 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4162 		return;
4163 
4164 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4165 		for (i = 0; i < vsi->num_q_vectors; i++)
4166 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4167 	} else {
4168 		i40e_intr(pf->pdev->irq, netdev);
4169 	}
4170 }
4171 #endif
4172 
4173 #define I40E_QTX_ENA_WAIT_COUNT 50
4174 
4175 /**
4176  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4177  * @pf: the PF being configured
4178  * @pf_q: the PF queue
4179  * @enable: enable or disable state of the queue
4180  *
4181  * This routine will wait for the given Tx queue of the PF to reach the
4182  * enabled or disabled state.
4183  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4184  * multiple retries; else will return 0 in case of success.
4185  **/
4186 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4187 {
4188 	int i;
4189 	u32 tx_reg;
4190 
4191 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4192 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4193 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4194 			break;
4195 
4196 		usleep_range(10, 20);
4197 	}
4198 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4199 		return -ETIMEDOUT;
4200 
4201 	return 0;
4202 }
4203 
4204 /**
4205  * i40e_control_tx_q - Start or stop a particular Tx queue
4206  * @pf: the PF structure
4207  * @pf_q: the PF queue to configure
4208  * @enable: start or stop the queue
4209  *
4210  * This function enables or disables a single queue. Note that any delay
4211  * required after the operation is expected to be handled by the caller of
4212  * this function.
4213  **/
4214 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4215 {
4216 	struct i40e_hw *hw = &pf->hw;
4217 	u32 tx_reg;
4218 	int i;
4219 
4220 	/* warn the TX unit of coming changes */
4221 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4222 	if (!enable)
4223 		usleep_range(10, 20);
4224 
4225 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4226 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4227 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4228 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4229 			break;
4230 		usleep_range(1000, 2000);
4231 	}
4232 
4233 	/* Skip if the queue is already in the requested state */
4234 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4235 		return;
4236 
4237 	/* turn on/off the queue */
4238 	if (enable) {
4239 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4240 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4241 	} else {
4242 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4243 	}
4244 
4245 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4246 }
4247 
4248 /**
4249  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4250  * @seid: VSI SEID
4251  * @pf: the PF structure
4252  * @pf_q: the PF queue to configure
4253  * @is_xdp: true if the queue is used for XDP
4254  * @enable: start or stop the queue
4255  **/
4256 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4257 			   bool is_xdp, bool enable)
4258 {
4259 	int ret;
4260 
4261 	i40e_control_tx_q(pf, pf_q, enable);
4262 
4263 	/* wait for the change to finish */
4264 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4265 	if (ret) {
4266 		dev_info(&pf->pdev->dev,
4267 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4268 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4269 			 (enable ? "en" : "dis"));
4270 	}
4271 
4272 	return ret;
4273 }
4274 
4275 /**
4276  * i40e_vsi_control_tx - Start or stop a VSI's rings
4277  * @vsi: the VSI being configured
4278  * @enable: start or stop the rings
4279  **/
4280 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4281 {
4282 	struct i40e_pf *pf = vsi->back;
4283 	int i, pf_q, ret = 0;
4284 
4285 	pf_q = vsi->base_queue;
4286 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4287 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4288 					     pf_q,
4289 					     false /*is xdp*/, enable);
4290 		if (ret)
4291 			break;
4292 
4293 		if (!i40e_enabled_xdp_vsi(vsi))
4294 			continue;
4295 
4296 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4297 					     pf_q + vsi->alloc_queue_pairs,
4298 					     true /*is xdp*/, enable);
4299 		if (ret)
4300 			break;
4301 	}
4302 	return ret;
4303 }
4304 
4305 /**
4306  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4307  * @pf: the PF being configured
4308  * @pf_q: the PF queue
4309  * @enable: enable or disable state of the queue
4310  *
4311  * This routine will wait for the given Rx queue of the PF to reach the
4312  * enabled or disabled state.
4313  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4314  * multiple retries; else will return 0 in case of success.
4315  **/
4316 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4317 {
4318 	int i;
4319 	u32 rx_reg;
4320 
4321 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4322 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4323 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4324 			break;
4325 
4326 		usleep_range(10, 20);
4327 	}
4328 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4329 		return -ETIMEDOUT;
4330 
4331 	return 0;
4332 }
4333 
4334 /**
4335  * i40e_control_rx_q - Start or stop a particular Rx queue
4336  * @pf: the PF structure
4337  * @pf_q: the PF queue to configure
4338  * @enable: start or stop the queue
4339  *
4340  * This function enables or disables a single queue. Note that
4341  * any delay required after the operation is expected to be
4342  * handled by the caller of this function.
4343  **/
4344 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4345 {
4346 	struct i40e_hw *hw = &pf->hw;
4347 	u32 rx_reg;
4348 	int i;
4349 
4350 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4351 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4352 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4353 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4354 			break;
4355 		usleep_range(1000, 2000);
4356 	}
4357 
4358 	/* Skip if the queue is already in the requested state */
4359 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4360 		return;
4361 
4362 	/* turn on/off the queue */
4363 	if (enable)
4364 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4365 	else
4366 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4367 
4368 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4369 }
4370 
4371 /**
4372  * i40e_control_wait_rx_q
4373  * @pf: the PF structure
4374  * @pf_q: queue being configured
4375  * @enable: start or stop the rings
4376  *
4377  * This function enables or disables a single queue along with waiting
4378  * for the change to finish. The caller of this function should handle
4379  * the delays needed in the case of disabling queues.
4380  **/
4381 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4382 {
4383 	int ret = 0;
4384 
4385 	i40e_control_rx_q(pf, pf_q, enable);
4386 
4387 	/* wait for the change to finish */
4388 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4389 	if (ret)
4390 		return ret;
4391 
4392 	return ret;
4393 }
4394 
4395 /**
4396  * i40e_vsi_control_rx - Start or stop a VSI's rings
4397  * @vsi: the VSI being configured
4398  * @enable: start or stop the rings
4399  **/
4400 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4401 {
4402 	struct i40e_pf *pf = vsi->back;
4403 	int i, pf_q, ret = 0;
4404 
4405 	pf_q = vsi->base_queue;
4406 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4407 		ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4408 		if (ret) {
4409 			dev_info(&pf->pdev->dev,
4410 				 "VSI seid %d Rx ring %d %sable timeout\n",
4411 				 vsi->seid, pf_q, (enable ? "en" : "dis"));
4412 			break;
4413 		}
4414 	}
4415 
4416 	/* Due to HW errata, on Rx disable only, the register can indicate done
4417 	 * before it really is. Needs 50ms to be sure
4418 	 */
4419 	if (!enable)
4420 		mdelay(50);
4421 
4422 	return ret;
4423 }
4424 
4425 /**
4426  * i40e_vsi_start_rings - Start a VSI's rings
4427  * @vsi: the VSI being configured
4428  **/
4429 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4430 {
4431 	int ret = 0;
4432 
4433 	/* do rx first for enable and last for disable */
4434 	ret = i40e_vsi_control_rx(vsi, true);
4435 	if (ret)
4436 		return ret;
4437 	ret = i40e_vsi_control_tx(vsi, true);
4438 
4439 	return ret;
4440 }
4441 
4442 /**
4443  * i40e_vsi_stop_rings - Stop a VSI's rings
4444  * @vsi: the VSI being configured
4445  **/
4446 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4447 {
4448 	/* When port TX is suspended, don't wait */
4449 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4450 		return i40e_vsi_stop_rings_no_wait(vsi);
4451 
4452 	/* do rx first for enable and last for disable
4453 	 * Ignore return value, we need to shutdown whatever we can
4454 	 */
4455 	i40e_vsi_control_tx(vsi, false);
4456 	i40e_vsi_control_rx(vsi, false);
4457 }
4458 
4459 /**
4460  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4461  * @vsi: the VSI being shutdown
4462  *
4463  * This function stops all the rings for a VSI but does not delay to verify
4464  * that rings have been disabled. It is expected that the caller is shutting
4465  * down multiple VSIs at once and will delay together for all the VSIs after
4466  * initiating the shutdown. This is particularly useful for shutting down lots
4467  * of VFs together. Otherwise, a large delay can be incurred while configuring
4468  * each VSI in serial.
4469  **/
4470 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4471 {
4472 	struct i40e_pf *pf = vsi->back;
4473 	int i, pf_q;
4474 
4475 	pf_q = vsi->base_queue;
4476 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4477 		i40e_control_tx_q(pf, pf_q, false);
4478 		i40e_control_rx_q(pf, pf_q, false);
4479 	}
4480 }
4481 
4482 /**
4483  * i40e_vsi_free_irq - Free the irq association with the OS
4484  * @vsi: the VSI being configured
4485  **/
4486 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4487 {
4488 	struct i40e_pf *pf = vsi->back;
4489 	struct i40e_hw *hw = &pf->hw;
4490 	int base = vsi->base_vector;
4491 	u32 val, qp;
4492 	int i;
4493 
4494 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4495 		if (!vsi->q_vectors)
4496 			return;
4497 
4498 		if (!vsi->irqs_ready)
4499 			return;
4500 
4501 		vsi->irqs_ready = false;
4502 		for (i = 0; i < vsi->num_q_vectors; i++) {
4503 			int irq_num;
4504 			u16 vector;
4505 
4506 			vector = i + base;
4507 			irq_num = pf->msix_entries[vector].vector;
4508 
4509 			/* free only the irqs that were actually requested */
4510 			if (!vsi->q_vectors[i] ||
4511 			    !vsi->q_vectors[i]->num_ringpairs)
4512 				continue;
4513 
4514 			/* clear the affinity notifier in the IRQ descriptor */
4515 			irq_set_affinity_notifier(irq_num, NULL);
4516 			/* remove our suggested affinity mask for this IRQ */
4517 			irq_set_affinity_hint(irq_num, NULL);
4518 			synchronize_irq(irq_num);
4519 			free_irq(irq_num, vsi->q_vectors[i]);
4520 
4521 			/* Tear down the interrupt queue link list
4522 			 *
4523 			 * We know that they come in pairs and always
4524 			 * the Rx first, then the Tx.  To clear the
4525 			 * link list, stick the EOL value into the
4526 			 * next_q field of the registers.
4527 			 */
4528 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4529 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4530 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4531 			val |= I40E_QUEUE_END_OF_LIST
4532 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4533 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4534 
4535 			while (qp != I40E_QUEUE_END_OF_LIST) {
4536 				u32 next;
4537 
4538 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4539 
4540 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4541 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4542 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4543 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4544 
4545 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4546 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4547 
4548 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4549 
4550 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4551 
4552 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4553 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4554 
4555 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4556 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4557 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4558 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4559 
4560 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4561 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4562 
4563 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4564 				qp = next;
4565 			}
4566 		}
4567 	} else {
4568 		free_irq(pf->pdev->irq, pf);
4569 
4570 		val = rd32(hw, I40E_PFINT_LNKLST0);
4571 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4572 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4573 		val |= I40E_QUEUE_END_OF_LIST
4574 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4575 		wr32(hw, I40E_PFINT_LNKLST0, val);
4576 
4577 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4578 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4579 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4580 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4581 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4582 
4583 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4584 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4585 
4586 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4587 
4588 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4589 
4590 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4591 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4592 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4593 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4594 
4595 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4596 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4597 
4598 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4599 	}
4600 }
4601 
4602 /**
4603  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4604  * @vsi: the VSI being configured
4605  * @v_idx: Index of vector to be freed
4606  *
4607  * This function frees the memory allocated to the q_vector.  In addition if
4608  * NAPI is enabled it will delete any references to the NAPI struct prior
4609  * to freeing the q_vector.
4610  **/
4611 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4612 {
4613 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4614 	struct i40e_ring *ring;
4615 
4616 	if (!q_vector)
4617 		return;
4618 
4619 	/* disassociate q_vector from rings */
4620 	i40e_for_each_ring(ring, q_vector->tx)
4621 		ring->q_vector = NULL;
4622 
4623 	i40e_for_each_ring(ring, q_vector->rx)
4624 		ring->q_vector = NULL;
4625 
4626 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4627 	if (vsi->netdev)
4628 		netif_napi_del(&q_vector->napi);
4629 
4630 	vsi->q_vectors[v_idx] = NULL;
4631 
4632 	kfree_rcu(q_vector, rcu);
4633 }
4634 
4635 /**
4636  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4637  * @vsi: the VSI being un-configured
4638  *
4639  * This frees the memory allocated to the q_vectors and
4640  * deletes references to the NAPI struct.
4641  **/
4642 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4643 {
4644 	int v_idx;
4645 
4646 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4647 		i40e_free_q_vector(vsi, v_idx);
4648 }
4649 
4650 /**
4651  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4652  * @pf: board private structure
4653  **/
4654 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4655 {
4656 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4657 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4658 		pci_disable_msix(pf->pdev);
4659 		kfree(pf->msix_entries);
4660 		pf->msix_entries = NULL;
4661 		kfree(pf->irq_pile);
4662 		pf->irq_pile = NULL;
4663 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4664 		pci_disable_msi(pf->pdev);
4665 	}
4666 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4667 }
4668 
4669 /**
4670  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4671  * @pf: board private structure
4672  *
4673  * We go through and clear interrupt specific resources and reset the structure
4674  * to pre-load conditions
4675  **/
4676 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4677 {
4678 	int i;
4679 
4680 	i40e_free_misc_vector(pf);
4681 
4682 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4683 		      I40E_IWARP_IRQ_PILE_ID);
4684 
4685 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4686 	for (i = 0; i < pf->num_alloc_vsi; i++)
4687 		if (pf->vsi[i])
4688 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4689 	i40e_reset_interrupt_capability(pf);
4690 }
4691 
4692 /**
4693  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4694  * @vsi: the VSI being configured
4695  **/
4696 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4697 {
4698 	int q_idx;
4699 
4700 	if (!vsi->netdev)
4701 		return;
4702 
4703 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4704 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4705 
4706 		if (q_vector->rx.ring || q_vector->tx.ring)
4707 			napi_enable(&q_vector->napi);
4708 	}
4709 }
4710 
4711 /**
4712  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4713  * @vsi: the VSI being configured
4714  **/
4715 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4716 {
4717 	int q_idx;
4718 
4719 	if (!vsi->netdev)
4720 		return;
4721 
4722 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4723 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4724 
4725 		if (q_vector->rx.ring || q_vector->tx.ring)
4726 			napi_disable(&q_vector->napi);
4727 	}
4728 }
4729 
4730 /**
4731  * i40e_vsi_close - Shut down a VSI
4732  * @vsi: the vsi to be quelled
4733  **/
4734 static void i40e_vsi_close(struct i40e_vsi *vsi)
4735 {
4736 	struct i40e_pf *pf = vsi->back;
4737 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4738 		i40e_down(vsi);
4739 	i40e_vsi_free_irq(vsi);
4740 	i40e_vsi_free_tx_resources(vsi);
4741 	i40e_vsi_free_rx_resources(vsi);
4742 	vsi->current_netdev_flags = 0;
4743 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4744 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4745 		set_bit(__I40E_CLIENT_RESET, pf->state);
4746 }
4747 
4748 /**
4749  * i40e_quiesce_vsi - Pause a given VSI
4750  * @vsi: the VSI being paused
4751  **/
4752 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4753 {
4754 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4755 		return;
4756 
4757 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4758 	if (vsi->netdev && netif_running(vsi->netdev))
4759 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4760 	else
4761 		i40e_vsi_close(vsi);
4762 }
4763 
4764 /**
4765  * i40e_unquiesce_vsi - Resume a given VSI
4766  * @vsi: the VSI being resumed
4767  **/
4768 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4769 {
4770 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4771 		return;
4772 
4773 	if (vsi->netdev && netif_running(vsi->netdev))
4774 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4775 	else
4776 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4777 }
4778 
4779 /**
4780  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4781  * @pf: the PF
4782  **/
4783 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4784 {
4785 	int v;
4786 
4787 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4788 		if (pf->vsi[v])
4789 			i40e_quiesce_vsi(pf->vsi[v]);
4790 	}
4791 }
4792 
4793 /**
4794  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4795  * @pf: the PF
4796  **/
4797 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4798 {
4799 	int v;
4800 
4801 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4802 		if (pf->vsi[v])
4803 			i40e_unquiesce_vsi(pf->vsi[v]);
4804 	}
4805 }
4806 
4807 /**
4808  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4809  * @vsi: the VSI being configured
4810  *
4811  * Wait until all queues on a given VSI have been disabled.
4812  **/
4813 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4814 {
4815 	struct i40e_pf *pf = vsi->back;
4816 	int i, pf_q, ret;
4817 
4818 	pf_q = vsi->base_queue;
4819 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4820 		/* Check and wait for the Tx queue */
4821 		ret = i40e_pf_txq_wait(pf, pf_q, false);
4822 		if (ret) {
4823 			dev_info(&pf->pdev->dev,
4824 				 "VSI seid %d Tx ring %d disable timeout\n",
4825 				 vsi->seid, pf_q);
4826 			return ret;
4827 		}
4828 
4829 		if (!i40e_enabled_xdp_vsi(vsi))
4830 			goto wait_rx;
4831 
4832 		/* Check and wait for the XDP Tx queue */
4833 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4834 				       false);
4835 		if (ret) {
4836 			dev_info(&pf->pdev->dev,
4837 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
4838 				 vsi->seid, pf_q);
4839 			return ret;
4840 		}
4841 wait_rx:
4842 		/* Check and wait for the Rx queue */
4843 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
4844 		if (ret) {
4845 			dev_info(&pf->pdev->dev,
4846 				 "VSI seid %d Rx ring %d disable timeout\n",
4847 				 vsi->seid, pf_q);
4848 			return ret;
4849 		}
4850 	}
4851 
4852 	return 0;
4853 }
4854 
4855 #ifdef CONFIG_I40E_DCB
4856 /**
4857  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4858  * @pf: the PF
4859  *
4860  * This function waits for the queues to be in disabled state for all the
4861  * VSIs that are managed by this PF.
4862  **/
4863 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4864 {
4865 	int v, ret = 0;
4866 
4867 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4868 		if (pf->vsi[v]) {
4869 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4870 			if (ret)
4871 				break;
4872 		}
4873 	}
4874 
4875 	return ret;
4876 }
4877 
4878 #endif
4879 
4880 /**
4881  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4882  * @pf: pointer to PF
4883  *
4884  * Get TC map for ISCSI PF type that will include iSCSI TC
4885  * and LAN TC.
4886  **/
4887 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4888 {
4889 	struct i40e_dcb_app_priority_table app;
4890 	struct i40e_hw *hw = &pf->hw;
4891 	u8 enabled_tc = 1; /* TC0 is always enabled */
4892 	u8 tc, i;
4893 	/* Get the iSCSI APP TLV */
4894 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4895 
4896 	for (i = 0; i < dcbcfg->numapps; i++) {
4897 		app = dcbcfg->app[i];
4898 		if (app.selector == I40E_APP_SEL_TCPIP &&
4899 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
4900 			tc = dcbcfg->etscfg.prioritytable[app.priority];
4901 			enabled_tc |= BIT(tc);
4902 			break;
4903 		}
4904 	}
4905 
4906 	return enabled_tc;
4907 }
4908 
4909 /**
4910  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
4911  * @dcbcfg: the corresponding DCBx configuration structure
4912  *
4913  * Return the number of TCs from given DCBx configuration
4914  **/
4915 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4916 {
4917 	int i, tc_unused = 0;
4918 	u8 num_tc = 0;
4919 	u8 ret = 0;
4920 
4921 	/* Scan the ETS Config Priority Table to find
4922 	 * traffic class enabled for a given priority
4923 	 * and create a bitmask of enabled TCs
4924 	 */
4925 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4926 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4927 
4928 	/* Now scan the bitmask to check for
4929 	 * contiguous TCs starting with TC0
4930 	 */
4931 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4932 		if (num_tc & BIT(i)) {
4933 			if (!tc_unused) {
4934 				ret++;
4935 			} else {
4936 				pr_err("Non-contiguous TC - Disabling DCB\n");
4937 				return 1;
4938 			}
4939 		} else {
4940 			tc_unused = 1;
4941 		}
4942 	}
4943 
4944 	/* There is always at least TC0 */
4945 	if (!ret)
4946 		ret = 1;
4947 
4948 	return ret;
4949 }
4950 
4951 /**
4952  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4953  * @dcbcfg: the corresponding DCBx configuration structure
4954  *
4955  * Query the current DCB configuration and return the number of
4956  * traffic classes enabled from the given DCBX config
4957  **/
4958 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4959 {
4960 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4961 	u8 enabled_tc = 1;
4962 	u8 i;
4963 
4964 	for (i = 0; i < num_tc; i++)
4965 		enabled_tc |= BIT(i);
4966 
4967 	return enabled_tc;
4968 }
4969 
4970 /**
4971  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
4972  * @pf: PF being queried
4973  *
4974  * Query the current MQPRIO configuration and return the number of
4975  * traffic classes enabled.
4976  **/
4977 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
4978 {
4979 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4980 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
4981 	u8 enabled_tc = 1, i;
4982 
4983 	for (i = 1; i < num_tc; i++)
4984 		enabled_tc |= BIT(i);
4985 	return enabled_tc;
4986 }
4987 
4988 /**
4989  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4990  * @pf: PF being queried
4991  *
4992  * Return number of traffic classes enabled for the given PF
4993  **/
4994 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4995 {
4996 	struct i40e_hw *hw = &pf->hw;
4997 	u8 i, enabled_tc = 1;
4998 	u8 num_tc = 0;
4999 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5000 
5001 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5002 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5003 
5004 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5005 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5006 		return 1;
5007 
5008 	/* SFP mode will be enabled for all TCs on port */
5009 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5010 		return i40e_dcb_get_num_tc(dcbcfg);
5011 
5012 	/* MFP mode return count of enabled TCs for this PF */
5013 	if (pf->hw.func_caps.iscsi)
5014 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5015 	else
5016 		return 1; /* Only TC0 */
5017 
5018 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5019 		if (enabled_tc & BIT(i))
5020 			num_tc++;
5021 	}
5022 	return num_tc;
5023 }
5024 
5025 /**
5026  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5027  * @pf: PF being queried
5028  *
5029  * Return a bitmap for enabled traffic classes for this PF.
5030  **/
5031 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5032 {
5033 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5034 		return i40e_mqprio_get_enabled_tc(pf);
5035 
5036 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5037 	 * default TC
5038 	 */
5039 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5040 		return I40E_DEFAULT_TRAFFIC_CLASS;
5041 
5042 	/* SFP mode we want PF to be enabled for all TCs */
5043 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5044 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5045 
5046 	/* MFP enabled and iSCSI PF type */
5047 	if (pf->hw.func_caps.iscsi)
5048 		return i40e_get_iscsi_tc_map(pf);
5049 	else
5050 		return I40E_DEFAULT_TRAFFIC_CLASS;
5051 }
5052 
5053 /**
5054  * i40e_vsi_get_bw_info - Query VSI BW Information
5055  * @vsi: the VSI being queried
5056  *
5057  * Returns 0 on success, negative value on failure
5058  **/
5059 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5060 {
5061 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5062 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5063 	struct i40e_pf *pf = vsi->back;
5064 	struct i40e_hw *hw = &pf->hw;
5065 	i40e_status ret;
5066 	u32 tc_bw_max;
5067 	int i;
5068 
5069 	/* Get the VSI level BW configuration */
5070 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5071 	if (ret) {
5072 		dev_info(&pf->pdev->dev,
5073 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5074 			 i40e_stat_str(&pf->hw, ret),
5075 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5076 		return -EINVAL;
5077 	}
5078 
5079 	/* Get the VSI level BW configuration per TC */
5080 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5081 					       NULL);
5082 	if (ret) {
5083 		dev_info(&pf->pdev->dev,
5084 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5085 			 i40e_stat_str(&pf->hw, ret),
5086 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5087 		return -EINVAL;
5088 	}
5089 
5090 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5091 		dev_info(&pf->pdev->dev,
5092 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5093 			 bw_config.tc_valid_bits,
5094 			 bw_ets_config.tc_valid_bits);
5095 		/* Still continuing */
5096 	}
5097 
5098 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5099 	vsi->bw_max_quanta = bw_config.max_bw;
5100 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5101 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5102 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5103 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5104 		vsi->bw_ets_limit_credits[i] =
5105 					le16_to_cpu(bw_ets_config.credits[i]);
5106 		/* 3 bits out of 4 for each TC */
5107 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5108 	}
5109 
5110 	return 0;
5111 }
5112 
5113 /**
5114  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5115  * @vsi: the VSI being configured
5116  * @enabled_tc: TC bitmap
5117  * @bw_share: BW shared credits per TC
5118  *
5119  * Returns 0 on success, negative value on failure
5120  **/
5121 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5122 				       u8 *bw_share)
5123 {
5124 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5125 	i40e_status ret;
5126 	int i;
5127 
5128 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
5129 		return 0;
5130 	if (!vsi->mqprio_qopt.qopt.hw) {
5131 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5132 		if (ret)
5133 			dev_info(&vsi->back->pdev->dev,
5134 				 "Failed to reset tx rate for vsi->seid %u\n",
5135 				 vsi->seid);
5136 		return ret;
5137 	}
5138 	bw_data.tc_valid_bits = enabled_tc;
5139 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5140 		bw_data.tc_bw_credits[i] = bw_share[i];
5141 
5142 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5143 				       NULL);
5144 	if (ret) {
5145 		dev_info(&vsi->back->pdev->dev,
5146 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5147 			 vsi->back->hw.aq.asq_last_status);
5148 		return -EINVAL;
5149 	}
5150 
5151 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5152 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5153 
5154 	return 0;
5155 }
5156 
5157 /**
5158  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5159  * @vsi: the VSI being configured
5160  * @enabled_tc: TC map to be enabled
5161  *
5162  **/
5163 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5164 {
5165 	struct net_device *netdev = vsi->netdev;
5166 	struct i40e_pf *pf = vsi->back;
5167 	struct i40e_hw *hw = &pf->hw;
5168 	u8 netdev_tc = 0;
5169 	int i;
5170 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5171 
5172 	if (!netdev)
5173 		return;
5174 
5175 	if (!enabled_tc) {
5176 		netdev_reset_tc(netdev);
5177 		return;
5178 	}
5179 
5180 	/* Set up actual enabled TCs on the VSI */
5181 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5182 		return;
5183 
5184 	/* set per TC queues for the VSI */
5185 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5186 		/* Only set TC queues for enabled tcs
5187 		 *
5188 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5189 		 * enabled_tc bitmap would be 0x00001001; the driver
5190 		 * will set the numtc for netdev as 2 that will be
5191 		 * referenced by the netdev layer as TC 0 and 1.
5192 		 */
5193 		if (vsi->tc_config.enabled_tc & BIT(i))
5194 			netdev_set_tc_queue(netdev,
5195 					vsi->tc_config.tc_info[i].netdev_tc,
5196 					vsi->tc_config.tc_info[i].qcount,
5197 					vsi->tc_config.tc_info[i].qoffset);
5198 	}
5199 
5200 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5201 		return;
5202 
5203 	/* Assign UP2TC map for the VSI */
5204 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5205 		/* Get the actual TC# for the UP */
5206 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5207 		/* Get the mapped netdev TC# for the UP */
5208 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5209 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5210 	}
5211 }
5212 
5213 /**
5214  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5215  * @vsi: the VSI being configured
5216  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5217  **/
5218 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5219 				      struct i40e_vsi_context *ctxt)
5220 {
5221 	/* copy just the sections touched not the entire info
5222 	 * since not all sections are valid as returned by
5223 	 * update vsi params
5224 	 */
5225 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5226 	memcpy(&vsi->info.queue_mapping,
5227 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5228 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5229 	       sizeof(vsi->info.tc_mapping));
5230 }
5231 
5232 /**
5233  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5234  * @vsi: VSI to be configured
5235  * @enabled_tc: TC bitmap
5236  *
5237  * This configures a particular VSI for TCs that are mapped to the
5238  * given TC bitmap. It uses default bandwidth share for TCs across
5239  * VSIs to configure TC for a particular VSI.
5240  *
5241  * NOTE:
5242  * It is expected that the VSI queues have been quisced before calling
5243  * this function.
5244  **/
5245 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5246 {
5247 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5248 	struct i40e_pf *pf = vsi->back;
5249 	struct i40e_hw *hw = &pf->hw;
5250 	struct i40e_vsi_context ctxt;
5251 	int ret = 0;
5252 	int i;
5253 
5254 	/* Check if enabled_tc is same as existing or new TCs */
5255 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5256 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5257 		return ret;
5258 
5259 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5260 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5261 		if (enabled_tc & BIT(i))
5262 			bw_share[i] = 1;
5263 	}
5264 
5265 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5266 	if (ret) {
5267 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5268 
5269 		dev_info(&pf->pdev->dev,
5270 			 "Failed configuring TC map %d for VSI %d\n",
5271 			 enabled_tc, vsi->seid);
5272 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5273 						  &bw_config, NULL);
5274 		if (ret) {
5275 			dev_info(&pf->pdev->dev,
5276 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5277 				 i40e_stat_str(hw, ret),
5278 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5279 			goto out;
5280 		}
5281 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5282 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5283 
5284 			if (!valid_tc)
5285 				valid_tc = bw_config.tc_valid_bits;
5286 			/* Always enable TC0, no matter what */
5287 			valid_tc |= 1;
5288 			dev_info(&pf->pdev->dev,
5289 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5290 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5291 			enabled_tc = valid_tc;
5292 		}
5293 
5294 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5295 		if (ret) {
5296 			dev_err(&pf->pdev->dev,
5297 				"Unable to  configure TC map %d for VSI %d\n",
5298 				enabled_tc, vsi->seid);
5299 			goto out;
5300 		}
5301 	}
5302 
5303 	/* Update Queue Pairs Mapping for currently enabled UPs */
5304 	ctxt.seid = vsi->seid;
5305 	ctxt.pf_num = vsi->back->hw.pf_id;
5306 	ctxt.vf_num = 0;
5307 	ctxt.uplink_seid = vsi->uplink_seid;
5308 	ctxt.info = vsi->info;
5309 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5310 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5311 		if (ret)
5312 			goto out;
5313 	} else {
5314 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5315 	}
5316 
5317 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5318 	 * queues changed.
5319 	 */
5320 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5321 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5322 				      vsi->num_queue_pairs);
5323 		ret = i40e_vsi_config_rss(vsi);
5324 		if (ret) {
5325 			dev_info(&vsi->back->pdev->dev,
5326 				 "Failed to reconfig rss for num_queues\n");
5327 			return ret;
5328 		}
5329 		vsi->reconfig_rss = false;
5330 	}
5331 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5332 		ctxt.info.valid_sections |=
5333 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5334 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5335 	}
5336 
5337 	/* Update the VSI after updating the VSI queue-mapping
5338 	 * information
5339 	 */
5340 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5341 	if (ret) {
5342 		dev_info(&pf->pdev->dev,
5343 			 "Update vsi tc config failed, err %s aq_err %s\n",
5344 			 i40e_stat_str(hw, ret),
5345 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5346 		goto out;
5347 	}
5348 	/* update the local VSI info with updated queue map */
5349 	i40e_vsi_update_queue_map(vsi, &ctxt);
5350 	vsi->info.valid_sections = 0;
5351 
5352 	/* Update current VSI BW information */
5353 	ret = i40e_vsi_get_bw_info(vsi);
5354 	if (ret) {
5355 		dev_info(&pf->pdev->dev,
5356 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5357 			 i40e_stat_str(hw, ret),
5358 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5359 		goto out;
5360 	}
5361 
5362 	/* Update the netdev TC setup */
5363 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5364 out:
5365 	return ret;
5366 }
5367 
5368 /**
5369  * i40e_get_link_speed - Returns link speed for the interface
5370  * @vsi: VSI to be configured
5371  *
5372  **/
5373 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5374 {
5375 	struct i40e_pf *pf = vsi->back;
5376 
5377 	switch (pf->hw.phy.link_info.link_speed) {
5378 	case I40E_LINK_SPEED_40GB:
5379 		return 40000;
5380 	case I40E_LINK_SPEED_25GB:
5381 		return 25000;
5382 	case I40E_LINK_SPEED_20GB:
5383 		return 20000;
5384 	case I40E_LINK_SPEED_10GB:
5385 		return 10000;
5386 	case I40E_LINK_SPEED_1GB:
5387 		return 1000;
5388 	default:
5389 		return -EINVAL;
5390 	}
5391 }
5392 
5393 /**
5394  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5395  * @vsi: VSI to be configured
5396  * @seid: seid of the channel/VSI
5397  * @max_tx_rate: max TX rate to be configured as BW limit
5398  *
5399  * Helper function to set BW limit for a given VSI
5400  **/
5401 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5402 {
5403 	struct i40e_pf *pf = vsi->back;
5404 	u64 credits = 0;
5405 	int speed = 0;
5406 	int ret = 0;
5407 
5408 	speed = i40e_get_link_speed(vsi);
5409 	if (max_tx_rate > speed) {
5410 		dev_err(&pf->pdev->dev,
5411 			"Invalid max tx rate %llu specified for VSI seid %d.",
5412 			max_tx_rate, seid);
5413 		return -EINVAL;
5414 	}
5415 	if (max_tx_rate && max_tx_rate < 50) {
5416 		dev_warn(&pf->pdev->dev,
5417 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5418 		max_tx_rate = 50;
5419 	}
5420 
5421 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5422 	credits = max_tx_rate;
5423 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5424 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5425 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5426 	if (ret)
5427 		dev_err(&pf->pdev->dev,
5428 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5429 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5430 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5431 	return ret;
5432 }
5433 
5434 /**
5435  * i40e_remove_queue_channels - Remove queue channels for the TCs
5436  * @vsi: VSI to be configured
5437  *
5438  * Remove queue channels for the TCs
5439  **/
5440 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5441 {
5442 	enum i40e_admin_queue_err last_aq_status;
5443 	struct i40e_cloud_filter *cfilter;
5444 	struct i40e_channel *ch, *ch_tmp;
5445 	struct i40e_pf *pf = vsi->back;
5446 	struct hlist_node *node;
5447 	int ret, i;
5448 
5449 	/* Reset rss size that was stored when reconfiguring rss for
5450 	 * channel VSIs with non-power-of-2 queue count.
5451 	 */
5452 	vsi->current_rss_size = 0;
5453 
5454 	/* perform cleanup for channels if they exist */
5455 	if (list_empty(&vsi->ch_list))
5456 		return;
5457 
5458 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5459 		struct i40e_vsi *p_vsi;
5460 
5461 		list_del(&ch->list);
5462 		p_vsi = ch->parent_vsi;
5463 		if (!p_vsi || !ch->initialized) {
5464 			kfree(ch);
5465 			continue;
5466 		}
5467 		/* Reset queue contexts */
5468 		for (i = 0; i < ch->num_queue_pairs; i++) {
5469 			struct i40e_ring *tx_ring, *rx_ring;
5470 			u16 pf_q;
5471 
5472 			pf_q = ch->base_queue + i;
5473 			tx_ring = vsi->tx_rings[pf_q];
5474 			tx_ring->ch = NULL;
5475 
5476 			rx_ring = vsi->rx_rings[pf_q];
5477 			rx_ring->ch = NULL;
5478 		}
5479 
5480 		/* Reset BW configured for this VSI via mqprio */
5481 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5482 		if (ret)
5483 			dev_info(&vsi->back->pdev->dev,
5484 				 "Failed to reset tx rate for ch->seid %u\n",
5485 				 ch->seid);
5486 
5487 		/* delete cloud filters associated with this channel */
5488 		hlist_for_each_entry_safe(cfilter, node,
5489 					  &pf->cloud_filter_list, cloud_node) {
5490 			if (cfilter->seid != ch->seid)
5491 				continue;
5492 
5493 			hash_del(&cfilter->cloud_node);
5494 			if (cfilter->dst_port)
5495 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5496 									cfilter,
5497 									false);
5498 			else
5499 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5500 								false);
5501 			last_aq_status = pf->hw.aq.asq_last_status;
5502 			if (ret)
5503 				dev_info(&pf->pdev->dev,
5504 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5505 					 i40e_stat_str(&pf->hw, ret),
5506 					 i40e_aq_str(&pf->hw, last_aq_status));
5507 			kfree(cfilter);
5508 		}
5509 
5510 		/* delete VSI from FW */
5511 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5512 					     NULL);
5513 		if (ret)
5514 			dev_err(&vsi->back->pdev->dev,
5515 				"unable to remove channel (%d) for parent VSI(%d)\n",
5516 				ch->seid, p_vsi->seid);
5517 		kfree(ch);
5518 	}
5519 	INIT_LIST_HEAD(&vsi->ch_list);
5520 }
5521 
5522 /**
5523  * i40e_is_any_channel - channel exist or not
5524  * @vsi: ptr to VSI to which channels are associated with
5525  *
5526  * Returns true or false if channel(s) exist for associated VSI or not
5527  **/
5528 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5529 {
5530 	struct i40e_channel *ch, *ch_tmp;
5531 
5532 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5533 		if (ch->initialized)
5534 			return true;
5535 	}
5536 
5537 	return false;
5538 }
5539 
5540 /**
5541  * i40e_get_max_queues_for_channel
5542  * @vsi: ptr to VSI to which channels are associated with
5543  *
5544  * Helper function which returns max value among the queue counts set on the
5545  * channels/TCs created.
5546  **/
5547 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5548 {
5549 	struct i40e_channel *ch, *ch_tmp;
5550 	int max = 0;
5551 
5552 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5553 		if (!ch->initialized)
5554 			continue;
5555 		if (ch->num_queue_pairs > max)
5556 			max = ch->num_queue_pairs;
5557 	}
5558 
5559 	return max;
5560 }
5561 
5562 /**
5563  * i40e_validate_num_queues - validate num_queues w.r.t channel
5564  * @pf: ptr to PF device
5565  * @num_queues: number of queues
5566  * @vsi: the parent VSI
5567  * @reconfig_rss: indicates should the RSS be reconfigured or not
5568  *
5569  * This function validates number of queues in the context of new channel
5570  * which is being established and determines if RSS should be reconfigured
5571  * or not for parent VSI.
5572  **/
5573 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5574 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5575 {
5576 	int max_ch_queues;
5577 
5578 	if (!reconfig_rss)
5579 		return -EINVAL;
5580 
5581 	*reconfig_rss = false;
5582 	if (vsi->current_rss_size) {
5583 		if (num_queues > vsi->current_rss_size) {
5584 			dev_dbg(&pf->pdev->dev,
5585 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5586 				num_queues, vsi->current_rss_size);
5587 			return -EINVAL;
5588 		} else if ((num_queues < vsi->current_rss_size) &&
5589 			   (!is_power_of_2(num_queues))) {
5590 			dev_dbg(&pf->pdev->dev,
5591 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5592 				num_queues, vsi->current_rss_size);
5593 			return -EINVAL;
5594 		}
5595 	}
5596 
5597 	if (!is_power_of_2(num_queues)) {
5598 		/* Find the max num_queues configured for channel if channel
5599 		 * exist.
5600 		 * if channel exist, then enforce 'num_queues' to be more than
5601 		 * max ever queues configured for channel.
5602 		 */
5603 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5604 		if (num_queues < max_ch_queues) {
5605 			dev_dbg(&pf->pdev->dev,
5606 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5607 				num_queues, max_ch_queues);
5608 			return -EINVAL;
5609 		}
5610 		*reconfig_rss = true;
5611 	}
5612 
5613 	return 0;
5614 }
5615 
5616 /**
5617  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5618  * @vsi: the VSI being setup
5619  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5620  *
5621  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5622  **/
5623 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5624 {
5625 	struct i40e_pf *pf = vsi->back;
5626 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5627 	struct i40e_hw *hw = &pf->hw;
5628 	int local_rss_size;
5629 	u8 *lut;
5630 	int ret;
5631 
5632 	if (!vsi->rss_size)
5633 		return -EINVAL;
5634 
5635 	if (rss_size > vsi->rss_size)
5636 		return -EINVAL;
5637 
5638 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5639 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5640 	if (!lut)
5641 		return -ENOMEM;
5642 
5643 	/* Ignoring user configured lut if there is one */
5644 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5645 
5646 	/* Use user configured hash key if there is one, otherwise
5647 	 * use default.
5648 	 */
5649 	if (vsi->rss_hkey_user)
5650 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5651 	else
5652 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5653 
5654 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5655 	if (ret) {
5656 		dev_info(&pf->pdev->dev,
5657 			 "Cannot set RSS lut, err %s aq_err %s\n",
5658 			 i40e_stat_str(hw, ret),
5659 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5660 		kfree(lut);
5661 		return ret;
5662 	}
5663 	kfree(lut);
5664 
5665 	/* Do the update w.r.t. storing rss_size */
5666 	if (!vsi->orig_rss_size)
5667 		vsi->orig_rss_size = vsi->rss_size;
5668 	vsi->current_rss_size = local_rss_size;
5669 
5670 	return ret;
5671 }
5672 
5673 /**
5674  * i40e_channel_setup_queue_map - Setup a channel queue map
5675  * @pf: ptr to PF device
5676  * @vsi: the VSI being setup
5677  * @ctxt: VSI context structure
5678  * @ch: ptr to channel structure
5679  *
5680  * Setup queue map for a specific channel
5681  **/
5682 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5683 					 struct i40e_vsi_context *ctxt,
5684 					 struct i40e_channel *ch)
5685 {
5686 	u16 qcount, qmap, sections = 0;
5687 	u8 offset = 0;
5688 	int pow;
5689 
5690 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5691 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5692 
5693 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5694 	ch->num_queue_pairs = qcount;
5695 
5696 	/* find the next higher power-of-2 of num queue pairs */
5697 	pow = ilog2(qcount);
5698 	if (!is_power_of_2(qcount))
5699 		pow++;
5700 
5701 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5702 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5703 
5704 	/* Setup queue TC[0].qmap for given VSI context */
5705 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5706 
5707 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5708 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5709 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5710 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5711 }
5712 
5713 /**
5714  * i40e_add_channel - add a channel by adding VSI
5715  * @pf: ptr to PF device
5716  * @uplink_seid: underlying HW switching element (VEB) ID
5717  * @ch: ptr to channel structure
5718  *
5719  * Add a channel (VSI) using add_vsi and queue_map
5720  **/
5721 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5722 			    struct i40e_channel *ch)
5723 {
5724 	struct i40e_hw *hw = &pf->hw;
5725 	struct i40e_vsi_context ctxt;
5726 	u8 enabled_tc = 0x1; /* TC0 enabled */
5727 	int ret;
5728 
5729 	if (ch->type != I40E_VSI_VMDQ2) {
5730 		dev_info(&pf->pdev->dev,
5731 			 "add new vsi failed, ch->type %d\n", ch->type);
5732 		return -EINVAL;
5733 	}
5734 
5735 	memset(&ctxt, 0, sizeof(ctxt));
5736 	ctxt.pf_num = hw->pf_id;
5737 	ctxt.vf_num = 0;
5738 	ctxt.uplink_seid = uplink_seid;
5739 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5740 	if (ch->type == I40E_VSI_VMDQ2)
5741 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5742 
5743 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5744 		ctxt.info.valid_sections |=
5745 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5746 		ctxt.info.switch_id =
5747 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5748 	}
5749 
5750 	/* Set queue map for a given VSI context */
5751 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5752 
5753 	/* Now time to create VSI */
5754 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5755 	if (ret) {
5756 		dev_info(&pf->pdev->dev,
5757 			 "add new vsi failed, err %s aq_err %s\n",
5758 			 i40e_stat_str(&pf->hw, ret),
5759 			 i40e_aq_str(&pf->hw,
5760 				     pf->hw.aq.asq_last_status));
5761 		return -ENOENT;
5762 	}
5763 
5764 	/* Success, update channel */
5765 	ch->enabled_tc = enabled_tc;
5766 	ch->seid = ctxt.seid;
5767 	ch->vsi_number = ctxt.vsi_number;
5768 	ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5769 
5770 	/* copy just the sections touched not the entire info
5771 	 * since not all sections are valid as returned by
5772 	 * update vsi params
5773 	 */
5774 	ch->info.mapping_flags = ctxt.info.mapping_flags;
5775 	memcpy(&ch->info.queue_mapping,
5776 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5777 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5778 	       sizeof(ctxt.info.tc_mapping));
5779 
5780 	return 0;
5781 }
5782 
5783 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5784 				  u8 *bw_share)
5785 {
5786 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5787 	i40e_status ret;
5788 	int i;
5789 
5790 	bw_data.tc_valid_bits = ch->enabled_tc;
5791 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5792 		bw_data.tc_bw_credits[i] = bw_share[i];
5793 
5794 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5795 				       &bw_data, NULL);
5796 	if (ret) {
5797 		dev_info(&vsi->back->pdev->dev,
5798 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5799 			 vsi->back->hw.aq.asq_last_status, ch->seid);
5800 		return -EINVAL;
5801 	}
5802 
5803 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5804 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
5805 
5806 	return 0;
5807 }
5808 
5809 /**
5810  * i40e_channel_config_tx_ring - config TX ring associated with new channel
5811  * @pf: ptr to PF device
5812  * @vsi: the VSI being setup
5813  * @ch: ptr to channel structure
5814  *
5815  * Configure TX rings associated with channel (VSI) since queues are being
5816  * from parent VSI.
5817  **/
5818 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5819 				       struct i40e_vsi *vsi,
5820 				       struct i40e_channel *ch)
5821 {
5822 	i40e_status ret;
5823 	int i;
5824 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5825 
5826 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5827 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5828 		if (ch->enabled_tc & BIT(i))
5829 			bw_share[i] = 1;
5830 	}
5831 
5832 	/* configure BW for new VSI */
5833 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
5834 	if (ret) {
5835 		dev_info(&vsi->back->pdev->dev,
5836 			 "Failed configuring TC map %d for channel (seid %u)\n",
5837 			 ch->enabled_tc, ch->seid);
5838 		return ret;
5839 	}
5840 
5841 	for (i = 0; i < ch->num_queue_pairs; i++) {
5842 		struct i40e_ring *tx_ring, *rx_ring;
5843 		u16 pf_q;
5844 
5845 		pf_q = ch->base_queue + i;
5846 
5847 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
5848 		 * context
5849 		 */
5850 		tx_ring = vsi->tx_rings[pf_q];
5851 		tx_ring->ch = ch;
5852 
5853 		/* Get the RX ring ptr */
5854 		rx_ring = vsi->rx_rings[pf_q];
5855 		rx_ring->ch = ch;
5856 	}
5857 
5858 	return 0;
5859 }
5860 
5861 /**
5862  * i40e_setup_hw_channel - setup new channel
5863  * @pf: ptr to PF device
5864  * @vsi: the VSI being setup
5865  * @ch: ptr to channel structure
5866  * @uplink_seid: underlying HW switching element (VEB) ID
5867  * @type: type of channel to be created (VMDq2/VF)
5868  *
5869  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5870  * and configures TX rings accordingly
5871  **/
5872 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5873 					struct i40e_vsi *vsi,
5874 					struct i40e_channel *ch,
5875 					u16 uplink_seid, u8 type)
5876 {
5877 	int ret;
5878 
5879 	ch->initialized = false;
5880 	ch->base_queue = vsi->next_base_queue;
5881 	ch->type = type;
5882 
5883 	/* Proceed with creation of channel (VMDq2) VSI */
5884 	ret = i40e_add_channel(pf, uplink_seid, ch);
5885 	if (ret) {
5886 		dev_info(&pf->pdev->dev,
5887 			 "failed to add_channel using uplink_seid %u\n",
5888 			 uplink_seid);
5889 		return ret;
5890 	}
5891 
5892 	/* Mark the successful creation of channel */
5893 	ch->initialized = true;
5894 
5895 	/* Reconfigure TX queues using QTX_CTL register */
5896 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5897 	if (ret) {
5898 		dev_info(&pf->pdev->dev,
5899 			 "failed to configure TX rings for channel %u\n",
5900 			 ch->seid);
5901 		return ret;
5902 	}
5903 
5904 	/* update 'next_base_queue' */
5905 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5906 	dev_dbg(&pf->pdev->dev,
5907 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5908 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
5909 		ch->num_queue_pairs,
5910 		vsi->next_base_queue);
5911 	return ret;
5912 }
5913 
5914 /**
5915  * i40e_setup_channel - setup new channel using uplink element
5916  * @pf: ptr to PF device
5917  * @type: type of channel to be created (VMDq2/VF)
5918  * @uplink_seid: underlying HW switching element (VEB) ID
5919  * @ch: ptr to channel structure
5920  *
5921  * Setup new channel (VSI) based on specified type (VMDq2/VF)
5922  * and uplink switching element (uplink_seid)
5923  **/
5924 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5925 			       struct i40e_channel *ch)
5926 {
5927 	u8 vsi_type;
5928 	u16 seid;
5929 	int ret;
5930 
5931 	if (vsi->type == I40E_VSI_MAIN) {
5932 		vsi_type = I40E_VSI_VMDQ2;
5933 	} else {
5934 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5935 			vsi->type);
5936 		return false;
5937 	}
5938 
5939 	/* underlying switching element */
5940 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5941 
5942 	/* create channel (VSI), configure TX rings */
5943 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5944 	if (ret) {
5945 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5946 		return false;
5947 	}
5948 
5949 	return ch->initialized ? true : false;
5950 }
5951 
5952 /**
5953  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
5954  * @vsi: ptr to VSI which has PF backing
5955  *
5956  * Sets up switch mode correctly if it needs to be changed and perform
5957  * what are allowed modes.
5958  **/
5959 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
5960 {
5961 	u8 mode;
5962 	struct i40e_pf *pf = vsi->back;
5963 	struct i40e_hw *hw = &pf->hw;
5964 	int ret;
5965 
5966 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
5967 	if (ret)
5968 		return -EINVAL;
5969 
5970 	if (hw->dev_caps.switch_mode) {
5971 		/* if switch mode is set, support mode2 (non-tunneled for
5972 		 * cloud filter) for now
5973 		 */
5974 		u32 switch_mode = hw->dev_caps.switch_mode &
5975 				  I40E_SWITCH_MODE_MASK;
5976 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
5977 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
5978 				return 0;
5979 			dev_err(&pf->pdev->dev,
5980 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
5981 				hw->dev_caps.switch_mode);
5982 			return -EINVAL;
5983 		}
5984 	}
5985 
5986 	/* Set Bit 7 to be valid */
5987 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
5988 
5989 	/* Set L4type for TCP support */
5990 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
5991 
5992 	/* Set cloud filter mode */
5993 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
5994 
5995 	/* Prep mode field for set_switch_config */
5996 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
5997 					pf->last_sw_conf_valid_flags,
5998 					mode, NULL);
5999 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6000 		dev_err(&pf->pdev->dev,
6001 			"couldn't set switch config bits, err %s aq_err %s\n",
6002 			i40e_stat_str(hw, ret),
6003 			i40e_aq_str(hw,
6004 				    hw->aq.asq_last_status));
6005 
6006 	return ret;
6007 }
6008 
6009 /**
6010  * i40e_create_queue_channel - function to create channel
6011  * @vsi: VSI to be configured
6012  * @ch: ptr to channel (it contains channel specific params)
6013  *
6014  * This function creates channel (VSI) using num_queues specified by user,
6015  * reconfigs RSS if needed.
6016  **/
6017 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6018 			      struct i40e_channel *ch)
6019 {
6020 	struct i40e_pf *pf = vsi->back;
6021 	bool reconfig_rss;
6022 	int err;
6023 
6024 	if (!ch)
6025 		return -EINVAL;
6026 
6027 	if (!ch->num_queue_pairs) {
6028 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6029 			ch->num_queue_pairs);
6030 		return -EINVAL;
6031 	}
6032 
6033 	/* validate user requested num_queues for channel */
6034 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6035 				       &reconfig_rss);
6036 	if (err) {
6037 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6038 			 ch->num_queue_pairs);
6039 		return -EINVAL;
6040 	}
6041 
6042 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6043 	 * VSI to be added switch to VEB mode.
6044 	 */
6045 	if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6046 	    (!i40e_is_any_channel(vsi))) {
6047 		if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6048 			dev_dbg(&pf->pdev->dev,
6049 				"Failed to create channel. Override queues (%u) not power of 2\n",
6050 				vsi->tc_config.tc_info[0].qcount);
6051 			return -EINVAL;
6052 		}
6053 
6054 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6055 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6056 
6057 			if (vsi->type == I40E_VSI_MAIN) {
6058 				if (pf->flags & I40E_FLAG_TC_MQPRIO)
6059 					i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6060 						      true);
6061 				else
6062 					i40e_do_reset_safe(pf,
6063 							   I40E_PF_RESET_FLAG);
6064 			}
6065 		}
6066 		/* now onwards for main VSI, number of queues will be value
6067 		 * of TC0's queue count
6068 		 */
6069 	}
6070 
6071 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6072 	 * it should be more than num_queues
6073 	 */
6074 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6075 		dev_dbg(&pf->pdev->dev,
6076 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6077 			vsi->cnt_q_avail, ch->num_queue_pairs);
6078 		return -EINVAL;
6079 	}
6080 
6081 	/* reconfig_rss only if vsi type is MAIN_VSI */
6082 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6083 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6084 		if (err) {
6085 			dev_info(&pf->pdev->dev,
6086 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6087 				 ch->num_queue_pairs);
6088 			return -EINVAL;
6089 		}
6090 	}
6091 
6092 	if (!i40e_setup_channel(pf, vsi, ch)) {
6093 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6094 		return -EINVAL;
6095 	}
6096 
6097 	dev_info(&pf->pdev->dev,
6098 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6099 		 ch->seid, ch->num_queue_pairs);
6100 
6101 	/* configure VSI for BW limit */
6102 	if (ch->max_tx_rate) {
6103 		u64 credits = ch->max_tx_rate;
6104 
6105 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6106 			return -EINVAL;
6107 
6108 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6109 		dev_dbg(&pf->pdev->dev,
6110 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6111 			ch->max_tx_rate,
6112 			credits,
6113 			ch->seid);
6114 	}
6115 
6116 	/* in case of VF, this will be main SRIOV VSI */
6117 	ch->parent_vsi = vsi;
6118 
6119 	/* and update main_vsi's count for queue_available to use */
6120 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6121 
6122 	return 0;
6123 }
6124 
6125 /**
6126  * i40e_configure_queue_channels - Add queue channel for the given TCs
6127  * @vsi: VSI to be configured
6128  *
6129  * Configures queue channel mapping to the given TCs
6130  **/
6131 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6132 {
6133 	struct i40e_channel *ch;
6134 	u64 max_rate = 0;
6135 	int ret = 0, i;
6136 
6137 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6138 	vsi->tc_seid_map[0] = vsi->seid;
6139 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6140 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6141 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6142 			if (!ch) {
6143 				ret = -ENOMEM;
6144 				goto err_free;
6145 			}
6146 
6147 			INIT_LIST_HEAD(&ch->list);
6148 			ch->num_queue_pairs =
6149 				vsi->tc_config.tc_info[i].qcount;
6150 			ch->base_queue =
6151 				vsi->tc_config.tc_info[i].qoffset;
6152 
6153 			/* Bandwidth limit through tc interface is in bytes/s,
6154 			 * change to Mbit/s
6155 			 */
6156 			max_rate = vsi->mqprio_qopt.max_rate[i];
6157 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6158 			ch->max_tx_rate = max_rate;
6159 
6160 			list_add_tail(&ch->list, &vsi->ch_list);
6161 
6162 			ret = i40e_create_queue_channel(vsi, ch);
6163 			if (ret) {
6164 				dev_err(&vsi->back->pdev->dev,
6165 					"Failed creating queue channel with TC%d: queues %d\n",
6166 					i, ch->num_queue_pairs);
6167 				goto err_free;
6168 			}
6169 			vsi->tc_seid_map[i] = ch->seid;
6170 		}
6171 	}
6172 	return ret;
6173 
6174 err_free:
6175 	i40e_remove_queue_channels(vsi);
6176 	return ret;
6177 }
6178 
6179 /**
6180  * i40e_veb_config_tc - Configure TCs for given VEB
6181  * @veb: given VEB
6182  * @enabled_tc: TC bitmap
6183  *
6184  * Configures given TC bitmap for VEB (switching) element
6185  **/
6186 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6187 {
6188 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6189 	struct i40e_pf *pf = veb->pf;
6190 	int ret = 0;
6191 	int i;
6192 
6193 	/* No TCs or already enabled TCs just return */
6194 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6195 		return ret;
6196 
6197 	bw_data.tc_valid_bits = enabled_tc;
6198 	/* bw_data.absolute_credits is not set (relative) */
6199 
6200 	/* Enable ETS TCs with equal BW Share for now */
6201 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6202 		if (enabled_tc & BIT(i))
6203 			bw_data.tc_bw_share_credits[i] = 1;
6204 	}
6205 
6206 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6207 						   &bw_data, NULL);
6208 	if (ret) {
6209 		dev_info(&pf->pdev->dev,
6210 			 "VEB bw config failed, err %s aq_err %s\n",
6211 			 i40e_stat_str(&pf->hw, ret),
6212 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6213 		goto out;
6214 	}
6215 
6216 	/* Update the BW information */
6217 	ret = i40e_veb_get_bw_info(veb);
6218 	if (ret) {
6219 		dev_info(&pf->pdev->dev,
6220 			 "Failed getting veb bw config, err %s aq_err %s\n",
6221 			 i40e_stat_str(&pf->hw, ret),
6222 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6223 	}
6224 
6225 out:
6226 	return ret;
6227 }
6228 
6229 #ifdef CONFIG_I40E_DCB
6230 /**
6231  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6232  * @pf: PF struct
6233  *
6234  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6235  * the caller would've quiesce all the VSIs before calling
6236  * this function
6237  **/
6238 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6239 {
6240 	u8 tc_map = 0;
6241 	int ret;
6242 	u8 v;
6243 
6244 	/* Enable the TCs available on PF to all VEBs */
6245 	tc_map = i40e_pf_get_tc_map(pf);
6246 	for (v = 0; v < I40E_MAX_VEB; v++) {
6247 		if (!pf->veb[v])
6248 			continue;
6249 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6250 		if (ret) {
6251 			dev_info(&pf->pdev->dev,
6252 				 "Failed configuring TC for VEB seid=%d\n",
6253 				 pf->veb[v]->seid);
6254 			/* Will try to configure as many components */
6255 		}
6256 	}
6257 
6258 	/* Update each VSI */
6259 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6260 		if (!pf->vsi[v])
6261 			continue;
6262 
6263 		/* - Enable all TCs for the LAN VSI
6264 		 * - For all others keep them at TC0 for now
6265 		 */
6266 		if (v == pf->lan_vsi)
6267 			tc_map = i40e_pf_get_tc_map(pf);
6268 		else
6269 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6270 
6271 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6272 		if (ret) {
6273 			dev_info(&pf->pdev->dev,
6274 				 "Failed configuring TC for VSI seid=%d\n",
6275 				 pf->vsi[v]->seid);
6276 			/* Will try to configure as many components */
6277 		} else {
6278 			/* Re-configure VSI vectors based on updated TC map */
6279 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6280 			if (pf->vsi[v]->netdev)
6281 				i40e_dcbnl_set_all(pf->vsi[v]);
6282 		}
6283 	}
6284 }
6285 
6286 /**
6287  * i40e_resume_port_tx - Resume port Tx
6288  * @pf: PF struct
6289  *
6290  * Resume a port's Tx and issue a PF reset in case of failure to
6291  * resume.
6292  **/
6293 static int i40e_resume_port_tx(struct i40e_pf *pf)
6294 {
6295 	struct i40e_hw *hw = &pf->hw;
6296 	int ret;
6297 
6298 	ret = i40e_aq_resume_port_tx(hw, NULL);
6299 	if (ret) {
6300 		dev_info(&pf->pdev->dev,
6301 			 "Resume Port Tx failed, err %s aq_err %s\n",
6302 			  i40e_stat_str(&pf->hw, ret),
6303 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6304 		/* Schedule PF reset to recover */
6305 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6306 		i40e_service_event_schedule(pf);
6307 	}
6308 
6309 	return ret;
6310 }
6311 
6312 /**
6313  * i40e_init_pf_dcb - Initialize DCB configuration
6314  * @pf: PF being configured
6315  *
6316  * Query the current DCB configuration and cache it
6317  * in the hardware structure
6318  **/
6319 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6320 {
6321 	struct i40e_hw *hw = &pf->hw;
6322 	int err = 0;
6323 
6324 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6325 	 * Also do not enable DCBx if FW LLDP agent is disabled
6326 	 */
6327 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6328 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
6329 		goto out;
6330 
6331 	/* Get the initial DCB configuration */
6332 	err = i40e_init_dcb(hw);
6333 	if (!err) {
6334 		/* Device/Function is not DCBX capable */
6335 		if ((!hw->func_caps.dcb) ||
6336 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6337 			dev_info(&pf->pdev->dev,
6338 				 "DCBX offload is not supported or is disabled for this PF.\n");
6339 		} else {
6340 			/* When status is not DISABLED then DCBX in FW */
6341 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6342 				       DCB_CAP_DCBX_VER_IEEE;
6343 
6344 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6345 			/* Enable DCB tagging only when more than one TC
6346 			 * or explicitly disable if only one TC
6347 			 */
6348 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6349 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6350 			else
6351 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6352 			dev_dbg(&pf->pdev->dev,
6353 				"DCBX offload is supported for this PF.\n");
6354 		}
6355 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6356 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6357 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6358 	} else {
6359 		dev_info(&pf->pdev->dev,
6360 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6361 			 i40e_stat_str(&pf->hw, err),
6362 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6363 	}
6364 
6365 out:
6366 	return err;
6367 }
6368 #endif /* CONFIG_I40E_DCB */
6369 #define SPEED_SIZE 14
6370 #define FC_SIZE 8
6371 /**
6372  * i40e_print_link_message - print link up or down
6373  * @vsi: the VSI for which link needs a message
6374  * @isup: true of link is up, false otherwise
6375  */
6376 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6377 {
6378 	enum i40e_aq_link_speed new_speed;
6379 	struct i40e_pf *pf = vsi->back;
6380 	char *speed = "Unknown";
6381 	char *fc = "Unknown";
6382 	char *fec = "";
6383 	char *req_fec = "";
6384 	char *an = "";
6385 
6386 	new_speed = pf->hw.phy.link_info.link_speed;
6387 
6388 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6389 		return;
6390 	vsi->current_isup = isup;
6391 	vsi->current_speed = new_speed;
6392 	if (!isup) {
6393 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6394 		return;
6395 	}
6396 
6397 	/* Warn user if link speed on NPAR enabled partition is not at
6398 	 * least 10GB
6399 	 */
6400 	if (pf->hw.func_caps.npar_enable &&
6401 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6402 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6403 		netdev_warn(vsi->netdev,
6404 			    "The partition detected link speed that is less than 10Gbps\n");
6405 
6406 	switch (pf->hw.phy.link_info.link_speed) {
6407 	case I40E_LINK_SPEED_40GB:
6408 		speed = "40 G";
6409 		break;
6410 	case I40E_LINK_SPEED_20GB:
6411 		speed = "20 G";
6412 		break;
6413 	case I40E_LINK_SPEED_25GB:
6414 		speed = "25 G";
6415 		break;
6416 	case I40E_LINK_SPEED_10GB:
6417 		speed = "10 G";
6418 		break;
6419 	case I40E_LINK_SPEED_1GB:
6420 		speed = "1000 M";
6421 		break;
6422 	case I40E_LINK_SPEED_100MB:
6423 		speed = "100 M";
6424 		break;
6425 	default:
6426 		break;
6427 	}
6428 
6429 	switch (pf->hw.fc.current_mode) {
6430 	case I40E_FC_FULL:
6431 		fc = "RX/TX";
6432 		break;
6433 	case I40E_FC_TX_PAUSE:
6434 		fc = "TX";
6435 		break;
6436 	case I40E_FC_RX_PAUSE:
6437 		fc = "RX";
6438 		break;
6439 	default:
6440 		fc = "None";
6441 		break;
6442 	}
6443 
6444 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6445 		req_fec = ", Requested FEC: None";
6446 		fec = ", FEC: None";
6447 		an = ", Autoneg: False";
6448 
6449 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6450 			an = ", Autoneg: True";
6451 
6452 		if (pf->hw.phy.link_info.fec_info &
6453 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6454 			fec = ", FEC: CL74 FC-FEC/BASE-R";
6455 		else if (pf->hw.phy.link_info.fec_info &
6456 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6457 			fec = ", FEC: CL108 RS-FEC";
6458 
6459 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6460 		 * both RS and FC are requested
6461 		 */
6462 		if (vsi->back->hw.phy.link_info.req_fec_info &
6463 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6464 			if (vsi->back->hw.phy.link_info.req_fec_info &
6465 			    I40E_AQ_REQUEST_FEC_RS)
6466 				req_fec = ", Requested FEC: CL108 RS-FEC";
6467 			else
6468 				req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6469 		}
6470 	}
6471 
6472 	netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6473 		    speed, req_fec, fec, an, fc);
6474 }
6475 
6476 /**
6477  * i40e_up_complete - Finish the last steps of bringing up a connection
6478  * @vsi: the VSI being configured
6479  **/
6480 static int i40e_up_complete(struct i40e_vsi *vsi)
6481 {
6482 	struct i40e_pf *pf = vsi->back;
6483 	int err;
6484 
6485 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6486 		i40e_vsi_configure_msix(vsi);
6487 	else
6488 		i40e_configure_msi_and_legacy(vsi);
6489 
6490 	/* start rings */
6491 	err = i40e_vsi_start_rings(vsi);
6492 	if (err)
6493 		return err;
6494 
6495 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6496 	i40e_napi_enable_all(vsi);
6497 	i40e_vsi_enable_irq(vsi);
6498 
6499 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6500 	    (vsi->netdev)) {
6501 		i40e_print_link_message(vsi, true);
6502 		netif_tx_start_all_queues(vsi->netdev);
6503 		netif_carrier_on(vsi->netdev);
6504 	}
6505 
6506 	/* replay FDIR SB filters */
6507 	if (vsi->type == I40E_VSI_FDIR) {
6508 		/* reset fd counters */
6509 		pf->fd_add_err = 0;
6510 		pf->fd_atr_cnt = 0;
6511 		i40e_fdir_filter_restore(vsi);
6512 	}
6513 
6514 	/* On the next run of the service_task, notify any clients of the new
6515 	 * opened netdev
6516 	 */
6517 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6518 	i40e_service_event_schedule(pf);
6519 
6520 	return 0;
6521 }
6522 
6523 /**
6524  * i40e_vsi_reinit_locked - Reset the VSI
6525  * @vsi: the VSI being configured
6526  *
6527  * Rebuild the ring structs after some configuration
6528  * has changed, e.g. MTU size.
6529  **/
6530 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6531 {
6532 	struct i40e_pf *pf = vsi->back;
6533 
6534 	WARN_ON(in_interrupt());
6535 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6536 		usleep_range(1000, 2000);
6537 	i40e_down(vsi);
6538 
6539 	i40e_up(vsi);
6540 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6541 }
6542 
6543 /**
6544  * i40e_up - Bring the connection back up after being down
6545  * @vsi: the VSI being configured
6546  **/
6547 int i40e_up(struct i40e_vsi *vsi)
6548 {
6549 	int err;
6550 
6551 	err = i40e_vsi_configure(vsi);
6552 	if (!err)
6553 		err = i40e_up_complete(vsi);
6554 
6555 	return err;
6556 }
6557 
6558 /**
6559  * i40e_force_link_state - Force the link status
6560  * @pf: board private structure
6561  * @is_up: whether the link state should be forced up or down
6562  **/
6563 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6564 {
6565 	struct i40e_aq_get_phy_abilities_resp abilities;
6566 	struct i40e_aq_set_phy_config config = {0};
6567 	struct i40e_hw *hw = &pf->hw;
6568 	i40e_status err;
6569 	u64 mask;
6570 
6571 	/* Get the current phy config */
6572 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6573 					   NULL);
6574 	if (err) {
6575 		dev_err(&pf->pdev->dev,
6576 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6577 			i40e_stat_str(hw, err),
6578 			i40e_aq_str(hw, hw->aq.asq_last_status));
6579 		return err;
6580 	}
6581 
6582 	/* If link needs to go up, but was not forced to go down,
6583 	 * no need for a flap
6584 	 */
6585 	if (is_up && abilities.phy_type != 0)
6586 		return I40E_SUCCESS;
6587 
6588 	/* To force link we need to set bits for all supported PHY types,
6589 	 * but there are now more than 32, so we need to split the bitmap
6590 	 * across two fields.
6591 	 */
6592 	mask = I40E_PHY_TYPES_BITMASK;
6593 	config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6594 	config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6595 	/* Copy the old settings, except of phy_type */
6596 	config.abilities = abilities.abilities;
6597 	config.link_speed = abilities.link_speed;
6598 	config.eee_capability = abilities.eee_capability;
6599 	config.eeer = abilities.eeer_val;
6600 	config.low_power_ctrl = abilities.d3_lpan;
6601 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6602 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6603 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6604 
6605 	if (err) {
6606 		dev_err(&pf->pdev->dev,
6607 			"set phy config ret =  %s last_status =  %s\n",
6608 			i40e_stat_str(&pf->hw, err),
6609 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6610 		return err;
6611 	}
6612 
6613 	/* Update the link info */
6614 	err = i40e_update_link_info(hw);
6615 	if (err) {
6616 		/* Wait a little bit (on 40G cards it sometimes takes a really
6617 		 * long time for link to come back from the atomic reset)
6618 		 * and try once more
6619 		 */
6620 		msleep(1000);
6621 		i40e_update_link_info(hw);
6622 	}
6623 
6624 	i40e_aq_set_link_restart_an(hw, true, NULL);
6625 
6626 	return I40E_SUCCESS;
6627 }
6628 
6629 /**
6630  * i40e_down - Shutdown the connection processing
6631  * @vsi: the VSI being stopped
6632  **/
6633 void i40e_down(struct i40e_vsi *vsi)
6634 {
6635 	int i;
6636 
6637 	/* It is assumed that the caller of this function
6638 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6639 	 */
6640 	if (vsi->netdev) {
6641 		netif_carrier_off(vsi->netdev);
6642 		netif_tx_disable(vsi->netdev);
6643 	}
6644 	i40e_vsi_disable_irq(vsi);
6645 	i40e_vsi_stop_rings(vsi);
6646 	if (vsi->type == I40E_VSI_MAIN &&
6647 	    vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6648 		i40e_force_link_state(vsi->back, false);
6649 	i40e_napi_disable_all(vsi);
6650 
6651 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6652 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6653 		if (i40e_enabled_xdp_vsi(vsi))
6654 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6655 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6656 	}
6657 
6658 }
6659 
6660 /**
6661  * i40e_validate_mqprio_qopt- validate queue mapping info
6662  * @vsi: the VSI being configured
6663  * @mqprio_qopt: queue parametrs
6664  **/
6665 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6666 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6667 {
6668 	u64 sum_max_rate = 0;
6669 	u64 max_rate = 0;
6670 	int i;
6671 
6672 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6673 	    mqprio_qopt->qopt.num_tc < 1 ||
6674 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6675 		return -EINVAL;
6676 	for (i = 0; ; i++) {
6677 		if (!mqprio_qopt->qopt.count[i])
6678 			return -EINVAL;
6679 		if (mqprio_qopt->min_rate[i]) {
6680 			dev_err(&vsi->back->pdev->dev,
6681 				"Invalid min tx rate (greater than 0) specified\n");
6682 			return -EINVAL;
6683 		}
6684 		max_rate = mqprio_qopt->max_rate[i];
6685 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6686 		sum_max_rate += max_rate;
6687 
6688 		if (i >= mqprio_qopt->qopt.num_tc - 1)
6689 			break;
6690 		if (mqprio_qopt->qopt.offset[i + 1] !=
6691 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6692 			return -EINVAL;
6693 	}
6694 	if (vsi->num_queue_pairs <
6695 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6696 		return -EINVAL;
6697 	}
6698 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
6699 		dev_err(&vsi->back->pdev->dev,
6700 			"Invalid max tx rate specified\n");
6701 		return -EINVAL;
6702 	}
6703 	return 0;
6704 }
6705 
6706 /**
6707  * i40e_vsi_set_default_tc_config - set default values for tc configuration
6708  * @vsi: the VSI being configured
6709  **/
6710 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6711 {
6712 	u16 qcount;
6713 	int i;
6714 
6715 	/* Only TC0 is enabled */
6716 	vsi->tc_config.numtc = 1;
6717 	vsi->tc_config.enabled_tc = 1;
6718 	qcount = min_t(int, vsi->alloc_queue_pairs,
6719 		       i40e_pf_get_max_q_per_tc(vsi->back));
6720 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6721 		/* For the TC that is not enabled set the offset to to default
6722 		 * queue and allocate one queue for the given TC.
6723 		 */
6724 		vsi->tc_config.tc_info[i].qoffset = 0;
6725 		if (i == 0)
6726 			vsi->tc_config.tc_info[i].qcount = qcount;
6727 		else
6728 			vsi->tc_config.tc_info[i].qcount = 1;
6729 		vsi->tc_config.tc_info[i].netdev_tc = 0;
6730 	}
6731 }
6732 
6733 /**
6734  * i40e_setup_tc - configure multiple traffic classes
6735  * @netdev: net device to configure
6736  * @type_data: tc offload data
6737  **/
6738 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6739 {
6740 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6741 	struct i40e_netdev_priv *np = netdev_priv(netdev);
6742 	struct i40e_vsi *vsi = np->vsi;
6743 	struct i40e_pf *pf = vsi->back;
6744 	u8 enabled_tc = 0, num_tc, hw;
6745 	bool need_reset = false;
6746 	int ret = -EINVAL;
6747 	u16 mode;
6748 	int i;
6749 
6750 	num_tc = mqprio_qopt->qopt.num_tc;
6751 	hw = mqprio_qopt->qopt.hw;
6752 	mode = mqprio_qopt->mode;
6753 	if (!hw) {
6754 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6755 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6756 		goto config_tc;
6757 	}
6758 
6759 	/* Check if MFP enabled */
6760 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6761 		netdev_info(netdev,
6762 			    "Configuring TC not supported in MFP mode\n");
6763 		return ret;
6764 	}
6765 	switch (mode) {
6766 	case TC_MQPRIO_MODE_DCB:
6767 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6768 
6769 		/* Check if DCB enabled to continue */
6770 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6771 			netdev_info(netdev,
6772 				    "DCB is not enabled for adapter\n");
6773 			return ret;
6774 		}
6775 
6776 		/* Check whether tc count is within enabled limit */
6777 		if (num_tc > i40e_pf_get_num_tc(pf)) {
6778 			netdev_info(netdev,
6779 				    "TC count greater than enabled on link for adapter\n");
6780 			return ret;
6781 		}
6782 		break;
6783 	case TC_MQPRIO_MODE_CHANNEL:
6784 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6785 			netdev_info(netdev,
6786 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6787 			return ret;
6788 		}
6789 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6790 			return ret;
6791 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6792 		if (ret)
6793 			return ret;
6794 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6795 		       sizeof(*mqprio_qopt));
6796 		pf->flags |= I40E_FLAG_TC_MQPRIO;
6797 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6798 		break;
6799 	default:
6800 		return -EINVAL;
6801 	}
6802 
6803 config_tc:
6804 	/* Generate TC map for number of tc requested */
6805 	for (i = 0; i < num_tc; i++)
6806 		enabled_tc |= BIT(i);
6807 
6808 	/* Requesting same TC configuration as already enabled */
6809 	if (enabled_tc == vsi->tc_config.enabled_tc &&
6810 	    mode != TC_MQPRIO_MODE_CHANNEL)
6811 		return 0;
6812 
6813 	/* Quiesce VSI queues */
6814 	i40e_quiesce_vsi(vsi);
6815 
6816 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6817 		i40e_remove_queue_channels(vsi);
6818 
6819 	/* Configure VSI for enabled TCs */
6820 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
6821 	if (ret) {
6822 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6823 			    vsi->seid);
6824 		need_reset = true;
6825 		goto exit;
6826 	}
6827 
6828 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6829 		if (vsi->mqprio_qopt.max_rate[0]) {
6830 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6831 
6832 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6833 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6834 			if (!ret) {
6835 				u64 credits = max_tx_rate;
6836 
6837 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
6838 				dev_dbg(&vsi->back->pdev->dev,
6839 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6840 					max_tx_rate,
6841 					credits,
6842 					vsi->seid);
6843 			} else {
6844 				need_reset = true;
6845 				goto exit;
6846 			}
6847 		}
6848 		ret = i40e_configure_queue_channels(vsi);
6849 		if (ret) {
6850 			netdev_info(netdev,
6851 				    "Failed configuring queue channels\n");
6852 			need_reset = true;
6853 			goto exit;
6854 		}
6855 	}
6856 
6857 exit:
6858 	/* Reset the configuration data to defaults, only TC0 is enabled */
6859 	if (need_reset) {
6860 		i40e_vsi_set_default_tc_config(vsi);
6861 		need_reset = false;
6862 	}
6863 
6864 	/* Unquiesce VSI */
6865 	i40e_unquiesce_vsi(vsi);
6866 	return ret;
6867 }
6868 
6869 /**
6870  * i40e_set_cld_element - sets cloud filter element data
6871  * @filter: cloud filter rule
6872  * @cld: ptr to cloud filter element data
6873  *
6874  * This is helper function to copy data into cloud filter element
6875  **/
6876 static inline void
6877 i40e_set_cld_element(struct i40e_cloud_filter *filter,
6878 		     struct i40e_aqc_cloud_filters_element_data *cld)
6879 {
6880 	int i, j;
6881 	u32 ipa;
6882 
6883 	memset(cld, 0, sizeof(*cld));
6884 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
6885 	ether_addr_copy(cld->inner_mac, filter->src_mac);
6886 
6887 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6888 		return;
6889 
6890 	if (filter->n_proto == ETH_P_IPV6) {
6891 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
6892 		for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6893 		     i++, j += 2) {
6894 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6895 			ipa = cpu_to_le32(ipa);
6896 			memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6897 		}
6898 	} else {
6899 		ipa = be32_to_cpu(filter->dst_ipv4);
6900 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6901 	}
6902 
6903 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6904 
6905 	/* tenant_id is not supported by FW now, once the support is enabled
6906 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6907 	 */
6908 	if (filter->tenant_id)
6909 		return;
6910 }
6911 
6912 /**
6913  * i40e_add_del_cloud_filter - Add/del cloud filter
6914  * @vsi: pointer to VSI
6915  * @filter: cloud filter rule
6916  * @add: if true, add, if false, delete
6917  *
6918  * Add or delete a cloud filter for a specific flow spec.
6919  * Returns 0 if the filter were successfully added.
6920  **/
6921 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
6922 			      struct i40e_cloud_filter *filter, bool add)
6923 {
6924 	struct i40e_aqc_cloud_filters_element_data cld_filter;
6925 	struct i40e_pf *pf = vsi->back;
6926 	int ret;
6927 	static const u16 flag_table[128] = {
6928 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
6929 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
6930 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
6931 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
6932 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
6933 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
6934 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
6935 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
6936 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
6937 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
6938 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
6939 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
6940 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
6941 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
6942 	};
6943 
6944 	if (filter->flags >= ARRAY_SIZE(flag_table))
6945 		return I40E_ERR_CONFIG;
6946 
6947 	/* copy element needed to add cloud filter from filter */
6948 	i40e_set_cld_element(filter, &cld_filter);
6949 
6950 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
6951 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
6952 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
6953 
6954 	if (filter->n_proto == ETH_P_IPV6)
6955 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6956 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6957 	else
6958 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6959 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6960 
6961 	if (add)
6962 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
6963 						&cld_filter, 1);
6964 	else
6965 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
6966 						&cld_filter, 1);
6967 	if (ret)
6968 		dev_dbg(&pf->pdev->dev,
6969 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
6970 			add ? "add" : "delete", filter->dst_port, ret,
6971 			pf->hw.aq.asq_last_status);
6972 	else
6973 		dev_info(&pf->pdev->dev,
6974 			 "%s cloud filter for VSI: %d\n",
6975 			 add ? "Added" : "Deleted", filter->seid);
6976 	return ret;
6977 }
6978 
6979 /**
6980  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
6981  * @vsi: pointer to VSI
6982  * @filter: cloud filter rule
6983  * @add: if true, add, if false, delete
6984  *
6985  * Add or delete a cloud filter for a specific flow spec using big buffer.
6986  * Returns 0 if the filter were successfully added.
6987  **/
6988 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
6989 				      struct i40e_cloud_filter *filter,
6990 				      bool add)
6991 {
6992 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
6993 	struct i40e_pf *pf = vsi->back;
6994 	int ret;
6995 
6996 	/* Both (src/dst) valid mac_addr are not supported */
6997 	if ((is_valid_ether_addr(filter->dst_mac) &&
6998 	     is_valid_ether_addr(filter->src_mac)) ||
6999 	    (is_multicast_ether_addr(filter->dst_mac) &&
7000 	     is_multicast_ether_addr(filter->src_mac)))
7001 		return -EOPNOTSUPP;
7002 
7003 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7004 	 * ports are not supported via big buffer now.
7005 	 */
7006 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7007 		return -EOPNOTSUPP;
7008 
7009 	/* adding filter using src_port/src_ip is not supported at this stage */
7010 	if (filter->src_port || filter->src_ipv4 ||
7011 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7012 		return -EOPNOTSUPP;
7013 
7014 	/* copy element needed to add cloud filter from filter */
7015 	i40e_set_cld_element(filter, &cld_filter.element);
7016 
7017 	if (is_valid_ether_addr(filter->dst_mac) ||
7018 	    is_valid_ether_addr(filter->src_mac) ||
7019 	    is_multicast_ether_addr(filter->dst_mac) ||
7020 	    is_multicast_ether_addr(filter->src_mac)) {
7021 		/* MAC + IP : unsupported mode */
7022 		if (filter->dst_ipv4)
7023 			return -EOPNOTSUPP;
7024 
7025 		/* since we validated that L4 port must be valid before
7026 		 * we get here, start with respective "flags" value
7027 		 * and update if vlan is present or not
7028 		 */
7029 		cld_filter.element.flags =
7030 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7031 
7032 		if (filter->vlan_id) {
7033 			cld_filter.element.flags =
7034 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7035 		}
7036 
7037 	} else if (filter->dst_ipv4 ||
7038 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7039 		cld_filter.element.flags =
7040 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7041 		if (filter->n_proto == ETH_P_IPV6)
7042 			cld_filter.element.flags |=
7043 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7044 		else
7045 			cld_filter.element.flags |=
7046 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7047 	} else {
7048 		dev_err(&pf->pdev->dev,
7049 			"either mac or ip has to be valid for cloud filter\n");
7050 		return -EINVAL;
7051 	}
7052 
7053 	/* Now copy L4 port in Byte 6..7 in general fields */
7054 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7055 						be16_to_cpu(filter->dst_port);
7056 
7057 	if (add) {
7058 		/* Validate current device switch mode, change if necessary */
7059 		ret = i40e_validate_and_set_switch_mode(vsi);
7060 		if (ret) {
7061 			dev_err(&pf->pdev->dev,
7062 				"failed to set switch mode, ret %d\n",
7063 				ret);
7064 			return ret;
7065 		}
7066 
7067 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7068 						   &cld_filter, 1);
7069 	} else {
7070 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7071 						   &cld_filter, 1);
7072 	}
7073 
7074 	if (ret)
7075 		dev_dbg(&pf->pdev->dev,
7076 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7077 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7078 	else
7079 		dev_info(&pf->pdev->dev,
7080 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7081 			 add ? "add" : "delete", filter->seid,
7082 			 ntohs(filter->dst_port));
7083 	return ret;
7084 }
7085 
7086 /**
7087  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7088  * @vsi: Pointer to VSI
7089  * @cls_flower: Pointer to struct tc_cls_flower_offload
7090  * @filter: Pointer to cloud filter structure
7091  *
7092  **/
7093 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7094 				 struct tc_cls_flower_offload *f,
7095 				 struct i40e_cloud_filter *filter)
7096 {
7097 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7098 	struct i40e_pf *pf = vsi->back;
7099 	u8 field_flags = 0;
7100 
7101 	if (f->dissector->used_keys &
7102 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7103 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7104 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7105 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7106 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7107 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7108 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7109 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7110 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7111 			f->dissector->used_keys);
7112 		return -EOPNOTSUPP;
7113 	}
7114 
7115 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7116 		struct flow_dissector_key_keyid *key =
7117 			skb_flow_dissector_target(f->dissector,
7118 						  FLOW_DISSECTOR_KEY_ENC_KEYID,
7119 						  f->key);
7120 
7121 		struct flow_dissector_key_keyid *mask =
7122 			skb_flow_dissector_target(f->dissector,
7123 						  FLOW_DISSECTOR_KEY_ENC_KEYID,
7124 						  f->mask);
7125 
7126 		if (mask->keyid != 0)
7127 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7128 
7129 		filter->tenant_id = be32_to_cpu(key->keyid);
7130 	}
7131 
7132 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
7133 		struct flow_dissector_key_basic *key =
7134 			skb_flow_dissector_target(f->dissector,
7135 						  FLOW_DISSECTOR_KEY_BASIC,
7136 						  f->key);
7137 
7138 		struct flow_dissector_key_basic *mask =
7139 			skb_flow_dissector_target(f->dissector,
7140 						  FLOW_DISSECTOR_KEY_BASIC,
7141 						  f->mask);
7142 
7143 		n_proto_key = ntohs(key->n_proto);
7144 		n_proto_mask = ntohs(mask->n_proto);
7145 
7146 		if (n_proto_key == ETH_P_ALL) {
7147 			n_proto_key = 0;
7148 			n_proto_mask = 0;
7149 		}
7150 		filter->n_proto = n_proto_key & n_proto_mask;
7151 		filter->ip_proto = key->ip_proto;
7152 	}
7153 
7154 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7155 		struct flow_dissector_key_eth_addrs *key =
7156 			skb_flow_dissector_target(f->dissector,
7157 						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
7158 						  f->key);
7159 
7160 		struct flow_dissector_key_eth_addrs *mask =
7161 			skb_flow_dissector_target(f->dissector,
7162 						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
7163 						  f->mask);
7164 
7165 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7166 		if (!is_zero_ether_addr(mask->dst)) {
7167 			if (is_broadcast_ether_addr(mask->dst)) {
7168 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7169 			} else {
7170 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7171 					mask->dst);
7172 				return I40E_ERR_CONFIG;
7173 			}
7174 		}
7175 
7176 		if (!is_zero_ether_addr(mask->src)) {
7177 			if (is_broadcast_ether_addr(mask->src)) {
7178 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7179 			} else {
7180 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7181 					mask->src);
7182 				return I40E_ERR_CONFIG;
7183 			}
7184 		}
7185 		ether_addr_copy(filter->dst_mac, key->dst);
7186 		ether_addr_copy(filter->src_mac, key->src);
7187 	}
7188 
7189 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
7190 		struct flow_dissector_key_vlan *key =
7191 			skb_flow_dissector_target(f->dissector,
7192 						  FLOW_DISSECTOR_KEY_VLAN,
7193 						  f->key);
7194 		struct flow_dissector_key_vlan *mask =
7195 			skb_flow_dissector_target(f->dissector,
7196 						  FLOW_DISSECTOR_KEY_VLAN,
7197 						  f->mask);
7198 
7199 		if (mask->vlan_id) {
7200 			if (mask->vlan_id == VLAN_VID_MASK) {
7201 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7202 
7203 			} else {
7204 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7205 					mask->vlan_id);
7206 				return I40E_ERR_CONFIG;
7207 			}
7208 		}
7209 
7210 		filter->vlan_id = cpu_to_be16(key->vlan_id);
7211 	}
7212 
7213 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
7214 		struct flow_dissector_key_control *key =
7215 			skb_flow_dissector_target(f->dissector,
7216 						  FLOW_DISSECTOR_KEY_CONTROL,
7217 						  f->key);
7218 
7219 		addr_type = key->addr_type;
7220 	}
7221 
7222 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7223 		struct flow_dissector_key_ipv4_addrs *key =
7224 			skb_flow_dissector_target(f->dissector,
7225 						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7226 						  f->key);
7227 		struct flow_dissector_key_ipv4_addrs *mask =
7228 			skb_flow_dissector_target(f->dissector,
7229 						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7230 						  f->mask);
7231 
7232 		if (mask->dst) {
7233 			if (mask->dst == cpu_to_be32(0xffffffff)) {
7234 				field_flags |= I40E_CLOUD_FIELD_IIP;
7235 			} else {
7236 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7237 					&mask->dst);
7238 				return I40E_ERR_CONFIG;
7239 			}
7240 		}
7241 
7242 		if (mask->src) {
7243 			if (mask->src == cpu_to_be32(0xffffffff)) {
7244 				field_flags |= I40E_CLOUD_FIELD_IIP;
7245 			} else {
7246 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7247 					&mask->src);
7248 				return I40E_ERR_CONFIG;
7249 			}
7250 		}
7251 
7252 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7253 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7254 			return I40E_ERR_CONFIG;
7255 		}
7256 		filter->dst_ipv4 = key->dst;
7257 		filter->src_ipv4 = key->src;
7258 	}
7259 
7260 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7261 		struct flow_dissector_key_ipv6_addrs *key =
7262 			skb_flow_dissector_target(f->dissector,
7263 						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7264 						  f->key);
7265 		struct flow_dissector_key_ipv6_addrs *mask =
7266 			skb_flow_dissector_target(f->dissector,
7267 						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7268 						  f->mask);
7269 
7270 		/* src and dest IPV6 address should not be LOOPBACK
7271 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7272 		 */
7273 		if (ipv6_addr_loopback(&key->dst) ||
7274 		    ipv6_addr_loopback(&key->src)) {
7275 			dev_err(&pf->pdev->dev,
7276 				"Bad ipv6, addr is LOOPBACK\n");
7277 			return I40E_ERR_CONFIG;
7278 		}
7279 		if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
7280 			field_flags |= I40E_CLOUD_FIELD_IIP;
7281 
7282 		memcpy(&filter->src_ipv6, &key->src.s6_addr32,
7283 		       sizeof(filter->src_ipv6));
7284 		memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
7285 		       sizeof(filter->dst_ipv6));
7286 	}
7287 
7288 	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
7289 		struct flow_dissector_key_ports *key =
7290 			skb_flow_dissector_target(f->dissector,
7291 						  FLOW_DISSECTOR_KEY_PORTS,
7292 						  f->key);
7293 		struct flow_dissector_key_ports *mask =
7294 			skb_flow_dissector_target(f->dissector,
7295 						  FLOW_DISSECTOR_KEY_PORTS,
7296 						  f->mask);
7297 
7298 		if (mask->src) {
7299 			if (mask->src == cpu_to_be16(0xffff)) {
7300 				field_flags |= I40E_CLOUD_FIELD_IIP;
7301 			} else {
7302 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7303 					be16_to_cpu(mask->src));
7304 				return I40E_ERR_CONFIG;
7305 			}
7306 		}
7307 
7308 		if (mask->dst) {
7309 			if (mask->dst == cpu_to_be16(0xffff)) {
7310 				field_flags |= I40E_CLOUD_FIELD_IIP;
7311 			} else {
7312 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7313 					be16_to_cpu(mask->dst));
7314 				return I40E_ERR_CONFIG;
7315 			}
7316 		}
7317 
7318 		filter->dst_port = key->dst;
7319 		filter->src_port = key->src;
7320 
7321 		switch (filter->ip_proto) {
7322 		case IPPROTO_TCP:
7323 		case IPPROTO_UDP:
7324 			break;
7325 		default:
7326 			dev_err(&pf->pdev->dev,
7327 				"Only UDP and TCP transport are supported\n");
7328 			return -EINVAL;
7329 		}
7330 	}
7331 	filter->flags = field_flags;
7332 	return 0;
7333 }
7334 
7335 /**
7336  * i40e_handle_tclass: Forward to a traffic class on the device
7337  * @vsi: Pointer to VSI
7338  * @tc: traffic class index on the device
7339  * @filter: Pointer to cloud filter structure
7340  *
7341  **/
7342 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7343 			      struct i40e_cloud_filter *filter)
7344 {
7345 	struct i40e_channel *ch, *ch_tmp;
7346 
7347 	/* direct to a traffic class on the same device */
7348 	if (tc == 0) {
7349 		filter->seid = vsi->seid;
7350 		return 0;
7351 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7352 		if (!filter->dst_port) {
7353 			dev_err(&vsi->back->pdev->dev,
7354 				"Specify destination port to direct to traffic class that is not default\n");
7355 			return -EINVAL;
7356 		}
7357 		if (list_empty(&vsi->ch_list))
7358 			return -EINVAL;
7359 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7360 					 list) {
7361 			if (ch->seid == vsi->tc_seid_map[tc])
7362 				filter->seid = ch->seid;
7363 		}
7364 		return 0;
7365 	}
7366 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7367 	return -EINVAL;
7368 }
7369 
7370 /**
7371  * i40e_configure_clsflower - Configure tc flower filters
7372  * @vsi: Pointer to VSI
7373  * @cls_flower: Pointer to struct tc_cls_flower_offload
7374  *
7375  **/
7376 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7377 				    struct tc_cls_flower_offload *cls_flower)
7378 {
7379 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7380 	struct i40e_cloud_filter *filter = NULL;
7381 	struct i40e_pf *pf = vsi->back;
7382 	int err = 0;
7383 
7384 	if (tc < 0) {
7385 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7386 		return -EOPNOTSUPP;
7387 	}
7388 
7389 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7390 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7391 		return -EBUSY;
7392 
7393 	if (pf->fdir_pf_active_filters ||
7394 	    (!hlist_empty(&pf->fdir_filter_list))) {
7395 		dev_err(&vsi->back->pdev->dev,
7396 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7397 		return -EINVAL;
7398 	}
7399 
7400 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7401 		dev_err(&vsi->back->pdev->dev,
7402 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7403 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7404 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7405 	}
7406 
7407 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7408 	if (!filter)
7409 		return -ENOMEM;
7410 
7411 	filter->cookie = cls_flower->cookie;
7412 
7413 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7414 	if (err < 0)
7415 		goto err;
7416 
7417 	err = i40e_handle_tclass(vsi, tc, filter);
7418 	if (err < 0)
7419 		goto err;
7420 
7421 	/* Add cloud filter */
7422 	if (filter->dst_port)
7423 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7424 	else
7425 		err = i40e_add_del_cloud_filter(vsi, filter, true);
7426 
7427 	if (err) {
7428 		dev_err(&pf->pdev->dev,
7429 			"Failed to add cloud filter, err %s\n",
7430 			i40e_stat_str(&pf->hw, err));
7431 		goto err;
7432 	}
7433 
7434 	/* add filter to the ordered list */
7435 	INIT_HLIST_NODE(&filter->cloud_node);
7436 
7437 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7438 
7439 	pf->num_cloud_filters++;
7440 
7441 	return err;
7442 err:
7443 	kfree(filter);
7444 	return err;
7445 }
7446 
7447 /**
7448  * i40e_find_cloud_filter - Find the could filter in the list
7449  * @vsi: Pointer to VSI
7450  * @cookie: filter specific cookie
7451  *
7452  **/
7453 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7454 							unsigned long *cookie)
7455 {
7456 	struct i40e_cloud_filter *filter = NULL;
7457 	struct hlist_node *node2;
7458 
7459 	hlist_for_each_entry_safe(filter, node2,
7460 				  &vsi->back->cloud_filter_list, cloud_node)
7461 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7462 			return filter;
7463 	return NULL;
7464 }
7465 
7466 /**
7467  * i40e_delete_clsflower - Remove tc flower filters
7468  * @vsi: Pointer to VSI
7469  * @cls_flower: Pointer to struct tc_cls_flower_offload
7470  *
7471  **/
7472 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7473 				 struct tc_cls_flower_offload *cls_flower)
7474 {
7475 	struct i40e_cloud_filter *filter = NULL;
7476 	struct i40e_pf *pf = vsi->back;
7477 	int err = 0;
7478 
7479 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7480 
7481 	if (!filter)
7482 		return -EINVAL;
7483 
7484 	hash_del(&filter->cloud_node);
7485 
7486 	if (filter->dst_port)
7487 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7488 	else
7489 		err = i40e_add_del_cloud_filter(vsi, filter, false);
7490 
7491 	kfree(filter);
7492 	if (err) {
7493 		dev_err(&pf->pdev->dev,
7494 			"Failed to delete cloud filter, err %s\n",
7495 			i40e_stat_str(&pf->hw, err));
7496 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7497 	}
7498 
7499 	pf->num_cloud_filters--;
7500 	if (!pf->num_cloud_filters)
7501 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7502 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7503 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7504 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7505 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7506 		}
7507 	return 0;
7508 }
7509 
7510 /**
7511  * i40e_setup_tc_cls_flower - flower classifier offloads
7512  * @netdev: net device to configure
7513  * @type_data: offload data
7514  **/
7515 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7516 				    struct tc_cls_flower_offload *cls_flower)
7517 {
7518 	struct i40e_vsi *vsi = np->vsi;
7519 
7520 	switch (cls_flower->command) {
7521 	case TC_CLSFLOWER_REPLACE:
7522 		return i40e_configure_clsflower(vsi, cls_flower);
7523 	case TC_CLSFLOWER_DESTROY:
7524 		return i40e_delete_clsflower(vsi, cls_flower);
7525 	case TC_CLSFLOWER_STATS:
7526 		return -EOPNOTSUPP;
7527 	default:
7528 		return -EOPNOTSUPP;
7529 	}
7530 }
7531 
7532 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7533 				  void *cb_priv)
7534 {
7535 	struct i40e_netdev_priv *np = cb_priv;
7536 
7537 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
7538 		return -EOPNOTSUPP;
7539 
7540 	switch (type) {
7541 	case TC_SETUP_CLSFLOWER:
7542 		return i40e_setup_tc_cls_flower(np, type_data);
7543 
7544 	default:
7545 		return -EOPNOTSUPP;
7546 	}
7547 }
7548 
7549 static int i40e_setup_tc_block(struct net_device *dev,
7550 			       struct tc_block_offload *f)
7551 {
7552 	struct i40e_netdev_priv *np = netdev_priv(dev);
7553 
7554 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7555 		return -EOPNOTSUPP;
7556 
7557 	switch (f->command) {
7558 	case TC_BLOCK_BIND:
7559 		return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7560 					     np, np, f->extack);
7561 	case TC_BLOCK_UNBIND:
7562 		tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7563 		return 0;
7564 	default:
7565 		return -EOPNOTSUPP;
7566 	}
7567 }
7568 
7569 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7570 			   void *type_data)
7571 {
7572 	switch (type) {
7573 	case TC_SETUP_QDISC_MQPRIO:
7574 		return i40e_setup_tc(netdev, type_data);
7575 	case TC_SETUP_BLOCK:
7576 		return i40e_setup_tc_block(netdev, type_data);
7577 	default:
7578 		return -EOPNOTSUPP;
7579 	}
7580 }
7581 
7582 /**
7583  * i40e_open - Called when a network interface is made active
7584  * @netdev: network interface device structure
7585  *
7586  * The open entry point is called when a network interface is made
7587  * active by the system (IFF_UP).  At this point all resources needed
7588  * for transmit and receive operations are allocated, the interrupt
7589  * handler is registered with the OS, the netdev watchdog subtask is
7590  * enabled, and the stack is notified that the interface is ready.
7591  *
7592  * Returns 0 on success, negative value on failure
7593  **/
7594 int i40e_open(struct net_device *netdev)
7595 {
7596 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7597 	struct i40e_vsi *vsi = np->vsi;
7598 	struct i40e_pf *pf = vsi->back;
7599 	int err;
7600 
7601 	/* disallow open during test or if eeprom is broken */
7602 	if (test_bit(__I40E_TESTING, pf->state) ||
7603 	    test_bit(__I40E_BAD_EEPROM, pf->state))
7604 		return -EBUSY;
7605 
7606 	netif_carrier_off(netdev);
7607 
7608 	if (i40e_force_link_state(pf, true))
7609 		return -EAGAIN;
7610 
7611 	err = i40e_vsi_open(vsi);
7612 	if (err)
7613 		return err;
7614 
7615 	/* configure global TSO hardware offload settings */
7616 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7617 						       TCP_FLAG_FIN) >> 16);
7618 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7619 						       TCP_FLAG_FIN |
7620 						       TCP_FLAG_CWR) >> 16);
7621 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7622 
7623 	udp_tunnel_get_rx_info(netdev);
7624 
7625 	return 0;
7626 }
7627 
7628 /**
7629  * i40e_vsi_open -
7630  * @vsi: the VSI to open
7631  *
7632  * Finish initialization of the VSI.
7633  *
7634  * Returns 0 on success, negative value on failure
7635  *
7636  * Note: expects to be called while under rtnl_lock()
7637  **/
7638 int i40e_vsi_open(struct i40e_vsi *vsi)
7639 {
7640 	struct i40e_pf *pf = vsi->back;
7641 	char int_name[I40E_INT_NAME_STR_LEN];
7642 	int err;
7643 
7644 	/* allocate descriptors */
7645 	err = i40e_vsi_setup_tx_resources(vsi);
7646 	if (err)
7647 		goto err_setup_tx;
7648 	err = i40e_vsi_setup_rx_resources(vsi);
7649 	if (err)
7650 		goto err_setup_rx;
7651 
7652 	err = i40e_vsi_configure(vsi);
7653 	if (err)
7654 		goto err_setup_rx;
7655 
7656 	if (vsi->netdev) {
7657 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7658 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7659 		err = i40e_vsi_request_irq(vsi, int_name);
7660 		if (err)
7661 			goto err_setup_rx;
7662 
7663 		/* Notify the stack of the actual queue counts. */
7664 		err = netif_set_real_num_tx_queues(vsi->netdev,
7665 						   vsi->num_queue_pairs);
7666 		if (err)
7667 			goto err_set_queues;
7668 
7669 		err = netif_set_real_num_rx_queues(vsi->netdev,
7670 						   vsi->num_queue_pairs);
7671 		if (err)
7672 			goto err_set_queues;
7673 
7674 	} else if (vsi->type == I40E_VSI_FDIR) {
7675 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7676 			 dev_driver_string(&pf->pdev->dev),
7677 			 dev_name(&pf->pdev->dev));
7678 		err = i40e_vsi_request_irq(vsi, int_name);
7679 
7680 	} else {
7681 		err = -EINVAL;
7682 		goto err_setup_rx;
7683 	}
7684 
7685 	err = i40e_up_complete(vsi);
7686 	if (err)
7687 		goto err_up_complete;
7688 
7689 	return 0;
7690 
7691 err_up_complete:
7692 	i40e_down(vsi);
7693 err_set_queues:
7694 	i40e_vsi_free_irq(vsi);
7695 err_setup_rx:
7696 	i40e_vsi_free_rx_resources(vsi);
7697 err_setup_tx:
7698 	i40e_vsi_free_tx_resources(vsi);
7699 	if (vsi == pf->vsi[pf->lan_vsi])
7700 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7701 
7702 	return err;
7703 }
7704 
7705 /**
7706  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7707  * @pf: Pointer to PF
7708  *
7709  * This function destroys the hlist where all the Flow Director
7710  * filters were saved.
7711  **/
7712 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7713 {
7714 	struct i40e_fdir_filter *filter;
7715 	struct i40e_flex_pit *pit_entry, *tmp;
7716 	struct hlist_node *node2;
7717 
7718 	hlist_for_each_entry_safe(filter, node2,
7719 				  &pf->fdir_filter_list, fdir_node) {
7720 		hlist_del(&filter->fdir_node);
7721 		kfree(filter);
7722 	}
7723 
7724 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7725 		list_del(&pit_entry->list);
7726 		kfree(pit_entry);
7727 	}
7728 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7729 
7730 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7731 		list_del(&pit_entry->list);
7732 		kfree(pit_entry);
7733 	}
7734 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7735 
7736 	pf->fdir_pf_active_filters = 0;
7737 	pf->fd_tcp4_filter_cnt = 0;
7738 	pf->fd_udp4_filter_cnt = 0;
7739 	pf->fd_sctp4_filter_cnt = 0;
7740 	pf->fd_ip4_filter_cnt = 0;
7741 
7742 	/* Reprogram the default input set for TCP/IPv4 */
7743 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7744 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7745 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7746 
7747 	/* Reprogram the default input set for UDP/IPv4 */
7748 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7749 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7750 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7751 
7752 	/* Reprogram the default input set for SCTP/IPv4 */
7753 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7754 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7755 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7756 
7757 	/* Reprogram the default input set for Other/IPv4 */
7758 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7759 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7760 
7761 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7762 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7763 }
7764 
7765 /**
7766  * i40e_cloud_filter_exit - Cleans up the cloud filters
7767  * @pf: Pointer to PF
7768  *
7769  * This function destroys the hlist where all the cloud filters
7770  * were saved.
7771  **/
7772 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7773 {
7774 	struct i40e_cloud_filter *cfilter;
7775 	struct hlist_node *node;
7776 
7777 	hlist_for_each_entry_safe(cfilter, node,
7778 				  &pf->cloud_filter_list, cloud_node) {
7779 		hlist_del(&cfilter->cloud_node);
7780 		kfree(cfilter);
7781 	}
7782 	pf->num_cloud_filters = 0;
7783 
7784 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7785 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7786 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7787 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7788 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7789 	}
7790 }
7791 
7792 /**
7793  * i40e_close - Disables a network interface
7794  * @netdev: network interface device structure
7795  *
7796  * The close entry point is called when an interface is de-activated
7797  * by the OS.  The hardware is still under the driver's control, but
7798  * this netdev interface is disabled.
7799  *
7800  * Returns 0, this is not allowed to fail
7801  **/
7802 int i40e_close(struct net_device *netdev)
7803 {
7804 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7805 	struct i40e_vsi *vsi = np->vsi;
7806 
7807 	i40e_vsi_close(vsi);
7808 
7809 	return 0;
7810 }
7811 
7812 /**
7813  * i40e_do_reset - Start a PF or Core Reset sequence
7814  * @pf: board private structure
7815  * @reset_flags: which reset is requested
7816  * @lock_acquired: indicates whether or not the lock has been acquired
7817  * before this function was called.
7818  *
7819  * The essential difference in resets is that the PF Reset
7820  * doesn't clear the packet buffers, doesn't reset the PE
7821  * firmware, and doesn't bother the other PFs on the chip.
7822  **/
7823 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7824 {
7825 	u32 val;
7826 
7827 	WARN_ON(in_interrupt());
7828 
7829 
7830 	/* do the biggest reset indicated */
7831 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7832 
7833 		/* Request a Global Reset
7834 		 *
7835 		 * This will start the chip's countdown to the actual full
7836 		 * chip reset event, and a warning interrupt to be sent
7837 		 * to all PFs, including the requestor.  Our handler
7838 		 * for the warning interrupt will deal with the shutdown
7839 		 * and recovery of the switch setup.
7840 		 */
7841 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7842 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7843 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7844 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7845 
7846 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7847 
7848 		/* Request a Core Reset
7849 		 *
7850 		 * Same as Global Reset, except does *not* include the MAC/PHY
7851 		 */
7852 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7853 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7854 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
7855 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7856 		i40e_flush(&pf->hw);
7857 
7858 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
7859 
7860 		/* Request a PF Reset
7861 		 *
7862 		 * Resets only the PF-specific registers
7863 		 *
7864 		 * This goes directly to the tear-down and rebuild of
7865 		 * the switch, since we need to do all the recovery as
7866 		 * for the Core Reset.
7867 		 */
7868 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
7869 		i40e_handle_reset_warning(pf, lock_acquired);
7870 
7871 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7872 		int v;
7873 
7874 		/* Find the VSI(s) that requested a re-init */
7875 		dev_info(&pf->pdev->dev,
7876 			 "VSI reinit requested\n");
7877 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7878 			struct i40e_vsi *vsi = pf->vsi[v];
7879 
7880 			if (vsi != NULL &&
7881 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7882 					       vsi->state))
7883 				i40e_vsi_reinit_locked(pf->vsi[v]);
7884 		}
7885 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7886 		int v;
7887 
7888 		/* Find the VSI(s) that needs to be brought down */
7889 		dev_info(&pf->pdev->dev, "VSI down requested\n");
7890 		for (v = 0; v < pf->num_alloc_vsi; v++) {
7891 			struct i40e_vsi *vsi = pf->vsi[v];
7892 
7893 			if (vsi != NULL &&
7894 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7895 					       vsi->state)) {
7896 				set_bit(__I40E_VSI_DOWN, vsi->state);
7897 				i40e_down(vsi);
7898 			}
7899 		}
7900 	} else {
7901 		dev_info(&pf->pdev->dev,
7902 			 "bad reset request 0x%08x\n", reset_flags);
7903 	}
7904 }
7905 
7906 #ifdef CONFIG_I40E_DCB
7907 /**
7908  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7909  * @pf: board private structure
7910  * @old_cfg: current DCB config
7911  * @new_cfg: new DCB config
7912  **/
7913 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7914 			    struct i40e_dcbx_config *old_cfg,
7915 			    struct i40e_dcbx_config *new_cfg)
7916 {
7917 	bool need_reconfig = false;
7918 
7919 	/* Check if ETS configuration has changed */
7920 	if (memcmp(&new_cfg->etscfg,
7921 		   &old_cfg->etscfg,
7922 		   sizeof(new_cfg->etscfg))) {
7923 		/* If Priority Table has changed reconfig is needed */
7924 		if (memcmp(&new_cfg->etscfg.prioritytable,
7925 			   &old_cfg->etscfg.prioritytable,
7926 			   sizeof(new_cfg->etscfg.prioritytable))) {
7927 			need_reconfig = true;
7928 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
7929 		}
7930 
7931 		if (memcmp(&new_cfg->etscfg.tcbwtable,
7932 			   &old_cfg->etscfg.tcbwtable,
7933 			   sizeof(new_cfg->etscfg.tcbwtable)))
7934 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
7935 
7936 		if (memcmp(&new_cfg->etscfg.tsatable,
7937 			   &old_cfg->etscfg.tsatable,
7938 			   sizeof(new_cfg->etscfg.tsatable)))
7939 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
7940 	}
7941 
7942 	/* Check if PFC configuration has changed */
7943 	if (memcmp(&new_cfg->pfc,
7944 		   &old_cfg->pfc,
7945 		   sizeof(new_cfg->pfc))) {
7946 		need_reconfig = true;
7947 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
7948 	}
7949 
7950 	/* Check if APP Table has changed */
7951 	if (memcmp(&new_cfg->app,
7952 		   &old_cfg->app,
7953 		   sizeof(new_cfg->app))) {
7954 		need_reconfig = true;
7955 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
7956 	}
7957 
7958 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
7959 	return need_reconfig;
7960 }
7961 
7962 /**
7963  * i40e_handle_lldp_event - Handle LLDP Change MIB event
7964  * @pf: board private structure
7965  * @e: event info posted on ARQ
7966  **/
7967 static int i40e_handle_lldp_event(struct i40e_pf *pf,
7968 				  struct i40e_arq_event_info *e)
7969 {
7970 	struct i40e_aqc_lldp_get_mib *mib =
7971 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7972 	struct i40e_hw *hw = &pf->hw;
7973 	struct i40e_dcbx_config tmp_dcbx_cfg;
7974 	bool need_reconfig = false;
7975 	int ret = 0;
7976 	u8 type;
7977 
7978 	/* Not DCB capable or capability disabled */
7979 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
7980 		return ret;
7981 
7982 	/* Ignore if event is not for Nearest Bridge */
7983 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7984 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
7985 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
7986 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7987 		return ret;
7988 
7989 	/* Check MIB Type and return if event for Remote MIB update */
7990 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
7991 	dev_dbg(&pf->pdev->dev,
7992 		"LLDP event mib type %s\n", type ? "remote" : "local");
7993 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
7994 		/* Update the remote cached instance and return */
7995 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
7996 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
7997 				&hw->remote_dcbx_config);
7998 		goto exit;
7999 	}
8000 
8001 	/* Store the old configuration */
8002 	tmp_dcbx_cfg = hw->local_dcbx_config;
8003 
8004 	/* Reset the old DCBx configuration data */
8005 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8006 	/* Get updated DCBX data from firmware */
8007 	ret = i40e_get_dcb_config(&pf->hw);
8008 	if (ret) {
8009 		dev_info(&pf->pdev->dev,
8010 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8011 			 i40e_stat_str(&pf->hw, ret),
8012 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8013 		goto exit;
8014 	}
8015 
8016 	/* No change detected in DCBX configs */
8017 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8018 		    sizeof(tmp_dcbx_cfg))) {
8019 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8020 		goto exit;
8021 	}
8022 
8023 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8024 					       &hw->local_dcbx_config);
8025 
8026 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8027 
8028 	if (!need_reconfig)
8029 		goto exit;
8030 
8031 	/* Enable DCB tagging only when more than one TC */
8032 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8033 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8034 	else
8035 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8036 
8037 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8038 	/* Reconfiguration needed quiesce all VSIs */
8039 	i40e_pf_quiesce_all_vsi(pf);
8040 
8041 	/* Changes in configuration update VEB/VSI */
8042 	i40e_dcb_reconfigure(pf);
8043 
8044 	ret = i40e_resume_port_tx(pf);
8045 
8046 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8047 	/* In case of error no point in resuming VSIs */
8048 	if (ret)
8049 		goto exit;
8050 
8051 	/* Wait for the PF's queues to be disabled */
8052 	ret = i40e_pf_wait_queues_disabled(pf);
8053 	if (ret) {
8054 		/* Schedule PF reset to recover */
8055 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8056 		i40e_service_event_schedule(pf);
8057 	} else {
8058 		i40e_pf_unquiesce_all_vsi(pf);
8059 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8060 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8061 	}
8062 
8063 exit:
8064 	return ret;
8065 }
8066 #endif /* CONFIG_I40E_DCB */
8067 
8068 /**
8069  * i40e_do_reset_safe - Protected reset path for userland calls.
8070  * @pf: board private structure
8071  * @reset_flags: which reset is requested
8072  *
8073  **/
8074 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8075 {
8076 	rtnl_lock();
8077 	i40e_do_reset(pf, reset_flags, true);
8078 	rtnl_unlock();
8079 }
8080 
8081 /**
8082  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8083  * @pf: board private structure
8084  * @e: event info posted on ARQ
8085  *
8086  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8087  * and VF queues
8088  **/
8089 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8090 					   struct i40e_arq_event_info *e)
8091 {
8092 	struct i40e_aqc_lan_overflow *data =
8093 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8094 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8095 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8096 	struct i40e_hw *hw = &pf->hw;
8097 	struct i40e_vf *vf;
8098 	u16 vf_id;
8099 
8100 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8101 		queue, qtx_ctl);
8102 
8103 	/* Queue belongs to VF, find the VF and issue VF reset */
8104 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8105 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8106 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8107 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8108 		vf_id -= hw->func_caps.vf_base_id;
8109 		vf = &pf->vf[vf_id];
8110 		i40e_vc_notify_vf_reset(vf);
8111 		/* Allow VF to process pending reset notification */
8112 		msleep(20);
8113 		i40e_reset_vf(vf, false);
8114 	}
8115 }
8116 
8117 /**
8118  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8119  * @pf: board private structure
8120  **/
8121 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8122 {
8123 	u32 val, fcnt_prog;
8124 
8125 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8126 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8127 	return fcnt_prog;
8128 }
8129 
8130 /**
8131  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8132  * @pf: board private structure
8133  **/
8134 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8135 {
8136 	u32 val, fcnt_prog;
8137 
8138 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8139 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8140 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8141 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8142 	return fcnt_prog;
8143 }
8144 
8145 /**
8146  * i40e_get_global_fd_count - Get total FD filters programmed on device
8147  * @pf: board private structure
8148  **/
8149 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8150 {
8151 	u32 val, fcnt_prog;
8152 
8153 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8154 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8155 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8156 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8157 	return fcnt_prog;
8158 }
8159 
8160 /**
8161  * i40e_reenable_fdir_sb - Restore FDir SB capability
8162  * @pf: board private structure
8163  **/
8164 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8165 {
8166 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8167 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8168 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8169 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8170 }
8171 
8172 /**
8173  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8174  * @pf: board private structure
8175  **/
8176 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8177 {
8178 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8179 		/* ATR uses the same filtering logic as SB rules. It only
8180 		 * functions properly if the input set mask is at the default
8181 		 * settings. It is safe to restore the default input set
8182 		 * because there are no active TCPv4 filter rules.
8183 		 */
8184 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8185 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8186 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8187 
8188 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8189 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8190 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8191 	}
8192 }
8193 
8194 /**
8195  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8196  * @pf: board private structure
8197  * @filter: FDir filter to remove
8198  */
8199 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8200 				       struct i40e_fdir_filter *filter)
8201 {
8202 	/* Update counters */
8203 	pf->fdir_pf_active_filters--;
8204 	pf->fd_inv = 0;
8205 
8206 	switch (filter->flow_type) {
8207 	case TCP_V4_FLOW:
8208 		pf->fd_tcp4_filter_cnt--;
8209 		break;
8210 	case UDP_V4_FLOW:
8211 		pf->fd_udp4_filter_cnt--;
8212 		break;
8213 	case SCTP_V4_FLOW:
8214 		pf->fd_sctp4_filter_cnt--;
8215 		break;
8216 	case IP_USER_FLOW:
8217 		switch (filter->ip4_proto) {
8218 		case IPPROTO_TCP:
8219 			pf->fd_tcp4_filter_cnt--;
8220 			break;
8221 		case IPPROTO_UDP:
8222 			pf->fd_udp4_filter_cnt--;
8223 			break;
8224 		case IPPROTO_SCTP:
8225 			pf->fd_sctp4_filter_cnt--;
8226 			break;
8227 		case IPPROTO_IP:
8228 			pf->fd_ip4_filter_cnt--;
8229 			break;
8230 		}
8231 		break;
8232 	}
8233 
8234 	/* Remove the filter from the list and free memory */
8235 	hlist_del(&filter->fdir_node);
8236 	kfree(filter);
8237 }
8238 
8239 /**
8240  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8241  * @pf: board private structure
8242  **/
8243 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8244 {
8245 	struct i40e_fdir_filter *filter;
8246 	u32 fcnt_prog, fcnt_avail;
8247 	struct hlist_node *node;
8248 
8249 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8250 		return;
8251 
8252 	/* Check if we have enough room to re-enable FDir SB capability. */
8253 	fcnt_prog = i40e_get_global_fd_count(pf);
8254 	fcnt_avail = pf->fdir_pf_filter_count;
8255 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8256 	    (pf->fd_add_err == 0) ||
8257 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8258 		i40e_reenable_fdir_sb(pf);
8259 
8260 	/* We should wait for even more space before re-enabling ATR.
8261 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
8262 	 * rules active.
8263 	 */
8264 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8265 	    (pf->fd_tcp4_filter_cnt == 0))
8266 		i40e_reenable_fdir_atr(pf);
8267 
8268 	/* if hw had a problem adding a filter, delete it */
8269 	if (pf->fd_inv > 0) {
8270 		hlist_for_each_entry_safe(filter, node,
8271 					  &pf->fdir_filter_list, fdir_node)
8272 			if (filter->fd_id == pf->fd_inv)
8273 				i40e_delete_invalid_filter(pf, filter);
8274 	}
8275 }
8276 
8277 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8278 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8279 /**
8280  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8281  * @pf: board private structure
8282  **/
8283 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8284 {
8285 	unsigned long min_flush_time;
8286 	int flush_wait_retry = 50;
8287 	bool disable_atr = false;
8288 	int fd_room;
8289 	int reg;
8290 
8291 	if (!time_after(jiffies, pf->fd_flush_timestamp +
8292 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8293 		return;
8294 
8295 	/* If the flush is happening too quick and we have mostly SB rules we
8296 	 * should not re-enable ATR for some time.
8297 	 */
8298 	min_flush_time = pf->fd_flush_timestamp +
8299 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8300 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8301 
8302 	if (!(time_after(jiffies, min_flush_time)) &&
8303 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8304 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8305 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8306 		disable_atr = true;
8307 	}
8308 
8309 	pf->fd_flush_timestamp = jiffies;
8310 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8311 	/* flush all filters */
8312 	wr32(&pf->hw, I40E_PFQF_CTL_1,
8313 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8314 	i40e_flush(&pf->hw);
8315 	pf->fd_flush_cnt++;
8316 	pf->fd_add_err = 0;
8317 	do {
8318 		/* Check FD flush status every 5-6msec */
8319 		usleep_range(5000, 6000);
8320 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8321 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8322 			break;
8323 	} while (flush_wait_retry--);
8324 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8325 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8326 	} else {
8327 		/* replay sideband filters */
8328 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8329 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8330 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8331 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8332 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
8333 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8334 	}
8335 }
8336 
8337 /**
8338  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8339  * @pf: board private structure
8340  **/
8341 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8342 {
8343 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8344 }
8345 
8346 /* We can see up to 256 filter programming desc in transit if the filters are
8347  * being applied really fast; before we see the first
8348  * filter miss error on Rx queue 0. Accumulating enough error messages before
8349  * reacting will make sure we don't cause flush too often.
8350  */
8351 #define I40E_MAX_FD_PROGRAM_ERROR 256
8352 
8353 /**
8354  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8355  * @pf: board private structure
8356  **/
8357 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8358 {
8359 
8360 	/* if interface is down do nothing */
8361 	if (test_bit(__I40E_DOWN, pf->state))
8362 		return;
8363 
8364 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8365 		i40e_fdir_flush_and_replay(pf);
8366 
8367 	i40e_fdir_check_and_reenable(pf);
8368 
8369 }
8370 
8371 /**
8372  * i40e_vsi_link_event - notify VSI of a link event
8373  * @vsi: vsi to be notified
8374  * @link_up: link up or down
8375  **/
8376 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8377 {
8378 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8379 		return;
8380 
8381 	switch (vsi->type) {
8382 	case I40E_VSI_MAIN:
8383 		if (!vsi->netdev || !vsi->netdev_registered)
8384 			break;
8385 
8386 		if (link_up) {
8387 			netif_carrier_on(vsi->netdev);
8388 			netif_tx_wake_all_queues(vsi->netdev);
8389 		} else {
8390 			netif_carrier_off(vsi->netdev);
8391 			netif_tx_stop_all_queues(vsi->netdev);
8392 		}
8393 		break;
8394 
8395 	case I40E_VSI_SRIOV:
8396 	case I40E_VSI_VMDQ2:
8397 	case I40E_VSI_CTRL:
8398 	case I40E_VSI_IWARP:
8399 	case I40E_VSI_MIRROR:
8400 	default:
8401 		/* there is no notification for other VSIs */
8402 		break;
8403 	}
8404 }
8405 
8406 /**
8407  * i40e_veb_link_event - notify elements on the veb of a link event
8408  * @veb: veb to be notified
8409  * @link_up: link up or down
8410  **/
8411 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8412 {
8413 	struct i40e_pf *pf;
8414 	int i;
8415 
8416 	if (!veb || !veb->pf)
8417 		return;
8418 	pf = veb->pf;
8419 
8420 	/* depth first... */
8421 	for (i = 0; i < I40E_MAX_VEB; i++)
8422 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8423 			i40e_veb_link_event(pf->veb[i], link_up);
8424 
8425 	/* ... now the local VSIs */
8426 	for (i = 0; i < pf->num_alloc_vsi; i++)
8427 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8428 			i40e_vsi_link_event(pf->vsi[i], link_up);
8429 }
8430 
8431 /**
8432  * i40e_link_event - Update netif_carrier status
8433  * @pf: board private structure
8434  **/
8435 static void i40e_link_event(struct i40e_pf *pf)
8436 {
8437 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8438 	u8 new_link_speed, old_link_speed;
8439 	i40e_status status;
8440 	bool new_link, old_link;
8441 
8442 	/* save off old link status information */
8443 	pf->hw.phy.link_info_old = pf->hw.phy.link_info;
8444 
8445 	/* set this to force the get_link_status call to refresh state */
8446 	pf->hw.phy.get_link_info = true;
8447 
8448 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8449 
8450 	status = i40e_get_link_status(&pf->hw, &new_link);
8451 
8452 	/* On success, disable temp link polling */
8453 	if (status == I40E_SUCCESS) {
8454 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8455 	} else {
8456 		/* Enable link polling temporarily until i40e_get_link_status
8457 		 * returns I40E_SUCCESS
8458 		 */
8459 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8460 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8461 			status);
8462 		return;
8463 	}
8464 
8465 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
8466 	new_link_speed = pf->hw.phy.link_info.link_speed;
8467 
8468 	if (new_link == old_link &&
8469 	    new_link_speed == old_link_speed &&
8470 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8471 	     new_link == netif_carrier_ok(vsi->netdev)))
8472 		return;
8473 
8474 	i40e_print_link_message(vsi, new_link);
8475 
8476 	/* Notify the base of the switch tree connected to
8477 	 * the link.  Floating VEBs are not notified.
8478 	 */
8479 	if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8480 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8481 	else
8482 		i40e_vsi_link_event(vsi, new_link);
8483 
8484 	if (pf->vf)
8485 		i40e_vc_notify_link_state(pf);
8486 
8487 	if (pf->flags & I40E_FLAG_PTP)
8488 		i40e_ptp_set_increment(pf);
8489 }
8490 
8491 /**
8492  * i40e_watchdog_subtask - periodic checks not using event driven response
8493  * @pf: board private structure
8494  **/
8495 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8496 {
8497 	int i;
8498 
8499 	/* if interface is down do nothing */
8500 	if (test_bit(__I40E_DOWN, pf->state) ||
8501 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
8502 		return;
8503 
8504 	/* make sure we don't do these things too often */
8505 	if (time_before(jiffies, (pf->service_timer_previous +
8506 				  pf->service_timer_period)))
8507 		return;
8508 	pf->service_timer_previous = jiffies;
8509 
8510 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8511 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
8512 		i40e_link_event(pf);
8513 
8514 	/* Update the stats for active netdevs so the network stack
8515 	 * can look at updated numbers whenever it cares to
8516 	 */
8517 	for (i = 0; i < pf->num_alloc_vsi; i++)
8518 		if (pf->vsi[i] && pf->vsi[i]->netdev)
8519 			i40e_update_stats(pf->vsi[i]);
8520 
8521 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8522 		/* Update the stats for the active switching components */
8523 		for (i = 0; i < I40E_MAX_VEB; i++)
8524 			if (pf->veb[i])
8525 				i40e_update_veb_stats(pf->veb[i]);
8526 	}
8527 
8528 	i40e_ptp_rx_hang(pf);
8529 	i40e_ptp_tx_hang(pf);
8530 }
8531 
8532 /**
8533  * i40e_reset_subtask - Set up for resetting the device and driver
8534  * @pf: board private structure
8535  **/
8536 static void i40e_reset_subtask(struct i40e_pf *pf)
8537 {
8538 	u32 reset_flags = 0;
8539 
8540 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8541 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8542 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8543 	}
8544 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8545 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8546 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8547 	}
8548 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8549 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8550 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8551 	}
8552 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8553 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8554 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8555 	}
8556 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8557 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8558 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8559 	}
8560 
8561 	/* If there's a recovery already waiting, it takes
8562 	 * precedence before starting a new reset sequence.
8563 	 */
8564 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8565 		i40e_prep_for_reset(pf, false);
8566 		i40e_reset(pf);
8567 		i40e_rebuild(pf, false, false);
8568 	}
8569 
8570 	/* If we're already down or resetting, just bail */
8571 	if (reset_flags &&
8572 	    !test_bit(__I40E_DOWN, pf->state) &&
8573 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8574 		i40e_do_reset(pf, reset_flags, false);
8575 	}
8576 }
8577 
8578 /**
8579  * i40e_handle_link_event - Handle link event
8580  * @pf: board private structure
8581  * @e: event info posted on ARQ
8582  **/
8583 static void i40e_handle_link_event(struct i40e_pf *pf,
8584 				   struct i40e_arq_event_info *e)
8585 {
8586 	struct i40e_aqc_get_link_status *status =
8587 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8588 
8589 	/* Do a new status request to re-enable LSE reporting
8590 	 * and load new status information into the hw struct
8591 	 * This completely ignores any state information
8592 	 * in the ARQ event info, instead choosing to always
8593 	 * issue the AQ update link status command.
8594 	 */
8595 	i40e_link_event(pf);
8596 
8597 	/* Check if module meets thermal requirements */
8598 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8599 		dev_err(&pf->pdev->dev,
8600 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8601 		dev_err(&pf->pdev->dev,
8602 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8603 	} else {
8604 		/* check for unqualified module, if link is down, suppress
8605 		 * the message if link was forced to be down.
8606 		 */
8607 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8608 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8609 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
8610 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8611 			dev_err(&pf->pdev->dev,
8612 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8613 			dev_err(&pf->pdev->dev,
8614 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8615 		}
8616 	}
8617 }
8618 
8619 /**
8620  * i40e_clean_adminq_subtask - Clean the AdminQ rings
8621  * @pf: board private structure
8622  **/
8623 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8624 {
8625 	struct i40e_arq_event_info event;
8626 	struct i40e_hw *hw = &pf->hw;
8627 	u16 pending, i = 0;
8628 	i40e_status ret;
8629 	u16 opcode;
8630 	u32 oldval;
8631 	u32 val;
8632 
8633 	/* Do not run clean AQ when PF reset fails */
8634 	if (test_bit(__I40E_RESET_FAILED, pf->state))
8635 		return;
8636 
8637 	/* check for error indications */
8638 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
8639 	oldval = val;
8640 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8641 		if (hw->debug_mask & I40E_DEBUG_AQ)
8642 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8643 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8644 	}
8645 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8646 		if (hw->debug_mask & I40E_DEBUG_AQ)
8647 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8648 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8649 		pf->arq_overflows++;
8650 	}
8651 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8652 		if (hw->debug_mask & I40E_DEBUG_AQ)
8653 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8654 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8655 	}
8656 	if (oldval != val)
8657 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
8658 
8659 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
8660 	oldval = val;
8661 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8662 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8663 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8664 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8665 	}
8666 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8667 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8668 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8669 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8670 	}
8671 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8672 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8673 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8674 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8675 	}
8676 	if (oldval != val)
8677 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
8678 
8679 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8680 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8681 	if (!event.msg_buf)
8682 		return;
8683 
8684 	do {
8685 		ret = i40e_clean_arq_element(hw, &event, &pending);
8686 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8687 			break;
8688 		else if (ret) {
8689 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8690 			break;
8691 		}
8692 
8693 		opcode = le16_to_cpu(event.desc.opcode);
8694 		switch (opcode) {
8695 
8696 		case i40e_aqc_opc_get_link_status:
8697 			i40e_handle_link_event(pf, &event);
8698 			break;
8699 		case i40e_aqc_opc_send_msg_to_pf:
8700 			ret = i40e_vc_process_vf_msg(pf,
8701 					le16_to_cpu(event.desc.retval),
8702 					le32_to_cpu(event.desc.cookie_high),
8703 					le32_to_cpu(event.desc.cookie_low),
8704 					event.msg_buf,
8705 					event.msg_len);
8706 			break;
8707 		case i40e_aqc_opc_lldp_update_mib:
8708 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8709 #ifdef CONFIG_I40E_DCB
8710 			rtnl_lock();
8711 			ret = i40e_handle_lldp_event(pf, &event);
8712 			rtnl_unlock();
8713 #endif /* CONFIG_I40E_DCB */
8714 			break;
8715 		case i40e_aqc_opc_event_lan_overflow:
8716 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8717 			i40e_handle_lan_overflow_event(pf, &event);
8718 			break;
8719 		case i40e_aqc_opc_send_msg_to_peer:
8720 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8721 			break;
8722 		case i40e_aqc_opc_nvm_erase:
8723 		case i40e_aqc_opc_nvm_update:
8724 		case i40e_aqc_opc_oem_post_update:
8725 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8726 				   "ARQ NVM operation 0x%04x completed\n",
8727 				   opcode);
8728 			break;
8729 		default:
8730 			dev_info(&pf->pdev->dev,
8731 				 "ARQ: Unknown event 0x%04x ignored\n",
8732 				 opcode);
8733 			break;
8734 		}
8735 	} while (i++ < pf->adminq_work_limit);
8736 
8737 	if (i < pf->adminq_work_limit)
8738 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8739 
8740 	/* re-enable Admin queue interrupt cause */
8741 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
8742 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8743 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
8744 	i40e_flush(hw);
8745 
8746 	kfree(event.msg_buf);
8747 }
8748 
8749 /**
8750  * i40e_verify_eeprom - make sure eeprom is good to use
8751  * @pf: board private structure
8752  **/
8753 static void i40e_verify_eeprom(struct i40e_pf *pf)
8754 {
8755 	int err;
8756 
8757 	err = i40e_diag_eeprom_test(&pf->hw);
8758 	if (err) {
8759 		/* retry in case of garbage read */
8760 		err = i40e_diag_eeprom_test(&pf->hw);
8761 		if (err) {
8762 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8763 				 err);
8764 			set_bit(__I40E_BAD_EEPROM, pf->state);
8765 		}
8766 	}
8767 
8768 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8769 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8770 		clear_bit(__I40E_BAD_EEPROM, pf->state);
8771 	}
8772 }
8773 
8774 /**
8775  * i40e_enable_pf_switch_lb
8776  * @pf: pointer to the PF structure
8777  *
8778  * enable switch loop back or die - no point in a return value
8779  **/
8780 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8781 {
8782 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8783 	struct i40e_vsi_context ctxt;
8784 	int ret;
8785 
8786 	ctxt.seid = pf->main_vsi_seid;
8787 	ctxt.pf_num = pf->hw.pf_id;
8788 	ctxt.vf_num = 0;
8789 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8790 	if (ret) {
8791 		dev_info(&pf->pdev->dev,
8792 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8793 			 i40e_stat_str(&pf->hw, ret),
8794 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8795 		return;
8796 	}
8797 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8798 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8799 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8800 
8801 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8802 	if (ret) {
8803 		dev_info(&pf->pdev->dev,
8804 			 "update vsi switch failed, err %s aq_err %s\n",
8805 			 i40e_stat_str(&pf->hw, ret),
8806 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8807 	}
8808 }
8809 
8810 /**
8811  * i40e_disable_pf_switch_lb
8812  * @pf: pointer to the PF structure
8813  *
8814  * disable switch loop back or die - no point in a return value
8815  **/
8816 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8817 {
8818 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8819 	struct i40e_vsi_context ctxt;
8820 	int ret;
8821 
8822 	ctxt.seid = pf->main_vsi_seid;
8823 	ctxt.pf_num = pf->hw.pf_id;
8824 	ctxt.vf_num = 0;
8825 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8826 	if (ret) {
8827 		dev_info(&pf->pdev->dev,
8828 			 "couldn't get PF vsi config, err %s aq_err %s\n",
8829 			 i40e_stat_str(&pf->hw, ret),
8830 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8831 		return;
8832 	}
8833 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8834 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8835 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8836 
8837 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8838 	if (ret) {
8839 		dev_info(&pf->pdev->dev,
8840 			 "update vsi switch failed, err %s aq_err %s\n",
8841 			 i40e_stat_str(&pf->hw, ret),
8842 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8843 	}
8844 }
8845 
8846 /**
8847  * i40e_config_bridge_mode - Configure the HW bridge mode
8848  * @veb: pointer to the bridge instance
8849  *
8850  * Configure the loop back mode for the LAN VSI that is downlink to the
8851  * specified HW bridge instance. It is expected this function is called
8852  * when a new HW bridge is instantiated.
8853  **/
8854 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8855 {
8856 	struct i40e_pf *pf = veb->pf;
8857 
8858 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8859 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8860 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8861 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8862 		i40e_disable_pf_switch_lb(pf);
8863 	else
8864 		i40e_enable_pf_switch_lb(pf);
8865 }
8866 
8867 /**
8868  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8869  * @veb: pointer to the VEB instance
8870  *
8871  * This is a recursive function that first builds the attached VSIs then
8872  * recurses in to build the next layer of VEB.  We track the connections
8873  * through our own index numbers because the seid's from the HW could
8874  * change across the reset.
8875  **/
8876 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8877 {
8878 	struct i40e_vsi *ctl_vsi = NULL;
8879 	struct i40e_pf *pf = veb->pf;
8880 	int v, veb_idx;
8881 	int ret;
8882 
8883 	/* build VSI that owns this VEB, temporarily attached to base VEB */
8884 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8885 		if (pf->vsi[v] &&
8886 		    pf->vsi[v]->veb_idx == veb->idx &&
8887 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8888 			ctl_vsi = pf->vsi[v];
8889 			break;
8890 		}
8891 	}
8892 	if (!ctl_vsi) {
8893 		dev_info(&pf->pdev->dev,
8894 			 "missing owner VSI for veb_idx %d\n", veb->idx);
8895 		ret = -ENOENT;
8896 		goto end_reconstitute;
8897 	}
8898 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
8899 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8900 	ret = i40e_add_vsi(ctl_vsi);
8901 	if (ret) {
8902 		dev_info(&pf->pdev->dev,
8903 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
8904 			 veb->idx, ret);
8905 		goto end_reconstitute;
8906 	}
8907 	i40e_vsi_reset_stats(ctl_vsi);
8908 
8909 	/* create the VEB in the switch and move the VSI onto the VEB */
8910 	ret = i40e_add_veb(veb, ctl_vsi);
8911 	if (ret)
8912 		goto end_reconstitute;
8913 
8914 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8915 		veb->bridge_mode = BRIDGE_MODE_VEB;
8916 	else
8917 		veb->bridge_mode = BRIDGE_MODE_VEPA;
8918 	i40e_config_bridge_mode(veb);
8919 
8920 	/* create the remaining VSIs attached to this VEB */
8921 	for (v = 0; v < pf->num_alloc_vsi; v++) {
8922 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8923 			continue;
8924 
8925 		if (pf->vsi[v]->veb_idx == veb->idx) {
8926 			struct i40e_vsi *vsi = pf->vsi[v];
8927 
8928 			vsi->uplink_seid = veb->seid;
8929 			ret = i40e_add_vsi(vsi);
8930 			if (ret) {
8931 				dev_info(&pf->pdev->dev,
8932 					 "rebuild of vsi_idx %d failed: %d\n",
8933 					 v, ret);
8934 				goto end_reconstitute;
8935 			}
8936 			i40e_vsi_reset_stats(vsi);
8937 		}
8938 	}
8939 
8940 	/* create any VEBs attached to this VEB - RECURSION */
8941 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8942 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8943 			pf->veb[veb_idx]->uplink_seid = veb->seid;
8944 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8945 			if (ret)
8946 				break;
8947 		}
8948 	}
8949 
8950 end_reconstitute:
8951 	return ret;
8952 }
8953 
8954 /**
8955  * i40e_get_capabilities - get info about the HW
8956  * @pf: the PF struct
8957  **/
8958 static int i40e_get_capabilities(struct i40e_pf *pf,
8959 				 enum i40e_admin_queue_opc list_type)
8960 {
8961 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8962 	u16 data_size;
8963 	int buf_len;
8964 	int err;
8965 
8966 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8967 	do {
8968 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
8969 		if (!cap_buf)
8970 			return -ENOMEM;
8971 
8972 		/* this loads the data into the hw struct for us */
8973 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
8974 						    &data_size, list_type,
8975 						    NULL);
8976 		/* data loaded, buffer no longer needed */
8977 		kfree(cap_buf);
8978 
8979 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8980 			/* retry with a larger buffer */
8981 			buf_len = data_size;
8982 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8983 			dev_info(&pf->pdev->dev,
8984 				 "capability discovery failed, err %s aq_err %s\n",
8985 				 i40e_stat_str(&pf->hw, err),
8986 				 i40e_aq_str(&pf->hw,
8987 					     pf->hw.aq.asq_last_status));
8988 			return -ENODEV;
8989 		}
8990 	} while (err);
8991 
8992 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
8993 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
8994 			dev_info(&pf->pdev->dev,
8995 				 "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",
8996 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
8997 				 pf->hw.func_caps.num_msix_vectors,
8998 				 pf->hw.func_caps.num_msix_vectors_vf,
8999 				 pf->hw.func_caps.fd_filters_guaranteed,
9000 				 pf->hw.func_caps.fd_filters_best_effort,
9001 				 pf->hw.func_caps.num_tx_qp,
9002 				 pf->hw.func_caps.num_vsis);
9003 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9004 			dev_info(&pf->pdev->dev,
9005 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9006 				 pf->hw.dev_caps.switch_mode,
9007 				 pf->hw.dev_caps.valid_functions);
9008 			dev_info(&pf->pdev->dev,
9009 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9010 				 pf->hw.dev_caps.sr_iov_1_1,
9011 				 pf->hw.dev_caps.num_vfs);
9012 			dev_info(&pf->pdev->dev,
9013 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9014 				 pf->hw.dev_caps.num_vsis,
9015 				 pf->hw.dev_caps.num_rx_qp,
9016 				 pf->hw.dev_caps.num_tx_qp);
9017 		}
9018 	}
9019 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9020 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9021 		       + pf->hw.func_caps.num_vfs)
9022 		if (pf->hw.revision_id == 0 &&
9023 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9024 			dev_info(&pf->pdev->dev,
9025 				 "got num_vsis %d, setting num_vsis to %d\n",
9026 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9027 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9028 		}
9029 	}
9030 	return 0;
9031 }
9032 
9033 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9034 
9035 /**
9036  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9037  * @pf: board private structure
9038  **/
9039 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9040 {
9041 	struct i40e_vsi *vsi;
9042 
9043 	/* quick workaround for an NVM issue that leaves a critical register
9044 	 * uninitialized
9045 	 */
9046 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9047 		static const u32 hkey[] = {
9048 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9049 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9050 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9051 			0x95b3a76d};
9052 		int i;
9053 
9054 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9055 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9056 	}
9057 
9058 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9059 		return;
9060 
9061 	/* find existing VSI and see if it needs configuring */
9062 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9063 
9064 	/* create a new VSI if none exists */
9065 	if (!vsi) {
9066 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9067 				     pf->vsi[pf->lan_vsi]->seid, 0);
9068 		if (!vsi) {
9069 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9070 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9071 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9072 			return;
9073 		}
9074 	}
9075 
9076 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9077 }
9078 
9079 /**
9080  * i40e_fdir_teardown - release the Flow Director resources
9081  * @pf: board private structure
9082  **/
9083 static void i40e_fdir_teardown(struct i40e_pf *pf)
9084 {
9085 	struct i40e_vsi *vsi;
9086 
9087 	i40e_fdir_filter_exit(pf);
9088 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9089 	if (vsi)
9090 		i40e_vsi_release(vsi);
9091 }
9092 
9093 /**
9094  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9095  * @vsi: PF main vsi
9096  * @seid: seid of main or channel VSIs
9097  *
9098  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9099  * existed before reset
9100  **/
9101 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9102 {
9103 	struct i40e_cloud_filter *cfilter;
9104 	struct i40e_pf *pf = vsi->back;
9105 	struct hlist_node *node;
9106 	i40e_status ret;
9107 
9108 	/* Add cloud filters back if they exist */
9109 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9110 				  cloud_node) {
9111 		if (cfilter->seid != seid)
9112 			continue;
9113 
9114 		if (cfilter->dst_port)
9115 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9116 								true);
9117 		else
9118 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9119 
9120 		if (ret) {
9121 			dev_dbg(&pf->pdev->dev,
9122 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9123 				i40e_stat_str(&pf->hw, ret),
9124 				i40e_aq_str(&pf->hw,
9125 					    pf->hw.aq.asq_last_status));
9126 			return ret;
9127 		}
9128 	}
9129 	return 0;
9130 }
9131 
9132 /**
9133  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9134  * @vsi: PF main vsi
9135  *
9136  * Rebuilds channel VSIs if they existed before reset
9137  **/
9138 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9139 {
9140 	struct i40e_channel *ch, *ch_tmp;
9141 	i40e_status ret;
9142 
9143 	if (list_empty(&vsi->ch_list))
9144 		return 0;
9145 
9146 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9147 		if (!ch->initialized)
9148 			break;
9149 		/* Proceed with creation of channel (VMDq2) VSI */
9150 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9151 		if (ret) {
9152 			dev_info(&vsi->back->pdev->dev,
9153 				 "failed to rebuild channels using uplink_seid %u\n",
9154 				 vsi->uplink_seid);
9155 			return ret;
9156 		}
9157 		/* Reconfigure TX queues using QTX_CTL register */
9158 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9159 		if (ret) {
9160 			dev_info(&vsi->back->pdev->dev,
9161 				 "failed to configure TX rings for channel %u\n",
9162 				 ch->seid);
9163 			return ret;
9164 		}
9165 		/* update 'next_base_queue' */
9166 		vsi->next_base_queue = vsi->next_base_queue +
9167 							ch->num_queue_pairs;
9168 		if (ch->max_tx_rate) {
9169 			u64 credits = ch->max_tx_rate;
9170 
9171 			if (i40e_set_bw_limit(vsi, ch->seid,
9172 					      ch->max_tx_rate))
9173 				return -EINVAL;
9174 
9175 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9176 			dev_dbg(&vsi->back->pdev->dev,
9177 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9178 				ch->max_tx_rate,
9179 				credits,
9180 				ch->seid);
9181 		}
9182 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9183 		if (ret) {
9184 			dev_dbg(&vsi->back->pdev->dev,
9185 				"Failed to rebuild cloud filters for channel VSI %u\n",
9186 				ch->seid);
9187 			return ret;
9188 		}
9189 	}
9190 	return 0;
9191 }
9192 
9193 /**
9194  * i40e_prep_for_reset - prep for the core to reset
9195  * @pf: board private structure
9196  * @lock_acquired: indicates whether or not the lock has been acquired
9197  * before this function was called.
9198  *
9199  * Close up the VFs and other things in prep for PF Reset.
9200   **/
9201 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9202 {
9203 	struct i40e_hw *hw = &pf->hw;
9204 	i40e_status ret = 0;
9205 	u32 v;
9206 
9207 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9208 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9209 		return;
9210 	if (i40e_check_asq_alive(&pf->hw))
9211 		i40e_vc_notify_reset(pf);
9212 
9213 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9214 
9215 	/* quiesce the VSIs and their queues that are not already DOWN */
9216 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9217 	if (!lock_acquired)
9218 		rtnl_lock();
9219 	i40e_pf_quiesce_all_vsi(pf);
9220 	if (!lock_acquired)
9221 		rtnl_unlock();
9222 
9223 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9224 		if (pf->vsi[v])
9225 			pf->vsi[v]->seid = 0;
9226 	}
9227 
9228 	i40e_shutdown_adminq(&pf->hw);
9229 
9230 	/* call shutdown HMC */
9231 	if (hw->hmc.hmc_obj) {
9232 		ret = i40e_shutdown_lan_hmc(hw);
9233 		if (ret)
9234 			dev_warn(&pf->pdev->dev,
9235 				 "shutdown_lan_hmc failed: %d\n", ret);
9236 	}
9237 }
9238 
9239 /**
9240  * i40e_send_version - update firmware with driver version
9241  * @pf: PF struct
9242  */
9243 static void i40e_send_version(struct i40e_pf *pf)
9244 {
9245 	struct i40e_driver_version dv;
9246 
9247 	dv.major_version = DRV_VERSION_MAJOR;
9248 	dv.minor_version = DRV_VERSION_MINOR;
9249 	dv.build_version = DRV_VERSION_BUILD;
9250 	dv.subbuild_version = 0;
9251 	strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9252 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9253 }
9254 
9255 /**
9256  * i40e_get_oem_version - get OEM specific version information
9257  * @hw: pointer to the hardware structure
9258  **/
9259 static void i40e_get_oem_version(struct i40e_hw *hw)
9260 {
9261 	u16 block_offset = 0xffff;
9262 	u16 block_length = 0;
9263 	u16 capabilities = 0;
9264 	u16 gen_snap = 0;
9265 	u16 release = 0;
9266 
9267 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
9268 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
9269 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
9270 #define I40E_NVM_OEM_GEN_OFFSET			0x02
9271 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
9272 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
9273 #define I40E_NVM_OEM_LENGTH			3
9274 
9275 	/* Check if pointer to OEM version block is valid. */
9276 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9277 	if (block_offset == 0xffff)
9278 		return;
9279 
9280 	/* Check if OEM version block has correct length. */
9281 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9282 			   &block_length);
9283 	if (block_length < I40E_NVM_OEM_LENGTH)
9284 		return;
9285 
9286 	/* Check if OEM version format is as expected. */
9287 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9288 			   &capabilities);
9289 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9290 		return;
9291 
9292 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9293 			   &gen_snap);
9294 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9295 			   &release);
9296 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9297 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9298 }
9299 
9300 /**
9301  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9302  * @pf: board private structure
9303  **/
9304 static int i40e_reset(struct i40e_pf *pf)
9305 {
9306 	struct i40e_hw *hw = &pf->hw;
9307 	i40e_status ret;
9308 
9309 	ret = i40e_pf_reset(hw);
9310 	if (ret) {
9311 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9312 		set_bit(__I40E_RESET_FAILED, pf->state);
9313 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9314 	} else {
9315 		pf->pfr_count++;
9316 	}
9317 	return ret;
9318 }
9319 
9320 /**
9321  * i40e_rebuild - rebuild using a saved config
9322  * @pf: board private structure
9323  * @reinit: if the Main VSI needs to re-initialized.
9324  * @lock_acquired: indicates whether or not the lock has been acquired
9325  * before this function was called.
9326  **/
9327 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9328 {
9329 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9330 	struct i40e_hw *hw = &pf->hw;
9331 	u8 set_fc_aq_fail = 0;
9332 	i40e_status ret;
9333 	u32 val;
9334 	int v;
9335 
9336 	if (test_bit(__I40E_DOWN, pf->state))
9337 		goto clear_recovery;
9338 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9339 
9340 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9341 	ret = i40e_init_adminq(&pf->hw);
9342 	if (ret) {
9343 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9344 			 i40e_stat_str(&pf->hw, ret),
9345 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9346 		goto clear_recovery;
9347 	}
9348 	i40e_get_oem_version(&pf->hw);
9349 
9350 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9351 	    ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9352 	     hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9353 		/* The following delay is necessary for 4.33 firmware and older
9354 		 * to recover after EMP reset. 200 ms should suffice but we
9355 		 * put here 300 ms to be sure that FW is ready to operate
9356 		 * after reset.
9357 		 */
9358 		mdelay(300);
9359 	}
9360 
9361 	/* re-verify the eeprom if we just had an EMP reset */
9362 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9363 		i40e_verify_eeprom(pf);
9364 
9365 	i40e_clear_pxe_mode(hw);
9366 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9367 	if (ret)
9368 		goto end_core_reset;
9369 
9370 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9371 				hw->func_caps.num_rx_qp, 0, 0);
9372 	if (ret) {
9373 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9374 		goto end_core_reset;
9375 	}
9376 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9377 	if (ret) {
9378 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9379 		goto end_core_reset;
9380 	}
9381 
9382 	/* Enable FW to write a default DCB config on link-up */
9383 	i40e_aq_set_dcb_parameters(hw, true, NULL);
9384 
9385 #ifdef CONFIG_I40E_DCB
9386 	ret = i40e_init_pf_dcb(pf);
9387 	if (ret) {
9388 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9389 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9390 		/* Continue without DCB enabled */
9391 	}
9392 #endif /* CONFIG_I40E_DCB */
9393 	/* do basic switch setup */
9394 	if (!lock_acquired)
9395 		rtnl_lock();
9396 	ret = i40e_setup_pf_switch(pf, reinit);
9397 	if (ret)
9398 		goto end_unlock;
9399 
9400 	/* The driver only wants link up/down and module qualification
9401 	 * reports from firmware.  Note the negative logic.
9402 	 */
9403 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
9404 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
9405 					 I40E_AQ_EVENT_MEDIA_NA |
9406 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9407 	if (ret)
9408 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9409 			 i40e_stat_str(&pf->hw, ret),
9410 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9411 
9412 	/* make sure our flow control settings are restored */
9413 	ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9414 	if (ret)
9415 		dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9416 			i40e_stat_str(&pf->hw, ret),
9417 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9418 
9419 	/* Rebuild the VSIs and VEBs that existed before reset.
9420 	 * They are still in our local switch element arrays, so only
9421 	 * need to rebuild the switch model in the HW.
9422 	 *
9423 	 * If there were VEBs but the reconstitution failed, we'll try
9424 	 * try to recover minimal use by getting the basic PF VSI working.
9425 	 */
9426 	if (vsi->uplink_seid != pf->mac_seid) {
9427 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9428 		/* find the one VEB connected to the MAC, and find orphans */
9429 		for (v = 0; v < I40E_MAX_VEB; v++) {
9430 			if (!pf->veb[v])
9431 				continue;
9432 
9433 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9434 			    pf->veb[v]->uplink_seid == 0) {
9435 				ret = i40e_reconstitute_veb(pf->veb[v]);
9436 
9437 				if (!ret)
9438 					continue;
9439 
9440 				/* If Main VEB failed, we're in deep doodoo,
9441 				 * so give up rebuilding the switch and set up
9442 				 * for minimal rebuild of PF VSI.
9443 				 * If orphan failed, we'll report the error
9444 				 * but try to keep going.
9445 				 */
9446 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9447 					dev_info(&pf->pdev->dev,
9448 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9449 						 ret);
9450 					vsi->uplink_seid = pf->mac_seid;
9451 					break;
9452 				} else if (pf->veb[v]->uplink_seid == 0) {
9453 					dev_info(&pf->pdev->dev,
9454 						 "rebuild of orphan VEB failed: %d\n",
9455 						 ret);
9456 				}
9457 			}
9458 		}
9459 	}
9460 
9461 	if (vsi->uplink_seid == pf->mac_seid) {
9462 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9463 		/* no VEB, so rebuild only the Main VSI */
9464 		ret = i40e_add_vsi(vsi);
9465 		if (ret) {
9466 			dev_info(&pf->pdev->dev,
9467 				 "rebuild of Main VSI failed: %d\n", ret);
9468 			goto end_unlock;
9469 		}
9470 	}
9471 
9472 	if (vsi->mqprio_qopt.max_rate[0]) {
9473 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9474 		u64 credits = 0;
9475 
9476 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9477 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9478 		if (ret)
9479 			goto end_unlock;
9480 
9481 		credits = max_tx_rate;
9482 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
9483 		dev_dbg(&vsi->back->pdev->dev,
9484 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9485 			max_tx_rate,
9486 			credits,
9487 			vsi->seid);
9488 	}
9489 
9490 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9491 	if (ret)
9492 		goto end_unlock;
9493 
9494 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9495 	 * for this main VSI if they exist
9496 	 */
9497 	ret = i40e_rebuild_channels(vsi);
9498 	if (ret)
9499 		goto end_unlock;
9500 
9501 	/* Reconfigure hardware for allowing smaller MSS in the case
9502 	 * of TSO, so that we avoid the MDD being fired and causing
9503 	 * a reset in the case of small MSS+TSO.
9504 	 */
9505 #define I40E_REG_MSS          0x000E64DC
9506 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9507 #define I40E_64BYTE_MSS       0x400000
9508 	val = rd32(hw, I40E_REG_MSS);
9509 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9510 		val &= ~I40E_REG_MSS_MIN_MASK;
9511 		val |= I40E_64BYTE_MSS;
9512 		wr32(hw, I40E_REG_MSS, val);
9513 	}
9514 
9515 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9516 		msleep(75);
9517 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9518 		if (ret)
9519 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9520 				 i40e_stat_str(&pf->hw, ret),
9521 				 i40e_aq_str(&pf->hw,
9522 					     pf->hw.aq.asq_last_status));
9523 	}
9524 	/* reinit the misc interrupt */
9525 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9526 		ret = i40e_setup_misc_vector(pf);
9527 
9528 	/* Add a filter to drop all Flow control frames from any VSI from being
9529 	 * transmitted. By doing so we stop a malicious VF from sending out
9530 	 * PAUSE or PFC frames and potentially controlling traffic for other
9531 	 * PF/VF VSIs.
9532 	 * The FW can still send Flow control frames if enabled.
9533 	 */
9534 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9535 						       pf->main_vsi_seid);
9536 
9537 	/* restart the VSIs that were rebuilt and running before the reset */
9538 	i40e_pf_unquiesce_all_vsi(pf);
9539 
9540 	/* Release the RTNL lock before we start resetting VFs */
9541 	if (!lock_acquired)
9542 		rtnl_unlock();
9543 
9544 	/* Restore promiscuous settings */
9545 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9546 	if (ret)
9547 		dev_warn(&pf->pdev->dev,
9548 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9549 			 pf->cur_promisc ? "on" : "off",
9550 			 i40e_stat_str(&pf->hw, ret),
9551 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9552 
9553 	i40e_reset_all_vfs(pf, true);
9554 
9555 	/* tell the firmware that we're starting */
9556 	i40e_send_version(pf);
9557 
9558 	/* We've already released the lock, so don't do it again */
9559 	goto end_core_reset;
9560 
9561 end_unlock:
9562 	if (!lock_acquired)
9563 		rtnl_unlock();
9564 end_core_reset:
9565 	clear_bit(__I40E_RESET_FAILED, pf->state);
9566 clear_recovery:
9567 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9568 }
9569 
9570 /**
9571  * i40e_reset_and_rebuild - reset and rebuild using a saved config
9572  * @pf: board private structure
9573  * @reinit: if the Main VSI needs to re-initialized.
9574  * @lock_acquired: indicates whether or not the lock has been acquired
9575  * before this function was called.
9576  **/
9577 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9578 				   bool lock_acquired)
9579 {
9580 	int ret;
9581 	/* Now we wait for GRST to settle out.
9582 	 * We don't have to delete the VEBs or VSIs from the hw switch
9583 	 * because the reset will make them disappear.
9584 	 */
9585 	ret = i40e_reset(pf);
9586 	if (!ret)
9587 		i40e_rebuild(pf, reinit, lock_acquired);
9588 }
9589 
9590 /**
9591  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9592  * @pf: board private structure
9593  *
9594  * Close up the VFs and other things in prep for a Core Reset,
9595  * then get ready to rebuild the world.
9596  * @lock_acquired: indicates whether or not the lock has been acquired
9597  * before this function was called.
9598  **/
9599 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9600 {
9601 	i40e_prep_for_reset(pf, lock_acquired);
9602 	i40e_reset_and_rebuild(pf, false, lock_acquired);
9603 }
9604 
9605 /**
9606  * i40e_handle_mdd_event
9607  * @pf: pointer to the PF structure
9608  *
9609  * Called from the MDD irq handler to identify possibly malicious vfs
9610  **/
9611 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9612 {
9613 	struct i40e_hw *hw = &pf->hw;
9614 	bool mdd_detected = false;
9615 	bool pf_mdd_detected = false;
9616 	struct i40e_vf *vf;
9617 	u32 reg;
9618 	int i;
9619 
9620 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9621 		return;
9622 
9623 	/* find what triggered the MDD event */
9624 	reg = rd32(hw, I40E_GL_MDET_TX);
9625 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9626 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9627 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
9628 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9629 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
9630 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9631 				I40E_GL_MDET_TX_EVENT_SHIFT;
9632 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9633 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
9634 				pf->hw.func_caps.base_queue;
9635 		if (netif_msg_tx_err(pf))
9636 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9637 				 event, queue, pf_num, vf_num);
9638 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9639 		mdd_detected = true;
9640 	}
9641 	reg = rd32(hw, I40E_GL_MDET_RX);
9642 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9643 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9644 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
9645 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9646 				I40E_GL_MDET_RX_EVENT_SHIFT;
9647 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9648 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
9649 				pf->hw.func_caps.base_queue;
9650 		if (netif_msg_rx_err(pf))
9651 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9652 				 event, queue, func);
9653 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9654 		mdd_detected = true;
9655 	}
9656 
9657 	if (mdd_detected) {
9658 		reg = rd32(hw, I40E_PF_MDET_TX);
9659 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9660 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9661 			dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
9662 			pf_mdd_detected = true;
9663 		}
9664 		reg = rd32(hw, I40E_PF_MDET_RX);
9665 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9666 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9667 			dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
9668 			pf_mdd_detected = true;
9669 		}
9670 		/* Queue belongs to the PF, initiate a reset */
9671 		if (pf_mdd_detected) {
9672 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9673 			i40e_service_event_schedule(pf);
9674 		}
9675 	}
9676 
9677 	/* see if one of the VFs needs its hand slapped */
9678 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9679 		vf = &(pf->vf[i]);
9680 		reg = rd32(hw, I40E_VP_MDET_TX(i));
9681 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9682 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9683 			vf->num_mdd_events++;
9684 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9685 				 i);
9686 		}
9687 
9688 		reg = rd32(hw, I40E_VP_MDET_RX(i));
9689 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9690 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9691 			vf->num_mdd_events++;
9692 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9693 				 i);
9694 		}
9695 
9696 		if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9697 			dev_info(&pf->pdev->dev,
9698 				 "Too many MDD events on VF %d, disabled\n", i);
9699 			dev_info(&pf->pdev->dev,
9700 				 "Use PF Control I/F to re-enable the VF\n");
9701 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9702 		}
9703 	}
9704 
9705 	/* re-enable mdd interrupt cause */
9706 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9707 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9708 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9709 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9710 	i40e_flush(hw);
9711 }
9712 
9713 static const char *i40e_tunnel_name(u8 type)
9714 {
9715 	switch (type) {
9716 	case UDP_TUNNEL_TYPE_VXLAN:
9717 		return "vxlan";
9718 	case UDP_TUNNEL_TYPE_GENEVE:
9719 		return "geneve";
9720 	default:
9721 		return "unknown";
9722 	}
9723 }
9724 
9725 /**
9726  * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9727  * @pf: board private structure
9728  **/
9729 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9730 {
9731 	int i;
9732 
9733 	/* loop through and set pending bit for all active UDP filters */
9734 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9735 		if (pf->udp_ports[i].port)
9736 			pf->pending_udp_bitmap |= BIT_ULL(i);
9737 	}
9738 
9739 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
9740 }
9741 
9742 /**
9743  * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9744  * @pf: board private structure
9745  **/
9746 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9747 {
9748 	struct i40e_hw *hw = &pf->hw;
9749 	u8 filter_index, type;
9750 	u16 port;
9751 	int i;
9752 
9753 	if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
9754 		return;
9755 
9756 	/* acquire RTNL to maintain state of flags and port requests */
9757 	rtnl_lock();
9758 
9759 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9760 		if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9761 			struct i40e_udp_port_config *udp_port;
9762 			i40e_status ret = 0;
9763 
9764 			udp_port = &pf->udp_ports[i];
9765 			pf->pending_udp_bitmap &= ~BIT_ULL(i);
9766 
9767 			port = READ_ONCE(udp_port->port);
9768 			type = READ_ONCE(udp_port->type);
9769 			filter_index = READ_ONCE(udp_port->filter_index);
9770 
9771 			/* release RTNL while we wait on AQ command */
9772 			rtnl_unlock();
9773 
9774 			if (port)
9775 				ret = i40e_aq_add_udp_tunnel(hw, port,
9776 							     type,
9777 							     &filter_index,
9778 							     NULL);
9779 			else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
9780 				ret = i40e_aq_del_udp_tunnel(hw, filter_index,
9781 							     NULL);
9782 
9783 			/* reacquire RTNL so we can update filter_index */
9784 			rtnl_lock();
9785 
9786 			if (ret) {
9787 				dev_info(&pf->pdev->dev,
9788 					 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9789 					 i40e_tunnel_name(type),
9790 					 port ? "add" : "delete",
9791 					 port,
9792 					 filter_index,
9793 					 i40e_stat_str(&pf->hw, ret),
9794 					 i40e_aq_str(&pf->hw,
9795 						     pf->hw.aq.asq_last_status));
9796 				if (port) {
9797 					/* failed to add, just reset port,
9798 					 * drop pending bit for any deletion
9799 					 */
9800 					udp_port->port = 0;
9801 					pf->pending_udp_bitmap &= ~BIT_ULL(i);
9802 				}
9803 			} else if (port) {
9804 				/* record filter index on success */
9805 				udp_port->filter_index = filter_index;
9806 			}
9807 		}
9808 	}
9809 
9810 	rtnl_unlock();
9811 }
9812 
9813 /**
9814  * i40e_service_task - Run the driver's async subtasks
9815  * @work: pointer to work_struct containing our data
9816  **/
9817 static void i40e_service_task(struct work_struct *work)
9818 {
9819 	struct i40e_pf *pf = container_of(work,
9820 					  struct i40e_pf,
9821 					  service_task);
9822 	unsigned long start_time = jiffies;
9823 
9824 	/* don't bother with service tasks if a reset is in progress */
9825 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9826 		return;
9827 
9828 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9829 		return;
9830 
9831 	i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
9832 	i40e_sync_filters_subtask(pf);
9833 	i40e_reset_subtask(pf);
9834 	i40e_handle_mdd_event(pf);
9835 	i40e_vc_process_vflr_event(pf);
9836 	i40e_watchdog_subtask(pf);
9837 	i40e_fdir_reinit_subtask(pf);
9838 	if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
9839 		/* Client subtask will reopen next time through. */
9840 		i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9841 	} else {
9842 		i40e_client_subtask(pf);
9843 		if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
9844 				       pf->state))
9845 			i40e_notify_client_of_l2_param_changes(
9846 							pf->vsi[pf->lan_vsi]);
9847 	}
9848 	i40e_sync_filters_subtask(pf);
9849 	i40e_sync_udp_filters_subtask(pf);
9850 	i40e_clean_adminq_subtask(pf);
9851 
9852 	/* flush memory to make sure state is correct before next watchdog */
9853 	smp_mb__before_atomic();
9854 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
9855 
9856 	/* If the tasks have taken longer than one timer cycle or there
9857 	 * is more work to be done, reschedule the service task now
9858 	 * rather than wait for the timer to tick again.
9859 	 */
9860 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
9861 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
9862 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
9863 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
9864 		i40e_service_event_schedule(pf);
9865 }
9866 
9867 /**
9868  * i40e_service_timer - timer callback
9869  * @data: pointer to PF struct
9870  **/
9871 static void i40e_service_timer(struct timer_list *t)
9872 {
9873 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
9874 
9875 	mod_timer(&pf->service_timer,
9876 		  round_jiffies(jiffies + pf->service_timer_period));
9877 	i40e_service_event_schedule(pf);
9878 }
9879 
9880 /**
9881  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9882  * @vsi: the VSI being configured
9883  **/
9884 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9885 {
9886 	struct i40e_pf *pf = vsi->back;
9887 
9888 	switch (vsi->type) {
9889 	case I40E_VSI_MAIN:
9890 		vsi->alloc_queue_pairs = pf->num_lan_qps;
9891 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9892 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9893 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9894 			vsi->num_q_vectors = pf->num_lan_msix;
9895 		else
9896 			vsi->num_q_vectors = 1;
9897 
9898 		break;
9899 
9900 	case I40E_VSI_FDIR:
9901 		vsi->alloc_queue_pairs = 1;
9902 		vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9903 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9904 		vsi->num_q_vectors = pf->num_fdsb_msix;
9905 		break;
9906 
9907 	case I40E_VSI_VMDQ2:
9908 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9909 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9910 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9911 		vsi->num_q_vectors = pf->num_vmdq_msix;
9912 		break;
9913 
9914 	case I40E_VSI_SRIOV:
9915 		vsi->alloc_queue_pairs = pf->num_vf_qps;
9916 		vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9917 				      I40E_REQ_DESCRIPTOR_MULTIPLE);
9918 		break;
9919 
9920 	default:
9921 		WARN_ON(1);
9922 		return -ENODATA;
9923 	}
9924 
9925 	return 0;
9926 }
9927 
9928 /**
9929  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
9930  * @vsi: VSI pointer
9931  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
9932  *
9933  * On error: returns error code (negative)
9934  * On success: returns 0
9935  **/
9936 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
9937 {
9938 	struct i40e_ring **next_rings;
9939 	int size;
9940 	int ret = 0;
9941 
9942 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9943 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9944 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
9945 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9946 	if (!vsi->tx_rings)
9947 		return -ENOMEM;
9948 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9949 	if (i40e_enabled_xdp_vsi(vsi)) {
9950 		vsi->xdp_rings = next_rings;
9951 		next_rings += vsi->alloc_queue_pairs;
9952 	}
9953 	vsi->rx_rings = next_rings;
9954 
9955 	if (alloc_qvectors) {
9956 		/* allocate memory for q_vector pointers */
9957 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
9958 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9959 		if (!vsi->q_vectors) {
9960 			ret = -ENOMEM;
9961 			goto err_vectors;
9962 		}
9963 	}
9964 	return ret;
9965 
9966 err_vectors:
9967 	kfree(vsi->tx_rings);
9968 	return ret;
9969 }
9970 
9971 /**
9972  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
9973  * @pf: board private structure
9974  * @type: type of VSI
9975  *
9976  * On error: returns error code (negative)
9977  * On success: returns vsi index in PF (positive)
9978  **/
9979 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
9980 {
9981 	int ret = -ENODEV;
9982 	struct i40e_vsi *vsi;
9983 	int vsi_idx;
9984 	int i;
9985 
9986 	/* Need to protect the allocation of the VSIs at the PF level */
9987 	mutex_lock(&pf->switch_mutex);
9988 
9989 	/* VSI list may be fragmented if VSI creation/destruction has
9990 	 * been happening.  We can afford to do a quick scan to look
9991 	 * for any free VSIs in the list.
9992 	 *
9993 	 * find next empty vsi slot, looping back around if necessary
9994 	 */
9995 	i = pf->next_vsi;
9996 	while (i < pf->num_alloc_vsi && pf->vsi[i])
9997 		i++;
9998 	if (i >= pf->num_alloc_vsi) {
9999 		i = 0;
10000 		while (i < pf->next_vsi && pf->vsi[i])
10001 			i++;
10002 	}
10003 
10004 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10005 		vsi_idx = i;             /* Found one! */
10006 	} else {
10007 		ret = -ENODEV;
10008 		goto unlock_pf;  /* out of VSI slots! */
10009 	}
10010 	pf->next_vsi = ++i;
10011 
10012 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10013 	if (!vsi) {
10014 		ret = -ENOMEM;
10015 		goto unlock_pf;
10016 	}
10017 	vsi->type = type;
10018 	vsi->back = pf;
10019 	set_bit(__I40E_VSI_DOWN, vsi->state);
10020 	vsi->flags = 0;
10021 	vsi->idx = vsi_idx;
10022 	vsi->int_rate_limit = 0;
10023 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10024 				pf->rss_table_size : 64;
10025 	vsi->netdev_registered = false;
10026 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10027 	hash_init(vsi->mac_filter_hash);
10028 	vsi->irqs_ready = false;
10029 
10030 	ret = i40e_set_num_rings_in_vsi(vsi);
10031 	if (ret)
10032 		goto err_rings;
10033 
10034 	ret = i40e_vsi_alloc_arrays(vsi, true);
10035 	if (ret)
10036 		goto err_rings;
10037 
10038 	/* Setup default MSIX irq handler for VSI */
10039 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10040 
10041 	/* Initialize VSI lock */
10042 	spin_lock_init(&vsi->mac_filter_hash_lock);
10043 	pf->vsi[vsi_idx] = vsi;
10044 	ret = vsi_idx;
10045 	goto unlock_pf;
10046 
10047 err_rings:
10048 	pf->next_vsi = i - 1;
10049 	kfree(vsi);
10050 unlock_pf:
10051 	mutex_unlock(&pf->switch_mutex);
10052 	return ret;
10053 }
10054 
10055 /**
10056  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10057  * @vsi: VSI pointer
10058  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10059  *
10060  * On error: returns error code (negative)
10061  * On success: returns 0
10062  **/
10063 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10064 {
10065 	/* free the ring and vector containers */
10066 	if (free_qvectors) {
10067 		kfree(vsi->q_vectors);
10068 		vsi->q_vectors = NULL;
10069 	}
10070 	kfree(vsi->tx_rings);
10071 	vsi->tx_rings = NULL;
10072 	vsi->rx_rings = NULL;
10073 	vsi->xdp_rings = NULL;
10074 }
10075 
10076 /**
10077  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10078  * and lookup table
10079  * @vsi: Pointer to VSI structure
10080  */
10081 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10082 {
10083 	if (!vsi)
10084 		return;
10085 
10086 	kfree(vsi->rss_hkey_user);
10087 	vsi->rss_hkey_user = NULL;
10088 
10089 	kfree(vsi->rss_lut_user);
10090 	vsi->rss_lut_user = NULL;
10091 }
10092 
10093 /**
10094  * i40e_vsi_clear - Deallocate the VSI provided
10095  * @vsi: the VSI being un-configured
10096  **/
10097 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10098 {
10099 	struct i40e_pf *pf;
10100 
10101 	if (!vsi)
10102 		return 0;
10103 
10104 	if (!vsi->back)
10105 		goto free_vsi;
10106 	pf = vsi->back;
10107 
10108 	mutex_lock(&pf->switch_mutex);
10109 	if (!pf->vsi[vsi->idx]) {
10110 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10111 			vsi->idx, vsi->idx, vsi->type);
10112 		goto unlock_vsi;
10113 	}
10114 
10115 	if (pf->vsi[vsi->idx] != vsi) {
10116 		dev_err(&pf->pdev->dev,
10117 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10118 			pf->vsi[vsi->idx]->idx,
10119 			pf->vsi[vsi->idx]->type,
10120 			vsi->idx, vsi->type);
10121 		goto unlock_vsi;
10122 	}
10123 
10124 	/* updates the PF for this cleared vsi */
10125 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10126 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10127 
10128 	i40e_vsi_free_arrays(vsi, true);
10129 	i40e_clear_rss_config_user(vsi);
10130 
10131 	pf->vsi[vsi->idx] = NULL;
10132 	if (vsi->idx < pf->next_vsi)
10133 		pf->next_vsi = vsi->idx;
10134 
10135 unlock_vsi:
10136 	mutex_unlock(&pf->switch_mutex);
10137 free_vsi:
10138 	kfree(vsi);
10139 
10140 	return 0;
10141 }
10142 
10143 /**
10144  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10145  * @vsi: the VSI being cleaned
10146  **/
10147 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10148 {
10149 	int i;
10150 
10151 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10152 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10153 			kfree_rcu(vsi->tx_rings[i], rcu);
10154 			vsi->tx_rings[i] = NULL;
10155 			vsi->rx_rings[i] = NULL;
10156 			if (vsi->xdp_rings)
10157 				vsi->xdp_rings[i] = NULL;
10158 		}
10159 	}
10160 }
10161 
10162 /**
10163  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10164  * @vsi: the VSI being configured
10165  **/
10166 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10167 {
10168 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10169 	struct i40e_pf *pf = vsi->back;
10170 	struct i40e_ring *ring;
10171 
10172 	/* Set basic values in the rings to be used later during open() */
10173 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10174 		/* allocate space for both Tx and Rx in one shot */
10175 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10176 		if (!ring)
10177 			goto err_out;
10178 
10179 		ring->queue_index = i;
10180 		ring->reg_idx = vsi->base_queue + i;
10181 		ring->ring_active = false;
10182 		ring->vsi = vsi;
10183 		ring->netdev = vsi->netdev;
10184 		ring->dev = &pf->pdev->dev;
10185 		ring->count = vsi->num_desc;
10186 		ring->size = 0;
10187 		ring->dcb_tc = 0;
10188 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10189 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10190 		ring->itr_setting = pf->tx_itr_default;
10191 		vsi->tx_rings[i] = ring++;
10192 
10193 		if (!i40e_enabled_xdp_vsi(vsi))
10194 			goto setup_rx;
10195 
10196 		ring->queue_index = vsi->alloc_queue_pairs + i;
10197 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10198 		ring->ring_active = false;
10199 		ring->vsi = vsi;
10200 		ring->netdev = NULL;
10201 		ring->dev = &pf->pdev->dev;
10202 		ring->count = vsi->num_desc;
10203 		ring->size = 0;
10204 		ring->dcb_tc = 0;
10205 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10206 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10207 		set_ring_xdp(ring);
10208 		ring->itr_setting = pf->tx_itr_default;
10209 		vsi->xdp_rings[i] = ring++;
10210 
10211 setup_rx:
10212 		ring->queue_index = i;
10213 		ring->reg_idx = vsi->base_queue + i;
10214 		ring->ring_active = false;
10215 		ring->vsi = vsi;
10216 		ring->netdev = vsi->netdev;
10217 		ring->dev = &pf->pdev->dev;
10218 		ring->count = vsi->num_desc;
10219 		ring->size = 0;
10220 		ring->dcb_tc = 0;
10221 		ring->itr_setting = pf->rx_itr_default;
10222 		vsi->rx_rings[i] = ring;
10223 	}
10224 
10225 	return 0;
10226 
10227 err_out:
10228 	i40e_vsi_clear_rings(vsi);
10229 	return -ENOMEM;
10230 }
10231 
10232 /**
10233  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10234  * @pf: board private structure
10235  * @vectors: the number of MSI-X vectors to request
10236  *
10237  * Returns the number of vectors reserved, or error
10238  **/
10239 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10240 {
10241 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10242 					I40E_MIN_MSIX, vectors);
10243 	if (vectors < 0) {
10244 		dev_info(&pf->pdev->dev,
10245 			 "MSI-X vector reservation failed: %d\n", vectors);
10246 		vectors = 0;
10247 	}
10248 
10249 	return vectors;
10250 }
10251 
10252 /**
10253  * i40e_init_msix - Setup the MSIX capability
10254  * @pf: board private structure
10255  *
10256  * Work with the OS to set up the MSIX vectors needed.
10257  *
10258  * Returns the number of vectors reserved or negative on failure
10259  **/
10260 static int i40e_init_msix(struct i40e_pf *pf)
10261 {
10262 	struct i40e_hw *hw = &pf->hw;
10263 	int cpus, extra_vectors;
10264 	int vectors_left;
10265 	int v_budget, i;
10266 	int v_actual;
10267 	int iwarp_requested = 0;
10268 
10269 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10270 		return -ENODEV;
10271 
10272 	/* The number of vectors we'll request will be comprised of:
10273 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
10274 	 *   - The number of LAN queue pairs
10275 	 *	- Queues being used for RSS.
10276 	 *		We don't need as many as max_rss_size vectors.
10277 	 *		use rss_size instead in the calculation since that
10278 	 *		is governed by number of cpus in the system.
10279 	 *	- assumes symmetric Tx/Rx pairing
10280 	 *   - The number of VMDq pairs
10281 	 *   - The CPU count within the NUMA node if iWARP is enabled
10282 	 * Once we count this up, try the request.
10283 	 *
10284 	 * If we can't get what we want, we'll simplify to nearly nothing
10285 	 * and try again.  If that still fails, we punt.
10286 	 */
10287 	vectors_left = hw->func_caps.num_msix_vectors;
10288 	v_budget = 0;
10289 
10290 	/* reserve one vector for miscellaneous handler */
10291 	if (vectors_left) {
10292 		v_budget++;
10293 		vectors_left--;
10294 	}
10295 
10296 	/* reserve some vectors for the main PF traffic queues. Initially we
10297 	 * only reserve at most 50% of the available vectors, in the case that
10298 	 * the number of online CPUs is large. This ensures that we can enable
10299 	 * extra features as well. Once we've enabled the other features, we
10300 	 * will use any remaining vectors to reach as close as we can to the
10301 	 * number of online CPUs.
10302 	 */
10303 	cpus = num_online_cpus();
10304 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10305 	vectors_left -= pf->num_lan_msix;
10306 
10307 	/* reserve one vector for sideband flow director */
10308 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10309 		if (vectors_left) {
10310 			pf->num_fdsb_msix = 1;
10311 			v_budget++;
10312 			vectors_left--;
10313 		} else {
10314 			pf->num_fdsb_msix = 0;
10315 		}
10316 	}
10317 
10318 	/* can we reserve enough for iWARP? */
10319 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10320 		iwarp_requested = pf->num_iwarp_msix;
10321 
10322 		if (!vectors_left)
10323 			pf->num_iwarp_msix = 0;
10324 		else if (vectors_left < pf->num_iwarp_msix)
10325 			pf->num_iwarp_msix = 1;
10326 		v_budget += pf->num_iwarp_msix;
10327 		vectors_left -= pf->num_iwarp_msix;
10328 	}
10329 
10330 	/* any vectors left over go for VMDq support */
10331 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10332 		if (!vectors_left) {
10333 			pf->num_vmdq_msix = 0;
10334 			pf->num_vmdq_qps = 0;
10335 		} else {
10336 			int vmdq_vecs_wanted =
10337 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
10338 			int vmdq_vecs =
10339 				min_t(int, vectors_left, vmdq_vecs_wanted);
10340 
10341 			/* if we're short on vectors for what's desired, we limit
10342 			 * the queues per vmdq.  If this is still more than are
10343 			 * available, the user will need to change the number of
10344 			 * queues/vectors used by the PF later with the ethtool
10345 			 * channels command
10346 			 */
10347 			if (vectors_left < vmdq_vecs_wanted) {
10348 				pf->num_vmdq_qps = 1;
10349 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
10350 				vmdq_vecs = min_t(int,
10351 						  vectors_left,
10352 						  vmdq_vecs_wanted);
10353 			}
10354 			pf->num_vmdq_msix = pf->num_vmdq_qps;
10355 
10356 			v_budget += vmdq_vecs;
10357 			vectors_left -= vmdq_vecs;
10358 		}
10359 	}
10360 
10361 	/* On systems with a large number of SMP cores, we previously limited
10362 	 * the number of vectors for num_lan_msix to be at most 50% of the
10363 	 * available vectors, to allow for other features. Now, we add back
10364 	 * the remaining vectors. However, we ensure that the total
10365 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10366 	 * calculate the number of vectors we can add without going over the
10367 	 * cap of CPUs. For systems with a small number of CPUs this will be
10368 	 * zero.
10369 	 */
10370 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10371 	pf->num_lan_msix += extra_vectors;
10372 	vectors_left -= extra_vectors;
10373 
10374 	WARN(vectors_left < 0,
10375 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10376 
10377 	v_budget += pf->num_lan_msix;
10378 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10379 				   GFP_KERNEL);
10380 	if (!pf->msix_entries)
10381 		return -ENOMEM;
10382 
10383 	for (i = 0; i < v_budget; i++)
10384 		pf->msix_entries[i].entry = i;
10385 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10386 
10387 	if (v_actual < I40E_MIN_MSIX) {
10388 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10389 		kfree(pf->msix_entries);
10390 		pf->msix_entries = NULL;
10391 		pci_disable_msix(pf->pdev);
10392 		return -ENODEV;
10393 
10394 	} else if (v_actual == I40E_MIN_MSIX) {
10395 		/* Adjust for minimal MSIX use */
10396 		pf->num_vmdq_vsis = 0;
10397 		pf->num_vmdq_qps = 0;
10398 		pf->num_lan_qps = 1;
10399 		pf->num_lan_msix = 1;
10400 
10401 	} else if (v_actual != v_budget) {
10402 		/* If we have limited resources, we will start with no vectors
10403 		 * for the special features and then allocate vectors to some
10404 		 * of these features based on the policy and at the end disable
10405 		 * the features that did not get any vectors.
10406 		 */
10407 		int vec;
10408 
10409 		dev_info(&pf->pdev->dev,
10410 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10411 			 v_actual, v_budget);
10412 		/* reserve the misc vector */
10413 		vec = v_actual - 1;
10414 
10415 		/* Scale vector usage down */
10416 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
10417 		pf->num_vmdq_vsis = 1;
10418 		pf->num_vmdq_qps = 1;
10419 
10420 		/* partition out the remaining vectors */
10421 		switch (vec) {
10422 		case 2:
10423 			pf->num_lan_msix = 1;
10424 			break;
10425 		case 3:
10426 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10427 				pf->num_lan_msix = 1;
10428 				pf->num_iwarp_msix = 1;
10429 			} else {
10430 				pf->num_lan_msix = 2;
10431 			}
10432 			break;
10433 		default:
10434 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10435 				pf->num_iwarp_msix = min_t(int, (vec / 3),
10436 						 iwarp_requested);
10437 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
10438 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10439 			} else {
10440 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
10441 						  I40E_DEFAULT_NUM_VMDQ_VSI);
10442 			}
10443 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10444 				pf->num_fdsb_msix = 1;
10445 				vec--;
10446 			}
10447 			pf->num_lan_msix = min_t(int,
10448 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10449 							      pf->num_lan_msix);
10450 			pf->num_lan_qps = pf->num_lan_msix;
10451 			break;
10452 		}
10453 	}
10454 
10455 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10456 	    (pf->num_fdsb_msix == 0)) {
10457 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10458 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10459 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10460 	}
10461 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10462 	    (pf->num_vmdq_msix == 0)) {
10463 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10464 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10465 	}
10466 
10467 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10468 	    (pf->num_iwarp_msix == 0)) {
10469 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10470 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10471 	}
10472 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10473 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10474 		   pf->num_lan_msix,
10475 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
10476 		   pf->num_fdsb_msix,
10477 		   pf->num_iwarp_msix);
10478 
10479 	return v_actual;
10480 }
10481 
10482 /**
10483  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10484  * @vsi: the VSI being configured
10485  * @v_idx: index of the vector in the vsi struct
10486  * @cpu: cpu to be used on affinity_mask
10487  *
10488  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
10489  **/
10490 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10491 {
10492 	struct i40e_q_vector *q_vector;
10493 
10494 	/* allocate q_vector */
10495 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10496 	if (!q_vector)
10497 		return -ENOMEM;
10498 
10499 	q_vector->vsi = vsi;
10500 	q_vector->v_idx = v_idx;
10501 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10502 
10503 	if (vsi->netdev)
10504 		netif_napi_add(vsi->netdev, &q_vector->napi,
10505 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
10506 
10507 	/* tie q_vector and vsi together */
10508 	vsi->q_vectors[v_idx] = q_vector;
10509 
10510 	return 0;
10511 }
10512 
10513 /**
10514  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10515  * @vsi: the VSI being configured
10516  *
10517  * We allocate one q_vector per queue interrupt.  If allocation fails we
10518  * return -ENOMEM.
10519  **/
10520 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10521 {
10522 	struct i40e_pf *pf = vsi->back;
10523 	int err, v_idx, num_q_vectors, current_cpu;
10524 
10525 	/* if not MSIX, give the one vector only to the LAN VSI */
10526 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10527 		num_q_vectors = vsi->num_q_vectors;
10528 	else if (vsi == pf->vsi[pf->lan_vsi])
10529 		num_q_vectors = 1;
10530 	else
10531 		return -EINVAL;
10532 
10533 	current_cpu = cpumask_first(cpu_online_mask);
10534 
10535 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10536 		err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10537 		if (err)
10538 			goto err_out;
10539 		current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10540 		if (unlikely(current_cpu >= nr_cpu_ids))
10541 			current_cpu = cpumask_first(cpu_online_mask);
10542 	}
10543 
10544 	return 0;
10545 
10546 err_out:
10547 	while (v_idx--)
10548 		i40e_free_q_vector(vsi, v_idx);
10549 
10550 	return err;
10551 }
10552 
10553 /**
10554  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10555  * @pf: board private structure to initialize
10556  **/
10557 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10558 {
10559 	int vectors = 0;
10560 	ssize_t size;
10561 
10562 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10563 		vectors = i40e_init_msix(pf);
10564 		if (vectors < 0) {
10565 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
10566 				       I40E_FLAG_IWARP_ENABLED	|
10567 				       I40E_FLAG_RSS_ENABLED	|
10568 				       I40E_FLAG_DCB_CAPABLE	|
10569 				       I40E_FLAG_DCB_ENABLED	|
10570 				       I40E_FLAG_SRIOV_ENABLED	|
10571 				       I40E_FLAG_FD_SB_ENABLED	|
10572 				       I40E_FLAG_FD_ATR_ENABLED	|
10573 				       I40E_FLAG_VMDQ_ENABLED);
10574 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10575 
10576 			/* rework the queue expectations without MSIX */
10577 			i40e_determine_queue_usage(pf);
10578 		}
10579 	}
10580 
10581 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10582 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10583 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10584 		vectors = pci_enable_msi(pf->pdev);
10585 		if (vectors < 0) {
10586 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10587 				 vectors);
10588 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10589 		}
10590 		vectors = 1;  /* one MSI or Legacy vector */
10591 	}
10592 
10593 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10594 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10595 
10596 	/* set up vector assignment tracking */
10597 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10598 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
10599 	if (!pf->irq_pile)
10600 		return -ENOMEM;
10601 
10602 	pf->irq_pile->num_entries = vectors;
10603 	pf->irq_pile->search_hint = 0;
10604 
10605 	/* track first vector for misc interrupts, ignore return */
10606 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10607 
10608 	return 0;
10609 }
10610 
10611 /**
10612  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10613  * @pf: private board data structure
10614  *
10615  * Restore the interrupt scheme that was cleared when we suspended the
10616  * device. This should be called during resume to re-allocate the q_vectors
10617  * and reacquire IRQs.
10618  */
10619 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10620 {
10621 	int err, i;
10622 
10623 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
10624 	 * scheme. We need to re-enabled them here in order to attempt to
10625 	 * re-acquire the MSI or MSI-X vectors
10626 	 */
10627 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10628 
10629 	err = i40e_init_interrupt_scheme(pf);
10630 	if (err)
10631 		return err;
10632 
10633 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
10634 	 * rings together again.
10635 	 */
10636 	for (i = 0; i < pf->num_alloc_vsi; i++) {
10637 		if (pf->vsi[i]) {
10638 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10639 			if (err)
10640 				goto err_unwind;
10641 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10642 		}
10643 	}
10644 
10645 	err = i40e_setup_misc_vector(pf);
10646 	if (err)
10647 		goto err_unwind;
10648 
10649 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
10650 		i40e_client_update_msix_info(pf);
10651 
10652 	return 0;
10653 
10654 err_unwind:
10655 	while (i--) {
10656 		if (pf->vsi[i])
10657 			i40e_vsi_free_q_vectors(pf->vsi[i]);
10658 	}
10659 
10660 	return err;
10661 }
10662 
10663 /**
10664  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10665  * @pf: board private structure
10666  *
10667  * This sets up the handler for MSIX 0, which is used to manage the
10668  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
10669  * when in MSI or Legacy interrupt mode.
10670  **/
10671 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10672 {
10673 	struct i40e_hw *hw = &pf->hw;
10674 	int err = 0;
10675 
10676 	/* Only request the IRQ once, the first time through. */
10677 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10678 		err = request_irq(pf->msix_entries[0].vector,
10679 				  i40e_intr, 0, pf->int_name, pf);
10680 		if (err) {
10681 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10682 			dev_info(&pf->pdev->dev,
10683 				 "request_irq for %s failed: %d\n",
10684 				 pf->int_name, err);
10685 			return -EFAULT;
10686 		}
10687 	}
10688 
10689 	i40e_enable_misc_int_causes(pf);
10690 
10691 	/* associate no queues to the misc vector */
10692 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10693 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10694 
10695 	i40e_flush(hw);
10696 
10697 	i40e_irq_dynamic_enable_icr0(pf);
10698 
10699 	return err;
10700 }
10701 
10702 /**
10703  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10704  * @vsi: Pointer to vsi structure
10705  * @seed: Buffter to store the hash keys
10706  * @lut: Buffer to store the lookup table entries
10707  * @lut_size: Size of buffer to store the lookup table entries
10708  *
10709  * Return 0 on success, negative on failure
10710  */
10711 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10712 			   u8 *lut, u16 lut_size)
10713 {
10714 	struct i40e_pf *pf = vsi->back;
10715 	struct i40e_hw *hw = &pf->hw;
10716 	int ret = 0;
10717 
10718 	if (seed) {
10719 		ret = i40e_aq_get_rss_key(hw, vsi->id,
10720 			(struct i40e_aqc_get_set_rss_key_data *)seed);
10721 		if (ret) {
10722 			dev_info(&pf->pdev->dev,
10723 				 "Cannot get RSS key, err %s aq_err %s\n",
10724 				 i40e_stat_str(&pf->hw, ret),
10725 				 i40e_aq_str(&pf->hw,
10726 					     pf->hw.aq.asq_last_status));
10727 			return ret;
10728 		}
10729 	}
10730 
10731 	if (lut) {
10732 		bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10733 
10734 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10735 		if (ret) {
10736 			dev_info(&pf->pdev->dev,
10737 				 "Cannot get RSS lut, err %s aq_err %s\n",
10738 				 i40e_stat_str(&pf->hw, ret),
10739 				 i40e_aq_str(&pf->hw,
10740 					     pf->hw.aq.asq_last_status));
10741 			return ret;
10742 		}
10743 	}
10744 
10745 	return ret;
10746 }
10747 
10748 /**
10749  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10750  * @vsi: Pointer to vsi structure
10751  * @seed: RSS hash seed
10752  * @lut: Lookup table
10753  * @lut_size: Lookup table size
10754  *
10755  * Returns 0 on success, negative on failure
10756  **/
10757 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10758 			       const u8 *lut, u16 lut_size)
10759 {
10760 	struct i40e_pf *pf = vsi->back;
10761 	struct i40e_hw *hw = &pf->hw;
10762 	u16 vf_id = vsi->vf_id;
10763 	u8 i;
10764 
10765 	/* Fill out hash function seed */
10766 	if (seed) {
10767 		u32 *seed_dw = (u32 *)seed;
10768 
10769 		if (vsi->type == I40E_VSI_MAIN) {
10770 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10771 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10772 		} else if (vsi->type == I40E_VSI_SRIOV) {
10773 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10774 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10775 		} else {
10776 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10777 		}
10778 	}
10779 
10780 	if (lut) {
10781 		u32 *lut_dw = (u32 *)lut;
10782 
10783 		if (vsi->type == I40E_VSI_MAIN) {
10784 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
10785 				return -EINVAL;
10786 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10787 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10788 		} else if (vsi->type == I40E_VSI_SRIOV) {
10789 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10790 				return -EINVAL;
10791 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10792 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10793 		} else {
10794 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10795 		}
10796 	}
10797 	i40e_flush(hw);
10798 
10799 	return 0;
10800 }
10801 
10802 /**
10803  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10804  * @vsi: Pointer to VSI structure
10805  * @seed: Buffer to store the keys
10806  * @lut: Buffer to store the lookup table entries
10807  * @lut_size: Size of buffer to store the lookup table entries
10808  *
10809  * Returns 0 on success, negative on failure
10810  */
10811 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10812 			    u8 *lut, u16 lut_size)
10813 {
10814 	struct i40e_pf *pf = vsi->back;
10815 	struct i40e_hw *hw = &pf->hw;
10816 	u16 i;
10817 
10818 	if (seed) {
10819 		u32 *seed_dw = (u32 *)seed;
10820 
10821 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10822 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
10823 	}
10824 	if (lut) {
10825 		u32 *lut_dw = (u32 *)lut;
10826 
10827 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
10828 			return -EINVAL;
10829 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10830 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10831 	}
10832 
10833 	return 0;
10834 }
10835 
10836 /**
10837  * i40e_config_rss - Configure RSS keys and lut
10838  * @vsi: Pointer to VSI structure
10839  * @seed: RSS hash seed
10840  * @lut: Lookup table
10841  * @lut_size: Lookup table size
10842  *
10843  * Returns 0 on success, negative on failure
10844  */
10845 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10846 {
10847 	struct i40e_pf *pf = vsi->back;
10848 
10849 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10850 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10851 	else
10852 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10853 }
10854 
10855 /**
10856  * i40e_get_rss - Get RSS keys and lut
10857  * @vsi: Pointer to VSI structure
10858  * @seed: Buffer to store the keys
10859  * @lut: Buffer to store the lookup table entries
10860  * @lut_size: Size of buffer to store the lookup table entries
10861  *
10862  * Returns 0 on success, negative on failure
10863  */
10864 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10865 {
10866 	struct i40e_pf *pf = vsi->back;
10867 
10868 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10869 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10870 	else
10871 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
10872 }
10873 
10874 /**
10875  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10876  * @pf: Pointer to board private structure
10877  * @lut: Lookup table
10878  * @rss_table_size: Lookup table size
10879  * @rss_size: Range of queue number for hashing
10880  */
10881 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10882 		       u16 rss_table_size, u16 rss_size)
10883 {
10884 	u16 i;
10885 
10886 	for (i = 0; i < rss_table_size; i++)
10887 		lut[i] = i % rss_size;
10888 }
10889 
10890 /**
10891  * i40e_pf_config_rss - Prepare for RSS if used
10892  * @pf: board private structure
10893  **/
10894 static int i40e_pf_config_rss(struct i40e_pf *pf)
10895 {
10896 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10897 	u8 seed[I40E_HKEY_ARRAY_SIZE];
10898 	u8 *lut;
10899 	struct i40e_hw *hw = &pf->hw;
10900 	u32 reg_val;
10901 	u64 hena;
10902 	int ret;
10903 
10904 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
10905 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10906 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
10907 	hena |= i40e_pf_get_default_rss_hena(pf);
10908 
10909 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10910 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
10911 
10912 	/* Determine the RSS table size based on the hardware capabilities */
10913 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
10914 	reg_val = (pf->rss_table_size == 512) ?
10915 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10916 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
10917 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
10918 
10919 	/* Determine the RSS size of the VSI */
10920 	if (!vsi->rss_size) {
10921 		u16 qcount;
10922 		/* If the firmware does something weird during VSI init, we
10923 		 * could end up with zero TCs. Check for that to avoid
10924 		 * divide-by-zero. It probably won't pass traffic, but it also
10925 		 * won't panic.
10926 		 */
10927 		qcount = vsi->num_queue_pairs /
10928 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
10929 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10930 	}
10931 	if (!vsi->rss_size)
10932 		return -EINVAL;
10933 
10934 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10935 	if (!lut)
10936 		return -ENOMEM;
10937 
10938 	/* Use user configured lut if there is one, otherwise use default */
10939 	if (vsi->rss_lut_user)
10940 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10941 	else
10942 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
10943 
10944 	/* Use user configured hash key if there is one, otherwise
10945 	 * use default.
10946 	 */
10947 	if (vsi->rss_hkey_user)
10948 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10949 	else
10950 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
10951 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
10952 	kfree(lut);
10953 
10954 	return ret;
10955 }
10956 
10957 /**
10958  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10959  * @pf: board private structure
10960  * @queue_count: the requested queue count for rss.
10961  *
10962  * returns 0 if rss is not enabled, if enabled returns the final rss queue
10963  * count which may be different from the requested queue count.
10964  * Note: expects to be called while under rtnl_lock()
10965  **/
10966 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
10967 {
10968 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10969 	int new_rss_size;
10970 
10971 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
10972 		return 0;
10973 
10974 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
10975 
10976 	if (queue_count != vsi->num_queue_pairs) {
10977 		u16 qcount;
10978 
10979 		vsi->req_queue_pairs = queue_count;
10980 		i40e_prep_for_reset(pf, true);
10981 
10982 		pf->alloc_rss_size = new_rss_size;
10983 
10984 		i40e_reset_and_rebuild(pf, true, true);
10985 
10986 		/* Discard the user configured hash keys and lut, if less
10987 		 * queues are enabled.
10988 		 */
10989 		if (queue_count < vsi->rss_size) {
10990 			i40e_clear_rss_config_user(vsi);
10991 			dev_dbg(&pf->pdev->dev,
10992 				"discard user configured hash keys and lut\n");
10993 		}
10994 
10995 		/* Reset vsi->rss_size, as number of enabled queues changed */
10996 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10997 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10998 
10999 		i40e_pf_config_rss(pf);
11000 	}
11001 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11002 		 vsi->req_queue_pairs, pf->rss_size_max);
11003 	return pf->alloc_rss_size;
11004 }
11005 
11006 /**
11007  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11008  * @pf: board private structure
11009  **/
11010 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11011 {
11012 	i40e_status status;
11013 	bool min_valid, max_valid;
11014 	u32 max_bw, min_bw;
11015 
11016 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11017 					   &min_valid, &max_valid);
11018 
11019 	if (!status) {
11020 		if (min_valid)
11021 			pf->min_bw = min_bw;
11022 		if (max_valid)
11023 			pf->max_bw = max_bw;
11024 	}
11025 
11026 	return status;
11027 }
11028 
11029 /**
11030  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11031  * @pf: board private structure
11032  **/
11033 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11034 {
11035 	struct i40e_aqc_configure_partition_bw_data bw_data;
11036 	i40e_status status;
11037 
11038 	/* Set the valid bit for this PF */
11039 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11040 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11041 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11042 
11043 	/* Set the new bandwidths */
11044 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11045 
11046 	return status;
11047 }
11048 
11049 /**
11050  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11051  * @pf: board private structure
11052  **/
11053 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11054 {
11055 	/* Commit temporary BW setting to permanent NVM image */
11056 	enum i40e_admin_queue_err last_aq_status;
11057 	i40e_status ret;
11058 	u16 nvm_word;
11059 
11060 	if (pf->hw.partition_id != 1) {
11061 		dev_info(&pf->pdev->dev,
11062 			 "Commit BW only works on partition 1! This is partition %d",
11063 			 pf->hw.partition_id);
11064 		ret = I40E_NOT_SUPPORTED;
11065 		goto bw_commit_out;
11066 	}
11067 
11068 	/* Acquire NVM for read access */
11069 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11070 	last_aq_status = pf->hw.aq.asq_last_status;
11071 	if (ret) {
11072 		dev_info(&pf->pdev->dev,
11073 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11074 			 i40e_stat_str(&pf->hw, ret),
11075 			 i40e_aq_str(&pf->hw, last_aq_status));
11076 		goto bw_commit_out;
11077 	}
11078 
11079 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11080 	ret = i40e_aq_read_nvm(&pf->hw,
11081 			       I40E_SR_NVM_CONTROL_WORD,
11082 			       0x10, sizeof(nvm_word), &nvm_word,
11083 			       false, NULL);
11084 	/* Save off last admin queue command status before releasing
11085 	 * the NVM
11086 	 */
11087 	last_aq_status = pf->hw.aq.asq_last_status;
11088 	i40e_release_nvm(&pf->hw);
11089 	if (ret) {
11090 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11091 			 i40e_stat_str(&pf->hw, ret),
11092 			 i40e_aq_str(&pf->hw, last_aq_status));
11093 		goto bw_commit_out;
11094 	}
11095 
11096 	/* Wait a bit for NVM release to complete */
11097 	msleep(50);
11098 
11099 	/* Acquire NVM for write access */
11100 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11101 	last_aq_status = pf->hw.aq.asq_last_status;
11102 	if (ret) {
11103 		dev_info(&pf->pdev->dev,
11104 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11105 			 i40e_stat_str(&pf->hw, ret),
11106 			 i40e_aq_str(&pf->hw, last_aq_status));
11107 		goto bw_commit_out;
11108 	}
11109 	/* Write it back out unchanged to initiate update NVM,
11110 	 * which will force a write of the shadow (alt) RAM to
11111 	 * the NVM - thus storing the bandwidth values permanently.
11112 	 */
11113 	ret = i40e_aq_update_nvm(&pf->hw,
11114 				 I40E_SR_NVM_CONTROL_WORD,
11115 				 0x10, sizeof(nvm_word),
11116 				 &nvm_word, true, 0, NULL);
11117 	/* Save off last admin queue command status before releasing
11118 	 * the NVM
11119 	 */
11120 	last_aq_status = pf->hw.aq.asq_last_status;
11121 	i40e_release_nvm(&pf->hw);
11122 	if (ret)
11123 		dev_info(&pf->pdev->dev,
11124 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11125 			 i40e_stat_str(&pf->hw, ret),
11126 			 i40e_aq_str(&pf->hw, last_aq_status));
11127 bw_commit_out:
11128 
11129 	return ret;
11130 }
11131 
11132 /**
11133  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11134  * @pf: board private structure to initialize
11135  *
11136  * i40e_sw_init initializes the Adapter private data structure.
11137  * Fields are initialized based on PCI device information and
11138  * OS network device settings (MTU size).
11139  **/
11140 static int i40e_sw_init(struct i40e_pf *pf)
11141 {
11142 	int err = 0;
11143 	int size;
11144 
11145 	/* Set default capability flags */
11146 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11147 		    I40E_FLAG_MSI_ENABLED     |
11148 		    I40E_FLAG_MSIX_ENABLED;
11149 
11150 	/* Set default ITR */
11151 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11152 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11153 
11154 	/* Depending on PF configurations, it is possible that the RSS
11155 	 * maximum might end up larger than the available queues
11156 	 */
11157 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11158 	pf->alloc_rss_size = 1;
11159 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11160 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11161 				 pf->hw.func_caps.num_tx_qp);
11162 	if (pf->hw.func_caps.rss) {
11163 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11164 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11165 					   num_online_cpus());
11166 	}
11167 
11168 	/* MFP mode enabled */
11169 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11170 		pf->flags |= I40E_FLAG_MFP_ENABLED;
11171 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11172 		if (i40e_get_partition_bw_setting(pf)) {
11173 			dev_warn(&pf->pdev->dev,
11174 				 "Could not get partition bw settings\n");
11175 		} else {
11176 			dev_info(&pf->pdev->dev,
11177 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
11178 				 pf->min_bw, pf->max_bw);
11179 
11180 			/* nudge the Tx scheduler */
11181 			i40e_set_partition_bw_setting(pf);
11182 		}
11183 	}
11184 
11185 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11186 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11187 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11188 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11189 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11190 		    pf->hw.num_partitions > 1)
11191 			dev_info(&pf->pdev->dev,
11192 				 "Flow Director Sideband mode Disabled in MFP mode\n");
11193 		else
11194 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11195 		pf->fdir_pf_filter_count =
11196 				 pf->hw.func_caps.fd_filters_guaranteed;
11197 		pf->hw.fdir_shared_filter_count =
11198 				 pf->hw.func_caps.fd_filters_best_effort;
11199 	}
11200 
11201 	if (pf->hw.mac.type == I40E_MAC_X722) {
11202 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11203 				    I40E_HW_128_QP_RSS_CAPABLE |
11204 				    I40E_HW_ATR_EVICT_CAPABLE |
11205 				    I40E_HW_WB_ON_ITR_CAPABLE |
11206 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11207 				    I40E_HW_NO_PCI_LINK_CHECK |
11208 				    I40E_HW_USE_SET_LLDP_MIB |
11209 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11210 				    I40E_HW_PTP_L4_CAPABLE |
11211 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11212 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11213 
11214 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11215 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11216 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
11217 			dev_warn(&pf->pdev->dev,
11218 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11219 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11220 		}
11221 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
11222 		   ((pf->hw.aq.api_maj_ver == 1) &&
11223 		    (pf->hw.aq.api_min_ver > 4))) {
11224 		/* Supported in FW API version higher than 1.4 */
11225 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11226 	}
11227 
11228 	/* Enable HW ATR eviction if possible */
11229 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11230 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11231 
11232 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11233 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11234 	    (pf->hw.aq.fw_maj_ver < 4))) {
11235 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11236 		/* No DCB support  for FW < v4.33 */
11237 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11238 	}
11239 
11240 	/* Disable FW LLDP if FW < v4.3 */
11241 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11242 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11243 	    (pf->hw.aq.fw_maj_ver < 4)))
11244 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11245 
11246 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
11247 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11248 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11249 	    (pf->hw.aq.fw_maj_ver >= 5)))
11250 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11251 
11252 	/* Enable PTP L4 if FW > v6.0 */
11253 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11254 	    pf->hw.aq.fw_maj_ver >= 6)
11255 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11256 
11257 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11258 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11259 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11260 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11261 	}
11262 
11263 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11264 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
11265 		/* IWARP needs one extra vector for CQP just like MISC.*/
11266 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11267 	}
11268 	/* Stopping the FW LLDP engine is only supported on the
11269 	 * XL710 with a FW ver >= 1.7.  Also, stopping FW LLDP
11270 	 * engine is not supported if NPAR is functioning on this
11271 	 * part
11272 	 */
11273 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
11274 	    !pf->hw.func_caps.npar_enable &&
11275 	    (pf->hw.aq.api_maj_ver > 1 ||
11276 	     (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
11277 		pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
11278 
11279 #ifdef CONFIG_PCI_IOV
11280 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11281 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11282 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11283 		pf->num_req_vfs = min_t(int,
11284 					pf->hw.func_caps.num_vfs,
11285 					I40E_MAX_VF_COUNT);
11286 	}
11287 #endif /* CONFIG_PCI_IOV */
11288 	pf->eeprom_version = 0xDEAD;
11289 	pf->lan_veb = I40E_NO_VEB;
11290 	pf->lan_vsi = I40E_NO_VSI;
11291 
11292 	/* By default FW has this off for performance reasons */
11293 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11294 
11295 	/* set up queue assignment tracking */
11296 	size = sizeof(struct i40e_lump_tracking)
11297 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11298 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
11299 	if (!pf->qp_pile) {
11300 		err = -ENOMEM;
11301 		goto sw_init_done;
11302 	}
11303 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11304 	pf->qp_pile->search_hint = 0;
11305 
11306 	pf->tx_timeout_recovery_level = 1;
11307 
11308 	mutex_init(&pf->switch_mutex);
11309 
11310 sw_init_done:
11311 	return err;
11312 }
11313 
11314 /**
11315  * i40e_set_ntuple - set the ntuple feature flag and take action
11316  * @pf: board private structure to initialize
11317  * @features: the feature set that the stack is suggesting
11318  *
11319  * returns a bool to indicate if reset needs to happen
11320  **/
11321 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11322 {
11323 	bool need_reset = false;
11324 
11325 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
11326 	 * the state changed, we need to reset.
11327 	 */
11328 	if (features & NETIF_F_NTUPLE) {
11329 		/* Enable filters and mark for reset */
11330 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11331 			need_reset = true;
11332 		/* enable FD_SB only if there is MSI-X vector and no cloud
11333 		 * filters exist
11334 		 */
11335 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11336 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11337 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11338 		}
11339 	} else {
11340 		/* turn off filters, mark for reset and clear SW filter list */
11341 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11342 			need_reset = true;
11343 			i40e_fdir_filter_exit(pf);
11344 		}
11345 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11346 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
11347 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11348 
11349 		/* reset fd counters */
11350 		pf->fd_add_err = 0;
11351 		pf->fd_atr_cnt = 0;
11352 		/* if ATR was auto disabled it can be re-enabled. */
11353 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
11354 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11355 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
11356 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11357 	}
11358 	return need_reset;
11359 }
11360 
11361 /**
11362  * i40e_clear_rss_lut - clear the rx hash lookup table
11363  * @vsi: the VSI being configured
11364  **/
11365 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11366 {
11367 	struct i40e_pf *pf = vsi->back;
11368 	struct i40e_hw *hw = &pf->hw;
11369 	u16 vf_id = vsi->vf_id;
11370 	u8 i;
11371 
11372 	if (vsi->type == I40E_VSI_MAIN) {
11373 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11374 			wr32(hw, I40E_PFQF_HLUT(i), 0);
11375 	} else if (vsi->type == I40E_VSI_SRIOV) {
11376 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11377 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11378 	} else {
11379 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11380 	}
11381 }
11382 
11383 /**
11384  * i40e_set_features - set the netdev feature flags
11385  * @netdev: ptr to the netdev being adjusted
11386  * @features: the feature set that the stack is suggesting
11387  * Note: expects to be called while under rtnl_lock()
11388  **/
11389 static int i40e_set_features(struct net_device *netdev,
11390 			     netdev_features_t features)
11391 {
11392 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11393 	struct i40e_vsi *vsi = np->vsi;
11394 	struct i40e_pf *pf = vsi->back;
11395 	bool need_reset;
11396 
11397 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11398 		i40e_pf_config_rss(pf);
11399 	else if (!(features & NETIF_F_RXHASH) &&
11400 		 netdev->features & NETIF_F_RXHASH)
11401 		i40e_clear_rss_lut(vsi);
11402 
11403 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
11404 		i40e_vlan_stripping_enable(vsi);
11405 	else
11406 		i40e_vlan_stripping_disable(vsi);
11407 
11408 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11409 		dev_err(&pf->pdev->dev,
11410 			"Offloaded tc filters active, can't turn hw_tc_offload off");
11411 		return -EINVAL;
11412 	}
11413 
11414 	need_reset = i40e_set_ntuple(pf, features);
11415 
11416 	if (need_reset)
11417 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11418 
11419 	return 0;
11420 }
11421 
11422 /**
11423  * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11424  * @pf: board private structure
11425  * @port: The UDP port to look up
11426  *
11427  * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11428  **/
11429 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11430 {
11431 	u8 i;
11432 
11433 	for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11434 		/* Do not report ports with pending deletions as
11435 		 * being available.
11436 		 */
11437 		if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
11438 			continue;
11439 		if (pf->udp_ports[i].port == port)
11440 			return i;
11441 	}
11442 
11443 	return i;
11444 }
11445 
11446 /**
11447  * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11448  * @netdev: This physical port's netdev
11449  * @ti: Tunnel endpoint information
11450  **/
11451 static void i40e_udp_tunnel_add(struct net_device *netdev,
11452 				struct udp_tunnel_info *ti)
11453 {
11454 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11455 	struct i40e_vsi *vsi = np->vsi;
11456 	struct i40e_pf *pf = vsi->back;
11457 	u16 port = ntohs(ti->port);
11458 	u8 next_idx;
11459 	u8 idx;
11460 
11461 	idx = i40e_get_udp_port_idx(pf, port);
11462 
11463 	/* Check if port already exists */
11464 	if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11465 		netdev_info(netdev, "port %d already offloaded\n", port);
11466 		return;
11467 	}
11468 
11469 	/* Now check if there is space to add the new port */
11470 	next_idx = i40e_get_udp_port_idx(pf, 0);
11471 
11472 	if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11473 		netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11474 			    port);
11475 		return;
11476 	}
11477 
11478 	switch (ti->type) {
11479 	case UDP_TUNNEL_TYPE_VXLAN:
11480 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11481 		break;
11482 	case UDP_TUNNEL_TYPE_GENEVE:
11483 		if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11484 			return;
11485 		pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11486 		break;
11487 	default:
11488 		return;
11489 	}
11490 
11491 	/* New port: add it and mark its index in the bitmap */
11492 	pf->udp_ports[next_idx].port = port;
11493 	pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
11494 	pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11495 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11496 }
11497 
11498 /**
11499  * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11500  * @netdev: This physical port's netdev
11501  * @ti: Tunnel endpoint information
11502  **/
11503 static void i40e_udp_tunnel_del(struct net_device *netdev,
11504 				struct udp_tunnel_info *ti)
11505 {
11506 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11507 	struct i40e_vsi *vsi = np->vsi;
11508 	struct i40e_pf *pf = vsi->back;
11509 	u16 port = ntohs(ti->port);
11510 	u8 idx;
11511 
11512 	idx = i40e_get_udp_port_idx(pf, port);
11513 
11514 	/* Check if port already exists */
11515 	if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11516 		goto not_found;
11517 
11518 	switch (ti->type) {
11519 	case UDP_TUNNEL_TYPE_VXLAN:
11520 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11521 			goto not_found;
11522 		break;
11523 	case UDP_TUNNEL_TYPE_GENEVE:
11524 		if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11525 			goto not_found;
11526 		break;
11527 	default:
11528 		goto not_found;
11529 	}
11530 
11531 	/* if port exists, set it to 0 (mark for deletion)
11532 	 * and make it pending
11533 	 */
11534 	pf->udp_ports[idx].port = 0;
11535 
11536 	/* Toggle pending bit instead of setting it. This way if we are
11537 	 * deleting a port that has yet to be added we just clear the pending
11538 	 * bit and don't have to worry about it.
11539 	 */
11540 	pf->pending_udp_bitmap ^= BIT_ULL(idx);
11541 	set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11542 
11543 	return;
11544 not_found:
11545 	netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11546 		    port);
11547 }
11548 
11549 static int i40e_get_phys_port_id(struct net_device *netdev,
11550 				 struct netdev_phys_item_id *ppid)
11551 {
11552 	struct i40e_netdev_priv *np = netdev_priv(netdev);
11553 	struct i40e_pf *pf = np->vsi->back;
11554 	struct i40e_hw *hw = &pf->hw;
11555 
11556 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11557 		return -EOPNOTSUPP;
11558 
11559 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11560 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11561 
11562 	return 0;
11563 }
11564 
11565 /**
11566  * i40e_ndo_fdb_add - add an entry to the hardware database
11567  * @ndm: the input from the stack
11568  * @tb: pointer to array of nladdr (unused)
11569  * @dev: the net device pointer
11570  * @addr: the MAC address entry being added
11571  * @vid: VLAN ID
11572  * @flags: instructions from stack about fdb operation
11573  */
11574 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11575 			    struct net_device *dev,
11576 			    const unsigned char *addr, u16 vid,
11577 			    u16 flags)
11578 {
11579 	struct i40e_netdev_priv *np = netdev_priv(dev);
11580 	struct i40e_pf *pf = np->vsi->back;
11581 	int err = 0;
11582 
11583 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11584 		return -EOPNOTSUPP;
11585 
11586 	if (vid) {
11587 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11588 		return -EINVAL;
11589 	}
11590 
11591 	/* Hardware does not support aging addresses so if a
11592 	 * ndm_state is given only allow permanent addresses
11593 	 */
11594 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11595 		netdev_info(dev, "FDB only supports static addresses\n");
11596 		return -EINVAL;
11597 	}
11598 
11599 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11600 		err = dev_uc_add_excl(dev, addr);
11601 	else if (is_multicast_ether_addr(addr))
11602 		err = dev_mc_add_excl(dev, addr);
11603 	else
11604 		err = -EINVAL;
11605 
11606 	/* Only return duplicate errors if NLM_F_EXCL is set */
11607 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
11608 		err = 0;
11609 
11610 	return err;
11611 }
11612 
11613 /**
11614  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11615  * @dev: the netdev being configured
11616  * @nlh: RTNL message
11617  * @flags: bridge flags
11618  *
11619  * Inserts a new hardware bridge if not already created and
11620  * enables the bridging mode requested (VEB or VEPA). If the
11621  * hardware bridge has already been inserted and the request
11622  * is to change the mode then that requires a PF reset to
11623  * allow rebuild of the components with required hardware
11624  * bridge mode enabled.
11625  *
11626  * Note: expects to be called while under rtnl_lock()
11627  **/
11628 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11629 				   struct nlmsghdr *nlh,
11630 				   u16 flags)
11631 {
11632 	struct i40e_netdev_priv *np = netdev_priv(dev);
11633 	struct i40e_vsi *vsi = np->vsi;
11634 	struct i40e_pf *pf = vsi->back;
11635 	struct i40e_veb *veb = NULL;
11636 	struct nlattr *attr, *br_spec;
11637 	int i, rem;
11638 
11639 	/* Only for PF VSI for now */
11640 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11641 		return -EOPNOTSUPP;
11642 
11643 	/* Find the HW bridge for PF VSI */
11644 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11645 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11646 			veb = pf->veb[i];
11647 	}
11648 
11649 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11650 
11651 	nla_for_each_nested(attr, br_spec, rem) {
11652 		__u16 mode;
11653 
11654 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
11655 			continue;
11656 
11657 		mode = nla_get_u16(attr);
11658 		if ((mode != BRIDGE_MODE_VEPA) &&
11659 		    (mode != BRIDGE_MODE_VEB))
11660 			return -EINVAL;
11661 
11662 		/* Insert a new HW bridge */
11663 		if (!veb) {
11664 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11665 					     vsi->tc_config.enabled_tc);
11666 			if (veb) {
11667 				veb->bridge_mode = mode;
11668 				i40e_config_bridge_mode(veb);
11669 			} else {
11670 				/* No Bridge HW offload available */
11671 				return -ENOENT;
11672 			}
11673 			break;
11674 		} else if (mode != veb->bridge_mode) {
11675 			/* Existing HW bridge but different mode needs reset */
11676 			veb->bridge_mode = mode;
11677 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11678 			if (mode == BRIDGE_MODE_VEB)
11679 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11680 			else
11681 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11682 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11683 			break;
11684 		}
11685 	}
11686 
11687 	return 0;
11688 }
11689 
11690 /**
11691  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11692  * @skb: skb buff
11693  * @pid: process id
11694  * @seq: RTNL message seq #
11695  * @dev: the netdev being configured
11696  * @filter_mask: unused
11697  * @nlflags: netlink flags passed in
11698  *
11699  * Return the mode in which the hardware bridge is operating in
11700  * i.e VEB or VEPA.
11701  **/
11702 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11703 				   struct net_device *dev,
11704 				   u32 __always_unused filter_mask,
11705 				   int nlflags)
11706 {
11707 	struct i40e_netdev_priv *np = netdev_priv(dev);
11708 	struct i40e_vsi *vsi = np->vsi;
11709 	struct i40e_pf *pf = vsi->back;
11710 	struct i40e_veb *veb = NULL;
11711 	int i;
11712 
11713 	/* Only for PF VSI for now */
11714 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11715 		return -EOPNOTSUPP;
11716 
11717 	/* Find the HW bridge for the PF VSI */
11718 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11719 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11720 			veb = pf->veb[i];
11721 	}
11722 
11723 	if (!veb)
11724 		return 0;
11725 
11726 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11727 				       0, 0, nlflags, filter_mask, NULL);
11728 }
11729 
11730 /**
11731  * i40e_features_check - Validate encapsulated packet conforms to limits
11732  * @skb: skb buff
11733  * @dev: This physical port's netdev
11734  * @features: Offload features that the stack believes apply
11735  **/
11736 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11737 					     struct net_device *dev,
11738 					     netdev_features_t features)
11739 {
11740 	size_t len;
11741 
11742 	/* No point in doing any of this if neither checksum nor GSO are
11743 	 * being requested for this frame.  We can rule out both by just
11744 	 * checking for CHECKSUM_PARTIAL
11745 	 */
11746 	if (skb->ip_summed != CHECKSUM_PARTIAL)
11747 		return features;
11748 
11749 	/* We cannot support GSO if the MSS is going to be less than
11750 	 * 64 bytes.  If it is then we need to drop support for GSO.
11751 	 */
11752 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11753 		features &= ~NETIF_F_GSO_MASK;
11754 
11755 	/* MACLEN can support at most 63 words */
11756 	len = skb_network_header(skb) - skb->data;
11757 	if (len & ~(63 * 2))
11758 		goto out_err;
11759 
11760 	/* IPLEN and EIPLEN can support at most 127 dwords */
11761 	len = skb_transport_header(skb) - skb_network_header(skb);
11762 	if (len & ~(127 * 4))
11763 		goto out_err;
11764 
11765 	if (skb->encapsulation) {
11766 		/* L4TUNLEN can support 127 words */
11767 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
11768 		if (len & ~(127 * 2))
11769 			goto out_err;
11770 
11771 		/* IPLEN can support at most 127 dwords */
11772 		len = skb_inner_transport_header(skb) -
11773 		      skb_inner_network_header(skb);
11774 		if (len & ~(127 * 4))
11775 			goto out_err;
11776 	}
11777 
11778 	/* No need to validate L4LEN as TCP is the only protocol with a
11779 	 * a flexible value and we support all possible values supported
11780 	 * by TCP, which is at most 15 dwords
11781 	 */
11782 
11783 	return features;
11784 out_err:
11785 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11786 }
11787 
11788 /**
11789  * i40e_xdp_setup - add/remove an XDP program
11790  * @vsi: VSI to changed
11791  * @prog: XDP program
11792  **/
11793 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11794 			  struct bpf_prog *prog)
11795 {
11796 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11797 	struct i40e_pf *pf = vsi->back;
11798 	struct bpf_prog *old_prog;
11799 	bool need_reset;
11800 	int i;
11801 
11802 	/* Don't allow frames that span over multiple buffers */
11803 	if (frame_size > vsi->rx_buf_len)
11804 		return -EINVAL;
11805 
11806 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11807 		return 0;
11808 
11809 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
11810 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11811 
11812 	if (need_reset)
11813 		i40e_prep_for_reset(pf, true);
11814 
11815 	old_prog = xchg(&vsi->xdp_prog, prog);
11816 
11817 	if (need_reset)
11818 		i40e_reset_and_rebuild(pf, true, true);
11819 
11820 	for (i = 0; i < vsi->num_queue_pairs; i++)
11821 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11822 
11823 	if (old_prog)
11824 		bpf_prog_put(old_prog);
11825 
11826 	return 0;
11827 }
11828 
11829 /**
11830  * i40e_xdp - implements ndo_bpf for i40e
11831  * @dev: netdevice
11832  * @xdp: XDP command
11833  **/
11834 static int i40e_xdp(struct net_device *dev,
11835 		    struct netdev_bpf *xdp)
11836 {
11837 	struct i40e_netdev_priv *np = netdev_priv(dev);
11838 	struct i40e_vsi *vsi = np->vsi;
11839 
11840 	if (vsi->type != I40E_VSI_MAIN)
11841 		return -EINVAL;
11842 
11843 	switch (xdp->command) {
11844 	case XDP_SETUP_PROG:
11845 		return i40e_xdp_setup(vsi, xdp->prog);
11846 	case XDP_QUERY_PROG:
11847 		xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
11848 		return 0;
11849 	default:
11850 		return -EINVAL;
11851 	}
11852 }
11853 
11854 static const struct net_device_ops i40e_netdev_ops = {
11855 	.ndo_open		= i40e_open,
11856 	.ndo_stop		= i40e_close,
11857 	.ndo_start_xmit		= i40e_lan_xmit_frame,
11858 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
11859 	.ndo_set_rx_mode	= i40e_set_rx_mode,
11860 	.ndo_validate_addr	= eth_validate_addr,
11861 	.ndo_set_mac_address	= i40e_set_mac,
11862 	.ndo_change_mtu		= i40e_change_mtu,
11863 	.ndo_do_ioctl		= i40e_ioctl,
11864 	.ndo_tx_timeout		= i40e_tx_timeout,
11865 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
11866 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
11867 #ifdef CONFIG_NET_POLL_CONTROLLER
11868 	.ndo_poll_controller	= i40e_netpoll,
11869 #endif
11870 	.ndo_setup_tc		= __i40e_setup_tc,
11871 	.ndo_set_features	= i40e_set_features,
11872 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
11873 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
11874 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
11875 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
11876 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
11877 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
11878 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
11879 	.ndo_udp_tunnel_add	= i40e_udp_tunnel_add,
11880 	.ndo_udp_tunnel_del	= i40e_udp_tunnel_del,
11881 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
11882 	.ndo_fdb_add		= i40e_ndo_fdb_add,
11883 	.ndo_features_check	= i40e_features_check,
11884 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
11885 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
11886 	.ndo_bpf		= i40e_xdp,
11887 	.ndo_xdp_xmit		= i40e_xdp_xmit,
11888 };
11889 
11890 /**
11891  * i40e_config_netdev - Setup the netdev flags
11892  * @vsi: the VSI being configured
11893  *
11894  * Returns 0 on success, negative value on failure
11895  **/
11896 static int i40e_config_netdev(struct i40e_vsi *vsi)
11897 {
11898 	struct i40e_pf *pf = vsi->back;
11899 	struct i40e_hw *hw = &pf->hw;
11900 	struct i40e_netdev_priv *np;
11901 	struct net_device *netdev;
11902 	u8 broadcast[ETH_ALEN];
11903 	u8 mac_addr[ETH_ALEN];
11904 	int etherdev_size;
11905 	netdev_features_t hw_enc_features;
11906 	netdev_features_t hw_features;
11907 
11908 	etherdev_size = sizeof(struct i40e_netdev_priv);
11909 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
11910 	if (!netdev)
11911 		return -ENOMEM;
11912 
11913 	vsi->netdev = netdev;
11914 	np = netdev_priv(netdev);
11915 	np->vsi = vsi;
11916 
11917 	hw_enc_features = NETIF_F_SG			|
11918 			  NETIF_F_IP_CSUM		|
11919 			  NETIF_F_IPV6_CSUM		|
11920 			  NETIF_F_HIGHDMA		|
11921 			  NETIF_F_SOFT_FEATURES		|
11922 			  NETIF_F_TSO			|
11923 			  NETIF_F_TSO_ECN		|
11924 			  NETIF_F_TSO6			|
11925 			  NETIF_F_GSO_GRE		|
11926 			  NETIF_F_GSO_GRE_CSUM		|
11927 			  NETIF_F_GSO_PARTIAL		|
11928 			  NETIF_F_GSO_UDP_TUNNEL	|
11929 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
11930 			  NETIF_F_SCTP_CRC		|
11931 			  NETIF_F_RXHASH		|
11932 			  NETIF_F_RXCSUM		|
11933 			  0;
11934 
11935 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
11936 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
11937 
11938 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
11939 
11940 	netdev->hw_enc_features |= hw_enc_features;
11941 
11942 	/* record features VLANs can make use of */
11943 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
11944 
11945 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
11946 		netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
11947 
11948 	hw_features = hw_enc_features		|
11949 		      NETIF_F_HW_VLAN_CTAG_TX	|
11950 		      NETIF_F_HW_VLAN_CTAG_RX;
11951 
11952 	netdev->hw_features |= hw_features;
11953 
11954 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
11955 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
11956 
11957 	if (vsi->type == I40E_VSI_MAIN) {
11958 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
11959 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
11960 		/* The following steps are necessary for two reasons. First,
11961 		 * some older NVM configurations load a default MAC-VLAN
11962 		 * filter that will accept any tagged packet, and we want to
11963 		 * replace this with a normal filter. Additionally, it is
11964 		 * possible our MAC address was provided by the platform using
11965 		 * Open Firmware or similar.
11966 		 *
11967 		 * Thus, we need to remove the default filter and install one
11968 		 * specific to the MAC address.
11969 		 */
11970 		i40e_rm_default_mac_filter(vsi, mac_addr);
11971 		spin_lock_bh(&vsi->mac_filter_hash_lock);
11972 		i40e_add_mac_filter(vsi, mac_addr);
11973 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
11974 	} else {
11975 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
11976 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
11977 		 * the end, which is 4 bytes long, so force truncation of the
11978 		 * original name by IFNAMSIZ - 4
11979 		 */
11980 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
11981 			 IFNAMSIZ - 4,
11982 			 pf->vsi[pf->lan_vsi]->netdev->name);
11983 		eth_random_addr(mac_addr);
11984 
11985 		spin_lock_bh(&vsi->mac_filter_hash_lock);
11986 		i40e_add_mac_filter(vsi, mac_addr);
11987 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
11988 	}
11989 
11990 	/* Add the broadcast filter so that we initially will receive
11991 	 * broadcast packets. Note that when a new VLAN is first added the
11992 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
11993 	 * specific filters as part of transitioning into "vlan" operation.
11994 	 * When more VLANs are added, the driver will copy each existing MAC
11995 	 * filter and add it for the new VLAN.
11996 	 *
11997 	 * Broadcast filters are handled specially by
11998 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
11999 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
12000 	 * filter. The subtask will update the correct broadcast promiscuous
12001 	 * bits as VLANs become active or inactive.
12002 	 */
12003 	eth_broadcast_addr(broadcast);
12004 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12005 	i40e_add_mac_filter(vsi, broadcast);
12006 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12007 
12008 	ether_addr_copy(netdev->dev_addr, mac_addr);
12009 	ether_addr_copy(netdev->perm_addr, mac_addr);
12010 
12011 	netdev->priv_flags |= IFF_UNICAST_FLT;
12012 	netdev->priv_flags |= IFF_SUPP_NOFCS;
12013 	/* Setup netdev TC information */
12014 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12015 
12016 	netdev->netdev_ops = &i40e_netdev_ops;
12017 	netdev->watchdog_timeo = 5 * HZ;
12018 	i40e_set_ethtool_ops(netdev);
12019 
12020 	/* MTU range: 68 - 9706 */
12021 	netdev->min_mtu = ETH_MIN_MTU;
12022 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12023 
12024 	return 0;
12025 }
12026 
12027 /**
12028  * i40e_vsi_delete - Delete a VSI from the switch
12029  * @vsi: the VSI being removed
12030  *
12031  * Returns 0 on success, negative value on failure
12032  **/
12033 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12034 {
12035 	/* remove default VSI is not allowed */
12036 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12037 		return;
12038 
12039 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
12040 }
12041 
12042 /**
12043  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
12044  * @vsi: the VSI being queried
12045  *
12046  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
12047  **/
12048 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
12049 {
12050 	struct i40e_veb *veb;
12051 	struct i40e_pf *pf = vsi->back;
12052 
12053 	/* Uplink is not a bridge so default to VEB */
12054 	if (vsi->veb_idx == I40E_NO_VEB)
12055 		return 1;
12056 
12057 	veb = pf->veb[vsi->veb_idx];
12058 	if (!veb) {
12059 		dev_info(&pf->pdev->dev,
12060 			 "There is no veb associated with the bridge\n");
12061 		return -ENOENT;
12062 	}
12063 
12064 	/* Uplink is a bridge in VEPA mode */
12065 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
12066 		return 0;
12067 	} else {
12068 		/* Uplink is a bridge in VEB mode */
12069 		return 1;
12070 	}
12071 
12072 	/* VEPA is now default bridge, so return 0 */
12073 	return 0;
12074 }
12075 
12076 /**
12077  * i40e_add_vsi - Add a VSI to the switch
12078  * @vsi: the VSI being configured
12079  *
12080  * This initializes a VSI context depending on the VSI type to be added and
12081  * passes it down to the add_vsi aq command.
12082  **/
12083 static int i40e_add_vsi(struct i40e_vsi *vsi)
12084 {
12085 	int ret = -ENODEV;
12086 	struct i40e_pf *pf = vsi->back;
12087 	struct i40e_hw *hw = &pf->hw;
12088 	struct i40e_vsi_context ctxt;
12089 	struct i40e_mac_filter *f;
12090 	struct hlist_node *h;
12091 	int bkt;
12092 
12093 	u8 enabled_tc = 0x1; /* TC0 enabled */
12094 	int f_count = 0;
12095 
12096 	memset(&ctxt, 0, sizeof(ctxt));
12097 	switch (vsi->type) {
12098 	case I40E_VSI_MAIN:
12099 		/* The PF's main VSI is already setup as part of the
12100 		 * device initialization, so we'll not bother with
12101 		 * the add_vsi call, but we will retrieve the current
12102 		 * VSI context.
12103 		 */
12104 		ctxt.seid = pf->main_vsi_seid;
12105 		ctxt.pf_num = pf->hw.pf_id;
12106 		ctxt.vf_num = 0;
12107 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
12108 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12109 		if (ret) {
12110 			dev_info(&pf->pdev->dev,
12111 				 "couldn't get PF vsi config, err %s aq_err %s\n",
12112 				 i40e_stat_str(&pf->hw, ret),
12113 				 i40e_aq_str(&pf->hw,
12114 					     pf->hw.aq.asq_last_status));
12115 			return -ENOENT;
12116 		}
12117 		vsi->info = ctxt.info;
12118 		vsi->info.valid_sections = 0;
12119 
12120 		vsi->seid = ctxt.seid;
12121 		vsi->id = ctxt.vsi_number;
12122 
12123 		enabled_tc = i40e_pf_get_tc_map(pf);
12124 
12125 		/* Source pruning is enabled by default, so the flag is
12126 		 * negative logic - if it's set, we need to fiddle with
12127 		 * the VSI to disable source pruning.
12128 		 */
12129 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
12130 			memset(&ctxt, 0, sizeof(ctxt));
12131 			ctxt.seid = pf->main_vsi_seid;
12132 			ctxt.pf_num = pf->hw.pf_id;
12133 			ctxt.vf_num = 0;
12134 			ctxt.info.valid_sections |=
12135 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12136 			ctxt.info.switch_id =
12137 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
12138 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12139 			if (ret) {
12140 				dev_info(&pf->pdev->dev,
12141 					 "update vsi failed, err %s aq_err %s\n",
12142 					 i40e_stat_str(&pf->hw, ret),
12143 					 i40e_aq_str(&pf->hw,
12144 						     pf->hw.aq.asq_last_status));
12145 				ret = -ENOENT;
12146 				goto err;
12147 			}
12148 		}
12149 
12150 		/* MFP mode setup queue map and update VSI */
12151 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
12152 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
12153 			memset(&ctxt, 0, sizeof(ctxt));
12154 			ctxt.seid = pf->main_vsi_seid;
12155 			ctxt.pf_num = pf->hw.pf_id;
12156 			ctxt.vf_num = 0;
12157 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
12158 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12159 			if (ret) {
12160 				dev_info(&pf->pdev->dev,
12161 					 "update vsi failed, err %s aq_err %s\n",
12162 					 i40e_stat_str(&pf->hw, ret),
12163 					 i40e_aq_str(&pf->hw,
12164 						    pf->hw.aq.asq_last_status));
12165 				ret = -ENOENT;
12166 				goto err;
12167 			}
12168 			/* update the local VSI info queue map */
12169 			i40e_vsi_update_queue_map(vsi, &ctxt);
12170 			vsi->info.valid_sections = 0;
12171 		} else {
12172 			/* Default/Main VSI is only enabled for TC0
12173 			 * reconfigure it to enable all TCs that are
12174 			 * available on the port in SFP mode.
12175 			 * For MFP case the iSCSI PF would use this
12176 			 * flow to enable LAN+iSCSI TC.
12177 			 */
12178 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
12179 			if (ret) {
12180 				/* Single TC condition is not fatal,
12181 				 * message and continue
12182 				 */
12183 				dev_info(&pf->pdev->dev,
12184 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12185 					 enabled_tc,
12186 					 i40e_stat_str(&pf->hw, ret),
12187 					 i40e_aq_str(&pf->hw,
12188 						    pf->hw.aq.asq_last_status));
12189 			}
12190 		}
12191 		break;
12192 
12193 	case I40E_VSI_FDIR:
12194 		ctxt.pf_num = hw->pf_id;
12195 		ctxt.vf_num = 0;
12196 		ctxt.uplink_seid = vsi->uplink_seid;
12197 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12198 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12199 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12200 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
12201 			ctxt.info.valid_sections |=
12202 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12203 			ctxt.info.switch_id =
12204 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12205 		}
12206 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12207 		break;
12208 
12209 	case I40E_VSI_VMDQ2:
12210 		ctxt.pf_num = hw->pf_id;
12211 		ctxt.vf_num = 0;
12212 		ctxt.uplink_seid = vsi->uplink_seid;
12213 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12214 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12215 
12216 		/* This VSI is connected to VEB so the switch_id
12217 		 * should be set to zero by default.
12218 		 */
12219 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12220 			ctxt.info.valid_sections |=
12221 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12222 			ctxt.info.switch_id =
12223 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12224 		}
12225 
12226 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12227 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12228 		break;
12229 
12230 	case I40E_VSI_SRIOV:
12231 		ctxt.pf_num = hw->pf_id;
12232 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12233 		ctxt.uplink_seid = vsi->uplink_seid;
12234 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12235 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12236 
12237 		/* This VSI is connected to VEB so the switch_id
12238 		 * should be set to zero by default.
12239 		 */
12240 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12241 			ctxt.info.valid_sections |=
12242 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12243 			ctxt.info.switch_id =
12244 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12245 		}
12246 
12247 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12248 			ctxt.info.valid_sections |=
12249 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12250 			ctxt.info.queueing_opt_flags |=
12251 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12252 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12253 		}
12254 
12255 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12256 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12257 		if (pf->vf[vsi->vf_id].spoofchk) {
12258 			ctxt.info.valid_sections |=
12259 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12260 			ctxt.info.sec_flags |=
12261 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12262 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12263 		}
12264 		/* Setup the VSI tx/rx queue map for TC0 only for now */
12265 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12266 		break;
12267 
12268 	case I40E_VSI_IWARP:
12269 		/* send down message to iWARP */
12270 		break;
12271 
12272 	default:
12273 		return -ENODEV;
12274 	}
12275 
12276 	if (vsi->type != I40E_VSI_MAIN) {
12277 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12278 		if (ret) {
12279 			dev_info(&vsi->back->pdev->dev,
12280 				 "add vsi failed, err %s aq_err %s\n",
12281 				 i40e_stat_str(&pf->hw, ret),
12282 				 i40e_aq_str(&pf->hw,
12283 					     pf->hw.aq.asq_last_status));
12284 			ret = -ENOENT;
12285 			goto err;
12286 		}
12287 		vsi->info = ctxt.info;
12288 		vsi->info.valid_sections = 0;
12289 		vsi->seid = ctxt.seid;
12290 		vsi->id = ctxt.vsi_number;
12291 	}
12292 
12293 	vsi->active_filters = 0;
12294 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12295 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12296 	/* If macvlan filters already exist, force them to get loaded */
12297 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12298 		f->state = I40E_FILTER_NEW;
12299 		f_count++;
12300 	}
12301 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12302 
12303 	if (f_count) {
12304 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12305 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
12306 	}
12307 
12308 	/* Update VSI BW information */
12309 	ret = i40e_vsi_get_bw_info(vsi);
12310 	if (ret) {
12311 		dev_info(&pf->pdev->dev,
12312 			 "couldn't get vsi bw info, err %s aq_err %s\n",
12313 			 i40e_stat_str(&pf->hw, ret),
12314 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12315 		/* VSI is already added so not tearing that up */
12316 		ret = 0;
12317 	}
12318 
12319 err:
12320 	return ret;
12321 }
12322 
12323 /**
12324  * i40e_vsi_release - Delete a VSI and free its resources
12325  * @vsi: the VSI being removed
12326  *
12327  * Returns 0 on success or < 0 on error
12328  **/
12329 int i40e_vsi_release(struct i40e_vsi *vsi)
12330 {
12331 	struct i40e_mac_filter *f;
12332 	struct hlist_node *h;
12333 	struct i40e_veb *veb = NULL;
12334 	struct i40e_pf *pf;
12335 	u16 uplink_seid;
12336 	int i, n, bkt;
12337 
12338 	pf = vsi->back;
12339 
12340 	/* release of a VEB-owner or last VSI is not allowed */
12341 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12342 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12343 			 vsi->seid, vsi->uplink_seid);
12344 		return -ENODEV;
12345 	}
12346 	if (vsi == pf->vsi[pf->lan_vsi] &&
12347 	    !test_bit(__I40E_DOWN, pf->state)) {
12348 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12349 		return -ENODEV;
12350 	}
12351 
12352 	uplink_seid = vsi->uplink_seid;
12353 	if (vsi->type != I40E_VSI_SRIOV) {
12354 		if (vsi->netdev_registered) {
12355 			vsi->netdev_registered = false;
12356 			if (vsi->netdev) {
12357 				/* results in a call to i40e_close() */
12358 				unregister_netdev(vsi->netdev);
12359 			}
12360 		} else {
12361 			i40e_vsi_close(vsi);
12362 		}
12363 		i40e_vsi_disable_irq(vsi);
12364 	}
12365 
12366 	spin_lock_bh(&vsi->mac_filter_hash_lock);
12367 
12368 	/* clear the sync flag on all filters */
12369 	if (vsi->netdev) {
12370 		__dev_uc_unsync(vsi->netdev, NULL);
12371 		__dev_mc_unsync(vsi->netdev, NULL);
12372 	}
12373 
12374 	/* make sure any remaining filters are marked for deletion */
12375 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12376 		__i40e_del_filter(vsi, f);
12377 
12378 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
12379 
12380 	i40e_sync_vsi_filters(vsi);
12381 
12382 	i40e_vsi_delete(vsi);
12383 	i40e_vsi_free_q_vectors(vsi);
12384 	if (vsi->netdev) {
12385 		free_netdev(vsi->netdev);
12386 		vsi->netdev = NULL;
12387 	}
12388 	i40e_vsi_clear_rings(vsi);
12389 	i40e_vsi_clear(vsi);
12390 
12391 	/* If this was the last thing on the VEB, except for the
12392 	 * controlling VSI, remove the VEB, which puts the controlling
12393 	 * VSI onto the next level down in the switch.
12394 	 *
12395 	 * Well, okay, there's one more exception here: don't remove
12396 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
12397 	 * from up the network stack.
12398 	 */
12399 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12400 		if (pf->vsi[i] &&
12401 		    pf->vsi[i]->uplink_seid == uplink_seid &&
12402 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12403 			n++;      /* count the VSIs */
12404 		}
12405 	}
12406 	for (i = 0; i < I40E_MAX_VEB; i++) {
12407 		if (!pf->veb[i])
12408 			continue;
12409 		if (pf->veb[i]->uplink_seid == uplink_seid)
12410 			n++;     /* count the VEBs */
12411 		if (pf->veb[i]->seid == uplink_seid)
12412 			veb = pf->veb[i];
12413 	}
12414 	if (n == 0 && veb && veb->uplink_seid != 0)
12415 		i40e_veb_release(veb);
12416 
12417 	return 0;
12418 }
12419 
12420 /**
12421  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12422  * @vsi: ptr to the VSI
12423  *
12424  * This should only be called after i40e_vsi_mem_alloc() which allocates the
12425  * corresponding SW VSI structure and initializes num_queue_pairs for the
12426  * newly allocated VSI.
12427  *
12428  * Returns 0 on success or negative on failure
12429  **/
12430 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12431 {
12432 	int ret = -ENOENT;
12433 	struct i40e_pf *pf = vsi->back;
12434 
12435 	if (vsi->q_vectors[0]) {
12436 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12437 			 vsi->seid);
12438 		return -EEXIST;
12439 	}
12440 
12441 	if (vsi->base_vector) {
12442 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12443 			 vsi->seid, vsi->base_vector);
12444 		return -EEXIST;
12445 	}
12446 
12447 	ret = i40e_vsi_alloc_q_vectors(vsi);
12448 	if (ret) {
12449 		dev_info(&pf->pdev->dev,
12450 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12451 			 vsi->num_q_vectors, vsi->seid, ret);
12452 		vsi->num_q_vectors = 0;
12453 		goto vector_setup_out;
12454 	}
12455 
12456 	/* In Legacy mode, we do not have to get any other vector since we
12457 	 * piggyback on the misc/ICR0 for queue interrupts.
12458 	*/
12459 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12460 		return ret;
12461 	if (vsi->num_q_vectors)
12462 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12463 						 vsi->num_q_vectors, vsi->idx);
12464 	if (vsi->base_vector < 0) {
12465 		dev_info(&pf->pdev->dev,
12466 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12467 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12468 		i40e_vsi_free_q_vectors(vsi);
12469 		ret = -ENOENT;
12470 		goto vector_setup_out;
12471 	}
12472 
12473 vector_setup_out:
12474 	return ret;
12475 }
12476 
12477 /**
12478  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12479  * @vsi: pointer to the vsi.
12480  *
12481  * This re-allocates a vsi's queue resources.
12482  *
12483  * Returns pointer to the successfully allocated and configured VSI sw struct
12484  * on success, otherwise returns NULL on failure.
12485  **/
12486 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12487 {
12488 	u16 alloc_queue_pairs;
12489 	struct i40e_pf *pf;
12490 	u8 enabled_tc;
12491 	int ret;
12492 
12493 	if (!vsi)
12494 		return NULL;
12495 
12496 	pf = vsi->back;
12497 
12498 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12499 	i40e_vsi_clear_rings(vsi);
12500 
12501 	i40e_vsi_free_arrays(vsi, false);
12502 	i40e_set_num_rings_in_vsi(vsi);
12503 	ret = i40e_vsi_alloc_arrays(vsi, false);
12504 	if (ret)
12505 		goto err_vsi;
12506 
12507 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12508 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12509 
12510 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12511 	if (ret < 0) {
12512 		dev_info(&pf->pdev->dev,
12513 			 "failed to get tracking for %d queues for VSI %d err %d\n",
12514 			 alloc_queue_pairs, vsi->seid, ret);
12515 		goto err_vsi;
12516 	}
12517 	vsi->base_queue = ret;
12518 
12519 	/* Update the FW view of the VSI. Force a reset of TC and queue
12520 	 * layout configurations.
12521 	 */
12522 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12523 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12524 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12525 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12526 	if (vsi->type == I40E_VSI_MAIN)
12527 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12528 
12529 	/* assign it some queues */
12530 	ret = i40e_alloc_rings(vsi);
12531 	if (ret)
12532 		goto err_rings;
12533 
12534 	/* map all of the rings to the q_vectors */
12535 	i40e_vsi_map_rings_to_vectors(vsi);
12536 	return vsi;
12537 
12538 err_rings:
12539 	i40e_vsi_free_q_vectors(vsi);
12540 	if (vsi->netdev_registered) {
12541 		vsi->netdev_registered = false;
12542 		unregister_netdev(vsi->netdev);
12543 		free_netdev(vsi->netdev);
12544 		vsi->netdev = NULL;
12545 	}
12546 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12547 err_vsi:
12548 	i40e_vsi_clear(vsi);
12549 	return NULL;
12550 }
12551 
12552 /**
12553  * i40e_vsi_setup - Set up a VSI by a given type
12554  * @pf: board private structure
12555  * @type: VSI type
12556  * @uplink_seid: the switch element to link to
12557  * @param1: usage depends upon VSI type. For VF types, indicates VF id
12558  *
12559  * This allocates the sw VSI structure and its queue resources, then add a VSI
12560  * to the identified VEB.
12561  *
12562  * Returns pointer to the successfully allocated and configure VSI sw struct on
12563  * success, otherwise returns NULL on failure.
12564  **/
12565 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12566 				u16 uplink_seid, u32 param1)
12567 {
12568 	struct i40e_vsi *vsi = NULL;
12569 	struct i40e_veb *veb = NULL;
12570 	u16 alloc_queue_pairs;
12571 	int ret, i;
12572 	int v_idx;
12573 
12574 	/* The requested uplink_seid must be either
12575 	 *     - the PF's port seid
12576 	 *              no VEB is needed because this is the PF
12577 	 *              or this is a Flow Director special case VSI
12578 	 *     - seid of an existing VEB
12579 	 *     - seid of a VSI that owns an existing VEB
12580 	 *     - seid of a VSI that doesn't own a VEB
12581 	 *              a new VEB is created and the VSI becomes the owner
12582 	 *     - seid of the PF VSI, which is what creates the first VEB
12583 	 *              this is a special case of the previous
12584 	 *
12585 	 * Find which uplink_seid we were given and create a new VEB if needed
12586 	 */
12587 	for (i = 0; i < I40E_MAX_VEB; i++) {
12588 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12589 			veb = pf->veb[i];
12590 			break;
12591 		}
12592 	}
12593 
12594 	if (!veb && uplink_seid != pf->mac_seid) {
12595 
12596 		for (i = 0; i < pf->num_alloc_vsi; i++) {
12597 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12598 				vsi = pf->vsi[i];
12599 				break;
12600 			}
12601 		}
12602 		if (!vsi) {
12603 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12604 				 uplink_seid);
12605 			return NULL;
12606 		}
12607 
12608 		if (vsi->uplink_seid == pf->mac_seid)
12609 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12610 					     vsi->tc_config.enabled_tc);
12611 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12612 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12613 					     vsi->tc_config.enabled_tc);
12614 		if (veb) {
12615 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12616 				dev_info(&vsi->back->pdev->dev,
12617 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
12618 				return NULL;
12619 			}
12620 			/* We come up by default in VEPA mode if SRIOV is not
12621 			 * already enabled, in which case we can't force VEPA
12622 			 * mode.
12623 			 */
12624 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12625 				veb->bridge_mode = BRIDGE_MODE_VEPA;
12626 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12627 			}
12628 			i40e_config_bridge_mode(veb);
12629 		}
12630 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12631 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12632 				veb = pf->veb[i];
12633 		}
12634 		if (!veb) {
12635 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12636 			return NULL;
12637 		}
12638 
12639 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12640 		uplink_seid = veb->seid;
12641 	}
12642 
12643 	/* get vsi sw struct */
12644 	v_idx = i40e_vsi_mem_alloc(pf, type);
12645 	if (v_idx < 0)
12646 		goto err_alloc;
12647 	vsi = pf->vsi[v_idx];
12648 	if (!vsi)
12649 		goto err_alloc;
12650 	vsi->type = type;
12651 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12652 
12653 	if (type == I40E_VSI_MAIN)
12654 		pf->lan_vsi = v_idx;
12655 	else if (type == I40E_VSI_SRIOV)
12656 		vsi->vf_id = param1;
12657 	/* assign it some queues */
12658 	alloc_queue_pairs = vsi->alloc_queue_pairs *
12659 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12660 
12661 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12662 	if (ret < 0) {
12663 		dev_info(&pf->pdev->dev,
12664 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
12665 			 alloc_queue_pairs, vsi->seid, ret);
12666 		goto err_vsi;
12667 	}
12668 	vsi->base_queue = ret;
12669 
12670 	/* get a VSI from the hardware */
12671 	vsi->uplink_seid = uplink_seid;
12672 	ret = i40e_add_vsi(vsi);
12673 	if (ret)
12674 		goto err_vsi;
12675 
12676 	switch (vsi->type) {
12677 	/* setup the netdev if needed */
12678 	case I40E_VSI_MAIN:
12679 	case I40E_VSI_VMDQ2:
12680 		ret = i40e_config_netdev(vsi);
12681 		if (ret)
12682 			goto err_netdev;
12683 		ret = register_netdev(vsi->netdev);
12684 		if (ret)
12685 			goto err_netdev;
12686 		vsi->netdev_registered = true;
12687 		netif_carrier_off(vsi->netdev);
12688 #ifdef CONFIG_I40E_DCB
12689 		/* Setup DCB netlink interface */
12690 		i40e_dcbnl_setup(vsi);
12691 #endif /* CONFIG_I40E_DCB */
12692 		/* fall through */
12693 
12694 	case I40E_VSI_FDIR:
12695 		/* set up vectors and rings if needed */
12696 		ret = i40e_vsi_setup_vectors(vsi);
12697 		if (ret)
12698 			goto err_msix;
12699 
12700 		ret = i40e_alloc_rings(vsi);
12701 		if (ret)
12702 			goto err_rings;
12703 
12704 		/* map all of the rings to the q_vectors */
12705 		i40e_vsi_map_rings_to_vectors(vsi);
12706 
12707 		i40e_vsi_reset_stats(vsi);
12708 		break;
12709 
12710 	default:
12711 		/* no netdev or rings for the other VSI types */
12712 		break;
12713 	}
12714 
12715 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
12716 	    (vsi->type == I40E_VSI_VMDQ2)) {
12717 		ret = i40e_vsi_config_rss(vsi);
12718 	}
12719 	return vsi;
12720 
12721 err_rings:
12722 	i40e_vsi_free_q_vectors(vsi);
12723 err_msix:
12724 	if (vsi->netdev_registered) {
12725 		vsi->netdev_registered = false;
12726 		unregister_netdev(vsi->netdev);
12727 		free_netdev(vsi->netdev);
12728 		vsi->netdev = NULL;
12729 	}
12730 err_netdev:
12731 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12732 err_vsi:
12733 	i40e_vsi_clear(vsi);
12734 err_alloc:
12735 	return NULL;
12736 }
12737 
12738 /**
12739  * i40e_veb_get_bw_info - Query VEB BW information
12740  * @veb: the veb to query
12741  *
12742  * Query the Tx scheduler BW configuration data for given VEB
12743  **/
12744 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
12745 {
12746 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
12747 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
12748 	struct i40e_pf *pf = veb->pf;
12749 	struct i40e_hw *hw = &pf->hw;
12750 	u32 tc_bw_max;
12751 	int ret = 0;
12752 	int i;
12753 
12754 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
12755 						  &bw_data, NULL);
12756 	if (ret) {
12757 		dev_info(&pf->pdev->dev,
12758 			 "query veb bw config failed, err %s aq_err %s\n",
12759 			 i40e_stat_str(&pf->hw, ret),
12760 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12761 		goto out;
12762 	}
12763 
12764 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
12765 						   &ets_data, NULL);
12766 	if (ret) {
12767 		dev_info(&pf->pdev->dev,
12768 			 "query veb bw ets config failed, err %s aq_err %s\n",
12769 			 i40e_stat_str(&pf->hw, ret),
12770 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12771 		goto out;
12772 	}
12773 
12774 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
12775 	veb->bw_max_quanta = ets_data.tc_bw_max;
12776 	veb->is_abs_credits = bw_data.absolute_credits_enable;
12777 	veb->enabled_tc = ets_data.tc_valid_bits;
12778 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
12779 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
12780 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
12781 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
12782 		veb->bw_tc_limit_credits[i] =
12783 					le16_to_cpu(bw_data.tc_bw_limits[i]);
12784 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
12785 	}
12786 
12787 out:
12788 	return ret;
12789 }
12790 
12791 /**
12792  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
12793  * @pf: board private structure
12794  *
12795  * On error: returns error code (negative)
12796  * On success: returns vsi index in PF (positive)
12797  **/
12798 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
12799 {
12800 	int ret = -ENOENT;
12801 	struct i40e_veb *veb;
12802 	int i;
12803 
12804 	/* Need to protect the allocation of switch elements at the PF level */
12805 	mutex_lock(&pf->switch_mutex);
12806 
12807 	/* VEB list may be fragmented if VEB creation/destruction has
12808 	 * been happening.  We can afford to do a quick scan to look
12809 	 * for any free slots in the list.
12810 	 *
12811 	 * find next empty veb slot, looping back around if necessary
12812 	 */
12813 	i = 0;
12814 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
12815 		i++;
12816 	if (i >= I40E_MAX_VEB) {
12817 		ret = -ENOMEM;
12818 		goto err_alloc_veb;  /* out of VEB slots! */
12819 	}
12820 
12821 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
12822 	if (!veb) {
12823 		ret = -ENOMEM;
12824 		goto err_alloc_veb;
12825 	}
12826 	veb->pf = pf;
12827 	veb->idx = i;
12828 	veb->enabled_tc = 1;
12829 
12830 	pf->veb[i] = veb;
12831 	ret = i;
12832 err_alloc_veb:
12833 	mutex_unlock(&pf->switch_mutex);
12834 	return ret;
12835 }
12836 
12837 /**
12838  * i40e_switch_branch_release - Delete a branch of the switch tree
12839  * @branch: where to start deleting
12840  *
12841  * This uses recursion to find the tips of the branch to be
12842  * removed, deleting until we get back to and can delete this VEB.
12843  **/
12844 static void i40e_switch_branch_release(struct i40e_veb *branch)
12845 {
12846 	struct i40e_pf *pf = branch->pf;
12847 	u16 branch_seid = branch->seid;
12848 	u16 veb_idx = branch->idx;
12849 	int i;
12850 
12851 	/* release any VEBs on this VEB - RECURSION */
12852 	for (i = 0; i < I40E_MAX_VEB; i++) {
12853 		if (!pf->veb[i])
12854 			continue;
12855 		if (pf->veb[i]->uplink_seid == branch->seid)
12856 			i40e_switch_branch_release(pf->veb[i]);
12857 	}
12858 
12859 	/* Release the VSIs on this VEB, but not the owner VSI.
12860 	 *
12861 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
12862 	 *       the VEB itself, so don't use (*branch) after this loop.
12863 	 */
12864 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12865 		if (!pf->vsi[i])
12866 			continue;
12867 		if (pf->vsi[i]->uplink_seid == branch_seid &&
12868 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12869 			i40e_vsi_release(pf->vsi[i]);
12870 		}
12871 	}
12872 
12873 	/* There's one corner case where the VEB might not have been
12874 	 * removed, so double check it here and remove it if needed.
12875 	 * This case happens if the veb was created from the debugfs
12876 	 * commands and no VSIs were added to it.
12877 	 */
12878 	if (pf->veb[veb_idx])
12879 		i40e_veb_release(pf->veb[veb_idx]);
12880 }
12881 
12882 /**
12883  * i40e_veb_clear - remove veb struct
12884  * @veb: the veb to remove
12885  **/
12886 static void i40e_veb_clear(struct i40e_veb *veb)
12887 {
12888 	if (!veb)
12889 		return;
12890 
12891 	if (veb->pf) {
12892 		struct i40e_pf *pf = veb->pf;
12893 
12894 		mutex_lock(&pf->switch_mutex);
12895 		if (pf->veb[veb->idx] == veb)
12896 			pf->veb[veb->idx] = NULL;
12897 		mutex_unlock(&pf->switch_mutex);
12898 	}
12899 
12900 	kfree(veb);
12901 }
12902 
12903 /**
12904  * i40e_veb_release - Delete a VEB and free its resources
12905  * @veb: the VEB being removed
12906  **/
12907 void i40e_veb_release(struct i40e_veb *veb)
12908 {
12909 	struct i40e_vsi *vsi = NULL;
12910 	struct i40e_pf *pf;
12911 	int i, n = 0;
12912 
12913 	pf = veb->pf;
12914 
12915 	/* find the remaining VSI and check for extras */
12916 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12917 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
12918 			n++;
12919 			vsi = pf->vsi[i];
12920 		}
12921 	}
12922 	if (n != 1) {
12923 		dev_info(&pf->pdev->dev,
12924 			 "can't remove VEB %d with %d VSIs left\n",
12925 			 veb->seid, n);
12926 		return;
12927 	}
12928 
12929 	/* move the remaining VSI to uplink veb */
12930 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
12931 	if (veb->uplink_seid) {
12932 		vsi->uplink_seid = veb->uplink_seid;
12933 		if (veb->uplink_seid == pf->mac_seid)
12934 			vsi->veb_idx = I40E_NO_VEB;
12935 		else
12936 			vsi->veb_idx = veb->veb_idx;
12937 	} else {
12938 		/* floating VEB */
12939 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
12940 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
12941 	}
12942 
12943 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12944 	i40e_veb_clear(veb);
12945 }
12946 
12947 /**
12948  * i40e_add_veb - create the VEB in the switch
12949  * @veb: the VEB to be instantiated
12950  * @vsi: the controlling VSI
12951  **/
12952 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
12953 {
12954 	struct i40e_pf *pf = veb->pf;
12955 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
12956 	int ret;
12957 
12958 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
12959 			      veb->enabled_tc, false,
12960 			      &veb->seid, enable_stats, NULL);
12961 
12962 	/* get a VEB from the hardware */
12963 	if (ret) {
12964 		dev_info(&pf->pdev->dev,
12965 			 "couldn't add VEB, err %s aq_err %s\n",
12966 			 i40e_stat_str(&pf->hw, ret),
12967 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12968 		return -EPERM;
12969 	}
12970 
12971 	/* get statistics counter */
12972 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
12973 					 &veb->stats_idx, NULL, NULL, NULL);
12974 	if (ret) {
12975 		dev_info(&pf->pdev->dev,
12976 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
12977 			 i40e_stat_str(&pf->hw, ret),
12978 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12979 		return -EPERM;
12980 	}
12981 	ret = i40e_veb_get_bw_info(veb);
12982 	if (ret) {
12983 		dev_info(&pf->pdev->dev,
12984 			 "couldn't get VEB bw info, err %s aq_err %s\n",
12985 			 i40e_stat_str(&pf->hw, ret),
12986 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12987 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12988 		return -ENOENT;
12989 	}
12990 
12991 	vsi->uplink_seid = veb->seid;
12992 	vsi->veb_idx = veb->idx;
12993 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12994 
12995 	return 0;
12996 }
12997 
12998 /**
12999  * i40e_veb_setup - Set up a VEB
13000  * @pf: board private structure
13001  * @flags: VEB setup flags
13002  * @uplink_seid: the switch element to link to
13003  * @vsi_seid: the initial VSI seid
13004  * @enabled_tc: Enabled TC bit-map
13005  *
13006  * This allocates the sw VEB structure and links it into the switch
13007  * It is possible and legal for this to be a duplicate of an already
13008  * existing VEB.  It is also possible for both uplink and vsi seids
13009  * to be zero, in order to create a floating VEB.
13010  *
13011  * Returns pointer to the successfully allocated VEB sw struct on
13012  * success, otherwise returns NULL on failure.
13013  **/
13014 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13015 				u16 uplink_seid, u16 vsi_seid,
13016 				u8 enabled_tc)
13017 {
13018 	struct i40e_veb *veb, *uplink_veb = NULL;
13019 	int vsi_idx, veb_idx;
13020 	int ret;
13021 
13022 	/* if one seid is 0, the other must be 0 to create a floating relay */
13023 	if ((uplink_seid == 0 || vsi_seid == 0) &&
13024 	    (uplink_seid + vsi_seid != 0)) {
13025 		dev_info(&pf->pdev->dev,
13026 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13027 			 uplink_seid, vsi_seid);
13028 		return NULL;
13029 	}
13030 
13031 	/* make sure there is such a vsi and uplink */
13032 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13033 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13034 			break;
13035 	if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
13036 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13037 			 vsi_seid);
13038 		return NULL;
13039 	}
13040 
13041 	if (uplink_seid && uplink_seid != pf->mac_seid) {
13042 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
13043 			if (pf->veb[veb_idx] &&
13044 			    pf->veb[veb_idx]->seid == uplink_seid) {
13045 				uplink_veb = pf->veb[veb_idx];
13046 				break;
13047 			}
13048 		}
13049 		if (!uplink_veb) {
13050 			dev_info(&pf->pdev->dev,
13051 				 "uplink seid %d not found\n", uplink_seid);
13052 			return NULL;
13053 		}
13054 	}
13055 
13056 	/* get veb sw struct */
13057 	veb_idx = i40e_veb_mem_alloc(pf);
13058 	if (veb_idx < 0)
13059 		goto err_alloc;
13060 	veb = pf->veb[veb_idx];
13061 	veb->flags = flags;
13062 	veb->uplink_seid = uplink_seid;
13063 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
13064 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
13065 
13066 	/* create the VEB in the switch */
13067 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
13068 	if (ret)
13069 		goto err_veb;
13070 	if (vsi_idx == pf->lan_vsi)
13071 		pf->lan_veb = veb->idx;
13072 
13073 	return veb;
13074 
13075 err_veb:
13076 	i40e_veb_clear(veb);
13077 err_alloc:
13078 	return NULL;
13079 }
13080 
13081 /**
13082  * i40e_setup_pf_switch_element - set PF vars based on switch type
13083  * @pf: board private structure
13084  * @ele: element we are building info from
13085  * @num_reported: total number of elements
13086  * @printconfig: should we print the contents
13087  *
13088  * helper function to assist in extracting a few useful SEID values.
13089  **/
13090 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
13091 				struct i40e_aqc_switch_config_element_resp *ele,
13092 				u16 num_reported, bool printconfig)
13093 {
13094 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
13095 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
13096 	u8 element_type = ele->element_type;
13097 	u16 seid = le16_to_cpu(ele->seid);
13098 
13099 	if (printconfig)
13100 		dev_info(&pf->pdev->dev,
13101 			 "type=%d seid=%d uplink=%d downlink=%d\n",
13102 			 element_type, seid, uplink_seid, downlink_seid);
13103 
13104 	switch (element_type) {
13105 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
13106 		pf->mac_seid = seid;
13107 		break;
13108 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
13109 		/* Main VEB? */
13110 		if (uplink_seid != pf->mac_seid)
13111 			break;
13112 		if (pf->lan_veb == I40E_NO_VEB) {
13113 			int v;
13114 
13115 			/* find existing or else empty VEB */
13116 			for (v = 0; v < I40E_MAX_VEB; v++) {
13117 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
13118 					pf->lan_veb = v;
13119 					break;
13120 				}
13121 			}
13122 			if (pf->lan_veb == I40E_NO_VEB) {
13123 				v = i40e_veb_mem_alloc(pf);
13124 				if (v < 0)
13125 					break;
13126 				pf->lan_veb = v;
13127 			}
13128 		}
13129 
13130 		pf->veb[pf->lan_veb]->seid = seid;
13131 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
13132 		pf->veb[pf->lan_veb]->pf = pf;
13133 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
13134 		break;
13135 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
13136 		if (num_reported != 1)
13137 			break;
13138 		/* This is immediately after a reset so we can assume this is
13139 		 * the PF's VSI
13140 		 */
13141 		pf->mac_seid = uplink_seid;
13142 		pf->pf_seid = downlink_seid;
13143 		pf->main_vsi_seid = seid;
13144 		if (printconfig)
13145 			dev_info(&pf->pdev->dev,
13146 				 "pf_seid=%d main_vsi_seid=%d\n",
13147 				 pf->pf_seid, pf->main_vsi_seid);
13148 		break;
13149 	case I40E_SWITCH_ELEMENT_TYPE_PF:
13150 	case I40E_SWITCH_ELEMENT_TYPE_VF:
13151 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
13152 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
13153 	case I40E_SWITCH_ELEMENT_TYPE_PE:
13154 	case I40E_SWITCH_ELEMENT_TYPE_PA:
13155 		/* ignore these for now */
13156 		break;
13157 	default:
13158 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
13159 			 element_type, seid);
13160 		break;
13161 	}
13162 }
13163 
13164 /**
13165  * i40e_fetch_switch_configuration - Get switch config from firmware
13166  * @pf: board private structure
13167  * @printconfig: should we print the contents
13168  *
13169  * Get the current switch configuration from the device and
13170  * extract a few useful SEID values.
13171  **/
13172 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
13173 {
13174 	struct i40e_aqc_get_switch_config_resp *sw_config;
13175 	u16 next_seid = 0;
13176 	int ret = 0;
13177 	u8 *aq_buf;
13178 	int i;
13179 
13180 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13181 	if (!aq_buf)
13182 		return -ENOMEM;
13183 
13184 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13185 	do {
13186 		u16 num_reported, num_total;
13187 
13188 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13189 						I40E_AQ_LARGE_BUF,
13190 						&next_seid, NULL);
13191 		if (ret) {
13192 			dev_info(&pf->pdev->dev,
13193 				 "get switch config failed err %s aq_err %s\n",
13194 				 i40e_stat_str(&pf->hw, ret),
13195 				 i40e_aq_str(&pf->hw,
13196 					     pf->hw.aq.asq_last_status));
13197 			kfree(aq_buf);
13198 			return -ENOENT;
13199 		}
13200 
13201 		num_reported = le16_to_cpu(sw_config->header.num_reported);
13202 		num_total = le16_to_cpu(sw_config->header.num_total);
13203 
13204 		if (printconfig)
13205 			dev_info(&pf->pdev->dev,
13206 				 "header: %d reported %d total\n",
13207 				 num_reported, num_total);
13208 
13209 		for (i = 0; i < num_reported; i++) {
13210 			struct i40e_aqc_switch_config_element_resp *ele =
13211 				&sw_config->element[i];
13212 
13213 			i40e_setup_pf_switch_element(pf, ele, num_reported,
13214 						     printconfig);
13215 		}
13216 	} while (next_seid != 0);
13217 
13218 	kfree(aq_buf);
13219 	return ret;
13220 }
13221 
13222 /**
13223  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13224  * @pf: board private structure
13225  * @reinit: if the Main VSI needs to re-initialized.
13226  *
13227  * Returns 0 on success, negative value on failure
13228  **/
13229 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13230 {
13231 	u16 flags = 0;
13232 	int ret;
13233 
13234 	/* find out what's out there already */
13235 	ret = i40e_fetch_switch_configuration(pf, false);
13236 	if (ret) {
13237 		dev_info(&pf->pdev->dev,
13238 			 "couldn't fetch switch config, err %s aq_err %s\n",
13239 			 i40e_stat_str(&pf->hw, ret),
13240 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13241 		return ret;
13242 	}
13243 	i40e_pf_reset_stats(pf);
13244 
13245 	/* set the switch config bit for the whole device to
13246 	 * support limited promisc or true promisc
13247 	 * when user requests promisc. The default is limited
13248 	 * promisc.
13249 	*/
13250 
13251 	if ((pf->hw.pf_id == 0) &&
13252 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13253 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13254 		pf->last_sw_conf_flags = flags;
13255 	}
13256 
13257 	if (pf->hw.pf_id == 0) {
13258 		u16 valid_flags;
13259 
13260 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13261 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13262 						NULL);
13263 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13264 			dev_info(&pf->pdev->dev,
13265 				 "couldn't set switch config bits, err %s aq_err %s\n",
13266 				 i40e_stat_str(&pf->hw, ret),
13267 				 i40e_aq_str(&pf->hw,
13268 					     pf->hw.aq.asq_last_status));
13269 			/* not a fatal problem, just keep going */
13270 		}
13271 		pf->last_sw_conf_valid_flags = valid_flags;
13272 	}
13273 
13274 	/* first time setup */
13275 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13276 		struct i40e_vsi *vsi = NULL;
13277 		u16 uplink_seid;
13278 
13279 		/* Set up the PF VSI associated with the PF's main VSI
13280 		 * that is already in the HW switch
13281 		 */
13282 		if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13283 			uplink_seid = pf->veb[pf->lan_veb]->seid;
13284 		else
13285 			uplink_seid = pf->mac_seid;
13286 		if (pf->lan_vsi == I40E_NO_VSI)
13287 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13288 		else if (reinit)
13289 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13290 		if (!vsi) {
13291 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13292 			i40e_cloud_filter_exit(pf);
13293 			i40e_fdir_teardown(pf);
13294 			return -EAGAIN;
13295 		}
13296 	} else {
13297 		/* force a reset of TC and queue layout configurations */
13298 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13299 
13300 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13301 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13302 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13303 	}
13304 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13305 
13306 	i40e_fdir_sb_setup(pf);
13307 
13308 	/* Setup static PF queue filter control settings */
13309 	ret = i40e_setup_pf_filter_control(pf);
13310 	if (ret) {
13311 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13312 			 ret);
13313 		/* Failure here should not stop continuing other steps */
13314 	}
13315 
13316 	/* enable RSS in the HW, even for only one queue, as the stack can use
13317 	 * the hash
13318 	 */
13319 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13320 		i40e_pf_config_rss(pf);
13321 
13322 	/* fill in link information and enable LSE reporting */
13323 	i40e_link_event(pf);
13324 
13325 	/* Initialize user-specific link properties */
13326 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13327 				  I40E_AQ_AN_COMPLETED) ? true : false);
13328 
13329 	i40e_ptp_init(pf);
13330 
13331 	/* repopulate tunnel port filters */
13332 	i40e_sync_udp_filters(pf);
13333 
13334 	return ret;
13335 }
13336 
13337 /**
13338  * i40e_determine_queue_usage - Work out queue distribution
13339  * @pf: board private structure
13340  **/
13341 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13342 {
13343 	int queues_left;
13344 	int q_max;
13345 
13346 	pf->num_lan_qps = 0;
13347 
13348 	/* Find the max queues to be put into basic use.  We'll always be
13349 	 * using TC0, whether or not DCB is running, and TC0 will get the
13350 	 * big RSS set.
13351 	 */
13352 	queues_left = pf->hw.func_caps.num_tx_qp;
13353 
13354 	if ((queues_left == 1) ||
13355 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13356 		/* one qp for PF, no queues for anything else */
13357 		queues_left = 0;
13358 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13359 
13360 		/* make sure all the fancies are disabled */
13361 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13362 			       I40E_FLAG_IWARP_ENABLED	|
13363 			       I40E_FLAG_FD_SB_ENABLED	|
13364 			       I40E_FLAG_FD_ATR_ENABLED	|
13365 			       I40E_FLAG_DCB_CAPABLE	|
13366 			       I40E_FLAG_DCB_ENABLED	|
13367 			       I40E_FLAG_SRIOV_ENABLED	|
13368 			       I40E_FLAG_VMDQ_ENABLED);
13369 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13370 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13371 				  I40E_FLAG_FD_SB_ENABLED |
13372 				  I40E_FLAG_FD_ATR_ENABLED |
13373 				  I40E_FLAG_DCB_CAPABLE))) {
13374 		/* one qp for PF */
13375 		pf->alloc_rss_size = pf->num_lan_qps = 1;
13376 		queues_left -= pf->num_lan_qps;
13377 
13378 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
13379 			       I40E_FLAG_IWARP_ENABLED	|
13380 			       I40E_FLAG_FD_SB_ENABLED	|
13381 			       I40E_FLAG_FD_ATR_ENABLED	|
13382 			       I40E_FLAG_DCB_ENABLED	|
13383 			       I40E_FLAG_VMDQ_ENABLED);
13384 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13385 	} else {
13386 		/* Not enough queues for all TCs */
13387 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13388 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13389 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13390 					I40E_FLAG_DCB_ENABLED);
13391 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13392 		}
13393 
13394 		/* limit lan qps to the smaller of qps, cpus or msix */
13395 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13396 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13397 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13398 		pf->num_lan_qps = q_max;
13399 
13400 		queues_left -= pf->num_lan_qps;
13401 	}
13402 
13403 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13404 		if (queues_left > 1) {
13405 			queues_left -= 1; /* save 1 queue for FD */
13406 		} else {
13407 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13408 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13409 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13410 		}
13411 	}
13412 
13413 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13414 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13415 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13416 					(queues_left / pf->num_vf_qps));
13417 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13418 	}
13419 
13420 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13421 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13422 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13423 					  (queues_left / pf->num_vmdq_qps));
13424 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13425 	}
13426 
13427 	pf->queues_left = queues_left;
13428 	dev_dbg(&pf->pdev->dev,
13429 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13430 		pf->hw.func_caps.num_tx_qp,
13431 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13432 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13433 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13434 		queues_left);
13435 }
13436 
13437 /**
13438  * i40e_setup_pf_filter_control - Setup PF static filter control
13439  * @pf: PF to be setup
13440  *
13441  * i40e_setup_pf_filter_control sets up a PF's initial filter control
13442  * settings. If PE/FCoE are enabled then it will also set the per PF
13443  * based filter sizes required for them. It also enables Flow director,
13444  * ethertype and macvlan type filter settings for the pf.
13445  *
13446  * Returns 0 on success, negative on failure
13447  **/
13448 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13449 {
13450 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
13451 
13452 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13453 
13454 	/* Flow Director is enabled */
13455 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13456 		settings->enable_fdir = true;
13457 
13458 	/* Ethtype and MACVLAN filters enabled for PF */
13459 	settings->enable_ethtype = true;
13460 	settings->enable_macvlan = true;
13461 
13462 	if (i40e_set_filter_control(&pf->hw, settings))
13463 		return -ENOENT;
13464 
13465 	return 0;
13466 }
13467 
13468 #define INFO_STRING_LEN 255
13469 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13470 static void i40e_print_features(struct i40e_pf *pf)
13471 {
13472 	struct i40e_hw *hw = &pf->hw;
13473 	char *buf;
13474 	int i;
13475 
13476 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13477 	if (!buf)
13478 		return;
13479 
13480 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13481 #ifdef CONFIG_PCI_IOV
13482 	i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13483 #endif
13484 	i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13485 		      pf->hw.func_caps.num_vsis,
13486 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
13487 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
13488 		i += snprintf(&buf[i], REMAIN(i), " RSS");
13489 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13490 		i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13491 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13492 		i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13493 		i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13494 	}
13495 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13496 		i += snprintf(&buf[i], REMAIN(i), " DCB");
13497 	i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13498 	i += snprintf(&buf[i], REMAIN(i), " Geneve");
13499 	if (pf->flags & I40E_FLAG_PTP)
13500 		i += snprintf(&buf[i], REMAIN(i), " PTP");
13501 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13502 		i += snprintf(&buf[i], REMAIN(i), " VEB");
13503 	else
13504 		i += snprintf(&buf[i], REMAIN(i), " VEPA");
13505 
13506 	dev_info(&pf->pdev->dev, "%s\n", buf);
13507 	kfree(buf);
13508 	WARN_ON(i > INFO_STRING_LEN);
13509 }
13510 
13511 /**
13512  * i40e_get_platform_mac_addr - get platform-specific MAC address
13513  * @pdev: PCI device information struct
13514  * @pf: board private structure
13515  *
13516  * Look up the MAC address for the device. First we'll try
13517  * eth_platform_get_mac_address, which will check Open Firmware, or arch
13518  * specific fallback. Otherwise, we'll default to the stored value in
13519  * firmware.
13520  **/
13521 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13522 {
13523 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13524 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13525 }
13526 
13527 /**
13528  * i40e_probe - Device initialization routine
13529  * @pdev: PCI device information struct
13530  * @ent: entry in i40e_pci_tbl
13531  *
13532  * i40e_probe initializes a PF identified by a pci_dev structure.
13533  * The OS initialization, configuring of the PF private structure,
13534  * and a hardware reset occur.
13535  *
13536  * Returns 0 on success, negative on failure
13537  **/
13538 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13539 {
13540 	struct i40e_aq_get_phy_abilities_resp abilities;
13541 	struct i40e_pf *pf;
13542 	struct i40e_hw *hw;
13543 	static u16 pfs_found;
13544 	u16 wol_nvm_bits;
13545 	u16 link_status;
13546 	int err;
13547 	u32 val;
13548 	u32 i;
13549 	u8 set_fc_aq_fail;
13550 
13551 	err = pci_enable_device_mem(pdev);
13552 	if (err)
13553 		return err;
13554 
13555 	/* set up for high or low dma */
13556 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13557 	if (err) {
13558 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13559 		if (err) {
13560 			dev_err(&pdev->dev,
13561 				"DMA configuration failed: 0x%x\n", err);
13562 			goto err_dma;
13563 		}
13564 	}
13565 
13566 	/* set up pci connections */
13567 	err = pci_request_mem_regions(pdev, i40e_driver_name);
13568 	if (err) {
13569 		dev_info(&pdev->dev,
13570 			 "pci_request_selected_regions failed %d\n", err);
13571 		goto err_pci_reg;
13572 	}
13573 
13574 	pci_enable_pcie_error_reporting(pdev);
13575 	pci_set_master(pdev);
13576 
13577 	/* Now that we have a PCI connection, we need to do the
13578 	 * low level device setup.  This is primarily setting up
13579 	 * the Admin Queue structures and then querying for the
13580 	 * device's current profile information.
13581 	 */
13582 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13583 	if (!pf) {
13584 		err = -ENOMEM;
13585 		goto err_pf_alloc;
13586 	}
13587 	pf->next_vsi = 0;
13588 	pf->pdev = pdev;
13589 	set_bit(__I40E_DOWN, pf->state);
13590 
13591 	hw = &pf->hw;
13592 	hw->back = pf;
13593 
13594 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13595 				I40E_MAX_CSR_SPACE);
13596 
13597 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
13598 	if (!hw->hw_addr) {
13599 		err = -EIO;
13600 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13601 			 (unsigned int)pci_resource_start(pdev, 0),
13602 			 pf->ioremap_len, err);
13603 		goto err_ioremap;
13604 	}
13605 	hw->vendor_id = pdev->vendor;
13606 	hw->device_id = pdev->device;
13607 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13608 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
13609 	hw->subsystem_device_id = pdev->subsystem_device;
13610 	hw->bus.device = PCI_SLOT(pdev->devfn);
13611 	hw->bus.func = PCI_FUNC(pdev->devfn);
13612 	hw->bus.bus_id = pdev->bus->number;
13613 	pf->instance = pfs_found;
13614 
13615 	/* Select something other than the 802.1ad ethertype for the
13616 	 * switch to use internally and drop on ingress.
13617 	 */
13618 	hw->switch_tag = 0xffff;
13619 	hw->first_tag = ETH_P_8021AD;
13620 	hw->second_tag = ETH_P_8021Q;
13621 
13622 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13623 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13624 
13625 	/* set up the locks for the AQ, do this only once in probe
13626 	 * and destroy them only once in remove
13627 	 */
13628 	mutex_init(&hw->aq.asq_mutex);
13629 	mutex_init(&hw->aq.arq_mutex);
13630 
13631 	pf->msg_enable = netif_msg_init(debug,
13632 					NETIF_MSG_DRV |
13633 					NETIF_MSG_PROBE |
13634 					NETIF_MSG_LINK);
13635 	if (debug < -1)
13636 		pf->hw.debug_mask = debug;
13637 
13638 	/* do a special CORER for clearing PXE mode once at init */
13639 	if (hw->revision_id == 0 &&
13640 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13641 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13642 		i40e_flush(hw);
13643 		msleep(200);
13644 		pf->corer_count++;
13645 
13646 		i40e_clear_pxe_mode(hw);
13647 	}
13648 
13649 	/* Reset here to make sure all is clean and to define PF 'n' */
13650 	i40e_clear_hw(hw);
13651 	err = i40e_pf_reset(hw);
13652 	if (err) {
13653 		dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13654 		goto err_pf_reset;
13655 	}
13656 	pf->pfr_count++;
13657 
13658 	hw->aq.num_arq_entries = I40E_AQ_LEN;
13659 	hw->aq.num_asq_entries = I40E_AQ_LEN;
13660 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13661 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13662 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
13663 
13664 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
13665 		 "%s-%s:misc",
13666 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
13667 
13668 	err = i40e_init_shared_code(hw);
13669 	if (err) {
13670 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
13671 			 err);
13672 		goto err_pf_reset;
13673 	}
13674 
13675 	/* set up a default setting for link flow control */
13676 	pf->hw.fc.requested_mode = I40E_FC_NONE;
13677 
13678 	err = i40e_init_adminq(hw);
13679 	if (err) {
13680 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
13681 			dev_info(&pdev->dev,
13682 				 "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");
13683 		else
13684 			dev_info(&pdev->dev,
13685 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
13686 
13687 		goto err_pf_reset;
13688 	}
13689 	i40e_get_oem_version(hw);
13690 
13691 	/* provide nvm, fw, api versions */
13692 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
13693 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
13694 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
13695 		 i40e_nvm_version_str(hw));
13696 
13697 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
13698 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
13699 		dev_info(&pdev->dev,
13700 			 "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");
13701 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
13702 		dev_info(&pdev->dev,
13703 			 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
13704 
13705 	i40e_verify_eeprom(pf);
13706 
13707 	/* Rev 0 hardware was never productized */
13708 	if (hw->revision_id < 1)
13709 		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");
13710 
13711 	i40e_clear_pxe_mode(hw);
13712 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
13713 	if (err)
13714 		goto err_adminq_setup;
13715 
13716 	err = i40e_sw_init(pf);
13717 	if (err) {
13718 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
13719 		goto err_sw_init;
13720 	}
13721 
13722 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
13723 				hw->func_caps.num_rx_qp, 0, 0);
13724 	if (err) {
13725 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
13726 		goto err_init_lan_hmc;
13727 	}
13728 
13729 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
13730 	if (err) {
13731 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
13732 		err = -ENOENT;
13733 		goto err_configure_lan_hmc;
13734 	}
13735 
13736 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
13737 	 * Ignore error return codes because if it was already disabled via
13738 	 * hardware settings this will fail
13739 	 */
13740 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
13741 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
13742 		i40e_aq_stop_lldp(hw, true, NULL);
13743 	}
13744 
13745 	/* allow a platform config to override the HW addr */
13746 	i40e_get_platform_mac_addr(pdev, pf);
13747 
13748 	if (!is_valid_ether_addr(hw->mac.addr)) {
13749 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
13750 		err = -EIO;
13751 		goto err_mac_addr;
13752 	}
13753 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
13754 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
13755 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
13756 	if (is_valid_ether_addr(hw->mac.port_addr))
13757 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
13758 
13759 	pci_set_drvdata(pdev, pf);
13760 	pci_save_state(pdev);
13761 
13762 	/* Enable FW to write default DCB config on link-up */
13763 	i40e_aq_set_dcb_parameters(hw, true, NULL);
13764 
13765 #ifdef CONFIG_I40E_DCB
13766 	err = i40e_init_pf_dcb(pf);
13767 	if (err) {
13768 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
13769 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
13770 		/* Continue without DCB enabled */
13771 	}
13772 #endif /* CONFIG_I40E_DCB */
13773 
13774 	/* set up periodic task facility */
13775 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
13776 	pf->service_timer_period = HZ;
13777 
13778 	INIT_WORK(&pf->service_task, i40e_service_task);
13779 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
13780 
13781 	/* NVM bit on means WoL disabled for the port */
13782 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
13783 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
13784 		pf->wol_en = false;
13785 	else
13786 		pf->wol_en = true;
13787 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
13788 
13789 	/* set up the main switch operations */
13790 	i40e_determine_queue_usage(pf);
13791 	err = i40e_init_interrupt_scheme(pf);
13792 	if (err)
13793 		goto err_switch_setup;
13794 
13795 	/* The number of VSIs reported by the FW is the minimum guaranteed
13796 	 * to us; HW supports far more and we share the remaining pool with
13797 	 * the other PFs. We allocate space for more than the guarantee with
13798 	 * the understanding that we might not get them all later.
13799 	 */
13800 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
13801 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
13802 	else
13803 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
13804 
13805 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
13806 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
13807 			  GFP_KERNEL);
13808 	if (!pf->vsi) {
13809 		err = -ENOMEM;
13810 		goto err_switch_setup;
13811 	}
13812 
13813 #ifdef CONFIG_PCI_IOV
13814 	/* prep for VF support */
13815 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13816 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13817 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13818 		if (pci_num_vf(pdev))
13819 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13820 	}
13821 #endif
13822 	err = i40e_setup_pf_switch(pf, false);
13823 	if (err) {
13824 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
13825 		goto err_vsis;
13826 	}
13827 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
13828 
13829 	/* Make sure flow control is set according to current settings */
13830 	err = i40e_set_fc(hw, &set_fc_aq_fail, true);
13831 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
13832 		dev_dbg(&pf->pdev->dev,
13833 			"Set fc with err %s aq_err %s on get_phy_cap\n",
13834 			i40e_stat_str(hw, err),
13835 			i40e_aq_str(hw, hw->aq.asq_last_status));
13836 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
13837 		dev_dbg(&pf->pdev->dev,
13838 			"Set fc with err %s aq_err %s on set_phy_config\n",
13839 			i40e_stat_str(hw, err),
13840 			i40e_aq_str(hw, hw->aq.asq_last_status));
13841 	if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
13842 		dev_dbg(&pf->pdev->dev,
13843 			"Set fc with err %s aq_err %s on get_link_info\n",
13844 			i40e_stat_str(hw, err),
13845 			i40e_aq_str(hw, hw->aq.asq_last_status));
13846 
13847 	/* if FDIR VSI was set up, start it now */
13848 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13849 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
13850 			i40e_vsi_open(pf->vsi[i]);
13851 			break;
13852 		}
13853 	}
13854 
13855 	/* The driver only wants link up/down and module qualification
13856 	 * reports from firmware.  Note the negative logic.
13857 	 */
13858 	err = i40e_aq_set_phy_int_mask(&pf->hw,
13859 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
13860 					 I40E_AQ_EVENT_MEDIA_NA |
13861 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
13862 	if (err)
13863 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
13864 			 i40e_stat_str(&pf->hw, err),
13865 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13866 
13867 	/* Reconfigure hardware for allowing smaller MSS in the case
13868 	 * of TSO, so that we avoid the MDD being fired and causing
13869 	 * a reset in the case of small MSS+TSO.
13870 	 */
13871 	val = rd32(hw, I40E_REG_MSS);
13872 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
13873 		val &= ~I40E_REG_MSS_MIN_MASK;
13874 		val |= I40E_64BYTE_MSS;
13875 		wr32(hw, I40E_REG_MSS, val);
13876 	}
13877 
13878 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
13879 		msleep(75);
13880 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
13881 		if (err)
13882 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
13883 				 i40e_stat_str(&pf->hw, err),
13884 				 i40e_aq_str(&pf->hw,
13885 					     pf->hw.aq.asq_last_status));
13886 	}
13887 	/* The main driver is (mostly) up and happy. We need to set this state
13888 	 * before setting up the misc vector or we get a race and the vector
13889 	 * ends up disabled forever.
13890 	 */
13891 	clear_bit(__I40E_DOWN, pf->state);
13892 
13893 	/* In case of MSIX we are going to setup the misc vector right here
13894 	 * to handle admin queue events etc. In case of legacy and MSI
13895 	 * the misc functionality and queue processing is combined in
13896 	 * the same vector and that gets setup at open.
13897 	 */
13898 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13899 		err = i40e_setup_misc_vector(pf);
13900 		if (err) {
13901 			dev_info(&pdev->dev,
13902 				 "setup of misc vector failed: %d\n", err);
13903 			goto err_vsis;
13904 		}
13905 	}
13906 
13907 #ifdef CONFIG_PCI_IOV
13908 	/* prep for VF support */
13909 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13910 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13911 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13912 		/* disable link interrupts for VFs */
13913 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
13914 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
13915 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
13916 		i40e_flush(hw);
13917 
13918 		if (pci_num_vf(pdev)) {
13919 			dev_info(&pdev->dev,
13920 				 "Active VFs found, allocating resources.\n");
13921 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
13922 			if (err)
13923 				dev_info(&pdev->dev,
13924 					 "Error %d allocating resources for existing VFs\n",
13925 					 err);
13926 		}
13927 	}
13928 #endif /* CONFIG_PCI_IOV */
13929 
13930 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13931 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
13932 						      pf->num_iwarp_msix,
13933 						      I40E_IWARP_IRQ_PILE_ID);
13934 		if (pf->iwarp_base_vector < 0) {
13935 			dev_info(&pdev->dev,
13936 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
13937 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
13938 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
13939 		}
13940 	}
13941 
13942 	i40e_dbg_pf_init(pf);
13943 
13944 	/* tell the firmware that we're starting */
13945 	i40e_send_version(pf);
13946 
13947 	/* since everything's happy, start the service_task timer */
13948 	mod_timer(&pf->service_timer,
13949 		  round_jiffies(jiffies + pf->service_timer_period));
13950 
13951 	/* add this PF to client device list and launch a client service task */
13952 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13953 		err = i40e_lan_add_device(pf);
13954 		if (err)
13955 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
13956 				 err);
13957 	}
13958 
13959 #define PCI_SPEED_SIZE 8
13960 #define PCI_WIDTH_SIZE 8
13961 	/* Devices on the IOSF bus do not have this information
13962 	 * and will report PCI Gen 1 x 1 by default so don't bother
13963 	 * checking them.
13964 	 */
13965 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
13966 		char speed[PCI_SPEED_SIZE] = "Unknown";
13967 		char width[PCI_WIDTH_SIZE] = "Unknown";
13968 
13969 		/* Get the negotiated link width and speed from PCI config
13970 		 * space
13971 		 */
13972 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
13973 					  &link_status);
13974 
13975 		i40e_set_pci_config_data(hw, link_status);
13976 
13977 		switch (hw->bus.speed) {
13978 		case i40e_bus_speed_8000:
13979 			strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
13980 		case i40e_bus_speed_5000:
13981 			strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
13982 		case i40e_bus_speed_2500:
13983 			strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
13984 		default:
13985 			break;
13986 		}
13987 		switch (hw->bus.width) {
13988 		case i40e_bus_width_pcie_x8:
13989 			strncpy(width, "8", PCI_WIDTH_SIZE); break;
13990 		case i40e_bus_width_pcie_x4:
13991 			strncpy(width, "4", PCI_WIDTH_SIZE); break;
13992 		case i40e_bus_width_pcie_x2:
13993 			strncpy(width, "2", PCI_WIDTH_SIZE); break;
13994 		case i40e_bus_width_pcie_x1:
13995 			strncpy(width, "1", PCI_WIDTH_SIZE); break;
13996 		default:
13997 			break;
13998 		}
13999 
14000 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
14001 			 speed, width);
14002 
14003 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
14004 		    hw->bus.speed < i40e_bus_speed_8000) {
14005 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
14006 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
14007 		}
14008 	}
14009 
14010 	/* get the requested speeds from the fw */
14011 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
14012 	if (err)
14013 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
14014 			i40e_stat_str(&pf->hw, err),
14015 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14016 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
14017 
14018 	/* get the supported phy types from the fw */
14019 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
14020 	if (err)
14021 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
14022 			i40e_stat_str(&pf->hw, err),
14023 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14024 
14025 	/* Add a filter to drop all Flow control frames from any VSI from being
14026 	 * transmitted. By doing so we stop a malicious VF from sending out
14027 	 * PAUSE or PFC frames and potentially controlling traffic for other
14028 	 * PF/VF VSIs.
14029 	 * The FW can still send Flow control frames if enabled.
14030 	 */
14031 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
14032 						       pf->main_vsi_seid);
14033 
14034 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
14035 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
14036 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
14037 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
14038 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
14039 	/* print a string summarizing features */
14040 	i40e_print_features(pf);
14041 
14042 	return 0;
14043 
14044 	/* Unwind what we've done if something failed in the setup */
14045 err_vsis:
14046 	set_bit(__I40E_DOWN, pf->state);
14047 	i40e_clear_interrupt_scheme(pf);
14048 	kfree(pf->vsi);
14049 err_switch_setup:
14050 	i40e_reset_interrupt_capability(pf);
14051 	del_timer_sync(&pf->service_timer);
14052 err_mac_addr:
14053 err_configure_lan_hmc:
14054 	(void)i40e_shutdown_lan_hmc(hw);
14055 err_init_lan_hmc:
14056 	kfree(pf->qp_pile);
14057 err_sw_init:
14058 err_adminq_setup:
14059 err_pf_reset:
14060 	iounmap(hw->hw_addr);
14061 err_ioremap:
14062 	kfree(pf);
14063 err_pf_alloc:
14064 	pci_disable_pcie_error_reporting(pdev);
14065 	pci_release_mem_regions(pdev);
14066 err_pci_reg:
14067 err_dma:
14068 	pci_disable_device(pdev);
14069 	return err;
14070 }
14071 
14072 /**
14073  * i40e_remove - Device removal routine
14074  * @pdev: PCI device information struct
14075  *
14076  * i40e_remove is called by the PCI subsystem to alert the driver
14077  * that is should release a PCI device.  This could be caused by a
14078  * Hot-Plug event, or because the driver is going to be removed from
14079  * memory.
14080  **/
14081 static void i40e_remove(struct pci_dev *pdev)
14082 {
14083 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14084 	struct i40e_hw *hw = &pf->hw;
14085 	i40e_status ret_code;
14086 	int i;
14087 
14088 	i40e_dbg_pf_exit(pf);
14089 
14090 	i40e_ptp_stop(pf);
14091 
14092 	/* Disable RSS in hw */
14093 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
14094 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
14095 
14096 	/* no more scheduling of any task */
14097 	set_bit(__I40E_SUSPENDED, pf->state);
14098 	set_bit(__I40E_DOWN, pf->state);
14099 	if (pf->service_timer.function)
14100 		del_timer_sync(&pf->service_timer);
14101 	if (pf->service_task.func)
14102 		cancel_work_sync(&pf->service_task);
14103 
14104 	/* Client close must be called explicitly here because the timer
14105 	 * has been stopped.
14106 	 */
14107 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14108 
14109 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
14110 		i40e_free_vfs(pf);
14111 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
14112 	}
14113 
14114 	i40e_fdir_teardown(pf);
14115 
14116 	/* If there is a switch structure or any orphans, remove them.
14117 	 * This will leave only the PF's VSI remaining.
14118 	 */
14119 	for (i = 0; i < I40E_MAX_VEB; i++) {
14120 		if (!pf->veb[i])
14121 			continue;
14122 
14123 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
14124 		    pf->veb[i]->uplink_seid == 0)
14125 			i40e_switch_branch_release(pf->veb[i]);
14126 	}
14127 
14128 	/* Now we can shutdown the PF's VSI, just before we kill
14129 	 * adminq and hmc.
14130 	 */
14131 	if (pf->vsi[pf->lan_vsi])
14132 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
14133 
14134 	i40e_cloud_filter_exit(pf);
14135 
14136 	/* remove attached clients */
14137 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14138 		ret_code = i40e_lan_del_device(pf);
14139 		if (ret_code)
14140 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
14141 				 ret_code);
14142 	}
14143 
14144 	/* shutdown and destroy the HMC */
14145 	if (hw->hmc.hmc_obj) {
14146 		ret_code = i40e_shutdown_lan_hmc(hw);
14147 		if (ret_code)
14148 			dev_warn(&pdev->dev,
14149 				 "Failed to destroy the HMC resources: %d\n",
14150 				 ret_code);
14151 	}
14152 
14153 	/* shutdown the adminq */
14154 	i40e_shutdown_adminq(hw);
14155 
14156 	/* destroy the locks only once, here */
14157 	mutex_destroy(&hw->aq.arq_mutex);
14158 	mutex_destroy(&hw->aq.asq_mutex);
14159 
14160 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
14161 	i40e_clear_interrupt_scheme(pf);
14162 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14163 		if (pf->vsi[i]) {
14164 			i40e_vsi_clear_rings(pf->vsi[i]);
14165 			i40e_vsi_clear(pf->vsi[i]);
14166 			pf->vsi[i] = NULL;
14167 		}
14168 	}
14169 
14170 	for (i = 0; i < I40E_MAX_VEB; i++) {
14171 		kfree(pf->veb[i]);
14172 		pf->veb[i] = NULL;
14173 	}
14174 
14175 	kfree(pf->qp_pile);
14176 	kfree(pf->vsi);
14177 
14178 	iounmap(hw->hw_addr);
14179 	kfree(pf);
14180 	pci_release_mem_regions(pdev);
14181 
14182 	pci_disable_pcie_error_reporting(pdev);
14183 	pci_disable_device(pdev);
14184 }
14185 
14186 /**
14187  * i40e_pci_error_detected - warning that something funky happened in PCI land
14188  * @pdev: PCI device information struct
14189  * @error: the type of PCI error
14190  *
14191  * Called to warn that something happened and the error handling steps
14192  * are in progress.  Allows the driver to quiesce things, be ready for
14193  * remediation.
14194  **/
14195 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14196 						enum pci_channel_state error)
14197 {
14198 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14199 
14200 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14201 
14202 	if (!pf) {
14203 		dev_info(&pdev->dev,
14204 			 "Cannot recover - error happened during device probe\n");
14205 		return PCI_ERS_RESULT_DISCONNECT;
14206 	}
14207 
14208 	/* shutdown all operations */
14209 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14210 		i40e_prep_for_reset(pf, false);
14211 
14212 	/* Request a slot reset */
14213 	return PCI_ERS_RESULT_NEED_RESET;
14214 }
14215 
14216 /**
14217  * i40e_pci_error_slot_reset - a PCI slot reset just happened
14218  * @pdev: PCI device information struct
14219  *
14220  * Called to find if the driver can work with the device now that
14221  * the pci slot has been reset.  If a basic connection seems good
14222  * (registers are readable and have sane content) then return a
14223  * happy little PCI_ERS_RESULT_xxx.
14224  **/
14225 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14226 {
14227 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14228 	pci_ers_result_t result;
14229 	int err;
14230 	u32 reg;
14231 
14232 	dev_dbg(&pdev->dev, "%s\n", __func__);
14233 	if (pci_enable_device_mem(pdev)) {
14234 		dev_info(&pdev->dev,
14235 			 "Cannot re-enable PCI device after reset.\n");
14236 		result = PCI_ERS_RESULT_DISCONNECT;
14237 	} else {
14238 		pci_set_master(pdev);
14239 		pci_restore_state(pdev);
14240 		pci_save_state(pdev);
14241 		pci_wake_from_d3(pdev, false);
14242 
14243 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14244 		if (reg == 0)
14245 			result = PCI_ERS_RESULT_RECOVERED;
14246 		else
14247 			result = PCI_ERS_RESULT_DISCONNECT;
14248 	}
14249 
14250 	err = pci_cleanup_aer_uncorrect_error_status(pdev);
14251 	if (err) {
14252 		dev_info(&pdev->dev,
14253 			 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
14254 			 err);
14255 		/* non-fatal, continue */
14256 	}
14257 
14258 	return result;
14259 }
14260 
14261 /**
14262  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14263  * @pdev: PCI device information struct
14264  */
14265 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14266 {
14267 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14268 
14269 	i40e_prep_for_reset(pf, false);
14270 }
14271 
14272 /**
14273  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14274  * @pdev: PCI device information struct
14275  */
14276 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14277 {
14278 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14279 
14280 	i40e_reset_and_rebuild(pf, false, false);
14281 }
14282 
14283 /**
14284  * i40e_pci_error_resume - restart operations after PCI error recovery
14285  * @pdev: PCI device information struct
14286  *
14287  * Called to allow the driver to bring things back up after PCI error
14288  * and/or reset recovery has finished.
14289  **/
14290 static void i40e_pci_error_resume(struct pci_dev *pdev)
14291 {
14292 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14293 
14294 	dev_dbg(&pdev->dev, "%s\n", __func__);
14295 	if (test_bit(__I40E_SUSPENDED, pf->state))
14296 		return;
14297 
14298 	i40e_handle_reset_warning(pf, false);
14299 }
14300 
14301 /**
14302  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14303  * using the mac_address_write admin q function
14304  * @pf: pointer to i40e_pf struct
14305  **/
14306 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14307 {
14308 	struct i40e_hw *hw = &pf->hw;
14309 	i40e_status ret;
14310 	u8 mac_addr[6];
14311 	u16 flags = 0;
14312 
14313 	/* Get current MAC address in case it's an LAA */
14314 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14315 		ether_addr_copy(mac_addr,
14316 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14317 	} else {
14318 		dev_err(&pf->pdev->dev,
14319 			"Failed to retrieve MAC address; using default\n");
14320 		ether_addr_copy(mac_addr, hw->mac.addr);
14321 	}
14322 
14323 	/* The FW expects the mac address write cmd to first be called with
14324 	 * one of these flags before calling it again with the multicast
14325 	 * enable flags.
14326 	 */
14327 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14328 
14329 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14330 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14331 
14332 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14333 	if (ret) {
14334 		dev_err(&pf->pdev->dev,
14335 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14336 		return;
14337 	}
14338 
14339 	flags = I40E_AQC_MC_MAG_EN
14340 			| I40E_AQC_WOL_PRESERVE_ON_PFR
14341 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14342 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14343 	if (ret)
14344 		dev_err(&pf->pdev->dev,
14345 			"Failed to enable Multicast Magic Packet wake up\n");
14346 }
14347 
14348 /**
14349  * i40e_shutdown - PCI callback for shutting down
14350  * @pdev: PCI device information struct
14351  **/
14352 static void i40e_shutdown(struct pci_dev *pdev)
14353 {
14354 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14355 	struct i40e_hw *hw = &pf->hw;
14356 
14357 	set_bit(__I40E_SUSPENDED, pf->state);
14358 	set_bit(__I40E_DOWN, pf->state);
14359 
14360 	del_timer_sync(&pf->service_timer);
14361 	cancel_work_sync(&pf->service_task);
14362 	i40e_cloud_filter_exit(pf);
14363 	i40e_fdir_teardown(pf);
14364 
14365 	/* Client close must be called explicitly here because the timer
14366 	 * has been stopped.
14367 	 */
14368 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14369 
14370 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14371 		i40e_enable_mc_magic_wake(pf);
14372 
14373 	i40e_prep_for_reset(pf, false);
14374 
14375 	wr32(hw, I40E_PFPM_APM,
14376 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14377 	wr32(hw, I40E_PFPM_WUFC,
14378 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14379 
14380 	i40e_clear_interrupt_scheme(pf);
14381 
14382 	if (system_state == SYSTEM_POWER_OFF) {
14383 		pci_wake_from_d3(pdev, pf->wol_en);
14384 		pci_set_power_state(pdev, PCI_D3hot);
14385 	}
14386 }
14387 
14388 /**
14389  * i40e_suspend - PM callback for moving to D3
14390  * @dev: generic device information structure
14391  **/
14392 static int __maybe_unused i40e_suspend(struct device *dev)
14393 {
14394 	struct pci_dev *pdev = to_pci_dev(dev);
14395 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14396 	struct i40e_hw *hw = &pf->hw;
14397 
14398 	/* If we're already suspended, then there is nothing to do */
14399 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14400 		return 0;
14401 
14402 	set_bit(__I40E_DOWN, pf->state);
14403 
14404 	/* Ensure service task will not be running */
14405 	del_timer_sync(&pf->service_timer);
14406 	cancel_work_sync(&pf->service_task);
14407 
14408 	/* Client close must be called explicitly here because the timer
14409 	 * has been stopped.
14410 	 */
14411 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14412 
14413 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14414 		i40e_enable_mc_magic_wake(pf);
14415 
14416 	/* Since we're going to destroy queues during the
14417 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14418 	 * whole section
14419 	 */
14420 	rtnl_lock();
14421 
14422 	i40e_prep_for_reset(pf, true);
14423 
14424 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14425 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14426 
14427 	/* Clear the interrupt scheme and release our IRQs so that the system
14428 	 * can safely hibernate even when there are a large number of CPUs.
14429 	 * Otherwise hibernation might fail when mapping all the vectors back
14430 	 * to CPU0.
14431 	 */
14432 	i40e_clear_interrupt_scheme(pf);
14433 
14434 	rtnl_unlock();
14435 
14436 	return 0;
14437 }
14438 
14439 /**
14440  * i40e_resume - PM callback for waking up from D3
14441  * @dev: generic device information structure
14442  **/
14443 static int __maybe_unused i40e_resume(struct device *dev)
14444 {
14445 	struct pci_dev *pdev = to_pci_dev(dev);
14446 	struct i40e_pf *pf = pci_get_drvdata(pdev);
14447 	int err;
14448 
14449 	/* If we're not suspended, then there is nothing to do */
14450 	if (!test_bit(__I40E_SUSPENDED, pf->state))
14451 		return 0;
14452 
14453 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
14454 	 * since we're going to be restoring queues
14455 	 */
14456 	rtnl_lock();
14457 
14458 	/* We cleared the interrupt scheme when we suspended, so we need to
14459 	 * restore it now to resume device functionality.
14460 	 */
14461 	err = i40e_restore_interrupt_scheme(pf);
14462 	if (err) {
14463 		dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14464 			err);
14465 	}
14466 
14467 	clear_bit(__I40E_DOWN, pf->state);
14468 	i40e_reset_and_rebuild(pf, false, true);
14469 
14470 	rtnl_unlock();
14471 
14472 	/* Clear suspended state last after everything is recovered */
14473 	clear_bit(__I40E_SUSPENDED, pf->state);
14474 
14475 	/* Restart the service task */
14476 	mod_timer(&pf->service_timer,
14477 		  round_jiffies(jiffies + pf->service_timer_period));
14478 
14479 	return 0;
14480 }
14481 
14482 static const struct pci_error_handlers i40e_err_handler = {
14483 	.error_detected = i40e_pci_error_detected,
14484 	.slot_reset = i40e_pci_error_slot_reset,
14485 	.reset_prepare = i40e_pci_error_reset_prepare,
14486 	.reset_done = i40e_pci_error_reset_done,
14487 	.resume = i40e_pci_error_resume,
14488 };
14489 
14490 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14491 
14492 static struct pci_driver i40e_driver = {
14493 	.name     = i40e_driver_name,
14494 	.id_table = i40e_pci_tbl,
14495 	.probe    = i40e_probe,
14496 	.remove   = i40e_remove,
14497 	.driver   = {
14498 		.pm = &i40e_pm_ops,
14499 	},
14500 	.shutdown = i40e_shutdown,
14501 	.err_handler = &i40e_err_handler,
14502 	.sriov_configure = i40e_pci_sriov_configure,
14503 };
14504 
14505 /**
14506  * i40e_init_module - Driver registration routine
14507  *
14508  * i40e_init_module is the first routine called when the driver is
14509  * loaded. All it does is register with the PCI subsystem.
14510  **/
14511 static int __init i40e_init_module(void)
14512 {
14513 	pr_info("%s: %s - version %s\n", i40e_driver_name,
14514 		i40e_driver_string, i40e_driver_version_str);
14515 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
14516 
14517 	/* There is no need to throttle the number of active tasks because
14518 	 * each device limits its own task using a state bit for scheduling
14519 	 * the service task, and the device tasks do not interfere with each
14520 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14521 	 * since we need to be able to guarantee forward progress even under
14522 	 * memory pressure.
14523 	 */
14524 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
14525 	if (!i40e_wq) {
14526 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14527 		return -ENOMEM;
14528 	}
14529 
14530 	i40e_dbg_init();
14531 	return pci_register_driver(&i40e_driver);
14532 }
14533 module_init(i40e_init_module);
14534 
14535 /**
14536  * i40e_exit_module - Driver exit cleanup routine
14537  *
14538  * i40e_exit_module is called just before the driver is removed
14539  * from memory.
14540  **/
14541 static void __exit i40e_exit_module(void)
14542 {
14543 	pci_unregister_driver(&i40e_driver);
14544 	destroy_workqueue(i40e_wq);
14545 	i40e_dbg_exit();
14546 }
14547 module_exit(i40e_exit_module);
14548