1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 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 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
87 	/* required last entry */
88 	{0, }
89 };
90 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
91 
92 #define I40E_MAX_VF_COUNT 128
93 static int debug = -1;
94 module_param(debug, uint, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
96 
97 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99 MODULE_LICENSE("GPL v2");
100 
101 static struct workqueue_struct *i40e_wq;
102 
103 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
104 				  struct net_device *netdev, int delta)
105 {
106 	struct netdev_hw_addr *ha;
107 
108 	if (!f || !netdev)
109 		return;
110 
111 	netdev_for_each_mc_addr(ha, netdev) {
112 		if (ether_addr_equal(ha->addr, f->macaddr)) {
113 			ha->refcount += delta;
114 			if (ha->refcount <= 0)
115 				ha->refcount = 1;
116 			break;
117 		}
118 	}
119 }
120 
121 /**
122  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
123  * @hw:   pointer to the HW structure
124  * @mem:  ptr to mem struct to fill out
125  * @size: size of memory requested
126  * @alignment: what to align the allocation to
127  **/
128 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
129 			    u64 size, u32 alignment)
130 {
131 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
132 
133 	mem->size = ALIGN(size, alignment);
134 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
135 				     GFP_KERNEL);
136 	if (!mem->va)
137 		return -ENOMEM;
138 
139 	return 0;
140 }
141 
142 /**
143  * i40e_free_dma_mem_d - OS specific memory free for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to free
146  **/
147 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
148 {
149 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
150 
151 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
152 	mem->va = NULL;
153 	mem->pa = 0;
154 	mem->size = 0;
155 
156 	return 0;
157 }
158 
159 /**
160  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
161  * @hw:   pointer to the HW structure
162  * @mem:  ptr to mem struct to fill out
163  * @size: size of memory requested
164  **/
165 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
166 			     u32 size)
167 {
168 	mem->size = size;
169 	mem->va = kzalloc(size, GFP_KERNEL);
170 
171 	if (!mem->va)
172 		return -ENOMEM;
173 
174 	return 0;
175 }
176 
177 /**
178  * i40e_free_virt_mem_d - OS specific memory free for shared code
179  * @hw:   pointer to the HW structure
180  * @mem:  ptr to mem struct to free
181  **/
182 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
183 {
184 	/* it's ok to kfree a NULL pointer */
185 	kfree(mem->va);
186 	mem->va = NULL;
187 	mem->size = 0;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_get_lump - find a lump of free generic resource
194  * @pf: board private structure
195  * @pile: the pile of resource to search
196  * @needed: the number of items needed
197  * @id: an owner id to stick on the items assigned
198  *
199  * Returns the base item index of the lump, or negative for error
200  **/
201 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
202 			 u16 needed, u16 id)
203 {
204 	int ret = -ENOMEM;
205 	int i, j;
206 
207 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 		dev_info(&pf->pdev->dev,
209 			 "param err: pile=%s needed=%d id=0x%04x\n",
210 			 pile ? "<valid>" : "<null>", needed, id);
211 		return -EINVAL;
212 	}
213 
214 	/* Allocate last queue in the pile for FDIR VSI queue
215 	 * so it doesn't fragment the qp_pile
216 	 */
217 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
218 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
219 			dev_err(&pf->pdev->dev,
220 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
221 				pile->num_entries - 1);
222 			return -ENOMEM;
223 		}
224 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
225 		return pile->num_entries - 1;
226 	}
227 
228 	i = 0;
229 	while (i < pile->num_entries) {
230 		/* skip already allocated entries */
231 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
232 			i++;
233 			continue;
234 		}
235 
236 		/* do we have enough in this lump? */
237 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
238 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
239 				break;
240 		}
241 
242 		if (j == needed) {
243 			/* there was enough, so assign it to the requestor */
244 			for (j = 0; j < needed; j++)
245 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
246 			ret = i;
247 			break;
248 		}
249 
250 		/* not enough, so skip over it and continue looking */
251 		i += j;
252 	}
253 
254 	return ret;
255 }
256 
257 /**
258  * i40e_put_lump - return a lump of generic resource
259  * @pile: the pile of resource to search
260  * @index: the base item index
261  * @id: the owner id of the items assigned
262  *
263  * Returns the count of items in the lump
264  **/
265 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
266 {
267 	int valid_id = (id | I40E_PILE_VALID_BIT);
268 	int count = 0;
269 	u16 i;
270 
271 	if (!pile || index >= pile->num_entries)
272 		return -EINVAL;
273 
274 	for (i = index;
275 	     i < pile->num_entries && pile->list[i] == valid_id;
276 	     i++) {
277 		pile->list[i] = 0;
278 		count++;
279 	}
280 
281 
282 	return count;
283 }
284 
285 /**
286  * i40e_find_vsi_from_id - searches for the vsi with the given id
287  * @pf: the pf structure to search for the vsi
288  * @id: id of the vsi it is searching for
289  **/
290 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
291 {
292 	int i;
293 
294 	for (i = 0; i < pf->num_alloc_vsi; i++)
295 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
296 			return pf->vsi[i];
297 
298 	return NULL;
299 }
300 
301 /**
302  * i40e_service_event_schedule - Schedule the service task to wake up
303  * @pf: board private structure
304  *
305  * If not already scheduled, this puts the task into the work queue
306  **/
307 void i40e_service_event_schedule(struct i40e_pf *pf)
308 {
309 	if ((!test_bit(__I40E_DOWN, pf->state) &&
310 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
311 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
312 		queue_work(i40e_wq, &pf->service_task);
313 }
314 
315 /**
316  * i40e_tx_timeout - Respond to a Tx Hang
317  * @netdev: network interface device structure
318  * @txqueue: queue number timing out
319  *
320  * If any port has noticed a Tx timeout, it is likely that the whole
321  * device is munged, not just the one netdev port, so go for the full
322  * reset.
323  **/
324 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
325 {
326 	struct i40e_netdev_priv *np = netdev_priv(netdev);
327 	struct i40e_vsi *vsi = np->vsi;
328 	struct i40e_pf *pf = vsi->back;
329 	struct i40e_ring *tx_ring = NULL;
330 	unsigned int i;
331 	u32 head, val;
332 
333 	pf->tx_timeout_count++;
334 
335 	/* with txqueue index, find the tx_ring struct */
336 	for (i = 0; i < vsi->num_queue_pairs; i++) {
337 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 			if (txqueue ==
339 			    vsi->tx_rings[i]->queue_index) {
340 				tx_ring = vsi->tx_rings[i];
341 				break;
342 			}
343 		}
344 	}
345 
346 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
347 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
348 	else if (time_before(jiffies,
349 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 		return;   /* don't do any new action before the next timeout */
351 
352 	/* don't kick off another recovery if one is already pending */
353 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
354 		return;
355 
356 	if (tx_ring) {
357 		head = i40e_get_head(tx_ring);
358 		/* Read interrupt register */
359 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
360 			val = rd32(&pf->hw,
361 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
362 						tx_ring->vsi->base_vector - 1));
363 		else
364 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
365 
366 		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",
367 			    vsi->seid, txqueue, tx_ring->next_to_clean,
368 			    head, tx_ring->next_to_use,
369 			    readl(tx_ring->tail), val);
370 	}
371 
372 	pf->tx_timeout_last_recovery = jiffies;
373 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
374 		    pf->tx_timeout_recovery_level, txqueue);
375 
376 	switch (pf->tx_timeout_recovery_level) {
377 	case 1:
378 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
379 		break;
380 	case 2:
381 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
382 		break;
383 	case 3:
384 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
385 		break;
386 	default:
387 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
388 		break;
389 	}
390 
391 	i40e_service_event_schedule(pf);
392 	pf->tx_timeout_recovery_level++;
393 }
394 
395 /**
396  * i40e_get_vsi_stats_struct - Get System Network Statistics
397  * @vsi: the VSI we care about
398  *
399  * Returns the address of the device statistics structure.
400  * The statistics are actually updated from the service task.
401  **/
402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
403 {
404 	return &vsi->net_stats;
405 }
406 
407 /**
408  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409  * @ring: Tx ring to get statistics from
410  * @stats: statistics entry to be updated
411  **/
412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 					    struct rtnl_link_stats64 *stats)
414 {
415 	u64 bytes, packets;
416 	unsigned int start;
417 
418 	do {
419 		start = u64_stats_fetch_begin_irq(&ring->syncp);
420 		packets = ring->stats.packets;
421 		bytes   = ring->stats.bytes;
422 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
423 
424 	stats->tx_packets += packets;
425 	stats->tx_bytes   += bytes;
426 }
427 
428 /**
429  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430  * @netdev: network interface device structure
431  * @stats: data structure to store statistics
432  *
433  * Returns the address of the device statistics structure.
434  * The statistics are actually updated from the service task.
435  **/
436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 				  struct rtnl_link_stats64 *stats)
438 {
439 	struct i40e_netdev_priv *np = netdev_priv(netdev);
440 	struct i40e_vsi *vsi = np->vsi;
441 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 	struct i40e_ring *ring;
443 	int i;
444 
445 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
446 		return;
447 
448 	if (!vsi->tx_rings)
449 		return;
450 
451 	rcu_read_lock();
452 	for (i = 0; i < vsi->num_queue_pairs; i++) {
453 		u64 bytes, packets;
454 		unsigned int start;
455 
456 		ring = READ_ONCE(vsi->tx_rings[i]);
457 		if (!ring)
458 			continue;
459 		i40e_get_netdev_stats_struct_tx(ring, stats);
460 
461 		if (i40e_enabled_xdp_vsi(vsi)) {
462 			ring = READ_ONCE(vsi->xdp_rings[i]);
463 			if (!ring)
464 				continue;
465 			i40e_get_netdev_stats_struct_tx(ring, stats);
466 		}
467 
468 		ring = READ_ONCE(vsi->rx_rings[i]);
469 		if (!ring)
470 			continue;
471 		do {
472 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
473 			packets = ring->stats.packets;
474 			bytes   = ring->stats.bytes;
475 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
476 
477 		stats->rx_packets += packets;
478 		stats->rx_bytes   += bytes;
479 
480 	}
481 	rcu_read_unlock();
482 
483 	/* following stats updated by i40e_watchdog_subtask() */
484 	stats->multicast	= vsi_stats->multicast;
485 	stats->tx_errors	= vsi_stats->tx_errors;
486 	stats->tx_dropped	= vsi_stats->tx_dropped;
487 	stats->rx_errors	= vsi_stats->rx_errors;
488 	stats->rx_dropped	= vsi_stats->rx_dropped;
489 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
490 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
491 }
492 
493 /**
494  * i40e_vsi_reset_stats - Resets all stats of the given vsi
495  * @vsi: the VSI to have its stats reset
496  **/
497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
498 {
499 	struct rtnl_link_stats64 *ns;
500 	int i;
501 
502 	if (!vsi)
503 		return;
504 
505 	ns = i40e_get_vsi_stats_struct(vsi);
506 	memset(ns, 0, sizeof(*ns));
507 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 	if (vsi->rx_rings && vsi->rx_rings[0]) {
511 		for (i = 0; i < vsi->num_queue_pairs; i++) {
512 			memset(&vsi->rx_rings[i]->stats, 0,
513 			       sizeof(vsi->rx_rings[i]->stats));
514 			memset(&vsi->rx_rings[i]->rx_stats, 0,
515 			       sizeof(vsi->rx_rings[i]->rx_stats));
516 			memset(&vsi->tx_rings[i]->stats, 0,
517 			       sizeof(vsi->tx_rings[i]->stats));
518 			memset(&vsi->tx_rings[i]->tx_stats, 0,
519 			       sizeof(vsi->tx_rings[i]->tx_stats));
520 		}
521 	}
522 	vsi->stat_offsets_loaded = false;
523 }
524 
525 /**
526  * i40e_pf_reset_stats - Reset all of the stats for the given PF
527  * @pf: the PF to be reset
528  **/
529 void i40e_pf_reset_stats(struct i40e_pf *pf)
530 {
531 	int i;
532 
533 	memset(&pf->stats, 0, sizeof(pf->stats));
534 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 	pf->stat_offsets_loaded = false;
536 
537 	for (i = 0; i < I40E_MAX_VEB; i++) {
538 		if (pf->veb[i]) {
539 			memset(&pf->veb[i]->stats, 0,
540 			       sizeof(pf->veb[i]->stats));
541 			memset(&pf->veb[i]->stats_offsets, 0,
542 			       sizeof(pf->veb[i]->stats_offsets));
543 			memset(&pf->veb[i]->tc_stats, 0,
544 			       sizeof(pf->veb[i]->tc_stats));
545 			memset(&pf->veb[i]->tc_stats_offsets, 0,
546 			       sizeof(pf->veb[i]->tc_stats_offsets));
547 			pf->veb[i]->stat_offsets_loaded = false;
548 		}
549 	}
550 	pf->hw_csum_rx_error = 0;
551 }
552 
553 /**
554  * i40e_stat_update48 - read and update a 48 bit stat from the chip
555  * @hw: ptr to the hardware info
556  * @hireg: the high 32 bit reg to read
557  * @loreg: the low 32 bit reg to read
558  * @offset_loaded: has the initial offset been loaded yet
559  * @offset: ptr to current offset value
560  * @stat: ptr to the stat
561  *
562  * Since the device stats are not reset at PFReset, they likely will not
563  * be zeroed when the driver starts.  We'll save the first values read
564  * and use them as offsets to be subtracted from the raw values in order
565  * to report stats that count from zero.  In the process, we also manage
566  * the potential roll-over.
567  **/
568 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
569 			       bool offset_loaded, u64 *offset, u64 *stat)
570 {
571 	u64 new_data;
572 
573 	if (hw->device_id == I40E_DEV_ID_QEMU) {
574 		new_data = rd32(hw, loreg);
575 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
576 	} else {
577 		new_data = rd64(hw, loreg);
578 	}
579 	if (!offset_loaded)
580 		*offset = new_data;
581 	if (likely(new_data >= *offset))
582 		*stat = new_data - *offset;
583 	else
584 		*stat = (new_data + BIT_ULL(48)) - *offset;
585 	*stat &= 0xFFFFFFFFFFFFULL;
586 }
587 
588 /**
589  * i40e_stat_update32 - read and update a 32 bit stat from the chip
590  * @hw: ptr to the hardware info
591  * @reg: the hw reg to read
592  * @offset_loaded: has the initial offset been loaded yet
593  * @offset: ptr to current offset value
594  * @stat: ptr to the stat
595  **/
596 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
597 			       bool offset_loaded, u64 *offset, u64 *stat)
598 {
599 	u32 new_data;
600 
601 	new_data = rd32(hw, reg);
602 	if (!offset_loaded)
603 		*offset = new_data;
604 	if (likely(new_data >= *offset))
605 		*stat = (u32)(new_data - *offset);
606 	else
607 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
608 }
609 
610 /**
611  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
612  * @hw: ptr to the hardware info
613  * @reg: the hw reg to read and clear
614  * @stat: ptr to the stat
615  **/
616 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
617 {
618 	u32 new_data = rd32(hw, reg);
619 
620 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
621 	*stat += new_data;
622 }
623 
624 /**
625  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
626  * @vsi: the VSI to be updated
627  **/
628 void i40e_update_eth_stats(struct i40e_vsi *vsi)
629 {
630 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
631 	struct i40e_pf *pf = vsi->back;
632 	struct i40e_hw *hw = &pf->hw;
633 	struct i40e_eth_stats *oes;
634 	struct i40e_eth_stats *es;     /* device's eth stats */
635 
636 	es = &vsi->eth_stats;
637 	oes = &vsi->eth_stats_offsets;
638 
639 	/* Gather up the stats that the hw collects */
640 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
641 			   vsi->stat_offsets_loaded,
642 			   &oes->tx_errors, &es->tx_errors);
643 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
644 			   vsi->stat_offsets_loaded,
645 			   &oes->rx_discards, &es->rx_discards);
646 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
647 			   vsi->stat_offsets_loaded,
648 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
649 
650 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
651 			   I40E_GLV_GORCL(stat_idx),
652 			   vsi->stat_offsets_loaded,
653 			   &oes->rx_bytes, &es->rx_bytes);
654 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
655 			   I40E_GLV_UPRCL(stat_idx),
656 			   vsi->stat_offsets_loaded,
657 			   &oes->rx_unicast, &es->rx_unicast);
658 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
659 			   I40E_GLV_MPRCL(stat_idx),
660 			   vsi->stat_offsets_loaded,
661 			   &oes->rx_multicast, &es->rx_multicast);
662 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
663 			   I40E_GLV_BPRCL(stat_idx),
664 			   vsi->stat_offsets_loaded,
665 			   &oes->rx_broadcast, &es->rx_broadcast);
666 
667 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
668 			   I40E_GLV_GOTCL(stat_idx),
669 			   vsi->stat_offsets_loaded,
670 			   &oes->tx_bytes, &es->tx_bytes);
671 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
672 			   I40E_GLV_UPTCL(stat_idx),
673 			   vsi->stat_offsets_loaded,
674 			   &oes->tx_unicast, &es->tx_unicast);
675 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
676 			   I40E_GLV_MPTCL(stat_idx),
677 			   vsi->stat_offsets_loaded,
678 			   &oes->tx_multicast, &es->tx_multicast);
679 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
680 			   I40E_GLV_BPTCL(stat_idx),
681 			   vsi->stat_offsets_loaded,
682 			   &oes->tx_broadcast, &es->tx_broadcast);
683 	vsi->stat_offsets_loaded = true;
684 }
685 
686 /**
687  * i40e_update_veb_stats - Update Switch component statistics
688  * @veb: the VEB being updated
689  **/
690 void i40e_update_veb_stats(struct i40e_veb *veb)
691 {
692 	struct i40e_pf *pf = veb->pf;
693 	struct i40e_hw *hw = &pf->hw;
694 	struct i40e_eth_stats *oes;
695 	struct i40e_eth_stats *es;     /* device's eth stats */
696 	struct i40e_veb_tc_stats *veb_oes;
697 	struct i40e_veb_tc_stats *veb_es;
698 	int i, idx = 0;
699 
700 	idx = veb->stats_idx;
701 	es = &veb->stats;
702 	oes = &veb->stats_offsets;
703 	veb_es = &veb->tc_stats;
704 	veb_oes = &veb->tc_stats_offsets;
705 
706 	/* Gather up the stats that the hw collects */
707 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
708 			   veb->stat_offsets_loaded,
709 			   &oes->tx_discards, &es->tx_discards);
710 	if (hw->revision_id > 0)
711 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
712 				   veb->stat_offsets_loaded,
713 				   &oes->rx_unknown_protocol,
714 				   &es->rx_unknown_protocol);
715 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
716 			   veb->stat_offsets_loaded,
717 			   &oes->rx_bytes, &es->rx_bytes);
718 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
719 			   veb->stat_offsets_loaded,
720 			   &oes->rx_unicast, &es->rx_unicast);
721 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
722 			   veb->stat_offsets_loaded,
723 			   &oes->rx_multicast, &es->rx_multicast);
724 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
725 			   veb->stat_offsets_loaded,
726 			   &oes->rx_broadcast, &es->rx_broadcast);
727 
728 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
729 			   veb->stat_offsets_loaded,
730 			   &oes->tx_bytes, &es->tx_bytes);
731 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
732 			   veb->stat_offsets_loaded,
733 			   &oes->tx_unicast, &es->tx_unicast);
734 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
735 			   veb->stat_offsets_loaded,
736 			   &oes->tx_multicast, &es->tx_multicast);
737 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
738 			   veb->stat_offsets_loaded,
739 			   &oes->tx_broadcast, &es->tx_broadcast);
740 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
741 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
742 				   I40E_GLVEBTC_RPCL(i, idx),
743 				   veb->stat_offsets_loaded,
744 				   &veb_oes->tc_rx_packets[i],
745 				   &veb_es->tc_rx_packets[i]);
746 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
747 				   I40E_GLVEBTC_RBCL(i, idx),
748 				   veb->stat_offsets_loaded,
749 				   &veb_oes->tc_rx_bytes[i],
750 				   &veb_es->tc_rx_bytes[i]);
751 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
752 				   I40E_GLVEBTC_TPCL(i, idx),
753 				   veb->stat_offsets_loaded,
754 				   &veb_oes->tc_tx_packets[i],
755 				   &veb_es->tc_tx_packets[i]);
756 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
757 				   I40E_GLVEBTC_TBCL(i, idx),
758 				   veb->stat_offsets_loaded,
759 				   &veb_oes->tc_tx_bytes[i],
760 				   &veb_es->tc_tx_bytes[i]);
761 	}
762 	veb->stat_offsets_loaded = true;
763 }
764 
765 /**
766  * i40e_update_vsi_stats - Update the vsi statistics counters.
767  * @vsi: the VSI to be updated
768  *
769  * There are a few instances where we store the same stat in a
770  * couple of different structs.  This is partly because we have
771  * the netdev stats that need to be filled out, which is slightly
772  * different from the "eth_stats" defined by the chip and used in
773  * VF communications.  We sort it out here.
774  **/
775 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
776 {
777 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
778 	struct i40e_pf *pf = vsi->back;
779 	struct rtnl_link_stats64 *ons;
780 	struct rtnl_link_stats64 *ns;   /* netdev stats */
781 	struct i40e_eth_stats *oes;
782 	struct i40e_eth_stats *es;     /* device's eth stats */
783 	u64 tx_restart, tx_busy;
784 	struct i40e_ring *p;
785 	u64 bytes, packets;
786 	unsigned int start;
787 	u64 tx_linearize;
788 	u64 tx_force_wb;
789 	u64 tx_stopped;
790 	u64 rx_p, rx_b;
791 	u64 tx_p, tx_b;
792 	u16 q;
793 
794 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
795 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
796 		return;
797 
798 	ns = i40e_get_vsi_stats_struct(vsi);
799 	ons = &vsi->net_stats_offsets;
800 	es = &vsi->eth_stats;
801 	oes = &vsi->eth_stats_offsets;
802 
803 	/* Gather up the netdev and vsi stats that the driver collects
804 	 * on the fly during packet processing
805 	 */
806 	rx_b = rx_p = 0;
807 	tx_b = tx_p = 0;
808 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
809 	tx_stopped = 0;
810 	rx_page = 0;
811 	rx_buf = 0;
812 	rx_reuse = 0;
813 	rx_alloc = 0;
814 	rx_waive = 0;
815 	rx_busy = 0;
816 	rcu_read_lock();
817 	for (q = 0; q < vsi->num_queue_pairs; q++) {
818 		/* locate Tx ring */
819 		p = READ_ONCE(vsi->tx_rings[q]);
820 		if (!p)
821 			continue;
822 
823 		do {
824 			start = u64_stats_fetch_begin_irq(&p->syncp);
825 			packets = p->stats.packets;
826 			bytes = p->stats.bytes;
827 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
828 		tx_b += bytes;
829 		tx_p += packets;
830 		tx_restart += p->tx_stats.restart_queue;
831 		tx_busy += p->tx_stats.tx_busy;
832 		tx_linearize += p->tx_stats.tx_linearize;
833 		tx_force_wb += p->tx_stats.tx_force_wb;
834 		tx_stopped += p->tx_stats.tx_stopped;
835 
836 		/* locate Rx ring */
837 		p = READ_ONCE(vsi->rx_rings[q]);
838 		if (!p)
839 			continue;
840 
841 		do {
842 			start = u64_stats_fetch_begin_irq(&p->syncp);
843 			packets = p->stats.packets;
844 			bytes = p->stats.bytes;
845 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
846 		rx_b += bytes;
847 		rx_p += packets;
848 		rx_buf += p->rx_stats.alloc_buff_failed;
849 		rx_page += p->rx_stats.alloc_page_failed;
850 		rx_reuse += p->rx_stats.page_reuse_count;
851 		rx_alloc += p->rx_stats.page_alloc_count;
852 		rx_waive += p->rx_stats.page_waive_count;
853 		rx_busy += p->rx_stats.page_busy_count;
854 
855 		if (i40e_enabled_xdp_vsi(vsi)) {
856 			/* locate XDP ring */
857 			p = READ_ONCE(vsi->xdp_rings[q]);
858 			if (!p)
859 				continue;
860 
861 			do {
862 				start = u64_stats_fetch_begin_irq(&p->syncp);
863 				packets = p->stats.packets;
864 				bytes = p->stats.bytes;
865 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
866 			tx_b += bytes;
867 			tx_p += packets;
868 			tx_restart += p->tx_stats.restart_queue;
869 			tx_busy += p->tx_stats.tx_busy;
870 			tx_linearize += p->tx_stats.tx_linearize;
871 			tx_force_wb += p->tx_stats.tx_force_wb;
872 		}
873 	}
874 	rcu_read_unlock();
875 	vsi->tx_restart = tx_restart;
876 	vsi->tx_busy = tx_busy;
877 	vsi->tx_linearize = tx_linearize;
878 	vsi->tx_force_wb = tx_force_wb;
879 	vsi->tx_stopped = tx_stopped;
880 	vsi->rx_page_failed = rx_page;
881 	vsi->rx_buf_failed = rx_buf;
882 	vsi->rx_page_reuse = rx_reuse;
883 	vsi->rx_page_alloc = rx_alloc;
884 	vsi->rx_page_waive = rx_waive;
885 	vsi->rx_page_busy = rx_busy;
886 
887 	ns->rx_packets = rx_p;
888 	ns->rx_bytes = rx_b;
889 	ns->tx_packets = tx_p;
890 	ns->tx_bytes = tx_b;
891 
892 	/* update netdev stats from eth stats */
893 	i40e_update_eth_stats(vsi);
894 	ons->tx_errors = oes->tx_errors;
895 	ns->tx_errors = es->tx_errors;
896 	ons->multicast = oes->rx_multicast;
897 	ns->multicast = es->rx_multicast;
898 	ons->rx_dropped = oes->rx_discards;
899 	ns->rx_dropped = es->rx_discards;
900 	ons->tx_dropped = oes->tx_discards;
901 	ns->tx_dropped = es->tx_discards;
902 
903 	/* pull in a couple PF stats if this is the main vsi */
904 	if (vsi == pf->vsi[pf->lan_vsi]) {
905 		ns->rx_crc_errors = pf->stats.crc_errors;
906 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
907 		ns->rx_length_errors = pf->stats.rx_length_errors;
908 	}
909 }
910 
911 /**
912  * i40e_update_pf_stats - Update the PF statistics counters.
913  * @pf: the PF to be updated
914  **/
915 static void i40e_update_pf_stats(struct i40e_pf *pf)
916 {
917 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
918 	struct i40e_hw_port_stats *nsd = &pf->stats;
919 	struct i40e_hw *hw = &pf->hw;
920 	u32 val;
921 	int i;
922 
923 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
924 			   I40E_GLPRT_GORCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
927 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
928 			   I40E_GLPRT_GOTCL(hw->port),
929 			   pf->stat_offsets_loaded,
930 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
931 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
932 			   pf->stat_offsets_loaded,
933 			   &osd->eth.rx_discards,
934 			   &nsd->eth.rx_discards);
935 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
936 			   I40E_GLPRT_UPRCL(hw->port),
937 			   pf->stat_offsets_loaded,
938 			   &osd->eth.rx_unicast,
939 			   &nsd->eth.rx_unicast);
940 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
941 			   I40E_GLPRT_MPRCL(hw->port),
942 			   pf->stat_offsets_loaded,
943 			   &osd->eth.rx_multicast,
944 			   &nsd->eth.rx_multicast);
945 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
946 			   I40E_GLPRT_BPRCL(hw->port),
947 			   pf->stat_offsets_loaded,
948 			   &osd->eth.rx_broadcast,
949 			   &nsd->eth.rx_broadcast);
950 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
951 			   I40E_GLPRT_UPTCL(hw->port),
952 			   pf->stat_offsets_loaded,
953 			   &osd->eth.tx_unicast,
954 			   &nsd->eth.tx_unicast);
955 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
956 			   I40E_GLPRT_MPTCL(hw->port),
957 			   pf->stat_offsets_loaded,
958 			   &osd->eth.tx_multicast,
959 			   &nsd->eth.tx_multicast);
960 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
961 			   I40E_GLPRT_BPTCL(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->eth.tx_broadcast,
964 			   &nsd->eth.tx_broadcast);
965 
966 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->tx_dropped_link_down,
969 			   &nsd->tx_dropped_link_down);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->crc_errors, &nsd->crc_errors);
974 
975 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->illegal_bytes, &nsd->illegal_bytes);
978 
979 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->mac_local_faults,
982 			   &nsd->mac_local_faults);
983 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
984 			   pf->stat_offsets_loaded,
985 			   &osd->mac_remote_faults,
986 			   &nsd->mac_remote_faults);
987 
988 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->rx_length_errors,
991 			   &nsd->rx_length_errors);
992 
993 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->link_xon_rx, &nsd->link_xon_rx);
996 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
997 			   pf->stat_offsets_loaded,
998 			   &osd->link_xon_tx, &nsd->link_xon_tx);
999 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1000 			   pf->stat_offsets_loaded,
1001 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1002 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1003 			   pf->stat_offsets_loaded,
1004 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1005 
1006 	for (i = 0; i < 8; i++) {
1007 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xoff_rx[i],
1010 				   &nsd->priority_xoff_rx[i]);
1011 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1012 				   pf->stat_offsets_loaded,
1013 				   &osd->priority_xon_rx[i],
1014 				   &nsd->priority_xon_rx[i]);
1015 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1016 				   pf->stat_offsets_loaded,
1017 				   &osd->priority_xon_tx[i],
1018 				   &nsd->priority_xon_tx[i]);
1019 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1020 				   pf->stat_offsets_loaded,
1021 				   &osd->priority_xoff_tx[i],
1022 				   &nsd->priority_xoff_tx[i]);
1023 		i40e_stat_update32(hw,
1024 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1025 				   pf->stat_offsets_loaded,
1026 				   &osd->priority_xon_2_xoff[i],
1027 				   &nsd->priority_xon_2_xoff[i]);
1028 	}
1029 
1030 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1031 			   I40E_GLPRT_PRC64L(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->rx_size_64, &nsd->rx_size_64);
1034 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1035 			   I40E_GLPRT_PRC127L(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->rx_size_127, &nsd->rx_size_127);
1038 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1039 			   I40E_GLPRT_PRC255L(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->rx_size_255, &nsd->rx_size_255);
1042 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1043 			   I40E_GLPRT_PRC511L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->rx_size_511, &nsd->rx_size_511);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1047 			   I40E_GLPRT_PRC1023L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1051 			   I40E_GLPRT_PRC1522L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1055 			   I40E_GLPRT_PRC9522L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->rx_size_big, &nsd->rx_size_big);
1058 
1059 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1060 			   I40E_GLPRT_PTC64L(hw->port),
1061 			   pf->stat_offsets_loaded,
1062 			   &osd->tx_size_64, &nsd->tx_size_64);
1063 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1064 			   I40E_GLPRT_PTC127L(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->tx_size_127, &nsd->tx_size_127);
1067 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1068 			   I40E_GLPRT_PTC255L(hw->port),
1069 			   pf->stat_offsets_loaded,
1070 			   &osd->tx_size_255, &nsd->tx_size_255);
1071 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1072 			   I40E_GLPRT_PTC511L(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->tx_size_511, &nsd->tx_size_511);
1075 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1076 			   I40E_GLPRT_PTC1023L(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1079 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1080 			   I40E_GLPRT_PTC1522L(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1083 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1084 			   I40E_GLPRT_PTC9522L(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->tx_size_big, &nsd->tx_size_big);
1087 
1088 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->rx_undersize, &nsd->rx_undersize);
1091 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->rx_fragments, &nsd->rx_fragments);
1094 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->rx_oversize, &nsd->rx_oversize);
1097 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1098 			   pf->stat_offsets_loaded,
1099 			   &osd->rx_jabber, &nsd->rx_jabber);
1100 
1101 	/* FDIR stats */
1102 	i40e_stat_update_and_clear32(hw,
1103 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1104 			&nsd->fd_atr_match);
1105 	i40e_stat_update_and_clear32(hw,
1106 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1107 			&nsd->fd_sb_match);
1108 	i40e_stat_update_and_clear32(hw,
1109 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1110 			&nsd->fd_atr_tunnel_match);
1111 
1112 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1113 	nsd->tx_lpi_status =
1114 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1115 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1116 	nsd->rx_lpi_status =
1117 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1118 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1119 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1122 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1123 			   pf->stat_offsets_loaded,
1124 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1125 
1126 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1127 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1128 		nsd->fd_sb_status = true;
1129 	else
1130 		nsd->fd_sb_status = false;
1131 
1132 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1133 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1134 		nsd->fd_atr_status = true;
1135 	else
1136 		nsd->fd_atr_status = false;
1137 
1138 	pf->stat_offsets_loaded = true;
1139 }
1140 
1141 /**
1142  * i40e_update_stats - Update the various statistics counters.
1143  * @vsi: the VSI to be updated
1144  *
1145  * Update the various stats for this VSI and its related entities.
1146  **/
1147 void i40e_update_stats(struct i40e_vsi *vsi)
1148 {
1149 	struct i40e_pf *pf = vsi->back;
1150 
1151 	if (vsi == pf->vsi[pf->lan_vsi])
1152 		i40e_update_pf_stats(pf);
1153 
1154 	i40e_update_vsi_stats(vsi);
1155 }
1156 
1157 /**
1158  * i40e_count_filters - counts VSI mac filters
1159  * @vsi: the VSI to be searched
1160  *
1161  * Returns count of mac filters
1162  **/
1163 int i40e_count_filters(struct i40e_vsi *vsi)
1164 {
1165 	struct i40e_mac_filter *f;
1166 	struct hlist_node *h;
1167 	int bkt;
1168 	int cnt = 0;
1169 
1170 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1171 		++cnt;
1172 
1173 	return cnt;
1174 }
1175 
1176 /**
1177  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1178  * @vsi: the VSI to be searched
1179  * @macaddr: the MAC address
1180  * @vlan: the vlan
1181  *
1182  * Returns ptr to the filter object or NULL
1183  **/
1184 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1185 						const u8 *macaddr, s16 vlan)
1186 {
1187 	struct i40e_mac_filter *f;
1188 	u64 key;
1189 
1190 	if (!vsi || !macaddr)
1191 		return NULL;
1192 
1193 	key = i40e_addr_to_hkey(macaddr);
1194 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1195 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1196 		    (vlan == f->vlan))
1197 			return f;
1198 	}
1199 	return NULL;
1200 }
1201 
1202 /**
1203  * i40e_find_mac - Find a mac addr in the macvlan filters list
1204  * @vsi: the VSI to be searched
1205  * @macaddr: the MAC address we are searching for
1206  *
1207  * Returns the first filter with the provided MAC address or NULL if
1208  * MAC address was not found
1209  **/
1210 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1211 {
1212 	struct i40e_mac_filter *f;
1213 	u64 key;
1214 
1215 	if (!vsi || !macaddr)
1216 		return NULL;
1217 
1218 	key = i40e_addr_to_hkey(macaddr);
1219 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1220 		if ((ether_addr_equal(macaddr, f->macaddr)))
1221 			return f;
1222 	}
1223 	return NULL;
1224 }
1225 
1226 /**
1227  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1228  * @vsi: the VSI to be searched
1229  *
1230  * Returns true if VSI is in vlan mode or false otherwise
1231  **/
1232 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1233 {
1234 	/* If we have a PVID, always operate in VLAN mode */
1235 	if (vsi->info.pvid)
1236 		return true;
1237 
1238 	/* We need to operate in VLAN mode whenever we have any filters with
1239 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1240 	 * time, incurring search cost repeatedly. However, we can notice two
1241 	 * things:
1242 	 *
1243 	 * 1) the only place where we can gain a VLAN filter is in
1244 	 *    i40e_add_filter.
1245 	 *
1246 	 * 2) the only place where filters are actually removed is in
1247 	 *    i40e_sync_filters_subtask.
1248 	 *
1249 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1250 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1251 	 * we have to perform the full search after deleting filters in
1252 	 * i40e_sync_filters_subtask, but we already have to search
1253 	 * filters here and can perform the check at the same time. This
1254 	 * results in avoiding embedding a loop for VLAN mode inside another
1255 	 * loop over all the filters, and should maintain correctness as noted
1256 	 * above.
1257 	 */
1258 	return vsi->has_vlan_filter;
1259 }
1260 
1261 /**
1262  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1263  * @vsi: the VSI to configure
1264  * @tmp_add_list: list of filters ready to be added
1265  * @tmp_del_list: list of filters ready to be deleted
1266  * @vlan_filters: the number of active VLAN filters
1267  *
1268  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1269  * behave as expected. If we have any active VLAN filters remaining or about
1270  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1271  * so that they only match against untagged traffic. If we no longer have any
1272  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1273  * so that they match against both tagged and untagged traffic. In this way,
1274  * we ensure that we correctly receive the desired traffic. This ensures that
1275  * when we have an active VLAN we will receive only untagged traffic and
1276  * traffic matching active VLANs. If we have no active VLANs then we will
1277  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1278  *
1279  * Finally, in a similar fashion, this function also corrects filters when
1280  * there is an active PVID assigned to this VSI.
1281  *
1282  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1283  *
1284  * This function is only expected to be called from within
1285  * i40e_sync_vsi_filters.
1286  *
1287  * NOTE: This function expects to be called while under the
1288  * mac_filter_hash_lock
1289  */
1290 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1291 					 struct hlist_head *tmp_add_list,
1292 					 struct hlist_head *tmp_del_list,
1293 					 int vlan_filters)
1294 {
1295 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1296 	struct i40e_mac_filter *f, *add_head;
1297 	struct i40e_new_mac_filter *new;
1298 	struct hlist_node *h;
1299 	int bkt, new_vlan;
1300 
1301 	/* To determine if a particular filter needs to be replaced we
1302 	 * have the three following conditions:
1303 	 *
1304 	 * a) if we have a PVID assigned, then all filters which are
1305 	 *    not marked as VLAN=PVID must be replaced with filters that
1306 	 *    are.
1307 	 * b) otherwise, if we have any active VLANS, all filters
1308 	 *    which are marked as VLAN=-1 must be replaced with
1309 	 *    filters marked as VLAN=0
1310 	 * c) finally, if we do not have any active VLANS, all filters
1311 	 *    which are marked as VLAN=0 must be replaced with filters
1312 	 *    marked as VLAN=-1
1313 	 */
1314 
1315 	/* Update the filters about to be added in place */
1316 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1317 		if (pvid && new->f->vlan != pvid)
1318 			new->f->vlan = pvid;
1319 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1320 			new->f->vlan = 0;
1321 		else if (!vlan_filters && new->f->vlan == 0)
1322 			new->f->vlan = I40E_VLAN_ANY;
1323 	}
1324 
1325 	/* Update the remaining active filters */
1326 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1327 		/* Combine the checks for whether a filter needs to be changed
1328 		 * and then determine the new VLAN inside the if block, in
1329 		 * order to avoid duplicating code for adding the new filter
1330 		 * then deleting the old filter.
1331 		 */
1332 		if ((pvid && f->vlan != pvid) ||
1333 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1334 		    (!vlan_filters && f->vlan == 0)) {
1335 			/* Determine the new vlan we will be adding */
1336 			if (pvid)
1337 				new_vlan = pvid;
1338 			else if (vlan_filters)
1339 				new_vlan = 0;
1340 			else
1341 				new_vlan = I40E_VLAN_ANY;
1342 
1343 			/* Create the new filter */
1344 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1345 			if (!add_head)
1346 				return -ENOMEM;
1347 
1348 			/* Create a temporary i40e_new_mac_filter */
1349 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1350 			if (!new)
1351 				return -ENOMEM;
1352 
1353 			new->f = add_head;
1354 			new->state = add_head->state;
1355 
1356 			/* Add the new filter to the tmp list */
1357 			hlist_add_head(&new->hlist, tmp_add_list);
1358 
1359 			/* Put the original filter into the delete list */
1360 			f->state = I40E_FILTER_REMOVE;
1361 			hash_del(&f->hlist);
1362 			hlist_add_head(&f->hlist, tmp_del_list);
1363 		}
1364 	}
1365 
1366 	vsi->has_vlan_filter = !!vlan_filters;
1367 
1368 	return 0;
1369 }
1370 
1371 /**
1372  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1373  * @vsi: the vsi to configure
1374  * @new_mac: new mac filter to be added
1375  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1376  * @vlan_filters: the number of active VLAN filters
1377  * @trusted: flag if the VF is trusted
1378  *
1379  * Get new VLAN id based on current VLAN filters, trust, PVID
1380  * and vf-vlan-prune-disable flag.
1381  *
1382  * Returns the value of the new vlan filter or
1383  * the old value if no new filter is needed.
1384  */
1385 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1386 				struct i40e_new_mac_filter *new_mac,
1387 				struct i40e_mac_filter *f,
1388 				int vlan_filters,
1389 				bool trusted)
1390 {
1391 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1392 	struct i40e_pf *pf = vsi->back;
1393 	bool is_any;
1394 
1395 	if (new_mac)
1396 		f = new_mac->f;
1397 
1398 	if (pvid && f->vlan != pvid)
1399 		return pvid;
1400 
1401 	is_any = (trusted ||
1402 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1403 
1404 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1405 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1406 	    (is_any && !vlan_filters && f->vlan == 0)) {
1407 		if (is_any)
1408 			return I40E_VLAN_ANY;
1409 		else
1410 			return 0;
1411 	}
1412 
1413 	return f->vlan;
1414 }
1415 
1416 /**
1417  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1418  * @vsi: the vsi to configure
1419  * @tmp_add_list: list of filters ready to be added
1420  * @tmp_del_list: list of filters ready to be deleted
1421  * @vlan_filters: the number of active VLAN filters
1422  * @trusted: flag if the VF is trusted
1423  *
1424  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1425  * and vf-vlan-prune-disable flag.
1426  *
1427  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1428  *
1429  * This function is only expected to be called from within
1430  * i40e_sync_vsi_filters.
1431  *
1432  * NOTE: This function expects to be called while under the
1433  * mac_filter_hash_lock
1434  */
1435 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1436 					    struct hlist_head *tmp_add_list,
1437 					    struct hlist_head *tmp_del_list,
1438 					    int vlan_filters,
1439 					    bool trusted)
1440 {
1441 	struct i40e_mac_filter *f, *add_head;
1442 	struct i40e_new_mac_filter *new_mac;
1443 	struct hlist_node *h;
1444 	int bkt, new_vlan;
1445 
1446 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1447 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1448 							vlan_filters, trusted);
1449 	}
1450 
1451 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1452 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1453 						trusted);
1454 		if (new_vlan != f->vlan) {
1455 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1456 			if (!add_head)
1457 				return -ENOMEM;
1458 			/* Create a temporary i40e_new_mac_filter */
1459 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1460 			if (!new_mac)
1461 				return -ENOMEM;
1462 			new_mac->f = add_head;
1463 			new_mac->state = add_head->state;
1464 
1465 			/* Add the new filter to the tmp list */
1466 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1467 
1468 			/* Put the original filter into the delete list */
1469 			f->state = I40E_FILTER_REMOVE;
1470 			hash_del(&f->hlist);
1471 			hlist_add_head(&f->hlist, tmp_del_list);
1472 		}
1473 	}
1474 
1475 	vsi->has_vlan_filter = !!vlan_filters;
1476 	return 0;
1477 }
1478 
1479 /**
1480  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1481  * @vsi: the PF Main VSI - inappropriate for any other VSI
1482  * @macaddr: the MAC address
1483  *
1484  * Remove whatever filter the firmware set up so the driver can manage
1485  * its own filtering intelligently.
1486  **/
1487 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1488 {
1489 	struct i40e_aqc_remove_macvlan_element_data element;
1490 	struct i40e_pf *pf = vsi->back;
1491 
1492 	/* Only appropriate for the PF main VSI */
1493 	if (vsi->type != I40E_VSI_MAIN)
1494 		return;
1495 
1496 	memset(&element, 0, sizeof(element));
1497 	ether_addr_copy(element.mac_addr, macaddr);
1498 	element.vlan_tag = 0;
1499 	/* Ignore error returns, some firmware does it this way... */
1500 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1501 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1502 
1503 	memset(&element, 0, sizeof(element));
1504 	ether_addr_copy(element.mac_addr, macaddr);
1505 	element.vlan_tag = 0;
1506 	/* ...and some firmware does it this way. */
1507 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1508 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1509 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1510 }
1511 
1512 /**
1513  * i40e_add_filter - Add a mac/vlan filter to the VSI
1514  * @vsi: the VSI to be searched
1515  * @macaddr: the MAC address
1516  * @vlan: the vlan
1517  *
1518  * Returns ptr to the filter object or NULL when no memory available.
1519  *
1520  * NOTE: This function is expected to be called with mac_filter_hash_lock
1521  * being held.
1522  **/
1523 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1524 					const u8 *macaddr, s16 vlan)
1525 {
1526 	struct i40e_mac_filter *f;
1527 	u64 key;
1528 
1529 	if (!vsi || !macaddr)
1530 		return NULL;
1531 
1532 	f = i40e_find_filter(vsi, macaddr, vlan);
1533 	if (!f) {
1534 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1535 		if (!f)
1536 			return NULL;
1537 
1538 		/* Update the boolean indicating if we need to function in
1539 		 * VLAN mode.
1540 		 */
1541 		if (vlan >= 0)
1542 			vsi->has_vlan_filter = true;
1543 
1544 		ether_addr_copy(f->macaddr, macaddr);
1545 		f->vlan = vlan;
1546 		f->state = I40E_FILTER_NEW;
1547 		INIT_HLIST_NODE(&f->hlist);
1548 
1549 		key = i40e_addr_to_hkey(macaddr);
1550 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1551 
1552 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1553 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1554 	}
1555 
1556 	/* If we're asked to add a filter that has been marked for removal, it
1557 	 * is safe to simply restore it to active state. __i40e_del_filter
1558 	 * will have simply deleted any filters which were previously marked
1559 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1560 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1561 	 * task, just restore this filter to the ACTIVE state so that the
1562 	 * sync task leaves it in place
1563 	 */
1564 	if (f->state == I40E_FILTER_REMOVE)
1565 		f->state = I40E_FILTER_ACTIVE;
1566 
1567 	return f;
1568 }
1569 
1570 /**
1571  * __i40e_del_filter - Remove a specific filter from the VSI
1572  * @vsi: VSI to remove from
1573  * @f: the filter to remove from the list
1574  *
1575  * This function should be called instead of i40e_del_filter only if you know
1576  * the exact filter you will remove already, such as via i40e_find_filter or
1577  * i40e_find_mac.
1578  *
1579  * NOTE: This function is expected to be called with mac_filter_hash_lock
1580  * being held.
1581  * ANOTHER NOTE: This function MUST be called from within the context of
1582  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1583  * instead of list_for_each_entry().
1584  **/
1585 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1586 {
1587 	if (!f)
1588 		return;
1589 
1590 	/* If the filter was never added to firmware then we can just delete it
1591 	 * directly and we don't want to set the status to remove or else an
1592 	 * admin queue command will unnecessarily fire.
1593 	 */
1594 	if ((f->state == I40E_FILTER_FAILED) ||
1595 	    (f->state == I40E_FILTER_NEW)) {
1596 		hash_del(&f->hlist);
1597 		kfree(f);
1598 	} else {
1599 		f->state = I40E_FILTER_REMOVE;
1600 	}
1601 
1602 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1603 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1604 }
1605 
1606 /**
1607  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1608  * @vsi: the VSI to be searched
1609  * @macaddr: the MAC address
1610  * @vlan: the VLAN
1611  *
1612  * NOTE: This function is expected to be called with mac_filter_hash_lock
1613  * being held.
1614  * ANOTHER NOTE: This function MUST be called from within the context of
1615  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1616  * instead of list_for_each_entry().
1617  **/
1618 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1619 {
1620 	struct i40e_mac_filter *f;
1621 
1622 	if (!vsi || !macaddr)
1623 		return;
1624 
1625 	f = i40e_find_filter(vsi, macaddr, vlan);
1626 	__i40e_del_filter(vsi, f);
1627 }
1628 
1629 /**
1630  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1631  * @vsi: the VSI to be searched
1632  * @macaddr: the mac address to be filtered
1633  *
1634  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1635  * go through all the macvlan filters and add a macvlan filter for each
1636  * unique vlan that already exists. If a PVID has been assigned, instead only
1637  * add the macaddr to that VLAN.
1638  *
1639  * Returns last filter added on success, else NULL
1640  **/
1641 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1642 					    const u8 *macaddr)
1643 {
1644 	struct i40e_mac_filter *f, *add = NULL;
1645 	struct hlist_node *h;
1646 	int bkt;
1647 
1648 	if (vsi->info.pvid)
1649 		return i40e_add_filter(vsi, macaddr,
1650 				       le16_to_cpu(vsi->info.pvid));
1651 
1652 	if (!i40e_is_vsi_in_vlan(vsi))
1653 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1654 
1655 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1656 		if (f->state == I40E_FILTER_REMOVE)
1657 			continue;
1658 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1659 		if (!add)
1660 			return NULL;
1661 	}
1662 
1663 	return add;
1664 }
1665 
1666 /**
1667  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1668  * @vsi: the VSI to be searched
1669  * @macaddr: the mac address to be removed
1670  *
1671  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1672  * associated with.
1673  *
1674  * Returns 0 for success, or error
1675  **/
1676 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1677 {
1678 	struct i40e_mac_filter *f;
1679 	struct hlist_node *h;
1680 	bool found = false;
1681 	int bkt;
1682 
1683 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1684 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1685 		if (ether_addr_equal(macaddr, f->macaddr)) {
1686 			__i40e_del_filter(vsi, f);
1687 			found = true;
1688 		}
1689 	}
1690 
1691 	if (found)
1692 		return 0;
1693 	else
1694 		return -ENOENT;
1695 }
1696 
1697 /**
1698  * i40e_set_mac - NDO callback to set mac address
1699  * @netdev: network interface device structure
1700  * @p: pointer to an address structure
1701  *
1702  * Returns 0 on success, negative on failure
1703  **/
1704 static int i40e_set_mac(struct net_device *netdev, void *p)
1705 {
1706 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1707 	struct i40e_vsi *vsi = np->vsi;
1708 	struct i40e_pf *pf = vsi->back;
1709 	struct i40e_hw *hw = &pf->hw;
1710 	struct sockaddr *addr = p;
1711 
1712 	if (!is_valid_ether_addr(addr->sa_data))
1713 		return -EADDRNOTAVAIL;
1714 
1715 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1716 		netdev_info(netdev, "already using mac address %pM\n",
1717 			    addr->sa_data);
1718 		return 0;
1719 	}
1720 
1721 	if (test_bit(__I40E_DOWN, pf->state) ||
1722 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1723 		return -EADDRNOTAVAIL;
1724 
1725 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1726 		netdev_info(netdev, "returning to hw mac address %pM\n",
1727 			    hw->mac.addr);
1728 	else
1729 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1730 
1731 	/* Copy the address first, so that we avoid a possible race with
1732 	 * .set_rx_mode().
1733 	 * - Remove old address from MAC filter
1734 	 * - Copy new address
1735 	 * - Add new address to MAC filter
1736 	 */
1737 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1738 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1739 	eth_hw_addr_set(netdev, addr->sa_data);
1740 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1741 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1742 
1743 	if (vsi->type == I40E_VSI_MAIN) {
1744 		i40e_status ret;
1745 
1746 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1747 						addr->sa_data, NULL);
1748 		if (ret)
1749 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1750 				    i40e_stat_str(hw, ret),
1751 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1752 	}
1753 
1754 	/* schedule our worker thread which will take care of
1755 	 * applying the new filter changes
1756 	 */
1757 	i40e_service_event_schedule(pf);
1758 	return 0;
1759 }
1760 
1761 /**
1762  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1763  * @vsi: vsi structure
1764  * @seed: RSS hash seed
1765  * @lut: pointer to lookup table of lut_size
1766  * @lut_size: size of the lookup table
1767  **/
1768 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1769 			      u8 *lut, u16 lut_size)
1770 {
1771 	struct i40e_pf *pf = vsi->back;
1772 	struct i40e_hw *hw = &pf->hw;
1773 	int ret = 0;
1774 
1775 	if (seed) {
1776 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1777 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1778 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1779 		if (ret) {
1780 			dev_info(&pf->pdev->dev,
1781 				 "Cannot set RSS key, err %s aq_err %s\n",
1782 				 i40e_stat_str(hw, ret),
1783 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1784 			return ret;
1785 		}
1786 	}
1787 	if (lut) {
1788 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1789 
1790 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1791 		if (ret) {
1792 			dev_info(&pf->pdev->dev,
1793 				 "Cannot set RSS lut, err %s aq_err %s\n",
1794 				 i40e_stat_str(hw, ret),
1795 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1796 			return ret;
1797 		}
1798 	}
1799 	return ret;
1800 }
1801 
1802 /**
1803  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1804  * @vsi: VSI structure
1805  **/
1806 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1807 {
1808 	struct i40e_pf *pf = vsi->back;
1809 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1810 	u8 *lut;
1811 	int ret;
1812 
1813 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1814 		return 0;
1815 	if (!vsi->rss_size)
1816 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1817 				      vsi->num_queue_pairs);
1818 	if (!vsi->rss_size)
1819 		return -EINVAL;
1820 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1821 	if (!lut)
1822 		return -ENOMEM;
1823 
1824 	/* Use the user configured hash keys and lookup table if there is one,
1825 	 * otherwise use default
1826 	 */
1827 	if (vsi->rss_lut_user)
1828 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1829 	else
1830 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1831 	if (vsi->rss_hkey_user)
1832 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1833 	else
1834 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1835 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1836 	kfree(lut);
1837 	return ret;
1838 }
1839 
1840 /**
1841  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1842  * @vsi: the VSI being configured,
1843  * @ctxt: VSI context structure
1844  * @enabled_tc: number of traffic classes to enable
1845  *
1846  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1847  **/
1848 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1849 					   struct i40e_vsi_context *ctxt,
1850 					   u8 enabled_tc)
1851 {
1852 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1853 	int i, override_q, pow, num_qps, ret;
1854 	u8 netdev_tc = 0, offset = 0;
1855 
1856 	if (vsi->type != I40E_VSI_MAIN)
1857 		return -EINVAL;
1858 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1859 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1860 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1861 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1862 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1863 
1864 	/* find the next higher power-of-2 of num queue pairs */
1865 	pow = ilog2(num_qps);
1866 	if (!is_power_of_2(num_qps))
1867 		pow++;
1868 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1869 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1870 
1871 	/* Setup queue offset/count for all TCs for given VSI */
1872 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1873 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1874 		/* See if the given TC is enabled for the given VSI */
1875 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1876 			offset = vsi->mqprio_qopt.qopt.offset[i];
1877 			qcount = vsi->mqprio_qopt.qopt.count[i];
1878 			if (qcount > max_qcount)
1879 				max_qcount = qcount;
1880 			vsi->tc_config.tc_info[i].qoffset = offset;
1881 			vsi->tc_config.tc_info[i].qcount = qcount;
1882 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1883 		} else {
1884 			/* TC is not enabled so set the offset to
1885 			 * default queue and allocate one queue
1886 			 * for the given TC.
1887 			 */
1888 			vsi->tc_config.tc_info[i].qoffset = 0;
1889 			vsi->tc_config.tc_info[i].qcount = 1;
1890 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1891 		}
1892 	}
1893 
1894 	/* Set actual Tx/Rx queue pairs */
1895 	vsi->num_queue_pairs = offset + qcount;
1896 
1897 	/* Setup queue TC[0].qmap for given VSI context */
1898 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1899 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1900 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1901 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1902 
1903 	/* Reconfigure RSS for main VSI with max queue count */
1904 	vsi->rss_size = max_qcount;
1905 	ret = i40e_vsi_config_rss(vsi);
1906 	if (ret) {
1907 		dev_info(&vsi->back->pdev->dev,
1908 			 "Failed to reconfig rss for num_queues (%u)\n",
1909 			 max_qcount);
1910 		return ret;
1911 	}
1912 	vsi->reconfig_rss = true;
1913 	dev_dbg(&vsi->back->pdev->dev,
1914 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1915 
1916 	/* Find queue count available for channel VSIs and starting offset
1917 	 * for channel VSIs
1918 	 */
1919 	override_q = vsi->mqprio_qopt.qopt.count[0];
1920 	if (override_q && override_q < vsi->num_queue_pairs) {
1921 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1922 		vsi->next_base_queue = override_q;
1923 	}
1924 	return 0;
1925 }
1926 
1927 /**
1928  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1929  * @vsi: the VSI being setup
1930  * @ctxt: VSI context structure
1931  * @enabled_tc: Enabled TCs bitmap
1932  * @is_add: True if called before Add VSI
1933  *
1934  * Setup VSI queue mapping for enabled traffic classes.
1935  **/
1936 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1937 				     struct i40e_vsi_context *ctxt,
1938 				     u8 enabled_tc,
1939 				     bool is_add)
1940 {
1941 	struct i40e_pf *pf = vsi->back;
1942 	u16 num_tc_qps = 0;
1943 	u16 sections = 0;
1944 	u8 netdev_tc = 0;
1945 	u16 numtc = 1;
1946 	u16 qcount;
1947 	u8 offset;
1948 	u16 qmap;
1949 	int i;
1950 
1951 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1952 	offset = 0;
1953 	/* zero out queue mapping, it will get updated on the end of the function */
1954 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1955 
1956 	if (vsi->type == I40E_VSI_MAIN) {
1957 		/* This code helps add more queue to the VSI if we have
1958 		 * more cores than RSS can support, the higher cores will
1959 		 * be served by ATR or other filters. Furthermore, the
1960 		 * non-zero req_queue_pairs says that user requested a new
1961 		 * queue count via ethtool's set_channels, so use this
1962 		 * value for queues distribution across traffic classes
1963 		 */
1964 		if (vsi->req_queue_pairs > 0)
1965 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1966 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1967 			vsi->num_queue_pairs = pf->num_lan_msix;
1968 	}
1969 
1970 	/* Number of queues per enabled TC */
1971 	if (vsi->type == I40E_VSI_MAIN ||
1972 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1973 		num_tc_qps = vsi->num_queue_pairs;
1974 	else
1975 		num_tc_qps = vsi->alloc_queue_pairs;
1976 
1977 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1978 		/* Find numtc from enabled TC bitmap */
1979 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1980 			if (enabled_tc & BIT(i)) /* TC is enabled */
1981 				numtc++;
1982 		}
1983 		if (!numtc) {
1984 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1985 			numtc = 1;
1986 		}
1987 		num_tc_qps = num_tc_qps / numtc;
1988 		num_tc_qps = min_t(int, num_tc_qps,
1989 				   i40e_pf_get_max_q_per_tc(pf));
1990 	}
1991 
1992 	vsi->tc_config.numtc = numtc;
1993 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1994 
1995 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1996 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1997 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1998 
1999 	/* Setup queue offset/count for all TCs for given VSI */
2000 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2001 		/* See if the given TC is enabled for the given VSI */
2002 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2003 			/* TC is enabled */
2004 			int pow, num_qps;
2005 
2006 			switch (vsi->type) {
2007 			case I40E_VSI_MAIN:
2008 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2009 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2010 				    vsi->tc_config.enabled_tc != 1) {
2011 					qcount = min_t(int, pf->alloc_rss_size,
2012 						       num_tc_qps);
2013 					break;
2014 				}
2015 				fallthrough;
2016 			case I40E_VSI_FDIR:
2017 			case I40E_VSI_SRIOV:
2018 			case I40E_VSI_VMDQ2:
2019 			default:
2020 				qcount = num_tc_qps;
2021 				WARN_ON(i != 0);
2022 				break;
2023 			}
2024 			vsi->tc_config.tc_info[i].qoffset = offset;
2025 			vsi->tc_config.tc_info[i].qcount = qcount;
2026 
2027 			/* find the next higher power-of-2 of num queue pairs */
2028 			num_qps = qcount;
2029 			pow = 0;
2030 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2031 				pow++;
2032 				num_qps >>= 1;
2033 			}
2034 
2035 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2036 			qmap =
2037 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2038 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2039 
2040 			offset += qcount;
2041 		} else {
2042 			/* TC is not enabled so set the offset to
2043 			 * default queue and allocate one queue
2044 			 * for the given TC.
2045 			 */
2046 			vsi->tc_config.tc_info[i].qoffset = 0;
2047 			vsi->tc_config.tc_info[i].qcount = 1;
2048 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2049 
2050 			qmap = 0;
2051 		}
2052 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2053 	}
2054 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2055 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2056 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2057 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2058 		vsi->num_queue_pairs = offset;
2059 
2060 	/* Scheduler section valid can only be set for ADD VSI */
2061 	if (is_add) {
2062 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2063 
2064 		ctxt->info.up_enable_bits = enabled_tc;
2065 	}
2066 	if (vsi->type == I40E_VSI_SRIOV) {
2067 		ctxt->info.mapping_flags |=
2068 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2069 		for (i = 0; i < vsi->num_queue_pairs; i++)
2070 			ctxt->info.queue_mapping[i] =
2071 					       cpu_to_le16(vsi->base_queue + i);
2072 	} else {
2073 		ctxt->info.mapping_flags |=
2074 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2075 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2076 	}
2077 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2078 }
2079 
2080 /**
2081  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2082  * @netdev: the netdevice
2083  * @addr: address to add
2084  *
2085  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2086  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2087  */
2088 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2089 {
2090 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2091 	struct i40e_vsi *vsi = np->vsi;
2092 
2093 	if (i40e_add_mac_filter(vsi, addr))
2094 		return 0;
2095 	else
2096 		return -ENOMEM;
2097 }
2098 
2099 /**
2100  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2101  * @netdev: the netdevice
2102  * @addr: address to add
2103  *
2104  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2105  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2106  */
2107 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2108 {
2109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2110 	struct i40e_vsi *vsi = np->vsi;
2111 
2112 	/* Under some circumstances, we might receive a request to delete
2113 	 * our own device address from our uc list. Because we store the
2114 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2115 	 * such requests and not delete our device address from this list.
2116 	 */
2117 	if (ether_addr_equal(addr, netdev->dev_addr))
2118 		return 0;
2119 
2120 	i40e_del_mac_filter(vsi, addr);
2121 
2122 	return 0;
2123 }
2124 
2125 /**
2126  * i40e_set_rx_mode - NDO callback to set the netdev filters
2127  * @netdev: network interface device structure
2128  **/
2129 static void i40e_set_rx_mode(struct net_device *netdev)
2130 {
2131 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2132 	struct i40e_vsi *vsi = np->vsi;
2133 
2134 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2135 
2136 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2137 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2138 
2139 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2140 
2141 	/* check for other flag changes */
2142 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2143 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2144 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2145 	}
2146 }
2147 
2148 /**
2149  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2150  * @vsi: Pointer to VSI struct
2151  * @from: Pointer to list which contains MAC filter entries - changes to
2152  *        those entries needs to be undone.
2153  *
2154  * MAC filter entries from this list were slated for deletion.
2155  **/
2156 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2157 					 struct hlist_head *from)
2158 {
2159 	struct i40e_mac_filter *f;
2160 	struct hlist_node *h;
2161 
2162 	hlist_for_each_entry_safe(f, h, from, hlist) {
2163 		u64 key = i40e_addr_to_hkey(f->macaddr);
2164 
2165 		/* Move the element back into MAC filter list*/
2166 		hlist_del(&f->hlist);
2167 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2168 	}
2169 }
2170 
2171 /**
2172  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2173  * @vsi: Pointer to vsi struct
2174  * @from: Pointer to list which contains MAC filter entries - changes to
2175  *        those entries needs to be undone.
2176  *
2177  * MAC filter entries from this list were slated for addition.
2178  **/
2179 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2180 					 struct hlist_head *from)
2181 {
2182 	struct i40e_new_mac_filter *new;
2183 	struct hlist_node *h;
2184 
2185 	hlist_for_each_entry_safe(new, h, from, hlist) {
2186 		/* We can simply free the wrapper structure */
2187 		hlist_del(&new->hlist);
2188 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2189 		kfree(new);
2190 	}
2191 }
2192 
2193 /**
2194  * i40e_next_filter - Get the next non-broadcast filter from a list
2195  * @next: pointer to filter in list
2196  *
2197  * Returns the next non-broadcast filter in the list. Required so that we
2198  * ignore broadcast filters within the list, since these are not handled via
2199  * the normal firmware update path.
2200  */
2201 static
2202 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2203 {
2204 	hlist_for_each_entry_continue(next, hlist) {
2205 		if (!is_broadcast_ether_addr(next->f->macaddr))
2206 			return next;
2207 	}
2208 
2209 	return NULL;
2210 }
2211 
2212 /**
2213  * i40e_update_filter_state - Update filter state based on return data
2214  * from firmware
2215  * @count: Number of filters added
2216  * @add_list: return data from fw
2217  * @add_head: pointer to first filter in current batch
2218  *
2219  * MAC filter entries from list were slated to be added to device. Returns
2220  * number of successful filters. Note that 0 does NOT mean success!
2221  **/
2222 static int
2223 i40e_update_filter_state(int count,
2224 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2225 			 struct i40e_new_mac_filter *add_head)
2226 {
2227 	int retval = 0;
2228 	int i;
2229 
2230 	for (i = 0; i < count; i++) {
2231 		/* Always check status of each filter. We don't need to check
2232 		 * the firmware return status because we pre-set the filter
2233 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2234 		 * request to the adminq. Thus, if it no longer matches then
2235 		 * we know the filter is active.
2236 		 */
2237 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2238 			add_head->state = I40E_FILTER_FAILED;
2239 		} else {
2240 			add_head->state = I40E_FILTER_ACTIVE;
2241 			retval++;
2242 		}
2243 
2244 		add_head = i40e_next_filter(add_head);
2245 		if (!add_head)
2246 			break;
2247 	}
2248 
2249 	return retval;
2250 }
2251 
2252 /**
2253  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2254  * @vsi: ptr to the VSI
2255  * @vsi_name: name to display in messages
2256  * @list: the list of filters to send to firmware
2257  * @num_del: the number of filters to delete
2258  * @retval: Set to -EIO on failure to delete
2259  *
2260  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2261  * *retval instead of a return value so that success does not force ret_val to
2262  * be set to 0. This ensures that a sequence of calls to this function
2263  * preserve the previous value of *retval on successful delete.
2264  */
2265 static
2266 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2267 			  struct i40e_aqc_remove_macvlan_element_data *list,
2268 			  int num_del, int *retval)
2269 {
2270 	struct i40e_hw *hw = &vsi->back->hw;
2271 	enum i40e_admin_queue_err aq_status;
2272 	i40e_status aq_ret;
2273 
2274 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2275 					   &aq_status);
2276 
2277 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2278 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2279 		*retval = -EIO;
2280 		dev_info(&vsi->back->pdev->dev,
2281 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2282 			 vsi_name, i40e_stat_str(hw, aq_ret),
2283 			 i40e_aq_str(hw, aq_status));
2284 	}
2285 }
2286 
2287 /**
2288  * i40e_aqc_add_filters - Request firmware to add a set of filters
2289  * @vsi: ptr to the VSI
2290  * @vsi_name: name to display in messages
2291  * @list: the list of filters to send to firmware
2292  * @add_head: Position in the add hlist
2293  * @num_add: the number of filters to add
2294  *
2295  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2296  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2297  * space for more filters.
2298  */
2299 static
2300 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2301 			  struct i40e_aqc_add_macvlan_element_data *list,
2302 			  struct i40e_new_mac_filter *add_head,
2303 			  int num_add)
2304 {
2305 	struct i40e_hw *hw = &vsi->back->hw;
2306 	enum i40e_admin_queue_err aq_status;
2307 	int fcnt;
2308 
2309 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2310 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2311 
2312 	if (fcnt != num_add) {
2313 		if (vsi->type == I40E_VSI_MAIN) {
2314 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2315 			dev_warn(&vsi->back->pdev->dev,
2316 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2317 				 i40e_aq_str(hw, aq_status), vsi_name);
2318 		} else if (vsi->type == I40E_VSI_SRIOV ||
2319 			   vsi->type == I40E_VSI_VMDQ1 ||
2320 			   vsi->type == I40E_VSI_VMDQ2) {
2321 			dev_warn(&vsi->back->pdev->dev,
2322 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2323 				 i40e_aq_str(hw, aq_status), vsi_name,
2324 					     vsi_name);
2325 		} else {
2326 			dev_warn(&vsi->back->pdev->dev,
2327 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2328 				 i40e_aq_str(hw, aq_status), vsi_name,
2329 					     vsi->type);
2330 		}
2331 	}
2332 }
2333 
2334 /**
2335  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2336  * @vsi: pointer to the VSI
2337  * @vsi_name: the VSI name
2338  * @f: filter data
2339  *
2340  * This function sets or clears the promiscuous broadcast flags for VLAN
2341  * filters in order to properly receive broadcast frames. Assumes that only
2342  * broadcast filters are passed.
2343  *
2344  * Returns status indicating success or failure;
2345  **/
2346 static i40e_status
2347 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2348 			  struct i40e_mac_filter *f)
2349 {
2350 	bool enable = f->state == I40E_FILTER_NEW;
2351 	struct i40e_hw *hw = &vsi->back->hw;
2352 	i40e_status aq_ret;
2353 
2354 	if (f->vlan == I40E_VLAN_ANY) {
2355 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2356 						   vsi->seid,
2357 						   enable,
2358 						   NULL);
2359 	} else {
2360 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2361 							    vsi->seid,
2362 							    enable,
2363 							    f->vlan,
2364 							    NULL);
2365 	}
2366 
2367 	if (aq_ret) {
2368 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2369 		dev_warn(&vsi->back->pdev->dev,
2370 			 "Error %s, forcing overflow promiscuous on %s\n",
2371 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2372 			 vsi_name);
2373 	}
2374 
2375 	return aq_ret;
2376 }
2377 
2378 /**
2379  * i40e_set_promiscuous - set promiscuous mode
2380  * @pf: board private structure
2381  * @promisc: promisc on or off
2382  *
2383  * There are different ways of setting promiscuous mode on a PF depending on
2384  * what state/environment we're in.  This identifies and sets it appropriately.
2385  * Returns 0 on success.
2386  **/
2387 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2388 {
2389 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2390 	struct i40e_hw *hw = &pf->hw;
2391 	i40e_status aq_ret;
2392 
2393 	if (vsi->type == I40E_VSI_MAIN &&
2394 	    pf->lan_veb != I40E_NO_VEB &&
2395 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2396 		/* set defport ON for Main VSI instead of true promisc
2397 		 * this way we will get all unicast/multicast and VLAN
2398 		 * promisc behavior but will not get VF or VMDq traffic
2399 		 * replicated on the Main VSI.
2400 		 */
2401 		if (promisc)
2402 			aq_ret = i40e_aq_set_default_vsi(hw,
2403 							 vsi->seid,
2404 							 NULL);
2405 		else
2406 			aq_ret = i40e_aq_clear_default_vsi(hw,
2407 							   vsi->seid,
2408 							   NULL);
2409 		if (aq_ret) {
2410 			dev_info(&pf->pdev->dev,
2411 				 "Set default VSI failed, err %s, aq_err %s\n",
2412 				 i40e_stat_str(hw, aq_ret),
2413 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2414 		}
2415 	} else {
2416 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2417 						  hw,
2418 						  vsi->seid,
2419 						  promisc, NULL,
2420 						  true);
2421 		if (aq_ret) {
2422 			dev_info(&pf->pdev->dev,
2423 				 "set unicast promisc failed, err %s, aq_err %s\n",
2424 				 i40e_stat_str(hw, aq_ret),
2425 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2426 		}
2427 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2428 						  hw,
2429 						  vsi->seid,
2430 						  promisc, NULL);
2431 		if (aq_ret) {
2432 			dev_info(&pf->pdev->dev,
2433 				 "set multicast promisc failed, err %s, aq_err %s\n",
2434 				 i40e_stat_str(hw, aq_ret),
2435 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2436 		}
2437 	}
2438 
2439 	if (!aq_ret)
2440 		pf->cur_promisc = promisc;
2441 
2442 	return aq_ret;
2443 }
2444 
2445 /**
2446  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2447  * @vsi: ptr to the VSI
2448  *
2449  * Push any outstanding VSI filter changes through the AdminQ.
2450  *
2451  * Returns 0 or error value
2452  **/
2453 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2454 {
2455 	struct hlist_head tmp_add_list, tmp_del_list;
2456 	struct i40e_mac_filter *f;
2457 	struct i40e_new_mac_filter *new, *add_head = NULL;
2458 	struct i40e_hw *hw = &vsi->back->hw;
2459 	bool old_overflow, new_overflow;
2460 	unsigned int failed_filters = 0;
2461 	unsigned int vlan_filters = 0;
2462 	char vsi_name[16] = "PF";
2463 	int filter_list_len = 0;
2464 	i40e_status aq_ret = 0;
2465 	u32 changed_flags = 0;
2466 	struct hlist_node *h;
2467 	struct i40e_pf *pf;
2468 	int num_add = 0;
2469 	int num_del = 0;
2470 	int retval = 0;
2471 	u16 cmd_flags;
2472 	int list_size;
2473 	int bkt;
2474 
2475 	/* empty array typed pointers, kcalloc later */
2476 	struct i40e_aqc_add_macvlan_element_data *add_list;
2477 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2478 
2479 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2480 		usleep_range(1000, 2000);
2481 	pf = vsi->back;
2482 
2483 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2484 
2485 	if (vsi->netdev) {
2486 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2487 		vsi->current_netdev_flags = vsi->netdev->flags;
2488 	}
2489 
2490 	INIT_HLIST_HEAD(&tmp_add_list);
2491 	INIT_HLIST_HEAD(&tmp_del_list);
2492 
2493 	if (vsi->type == I40E_VSI_SRIOV)
2494 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2495 	else if (vsi->type != I40E_VSI_MAIN)
2496 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2497 
2498 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2499 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2500 
2501 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2502 		/* Create a list of filters to delete. */
2503 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2504 			if (f->state == I40E_FILTER_REMOVE) {
2505 				/* Move the element into temporary del_list */
2506 				hash_del(&f->hlist);
2507 				hlist_add_head(&f->hlist, &tmp_del_list);
2508 
2509 				/* Avoid counting removed filters */
2510 				continue;
2511 			}
2512 			if (f->state == I40E_FILTER_NEW) {
2513 				/* Create a temporary i40e_new_mac_filter */
2514 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2515 				if (!new)
2516 					goto err_no_memory_locked;
2517 
2518 				/* Store pointer to the real filter */
2519 				new->f = f;
2520 				new->state = f->state;
2521 
2522 				/* Add it to the hash list */
2523 				hlist_add_head(&new->hlist, &tmp_add_list);
2524 			}
2525 
2526 			/* Count the number of active (current and new) VLAN
2527 			 * filters we have now. Does not count filters which
2528 			 * are marked for deletion.
2529 			 */
2530 			if (f->vlan > 0)
2531 				vlan_filters++;
2532 		}
2533 
2534 		if (vsi->type != I40E_VSI_SRIOV)
2535 			retval = i40e_correct_mac_vlan_filters
2536 				(vsi, &tmp_add_list, &tmp_del_list,
2537 				 vlan_filters);
2538 		else
2539 			retval = i40e_correct_vf_mac_vlan_filters
2540 				(vsi, &tmp_add_list, &tmp_del_list,
2541 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2542 
2543 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2544 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2545 
2546 		if (retval)
2547 			goto err_no_memory_locked;
2548 
2549 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2550 	}
2551 
2552 	/* Now process 'del_list' outside the lock */
2553 	if (!hlist_empty(&tmp_del_list)) {
2554 		filter_list_len = hw->aq.asq_buf_size /
2555 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2556 		list_size = filter_list_len *
2557 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2558 		del_list = kzalloc(list_size, GFP_ATOMIC);
2559 		if (!del_list)
2560 			goto err_no_memory;
2561 
2562 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2563 			cmd_flags = 0;
2564 
2565 			/* handle broadcast filters by updating the broadcast
2566 			 * promiscuous flag and release filter list.
2567 			 */
2568 			if (is_broadcast_ether_addr(f->macaddr)) {
2569 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2570 
2571 				hlist_del(&f->hlist);
2572 				kfree(f);
2573 				continue;
2574 			}
2575 
2576 			/* add to delete list */
2577 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2578 			if (f->vlan == I40E_VLAN_ANY) {
2579 				del_list[num_del].vlan_tag = 0;
2580 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2581 			} else {
2582 				del_list[num_del].vlan_tag =
2583 					cpu_to_le16((u16)(f->vlan));
2584 			}
2585 
2586 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2587 			del_list[num_del].flags = cmd_flags;
2588 			num_del++;
2589 
2590 			/* flush a full buffer */
2591 			if (num_del == filter_list_len) {
2592 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2593 						     num_del, &retval);
2594 				memset(del_list, 0, list_size);
2595 				num_del = 0;
2596 			}
2597 			/* Release memory for MAC filter entries which were
2598 			 * synced up with HW.
2599 			 */
2600 			hlist_del(&f->hlist);
2601 			kfree(f);
2602 		}
2603 
2604 		if (num_del) {
2605 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2606 					     num_del, &retval);
2607 		}
2608 
2609 		kfree(del_list);
2610 		del_list = NULL;
2611 	}
2612 
2613 	if (!hlist_empty(&tmp_add_list)) {
2614 		/* Do all the adds now. */
2615 		filter_list_len = hw->aq.asq_buf_size /
2616 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2617 		list_size = filter_list_len *
2618 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2619 		add_list = kzalloc(list_size, GFP_ATOMIC);
2620 		if (!add_list)
2621 			goto err_no_memory;
2622 
2623 		num_add = 0;
2624 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2625 			/* handle broadcast filters by updating the broadcast
2626 			 * promiscuous flag instead of adding a MAC filter.
2627 			 */
2628 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2629 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2630 							      new->f))
2631 					new->state = I40E_FILTER_FAILED;
2632 				else
2633 					new->state = I40E_FILTER_ACTIVE;
2634 				continue;
2635 			}
2636 
2637 			/* add to add array */
2638 			if (num_add == 0)
2639 				add_head = new;
2640 			cmd_flags = 0;
2641 			ether_addr_copy(add_list[num_add].mac_addr,
2642 					new->f->macaddr);
2643 			if (new->f->vlan == I40E_VLAN_ANY) {
2644 				add_list[num_add].vlan_tag = 0;
2645 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2646 			} else {
2647 				add_list[num_add].vlan_tag =
2648 					cpu_to_le16((u16)(new->f->vlan));
2649 			}
2650 			add_list[num_add].queue_number = 0;
2651 			/* set invalid match method for later detection */
2652 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2653 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2654 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2655 			num_add++;
2656 
2657 			/* flush a full buffer */
2658 			if (num_add == filter_list_len) {
2659 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2660 						     add_head, num_add);
2661 				memset(add_list, 0, list_size);
2662 				num_add = 0;
2663 			}
2664 		}
2665 		if (num_add) {
2666 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2667 					     num_add);
2668 		}
2669 		/* Now move all of the filters from the temp add list back to
2670 		 * the VSI's list.
2671 		 */
2672 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2673 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2674 			/* Only update the state if we're still NEW */
2675 			if (new->f->state == I40E_FILTER_NEW)
2676 				new->f->state = new->state;
2677 			hlist_del(&new->hlist);
2678 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2679 			kfree(new);
2680 		}
2681 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2682 		kfree(add_list);
2683 		add_list = NULL;
2684 	}
2685 
2686 	/* Determine the number of active and failed filters. */
2687 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2688 	vsi->active_filters = 0;
2689 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2690 		if (f->state == I40E_FILTER_ACTIVE)
2691 			vsi->active_filters++;
2692 		else if (f->state == I40E_FILTER_FAILED)
2693 			failed_filters++;
2694 	}
2695 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2696 
2697 	/* Check if we are able to exit overflow promiscuous mode. We can
2698 	 * safely exit if we didn't just enter, we no longer have any failed
2699 	 * filters, and we have reduced filters below the threshold value.
2700 	 */
2701 	if (old_overflow && !failed_filters &&
2702 	    vsi->active_filters < vsi->promisc_threshold) {
2703 		dev_info(&pf->pdev->dev,
2704 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2705 			 vsi_name);
2706 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2707 		vsi->promisc_threshold = 0;
2708 	}
2709 
2710 	/* if the VF is not trusted do not do promisc */
2711 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2712 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2713 		goto out;
2714 	}
2715 
2716 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2717 
2718 	/* If we are entering overflow promiscuous, we need to calculate a new
2719 	 * threshold for when we are safe to exit
2720 	 */
2721 	if (!old_overflow && new_overflow)
2722 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2723 
2724 	/* check for changes in promiscuous modes */
2725 	if (changed_flags & IFF_ALLMULTI) {
2726 		bool cur_multipromisc;
2727 
2728 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2729 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2730 							       vsi->seid,
2731 							       cur_multipromisc,
2732 							       NULL);
2733 		if (aq_ret) {
2734 			retval = i40e_aq_rc_to_posix(aq_ret,
2735 						     hw->aq.asq_last_status);
2736 			dev_info(&pf->pdev->dev,
2737 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2738 				 vsi_name,
2739 				 i40e_stat_str(hw, aq_ret),
2740 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2741 		} else {
2742 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2743 				 cur_multipromisc ? "entering" : "leaving");
2744 		}
2745 	}
2746 
2747 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2748 		bool cur_promisc;
2749 
2750 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2751 			       new_overflow);
2752 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2753 		if (aq_ret) {
2754 			retval = i40e_aq_rc_to_posix(aq_ret,
2755 						     hw->aq.asq_last_status);
2756 			dev_info(&pf->pdev->dev,
2757 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2758 				 cur_promisc ? "on" : "off",
2759 				 vsi_name,
2760 				 i40e_stat_str(hw, aq_ret),
2761 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2762 		}
2763 	}
2764 out:
2765 	/* if something went wrong then set the changed flag so we try again */
2766 	if (retval)
2767 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2768 
2769 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2770 	return retval;
2771 
2772 err_no_memory:
2773 	/* Restore elements on the temporary add and delete lists */
2774 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2775 err_no_memory_locked:
2776 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2777 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2778 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2779 
2780 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2781 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2782 	return -ENOMEM;
2783 }
2784 
2785 /**
2786  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2787  * @pf: board private structure
2788  **/
2789 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2790 {
2791 	int v;
2792 
2793 	if (!pf)
2794 		return;
2795 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2796 		return;
2797 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2798 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2799 		return;
2800 	}
2801 
2802 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2803 		if (pf->vsi[v] &&
2804 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2805 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2806 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2807 
2808 			if (ret) {
2809 				/* come back and try again later */
2810 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2811 					pf->state);
2812 				break;
2813 			}
2814 		}
2815 	}
2816 }
2817 
2818 /**
2819  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2820  * @vsi: the vsi
2821  **/
2822 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2823 {
2824 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2825 		return I40E_RXBUFFER_2048;
2826 	else
2827 		return I40E_RXBUFFER_3072;
2828 }
2829 
2830 /**
2831  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2832  * @netdev: network interface device structure
2833  * @new_mtu: new value for maximum frame size
2834  *
2835  * Returns 0 on success, negative on failure
2836  **/
2837 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2838 {
2839 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2840 	struct i40e_vsi *vsi = np->vsi;
2841 	struct i40e_pf *pf = vsi->back;
2842 
2843 	if (i40e_enabled_xdp_vsi(vsi)) {
2844 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2845 
2846 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2847 			return -EINVAL;
2848 	}
2849 
2850 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2851 		   netdev->mtu, new_mtu);
2852 	netdev->mtu = new_mtu;
2853 	if (netif_running(netdev))
2854 		i40e_vsi_reinit_locked(vsi);
2855 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2856 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2857 	return 0;
2858 }
2859 
2860 /**
2861  * i40e_ioctl - Access the hwtstamp interface
2862  * @netdev: network interface device structure
2863  * @ifr: interface request data
2864  * @cmd: ioctl command
2865  **/
2866 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2867 {
2868 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2869 	struct i40e_pf *pf = np->vsi->back;
2870 
2871 	switch (cmd) {
2872 	case SIOCGHWTSTAMP:
2873 		return i40e_ptp_get_ts_config(pf, ifr);
2874 	case SIOCSHWTSTAMP:
2875 		return i40e_ptp_set_ts_config(pf, ifr);
2876 	default:
2877 		return -EOPNOTSUPP;
2878 	}
2879 }
2880 
2881 /**
2882  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2883  * @vsi: the vsi being adjusted
2884  **/
2885 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2886 {
2887 	struct i40e_vsi_context ctxt;
2888 	i40e_status ret;
2889 
2890 	/* Don't modify stripping options if a port VLAN is active */
2891 	if (vsi->info.pvid)
2892 		return;
2893 
2894 	if ((vsi->info.valid_sections &
2895 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2896 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2897 		return;  /* already enabled */
2898 
2899 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2900 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2901 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2902 
2903 	ctxt.seid = vsi->seid;
2904 	ctxt.info = vsi->info;
2905 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2906 	if (ret) {
2907 		dev_info(&vsi->back->pdev->dev,
2908 			 "update vlan stripping failed, err %s aq_err %s\n",
2909 			 i40e_stat_str(&vsi->back->hw, ret),
2910 			 i40e_aq_str(&vsi->back->hw,
2911 				     vsi->back->hw.aq.asq_last_status));
2912 	}
2913 }
2914 
2915 /**
2916  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2917  * @vsi: the vsi being adjusted
2918  **/
2919 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2920 {
2921 	struct i40e_vsi_context ctxt;
2922 	i40e_status ret;
2923 
2924 	/* Don't modify stripping options if a port VLAN is active */
2925 	if (vsi->info.pvid)
2926 		return;
2927 
2928 	if ((vsi->info.valid_sections &
2929 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2930 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2931 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2932 		return;  /* already disabled */
2933 
2934 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2935 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2936 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2937 
2938 	ctxt.seid = vsi->seid;
2939 	ctxt.info = vsi->info;
2940 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2941 	if (ret) {
2942 		dev_info(&vsi->back->pdev->dev,
2943 			 "update vlan stripping failed, err %s aq_err %s\n",
2944 			 i40e_stat_str(&vsi->back->hw, ret),
2945 			 i40e_aq_str(&vsi->back->hw,
2946 				     vsi->back->hw.aq.asq_last_status));
2947 	}
2948 }
2949 
2950 /**
2951  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2952  * @vsi: the vsi being configured
2953  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2954  *
2955  * This is a helper function for adding a new MAC/VLAN filter with the
2956  * specified VLAN for each existing MAC address already in the hash table.
2957  * This function does *not* perform any accounting to update filters based on
2958  * VLAN mode.
2959  *
2960  * NOTE: this function expects to be called while under the
2961  * mac_filter_hash_lock
2962  **/
2963 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2964 {
2965 	struct i40e_mac_filter *f, *add_f;
2966 	struct hlist_node *h;
2967 	int bkt;
2968 
2969 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2970 		/* If we're asked to add a filter that has been marked for
2971 		 * removal, it is safe to simply restore it to active state.
2972 		 * __i40e_del_filter will have simply deleted any filters which
2973 		 * were previously marked NEW or FAILED, so if it is currently
2974 		 * marked REMOVE it must have previously been ACTIVE. Since we
2975 		 * haven't yet run the sync filters task, just restore this
2976 		 * filter to the ACTIVE state so that the sync task leaves it
2977 		 * in place.
2978 		 */
2979 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
2980 			f->state = I40E_FILTER_ACTIVE;
2981 			continue;
2982 		} else if (f->state == I40E_FILTER_REMOVE) {
2983 			continue;
2984 		}
2985 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2986 		if (!add_f) {
2987 			dev_info(&vsi->back->pdev->dev,
2988 				 "Could not add vlan filter %d for %pM\n",
2989 				 vid, f->macaddr);
2990 			return -ENOMEM;
2991 		}
2992 	}
2993 
2994 	return 0;
2995 }
2996 
2997 /**
2998  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2999  * @vsi: the VSI being configured
3000  * @vid: VLAN id to be added
3001  **/
3002 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3003 {
3004 	int err;
3005 
3006 	if (vsi->info.pvid)
3007 		return -EINVAL;
3008 
3009 	/* The network stack will attempt to add VID=0, with the intention to
3010 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3011 	 * these packets by default when configured to receive untagged
3012 	 * packets, so we don't need to add a filter for this case.
3013 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3014 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3015 	 * Thus, we do not want to actually add a filter for VID=0
3016 	 */
3017 	if (!vid)
3018 		return 0;
3019 
3020 	/* Locked once because all functions invoked below iterates list*/
3021 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3022 	err = i40e_add_vlan_all_mac(vsi, vid);
3023 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3024 	if (err)
3025 		return err;
3026 
3027 	/* schedule our worker thread which will take care of
3028 	 * applying the new filter changes
3029 	 */
3030 	i40e_service_event_schedule(vsi->back);
3031 	return 0;
3032 }
3033 
3034 /**
3035  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3036  * @vsi: the vsi being configured
3037  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3038  *
3039  * This function should be used to remove all VLAN filters which match the
3040  * given VID. It does not schedule the service event and does not take the
3041  * mac_filter_hash_lock so it may be combined with other operations under
3042  * a single invocation of the mac_filter_hash_lock.
3043  *
3044  * NOTE: this function expects to be called while under the
3045  * mac_filter_hash_lock
3046  */
3047 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3048 {
3049 	struct i40e_mac_filter *f;
3050 	struct hlist_node *h;
3051 	int bkt;
3052 
3053 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3054 		if (f->vlan == vid)
3055 			__i40e_del_filter(vsi, f);
3056 	}
3057 }
3058 
3059 /**
3060  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3061  * @vsi: the VSI being configured
3062  * @vid: VLAN id to be removed
3063  **/
3064 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3065 {
3066 	if (!vid || vsi->info.pvid)
3067 		return;
3068 
3069 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3070 	i40e_rm_vlan_all_mac(vsi, vid);
3071 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3072 
3073 	/* schedule our worker thread which will take care of
3074 	 * applying the new filter changes
3075 	 */
3076 	i40e_service_event_schedule(vsi->back);
3077 }
3078 
3079 /**
3080  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3081  * @netdev: network interface to be adjusted
3082  * @proto: unused protocol value
3083  * @vid: vlan id to be added
3084  *
3085  * net_device_ops implementation for adding vlan ids
3086  **/
3087 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3088 				__always_unused __be16 proto, u16 vid)
3089 {
3090 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3091 	struct i40e_vsi *vsi = np->vsi;
3092 	int ret = 0;
3093 
3094 	if (vid >= VLAN_N_VID)
3095 		return -EINVAL;
3096 
3097 	ret = i40e_vsi_add_vlan(vsi, vid);
3098 	if (!ret)
3099 		set_bit(vid, vsi->active_vlans);
3100 
3101 	return ret;
3102 }
3103 
3104 /**
3105  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3106  * @netdev: network interface to be adjusted
3107  * @proto: unused protocol value
3108  * @vid: vlan id to be added
3109  **/
3110 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3111 				    __always_unused __be16 proto, u16 vid)
3112 {
3113 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3114 	struct i40e_vsi *vsi = np->vsi;
3115 
3116 	if (vid >= VLAN_N_VID)
3117 		return;
3118 	set_bit(vid, vsi->active_vlans);
3119 }
3120 
3121 /**
3122  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3123  * @netdev: network interface to be adjusted
3124  * @proto: unused protocol value
3125  * @vid: vlan id to be removed
3126  *
3127  * net_device_ops implementation for removing vlan ids
3128  **/
3129 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3130 				 __always_unused __be16 proto, u16 vid)
3131 {
3132 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3133 	struct i40e_vsi *vsi = np->vsi;
3134 
3135 	/* return code is ignored as there is nothing a user
3136 	 * can do about failure to remove and a log message was
3137 	 * already printed from the other function
3138 	 */
3139 	i40e_vsi_kill_vlan(vsi, vid);
3140 
3141 	clear_bit(vid, vsi->active_vlans);
3142 
3143 	return 0;
3144 }
3145 
3146 /**
3147  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3148  * @vsi: the vsi being brought back up
3149  **/
3150 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3151 {
3152 	u16 vid;
3153 
3154 	if (!vsi->netdev)
3155 		return;
3156 
3157 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3158 		i40e_vlan_stripping_enable(vsi);
3159 	else
3160 		i40e_vlan_stripping_disable(vsi);
3161 
3162 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3163 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3164 					vid);
3165 }
3166 
3167 /**
3168  * i40e_vsi_add_pvid - Add pvid for the VSI
3169  * @vsi: the vsi being adjusted
3170  * @vid: the vlan id to set as a PVID
3171  **/
3172 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3173 {
3174 	struct i40e_vsi_context ctxt;
3175 	i40e_status ret;
3176 
3177 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3178 	vsi->info.pvid = cpu_to_le16(vid);
3179 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3180 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3181 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3182 
3183 	ctxt.seid = vsi->seid;
3184 	ctxt.info = vsi->info;
3185 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3186 	if (ret) {
3187 		dev_info(&vsi->back->pdev->dev,
3188 			 "add pvid failed, err %s aq_err %s\n",
3189 			 i40e_stat_str(&vsi->back->hw, ret),
3190 			 i40e_aq_str(&vsi->back->hw,
3191 				     vsi->back->hw.aq.asq_last_status));
3192 		return -ENOENT;
3193 	}
3194 
3195 	return 0;
3196 }
3197 
3198 /**
3199  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3200  * @vsi: the vsi being adjusted
3201  *
3202  * Just use the vlan_rx_register() service to put it back to normal
3203  **/
3204 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3205 {
3206 	vsi->info.pvid = 0;
3207 
3208 	i40e_vlan_stripping_disable(vsi);
3209 }
3210 
3211 /**
3212  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3213  * @vsi: ptr to the VSI
3214  *
3215  * If this function returns with an error, then it's possible one or
3216  * more of the rings is populated (while the rest are not).  It is the
3217  * callers duty to clean those orphaned rings.
3218  *
3219  * Return 0 on success, negative on failure
3220  **/
3221 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3222 {
3223 	int i, err = 0;
3224 
3225 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3226 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3227 
3228 	if (!i40e_enabled_xdp_vsi(vsi))
3229 		return err;
3230 
3231 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3232 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3233 
3234 	return err;
3235 }
3236 
3237 /**
3238  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3239  * @vsi: ptr to the VSI
3240  *
3241  * Free VSI's transmit software resources
3242  **/
3243 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3244 {
3245 	int i;
3246 
3247 	if (vsi->tx_rings) {
3248 		for (i = 0; i < vsi->num_queue_pairs; i++)
3249 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3250 				i40e_free_tx_resources(vsi->tx_rings[i]);
3251 	}
3252 
3253 	if (vsi->xdp_rings) {
3254 		for (i = 0; i < vsi->num_queue_pairs; i++)
3255 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3256 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3257 	}
3258 }
3259 
3260 /**
3261  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3262  * @vsi: ptr to the VSI
3263  *
3264  * If this function returns with an error, then it's possible one or
3265  * more of the rings is populated (while the rest are not).  It is the
3266  * callers duty to clean those orphaned rings.
3267  *
3268  * Return 0 on success, negative on failure
3269  **/
3270 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3271 {
3272 	int i, err = 0;
3273 
3274 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3275 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3276 	return err;
3277 }
3278 
3279 /**
3280  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3281  * @vsi: ptr to the VSI
3282  *
3283  * Free all receive software resources
3284  **/
3285 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3286 {
3287 	int i;
3288 
3289 	if (!vsi->rx_rings)
3290 		return;
3291 
3292 	for (i = 0; i < vsi->num_queue_pairs; i++)
3293 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3294 			i40e_free_rx_resources(vsi->rx_rings[i]);
3295 }
3296 
3297 /**
3298  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3299  * @ring: The Tx ring to configure
3300  *
3301  * This enables/disables XPS for a given Tx descriptor ring
3302  * based on the TCs enabled for the VSI that ring belongs to.
3303  **/
3304 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3305 {
3306 	int cpu;
3307 
3308 	if (!ring->q_vector || !ring->netdev || ring->ch)
3309 		return;
3310 
3311 	/* We only initialize XPS once, so as not to overwrite user settings */
3312 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3313 		return;
3314 
3315 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3316 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3317 			    ring->queue_index);
3318 }
3319 
3320 /**
3321  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3322  * @ring: The Tx or Rx ring
3323  *
3324  * Returns the AF_XDP buffer pool or NULL.
3325  **/
3326 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3327 {
3328 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3329 	int qid = ring->queue_index;
3330 
3331 	if (ring_is_xdp(ring))
3332 		qid -= ring->vsi->alloc_queue_pairs;
3333 
3334 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3335 		return NULL;
3336 
3337 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3338 }
3339 
3340 /**
3341  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3342  * @ring: The Tx ring to configure
3343  *
3344  * Configure the Tx descriptor ring in the HMC context.
3345  **/
3346 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3347 {
3348 	struct i40e_vsi *vsi = ring->vsi;
3349 	u16 pf_q = vsi->base_queue + ring->queue_index;
3350 	struct i40e_hw *hw = &vsi->back->hw;
3351 	struct i40e_hmc_obj_txq tx_ctx;
3352 	i40e_status err = 0;
3353 	u32 qtx_ctl = 0;
3354 
3355 	if (ring_is_xdp(ring))
3356 		ring->xsk_pool = i40e_xsk_pool(ring);
3357 
3358 	/* some ATR related tx ring init */
3359 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3360 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3361 		ring->atr_count = 0;
3362 	} else {
3363 		ring->atr_sample_rate = 0;
3364 	}
3365 
3366 	/* configure XPS */
3367 	i40e_config_xps_tx_ring(ring);
3368 
3369 	/* clear the context structure first */
3370 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3371 
3372 	tx_ctx.new_context = 1;
3373 	tx_ctx.base = (ring->dma / 128);
3374 	tx_ctx.qlen = ring->count;
3375 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3376 					       I40E_FLAG_FD_ATR_ENABLED));
3377 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3378 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3379 	if (vsi->type != I40E_VSI_FDIR)
3380 		tx_ctx.head_wb_ena = 1;
3381 	tx_ctx.head_wb_addr = ring->dma +
3382 			      (ring->count * sizeof(struct i40e_tx_desc));
3383 
3384 	/* As part of VSI creation/update, FW allocates certain
3385 	 * Tx arbitration queue sets for each TC enabled for
3386 	 * the VSI. The FW returns the handles to these queue
3387 	 * sets as part of the response buffer to Add VSI,
3388 	 * Update VSI, etc. AQ commands. It is expected that
3389 	 * these queue set handles be associated with the Tx
3390 	 * queues by the driver as part of the TX queue context
3391 	 * initialization. This has to be done regardless of
3392 	 * DCB as by default everything is mapped to TC0.
3393 	 */
3394 
3395 	if (ring->ch)
3396 		tx_ctx.rdylist =
3397 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3398 
3399 	else
3400 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3401 
3402 	tx_ctx.rdylist_act = 0;
3403 
3404 	/* clear the context in the HMC */
3405 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3406 	if (err) {
3407 		dev_info(&vsi->back->pdev->dev,
3408 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3409 			 ring->queue_index, pf_q, err);
3410 		return -ENOMEM;
3411 	}
3412 
3413 	/* set the context in the HMC */
3414 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3415 	if (err) {
3416 		dev_info(&vsi->back->pdev->dev,
3417 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3418 			 ring->queue_index, pf_q, err);
3419 		return -ENOMEM;
3420 	}
3421 
3422 	/* Now associate this queue with this PCI function */
3423 	if (ring->ch) {
3424 		if (ring->ch->type == I40E_VSI_VMDQ2)
3425 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3426 		else
3427 			return -EINVAL;
3428 
3429 		qtx_ctl |= (ring->ch->vsi_number <<
3430 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3431 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3432 	} else {
3433 		if (vsi->type == I40E_VSI_VMDQ2) {
3434 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3435 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3436 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3437 		} else {
3438 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3439 		}
3440 	}
3441 
3442 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3443 		    I40E_QTX_CTL_PF_INDX_MASK);
3444 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3445 	i40e_flush(hw);
3446 
3447 	/* cache tail off for easier writes later */
3448 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3449 
3450 	return 0;
3451 }
3452 
3453 /**
3454  * i40e_rx_offset - Return expected offset into page to access data
3455  * @rx_ring: Ring we are requesting offset of
3456  *
3457  * Returns the offset value for ring into the data buffer.
3458  */
3459 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3460 {
3461 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3462 }
3463 
3464 /**
3465  * i40e_configure_rx_ring - Configure a receive ring context
3466  * @ring: The Rx ring to configure
3467  *
3468  * Configure the Rx descriptor ring in the HMC context.
3469  **/
3470 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3471 {
3472 	struct i40e_vsi *vsi = ring->vsi;
3473 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3474 	u16 pf_q = vsi->base_queue + ring->queue_index;
3475 	struct i40e_hw *hw = &vsi->back->hw;
3476 	struct i40e_hmc_obj_rxq rx_ctx;
3477 	i40e_status err = 0;
3478 	bool ok;
3479 	int ret;
3480 
3481 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3482 
3483 	/* clear the context structure first */
3484 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3485 
3486 	if (ring->vsi->type == I40E_VSI_MAIN)
3487 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3488 
3489 	kfree(ring->rx_bi);
3490 	ring->xsk_pool = i40e_xsk_pool(ring);
3491 	if (ring->xsk_pool) {
3492 		ret = i40e_alloc_rx_bi_zc(ring);
3493 		if (ret)
3494 			return ret;
3495 		ring->rx_buf_len =
3496 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3497 		/* For AF_XDP ZC, we disallow packets to span on
3498 		 * multiple buffers, thus letting us skip that
3499 		 * handling in the fast-path.
3500 		 */
3501 		chain_len = 1;
3502 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3503 						 MEM_TYPE_XSK_BUFF_POOL,
3504 						 NULL);
3505 		if (ret)
3506 			return ret;
3507 		dev_info(&vsi->back->pdev->dev,
3508 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3509 			 ring->queue_index);
3510 
3511 	} else {
3512 		ret = i40e_alloc_rx_bi(ring);
3513 		if (ret)
3514 			return ret;
3515 		ring->rx_buf_len = vsi->rx_buf_len;
3516 		if (ring->vsi->type == I40E_VSI_MAIN) {
3517 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3518 							 MEM_TYPE_PAGE_SHARED,
3519 							 NULL);
3520 			if (ret)
3521 				return ret;
3522 		}
3523 	}
3524 
3525 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3526 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3527 
3528 	rx_ctx.base = (ring->dma / 128);
3529 	rx_ctx.qlen = ring->count;
3530 
3531 	/* use 16 byte descriptors */
3532 	rx_ctx.dsize = 0;
3533 
3534 	/* descriptor type is always zero
3535 	 * rx_ctx.dtype = 0;
3536 	 */
3537 	rx_ctx.hsplit_0 = 0;
3538 
3539 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3540 	if (hw->revision_id == 0)
3541 		rx_ctx.lrxqthresh = 0;
3542 	else
3543 		rx_ctx.lrxqthresh = 1;
3544 	rx_ctx.crcstrip = 1;
3545 	rx_ctx.l2tsel = 1;
3546 	/* this controls whether VLAN is stripped from inner headers */
3547 	rx_ctx.showiv = 0;
3548 	/* set the prefena field to 1 because the manual says to */
3549 	rx_ctx.prefena = 1;
3550 
3551 	/* clear the context in the HMC */
3552 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3553 	if (err) {
3554 		dev_info(&vsi->back->pdev->dev,
3555 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3556 			 ring->queue_index, pf_q, err);
3557 		return -ENOMEM;
3558 	}
3559 
3560 	/* set the context in the HMC */
3561 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3562 	if (err) {
3563 		dev_info(&vsi->back->pdev->dev,
3564 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3565 			 ring->queue_index, pf_q, err);
3566 		return -ENOMEM;
3567 	}
3568 
3569 	/* configure Rx buffer alignment */
3570 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3571 		clear_ring_build_skb_enabled(ring);
3572 	else
3573 		set_ring_build_skb_enabled(ring);
3574 
3575 	ring->rx_offset = i40e_rx_offset(ring);
3576 
3577 	/* cache tail for quicker writes, and clear the reg before use */
3578 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3579 	writel(0, ring->tail);
3580 
3581 	if (ring->xsk_pool) {
3582 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3583 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3584 	} else {
3585 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3586 	}
3587 	if (!ok) {
3588 		/* Log this in case the user has forgotten to give the kernel
3589 		 * any buffers, even later in the application.
3590 		 */
3591 		dev_info(&vsi->back->pdev->dev,
3592 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3593 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3594 			 ring->queue_index, pf_q);
3595 	}
3596 
3597 	return 0;
3598 }
3599 
3600 /**
3601  * i40e_vsi_configure_tx - Configure the VSI for Tx
3602  * @vsi: VSI structure describing this set of rings and resources
3603  *
3604  * Configure the Tx VSI for operation.
3605  **/
3606 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3607 {
3608 	int err = 0;
3609 	u16 i;
3610 
3611 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3612 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3613 
3614 	if (err || !i40e_enabled_xdp_vsi(vsi))
3615 		return err;
3616 
3617 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3618 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3619 
3620 	return err;
3621 }
3622 
3623 /**
3624  * i40e_vsi_configure_rx - Configure the VSI for Rx
3625  * @vsi: the VSI being configured
3626  *
3627  * Configure the Rx VSI for operation.
3628  **/
3629 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3630 {
3631 	int err = 0;
3632 	u16 i;
3633 
3634 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3635 		vsi->max_frame = I40E_MAX_RXBUFFER;
3636 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3637 #if (PAGE_SIZE < 8192)
3638 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3639 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3640 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3641 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3642 #endif
3643 	} else {
3644 		vsi->max_frame = I40E_MAX_RXBUFFER;
3645 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3646 						       I40E_RXBUFFER_2048;
3647 	}
3648 
3649 	/* set up individual rings */
3650 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3651 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3652 
3653 	return err;
3654 }
3655 
3656 /**
3657  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3658  * @vsi: ptr to the VSI
3659  **/
3660 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3661 {
3662 	struct i40e_ring *tx_ring, *rx_ring;
3663 	u16 qoffset, qcount;
3664 	int i, n;
3665 
3666 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3667 		/* Reset the TC information */
3668 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3669 			rx_ring = vsi->rx_rings[i];
3670 			tx_ring = vsi->tx_rings[i];
3671 			rx_ring->dcb_tc = 0;
3672 			tx_ring->dcb_tc = 0;
3673 		}
3674 		return;
3675 	}
3676 
3677 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3678 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3679 			continue;
3680 
3681 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3682 		qcount = vsi->tc_config.tc_info[n].qcount;
3683 		for (i = qoffset; i < (qoffset + qcount); i++) {
3684 			rx_ring = vsi->rx_rings[i];
3685 			tx_ring = vsi->tx_rings[i];
3686 			rx_ring->dcb_tc = n;
3687 			tx_ring->dcb_tc = n;
3688 		}
3689 	}
3690 }
3691 
3692 /**
3693  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3694  * @vsi: ptr to the VSI
3695  **/
3696 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3697 {
3698 	if (vsi->netdev)
3699 		i40e_set_rx_mode(vsi->netdev);
3700 }
3701 
3702 /**
3703  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3704  * @pf: Pointer to the targeted PF
3705  *
3706  * Set all flow director counters to 0.
3707  */
3708 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3709 {
3710 	pf->fd_tcp4_filter_cnt = 0;
3711 	pf->fd_udp4_filter_cnt = 0;
3712 	pf->fd_sctp4_filter_cnt = 0;
3713 	pf->fd_ip4_filter_cnt = 0;
3714 	pf->fd_tcp6_filter_cnt = 0;
3715 	pf->fd_udp6_filter_cnt = 0;
3716 	pf->fd_sctp6_filter_cnt = 0;
3717 	pf->fd_ip6_filter_cnt = 0;
3718 }
3719 
3720 /**
3721  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3722  * @vsi: Pointer to the targeted VSI
3723  *
3724  * This function replays the hlist on the hw where all the SB Flow Director
3725  * filters were saved.
3726  **/
3727 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3728 {
3729 	struct i40e_fdir_filter *filter;
3730 	struct i40e_pf *pf = vsi->back;
3731 	struct hlist_node *node;
3732 
3733 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3734 		return;
3735 
3736 	/* Reset FDir counters as we're replaying all existing filters */
3737 	i40e_reset_fdir_filter_cnt(pf);
3738 
3739 	hlist_for_each_entry_safe(filter, node,
3740 				  &pf->fdir_filter_list, fdir_node) {
3741 		i40e_add_del_fdir(vsi, filter, true);
3742 	}
3743 }
3744 
3745 /**
3746  * i40e_vsi_configure - Set up the VSI for action
3747  * @vsi: the VSI being configured
3748  **/
3749 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3750 {
3751 	int err;
3752 
3753 	i40e_set_vsi_rx_mode(vsi);
3754 	i40e_restore_vlan(vsi);
3755 	i40e_vsi_config_dcb_rings(vsi);
3756 	err = i40e_vsi_configure_tx(vsi);
3757 	if (!err)
3758 		err = i40e_vsi_configure_rx(vsi);
3759 
3760 	return err;
3761 }
3762 
3763 /**
3764  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3765  * @vsi: the VSI being configured
3766  **/
3767 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3768 {
3769 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3770 	struct i40e_pf *pf = vsi->back;
3771 	struct i40e_hw *hw = &pf->hw;
3772 	u16 vector;
3773 	int i, q;
3774 	u32 qp;
3775 
3776 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3777 	 * and PFINT_LNKLSTn registers, e.g.:
3778 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3779 	 */
3780 	qp = vsi->base_queue;
3781 	vector = vsi->base_vector;
3782 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3783 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3784 
3785 		q_vector->rx.next_update = jiffies + 1;
3786 		q_vector->rx.target_itr =
3787 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3788 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3789 		     q_vector->rx.target_itr >> 1);
3790 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3791 
3792 		q_vector->tx.next_update = jiffies + 1;
3793 		q_vector->tx.target_itr =
3794 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3795 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3796 		     q_vector->tx.target_itr >> 1);
3797 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3798 
3799 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3800 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3801 
3802 		/* Linked list for the queuepairs assigned to this vector */
3803 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3804 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3805 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3806 			u32 val;
3807 
3808 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3809 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3810 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3811 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3812 			      (I40E_QUEUE_TYPE_TX <<
3813 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3814 
3815 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3816 
3817 			if (has_xdp) {
3818 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3819 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3820 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3821 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3822 				      (I40E_QUEUE_TYPE_TX <<
3823 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3824 
3825 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3826 			}
3827 
3828 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3829 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3830 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3831 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3832 			      (I40E_QUEUE_TYPE_RX <<
3833 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3834 
3835 			/* Terminate the linked list */
3836 			if (q == (q_vector->num_ringpairs - 1))
3837 				val |= (I40E_QUEUE_END_OF_LIST <<
3838 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3839 
3840 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3841 			qp++;
3842 		}
3843 	}
3844 
3845 	i40e_flush(hw);
3846 }
3847 
3848 /**
3849  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3850  * @pf: pointer to private device data structure
3851  **/
3852 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3853 {
3854 	struct i40e_hw *hw = &pf->hw;
3855 	u32 val;
3856 
3857 	/* clear things first */
3858 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3859 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3860 
3861 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3862 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3863 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3864 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3865 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3866 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3867 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3868 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3869 
3870 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3871 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3872 
3873 	if (pf->flags & I40E_FLAG_PTP)
3874 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3875 
3876 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3877 
3878 	/* SW_ITR_IDX = 0, but don't change INTENA */
3879 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3880 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3881 
3882 	/* OTHER_ITR_IDX = 0 */
3883 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3884 }
3885 
3886 /**
3887  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3888  * @vsi: the VSI being configured
3889  **/
3890 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3891 {
3892 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3893 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3894 	struct i40e_pf *pf = vsi->back;
3895 	struct i40e_hw *hw = &pf->hw;
3896 	u32 val;
3897 
3898 	/* set the ITR configuration */
3899 	q_vector->rx.next_update = jiffies + 1;
3900 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3901 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3902 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3903 	q_vector->tx.next_update = jiffies + 1;
3904 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3905 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3906 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3907 
3908 	i40e_enable_misc_int_causes(pf);
3909 
3910 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3911 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3912 
3913 	/* Associate the queue pair to the vector and enable the queue int */
3914 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3915 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3916 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3917 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3918 
3919 	wr32(hw, I40E_QINT_RQCTL(0), val);
3920 
3921 	if (i40e_enabled_xdp_vsi(vsi)) {
3922 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3923 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3924 		      (I40E_QUEUE_TYPE_TX
3925 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3926 
3927 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3928 	}
3929 
3930 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3931 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3932 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3933 
3934 	wr32(hw, I40E_QINT_TQCTL(0), val);
3935 	i40e_flush(hw);
3936 }
3937 
3938 /**
3939  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3940  * @pf: board private structure
3941  **/
3942 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3943 {
3944 	struct i40e_hw *hw = &pf->hw;
3945 
3946 	wr32(hw, I40E_PFINT_DYN_CTL0,
3947 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3948 	i40e_flush(hw);
3949 }
3950 
3951 /**
3952  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3953  * @pf: board private structure
3954  **/
3955 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3956 {
3957 	struct i40e_hw *hw = &pf->hw;
3958 	u32 val;
3959 
3960 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3961 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3962 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3963 
3964 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3965 	i40e_flush(hw);
3966 }
3967 
3968 /**
3969  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3970  * @irq: interrupt number
3971  * @data: pointer to a q_vector
3972  **/
3973 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3974 {
3975 	struct i40e_q_vector *q_vector = data;
3976 
3977 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3978 		return IRQ_HANDLED;
3979 
3980 	napi_schedule_irqoff(&q_vector->napi);
3981 
3982 	return IRQ_HANDLED;
3983 }
3984 
3985 /**
3986  * i40e_irq_affinity_notify - Callback for affinity changes
3987  * @notify: context as to what irq was changed
3988  * @mask: the new affinity mask
3989  *
3990  * This is a callback function used by the irq_set_affinity_notifier function
3991  * so that we may register to receive changes to the irq affinity masks.
3992  **/
3993 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3994 				     const cpumask_t *mask)
3995 {
3996 	struct i40e_q_vector *q_vector =
3997 		container_of(notify, struct i40e_q_vector, affinity_notify);
3998 
3999 	cpumask_copy(&q_vector->affinity_mask, mask);
4000 }
4001 
4002 /**
4003  * i40e_irq_affinity_release - Callback for affinity notifier release
4004  * @ref: internal core kernel usage
4005  *
4006  * This is a callback function used by the irq_set_affinity_notifier function
4007  * to inform the current notification subscriber that they will no longer
4008  * receive notifications.
4009  **/
4010 static void i40e_irq_affinity_release(struct kref *ref) {}
4011 
4012 /**
4013  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4014  * @vsi: the VSI being configured
4015  * @basename: name for the vector
4016  *
4017  * Allocates MSI-X vectors and requests interrupts from the kernel.
4018  **/
4019 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4020 {
4021 	int q_vectors = vsi->num_q_vectors;
4022 	struct i40e_pf *pf = vsi->back;
4023 	int base = vsi->base_vector;
4024 	int rx_int_idx = 0;
4025 	int tx_int_idx = 0;
4026 	int vector, err;
4027 	int irq_num;
4028 	int cpu;
4029 
4030 	for (vector = 0; vector < q_vectors; vector++) {
4031 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4032 
4033 		irq_num = pf->msix_entries[base + vector].vector;
4034 
4035 		if (q_vector->tx.ring && q_vector->rx.ring) {
4036 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4037 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4038 			tx_int_idx++;
4039 		} else if (q_vector->rx.ring) {
4040 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4041 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4042 		} else if (q_vector->tx.ring) {
4043 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4044 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4045 		} else {
4046 			/* skip this unused q_vector */
4047 			continue;
4048 		}
4049 		err = request_irq(irq_num,
4050 				  vsi->irq_handler,
4051 				  0,
4052 				  q_vector->name,
4053 				  q_vector);
4054 		if (err) {
4055 			dev_info(&pf->pdev->dev,
4056 				 "MSIX request_irq failed, error: %d\n", err);
4057 			goto free_queue_irqs;
4058 		}
4059 
4060 		/* register for affinity change notifications */
4061 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4062 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4063 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4064 		/* Spread affinity hints out across online CPUs.
4065 		 *
4066 		 * get_cpu_mask returns a static constant mask with
4067 		 * a permanent lifetime so it's ok to pass to
4068 		 * irq_update_affinity_hint without making a copy.
4069 		 */
4070 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4071 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4072 	}
4073 
4074 	vsi->irqs_ready = true;
4075 	return 0;
4076 
4077 free_queue_irqs:
4078 	while (vector) {
4079 		vector--;
4080 		irq_num = pf->msix_entries[base + vector].vector;
4081 		irq_set_affinity_notifier(irq_num, NULL);
4082 		irq_update_affinity_hint(irq_num, NULL);
4083 		free_irq(irq_num, &vsi->q_vectors[vector]);
4084 	}
4085 	return err;
4086 }
4087 
4088 /**
4089  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4090  * @vsi: the VSI being un-configured
4091  **/
4092 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4093 {
4094 	struct i40e_pf *pf = vsi->back;
4095 	struct i40e_hw *hw = &pf->hw;
4096 	int base = vsi->base_vector;
4097 	int i;
4098 
4099 	/* disable interrupt causation from each queue */
4100 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4101 		u32 val;
4102 
4103 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4104 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4105 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4106 
4107 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4108 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4109 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4110 
4111 		if (!i40e_enabled_xdp_vsi(vsi))
4112 			continue;
4113 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4114 	}
4115 
4116 	/* disable each interrupt */
4117 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4118 		for (i = vsi->base_vector;
4119 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4120 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4121 
4122 		i40e_flush(hw);
4123 		for (i = 0; i < vsi->num_q_vectors; i++)
4124 			synchronize_irq(pf->msix_entries[i + base].vector);
4125 	} else {
4126 		/* Legacy and MSI mode - this stops all interrupt handling */
4127 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4128 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4129 		i40e_flush(hw);
4130 		synchronize_irq(pf->pdev->irq);
4131 	}
4132 }
4133 
4134 /**
4135  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4136  * @vsi: the VSI being configured
4137  **/
4138 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4139 {
4140 	struct i40e_pf *pf = vsi->back;
4141 	int i;
4142 
4143 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4144 		for (i = 0; i < vsi->num_q_vectors; i++)
4145 			i40e_irq_dynamic_enable(vsi, i);
4146 	} else {
4147 		i40e_irq_dynamic_enable_icr0(pf);
4148 	}
4149 
4150 	i40e_flush(&pf->hw);
4151 	return 0;
4152 }
4153 
4154 /**
4155  * i40e_free_misc_vector - Free the vector that handles non-queue events
4156  * @pf: board private structure
4157  **/
4158 static void i40e_free_misc_vector(struct i40e_pf *pf)
4159 {
4160 	/* Disable ICR 0 */
4161 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4162 	i40e_flush(&pf->hw);
4163 
4164 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4165 		free_irq(pf->msix_entries[0].vector, pf);
4166 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4167 	}
4168 }
4169 
4170 /**
4171  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4172  * @irq: interrupt number
4173  * @data: pointer to a q_vector
4174  *
4175  * This is the handler used for all MSI/Legacy interrupts, and deals
4176  * with both queue and non-queue interrupts.  This is also used in
4177  * MSIX mode to handle the non-queue interrupts.
4178  **/
4179 static irqreturn_t i40e_intr(int irq, void *data)
4180 {
4181 	struct i40e_pf *pf = (struct i40e_pf *)data;
4182 	struct i40e_hw *hw = &pf->hw;
4183 	irqreturn_t ret = IRQ_NONE;
4184 	u32 icr0, icr0_remaining;
4185 	u32 val, ena_mask;
4186 
4187 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4188 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4189 
4190 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4191 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4192 		goto enable_intr;
4193 
4194 	/* if interrupt but no bits showing, must be SWINT */
4195 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4196 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4197 		pf->sw_int_count++;
4198 
4199 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4200 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4201 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4202 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4203 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4204 	}
4205 
4206 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4207 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4208 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4209 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4210 
4211 		/* We do not have a way to disarm Queue causes while leaving
4212 		 * interrupt enabled for all other causes, ideally
4213 		 * interrupt should be disabled while we are in NAPI but
4214 		 * this is not a performance path and napi_schedule()
4215 		 * can deal with rescheduling.
4216 		 */
4217 		if (!test_bit(__I40E_DOWN, pf->state))
4218 			napi_schedule_irqoff(&q_vector->napi);
4219 	}
4220 
4221 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4222 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4223 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4224 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4225 	}
4226 
4227 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4228 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4229 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4230 	}
4231 
4232 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4233 		/* disable any further VFLR event notifications */
4234 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4235 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4236 
4237 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4238 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4239 		} else {
4240 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4241 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4242 		}
4243 	}
4244 
4245 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4246 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4247 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4248 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4249 		val = rd32(hw, I40E_GLGEN_RSTAT);
4250 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4251 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4252 		if (val == I40E_RESET_CORER) {
4253 			pf->corer_count++;
4254 		} else if (val == I40E_RESET_GLOBR) {
4255 			pf->globr_count++;
4256 		} else if (val == I40E_RESET_EMPR) {
4257 			pf->empr_count++;
4258 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4259 		}
4260 	}
4261 
4262 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4263 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4264 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4265 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4266 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4267 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4268 	}
4269 
4270 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4271 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4272 
4273 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4274 			schedule_work(&pf->ptp_extts0_work);
4275 
4276 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4277 			i40e_ptp_tx_hwtstamp(pf);
4278 
4279 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4280 	}
4281 
4282 	/* If a critical error is pending we have no choice but to reset the
4283 	 * device.
4284 	 * Report and mask out any remaining unexpected interrupts.
4285 	 */
4286 	icr0_remaining = icr0 & ena_mask;
4287 	if (icr0_remaining) {
4288 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4289 			 icr0_remaining);
4290 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4291 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4292 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4293 			dev_info(&pf->pdev->dev, "device will be reset\n");
4294 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4295 			i40e_service_event_schedule(pf);
4296 		}
4297 		ena_mask &= ~icr0_remaining;
4298 	}
4299 	ret = IRQ_HANDLED;
4300 
4301 enable_intr:
4302 	/* re-enable interrupt causes */
4303 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4304 	if (!test_bit(__I40E_DOWN, pf->state) ||
4305 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4306 		i40e_service_event_schedule(pf);
4307 		i40e_irq_dynamic_enable_icr0(pf);
4308 	}
4309 
4310 	return ret;
4311 }
4312 
4313 /**
4314  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4315  * @tx_ring:  tx ring to clean
4316  * @budget:   how many cleans we're allowed
4317  *
4318  * Returns true if there's any budget left (e.g. the clean is finished)
4319  **/
4320 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4321 {
4322 	struct i40e_vsi *vsi = tx_ring->vsi;
4323 	u16 i = tx_ring->next_to_clean;
4324 	struct i40e_tx_buffer *tx_buf;
4325 	struct i40e_tx_desc *tx_desc;
4326 
4327 	tx_buf = &tx_ring->tx_bi[i];
4328 	tx_desc = I40E_TX_DESC(tx_ring, i);
4329 	i -= tx_ring->count;
4330 
4331 	do {
4332 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4333 
4334 		/* if next_to_watch is not set then there is no work pending */
4335 		if (!eop_desc)
4336 			break;
4337 
4338 		/* prevent any other reads prior to eop_desc */
4339 		smp_rmb();
4340 
4341 		/* if the descriptor isn't done, no work yet to do */
4342 		if (!(eop_desc->cmd_type_offset_bsz &
4343 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4344 			break;
4345 
4346 		/* clear next_to_watch to prevent false hangs */
4347 		tx_buf->next_to_watch = NULL;
4348 
4349 		tx_desc->buffer_addr = 0;
4350 		tx_desc->cmd_type_offset_bsz = 0;
4351 		/* move past filter desc */
4352 		tx_buf++;
4353 		tx_desc++;
4354 		i++;
4355 		if (unlikely(!i)) {
4356 			i -= tx_ring->count;
4357 			tx_buf = tx_ring->tx_bi;
4358 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4359 		}
4360 		/* unmap skb header data */
4361 		dma_unmap_single(tx_ring->dev,
4362 				 dma_unmap_addr(tx_buf, dma),
4363 				 dma_unmap_len(tx_buf, len),
4364 				 DMA_TO_DEVICE);
4365 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4366 			kfree(tx_buf->raw_buf);
4367 
4368 		tx_buf->raw_buf = NULL;
4369 		tx_buf->tx_flags = 0;
4370 		tx_buf->next_to_watch = NULL;
4371 		dma_unmap_len_set(tx_buf, len, 0);
4372 		tx_desc->buffer_addr = 0;
4373 		tx_desc->cmd_type_offset_bsz = 0;
4374 
4375 		/* move us past the eop_desc for start of next FD desc */
4376 		tx_buf++;
4377 		tx_desc++;
4378 		i++;
4379 		if (unlikely(!i)) {
4380 			i -= tx_ring->count;
4381 			tx_buf = tx_ring->tx_bi;
4382 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4383 		}
4384 
4385 		/* update budget accounting */
4386 		budget--;
4387 	} while (likely(budget));
4388 
4389 	i += tx_ring->count;
4390 	tx_ring->next_to_clean = i;
4391 
4392 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4393 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4394 
4395 	return budget > 0;
4396 }
4397 
4398 /**
4399  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4400  * @irq: interrupt number
4401  * @data: pointer to a q_vector
4402  **/
4403 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4404 {
4405 	struct i40e_q_vector *q_vector = data;
4406 	struct i40e_vsi *vsi;
4407 
4408 	if (!q_vector->tx.ring)
4409 		return IRQ_HANDLED;
4410 
4411 	vsi = q_vector->tx.ring->vsi;
4412 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4413 
4414 	return IRQ_HANDLED;
4415 }
4416 
4417 /**
4418  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4419  * @vsi: the VSI being configured
4420  * @v_idx: vector index
4421  * @qp_idx: queue pair index
4422  **/
4423 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4424 {
4425 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4426 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4427 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4428 
4429 	tx_ring->q_vector = q_vector;
4430 	tx_ring->next = q_vector->tx.ring;
4431 	q_vector->tx.ring = tx_ring;
4432 	q_vector->tx.count++;
4433 
4434 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4435 	if (i40e_enabled_xdp_vsi(vsi)) {
4436 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4437 
4438 		xdp_ring->q_vector = q_vector;
4439 		xdp_ring->next = q_vector->tx.ring;
4440 		q_vector->tx.ring = xdp_ring;
4441 		q_vector->tx.count++;
4442 	}
4443 
4444 	rx_ring->q_vector = q_vector;
4445 	rx_ring->next = q_vector->rx.ring;
4446 	q_vector->rx.ring = rx_ring;
4447 	q_vector->rx.count++;
4448 }
4449 
4450 /**
4451  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4452  * @vsi: the VSI being configured
4453  *
4454  * This function maps descriptor rings to the queue-specific vectors
4455  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4456  * one vector per queue pair, but on a constrained vector budget, we
4457  * group the queue pairs as "efficiently" as possible.
4458  **/
4459 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4460 {
4461 	int qp_remaining = vsi->num_queue_pairs;
4462 	int q_vectors = vsi->num_q_vectors;
4463 	int num_ringpairs;
4464 	int v_start = 0;
4465 	int qp_idx = 0;
4466 
4467 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4468 	 * group them so there are multiple queues per vector.
4469 	 * It is also important to go through all the vectors available to be
4470 	 * sure that if we don't use all the vectors, that the remaining vectors
4471 	 * are cleared. This is especially important when decreasing the
4472 	 * number of queues in use.
4473 	 */
4474 	for (; v_start < q_vectors; v_start++) {
4475 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4476 
4477 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4478 
4479 		q_vector->num_ringpairs = num_ringpairs;
4480 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4481 
4482 		q_vector->rx.count = 0;
4483 		q_vector->tx.count = 0;
4484 		q_vector->rx.ring = NULL;
4485 		q_vector->tx.ring = NULL;
4486 
4487 		while (num_ringpairs--) {
4488 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4489 			qp_idx++;
4490 			qp_remaining--;
4491 		}
4492 	}
4493 }
4494 
4495 /**
4496  * i40e_vsi_request_irq - Request IRQ from the OS
4497  * @vsi: the VSI being configured
4498  * @basename: name for the vector
4499  **/
4500 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4501 {
4502 	struct i40e_pf *pf = vsi->back;
4503 	int err;
4504 
4505 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4506 		err = i40e_vsi_request_irq_msix(vsi, basename);
4507 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4508 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4509 				  pf->int_name, pf);
4510 	else
4511 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4512 				  pf->int_name, pf);
4513 
4514 	if (err)
4515 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4516 
4517 	return err;
4518 }
4519 
4520 #ifdef CONFIG_NET_POLL_CONTROLLER
4521 /**
4522  * i40e_netpoll - A Polling 'interrupt' handler
4523  * @netdev: network interface device structure
4524  *
4525  * This is used by netconsole to send skbs without having to re-enable
4526  * interrupts.  It's not called while the normal interrupt routine is executing.
4527  **/
4528 static void i40e_netpoll(struct net_device *netdev)
4529 {
4530 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4531 	struct i40e_vsi *vsi = np->vsi;
4532 	struct i40e_pf *pf = vsi->back;
4533 	int i;
4534 
4535 	/* if interface is down do nothing */
4536 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4537 		return;
4538 
4539 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4540 		for (i = 0; i < vsi->num_q_vectors; i++)
4541 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4542 	} else {
4543 		i40e_intr(pf->pdev->irq, netdev);
4544 	}
4545 }
4546 #endif
4547 
4548 #define I40E_QTX_ENA_WAIT_COUNT 50
4549 
4550 /**
4551  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4552  * @pf: the PF being configured
4553  * @pf_q: the PF queue
4554  * @enable: enable or disable state of the queue
4555  *
4556  * This routine will wait for the given Tx queue of the PF to reach the
4557  * enabled or disabled state.
4558  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4559  * multiple retries; else will return 0 in case of success.
4560  **/
4561 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4562 {
4563 	int i;
4564 	u32 tx_reg;
4565 
4566 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4567 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4568 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4569 			break;
4570 
4571 		usleep_range(10, 20);
4572 	}
4573 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4574 		return -ETIMEDOUT;
4575 
4576 	return 0;
4577 }
4578 
4579 /**
4580  * i40e_control_tx_q - Start or stop a particular Tx queue
4581  * @pf: the PF structure
4582  * @pf_q: the PF queue to configure
4583  * @enable: start or stop the queue
4584  *
4585  * This function enables or disables a single queue. Note that any delay
4586  * required after the operation is expected to be handled by the caller of
4587  * this function.
4588  **/
4589 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4590 {
4591 	struct i40e_hw *hw = &pf->hw;
4592 	u32 tx_reg;
4593 	int i;
4594 
4595 	/* warn the TX unit of coming changes */
4596 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4597 	if (!enable)
4598 		usleep_range(10, 20);
4599 
4600 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4601 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4602 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4603 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4604 			break;
4605 		usleep_range(1000, 2000);
4606 	}
4607 
4608 	/* Skip if the queue is already in the requested state */
4609 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4610 		return;
4611 
4612 	/* turn on/off the queue */
4613 	if (enable) {
4614 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4615 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4616 	} else {
4617 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4618 	}
4619 
4620 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4621 }
4622 
4623 /**
4624  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4625  * @seid: VSI SEID
4626  * @pf: the PF structure
4627  * @pf_q: the PF queue to configure
4628  * @is_xdp: true if the queue is used for XDP
4629  * @enable: start or stop the queue
4630  **/
4631 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4632 			   bool is_xdp, bool enable)
4633 {
4634 	int ret;
4635 
4636 	i40e_control_tx_q(pf, pf_q, enable);
4637 
4638 	/* wait for the change to finish */
4639 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4640 	if (ret) {
4641 		dev_info(&pf->pdev->dev,
4642 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4643 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4644 			 (enable ? "en" : "dis"));
4645 	}
4646 
4647 	return ret;
4648 }
4649 
4650 /**
4651  * i40e_vsi_enable_tx - Start a VSI's rings
4652  * @vsi: the VSI being configured
4653  **/
4654 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4655 {
4656 	struct i40e_pf *pf = vsi->back;
4657 	int i, pf_q, ret = 0;
4658 
4659 	pf_q = vsi->base_queue;
4660 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4661 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4662 					     pf_q,
4663 					     false /*is xdp*/, true);
4664 		if (ret)
4665 			break;
4666 
4667 		if (!i40e_enabled_xdp_vsi(vsi))
4668 			continue;
4669 
4670 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4671 					     pf_q + vsi->alloc_queue_pairs,
4672 					     true /*is xdp*/, true);
4673 		if (ret)
4674 			break;
4675 	}
4676 	return ret;
4677 }
4678 
4679 /**
4680  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4681  * @pf: the PF being configured
4682  * @pf_q: the PF queue
4683  * @enable: enable or disable state of the queue
4684  *
4685  * This routine will wait for the given Rx queue of the PF to reach the
4686  * enabled or disabled state.
4687  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4688  * multiple retries; else will return 0 in case of success.
4689  **/
4690 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4691 {
4692 	int i;
4693 	u32 rx_reg;
4694 
4695 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4696 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4697 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4698 			break;
4699 
4700 		usleep_range(10, 20);
4701 	}
4702 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4703 		return -ETIMEDOUT;
4704 
4705 	return 0;
4706 }
4707 
4708 /**
4709  * i40e_control_rx_q - Start or stop a particular Rx queue
4710  * @pf: the PF structure
4711  * @pf_q: the PF queue to configure
4712  * @enable: start or stop the queue
4713  *
4714  * This function enables or disables a single queue. Note that
4715  * any delay required after the operation is expected to be
4716  * handled by the caller of this function.
4717  **/
4718 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4719 {
4720 	struct i40e_hw *hw = &pf->hw;
4721 	u32 rx_reg;
4722 	int i;
4723 
4724 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4725 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4726 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4727 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4728 			break;
4729 		usleep_range(1000, 2000);
4730 	}
4731 
4732 	/* Skip if the queue is already in the requested state */
4733 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4734 		return;
4735 
4736 	/* turn on/off the queue */
4737 	if (enable)
4738 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4739 	else
4740 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4741 
4742 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4743 }
4744 
4745 /**
4746  * i40e_control_wait_rx_q
4747  * @pf: the PF structure
4748  * @pf_q: queue being configured
4749  * @enable: start or stop the rings
4750  *
4751  * This function enables or disables a single queue along with waiting
4752  * for the change to finish. The caller of this function should handle
4753  * the delays needed in the case of disabling queues.
4754  **/
4755 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4756 {
4757 	int ret = 0;
4758 
4759 	i40e_control_rx_q(pf, pf_q, enable);
4760 
4761 	/* wait for the change to finish */
4762 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4763 	if (ret)
4764 		return ret;
4765 
4766 	return ret;
4767 }
4768 
4769 /**
4770  * i40e_vsi_enable_rx - Start a VSI's rings
4771  * @vsi: the VSI being configured
4772  **/
4773 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4774 {
4775 	struct i40e_pf *pf = vsi->back;
4776 	int i, pf_q, ret = 0;
4777 
4778 	pf_q = vsi->base_queue;
4779 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4780 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4781 		if (ret) {
4782 			dev_info(&pf->pdev->dev,
4783 				 "VSI seid %d Rx ring %d enable timeout\n",
4784 				 vsi->seid, pf_q);
4785 			break;
4786 		}
4787 	}
4788 
4789 	return ret;
4790 }
4791 
4792 /**
4793  * i40e_vsi_start_rings - Start a VSI's rings
4794  * @vsi: the VSI being configured
4795  **/
4796 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4797 {
4798 	int ret = 0;
4799 
4800 	/* do rx first for enable and last for disable */
4801 	ret = i40e_vsi_enable_rx(vsi);
4802 	if (ret)
4803 		return ret;
4804 	ret = i40e_vsi_enable_tx(vsi);
4805 
4806 	return ret;
4807 }
4808 
4809 #define I40E_DISABLE_TX_GAP_MSEC	50
4810 
4811 /**
4812  * i40e_vsi_stop_rings - Stop a VSI's rings
4813  * @vsi: the VSI being configured
4814  **/
4815 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4816 {
4817 	struct i40e_pf *pf = vsi->back;
4818 	int pf_q, err, q_end;
4819 
4820 	/* When port TX is suspended, don't wait */
4821 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4822 		return i40e_vsi_stop_rings_no_wait(vsi);
4823 
4824 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4825 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4826 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4827 
4828 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4829 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4830 		if (err)
4831 			dev_info(&pf->pdev->dev,
4832 				 "VSI seid %d Rx ring %d disable timeout\n",
4833 				 vsi->seid, pf_q);
4834 	}
4835 
4836 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4837 	pf_q = vsi->base_queue;
4838 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4839 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4840 
4841 	i40e_vsi_wait_queues_disabled(vsi);
4842 }
4843 
4844 /**
4845  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4846  * @vsi: the VSI being shutdown
4847  *
4848  * This function stops all the rings for a VSI but does not delay to verify
4849  * that rings have been disabled. It is expected that the caller is shutting
4850  * down multiple VSIs at once and will delay together for all the VSIs after
4851  * initiating the shutdown. This is particularly useful for shutting down lots
4852  * of VFs together. Otherwise, a large delay can be incurred while configuring
4853  * each VSI in serial.
4854  **/
4855 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4856 {
4857 	struct i40e_pf *pf = vsi->back;
4858 	int i, pf_q;
4859 
4860 	pf_q = vsi->base_queue;
4861 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4862 		i40e_control_tx_q(pf, pf_q, false);
4863 		i40e_control_rx_q(pf, pf_q, false);
4864 	}
4865 }
4866 
4867 /**
4868  * i40e_vsi_free_irq - Free the irq association with the OS
4869  * @vsi: the VSI being configured
4870  **/
4871 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4872 {
4873 	struct i40e_pf *pf = vsi->back;
4874 	struct i40e_hw *hw = &pf->hw;
4875 	int base = vsi->base_vector;
4876 	u32 val, qp;
4877 	int i;
4878 
4879 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4880 		if (!vsi->q_vectors)
4881 			return;
4882 
4883 		if (!vsi->irqs_ready)
4884 			return;
4885 
4886 		vsi->irqs_ready = false;
4887 		for (i = 0; i < vsi->num_q_vectors; i++) {
4888 			int irq_num;
4889 			u16 vector;
4890 
4891 			vector = i + base;
4892 			irq_num = pf->msix_entries[vector].vector;
4893 
4894 			/* free only the irqs that were actually requested */
4895 			if (!vsi->q_vectors[i] ||
4896 			    !vsi->q_vectors[i]->num_ringpairs)
4897 				continue;
4898 
4899 			/* clear the affinity notifier in the IRQ descriptor */
4900 			irq_set_affinity_notifier(irq_num, NULL);
4901 			/* remove our suggested affinity mask for this IRQ */
4902 			irq_update_affinity_hint(irq_num, NULL);
4903 			free_irq(irq_num, vsi->q_vectors[i]);
4904 
4905 			/* Tear down the interrupt queue link list
4906 			 *
4907 			 * We know that they come in pairs and always
4908 			 * the Rx first, then the Tx.  To clear the
4909 			 * link list, stick the EOL value into the
4910 			 * next_q field of the registers.
4911 			 */
4912 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4913 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4914 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4915 			val |= I40E_QUEUE_END_OF_LIST
4916 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4917 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4918 
4919 			while (qp != I40E_QUEUE_END_OF_LIST) {
4920 				u32 next;
4921 
4922 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4923 
4924 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4925 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4926 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4927 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4928 
4929 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4930 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4931 
4932 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4933 
4934 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4935 
4936 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4937 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4938 
4939 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4940 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4941 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4942 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4943 
4944 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4945 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4946 
4947 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4948 				qp = next;
4949 			}
4950 		}
4951 	} else {
4952 		free_irq(pf->pdev->irq, pf);
4953 
4954 		val = rd32(hw, I40E_PFINT_LNKLST0);
4955 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4956 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4957 		val |= I40E_QUEUE_END_OF_LIST
4958 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4959 		wr32(hw, I40E_PFINT_LNKLST0, val);
4960 
4961 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4962 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4963 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4964 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4965 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4966 
4967 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4968 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4969 
4970 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4971 
4972 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4973 
4974 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4975 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4976 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4977 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4978 
4979 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4980 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4981 
4982 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4983 	}
4984 }
4985 
4986 /**
4987  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4988  * @vsi: the VSI being configured
4989  * @v_idx: Index of vector to be freed
4990  *
4991  * This function frees the memory allocated to the q_vector.  In addition if
4992  * NAPI is enabled it will delete any references to the NAPI struct prior
4993  * to freeing the q_vector.
4994  **/
4995 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4996 {
4997 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4998 	struct i40e_ring *ring;
4999 
5000 	if (!q_vector)
5001 		return;
5002 
5003 	/* disassociate q_vector from rings */
5004 	i40e_for_each_ring(ring, q_vector->tx)
5005 		ring->q_vector = NULL;
5006 
5007 	i40e_for_each_ring(ring, q_vector->rx)
5008 		ring->q_vector = NULL;
5009 
5010 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5011 	if (vsi->netdev)
5012 		netif_napi_del(&q_vector->napi);
5013 
5014 	vsi->q_vectors[v_idx] = NULL;
5015 
5016 	kfree_rcu(q_vector, rcu);
5017 }
5018 
5019 /**
5020  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5021  * @vsi: the VSI being un-configured
5022  *
5023  * This frees the memory allocated to the q_vectors and
5024  * deletes references to the NAPI struct.
5025  **/
5026 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5027 {
5028 	int v_idx;
5029 
5030 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5031 		i40e_free_q_vector(vsi, v_idx);
5032 }
5033 
5034 /**
5035  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5036  * @pf: board private structure
5037  **/
5038 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5039 {
5040 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5041 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5042 		pci_disable_msix(pf->pdev);
5043 		kfree(pf->msix_entries);
5044 		pf->msix_entries = NULL;
5045 		kfree(pf->irq_pile);
5046 		pf->irq_pile = NULL;
5047 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5048 		pci_disable_msi(pf->pdev);
5049 	}
5050 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5051 }
5052 
5053 /**
5054  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5055  * @pf: board private structure
5056  *
5057  * We go through and clear interrupt specific resources and reset the structure
5058  * to pre-load conditions
5059  **/
5060 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5061 {
5062 	int i;
5063 
5064 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5065 		i40e_free_misc_vector(pf);
5066 
5067 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5068 		      I40E_IWARP_IRQ_PILE_ID);
5069 
5070 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5071 	for (i = 0; i < pf->num_alloc_vsi; i++)
5072 		if (pf->vsi[i])
5073 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5074 	i40e_reset_interrupt_capability(pf);
5075 }
5076 
5077 /**
5078  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5079  * @vsi: the VSI being configured
5080  **/
5081 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5082 {
5083 	int q_idx;
5084 
5085 	if (!vsi->netdev)
5086 		return;
5087 
5088 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5089 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5090 
5091 		if (q_vector->rx.ring || q_vector->tx.ring)
5092 			napi_enable(&q_vector->napi);
5093 	}
5094 }
5095 
5096 /**
5097  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5098  * @vsi: the VSI being configured
5099  **/
5100 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5101 {
5102 	int q_idx;
5103 
5104 	if (!vsi->netdev)
5105 		return;
5106 
5107 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5108 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5109 
5110 		if (q_vector->rx.ring || q_vector->tx.ring)
5111 			napi_disable(&q_vector->napi);
5112 	}
5113 }
5114 
5115 /**
5116  * i40e_vsi_close - Shut down a VSI
5117  * @vsi: the vsi to be quelled
5118  **/
5119 static void i40e_vsi_close(struct i40e_vsi *vsi)
5120 {
5121 	struct i40e_pf *pf = vsi->back;
5122 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5123 		i40e_down(vsi);
5124 	i40e_vsi_free_irq(vsi);
5125 	i40e_vsi_free_tx_resources(vsi);
5126 	i40e_vsi_free_rx_resources(vsi);
5127 	vsi->current_netdev_flags = 0;
5128 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5129 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5130 		set_bit(__I40E_CLIENT_RESET, pf->state);
5131 }
5132 
5133 /**
5134  * i40e_quiesce_vsi - Pause a given VSI
5135  * @vsi: the VSI being paused
5136  **/
5137 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5138 {
5139 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5140 		return;
5141 
5142 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5143 	if (vsi->netdev && netif_running(vsi->netdev))
5144 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5145 	else
5146 		i40e_vsi_close(vsi);
5147 }
5148 
5149 /**
5150  * i40e_unquiesce_vsi - Resume a given VSI
5151  * @vsi: the VSI being resumed
5152  **/
5153 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5154 {
5155 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5156 		return;
5157 
5158 	if (vsi->netdev && netif_running(vsi->netdev))
5159 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5160 	else
5161 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5162 }
5163 
5164 /**
5165  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5166  * @pf: the PF
5167  **/
5168 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5169 {
5170 	int v;
5171 
5172 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5173 		if (pf->vsi[v])
5174 			i40e_quiesce_vsi(pf->vsi[v]);
5175 	}
5176 }
5177 
5178 /**
5179  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5180  * @pf: the PF
5181  **/
5182 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5183 {
5184 	int v;
5185 
5186 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5187 		if (pf->vsi[v])
5188 			i40e_unquiesce_vsi(pf->vsi[v]);
5189 	}
5190 }
5191 
5192 /**
5193  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5194  * @vsi: the VSI being configured
5195  *
5196  * Wait until all queues on a given VSI have been disabled.
5197  **/
5198 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5199 {
5200 	struct i40e_pf *pf = vsi->back;
5201 	int i, pf_q, ret;
5202 
5203 	pf_q = vsi->base_queue;
5204 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5205 		/* Check and wait for the Tx queue */
5206 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5207 		if (ret) {
5208 			dev_info(&pf->pdev->dev,
5209 				 "VSI seid %d Tx ring %d disable timeout\n",
5210 				 vsi->seid, pf_q);
5211 			return ret;
5212 		}
5213 
5214 		if (!i40e_enabled_xdp_vsi(vsi))
5215 			goto wait_rx;
5216 
5217 		/* Check and wait for the XDP Tx queue */
5218 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5219 				       false);
5220 		if (ret) {
5221 			dev_info(&pf->pdev->dev,
5222 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5223 				 vsi->seid, pf_q);
5224 			return ret;
5225 		}
5226 wait_rx:
5227 		/* Check and wait for the Rx queue */
5228 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5229 		if (ret) {
5230 			dev_info(&pf->pdev->dev,
5231 				 "VSI seid %d Rx ring %d disable timeout\n",
5232 				 vsi->seid, pf_q);
5233 			return ret;
5234 		}
5235 	}
5236 
5237 	return 0;
5238 }
5239 
5240 #ifdef CONFIG_I40E_DCB
5241 /**
5242  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5243  * @pf: the PF
5244  *
5245  * This function waits for the queues to be in disabled state for all the
5246  * VSIs that are managed by this PF.
5247  **/
5248 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5249 {
5250 	int v, ret = 0;
5251 
5252 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5253 		if (pf->vsi[v]) {
5254 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5255 			if (ret)
5256 				break;
5257 		}
5258 	}
5259 
5260 	return ret;
5261 }
5262 
5263 #endif
5264 
5265 /**
5266  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5267  * @pf: pointer to PF
5268  *
5269  * Get TC map for ISCSI PF type that will include iSCSI TC
5270  * and LAN TC.
5271  **/
5272 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5273 {
5274 	struct i40e_dcb_app_priority_table app;
5275 	struct i40e_hw *hw = &pf->hw;
5276 	u8 enabled_tc = 1; /* TC0 is always enabled */
5277 	u8 tc, i;
5278 	/* Get the iSCSI APP TLV */
5279 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5280 
5281 	for (i = 0; i < dcbcfg->numapps; i++) {
5282 		app = dcbcfg->app[i];
5283 		if (app.selector == I40E_APP_SEL_TCPIP &&
5284 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5285 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5286 			enabled_tc |= BIT(tc);
5287 			break;
5288 		}
5289 	}
5290 
5291 	return enabled_tc;
5292 }
5293 
5294 /**
5295  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5296  * @dcbcfg: the corresponding DCBx configuration structure
5297  *
5298  * Return the number of TCs from given DCBx configuration
5299  **/
5300 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5301 {
5302 	int i, tc_unused = 0;
5303 	u8 num_tc = 0;
5304 	u8 ret = 0;
5305 
5306 	/* Scan the ETS Config Priority Table to find
5307 	 * traffic class enabled for a given priority
5308 	 * and create a bitmask of enabled TCs
5309 	 */
5310 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5311 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5312 
5313 	/* Now scan the bitmask to check for
5314 	 * contiguous TCs starting with TC0
5315 	 */
5316 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5317 		if (num_tc & BIT(i)) {
5318 			if (!tc_unused) {
5319 				ret++;
5320 			} else {
5321 				pr_err("Non-contiguous TC - Disabling DCB\n");
5322 				return 1;
5323 			}
5324 		} else {
5325 			tc_unused = 1;
5326 		}
5327 	}
5328 
5329 	/* There is always at least TC0 */
5330 	if (!ret)
5331 		ret = 1;
5332 
5333 	return ret;
5334 }
5335 
5336 /**
5337  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5338  * @dcbcfg: the corresponding DCBx configuration structure
5339  *
5340  * Query the current DCB configuration and return the number of
5341  * traffic classes enabled from the given DCBX config
5342  **/
5343 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5344 {
5345 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5346 	u8 enabled_tc = 1;
5347 	u8 i;
5348 
5349 	for (i = 0; i < num_tc; i++)
5350 		enabled_tc |= BIT(i);
5351 
5352 	return enabled_tc;
5353 }
5354 
5355 /**
5356  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5357  * @pf: PF being queried
5358  *
5359  * Query the current MQPRIO configuration and return the number of
5360  * traffic classes enabled.
5361  **/
5362 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5363 {
5364 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5365 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5366 	u8 enabled_tc = 1, i;
5367 
5368 	for (i = 1; i < num_tc; i++)
5369 		enabled_tc |= BIT(i);
5370 	return enabled_tc;
5371 }
5372 
5373 /**
5374  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5375  * @pf: PF being queried
5376  *
5377  * Return number of traffic classes enabled for the given PF
5378  **/
5379 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5380 {
5381 	struct i40e_hw *hw = &pf->hw;
5382 	u8 i, enabled_tc = 1;
5383 	u8 num_tc = 0;
5384 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5385 
5386 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5387 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5388 
5389 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5390 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5391 		return 1;
5392 
5393 	/* SFP mode will be enabled for all TCs on port */
5394 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5395 		return i40e_dcb_get_num_tc(dcbcfg);
5396 
5397 	/* MFP mode return count of enabled TCs for this PF */
5398 	if (pf->hw.func_caps.iscsi)
5399 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5400 	else
5401 		return 1; /* Only TC0 */
5402 
5403 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5404 		if (enabled_tc & BIT(i))
5405 			num_tc++;
5406 	}
5407 	return num_tc;
5408 }
5409 
5410 /**
5411  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5412  * @pf: PF being queried
5413  *
5414  * Return a bitmap for enabled traffic classes for this PF.
5415  **/
5416 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5417 {
5418 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5419 		return i40e_mqprio_get_enabled_tc(pf);
5420 
5421 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5422 	 * default TC
5423 	 */
5424 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5425 		return I40E_DEFAULT_TRAFFIC_CLASS;
5426 
5427 	/* SFP mode we want PF to be enabled for all TCs */
5428 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5429 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5430 
5431 	/* MFP enabled and iSCSI PF type */
5432 	if (pf->hw.func_caps.iscsi)
5433 		return i40e_get_iscsi_tc_map(pf);
5434 	else
5435 		return I40E_DEFAULT_TRAFFIC_CLASS;
5436 }
5437 
5438 /**
5439  * i40e_vsi_get_bw_info - Query VSI BW Information
5440  * @vsi: the VSI being queried
5441  *
5442  * Returns 0 on success, negative value on failure
5443  **/
5444 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5445 {
5446 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5447 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5448 	struct i40e_pf *pf = vsi->back;
5449 	struct i40e_hw *hw = &pf->hw;
5450 	i40e_status ret;
5451 	u32 tc_bw_max;
5452 	int i;
5453 
5454 	/* Get the VSI level BW configuration */
5455 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5456 	if (ret) {
5457 		dev_info(&pf->pdev->dev,
5458 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5459 			 i40e_stat_str(&pf->hw, ret),
5460 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5461 		return -EINVAL;
5462 	}
5463 
5464 	/* Get the VSI level BW configuration per TC */
5465 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5466 					       NULL);
5467 	if (ret) {
5468 		dev_info(&pf->pdev->dev,
5469 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5470 			 i40e_stat_str(&pf->hw, ret),
5471 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5472 		return -EINVAL;
5473 	}
5474 
5475 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5476 		dev_info(&pf->pdev->dev,
5477 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5478 			 bw_config.tc_valid_bits,
5479 			 bw_ets_config.tc_valid_bits);
5480 		/* Still continuing */
5481 	}
5482 
5483 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5484 	vsi->bw_max_quanta = bw_config.max_bw;
5485 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5486 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5487 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5488 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5489 		vsi->bw_ets_limit_credits[i] =
5490 					le16_to_cpu(bw_ets_config.credits[i]);
5491 		/* 3 bits out of 4 for each TC */
5492 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5493 	}
5494 
5495 	return 0;
5496 }
5497 
5498 /**
5499  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5500  * @vsi: the VSI being configured
5501  * @enabled_tc: TC bitmap
5502  * @bw_share: BW shared credits per TC
5503  *
5504  * Returns 0 on success, negative value on failure
5505  **/
5506 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5507 				       u8 *bw_share)
5508 {
5509 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5510 	struct i40e_pf *pf = vsi->back;
5511 	i40e_status ret;
5512 	int i;
5513 
5514 	/* There is no need to reset BW when mqprio mode is on.  */
5515 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5516 		return 0;
5517 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5518 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5519 		if (ret)
5520 			dev_info(&pf->pdev->dev,
5521 				 "Failed to reset tx rate for vsi->seid %u\n",
5522 				 vsi->seid);
5523 		return ret;
5524 	}
5525 	memset(&bw_data, 0, sizeof(bw_data));
5526 	bw_data.tc_valid_bits = enabled_tc;
5527 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5528 		bw_data.tc_bw_credits[i] = bw_share[i];
5529 
5530 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5531 	if (ret) {
5532 		dev_info(&pf->pdev->dev,
5533 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5534 			 pf->hw.aq.asq_last_status);
5535 		return -EINVAL;
5536 	}
5537 
5538 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5539 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5540 
5541 	return 0;
5542 }
5543 
5544 /**
5545  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5546  * @vsi: the VSI being configured
5547  * @enabled_tc: TC map to be enabled
5548  *
5549  **/
5550 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5551 {
5552 	struct net_device *netdev = vsi->netdev;
5553 	struct i40e_pf *pf = vsi->back;
5554 	struct i40e_hw *hw = &pf->hw;
5555 	u8 netdev_tc = 0;
5556 	int i;
5557 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5558 
5559 	if (!netdev)
5560 		return;
5561 
5562 	if (!enabled_tc) {
5563 		netdev_reset_tc(netdev);
5564 		return;
5565 	}
5566 
5567 	/* Set up actual enabled TCs on the VSI */
5568 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5569 		return;
5570 
5571 	/* set per TC queues for the VSI */
5572 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5573 		/* Only set TC queues for enabled tcs
5574 		 *
5575 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5576 		 * enabled_tc bitmap would be 0x00001001; the driver
5577 		 * will set the numtc for netdev as 2 that will be
5578 		 * referenced by the netdev layer as TC 0 and 1.
5579 		 */
5580 		if (vsi->tc_config.enabled_tc & BIT(i))
5581 			netdev_set_tc_queue(netdev,
5582 					vsi->tc_config.tc_info[i].netdev_tc,
5583 					vsi->tc_config.tc_info[i].qcount,
5584 					vsi->tc_config.tc_info[i].qoffset);
5585 	}
5586 
5587 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5588 		return;
5589 
5590 	/* Assign UP2TC map for the VSI */
5591 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5592 		/* Get the actual TC# for the UP */
5593 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5594 		/* Get the mapped netdev TC# for the UP */
5595 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5596 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5597 	}
5598 }
5599 
5600 /**
5601  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5602  * @vsi: the VSI being configured
5603  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5604  **/
5605 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5606 				      struct i40e_vsi_context *ctxt)
5607 {
5608 	/* copy just the sections touched not the entire info
5609 	 * since not all sections are valid as returned by
5610 	 * update vsi params
5611 	 */
5612 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5613 	memcpy(&vsi->info.queue_mapping,
5614 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5615 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5616 	       sizeof(vsi->info.tc_mapping));
5617 }
5618 
5619 /**
5620  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5621  * @vsi: the VSI being reconfigured
5622  * @vsi_offset: offset from main VF VSI
5623  */
5624 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5625 {
5626 	struct i40e_vsi_context ctxt = {};
5627 	struct i40e_pf *pf;
5628 	struct i40e_hw *hw;
5629 	int ret;
5630 
5631 	if (!vsi)
5632 		return I40E_ERR_PARAM;
5633 	pf = vsi->back;
5634 	hw = &pf->hw;
5635 
5636 	ctxt.seid = vsi->seid;
5637 	ctxt.pf_num = hw->pf_id;
5638 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5639 	ctxt.uplink_seid = vsi->uplink_seid;
5640 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5641 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5642 	ctxt.info = vsi->info;
5643 
5644 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5645 				 false);
5646 	if (vsi->reconfig_rss) {
5647 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5648 				      vsi->num_queue_pairs);
5649 		ret = i40e_vsi_config_rss(vsi);
5650 		if (ret) {
5651 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5652 			return ret;
5653 		}
5654 		vsi->reconfig_rss = false;
5655 	}
5656 
5657 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5658 	if (ret) {
5659 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5660 			 i40e_stat_str(hw, ret),
5661 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5662 		return ret;
5663 	}
5664 	/* update the local VSI info with updated queue map */
5665 	i40e_vsi_update_queue_map(vsi, &ctxt);
5666 	vsi->info.valid_sections = 0;
5667 
5668 	return ret;
5669 }
5670 
5671 /**
5672  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5673  * @vsi: VSI to be configured
5674  * @enabled_tc: TC bitmap
5675  *
5676  * This configures a particular VSI for TCs that are mapped to the
5677  * given TC bitmap. It uses default bandwidth share for TCs across
5678  * VSIs to configure TC for a particular VSI.
5679  *
5680  * NOTE:
5681  * It is expected that the VSI queues have been quisced before calling
5682  * this function.
5683  **/
5684 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5685 {
5686 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5687 	struct i40e_pf *pf = vsi->back;
5688 	struct i40e_hw *hw = &pf->hw;
5689 	struct i40e_vsi_context ctxt;
5690 	int ret = 0;
5691 	int i;
5692 
5693 	/* Check if enabled_tc is same as existing or new TCs */
5694 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5695 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5696 		return ret;
5697 
5698 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5699 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5700 		if (enabled_tc & BIT(i))
5701 			bw_share[i] = 1;
5702 	}
5703 
5704 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5705 	if (ret) {
5706 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5707 
5708 		dev_info(&pf->pdev->dev,
5709 			 "Failed configuring TC map %d for VSI %d\n",
5710 			 enabled_tc, vsi->seid);
5711 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5712 						  &bw_config, NULL);
5713 		if (ret) {
5714 			dev_info(&pf->pdev->dev,
5715 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5716 				 i40e_stat_str(hw, ret),
5717 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5718 			goto out;
5719 		}
5720 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5721 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5722 
5723 			if (!valid_tc)
5724 				valid_tc = bw_config.tc_valid_bits;
5725 			/* Always enable TC0, no matter what */
5726 			valid_tc |= 1;
5727 			dev_info(&pf->pdev->dev,
5728 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5729 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5730 			enabled_tc = valid_tc;
5731 		}
5732 
5733 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5734 		if (ret) {
5735 			dev_err(&pf->pdev->dev,
5736 				"Unable to  configure TC map %d for VSI %d\n",
5737 				enabled_tc, vsi->seid);
5738 			goto out;
5739 		}
5740 	}
5741 
5742 	/* Update Queue Pairs Mapping for currently enabled UPs */
5743 	ctxt.seid = vsi->seid;
5744 	ctxt.pf_num = vsi->back->hw.pf_id;
5745 	ctxt.vf_num = 0;
5746 	ctxt.uplink_seid = vsi->uplink_seid;
5747 	ctxt.info = vsi->info;
5748 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5749 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5750 		if (ret)
5751 			goto out;
5752 	} else {
5753 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5754 	}
5755 
5756 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5757 	 * queues changed.
5758 	 */
5759 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5760 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5761 				      vsi->num_queue_pairs);
5762 		ret = i40e_vsi_config_rss(vsi);
5763 		if (ret) {
5764 			dev_info(&vsi->back->pdev->dev,
5765 				 "Failed to reconfig rss for num_queues\n");
5766 			return ret;
5767 		}
5768 		vsi->reconfig_rss = false;
5769 	}
5770 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5771 		ctxt.info.valid_sections |=
5772 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5773 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5774 	}
5775 
5776 	/* Update the VSI after updating the VSI queue-mapping
5777 	 * information
5778 	 */
5779 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5780 	if (ret) {
5781 		dev_info(&pf->pdev->dev,
5782 			 "Update vsi tc config failed, err %s aq_err %s\n",
5783 			 i40e_stat_str(hw, ret),
5784 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5785 		goto out;
5786 	}
5787 	/* update the local VSI info with updated queue map */
5788 	i40e_vsi_update_queue_map(vsi, &ctxt);
5789 	vsi->info.valid_sections = 0;
5790 
5791 	/* Update current VSI BW information */
5792 	ret = i40e_vsi_get_bw_info(vsi);
5793 	if (ret) {
5794 		dev_info(&pf->pdev->dev,
5795 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5796 			 i40e_stat_str(hw, ret),
5797 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5798 		goto out;
5799 	}
5800 
5801 	/* Update the netdev TC setup */
5802 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5803 out:
5804 	return ret;
5805 }
5806 
5807 /**
5808  * i40e_get_link_speed - Returns link speed for the interface
5809  * @vsi: VSI to be configured
5810  *
5811  **/
5812 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5813 {
5814 	struct i40e_pf *pf = vsi->back;
5815 
5816 	switch (pf->hw.phy.link_info.link_speed) {
5817 	case I40E_LINK_SPEED_40GB:
5818 		return 40000;
5819 	case I40E_LINK_SPEED_25GB:
5820 		return 25000;
5821 	case I40E_LINK_SPEED_20GB:
5822 		return 20000;
5823 	case I40E_LINK_SPEED_10GB:
5824 		return 10000;
5825 	case I40E_LINK_SPEED_1GB:
5826 		return 1000;
5827 	default:
5828 		return -EINVAL;
5829 	}
5830 }
5831 
5832 /**
5833  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5834  * @vsi: VSI to be configured
5835  * @seid: seid of the channel/VSI
5836  * @max_tx_rate: max TX rate to be configured as BW limit
5837  *
5838  * Helper function to set BW limit for a given VSI
5839  **/
5840 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5841 {
5842 	struct i40e_pf *pf = vsi->back;
5843 	u64 credits = 0;
5844 	int speed = 0;
5845 	int ret = 0;
5846 
5847 	speed = i40e_get_link_speed(vsi);
5848 	if (max_tx_rate > speed) {
5849 		dev_err(&pf->pdev->dev,
5850 			"Invalid max tx rate %llu specified for VSI seid %d.",
5851 			max_tx_rate, seid);
5852 		return -EINVAL;
5853 	}
5854 	if (max_tx_rate && max_tx_rate < 50) {
5855 		dev_warn(&pf->pdev->dev,
5856 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5857 		max_tx_rate = 50;
5858 	}
5859 
5860 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5861 	credits = max_tx_rate;
5862 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5863 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5864 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5865 	if (ret)
5866 		dev_err(&pf->pdev->dev,
5867 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5868 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5869 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5870 	return ret;
5871 }
5872 
5873 /**
5874  * i40e_remove_queue_channels - Remove queue channels for the TCs
5875  * @vsi: VSI to be configured
5876  *
5877  * Remove queue channels for the TCs
5878  **/
5879 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5880 {
5881 	enum i40e_admin_queue_err last_aq_status;
5882 	struct i40e_cloud_filter *cfilter;
5883 	struct i40e_channel *ch, *ch_tmp;
5884 	struct i40e_pf *pf = vsi->back;
5885 	struct hlist_node *node;
5886 	int ret, i;
5887 
5888 	/* Reset rss size that was stored when reconfiguring rss for
5889 	 * channel VSIs with non-power-of-2 queue count.
5890 	 */
5891 	vsi->current_rss_size = 0;
5892 
5893 	/* perform cleanup for channels if they exist */
5894 	if (list_empty(&vsi->ch_list))
5895 		return;
5896 
5897 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5898 		struct i40e_vsi *p_vsi;
5899 
5900 		list_del(&ch->list);
5901 		p_vsi = ch->parent_vsi;
5902 		if (!p_vsi || !ch->initialized) {
5903 			kfree(ch);
5904 			continue;
5905 		}
5906 		/* Reset queue contexts */
5907 		for (i = 0; i < ch->num_queue_pairs; i++) {
5908 			struct i40e_ring *tx_ring, *rx_ring;
5909 			u16 pf_q;
5910 
5911 			pf_q = ch->base_queue + i;
5912 			tx_ring = vsi->tx_rings[pf_q];
5913 			tx_ring->ch = NULL;
5914 
5915 			rx_ring = vsi->rx_rings[pf_q];
5916 			rx_ring->ch = NULL;
5917 		}
5918 
5919 		/* Reset BW configured for this VSI via mqprio */
5920 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5921 		if (ret)
5922 			dev_info(&vsi->back->pdev->dev,
5923 				 "Failed to reset tx rate for ch->seid %u\n",
5924 				 ch->seid);
5925 
5926 		/* delete cloud filters associated with this channel */
5927 		hlist_for_each_entry_safe(cfilter, node,
5928 					  &pf->cloud_filter_list, cloud_node) {
5929 			if (cfilter->seid != ch->seid)
5930 				continue;
5931 
5932 			hash_del(&cfilter->cloud_node);
5933 			if (cfilter->dst_port)
5934 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5935 									cfilter,
5936 									false);
5937 			else
5938 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5939 								false);
5940 			last_aq_status = pf->hw.aq.asq_last_status;
5941 			if (ret)
5942 				dev_info(&pf->pdev->dev,
5943 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5944 					 i40e_stat_str(&pf->hw, ret),
5945 					 i40e_aq_str(&pf->hw, last_aq_status));
5946 			kfree(cfilter);
5947 		}
5948 
5949 		/* delete VSI from FW */
5950 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5951 					     NULL);
5952 		if (ret)
5953 			dev_err(&vsi->back->pdev->dev,
5954 				"unable to remove channel (%d) for parent VSI(%d)\n",
5955 				ch->seid, p_vsi->seid);
5956 		kfree(ch);
5957 	}
5958 	INIT_LIST_HEAD(&vsi->ch_list);
5959 }
5960 
5961 /**
5962  * i40e_get_max_queues_for_channel
5963  * @vsi: ptr to VSI to which channels are associated with
5964  *
5965  * Helper function which returns max value among the queue counts set on the
5966  * channels/TCs created.
5967  **/
5968 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5969 {
5970 	struct i40e_channel *ch, *ch_tmp;
5971 	int max = 0;
5972 
5973 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5974 		if (!ch->initialized)
5975 			continue;
5976 		if (ch->num_queue_pairs > max)
5977 			max = ch->num_queue_pairs;
5978 	}
5979 
5980 	return max;
5981 }
5982 
5983 /**
5984  * i40e_validate_num_queues - validate num_queues w.r.t channel
5985  * @pf: ptr to PF device
5986  * @num_queues: number of queues
5987  * @vsi: the parent VSI
5988  * @reconfig_rss: indicates should the RSS be reconfigured or not
5989  *
5990  * This function validates number of queues in the context of new channel
5991  * which is being established and determines if RSS should be reconfigured
5992  * or not for parent VSI.
5993  **/
5994 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5995 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5996 {
5997 	int max_ch_queues;
5998 
5999 	if (!reconfig_rss)
6000 		return -EINVAL;
6001 
6002 	*reconfig_rss = false;
6003 	if (vsi->current_rss_size) {
6004 		if (num_queues > vsi->current_rss_size) {
6005 			dev_dbg(&pf->pdev->dev,
6006 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6007 				num_queues, vsi->current_rss_size);
6008 			return -EINVAL;
6009 		} else if ((num_queues < vsi->current_rss_size) &&
6010 			   (!is_power_of_2(num_queues))) {
6011 			dev_dbg(&pf->pdev->dev,
6012 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6013 				num_queues, vsi->current_rss_size);
6014 			return -EINVAL;
6015 		}
6016 	}
6017 
6018 	if (!is_power_of_2(num_queues)) {
6019 		/* Find the max num_queues configured for channel if channel
6020 		 * exist.
6021 		 * if channel exist, then enforce 'num_queues' to be more than
6022 		 * max ever queues configured for channel.
6023 		 */
6024 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6025 		if (num_queues < max_ch_queues) {
6026 			dev_dbg(&pf->pdev->dev,
6027 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6028 				num_queues, max_ch_queues);
6029 			return -EINVAL;
6030 		}
6031 		*reconfig_rss = true;
6032 	}
6033 
6034 	return 0;
6035 }
6036 
6037 /**
6038  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6039  * @vsi: the VSI being setup
6040  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6041  *
6042  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6043  **/
6044 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6045 {
6046 	struct i40e_pf *pf = vsi->back;
6047 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6048 	struct i40e_hw *hw = &pf->hw;
6049 	int local_rss_size;
6050 	u8 *lut;
6051 	int ret;
6052 
6053 	if (!vsi->rss_size)
6054 		return -EINVAL;
6055 
6056 	if (rss_size > vsi->rss_size)
6057 		return -EINVAL;
6058 
6059 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6060 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6061 	if (!lut)
6062 		return -ENOMEM;
6063 
6064 	/* Ignoring user configured lut if there is one */
6065 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6066 
6067 	/* Use user configured hash key if there is one, otherwise
6068 	 * use default.
6069 	 */
6070 	if (vsi->rss_hkey_user)
6071 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6072 	else
6073 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6074 
6075 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6076 	if (ret) {
6077 		dev_info(&pf->pdev->dev,
6078 			 "Cannot set RSS lut, err %s aq_err %s\n",
6079 			 i40e_stat_str(hw, ret),
6080 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6081 		kfree(lut);
6082 		return ret;
6083 	}
6084 	kfree(lut);
6085 
6086 	/* Do the update w.r.t. storing rss_size */
6087 	if (!vsi->orig_rss_size)
6088 		vsi->orig_rss_size = vsi->rss_size;
6089 	vsi->current_rss_size = local_rss_size;
6090 
6091 	return ret;
6092 }
6093 
6094 /**
6095  * i40e_channel_setup_queue_map - Setup a channel queue map
6096  * @pf: ptr to PF device
6097  * @ctxt: VSI context structure
6098  * @ch: ptr to channel structure
6099  *
6100  * Setup queue map for a specific channel
6101  **/
6102 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6103 					 struct i40e_vsi_context *ctxt,
6104 					 struct i40e_channel *ch)
6105 {
6106 	u16 qcount, qmap, sections = 0;
6107 	u8 offset = 0;
6108 	int pow;
6109 
6110 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6111 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6112 
6113 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6114 	ch->num_queue_pairs = qcount;
6115 
6116 	/* find the next higher power-of-2 of num queue pairs */
6117 	pow = ilog2(qcount);
6118 	if (!is_power_of_2(qcount))
6119 		pow++;
6120 
6121 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6122 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6123 
6124 	/* Setup queue TC[0].qmap for given VSI context */
6125 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6126 
6127 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6128 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6129 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6130 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6131 }
6132 
6133 /**
6134  * i40e_add_channel - add a channel by adding VSI
6135  * @pf: ptr to PF device
6136  * @uplink_seid: underlying HW switching element (VEB) ID
6137  * @ch: ptr to channel structure
6138  *
6139  * Add a channel (VSI) using add_vsi and queue_map
6140  **/
6141 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6142 			    struct i40e_channel *ch)
6143 {
6144 	struct i40e_hw *hw = &pf->hw;
6145 	struct i40e_vsi_context ctxt;
6146 	u8 enabled_tc = 0x1; /* TC0 enabled */
6147 	int ret;
6148 
6149 	if (ch->type != I40E_VSI_VMDQ2) {
6150 		dev_info(&pf->pdev->dev,
6151 			 "add new vsi failed, ch->type %d\n", ch->type);
6152 		return -EINVAL;
6153 	}
6154 
6155 	memset(&ctxt, 0, sizeof(ctxt));
6156 	ctxt.pf_num = hw->pf_id;
6157 	ctxt.vf_num = 0;
6158 	ctxt.uplink_seid = uplink_seid;
6159 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6160 	if (ch->type == I40E_VSI_VMDQ2)
6161 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6162 
6163 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6164 		ctxt.info.valid_sections |=
6165 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6166 		ctxt.info.switch_id =
6167 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6168 	}
6169 
6170 	/* Set queue map for a given VSI context */
6171 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6172 
6173 	/* Now time to create VSI */
6174 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6175 	if (ret) {
6176 		dev_info(&pf->pdev->dev,
6177 			 "add new vsi failed, err %s aq_err %s\n",
6178 			 i40e_stat_str(&pf->hw, ret),
6179 			 i40e_aq_str(&pf->hw,
6180 				     pf->hw.aq.asq_last_status));
6181 		return -ENOENT;
6182 	}
6183 
6184 	/* Success, update channel, set enabled_tc only if the channel
6185 	 * is not a macvlan
6186 	 */
6187 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6188 	ch->seid = ctxt.seid;
6189 	ch->vsi_number = ctxt.vsi_number;
6190 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6191 
6192 	/* copy just the sections touched not the entire info
6193 	 * since not all sections are valid as returned by
6194 	 * update vsi params
6195 	 */
6196 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6197 	memcpy(&ch->info.queue_mapping,
6198 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6199 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6200 	       sizeof(ctxt.info.tc_mapping));
6201 
6202 	return 0;
6203 }
6204 
6205 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6206 				  u8 *bw_share)
6207 {
6208 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6209 	i40e_status ret;
6210 	int i;
6211 
6212 	memset(&bw_data, 0, sizeof(bw_data));
6213 	bw_data.tc_valid_bits = ch->enabled_tc;
6214 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6215 		bw_data.tc_bw_credits[i] = bw_share[i];
6216 
6217 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6218 				       &bw_data, NULL);
6219 	if (ret) {
6220 		dev_info(&vsi->back->pdev->dev,
6221 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6222 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6223 		return -EINVAL;
6224 	}
6225 
6226 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6227 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6228 
6229 	return 0;
6230 }
6231 
6232 /**
6233  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6234  * @pf: ptr to PF device
6235  * @vsi: the VSI being setup
6236  * @ch: ptr to channel structure
6237  *
6238  * Configure TX rings associated with channel (VSI) since queues are being
6239  * from parent VSI.
6240  **/
6241 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6242 				       struct i40e_vsi *vsi,
6243 				       struct i40e_channel *ch)
6244 {
6245 	i40e_status ret;
6246 	int i;
6247 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6248 
6249 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6250 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6251 		if (ch->enabled_tc & BIT(i))
6252 			bw_share[i] = 1;
6253 	}
6254 
6255 	/* configure BW for new VSI */
6256 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6257 	if (ret) {
6258 		dev_info(&vsi->back->pdev->dev,
6259 			 "Failed configuring TC map %d for channel (seid %u)\n",
6260 			 ch->enabled_tc, ch->seid);
6261 		return ret;
6262 	}
6263 
6264 	for (i = 0; i < ch->num_queue_pairs; i++) {
6265 		struct i40e_ring *tx_ring, *rx_ring;
6266 		u16 pf_q;
6267 
6268 		pf_q = ch->base_queue + i;
6269 
6270 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6271 		 * context
6272 		 */
6273 		tx_ring = vsi->tx_rings[pf_q];
6274 		tx_ring->ch = ch;
6275 
6276 		/* Get the RX ring ptr */
6277 		rx_ring = vsi->rx_rings[pf_q];
6278 		rx_ring->ch = ch;
6279 	}
6280 
6281 	return 0;
6282 }
6283 
6284 /**
6285  * i40e_setup_hw_channel - setup new channel
6286  * @pf: ptr to PF device
6287  * @vsi: the VSI being setup
6288  * @ch: ptr to channel structure
6289  * @uplink_seid: underlying HW switching element (VEB) ID
6290  * @type: type of channel to be created (VMDq2/VF)
6291  *
6292  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6293  * and configures TX rings accordingly
6294  **/
6295 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6296 					struct i40e_vsi *vsi,
6297 					struct i40e_channel *ch,
6298 					u16 uplink_seid, u8 type)
6299 {
6300 	int ret;
6301 
6302 	ch->initialized = false;
6303 	ch->base_queue = vsi->next_base_queue;
6304 	ch->type = type;
6305 
6306 	/* Proceed with creation of channel (VMDq2) VSI */
6307 	ret = i40e_add_channel(pf, uplink_seid, ch);
6308 	if (ret) {
6309 		dev_info(&pf->pdev->dev,
6310 			 "failed to add_channel using uplink_seid %u\n",
6311 			 uplink_seid);
6312 		return ret;
6313 	}
6314 
6315 	/* Mark the successful creation of channel */
6316 	ch->initialized = true;
6317 
6318 	/* Reconfigure TX queues using QTX_CTL register */
6319 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6320 	if (ret) {
6321 		dev_info(&pf->pdev->dev,
6322 			 "failed to configure TX rings for channel %u\n",
6323 			 ch->seid);
6324 		return ret;
6325 	}
6326 
6327 	/* update 'next_base_queue' */
6328 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6329 	dev_dbg(&pf->pdev->dev,
6330 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6331 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6332 		ch->num_queue_pairs,
6333 		vsi->next_base_queue);
6334 	return ret;
6335 }
6336 
6337 /**
6338  * i40e_setup_channel - setup new channel using uplink element
6339  * @pf: ptr to PF device
6340  * @vsi: pointer to the VSI to set up the channel within
6341  * @ch: ptr to channel structure
6342  *
6343  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6344  * and uplink switching element (uplink_seid)
6345  **/
6346 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6347 			       struct i40e_channel *ch)
6348 {
6349 	u8 vsi_type;
6350 	u16 seid;
6351 	int ret;
6352 
6353 	if (vsi->type == I40E_VSI_MAIN) {
6354 		vsi_type = I40E_VSI_VMDQ2;
6355 	} else {
6356 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6357 			vsi->type);
6358 		return false;
6359 	}
6360 
6361 	/* underlying switching element */
6362 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6363 
6364 	/* create channel (VSI), configure TX rings */
6365 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6366 	if (ret) {
6367 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6368 		return false;
6369 	}
6370 
6371 	return ch->initialized ? true : false;
6372 }
6373 
6374 /**
6375  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6376  * @vsi: ptr to VSI which has PF backing
6377  *
6378  * Sets up switch mode correctly if it needs to be changed and perform
6379  * what are allowed modes.
6380  **/
6381 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6382 {
6383 	u8 mode;
6384 	struct i40e_pf *pf = vsi->back;
6385 	struct i40e_hw *hw = &pf->hw;
6386 	int ret;
6387 
6388 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6389 	if (ret)
6390 		return -EINVAL;
6391 
6392 	if (hw->dev_caps.switch_mode) {
6393 		/* if switch mode is set, support mode2 (non-tunneled for
6394 		 * cloud filter) for now
6395 		 */
6396 		u32 switch_mode = hw->dev_caps.switch_mode &
6397 				  I40E_SWITCH_MODE_MASK;
6398 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6399 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6400 				return 0;
6401 			dev_err(&pf->pdev->dev,
6402 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6403 				hw->dev_caps.switch_mode);
6404 			return -EINVAL;
6405 		}
6406 	}
6407 
6408 	/* Set Bit 7 to be valid */
6409 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6410 
6411 	/* Set L4type for TCP support */
6412 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6413 
6414 	/* Set cloud filter mode */
6415 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6416 
6417 	/* Prep mode field for set_switch_config */
6418 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6419 					pf->last_sw_conf_valid_flags,
6420 					mode, NULL);
6421 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6422 		dev_err(&pf->pdev->dev,
6423 			"couldn't set switch config bits, err %s aq_err %s\n",
6424 			i40e_stat_str(hw, ret),
6425 			i40e_aq_str(hw,
6426 				    hw->aq.asq_last_status));
6427 
6428 	return ret;
6429 }
6430 
6431 /**
6432  * i40e_create_queue_channel - function to create channel
6433  * @vsi: VSI to be configured
6434  * @ch: ptr to channel (it contains channel specific params)
6435  *
6436  * This function creates channel (VSI) using num_queues specified by user,
6437  * reconfigs RSS if needed.
6438  **/
6439 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6440 			      struct i40e_channel *ch)
6441 {
6442 	struct i40e_pf *pf = vsi->back;
6443 	bool reconfig_rss;
6444 	int err;
6445 
6446 	if (!ch)
6447 		return -EINVAL;
6448 
6449 	if (!ch->num_queue_pairs) {
6450 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6451 			ch->num_queue_pairs);
6452 		return -EINVAL;
6453 	}
6454 
6455 	/* validate user requested num_queues for channel */
6456 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6457 				       &reconfig_rss);
6458 	if (err) {
6459 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6460 			 ch->num_queue_pairs);
6461 		return -EINVAL;
6462 	}
6463 
6464 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6465 	 * VSI to be added switch to VEB mode.
6466 	 */
6467 
6468 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6469 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6470 
6471 		if (vsi->type == I40E_VSI_MAIN) {
6472 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6473 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6474 			else
6475 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6476 		}
6477 		/* now onwards for main VSI, number of queues will be value
6478 		 * of TC0's queue count
6479 		 */
6480 	}
6481 
6482 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6483 	 * it should be more than num_queues
6484 	 */
6485 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6486 		dev_dbg(&pf->pdev->dev,
6487 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6488 			vsi->cnt_q_avail, ch->num_queue_pairs);
6489 		return -EINVAL;
6490 	}
6491 
6492 	/* reconfig_rss only if vsi type is MAIN_VSI */
6493 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6494 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6495 		if (err) {
6496 			dev_info(&pf->pdev->dev,
6497 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6498 				 ch->num_queue_pairs);
6499 			return -EINVAL;
6500 		}
6501 	}
6502 
6503 	if (!i40e_setup_channel(pf, vsi, ch)) {
6504 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6505 		return -EINVAL;
6506 	}
6507 
6508 	dev_info(&pf->pdev->dev,
6509 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6510 		 ch->seid, ch->num_queue_pairs);
6511 
6512 	/* configure VSI for BW limit */
6513 	if (ch->max_tx_rate) {
6514 		u64 credits = ch->max_tx_rate;
6515 
6516 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6517 			return -EINVAL;
6518 
6519 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6520 		dev_dbg(&pf->pdev->dev,
6521 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6522 			ch->max_tx_rate,
6523 			credits,
6524 			ch->seid);
6525 	}
6526 
6527 	/* in case of VF, this will be main SRIOV VSI */
6528 	ch->parent_vsi = vsi;
6529 
6530 	/* and update main_vsi's count for queue_available to use */
6531 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6532 
6533 	return 0;
6534 }
6535 
6536 /**
6537  * i40e_configure_queue_channels - Add queue channel for the given TCs
6538  * @vsi: VSI to be configured
6539  *
6540  * Configures queue channel mapping to the given TCs
6541  **/
6542 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6543 {
6544 	struct i40e_channel *ch;
6545 	u64 max_rate = 0;
6546 	int ret = 0, i;
6547 
6548 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6549 	vsi->tc_seid_map[0] = vsi->seid;
6550 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6551 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6552 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6553 			if (!ch) {
6554 				ret = -ENOMEM;
6555 				goto err_free;
6556 			}
6557 
6558 			INIT_LIST_HEAD(&ch->list);
6559 			ch->num_queue_pairs =
6560 				vsi->tc_config.tc_info[i].qcount;
6561 			ch->base_queue =
6562 				vsi->tc_config.tc_info[i].qoffset;
6563 
6564 			/* Bandwidth limit through tc interface is in bytes/s,
6565 			 * change to Mbit/s
6566 			 */
6567 			max_rate = vsi->mqprio_qopt.max_rate[i];
6568 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6569 			ch->max_tx_rate = max_rate;
6570 
6571 			list_add_tail(&ch->list, &vsi->ch_list);
6572 
6573 			ret = i40e_create_queue_channel(vsi, ch);
6574 			if (ret) {
6575 				dev_err(&vsi->back->pdev->dev,
6576 					"Failed creating queue channel with TC%d: queues %d\n",
6577 					i, ch->num_queue_pairs);
6578 				goto err_free;
6579 			}
6580 			vsi->tc_seid_map[i] = ch->seid;
6581 		}
6582 	}
6583 	return ret;
6584 
6585 err_free:
6586 	i40e_remove_queue_channels(vsi);
6587 	return ret;
6588 }
6589 
6590 /**
6591  * i40e_veb_config_tc - Configure TCs for given VEB
6592  * @veb: given VEB
6593  * @enabled_tc: TC bitmap
6594  *
6595  * Configures given TC bitmap for VEB (switching) element
6596  **/
6597 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6598 {
6599 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6600 	struct i40e_pf *pf = veb->pf;
6601 	int ret = 0;
6602 	int i;
6603 
6604 	/* No TCs or already enabled TCs just return */
6605 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6606 		return ret;
6607 
6608 	bw_data.tc_valid_bits = enabled_tc;
6609 	/* bw_data.absolute_credits is not set (relative) */
6610 
6611 	/* Enable ETS TCs with equal BW Share for now */
6612 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6613 		if (enabled_tc & BIT(i))
6614 			bw_data.tc_bw_share_credits[i] = 1;
6615 	}
6616 
6617 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6618 						   &bw_data, NULL);
6619 	if (ret) {
6620 		dev_info(&pf->pdev->dev,
6621 			 "VEB bw config failed, err %s aq_err %s\n",
6622 			 i40e_stat_str(&pf->hw, ret),
6623 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6624 		goto out;
6625 	}
6626 
6627 	/* Update the BW information */
6628 	ret = i40e_veb_get_bw_info(veb);
6629 	if (ret) {
6630 		dev_info(&pf->pdev->dev,
6631 			 "Failed getting veb bw config, err %s aq_err %s\n",
6632 			 i40e_stat_str(&pf->hw, ret),
6633 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6634 	}
6635 
6636 out:
6637 	return ret;
6638 }
6639 
6640 #ifdef CONFIG_I40E_DCB
6641 /**
6642  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6643  * @pf: PF struct
6644  *
6645  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6646  * the caller would've quiesce all the VSIs before calling
6647  * this function
6648  **/
6649 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6650 {
6651 	u8 tc_map = 0;
6652 	int ret;
6653 	u8 v;
6654 
6655 	/* Enable the TCs available on PF to all VEBs */
6656 	tc_map = i40e_pf_get_tc_map(pf);
6657 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6658 		return;
6659 
6660 	for (v = 0; v < I40E_MAX_VEB; v++) {
6661 		if (!pf->veb[v])
6662 			continue;
6663 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6664 		if (ret) {
6665 			dev_info(&pf->pdev->dev,
6666 				 "Failed configuring TC for VEB seid=%d\n",
6667 				 pf->veb[v]->seid);
6668 			/* Will try to configure as many components */
6669 		}
6670 	}
6671 
6672 	/* Update each VSI */
6673 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6674 		if (!pf->vsi[v])
6675 			continue;
6676 
6677 		/* - Enable all TCs for the LAN VSI
6678 		 * - For all others keep them at TC0 for now
6679 		 */
6680 		if (v == pf->lan_vsi)
6681 			tc_map = i40e_pf_get_tc_map(pf);
6682 		else
6683 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6684 
6685 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6686 		if (ret) {
6687 			dev_info(&pf->pdev->dev,
6688 				 "Failed configuring TC for VSI seid=%d\n",
6689 				 pf->vsi[v]->seid);
6690 			/* Will try to configure as many components */
6691 		} else {
6692 			/* Re-configure VSI vectors based on updated TC map */
6693 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6694 			if (pf->vsi[v]->netdev)
6695 				i40e_dcbnl_set_all(pf->vsi[v]);
6696 		}
6697 	}
6698 }
6699 
6700 /**
6701  * i40e_resume_port_tx - Resume port Tx
6702  * @pf: PF struct
6703  *
6704  * Resume a port's Tx and issue a PF reset in case of failure to
6705  * resume.
6706  **/
6707 static int i40e_resume_port_tx(struct i40e_pf *pf)
6708 {
6709 	struct i40e_hw *hw = &pf->hw;
6710 	int ret;
6711 
6712 	ret = i40e_aq_resume_port_tx(hw, NULL);
6713 	if (ret) {
6714 		dev_info(&pf->pdev->dev,
6715 			 "Resume Port Tx failed, err %s aq_err %s\n",
6716 			  i40e_stat_str(&pf->hw, ret),
6717 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6718 		/* Schedule PF reset to recover */
6719 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6720 		i40e_service_event_schedule(pf);
6721 	}
6722 
6723 	return ret;
6724 }
6725 
6726 /**
6727  * i40e_suspend_port_tx - Suspend port Tx
6728  * @pf: PF struct
6729  *
6730  * Suspend a port's Tx and issue a PF reset in case of failure.
6731  **/
6732 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6733 {
6734 	struct i40e_hw *hw = &pf->hw;
6735 	int ret;
6736 
6737 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6738 	if (ret) {
6739 		dev_info(&pf->pdev->dev,
6740 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6741 			 i40e_stat_str(&pf->hw, ret),
6742 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6743 		/* Schedule PF reset to recover */
6744 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6745 		i40e_service_event_schedule(pf);
6746 	}
6747 
6748 	return ret;
6749 }
6750 
6751 /**
6752  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6753  * @pf: PF being configured
6754  * @new_cfg: New DCBX configuration
6755  *
6756  * Program DCB settings into HW and reconfigure VEB/VSIs on
6757  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6758  **/
6759 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6760 				  struct i40e_dcbx_config *new_cfg)
6761 {
6762 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6763 	int ret;
6764 
6765 	/* Check if need reconfiguration */
6766 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6767 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6768 		return 0;
6769 	}
6770 
6771 	/* Config change disable all VSIs */
6772 	i40e_pf_quiesce_all_vsi(pf);
6773 
6774 	/* Copy the new config to the current config */
6775 	*old_cfg = *new_cfg;
6776 	old_cfg->etsrec = old_cfg->etscfg;
6777 	ret = i40e_set_dcb_config(&pf->hw);
6778 	if (ret) {
6779 		dev_info(&pf->pdev->dev,
6780 			 "Set DCB Config failed, err %s aq_err %s\n",
6781 			 i40e_stat_str(&pf->hw, ret),
6782 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6783 		goto out;
6784 	}
6785 
6786 	/* Changes in configuration update VEB/VSI */
6787 	i40e_dcb_reconfigure(pf);
6788 out:
6789 	/* In case of reset do not try to resume anything */
6790 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6791 		/* Re-start the VSIs if disabled */
6792 		ret = i40e_resume_port_tx(pf);
6793 		/* In case of error no point in resuming VSIs */
6794 		if (ret)
6795 			goto err;
6796 		i40e_pf_unquiesce_all_vsi(pf);
6797 	}
6798 err:
6799 	return ret;
6800 }
6801 
6802 /**
6803  * i40e_hw_dcb_config - Program new DCBX settings into HW
6804  * @pf: PF being configured
6805  * @new_cfg: New DCBX configuration
6806  *
6807  * Program DCB settings into HW and reconfigure VEB/VSIs on
6808  * given PF
6809  **/
6810 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6811 {
6812 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6813 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6814 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6815 	struct i40e_dcbx_config *old_cfg;
6816 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6817 	struct i40e_rx_pb_config pb_cfg;
6818 	struct i40e_hw *hw = &pf->hw;
6819 	u8 num_ports = hw->num_ports;
6820 	bool need_reconfig;
6821 	int ret = -EINVAL;
6822 	u8 lltc_map = 0;
6823 	u8 tc_map = 0;
6824 	u8 new_numtc;
6825 	u8 i;
6826 
6827 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6828 	/* Un-pack information to Program ETS HW via shared API
6829 	 * numtc, tcmap
6830 	 * LLTC map
6831 	 * ETS/NON-ETS arbiter mode
6832 	 * max exponent (credit refills)
6833 	 * Total number of ports
6834 	 * PFC priority bit-map
6835 	 * Priority Table
6836 	 * BW % per TC
6837 	 * Arbiter mode between UPs sharing same TC
6838 	 * TSA table (ETS or non-ETS)
6839 	 * EEE enabled or not
6840 	 * MFS TC table
6841 	 */
6842 
6843 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6844 
6845 	memset(&ets_data, 0, sizeof(ets_data));
6846 	for (i = 0; i < new_numtc; i++) {
6847 		tc_map |= BIT(i);
6848 		switch (new_cfg->etscfg.tsatable[i]) {
6849 		case I40E_IEEE_TSA_ETS:
6850 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6851 			ets_data.tc_bw_share_credits[i] =
6852 					new_cfg->etscfg.tcbwtable[i];
6853 			break;
6854 		case I40E_IEEE_TSA_STRICT:
6855 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6856 			lltc_map |= BIT(i);
6857 			ets_data.tc_bw_share_credits[i] =
6858 					I40E_DCB_STRICT_PRIO_CREDITS;
6859 			break;
6860 		default:
6861 			/* Invalid TSA type */
6862 			need_reconfig = false;
6863 			goto out;
6864 		}
6865 	}
6866 
6867 	old_cfg = &hw->local_dcbx_config;
6868 	/* Check if need reconfiguration */
6869 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6870 
6871 	/* If needed, enable/disable frame tagging, disable all VSIs
6872 	 * and suspend port tx
6873 	 */
6874 	if (need_reconfig) {
6875 		/* Enable DCB tagging only when more than one TC */
6876 		if (new_numtc > 1)
6877 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6878 		else
6879 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6880 
6881 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6882 		/* Reconfiguration needed quiesce all VSIs */
6883 		i40e_pf_quiesce_all_vsi(pf);
6884 		ret = i40e_suspend_port_tx(pf);
6885 		if (ret)
6886 			goto err;
6887 	}
6888 
6889 	/* Configure Port ETS Tx Scheduler */
6890 	ets_data.tc_valid_bits = tc_map;
6891 	ets_data.tc_strict_priority_flags = lltc_map;
6892 	ret = i40e_aq_config_switch_comp_ets
6893 		(hw, pf->mac_seid, &ets_data,
6894 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6895 	if (ret) {
6896 		dev_info(&pf->pdev->dev,
6897 			 "Modify Port ETS failed, err %s aq_err %s\n",
6898 			 i40e_stat_str(&pf->hw, ret),
6899 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6900 		goto out;
6901 	}
6902 
6903 	/* Configure Rx ETS HW */
6904 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6905 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6906 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6907 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6908 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6909 				   lltc_map);
6910 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6911 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6912 				     prio_type);
6913 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6914 			       new_cfg->etscfg.prioritytable);
6915 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6916 
6917 	/* Configure Rx Packet Buffers in HW */
6918 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6919 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6920 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6921 	}
6922 
6923 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6924 					 false, new_cfg->pfc.pfcenable,
6925 					 mfs_tc, &pb_cfg);
6926 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6927 
6928 	/* Update the local Rx Packet buffer config */
6929 	pf->pb_cfg = pb_cfg;
6930 
6931 	/* Inform the FW about changes to DCB configuration */
6932 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6933 	if (ret) {
6934 		dev_info(&pf->pdev->dev,
6935 			 "DCB Updated failed, err %s aq_err %s\n",
6936 			 i40e_stat_str(&pf->hw, ret),
6937 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6938 		goto out;
6939 	}
6940 
6941 	/* Update the port DCBx configuration */
6942 	*old_cfg = *new_cfg;
6943 
6944 	/* Changes in configuration update VEB/VSI */
6945 	i40e_dcb_reconfigure(pf);
6946 out:
6947 	/* Re-start the VSIs if disabled */
6948 	if (need_reconfig) {
6949 		ret = i40e_resume_port_tx(pf);
6950 
6951 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6952 		/* In case of error no point in resuming VSIs */
6953 		if (ret)
6954 			goto err;
6955 
6956 		/* Wait for the PF's queues to be disabled */
6957 		ret = i40e_pf_wait_queues_disabled(pf);
6958 		if (ret) {
6959 			/* Schedule PF reset to recover */
6960 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6961 			i40e_service_event_schedule(pf);
6962 			goto err;
6963 		} else {
6964 			i40e_pf_unquiesce_all_vsi(pf);
6965 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6966 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6967 		}
6968 		/* registers are set, lets apply */
6969 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6970 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6971 	}
6972 
6973 err:
6974 	return ret;
6975 }
6976 
6977 /**
6978  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6979  * @pf: PF being queried
6980  *
6981  * Set default DCB configuration in case DCB is to be done in SW.
6982  **/
6983 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6984 {
6985 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6986 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6987 	struct i40e_hw *hw = &pf->hw;
6988 	int err;
6989 
6990 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6991 		/* Update the local cached instance with TC0 ETS */
6992 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6993 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6994 		pf->tmp_cfg.etscfg.maxtcs = 0;
6995 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6996 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6997 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6998 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6999 		/* FW needs one App to configure HW */
7000 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7001 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7002 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7003 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7004 
7005 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7006 	}
7007 
7008 	memset(&ets_data, 0, sizeof(ets_data));
7009 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7010 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7011 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7012 
7013 	/* Enable ETS on the Physical port */
7014 	err = i40e_aq_config_switch_comp_ets
7015 		(hw, pf->mac_seid, &ets_data,
7016 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7017 	if (err) {
7018 		dev_info(&pf->pdev->dev,
7019 			 "Enable Port ETS failed, err %s aq_err %s\n",
7020 			 i40e_stat_str(&pf->hw, err),
7021 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7022 		err = -ENOENT;
7023 		goto out;
7024 	}
7025 
7026 	/* Update the local cached instance with TC0 ETS */
7027 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7028 	dcb_cfg->etscfg.cbs = 0;
7029 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7030 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7031 
7032 out:
7033 	return err;
7034 }
7035 
7036 /**
7037  * i40e_init_pf_dcb - Initialize DCB configuration
7038  * @pf: PF being configured
7039  *
7040  * Query the current DCB configuration and cache it
7041  * in the hardware structure
7042  **/
7043 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7044 {
7045 	struct i40e_hw *hw = &pf->hw;
7046 	int err;
7047 
7048 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7049 	 * Also do not enable DCBx if FW LLDP agent is disabled
7050 	 */
7051 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7052 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7053 		err = I40E_NOT_SUPPORTED;
7054 		goto out;
7055 	}
7056 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7057 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7058 		err = i40e_dcb_sw_default_config(pf);
7059 		if (err) {
7060 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7061 			goto out;
7062 		}
7063 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7064 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7065 			       DCB_CAP_DCBX_VER_IEEE;
7066 		/* at init capable but disabled */
7067 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7068 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7069 		goto out;
7070 	}
7071 	err = i40e_init_dcb(hw, true);
7072 	if (!err) {
7073 		/* Device/Function is not DCBX capable */
7074 		if ((!hw->func_caps.dcb) ||
7075 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7076 			dev_info(&pf->pdev->dev,
7077 				 "DCBX offload is not supported or is disabled for this PF.\n");
7078 		} else {
7079 			/* When status is not DISABLED then DCBX in FW */
7080 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7081 				       DCB_CAP_DCBX_VER_IEEE;
7082 
7083 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7084 			/* Enable DCB tagging only when more than one TC
7085 			 * or explicitly disable if only one TC
7086 			 */
7087 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7088 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7089 			else
7090 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7091 			dev_dbg(&pf->pdev->dev,
7092 				"DCBX offload is supported for this PF.\n");
7093 		}
7094 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7095 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7096 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7097 	} else {
7098 		dev_info(&pf->pdev->dev,
7099 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7100 			 i40e_stat_str(&pf->hw, err),
7101 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7102 	}
7103 
7104 out:
7105 	return err;
7106 }
7107 #endif /* CONFIG_I40E_DCB */
7108 
7109 /**
7110  * i40e_print_link_message - print link up or down
7111  * @vsi: the VSI for which link needs a message
7112  * @isup: true of link is up, false otherwise
7113  */
7114 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7115 {
7116 	enum i40e_aq_link_speed new_speed;
7117 	struct i40e_pf *pf = vsi->back;
7118 	char *speed = "Unknown";
7119 	char *fc = "Unknown";
7120 	char *fec = "";
7121 	char *req_fec = "";
7122 	char *an = "";
7123 
7124 	if (isup)
7125 		new_speed = pf->hw.phy.link_info.link_speed;
7126 	else
7127 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7128 
7129 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7130 		return;
7131 	vsi->current_isup = isup;
7132 	vsi->current_speed = new_speed;
7133 	if (!isup) {
7134 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7135 		return;
7136 	}
7137 
7138 	/* Warn user if link speed on NPAR enabled partition is not at
7139 	 * least 10GB
7140 	 */
7141 	if (pf->hw.func_caps.npar_enable &&
7142 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7143 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7144 		netdev_warn(vsi->netdev,
7145 			    "The partition detected link speed that is less than 10Gbps\n");
7146 
7147 	switch (pf->hw.phy.link_info.link_speed) {
7148 	case I40E_LINK_SPEED_40GB:
7149 		speed = "40 G";
7150 		break;
7151 	case I40E_LINK_SPEED_20GB:
7152 		speed = "20 G";
7153 		break;
7154 	case I40E_LINK_SPEED_25GB:
7155 		speed = "25 G";
7156 		break;
7157 	case I40E_LINK_SPEED_10GB:
7158 		speed = "10 G";
7159 		break;
7160 	case I40E_LINK_SPEED_5GB:
7161 		speed = "5 G";
7162 		break;
7163 	case I40E_LINK_SPEED_2_5GB:
7164 		speed = "2.5 G";
7165 		break;
7166 	case I40E_LINK_SPEED_1GB:
7167 		speed = "1000 M";
7168 		break;
7169 	case I40E_LINK_SPEED_100MB:
7170 		speed = "100 M";
7171 		break;
7172 	default:
7173 		break;
7174 	}
7175 
7176 	switch (pf->hw.fc.current_mode) {
7177 	case I40E_FC_FULL:
7178 		fc = "RX/TX";
7179 		break;
7180 	case I40E_FC_TX_PAUSE:
7181 		fc = "TX";
7182 		break;
7183 	case I40E_FC_RX_PAUSE:
7184 		fc = "RX";
7185 		break;
7186 	default:
7187 		fc = "None";
7188 		break;
7189 	}
7190 
7191 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7192 		req_fec = "None";
7193 		fec = "None";
7194 		an = "False";
7195 
7196 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7197 			an = "True";
7198 
7199 		if (pf->hw.phy.link_info.fec_info &
7200 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7201 			fec = "CL74 FC-FEC/BASE-R";
7202 		else if (pf->hw.phy.link_info.fec_info &
7203 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7204 			fec = "CL108 RS-FEC";
7205 
7206 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7207 		 * both RS and FC are requested
7208 		 */
7209 		if (vsi->back->hw.phy.link_info.req_fec_info &
7210 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7211 			if (vsi->back->hw.phy.link_info.req_fec_info &
7212 			    I40E_AQ_REQUEST_FEC_RS)
7213 				req_fec = "CL108 RS-FEC";
7214 			else
7215 				req_fec = "CL74 FC-FEC/BASE-R";
7216 		}
7217 		netdev_info(vsi->netdev,
7218 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7219 			    speed, req_fec, fec, an, fc);
7220 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7221 		req_fec = "None";
7222 		fec = "None";
7223 		an = "False";
7224 
7225 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7226 			an = "True";
7227 
7228 		if (pf->hw.phy.link_info.fec_info &
7229 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7230 			fec = "CL74 FC-FEC/BASE-R";
7231 
7232 		if (pf->hw.phy.link_info.req_fec_info &
7233 		    I40E_AQ_REQUEST_FEC_KR)
7234 			req_fec = "CL74 FC-FEC/BASE-R";
7235 
7236 		netdev_info(vsi->netdev,
7237 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7238 			    speed, req_fec, fec, an, fc);
7239 	} else {
7240 		netdev_info(vsi->netdev,
7241 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7242 			    speed, fc);
7243 	}
7244 
7245 }
7246 
7247 /**
7248  * i40e_up_complete - Finish the last steps of bringing up a connection
7249  * @vsi: the VSI being configured
7250  **/
7251 static int i40e_up_complete(struct i40e_vsi *vsi)
7252 {
7253 	struct i40e_pf *pf = vsi->back;
7254 	int err;
7255 
7256 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7257 		i40e_vsi_configure_msix(vsi);
7258 	else
7259 		i40e_configure_msi_and_legacy(vsi);
7260 
7261 	/* start rings */
7262 	err = i40e_vsi_start_rings(vsi);
7263 	if (err)
7264 		return err;
7265 
7266 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7267 	i40e_napi_enable_all(vsi);
7268 	i40e_vsi_enable_irq(vsi);
7269 
7270 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7271 	    (vsi->netdev)) {
7272 		i40e_print_link_message(vsi, true);
7273 		netif_tx_start_all_queues(vsi->netdev);
7274 		netif_carrier_on(vsi->netdev);
7275 	}
7276 
7277 	/* replay FDIR SB filters */
7278 	if (vsi->type == I40E_VSI_FDIR) {
7279 		/* reset fd counters */
7280 		pf->fd_add_err = 0;
7281 		pf->fd_atr_cnt = 0;
7282 		i40e_fdir_filter_restore(vsi);
7283 	}
7284 
7285 	/* On the next run of the service_task, notify any clients of the new
7286 	 * opened netdev
7287 	 */
7288 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7289 	i40e_service_event_schedule(pf);
7290 
7291 	return 0;
7292 }
7293 
7294 /**
7295  * i40e_vsi_reinit_locked - Reset the VSI
7296  * @vsi: the VSI being configured
7297  *
7298  * Rebuild the ring structs after some configuration
7299  * has changed, e.g. MTU size.
7300  **/
7301 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7302 {
7303 	struct i40e_pf *pf = vsi->back;
7304 
7305 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7306 		usleep_range(1000, 2000);
7307 	i40e_down(vsi);
7308 
7309 	i40e_up(vsi);
7310 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7311 }
7312 
7313 /**
7314  * i40e_force_link_state - Force the link status
7315  * @pf: board private structure
7316  * @is_up: whether the link state should be forced up or down
7317  **/
7318 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7319 {
7320 	struct i40e_aq_get_phy_abilities_resp abilities;
7321 	struct i40e_aq_set_phy_config config = {0};
7322 	bool non_zero_phy_type = is_up;
7323 	struct i40e_hw *hw = &pf->hw;
7324 	i40e_status err;
7325 	u64 mask;
7326 	u8 speed;
7327 
7328 	/* Card might've been put in an unstable state by other drivers
7329 	 * and applications, which causes incorrect speed values being
7330 	 * set on startup. In order to clear speed registers, we call
7331 	 * get_phy_capabilities twice, once to get initial state of
7332 	 * available speeds, and once to get current PHY config.
7333 	 */
7334 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7335 					   NULL);
7336 	if (err) {
7337 		dev_err(&pf->pdev->dev,
7338 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7339 			i40e_stat_str(hw, err),
7340 			i40e_aq_str(hw, hw->aq.asq_last_status));
7341 		return err;
7342 	}
7343 	speed = abilities.link_speed;
7344 
7345 	/* Get the current phy config */
7346 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7347 					   NULL);
7348 	if (err) {
7349 		dev_err(&pf->pdev->dev,
7350 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7351 			i40e_stat_str(hw, err),
7352 			i40e_aq_str(hw, hw->aq.asq_last_status));
7353 		return err;
7354 	}
7355 
7356 	/* If link needs to go up, but was not forced to go down,
7357 	 * and its speed values are OK, no need for a flap
7358 	 * if non_zero_phy_type was set, still need to force up
7359 	 */
7360 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7361 		non_zero_phy_type = true;
7362 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7363 		return I40E_SUCCESS;
7364 
7365 	/* To force link we need to set bits for all supported PHY types,
7366 	 * but there are now more than 32, so we need to split the bitmap
7367 	 * across two fields.
7368 	 */
7369 	mask = I40E_PHY_TYPES_BITMASK;
7370 	config.phy_type =
7371 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7372 	config.phy_type_ext =
7373 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7374 	/* Copy the old settings, except of phy_type */
7375 	config.abilities = abilities.abilities;
7376 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7377 		if (is_up)
7378 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7379 		else
7380 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7381 	}
7382 	if (abilities.link_speed != 0)
7383 		config.link_speed = abilities.link_speed;
7384 	else
7385 		config.link_speed = speed;
7386 	config.eee_capability = abilities.eee_capability;
7387 	config.eeer = abilities.eeer_val;
7388 	config.low_power_ctrl = abilities.d3_lpan;
7389 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7390 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7391 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7392 
7393 	if (err) {
7394 		dev_err(&pf->pdev->dev,
7395 			"set phy config ret =  %s last_status =  %s\n",
7396 			i40e_stat_str(&pf->hw, err),
7397 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7398 		return err;
7399 	}
7400 
7401 	/* Update the link info */
7402 	err = i40e_update_link_info(hw);
7403 	if (err) {
7404 		/* Wait a little bit (on 40G cards it sometimes takes a really
7405 		 * long time for link to come back from the atomic reset)
7406 		 * and try once more
7407 		 */
7408 		msleep(1000);
7409 		i40e_update_link_info(hw);
7410 	}
7411 
7412 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7413 
7414 	return I40E_SUCCESS;
7415 }
7416 
7417 /**
7418  * i40e_up - Bring the connection back up after being down
7419  * @vsi: the VSI being configured
7420  **/
7421 int i40e_up(struct i40e_vsi *vsi)
7422 {
7423 	int err;
7424 
7425 	if (vsi->type == I40E_VSI_MAIN &&
7426 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7427 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7428 		i40e_force_link_state(vsi->back, true);
7429 
7430 	err = i40e_vsi_configure(vsi);
7431 	if (!err)
7432 		err = i40e_up_complete(vsi);
7433 
7434 	return err;
7435 }
7436 
7437 /**
7438  * i40e_down - Shutdown the connection processing
7439  * @vsi: the VSI being stopped
7440  **/
7441 void i40e_down(struct i40e_vsi *vsi)
7442 {
7443 	int i;
7444 
7445 	/* It is assumed that the caller of this function
7446 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7447 	 */
7448 	if (vsi->netdev) {
7449 		netif_carrier_off(vsi->netdev);
7450 		netif_tx_disable(vsi->netdev);
7451 	}
7452 	i40e_vsi_disable_irq(vsi);
7453 	i40e_vsi_stop_rings(vsi);
7454 	if (vsi->type == I40E_VSI_MAIN &&
7455 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7456 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7457 		i40e_force_link_state(vsi->back, false);
7458 	i40e_napi_disable_all(vsi);
7459 
7460 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7461 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7462 		if (i40e_enabled_xdp_vsi(vsi)) {
7463 			/* Make sure that in-progress ndo_xdp_xmit and
7464 			 * ndo_xsk_wakeup calls are completed.
7465 			 */
7466 			synchronize_rcu();
7467 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7468 		}
7469 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7470 	}
7471 
7472 }
7473 
7474 /**
7475  * i40e_validate_mqprio_qopt- validate queue mapping info
7476  * @vsi: the VSI being configured
7477  * @mqprio_qopt: queue parametrs
7478  **/
7479 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7480 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7481 {
7482 	u64 sum_max_rate = 0;
7483 	u64 max_rate = 0;
7484 	int i;
7485 
7486 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7487 	    mqprio_qopt->qopt.num_tc < 1 ||
7488 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7489 		return -EINVAL;
7490 	for (i = 0; ; i++) {
7491 		if (!mqprio_qopt->qopt.count[i])
7492 			return -EINVAL;
7493 		if (mqprio_qopt->min_rate[i]) {
7494 			dev_err(&vsi->back->pdev->dev,
7495 				"Invalid min tx rate (greater than 0) specified\n");
7496 			return -EINVAL;
7497 		}
7498 		max_rate = mqprio_qopt->max_rate[i];
7499 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7500 		sum_max_rate += max_rate;
7501 
7502 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7503 			break;
7504 		if (mqprio_qopt->qopt.offset[i + 1] !=
7505 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7506 			return -EINVAL;
7507 	}
7508 	if (vsi->num_queue_pairs <
7509 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7510 		dev_err(&vsi->back->pdev->dev,
7511 			"Failed to create traffic channel, insufficient number of queues.\n");
7512 		return -EINVAL;
7513 	}
7514 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7515 		dev_err(&vsi->back->pdev->dev,
7516 			"Invalid max tx rate specified\n");
7517 		return -EINVAL;
7518 	}
7519 	return 0;
7520 }
7521 
7522 /**
7523  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7524  * @vsi: the VSI being configured
7525  **/
7526 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7527 {
7528 	u16 qcount;
7529 	int i;
7530 
7531 	/* Only TC0 is enabled */
7532 	vsi->tc_config.numtc = 1;
7533 	vsi->tc_config.enabled_tc = 1;
7534 	qcount = min_t(int, vsi->alloc_queue_pairs,
7535 		       i40e_pf_get_max_q_per_tc(vsi->back));
7536 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7537 		/* For the TC that is not enabled set the offset to default
7538 		 * queue and allocate one queue for the given TC.
7539 		 */
7540 		vsi->tc_config.tc_info[i].qoffset = 0;
7541 		if (i == 0)
7542 			vsi->tc_config.tc_info[i].qcount = qcount;
7543 		else
7544 			vsi->tc_config.tc_info[i].qcount = 1;
7545 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7546 	}
7547 }
7548 
7549 /**
7550  * i40e_del_macvlan_filter
7551  * @hw: pointer to the HW structure
7552  * @seid: seid of the channel VSI
7553  * @macaddr: the mac address to apply as a filter
7554  * @aq_err: store the admin Q error
7555  *
7556  * This function deletes a mac filter on the channel VSI which serves as the
7557  * macvlan. Returns 0 on success.
7558  **/
7559 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7560 					   const u8 *macaddr, int *aq_err)
7561 {
7562 	struct i40e_aqc_remove_macvlan_element_data element;
7563 	i40e_status status;
7564 
7565 	memset(&element, 0, sizeof(element));
7566 	ether_addr_copy(element.mac_addr, macaddr);
7567 	element.vlan_tag = 0;
7568 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7569 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7570 	*aq_err = hw->aq.asq_last_status;
7571 
7572 	return status;
7573 }
7574 
7575 /**
7576  * i40e_add_macvlan_filter
7577  * @hw: pointer to the HW structure
7578  * @seid: seid of the channel VSI
7579  * @macaddr: the mac address to apply as a filter
7580  * @aq_err: store the admin Q error
7581  *
7582  * This function adds a mac filter on the channel VSI which serves as the
7583  * macvlan. Returns 0 on success.
7584  **/
7585 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7586 					   const u8 *macaddr, int *aq_err)
7587 {
7588 	struct i40e_aqc_add_macvlan_element_data element;
7589 	i40e_status status;
7590 	u16 cmd_flags = 0;
7591 
7592 	ether_addr_copy(element.mac_addr, macaddr);
7593 	element.vlan_tag = 0;
7594 	element.queue_number = 0;
7595 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7596 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7597 	element.flags = cpu_to_le16(cmd_flags);
7598 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7599 	*aq_err = hw->aq.asq_last_status;
7600 
7601 	return status;
7602 }
7603 
7604 /**
7605  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7606  * @vsi: the VSI we want to access
7607  * @ch: the channel we want to access
7608  */
7609 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7610 {
7611 	struct i40e_ring *tx_ring, *rx_ring;
7612 	u16 pf_q;
7613 	int i;
7614 
7615 	for (i = 0; i < ch->num_queue_pairs; i++) {
7616 		pf_q = ch->base_queue + i;
7617 		tx_ring = vsi->tx_rings[pf_q];
7618 		tx_ring->ch = NULL;
7619 		rx_ring = vsi->rx_rings[pf_q];
7620 		rx_ring->ch = NULL;
7621 	}
7622 }
7623 
7624 /**
7625  * i40e_free_macvlan_channels
7626  * @vsi: the VSI we want to access
7627  *
7628  * This function frees the Qs of the channel VSI from
7629  * the stack and also deletes the channel VSIs which
7630  * serve as macvlans.
7631  */
7632 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7633 {
7634 	struct i40e_channel *ch, *ch_tmp;
7635 	int ret;
7636 
7637 	if (list_empty(&vsi->macvlan_list))
7638 		return;
7639 
7640 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7641 		struct i40e_vsi *parent_vsi;
7642 
7643 		if (i40e_is_channel_macvlan(ch)) {
7644 			i40e_reset_ch_rings(vsi, ch);
7645 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7646 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7647 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7648 			kfree(ch->fwd);
7649 			ch->fwd = NULL;
7650 		}
7651 
7652 		list_del(&ch->list);
7653 		parent_vsi = ch->parent_vsi;
7654 		if (!parent_vsi || !ch->initialized) {
7655 			kfree(ch);
7656 			continue;
7657 		}
7658 
7659 		/* remove the VSI */
7660 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7661 					     NULL);
7662 		if (ret)
7663 			dev_err(&vsi->back->pdev->dev,
7664 				"unable to remove channel (%d) for parent VSI(%d)\n",
7665 				ch->seid, parent_vsi->seid);
7666 		kfree(ch);
7667 	}
7668 	vsi->macvlan_cnt = 0;
7669 }
7670 
7671 /**
7672  * i40e_fwd_ring_up - bring the macvlan device up
7673  * @vsi: the VSI we want to access
7674  * @vdev: macvlan netdevice
7675  * @fwd: the private fwd structure
7676  */
7677 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7678 			    struct i40e_fwd_adapter *fwd)
7679 {
7680 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7681 	int ret = 0, num_tc = 1,  i, aq_err;
7682 	struct i40e_pf *pf = vsi->back;
7683 	struct i40e_hw *hw = &pf->hw;
7684 
7685 	/* Go through the list and find an available channel */
7686 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7687 		if (!i40e_is_channel_macvlan(iter)) {
7688 			iter->fwd = fwd;
7689 			/* record configuration for macvlan interface in vdev */
7690 			for (i = 0; i < num_tc; i++)
7691 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7692 							     i,
7693 							     iter->num_queue_pairs,
7694 							     iter->base_queue);
7695 			for (i = 0; i < iter->num_queue_pairs; i++) {
7696 				struct i40e_ring *tx_ring, *rx_ring;
7697 				u16 pf_q;
7698 
7699 				pf_q = iter->base_queue + i;
7700 
7701 				/* Get to TX ring ptr */
7702 				tx_ring = vsi->tx_rings[pf_q];
7703 				tx_ring->ch = iter;
7704 
7705 				/* Get the RX ring ptr */
7706 				rx_ring = vsi->rx_rings[pf_q];
7707 				rx_ring->ch = iter;
7708 			}
7709 			ch = iter;
7710 			break;
7711 		}
7712 	}
7713 
7714 	if (!ch)
7715 		return -EINVAL;
7716 
7717 	/* Guarantee all rings are updated before we update the
7718 	 * MAC address filter.
7719 	 */
7720 	wmb();
7721 
7722 	/* Add a mac filter */
7723 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7724 	if (ret) {
7725 		/* if we cannot add the MAC rule then disable the offload */
7726 		macvlan_release_l2fw_offload(vdev);
7727 		for (i = 0; i < ch->num_queue_pairs; i++) {
7728 			struct i40e_ring *rx_ring;
7729 			u16 pf_q;
7730 
7731 			pf_q = ch->base_queue + i;
7732 			rx_ring = vsi->rx_rings[pf_q];
7733 			rx_ring->netdev = NULL;
7734 		}
7735 		dev_info(&pf->pdev->dev,
7736 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7737 			  i40e_stat_str(hw, ret),
7738 			  i40e_aq_str(hw, aq_err));
7739 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7740 	}
7741 
7742 	return ret;
7743 }
7744 
7745 /**
7746  * i40e_setup_macvlans - create the channels which will be macvlans
7747  * @vsi: the VSI we want to access
7748  * @macvlan_cnt: no. of macvlans to be setup
7749  * @qcnt: no. of Qs per macvlan
7750  * @vdev: macvlan netdevice
7751  */
7752 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7753 			       struct net_device *vdev)
7754 {
7755 	struct i40e_pf *pf = vsi->back;
7756 	struct i40e_hw *hw = &pf->hw;
7757 	struct i40e_vsi_context ctxt;
7758 	u16 sections, qmap, num_qps;
7759 	struct i40e_channel *ch;
7760 	int i, pow, ret = 0;
7761 	u8 offset = 0;
7762 
7763 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7764 		return -EINVAL;
7765 
7766 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7767 
7768 	/* find the next higher power-of-2 of num queue pairs */
7769 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7770 
7771 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7772 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7773 
7774 	/* Setup context bits for the main VSI */
7775 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7776 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7777 	memset(&ctxt, 0, sizeof(ctxt));
7778 	ctxt.seid = vsi->seid;
7779 	ctxt.pf_num = vsi->back->hw.pf_id;
7780 	ctxt.vf_num = 0;
7781 	ctxt.uplink_seid = vsi->uplink_seid;
7782 	ctxt.info = vsi->info;
7783 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7784 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7785 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7786 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7787 
7788 	/* Reconfigure RSS for main VSI with new max queue count */
7789 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7790 	ret = i40e_vsi_config_rss(vsi);
7791 	if (ret) {
7792 		dev_info(&pf->pdev->dev,
7793 			 "Failed to reconfig RSS for num_queues (%u)\n",
7794 			 vsi->rss_size);
7795 		return ret;
7796 	}
7797 	vsi->reconfig_rss = true;
7798 	dev_dbg(&vsi->back->pdev->dev,
7799 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7800 	vsi->next_base_queue = num_qps;
7801 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7802 
7803 	/* Update the VSI after updating the VSI queue-mapping
7804 	 * information
7805 	 */
7806 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7807 	if (ret) {
7808 		dev_info(&pf->pdev->dev,
7809 			 "Update vsi tc config failed, err %s aq_err %s\n",
7810 			 i40e_stat_str(hw, ret),
7811 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7812 		return ret;
7813 	}
7814 	/* update the local VSI info with updated queue map */
7815 	i40e_vsi_update_queue_map(vsi, &ctxt);
7816 	vsi->info.valid_sections = 0;
7817 
7818 	/* Create channels for macvlans */
7819 	INIT_LIST_HEAD(&vsi->macvlan_list);
7820 	for (i = 0; i < macvlan_cnt; i++) {
7821 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7822 		if (!ch) {
7823 			ret = -ENOMEM;
7824 			goto err_free;
7825 		}
7826 		INIT_LIST_HEAD(&ch->list);
7827 		ch->num_queue_pairs = qcnt;
7828 		if (!i40e_setup_channel(pf, vsi, ch)) {
7829 			ret = -EINVAL;
7830 			kfree(ch);
7831 			goto err_free;
7832 		}
7833 		ch->parent_vsi = vsi;
7834 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7835 		vsi->macvlan_cnt++;
7836 		list_add_tail(&ch->list, &vsi->macvlan_list);
7837 	}
7838 
7839 	return ret;
7840 
7841 err_free:
7842 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7843 	i40e_free_macvlan_channels(vsi);
7844 
7845 	return ret;
7846 }
7847 
7848 /**
7849  * i40e_fwd_add - configure macvlans
7850  * @netdev: net device to configure
7851  * @vdev: macvlan netdevice
7852  **/
7853 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7854 {
7855 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7856 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7857 	struct i40e_vsi *vsi = np->vsi;
7858 	struct i40e_pf *pf = vsi->back;
7859 	struct i40e_fwd_adapter *fwd;
7860 	int avail_macvlan, ret;
7861 
7862 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7863 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7864 		return ERR_PTR(-EINVAL);
7865 	}
7866 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7867 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7868 		return ERR_PTR(-EINVAL);
7869 	}
7870 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7871 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7872 		return ERR_PTR(-EINVAL);
7873 	}
7874 
7875 	/* The macvlan device has to be a single Q device so that the
7876 	 * tc_to_txq field can be reused to pick the tx queue.
7877 	 */
7878 	if (netif_is_multiqueue(vdev))
7879 		return ERR_PTR(-ERANGE);
7880 
7881 	if (!vsi->macvlan_cnt) {
7882 		/* reserve bit 0 for the pf device */
7883 		set_bit(0, vsi->fwd_bitmask);
7884 
7885 		/* Try to reserve as many queues as possible for macvlans. First
7886 		 * reserve 3/4th of max vectors, then half, then quarter and
7887 		 * calculate Qs per macvlan as you go
7888 		 */
7889 		vectors = pf->num_lan_msix;
7890 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7891 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7892 			q_per_macvlan = 4;
7893 			macvlan_cnt = (vectors - 32) / 4;
7894 		} else if (vectors <= 64 && vectors > 32) {
7895 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7896 			q_per_macvlan = 2;
7897 			macvlan_cnt = (vectors - 16) / 2;
7898 		} else if (vectors <= 32 && vectors > 16) {
7899 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7900 			q_per_macvlan = 1;
7901 			macvlan_cnt = vectors - 16;
7902 		} else if (vectors <= 16 && vectors > 8) {
7903 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7904 			q_per_macvlan = 1;
7905 			macvlan_cnt = vectors - 8;
7906 		} else {
7907 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7908 			q_per_macvlan = 1;
7909 			macvlan_cnt = vectors - 1;
7910 		}
7911 
7912 		if (macvlan_cnt == 0)
7913 			return ERR_PTR(-EBUSY);
7914 
7915 		/* Quiesce VSI queues */
7916 		i40e_quiesce_vsi(vsi);
7917 
7918 		/* sets up the macvlans but does not "enable" them */
7919 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7920 					  vdev);
7921 		if (ret)
7922 			return ERR_PTR(ret);
7923 
7924 		/* Unquiesce VSI */
7925 		i40e_unquiesce_vsi(vsi);
7926 	}
7927 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7928 					    vsi->macvlan_cnt);
7929 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7930 		return ERR_PTR(-EBUSY);
7931 
7932 	/* create the fwd struct */
7933 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7934 	if (!fwd)
7935 		return ERR_PTR(-ENOMEM);
7936 
7937 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7938 	fwd->bit_no = avail_macvlan;
7939 	netdev_set_sb_channel(vdev, avail_macvlan);
7940 	fwd->netdev = vdev;
7941 
7942 	if (!netif_running(netdev))
7943 		return fwd;
7944 
7945 	/* Set fwd ring up */
7946 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7947 	if (ret) {
7948 		/* unbind the queues and drop the subordinate channel config */
7949 		netdev_unbind_sb_channel(netdev, vdev);
7950 		netdev_set_sb_channel(vdev, 0);
7951 
7952 		kfree(fwd);
7953 		return ERR_PTR(-EINVAL);
7954 	}
7955 
7956 	return fwd;
7957 }
7958 
7959 /**
7960  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7961  * @vsi: the VSI we want to access
7962  */
7963 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7964 {
7965 	struct i40e_channel *ch, *ch_tmp;
7966 	struct i40e_pf *pf = vsi->back;
7967 	struct i40e_hw *hw = &pf->hw;
7968 	int aq_err, ret = 0;
7969 
7970 	if (list_empty(&vsi->macvlan_list))
7971 		return;
7972 
7973 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7974 		if (i40e_is_channel_macvlan(ch)) {
7975 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7976 						      i40e_channel_mac(ch),
7977 						      &aq_err);
7978 			if (!ret) {
7979 				/* Reset queue contexts */
7980 				i40e_reset_ch_rings(vsi, ch);
7981 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7982 				netdev_unbind_sb_channel(vsi->netdev,
7983 							 ch->fwd->netdev);
7984 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7985 				kfree(ch->fwd);
7986 				ch->fwd = NULL;
7987 			}
7988 		}
7989 	}
7990 }
7991 
7992 /**
7993  * i40e_fwd_del - delete macvlan interfaces
7994  * @netdev: net device to configure
7995  * @vdev: macvlan netdevice
7996  */
7997 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7998 {
7999 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8000 	struct i40e_fwd_adapter *fwd = vdev;
8001 	struct i40e_channel *ch, *ch_tmp;
8002 	struct i40e_vsi *vsi = np->vsi;
8003 	struct i40e_pf *pf = vsi->back;
8004 	struct i40e_hw *hw = &pf->hw;
8005 	int aq_err, ret = 0;
8006 
8007 	/* Find the channel associated with the macvlan and del mac filter */
8008 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8009 		if (i40e_is_channel_macvlan(ch) &&
8010 		    ether_addr_equal(i40e_channel_mac(ch),
8011 				     fwd->netdev->dev_addr)) {
8012 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8013 						      i40e_channel_mac(ch),
8014 						      &aq_err);
8015 			if (!ret) {
8016 				/* Reset queue contexts */
8017 				i40e_reset_ch_rings(vsi, ch);
8018 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8019 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8020 				netdev_set_sb_channel(fwd->netdev, 0);
8021 				kfree(ch->fwd);
8022 				ch->fwd = NULL;
8023 			} else {
8024 				dev_info(&pf->pdev->dev,
8025 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8026 					  i40e_stat_str(hw, ret),
8027 					  i40e_aq_str(hw, aq_err));
8028 			}
8029 			break;
8030 		}
8031 	}
8032 }
8033 
8034 /**
8035  * i40e_setup_tc - configure multiple traffic classes
8036  * @netdev: net device to configure
8037  * @type_data: tc offload data
8038  **/
8039 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8040 {
8041 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8042 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8043 	struct i40e_vsi *vsi = np->vsi;
8044 	struct i40e_pf *pf = vsi->back;
8045 	u8 enabled_tc = 0, num_tc, hw;
8046 	bool need_reset = false;
8047 	int old_queue_pairs;
8048 	int ret = -EINVAL;
8049 	u16 mode;
8050 	int i;
8051 
8052 	old_queue_pairs = vsi->num_queue_pairs;
8053 	num_tc = mqprio_qopt->qopt.num_tc;
8054 	hw = mqprio_qopt->qopt.hw;
8055 	mode = mqprio_qopt->mode;
8056 	if (!hw) {
8057 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8058 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8059 		goto config_tc;
8060 	}
8061 
8062 	/* Check if MFP enabled */
8063 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8064 		netdev_info(netdev,
8065 			    "Configuring TC not supported in MFP mode\n");
8066 		return ret;
8067 	}
8068 	switch (mode) {
8069 	case TC_MQPRIO_MODE_DCB:
8070 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8071 
8072 		/* Check if DCB enabled to continue */
8073 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8074 			netdev_info(netdev,
8075 				    "DCB is not enabled for adapter\n");
8076 			return ret;
8077 		}
8078 
8079 		/* Check whether tc count is within enabled limit */
8080 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8081 			netdev_info(netdev,
8082 				    "TC count greater than enabled on link for adapter\n");
8083 			return ret;
8084 		}
8085 		break;
8086 	case TC_MQPRIO_MODE_CHANNEL:
8087 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8088 			netdev_info(netdev,
8089 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8090 			return ret;
8091 		}
8092 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8093 			return ret;
8094 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8095 		if (ret)
8096 			return ret;
8097 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8098 		       sizeof(*mqprio_qopt));
8099 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8100 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8101 		break;
8102 	default:
8103 		return -EINVAL;
8104 	}
8105 
8106 config_tc:
8107 	/* Generate TC map for number of tc requested */
8108 	for (i = 0; i < num_tc; i++)
8109 		enabled_tc |= BIT(i);
8110 
8111 	/* Requesting same TC configuration as already enabled */
8112 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8113 	    mode != TC_MQPRIO_MODE_CHANNEL)
8114 		return 0;
8115 
8116 	/* Quiesce VSI queues */
8117 	i40e_quiesce_vsi(vsi);
8118 
8119 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
8120 		i40e_remove_queue_channels(vsi);
8121 
8122 	/* Configure VSI for enabled TCs */
8123 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8124 	if (ret) {
8125 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8126 			    vsi->seid);
8127 		need_reset = true;
8128 		goto exit;
8129 	} else if (enabled_tc &&
8130 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8131 		netdev_info(netdev,
8132 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8133 			    vsi->tc_config.tc_info[0].qcount);
8134 		ret = -EINVAL;
8135 		need_reset = true;
8136 		goto exit;
8137 	}
8138 
8139 	dev_info(&vsi->back->pdev->dev,
8140 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8141 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8142 
8143 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8144 		if (vsi->mqprio_qopt.max_rate[0]) {
8145 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8146 
8147 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8148 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8149 			if (!ret) {
8150 				u64 credits = max_tx_rate;
8151 
8152 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8153 				dev_dbg(&vsi->back->pdev->dev,
8154 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8155 					max_tx_rate,
8156 					credits,
8157 					vsi->seid);
8158 			} else {
8159 				need_reset = true;
8160 				goto exit;
8161 			}
8162 		}
8163 		ret = i40e_configure_queue_channels(vsi);
8164 		if (ret) {
8165 			vsi->num_queue_pairs = old_queue_pairs;
8166 			netdev_info(netdev,
8167 				    "Failed configuring queue channels\n");
8168 			need_reset = true;
8169 			goto exit;
8170 		}
8171 	}
8172 
8173 exit:
8174 	/* Reset the configuration data to defaults, only TC0 is enabled */
8175 	if (need_reset) {
8176 		i40e_vsi_set_default_tc_config(vsi);
8177 		need_reset = false;
8178 	}
8179 
8180 	/* Unquiesce VSI */
8181 	i40e_unquiesce_vsi(vsi);
8182 	return ret;
8183 }
8184 
8185 /**
8186  * i40e_set_cld_element - sets cloud filter element data
8187  * @filter: cloud filter rule
8188  * @cld: ptr to cloud filter element data
8189  *
8190  * This is helper function to copy data into cloud filter element
8191  **/
8192 static inline void
8193 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8194 		     struct i40e_aqc_cloud_filters_element_data *cld)
8195 {
8196 	u32 ipa;
8197 	int i;
8198 
8199 	memset(cld, 0, sizeof(*cld));
8200 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8201 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8202 
8203 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8204 		return;
8205 
8206 	if (filter->n_proto == ETH_P_IPV6) {
8207 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8208 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8209 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8210 
8211 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8212 		}
8213 	} else {
8214 		ipa = be32_to_cpu(filter->dst_ipv4);
8215 
8216 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8217 	}
8218 
8219 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8220 
8221 	/* tenant_id is not supported by FW now, once the support is enabled
8222 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8223 	 */
8224 	if (filter->tenant_id)
8225 		return;
8226 }
8227 
8228 /**
8229  * i40e_add_del_cloud_filter - Add/del cloud filter
8230  * @vsi: pointer to VSI
8231  * @filter: cloud filter rule
8232  * @add: if true, add, if false, delete
8233  *
8234  * Add or delete a cloud filter for a specific flow spec.
8235  * Returns 0 if the filter were successfully added.
8236  **/
8237 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8238 			      struct i40e_cloud_filter *filter, bool add)
8239 {
8240 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8241 	struct i40e_pf *pf = vsi->back;
8242 	int ret;
8243 	static const u16 flag_table[128] = {
8244 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8245 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8246 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8247 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8248 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8249 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8250 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8251 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8252 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8253 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8254 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8255 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8256 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8257 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8258 	};
8259 
8260 	if (filter->flags >= ARRAY_SIZE(flag_table))
8261 		return I40E_ERR_CONFIG;
8262 
8263 	memset(&cld_filter, 0, sizeof(cld_filter));
8264 
8265 	/* copy element needed to add cloud filter from filter */
8266 	i40e_set_cld_element(filter, &cld_filter);
8267 
8268 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8269 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8270 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8271 
8272 	if (filter->n_proto == ETH_P_IPV6)
8273 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8274 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8275 	else
8276 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8277 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8278 
8279 	if (add)
8280 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8281 						&cld_filter, 1);
8282 	else
8283 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8284 						&cld_filter, 1);
8285 	if (ret)
8286 		dev_dbg(&pf->pdev->dev,
8287 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8288 			add ? "add" : "delete", filter->dst_port, ret,
8289 			pf->hw.aq.asq_last_status);
8290 	else
8291 		dev_info(&pf->pdev->dev,
8292 			 "%s cloud filter for VSI: %d\n",
8293 			 add ? "Added" : "Deleted", filter->seid);
8294 	return ret;
8295 }
8296 
8297 /**
8298  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8299  * @vsi: pointer to VSI
8300  * @filter: cloud filter rule
8301  * @add: if true, add, if false, delete
8302  *
8303  * Add or delete a cloud filter for a specific flow spec using big buffer.
8304  * Returns 0 if the filter were successfully added.
8305  **/
8306 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8307 				      struct i40e_cloud_filter *filter,
8308 				      bool add)
8309 {
8310 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8311 	struct i40e_pf *pf = vsi->back;
8312 	int ret;
8313 
8314 	/* Both (src/dst) valid mac_addr are not supported */
8315 	if ((is_valid_ether_addr(filter->dst_mac) &&
8316 	     is_valid_ether_addr(filter->src_mac)) ||
8317 	    (is_multicast_ether_addr(filter->dst_mac) &&
8318 	     is_multicast_ether_addr(filter->src_mac)))
8319 		return -EOPNOTSUPP;
8320 
8321 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8322 	 * ports are not supported via big buffer now.
8323 	 */
8324 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8325 		return -EOPNOTSUPP;
8326 
8327 	/* adding filter using src_port/src_ip is not supported at this stage */
8328 	if (filter->src_port ||
8329 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8330 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8331 		return -EOPNOTSUPP;
8332 
8333 	memset(&cld_filter, 0, sizeof(cld_filter));
8334 
8335 	/* copy element needed to add cloud filter from filter */
8336 	i40e_set_cld_element(filter, &cld_filter.element);
8337 
8338 	if (is_valid_ether_addr(filter->dst_mac) ||
8339 	    is_valid_ether_addr(filter->src_mac) ||
8340 	    is_multicast_ether_addr(filter->dst_mac) ||
8341 	    is_multicast_ether_addr(filter->src_mac)) {
8342 		/* MAC + IP : unsupported mode */
8343 		if (filter->dst_ipv4)
8344 			return -EOPNOTSUPP;
8345 
8346 		/* since we validated that L4 port must be valid before
8347 		 * we get here, start with respective "flags" value
8348 		 * and update if vlan is present or not
8349 		 */
8350 		cld_filter.element.flags =
8351 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8352 
8353 		if (filter->vlan_id) {
8354 			cld_filter.element.flags =
8355 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8356 		}
8357 
8358 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8359 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8360 		cld_filter.element.flags =
8361 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8362 		if (filter->n_proto == ETH_P_IPV6)
8363 			cld_filter.element.flags |=
8364 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8365 		else
8366 			cld_filter.element.flags |=
8367 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8368 	} else {
8369 		dev_err(&pf->pdev->dev,
8370 			"either mac or ip has to be valid for cloud filter\n");
8371 		return -EINVAL;
8372 	}
8373 
8374 	/* Now copy L4 port in Byte 6..7 in general fields */
8375 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8376 						be16_to_cpu(filter->dst_port);
8377 
8378 	if (add) {
8379 		/* Validate current device switch mode, change if necessary */
8380 		ret = i40e_validate_and_set_switch_mode(vsi);
8381 		if (ret) {
8382 			dev_err(&pf->pdev->dev,
8383 				"failed to set switch mode, ret %d\n",
8384 				ret);
8385 			return ret;
8386 		}
8387 
8388 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8389 						   &cld_filter, 1);
8390 	} else {
8391 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8392 						   &cld_filter, 1);
8393 	}
8394 
8395 	if (ret)
8396 		dev_dbg(&pf->pdev->dev,
8397 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8398 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8399 	else
8400 		dev_info(&pf->pdev->dev,
8401 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8402 			 add ? "add" : "delete", filter->seid,
8403 			 ntohs(filter->dst_port));
8404 	return ret;
8405 }
8406 
8407 /**
8408  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8409  * @vsi: Pointer to VSI
8410  * @f: Pointer to struct flow_cls_offload
8411  * @filter: Pointer to cloud filter structure
8412  *
8413  **/
8414 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8415 				 struct flow_cls_offload *f,
8416 				 struct i40e_cloud_filter *filter)
8417 {
8418 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8419 	struct flow_dissector *dissector = rule->match.dissector;
8420 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8421 	struct i40e_pf *pf = vsi->back;
8422 	u8 field_flags = 0;
8423 
8424 	if (dissector->used_keys &
8425 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8426 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8427 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8428 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8429 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8430 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8431 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8432 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8433 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8434 			dissector->used_keys);
8435 		return -EOPNOTSUPP;
8436 	}
8437 
8438 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8439 		struct flow_match_enc_keyid match;
8440 
8441 		flow_rule_match_enc_keyid(rule, &match);
8442 		if (match.mask->keyid != 0)
8443 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8444 
8445 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8446 	}
8447 
8448 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8449 		struct flow_match_basic match;
8450 
8451 		flow_rule_match_basic(rule, &match);
8452 		n_proto_key = ntohs(match.key->n_proto);
8453 		n_proto_mask = ntohs(match.mask->n_proto);
8454 
8455 		if (n_proto_key == ETH_P_ALL) {
8456 			n_proto_key = 0;
8457 			n_proto_mask = 0;
8458 		}
8459 		filter->n_proto = n_proto_key & n_proto_mask;
8460 		filter->ip_proto = match.key->ip_proto;
8461 	}
8462 
8463 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8464 		struct flow_match_eth_addrs match;
8465 
8466 		flow_rule_match_eth_addrs(rule, &match);
8467 
8468 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8469 		if (!is_zero_ether_addr(match.mask->dst)) {
8470 			if (is_broadcast_ether_addr(match.mask->dst)) {
8471 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8472 			} else {
8473 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8474 					match.mask->dst);
8475 				return I40E_ERR_CONFIG;
8476 			}
8477 		}
8478 
8479 		if (!is_zero_ether_addr(match.mask->src)) {
8480 			if (is_broadcast_ether_addr(match.mask->src)) {
8481 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8482 			} else {
8483 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8484 					match.mask->src);
8485 				return I40E_ERR_CONFIG;
8486 			}
8487 		}
8488 		ether_addr_copy(filter->dst_mac, match.key->dst);
8489 		ether_addr_copy(filter->src_mac, match.key->src);
8490 	}
8491 
8492 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8493 		struct flow_match_vlan match;
8494 
8495 		flow_rule_match_vlan(rule, &match);
8496 		if (match.mask->vlan_id) {
8497 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8498 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8499 
8500 			} else {
8501 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8502 					match.mask->vlan_id);
8503 				return I40E_ERR_CONFIG;
8504 			}
8505 		}
8506 
8507 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8508 	}
8509 
8510 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8511 		struct flow_match_control match;
8512 
8513 		flow_rule_match_control(rule, &match);
8514 		addr_type = match.key->addr_type;
8515 	}
8516 
8517 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8518 		struct flow_match_ipv4_addrs match;
8519 
8520 		flow_rule_match_ipv4_addrs(rule, &match);
8521 		if (match.mask->dst) {
8522 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8523 				field_flags |= I40E_CLOUD_FIELD_IIP;
8524 			} else {
8525 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8526 					&match.mask->dst);
8527 				return I40E_ERR_CONFIG;
8528 			}
8529 		}
8530 
8531 		if (match.mask->src) {
8532 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8533 				field_flags |= I40E_CLOUD_FIELD_IIP;
8534 			} else {
8535 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8536 					&match.mask->src);
8537 				return I40E_ERR_CONFIG;
8538 			}
8539 		}
8540 
8541 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8542 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8543 			return I40E_ERR_CONFIG;
8544 		}
8545 		filter->dst_ipv4 = match.key->dst;
8546 		filter->src_ipv4 = match.key->src;
8547 	}
8548 
8549 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8550 		struct flow_match_ipv6_addrs match;
8551 
8552 		flow_rule_match_ipv6_addrs(rule, &match);
8553 
8554 		/* src and dest IPV6 address should not be LOOPBACK
8555 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8556 		 */
8557 		if (ipv6_addr_loopback(&match.key->dst) ||
8558 		    ipv6_addr_loopback(&match.key->src)) {
8559 			dev_err(&pf->pdev->dev,
8560 				"Bad ipv6, addr is LOOPBACK\n");
8561 			return I40E_ERR_CONFIG;
8562 		}
8563 		if (!ipv6_addr_any(&match.mask->dst) ||
8564 		    !ipv6_addr_any(&match.mask->src))
8565 			field_flags |= I40E_CLOUD_FIELD_IIP;
8566 
8567 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8568 		       sizeof(filter->src_ipv6));
8569 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8570 		       sizeof(filter->dst_ipv6));
8571 	}
8572 
8573 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8574 		struct flow_match_ports match;
8575 
8576 		flow_rule_match_ports(rule, &match);
8577 		if (match.mask->src) {
8578 			if (match.mask->src == cpu_to_be16(0xffff)) {
8579 				field_flags |= I40E_CLOUD_FIELD_IIP;
8580 			} else {
8581 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8582 					be16_to_cpu(match.mask->src));
8583 				return I40E_ERR_CONFIG;
8584 			}
8585 		}
8586 
8587 		if (match.mask->dst) {
8588 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8589 				field_flags |= I40E_CLOUD_FIELD_IIP;
8590 			} else {
8591 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8592 					be16_to_cpu(match.mask->dst));
8593 				return I40E_ERR_CONFIG;
8594 			}
8595 		}
8596 
8597 		filter->dst_port = match.key->dst;
8598 		filter->src_port = match.key->src;
8599 
8600 		switch (filter->ip_proto) {
8601 		case IPPROTO_TCP:
8602 		case IPPROTO_UDP:
8603 			break;
8604 		default:
8605 			dev_err(&pf->pdev->dev,
8606 				"Only UDP and TCP transport are supported\n");
8607 			return -EINVAL;
8608 		}
8609 	}
8610 	filter->flags = field_flags;
8611 	return 0;
8612 }
8613 
8614 /**
8615  * i40e_handle_tclass: Forward to a traffic class on the device
8616  * @vsi: Pointer to VSI
8617  * @tc: traffic class index on the device
8618  * @filter: Pointer to cloud filter structure
8619  *
8620  **/
8621 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8622 			      struct i40e_cloud_filter *filter)
8623 {
8624 	struct i40e_channel *ch, *ch_tmp;
8625 
8626 	/* direct to a traffic class on the same device */
8627 	if (tc == 0) {
8628 		filter->seid = vsi->seid;
8629 		return 0;
8630 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8631 		if (!filter->dst_port) {
8632 			dev_err(&vsi->back->pdev->dev,
8633 				"Specify destination port to direct to traffic class that is not default\n");
8634 			return -EINVAL;
8635 		}
8636 		if (list_empty(&vsi->ch_list))
8637 			return -EINVAL;
8638 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8639 					 list) {
8640 			if (ch->seid == vsi->tc_seid_map[tc])
8641 				filter->seid = ch->seid;
8642 		}
8643 		return 0;
8644 	}
8645 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8646 	return -EINVAL;
8647 }
8648 
8649 /**
8650  * i40e_configure_clsflower - Configure tc flower filters
8651  * @vsi: Pointer to VSI
8652  * @cls_flower: Pointer to struct flow_cls_offload
8653  *
8654  **/
8655 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8656 				    struct flow_cls_offload *cls_flower)
8657 {
8658 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8659 	struct i40e_cloud_filter *filter = NULL;
8660 	struct i40e_pf *pf = vsi->back;
8661 	int err = 0;
8662 
8663 	if (tc < 0) {
8664 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8665 		return -EOPNOTSUPP;
8666 	}
8667 
8668 	if (!tc) {
8669 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8670 		return -EINVAL;
8671 	}
8672 
8673 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8674 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8675 		return -EBUSY;
8676 
8677 	if (pf->fdir_pf_active_filters ||
8678 	    (!hlist_empty(&pf->fdir_filter_list))) {
8679 		dev_err(&vsi->back->pdev->dev,
8680 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8681 		return -EINVAL;
8682 	}
8683 
8684 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8685 		dev_err(&vsi->back->pdev->dev,
8686 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8687 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8688 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8689 	}
8690 
8691 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8692 	if (!filter)
8693 		return -ENOMEM;
8694 
8695 	filter->cookie = cls_flower->cookie;
8696 
8697 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8698 	if (err < 0)
8699 		goto err;
8700 
8701 	err = i40e_handle_tclass(vsi, tc, filter);
8702 	if (err < 0)
8703 		goto err;
8704 
8705 	/* Add cloud filter */
8706 	if (filter->dst_port)
8707 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8708 	else
8709 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8710 
8711 	if (err) {
8712 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8713 			err);
8714 		goto err;
8715 	}
8716 
8717 	/* add filter to the ordered list */
8718 	INIT_HLIST_NODE(&filter->cloud_node);
8719 
8720 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8721 
8722 	pf->num_cloud_filters++;
8723 
8724 	return err;
8725 err:
8726 	kfree(filter);
8727 	return err;
8728 }
8729 
8730 /**
8731  * i40e_find_cloud_filter - Find the could filter in the list
8732  * @vsi: Pointer to VSI
8733  * @cookie: filter specific cookie
8734  *
8735  **/
8736 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8737 							unsigned long *cookie)
8738 {
8739 	struct i40e_cloud_filter *filter = NULL;
8740 	struct hlist_node *node2;
8741 
8742 	hlist_for_each_entry_safe(filter, node2,
8743 				  &vsi->back->cloud_filter_list, cloud_node)
8744 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8745 			return filter;
8746 	return NULL;
8747 }
8748 
8749 /**
8750  * i40e_delete_clsflower - Remove tc flower filters
8751  * @vsi: Pointer to VSI
8752  * @cls_flower: Pointer to struct flow_cls_offload
8753  *
8754  **/
8755 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8756 				 struct flow_cls_offload *cls_flower)
8757 {
8758 	struct i40e_cloud_filter *filter = NULL;
8759 	struct i40e_pf *pf = vsi->back;
8760 	int err = 0;
8761 
8762 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8763 
8764 	if (!filter)
8765 		return -EINVAL;
8766 
8767 	hash_del(&filter->cloud_node);
8768 
8769 	if (filter->dst_port)
8770 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8771 	else
8772 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8773 
8774 	kfree(filter);
8775 	if (err) {
8776 		dev_err(&pf->pdev->dev,
8777 			"Failed to delete cloud filter, err %s\n",
8778 			i40e_stat_str(&pf->hw, err));
8779 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8780 	}
8781 
8782 	pf->num_cloud_filters--;
8783 	if (!pf->num_cloud_filters)
8784 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8785 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8786 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8787 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8788 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8789 		}
8790 	return 0;
8791 }
8792 
8793 /**
8794  * i40e_setup_tc_cls_flower - flower classifier offloads
8795  * @np: net device to configure
8796  * @cls_flower: offload data
8797  **/
8798 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8799 				    struct flow_cls_offload *cls_flower)
8800 {
8801 	struct i40e_vsi *vsi = np->vsi;
8802 
8803 	switch (cls_flower->command) {
8804 	case FLOW_CLS_REPLACE:
8805 		return i40e_configure_clsflower(vsi, cls_flower);
8806 	case FLOW_CLS_DESTROY:
8807 		return i40e_delete_clsflower(vsi, cls_flower);
8808 	case FLOW_CLS_STATS:
8809 		return -EOPNOTSUPP;
8810 	default:
8811 		return -EOPNOTSUPP;
8812 	}
8813 }
8814 
8815 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8816 				  void *cb_priv)
8817 {
8818 	struct i40e_netdev_priv *np = cb_priv;
8819 
8820 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8821 		return -EOPNOTSUPP;
8822 
8823 	switch (type) {
8824 	case TC_SETUP_CLSFLOWER:
8825 		return i40e_setup_tc_cls_flower(np, type_data);
8826 
8827 	default:
8828 		return -EOPNOTSUPP;
8829 	}
8830 }
8831 
8832 static LIST_HEAD(i40e_block_cb_list);
8833 
8834 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8835 			   void *type_data)
8836 {
8837 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8838 
8839 	switch (type) {
8840 	case TC_SETUP_QDISC_MQPRIO:
8841 		return i40e_setup_tc(netdev, type_data);
8842 	case TC_SETUP_BLOCK:
8843 		return flow_block_cb_setup_simple(type_data,
8844 						  &i40e_block_cb_list,
8845 						  i40e_setup_tc_block_cb,
8846 						  np, np, true);
8847 	default:
8848 		return -EOPNOTSUPP;
8849 	}
8850 }
8851 
8852 /**
8853  * i40e_open - Called when a network interface is made active
8854  * @netdev: network interface device structure
8855  *
8856  * The open entry point is called when a network interface is made
8857  * active by the system (IFF_UP).  At this point all resources needed
8858  * for transmit and receive operations are allocated, the interrupt
8859  * handler is registered with the OS, the netdev watchdog subtask is
8860  * enabled, and the stack is notified that the interface is ready.
8861  *
8862  * Returns 0 on success, negative value on failure
8863  **/
8864 int i40e_open(struct net_device *netdev)
8865 {
8866 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8867 	struct i40e_vsi *vsi = np->vsi;
8868 	struct i40e_pf *pf = vsi->back;
8869 	int err;
8870 
8871 	/* disallow open during test or if eeprom is broken */
8872 	if (test_bit(__I40E_TESTING, pf->state) ||
8873 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8874 		return -EBUSY;
8875 
8876 	netif_carrier_off(netdev);
8877 
8878 	if (i40e_force_link_state(pf, true))
8879 		return -EAGAIN;
8880 
8881 	err = i40e_vsi_open(vsi);
8882 	if (err)
8883 		return err;
8884 
8885 	/* configure global TSO hardware offload settings */
8886 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8887 						       TCP_FLAG_FIN) >> 16);
8888 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8889 						       TCP_FLAG_FIN |
8890 						       TCP_FLAG_CWR) >> 16);
8891 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8892 	udp_tunnel_get_rx_info(netdev);
8893 
8894 	return 0;
8895 }
8896 
8897 /**
8898  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8899  * @vsi: vsi structure
8900  *
8901  * This updates netdev's number of tx/rx queues
8902  *
8903  * Returns status of setting tx/rx queues
8904  **/
8905 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8906 {
8907 	int ret;
8908 
8909 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8910 					   vsi->num_queue_pairs);
8911 	if (ret)
8912 		return ret;
8913 
8914 	return netif_set_real_num_tx_queues(vsi->netdev,
8915 					    vsi->num_queue_pairs);
8916 }
8917 
8918 /**
8919  * i40e_vsi_open -
8920  * @vsi: the VSI to open
8921  *
8922  * Finish initialization of the VSI.
8923  *
8924  * Returns 0 on success, negative value on failure
8925  *
8926  * Note: expects to be called while under rtnl_lock()
8927  **/
8928 int i40e_vsi_open(struct i40e_vsi *vsi)
8929 {
8930 	struct i40e_pf *pf = vsi->back;
8931 	char int_name[I40E_INT_NAME_STR_LEN];
8932 	int err;
8933 
8934 	/* allocate descriptors */
8935 	err = i40e_vsi_setup_tx_resources(vsi);
8936 	if (err)
8937 		goto err_setup_tx;
8938 	err = i40e_vsi_setup_rx_resources(vsi);
8939 	if (err)
8940 		goto err_setup_rx;
8941 
8942 	err = i40e_vsi_configure(vsi);
8943 	if (err)
8944 		goto err_setup_rx;
8945 
8946 	if (vsi->netdev) {
8947 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8948 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8949 		err = i40e_vsi_request_irq(vsi, int_name);
8950 		if (err)
8951 			goto err_setup_rx;
8952 
8953 		/* Notify the stack of the actual queue counts. */
8954 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8955 		if (err)
8956 			goto err_set_queues;
8957 
8958 	} else if (vsi->type == I40E_VSI_FDIR) {
8959 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8960 			 dev_driver_string(&pf->pdev->dev),
8961 			 dev_name(&pf->pdev->dev));
8962 		err = i40e_vsi_request_irq(vsi, int_name);
8963 		if (err)
8964 			goto err_setup_rx;
8965 
8966 	} else {
8967 		err = -EINVAL;
8968 		goto err_setup_rx;
8969 	}
8970 
8971 	err = i40e_up_complete(vsi);
8972 	if (err)
8973 		goto err_up_complete;
8974 
8975 	return 0;
8976 
8977 err_up_complete:
8978 	i40e_down(vsi);
8979 err_set_queues:
8980 	i40e_vsi_free_irq(vsi);
8981 err_setup_rx:
8982 	i40e_vsi_free_rx_resources(vsi);
8983 err_setup_tx:
8984 	i40e_vsi_free_tx_resources(vsi);
8985 	if (vsi == pf->vsi[pf->lan_vsi])
8986 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8987 
8988 	return err;
8989 }
8990 
8991 /**
8992  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8993  * @pf: Pointer to PF
8994  *
8995  * This function destroys the hlist where all the Flow Director
8996  * filters were saved.
8997  **/
8998 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8999 {
9000 	struct i40e_fdir_filter *filter;
9001 	struct i40e_flex_pit *pit_entry, *tmp;
9002 	struct hlist_node *node2;
9003 
9004 	hlist_for_each_entry_safe(filter, node2,
9005 				  &pf->fdir_filter_list, fdir_node) {
9006 		hlist_del(&filter->fdir_node);
9007 		kfree(filter);
9008 	}
9009 
9010 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9011 		list_del(&pit_entry->list);
9012 		kfree(pit_entry);
9013 	}
9014 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9015 
9016 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9017 		list_del(&pit_entry->list);
9018 		kfree(pit_entry);
9019 	}
9020 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9021 
9022 	pf->fdir_pf_active_filters = 0;
9023 	i40e_reset_fdir_filter_cnt(pf);
9024 
9025 	/* Reprogram the default input set for TCP/IPv4 */
9026 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9027 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9028 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9029 
9030 	/* Reprogram the default input set for TCP/IPv6 */
9031 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9032 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9033 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9034 
9035 	/* Reprogram the default input set for UDP/IPv4 */
9036 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9037 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9038 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9039 
9040 	/* Reprogram the default input set for UDP/IPv6 */
9041 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9042 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9043 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9044 
9045 	/* Reprogram the default input set for SCTP/IPv4 */
9046 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9047 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9048 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9049 
9050 	/* Reprogram the default input set for SCTP/IPv6 */
9051 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9052 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9053 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9054 
9055 	/* Reprogram the default input set for Other/IPv4 */
9056 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9057 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9058 
9059 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9060 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9061 
9062 	/* Reprogram the default input set for Other/IPv6 */
9063 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9064 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9065 
9066 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9067 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9068 }
9069 
9070 /**
9071  * i40e_cloud_filter_exit - Cleans up the cloud filters
9072  * @pf: Pointer to PF
9073  *
9074  * This function destroys the hlist where all the cloud filters
9075  * were saved.
9076  **/
9077 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9078 {
9079 	struct i40e_cloud_filter *cfilter;
9080 	struct hlist_node *node;
9081 
9082 	hlist_for_each_entry_safe(cfilter, node,
9083 				  &pf->cloud_filter_list, cloud_node) {
9084 		hlist_del(&cfilter->cloud_node);
9085 		kfree(cfilter);
9086 	}
9087 	pf->num_cloud_filters = 0;
9088 
9089 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9090 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9091 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9092 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9093 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9094 	}
9095 }
9096 
9097 /**
9098  * i40e_close - Disables a network interface
9099  * @netdev: network interface device structure
9100  *
9101  * The close entry point is called when an interface is de-activated
9102  * by the OS.  The hardware is still under the driver's control, but
9103  * this netdev interface is disabled.
9104  *
9105  * Returns 0, this is not allowed to fail
9106  **/
9107 int i40e_close(struct net_device *netdev)
9108 {
9109 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9110 	struct i40e_vsi *vsi = np->vsi;
9111 
9112 	i40e_vsi_close(vsi);
9113 
9114 	return 0;
9115 }
9116 
9117 /**
9118  * i40e_do_reset - Start a PF or Core Reset sequence
9119  * @pf: board private structure
9120  * @reset_flags: which reset is requested
9121  * @lock_acquired: indicates whether or not the lock has been acquired
9122  * before this function was called.
9123  *
9124  * The essential difference in resets is that the PF Reset
9125  * doesn't clear the packet buffers, doesn't reset the PE
9126  * firmware, and doesn't bother the other PFs on the chip.
9127  **/
9128 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9129 {
9130 	u32 val;
9131 
9132 	/* do the biggest reset indicated */
9133 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9134 
9135 		/* Request a Global Reset
9136 		 *
9137 		 * This will start the chip's countdown to the actual full
9138 		 * chip reset event, and a warning interrupt to be sent
9139 		 * to all PFs, including the requestor.  Our handler
9140 		 * for the warning interrupt will deal with the shutdown
9141 		 * and recovery of the switch setup.
9142 		 */
9143 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9144 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9145 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9146 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9147 
9148 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9149 
9150 		/* Request a Core Reset
9151 		 *
9152 		 * Same as Global Reset, except does *not* include the MAC/PHY
9153 		 */
9154 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9155 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9156 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9157 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9158 		i40e_flush(&pf->hw);
9159 
9160 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9161 
9162 		/* Request a PF Reset
9163 		 *
9164 		 * Resets only the PF-specific registers
9165 		 *
9166 		 * This goes directly to the tear-down and rebuild of
9167 		 * the switch, since we need to do all the recovery as
9168 		 * for the Core Reset.
9169 		 */
9170 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9171 		i40e_handle_reset_warning(pf, lock_acquired);
9172 
9173 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9174 		/* Request a PF Reset
9175 		 *
9176 		 * Resets PF and reinitializes PFs VSI.
9177 		 */
9178 		i40e_prep_for_reset(pf);
9179 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9180 		dev_info(&pf->pdev->dev,
9181 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9182 			 "FW LLDP is disabled\n" :
9183 			 "FW LLDP is enabled\n");
9184 
9185 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9186 		int v;
9187 
9188 		/* Find the VSI(s) that requested a re-init */
9189 		dev_info(&pf->pdev->dev,
9190 			 "VSI reinit requested\n");
9191 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9192 			struct i40e_vsi *vsi = pf->vsi[v];
9193 
9194 			if (vsi != NULL &&
9195 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9196 					       vsi->state))
9197 				i40e_vsi_reinit_locked(pf->vsi[v]);
9198 		}
9199 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9200 		int v;
9201 
9202 		/* Find the VSI(s) that needs to be brought down */
9203 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9204 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9205 			struct i40e_vsi *vsi = pf->vsi[v];
9206 
9207 			if (vsi != NULL &&
9208 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9209 					       vsi->state)) {
9210 				set_bit(__I40E_VSI_DOWN, vsi->state);
9211 				i40e_down(vsi);
9212 			}
9213 		}
9214 	} else {
9215 		dev_info(&pf->pdev->dev,
9216 			 "bad reset request 0x%08x\n", reset_flags);
9217 	}
9218 }
9219 
9220 #ifdef CONFIG_I40E_DCB
9221 /**
9222  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9223  * @pf: board private structure
9224  * @old_cfg: current DCB config
9225  * @new_cfg: new DCB config
9226  **/
9227 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9228 			    struct i40e_dcbx_config *old_cfg,
9229 			    struct i40e_dcbx_config *new_cfg)
9230 {
9231 	bool need_reconfig = false;
9232 
9233 	/* Check if ETS configuration has changed */
9234 	if (memcmp(&new_cfg->etscfg,
9235 		   &old_cfg->etscfg,
9236 		   sizeof(new_cfg->etscfg))) {
9237 		/* If Priority Table has changed reconfig is needed */
9238 		if (memcmp(&new_cfg->etscfg.prioritytable,
9239 			   &old_cfg->etscfg.prioritytable,
9240 			   sizeof(new_cfg->etscfg.prioritytable))) {
9241 			need_reconfig = true;
9242 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9243 		}
9244 
9245 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9246 			   &old_cfg->etscfg.tcbwtable,
9247 			   sizeof(new_cfg->etscfg.tcbwtable)))
9248 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9249 
9250 		if (memcmp(&new_cfg->etscfg.tsatable,
9251 			   &old_cfg->etscfg.tsatable,
9252 			   sizeof(new_cfg->etscfg.tsatable)))
9253 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9254 	}
9255 
9256 	/* Check if PFC configuration has changed */
9257 	if (memcmp(&new_cfg->pfc,
9258 		   &old_cfg->pfc,
9259 		   sizeof(new_cfg->pfc))) {
9260 		need_reconfig = true;
9261 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9262 	}
9263 
9264 	/* Check if APP Table has changed */
9265 	if (memcmp(&new_cfg->app,
9266 		   &old_cfg->app,
9267 		   sizeof(new_cfg->app))) {
9268 		need_reconfig = true;
9269 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9270 	}
9271 
9272 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9273 	return need_reconfig;
9274 }
9275 
9276 /**
9277  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9278  * @pf: board private structure
9279  * @e: event info posted on ARQ
9280  **/
9281 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9282 				  struct i40e_arq_event_info *e)
9283 {
9284 	struct i40e_aqc_lldp_get_mib *mib =
9285 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9286 	struct i40e_hw *hw = &pf->hw;
9287 	struct i40e_dcbx_config tmp_dcbx_cfg;
9288 	bool need_reconfig = false;
9289 	int ret = 0;
9290 	u8 type;
9291 
9292 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9293 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9294 	    (hw->phy.link_info.link_speed &
9295 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9296 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9297 		/* let firmware decide if the DCB should be disabled */
9298 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9299 
9300 	/* Not DCB capable or capability disabled */
9301 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9302 		return ret;
9303 
9304 	/* Ignore if event is not for Nearest Bridge */
9305 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9306 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9307 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9308 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9309 		return ret;
9310 
9311 	/* Check MIB Type and return if event for Remote MIB update */
9312 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9313 	dev_dbg(&pf->pdev->dev,
9314 		"LLDP event mib type %s\n", type ? "remote" : "local");
9315 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9316 		/* Update the remote cached instance and return */
9317 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9318 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9319 				&hw->remote_dcbx_config);
9320 		goto exit;
9321 	}
9322 
9323 	/* Store the old configuration */
9324 	tmp_dcbx_cfg = hw->local_dcbx_config;
9325 
9326 	/* Reset the old DCBx configuration data */
9327 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9328 	/* Get updated DCBX data from firmware */
9329 	ret = i40e_get_dcb_config(&pf->hw);
9330 	if (ret) {
9331 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9332 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9333 		    (hw->phy.link_info.link_speed &
9334 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9335 			dev_warn(&pf->pdev->dev,
9336 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9337 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9338 		} else {
9339 			dev_info(&pf->pdev->dev,
9340 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9341 				 i40e_stat_str(&pf->hw, ret),
9342 				 i40e_aq_str(&pf->hw,
9343 					     pf->hw.aq.asq_last_status));
9344 		}
9345 		goto exit;
9346 	}
9347 
9348 	/* No change detected in DCBX configs */
9349 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9350 		    sizeof(tmp_dcbx_cfg))) {
9351 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9352 		goto exit;
9353 	}
9354 
9355 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9356 					       &hw->local_dcbx_config);
9357 
9358 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9359 
9360 	if (!need_reconfig)
9361 		goto exit;
9362 
9363 	/* Enable DCB tagging only when more than one TC */
9364 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9365 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9366 	else
9367 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9368 
9369 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9370 	/* Reconfiguration needed quiesce all VSIs */
9371 	i40e_pf_quiesce_all_vsi(pf);
9372 
9373 	/* Changes in configuration update VEB/VSI */
9374 	i40e_dcb_reconfigure(pf);
9375 
9376 	ret = i40e_resume_port_tx(pf);
9377 
9378 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9379 	/* In case of error no point in resuming VSIs */
9380 	if (ret)
9381 		goto exit;
9382 
9383 	/* Wait for the PF's queues to be disabled */
9384 	ret = i40e_pf_wait_queues_disabled(pf);
9385 	if (ret) {
9386 		/* Schedule PF reset to recover */
9387 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9388 		i40e_service_event_schedule(pf);
9389 	} else {
9390 		i40e_pf_unquiesce_all_vsi(pf);
9391 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9392 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9393 	}
9394 
9395 exit:
9396 	return ret;
9397 }
9398 #endif /* CONFIG_I40E_DCB */
9399 
9400 /**
9401  * i40e_do_reset_safe - Protected reset path for userland calls.
9402  * @pf: board private structure
9403  * @reset_flags: which reset is requested
9404  *
9405  **/
9406 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9407 {
9408 	rtnl_lock();
9409 	i40e_do_reset(pf, reset_flags, true);
9410 	rtnl_unlock();
9411 }
9412 
9413 /**
9414  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9415  * @pf: board private structure
9416  * @e: event info posted on ARQ
9417  *
9418  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9419  * and VF queues
9420  **/
9421 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9422 					   struct i40e_arq_event_info *e)
9423 {
9424 	struct i40e_aqc_lan_overflow *data =
9425 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9426 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9427 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9428 	struct i40e_hw *hw = &pf->hw;
9429 	struct i40e_vf *vf;
9430 	u16 vf_id;
9431 
9432 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9433 		queue, qtx_ctl);
9434 
9435 	/* Queue belongs to VF, find the VF and issue VF reset */
9436 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9437 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9438 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9439 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9440 		vf_id -= hw->func_caps.vf_base_id;
9441 		vf = &pf->vf[vf_id];
9442 		i40e_vc_notify_vf_reset(vf);
9443 		/* Allow VF to process pending reset notification */
9444 		msleep(20);
9445 		i40e_reset_vf(vf, false);
9446 	}
9447 }
9448 
9449 /**
9450  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9451  * @pf: board private structure
9452  **/
9453 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9454 {
9455 	u32 val, fcnt_prog;
9456 
9457 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9458 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9459 	return fcnt_prog;
9460 }
9461 
9462 /**
9463  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9464  * @pf: board private structure
9465  **/
9466 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9467 {
9468 	u32 val, fcnt_prog;
9469 
9470 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9471 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9472 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9473 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9474 	return fcnt_prog;
9475 }
9476 
9477 /**
9478  * i40e_get_global_fd_count - Get total FD filters programmed on device
9479  * @pf: board private structure
9480  **/
9481 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9482 {
9483 	u32 val, fcnt_prog;
9484 
9485 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9486 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9487 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9488 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9489 	return fcnt_prog;
9490 }
9491 
9492 /**
9493  * i40e_reenable_fdir_sb - Restore FDir SB capability
9494  * @pf: board private structure
9495  **/
9496 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9497 {
9498 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9499 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9500 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9501 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9502 }
9503 
9504 /**
9505  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9506  * @pf: board private structure
9507  **/
9508 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9509 {
9510 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9511 		/* ATR uses the same filtering logic as SB rules. It only
9512 		 * functions properly if the input set mask is at the default
9513 		 * settings. It is safe to restore the default input set
9514 		 * because there are no active TCPv4 filter rules.
9515 		 */
9516 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9517 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9518 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9519 
9520 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9521 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9522 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9523 	}
9524 }
9525 
9526 /**
9527  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9528  * @pf: board private structure
9529  * @filter: FDir filter to remove
9530  */
9531 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9532 				       struct i40e_fdir_filter *filter)
9533 {
9534 	/* Update counters */
9535 	pf->fdir_pf_active_filters--;
9536 	pf->fd_inv = 0;
9537 
9538 	switch (filter->flow_type) {
9539 	case TCP_V4_FLOW:
9540 		pf->fd_tcp4_filter_cnt--;
9541 		break;
9542 	case UDP_V4_FLOW:
9543 		pf->fd_udp4_filter_cnt--;
9544 		break;
9545 	case SCTP_V4_FLOW:
9546 		pf->fd_sctp4_filter_cnt--;
9547 		break;
9548 	case TCP_V6_FLOW:
9549 		pf->fd_tcp6_filter_cnt--;
9550 		break;
9551 	case UDP_V6_FLOW:
9552 		pf->fd_udp6_filter_cnt--;
9553 		break;
9554 	case SCTP_V6_FLOW:
9555 		pf->fd_udp6_filter_cnt--;
9556 		break;
9557 	case IP_USER_FLOW:
9558 		switch (filter->ipl4_proto) {
9559 		case IPPROTO_TCP:
9560 			pf->fd_tcp4_filter_cnt--;
9561 			break;
9562 		case IPPROTO_UDP:
9563 			pf->fd_udp4_filter_cnt--;
9564 			break;
9565 		case IPPROTO_SCTP:
9566 			pf->fd_sctp4_filter_cnt--;
9567 			break;
9568 		case IPPROTO_IP:
9569 			pf->fd_ip4_filter_cnt--;
9570 			break;
9571 		}
9572 		break;
9573 	case IPV6_USER_FLOW:
9574 		switch (filter->ipl4_proto) {
9575 		case IPPROTO_TCP:
9576 			pf->fd_tcp6_filter_cnt--;
9577 			break;
9578 		case IPPROTO_UDP:
9579 			pf->fd_udp6_filter_cnt--;
9580 			break;
9581 		case IPPROTO_SCTP:
9582 			pf->fd_sctp6_filter_cnt--;
9583 			break;
9584 		case IPPROTO_IP:
9585 			pf->fd_ip6_filter_cnt--;
9586 			break;
9587 		}
9588 		break;
9589 	}
9590 
9591 	/* Remove the filter from the list and free memory */
9592 	hlist_del(&filter->fdir_node);
9593 	kfree(filter);
9594 }
9595 
9596 /**
9597  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9598  * @pf: board private structure
9599  **/
9600 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9601 {
9602 	struct i40e_fdir_filter *filter;
9603 	u32 fcnt_prog, fcnt_avail;
9604 	struct hlist_node *node;
9605 
9606 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9607 		return;
9608 
9609 	/* Check if we have enough room to re-enable FDir SB capability. */
9610 	fcnt_prog = i40e_get_global_fd_count(pf);
9611 	fcnt_avail = pf->fdir_pf_filter_count;
9612 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9613 	    (pf->fd_add_err == 0) ||
9614 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9615 		i40e_reenable_fdir_sb(pf);
9616 
9617 	/* We should wait for even more space before re-enabling ATR.
9618 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9619 	 * rules active.
9620 	 */
9621 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9622 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9623 		i40e_reenable_fdir_atr(pf);
9624 
9625 	/* if hw had a problem adding a filter, delete it */
9626 	if (pf->fd_inv > 0) {
9627 		hlist_for_each_entry_safe(filter, node,
9628 					  &pf->fdir_filter_list, fdir_node)
9629 			if (filter->fd_id == pf->fd_inv)
9630 				i40e_delete_invalid_filter(pf, filter);
9631 	}
9632 }
9633 
9634 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9635 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9636 /**
9637  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9638  * @pf: board private structure
9639  **/
9640 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9641 {
9642 	unsigned long min_flush_time;
9643 	int flush_wait_retry = 50;
9644 	bool disable_atr = false;
9645 	int fd_room;
9646 	int reg;
9647 
9648 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9649 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9650 		return;
9651 
9652 	/* If the flush is happening too quick and we have mostly SB rules we
9653 	 * should not re-enable ATR for some time.
9654 	 */
9655 	min_flush_time = pf->fd_flush_timestamp +
9656 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9657 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9658 
9659 	if (!(time_after(jiffies, min_flush_time)) &&
9660 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9661 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9662 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9663 		disable_atr = true;
9664 	}
9665 
9666 	pf->fd_flush_timestamp = jiffies;
9667 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9668 	/* flush all filters */
9669 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9670 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9671 	i40e_flush(&pf->hw);
9672 	pf->fd_flush_cnt++;
9673 	pf->fd_add_err = 0;
9674 	do {
9675 		/* Check FD flush status every 5-6msec */
9676 		usleep_range(5000, 6000);
9677 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9678 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9679 			break;
9680 	} while (flush_wait_retry--);
9681 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9682 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9683 	} else {
9684 		/* replay sideband filters */
9685 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9686 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9687 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9688 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9689 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9690 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9691 	}
9692 }
9693 
9694 /**
9695  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9696  * @pf: board private structure
9697  **/
9698 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9699 {
9700 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9701 }
9702 
9703 /**
9704  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9705  * @pf: board private structure
9706  **/
9707 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9708 {
9709 
9710 	/* if interface is down do nothing */
9711 	if (test_bit(__I40E_DOWN, pf->state))
9712 		return;
9713 
9714 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9715 		i40e_fdir_flush_and_replay(pf);
9716 
9717 	i40e_fdir_check_and_reenable(pf);
9718 
9719 }
9720 
9721 /**
9722  * i40e_vsi_link_event - notify VSI of a link event
9723  * @vsi: vsi to be notified
9724  * @link_up: link up or down
9725  **/
9726 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9727 {
9728 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9729 		return;
9730 
9731 	switch (vsi->type) {
9732 	case I40E_VSI_MAIN:
9733 		if (!vsi->netdev || !vsi->netdev_registered)
9734 			break;
9735 
9736 		if (link_up) {
9737 			netif_carrier_on(vsi->netdev);
9738 			netif_tx_wake_all_queues(vsi->netdev);
9739 		} else {
9740 			netif_carrier_off(vsi->netdev);
9741 			netif_tx_stop_all_queues(vsi->netdev);
9742 		}
9743 		break;
9744 
9745 	case I40E_VSI_SRIOV:
9746 	case I40E_VSI_VMDQ2:
9747 	case I40E_VSI_CTRL:
9748 	case I40E_VSI_IWARP:
9749 	case I40E_VSI_MIRROR:
9750 	default:
9751 		/* there is no notification for other VSIs */
9752 		break;
9753 	}
9754 }
9755 
9756 /**
9757  * i40e_veb_link_event - notify elements on the veb of a link event
9758  * @veb: veb to be notified
9759  * @link_up: link up or down
9760  **/
9761 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9762 {
9763 	struct i40e_pf *pf;
9764 	int i;
9765 
9766 	if (!veb || !veb->pf)
9767 		return;
9768 	pf = veb->pf;
9769 
9770 	/* depth first... */
9771 	for (i = 0; i < I40E_MAX_VEB; i++)
9772 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9773 			i40e_veb_link_event(pf->veb[i], link_up);
9774 
9775 	/* ... now the local VSIs */
9776 	for (i = 0; i < pf->num_alloc_vsi; i++)
9777 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9778 			i40e_vsi_link_event(pf->vsi[i], link_up);
9779 }
9780 
9781 /**
9782  * i40e_link_event - Update netif_carrier status
9783  * @pf: board private structure
9784  **/
9785 static void i40e_link_event(struct i40e_pf *pf)
9786 {
9787 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9788 	u8 new_link_speed, old_link_speed;
9789 	i40e_status status;
9790 	bool new_link, old_link;
9791 #ifdef CONFIG_I40E_DCB
9792 	int err;
9793 #endif /* CONFIG_I40E_DCB */
9794 
9795 	/* set this to force the get_link_status call to refresh state */
9796 	pf->hw.phy.get_link_info = true;
9797 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9798 	status = i40e_get_link_status(&pf->hw, &new_link);
9799 
9800 	/* On success, disable temp link polling */
9801 	if (status == I40E_SUCCESS) {
9802 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9803 	} else {
9804 		/* Enable link polling temporarily until i40e_get_link_status
9805 		 * returns I40E_SUCCESS
9806 		 */
9807 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9808 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9809 			status);
9810 		return;
9811 	}
9812 
9813 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9814 	new_link_speed = pf->hw.phy.link_info.link_speed;
9815 
9816 	if (new_link == old_link &&
9817 	    new_link_speed == old_link_speed &&
9818 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9819 	     new_link == netif_carrier_ok(vsi->netdev)))
9820 		return;
9821 
9822 	i40e_print_link_message(vsi, new_link);
9823 
9824 	/* Notify the base of the switch tree connected to
9825 	 * the link.  Floating VEBs are not notified.
9826 	 */
9827 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9828 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9829 	else
9830 		i40e_vsi_link_event(vsi, new_link);
9831 
9832 	if (pf->vf)
9833 		i40e_vc_notify_link_state(pf);
9834 
9835 	if (pf->flags & I40E_FLAG_PTP)
9836 		i40e_ptp_set_increment(pf);
9837 #ifdef CONFIG_I40E_DCB
9838 	if (new_link == old_link)
9839 		return;
9840 	/* Not SW DCB so firmware will take care of default settings */
9841 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9842 		return;
9843 
9844 	/* We cover here only link down, as after link up in case of SW DCB
9845 	 * SW LLDP agent will take care of setting it up
9846 	 */
9847 	if (!new_link) {
9848 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9849 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9850 		err = i40e_dcb_sw_default_config(pf);
9851 		if (err) {
9852 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9853 				       I40E_FLAG_DCB_ENABLED);
9854 		} else {
9855 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9856 				       DCB_CAP_DCBX_VER_IEEE;
9857 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9858 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9859 		}
9860 	}
9861 #endif /* CONFIG_I40E_DCB */
9862 }
9863 
9864 /**
9865  * i40e_watchdog_subtask - periodic checks not using event driven response
9866  * @pf: board private structure
9867  **/
9868 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9869 {
9870 	int i;
9871 
9872 	/* if interface is down do nothing */
9873 	if (test_bit(__I40E_DOWN, pf->state) ||
9874 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9875 		return;
9876 
9877 	/* make sure we don't do these things too often */
9878 	if (time_before(jiffies, (pf->service_timer_previous +
9879 				  pf->service_timer_period)))
9880 		return;
9881 	pf->service_timer_previous = jiffies;
9882 
9883 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9884 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9885 		i40e_link_event(pf);
9886 
9887 	/* Update the stats for active netdevs so the network stack
9888 	 * can look at updated numbers whenever it cares to
9889 	 */
9890 	for (i = 0; i < pf->num_alloc_vsi; i++)
9891 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9892 			i40e_update_stats(pf->vsi[i]);
9893 
9894 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9895 		/* Update the stats for the active switching components */
9896 		for (i = 0; i < I40E_MAX_VEB; i++)
9897 			if (pf->veb[i])
9898 				i40e_update_veb_stats(pf->veb[i]);
9899 	}
9900 
9901 	i40e_ptp_rx_hang(pf);
9902 	i40e_ptp_tx_hang(pf);
9903 }
9904 
9905 /**
9906  * i40e_reset_subtask - Set up for resetting the device and driver
9907  * @pf: board private structure
9908  **/
9909 static void i40e_reset_subtask(struct i40e_pf *pf)
9910 {
9911 	u32 reset_flags = 0;
9912 
9913 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9914 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9915 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9916 	}
9917 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9918 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9919 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9920 	}
9921 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9922 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9923 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9924 	}
9925 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9926 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9927 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9928 	}
9929 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9930 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9931 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9932 	}
9933 
9934 	/* If there's a recovery already waiting, it takes
9935 	 * precedence before starting a new reset sequence.
9936 	 */
9937 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9938 		i40e_prep_for_reset(pf);
9939 		i40e_reset(pf);
9940 		i40e_rebuild(pf, false, false);
9941 	}
9942 
9943 	/* If we're already down or resetting, just bail */
9944 	if (reset_flags &&
9945 	    !test_bit(__I40E_DOWN, pf->state) &&
9946 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9947 		i40e_do_reset(pf, reset_flags, false);
9948 	}
9949 }
9950 
9951 /**
9952  * i40e_handle_link_event - Handle link event
9953  * @pf: board private structure
9954  * @e: event info posted on ARQ
9955  **/
9956 static void i40e_handle_link_event(struct i40e_pf *pf,
9957 				   struct i40e_arq_event_info *e)
9958 {
9959 	struct i40e_aqc_get_link_status *status =
9960 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9961 
9962 	/* Do a new status request to re-enable LSE reporting
9963 	 * and load new status information into the hw struct
9964 	 * This completely ignores any state information
9965 	 * in the ARQ event info, instead choosing to always
9966 	 * issue the AQ update link status command.
9967 	 */
9968 	i40e_link_event(pf);
9969 
9970 	/* Check if module meets thermal requirements */
9971 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9972 		dev_err(&pf->pdev->dev,
9973 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9974 		dev_err(&pf->pdev->dev,
9975 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9976 	} else {
9977 		/* check for unqualified module, if link is down, suppress
9978 		 * the message if link was forced to be down.
9979 		 */
9980 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9981 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9982 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9983 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9984 			dev_err(&pf->pdev->dev,
9985 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9986 			dev_err(&pf->pdev->dev,
9987 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9988 		}
9989 	}
9990 }
9991 
9992 /**
9993  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9994  * @pf: board private structure
9995  **/
9996 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9997 {
9998 	struct i40e_arq_event_info event;
9999 	struct i40e_hw *hw = &pf->hw;
10000 	u16 pending, i = 0;
10001 	i40e_status ret;
10002 	u16 opcode;
10003 	u32 oldval;
10004 	u32 val;
10005 
10006 	/* Do not run clean AQ when PF reset fails */
10007 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10008 		return;
10009 
10010 	/* check for error indications */
10011 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10012 	oldval = val;
10013 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10014 		if (hw->debug_mask & I40E_DEBUG_AQ)
10015 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10016 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10017 	}
10018 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10019 		if (hw->debug_mask & I40E_DEBUG_AQ)
10020 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10021 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10022 		pf->arq_overflows++;
10023 	}
10024 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10025 		if (hw->debug_mask & I40E_DEBUG_AQ)
10026 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10027 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10028 	}
10029 	if (oldval != val)
10030 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10031 
10032 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10033 	oldval = val;
10034 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10035 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10036 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10037 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10038 	}
10039 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10040 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10041 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10042 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10043 	}
10044 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10045 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10046 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10047 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10048 	}
10049 	if (oldval != val)
10050 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10051 
10052 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10053 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10054 	if (!event.msg_buf)
10055 		return;
10056 
10057 	do {
10058 		ret = i40e_clean_arq_element(hw, &event, &pending);
10059 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10060 			break;
10061 		else if (ret) {
10062 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10063 			break;
10064 		}
10065 
10066 		opcode = le16_to_cpu(event.desc.opcode);
10067 		switch (opcode) {
10068 
10069 		case i40e_aqc_opc_get_link_status:
10070 			rtnl_lock();
10071 			i40e_handle_link_event(pf, &event);
10072 			rtnl_unlock();
10073 			break;
10074 		case i40e_aqc_opc_send_msg_to_pf:
10075 			ret = i40e_vc_process_vf_msg(pf,
10076 					le16_to_cpu(event.desc.retval),
10077 					le32_to_cpu(event.desc.cookie_high),
10078 					le32_to_cpu(event.desc.cookie_low),
10079 					event.msg_buf,
10080 					event.msg_len);
10081 			break;
10082 		case i40e_aqc_opc_lldp_update_mib:
10083 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10084 #ifdef CONFIG_I40E_DCB
10085 			rtnl_lock();
10086 			i40e_handle_lldp_event(pf, &event);
10087 			rtnl_unlock();
10088 #endif /* CONFIG_I40E_DCB */
10089 			break;
10090 		case i40e_aqc_opc_event_lan_overflow:
10091 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10092 			i40e_handle_lan_overflow_event(pf, &event);
10093 			break;
10094 		case i40e_aqc_opc_send_msg_to_peer:
10095 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10096 			break;
10097 		case i40e_aqc_opc_nvm_erase:
10098 		case i40e_aqc_opc_nvm_update:
10099 		case i40e_aqc_opc_oem_post_update:
10100 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10101 				   "ARQ NVM operation 0x%04x completed\n",
10102 				   opcode);
10103 			break;
10104 		default:
10105 			dev_info(&pf->pdev->dev,
10106 				 "ARQ: Unknown event 0x%04x ignored\n",
10107 				 opcode);
10108 			break;
10109 		}
10110 	} while (i++ < pf->adminq_work_limit);
10111 
10112 	if (i < pf->adminq_work_limit)
10113 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10114 
10115 	/* re-enable Admin queue interrupt cause */
10116 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10117 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10118 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10119 	i40e_flush(hw);
10120 
10121 	kfree(event.msg_buf);
10122 }
10123 
10124 /**
10125  * i40e_verify_eeprom - make sure eeprom is good to use
10126  * @pf: board private structure
10127  **/
10128 static void i40e_verify_eeprom(struct i40e_pf *pf)
10129 {
10130 	int err;
10131 
10132 	err = i40e_diag_eeprom_test(&pf->hw);
10133 	if (err) {
10134 		/* retry in case of garbage read */
10135 		err = i40e_diag_eeprom_test(&pf->hw);
10136 		if (err) {
10137 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10138 				 err);
10139 			set_bit(__I40E_BAD_EEPROM, pf->state);
10140 		}
10141 	}
10142 
10143 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10144 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10145 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10146 	}
10147 }
10148 
10149 /**
10150  * i40e_enable_pf_switch_lb
10151  * @pf: pointer to the PF structure
10152  *
10153  * enable switch loop back or die - no point in a return value
10154  **/
10155 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10156 {
10157 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10158 	struct i40e_vsi_context ctxt;
10159 	int ret;
10160 
10161 	ctxt.seid = pf->main_vsi_seid;
10162 	ctxt.pf_num = pf->hw.pf_id;
10163 	ctxt.vf_num = 0;
10164 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10165 	if (ret) {
10166 		dev_info(&pf->pdev->dev,
10167 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10168 			 i40e_stat_str(&pf->hw, ret),
10169 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10170 		return;
10171 	}
10172 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10173 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10174 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10175 
10176 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10177 	if (ret) {
10178 		dev_info(&pf->pdev->dev,
10179 			 "update vsi switch failed, err %s aq_err %s\n",
10180 			 i40e_stat_str(&pf->hw, ret),
10181 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10182 	}
10183 }
10184 
10185 /**
10186  * i40e_disable_pf_switch_lb
10187  * @pf: pointer to the PF structure
10188  *
10189  * disable switch loop back or die - no point in a return value
10190  **/
10191 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10192 {
10193 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10194 	struct i40e_vsi_context ctxt;
10195 	int ret;
10196 
10197 	ctxt.seid = pf->main_vsi_seid;
10198 	ctxt.pf_num = pf->hw.pf_id;
10199 	ctxt.vf_num = 0;
10200 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10201 	if (ret) {
10202 		dev_info(&pf->pdev->dev,
10203 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10204 			 i40e_stat_str(&pf->hw, ret),
10205 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10206 		return;
10207 	}
10208 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10209 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10210 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10211 
10212 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10213 	if (ret) {
10214 		dev_info(&pf->pdev->dev,
10215 			 "update vsi switch failed, err %s aq_err %s\n",
10216 			 i40e_stat_str(&pf->hw, ret),
10217 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10218 	}
10219 }
10220 
10221 /**
10222  * i40e_config_bridge_mode - Configure the HW bridge mode
10223  * @veb: pointer to the bridge instance
10224  *
10225  * Configure the loop back mode for the LAN VSI that is downlink to the
10226  * specified HW bridge instance. It is expected this function is called
10227  * when a new HW bridge is instantiated.
10228  **/
10229 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10230 {
10231 	struct i40e_pf *pf = veb->pf;
10232 
10233 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10234 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10235 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10236 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10237 		i40e_disable_pf_switch_lb(pf);
10238 	else
10239 		i40e_enable_pf_switch_lb(pf);
10240 }
10241 
10242 /**
10243  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10244  * @veb: pointer to the VEB instance
10245  *
10246  * This is a recursive function that first builds the attached VSIs then
10247  * recurses in to build the next layer of VEB.  We track the connections
10248  * through our own index numbers because the seid's from the HW could
10249  * change across the reset.
10250  **/
10251 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10252 {
10253 	struct i40e_vsi *ctl_vsi = NULL;
10254 	struct i40e_pf *pf = veb->pf;
10255 	int v, veb_idx;
10256 	int ret;
10257 
10258 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10259 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10260 		if (pf->vsi[v] &&
10261 		    pf->vsi[v]->veb_idx == veb->idx &&
10262 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10263 			ctl_vsi = pf->vsi[v];
10264 			break;
10265 		}
10266 	}
10267 	if (!ctl_vsi) {
10268 		dev_info(&pf->pdev->dev,
10269 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10270 		ret = -ENOENT;
10271 		goto end_reconstitute;
10272 	}
10273 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10274 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10275 	ret = i40e_add_vsi(ctl_vsi);
10276 	if (ret) {
10277 		dev_info(&pf->pdev->dev,
10278 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10279 			 veb->idx, ret);
10280 		goto end_reconstitute;
10281 	}
10282 	i40e_vsi_reset_stats(ctl_vsi);
10283 
10284 	/* create the VEB in the switch and move the VSI onto the VEB */
10285 	ret = i40e_add_veb(veb, ctl_vsi);
10286 	if (ret)
10287 		goto end_reconstitute;
10288 
10289 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10290 		veb->bridge_mode = BRIDGE_MODE_VEB;
10291 	else
10292 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10293 	i40e_config_bridge_mode(veb);
10294 
10295 	/* create the remaining VSIs attached to this VEB */
10296 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10297 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10298 			continue;
10299 
10300 		if (pf->vsi[v]->veb_idx == veb->idx) {
10301 			struct i40e_vsi *vsi = pf->vsi[v];
10302 
10303 			vsi->uplink_seid = veb->seid;
10304 			ret = i40e_add_vsi(vsi);
10305 			if (ret) {
10306 				dev_info(&pf->pdev->dev,
10307 					 "rebuild of vsi_idx %d failed: %d\n",
10308 					 v, ret);
10309 				goto end_reconstitute;
10310 			}
10311 			i40e_vsi_reset_stats(vsi);
10312 		}
10313 	}
10314 
10315 	/* create any VEBs attached to this VEB - RECURSION */
10316 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10317 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10318 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10319 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10320 			if (ret)
10321 				break;
10322 		}
10323 	}
10324 
10325 end_reconstitute:
10326 	return ret;
10327 }
10328 
10329 /**
10330  * i40e_get_capabilities - get info about the HW
10331  * @pf: the PF struct
10332  * @list_type: AQ capability to be queried
10333  **/
10334 static int i40e_get_capabilities(struct i40e_pf *pf,
10335 				 enum i40e_admin_queue_opc list_type)
10336 {
10337 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10338 	u16 data_size;
10339 	int buf_len;
10340 	int err;
10341 
10342 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10343 	do {
10344 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10345 		if (!cap_buf)
10346 			return -ENOMEM;
10347 
10348 		/* this loads the data into the hw struct for us */
10349 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10350 						    &data_size, list_type,
10351 						    NULL);
10352 		/* data loaded, buffer no longer needed */
10353 		kfree(cap_buf);
10354 
10355 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10356 			/* retry with a larger buffer */
10357 			buf_len = data_size;
10358 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10359 			dev_info(&pf->pdev->dev,
10360 				 "capability discovery failed, err %s aq_err %s\n",
10361 				 i40e_stat_str(&pf->hw, err),
10362 				 i40e_aq_str(&pf->hw,
10363 					     pf->hw.aq.asq_last_status));
10364 			return -ENODEV;
10365 		}
10366 	} while (err);
10367 
10368 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10369 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10370 			dev_info(&pf->pdev->dev,
10371 				 "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",
10372 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10373 				 pf->hw.func_caps.num_msix_vectors,
10374 				 pf->hw.func_caps.num_msix_vectors_vf,
10375 				 pf->hw.func_caps.fd_filters_guaranteed,
10376 				 pf->hw.func_caps.fd_filters_best_effort,
10377 				 pf->hw.func_caps.num_tx_qp,
10378 				 pf->hw.func_caps.num_vsis);
10379 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10380 			dev_info(&pf->pdev->dev,
10381 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10382 				 pf->hw.dev_caps.switch_mode,
10383 				 pf->hw.dev_caps.valid_functions);
10384 			dev_info(&pf->pdev->dev,
10385 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10386 				 pf->hw.dev_caps.sr_iov_1_1,
10387 				 pf->hw.dev_caps.num_vfs);
10388 			dev_info(&pf->pdev->dev,
10389 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10390 				 pf->hw.dev_caps.num_vsis,
10391 				 pf->hw.dev_caps.num_rx_qp,
10392 				 pf->hw.dev_caps.num_tx_qp);
10393 		}
10394 	}
10395 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10396 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10397 		       + pf->hw.func_caps.num_vfs)
10398 		if (pf->hw.revision_id == 0 &&
10399 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10400 			dev_info(&pf->pdev->dev,
10401 				 "got num_vsis %d, setting num_vsis to %d\n",
10402 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10403 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10404 		}
10405 	}
10406 	return 0;
10407 }
10408 
10409 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10410 
10411 /**
10412  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10413  * @pf: board private structure
10414  **/
10415 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10416 {
10417 	struct i40e_vsi *vsi;
10418 
10419 	/* quick workaround for an NVM issue that leaves a critical register
10420 	 * uninitialized
10421 	 */
10422 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10423 		static const u32 hkey[] = {
10424 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10425 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10426 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10427 			0x95b3a76d};
10428 		int i;
10429 
10430 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10431 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10432 	}
10433 
10434 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10435 		return;
10436 
10437 	/* find existing VSI and see if it needs configuring */
10438 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10439 
10440 	/* create a new VSI if none exists */
10441 	if (!vsi) {
10442 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10443 				     pf->vsi[pf->lan_vsi]->seid, 0);
10444 		if (!vsi) {
10445 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10446 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10447 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10448 			return;
10449 		}
10450 	}
10451 
10452 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10453 }
10454 
10455 /**
10456  * i40e_fdir_teardown - release the Flow Director resources
10457  * @pf: board private structure
10458  **/
10459 static void i40e_fdir_teardown(struct i40e_pf *pf)
10460 {
10461 	struct i40e_vsi *vsi;
10462 
10463 	i40e_fdir_filter_exit(pf);
10464 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10465 	if (vsi)
10466 		i40e_vsi_release(vsi);
10467 }
10468 
10469 /**
10470  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10471  * @vsi: PF main vsi
10472  * @seid: seid of main or channel VSIs
10473  *
10474  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10475  * existed before reset
10476  **/
10477 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10478 {
10479 	struct i40e_cloud_filter *cfilter;
10480 	struct i40e_pf *pf = vsi->back;
10481 	struct hlist_node *node;
10482 	i40e_status ret;
10483 
10484 	/* Add cloud filters back if they exist */
10485 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10486 				  cloud_node) {
10487 		if (cfilter->seid != seid)
10488 			continue;
10489 
10490 		if (cfilter->dst_port)
10491 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10492 								true);
10493 		else
10494 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10495 
10496 		if (ret) {
10497 			dev_dbg(&pf->pdev->dev,
10498 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10499 				i40e_stat_str(&pf->hw, ret),
10500 				i40e_aq_str(&pf->hw,
10501 					    pf->hw.aq.asq_last_status));
10502 			return ret;
10503 		}
10504 	}
10505 	return 0;
10506 }
10507 
10508 /**
10509  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10510  * @vsi: PF main vsi
10511  *
10512  * Rebuilds channel VSIs if they existed before reset
10513  **/
10514 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10515 {
10516 	struct i40e_channel *ch, *ch_tmp;
10517 	i40e_status ret;
10518 
10519 	if (list_empty(&vsi->ch_list))
10520 		return 0;
10521 
10522 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10523 		if (!ch->initialized)
10524 			break;
10525 		/* Proceed with creation of channel (VMDq2) VSI */
10526 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10527 		if (ret) {
10528 			dev_info(&vsi->back->pdev->dev,
10529 				 "failed to rebuild channels using uplink_seid %u\n",
10530 				 vsi->uplink_seid);
10531 			return ret;
10532 		}
10533 		/* Reconfigure TX queues using QTX_CTL register */
10534 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10535 		if (ret) {
10536 			dev_info(&vsi->back->pdev->dev,
10537 				 "failed to configure TX rings for channel %u\n",
10538 				 ch->seid);
10539 			return ret;
10540 		}
10541 		/* update 'next_base_queue' */
10542 		vsi->next_base_queue = vsi->next_base_queue +
10543 							ch->num_queue_pairs;
10544 		if (ch->max_tx_rate) {
10545 			u64 credits = ch->max_tx_rate;
10546 
10547 			if (i40e_set_bw_limit(vsi, ch->seid,
10548 					      ch->max_tx_rate))
10549 				return -EINVAL;
10550 
10551 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10552 			dev_dbg(&vsi->back->pdev->dev,
10553 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10554 				ch->max_tx_rate,
10555 				credits,
10556 				ch->seid);
10557 		}
10558 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10559 		if (ret) {
10560 			dev_dbg(&vsi->back->pdev->dev,
10561 				"Failed to rebuild cloud filters for channel VSI %u\n",
10562 				ch->seid);
10563 			return ret;
10564 		}
10565 	}
10566 	return 0;
10567 }
10568 
10569 /**
10570  * i40e_prep_for_reset - prep for the core to reset
10571  * @pf: board private structure
10572  *
10573  * Close up the VFs and other things in prep for PF Reset.
10574   **/
10575 static void i40e_prep_for_reset(struct i40e_pf *pf)
10576 {
10577 	struct i40e_hw *hw = &pf->hw;
10578 	i40e_status ret = 0;
10579 	u32 v;
10580 
10581 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10582 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10583 		return;
10584 	if (i40e_check_asq_alive(&pf->hw))
10585 		i40e_vc_notify_reset(pf);
10586 
10587 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10588 
10589 	/* quiesce the VSIs and their queues that are not already DOWN */
10590 	i40e_pf_quiesce_all_vsi(pf);
10591 
10592 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10593 		if (pf->vsi[v])
10594 			pf->vsi[v]->seid = 0;
10595 	}
10596 
10597 	i40e_shutdown_adminq(&pf->hw);
10598 
10599 	/* call shutdown HMC */
10600 	if (hw->hmc.hmc_obj) {
10601 		ret = i40e_shutdown_lan_hmc(hw);
10602 		if (ret)
10603 			dev_warn(&pf->pdev->dev,
10604 				 "shutdown_lan_hmc failed: %d\n", ret);
10605 	}
10606 
10607 	/* Save the current PTP time so that we can restore the time after the
10608 	 * reset completes.
10609 	 */
10610 	i40e_ptp_save_hw_time(pf);
10611 }
10612 
10613 /**
10614  * i40e_send_version - update firmware with driver version
10615  * @pf: PF struct
10616  */
10617 static void i40e_send_version(struct i40e_pf *pf)
10618 {
10619 	struct i40e_driver_version dv;
10620 
10621 	dv.major_version = 0xff;
10622 	dv.minor_version = 0xff;
10623 	dv.build_version = 0xff;
10624 	dv.subbuild_version = 0;
10625 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10626 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10627 }
10628 
10629 /**
10630  * i40e_get_oem_version - get OEM specific version information
10631  * @hw: pointer to the hardware structure
10632  **/
10633 static void i40e_get_oem_version(struct i40e_hw *hw)
10634 {
10635 	u16 block_offset = 0xffff;
10636 	u16 block_length = 0;
10637 	u16 capabilities = 0;
10638 	u16 gen_snap = 0;
10639 	u16 release = 0;
10640 
10641 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10642 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10643 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10644 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10645 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10646 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10647 #define I40E_NVM_OEM_LENGTH			3
10648 
10649 	/* Check if pointer to OEM version block is valid. */
10650 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10651 	if (block_offset == 0xffff)
10652 		return;
10653 
10654 	/* Check if OEM version block has correct length. */
10655 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10656 			   &block_length);
10657 	if (block_length < I40E_NVM_OEM_LENGTH)
10658 		return;
10659 
10660 	/* Check if OEM version format is as expected. */
10661 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10662 			   &capabilities);
10663 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10664 		return;
10665 
10666 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10667 			   &gen_snap);
10668 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10669 			   &release);
10670 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10671 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10672 }
10673 
10674 /**
10675  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10676  * @pf: board private structure
10677  **/
10678 static int i40e_reset(struct i40e_pf *pf)
10679 {
10680 	struct i40e_hw *hw = &pf->hw;
10681 	i40e_status ret;
10682 
10683 	ret = i40e_pf_reset(hw);
10684 	if (ret) {
10685 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10686 		set_bit(__I40E_RESET_FAILED, pf->state);
10687 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10688 	} else {
10689 		pf->pfr_count++;
10690 	}
10691 	return ret;
10692 }
10693 
10694 /**
10695  * i40e_rebuild - rebuild using a saved config
10696  * @pf: board private structure
10697  * @reinit: if the Main VSI needs to re-initialized.
10698  * @lock_acquired: indicates whether or not the lock has been acquired
10699  * before this function was called.
10700  **/
10701 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10702 {
10703 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10704 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10705 	struct i40e_hw *hw = &pf->hw;
10706 	i40e_status ret;
10707 	u32 val;
10708 	int v;
10709 
10710 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10711 	    i40e_check_recovery_mode(pf)) {
10712 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10713 	}
10714 
10715 	if (test_bit(__I40E_DOWN, pf->state) &&
10716 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10717 	    !old_recovery_mode_bit)
10718 		goto clear_recovery;
10719 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10720 
10721 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10722 	ret = i40e_init_adminq(&pf->hw);
10723 	if (ret) {
10724 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10725 			 i40e_stat_str(&pf->hw, ret),
10726 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10727 		goto clear_recovery;
10728 	}
10729 	i40e_get_oem_version(&pf->hw);
10730 
10731 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10732 		/* The following delay is necessary for firmware update. */
10733 		mdelay(1000);
10734 	}
10735 
10736 	/* re-verify the eeprom if we just had an EMP reset */
10737 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10738 		i40e_verify_eeprom(pf);
10739 
10740 	/* if we are going out of or into recovery mode we have to act
10741 	 * accordingly with regard to resources initialization
10742 	 * and deinitialization
10743 	 */
10744 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10745 	    old_recovery_mode_bit) {
10746 		if (i40e_get_capabilities(pf,
10747 					  i40e_aqc_opc_list_func_capabilities))
10748 			goto end_unlock;
10749 
10750 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10751 			/* we're staying in recovery mode so we'll reinitialize
10752 			 * misc vector here
10753 			 */
10754 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10755 				goto end_unlock;
10756 		} else {
10757 			if (!lock_acquired)
10758 				rtnl_lock();
10759 			/* we're going out of recovery mode so we'll free
10760 			 * the IRQ allocated specifically for recovery mode
10761 			 * and restore the interrupt scheme
10762 			 */
10763 			free_irq(pf->pdev->irq, pf);
10764 			i40e_clear_interrupt_scheme(pf);
10765 			if (i40e_restore_interrupt_scheme(pf))
10766 				goto end_unlock;
10767 		}
10768 
10769 		/* tell the firmware that we're starting */
10770 		i40e_send_version(pf);
10771 
10772 		/* bail out in case recovery mode was detected, as there is
10773 		 * no need for further configuration.
10774 		 */
10775 		goto end_unlock;
10776 	}
10777 
10778 	i40e_clear_pxe_mode(hw);
10779 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10780 	if (ret)
10781 		goto end_core_reset;
10782 
10783 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10784 				hw->func_caps.num_rx_qp, 0, 0);
10785 	if (ret) {
10786 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10787 		goto end_core_reset;
10788 	}
10789 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10790 	if (ret) {
10791 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10792 		goto end_core_reset;
10793 	}
10794 
10795 #ifdef CONFIG_I40E_DCB
10796 	/* Enable FW to write a default DCB config on link-up
10797 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10798 	 * is not supported with new link speed
10799 	 */
10800 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10801 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10802 	} else {
10803 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10804 		    (hw->phy.link_info.link_speed &
10805 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10806 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10807 			dev_warn(&pf->pdev->dev,
10808 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10809 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10810 		} else {
10811 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10812 			ret = i40e_init_pf_dcb(pf);
10813 			if (ret) {
10814 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10815 					 ret);
10816 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10817 				/* Continue without DCB enabled */
10818 			}
10819 		}
10820 	}
10821 
10822 #endif /* CONFIG_I40E_DCB */
10823 	if (!lock_acquired)
10824 		rtnl_lock();
10825 	ret = i40e_setup_pf_switch(pf, reinit, true);
10826 	if (ret)
10827 		goto end_unlock;
10828 
10829 	/* The driver only wants link up/down and module qualification
10830 	 * reports from firmware.  Note the negative logic.
10831 	 */
10832 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10833 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10834 					 I40E_AQ_EVENT_MEDIA_NA |
10835 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10836 	if (ret)
10837 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10838 			 i40e_stat_str(&pf->hw, ret),
10839 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10840 
10841 	/* Rebuild the VSIs and VEBs that existed before reset.
10842 	 * They are still in our local switch element arrays, so only
10843 	 * need to rebuild the switch model in the HW.
10844 	 *
10845 	 * If there were VEBs but the reconstitution failed, we'll try
10846 	 * to recover minimal use by getting the basic PF VSI working.
10847 	 */
10848 	if (vsi->uplink_seid != pf->mac_seid) {
10849 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10850 		/* find the one VEB connected to the MAC, and find orphans */
10851 		for (v = 0; v < I40E_MAX_VEB; v++) {
10852 			if (!pf->veb[v])
10853 				continue;
10854 
10855 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10856 			    pf->veb[v]->uplink_seid == 0) {
10857 				ret = i40e_reconstitute_veb(pf->veb[v]);
10858 
10859 				if (!ret)
10860 					continue;
10861 
10862 				/* If Main VEB failed, we're in deep doodoo,
10863 				 * so give up rebuilding the switch and set up
10864 				 * for minimal rebuild of PF VSI.
10865 				 * If orphan failed, we'll report the error
10866 				 * but try to keep going.
10867 				 */
10868 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10869 					dev_info(&pf->pdev->dev,
10870 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10871 						 ret);
10872 					vsi->uplink_seid = pf->mac_seid;
10873 					break;
10874 				} else if (pf->veb[v]->uplink_seid == 0) {
10875 					dev_info(&pf->pdev->dev,
10876 						 "rebuild of orphan VEB failed: %d\n",
10877 						 ret);
10878 				}
10879 			}
10880 		}
10881 	}
10882 
10883 	if (vsi->uplink_seid == pf->mac_seid) {
10884 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10885 		/* no VEB, so rebuild only the Main VSI */
10886 		ret = i40e_add_vsi(vsi);
10887 		if (ret) {
10888 			dev_info(&pf->pdev->dev,
10889 				 "rebuild of Main VSI failed: %d\n", ret);
10890 			goto end_unlock;
10891 		}
10892 	}
10893 
10894 	if (vsi->mqprio_qopt.max_rate[0]) {
10895 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10896 		u64 credits = 0;
10897 
10898 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10899 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10900 		if (ret)
10901 			goto end_unlock;
10902 
10903 		credits = max_tx_rate;
10904 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10905 		dev_dbg(&vsi->back->pdev->dev,
10906 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10907 			max_tx_rate,
10908 			credits,
10909 			vsi->seid);
10910 	}
10911 
10912 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10913 	if (ret)
10914 		goto end_unlock;
10915 
10916 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10917 	 * for this main VSI if they exist
10918 	 */
10919 	ret = i40e_rebuild_channels(vsi);
10920 	if (ret)
10921 		goto end_unlock;
10922 
10923 	/* Reconfigure hardware for allowing smaller MSS in the case
10924 	 * of TSO, so that we avoid the MDD being fired and causing
10925 	 * a reset in the case of small MSS+TSO.
10926 	 */
10927 #define I40E_REG_MSS          0x000E64DC
10928 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10929 #define I40E_64BYTE_MSS       0x400000
10930 	val = rd32(hw, I40E_REG_MSS);
10931 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10932 		val &= ~I40E_REG_MSS_MIN_MASK;
10933 		val |= I40E_64BYTE_MSS;
10934 		wr32(hw, I40E_REG_MSS, val);
10935 	}
10936 
10937 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10938 		msleep(75);
10939 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10940 		if (ret)
10941 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10942 				 i40e_stat_str(&pf->hw, ret),
10943 				 i40e_aq_str(&pf->hw,
10944 					     pf->hw.aq.asq_last_status));
10945 	}
10946 	/* reinit the misc interrupt */
10947 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10948 		ret = i40e_setup_misc_vector(pf);
10949 
10950 	/* Add a filter to drop all Flow control frames from any VSI from being
10951 	 * transmitted. By doing so we stop a malicious VF from sending out
10952 	 * PAUSE or PFC frames and potentially controlling traffic for other
10953 	 * PF/VF VSIs.
10954 	 * The FW can still send Flow control frames if enabled.
10955 	 */
10956 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10957 						       pf->main_vsi_seid);
10958 
10959 	/* restart the VSIs that were rebuilt and running before the reset */
10960 	i40e_pf_unquiesce_all_vsi(pf);
10961 
10962 	/* Release the RTNL lock before we start resetting VFs */
10963 	if (!lock_acquired)
10964 		rtnl_unlock();
10965 
10966 	/* Restore promiscuous settings */
10967 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10968 	if (ret)
10969 		dev_warn(&pf->pdev->dev,
10970 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10971 			 pf->cur_promisc ? "on" : "off",
10972 			 i40e_stat_str(&pf->hw, ret),
10973 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10974 
10975 	i40e_reset_all_vfs(pf, true);
10976 
10977 	/* tell the firmware that we're starting */
10978 	i40e_send_version(pf);
10979 
10980 	/* We've already released the lock, so don't do it again */
10981 	goto end_core_reset;
10982 
10983 end_unlock:
10984 	if (!lock_acquired)
10985 		rtnl_unlock();
10986 end_core_reset:
10987 	clear_bit(__I40E_RESET_FAILED, pf->state);
10988 clear_recovery:
10989 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10990 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10991 }
10992 
10993 /**
10994  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10995  * @pf: board private structure
10996  * @reinit: if the Main VSI needs to re-initialized.
10997  * @lock_acquired: indicates whether or not the lock has been acquired
10998  * before this function was called.
10999  **/
11000 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11001 				   bool lock_acquired)
11002 {
11003 	int ret;
11004 
11005 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11006 		return;
11007 	/* Now we wait for GRST to settle out.
11008 	 * We don't have to delete the VEBs or VSIs from the hw switch
11009 	 * because the reset will make them disappear.
11010 	 */
11011 	ret = i40e_reset(pf);
11012 	if (!ret)
11013 		i40e_rebuild(pf, reinit, lock_acquired);
11014 }
11015 
11016 /**
11017  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11018  * @pf: board private structure
11019  *
11020  * Close up the VFs and other things in prep for a Core Reset,
11021  * then get ready to rebuild the world.
11022  * @lock_acquired: indicates whether or not the lock has been acquired
11023  * before this function was called.
11024  **/
11025 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11026 {
11027 	i40e_prep_for_reset(pf);
11028 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11029 }
11030 
11031 /**
11032  * i40e_handle_mdd_event
11033  * @pf: pointer to the PF structure
11034  *
11035  * Called from the MDD irq handler to identify possibly malicious vfs
11036  **/
11037 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11038 {
11039 	struct i40e_hw *hw = &pf->hw;
11040 	bool mdd_detected = false;
11041 	struct i40e_vf *vf;
11042 	u32 reg;
11043 	int i;
11044 
11045 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11046 		return;
11047 
11048 	/* find what triggered the MDD event */
11049 	reg = rd32(hw, I40E_GL_MDET_TX);
11050 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11051 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11052 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11053 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11054 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11055 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11056 				I40E_GL_MDET_TX_EVENT_SHIFT;
11057 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11058 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11059 				pf->hw.func_caps.base_queue;
11060 		if (netif_msg_tx_err(pf))
11061 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11062 				 event, queue, pf_num, vf_num);
11063 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11064 		mdd_detected = true;
11065 	}
11066 	reg = rd32(hw, I40E_GL_MDET_RX);
11067 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11068 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11069 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11070 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11071 				I40E_GL_MDET_RX_EVENT_SHIFT;
11072 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11073 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11074 				pf->hw.func_caps.base_queue;
11075 		if (netif_msg_rx_err(pf))
11076 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11077 				 event, queue, func);
11078 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11079 		mdd_detected = true;
11080 	}
11081 
11082 	if (mdd_detected) {
11083 		reg = rd32(hw, I40E_PF_MDET_TX);
11084 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11085 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11086 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11087 		}
11088 		reg = rd32(hw, I40E_PF_MDET_RX);
11089 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11090 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11091 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11092 		}
11093 	}
11094 
11095 	/* see if one of the VFs needs its hand slapped */
11096 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11097 		vf = &(pf->vf[i]);
11098 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11099 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11100 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11101 			vf->num_mdd_events++;
11102 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11103 				 i);
11104 			dev_info(&pf->pdev->dev,
11105 				 "Use PF Control I/F to re-enable the VF\n");
11106 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11107 		}
11108 
11109 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11110 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11111 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11112 			vf->num_mdd_events++;
11113 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11114 				 i);
11115 			dev_info(&pf->pdev->dev,
11116 				 "Use PF Control I/F to re-enable the VF\n");
11117 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11118 		}
11119 	}
11120 
11121 	/* re-enable mdd interrupt cause */
11122 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11123 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11124 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11125 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11126 	i40e_flush(hw);
11127 }
11128 
11129 /**
11130  * i40e_service_task - Run the driver's async subtasks
11131  * @work: pointer to work_struct containing our data
11132  **/
11133 static void i40e_service_task(struct work_struct *work)
11134 {
11135 	struct i40e_pf *pf = container_of(work,
11136 					  struct i40e_pf,
11137 					  service_task);
11138 	unsigned long start_time = jiffies;
11139 
11140 	/* don't bother with service tasks if a reset is in progress */
11141 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11142 	    test_bit(__I40E_SUSPENDED, pf->state))
11143 		return;
11144 
11145 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11146 		return;
11147 
11148 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11149 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11150 		i40e_sync_filters_subtask(pf);
11151 		i40e_reset_subtask(pf);
11152 		i40e_handle_mdd_event(pf);
11153 		i40e_vc_process_vflr_event(pf);
11154 		i40e_watchdog_subtask(pf);
11155 		i40e_fdir_reinit_subtask(pf);
11156 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11157 			/* Client subtask will reopen next time through. */
11158 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11159 							   true);
11160 		} else {
11161 			i40e_client_subtask(pf);
11162 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11163 					       pf->state))
11164 				i40e_notify_client_of_l2_param_changes(
11165 								pf->vsi[pf->lan_vsi]);
11166 		}
11167 		i40e_sync_filters_subtask(pf);
11168 	} else {
11169 		i40e_reset_subtask(pf);
11170 	}
11171 
11172 	i40e_clean_adminq_subtask(pf);
11173 
11174 	/* flush memory to make sure state is correct before next watchdog */
11175 	smp_mb__before_atomic();
11176 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11177 
11178 	/* If the tasks have taken longer than one timer cycle or there
11179 	 * is more work to be done, reschedule the service task now
11180 	 * rather than wait for the timer to tick again.
11181 	 */
11182 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11183 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11184 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11185 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11186 		i40e_service_event_schedule(pf);
11187 }
11188 
11189 /**
11190  * i40e_service_timer - timer callback
11191  * @t: timer list pointer
11192  **/
11193 static void i40e_service_timer(struct timer_list *t)
11194 {
11195 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11196 
11197 	mod_timer(&pf->service_timer,
11198 		  round_jiffies(jiffies + pf->service_timer_period));
11199 	i40e_service_event_schedule(pf);
11200 }
11201 
11202 /**
11203  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11204  * @vsi: the VSI being configured
11205  **/
11206 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11207 {
11208 	struct i40e_pf *pf = vsi->back;
11209 
11210 	switch (vsi->type) {
11211 	case I40E_VSI_MAIN:
11212 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11213 		if (!vsi->num_tx_desc)
11214 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11215 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11216 		if (!vsi->num_rx_desc)
11217 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11218 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11219 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11220 			vsi->num_q_vectors = pf->num_lan_msix;
11221 		else
11222 			vsi->num_q_vectors = 1;
11223 
11224 		break;
11225 
11226 	case I40E_VSI_FDIR:
11227 		vsi->alloc_queue_pairs = 1;
11228 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11229 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11230 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11231 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11232 		vsi->num_q_vectors = pf->num_fdsb_msix;
11233 		break;
11234 
11235 	case I40E_VSI_VMDQ2:
11236 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11237 		if (!vsi->num_tx_desc)
11238 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11239 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11240 		if (!vsi->num_rx_desc)
11241 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11242 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11243 		vsi->num_q_vectors = pf->num_vmdq_msix;
11244 		break;
11245 
11246 	case I40E_VSI_SRIOV:
11247 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11248 		if (!vsi->num_tx_desc)
11249 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11250 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11251 		if (!vsi->num_rx_desc)
11252 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11253 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11254 		break;
11255 
11256 	default:
11257 		WARN_ON(1);
11258 		return -ENODATA;
11259 	}
11260 
11261 	if (is_kdump_kernel()) {
11262 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11263 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11264 	}
11265 
11266 	return 0;
11267 }
11268 
11269 /**
11270  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11271  * @vsi: VSI pointer
11272  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11273  *
11274  * On error: returns error code (negative)
11275  * On success: returns 0
11276  **/
11277 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11278 {
11279 	struct i40e_ring **next_rings;
11280 	int size;
11281 	int ret = 0;
11282 
11283 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11284 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11285 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11286 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11287 	if (!vsi->tx_rings)
11288 		return -ENOMEM;
11289 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11290 	if (i40e_enabled_xdp_vsi(vsi)) {
11291 		vsi->xdp_rings = next_rings;
11292 		next_rings += vsi->alloc_queue_pairs;
11293 	}
11294 	vsi->rx_rings = next_rings;
11295 
11296 	if (alloc_qvectors) {
11297 		/* allocate memory for q_vector pointers */
11298 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11299 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11300 		if (!vsi->q_vectors) {
11301 			ret = -ENOMEM;
11302 			goto err_vectors;
11303 		}
11304 	}
11305 	return ret;
11306 
11307 err_vectors:
11308 	kfree(vsi->tx_rings);
11309 	return ret;
11310 }
11311 
11312 /**
11313  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11314  * @pf: board private structure
11315  * @type: type of VSI
11316  *
11317  * On error: returns error code (negative)
11318  * On success: returns vsi index in PF (positive)
11319  **/
11320 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11321 {
11322 	int ret = -ENODEV;
11323 	struct i40e_vsi *vsi;
11324 	int vsi_idx;
11325 	int i;
11326 
11327 	/* Need to protect the allocation of the VSIs at the PF level */
11328 	mutex_lock(&pf->switch_mutex);
11329 
11330 	/* VSI list may be fragmented if VSI creation/destruction has
11331 	 * been happening.  We can afford to do a quick scan to look
11332 	 * for any free VSIs in the list.
11333 	 *
11334 	 * find next empty vsi slot, looping back around if necessary
11335 	 */
11336 	i = pf->next_vsi;
11337 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11338 		i++;
11339 	if (i >= pf->num_alloc_vsi) {
11340 		i = 0;
11341 		while (i < pf->next_vsi && pf->vsi[i])
11342 			i++;
11343 	}
11344 
11345 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11346 		vsi_idx = i;             /* Found one! */
11347 	} else {
11348 		ret = -ENODEV;
11349 		goto unlock_pf;  /* out of VSI slots! */
11350 	}
11351 	pf->next_vsi = ++i;
11352 
11353 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11354 	if (!vsi) {
11355 		ret = -ENOMEM;
11356 		goto unlock_pf;
11357 	}
11358 	vsi->type = type;
11359 	vsi->back = pf;
11360 	set_bit(__I40E_VSI_DOWN, vsi->state);
11361 	vsi->flags = 0;
11362 	vsi->idx = vsi_idx;
11363 	vsi->int_rate_limit = 0;
11364 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11365 				pf->rss_table_size : 64;
11366 	vsi->netdev_registered = false;
11367 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11368 	hash_init(vsi->mac_filter_hash);
11369 	vsi->irqs_ready = false;
11370 
11371 	if (type == I40E_VSI_MAIN) {
11372 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11373 		if (!vsi->af_xdp_zc_qps)
11374 			goto err_rings;
11375 	}
11376 
11377 	ret = i40e_set_num_rings_in_vsi(vsi);
11378 	if (ret)
11379 		goto err_rings;
11380 
11381 	ret = i40e_vsi_alloc_arrays(vsi, true);
11382 	if (ret)
11383 		goto err_rings;
11384 
11385 	/* Setup default MSIX irq handler for VSI */
11386 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11387 
11388 	/* Initialize VSI lock */
11389 	spin_lock_init(&vsi->mac_filter_hash_lock);
11390 	pf->vsi[vsi_idx] = vsi;
11391 	ret = vsi_idx;
11392 	goto unlock_pf;
11393 
11394 err_rings:
11395 	bitmap_free(vsi->af_xdp_zc_qps);
11396 	pf->next_vsi = i - 1;
11397 	kfree(vsi);
11398 unlock_pf:
11399 	mutex_unlock(&pf->switch_mutex);
11400 	return ret;
11401 }
11402 
11403 /**
11404  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11405  * @vsi: VSI pointer
11406  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11407  *
11408  * On error: returns error code (negative)
11409  * On success: returns 0
11410  **/
11411 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11412 {
11413 	/* free the ring and vector containers */
11414 	if (free_qvectors) {
11415 		kfree(vsi->q_vectors);
11416 		vsi->q_vectors = NULL;
11417 	}
11418 	kfree(vsi->tx_rings);
11419 	vsi->tx_rings = NULL;
11420 	vsi->rx_rings = NULL;
11421 	vsi->xdp_rings = NULL;
11422 }
11423 
11424 /**
11425  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11426  * and lookup table
11427  * @vsi: Pointer to VSI structure
11428  */
11429 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11430 {
11431 	if (!vsi)
11432 		return;
11433 
11434 	kfree(vsi->rss_hkey_user);
11435 	vsi->rss_hkey_user = NULL;
11436 
11437 	kfree(vsi->rss_lut_user);
11438 	vsi->rss_lut_user = NULL;
11439 }
11440 
11441 /**
11442  * i40e_vsi_clear - Deallocate the VSI provided
11443  * @vsi: the VSI being un-configured
11444  **/
11445 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11446 {
11447 	struct i40e_pf *pf;
11448 
11449 	if (!vsi)
11450 		return 0;
11451 
11452 	if (!vsi->back)
11453 		goto free_vsi;
11454 	pf = vsi->back;
11455 
11456 	mutex_lock(&pf->switch_mutex);
11457 	if (!pf->vsi[vsi->idx]) {
11458 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11459 			vsi->idx, vsi->idx, vsi->type);
11460 		goto unlock_vsi;
11461 	}
11462 
11463 	if (pf->vsi[vsi->idx] != vsi) {
11464 		dev_err(&pf->pdev->dev,
11465 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11466 			pf->vsi[vsi->idx]->idx,
11467 			pf->vsi[vsi->idx]->type,
11468 			vsi->idx, vsi->type);
11469 		goto unlock_vsi;
11470 	}
11471 
11472 	/* updates the PF for this cleared vsi */
11473 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11474 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11475 
11476 	bitmap_free(vsi->af_xdp_zc_qps);
11477 	i40e_vsi_free_arrays(vsi, true);
11478 	i40e_clear_rss_config_user(vsi);
11479 
11480 	pf->vsi[vsi->idx] = NULL;
11481 	if (vsi->idx < pf->next_vsi)
11482 		pf->next_vsi = vsi->idx;
11483 
11484 unlock_vsi:
11485 	mutex_unlock(&pf->switch_mutex);
11486 free_vsi:
11487 	kfree(vsi);
11488 
11489 	return 0;
11490 }
11491 
11492 /**
11493  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11494  * @vsi: the VSI being cleaned
11495  **/
11496 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11497 {
11498 	int i;
11499 
11500 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11501 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11502 			kfree_rcu(vsi->tx_rings[i], rcu);
11503 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11504 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11505 			if (vsi->xdp_rings)
11506 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11507 		}
11508 	}
11509 }
11510 
11511 /**
11512  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11513  * @vsi: the VSI being configured
11514  **/
11515 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11516 {
11517 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11518 	struct i40e_pf *pf = vsi->back;
11519 	struct i40e_ring *ring;
11520 
11521 	/* Set basic values in the rings to be used later during open() */
11522 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11523 		/* allocate space for both Tx and Rx in one shot */
11524 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11525 		if (!ring)
11526 			goto err_out;
11527 
11528 		ring->queue_index = i;
11529 		ring->reg_idx = vsi->base_queue + i;
11530 		ring->ring_active = false;
11531 		ring->vsi = vsi;
11532 		ring->netdev = vsi->netdev;
11533 		ring->dev = &pf->pdev->dev;
11534 		ring->count = vsi->num_tx_desc;
11535 		ring->size = 0;
11536 		ring->dcb_tc = 0;
11537 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11538 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11539 		ring->itr_setting = pf->tx_itr_default;
11540 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11541 
11542 		if (!i40e_enabled_xdp_vsi(vsi))
11543 			goto setup_rx;
11544 
11545 		ring->queue_index = vsi->alloc_queue_pairs + i;
11546 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11547 		ring->ring_active = false;
11548 		ring->vsi = vsi;
11549 		ring->netdev = NULL;
11550 		ring->dev = &pf->pdev->dev;
11551 		ring->count = vsi->num_tx_desc;
11552 		ring->size = 0;
11553 		ring->dcb_tc = 0;
11554 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11555 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11556 		set_ring_xdp(ring);
11557 		ring->itr_setting = pf->tx_itr_default;
11558 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11559 
11560 setup_rx:
11561 		ring->queue_index = i;
11562 		ring->reg_idx = vsi->base_queue + i;
11563 		ring->ring_active = false;
11564 		ring->vsi = vsi;
11565 		ring->netdev = vsi->netdev;
11566 		ring->dev = &pf->pdev->dev;
11567 		ring->count = vsi->num_rx_desc;
11568 		ring->size = 0;
11569 		ring->dcb_tc = 0;
11570 		ring->itr_setting = pf->rx_itr_default;
11571 		WRITE_ONCE(vsi->rx_rings[i], ring);
11572 	}
11573 
11574 	return 0;
11575 
11576 err_out:
11577 	i40e_vsi_clear_rings(vsi);
11578 	return -ENOMEM;
11579 }
11580 
11581 /**
11582  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11583  * @pf: board private structure
11584  * @vectors: the number of MSI-X vectors to request
11585  *
11586  * Returns the number of vectors reserved, or error
11587  **/
11588 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11589 {
11590 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11591 					I40E_MIN_MSIX, vectors);
11592 	if (vectors < 0) {
11593 		dev_info(&pf->pdev->dev,
11594 			 "MSI-X vector reservation failed: %d\n", vectors);
11595 		vectors = 0;
11596 	}
11597 
11598 	return vectors;
11599 }
11600 
11601 /**
11602  * i40e_init_msix - Setup the MSIX capability
11603  * @pf: board private structure
11604  *
11605  * Work with the OS to set up the MSIX vectors needed.
11606  *
11607  * Returns the number of vectors reserved or negative on failure
11608  **/
11609 static int i40e_init_msix(struct i40e_pf *pf)
11610 {
11611 	struct i40e_hw *hw = &pf->hw;
11612 	int cpus, extra_vectors;
11613 	int vectors_left;
11614 	int v_budget, i;
11615 	int v_actual;
11616 	int iwarp_requested = 0;
11617 
11618 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11619 		return -ENODEV;
11620 
11621 	/* The number of vectors we'll request will be comprised of:
11622 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11623 	 *   - The number of LAN queue pairs
11624 	 *	- Queues being used for RSS.
11625 	 *		We don't need as many as max_rss_size vectors.
11626 	 *		use rss_size instead in the calculation since that
11627 	 *		is governed by number of cpus in the system.
11628 	 *	- assumes symmetric Tx/Rx pairing
11629 	 *   - The number of VMDq pairs
11630 	 *   - The CPU count within the NUMA node if iWARP is enabled
11631 	 * Once we count this up, try the request.
11632 	 *
11633 	 * If we can't get what we want, we'll simplify to nearly nothing
11634 	 * and try again.  If that still fails, we punt.
11635 	 */
11636 	vectors_left = hw->func_caps.num_msix_vectors;
11637 	v_budget = 0;
11638 
11639 	/* reserve one vector for miscellaneous handler */
11640 	if (vectors_left) {
11641 		v_budget++;
11642 		vectors_left--;
11643 	}
11644 
11645 	/* reserve some vectors for the main PF traffic queues. Initially we
11646 	 * only reserve at most 50% of the available vectors, in the case that
11647 	 * the number of online CPUs is large. This ensures that we can enable
11648 	 * extra features as well. Once we've enabled the other features, we
11649 	 * will use any remaining vectors to reach as close as we can to the
11650 	 * number of online CPUs.
11651 	 */
11652 	cpus = num_online_cpus();
11653 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11654 	vectors_left -= pf->num_lan_msix;
11655 
11656 	/* reserve one vector for sideband flow director */
11657 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11658 		if (vectors_left) {
11659 			pf->num_fdsb_msix = 1;
11660 			v_budget++;
11661 			vectors_left--;
11662 		} else {
11663 			pf->num_fdsb_msix = 0;
11664 		}
11665 	}
11666 
11667 	/* can we reserve enough for iWARP? */
11668 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11669 		iwarp_requested = pf->num_iwarp_msix;
11670 
11671 		if (!vectors_left)
11672 			pf->num_iwarp_msix = 0;
11673 		else if (vectors_left < pf->num_iwarp_msix)
11674 			pf->num_iwarp_msix = 1;
11675 		v_budget += pf->num_iwarp_msix;
11676 		vectors_left -= pf->num_iwarp_msix;
11677 	}
11678 
11679 	/* any vectors left over go for VMDq support */
11680 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11681 		if (!vectors_left) {
11682 			pf->num_vmdq_msix = 0;
11683 			pf->num_vmdq_qps = 0;
11684 		} else {
11685 			int vmdq_vecs_wanted =
11686 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11687 			int vmdq_vecs =
11688 				min_t(int, vectors_left, vmdq_vecs_wanted);
11689 
11690 			/* if we're short on vectors for what's desired, we limit
11691 			 * the queues per vmdq.  If this is still more than are
11692 			 * available, the user will need to change the number of
11693 			 * queues/vectors used by the PF later with the ethtool
11694 			 * channels command
11695 			 */
11696 			if (vectors_left < vmdq_vecs_wanted) {
11697 				pf->num_vmdq_qps = 1;
11698 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11699 				vmdq_vecs = min_t(int,
11700 						  vectors_left,
11701 						  vmdq_vecs_wanted);
11702 			}
11703 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11704 
11705 			v_budget += vmdq_vecs;
11706 			vectors_left -= vmdq_vecs;
11707 		}
11708 	}
11709 
11710 	/* On systems with a large number of SMP cores, we previously limited
11711 	 * the number of vectors for num_lan_msix to be at most 50% of the
11712 	 * available vectors, to allow for other features. Now, we add back
11713 	 * the remaining vectors. However, we ensure that the total
11714 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11715 	 * calculate the number of vectors we can add without going over the
11716 	 * cap of CPUs. For systems with a small number of CPUs this will be
11717 	 * zero.
11718 	 */
11719 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11720 	pf->num_lan_msix += extra_vectors;
11721 	vectors_left -= extra_vectors;
11722 
11723 	WARN(vectors_left < 0,
11724 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11725 
11726 	v_budget += pf->num_lan_msix;
11727 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11728 				   GFP_KERNEL);
11729 	if (!pf->msix_entries)
11730 		return -ENOMEM;
11731 
11732 	for (i = 0; i < v_budget; i++)
11733 		pf->msix_entries[i].entry = i;
11734 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11735 
11736 	if (v_actual < I40E_MIN_MSIX) {
11737 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11738 		kfree(pf->msix_entries);
11739 		pf->msix_entries = NULL;
11740 		pci_disable_msix(pf->pdev);
11741 		return -ENODEV;
11742 
11743 	} else if (v_actual == I40E_MIN_MSIX) {
11744 		/* Adjust for minimal MSIX use */
11745 		pf->num_vmdq_vsis = 0;
11746 		pf->num_vmdq_qps = 0;
11747 		pf->num_lan_qps = 1;
11748 		pf->num_lan_msix = 1;
11749 
11750 	} else if (v_actual != v_budget) {
11751 		/* If we have limited resources, we will start with no vectors
11752 		 * for the special features and then allocate vectors to some
11753 		 * of these features based on the policy and at the end disable
11754 		 * the features that did not get any vectors.
11755 		 */
11756 		int vec;
11757 
11758 		dev_info(&pf->pdev->dev,
11759 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11760 			 v_actual, v_budget);
11761 		/* reserve the misc vector */
11762 		vec = v_actual - 1;
11763 
11764 		/* Scale vector usage down */
11765 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11766 		pf->num_vmdq_vsis = 1;
11767 		pf->num_vmdq_qps = 1;
11768 
11769 		/* partition out the remaining vectors */
11770 		switch (vec) {
11771 		case 2:
11772 			pf->num_lan_msix = 1;
11773 			break;
11774 		case 3:
11775 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11776 				pf->num_lan_msix = 1;
11777 				pf->num_iwarp_msix = 1;
11778 			} else {
11779 				pf->num_lan_msix = 2;
11780 			}
11781 			break;
11782 		default:
11783 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11784 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11785 						 iwarp_requested);
11786 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11787 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11788 			} else {
11789 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11790 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11791 			}
11792 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11793 				pf->num_fdsb_msix = 1;
11794 				vec--;
11795 			}
11796 			pf->num_lan_msix = min_t(int,
11797 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11798 							      pf->num_lan_msix);
11799 			pf->num_lan_qps = pf->num_lan_msix;
11800 			break;
11801 		}
11802 	}
11803 
11804 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11805 	    (pf->num_fdsb_msix == 0)) {
11806 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11807 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11808 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11809 	}
11810 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11811 	    (pf->num_vmdq_msix == 0)) {
11812 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11813 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11814 	}
11815 
11816 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11817 	    (pf->num_iwarp_msix == 0)) {
11818 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11819 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11820 	}
11821 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11822 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11823 		   pf->num_lan_msix,
11824 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11825 		   pf->num_fdsb_msix,
11826 		   pf->num_iwarp_msix);
11827 
11828 	return v_actual;
11829 }
11830 
11831 /**
11832  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11833  * @vsi: the VSI being configured
11834  * @v_idx: index of the vector in the vsi struct
11835  *
11836  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11837  **/
11838 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11839 {
11840 	struct i40e_q_vector *q_vector;
11841 
11842 	/* allocate q_vector */
11843 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11844 	if (!q_vector)
11845 		return -ENOMEM;
11846 
11847 	q_vector->vsi = vsi;
11848 	q_vector->v_idx = v_idx;
11849 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11850 
11851 	if (vsi->netdev)
11852 		netif_napi_add(vsi->netdev, &q_vector->napi,
11853 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11854 
11855 	/* tie q_vector and vsi together */
11856 	vsi->q_vectors[v_idx] = q_vector;
11857 
11858 	return 0;
11859 }
11860 
11861 /**
11862  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11863  * @vsi: the VSI being configured
11864  *
11865  * We allocate one q_vector per queue interrupt.  If allocation fails we
11866  * return -ENOMEM.
11867  **/
11868 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11869 {
11870 	struct i40e_pf *pf = vsi->back;
11871 	int err, v_idx, num_q_vectors;
11872 
11873 	/* if not MSIX, give the one vector only to the LAN VSI */
11874 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11875 		num_q_vectors = vsi->num_q_vectors;
11876 	else if (vsi == pf->vsi[pf->lan_vsi])
11877 		num_q_vectors = 1;
11878 	else
11879 		return -EINVAL;
11880 
11881 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11882 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11883 		if (err)
11884 			goto err_out;
11885 	}
11886 
11887 	return 0;
11888 
11889 err_out:
11890 	while (v_idx--)
11891 		i40e_free_q_vector(vsi, v_idx);
11892 
11893 	return err;
11894 }
11895 
11896 /**
11897  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11898  * @pf: board private structure to initialize
11899  **/
11900 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11901 {
11902 	int vectors = 0;
11903 	ssize_t size;
11904 
11905 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11906 		vectors = i40e_init_msix(pf);
11907 		if (vectors < 0) {
11908 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11909 				       I40E_FLAG_IWARP_ENABLED	|
11910 				       I40E_FLAG_RSS_ENABLED	|
11911 				       I40E_FLAG_DCB_CAPABLE	|
11912 				       I40E_FLAG_DCB_ENABLED	|
11913 				       I40E_FLAG_SRIOV_ENABLED	|
11914 				       I40E_FLAG_FD_SB_ENABLED	|
11915 				       I40E_FLAG_FD_ATR_ENABLED	|
11916 				       I40E_FLAG_VMDQ_ENABLED);
11917 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11918 
11919 			/* rework the queue expectations without MSIX */
11920 			i40e_determine_queue_usage(pf);
11921 		}
11922 	}
11923 
11924 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11925 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11926 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11927 		vectors = pci_enable_msi(pf->pdev);
11928 		if (vectors < 0) {
11929 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11930 				 vectors);
11931 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11932 		}
11933 		vectors = 1;  /* one MSI or Legacy vector */
11934 	}
11935 
11936 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11937 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11938 
11939 	/* set up vector assignment tracking */
11940 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11941 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11942 	if (!pf->irq_pile)
11943 		return -ENOMEM;
11944 
11945 	pf->irq_pile->num_entries = vectors;
11946 
11947 	/* track first vector for misc interrupts, ignore return */
11948 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11949 
11950 	return 0;
11951 }
11952 
11953 /**
11954  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11955  * @pf: private board data structure
11956  *
11957  * Restore the interrupt scheme that was cleared when we suspended the
11958  * device. This should be called during resume to re-allocate the q_vectors
11959  * and reacquire IRQs.
11960  */
11961 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11962 {
11963 	int err, i;
11964 
11965 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11966 	 * scheme. We need to re-enabled them here in order to attempt to
11967 	 * re-acquire the MSI or MSI-X vectors
11968 	 */
11969 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11970 
11971 	err = i40e_init_interrupt_scheme(pf);
11972 	if (err)
11973 		return err;
11974 
11975 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11976 	 * rings together again.
11977 	 */
11978 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11979 		if (pf->vsi[i]) {
11980 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11981 			if (err)
11982 				goto err_unwind;
11983 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11984 		}
11985 	}
11986 
11987 	err = i40e_setup_misc_vector(pf);
11988 	if (err)
11989 		goto err_unwind;
11990 
11991 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11992 		i40e_client_update_msix_info(pf);
11993 
11994 	return 0;
11995 
11996 err_unwind:
11997 	while (i--) {
11998 		if (pf->vsi[i])
11999 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12000 	}
12001 
12002 	return err;
12003 }
12004 
12005 /**
12006  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12007  * non queue events in recovery mode
12008  * @pf: board private structure
12009  *
12010  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12011  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12012  * This is handled differently than in recovery mode since no Tx/Rx resources
12013  * are being allocated.
12014  **/
12015 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12016 {
12017 	int err;
12018 
12019 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12020 		err = i40e_setup_misc_vector(pf);
12021 
12022 		if (err) {
12023 			dev_info(&pf->pdev->dev,
12024 				 "MSI-X misc vector request failed, error %d\n",
12025 				 err);
12026 			return err;
12027 		}
12028 	} else {
12029 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12030 
12031 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12032 				  pf->int_name, pf);
12033 
12034 		if (err) {
12035 			dev_info(&pf->pdev->dev,
12036 				 "MSI/legacy misc vector request failed, error %d\n",
12037 				 err);
12038 			return err;
12039 		}
12040 		i40e_enable_misc_int_causes(pf);
12041 		i40e_irq_dynamic_enable_icr0(pf);
12042 	}
12043 
12044 	return 0;
12045 }
12046 
12047 /**
12048  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12049  * @pf: board private structure
12050  *
12051  * This sets up the handler for MSIX 0, which is used to manage the
12052  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12053  * when in MSI or Legacy interrupt mode.
12054  **/
12055 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12056 {
12057 	struct i40e_hw *hw = &pf->hw;
12058 	int err = 0;
12059 
12060 	/* Only request the IRQ once, the first time through. */
12061 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12062 		err = request_irq(pf->msix_entries[0].vector,
12063 				  i40e_intr, 0, pf->int_name, pf);
12064 		if (err) {
12065 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12066 			dev_info(&pf->pdev->dev,
12067 				 "request_irq for %s failed: %d\n",
12068 				 pf->int_name, err);
12069 			return -EFAULT;
12070 		}
12071 	}
12072 
12073 	i40e_enable_misc_int_causes(pf);
12074 
12075 	/* associate no queues to the misc vector */
12076 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12077 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12078 
12079 	i40e_flush(hw);
12080 
12081 	i40e_irq_dynamic_enable_icr0(pf);
12082 
12083 	return err;
12084 }
12085 
12086 /**
12087  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12088  * @vsi: Pointer to vsi structure
12089  * @seed: Buffter to store the hash keys
12090  * @lut: Buffer to store the lookup table entries
12091  * @lut_size: Size of buffer to store the lookup table entries
12092  *
12093  * Return 0 on success, negative on failure
12094  */
12095 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12096 			   u8 *lut, u16 lut_size)
12097 {
12098 	struct i40e_pf *pf = vsi->back;
12099 	struct i40e_hw *hw = &pf->hw;
12100 	int ret = 0;
12101 
12102 	if (seed) {
12103 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12104 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12105 		if (ret) {
12106 			dev_info(&pf->pdev->dev,
12107 				 "Cannot get RSS key, err %s aq_err %s\n",
12108 				 i40e_stat_str(&pf->hw, ret),
12109 				 i40e_aq_str(&pf->hw,
12110 					     pf->hw.aq.asq_last_status));
12111 			return ret;
12112 		}
12113 	}
12114 
12115 	if (lut) {
12116 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12117 
12118 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12119 		if (ret) {
12120 			dev_info(&pf->pdev->dev,
12121 				 "Cannot get RSS lut, err %s aq_err %s\n",
12122 				 i40e_stat_str(&pf->hw, ret),
12123 				 i40e_aq_str(&pf->hw,
12124 					     pf->hw.aq.asq_last_status));
12125 			return ret;
12126 		}
12127 	}
12128 
12129 	return ret;
12130 }
12131 
12132 /**
12133  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12134  * @vsi: Pointer to vsi structure
12135  * @seed: RSS hash seed
12136  * @lut: Lookup table
12137  * @lut_size: Lookup table size
12138  *
12139  * Returns 0 on success, negative on failure
12140  **/
12141 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12142 			       const u8 *lut, u16 lut_size)
12143 {
12144 	struct i40e_pf *pf = vsi->back;
12145 	struct i40e_hw *hw = &pf->hw;
12146 	u16 vf_id = vsi->vf_id;
12147 	u8 i;
12148 
12149 	/* Fill out hash function seed */
12150 	if (seed) {
12151 		u32 *seed_dw = (u32 *)seed;
12152 
12153 		if (vsi->type == I40E_VSI_MAIN) {
12154 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12155 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12156 		} else if (vsi->type == I40E_VSI_SRIOV) {
12157 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12158 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12159 		} else {
12160 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12161 		}
12162 	}
12163 
12164 	if (lut) {
12165 		u32 *lut_dw = (u32 *)lut;
12166 
12167 		if (vsi->type == I40E_VSI_MAIN) {
12168 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12169 				return -EINVAL;
12170 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12171 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12172 		} else if (vsi->type == I40E_VSI_SRIOV) {
12173 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12174 				return -EINVAL;
12175 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12176 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12177 		} else {
12178 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12179 		}
12180 	}
12181 	i40e_flush(hw);
12182 
12183 	return 0;
12184 }
12185 
12186 /**
12187  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12188  * @vsi: Pointer to VSI structure
12189  * @seed: Buffer to store the keys
12190  * @lut: Buffer to store the lookup table entries
12191  * @lut_size: Size of buffer to store the lookup table entries
12192  *
12193  * Returns 0 on success, negative on failure
12194  */
12195 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12196 			    u8 *lut, u16 lut_size)
12197 {
12198 	struct i40e_pf *pf = vsi->back;
12199 	struct i40e_hw *hw = &pf->hw;
12200 	u16 i;
12201 
12202 	if (seed) {
12203 		u32 *seed_dw = (u32 *)seed;
12204 
12205 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12206 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12207 	}
12208 	if (lut) {
12209 		u32 *lut_dw = (u32 *)lut;
12210 
12211 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12212 			return -EINVAL;
12213 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12214 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12215 	}
12216 
12217 	return 0;
12218 }
12219 
12220 /**
12221  * i40e_config_rss - Configure RSS keys and lut
12222  * @vsi: Pointer to VSI structure
12223  * @seed: RSS hash seed
12224  * @lut: Lookup table
12225  * @lut_size: Lookup table size
12226  *
12227  * Returns 0 on success, negative on failure
12228  */
12229 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12230 {
12231 	struct i40e_pf *pf = vsi->back;
12232 
12233 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12234 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12235 	else
12236 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12237 }
12238 
12239 /**
12240  * i40e_get_rss - Get RSS keys and lut
12241  * @vsi: Pointer to VSI structure
12242  * @seed: Buffer to store the keys
12243  * @lut: Buffer to store the lookup table entries
12244  * @lut_size: Size of buffer to store the lookup table entries
12245  *
12246  * Returns 0 on success, negative on failure
12247  */
12248 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12249 {
12250 	struct i40e_pf *pf = vsi->back;
12251 
12252 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12253 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12254 	else
12255 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12256 }
12257 
12258 /**
12259  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12260  * @pf: Pointer to board private structure
12261  * @lut: Lookup table
12262  * @rss_table_size: Lookup table size
12263  * @rss_size: Range of queue number for hashing
12264  */
12265 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12266 		       u16 rss_table_size, u16 rss_size)
12267 {
12268 	u16 i;
12269 
12270 	for (i = 0; i < rss_table_size; i++)
12271 		lut[i] = i % rss_size;
12272 }
12273 
12274 /**
12275  * i40e_pf_config_rss - Prepare for RSS if used
12276  * @pf: board private structure
12277  **/
12278 static int i40e_pf_config_rss(struct i40e_pf *pf)
12279 {
12280 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12281 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12282 	u8 *lut;
12283 	struct i40e_hw *hw = &pf->hw;
12284 	u32 reg_val;
12285 	u64 hena;
12286 	int ret;
12287 
12288 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12289 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12290 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12291 	hena |= i40e_pf_get_default_rss_hena(pf);
12292 
12293 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12294 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12295 
12296 	/* Determine the RSS table size based on the hardware capabilities */
12297 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12298 	reg_val = (pf->rss_table_size == 512) ?
12299 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12300 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12301 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12302 
12303 	/* Determine the RSS size of the VSI */
12304 	if (!vsi->rss_size) {
12305 		u16 qcount;
12306 		/* If the firmware does something weird during VSI init, we
12307 		 * could end up with zero TCs. Check for that to avoid
12308 		 * divide-by-zero. It probably won't pass traffic, but it also
12309 		 * won't panic.
12310 		 */
12311 		qcount = vsi->num_queue_pairs /
12312 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12313 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12314 	}
12315 	if (!vsi->rss_size)
12316 		return -EINVAL;
12317 
12318 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12319 	if (!lut)
12320 		return -ENOMEM;
12321 
12322 	/* Use user configured lut if there is one, otherwise use default */
12323 	if (vsi->rss_lut_user)
12324 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12325 	else
12326 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12327 
12328 	/* Use user configured hash key if there is one, otherwise
12329 	 * use default.
12330 	 */
12331 	if (vsi->rss_hkey_user)
12332 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12333 	else
12334 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12335 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12336 	kfree(lut);
12337 
12338 	return ret;
12339 }
12340 
12341 /**
12342  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12343  * @pf: board private structure
12344  * @queue_count: the requested queue count for rss.
12345  *
12346  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12347  * count which may be different from the requested queue count.
12348  * Note: expects to be called while under rtnl_lock()
12349  **/
12350 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12351 {
12352 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12353 	int new_rss_size;
12354 
12355 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12356 		return 0;
12357 
12358 	queue_count = min_t(int, queue_count, num_online_cpus());
12359 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12360 
12361 	if (queue_count != vsi->num_queue_pairs) {
12362 		u16 qcount;
12363 
12364 		vsi->req_queue_pairs = queue_count;
12365 		i40e_prep_for_reset(pf);
12366 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12367 			return pf->alloc_rss_size;
12368 
12369 		pf->alloc_rss_size = new_rss_size;
12370 
12371 		i40e_reset_and_rebuild(pf, true, true);
12372 
12373 		/* Discard the user configured hash keys and lut, if less
12374 		 * queues are enabled.
12375 		 */
12376 		if (queue_count < vsi->rss_size) {
12377 			i40e_clear_rss_config_user(vsi);
12378 			dev_dbg(&pf->pdev->dev,
12379 				"discard user configured hash keys and lut\n");
12380 		}
12381 
12382 		/* Reset vsi->rss_size, as number of enabled queues changed */
12383 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12384 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12385 
12386 		i40e_pf_config_rss(pf);
12387 	}
12388 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12389 		 vsi->req_queue_pairs, pf->rss_size_max);
12390 	return pf->alloc_rss_size;
12391 }
12392 
12393 /**
12394  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12395  * @pf: board private structure
12396  **/
12397 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12398 {
12399 	i40e_status status;
12400 	bool min_valid, max_valid;
12401 	u32 max_bw, min_bw;
12402 
12403 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12404 					   &min_valid, &max_valid);
12405 
12406 	if (!status) {
12407 		if (min_valid)
12408 			pf->min_bw = min_bw;
12409 		if (max_valid)
12410 			pf->max_bw = max_bw;
12411 	}
12412 
12413 	return status;
12414 }
12415 
12416 /**
12417  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12418  * @pf: board private structure
12419  **/
12420 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12421 {
12422 	struct i40e_aqc_configure_partition_bw_data bw_data;
12423 	i40e_status status;
12424 
12425 	memset(&bw_data, 0, sizeof(bw_data));
12426 
12427 	/* Set the valid bit for this PF */
12428 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12429 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12430 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12431 
12432 	/* Set the new bandwidths */
12433 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12434 
12435 	return status;
12436 }
12437 
12438 /**
12439  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12440  * @pf: board private structure
12441  **/
12442 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12443 {
12444 	/* Commit temporary BW setting to permanent NVM image */
12445 	enum i40e_admin_queue_err last_aq_status;
12446 	i40e_status ret;
12447 	u16 nvm_word;
12448 
12449 	if (pf->hw.partition_id != 1) {
12450 		dev_info(&pf->pdev->dev,
12451 			 "Commit BW only works on partition 1! This is partition %d",
12452 			 pf->hw.partition_id);
12453 		ret = I40E_NOT_SUPPORTED;
12454 		goto bw_commit_out;
12455 	}
12456 
12457 	/* Acquire NVM for read access */
12458 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12459 	last_aq_status = pf->hw.aq.asq_last_status;
12460 	if (ret) {
12461 		dev_info(&pf->pdev->dev,
12462 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12463 			 i40e_stat_str(&pf->hw, ret),
12464 			 i40e_aq_str(&pf->hw, last_aq_status));
12465 		goto bw_commit_out;
12466 	}
12467 
12468 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12469 	ret = i40e_aq_read_nvm(&pf->hw,
12470 			       I40E_SR_NVM_CONTROL_WORD,
12471 			       0x10, sizeof(nvm_word), &nvm_word,
12472 			       false, NULL);
12473 	/* Save off last admin queue command status before releasing
12474 	 * the NVM
12475 	 */
12476 	last_aq_status = pf->hw.aq.asq_last_status;
12477 	i40e_release_nvm(&pf->hw);
12478 	if (ret) {
12479 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12480 			 i40e_stat_str(&pf->hw, ret),
12481 			 i40e_aq_str(&pf->hw, last_aq_status));
12482 		goto bw_commit_out;
12483 	}
12484 
12485 	/* Wait a bit for NVM release to complete */
12486 	msleep(50);
12487 
12488 	/* Acquire NVM for write access */
12489 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12490 	last_aq_status = pf->hw.aq.asq_last_status;
12491 	if (ret) {
12492 		dev_info(&pf->pdev->dev,
12493 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12494 			 i40e_stat_str(&pf->hw, ret),
12495 			 i40e_aq_str(&pf->hw, last_aq_status));
12496 		goto bw_commit_out;
12497 	}
12498 	/* Write it back out unchanged to initiate update NVM,
12499 	 * which will force a write of the shadow (alt) RAM to
12500 	 * the NVM - thus storing the bandwidth values permanently.
12501 	 */
12502 	ret = i40e_aq_update_nvm(&pf->hw,
12503 				 I40E_SR_NVM_CONTROL_WORD,
12504 				 0x10, sizeof(nvm_word),
12505 				 &nvm_word, true, 0, NULL);
12506 	/* Save off last admin queue command status before releasing
12507 	 * the NVM
12508 	 */
12509 	last_aq_status = pf->hw.aq.asq_last_status;
12510 	i40e_release_nvm(&pf->hw);
12511 	if (ret)
12512 		dev_info(&pf->pdev->dev,
12513 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12514 			 i40e_stat_str(&pf->hw, ret),
12515 			 i40e_aq_str(&pf->hw, last_aq_status));
12516 bw_commit_out:
12517 
12518 	return ret;
12519 }
12520 
12521 /**
12522  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12523  * if total port shutdown feature is enabled for this PF
12524  * @pf: board private structure
12525  **/
12526 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12527 {
12528 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12529 #define I40E_FEATURES_ENABLE_PTR		0x2A
12530 #define I40E_CURRENT_SETTING_PTR		0x2B
12531 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12532 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12533 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12534 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12535 	i40e_status read_status = I40E_SUCCESS;
12536 	u16 sr_emp_sr_settings_ptr = 0;
12537 	u16 features_enable = 0;
12538 	u16 link_behavior = 0;
12539 	bool ret = false;
12540 
12541 	read_status = i40e_read_nvm_word(&pf->hw,
12542 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12543 					 &sr_emp_sr_settings_ptr);
12544 	if (read_status)
12545 		goto err_nvm;
12546 	read_status = i40e_read_nvm_word(&pf->hw,
12547 					 sr_emp_sr_settings_ptr +
12548 					 I40E_FEATURES_ENABLE_PTR,
12549 					 &features_enable);
12550 	if (read_status)
12551 		goto err_nvm;
12552 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12553 		read_status = i40e_read_nvm_module_data(&pf->hw,
12554 							I40E_SR_EMP_SR_SETTINGS_PTR,
12555 							I40E_CURRENT_SETTING_PTR,
12556 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12557 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12558 							&link_behavior);
12559 		if (read_status)
12560 			goto err_nvm;
12561 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12562 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12563 	}
12564 	return ret;
12565 
12566 err_nvm:
12567 	dev_warn(&pf->pdev->dev,
12568 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12569 		 i40e_stat_str(&pf->hw, read_status));
12570 	return ret;
12571 }
12572 
12573 /**
12574  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12575  * @pf: board private structure to initialize
12576  *
12577  * i40e_sw_init initializes the Adapter private data structure.
12578  * Fields are initialized based on PCI device information and
12579  * OS network device settings (MTU size).
12580  **/
12581 static int i40e_sw_init(struct i40e_pf *pf)
12582 {
12583 	int err = 0;
12584 	int size;
12585 	u16 pow;
12586 
12587 	/* Set default capability flags */
12588 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12589 		    I40E_FLAG_MSI_ENABLED     |
12590 		    I40E_FLAG_MSIX_ENABLED;
12591 
12592 	/* Set default ITR */
12593 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12594 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12595 
12596 	/* Depending on PF configurations, it is possible that the RSS
12597 	 * maximum might end up larger than the available queues
12598 	 */
12599 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12600 	pf->alloc_rss_size = 1;
12601 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12602 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12603 				 pf->hw.func_caps.num_tx_qp);
12604 
12605 	/* find the next higher power-of-2 of num cpus */
12606 	pow = roundup_pow_of_two(num_online_cpus());
12607 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12608 
12609 	if (pf->hw.func_caps.rss) {
12610 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12611 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12612 					   num_online_cpus());
12613 	}
12614 
12615 	/* MFP mode enabled */
12616 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12617 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12618 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12619 		if (i40e_get_partition_bw_setting(pf)) {
12620 			dev_warn(&pf->pdev->dev,
12621 				 "Could not get partition bw settings\n");
12622 		} else {
12623 			dev_info(&pf->pdev->dev,
12624 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12625 				 pf->min_bw, pf->max_bw);
12626 
12627 			/* nudge the Tx scheduler */
12628 			i40e_set_partition_bw_setting(pf);
12629 		}
12630 	}
12631 
12632 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12633 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12634 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12635 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12636 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12637 		    pf->hw.num_partitions > 1)
12638 			dev_info(&pf->pdev->dev,
12639 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12640 		else
12641 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12642 		pf->fdir_pf_filter_count =
12643 				 pf->hw.func_caps.fd_filters_guaranteed;
12644 		pf->hw.fdir_shared_filter_count =
12645 				 pf->hw.func_caps.fd_filters_best_effort;
12646 	}
12647 
12648 	if (pf->hw.mac.type == I40E_MAC_X722) {
12649 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12650 				    I40E_HW_128_QP_RSS_CAPABLE |
12651 				    I40E_HW_ATR_EVICT_CAPABLE |
12652 				    I40E_HW_WB_ON_ITR_CAPABLE |
12653 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12654 				    I40E_HW_NO_PCI_LINK_CHECK |
12655 				    I40E_HW_USE_SET_LLDP_MIB |
12656 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12657 				    I40E_HW_PTP_L4_CAPABLE |
12658 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12659 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12660 
12661 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12662 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12663 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12664 			dev_warn(&pf->pdev->dev,
12665 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12666 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12667 		}
12668 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12669 		   ((pf->hw.aq.api_maj_ver == 1) &&
12670 		    (pf->hw.aq.api_min_ver > 4))) {
12671 		/* Supported in FW API version higher than 1.4 */
12672 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12673 	}
12674 
12675 	/* Enable HW ATR eviction if possible */
12676 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12677 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12678 
12679 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12680 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12681 	    (pf->hw.aq.fw_maj_ver < 4))) {
12682 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12683 		/* No DCB support  for FW < v4.33 */
12684 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12685 	}
12686 
12687 	/* Disable FW LLDP if FW < v4.3 */
12688 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12689 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12690 	    (pf->hw.aq.fw_maj_ver < 4)))
12691 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12692 
12693 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12694 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12695 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12696 	    (pf->hw.aq.fw_maj_ver >= 5)))
12697 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12698 
12699 	/* Enable PTP L4 if FW > v6.0 */
12700 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12701 	    pf->hw.aq.fw_maj_ver >= 6)
12702 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12703 
12704 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12705 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12706 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12707 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12708 	}
12709 
12710 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12711 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12712 		/* IWARP needs one extra vector for CQP just like MISC.*/
12713 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12714 	}
12715 	/* Stopping FW LLDP engine is supported on XL710 and X722
12716 	 * starting from FW versions determined in i40e_init_adminq.
12717 	 * Stopping the FW LLDP engine is not supported on XL710
12718 	 * if NPAR is functioning so unset this hw flag in this case.
12719 	 */
12720 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12721 	    pf->hw.func_caps.npar_enable &&
12722 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12723 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12724 
12725 #ifdef CONFIG_PCI_IOV
12726 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12727 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12728 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12729 		pf->num_req_vfs = min_t(int,
12730 					pf->hw.func_caps.num_vfs,
12731 					I40E_MAX_VF_COUNT);
12732 	}
12733 #endif /* CONFIG_PCI_IOV */
12734 	pf->eeprom_version = 0xDEAD;
12735 	pf->lan_veb = I40E_NO_VEB;
12736 	pf->lan_vsi = I40E_NO_VSI;
12737 
12738 	/* By default FW has this off for performance reasons */
12739 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12740 
12741 	/* set up queue assignment tracking */
12742 	size = sizeof(struct i40e_lump_tracking)
12743 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12744 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12745 	if (!pf->qp_pile) {
12746 		err = -ENOMEM;
12747 		goto sw_init_done;
12748 	}
12749 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12750 
12751 	pf->tx_timeout_recovery_level = 1;
12752 
12753 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12754 	    i40e_is_total_port_shutdown_enabled(pf)) {
12755 		/* Link down on close must be on when total port shutdown
12756 		 * is enabled for a given port
12757 		 */
12758 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12759 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12760 		dev_info(&pf->pdev->dev,
12761 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12762 	}
12763 	mutex_init(&pf->switch_mutex);
12764 
12765 sw_init_done:
12766 	return err;
12767 }
12768 
12769 /**
12770  * i40e_set_ntuple - set the ntuple feature flag and take action
12771  * @pf: board private structure to initialize
12772  * @features: the feature set that the stack is suggesting
12773  *
12774  * returns a bool to indicate if reset needs to happen
12775  **/
12776 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12777 {
12778 	bool need_reset = false;
12779 
12780 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12781 	 * the state changed, we need to reset.
12782 	 */
12783 	if (features & NETIF_F_NTUPLE) {
12784 		/* Enable filters and mark for reset */
12785 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12786 			need_reset = true;
12787 		/* enable FD_SB only if there is MSI-X vector and no cloud
12788 		 * filters exist
12789 		 */
12790 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12791 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12792 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12793 		}
12794 	} else {
12795 		/* turn off filters, mark for reset and clear SW filter list */
12796 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12797 			need_reset = true;
12798 			i40e_fdir_filter_exit(pf);
12799 		}
12800 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12801 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12802 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12803 
12804 		/* reset fd counters */
12805 		pf->fd_add_err = 0;
12806 		pf->fd_atr_cnt = 0;
12807 		/* if ATR was auto disabled it can be re-enabled. */
12808 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12809 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12810 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12811 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12812 	}
12813 	return need_reset;
12814 }
12815 
12816 /**
12817  * i40e_clear_rss_lut - clear the rx hash lookup table
12818  * @vsi: the VSI being configured
12819  **/
12820 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12821 {
12822 	struct i40e_pf *pf = vsi->back;
12823 	struct i40e_hw *hw = &pf->hw;
12824 	u16 vf_id = vsi->vf_id;
12825 	u8 i;
12826 
12827 	if (vsi->type == I40E_VSI_MAIN) {
12828 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12829 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12830 	} else if (vsi->type == I40E_VSI_SRIOV) {
12831 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12832 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12833 	} else {
12834 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12835 	}
12836 }
12837 
12838 /**
12839  * i40e_set_features - set the netdev feature flags
12840  * @netdev: ptr to the netdev being adjusted
12841  * @features: the feature set that the stack is suggesting
12842  * Note: expects to be called while under rtnl_lock()
12843  **/
12844 static int i40e_set_features(struct net_device *netdev,
12845 			     netdev_features_t features)
12846 {
12847 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12848 	struct i40e_vsi *vsi = np->vsi;
12849 	struct i40e_pf *pf = vsi->back;
12850 	bool need_reset;
12851 
12852 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12853 		i40e_pf_config_rss(pf);
12854 	else if (!(features & NETIF_F_RXHASH) &&
12855 		 netdev->features & NETIF_F_RXHASH)
12856 		i40e_clear_rss_lut(vsi);
12857 
12858 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12859 		i40e_vlan_stripping_enable(vsi);
12860 	else
12861 		i40e_vlan_stripping_disable(vsi);
12862 
12863 	if (!(features & NETIF_F_HW_TC) &&
12864 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12865 		dev_err(&pf->pdev->dev,
12866 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12867 		return -EINVAL;
12868 	}
12869 
12870 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12871 		i40e_del_all_macvlans(vsi);
12872 
12873 	need_reset = i40e_set_ntuple(pf, features);
12874 
12875 	if (need_reset)
12876 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12877 
12878 	return 0;
12879 }
12880 
12881 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12882 				    unsigned int table, unsigned int idx,
12883 				    struct udp_tunnel_info *ti)
12884 {
12885 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12886 	struct i40e_hw *hw = &np->vsi->back->hw;
12887 	u8 type, filter_index;
12888 	i40e_status ret;
12889 
12890 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12891 						   I40E_AQC_TUNNEL_TYPE_NGE;
12892 
12893 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12894 				     NULL);
12895 	if (ret) {
12896 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12897 			    i40e_stat_str(hw, ret),
12898 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12899 		return -EIO;
12900 	}
12901 
12902 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12903 	return 0;
12904 }
12905 
12906 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12907 				      unsigned int table, unsigned int idx,
12908 				      struct udp_tunnel_info *ti)
12909 {
12910 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12911 	struct i40e_hw *hw = &np->vsi->back->hw;
12912 	i40e_status ret;
12913 
12914 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12915 	if (ret) {
12916 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12917 			    i40e_stat_str(hw, ret),
12918 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12919 		return -EIO;
12920 	}
12921 
12922 	return 0;
12923 }
12924 
12925 static int i40e_get_phys_port_id(struct net_device *netdev,
12926 				 struct netdev_phys_item_id *ppid)
12927 {
12928 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12929 	struct i40e_pf *pf = np->vsi->back;
12930 	struct i40e_hw *hw = &pf->hw;
12931 
12932 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12933 		return -EOPNOTSUPP;
12934 
12935 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12936 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12937 
12938 	return 0;
12939 }
12940 
12941 /**
12942  * i40e_ndo_fdb_add - add an entry to the hardware database
12943  * @ndm: the input from the stack
12944  * @tb: pointer to array of nladdr (unused)
12945  * @dev: the net device pointer
12946  * @addr: the MAC address entry being added
12947  * @vid: VLAN ID
12948  * @flags: instructions from stack about fdb operation
12949  * @extack: netlink extended ack, unused currently
12950  */
12951 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12952 			    struct net_device *dev,
12953 			    const unsigned char *addr, u16 vid,
12954 			    u16 flags,
12955 			    struct netlink_ext_ack *extack)
12956 {
12957 	struct i40e_netdev_priv *np = netdev_priv(dev);
12958 	struct i40e_pf *pf = np->vsi->back;
12959 	int err = 0;
12960 
12961 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12962 		return -EOPNOTSUPP;
12963 
12964 	if (vid) {
12965 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12966 		return -EINVAL;
12967 	}
12968 
12969 	/* Hardware does not support aging addresses so if a
12970 	 * ndm_state is given only allow permanent addresses
12971 	 */
12972 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12973 		netdev_info(dev, "FDB only supports static addresses\n");
12974 		return -EINVAL;
12975 	}
12976 
12977 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12978 		err = dev_uc_add_excl(dev, addr);
12979 	else if (is_multicast_ether_addr(addr))
12980 		err = dev_mc_add_excl(dev, addr);
12981 	else
12982 		err = -EINVAL;
12983 
12984 	/* Only return duplicate errors if NLM_F_EXCL is set */
12985 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12986 		err = 0;
12987 
12988 	return err;
12989 }
12990 
12991 /**
12992  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12993  * @dev: the netdev being configured
12994  * @nlh: RTNL message
12995  * @flags: bridge flags
12996  * @extack: netlink extended ack
12997  *
12998  * Inserts a new hardware bridge if not already created and
12999  * enables the bridging mode requested (VEB or VEPA). If the
13000  * hardware bridge has already been inserted and the request
13001  * is to change the mode then that requires a PF reset to
13002  * allow rebuild of the components with required hardware
13003  * bridge mode enabled.
13004  *
13005  * Note: expects to be called while under rtnl_lock()
13006  **/
13007 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13008 				   struct nlmsghdr *nlh,
13009 				   u16 flags,
13010 				   struct netlink_ext_ack *extack)
13011 {
13012 	struct i40e_netdev_priv *np = netdev_priv(dev);
13013 	struct i40e_vsi *vsi = np->vsi;
13014 	struct i40e_pf *pf = vsi->back;
13015 	struct i40e_veb *veb = NULL;
13016 	struct nlattr *attr, *br_spec;
13017 	int i, rem;
13018 
13019 	/* Only for PF VSI for now */
13020 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13021 		return -EOPNOTSUPP;
13022 
13023 	/* Find the HW bridge for PF VSI */
13024 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13025 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13026 			veb = pf->veb[i];
13027 	}
13028 
13029 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13030 
13031 	nla_for_each_nested(attr, br_spec, rem) {
13032 		__u16 mode;
13033 
13034 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13035 			continue;
13036 
13037 		mode = nla_get_u16(attr);
13038 		if ((mode != BRIDGE_MODE_VEPA) &&
13039 		    (mode != BRIDGE_MODE_VEB))
13040 			return -EINVAL;
13041 
13042 		/* Insert a new HW bridge */
13043 		if (!veb) {
13044 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13045 					     vsi->tc_config.enabled_tc);
13046 			if (veb) {
13047 				veb->bridge_mode = mode;
13048 				i40e_config_bridge_mode(veb);
13049 			} else {
13050 				/* No Bridge HW offload available */
13051 				return -ENOENT;
13052 			}
13053 			break;
13054 		} else if (mode != veb->bridge_mode) {
13055 			/* Existing HW bridge but different mode needs reset */
13056 			veb->bridge_mode = mode;
13057 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13058 			if (mode == BRIDGE_MODE_VEB)
13059 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13060 			else
13061 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13062 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13063 			break;
13064 		}
13065 	}
13066 
13067 	return 0;
13068 }
13069 
13070 /**
13071  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13072  * @skb: skb buff
13073  * @pid: process id
13074  * @seq: RTNL message seq #
13075  * @dev: the netdev being configured
13076  * @filter_mask: unused
13077  * @nlflags: netlink flags passed in
13078  *
13079  * Return the mode in which the hardware bridge is operating in
13080  * i.e VEB or VEPA.
13081  **/
13082 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13083 				   struct net_device *dev,
13084 				   u32 __always_unused filter_mask,
13085 				   int nlflags)
13086 {
13087 	struct i40e_netdev_priv *np = netdev_priv(dev);
13088 	struct i40e_vsi *vsi = np->vsi;
13089 	struct i40e_pf *pf = vsi->back;
13090 	struct i40e_veb *veb = NULL;
13091 	int i;
13092 
13093 	/* Only for PF VSI for now */
13094 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13095 		return -EOPNOTSUPP;
13096 
13097 	/* Find the HW bridge for the PF VSI */
13098 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13099 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13100 			veb = pf->veb[i];
13101 	}
13102 
13103 	if (!veb)
13104 		return 0;
13105 
13106 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13107 				       0, 0, nlflags, filter_mask, NULL);
13108 }
13109 
13110 /**
13111  * i40e_features_check - Validate encapsulated packet conforms to limits
13112  * @skb: skb buff
13113  * @dev: This physical port's netdev
13114  * @features: Offload features that the stack believes apply
13115  **/
13116 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13117 					     struct net_device *dev,
13118 					     netdev_features_t features)
13119 {
13120 	size_t len;
13121 
13122 	/* No point in doing any of this if neither checksum nor GSO are
13123 	 * being requested for this frame.  We can rule out both by just
13124 	 * checking for CHECKSUM_PARTIAL
13125 	 */
13126 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13127 		return features;
13128 
13129 	/* We cannot support GSO if the MSS is going to be less than
13130 	 * 64 bytes.  If it is then we need to drop support for GSO.
13131 	 */
13132 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13133 		features &= ~NETIF_F_GSO_MASK;
13134 
13135 	/* MACLEN can support at most 63 words */
13136 	len = skb_network_header(skb) - skb->data;
13137 	if (len & ~(63 * 2))
13138 		goto out_err;
13139 
13140 	/* IPLEN and EIPLEN can support at most 127 dwords */
13141 	len = skb_transport_header(skb) - skb_network_header(skb);
13142 	if (len & ~(127 * 4))
13143 		goto out_err;
13144 
13145 	if (skb->encapsulation) {
13146 		/* L4TUNLEN can support 127 words */
13147 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13148 		if (len & ~(127 * 2))
13149 			goto out_err;
13150 
13151 		/* IPLEN can support at most 127 dwords */
13152 		len = skb_inner_transport_header(skb) -
13153 		      skb_inner_network_header(skb);
13154 		if (len & ~(127 * 4))
13155 			goto out_err;
13156 	}
13157 
13158 	/* No need to validate L4LEN as TCP is the only protocol with a
13159 	 * flexible value and we support all possible values supported
13160 	 * by TCP, which is at most 15 dwords
13161 	 */
13162 
13163 	return features;
13164 out_err:
13165 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13166 }
13167 
13168 /**
13169  * i40e_xdp_setup - add/remove an XDP program
13170  * @vsi: VSI to changed
13171  * @prog: XDP program
13172  * @extack: netlink extended ack
13173  **/
13174 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13175 			  struct netlink_ext_ack *extack)
13176 {
13177 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13178 	struct i40e_pf *pf = vsi->back;
13179 	struct bpf_prog *old_prog;
13180 	bool need_reset;
13181 	int i;
13182 
13183 	/* Don't allow frames that span over multiple buffers */
13184 	if (frame_size > vsi->rx_buf_len) {
13185 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13186 		return -EINVAL;
13187 	}
13188 
13189 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13190 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13191 
13192 	if (need_reset)
13193 		i40e_prep_for_reset(pf);
13194 
13195 	/* VSI shall be deleted in a moment, just return EINVAL */
13196 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13197 		return -EINVAL;
13198 
13199 	old_prog = xchg(&vsi->xdp_prog, prog);
13200 
13201 	if (need_reset) {
13202 		if (!prog)
13203 			/* Wait until ndo_xsk_wakeup completes. */
13204 			synchronize_rcu();
13205 		i40e_reset_and_rebuild(pf, true, true);
13206 	}
13207 
13208 	for (i = 0; i < vsi->num_queue_pairs; i++)
13209 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13210 
13211 	if (old_prog)
13212 		bpf_prog_put(old_prog);
13213 
13214 	/* Kick start the NAPI context if there is an AF_XDP socket open
13215 	 * on that queue id. This so that receiving will start.
13216 	 */
13217 	if (need_reset && prog)
13218 		for (i = 0; i < vsi->num_queue_pairs; i++)
13219 			if (vsi->xdp_rings[i]->xsk_pool)
13220 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13221 						      XDP_WAKEUP_RX);
13222 
13223 	return 0;
13224 }
13225 
13226 /**
13227  * i40e_enter_busy_conf - Enters busy config state
13228  * @vsi: vsi
13229  *
13230  * Returns 0 on success, <0 for failure.
13231  **/
13232 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13233 {
13234 	struct i40e_pf *pf = vsi->back;
13235 	int timeout = 50;
13236 
13237 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13238 		timeout--;
13239 		if (!timeout)
13240 			return -EBUSY;
13241 		usleep_range(1000, 2000);
13242 	}
13243 
13244 	return 0;
13245 }
13246 
13247 /**
13248  * i40e_exit_busy_conf - Exits busy config state
13249  * @vsi: vsi
13250  **/
13251 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13252 {
13253 	struct i40e_pf *pf = vsi->back;
13254 
13255 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13256 }
13257 
13258 /**
13259  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13260  * @vsi: vsi
13261  * @queue_pair: queue pair
13262  **/
13263 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13264 {
13265 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13266 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13267 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13268 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13269 	if (i40e_enabled_xdp_vsi(vsi)) {
13270 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13271 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13272 	}
13273 }
13274 
13275 /**
13276  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13277  * @vsi: vsi
13278  * @queue_pair: queue pair
13279  **/
13280 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13281 {
13282 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13283 	if (i40e_enabled_xdp_vsi(vsi)) {
13284 		/* Make sure that in-progress ndo_xdp_xmit calls are
13285 		 * completed.
13286 		 */
13287 		synchronize_rcu();
13288 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13289 	}
13290 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13291 }
13292 
13293 /**
13294  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13295  * @vsi: vsi
13296  * @queue_pair: queue pair
13297  * @enable: true for enable, false for disable
13298  **/
13299 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13300 					bool enable)
13301 {
13302 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13303 	struct i40e_q_vector *q_vector = rxr->q_vector;
13304 
13305 	if (!vsi->netdev)
13306 		return;
13307 
13308 	/* All rings in a qp belong to the same qvector. */
13309 	if (q_vector->rx.ring || q_vector->tx.ring) {
13310 		if (enable)
13311 			napi_enable(&q_vector->napi);
13312 		else
13313 			napi_disable(&q_vector->napi);
13314 	}
13315 }
13316 
13317 /**
13318  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13319  * @vsi: vsi
13320  * @queue_pair: queue pair
13321  * @enable: true for enable, false for disable
13322  *
13323  * Returns 0 on success, <0 on failure.
13324  **/
13325 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13326 					bool enable)
13327 {
13328 	struct i40e_pf *pf = vsi->back;
13329 	int pf_q, ret = 0;
13330 
13331 	pf_q = vsi->base_queue + queue_pair;
13332 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13333 				     false /*is xdp*/, enable);
13334 	if (ret) {
13335 		dev_info(&pf->pdev->dev,
13336 			 "VSI seid %d Tx ring %d %sable timeout\n",
13337 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13338 		return ret;
13339 	}
13340 
13341 	i40e_control_rx_q(pf, pf_q, enable);
13342 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13343 	if (ret) {
13344 		dev_info(&pf->pdev->dev,
13345 			 "VSI seid %d Rx ring %d %sable timeout\n",
13346 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13347 		return ret;
13348 	}
13349 
13350 	/* Due to HW errata, on Rx disable only, the register can
13351 	 * indicate done before it really is. Needs 50ms to be sure
13352 	 */
13353 	if (!enable)
13354 		mdelay(50);
13355 
13356 	if (!i40e_enabled_xdp_vsi(vsi))
13357 		return ret;
13358 
13359 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13360 				     pf_q + vsi->alloc_queue_pairs,
13361 				     true /*is xdp*/, enable);
13362 	if (ret) {
13363 		dev_info(&pf->pdev->dev,
13364 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13365 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13366 	}
13367 
13368 	return ret;
13369 }
13370 
13371 /**
13372  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13373  * @vsi: vsi
13374  * @queue_pair: queue_pair
13375  **/
13376 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13377 {
13378 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13379 	struct i40e_pf *pf = vsi->back;
13380 	struct i40e_hw *hw = &pf->hw;
13381 
13382 	/* All rings in a qp belong to the same qvector. */
13383 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13384 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13385 	else
13386 		i40e_irq_dynamic_enable_icr0(pf);
13387 
13388 	i40e_flush(hw);
13389 }
13390 
13391 /**
13392  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13393  * @vsi: vsi
13394  * @queue_pair: queue_pair
13395  **/
13396 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13397 {
13398 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13399 	struct i40e_pf *pf = vsi->back;
13400 	struct i40e_hw *hw = &pf->hw;
13401 
13402 	/* For simplicity, instead of removing the qp interrupt causes
13403 	 * from the interrupt linked list, we simply disable the interrupt, and
13404 	 * leave the list intact.
13405 	 *
13406 	 * All rings in a qp belong to the same qvector.
13407 	 */
13408 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13409 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13410 
13411 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13412 		i40e_flush(hw);
13413 		synchronize_irq(pf->msix_entries[intpf].vector);
13414 	} else {
13415 		/* Legacy and MSI mode - this stops all interrupt handling */
13416 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13417 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13418 		i40e_flush(hw);
13419 		synchronize_irq(pf->pdev->irq);
13420 	}
13421 }
13422 
13423 /**
13424  * i40e_queue_pair_disable - Disables a queue pair
13425  * @vsi: vsi
13426  * @queue_pair: queue pair
13427  *
13428  * Returns 0 on success, <0 on failure.
13429  **/
13430 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13431 {
13432 	int err;
13433 
13434 	err = i40e_enter_busy_conf(vsi);
13435 	if (err)
13436 		return err;
13437 
13438 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13439 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13440 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13441 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13442 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13443 
13444 	return err;
13445 }
13446 
13447 /**
13448  * i40e_queue_pair_enable - Enables a queue pair
13449  * @vsi: vsi
13450  * @queue_pair: queue pair
13451  *
13452  * Returns 0 on success, <0 on failure.
13453  **/
13454 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13455 {
13456 	int err;
13457 
13458 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13459 	if (err)
13460 		return err;
13461 
13462 	if (i40e_enabled_xdp_vsi(vsi)) {
13463 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13464 		if (err)
13465 			return err;
13466 	}
13467 
13468 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13469 	if (err)
13470 		return err;
13471 
13472 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13473 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13474 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13475 
13476 	i40e_exit_busy_conf(vsi);
13477 
13478 	return err;
13479 }
13480 
13481 /**
13482  * i40e_xdp - implements ndo_bpf for i40e
13483  * @dev: netdevice
13484  * @xdp: XDP command
13485  **/
13486 static int i40e_xdp(struct net_device *dev,
13487 		    struct netdev_bpf *xdp)
13488 {
13489 	struct i40e_netdev_priv *np = netdev_priv(dev);
13490 	struct i40e_vsi *vsi = np->vsi;
13491 
13492 	if (vsi->type != I40E_VSI_MAIN)
13493 		return -EINVAL;
13494 
13495 	switch (xdp->command) {
13496 	case XDP_SETUP_PROG:
13497 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13498 	case XDP_SETUP_XSK_POOL:
13499 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13500 					   xdp->xsk.queue_id);
13501 	default:
13502 		return -EINVAL;
13503 	}
13504 }
13505 
13506 static const struct net_device_ops i40e_netdev_ops = {
13507 	.ndo_open		= i40e_open,
13508 	.ndo_stop		= i40e_close,
13509 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13510 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13511 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13512 	.ndo_validate_addr	= eth_validate_addr,
13513 	.ndo_set_mac_address	= i40e_set_mac,
13514 	.ndo_change_mtu		= i40e_change_mtu,
13515 	.ndo_eth_ioctl		= i40e_ioctl,
13516 	.ndo_tx_timeout		= i40e_tx_timeout,
13517 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13518 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13519 #ifdef CONFIG_NET_POLL_CONTROLLER
13520 	.ndo_poll_controller	= i40e_netpoll,
13521 #endif
13522 	.ndo_setup_tc		= __i40e_setup_tc,
13523 	.ndo_select_queue	= i40e_lan_select_queue,
13524 	.ndo_set_features	= i40e_set_features,
13525 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13526 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13527 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13528 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13529 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13530 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13531 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13532 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13533 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13534 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13535 	.ndo_features_check	= i40e_features_check,
13536 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13537 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13538 	.ndo_bpf		= i40e_xdp,
13539 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13540 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13541 	.ndo_dfwd_add_station	= i40e_fwd_add,
13542 	.ndo_dfwd_del_station	= i40e_fwd_del,
13543 };
13544 
13545 /**
13546  * i40e_config_netdev - Setup the netdev flags
13547  * @vsi: the VSI being configured
13548  *
13549  * Returns 0 on success, negative value on failure
13550  **/
13551 static int i40e_config_netdev(struct i40e_vsi *vsi)
13552 {
13553 	struct i40e_pf *pf = vsi->back;
13554 	struct i40e_hw *hw = &pf->hw;
13555 	struct i40e_netdev_priv *np;
13556 	struct net_device *netdev;
13557 	u8 broadcast[ETH_ALEN];
13558 	u8 mac_addr[ETH_ALEN];
13559 	int etherdev_size;
13560 	netdev_features_t hw_enc_features;
13561 	netdev_features_t hw_features;
13562 
13563 	etherdev_size = sizeof(struct i40e_netdev_priv);
13564 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13565 	if (!netdev)
13566 		return -ENOMEM;
13567 
13568 	vsi->netdev = netdev;
13569 	np = netdev_priv(netdev);
13570 	np->vsi = vsi;
13571 
13572 	hw_enc_features = NETIF_F_SG			|
13573 			  NETIF_F_HW_CSUM		|
13574 			  NETIF_F_HIGHDMA		|
13575 			  NETIF_F_SOFT_FEATURES		|
13576 			  NETIF_F_TSO			|
13577 			  NETIF_F_TSO_ECN		|
13578 			  NETIF_F_TSO6			|
13579 			  NETIF_F_GSO_GRE		|
13580 			  NETIF_F_GSO_GRE_CSUM		|
13581 			  NETIF_F_GSO_PARTIAL		|
13582 			  NETIF_F_GSO_IPXIP4		|
13583 			  NETIF_F_GSO_IPXIP6		|
13584 			  NETIF_F_GSO_UDP_TUNNEL	|
13585 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13586 			  NETIF_F_GSO_UDP_L4		|
13587 			  NETIF_F_SCTP_CRC		|
13588 			  NETIF_F_RXHASH		|
13589 			  NETIF_F_RXCSUM		|
13590 			  0;
13591 
13592 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13593 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13594 
13595 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13596 
13597 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13598 
13599 	netdev->hw_enc_features |= hw_enc_features;
13600 
13601 	/* record features VLANs can make use of */
13602 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13603 
13604 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13605 				   NETIF_F_GSO_GRE_CSUM |	\
13606 				   NETIF_F_GSO_IPXIP4 |		\
13607 				   NETIF_F_GSO_IPXIP6 |		\
13608 				   NETIF_F_GSO_UDP_TUNNEL |	\
13609 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13610 
13611 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13612 	netdev->features |= NETIF_F_GSO_PARTIAL |
13613 			    I40E_GSO_PARTIAL_FEATURES;
13614 
13615 	netdev->mpls_features |= NETIF_F_SG;
13616 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13617 	netdev->mpls_features |= NETIF_F_TSO;
13618 	netdev->mpls_features |= NETIF_F_TSO6;
13619 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13620 
13621 	/* enable macvlan offloads */
13622 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13623 
13624 	hw_features = hw_enc_features		|
13625 		      NETIF_F_HW_VLAN_CTAG_TX	|
13626 		      NETIF_F_HW_VLAN_CTAG_RX;
13627 
13628 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13629 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13630 
13631 	netdev->hw_features |= hw_features;
13632 
13633 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13634 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13635 
13636 	netdev->features &= ~NETIF_F_HW_TC;
13637 
13638 	if (vsi->type == I40E_VSI_MAIN) {
13639 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13640 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13641 		/* The following steps are necessary for two reasons. First,
13642 		 * some older NVM configurations load a default MAC-VLAN
13643 		 * filter that will accept any tagged packet, and we want to
13644 		 * replace this with a normal filter. Additionally, it is
13645 		 * possible our MAC address was provided by the platform using
13646 		 * Open Firmware or similar.
13647 		 *
13648 		 * Thus, we need to remove the default filter and install one
13649 		 * specific to the MAC address.
13650 		 */
13651 		i40e_rm_default_mac_filter(vsi, mac_addr);
13652 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13653 		i40e_add_mac_filter(vsi, mac_addr);
13654 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13655 	} else {
13656 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13657 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13658 		 * the end, which is 4 bytes long, so force truncation of the
13659 		 * original name by IFNAMSIZ - 4
13660 		 */
13661 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13662 			 IFNAMSIZ - 4,
13663 			 pf->vsi[pf->lan_vsi]->netdev->name);
13664 		eth_random_addr(mac_addr);
13665 
13666 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13667 		i40e_add_mac_filter(vsi, mac_addr);
13668 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13669 	}
13670 
13671 	/* Add the broadcast filter so that we initially will receive
13672 	 * broadcast packets. Note that when a new VLAN is first added the
13673 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13674 	 * specific filters as part of transitioning into "vlan" operation.
13675 	 * When more VLANs are added, the driver will copy each existing MAC
13676 	 * filter and add it for the new VLAN.
13677 	 *
13678 	 * Broadcast filters are handled specially by
13679 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13680 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13681 	 * filter. The subtask will update the correct broadcast promiscuous
13682 	 * bits as VLANs become active or inactive.
13683 	 */
13684 	eth_broadcast_addr(broadcast);
13685 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13686 	i40e_add_mac_filter(vsi, broadcast);
13687 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13688 
13689 	eth_hw_addr_set(netdev, mac_addr);
13690 	ether_addr_copy(netdev->perm_addr, mac_addr);
13691 
13692 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13693 	netdev->neigh_priv_len = sizeof(u32) * 4;
13694 
13695 	netdev->priv_flags |= IFF_UNICAST_FLT;
13696 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13697 	/* Setup netdev TC information */
13698 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13699 
13700 	netdev->netdev_ops = &i40e_netdev_ops;
13701 	netdev->watchdog_timeo = 5 * HZ;
13702 	i40e_set_ethtool_ops(netdev);
13703 
13704 	/* MTU range: 68 - 9706 */
13705 	netdev->min_mtu = ETH_MIN_MTU;
13706 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13707 
13708 	return 0;
13709 }
13710 
13711 /**
13712  * i40e_vsi_delete - Delete a VSI from the switch
13713  * @vsi: the VSI being removed
13714  *
13715  * Returns 0 on success, negative value on failure
13716  **/
13717 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13718 {
13719 	/* remove default VSI is not allowed */
13720 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13721 		return;
13722 
13723 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13724 }
13725 
13726 /**
13727  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13728  * @vsi: the VSI being queried
13729  *
13730  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13731  **/
13732 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13733 {
13734 	struct i40e_veb *veb;
13735 	struct i40e_pf *pf = vsi->back;
13736 
13737 	/* Uplink is not a bridge so default to VEB */
13738 	if (vsi->veb_idx >= I40E_MAX_VEB)
13739 		return 1;
13740 
13741 	veb = pf->veb[vsi->veb_idx];
13742 	if (!veb) {
13743 		dev_info(&pf->pdev->dev,
13744 			 "There is no veb associated with the bridge\n");
13745 		return -ENOENT;
13746 	}
13747 
13748 	/* Uplink is a bridge in VEPA mode */
13749 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13750 		return 0;
13751 	} else {
13752 		/* Uplink is a bridge in VEB mode */
13753 		return 1;
13754 	}
13755 
13756 	/* VEPA is now default bridge, so return 0 */
13757 	return 0;
13758 }
13759 
13760 /**
13761  * i40e_add_vsi - Add a VSI to the switch
13762  * @vsi: the VSI being configured
13763  *
13764  * This initializes a VSI context depending on the VSI type to be added and
13765  * passes it down to the add_vsi aq command.
13766  **/
13767 static int i40e_add_vsi(struct i40e_vsi *vsi)
13768 {
13769 	int ret = -ENODEV;
13770 	struct i40e_pf *pf = vsi->back;
13771 	struct i40e_hw *hw = &pf->hw;
13772 	struct i40e_vsi_context ctxt;
13773 	struct i40e_mac_filter *f;
13774 	struct hlist_node *h;
13775 	int bkt;
13776 
13777 	u8 enabled_tc = 0x1; /* TC0 enabled */
13778 	int f_count = 0;
13779 
13780 	memset(&ctxt, 0, sizeof(ctxt));
13781 	switch (vsi->type) {
13782 	case I40E_VSI_MAIN:
13783 		/* The PF's main VSI is already setup as part of the
13784 		 * device initialization, so we'll not bother with
13785 		 * the add_vsi call, but we will retrieve the current
13786 		 * VSI context.
13787 		 */
13788 		ctxt.seid = pf->main_vsi_seid;
13789 		ctxt.pf_num = pf->hw.pf_id;
13790 		ctxt.vf_num = 0;
13791 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13792 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13793 		if (ret) {
13794 			dev_info(&pf->pdev->dev,
13795 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13796 				 i40e_stat_str(&pf->hw, ret),
13797 				 i40e_aq_str(&pf->hw,
13798 					     pf->hw.aq.asq_last_status));
13799 			return -ENOENT;
13800 		}
13801 		vsi->info = ctxt.info;
13802 		vsi->info.valid_sections = 0;
13803 
13804 		vsi->seid = ctxt.seid;
13805 		vsi->id = ctxt.vsi_number;
13806 
13807 		enabled_tc = i40e_pf_get_tc_map(pf);
13808 
13809 		/* Source pruning is enabled by default, so the flag is
13810 		 * negative logic - if it's set, we need to fiddle with
13811 		 * the VSI to disable source pruning.
13812 		 */
13813 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13814 			memset(&ctxt, 0, sizeof(ctxt));
13815 			ctxt.seid = pf->main_vsi_seid;
13816 			ctxt.pf_num = pf->hw.pf_id;
13817 			ctxt.vf_num = 0;
13818 			ctxt.info.valid_sections |=
13819 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13820 			ctxt.info.switch_id =
13821 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13822 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13823 			if (ret) {
13824 				dev_info(&pf->pdev->dev,
13825 					 "update vsi failed, err %s aq_err %s\n",
13826 					 i40e_stat_str(&pf->hw, ret),
13827 					 i40e_aq_str(&pf->hw,
13828 						     pf->hw.aq.asq_last_status));
13829 				ret = -ENOENT;
13830 				goto err;
13831 			}
13832 		}
13833 
13834 		/* MFP mode setup queue map and update VSI */
13835 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13836 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13837 			memset(&ctxt, 0, sizeof(ctxt));
13838 			ctxt.seid = pf->main_vsi_seid;
13839 			ctxt.pf_num = pf->hw.pf_id;
13840 			ctxt.vf_num = 0;
13841 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13842 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13843 			if (ret) {
13844 				dev_info(&pf->pdev->dev,
13845 					 "update vsi failed, err %s aq_err %s\n",
13846 					 i40e_stat_str(&pf->hw, ret),
13847 					 i40e_aq_str(&pf->hw,
13848 						    pf->hw.aq.asq_last_status));
13849 				ret = -ENOENT;
13850 				goto err;
13851 			}
13852 			/* update the local VSI info queue map */
13853 			i40e_vsi_update_queue_map(vsi, &ctxt);
13854 			vsi->info.valid_sections = 0;
13855 		} else {
13856 			/* Default/Main VSI is only enabled for TC0
13857 			 * reconfigure it to enable all TCs that are
13858 			 * available on the port in SFP mode.
13859 			 * For MFP case the iSCSI PF would use this
13860 			 * flow to enable LAN+iSCSI TC.
13861 			 */
13862 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13863 			if (ret) {
13864 				/* Single TC condition is not fatal,
13865 				 * message and continue
13866 				 */
13867 				dev_info(&pf->pdev->dev,
13868 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13869 					 enabled_tc,
13870 					 i40e_stat_str(&pf->hw, ret),
13871 					 i40e_aq_str(&pf->hw,
13872 						    pf->hw.aq.asq_last_status));
13873 			}
13874 		}
13875 		break;
13876 
13877 	case I40E_VSI_FDIR:
13878 		ctxt.pf_num = hw->pf_id;
13879 		ctxt.vf_num = 0;
13880 		ctxt.uplink_seid = vsi->uplink_seid;
13881 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13882 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13883 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13884 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13885 			ctxt.info.valid_sections |=
13886 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13887 			ctxt.info.switch_id =
13888 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13889 		}
13890 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13891 		break;
13892 
13893 	case I40E_VSI_VMDQ2:
13894 		ctxt.pf_num = hw->pf_id;
13895 		ctxt.vf_num = 0;
13896 		ctxt.uplink_seid = vsi->uplink_seid;
13897 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13898 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13899 
13900 		/* This VSI is connected to VEB so the switch_id
13901 		 * should be set to zero by default.
13902 		 */
13903 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13904 			ctxt.info.valid_sections |=
13905 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13906 			ctxt.info.switch_id =
13907 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13908 		}
13909 
13910 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13911 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13912 		break;
13913 
13914 	case I40E_VSI_SRIOV:
13915 		ctxt.pf_num = hw->pf_id;
13916 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13917 		ctxt.uplink_seid = vsi->uplink_seid;
13918 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13919 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13920 
13921 		/* This VSI is connected to VEB so the switch_id
13922 		 * should be set to zero by default.
13923 		 */
13924 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13925 			ctxt.info.valid_sections |=
13926 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13927 			ctxt.info.switch_id =
13928 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13929 		}
13930 
13931 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13932 			ctxt.info.valid_sections |=
13933 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13934 			ctxt.info.queueing_opt_flags |=
13935 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13936 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13937 		}
13938 
13939 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13940 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13941 		if (pf->vf[vsi->vf_id].spoofchk) {
13942 			ctxt.info.valid_sections |=
13943 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13944 			ctxt.info.sec_flags |=
13945 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13946 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13947 		}
13948 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13949 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13950 		break;
13951 
13952 	case I40E_VSI_IWARP:
13953 		/* send down message to iWARP */
13954 		break;
13955 
13956 	default:
13957 		return -ENODEV;
13958 	}
13959 
13960 	if (vsi->type != I40E_VSI_MAIN) {
13961 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13962 		if (ret) {
13963 			dev_info(&vsi->back->pdev->dev,
13964 				 "add vsi failed, err %s aq_err %s\n",
13965 				 i40e_stat_str(&pf->hw, ret),
13966 				 i40e_aq_str(&pf->hw,
13967 					     pf->hw.aq.asq_last_status));
13968 			ret = -ENOENT;
13969 			goto err;
13970 		}
13971 		vsi->info = ctxt.info;
13972 		vsi->info.valid_sections = 0;
13973 		vsi->seid = ctxt.seid;
13974 		vsi->id = ctxt.vsi_number;
13975 	}
13976 
13977 	vsi->active_filters = 0;
13978 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13979 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13980 	/* If macvlan filters already exist, force them to get loaded */
13981 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13982 		f->state = I40E_FILTER_NEW;
13983 		f_count++;
13984 	}
13985 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13986 
13987 	if (f_count) {
13988 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13989 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13990 	}
13991 
13992 	/* Update VSI BW information */
13993 	ret = i40e_vsi_get_bw_info(vsi);
13994 	if (ret) {
13995 		dev_info(&pf->pdev->dev,
13996 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13997 			 i40e_stat_str(&pf->hw, ret),
13998 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13999 		/* VSI is already added so not tearing that up */
14000 		ret = 0;
14001 	}
14002 
14003 err:
14004 	return ret;
14005 }
14006 
14007 /**
14008  * i40e_vsi_release - Delete a VSI and free its resources
14009  * @vsi: the VSI being removed
14010  *
14011  * Returns 0 on success or < 0 on error
14012  **/
14013 int i40e_vsi_release(struct i40e_vsi *vsi)
14014 {
14015 	struct i40e_mac_filter *f;
14016 	struct hlist_node *h;
14017 	struct i40e_veb *veb = NULL;
14018 	struct i40e_pf *pf;
14019 	u16 uplink_seid;
14020 	int i, n, bkt;
14021 
14022 	pf = vsi->back;
14023 
14024 	/* release of a VEB-owner or last VSI is not allowed */
14025 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14026 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14027 			 vsi->seid, vsi->uplink_seid);
14028 		return -ENODEV;
14029 	}
14030 	if (vsi == pf->vsi[pf->lan_vsi] &&
14031 	    !test_bit(__I40E_DOWN, pf->state)) {
14032 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14033 		return -ENODEV;
14034 	}
14035 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14036 	uplink_seid = vsi->uplink_seid;
14037 	if (vsi->type != I40E_VSI_SRIOV) {
14038 		if (vsi->netdev_registered) {
14039 			vsi->netdev_registered = false;
14040 			if (vsi->netdev) {
14041 				/* results in a call to i40e_close() */
14042 				unregister_netdev(vsi->netdev);
14043 			}
14044 		} else {
14045 			i40e_vsi_close(vsi);
14046 		}
14047 		i40e_vsi_disable_irq(vsi);
14048 	}
14049 
14050 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14051 
14052 	/* clear the sync flag on all filters */
14053 	if (vsi->netdev) {
14054 		__dev_uc_unsync(vsi->netdev, NULL);
14055 		__dev_mc_unsync(vsi->netdev, NULL);
14056 	}
14057 
14058 	/* make sure any remaining filters are marked for deletion */
14059 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14060 		__i40e_del_filter(vsi, f);
14061 
14062 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14063 
14064 	i40e_sync_vsi_filters(vsi);
14065 
14066 	i40e_vsi_delete(vsi);
14067 	i40e_vsi_free_q_vectors(vsi);
14068 	if (vsi->netdev) {
14069 		free_netdev(vsi->netdev);
14070 		vsi->netdev = NULL;
14071 	}
14072 	i40e_vsi_clear_rings(vsi);
14073 	i40e_vsi_clear(vsi);
14074 
14075 	/* If this was the last thing on the VEB, except for the
14076 	 * controlling VSI, remove the VEB, which puts the controlling
14077 	 * VSI onto the next level down in the switch.
14078 	 *
14079 	 * Well, okay, there's one more exception here: don't remove
14080 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14081 	 * from up the network stack.
14082 	 */
14083 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14084 		if (pf->vsi[i] &&
14085 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14086 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14087 			n++;      /* count the VSIs */
14088 		}
14089 	}
14090 	for (i = 0; i < I40E_MAX_VEB; i++) {
14091 		if (!pf->veb[i])
14092 			continue;
14093 		if (pf->veb[i]->uplink_seid == uplink_seid)
14094 			n++;     /* count the VEBs */
14095 		if (pf->veb[i]->seid == uplink_seid)
14096 			veb = pf->veb[i];
14097 	}
14098 	if (n == 0 && veb && veb->uplink_seid != 0)
14099 		i40e_veb_release(veb);
14100 
14101 	return 0;
14102 }
14103 
14104 /**
14105  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14106  * @vsi: ptr to the VSI
14107  *
14108  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14109  * corresponding SW VSI structure and initializes num_queue_pairs for the
14110  * newly allocated VSI.
14111  *
14112  * Returns 0 on success or negative on failure
14113  **/
14114 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14115 {
14116 	int ret = -ENOENT;
14117 	struct i40e_pf *pf = vsi->back;
14118 
14119 	if (vsi->q_vectors[0]) {
14120 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14121 			 vsi->seid);
14122 		return -EEXIST;
14123 	}
14124 
14125 	if (vsi->base_vector) {
14126 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14127 			 vsi->seid, vsi->base_vector);
14128 		return -EEXIST;
14129 	}
14130 
14131 	ret = i40e_vsi_alloc_q_vectors(vsi);
14132 	if (ret) {
14133 		dev_info(&pf->pdev->dev,
14134 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14135 			 vsi->num_q_vectors, vsi->seid, ret);
14136 		vsi->num_q_vectors = 0;
14137 		goto vector_setup_out;
14138 	}
14139 
14140 	/* In Legacy mode, we do not have to get any other vector since we
14141 	 * piggyback on the misc/ICR0 for queue interrupts.
14142 	*/
14143 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14144 		return ret;
14145 	if (vsi->num_q_vectors)
14146 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14147 						 vsi->num_q_vectors, vsi->idx);
14148 	if (vsi->base_vector < 0) {
14149 		dev_info(&pf->pdev->dev,
14150 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14151 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14152 		i40e_vsi_free_q_vectors(vsi);
14153 		ret = -ENOENT;
14154 		goto vector_setup_out;
14155 	}
14156 
14157 vector_setup_out:
14158 	return ret;
14159 }
14160 
14161 /**
14162  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14163  * @vsi: pointer to the vsi.
14164  *
14165  * This re-allocates a vsi's queue resources.
14166  *
14167  * Returns pointer to the successfully allocated and configured VSI sw struct
14168  * on success, otherwise returns NULL on failure.
14169  **/
14170 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14171 {
14172 	u16 alloc_queue_pairs;
14173 	struct i40e_pf *pf;
14174 	u8 enabled_tc;
14175 	int ret;
14176 
14177 	if (!vsi)
14178 		return NULL;
14179 
14180 	pf = vsi->back;
14181 
14182 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14183 	i40e_vsi_clear_rings(vsi);
14184 
14185 	i40e_vsi_free_arrays(vsi, false);
14186 	i40e_set_num_rings_in_vsi(vsi);
14187 	ret = i40e_vsi_alloc_arrays(vsi, false);
14188 	if (ret)
14189 		goto err_vsi;
14190 
14191 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14192 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14193 
14194 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14195 	if (ret < 0) {
14196 		dev_info(&pf->pdev->dev,
14197 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14198 			 alloc_queue_pairs, vsi->seid, ret);
14199 		goto err_vsi;
14200 	}
14201 	vsi->base_queue = ret;
14202 
14203 	/* Update the FW view of the VSI. Force a reset of TC and queue
14204 	 * layout configurations.
14205 	 */
14206 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14207 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14208 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14209 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14210 	if (vsi->type == I40E_VSI_MAIN)
14211 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14212 
14213 	/* assign it some queues */
14214 	ret = i40e_alloc_rings(vsi);
14215 	if (ret)
14216 		goto err_rings;
14217 
14218 	/* map all of the rings to the q_vectors */
14219 	i40e_vsi_map_rings_to_vectors(vsi);
14220 	return vsi;
14221 
14222 err_rings:
14223 	i40e_vsi_free_q_vectors(vsi);
14224 	if (vsi->netdev_registered) {
14225 		vsi->netdev_registered = false;
14226 		unregister_netdev(vsi->netdev);
14227 		free_netdev(vsi->netdev);
14228 		vsi->netdev = NULL;
14229 	}
14230 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14231 err_vsi:
14232 	i40e_vsi_clear(vsi);
14233 	return NULL;
14234 }
14235 
14236 /**
14237  * i40e_vsi_setup - Set up a VSI by a given type
14238  * @pf: board private structure
14239  * @type: VSI type
14240  * @uplink_seid: the switch element to link to
14241  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14242  *
14243  * This allocates the sw VSI structure and its queue resources, then add a VSI
14244  * to the identified VEB.
14245  *
14246  * Returns pointer to the successfully allocated and configure VSI sw struct on
14247  * success, otherwise returns NULL on failure.
14248  **/
14249 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14250 				u16 uplink_seid, u32 param1)
14251 {
14252 	struct i40e_vsi *vsi = NULL;
14253 	struct i40e_veb *veb = NULL;
14254 	u16 alloc_queue_pairs;
14255 	int ret, i;
14256 	int v_idx;
14257 
14258 	/* The requested uplink_seid must be either
14259 	 *     - the PF's port seid
14260 	 *              no VEB is needed because this is the PF
14261 	 *              or this is a Flow Director special case VSI
14262 	 *     - seid of an existing VEB
14263 	 *     - seid of a VSI that owns an existing VEB
14264 	 *     - seid of a VSI that doesn't own a VEB
14265 	 *              a new VEB is created and the VSI becomes the owner
14266 	 *     - seid of the PF VSI, which is what creates the first VEB
14267 	 *              this is a special case of the previous
14268 	 *
14269 	 * Find which uplink_seid we were given and create a new VEB if needed
14270 	 */
14271 	for (i = 0; i < I40E_MAX_VEB; i++) {
14272 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14273 			veb = pf->veb[i];
14274 			break;
14275 		}
14276 	}
14277 
14278 	if (!veb && uplink_seid != pf->mac_seid) {
14279 
14280 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14281 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14282 				vsi = pf->vsi[i];
14283 				break;
14284 			}
14285 		}
14286 		if (!vsi) {
14287 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14288 				 uplink_seid);
14289 			return NULL;
14290 		}
14291 
14292 		if (vsi->uplink_seid == pf->mac_seid)
14293 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14294 					     vsi->tc_config.enabled_tc);
14295 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14296 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14297 					     vsi->tc_config.enabled_tc);
14298 		if (veb) {
14299 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14300 				dev_info(&vsi->back->pdev->dev,
14301 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14302 				return NULL;
14303 			}
14304 			/* We come up by default in VEPA mode if SRIOV is not
14305 			 * already enabled, in which case we can't force VEPA
14306 			 * mode.
14307 			 */
14308 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14309 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14310 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14311 			}
14312 			i40e_config_bridge_mode(veb);
14313 		}
14314 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14315 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14316 				veb = pf->veb[i];
14317 		}
14318 		if (!veb) {
14319 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14320 			return NULL;
14321 		}
14322 
14323 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14324 		uplink_seid = veb->seid;
14325 	}
14326 
14327 	/* get vsi sw struct */
14328 	v_idx = i40e_vsi_mem_alloc(pf, type);
14329 	if (v_idx < 0)
14330 		goto err_alloc;
14331 	vsi = pf->vsi[v_idx];
14332 	if (!vsi)
14333 		goto err_alloc;
14334 	vsi->type = type;
14335 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14336 
14337 	if (type == I40E_VSI_MAIN)
14338 		pf->lan_vsi = v_idx;
14339 	else if (type == I40E_VSI_SRIOV)
14340 		vsi->vf_id = param1;
14341 	/* assign it some queues */
14342 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14343 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14344 
14345 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14346 	if (ret < 0) {
14347 		dev_info(&pf->pdev->dev,
14348 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14349 			 alloc_queue_pairs, vsi->seid, ret);
14350 		goto err_vsi;
14351 	}
14352 	vsi->base_queue = ret;
14353 
14354 	/* get a VSI from the hardware */
14355 	vsi->uplink_seid = uplink_seid;
14356 	ret = i40e_add_vsi(vsi);
14357 	if (ret)
14358 		goto err_vsi;
14359 
14360 	switch (vsi->type) {
14361 	/* setup the netdev if needed */
14362 	case I40E_VSI_MAIN:
14363 	case I40E_VSI_VMDQ2:
14364 		ret = i40e_config_netdev(vsi);
14365 		if (ret)
14366 			goto err_netdev;
14367 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14368 		if (ret)
14369 			goto err_netdev;
14370 		ret = register_netdev(vsi->netdev);
14371 		if (ret)
14372 			goto err_netdev;
14373 		vsi->netdev_registered = true;
14374 		netif_carrier_off(vsi->netdev);
14375 #ifdef CONFIG_I40E_DCB
14376 		/* Setup DCB netlink interface */
14377 		i40e_dcbnl_setup(vsi);
14378 #endif /* CONFIG_I40E_DCB */
14379 		fallthrough;
14380 	case I40E_VSI_FDIR:
14381 		/* set up vectors and rings if needed */
14382 		ret = i40e_vsi_setup_vectors(vsi);
14383 		if (ret)
14384 			goto err_msix;
14385 
14386 		ret = i40e_alloc_rings(vsi);
14387 		if (ret)
14388 			goto err_rings;
14389 
14390 		/* map all of the rings to the q_vectors */
14391 		i40e_vsi_map_rings_to_vectors(vsi);
14392 
14393 		i40e_vsi_reset_stats(vsi);
14394 		break;
14395 	default:
14396 		/* no netdev or rings for the other VSI types */
14397 		break;
14398 	}
14399 
14400 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14401 	    (vsi->type == I40E_VSI_VMDQ2)) {
14402 		ret = i40e_vsi_config_rss(vsi);
14403 	}
14404 	return vsi;
14405 
14406 err_rings:
14407 	i40e_vsi_free_q_vectors(vsi);
14408 err_msix:
14409 	if (vsi->netdev_registered) {
14410 		vsi->netdev_registered = false;
14411 		unregister_netdev(vsi->netdev);
14412 		free_netdev(vsi->netdev);
14413 		vsi->netdev = NULL;
14414 	}
14415 err_netdev:
14416 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14417 err_vsi:
14418 	i40e_vsi_clear(vsi);
14419 err_alloc:
14420 	return NULL;
14421 }
14422 
14423 /**
14424  * i40e_veb_get_bw_info - Query VEB BW information
14425  * @veb: the veb to query
14426  *
14427  * Query the Tx scheduler BW configuration data for given VEB
14428  **/
14429 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14430 {
14431 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14432 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14433 	struct i40e_pf *pf = veb->pf;
14434 	struct i40e_hw *hw = &pf->hw;
14435 	u32 tc_bw_max;
14436 	int ret = 0;
14437 	int i;
14438 
14439 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14440 						  &bw_data, NULL);
14441 	if (ret) {
14442 		dev_info(&pf->pdev->dev,
14443 			 "query veb bw config failed, err %s aq_err %s\n",
14444 			 i40e_stat_str(&pf->hw, ret),
14445 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14446 		goto out;
14447 	}
14448 
14449 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14450 						   &ets_data, NULL);
14451 	if (ret) {
14452 		dev_info(&pf->pdev->dev,
14453 			 "query veb bw ets config failed, err %s aq_err %s\n",
14454 			 i40e_stat_str(&pf->hw, ret),
14455 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14456 		goto out;
14457 	}
14458 
14459 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14460 	veb->bw_max_quanta = ets_data.tc_bw_max;
14461 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14462 	veb->enabled_tc = ets_data.tc_valid_bits;
14463 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14464 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14465 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14466 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14467 		veb->bw_tc_limit_credits[i] =
14468 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14469 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14470 	}
14471 
14472 out:
14473 	return ret;
14474 }
14475 
14476 /**
14477  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14478  * @pf: board private structure
14479  *
14480  * On error: returns error code (negative)
14481  * On success: returns vsi index in PF (positive)
14482  **/
14483 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14484 {
14485 	int ret = -ENOENT;
14486 	struct i40e_veb *veb;
14487 	int i;
14488 
14489 	/* Need to protect the allocation of switch elements at the PF level */
14490 	mutex_lock(&pf->switch_mutex);
14491 
14492 	/* VEB list may be fragmented if VEB creation/destruction has
14493 	 * been happening.  We can afford to do a quick scan to look
14494 	 * for any free slots in the list.
14495 	 *
14496 	 * find next empty veb slot, looping back around if necessary
14497 	 */
14498 	i = 0;
14499 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14500 		i++;
14501 	if (i >= I40E_MAX_VEB) {
14502 		ret = -ENOMEM;
14503 		goto err_alloc_veb;  /* out of VEB slots! */
14504 	}
14505 
14506 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14507 	if (!veb) {
14508 		ret = -ENOMEM;
14509 		goto err_alloc_veb;
14510 	}
14511 	veb->pf = pf;
14512 	veb->idx = i;
14513 	veb->enabled_tc = 1;
14514 
14515 	pf->veb[i] = veb;
14516 	ret = i;
14517 err_alloc_veb:
14518 	mutex_unlock(&pf->switch_mutex);
14519 	return ret;
14520 }
14521 
14522 /**
14523  * i40e_switch_branch_release - Delete a branch of the switch tree
14524  * @branch: where to start deleting
14525  *
14526  * This uses recursion to find the tips of the branch to be
14527  * removed, deleting until we get back to and can delete this VEB.
14528  **/
14529 static void i40e_switch_branch_release(struct i40e_veb *branch)
14530 {
14531 	struct i40e_pf *pf = branch->pf;
14532 	u16 branch_seid = branch->seid;
14533 	u16 veb_idx = branch->idx;
14534 	int i;
14535 
14536 	/* release any VEBs on this VEB - RECURSION */
14537 	for (i = 0; i < I40E_MAX_VEB; i++) {
14538 		if (!pf->veb[i])
14539 			continue;
14540 		if (pf->veb[i]->uplink_seid == branch->seid)
14541 			i40e_switch_branch_release(pf->veb[i]);
14542 	}
14543 
14544 	/* Release the VSIs on this VEB, but not the owner VSI.
14545 	 *
14546 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14547 	 *       the VEB itself, so don't use (*branch) after this loop.
14548 	 */
14549 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14550 		if (!pf->vsi[i])
14551 			continue;
14552 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14553 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14554 			i40e_vsi_release(pf->vsi[i]);
14555 		}
14556 	}
14557 
14558 	/* There's one corner case where the VEB might not have been
14559 	 * removed, so double check it here and remove it if needed.
14560 	 * This case happens if the veb was created from the debugfs
14561 	 * commands and no VSIs were added to it.
14562 	 */
14563 	if (pf->veb[veb_idx])
14564 		i40e_veb_release(pf->veb[veb_idx]);
14565 }
14566 
14567 /**
14568  * i40e_veb_clear - remove veb struct
14569  * @veb: the veb to remove
14570  **/
14571 static void i40e_veb_clear(struct i40e_veb *veb)
14572 {
14573 	if (!veb)
14574 		return;
14575 
14576 	if (veb->pf) {
14577 		struct i40e_pf *pf = veb->pf;
14578 
14579 		mutex_lock(&pf->switch_mutex);
14580 		if (pf->veb[veb->idx] == veb)
14581 			pf->veb[veb->idx] = NULL;
14582 		mutex_unlock(&pf->switch_mutex);
14583 	}
14584 
14585 	kfree(veb);
14586 }
14587 
14588 /**
14589  * i40e_veb_release - Delete a VEB and free its resources
14590  * @veb: the VEB being removed
14591  **/
14592 void i40e_veb_release(struct i40e_veb *veb)
14593 {
14594 	struct i40e_vsi *vsi = NULL;
14595 	struct i40e_pf *pf;
14596 	int i, n = 0;
14597 
14598 	pf = veb->pf;
14599 
14600 	/* find the remaining VSI and check for extras */
14601 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14602 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14603 			n++;
14604 			vsi = pf->vsi[i];
14605 		}
14606 	}
14607 	if (n != 1) {
14608 		dev_info(&pf->pdev->dev,
14609 			 "can't remove VEB %d with %d VSIs left\n",
14610 			 veb->seid, n);
14611 		return;
14612 	}
14613 
14614 	/* move the remaining VSI to uplink veb */
14615 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14616 	if (veb->uplink_seid) {
14617 		vsi->uplink_seid = veb->uplink_seid;
14618 		if (veb->uplink_seid == pf->mac_seid)
14619 			vsi->veb_idx = I40E_NO_VEB;
14620 		else
14621 			vsi->veb_idx = veb->veb_idx;
14622 	} else {
14623 		/* floating VEB */
14624 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14625 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14626 	}
14627 
14628 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14629 	i40e_veb_clear(veb);
14630 }
14631 
14632 /**
14633  * i40e_add_veb - create the VEB in the switch
14634  * @veb: the VEB to be instantiated
14635  * @vsi: the controlling VSI
14636  **/
14637 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14638 {
14639 	struct i40e_pf *pf = veb->pf;
14640 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14641 	int ret;
14642 
14643 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14644 			      veb->enabled_tc, false,
14645 			      &veb->seid, enable_stats, NULL);
14646 
14647 	/* get a VEB from the hardware */
14648 	if (ret) {
14649 		dev_info(&pf->pdev->dev,
14650 			 "couldn't add VEB, err %s aq_err %s\n",
14651 			 i40e_stat_str(&pf->hw, ret),
14652 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14653 		return -EPERM;
14654 	}
14655 
14656 	/* get statistics counter */
14657 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14658 					 &veb->stats_idx, NULL, NULL, NULL);
14659 	if (ret) {
14660 		dev_info(&pf->pdev->dev,
14661 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14662 			 i40e_stat_str(&pf->hw, ret),
14663 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14664 		return -EPERM;
14665 	}
14666 	ret = i40e_veb_get_bw_info(veb);
14667 	if (ret) {
14668 		dev_info(&pf->pdev->dev,
14669 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14670 			 i40e_stat_str(&pf->hw, ret),
14671 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14672 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14673 		return -ENOENT;
14674 	}
14675 
14676 	vsi->uplink_seid = veb->seid;
14677 	vsi->veb_idx = veb->idx;
14678 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14679 
14680 	return 0;
14681 }
14682 
14683 /**
14684  * i40e_veb_setup - Set up a VEB
14685  * @pf: board private structure
14686  * @flags: VEB setup flags
14687  * @uplink_seid: the switch element to link to
14688  * @vsi_seid: the initial VSI seid
14689  * @enabled_tc: Enabled TC bit-map
14690  *
14691  * This allocates the sw VEB structure and links it into the switch
14692  * It is possible and legal for this to be a duplicate of an already
14693  * existing VEB.  It is also possible for both uplink and vsi seids
14694  * to be zero, in order to create a floating VEB.
14695  *
14696  * Returns pointer to the successfully allocated VEB sw struct on
14697  * success, otherwise returns NULL on failure.
14698  **/
14699 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14700 				u16 uplink_seid, u16 vsi_seid,
14701 				u8 enabled_tc)
14702 {
14703 	struct i40e_veb *veb, *uplink_veb = NULL;
14704 	int vsi_idx, veb_idx;
14705 	int ret;
14706 
14707 	/* if one seid is 0, the other must be 0 to create a floating relay */
14708 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14709 	    (uplink_seid + vsi_seid != 0)) {
14710 		dev_info(&pf->pdev->dev,
14711 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14712 			 uplink_seid, vsi_seid);
14713 		return NULL;
14714 	}
14715 
14716 	/* make sure there is such a vsi and uplink */
14717 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14718 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14719 			break;
14720 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14721 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14722 			 vsi_seid);
14723 		return NULL;
14724 	}
14725 
14726 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14727 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14728 			if (pf->veb[veb_idx] &&
14729 			    pf->veb[veb_idx]->seid == uplink_seid) {
14730 				uplink_veb = pf->veb[veb_idx];
14731 				break;
14732 			}
14733 		}
14734 		if (!uplink_veb) {
14735 			dev_info(&pf->pdev->dev,
14736 				 "uplink seid %d not found\n", uplink_seid);
14737 			return NULL;
14738 		}
14739 	}
14740 
14741 	/* get veb sw struct */
14742 	veb_idx = i40e_veb_mem_alloc(pf);
14743 	if (veb_idx < 0)
14744 		goto err_alloc;
14745 	veb = pf->veb[veb_idx];
14746 	veb->flags = flags;
14747 	veb->uplink_seid = uplink_seid;
14748 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14749 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14750 
14751 	/* create the VEB in the switch */
14752 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14753 	if (ret)
14754 		goto err_veb;
14755 	if (vsi_idx == pf->lan_vsi)
14756 		pf->lan_veb = veb->idx;
14757 
14758 	return veb;
14759 
14760 err_veb:
14761 	i40e_veb_clear(veb);
14762 err_alloc:
14763 	return NULL;
14764 }
14765 
14766 /**
14767  * i40e_setup_pf_switch_element - set PF vars based on switch type
14768  * @pf: board private structure
14769  * @ele: element we are building info from
14770  * @num_reported: total number of elements
14771  * @printconfig: should we print the contents
14772  *
14773  * helper function to assist in extracting a few useful SEID values.
14774  **/
14775 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14776 				struct i40e_aqc_switch_config_element_resp *ele,
14777 				u16 num_reported, bool printconfig)
14778 {
14779 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14780 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14781 	u8 element_type = ele->element_type;
14782 	u16 seid = le16_to_cpu(ele->seid);
14783 
14784 	if (printconfig)
14785 		dev_info(&pf->pdev->dev,
14786 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14787 			 element_type, seid, uplink_seid, downlink_seid);
14788 
14789 	switch (element_type) {
14790 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14791 		pf->mac_seid = seid;
14792 		break;
14793 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14794 		/* Main VEB? */
14795 		if (uplink_seid != pf->mac_seid)
14796 			break;
14797 		if (pf->lan_veb >= I40E_MAX_VEB) {
14798 			int v;
14799 
14800 			/* find existing or else empty VEB */
14801 			for (v = 0; v < I40E_MAX_VEB; v++) {
14802 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14803 					pf->lan_veb = v;
14804 					break;
14805 				}
14806 			}
14807 			if (pf->lan_veb >= I40E_MAX_VEB) {
14808 				v = i40e_veb_mem_alloc(pf);
14809 				if (v < 0)
14810 					break;
14811 				pf->lan_veb = v;
14812 			}
14813 		}
14814 		if (pf->lan_veb >= I40E_MAX_VEB)
14815 			break;
14816 
14817 		pf->veb[pf->lan_veb]->seid = seid;
14818 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14819 		pf->veb[pf->lan_veb]->pf = pf;
14820 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14821 		break;
14822 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14823 		if (num_reported != 1)
14824 			break;
14825 		/* This is immediately after a reset so we can assume this is
14826 		 * the PF's VSI
14827 		 */
14828 		pf->mac_seid = uplink_seid;
14829 		pf->pf_seid = downlink_seid;
14830 		pf->main_vsi_seid = seid;
14831 		if (printconfig)
14832 			dev_info(&pf->pdev->dev,
14833 				 "pf_seid=%d main_vsi_seid=%d\n",
14834 				 pf->pf_seid, pf->main_vsi_seid);
14835 		break;
14836 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14837 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14838 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14839 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14840 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14841 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14842 		/* ignore these for now */
14843 		break;
14844 	default:
14845 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14846 			 element_type, seid);
14847 		break;
14848 	}
14849 }
14850 
14851 /**
14852  * i40e_fetch_switch_configuration - Get switch config from firmware
14853  * @pf: board private structure
14854  * @printconfig: should we print the contents
14855  *
14856  * Get the current switch configuration from the device and
14857  * extract a few useful SEID values.
14858  **/
14859 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14860 {
14861 	struct i40e_aqc_get_switch_config_resp *sw_config;
14862 	u16 next_seid = 0;
14863 	int ret = 0;
14864 	u8 *aq_buf;
14865 	int i;
14866 
14867 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14868 	if (!aq_buf)
14869 		return -ENOMEM;
14870 
14871 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14872 	do {
14873 		u16 num_reported, num_total;
14874 
14875 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14876 						I40E_AQ_LARGE_BUF,
14877 						&next_seid, NULL);
14878 		if (ret) {
14879 			dev_info(&pf->pdev->dev,
14880 				 "get switch config failed err %s aq_err %s\n",
14881 				 i40e_stat_str(&pf->hw, ret),
14882 				 i40e_aq_str(&pf->hw,
14883 					     pf->hw.aq.asq_last_status));
14884 			kfree(aq_buf);
14885 			return -ENOENT;
14886 		}
14887 
14888 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14889 		num_total = le16_to_cpu(sw_config->header.num_total);
14890 
14891 		if (printconfig)
14892 			dev_info(&pf->pdev->dev,
14893 				 "header: %d reported %d total\n",
14894 				 num_reported, num_total);
14895 
14896 		for (i = 0; i < num_reported; i++) {
14897 			struct i40e_aqc_switch_config_element_resp *ele =
14898 				&sw_config->element[i];
14899 
14900 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14901 						     printconfig);
14902 		}
14903 	} while (next_seid != 0);
14904 
14905 	kfree(aq_buf);
14906 	return ret;
14907 }
14908 
14909 /**
14910  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14911  * @pf: board private structure
14912  * @reinit: if the Main VSI needs to re-initialized.
14913  * @lock_acquired: indicates whether or not the lock has been acquired
14914  *
14915  * Returns 0 on success, negative value on failure
14916  **/
14917 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14918 {
14919 	u16 flags = 0;
14920 	int ret;
14921 
14922 	/* find out what's out there already */
14923 	ret = i40e_fetch_switch_configuration(pf, false);
14924 	if (ret) {
14925 		dev_info(&pf->pdev->dev,
14926 			 "couldn't fetch switch config, err %s aq_err %s\n",
14927 			 i40e_stat_str(&pf->hw, ret),
14928 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14929 		return ret;
14930 	}
14931 	i40e_pf_reset_stats(pf);
14932 
14933 	/* set the switch config bit for the whole device to
14934 	 * support limited promisc or true promisc
14935 	 * when user requests promisc. The default is limited
14936 	 * promisc.
14937 	*/
14938 
14939 	if ((pf->hw.pf_id == 0) &&
14940 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14941 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14942 		pf->last_sw_conf_flags = flags;
14943 	}
14944 
14945 	if (pf->hw.pf_id == 0) {
14946 		u16 valid_flags;
14947 
14948 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14949 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14950 						NULL);
14951 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14952 			dev_info(&pf->pdev->dev,
14953 				 "couldn't set switch config bits, err %s aq_err %s\n",
14954 				 i40e_stat_str(&pf->hw, ret),
14955 				 i40e_aq_str(&pf->hw,
14956 					     pf->hw.aq.asq_last_status));
14957 			/* not a fatal problem, just keep going */
14958 		}
14959 		pf->last_sw_conf_valid_flags = valid_flags;
14960 	}
14961 
14962 	/* first time setup */
14963 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14964 		struct i40e_vsi *vsi = NULL;
14965 		u16 uplink_seid;
14966 
14967 		/* Set up the PF VSI associated with the PF's main VSI
14968 		 * that is already in the HW switch
14969 		 */
14970 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14971 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14972 		else
14973 			uplink_seid = pf->mac_seid;
14974 		if (pf->lan_vsi == I40E_NO_VSI)
14975 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14976 		else if (reinit)
14977 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14978 		if (!vsi) {
14979 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14980 			i40e_cloud_filter_exit(pf);
14981 			i40e_fdir_teardown(pf);
14982 			return -EAGAIN;
14983 		}
14984 	} else {
14985 		/* force a reset of TC and queue layout configurations */
14986 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14987 
14988 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14989 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14990 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14991 	}
14992 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14993 
14994 	i40e_fdir_sb_setup(pf);
14995 
14996 	/* Setup static PF queue filter control settings */
14997 	ret = i40e_setup_pf_filter_control(pf);
14998 	if (ret) {
14999 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15000 			 ret);
15001 		/* Failure here should not stop continuing other steps */
15002 	}
15003 
15004 	/* enable RSS in the HW, even for only one queue, as the stack can use
15005 	 * the hash
15006 	 */
15007 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15008 		i40e_pf_config_rss(pf);
15009 
15010 	/* fill in link information and enable LSE reporting */
15011 	i40e_link_event(pf);
15012 
15013 	/* Initialize user-specific link properties */
15014 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15015 				  I40E_AQ_AN_COMPLETED) ? true : false);
15016 
15017 	i40e_ptp_init(pf);
15018 
15019 	if (!lock_acquired)
15020 		rtnl_lock();
15021 
15022 	/* repopulate tunnel port filters */
15023 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15024 
15025 	if (!lock_acquired)
15026 		rtnl_unlock();
15027 
15028 	return ret;
15029 }
15030 
15031 /**
15032  * i40e_determine_queue_usage - Work out queue distribution
15033  * @pf: board private structure
15034  **/
15035 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15036 {
15037 	int queues_left;
15038 	int q_max;
15039 
15040 	pf->num_lan_qps = 0;
15041 
15042 	/* Find the max queues to be put into basic use.  We'll always be
15043 	 * using TC0, whether or not DCB is running, and TC0 will get the
15044 	 * big RSS set.
15045 	 */
15046 	queues_left = pf->hw.func_caps.num_tx_qp;
15047 
15048 	if ((queues_left == 1) ||
15049 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15050 		/* one qp for PF, no queues for anything else */
15051 		queues_left = 0;
15052 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15053 
15054 		/* make sure all the fancies are disabled */
15055 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15056 			       I40E_FLAG_IWARP_ENABLED	|
15057 			       I40E_FLAG_FD_SB_ENABLED	|
15058 			       I40E_FLAG_FD_ATR_ENABLED	|
15059 			       I40E_FLAG_DCB_CAPABLE	|
15060 			       I40E_FLAG_DCB_ENABLED	|
15061 			       I40E_FLAG_SRIOV_ENABLED	|
15062 			       I40E_FLAG_VMDQ_ENABLED);
15063 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15064 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15065 				  I40E_FLAG_FD_SB_ENABLED |
15066 				  I40E_FLAG_FD_ATR_ENABLED |
15067 				  I40E_FLAG_DCB_CAPABLE))) {
15068 		/* one qp for PF */
15069 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15070 		queues_left -= pf->num_lan_qps;
15071 
15072 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15073 			       I40E_FLAG_IWARP_ENABLED	|
15074 			       I40E_FLAG_FD_SB_ENABLED	|
15075 			       I40E_FLAG_FD_ATR_ENABLED	|
15076 			       I40E_FLAG_DCB_ENABLED	|
15077 			       I40E_FLAG_VMDQ_ENABLED);
15078 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15079 	} else {
15080 		/* Not enough queues for all TCs */
15081 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15082 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15083 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15084 					I40E_FLAG_DCB_ENABLED);
15085 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15086 		}
15087 
15088 		/* limit lan qps to the smaller of qps, cpus or msix */
15089 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15090 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15091 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15092 		pf->num_lan_qps = q_max;
15093 
15094 		queues_left -= pf->num_lan_qps;
15095 	}
15096 
15097 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15098 		if (queues_left > 1) {
15099 			queues_left -= 1; /* save 1 queue for FD */
15100 		} else {
15101 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15102 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15103 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15104 		}
15105 	}
15106 
15107 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15108 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15109 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15110 					(queues_left / pf->num_vf_qps));
15111 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15112 	}
15113 
15114 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15115 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15116 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15117 					  (queues_left / pf->num_vmdq_qps));
15118 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15119 	}
15120 
15121 	pf->queues_left = queues_left;
15122 	dev_dbg(&pf->pdev->dev,
15123 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15124 		pf->hw.func_caps.num_tx_qp,
15125 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15126 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15127 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15128 		queues_left);
15129 }
15130 
15131 /**
15132  * i40e_setup_pf_filter_control - Setup PF static filter control
15133  * @pf: PF to be setup
15134  *
15135  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15136  * settings. If PE/FCoE are enabled then it will also set the per PF
15137  * based filter sizes required for them. It also enables Flow director,
15138  * ethertype and macvlan type filter settings for the pf.
15139  *
15140  * Returns 0 on success, negative on failure
15141  **/
15142 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15143 {
15144 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15145 
15146 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15147 
15148 	/* Flow Director is enabled */
15149 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15150 		settings->enable_fdir = true;
15151 
15152 	/* Ethtype and MACVLAN filters enabled for PF */
15153 	settings->enable_ethtype = true;
15154 	settings->enable_macvlan = true;
15155 
15156 	if (i40e_set_filter_control(&pf->hw, settings))
15157 		return -ENOENT;
15158 
15159 	return 0;
15160 }
15161 
15162 #define INFO_STRING_LEN 255
15163 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15164 static void i40e_print_features(struct i40e_pf *pf)
15165 {
15166 	struct i40e_hw *hw = &pf->hw;
15167 	char *buf;
15168 	int i;
15169 
15170 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15171 	if (!buf)
15172 		return;
15173 
15174 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15175 #ifdef CONFIG_PCI_IOV
15176 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15177 #endif
15178 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15179 		      pf->hw.func_caps.num_vsis,
15180 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15181 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15182 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15183 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15184 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15185 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15186 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15187 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15188 	}
15189 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15190 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15191 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15192 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15193 	if (pf->flags & I40E_FLAG_PTP)
15194 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15195 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15196 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15197 	else
15198 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15199 
15200 	dev_info(&pf->pdev->dev, "%s\n", buf);
15201 	kfree(buf);
15202 	WARN_ON(i > INFO_STRING_LEN);
15203 }
15204 
15205 /**
15206  * i40e_get_platform_mac_addr - get platform-specific MAC address
15207  * @pdev: PCI device information struct
15208  * @pf: board private structure
15209  *
15210  * Look up the MAC address for the device. First we'll try
15211  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15212  * specific fallback. Otherwise, we'll default to the stored value in
15213  * firmware.
15214  **/
15215 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15216 {
15217 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15218 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15219 }
15220 
15221 /**
15222  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15223  * @fec_cfg: FEC option to set in flags
15224  * @flags: ptr to flags in which we set FEC option
15225  **/
15226 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15227 {
15228 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15229 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15230 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15231 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15232 		*flags |= I40E_FLAG_RS_FEC;
15233 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15234 	}
15235 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15236 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15237 		*flags |= I40E_FLAG_BASE_R_FEC;
15238 		*flags &= ~I40E_FLAG_RS_FEC;
15239 	}
15240 	if (fec_cfg == 0)
15241 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15242 }
15243 
15244 /**
15245  * i40e_check_recovery_mode - check if we are running transition firmware
15246  * @pf: board private structure
15247  *
15248  * Check registers indicating the firmware runs in recovery mode. Sets the
15249  * appropriate driver state.
15250  *
15251  * Returns true if the recovery mode was detected, false otherwise
15252  **/
15253 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15254 {
15255 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15256 
15257 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15258 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15259 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15260 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15261 
15262 		return true;
15263 	}
15264 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15265 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15266 
15267 	return false;
15268 }
15269 
15270 /**
15271  * i40e_pf_loop_reset - perform reset in a loop.
15272  * @pf: board private structure
15273  *
15274  * This function is useful when a NIC is about to enter recovery mode.
15275  * When a NIC's internal data structures are corrupted the NIC's
15276  * firmware is going to enter recovery mode.
15277  * Right after a POR it takes about 7 minutes for firmware to enter
15278  * recovery mode. Until that time a NIC is in some kind of intermediate
15279  * state. After that time period the NIC almost surely enters
15280  * recovery mode. The only way for a driver to detect intermediate
15281  * state is to issue a series of pf-resets and check a return value.
15282  * If a PF reset returns success then the firmware could be in recovery
15283  * mode so the caller of this code needs to check for recovery mode
15284  * if this function returns success. There is a little chance that
15285  * firmware will hang in intermediate state forever.
15286  * Since waiting 7 minutes is quite a lot of time this function waits
15287  * 10 seconds and then gives up by returning an error.
15288  *
15289  * Return 0 on success, negative on failure.
15290  **/
15291 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15292 {
15293 	/* wait max 10 seconds for PF reset to succeed */
15294 	const unsigned long time_end = jiffies + 10 * HZ;
15295 
15296 	struct i40e_hw *hw = &pf->hw;
15297 	i40e_status ret;
15298 
15299 	ret = i40e_pf_reset(hw);
15300 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15301 		usleep_range(10000, 20000);
15302 		ret = i40e_pf_reset(hw);
15303 	}
15304 
15305 	if (ret == I40E_SUCCESS)
15306 		pf->pfr_count++;
15307 	else
15308 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15309 
15310 	return ret;
15311 }
15312 
15313 /**
15314  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15315  * @pf: board private structure
15316  *
15317  * Check FW registers to determine if FW issued unexpected EMP Reset.
15318  * Every time when unexpected EMP Reset occurs the FW increments
15319  * a counter of unexpected EMP Resets. When the counter reaches 10
15320  * the FW should enter the Recovery mode
15321  *
15322  * Returns true if FW issued unexpected EMP Reset
15323  **/
15324 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15325 {
15326 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15327 			   I40E_GL_FWSTS_FWS1B_MASK;
15328 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15329 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15330 }
15331 
15332 /**
15333  * i40e_handle_resets - handle EMP resets and PF resets
15334  * @pf: board private structure
15335  *
15336  * Handle both EMP resets and PF resets and conclude whether there are
15337  * any issues regarding these resets. If there are any issues then
15338  * generate log entry.
15339  *
15340  * Return 0 if NIC is healthy or negative value when there are issues
15341  * with resets
15342  **/
15343 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15344 {
15345 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15346 	const bool is_empr = i40e_check_fw_empr(pf);
15347 
15348 	if (is_empr || pfr != I40E_SUCCESS)
15349 		dev_crit(&pf->pdev->dev, "Entering recovery mode due to repeated FW resets. This may take several minutes. Refer to the Intel(R) Ethernet Adapters and Devices User Guide.\n");
15350 
15351 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15352 }
15353 
15354 /**
15355  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15356  * @pf: board private structure
15357  * @hw: ptr to the hardware info
15358  *
15359  * This function does a minimal setup of all subsystems needed for running
15360  * recovery mode.
15361  *
15362  * Returns 0 on success, negative on failure
15363  **/
15364 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15365 {
15366 	struct i40e_vsi *vsi;
15367 	int err;
15368 	int v_idx;
15369 
15370 	pci_save_state(pf->pdev);
15371 
15372 	/* set up periodic task facility */
15373 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15374 	pf->service_timer_period = HZ;
15375 
15376 	INIT_WORK(&pf->service_task, i40e_service_task);
15377 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15378 
15379 	err = i40e_init_interrupt_scheme(pf);
15380 	if (err)
15381 		goto err_switch_setup;
15382 
15383 	/* The number of VSIs reported by the FW is the minimum guaranteed
15384 	 * to us; HW supports far more and we share the remaining pool with
15385 	 * the other PFs. We allocate space for more than the guarantee with
15386 	 * the understanding that we might not get them all later.
15387 	 */
15388 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15389 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15390 	else
15391 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15392 
15393 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15394 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15395 			  GFP_KERNEL);
15396 	if (!pf->vsi) {
15397 		err = -ENOMEM;
15398 		goto err_switch_setup;
15399 	}
15400 
15401 	/* We allocate one VSI which is needed as absolute minimum
15402 	 * in order to register the netdev
15403 	 */
15404 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15405 	if (v_idx < 0) {
15406 		err = v_idx;
15407 		goto err_switch_setup;
15408 	}
15409 	pf->lan_vsi = v_idx;
15410 	vsi = pf->vsi[v_idx];
15411 	if (!vsi) {
15412 		err = -EFAULT;
15413 		goto err_switch_setup;
15414 	}
15415 	vsi->alloc_queue_pairs = 1;
15416 	err = i40e_config_netdev(vsi);
15417 	if (err)
15418 		goto err_switch_setup;
15419 	err = register_netdev(vsi->netdev);
15420 	if (err)
15421 		goto err_switch_setup;
15422 	vsi->netdev_registered = true;
15423 	i40e_dbg_pf_init(pf);
15424 
15425 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15426 	if (err)
15427 		goto err_switch_setup;
15428 
15429 	/* tell the firmware that we're starting */
15430 	i40e_send_version(pf);
15431 
15432 	/* since everything's happy, start the service_task timer */
15433 	mod_timer(&pf->service_timer,
15434 		  round_jiffies(jiffies + pf->service_timer_period));
15435 
15436 	return 0;
15437 
15438 err_switch_setup:
15439 	i40e_reset_interrupt_capability(pf);
15440 	del_timer_sync(&pf->service_timer);
15441 	i40e_shutdown_adminq(hw);
15442 	iounmap(hw->hw_addr);
15443 	pci_disable_pcie_error_reporting(pf->pdev);
15444 	pci_release_mem_regions(pf->pdev);
15445 	pci_disable_device(pf->pdev);
15446 	kfree(pf);
15447 
15448 	return err;
15449 }
15450 
15451 /**
15452  * i40e_set_subsystem_device_id - set subsystem device id
15453  * @hw: pointer to the hardware info
15454  *
15455  * Set PCI subsystem device id either from a pci_dev structure or
15456  * a specific FW register.
15457  **/
15458 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15459 {
15460 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15461 
15462 	hw->subsystem_device_id = pdev->subsystem_device ?
15463 		pdev->subsystem_device :
15464 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15465 }
15466 
15467 /**
15468  * i40e_probe - Device initialization routine
15469  * @pdev: PCI device information struct
15470  * @ent: entry in i40e_pci_tbl
15471  *
15472  * i40e_probe initializes a PF identified by a pci_dev structure.
15473  * The OS initialization, configuring of the PF private structure,
15474  * and a hardware reset occur.
15475  *
15476  * Returns 0 on success, negative on failure
15477  **/
15478 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15479 {
15480 	struct i40e_aq_get_phy_abilities_resp abilities;
15481 #ifdef CONFIG_I40E_DCB
15482 	enum i40e_get_fw_lldp_status_resp lldp_status;
15483 	i40e_status status;
15484 #endif /* CONFIG_I40E_DCB */
15485 	struct i40e_pf *pf;
15486 	struct i40e_hw *hw;
15487 	static u16 pfs_found;
15488 	u16 wol_nvm_bits;
15489 	u16 link_status;
15490 	int err;
15491 	u32 val;
15492 	u32 i;
15493 
15494 	err = pci_enable_device_mem(pdev);
15495 	if (err)
15496 		return err;
15497 
15498 	/* set up for high or low dma */
15499 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15500 	if (err) {
15501 		dev_err(&pdev->dev,
15502 			"DMA configuration failed: 0x%x\n", err);
15503 		goto err_dma;
15504 	}
15505 
15506 	/* set up pci connections */
15507 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15508 	if (err) {
15509 		dev_info(&pdev->dev,
15510 			 "pci_request_selected_regions failed %d\n", err);
15511 		goto err_pci_reg;
15512 	}
15513 
15514 	pci_enable_pcie_error_reporting(pdev);
15515 	pci_set_master(pdev);
15516 
15517 	/* Now that we have a PCI connection, we need to do the
15518 	 * low level device setup.  This is primarily setting up
15519 	 * the Admin Queue structures and then querying for the
15520 	 * device's current profile information.
15521 	 */
15522 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15523 	if (!pf) {
15524 		err = -ENOMEM;
15525 		goto err_pf_alloc;
15526 	}
15527 	pf->next_vsi = 0;
15528 	pf->pdev = pdev;
15529 	set_bit(__I40E_DOWN, pf->state);
15530 
15531 	hw = &pf->hw;
15532 	hw->back = pf;
15533 
15534 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15535 				I40E_MAX_CSR_SPACE);
15536 	/* We believe that the highest register to read is
15537 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15538 	 * is not less than that before mapping to prevent a
15539 	 * kernel panic.
15540 	 */
15541 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15542 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15543 			pf->ioremap_len);
15544 		err = -ENOMEM;
15545 		goto err_ioremap;
15546 	}
15547 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15548 	if (!hw->hw_addr) {
15549 		err = -EIO;
15550 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15551 			 (unsigned int)pci_resource_start(pdev, 0),
15552 			 pf->ioremap_len, err);
15553 		goto err_ioremap;
15554 	}
15555 	hw->vendor_id = pdev->vendor;
15556 	hw->device_id = pdev->device;
15557 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15558 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15559 	i40e_set_subsystem_device_id(hw);
15560 	hw->bus.device = PCI_SLOT(pdev->devfn);
15561 	hw->bus.func = PCI_FUNC(pdev->devfn);
15562 	hw->bus.bus_id = pdev->bus->number;
15563 	pf->instance = pfs_found;
15564 
15565 	/* Select something other than the 802.1ad ethertype for the
15566 	 * switch to use internally and drop on ingress.
15567 	 */
15568 	hw->switch_tag = 0xffff;
15569 	hw->first_tag = ETH_P_8021AD;
15570 	hw->second_tag = ETH_P_8021Q;
15571 
15572 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15573 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15574 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15575 
15576 	/* set up the locks for the AQ, do this only once in probe
15577 	 * and destroy them only once in remove
15578 	 */
15579 	mutex_init(&hw->aq.asq_mutex);
15580 	mutex_init(&hw->aq.arq_mutex);
15581 
15582 	pf->msg_enable = netif_msg_init(debug,
15583 					NETIF_MSG_DRV |
15584 					NETIF_MSG_PROBE |
15585 					NETIF_MSG_LINK);
15586 	if (debug < -1)
15587 		pf->hw.debug_mask = debug;
15588 
15589 	/* do a special CORER for clearing PXE mode once at init */
15590 	if (hw->revision_id == 0 &&
15591 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15592 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15593 		i40e_flush(hw);
15594 		msleep(200);
15595 		pf->corer_count++;
15596 
15597 		i40e_clear_pxe_mode(hw);
15598 	}
15599 
15600 	/* Reset here to make sure all is clean and to define PF 'n' */
15601 	i40e_clear_hw(hw);
15602 
15603 	err = i40e_set_mac_type(hw);
15604 	if (err) {
15605 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15606 			 err);
15607 		goto err_pf_reset;
15608 	}
15609 
15610 	err = i40e_handle_resets(pf);
15611 	if (err)
15612 		goto err_pf_reset;
15613 
15614 	i40e_check_recovery_mode(pf);
15615 
15616 	if (is_kdump_kernel()) {
15617 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15618 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15619 	} else {
15620 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15621 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15622 	}
15623 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15624 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15625 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15626 
15627 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15628 		 "%s-%s:misc",
15629 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15630 
15631 	err = i40e_init_shared_code(hw);
15632 	if (err) {
15633 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15634 			 err);
15635 		goto err_pf_reset;
15636 	}
15637 
15638 	/* set up a default setting for link flow control */
15639 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15640 
15641 	err = i40e_init_adminq(hw);
15642 	if (err) {
15643 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15644 			dev_info(&pdev->dev,
15645 				 "The driver for the device stopped because the NVM image v%u.%u is newer than expected v%u.%u. You must install the most recent version of the network driver.\n",
15646 				 hw->aq.api_maj_ver,
15647 				 hw->aq.api_min_ver,
15648 				 I40E_FW_API_VERSION_MAJOR,
15649 				 I40E_FW_MINOR_VERSION(hw));
15650 		else
15651 			dev_info(&pdev->dev,
15652 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15653 
15654 		goto err_pf_reset;
15655 	}
15656 	i40e_get_oem_version(hw);
15657 
15658 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15659 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15660 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15661 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15662 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15663 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15664 
15665 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15666 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15667 		dev_dbg(&pdev->dev,
15668 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15669 			 hw->aq.api_maj_ver,
15670 			 hw->aq.api_min_ver,
15671 			 I40E_FW_API_VERSION_MAJOR,
15672 			 I40E_FW_MINOR_VERSION(hw));
15673 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15674 		dev_info(&pdev->dev,
15675 			 "The driver for the device detected an older version of the NVM image v%u.%u than expected v%u.%u. Please update the NVM image.\n",
15676 			 hw->aq.api_maj_ver,
15677 			 hw->aq.api_min_ver,
15678 			 I40E_FW_API_VERSION_MAJOR,
15679 			 I40E_FW_MINOR_VERSION(hw));
15680 
15681 	i40e_verify_eeprom(pf);
15682 
15683 	/* Rev 0 hardware was never productized */
15684 	if (hw->revision_id < 1)
15685 		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");
15686 
15687 	i40e_clear_pxe_mode(hw);
15688 
15689 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15690 	if (err)
15691 		goto err_adminq_setup;
15692 
15693 	err = i40e_sw_init(pf);
15694 	if (err) {
15695 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15696 		goto err_sw_init;
15697 	}
15698 
15699 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15700 		return i40e_init_recovery_mode(pf, hw);
15701 
15702 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15703 				hw->func_caps.num_rx_qp, 0, 0);
15704 	if (err) {
15705 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15706 		goto err_init_lan_hmc;
15707 	}
15708 
15709 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15710 	if (err) {
15711 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15712 		err = -ENOENT;
15713 		goto err_configure_lan_hmc;
15714 	}
15715 
15716 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15717 	 * Ignore error return codes because if it was already disabled via
15718 	 * hardware settings this will fail
15719 	 */
15720 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15721 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15722 		i40e_aq_stop_lldp(hw, true, false, NULL);
15723 	}
15724 
15725 	/* allow a platform config to override the HW addr */
15726 	i40e_get_platform_mac_addr(pdev, pf);
15727 
15728 	if (!is_valid_ether_addr(hw->mac.addr)) {
15729 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15730 		err = -EIO;
15731 		goto err_mac_addr;
15732 	}
15733 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15734 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15735 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15736 	if (is_valid_ether_addr(hw->mac.port_addr))
15737 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15738 
15739 	i40e_ptp_alloc_pins(pf);
15740 	pci_set_drvdata(pdev, pf);
15741 	pci_save_state(pdev);
15742 
15743 #ifdef CONFIG_I40E_DCB
15744 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15745 	(!status &&
15746 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15747 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15748 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15749 	dev_info(&pdev->dev,
15750 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15751 			"FW LLDP is disabled\n" :
15752 			"FW LLDP is enabled\n");
15753 
15754 	/* Enable FW to write default DCB config on link-up */
15755 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15756 
15757 	err = i40e_init_pf_dcb(pf);
15758 	if (err) {
15759 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15760 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15761 		/* Continue without DCB enabled */
15762 	}
15763 #endif /* CONFIG_I40E_DCB */
15764 
15765 	/* set up periodic task facility */
15766 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15767 	pf->service_timer_period = HZ;
15768 
15769 	INIT_WORK(&pf->service_task, i40e_service_task);
15770 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15771 
15772 	/* NVM bit on means WoL disabled for the port */
15773 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15774 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15775 		pf->wol_en = false;
15776 	else
15777 		pf->wol_en = true;
15778 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15779 
15780 	/* set up the main switch operations */
15781 	i40e_determine_queue_usage(pf);
15782 	err = i40e_init_interrupt_scheme(pf);
15783 	if (err)
15784 		goto err_switch_setup;
15785 
15786 	/* Reduce Tx and Rx pairs for kdump
15787 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15788 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15789 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15790 	 */
15791 	if (is_kdump_kernel())
15792 		pf->num_lan_msix = 1;
15793 
15794 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15795 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15796 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15797 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15798 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15799 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15800 						    UDP_TUNNEL_TYPE_GENEVE;
15801 
15802 	/* The number of VSIs reported by the FW is the minimum guaranteed
15803 	 * to us; HW supports far more and we share the remaining pool with
15804 	 * the other PFs. We allocate space for more than the guarantee with
15805 	 * the understanding that we might not get them all later.
15806 	 */
15807 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15808 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15809 	else
15810 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15811 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15812 		dev_warn(&pf->pdev->dev,
15813 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15814 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15815 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15816 	}
15817 
15818 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15819 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15820 			  GFP_KERNEL);
15821 	if (!pf->vsi) {
15822 		err = -ENOMEM;
15823 		goto err_switch_setup;
15824 	}
15825 
15826 #ifdef CONFIG_PCI_IOV
15827 	/* prep for VF support */
15828 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15829 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15830 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15831 		if (pci_num_vf(pdev))
15832 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15833 	}
15834 #endif
15835 	err = i40e_setup_pf_switch(pf, false, false);
15836 	if (err) {
15837 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15838 		goto err_vsis;
15839 	}
15840 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15841 
15842 	/* if FDIR VSI was set up, start it now */
15843 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15844 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15845 			i40e_vsi_open(pf->vsi[i]);
15846 			break;
15847 		}
15848 	}
15849 
15850 	/* The driver only wants link up/down and module qualification
15851 	 * reports from firmware.  Note the negative logic.
15852 	 */
15853 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15854 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15855 					 I40E_AQ_EVENT_MEDIA_NA |
15856 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15857 	if (err)
15858 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15859 			 i40e_stat_str(&pf->hw, err),
15860 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15861 
15862 	/* Reconfigure hardware for allowing smaller MSS in the case
15863 	 * of TSO, so that we avoid the MDD being fired and causing
15864 	 * a reset in the case of small MSS+TSO.
15865 	 */
15866 	val = rd32(hw, I40E_REG_MSS);
15867 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15868 		val &= ~I40E_REG_MSS_MIN_MASK;
15869 		val |= I40E_64BYTE_MSS;
15870 		wr32(hw, I40E_REG_MSS, val);
15871 	}
15872 
15873 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15874 		msleep(75);
15875 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15876 		if (err)
15877 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15878 				 i40e_stat_str(&pf->hw, err),
15879 				 i40e_aq_str(&pf->hw,
15880 					     pf->hw.aq.asq_last_status));
15881 	}
15882 	/* The main driver is (mostly) up and happy. We need to set this state
15883 	 * before setting up the misc vector or we get a race and the vector
15884 	 * ends up disabled forever.
15885 	 */
15886 	clear_bit(__I40E_DOWN, pf->state);
15887 
15888 	/* In case of MSIX we are going to setup the misc vector right here
15889 	 * to handle admin queue events etc. In case of legacy and MSI
15890 	 * the misc functionality and queue processing is combined in
15891 	 * the same vector and that gets setup at open.
15892 	 */
15893 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15894 		err = i40e_setup_misc_vector(pf);
15895 		if (err) {
15896 			dev_info(&pdev->dev,
15897 				 "setup of misc vector failed: %d\n", err);
15898 			i40e_cloud_filter_exit(pf);
15899 			i40e_fdir_teardown(pf);
15900 			goto err_vsis;
15901 		}
15902 	}
15903 
15904 #ifdef CONFIG_PCI_IOV
15905 	/* prep for VF support */
15906 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15907 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15908 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15909 		/* disable link interrupts for VFs */
15910 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15911 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15912 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15913 		i40e_flush(hw);
15914 
15915 		if (pci_num_vf(pdev)) {
15916 			dev_info(&pdev->dev,
15917 				 "Active VFs found, allocating resources.\n");
15918 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15919 			if (err)
15920 				dev_info(&pdev->dev,
15921 					 "Error %d allocating resources for existing VFs\n",
15922 					 err);
15923 		}
15924 	}
15925 #endif /* CONFIG_PCI_IOV */
15926 
15927 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15928 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15929 						      pf->num_iwarp_msix,
15930 						      I40E_IWARP_IRQ_PILE_ID);
15931 		if (pf->iwarp_base_vector < 0) {
15932 			dev_info(&pdev->dev,
15933 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15934 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15935 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15936 		}
15937 	}
15938 
15939 	i40e_dbg_pf_init(pf);
15940 
15941 	/* tell the firmware that we're starting */
15942 	i40e_send_version(pf);
15943 
15944 	/* since everything's happy, start the service_task timer */
15945 	mod_timer(&pf->service_timer,
15946 		  round_jiffies(jiffies + pf->service_timer_period));
15947 
15948 	/* add this PF to client device list and launch a client service task */
15949 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15950 		err = i40e_lan_add_device(pf);
15951 		if (err)
15952 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15953 				 err);
15954 	}
15955 
15956 #define PCI_SPEED_SIZE 8
15957 #define PCI_WIDTH_SIZE 8
15958 	/* Devices on the IOSF bus do not have this information
15959 	 * and will report PCI Gen 1 x 1 by default so don't bother
15960 	 * checking them.
15961 	 */
15962 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15963 		char speed[PCI_SPEED_SIZE] = "Unknown";
15964 		char width[PCI_WIDTH_SIZE] = "Unknown";
15965 
15966 		/* Get the negotiated link width and speed from PCI config
15967 		 * space
15968 		 */
15969 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15970 					  &link_status);
15971 
15972 		i40e_set_pci_config_data(hw, link_status);
15973 
15974 		switch (hw->bus.speed) {
15975 		case i40e_bus_speed_8000:
15976 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15977 		case i40e_bus_speed_5000:
15978 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15979 		case i40e_bus_speed_2500:
15980 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15981 		default:
15982 			break;
15983 		}
15984 		switch (hw->bus.width) {
15985 		case i40e_bus_width_pcie_x8:
15986 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15987 		case i40e_bus_width_pcie_x4:
15988 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15989 		case i40e_bus_width_pcie_x2:
15990 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15991 		case i40e_bus_width_pcie_x1:
15992 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15993 		default:
15994 			break;
15995 		}
15996 
15997 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15998 			 speed, width);
15999 
16000 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16001 		    hw->bus.speed < i40e_bus_speed_8000) {
16002 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16003 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16004 		}
16005 	}
16006 
16007 	/* get the requested speeds from the fw */
16008 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16009 	if (err)
16010 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16011 			i40e_stat_str(&pf->hw, err),
16012 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16013 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16014 
16015 	/* set the FEC config due to the board capabilities */
16016 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16017 
16018 	/* get the supported phy types from the fw */
16019 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16020 	if (err)
16021 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16022 			i40e_stat_str(&pf->hw, err),
16023 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16024 
16025 	/* make sure the MFS hasn't been set lower than the default */
16026 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16027 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16028 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16029 	if (val < MAX_FRAME_SIZE_DEFAULT)
16030 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16031 			 i, val);
16032 
16033 	/* Add a filter to drop all Flow control frames from any VSI from being
16034 	 * transmitted. By doing so we stop a malicious VF from sending out
16035 	 * PAUSE or PFC frames and potentially controlling traffic for other
16036 	 * PF/VF VSIs.
16037 	 * The FW can still send Flow control frames if enabled.
16038 	 */
16039 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16040 						       pf->main_vsi_seid);
16041 
16042 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16043 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16044 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16045 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16046 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16047 	/* print a string summarizing features */
16048 	i40e_print_features(pf);
16049 
16050 	return 0;
16051 
16052 	/* Unwind what we've done if something failed in the setup */
16053 err_vsis:
16054 	set_bit(__I40E_DOWN, pf->state);
16055 	i40e_clear_interrupt_scheme(pf);
16056 	kfree(pf->vsi);
16057 err_switch_setup:
16058 	i40e_reset_interrupt_capability(pf);
16059 	del_timer_sync(&pf->service_timer);
16060 err_mac_addr:
16061 err_configure_lan_hmc:
16062 	(void)i40e_shutdown_lan_hmc(hw);
16063 err_init_lan_hmc:
16064 	kfree(pf->qp_pile);
16065 err_sw_init:
16066 err_adminq_setup:
16067 err_pf_reset:
16068 	iounmap(hw->hw_addr);
16069 err_ioremap:
16070 	kfree(pf);
16071 err_pf_alloc:
16072 	pci_disable_pcie_error_reporting(pdev);
16073 	pci_release_mem_regions(pdev);
16074 err_pci_reg:
16075 err_dma:
16076 	pci_disable_device(pdev);
16077 	return err;
16078 }
16079 
16080 /**
16081  * i40e_remove - Device removal routine
16082  * @pdev: PCI device information struct
16083  *
16084  * i40e_remove is called by the PCI subsystem to alert the driver
16085  * that is should release a PCI device.  This could be caused by a
16086  * Hot-Plug event, or because the driver is going to be removed from
16087  * memory.
16088  **/
16089 static void i40e_remove(struct pci_dev *pdev)
16090 {
16091 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16092 	struct i40e_hw *hw = &pf->hw;
16093 	i40e_status ret_code;
16094 	int i;
16095 
16096 	i40e_dbg_pf_exit(pf);
16097 
16098 	i40e_ptp_stop(pf);
16099 
16100 	/* Disable RSS in hw */
16101 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16102 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16103 
16104 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16105 	 * flags, once they are set, i40e_rebuild should not be called as
16106 	 * i40e_prep_for_reset always returns early.
16107 	 */
16108 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16109 		usleep_range(1000, 2000);
16110 	set_bit(__I40E_IN_REMOVE, pf->state);
16111 
16112 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16113 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16114 		i40e_free_vfs(pf);
16115 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16116 	}
16117 	/* no more scheduling of any task */
16118 	set_bit(__I40E_SUSPENDED, pf->state);
16119 	set_bit(__I40E_DOWN, pf->state);
16120 	if (pf->service_timer.function)
16121 		del_timer_sync(&pf->service_timer);
16122 	if (pf->service_task.func)
16123 		cancel_work_sync(&pf->service_task);
16124 
16125 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16126 		struct i40e_vsi *vsi = pf->vsi[0];
16127 
16128 		/* We know that we have allocated only one vsi for this PF,
16129 		 * it was just for registering netdevice, so the interface
16130 		 * could be visible in the 'ifconfig' output
16131 		 */
16132 		unregister_netdev(vsi->netdev);
16133 		free_netdev(vsi->netdev);
16134 
16135 		goto unmap;
16136 	}
16137 
16138 	/* Client close must be called explicitly here because the timer
16139 	 * has been stopped.
16140 	 */
16141 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16142 
16143 	i40e_fdir_teardown(pf);
16144 
16145 	/* If there is a switch structure or any orphans, remove them.
16146 	 * This will leave only the PF's VSI remaining.
16147 	 */
16148 	for (i = 0; i < I40E_MAX_VEB; i++) {
16149 		if (!pf->veb[i])
16150 			continue;
16151 
16152 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16153 		    pf->veb[i]->uplink_seid == 0)
16154 			i40e_switch_branch_release(pf->veb[i]);
16155 	}
16156 
16157 	/* Now we can shutdown the PF's VSI, just before we kill
16158 	 * adminq and hmc.
16159 	 */
16160 	if (pf->vsi[pf->lan_vsi])
16161 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16162 
16163 	i40e_cloud_filter_exit(pf);
16164 
16165 	/* remove attached clients */
16166 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16167 		ret_code = i40e_lan_del_device(pf);
16168 		if (ret_code)
16169 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16170 				 ret_code);
16171 	}
16172 
16173 	/* shutdown and destroy the HMC */
16174 	if (hw->hmc.hmc_obj) {
16175 		ret_code = i40e_shutdown_lan_hmc(hw);
16176 		if (ret_code)
16177 			dev_warn(&pdev->dev,
16178 				 "Failed to destroy the HMC resources: %d\n",
16179 				 ret_code);
16180 	}
16181 
16182 unmap:
16183 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16184 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16185 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16186 		free_irq(pf->pdev->irq, pf);
16187 
16188 	/* shutdown the adminq */
16189 	i40e_shutdown_adminq(hw);
16190 
16191 	/* destroy the locks only once, here */
16192 	mutex_destroy(&hw->aq.arq_mutex);
16193 	mutex_destroy(&hw->aq.asq_mutex);
16194 
16195 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16196 	rtnl_lock();
16197 	i40e_clear_interrupt_scheme(pf);
16198 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16199 		if (pf->vsi[i]) {
16200 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16201 				i40e_vsi_clear_rings(pf->vsi[i]);
16202 			i40e_vsi_clear(pf->vsi[i]);
16203 			pf->vsi[i] = NULL;
16204 		}
16205 	}
16206 	rtnl_unlock();
16207 
16208 	for (i = 0; i < I40E_MAX_VEB; i++) {
16209 		kfree(pf->veb[i]);
16210 		pf->veb[i] = NULL;
16211 	}
16212 
16213 	kfree(pf->qp_pile);
16214 	kfree(pf->vsi);
16215 
16216 	iounmap(hw->hw_addr);
16217 	kfree(pf);
16218 	pci_release_mem_regions(pdev);
16219 
16220 	pci_disable_pcie_error_reporting(pdev);
16221 	pci_disable_device(pdev);
16222 }
16223 
16224 /**
16225  * i40e_pci_error_detected - warning that something funky happened in PCI land
16226  * @pdev: PCI device information struct
16227  * @error: the type of PCI error
16228  *
16229  * Called to warn that something happened and the error handling steps
16230  * are in progress.  Allows the driver to quiesce things, be ready for
16231  * remediation.
16232  **/
16233 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16234 						pci_channel_state_t error)
16235 {
16236 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16237 
16238 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16239 
16240 	if (!pf) {
16241 		dev_info(&pdev->dev,
16242 			 "Cannot recover - error happened during device probe\n");
16243 		return PCI_ERS_RESULT_DISCONNECT;
16244 	}
16245 
16246 	/* shutdown all operations */
16247 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16248 		i40e_prep_for_reset(pf);
16249 
16250 	/* Request a slot reset */
16251 	return PCI_ERS_RESULT_NEED_RESET;
16252 }
16253 
16254 /**
16255  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16256  * @pdev: PCI device information struct
16257  *
16258  * Called to find if the driver can work with the device now that
16259  * the pci slot has been reset.  If a basic connection seems good
16260  * (registers are readable and have sane content) then return a
16261  * happy little PCI_ERS_RESULT_xxx.
16262  **/
16263 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16264 {
16265 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16266 	pci_ers_result_t result;
16267 	u32 reg;
16268 
16269 	dev_dbg(&pdev->dev, "%s\n", __func__);
16270 	if (pci_enable_device_mem(pdev)) {
16271 		dev_info(&pdev->dev,
16272 			 "Cannot re-enable PCI device after reset.\n");
16273 		result = PCI_ERS_RESULT_DISCONNECT;
16274 	} else {
16275 		pci_set_master(pdev);
16276 		pci_restore_state(pdev);
16277 		pci_save_state(pdev);
16278 		pci_wake_from_d3(pdev, false);
16279 
16280 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16281 		if (reg == 0)
16282 			result = PCI_ERS_RESULT_RECOVERED;
16283 		else
16284 			result = PCI_ERS_RESULT_DISCONNECT;
16285 	}
16286 
16287 	return result;
16288 }
16289 
16290 /**
16291  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16292  * @pdev: PCI device information struct
16293  */
16294 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16295 {
16296 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16297 
16298 	i40e_prep_for_reset(pf);
16299 }
16300 
16301 /**
16302  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16303  * @pdev: PCI device information struct
16304  */
16305 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16306 {
16307 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16308 
16309 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16310 		return;
16311 
16312 	i40e_reset_and_rebuild(pf, false, false);
16313 }
16314 
16315 /**
16316  * i40e_pci_error_resume - restart operations after PCI error recovery
16317  * @pdev: PCI device information struct
16318  *
16319  * Called to allow the driver to bring things back up after PCI error
16320  * and/or reset recovery has finished.
16321  **/
16322 static void i40e_pci_error_resume(struct pci_dev *pdev)
16323 {
16324 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16325 
16326 	dev_dbg(&pdev->dev, "%s\n", __func__);
16327 	if (test_bit(__I40E_SUSPENDED, pf->state))
16328 		return;
16329 
16330 	i40e_handle_reset_warning(pf, false);
16331 }
16332 
16333 /**
16334  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16335  * using the mac_address_write admin q function
16336  * @pf: pointer to i40e_pf struct
16337  **/
16338 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16339 {
16340 	struct i40e_hw *hw = &pf->hw;
16341 	i40e_status ret;
16342 	u8 mac_addr[6];
16343 	u16 flags = 0;
16344 
16345 	/* Get current MAC address in case it's an LAA */
16346 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16347 		ether_addr_copy(mac_addr,
16348 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16349 	} else {
16350 		dev_err(&pf->pdev->dev,
16351 			"Failed to retrieve MAC address; using default\n");
16352 		ether_addr_copy(mac_addr, hw->mac.addr);
16353 	}
16354 
16355 	/* The FW expects the mac address write cmd to first be called with
16356 	 * one of these flags before calling it again with the multicast
16357 	 * enable flags.
16358 	 */
16359 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16360 
16361 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16362 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16363 
16364 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16365 	if (ret) {
16366 		dev_err(&pf->pdev->dev,
16367 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16368 		return;
16369 	}
16370 
16371 	flags = I40E_AQC_MC_MAG_EN
16372 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16373 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16374 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16375 	if (ret)
16376 		dev_err(&pf->pdev->dev,
16377 			"Failed to enable Multicast Magic Packet wake up\n");
16378 }
16379 
16380 /**
16381  * i40e_shutdown - PCI callback for shutting down
16382  * @pdev: PCI device information struct
16383  **/
16384 static void i40e_shutdown(struct pci_dev *pdev)
16385 {
16386 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16387 	struct i40e_hw *hw = &pf->hw;
16388 
16389 	set_bit(__I40E_SUSPENDED, pf->state);
16390 	set_bit(__I40E_DOWN, pf->state);
16391 
16392 	del_timer_sync(&pf->service_timer);
16393 	cancel_work_sync(&pf->service_task);
16394 	i40e_cloud_filter_exit(pf);
16395 	i40e_fdir_teardown(pf);
16396 
16397 	/* Client close must be called explicitly here because the timer
16398 	 * has been stopped.
16399 	 */
16400 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16401 
16402 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16403 		i40e_enable_mc_magic_wake(pf);
16404 
16405 	i40e_prep_for_reset(pf);
16406 
16407 	wr32(hw, I40E_PFPM_APM,
16408 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16409 	wr32(hw, I40E_PFPM_WUFC,
16410 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16411 
16412 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16413 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16414 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16415 		free_irq(pf->pdev->irq, pf);
16416 
16417 	/* Since we're going to destroy queues during the
16418 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16419 	 * whole section
16420 	 */
16421 	rtnl_lock();
16422 	i40e_clear_interrupt_scheme(pf);
16423 	rtnl_unlock();
16424 
16425 	if (system_state == SYSTEM_POWER_OFF) {
16426 		pci_wake_from_d3(pdev, pf->wol_en);
16427 		pci_set_power_state(pdev, PCI_D3hot);
16428 	}
16429 }
16430 
16431 /**
16432  * i40e_suspend - PM callback for moving to D3
16433  * @dev: generic device information structure
16434  **/
16435 static int __maybe_unused i40e_suspend(struct device *dev)
16436 {
16437 	struct i40e_pf *pf = dev_get_drvdata(dev);
16438 	struct i40e_hw *hw = &pf->hw;
16439 
16440 	/* If we're already suspended, then there is nothing to do */
16441 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16442 		return 0;
16443 
16444 	set_bit(__I40E_DOWN, pf->state);
16445 
16446 	/* Ensure service task will not be running */
16447 	del_timer_sync(&pf->service_timer);
16448 	cancel_work_sync(&pf->service_task);
16449 
16450 	/* Client close must be called explicitly here because the timer
16451 	 * has been stopped.
16452 	 */
16453 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16454 
16455 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16456 		i40e_enable_mc_magic_wake(pf);
16457 
16458 	/* Since we're going to destroy queues during the
16459 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16460 	 * whole section
16461 	 */
16462 	rtnl_lock();
16463 
16464 	i40e_prep_for_reset(pf);
16465 
16466 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16467 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16468 
16469 	/* Clear the interrupt scheme and release our IRQs so that the system
16470 	 * can safely hibernate even when there are a large number of CPUs.
16471 	 * Otherwise hibernation might fail when mapping all the vectors back
16472 	 * to CPU0.
16473 	 */
16474 	i40e_clear_interrupt_scheme(pf);
16475 
16476 	rtnl_unlock();
16477 
16478 	return 0;
16479 }
16480 
16481 /**
16482  * i40e_resume - PM callback for waking up from D3
16483  * @dev: generic device information structure
16484  **/
16485 static int __maybe_unused i40e_resume(struct device *dev)
16486 {
16487 	struct i40e_pf *pf = dev_get_drvdata(dev);
16488 	int err;
16489 
16490 	/* If we're not suspended, then there is nothing to do */
16491 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16492 		return 0;
16493 
16494 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16495 	 * since we're going to be restoring queues
16496 	 */
16497 	rtnl_lock();
16498 
16499 	/* We cleared the interrupt scheme when we suspended, so we need to
16500 	 * restore it now to resume device functionality.
16501 	 */
16502 	err = i40e_restore_interrupt_scheme(pf);
16503 	if (err) {
16504 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16505 			err);
16506 	}
16507 
16508 	clear_bit(__I40E_DOWN, pf->state);
16509 	i40e_reset_and_rebuild(pf, false, true);
16510 
16511 	rtnl_unlock();
16512 
16513 	/* Clear suspended state last after everything is recovered */
16514 	clear_bit(__I40E_SUSPENDED, pf->state);
16515 
16516 	/* Restart the service task */
16517 	mod_timer(&pf->service_timer,
16518 		  round_jiffies(jiffies + pf->service_timer_period));
16519 
16520 	return 0;
16521 }
16522 
16523 static const struct pci_error_handlers i40e_err_handler = {
16524 	.error_detected = i40e_pci_error_detected,
16525 	.slot_reset = i40e_pci_error_slot_reset,
16526 	.reset_prepare = i40e_pci_error_reset_prepare,
16527 	.reset_done = i40e_pci_error_reset_done,
16528 	.resume = i40e_pci_error_resume,
16529 };
16530 
16531 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16532 
16533 static struct pci_driver i40e_driver = {
16534 	.name     = i40e_driver_name,
16535 	.id_table = i40e_pci_tbl,
16536 	.probe    = i40e_probe,
16537 	.remove   = i40e_remove,
16538 	.driver   = {
16539 		.pm = &i40e_pm_ops,
16540 	},
16541 	.shutdown = i40e_shutdown,
16542 	.err_handler = &i40e_err_handler,
16543 	.sriov_configure = i40e_pci_sriov_configure,
16544 };
16545 
16546 /**
16547  * i40e_init_module - Driver registration routine
16548  *
16549  * i40e_init_module is the first routine called when the driver is
16550  * loaded. All it does is register with the PCI subsystem.
16551  **/
16552 static int __init i40e_init_module(void)
16553 {
16554 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16555 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16556 
16557 	/* There is no need to throttle the number of active tasks because
16558 	 * each device limits its own task using a state bit for scheduling
16559 	 * the service task, and the device tasks do not interfere with each
16560 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16561 	 * since we need to be able to guarantee forward progress even under
16562 	 * memory pressure.
16563 	 */
16564 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16565 	if (!i40e_wq) {
16566 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16567 		return -ENOMEM;
16568 	}
16569 
16570 	i40e_dbg_init();
16571 	return pci_register_driver(&i40e_driver);
16572 }
16573 module_init(i40e_init_module);
16574 
16575 /**
16576  * i40e_exit_module - Driver exit cleanup routine
16577  *
16578  * i40e_exit_module is called just before the driver is removed
16579  * from memory.
16580  **/
16581 static void __exit i40e_exit_module(void)
16582 {
16583 	pci_unregister_driver(&i40e_driver);
16584 	destroy_workqueue(i40e_wq);
16585 	ida_destroy(&i40e_client_ida);
16586 	i40e_dbg_exit();
16587 }
16588 module_exit(i40e_exit_module);
16589