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_20G_KR2), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
86 	/* required last entry */
87 	{0, }
88 };
89 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
90 
91 #define I40E_MAX_VF_COUNT 128
92 static int debug = -1;
93 module_param(debug, uint, 0);
94 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
95 
96 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
97 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
98 MODULE_LICENSE("GPL v2");
99 
100 static struct workqueue_struct *i40e_wq;
101 
102 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
103 				  struct net_device *netdev, int delta)
104 {
105 	struct netdev_hw_addr *ha;
106 
107 	if (!f || !netdev)
108 		return;
109 
110 	netdev_for_each_mc_addr(ha, netdev) {
111 		if (ether_addr_equal(ha->addr, f->macaddr)) {
112 			ha->refcount += delta;
113 			if (ha->refcount <= 0)
114 				ha->refcount = 1;
115 			break;
116 		}
117 	}
118 }
119 
120 /**
121  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
122  * @hw:   pointer to the HW structure
123  * @mem:  ptr to mem struct to fill out
124  * @size: size of memory requested
125  * @alignment: what to align the allocation to
126  **/
127 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
128 			    u64 size, u32 alignment)
129 {
130 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
131 
132 	mem->size = ALIGN(size, alignment);
133 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
134 				     GFP_KERNEL);
135 	if (!mem->va)
136 		return -ENOMEM;
137 
138 	return 0;
139 }
140 
141 /**
142  * i40e_free_dma_mem_d - OS specific memory free for shared code
143  * @hw:   pointer to the HW structure
144  * @mem:  ptr to mem struct to free
145  **/
146 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
147 {
148 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
149 
150 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
151 	mem->va = NULL;
152 	mem->pa = 0;
153 	mem->size = 0;
154 
155 	return 0;
156 }
157 
158 /**
159  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
160  * @hw:   pointer to the HW structure
161  * @mem:  ptr to mem struct to fill out
162  * @size: size of memory requested
163  **/
164 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
165 			     u32 size)
166 {
167 	mem->size = size;
168 	mem->va = kzalloc(size, GFP_KERNEL);
169 
170 	if (!mem->va)
171 		return -ENOMEM;
172 
173 	return 0;
174 }
175 
176 /**
177  * i40e_free_virt_mem_d - OS specific memory free for shared code
178  * @hw:   pointer to the HW structure
179  * @mem:  ptr to mem struct to free
180  **/
181 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
182 {
183 	/* it's ok to kfree a NULL pointer */
184 	kfree(mem->va);
185 	mem->va = NULL;
186 	mem->size = 0;
187 
188 	return 0;
189 }
190 
191 /**
192  * i40e_get_lump - find a lump of free generic resource
193  * @pf: board private structure
194  * @pile: the pile of resource to search
195  * @needed: the number of items needed
196  * @id: an owner id to stick on the items assigned
197  *
198  * Returns the base item index of the lump, or negative for error
199  **/
200 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
201 			 u16 needed, u16 id)
202 {
203 	int ret = -ENOMEM;
204 	int i, j;
205 
206 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
207 		dev_info(&pf->pdev->dev,
208 			 "param err: pile=%s needed=%d id=0x%04x\n",
209 			 pile ? "<valid>" : "<null>", needed, id);
210 		return -EINVAL;
211 	}
212 
213 	/* Allocate last queue in the pile for FDIR VSI queue
214 	 * so it doesn't fragment the qp_pile
215 	 */
216 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
217 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
218 			dev_err(&pf->pdev->dev,
219 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
220 				pile->num_entries - 1);
221 			return -ENOMEM;
222 		}
223 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
224 		return pile->num_entries - 1;
225 	}
226 
227 	i = 0;
228 	while (i < pile->num_entries) {
229 		/* skip already allocated entries */
230 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
231 			i++;
232 			continue;
233 		}
234 
235 		/* do we have enough in this lump? */
236 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
237 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
238 				break;
239 		}
240 
241 		if (j == needed) {
242 			/* there was enough, so assign it to the requestor */
243 			for (j = 0; j < needed; j++)
244 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
245 			ret = i;
246 			break;
247 		}
248 
249 		/* not enough, so skip over it and continue looking */
250 		i += j;
251 	}
252 
253 	return ret;
254 }
255 
256 /**
257  * i40e_put_lump - return a lump of generic resource
258  * @pile: the pile of resource to search
259  * @index: the base item index
260  * @id: the owner id of the items assigned
261  *
262  * Returns the count of items in the lump
263  **/
264 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
265 {
266 	int valid_id = (id | I40E_PILE_VALID_BIT);
267 	int count = 0;
268 	u16 i;
269 
270 	if (!pile || index >= pile->num_entries)
271 		return -EINVAL;
272 
273 	for (i = index;
274 	     i < pile->num_entries && pile->list[i] == valid_id;
275 	     i++) {
276 		pile->list[i] = 0;
277 		count++;
278 	}
279 
280 
281 	return count;
282 }
283 
284 /**
285  * i40e_find_vsi_from_id - searches for the vsi with the given id
286  * @pf: the pf structure to search for the vsi
287  * @id: id of the vsi it is searching for
288  **/
289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290 {
291 	int i;
292 
293 	for (i = 0; i < pf->num_alloc_vsi; i++)
294 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 			return pf->vsi[i];
296 
297 	return NULL;
298 }
299 
300 /**
301  * i40e_service_event_schedule - Schedule the service task to wake up
302  * @pf: board private structure
303  *
304  * If not already scheduled, this puts the task into the work queue
305  **/
306 void i40e_service_event_schedule(struct i40e_pf *pf)
307 {
308 	if ((!test_bit(__I40E_DOWN, pf->state) &&
309 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
310 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
311 		queue_work(i40e_wq, &pf->service_task);
312 }
313 
314 /**
315  * i40e_tx_timeout - Respond to a Tx Hang
316  * @netdev: network interface device structure
317  * @txqueue: queue number timing out
318  *
319  * If any port has noticed a Tx timeout, it is likely that the whole
320  * device is munged, not just the one netdev port, so go for the full
321  * reset.
322  **/
323 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
324 {
325 	struct i40e_netdev_priv *np = netdev_priv(netdev);
326 	struct i40e_vsi *vsi = np->vsi;
327 	struct i40e_pf *pf = vsi->back;
328 	struct i40e_ring *tx_ring = NULL;
329 	unsigned int i;
330 	u32 head, val;
331 
332 	pf->tx_timeout_count++;
333 
334 	/* with txqueue index, find the tx_ring struct */
335 	for (i = 0; i < vsi->num_queue_pairs; i++) {
336 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
337 			if (txqueue ==
338 			    vsi->tx_rings[i]->queue_index) {
339 				tx_ring = vsi->tx_rings[i];
340 				break;
341 			}
342 		}
343 	}
344 
345 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
346 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
347 	else if (time_before(jiffies,
348 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
349 		return;   /* don't do any new action before the next timeout */
350 
351 	/* don't kick off another recovery if one is already pending */
352 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
353 		return;
354 
355 	if (tx_ring) {
356 		head = i40e_get_head(tx_ring);
357 		/* Read interrupt register */
358 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
359 			val = rd32(&pf->hw,
360 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
361 						tx_ring->vsi->base_vector - 1));
362 		else
363 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
364 
365 		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",
366 			    vsi->seid, txqueue, tx_ring->next_to_clean,
367 			    head, tx_ring->next_to_use,
368 			    readl(tx_ring->tail), val);
369 	}
370 
371 	pf->tx_timeout_last_recovery = jiffies;
372 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
373 		    pf->tx_timeout_recovery_level, txqueue);
374 
375 	switch (pf->tx_timeout_recovery_level) {
376 	case 1:
377 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
378 		break;
379 	case 2:
380 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
381 		break;
382 	case 3:
383 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
384 		break;
385 	default:
386 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
387 		break;
388 	}
389 
390 	i40e_service_event_schedule(pf);
391 	pf->tx_timeout_recovery_level++;
392 }
393 
394 /**
395  * i40e_get_vsi_stats_struct - Get System Network Statistics
396  * @vsi: the VSI we care about
397  *
398  * Returns the address of the device statistics structure.
399  * The statistics are actually updated from the service task.
400  **/
401 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
402 {
403 	return &vsi->net_stats;
404 }
405 
406 /**
407  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
408  * @ring: Tx ring to get statistics from
409  * @stats: statistics entry to be updated
410  **/
411 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
412 					    struct rtnl_link_stats64 *stats)
413 {
414 	u64 bytes, packets;
415 	unsigned int start;
416 
417 	do {
418 		start = u64_stats_fetch_begin_irq(&ring->syncp);
419 		packets = ring->stats.packets;
420 		bytes   = ring->stats.bytes;
421 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
422 
423 	stats->tx_packets += packets;
424 	stats->tx_bytes   += bytes;
425 }
426 
427 /**
428  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
429  * @netdev: network interface device structure
430  * @stats: data structure to store statistics
431  *
432  * Returns the address of the device statistics structure.
433  * The statistics are actually updated from the service task.
434  **/
435 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
436 				  struct rtnl_link_stats64 *stats)
437 {
438 	struct i40e_netdev_priv *np = netdev_priv(netdev);
439 	struct i40e_vsi *vsi = np->vsi;
440 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
441 	struct i40e_ring *ring;
442 	int i;
443 
444 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
445 		return;
446 
447 	if (!vsi->tx_rings)
448 		return;
449 
450 	rcu_read_lock();
451 	for (i = 0; i < vsi->num_queue_pairs; i++) {
452 		u64 bytes, packets;
453 		unsigned int start;
454 
455 		ring = READ_ONCE(vsi->tx_rings[i]);
456 		if (!ring)
457 			continue;
458 		i40e_get_netdev_stats_struct_tx(ring, stats);
459 
460 		if (i40e_enabled_xdp_vsi(vsi)) {
461 			ring = READ_ONCE(vsi->xdp_rings[i]);
462 			if (!ring)
463 				continue;
464 			i40e_get_netdev_stats_struct_tx(ring, stats);
465 		}
466 
467 		ring = READ_ONCE(vsi->rx_rings[i]);
468 		if (!ring)
469 			continue;
470 		do {
471 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
472 			packets = ring->stats.packets;
473 			bytes   = ring->stats.bytes;
474 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
475 
476 		stats->rx_packets += packets;
477 		stats->rx_bytes   += bytes;
478 
479 	}
480 	rcu_read_unlock();
481 
482 	/* following stats updated by i40e_watchdog_subtask() */
483 	stats->multicast	= vsi_stats->multicast;
484 	stats->tx_errors	= vsi_stats->tx_errors;
485 	stats->tx_dropped	= vsi_stats->tx_dropped;
486 	stats->rx_errors	= vsi_stats->rx_errors;
487 	stats->rx_dropped	= vsi_stats->rx_dropped;
488 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
489 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
490 }
491 
492 /**
493  * i40e_vsi_reset_stats - Resets all stats of the given vsi
494  * @vsi: the VSI to have its stats reset
495  **/
496 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
497 {
498 	struct rtnl_link_stats64 *ns;
499 	int i;
500 
501 	if (!vsi)
502 		return;
503 
504 	ns = i40e_get_vsi_stats_struct(vsi);
505 	memset(ns, 0, sizeof(*ns));
506 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
507 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
508 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
509 	if (vsi->rx_rings && vsi->rx_rings[0]) {
510 		for (i = 0; i < vsi->num_queue_pairs; i++) {
511 			memset(&vsi->rx_rings[i]->stats, 0,
512 			       sizeof(vsi->rx_rings[i]->stats));
513 			memset(&vsi->rx_rings[i]->rx_stats, 0,
514 			       sizeof(vsi->rx_rings[i]->rx_stats));
515 			memset(&vsi->tx_rings[i]->stats, 0,
516 			       sizeof(vsi->tx_rings[i]->stats));
517 			memset(&vsi->tx_rings[i]->tx_stats, 0,
518 			       sizeof(vsi->tx_rings[i]->tx_stats));
519 		}
520 	}
521 	vsi->stat_offsets_loaded = false;
522 }
523 
524 /**
525  * i40e_pf_reset_stats - Reset all of the stats for the given PF
526  * @pf: the PF to be reset
527  **/
528 void i40e_pf_reset_stats(struct i40e_pf *pf)
529 {
530 	int i;
531 
532 	memset(&pf->stats, 0, sizeof(pf->stats));
533 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
534 	pf->stat_offsets_loaded = false;
535 
536 	for (i = 0; i < I40E_MAX_VEB; i++) {
537 		if (pf->veb[i]) {
538 			memset(&pf->veb[i]->stats, 0,
539 			       sizeof(pf->veb[i]->stats));
540 			memset(&pf->veb[i]->stats_offsets, 0,
541 			       sizeof(pf->veb[i]->stats_offsets));
542 			memset(&pf->veb[i]->tc_stats, 0,
543 			       sizeof(pf->veb[i]->tc_stats));
544 			memset(&pf->veb[i]->tc_stats_offsets, 0,
545 			       sizeof(pf->veb[i]->tc_stats_offsets));
546 			pf->veb[i]->stat_offsets_loaded = false;
547 		}
548 	}
549 	pf->hw_csum_rx_error = 0;
550 }
551 
552 /**
553  * i40e_stat_update48 - read and update a 48 bit stat from the chip
554  * @hw: ptr to the hardware info
555  * @hireg: the high 32 bit reg to read
556  * @loreg: the low 32 bit reg to read
557  * @offset_loaded: has the initial offset been loaded yet
558  * @offset: ptr to current offset value
559  * @stat: ptr to the stat
560  *
561  * Since the device stats are not reset at PFReset, they likely will not
562  * be zeroed when the driver starts.  We'll save the first values read
563  * and use them as offsets to be subtracted from the raw values in order
564  * to report stats that count from zero.  In the process, we also manage
565  * the potential roll-over.
566  **/
567 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
568 			       bool offset_loaded, u64 *offset, u64 *stat)
569 {
570 	u64 new_data;
571 
572 	if (hw->device_id == I40E_DEV_ID_QEMU) {
573 		new_data = rd32(hw, loreg);
574 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
575 	} else {
576 		new_data = rd64(hw, loreg);
577 	}
578 	if (!offset_loaded)
579 		*offset = new_data;
580 	if (likely(new_data >= *offset))
581 		*stat = new_data - *offset;
582 	else
583 		*stat = (new_data + BIT_ULL(48)) - *offset;
584 	*stat &= 0xFFFFFFFFFFFFULL;
585 }
586 
587 /**
588  * i40e_stat_update32 - read and update a 32 bit stat from the chip
589  * @hw: ptr to the hardware info
590  * @reg: the hw reg to read
591  * @offset_loaded: has the initial offset been loaded yet
592  * @offset: ptr to current offset value
593  * @stat: ptr to the stat
594  **/
595 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
596 			       bool offset_loaded, u64 *offset, u64 *stat)
597 {
598 	u32 new_data;
599 
600 	new_data = rd32(hw, reg);
601 	if (!offset_loaded)
602 		*offset = new_data;
603 	if (likely(new_data >= *offset))
604 		*stat = (u32)(new_data - *offset);
605 	else
606 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
607 }
608 
609 /**
610  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
611  * @hw: ptr to the hardware info
612  * @reg: the hw reg to read and clear
613  * @stat: ptr to the stat
614  **/
615 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
616 {
617 	u32 new_data = rd32(hw, reg);
618 
619 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
620 	*stat += new_data;
621 }
622 
623 /**
624  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
625  * @vsi: the VSI to be updated
626  **/
627 void i40e_update_eth_stats(struct i40e_vsi *vsi)
628 {
629 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
630 	struct i40e_pf *pf = vsi->back;
631 	struct i40e_hw *hw = &pf->hw;
632 	struct i40e_eth_stats *oes;
633 	struct i40e_eth_stats *es;     /* device's eth stats */
634 
635 	es = &vsi->eth_stats;
636 	oes = &vsi->eth_stats_offsets;
637 
638 	/* Gather up the stats that the hw collects */
639 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
640 			   vsi->stat_offsets_loaded,
641 			   &oes->tx_errors, &es->tx_errors);
642 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
643 			   vsi->stat_offsets_loaded,
644 			   &oes->rx_discards, &es->rx_discards);
645 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
646 			   vsi->stat_offsets_loaded,
647 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
648 
649 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
650 			   I40E_GLV_GORCL(stat_idx),
651 			   vsi->stat_offsets_loaded,
652 			   &oes->rx_bytes, &es->rx_bytes);
653 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
654 			   I40E_GLV_UPRCL(stat_idx),
655 			   vsi->stat_offsets_loaded,
656 			   &oes->rx_unicast, &es->rx_unicast);
657 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
658 			   I40E_GLV_MPRCL(stat_idx),
659 			   vsi->stat_offsets_loaded,
660 			   &oes->rx_multicast, &es->rx_multicast);
661 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
662 			   I40E_GLV_BPRCL(stat_idx),
663 			   vsi->stat_offsets_loaded,
664 			   &oes->rx_broadcast, &es->rx_broadcast);
665 
666 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
667 			   I40E_GLV_GOTCL(stat_idx),
668 			   vsi->stat_offsets_loaded,
669 			   &oes->tx_bytes, &es->tx_bytes);
670 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
671 			   I40E_GLV_UPTCL(stat_idx),
672 			   vsi->stat_offsets_loaded,
673 			   &oes->tx_unicast, &es->tx_unicast);
674 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
675 			   I40E_GLV_MPTCL(stat_idx),
676 			   vsi->stat_offsets_loaded,
677 			   &oes->tx_multicast, &es->tx_multicast);
678 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
679 			   I40E_GLV_BPTCL(stat_idx),
680 			   vsi->stat_offsets_loaded,
681 			   &oes->tx_broadcast, &es->tx_broadcast);
682 	vsi->stat_offsets_loaded = true;
683 }
684 
685 /**
686  * i40e_update_veb_stats - Update Switch component statistics
687  * @veb: the VEB being updated
688  **/
689 void i40e_update_veb_stats(struct i40e_veb *veb)
690 {
691 	struct i40e_pf *pf = veb->pf;
692 	struct i40e_hw *hw = &pf->hw;
693 	struct i40e_eth_stats *oes;
694 	struct i40e_eth_stats *es;     /* device's eth stats */
695 	struct i40e_veb_tc_stats *veb_oes;
696 	struct i40e_veb_tc_stats *veb_es;
697 	int i, idx = 0;
698 
699 	idx = veb->stats_idx;
700 	es = &veb->stats;
701 	oes = &veb->stats_offsets;
702 	veb_es = &veb->tc_stats;
703 	veb_oes = &veb->tc_stats_offsets;
704 
705 	/* Gather up the stats that the hw collects */
706 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
707 			   veb->stat_offsets_loaded,
708 			   &oes->tx_discards, &es->tx_discards);
709 	if (hw->revision_id > 0)
710 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
711 				   veb->stat_offsets_loaded,
712 				   &oes->rx_unknown_protocol,
713 				   &es->rx_unknown_protocol);
714 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
715 			   veb->stat_offsets_loaded,
716 			   &oes->rx_bytes, &es->rx_bytes);
717 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
718 			   veb->stat_offsets_loaded,
719 			   &oes->rx_unicast, &es->rx_unicast);
720 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
721 			   veb->stat_offsets_loaded,
722 			   &oes->rx_multicast, &es->rx_multicast);
723 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
724 			   veb->stat_offsets_loaded,
725 			   &oes->rx_broadcast, &es->rx_broadcast);
726 
727 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
728 			   veb->stat_offsets_loaded,
729 			   &oes->tx_bytes, &es->tx_bytes);
730 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
731 			   veb->stat_offsets_loaded,
732 			   &oes->tx_unicast, &es->tx_unicast);
733 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
734 			   veb->stat_offsets_loaded,
735 			   &oes->tx_multicast, &es->tx_multicast);
736 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
737 			   veb->stat_offsets_loaded,
738 			   &oes->tx_broadcast, &es->tx_broadcast);
739 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
740 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
741 				   I40E_GLVEBTC_RPCL(i, idx),
742 				   veb->stat_offsets_loaded,
743 				   &veb_oes->tc_rx_packets[i],
744 				   &veb_es->tc_rx_packets[i]);
745 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
746 				   I40E_GLVEBTC_RBCL(i, idx),
747 				   veb->stat_offsets_loaded,
748 				   &veb_oes->tc_rx_bytes[i],
749 				   &veb_es->tc_rx_bytes[i]);
750 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
751 				   I40E_GLVEBTC_TPCL(i, idx),
752 				   veb->stat_offsets_loaded,
753 				   &veb_oes->tc_tx_packets[i],
754 				   &veb_es->tc_tx_packets[i]);
755 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
756 				   I40E_GLVEBTC_TBCL(i, idx),
757 				   veb->stat_offsets_loaded,
758 				   &veb_oes->tc_tx_bytes[i],
759 				   &veb_es->tc_tx_bytes[i]);
760 	}
761 	veb->stat_offsets_loaded = true;
762 }
763 
764 /**
765  * i40e_update_vsi_stats - Update the vsi statistics counters.
766  * @vsi: the VSI to be updated
767  *
768  * There are a few instances where we store the same stat in a
769  * couple of different structs.  This is partly because we have
770  * the netdev stats that need to be filled out, which is slightly
771  * different from the "eth_stats" defined by the chip and used in
772  * VF communications.  We sort it out here.
773  **/
774 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
775 {
776 	struct i40e_pf *pf = vsi->back;
777 	struct rtnl_link_stats64 *ons;
778 	struct rtnl_link_stats64 *ns;   /* netdev stats */
779 	struct i40e_eth_stats *oes;
780 	struct i40e_eth_stats *es;     /* device's eth stats */
781 	u64 tx_restart, tx_busy;
782 	struct i40e_ring *p;
783 	u64 rx_page, rx_buf;
784 	u64 bytes, packets;
785 	unsigned int start;
786 	u64 tx_linearize;
787 	u64 tx_force_wb;
788 	u64 rx_p, rx_b;
789 	u64 tx_p, tx_b;
790 	u16 q;
791 
792 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
793 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
794 		return;
795 
796 	ns = i40e_get_vsi_stats_struct(vsi);
797 	ons = &vsi->net_stats_offsets;
798 	es = &vsi->eth_stats;
799 	oes = &vsi->eth_stats_offsets;
800 
801 	/* Gather up the netdev and vsi stats that the driver collects
802 	 * on the fly during packet processing
803 	 */
804 	rx_b = rx_p = 0;
805 	tx_b = tx_p = 0;
806 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
807 	rx_page = 0;
808 	rx_buf = 0;
809 	rcu_read_lock();
810 	for (q = 0; q < vsi->num_queue_pairs; q++) {
811 		/* locate Tx ring */
812 		p = READ_ONCE(vsi->tx_rings[q]);
813 		if (!p)
814 			continue;
815 
816 		do {
817 			start = u64_stats_fetch_begin_irq(&p->syncp);
818 			packets = p->stats.packets;
819 			bytes = p->stats.bytes;
820 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
821 		tx_b += bytes;
822 		tx_p += packets;
823 		tx_restart += p->tx_stats.restart_queue;
824 		tx_busy += p->tx_stats.tx_busy;
825 		tx_linearize += p->tx_stats.tx_linearize;
826 		tx_force_wb += p->tx_stats.tx_force_wb;
827 
828 		/* locate Rx ring */
829 		p = READ_ONCE(vsi->rx_rings[q]);
830 		if (!p)
831 			continue;
832 
833 		do {
834 			start = u64_stats_fetch_begin_irq(&p->syncp);
835 			packets = p->stats.packets;
836 			bytes = p->stats.bytes;
837 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
838 		rx_b += bytes;
839 		rx_p += packets;
840 		rx_buf += p->rx_stats.alloc_buff_failed;
841 		rx_page += p->rx_stats.alloc_page_failed;
842 
843 		if (i40e_enabled_xdp_vsi(vsi)) {
844 			/* locate XDP ring */
845 			p = READ_ONCE(vsi->xdp_rings[q]);
846 			if (!p)
847 				continue;
848 
849 			do {
850 				start = u64_stats_fetch_begin_irq(&p->syncp);
851 				packets = p->stats.packets;
852 				bytes = p->stats.bytes;
853 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
854 			tx_b += bytes;
855 			tx_p += packets;
856 			tx_restart += p->tx_stats.restart_queue;
857 			tx_busy += p->tx_stats.tx_busy;
858 			tx_linearize += p->tx_stats.tx_linearize;
859 			tx_force_wb += p->tx_stats.tx_force_wb;
860 		}
861 	}
862 	rcu_read_unlock();
863 	vsi->tx_restart = tx_restart;
864 	vsi->tx_busy = tx_busy;
865 	vsi->tx_linearize = tx_linearize;
866 	vsi->tx_force_wb = tx_force_wb;
867 	vsi->rx_page_failed = rx_page;
868 	vsi->rx_buf_failed = rx_buf;
869 
870 	ns->rx_packets = rx_p;
871 	ns->rx_bytes = rx_b;
872 	ns->tx_packets = tx_p;
873 	ns->tx_bytes = tx_b;
874 
875 	/* update netdev stats from eth stats */
876 	i40e_update_eth_stats(vsi);
877 	ons->tx_errors = oes->tx_errors;
878 	ns->tx_errors = es->tx_errors;
879 	ons->multicast = oes->rx_multicast;
880 	ns->multicast = es->rx_multicast;
881 	ons->rx_dropped = oes->rx_discards;
882 	ns->rx_dropped = es->rx_discards;
883 	ons->tx_dropped = oes->tx_discards;
884 	ns->tx_dropped = es->tx_discards;
885 
886 	/* pull in a couple PF stats if this is the main vsi */
887 	if (vsi == pf->vsi[pf->lan_vsi]) {
888 		ns->rx_crc_errors = pf->stats.crc_errors;
889 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
890 		ns->rx_length_errors = pf->stats.rx_length_errors;
891 	}
892 }
893 
894 /**
895  * i40e_update_pf_stats - Update the PF statistics counters.
896  * @pf: the PF to be updated
897  **/
898 static void i40e_update_pf_stats(struct i40e_pf *pf)
899 {
900 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
901 	struct i40e_hw_port_stats *nsd = &pf->stats;
902 	struct i40e_hw *hw = &pf->hw;
903 	u32 val;
904 	int i;
905 
906 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
907 			   I40E_GLPRT_GORCL(hw->port),
908 			   pf->stat_offsets_loaded,
909 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
910 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
911 			   I40E_GLPRT_GOTCL(hw->port),
912 			   pf->stat_offsets_loaded,
913 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
914 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
915 			   pf->stat_offsets_loaded,
916 			   &osd->eth.rx_discards,
917 			   &nsd->eth.rx_discards);
918 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
919 			   I40E_GLPRT_UPRCL(hw->port),
920 			   pf->stat_offsets_loaded,
921 			   &osd->eth.rx_unicast,
922 			   &nsd->eth.rx_unicast);
923 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
924 			   I40E_GLPRT_MPRCL(hw->port),
925 			   pf->stat_offsets_loaded,
926 			   &osd->eth.rx_multicast,
927 			   &nsd->eth.rx_multicast);
928 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
929 			   I40E_GLPRT_BPRCL(hw->port),
930 			   pf->stat_offsets_loaded,
931 			   &osd->eth.rx_broadcast,
932 			   &nsd->eth.rx_broadcast);
933 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
934 			   I40E_GLPRT_UPTCL(hw->port),
935 			   pf->stat_offsets_loaded,
936 			   &osd->eth.tx_unicast,
937 			   &nsd->eth.tx_unicast);
938 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
939 			   I40E_GLPRT_MPTCL(hw->port),
940 			   pf->stat_offsets_loaded,
941 			   &osd->eth.tx_multicast,
942 			   &nsd->eth.tx_multicast);
943 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
944 			   I40E_GLPRT_BPTCL(hw->port),
945 			   pf->stat_offsets_loaded,
946 			   &osd->eth.tx_broadcast,
947 			   &nsd->eth.tx_broadcast);
948 
949 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
950 			   pf->stat_offsets_loaded,
951 			   &osd->tx_dropped_link_down,
952 			   &nsd->tx_dropped_link_down);
953 
954 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
955 			   pf->stat_offsets_loaded,
956 			   &osd->crc_errors, &nsd->crc_errors);
957 
958 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
959 			   pf->stat_offsets_loaded,
960 			   &osd->illegal_bytes, &nsd->illegal_bytes);
961 
962 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
963 			   pf->stat_offsets_loaded,
964 			   &osd->mac_local_faults,
965 			   &nsd->mac_local_faults);
966 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
967 			   pf->stat_offsets_loaded,
968 			   &osd->mac_remote_faults,
969 			   &nsd->mac_remote_faults);
970 
971 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
972 			   pf->stat_offsets_loaded,
973 			   &osd->rx_length_errors,
974 			   &nsd->rx_length_errors);
975 
976 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
977 			   pf->stat_offsets_loaded,
978 			   &osd->link_xon_rx, &nsd->link_xon_rx);
979 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
980 			   pf->stat_offsets_loaded,
981 			   &osd->link_xon_tx, &nsd->link_xon_tx);
982 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
983 			   pf->stat_offsets_loaded,
984 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
985 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
988 
989 	for (i = 0; i < 8; i++) {
990 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
991 				   pf->stat_offsets_loaded,
992 				   &osd->priority_xoff_rx[i],
993 				   &nsd->priority_xoff_rx[i]);
994 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
995 				   pf->stat_offsets_loaded,
996 				   &osd->priority_xon_rx[i],
997 				   &nsd->priority_xon_rx[i]);
998 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
999 				   pf->stat_offsets_loaded,
1000 				   &osd->priority_xon_tx[i],
1001 				   &nsd->priority_xon_tx[i]);
1002 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1003 				   pf->stat_offsets_loaded,
1004 				   &osd->priority_xoff_tx[i],
1005 				   &nsd->priority_xoff_tx[i]);
1006 		i40e_stat_update32(hw,
1007 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1008 				   pf->stat_offsets_loaded,
1009 				   &osd->priority_xon_2_xoff[i],
1010 				   &nsd->priority_xon_2_xoff[i]);
1011 	}
1012 
1013 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1014 			   I40E_GLPRT_PRC64L(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->rx_size_64, &nsd->rx_size_64);
1017 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1018 			   I40E_GLPRT_PRC127L(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->rx_size_127, &nsd->rx_size_127);
1021 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1022 			   I40E_GLPRT_PRC255L(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->rx_size_255, &nsd->rx_size_255);
1025 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1026 			   I40E_GLPRT_PRC511L(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->rx_size_511, &nsd->rx_size_511);
1029 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1030 			   I40E_GLPRT_PRC1023L(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1033 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1034 			   I40E_GLPRT_PRC1522L(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1037 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1038 			   I40E_GLPRT_PRC9522L(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->rx_size_big, &nsd->rx_size_big);
1041 
1042 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1043 			   I40E_GLPRT_PTC64L(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->tx_size_64, &nsd->tx_size_64);
1046 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1047 			   I40E_GLPRT_PTC127L(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->tx_size_127, &nsd->tx_size_127);
1050 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1051 			   I40E_GLPRT_PTC255L(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->tx_size_255, &nsd->tx_size_255);
1054 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1055 			   I40E_GLPRT_PTC511L(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_size_511, &nsd->tx_size_511);
1058 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1059 			   I40E_GLPRT_PTC1023L(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1062 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1063 			   I40E_GLPRT_PTC1522L(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1066 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1067 			   I40E_GLPRT_PTC9522L(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->tx_size_big, &nsd->tx_size_big);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->rx_undersize, &nsd->rx_undersize);
1074 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1075 			   pf->stat_offsets_loaded,
1076 			   &osd->rx_fragments, &nsd->rx_fragments);
1077 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1078 			   pf->stat_offsets_loaded,
1079 			   &osd->rx_oversize, &nsd->rx_oversize);
1080 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_jabber, &nsd->rx_jabber);
1083 
1084 	/* FDIR stats */
1085 	i40e_stat_update_and_clear32(hw,
1086 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1087 			&nsd->fd_atr_match);
1088 	i40e_stat_update_and_clear32(hw,
1089 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1090 			&nsd->fd_sb_match);
1091 	i40e_stat_update_and_clear32(hw,
1092 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1093 			&nsd->fd_atr_tunnel_match);
1094 
1095 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1096 	nsd->tx_lpi_status =
1097 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1098 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1099 	nsd->rx_lpi_status =
1100 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1101 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1102 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1103 			   pf->stat_offsets_loaded,
1104 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1105 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1106 			   pf->stat_offsets_loaded,
1107 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1108 
1109 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1110 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1111 		nsd->fd_sb_status = true;
1112 	else
1113 		nsd->fd_sb_status = false;
1114 
1115 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1116 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1117 		nsd->fd_atr_status = true;
1118 	else
1119 		nsd->fd_atr_status = false;
1120 
1121 	pf->stat_offsets_loaded = true;
1122 }
1123 
1124 /**
1125  * i40e_update_stats - Update the various statistics counters.
1126  * @vsi: the VSI to be updated
1127  *
1128  * Update the various stats for this VSI and its related entities.
1129  **/
1130 void i40e_update_stats(struct i40e_vsi *vsi)
1131 {
1132 	struct i40e_pf *pf = vsi->back;
1133 
1134 	if (vsi == pf->vsi[pf->lan_vsi])
1135 		i40e_update_pf_stats(pf);
1136 
1137 	i40e_update_vsi_stats(vsi);
1138 }
1139 
1140 /**
1141  * i40e_count_filters - counts VSI mac filters
1142  * @vsi: the VSI to be searched
1143  *
1144  * Returns count of mac filters
1145  **/
1146 int i40e_count_filters(struct i40e_vsi *vsi)
1147 {
1148 	struct i40e_mac_filter *f;
1149 	struct hlist_node *h;
1150 	int bkt;
1151 	int cnt = 0;
1152 
1153 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1154 		++cnt;
1155 
1156 	return cnt;
1157 }
1158 
1159 /**
1160  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1161  * @vsi: the VSI to be searched
1162  * @macaddr: the MAC address
1163  * @vlan: the vlan
1164  *
1165  * Returns ptr to the filter object or NULL
1166  **/
1167 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1168 						const u8 *macaddr, s16 vlan)
1169 {
1170 	struct i40e_mac_filter *f;
1171 	u64 key;
1172 
1173 	if (!vsi || !macaddr)
1174 		return NULL;
1175 
1176 	key = i40e_addr_to_hkey(macaddr);
1177 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1178 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1179 		    (vlan == f->vlan))
1180 			return f;
1181 	}
1182 	return NULL;
1183 }
1184 
1185 /**
1186  * i40e_find_mac - Find a mac addr in the macvlan filters list
1187  * @vsi: the VSI to be searched
1188  * @macaddr: the MAC address we are searching for
1189  *
1190  * Returns the first filter with the provided MAC address or NULL if
1191  * MAC address was not found
1192  **/
1193 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1194 {
1195 	struct i40e_mac_filter *f;
1196 	u64 key;
1197 
1198 	if (!vsi || !macaddr)
1199 		return NULL;
1200 
1201 	key = i40e_addr_to_hkey(macaddr);
1202 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1203 		if ((ether_addr_equal(macaddr, f->macaddr)))
1204 			return f;
1205 	}
1206 	return NULL;
1207 }
1208 
1209 /**
1210  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1211  * @vsi: the VSI to be searched
1212  *
1213  * Returns true if VSI is in vlan mode or false otherwise
1214  **/
1215 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1216 {
1217 	/* If we have a PVID, always operate in VLAN mode */
1218 	if (vsi->info.pvid)
1219 		return true;
1220 
1221 	/* We need to operate in VLAN mode whenever we have any filters with
1222 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1223 	 * time, incurring search cost repeatedly. However, we can notice two
1224 	 * things:
1225 	 *
1226 	 * 1) the only place where we can gain a VLAN filter is in
1227 	 *    i40e_add_filter.
1228 	 *
1229 	 * 2) the only place where filters are actually removed is in
1230 	 *    i40e_sync_filters_subtask.
1231 	 *
1232 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1233 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1234 	 * we have to perform the full search after deleting filters in
1235 	 * i40e_sync_filters_subtask, but we already have to search
1236 	 * filters here and can perform the check at the same time. This
1237 	 * results in avoiding embedding a loop for VLAN mode inside another
1238 	 * loop over all the filters, and should maintain correctness as noted
1239 	 * above.
1240 	 */
1241 	return vsi->has_vlan_filter;
1242 }
1243 
1244 /**
1245  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1246  * @vsi: the VSI to configure
1247  * @tmp_add_list: list of filters ready to be added
1248  * @tmp_del_list: list of filters ready to be deleted
1249  * @vlan_filters: the number of active VLAN filters
1250  *
1251  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1252  * behave as expected. If we have any active VLAN filters remaining or about
1253  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1254  * so that they only match against untagged traffic. If we no longer have any
1255  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1256  * so that they match against both tagged and untagged traffic. In this way,
1257  * we ensure that we correctly receive the desired traffic. This ensures that
1258  * when we have an active VLAN we will receive only untagged traffic and
1259  * traffic matching active VLANs. If we have no active VLANs then we will
1260  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1261  *
1262  * Finally, in a similar fashion, this function also corrects filters when
1263  * there is an active PVID assigned to this VSI.
1264  *
1265  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1266  *
1267  * This function is only expected to be called from within
1268  * i40e_sync_vsi_filters.
1269  *
1270  * NOTE: This function expects to be called while under the
1271  * mac_filter_hash_lock
1272  */
1273 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1274 					 struct hlist_head *tmp_add_list,
1275 					 struct hlist_head *tmp_del_list,
1276 					 int vlan_filters)
1277 {
1278 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1279 	struct i40e_mac_filter *f, *add_head;
1280 	struct i40e_new_mac_filter *new;
1281 	struct hlist_node *h;
1282 	int bkt, new_vlan;
1283 
1284 	/* To determine if a particular filter needs to be replaced we
1285 	 * have the three following conditions:
1286 	 *
1287 	 * a) if we have a PVID assigned, then all filters which are
1288 	 *    not marked as VLAN=PVID must be replaced with filters that
1289 	 *    are.
1290 	 * b) otherwise, if we have any active VLANS, all filters
1291 	 *    which are marked as VLAN=-1 must be replaced with
1292 	 *    filters marked as VLAN=0
1293 	 * c) finally, if we do not have any active VLANS, all filters
1294 	 *    which are marked as VLAN=0 must be replaced with filters
1295 	 *    marked as VLAN=-1
1296 	 */
1297 
1298 	/* Update the filters about to be added in place */
1299 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1300 		if (pvid && new->f->vlan != pvid)
1301 			new->f->vlan = pvid;
1302 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1303 			new->f->vlan = 0;
1304 		else if (!vlan_filters && new->f->vlan == 0)
1305 			new->f->vlan = I40E_VLAN_ANY;
1306 	}
1307 
1308 	/* Update the remaining active filters */
1309 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1310 		/* Combine the checks for whether a filter needs to be changed
1311 		 * and then determine the new VLAN inside the if block, in
1312 		 * order to avoid duplicating code for adding the new filter
1313 		 * then deleting the old filter.
1314 		 */
1315 		if ((pvid && f->vlan != pvid) ||
1316 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1317 		    (!vlan_filters && f->vlan == 0)) {
1318 			/* Determine the new vlan we will be adding */
1319 			if (pvid)
1320 				new_vlan = pvid;
1321 			else if (vlan_filters)
1322 				new_vlan = 0;
1323 			else
1324 				new_vlan = I40E_VLAN_ANY;
1325 
1326 			/* Create the new filter */
1327 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1328 			if (!add_head)
1329 				return -ENOMEM;
1330 
1331 			/* Create a temporary i40e_new_mac_filter */
1332 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1333 			if (!new)
1334 				return -ENOMEM;
1335 
1336 			new->f = add_head;
1337 			new->state = add_head->state;
1338 
1339 			/* Add the new filter to the tmp list */
1340 			hlist_add_head(&new->hlist, tmp_add_list);
1341 
1342 			/* Put the original filter into the delete list */
1343 			f->state = I40E_FILTER_REMOVE;
1344 			hash_del(&f->hlist);
1345 			hlist_add_head(&f->hlist, tmp_del_list);
1346 		}
1347 	}
1348 
1349 	vsi->has_vlan_filter = !!vlan_filters;
1350 
1351 	return 0;
1352 }
1353 
1354 /**
1355  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1356  * @vsi: the PF Main VSI - inappropriate for any other VSI
1357  * @macaddr: the MAC address
1358  *
1359  * Remove whatever filter the firmware set up so the driver can manage
1360  * its own filtering intelligently.
1361  **/
1362 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1363 {
1364 	struct i40e_aqc_remove_macvlan_element_data element;
1365 	struct i40e_pf *pf = vsi->back;
1366 
1367 	/* Only appropriate for the PF main VSI */
1368 	if (vsi->type != I40E_VSI_MAIN)
1369 		return;
1370 
1371 	memset(&element, 0, sizeof(element));
1372 	ether_addr_copy(element.mac_addr, macaddr);
1373 	element.vlan_tag = 0;
1374 	/* Ignore error returns, some firmware does it this way... */
1375 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1376 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1377 
1378 	memset(&element, 0, sizeof(element));
1379 	ether_addr_copy(element.mac_addr, macaddr);
1380 	element.vlan_tag = 0;
1381 	/* ...and some firmware does it this way. */
1382 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1383 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1384 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1385 }
1386 
1387 /**
1388  * i40e_add_filter - Add a mac/vlan filter to the VSI
1389  * @vsi: the VSI to be searched
1390  * @macaddr: the MAC address
1391  * @vlan: the vlan
1392  *
1393  * Returns ptr to the filter object or NULL when no memory available.
1394  *
1395  * NOTE: This function is expected to be called with mac_filter_hash_lock
1396  * being held.
1397  **/
1398 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1399 					const u8 *macaddr, s16 vlan)
1400 {
1401 	struct i40e_mac_filter *f;
1402 	u64 key;
1403 
1404 	if (!vsi || !macaddr)
1405 		return NULL;
1406 
1407 	f = i40e_find_filter(vsi, macaddr, vlan);
1408 	if (!f) {
1409 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1410 		if (!f)
1411 			return NULL;
1412 
1413 		/* Update the boolean indicating if we need to function in
1414 		 * VLAN mode.
1415 		 */
1416 		if (vlan >= 0)
1417 			vsi->has_vlan_filter = true;
1418 
1419 		ether_addr_copy(f->macaddr, macaddr);
1420 		f->vlan = vlan;
1421 		f->state = I40E_FILTER_NEW;
1422 		INIT_HLIST_NODE(&f->hlist);
1423 
1424 		key = i40e_addr_to_hkey(macaddr);
1425 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1426 
1427 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1428 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1429 	}
1430 
1431 	/* If we're asked to add a filter that has been marked for removal, it
1432 	 * is safe to simply restore it to active state. __i40e_del_filter
1433 	 * will have simply deleted any filters which were previously marked
1434 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1435 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1436 	 * task, just restore this filter to the ACTIVE state so that the
1437 	 * sync task leaves it in place
1438 	 */
1439 	if (f->state == I40E_FILTER_REMOVE)
1440 		f->state = I40E_FILTER_ACTIVE;
1441 
1442 	return f;
1443 }
1444 
1445 /**
1446  * __i40e_del_filter - Remove a specific filter from the VSI
1447  * @vsi: VSI to remove from
1448  * @f: the filter to remove from the list
1449  *
1450  * This function should be called instead of i40e_del_filter only if you know
1451  * the exact filter you will remove already, such as via i40e_find_filter or
1452  * i40e_find_mac.
1453  *
1454  * NOTE: This function is expected to be called with mac_filter_hash_lock
1455  * being held.
1456  * ANOTHER NOTE: This function MUST be called from within the context of
1457  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1458  * instead of list_for_each_entry().
1459  **/
1460 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1461 {
1462 	if (!f)
1463 		return;
1464 
1465 	/* If the filter was never added to firmware then we can just delete it
1466 	 * directly and we don't want to set the status to remove or else an
1467 	 * admin queue command will unnecessarily fire.
1468 	 */
1469 	if ((f->state == I40E_FILTER_FAILED) ||
1470 	    (f->state == I40E_FILTER_NEW)) {
1471 		hash_del(&f->hlist);
1472 		kfree(f);
1473 	} else {
1474 		f->state = I40E_FILTER_REMOVE;
1475 	}
1476 
1477 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1478 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1479 }
1480 
1481 /**
1482  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1483  * @vsi: the VSI to be searched
1484  * @macaddr: the MAC address
1485  * @vlan: the VLAN
1486  *
1487  * NOTE: This function is expected to be called with mac_filter_hash_lock
1488  * being held.
1489  * ANOTHER NOTE: This function MUST be called from within the context of
1490  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1491  * instead of list_for_each_entry().
1492  **/
1493 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1494 {
1495 	struct i40e_mac_filter *f;
1496 
1497 	if (!vsi || !macaddr)
1498 		return;
1499 
1500 	f = i40e_find_filter(vsi, macaddr, vlan);
1501 	__i40e_del_filter(vsi, f);
1502 }
1503 
1504 /**
1505  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1506  * @vsi: the VSI to be searched
1507  * @macaddr: the mac address to be filtered
1508  *
1509  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1510  * go through all the macvlan filters and add a macvlan filter for each
1511  * unique vlan that already exists. If a PVID has been assigned, instead only
1512  * add the macaddr to that VLAN.
1513  *
1514  * Returns last filter added on success, else NULL
1515  **/
1516 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1517 					    const u8 *macaddr)
1518 {
1519 	struct i40e_mac_filter *f, *add = NULL;
1520 	struct hlist_node *h;
1521 	int bkt;
1522 
1523 	if (vsi->info.pvid)
1524 		return i40e_add_filter(vsi, macaddr,
1525 				       le16_to_cpu(vsi->info.pvid));
1526 
1527 	if (!i40e_is_vsi_in_vlan(vsi))
1528 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1529 
1530 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1531 		if (f->state == I40E_FILTER_REMOVE)
1532 			continue;
1533 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1534 		if (!add)
1535 			return NULL;
1536 	}
1537 
1538 	return add;
1539 }
1540 
1541 /**
1542  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1543  * @vsi: the VSI to be searched
1544  * @macaddr: the mac address to be removed
1545  *
1546  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1547  * associated with.
1548  *
1549  * Returns 0 for success, or error
1550  **/
1551 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1552 {
1553 	struct i40e_mac_filter *f;
1554 	struct hlist_node *h;
1555 	bool found = false;
1556 	int bkt;
1557 
1558 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1559 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1560 		if (ether_addr_equal(macaddr, f->macaddr)) {
1561 			__i40e_del_filter(vsi, f);
1562 			found = true;
1563 		}
1564 	}
1565 
1566 	if (found)
1567 		return 0;
1568 	else
1569 		return -ENOENT;
1570 }
1571 
1572 /**
1573  * i40e_set_mac - NDO callback to set mac address
1574  * @netdev: network interface device structure
1575  * @p: pointer to an address structure
1576  *
1577  * Returns 0 on success, negative on failure
1578  **/
1579 static int i40e_set_mac(struct net_device *netdev, void *p)
1580 {
1581 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1582 	struct i40e_vsi *vsi = np->vsi;
1583 	struct i40e_pf *pf = vsi->back;
1584 	struct i40e_hw *hw = &pf->hw;
1585 	struct sockaddr *addr = p;
1586 
1587 	if (!is_valid_ether_addr(addr->sa_data))
1588 		return -EADDRNOTAVAIL;
1589 
1590 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1591 		netdev_info(netdev, "already using mac address %pM\n",
1592 			    addr->sa_data);
1593 		return 0;
1594 	}
1595 
1596 	if (test_bit(__I40E_DOWN, pf->state) ||
1597 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1598 		return -EADDRNOTAVAIL;
1599 
1600 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1601 		netdev_info(netdev, "returning to hw mac address %pM\n",
1602 			    hw->mac.addr);
1603 	else
1604 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1605 
1606 	/* Copy the address first, so that we avoid a possible race with
1607 	 * .set_rx_mode().
1608 	 * - Remove old address from MAC filter
1609 	 * - Copy new address
1610 	 * - Add new address to MAC filter
1611 	 */
1612 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1613 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1614 	eth_hw_addr_set(netdev, addr->sa_data);
1615 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1616 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1617 
1618 	if (vsi->type == I40E_VSI_MAIN) {
1619 		i40e_status ret;
1620 
1621 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1622 						addr->sa_data, NULL);
1623 		if (ret)
1624 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1625 				    i40e_stat_str(hw, ret),
1626 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1627 	}
1628 
1629 	/* schedule our worker thread which will take care of
1630 	 * applying the new filter changes
1631 	 */
1632 	i40e_service_event_schedule(pf);
1633 	return 0;
1634 }
1635 
1636 /**
1637  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1638  * @vsi: vsi structure
1639  * @seed: RSS hash seed
1640  * @lut: pointer to lookup table of lut_size
1641  * @lut_size: size of the lookup table
1642  **/
1643 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1644 			      u8 *lut, u16 lut_size)
1645 {
1646 	struct i40e_pf *pf = vsi->back;
1647 	struct i40e_hw *hw = &pf->hw;
1648 	int ret = 0;
1649 
1650 	if (seed) {
1651 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1652 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1653 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1654 		if (ret) {
1655 			dev_info(&pf->pdev->dev,
1656 				 "Cannot set RSS key, err %s aq_err %s\n",
1657 				 i40e_stat_str(hw, ret),
1658 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1659 			return ret;
1660 		}
1661 	}
1662 	if (lut) {
1663 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1664 
1665 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1666 		if (ret) {
1667 			dev_info(&pf->pdev->dev,
1668 				 "Cannot set RSS lut, err %s aq_err %s\n",
1669 				 i40e_stat_str(hw, ret),
1670 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1671 			return ret;
1672 		}
1673 	}
1674 	return ret;
1675 }
1676 
1677 /**
1678  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1679  * @vsi: VSI structure
1680  **/
1681 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1682 {
1683 	struct i40e_pf *pf = vsi->back;
1684 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1685 	u8 *lut;
1686 	int ret;
1687 
1688 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1689 		return 0;
1690 	if (!vsi->rss_size)
1691 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1692 				      vsi->num_queue_pairs);
1693 	if (!vsi->rss_size)
1694 		return -EINVAL;
1695 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1696 	if (!lut)
1697 		return -ENOMEM;
1698 
1699 	/* Use the user configured hash keys and lookup table if there is one,
1700 	 * otherwise use default
1701 	 */
1702 	if (vsi->rss_lut_user)
1703 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1704 	else
1705 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1706 	if (vsi->rss_hkey_user)
1707 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1708 	else
1709 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1710 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1711 	kfree(lut);
1712 	return ret;
1713 }
1714 
1715 /**
1716  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1717  * @vsi: the VSI being configured,
1718  * @ctxt: VSI context structure
1719  * @enabled_tc: number of traffic classes to enable
1720  *
1721  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1722  **/
1723 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1724 					   struct i40e_vsi_context *ctxt,
1725 					   u8 enabled_tc)
1726 {
1727 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1728 	int i, override_q, pow, num_qps, ret;
1729 	u8 netdev_tc = 0, offset = 0;
1730 
1731 	if (vsi->type != I40E_VSI_MAIN)
1732 		return -EINVAL;
1733 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1734 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1735 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1736 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1737 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1738 
1739 	/* find the next higher power-of-2 of num queue pairs */
1740 	pow = ilog2(num_qps);
1741 	if (!is_power_of_2(num_qps))
1742 		pow++;
1743 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1744 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1745 
1746 	/* Setup queue offset/count for all TCs for given VSI */
1747 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1748 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1749 		/* See if the given TC is enabled for the given VSI */
1750 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1751 			offset = vsi->mqprio_qopt.qopt.offset[i];
1752 			qcount = vsi->mqprio_qopt.qopt.count[i];
1753 			if (qcount > max_qcount)
1754 				max_qcount = qcount;
1755 			vsi->tc_config.tc_info[i].qoffset = offset;
1756 			vsi->tc_config.tc_info[i].qcount = qcount;
1757 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1758 		} else {
1759 			/* TC is not enabled so set the offset to
1760 			 * default queue and allocate one queue
1761 			 * for the given TC.
1762 			 */
1763 			vsi->tc_config.tc_info[i].qoffset = 0;
1764 			vsi->tc_config.tc_info[i].qcount = 1;
1765 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1766 		}
1767 	}
1768 
1769 	/* Set actual Tx/Rx queue pairs */
1770 	vsi->num_queue_pairs = offset + qcount;
1771 
1772 	/* Setup queue TC[0].qmap for given VSI context */
1773 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1774 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1775 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1776 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1777 
1778 	/* Reconfigure RSS for main VSI with max queue count */
1779 	vsi->rss_size = max_qcount;
1780 	ret = i40e_vsi_config_rss(vsi);
1781 	if (ret) {
1782 		dev_info(&vsi->back->pdev->dev,
1783 			 "Failed to reconfig rss for num_queues (%u)\n",
1784 			 max_qcount);
1785 		return ret;
1786 	}
1787 	vsi->reconfig_rss = true;
1788 	dev_dbg(&vsi->back->pdev->dev,
1789 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1790 
1791 	/* Find queue count available for channel VSIs and starting offset
1792 	 * for channel VSIs
1793 	 */
1794 	override_q = vsi->mqprio_qopt.qopt.count[0];
1795 	if (override_q && override_q < vsi->num_queue_pairs) {
1796 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1797 		vsi->next_base_queue = override_q;
1798 	}
1799 	return 0;
1800 }
1801 
1802 /**
1803  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1804  * @vsi: the VSI being setup
1805  * @ctxt: VSI context structure
1806  * @enabled_tc: Enabled TCs bitmap
1807  * @is_add: True if called before Add VSI
1808  *
1809  * Setup VSI queue mapping for enabled traffic classes.
1810  **/
1811 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1812 				     struct i40e_vsi_context *ctxt,
1813 				     u8 enabled_tc,
1814 				     bool is_add)
1815 {
1816 	struct i40e_pf *pf = vsi->back;
1817 	u16 num_tc_qps = 0;
1818 	u16 sections = 0;
1819 	u8 netdev_tc = 0;
1820 	u16 numtc = 1;
1821 	u16 qcount;
1822 	u8 offset;
1823 	u16 qmap;
1824 	int i;
1825 
1826 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1827 	offset = 0;
1828 	/* zero out queue mapping, it will get updated on the end of the function */
1829 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1830 
1831 	if (vsi->type == I40E_VSI_MAIN) {
1832 		/* This code helps add more queue to the VSI if we have
1833 		 * more cores than RSS can support, the higher cores will
1834 		 * be served by ATR or other filters. Furthermore, the
1835 		 * non-zero req_queue_pairs says that user requested a new
1836 		 * queue count via ethtool's set_channels, so use this
1837 		 * value for queues distribution across traffic classes
1838 		 */
1839 		if (vsi->req_queue_pairs > 0)
1840 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1841 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1842 			vsi->num_queue_pairs = pf->num_lan_msix;
1843 	}
1844 
1845 	/* Number of queues per enabled TC */
1846 	if (vsi->type == I40E_VSI_MAIN ||
1847 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1848 		num_tc_qps = vsi->num_queue_pairs;
1849 	else
1850 		num_tc_qps = vsi->alloc_queue_pairs;
1851 
1852 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1853 		/* Find numtc from enabled TC bitmap */
1854 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1855 			if (enabled_tc & BIT(i)) /* TC is enabled */
1856 				numtc++;
1857 		}
1858 		if (!numtc) {
1859 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1860 			numtc = 1;
1861 		}
1862 		num_tc_qps = num_tc_qps / numtc;
1863 		num_tc_qps = min_t(int, num_tc_qps,
1864 				   i40e_pf_get_max_q_per_tc(pf));
1865 	}
1866 
1867 	vsi->tc_config.numtc = numtc;
1868 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1869 
1870 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1871 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1872 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1873 
1874 	/* Setup queue offset/count for all TCs for given VSI */
1875 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1876 		/* See if the given TC is enabled for the given VSI */
1877 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1878 			/* TC is enabled */
1879 			int pow, num_qps;
1880 
1881 			switch (vsi->type) {
1882 			case I40E_VSI_MAIN:
1883 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1884 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1885 				    vsi->tc_config.enabled_tc != 1) {
1886 					qcount = min_t(int, pf->alloc_rss_size,
1887 						       num_tc_qps);
1888 					break;
1889 				}
1890 				fallthrough;
1891 			case I40E_VSI_FDIR:
1892 			case I40E_VSI_SRIOV:
1893 			case I40E_VSI_VMDQ2:
1894 			default:
1895 				qcount = num_tc_qps;
1896 				WARN_ON(i != 0);
1897 				break;
1898 			}
1899 			vsi->tc_config.tc_info[i].qoffset = offset;
1900 			vsi->tc_config.tc_info[i].qcount = qcount;
1901 
1902 			/* find the next higher power-of-2 of num queue pairs */
1903 			num_qps = qcount;
1904 			pow = 0;
1905 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1906 				pow++;
1907 				num_qps >>= 1;
1908 			}
1909 
1910 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1911 			qmap =
1912 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1913 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1914 
1915 			offset += qcount;
1916 		} else {
1917 			/* TC is not enabled so set the offset to
1918 			 * default queue and allocate one queue
1919 			 * for the given TC.
1920 			 */
1921 			vsi->tc_config.tc_info[i].qoffset = 0;
1922 			vsi->tc_config.tc_info[i].qcount = 1;
1923 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1924 
1925 			qmap = 0;
1926 		}
1927 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1928 	}
1929 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1930 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1931 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1932 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1933 		vsi->num_queue_pairs = offset;
1934 
1935 	/* Scheduler section valid can only be set for ADD VSI */
1936 	if (is_add) {
1937 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1938 
1939 		ctxt->info.up_enable_bits = enabled_tc;
1940 	}
1941 	if (vsi->type == I40E_VSI_SRIOV) {
1942 		ctxt->info.mapping_flags |=
1943 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1944 		for (i = 0; i < vsi->num_queue_pairs; i++)
1945 			ctxt->info.queue_mapping[i] =
1946 					       cpu_to_le16(vsi->base_queue + i);
1947 	} else {
1948 		ctxt->info.mapping_flags |=
1949 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1950 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1951 	}
1952 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1953 }
1954 
1955 /**
1956  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1957  * @netdev: the netdevice
1958  * @addr: address to add
1959  *
1960  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1961  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1962  */
1963 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1964 {
1965 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1966 	struct i40e_vsi *vsi = np->vsi;
1967 
1968 	if (i40e_add_mac_filter(vsi, addr))
1969 		return 0;
1970 	else
1971 		return -ENOMEM;
1972 }
1973 
1974 /**
1975  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1976  * @netdev: the netdevice
1977  * @addr: address to add
1978  *
1979  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1980  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1981  */
1982 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1983 {
1984 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1985 	struct i40e_vsi *vsi = np->vsi;
1986 
1987 	/* Under some circumstances, we might receive a request to delete
1988 	 * our own device address from our uc list. Because we store the
1989 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1990 	 * such requests and not delete our device address from this list.
1991 	 */
1992 	if (ether_addr_equal(addr, netdev->dev_addr))
1993 		return 0;
1994 
1995 	i40e_del_mac_filter(vsi, addr);
1996 
1997 	return 0;
1998 }
1999 
2000 /**
2001  * i40e_set_rx_mode - NDO callback to set the netdev filters
2002  * @netdev: network interface device structure
2003  **/
2004 static void i40e_set_rx_mode(struct net_device *netdev)
2005 {
2006 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2007 	struct i40e_vsi *vsi = np->vsi;
2008 
2009 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2010 
2011 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2012 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2013 
2014 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2015 
2016 	/* check for other flag changes */
2017 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2018 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2019 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2020 	}
2021 }
2022 
2023 /**
2024  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2025  * @vsi: Pointer to VSI struct
2026  * @from: Pointer to list which contains MAC filter entries - changes to
2027  *        those entries needs to be undone.
2028  *
2029  * MAC filter entries from this list were slated for deletion.
2030  **/
2031 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2032 					 struct hlist_head *from)
2033 {
2034 	struct i40e_mac_filter *f;
2035 	struct hlist_node *h;
2036 
2037 	hlist_for_each_entry_safe(f, h, from, hlist) {
2038 		u64 key = i40e_addr_to_hkey(f->macaddr);
2039 
2040 		/* Move the element back into MAC filter list*/
2041 		hlist_del(&f->hlist);
2042 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2043 	}
2044 }
2045 
2046 /**
2047  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2048  * @vsi: Pointer to vsi struct
2049  * @from: Pointer to list which contains MAC filter entries - changes to
2050  *        those entries needs to be undone.
2051  *
2052  * MAC filter entries from this list were slated for addition.
2053  **/
2054 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2055 					 struct hlist_head *from)
2056 {
2057 	struct i40e_new_mac_filter *new;
2058 	struct hlist_node *h;
2059 
2060 	hlist_for_each_entry_safe(new, h, from, hlist) {
2061 		/* We can simply free the wrapper structure */
2062 		hlist_del(&new->hlist);
2063 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2064 		kfree(new);
2065 	}
2066 }
2067 
2068 /**
2069  * i40e_next_filter - Get the next non-broadcast filter from a list
2070  * @next: pointer to filter in list
2071  *
2072  * Returns the next non-broadcast filter in the list. Required so that we
2073  * ignore broadcast filters within the list, since these are not handled via
2074  * the normal firmware update path.
2075  */
2076 static
2077 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2078 {
2079 	hlist_for_each_entry_continue(next, hlist) {
2080 		if (!is_broadcast_ether_addr(next->f->macaddr))
2081 			return next;
2082 	}
2083 
2084 	return NULL;
2085 }
2086 
2087 /**
2088  * i40e_update_filter_state - Update filter state based on return data
2089  * from firmware
2090  * @count: Number of filters added
2091  * @add_list: return data from fw
2092  * @add_head: pointer to first filter in current batch
2093  *
2094  * MAC filter entries from list were slated to be added to device. Returns
2095  * number of successful filters. Note that 0 does NOT mean success!
2096  **/
2097 static int
2098 i40e_update_filter_state(int count,
2099 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2100 			 struct i40e_new_mac_filter *add_head)
2101 {
2102 	int retval = 0;
2103 	int i;
2104 
2105 	for (i = 0; i < count; i++) {
2106 		/* Always check status of each filter. We don't need to check
2107 		 * the firmware return status because we pre-set the filter
2108 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2109 		 * request to the adminq. Thus, if it no longer matches then
2110 		 * we know the filter is active.
2111 		 */
2112 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2113 			add_head->state = I40E_FILTER_FAILED;
2114 		} else {
2115 			add_head->state = I40E_FILTER_ACTIVE;
2116 			retval++;
2117 		}
2118 
2119 		add_head = i40e_next_filter(add_head);
2120 		if (!add_head)
2121 			break;
2122 	}
2123 
2124 	return retval;
2125 }
2126 
2127 /**
2128  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2129  * @vsi: ptr to the VSI
2130  * @vsi_name: name to display in messages
2131  * @list: the list of filters to send to firmware
2132  * @num_del: the number of filters to delete
2133  * @retval: Set to -EIO on failure to delete
2134  *
2135  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2136  * *retval instead of a return value so that success does not force ret_val to
2137  * be set to 0. This ensures that a sequence of calls to this function
2138  * preserve the previous value of *retval on successful delete.
2139  */
2140 static
2141 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2142 			  struct i40e_aqc_remove_macvlan_element_data *list,
2143 			  int num_del, int *retval)
2144 {
2145 	struct i40e_hw *hw = &vsi->back->hw;
2146 	i40e_status aq_ret;
2147 	int aq_err;
2148 
2149 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2150 	aq_err = hw->aq.asq_last_status;
2151 
2152 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2153 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2154 		*retval = -EIO;
2155 		dev_info(&vsi->back->pdev->dev,
2156 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2157 			 vsi_name, i40e_stat_str(hw, aq_ret),
2158 			 i40e_aq_str(hw, aq_err));
2159 	}
2160 }
2161 
2162 /**
2163  * i40e_aqc_add_filters - Request firmware to add a set of filters
2164  * @vsi: ptr to the VSI
2165  * @vsi_name: name to display in messages
2166  * @list: the list of filters to send to firmware
2167  * @add_head: Position in the add hlist
2168  * @num_add: the number of filters to add
2169  *
2170  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2171  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2172  * space for more filters.
2173  */
2174 static
2175 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2176 			  struct i40e_aqc_add_macvlan_element_data *list,
2177 			  struct i40e_new_mac_filter *add_head,
2178 			  int num_add)
2179 {
2180 	struct i40e_hw *hw = &vsi->back->hw;
2181 	int aq_err, fcnt;
2182 
2183 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2184 	aq_err = hw->aq.asq_last_status;
2185 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2186 
2187 	if (fcnt != num_add) {
2188 		if (vsi->type == I40E_VSI_MAIN) {
2189 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2190 			dev_warn(&vsi->back->pdev->dev,
2191 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2192 				 i40e_aq_str(hw, aq_err), vsi_name);
2193 		} else if (vsi->type == I40E_VSI_SRIOV ||
2194 			   vsi->type == I40E_VSI_VMDQ1 ||
2195 			   vsi->type == I40E_VSI_VMDQ2) {
2196 			dev_warn(&vsi->back->pdev->dev,
2197 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2198 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2199 		} else {
2200 			dev_warn(&vsi->back->pdev->dev,
2201 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2202 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2203 		}
2204 	}
2205 }
2206 
2207 /**
2208  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2209  * @vsi: pointer to the VSI
2210  * @vsi_name: the VSI name
2211  * @f: filter data
2212  *
2213  * This function sets or clears the promiscuous broadcast flags for VLAN
2214  * filters in order to properly receive broadcast frames. Assumes that only
2215  * broadcast filters are passed.
2216  *
2217  * Returns status indicating success or failure;
2218  **/
2219 static i40e_status
2220 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2221 			  struct i40e_mac_filter *f)
2222 {
2223 	bool enable = f->state == I40E_FILTER_NEW;
2224 	struct i40e_hw *hw = &vsi->back->hw;
2225 	i40e_status aq_ret;
2226 
2227 	if (f->vlan == I40E_VLAN_ANY) {
2228 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2229 						   vsi->seid,
2230 						   enable,
2231 						   NULL);
2232 	} else {
2233 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2234 							    vsi->seid,
2235 							    enable,
2236 							    f->vlan,
2237 							    NULL);
2238 	}
2239 
2240 	if (aq_ret) {
2241 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2242 		dev_warn(&vsi->back->pdev->dev,
2243 			 "Error %s, forcing overflow promiscuous on %s\n",
2244 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2245 			 vsi_name);
2246 	}
2247 
2248 	return aq_ret;
2249 }
2250 
2251 /**
2252  * i40e_set_promiscuous - set promiscuous mode
2253  * @pf: board private structure
2254  * @promisc: promisc on or off
2255  *
2256  * There are different ways of setting promiscuous mode on a PF depending on
2257  * what state/environment we're in.  This identifies and sets it appropriately.
2258  * Returns 0 on success.
2259  **/
2260 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2261 {
2262 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2263 	struct i40e_hw *hw = &pf->hw;
2264 	i40e_status aq_ret;
2265 
2266 	if (vsi->type == I40E_VSI_MAIN &&
2267 	    pf->lan_veb != I40E_NO_VEB &&
2268 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2269 		/* set defport ON for Main VSI instead of true promisc
2270 		 * this way we will get all unicast/multicast and VLAN
2271 		 * promisc behavior but will not get VF or VMDq traffic
2272 		 * replicated on the Main VSI.
2273 		 */
2274 		if (promisc)
2275 			aq_ret = i40e_aq_set_default_vsi(hw,
2276 							 vsi->seid,
2277 							 NULL);
2278 		else
2279 			aq_ret = i40e_aq_clear_default_vsi(hw,
2280 							   vsi->seid,
2281 							   NULL);
2282 		if (aq_ret) {
2283 			dev_info(&pf->pdev->dev,
2284 				 "Set default VSI failed, err %s, aq_err %s\n",
2285 				 i40e_stat_str(hw, aq_ret),
2286 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2287 		}
2288 	} else {
2289 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2290 						  hw,
2291 						  vsi->seid,
2292 						  promisc, NULL,
2293 						  true);
2294 		if (aq_ret) {
2295 			dev_info(&pf->pdev->dev,
2296 				 "set unicast promisc failed, err %s, aq_err %s\n",
2297 				 i40e_stat_str(hw, aq_ret),
2298 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2299 		}
2300 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2301 						  hw,
2302 						  vsi->seid,
2303 						  promisc, NULL);
2304 		if (aq_ret) {
2305 			dev_info(&pf->pdev->dev,
2306 				 "set multicast promisc failed, err %s, aq_err %s\n",
2307 				 i40e_stat_str(hw, aq_ret),
2308 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2309 		}
2310 	}
2311 
2312 	if (!aq_ret)
2313 		pf->cur_promisc = promisc;
2314 
2315 	return aq_ret;
2316 }
2317 
2318 /**
2319  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2320  * @vsi: ptr to the VSI
2321  *
2322  * Push any outstanding VSI filter changes through the AdminQ.
2323  *
2324  * Returns 0 or error value
2325  **/
2326 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2327 {
2328 	struct hlist_head tmp_add_list, tmp_del_list;
2329 	struct i40e_mac_filter *f;
2330 	struct i40e_new_mac_filter *new, *add_head = NULL;
2331 	struct i40e_hw *hw = &vsi->back->hw;
2332 	bool old_overflow, new_overflow;
2333 	unsigned int failed_filters = 0;
2334 	unsigned int vlan_filters = 0;
2335 	char vsi_name[16] = "PF";
2336 	int filter_list_len = 0;
2337 	i40e_status aq_ret = 0;
2338 	u32 changed_flags = 0;
2339 	struct hlist_node *h;
2340 	struct i40e_pf *pf;
2341 	int num_add = 0;
2342 	int num_del = 0;
2343 	int retval = 0;
2344 	u16 cmd_flags;
2345 	int list_size;
2346 	int bkt;
2347 
2348 	/* empty array typed pointers, kcalloc later */
2349 	struct i40e_aqc_add_macvlan_element_data *add_list;
2350 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2351 
2352 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2353 		usleep_range(1000, 2000);
2354 	pf = vsi->back;
2355 
2356 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2357 
2358 	if (vsi->netdev) {
2359 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2360 		vsi->current_netdev_flags = vsi->netdev->flags;
2361 	}
2362 
2363 	INIT_HLIST_HEAD(&tmp_add_list);
2364 	INIT_HLIST_HEAD(&tmp_del_list);
2365 
2366 	if (vsi->type == I40E_VSI_SRIOV)
2367 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2368 	else if (vsi->type != I40E_VSI_MAIN)
2369 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2370 
2371 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2372 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2373 
2374 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2375 		/* Create a list of filters to delete. */
2376 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2377 			if (f->state == I40E_FILTER_REMOVE) {
2378 				/* Move the element into temporary del_list */
2379 				hash_del(&f->hlist);
2380 				hlist_add_head(&f->hlist, &tmp_del_list);
2381 
2382 				/* Avoid counting removed filters */
2383 				continue;
2384 			}
2385 			if (f->state == I40E_FILTER_NEW) {
2386 				/* Create a temporary i40e_new_mac_filter */
2387 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2388 				if (!new)
2389 					goto err_no_memory_locked;
2390 
2391 				/* Store pointer to the real filter */
2392 				new->f = f;
2393 				new->state = f->state;
2394 
2395 				/* Add it to the hash list */
2396 				hlist_add_head(&new->hlist, &tmp_add_list);
2397 			}
2398 
2399 			/* Count the number of active (current and new) VLAN
2400 			 * filters we have now. Does not count filters which
2401 			 * are marked for deletion.
2402 			 */
2403 			if (f->vlan > 0)
2404 				vlan_filters++;
2405 		}
2406 
2407 		retval = i40e_correct_mac_vlan_filters(vsi,
2408 						       &tmp_add_list,
2409 						       &tmp_del_list,
2410 						       vlan_filters);
2411 
2412 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2413 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2414 
2415 		if (retval)
2416 			goto err_no_memory_locked;
2417 
2418 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2419 	}
2420 
2421 	/* Now process 'del_list' outside the lock */
2422 	if (!hlist_empty(&tmp_del_list)) {
2423 		filter_list_len = hw->aq.asq_buf_size /
2424 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2425 		list_size = filter_list_len *
2426 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2427 		del_list = kzalloc(list_size, GFP_ATOMIC);
2428 		if (!del_list)
2429 			goto err_no_memory;
2430 
2431 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2432 			cmd_flags = 0;
2433 
2434 			/* handle broadcast filters by updating the broadcast
2435 			 * promiscuous flag and release filter list.
2436 			 */
2437 			if (is_broadcast_ether_addr(f->macaddr)) {
2438 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2439 
2440 				hlist_del(&f->hlist);
2441 				kfree(f);
2442 				continue;
2443 			}
2444 
2445 			/* add to delete list */
2446 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2447 			if (f->vlan == I40E_VLAN_ANY) {
2448 				del_list[num_del].vlan_tag = 0;
2449 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2450 			} else {
2451 				del_list[num_del].vlan_tag =
2452 					cpu_to_le16((u16)(f->vlan));
2453 			}
2454 
2455 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2456 			del_list[num_del].flags = cmd_flags;
2457 			num_del++;
2458 
2459 			/* flush a full buffer */
2460 			if (num_del == filter_list_len) {
2461 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2462 						     num_del, &retval);
2463 				memset(del_list, 0, list_size);
2464 				num_del = 0;
2465 			}
2466 			/* Release memory for MAC filter entries which were
2467 			 * synced up with HW.
2468 			 */
2469 			hlist_del(&f->hlist);
2470 			kfree(f);
2471 		}
2472 
2473 		if (num_del) {
2474 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2475 					     num_del, &retval);
2476 		}
2477 
2478 		kfree(del_list);
2479 		del_list = NULL;
2480 	}
2481 
2482 	if (!hlist_empty(&tmp_add_list)) {
2483 		/* Do all the adds now. */
2484 		filter_list_len = hw->aq.asq_buf_size /
2485 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2486 		list_size = filter_list_len *
2487 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2488 		add_list = kzalloc(list_size, GFP_ATOMIC);
2489 		if (!add_list)
2490 			goto err_no_memory;
2491 
2492 		num_add = 0;
2493 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2494 			/* handle broadcast filters by updating the broadcast
2495 			 * promiscuous flag instead of adding a MAC filter.
2496 			 */
2497 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2498 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2499 							      new->f))
2500 					new->state = I40E_FILTER_FAILED;
2501 				else
2502 					new->state = I40E_FILTER_ACTIVE;
2503 				continue;
2504 			}
2505 
2506 			/* add to add array */
2507 			if (num_add == 0)
2508 				add_head = new;
2509 			cmd_flags = 0;
2510 			ether_addr_copy(add_list[num_add].mac_addr,
2511 					new->f->macaddr);
2512 			if (new->f->vlan == I40E_VLAN_ANY) {
2513 				add_list[num_add].vlan_tag = 0;
2514 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2515 			} else {
2516 				add_list[num_add].vlan_tag =
2517 					cpu_to_le16((u16)(new->f->vlan));
2518 			}
2519 			add_list[num_add].queue_number = 0;
2520 			/* set invalid match method for later detection */
2521 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2522 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2523 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2524 			num_add++;
2525 
2526 			/* flush a full buffer */
2527 			if (num_add == filter_list_len) {
2528 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2529 						     add_head, num_add);
2530 				memset(add_list, 0, list_size);
2531 				num_add = 0;
2532 			}
2533 		}
2534 		if (num_add) {
2535 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2536 					     num_add);
2537 		}
2538 		/* Now move all of the filters from the temp add list back to
2539 		 * the VSI's list.
2540 		 */
2541 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2542 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2543 			/* Only update the state if we're still NEW */
2544 			if (new->f->state == I40E_FILTER_NEW)
2545 				new->f->state = new->state;
2546 			hlist_del(&new->hlist);
2547 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2548 			kfree(new);
2549 		}
2550 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2551 		kfree(add_list);
2552 		add_list = NULL;
2553 	}
2554 
2555 	/* Determine the number of active and failed filters. */
2556 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2557 	vsi->active_filters = 0;
2558 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2559 		if (f->state == I40E_FILTER_ACTIVE)
2560 			vsi->active_filters++;
2561 		else if (f->state == I40E_FILTER_FAILED)
2562 			failed_filters++;
2563 	}
2564 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2565 
2566 	/* Check if we are able to exit overflow promiscuous mode. We can
2567 	 * safely exit if we didn't just enter, we no longer have any failed
2568 	 * filters, and we have reduced filters below the threshold value.
2569 	 */
2570 	if (old_overflow && !failed_filters &&
2571 	    vsi->active_filters < vsi->promisc_threshold) {
2572 		dev_info(&pf->pdev->dev,
2573 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2574 			 vsi_name);
2575 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2576 		vsi->promisc_threshold = 0;
2577 	}
2578 
2579 	/* if the VF is not trusted do not do promisc */
2580 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2581 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2582 		goto out;
2583 	}
2584 
2585 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2586 
2587 	/* If we are entering overflow promiscuous, we need to calculate a new
2588 	 * threshold for when we are safe to exit
2589 	 */
2590 	if (!old_overflow && new_overflow)
2591 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2592 
2593 	/* check for changes in promiscuous modes */
2594 	if (changed_flags & IFF_ALLMULTI) {
2595 		bool cur_multipromisc;
2596 
2597 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2598 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2599 							       vsi->seid,
2600 							       cur_multipromisc,
2601 							       NULL);
2602 		if (aq_ret) {
2603 			retval = i40e_aq_rc_to_posix(aq_ret,
2604 						     hw->aq.asq_last_status);
2605 			dev_info(&pf->pdev->dev,
2606 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2607 				 vsi_name,
2608 				 i40e_stat_str(hw, aq_ret),
2609 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2610 		} else {
2611 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2612 				 cur_multipromisc ? "entering" : "leaving");
2613 		}
2614 	}
2615 
2616 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2617 		bool cur_promisc;
2618 
2619 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2620 			       new_overflow);
2621 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2622 		if (aq_ret) {
2623 			retval = i40e_aq_rc_to_posix(aq_ret,
2624 						     hw->aq.asq_last_status);
2625 			dev_info(&pf->pdev->dev,
2626 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2627 				 cur_promisc ? "on" : "off",
2628 				 vsi_name,
2629 				 i40e_stat_str(hw, aq_ret),
2630 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2631 		}
2632 	}
2633 out:
2634 	/* if something went wrong then set the changed flag so we try again */
2635 	if (retval)
2636 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2637 
2638 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2639 	return retval;
2640 
2641 err_no_memory:
2642 	/* Restore elements on the temporary add and delete lists */
2643 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2644 err_no_memory_locked:
2645 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2646 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2647 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2648 
2649 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2650 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2651 	return -ENOMEM;
2652 }
2653 
2654 /**
2655  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2656  * @pf: board private structure
2657  **/
2658 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2659 {
2660 	int v;
2661 
2662 	if (!pf)
2663 		return;
2664 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2665 		return;
2666 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2667 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2668 		return;
2669 	}
2670 
2671 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2672 		if (pf->vsi[v] &&
2673 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2674 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2675 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2676 
2677 			if (ret) {
2678 				/* come back and try again later */
2679 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2680 					pf->state);
2681 				break;
2682 			}
2683 		}
2684 	}
2685 }
2686 
2687 /**
2688  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2689  * @vsi: the vsi
2690  **/
2691 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2692 {
2693 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2694 		return I40E_RXBUFFER_2048;
2695 	else
2696 		return I40E_RXBUFFER_3072;
2697 }
2698 
2699 /**
2700  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2701  * @netdev: network interface device structure
2702  * @new_mtu: new value for maximum frame size
2703  *
2704  * Returns 0 on success, negative on failure
2705  **/
2706 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2707 {
2708 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2709 	struct i40e_vsi *vsi = np->vsi;
2710 	struct i40e_pf *pf = vsi->back;
2711 
2712 	if (i40e_enabled_xdp_vsi(vsi)) {
2713 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2714 
2715 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2716 			return -EINVAL;
2717 	}
2718 
2719 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2720 		   netdev->mtu, new_mtu);
2721 	netdev->mtu = new_mtu;
2722 	if (netif_running(netdev))
2723 		i40e_vsi_reinit_locked(vsi);
2724 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2725 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2726 	return 0;
2727 }
2728 
2729 /**
2730  * i40e_ioctl - Access the hwtstamp interface
2731  * @netdev: network interface device structure
2732  * @ifr: interface request data
2733  * @cmd: ioctl command
2734  **/
2735 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2736 {
2737 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2738 	struct i40e_pf *pf = np->vsi->back;
2739 
2740 	switch (cmd) {
2741 	case SIOCGHWTSTAMP:
2742 		return i40e_ptp_get_ts_config(pf, ifr);
2743 	case SIOCSHWTSTAMP:
2744 		return i40e_ptp_set_ts_config(pf, ifr);
2745 	default:
2746 		return -EOPNOTSUPP;
2747 	}
2748 }
2749 
2750 /**
2751  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2752  * @vsi: the vsi being adjusted
2753  **/
2754 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2755 {
2756 	struct i40e_vsi_context ctxt;
2757 	i40e_status ret;
2758 
2759 	/* Don't modify stripping options if a port VLAN is active */
2760 	if (vsi->info.pvid)
2761 		return;
2762 
2763 	if ((vsi->info.valid_sections &
2764 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2765 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2766 		return;  /* already enabled */
2767 
2768 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2769 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2770 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2771 
2772 	ctxt.seid = vsi->seid;
2773 	ctxt.info = vsi->info;
2774 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2775 	if (ret) {
2776 		dev_info(&vsi->back->pdev->dev,
2777 			 "update vlan stripping failed, err %s aq_err %s\n",
2778 			 i40e_stat_str(&vsi->back->hw, ret),
2779 			 i40e_aq_str(&vsi->back->hw,
2780 				     vsi->back->hw.aq.asq_last_status));
2781 	}
2782 }
2783 
2784 /**
2785  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2786  * @vsi: the vsi being adjusted
2787  **/
2788 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2789 {
2790 	struct i40e_vsi_context ctxt;
2791 	i40e_status ret;
2792 
2793 	/* Don't modify stripping options if a port VLAN is active */
2794 	if (vsi->info.pvid)
2795 		return;
2796 
2797 	if ((vsi->info.valid_sections &
2798 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2799 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2800 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2801 		return;  /* already disabled */
2802 
2803 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2804 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2805 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2806 
2807 	ctxt.seid = vsi->seid;
2808 	ctxt.info = vsi->info;
2809 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2810 	if (ret) {
2811 		dev_info(&vsi->back->pdev->dev,
2812 			 "update vlan stripping failed, err %s aq_err %s\n",
2813 			 i40e_stat_str(&vsi->back->hw, ret),
2814 			 i40e_aq_str(&vsi->back->hw,
2815 				     vsi->back->hw.aq.asq_last_status));
2816 	}
2817 }
2818 
2819 /**
2820  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2821  * @vsi: the vsi being configured
2822  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2823  *
2824  * This is a helper function for adding a new MAC/VLAN filter with the
2825  * specified VLAN for each existing MAC address already in the hash table.
2826  * This function does *not* perform any accounting to update filters based on
2827  * VLAN mode.
2828  *
2829  * NOTE: this function expects to be called while under the
2830  * mac_filter_hash_lock
2831  **/
2832 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2833 {
2834 	struct i40e_mac_filter *f, *add_f;
2835 	struct hlist_node *h;
2836 	int bkt;
2837 
2838 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2839 		if (f->state == I40E_FILTER_REMOVE)
2840 			continue;
2841 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2842 		if (!add_f) {
2843 			dev_info(&vsi->back->pdev->dev,
2844 				 "Could not add vlan filter %d for %pM\n",
2845 				 vid, f->macaddr);
2846 			return -ENOMEM;
2847 		}
2848 	}
2849 
2850 	return 0;
2851 }
2852 
2853 /**
2854  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2855  * @vsi: the VSI being configured
2856  * @vid: VLAN id to be added
2857  **/
2858 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2859 {
2860 	int err;
2861 
2862 	if (vsi->info.pvid)
2863 		return -EINVAL;
2864 
2865 	/* The network stack will attempt to add VID=0, with the intention to
2866 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2867 	 * these packets by default when configured to receive untagged
2868 	 * packets, so we don't need to add a filter for this case.
2869 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2870 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2871 	 * Thus, we do not want to actually add a filter for VID=0
2872 	 */
2873 	if (!vid)
2874 		return 0;
2875 
2876 	/* Locked once because all functions invoked below iterates list*/
2877 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2878 	err = i40e_add_vlan_all_mac(vsi, vid);
2879 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2880 	if (err)
2881 		return err;
2882 
2883 	/* schedule our worker thread which will take care of
2884 	 * applying the new filter changes
2885 	 */
2886 	i40e_service_event_schedule(vsi->back);
2887 	return 0;
2888 }
2889 
2890 /**
2891  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2892  * @vsi: the vsi being configured
2893  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2894  *
2895  * This function should be used to remove all VLAN filters which match the
2896  * given VID. It does not schedule the service event and does not take the
2897  * mac_filter_hash_lock so it may be combined with other operations under
2898  * a single invocation of the mac_filter_hash_lock.
2899  *
2900  * NOTE: this function expects to be called while under the
2901  * mac_filter_hash_lock
2902  */
2903 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2904 {
2905 	struct i40e_mac_filter *f;
2906 	struct hlist_node *h;
2907 	int bkt;
2908 
2909 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2910 		if (f->vlan == vid)
2911 			__i40e_del_filter(vsi, f);
2912 	}
2913 }
2914 
2915 /**
2916  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2917  * @vsi: the VSI being configured
2918  * @vid: VLAN id to be removed
2919  **/
2920 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2921 {
2922 	if (!vid || vsi->info.pvid)
2923 		return;
2924 
2925 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2926 	i40e_rm_vlan_all_mac(vsi, vid);
2927 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2928 
2929 	/* schedule our worker thread which will take care of
2930 	 * applying the new filter changes
2931 	 */
2932 	i40e_service_event_schedule(vsi->back);
2933 }
2934 
2935 /**
2936  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2937  * @netdev: network interface to be adjusted
2938  * @proto: unused protocol value
2939  * @vid: vlan id to be added
2940  *
2941  * net_device_ops implementation for adding vlan ids
2942  **/
2943 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2944 				__always_unused __be16 proto, u16 vid)
2945 {
2946 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2947 	struct i40e_vsi *vsi = np->vsi;
2948 	int ret = 0;
2949 
2950 	if (vid >= VLAN_N_VID)
2951 		return -EINVAL;
2952 
2953 	ret = i40e_vsi_add_vlan(vsi, vid);
2954 	if (!ret)
2955 		set_bit(vid, vsi->active_vlans);
2956 
2957 	return ret;
2958 }
2959 
2960 /**
2961  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2962  * @netdev: network interface to be adjusted
2963  * @proto: unused protocol value
2964  * @vid: vlan id to be added
2965  **/
2966 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2967 				    __always_unused __be16 proto, u16 vid)
2968 {
2969 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2970 	struct i40e_vsi *vsi = np->vsi;
2971 
2972 	if (vid >= VLAN_N_VID)
2973 		return;
2974 	set_bit(vid, vsi->active_vlans);
2975 }
2976 
2977 /**
2978  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2979  * @netdev: network interface to be adjusted
2980  * @proto: unused protocol value
2981  * @vid: vlan id to be removed
2982  *
2983  * net_device_ops implementation for removing vlan ids
2984  **/
2985 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2986 				 __always_unused __be16 proto, u16 vid)
2987 {
2988 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2989 	struct i40e_vsi *vsi = np->vsi;
2990 
2991 	/* return code is ignored as there is nothing a user
2992 	 * can do about failure to remove and a log message was
2993 	 * already printed from the other function
2994 	 */
2995 	i40e_vsi_kill_vlan(vsi, vid);
2996 
2997 	clear_bit(vid, vsi->active_vlans);
2998 
2999 	return 0;
3000 }
3001 
3002 /**
3003  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3004  * @vsi: the vsi being brought back up
3005  **/
3006 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3007 {
3008 	u16 vid;
3009 
3010 	if (!vsi->netdev)
3011 		return;
3012 
3013 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3014 		i40e_vlan_stripping_enable(vsi);
3015 	else
3016 		i40e_vlan_stripping_disable(vsi);
3017 
3018 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3019 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3020 					vid);
3021 }
3022 
3023 /**
3024  * i40e_vsi_add_pvid - Add pvid for the VSI
3025  * @vsi: the vsi being adjusted
3026  * @vid: the vlan id to set as a PVID
3027  **/
3028 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3029 {
3030 	struct i40e_vsi_context ctxt;
3031 	i40e_status ret;
3032 
3033 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3034 	vsi->info.pvid = cpu_to_le16(vid);
3035 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3036 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3037 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3038 
3039 	ctxt.seid = vsi->seid;
3040 	ctxt.info = vsi->info;
3041 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3042 	if (ret) {
3043 		dev_info(&vsi->back->pdev->dev,
3044 			 "add pvid failed, err %s aq_err %s\n",
3045 			 i40e_stat_str(&vsi->back->hw, ret),
3046 			 i40e_aq_str(&vsi->back->hw,
3047 				     vsi->back->hw.aq.asq_last_status));
3048 		return -ENOENT;
3049 	}
3050 
3051 	return 0;
3052 }
3053 
3054 /**
3055  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3056  * @vsi: the vsi being adjusted
3057  *
3058  * Just use the vlan_rx_register() service to put it back to normal
3059  **/
3060 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3061 {
3062 	vsi->info.pvid = 0;
3063 
3064 	i40e_vlan_stripping_disable(vsi);
3065 }
3066 
3067 /**
3068  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3069  * @vsi: ptr to the VSI
3070  *
3071  * If this function returns with an error, then it's possible one or
3072  * more of the rings is populated (while the rest are not).  It is the
3073  * callers duty to clean those orphaned rings.
3074  *
3075  * Return 0 on success, negative on failure
3076  **/
3077 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3078 {
3079 	int i, err = 0;
3080 
3081 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3082 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3083 
3084 	if (!i40e_enabled_xdp_vsi(vsi))
3085 		return err;
3086 
3087 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3088 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3089 
3090 	return err;
3091 }
3092 
3093 /**
3094  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3095  * @vsi: ptr to the VSI
3096  *
3097  * Free VSI's transmit software resources
3098  **/
3099 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3100 {
3101 	int i;
3102 
3103 	if (vsi->tx_rings) {
3104 		for (i = 0; i < vsi->num_queue_pairs; i++)
3105 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3106 				i40e_free_tx_resources(vsi->tx_rings[i]);
3107 	}
3108 
3109 	if (vsi->xdp_rings) {
3110 		for (i = 0; i < vsi->num_queue_pairs; i++)
3111 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3112 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3113 	}
3114 }
3115 
3116 /**
3117  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3118  * @vsi: ptr to the VSI
3119  *
3120  * If this function returns with an error, then it's possible one or
3121  * more of the rings is populated (while the rest are not).  It is the
3122  * callers duty to clean those orphaned rings.
3123  *
3124  * Return 0 on success, negative on failure
3125  **/
3126 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3127 {
3128 	int i, err = 0;
3129 
3130 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3131 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3132 	return err;
3133 }
3134 
3135 /**
3136  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3137  * @vsi: ptr to the VSI
3138  *
3139  * Free all receive software resources
3140  **/
3141 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3142 {
3143 	int i;
3144 
3145 	if (!vsi->rx_rings)
3146 		return;
3147 
3148 	for (i = 0; i < vsi->num_queue_pairs; i++)
3149 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3150 			i40e_free_rx_resources(vsi->rx_rings[i]);
3151 }
3152 
3153 /**
3154  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3155  * @ring: The Tx ring to configure
3156  *
3157  * This enables/disables XPS for a given Tx descriptor ring
3158  * based on the TCs enabled for the VSI that ring belongs to.
3159  **/
3160 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3161 {
3162 	int cpu;
3163 
3164 	if (!ring->q_vector || !ring->netdev || ring->ch)
3165 		return;
3166 
3167 	/* We only initialize XPS once, so as not to overwrite user settings */
3168 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3169 		return;
3170 
3171 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3172 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3173 			    ring->queue_index);
3174 }
3175 
3176 /**
3177  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3178  * @ring: The Tx or Rx ring
3179  *
3180  * Returns the AF_XDP buffer pool or NULL.
3181  **/
3182 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3183 {
3184 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3185 	int qid = ring->queue_index;
3186 
3187 	if (ring_is_xdp(ring))
3188 		qid -= ring->vsi->alloc_queue_pairs;
3189 
3190 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3191 		return NULL;
3192 
3193 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3194 }
3195 
3196 /**
3197  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3198  * @ring: The Tx ring to configure
3199  *
3200  * Configure the Tx descriptor ring in the HMC context.
3201  **/
3202 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3203 {
3204 	struct i40e_vsi *vsi = ring->vsi;
3205 	u16 pf_q = vsi->base_queue + ring->queue_index;
3206 	struct i40e_hw *hw = &vsi->back->hw;
3207 	struct i40e_hmc_obj_txq tx_ctx;
3208 	i40e_status err = 0;
3209 	u32 qtx_ctl = 0;
3210 
3211 	if (ring_is_xdp(ring))
3212 		ring->xsk_pool = i40e_xsk_pool(ring);
3213 
3214 	/* some ATR related tx ring init */
3215 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3216 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3217 		ring->atr_count = 0;
3218 	} else {
3219 		ring->atr_sample_rate = 0;
3220 	}
3221 
3222 	/* configure XPS */
3223 	i40e_config_xps_tx_ring(ring);
3224 
3225 	/* clear the context structure first */
3226 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3227 
3228 	tx_ctx.new_context = 1;
3229 	tx_ctx.base = (ring->dma / 128);
3230 	tx_ctx.qlen = ring->count;
3231 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3232 					       I40E_FLAG_FD_ATR_ENABLED));
3233 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3234 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3235 	if (vsi->type != I40E_VSI_FDIR)
3236 		tx_ctx.head_wb_ena = 1;
3237 	tx_ctx.head_wb_addr = ring->dma +
3238 			      (ring->count * sizeof(struct i40e_tx_desc));
3239 
3240 	/* As part of VSI creation/update, FW allocates certain
3241 	 * Tx arbitration queue sets for each TC enabled for
3242 	 * the VSI. The FW returns the handles to these queue
3243 	 * sets as part of the response buffer to Add VSI,
3244 	 * Update VSI, etc. AQ commands. It is expected that
3245 	 * these queue set handles be associated with the Tx
3246 	 * queues by the driver as part of the TX queue context
3247 	 * initialization. This has to be done regardless of
3248 	 * DCB as by default everything is mapped to TC0.
3249 	 */
3250 
3251 	if (ring->ch)
3252 		tx_ctx.rdylist =
3253 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3254 
3255 	else
3256 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3257 
3258 	tx_ctx.rdylist_act = 0;
3259 
3260 	/* clear the context in the HMC */
3261 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3262 	if (err) {
3263 		dev_info(&vsi->back->pdev->dev,
3264 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3265 			 ring->queue_index, pf_q, err);
3266 		return -ENOMEM;
3267 	}
3268 
3269 	/* set the context in the HMC */
3270 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3271 	if (err) {
3272 		dev_info(&vsi->back->pdev->dev,
3273 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3274 			 ring->queue_index, pf_q, err);
3275 		return -ENOMEM;
3276 	}
3277 
3278 	/* Now associate this queue with this PCI function */
3279 	if (ring->ch) {
3280 		if (ring->ch->type == I40E_VSI_VMDQ2)
3281 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3282 		else
3283 			return -EINVAL;
3284 
3285 		qtx_ctl |= (ring->ch->vsi_number <<
3286 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3287 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3288 	} else {
3289 		if (vsi->type == I40E_VSI_VMDQ2) {
3290 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3291 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3292 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3293 		} else {
3294 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3295 		}
3296 	}
3297 
3298 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3299 		    I40E_QTX_CTL_PF_INDX_MASK);
3300 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3301 	i40e_flush(hw);
3302 
3303 	/* cache tail off for easier writes later */
3304 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3305 
3306 	return 0;
3307 }
3308 
3309 /**
3310  * i40e_rx_offset - Return expected offset into page to access data
3311  * @rx_ring: Ring we are requesting offset of
3312  *
3313  * Returns the offset value for ring into the data buffer.
3314  */
3315 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3316 {
3317 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3318 }
3319 
3320 /**
3321  * i40e_configure_rx_ring - Configure a receive ring context
3322  * @ring: The Rx ring to configure
3323  *
3324  * Configure the Rx descriptor ring in the HMC context.
3325  **/
3326 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3327 {
3328 	struct i40e_vsi *vsi = ring->vsi;
3329 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3330 	u16 pf_q = vsi->base_queue + ring->queue_index;
3331 	struct i40e_hw *hw = &vsi->back->hw;
3332 	struct i40e_hmc_obj_rxq rx_ctx;
3333 	i40e_status err = 0;
3334 	bool ok;
3335 	int ret;
3336 
3337 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3338 
3339 	/* clear the context structure first */
3340 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3341 
3342 	if (ring->vsi->type == I40E_VSI_MAIN)
3343 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3344 
3345 	kfree(ring->rx_bi);
3346 	ring->xsk_pool = i40e_xsk_pool(ring);
3347 	if (ring->xsk_pool) {
3348 		ret = i40e_alloc_rx_bi_zc(ring);
3349 		if (ret)
3350 			return ret;
3351 		ring->rx_buf_len =
3352 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3353 		/* For AF_XDP ZC, we disallow packets to span on
3354 		 * multiple buffers, thus letting us skip that
3355 		 * handling in the fast-path.
3356 		 */
3357 		chain_len = 1;
3358 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3359 						 MEM_TYPE_XSK_BUFF_POOL,
3360 						 NULL);
3361 		if (ret)
3362 			return ret;
3363 		dev_info(&vsi->back->pdev->dev,
3364 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3365 			 ring->queue_index);
3366 
3367 	} else {
3368 		ret = i40e_alloc_rx_bi(ring);
3369 		if (ret)
3370 			return ret;
3371 		ring->rx_buf_len = vsi->rx_buf_len;
3372 		if (ring->vsi->type == I40E_VSI_MAIN) {
3373 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3374 							 MEM_TYPE_PAGE_SHARED,
3375 							 NULL);
3376 			if (ret)
3377 				return ret;
3378 		}
3379 	}
3380 
3381 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3382 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3383 
3384 	rx_ctx.base = (ring->dma / 128);
3385 	rx_ctx.qlen = ring->count;
3386 
3387 	/* use 16 byte descriptors */
3388 	rx_ctx.dsize = 0;
3389 
3390 	/* descriptor type is always zero
3391 	 * rx_ctx.dtype = 0;
3392 	 */
3393 	rx_ctx.hsplit_0 = 0;
3394 
3395 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3396 	if (hw->revision_id == 0)
3397 		rx_ctx.lrxqthresh = 0;
3398 	else
3399 		rx_ctx.lrxqthresh = 1;
3400 	rx_ctx.crcstrip = 1;
3401 	rx_ctx.l2tsel = 1;
3402 	/* this controls whether VLAN is stripped from inner headers */
3403 	rx_ctx.showiv = 0;
3404 	/* set the prefena field to 1 because the manual says to */
3405 	rx_ctx.prefena = 1;
3406 
3407 	/* clear the context in the HMC */
3408 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3409 	if (err) {
3410 		dev_info(&vsi->back->pdev->dev,
3411 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3412 			 ring->queue_index, pf_q, err);
3413 		return -ENOMEM;
3414 	}
3415 
3416 	/* set the context in the HMC */
3417 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3418 	if (err) {
3419 		dev_info(&vsi->back->pdev->dev,
3420 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3421 			 ring->queue_index, pf_q, err);
3422 		return -ENOMEM;
3423 	}
3424 
3425 	/* configure Rx buffer alignment */
3426 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3427 		clear_ring_build_skb_enabled(ring);
3428 	else
3429 		set_ring_build_skb_enabled(ring);
3430 
3431 	ring->rx_offset = i40e_rx_offset(ring);
3432 
3433 	/* cache tail for quicker writes, and clear the reg before use */
3434 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3435 	writel(0, ring->tail);
3436 
3437 	if (ring->xsk_pool) {
3438 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3439 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3440 	} else {
3441 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3442 	}
3443 	if (!ok) {
3444 		/* Log this in case the user has forgotten to give the kernel
3445 		 * any buffers, even later in the application.
3446 		 */
3447 		dev_info(&vsi->back->pdev->dev,
3448 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3449 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3450 			 ring->queue_index, pf_q);
3451 	}
3452 
3453 	return 0;
3454 }
3455 
3456 /**
3457  * i40e_vsi_configure_tx - Configure the VSI for Tx
3458  * @vsi: VSI structure describing this set of rings and resources
3459  *
3460  * Configure the Tx VSI for operation.
3461  **/
3462 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3463 {
3464 	int err = 0;
3465 	u16 i;
3466 
3467 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3468 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3469 
3470 	if (err || !i40e_enabled_xdp_vsi(vsi))
3471 		return err;
3472 
3473 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3474 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3475 
3476 	return err;
3477 }
3478 
3479 /**
3480  * i40e_vsi_configure_rx - Configure the VSI for Rx
3481  * @vsi: the VSI being configured
3482  *
3483  * Configure the Rx VSI for operation.
3484  **/
3485 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3486 {
3487 	int err = 0;
3488 	u16 i;
3489 
3490 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3491 		vsi->max_frame = I40E_MAX_RXBUFFER;
3492 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3493 #if (PAGE_SIZE < 8192)
3494 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3495 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3496 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3497 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3498 #endif
3499 	} else {
3500 		vsi->max_frame = I40E_MAX_RXBUFFER;
3501 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3502 						       I40E_RXBUFFER_2048;
3503 	}
3504 
3505 	/* set up individual rings */
3506 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3507 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3508 
3509 	return err;
3510 }
3511 
3512 /**
3513  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3514  * @vsi: ptr to the VSI
3515  **/
3516 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3517 {
3518 	struct i40e_ring *tx_ring, *rx_ring;
3519 	u16 qoffset, qcount;
3520 	int i, n;
3521 
3522 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3523 		/* Reset the TC information */
3524 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3525 			rx_ring = vsi->rx_rings[i];
3526 			tx_ring = vsi->tx_rings[i];
3527 			rx_ring->dcb_tc = 0;
3528 			tx_ring->dcb_tc = 0;
3529 		}
3530 		return;
3531 	}
3532 
3533 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3534 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3535 			continue;
3536 
3537 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3538 		qcount = vsi->tc_config.tc_info[n].qcount;
3539 		for (i = qoffset; i < (qoffset + qcount); i++) {
3540 			rx_ring = vsi->rx_rings[i];
3541 			tx_ring = vsi->tx_rings[i];
3542 			rx_ring->dcb_tc = n;
3543 			tx_ring->dcb_tc = n;
3544 		}
3545 	}
3546 }
3547 
3548 /**
3549  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3550  * @vsi: ptr to the VSI
3551  **/
3552 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3553 {
3554 	if (vsi->netdev)
3555 		i40e_set_rx_mode(vsi->netdev);
3556 }
3557 
3558 /**
3559  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3560  * @pf: Pointer to the targeted PF
3561  *
3562  * Set all flow director counters to 0.
3563  */
3564 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3565 {
3566 	pf->fd_tcp4_filter_cnt = 0;
3567 	pf->fd_udp4_filter_cnt = 0;
3568 	pf->fd_sctp4_filter_cnt = 0;
3569 	pf->fd_ip4_filter_cnt = 0;
3570 	pf->fd_tcp6_filter_cnt = 0;
3571 	pf->fd_udp6_filter_cnt = 0;
3572 	pf->fd_sctp6_filter_cnt = 0;
3573 	pf->fd_ip6_filter_cnt = 0;
3574 }
3575 
3576 /**
3577  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3578  * @vsi: Pointer to the targeted VSI
3579  *
3580  * This function replays the hlist on the hw where all the SB Flow Director
3581  * filters were saved.
3582  **/
3583 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3584 {
3585 	struct i40e_fdir_filter *filter;
3586 	struct i40e_pf *pf = vsi->back;
3587 	struct hlist_node *node;
3588 
3589 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3590 		return;
3591 
3592 	/* Reset FDir counters as we're replaying all existing filters */
3593 	i40e_reset_fdir_filter_cnt(pf);
3594 
3595 	hlist_for_each_entry_safe(filter, node,
3596 				  &pf->fdir_filter_list, fdir_node) {
3597 		i40e_add_del_fdir(vsi, filter, true);
3598 	}
3599 }
3600 
3601 /**
3602  * i40e_vsi_configure - Set up the VSI for action
3603  * @vsi: the VSI being configured
3604  **/
3605 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3606 {
3607 	int err;
3608 
3609 	i40e_set_vsi_rx_mode(vsi);
3610 	i40e_restore_vlan(vsi);
3611 	i40e_vsi_config_dcb_rings(vsi);
3612 	err = i40e_vsi_configure_tx(vsi);
3613 	if (!err)
3614 		err = i40e_vsi_configure_rx(vsi);
3615 
3616 	return err;
3617 }
3618 
3619 /**
3620  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3621  * @vsi: the VSI being configured
3622  **/
3623 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3624 {
3625 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3626 	struct i40e_pf *pf = vsi->back;
3627 	struct i40e_hw *hw = &pf->hw;
3628 	u16 vector;
3629 	int i, q;
3630 	u32 qp;
3631 
3632 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3633 	 * and PFINT_LNKLSTn registers, e.g.:
3634 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3635 	 */
3636 	qp = vsi->base_queue;
3637 	vector = vsi->base_vector;
3638 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3639 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3640 
3641 		q_vector->rx.next_update = jiffies + 1;
3642 		q_vector->rx.target_itr =
3643 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3644 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3645 		     q_vector->rx.target_itr >> 1);
3646 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3647 
3648 		q_vector->tx.next_update = jiffies + 1;
3649 		q_vector->tx.target_itr =
3650 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3651 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3652 		     q_vector->tx.target_itr >> 1);
3653 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3654 
3655 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3656 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3657 
3658 		/* Linked list for the queuepairs assigned to this vector */
3659 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3660 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3661 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3662 			u32 val;
3663 
3664 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3665 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3666 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3667 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3668 			      (I40E_QUEUE_TYPE_TX <<
3669 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3670 
3671 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3672 
3673 			if (has_xdp) {
3674 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3675 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3676 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3677 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3678 				      (I40E_QUEUE_TYPE_TX <<
3679 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3680 
3681 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3682 			}
3683 
3684 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3685 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3686 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3687 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3688 			      (I40E_QUEUE_TYPE_RX <<
3689 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3690 
3691 			/* Terminate the linked list */
3692 			if (q == (q_vector->num_ringpairs - 1))
3693 				val |= (I40E_QUEUE_END_OF_LIST <<
3694 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3695 
3696 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3697 			qp++;
3698 		}
3699 	}
3700 
3701 	i40e_flush(hw);
3702 }
3703 
3704 /**
3705  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3706  * @pf: pointer to private device data structure
3707  **/
3708 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3709 {
3710 	struct i40e_hw *hw = &pf->hw;
3711 	u32 val;
3712 
3713 	/* clear things first */
3714 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3715 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3716 
3717 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3718 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3719 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3720 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3721 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3722 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3723 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3724 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3725 
3726 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3727 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3728 
3729 	if (pf->flags & I40E_FLAG_PTP)
3730 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3731 
3732 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3733 
3734 	/* SW_ITR_IDX = 0, but don't change INTENA */
3735 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3736 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3737 
3738 	/* OTHER_ITR_IDX = 0 */
3739 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3740 }
3741 
3742 /**
3743  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3744  * @vsi: the VSI being configured
3745  **/
3746 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3747 {
3748 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3749 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3750 	struct i40e_pf *pf = vsi->back;
3751 	struct i40e_hw *hw = &pf->hw;
3752 	u32 val;
3753 
3754 	/* set the ITR configuration */
3755 	q_vector->rx.next_update = jiffies + 1;
3756 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3757 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3758 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3759 	q_vector->tx.next_update = jiffies + 1;
3760 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3761 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3762 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3763 
3764 	i40e_enable_misc_int_causes(pf);
3765 
3766 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3767 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3768 
3769 	/* Associate the queue pair to the vector and enable the queue int */
3770 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3771 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3772 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3773 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3774 
3775 	wr32(hw, I40E_QINT_RQCTL(0), val);
3776 
3777 	if (i40e_enabled_xdp_vsi(vsi)) {
3778 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3779 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3780 		      (I40E_QUEUE_TYPE_TX
3781 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3782 
3783 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3784 	}
3785 
3786 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3787 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3788 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3789 
3790 	wr32(hw, I40E_QINT_TQCTL(0), val);
3791 	i40e_flush(hw);
3792 }
3793 
3794 /**
3795  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3796  * @pf: board private structure
3797  **/
3798 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3799 {
3800 	struct i40e_hw *hw = &pf->hw;
3801 
3802 	wr32(hw, I40E_PFINT_DYN_CTL0,
3803 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3804 	i40e_flush(hw);
3805 }
3806 
3807 /**
3808  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3809  * @pf: board private structure
3810  **/
3811 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3812 {
3813 	struct i40e_hw *hw = &pf->hw;
3814 	u32 val;
3815 
3816 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3817 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3818 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3819 
3820 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3821 	i40e_flush(hw);
3822 }
3823 
3824 /**
3825  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3826  * @irq: interrupt number
3827  * @data: pointer to a q_vector
3828  **/
3829 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3830 {
3831 	struct i40e_q_vector *q_vector = data;
3832 
3833 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3834 		return IRQ_HANDLED;
3835 
3836 	napi_schedule_irqoff(&q_vector->napi);
3837 
3838 	return IRQ_HANDLED;
3839 }
3840 
3841 /**
3842  * i40e_irq_affinity_notify - Callback for affinity changes
3843  * @notify: context as to what irq was changed
3844  * @mask: the new affinity mask
3845  *
3846  * This is a callback function used by the irq_set_affinity_notifier function
3847  * so that we may register to receive changes to the irq affinity masks.
3848  **/
3849 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3850 				     const cpumask_t *mask)
3851 {
3852 	struct i40e_q_vector *q_vector =
3853 		container_of(notify, struct i40e_q_vector, affinity_notify);
3854 
3855 	cpumask_copy(&q_vector->affinity_mask, mask);
3856 }
3857 
3858 /**
3859  * i40e_irq_affinity_release - Callback for affinity notifier release
3860  * @ref: internal core kernel usage
3861  *
3862  * This is a callback function used by the irq_set_affinity_notifier function
3863  * to inform the current notification subscriber that they will no longer
3864  * receive notifications.
3865  **/
3866 static void i40e_irq_affinity_release(struct kref *ref) {}
3867 
3868 /**
3869  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3870  * @vsi: the VSI being configured
3871  * @basename: name for the vector
3872  *
3873  * Allocates MSI-X vectors and requests interrupts from the kernel.
3874  **/
3875 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3876 {
3877 	int q_vectors = vsi->num_q_vectors;
3878 	struct i40e_pf *pf = vsi->back;
3879 	int base = vsi->base_vector;
3880 	int rx_int_idx = 0;
3881 	int tx_int_idx = 0;
3882 	int vector, err;
3883 	int irq_num;
3884 	int cpu;
3885 
3886 	for (vector = 0; vector < q_vectors; vector++) {
3887 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3888 
3889 		irq_num = pf->msix_entries[base + vector].vector;
3890 
3891 		if (q_vector->tx.ring && q_vector->rx.ring) {
3892 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3893 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3894 			tx_int_idx++;
3895 		} else if (q_vector->rx.ring) {
3896 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3897 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3898 		} else if (q_vector->tx.ring) {
3899 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3900 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3901 		} else {
3902 			/* skip this unused q_vector */
3903 			continue;
3904 		}
3905 		err = request_irq(irq_num,
3906 				  vsi->irq_handler,
3907 				  0,
3908 				  q_vector->name,
3909 				  q_vector);
3910 		if (err) {
3911 			dev_info(&pf->pdev->dev,
3912 				 "MSIX request_irq failed, error: %d\n", err);
3913 			goto free_queue_irqs;
3914 		}
3915 
3916 		/* register for affinity change notifications */
3917 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3918 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3919 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3920 		/* Spread affinity hints out across online CPUs.
3921 		 *
3922 		 * get_cpu_mask returns a static constant mask with
3923 		 * a permanent lifetime so it's ok to pass to
3924 		 * irq_update_affinity_hint without making a copy.
3925 		 */
3926 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3927 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
3928 	}
3929 
3930 	vsi->irqs_ready = true;
3931 	return 0;
3932 
3933 free_queue_irqs:
3934 	while (vector) {
3935 		vector--;
3936 		irq_num = pf->msix_entries[base + vector].vector;
3937 		irq_set_affinity_notifier(irq_num, NULL);
3938 		irq_update_affinity_hint(irq_num, NULL);
3939 		free_irq(irq_num, &vsi->q_vectors[vector]);
3940 	}
3941 	return err;
3942 }
3943 
3944 /**
3945  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3946  * @vsi: the VSI being un-configured
3947  **/
3948 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3949 {
3950 	struct i40e_pf *pf = vsi->back;
3951 	struct i40e_hw *hw = &pf->hw;
3952 	int base = vsi->base_vector;
3953 	int i;
3954 
3955 	/* disable interrupt causation from each queue */
3956 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3957 		u32 val;
3958 
3959 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3960 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3961 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3962 
3963 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3964 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3965 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3966 
3967 		if (!i40e_enabled_xdp_vsi(vsi))
3968 			continue;
3969 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3970 	}
3971 
3972 	/* disable each interrupt */
3973 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3974 		for (i = vsi->base_vector;
3975 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3976 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3977 
3978 		i40e_flush(hw);
3979 		for (i = 0; i < vsi->num_q_vectors; i++)
3980 			synchronize_irq(pf->msix_entries[i + base].vector);
3981 	} else {
3982 		/* Legacy and MSI mode - this stops all interrupt handling */
3983 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3984 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3985 		i40e_flush(hw);
3986 		synchronize_irq(pf->pdev->irq);
3987 	}
3988 }
3989 
3990 /**
3991  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3992  * @vsi: the VSI being configured
3993  **/
3994 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3995 {
3996 	struct i40e_pf *pf = vsi->back;
3997 	int i;
3998 
3999 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4000 		for (i = 0; i < vsi->num_q_vectors; i++)
4001 			i40e_irq_dynamic_enable(vsi, i);
4002 	} else {
4003 		i40e_irq_dynamic_enable_icr0(pf);
4004 	}
4005 
4006 	i40e_flush(&pf->hw);
4007 	return 0;
4008 }
4009 
4010 /**
4011  * i40e_free_misc_vector - Free the vector that handles non-queue events
4012  * @pf: board private structure
4013  **/
4014 static void i40e_free_misc_vector(struct i40e_pf *pf)
4015 {
4016 	/* Disable ICR 0 */
4017 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4018 	i40e_flush(&pf->hw);
4019 
4020 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4021 		synchronize_irq(pf->msix_entries[0].vector);
4022 		free_irq(pf->msix_entries[0].vector, pf);
4023 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4024 	}
4025 }
4026 
4027 /**
4028  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4029  * @irq: interrupt number
4030  * @data: pointer to a q_vector
4031  *
4032  * This is the handler used for all MSI/Legacy interrupts, and deals
4033  * with both queue and non-queue interrupts.  This is also used in
4034  * MSIX mode to handle the non-queue interrupts.
4035  **/
4036 static irqreturn_t i40e_intr(int irq, void *data)
4037 {
4038 	struct i40e_pf *pf = (struct i40e_pf *)data;
4039 	struct i40e_hw *hw = &pf->hw;
4040 	irqreturn_t ret = IRQ_NONE;
4041 	u32 icr0, icr0_remaining;
4042 	u32 val, ena_mask;
4043 
4044 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4045 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4046 
4047 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4048 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4049 		goto enable_intr;
4050 
4051 	/* if interrupt but no bits showing, must be SWINT */
4052 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4053 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4054 		pf->sw_int_count++;
4055 
4056 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4057 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4058 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4059 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4060 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4061 	}
4062 
4063 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4064 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4065 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4066 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4067 
4068 		/* We do not have a way to disarm Queue causes while leaving
4069 		 * interrupt enabled for all other causes, ideally
4070 		 * interrupt should be disabled while we are in NAPI but
4071 		 * this is not a performance path and napi_schedule()
4072 		 * can deal with rescheduling.
4073 		 */
4074 		if (!test_bit(__I40E_DOWN, pf->state))
4075 			napi_schedule_irqoff(&q_vector->napi);
4076 	}
4077 
4078 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4079 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4080 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4081 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4082 	}
4083 
4084 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4085 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4086 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4087 	}
4088 
4089 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4090 		/* disable any further VFLR event notifications */
4091 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4092 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4093 
4094 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4095 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4096 		} else {
4097 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4098 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4099 		}
4100 	}
4101 
4102 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4103 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4104 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4105 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4106 		val = rd32(hw, I40E_GLGEN_RSTAT);
4107 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4108 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4109 		if (val == I40E_RESET_CORER) {
4110 			pf->corer_count++;
4111 		} else if (val == I40E_RESET_GLOBR) {
4112 			pf->globr_count++;
4113 		} else if (val == I40E_RESET_EMPR) {
4114 			pf->empr_count++;
4115 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4116 		}
4117 	}
4118 
4119 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4120 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4121 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4122 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4123 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4124 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4125 	}
4126 
4127 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4128 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4129 
4130 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4131 			schedule_work(&pf->ptp_extts0_work);
4132 
4133 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4134 			i40e_ptp_tx_hwtstamp(pf);
4135 
4136 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4137 	}
4138 
4139 	/* If a critical error is pending we have no choice but to reset the
4140 	 * device.
4141 	 * Report and mask out any remaining unexpected interrupts.
4142 	 */
4143 	icr0_remaining = icr0 & ena_mask;
4144 	if (icr0_remaining) {
4145 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4146 			 icr0_remaining);
4147 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4148 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4149 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4150 			dev_info(&pf->pdev->dev, "device will be reset\n");
4151 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4152 			i40e_service_event_schedule(pf);
4153 		}
4154 		ena_mask &= ~icr0_remaining;
4155 	}
4156 	ret = IRQ_HANDLED;
4157 
4158 enable_intr:
4159 	/* re-enable interrupt causes */
4160 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4161 	if (!test_bit(__I40E_DOWN, pf->state) ||
4162 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4163 		i40e_service_event_schedule(pf);
4164 		i40e_irq_dynamic_enable_icr0(pf);
4165 	}
4166 
4167 	return ret;
4168 }
4169 
4170 /**
4171  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4172  * @tx_ring:  tx ring to clean
4173  * @budget:   how many cleans we're allowed
4174  *
4175  * Returns true if there's any budget left (e.g. the clean is finished)
4176  **/
4177 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4178 {
4179 	struct i40e_vsi *vsi = tx_ring->vsi;
4180 	u16 i = tx_ring->next_to_clean;
4181 	struct i40e_tx_buffer *tx_buf;
4182 	struct i40e_tx_desc *tx_desc;
4183 
4184 	tx_buf = &tx_ring->tx_bi[i];
4185 	tx_desc = I40E_TX_DESC(tx_ring, i);
4186 	i -= tx_ring->count;
4187 
4188 	do {
4189 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4190 
4191 		/* if next_to_watch is not set then there is no work pending */
4192 		if (!eop_desc)
4193 			break;
4194 
4195 		/* prevent any other reads prior to eop_desc */
4196 		smp_rmb();
4197 
4198 		/* if the descriptor isn't done, no work yet to do */
4199 		if (!(eop_desc->cmd_type_offset_bsz &
4200 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4201 			break;
4202 
4203 		/* clear next_to_watch to prevent false hangs */
4204 		tx_buf->next_to_watch = NULL;
4205 
4206 		tx_desc->buffer_addr = 0;
4207 		tx_desc->cmd_type_offset_bsz = 0;
4208 		/* move past filter desc */
4209 		tx_buf++;
4210 		tx_desc++;
4211 		i++;
4212 		if (unlikely(!i)) {
4213 			i -= tx_ring->count;
4214 			tx_buf = tx_ring->tx_bi;
4215 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4216 		}
4217 		/* unmap skb header data */
4218 		dma_unmap_single(tx_ring->dev,
4219 				 dma_unmap_addr(tx_buf, dma),
4220 				 dma_unmap_len(tx_buf, len),
4221 				 DMA_TO_DEVICE);
4222 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4223 			kfree(tx_buf->raw_buf);
4224 
4225 		tx_buf->raw_buf = NULL;
4226 		tx_buf->tx_flags = 0;
4227 		tx_buf->next_to_watch = NULL;
4228 		dma_unmap_len_set(tx_buf, len, 0);
4229 		tx_desc->buffer_addr = 0;
4230 		tx_desc->cmd_type_offset_bsz = 0;
4231 
4232 		/* move us past the eop_desc for start of next FD desc */
4233 		tx_buf++;
4234 		tx_desc++;
4235 		i++;
4236 		if (unlikely(!i)) {
4237 			i -= tx_ring->count;
4238 			tx_buf = tx_ring->tx_bi;
4239 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4240 		}
4241 
4242 		/* update budget accounting */
4243 		budget--;
4244 	} while (likely(budget));
4245 
4246 	i += tx_ring->count;
4247 	tx_ring->next_to_clean = i;
4248 
4249 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4250 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4251 
4252 	return budget > 0;
4253 }
4254 
4255 /**
4256  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4257  * @irq: interrupt number
4258  * @data: pointer to a q_vector
4259  **/
4260 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4261 {
4262 	struct i40e_q_vector *q_vector = data;
4263 	struct i40e_vsi *vsi;
4264 
4265 	if (!q_vector->tx.ring)
4266 		return IRQ_HANDLED;
4267 
4268 	vsi = q_vector->tx.ring->vsi;
4269 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4270 
4271 	return IRQ_HANDLED;
4272 }
4273 
4274 /**
4275  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4276  * @vsi: the VSI being configured
4277  * @v_idx: vector index
4278  * @qp_idx: queue pair index
4279  **/
4280 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4281 {
4282 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4283 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4284 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4285 
4286 	tx_ring->q_vector = q_vector;
4287 	tx_ring->next = q_vector->tx.ring;
4288 	q_vector->tx.ring = tx_ring;
4289 	q_vector->tx.count++;
4290 
4291 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4292 	if (i40e_enabled_xdp_vsi(vsi)) {
4293 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4294 
4295 		xdp_ring->q_vector = q_vector;
4296 		xdp_ring->next = q_vector->tx.ring;
4297 		q_vector->tx.ring = xdp_ring;
4298 		q_vector->tx.count++;
4299 	}
4300 
4301 	rx_ring->q_vector = q_vector;
4302 	rx_ring->next = q_vector->rx.ring;
4303 	q_vector->rx.ring = rx_ring;
4304 	q_vector->rx.count++;
4305 }
4306 
4307 /**
4308  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4309  * @vsi: the VSI being configured
4310  *
4311  * This function maps descriptor rings to the queue-specific vectors
4312  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4313  * one vector per queue pair, but on a constrained vector budget, we
4314  * group the queue pairs as "efficiently" as possible.
4315  **/
4316 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4317 {
4318 	int qp_remaining = vsi->num_queue_pairs;
4319 	int q_vectors = vsi->num_q_vectors;
4320 	int num_ringpairs;
4321 	int v_start = 0;
4322 	int qp_idx = 0;
4323 
4324 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4325 	 * group them so there are multiple queues per vector.
4326 	 * It is also important to go through all the vectors available to be
4327 	 * sure that if we don't use all the vectors, that the remaining vectors
4328 	 * are cleared. This is especially important when decreasing the
4329 	 * number of queues in use.
4330 	 */
4331 	for (; v_start < q_vectors; v_start++) {
4332 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4333 
4334 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4335 
4336 		q_vector->num_ringpairs = num_ringpairs;
4337 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4338 
4339 		q_vector->rx.count = 0;
4340 		q_vector->tx.count = 0;
4341 		q_vector->rx.ring = NULL;
4342 		q_vector->tx.ring = NULL;
4343 
4344 		while (num_ringpairs--) {
4345 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4346 			qp_idx++;
4347 			qp_remaining--;
4348 		}
4349 	}
4350 }
4351 
4352 /**
4353  * i40e_vsi_request_irq - Request IRQ from the OS
4354  * @vsi: the VSI being configured
4355  * @basename: name for the vector
4356  **/
4357 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4358 {
4359 	struct i40e_pf *pf = vsi->back;
4360 	int err;
4361 
4362 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4363 		err = i40e_vsi_request_irq_msix(vsi, basename);
4364 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4365 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4366 				  pf->int_name, pf);
4367 	else
4368 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4369 				  pf->int_name, pf);
4370 
4371 	if (err)
4372 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4373 
4374 	return err;
4375 }
4376 
4377 #ifdef CONFIG_NET_POLL_CONTROLLER
4378 /**
4379  * i40e_netpoll - A Polling 'interrupt' handler
4380  * @netdev: network interface device structure
4381  *
4382  * This is used by netconsole to send skbs without having to re-enable
4383  * interrupts.  It's not called while the normal interrupt routine is executing.
4384  **/
4385 static void i40e_netpoll(struct net_device *netdev)
4386 {
4387 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4388 	struct i40e_vsi *vsi = np->vsi;
4389 	struct i40e_pf *pf = vsi->back;
4390 	int i;
4391 
4392 	/* if interface is down do nothing */
4393 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4394 		return;
4395 
4396 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4397 		for (i = 0; i < vsi->num_q_vectors; i++)
4398 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4399 	} else {
4400 		i40e_intr(pf->pdev->irq, netdev);
4401 	}
4402 }
4403 #endif
4404 
4405 #define I40E_QTX_ENA_WAIT_COUNT 50
4406 
4407 /**
4408  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4409  * @pf: the PF being configured
4410  * @pf_q: the PF queue
4411  * @enable: enable or disable state of the queue
4412  *
4413  * This routine will wait for the given Tx queue of the PF to reach the
4414  * enabled or disabled state.
4415  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4416  * multiple retries; else will return 0 in case of success.
4417  **/
4418 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4419 {
4420 	int i;
4421 	u32 tx_reg;
4422 
4423 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4424 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4425 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4426 			break;
4427 
4428 		usleep_range(10, 20);
4429 	}
4430 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4431 		return -ETIMEDOUT;
4432 
4433 	return 0;
4434 }
4435 
4436 /**
4437  * i40e_control_tx_q - Start or stop a particular Tx queue
4438  * @pf: the PF structure
4439  * @pf_q: the PF queue to configure
4440  * @enable: start or stop the queue
4441  *
4442  * This function enables or disables a single queue. Note that any delay
4443  * required after the operation is expected to be handled by the caller of
4444  * this function.
4445  **/
4446 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4447 {
4448 	struct i40e_hw *hw = &pf->hw;
4449 	u32 tx_reg;
4450 	int i;
4451 
4452 	/* warn the TX unit of coming changes */
4453 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4454 	if (!enable)
4455 		usleep_range(10, 20);
4456 
4457 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4458 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4459 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4460 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4461 			break;
4462 		usleep_range(1000, 2000);
4463 	}
4464 
4465 	/* Skip if the queue is already in the requested state */
4466 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4467 		return;
4468 
4469 	/* turn on/off the queue */
4470 	if (enable) {
4471 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4472 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4473 	} else {
4474 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4475 	}
4476 
4477 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4478 }
4479 
4480 /**
4481  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4482  * @seid: VSI SEID
4483  * @pf: the PF structure
4484  * @pf_q: the PF queue to configure
4485  * @is_xdp: true if the queue is used for XDP
4486  * @enable: start or stop the queue
4487  **/
4488 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4489 			   bool is_xdp, bool enable)
4490 {
4491 	int ret;
4492 
4493 	i40e_control_tx_q(pf, pf_q, enable);
4494 
4495 	/* wait for the change to finish */
4496 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4497 	if (ret) {
4498 		dev_info(&pf->pdev->dev,
4499 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4500 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4501 			 (enable ? "en" : "dis"));
4502 	}
4503 
4504 	return ret;
4505 }
4506 
4507 /**
4508  * i40e_vsi_enable_tx - Start a VSI's rings
4509  * @vsi: the VSI being configured
4510  **/
4511 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4512 {
4513 	struct i40e_pf *pf = vsi->back;
4514 	int i, pf_q, ret = 0;
4515 
4516 	pf_q = vsi->base_queue;
4517 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4518 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4519 					     pf_q,
4520 					     false /*is xdp*/, true);
4521 		if (ret)
4522 			break;
4523 
4524 		if (!i40e_enabled_xdp_vsi(vsi))
4525 			continue;
4526 
4527 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4528 					     pf_q + vsi->alloc_queue_pairs,
4529 					     true /*is xdp*/, true);
4530 		if (ret)
4531 			break;
4532 	}
4533 	return ret;
4534 }
4535 
4536 /**
4537  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4538  * @pf: the PF being configured
4539  * @pf_q: the PF queue
4540  * @enable: enable or disable state of the queue
4541  *
4542  * This routine will wait for the given Rx queue of the PF to reach the
4543  * enabled or disabled state.
4544  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4545  * multiple retries; else will return 0 in case of success.
4546  **/
4547 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4548 {
4549 	int i;
4550 	u32 rx_reg;
4551 
4552 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4553 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4554 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4555 			break;
4556 
4557 		usleep_range(10, 20);
4558 	}
4559 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4560 		return -ETIMEDOUT;
4561 
4562 	return 0;
4563 }
4564 
4565 /**
4566  * i40e_control_rx_q - Start or stop a particular Rx queue
4567  * @pf: the PF structure
4568  * @pf_q: the PF queue to configure
4569  * @enable: start or stop the queue
4570  *
4571  * This function enables or disables a single queue. Note that
4572  * any delay required after the operation is expected to be
4573  * handled by the caller of this function.
4574  **/
4575 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4576 {
4577 	struct i40e_hw *hw = &pf->hw;
4578 	u32 rx_reg;
4579 	int i;
4580 
4581 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4582 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4583 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4584 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4585 			break;
4586 		usleep_range(1000, 2000);
4587 	}
4588 
4589 	/* Skip if the queue is already in the requested state */
4590 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4591 		return;
4592 
4593 	/* turn on/off the queue */
4594 	if (enable)
4595 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4596 	else
4597 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4598 
4599 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4600 }
4601 
4602 /**
4603  * i40e_control_wait_rx_q
4604  * @pf: the PF structure
4605  * @pf_q: queue being configured
4606  * @enable: start or stop the rings
4607  *
4608  * This function enables or disables a single queue along with waiting
4609  * for the change to finish. The caller of this function should handle
4610  * the delays needed in the case of disabling queues.
4611  **/
4612 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4613 {
4614 	int ret = 0;
4615 
4616 	i40e_control_rx_q(pf, pf_q, enable);
4617 
4618 	/* wait for the change to finish */
4619 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4620 	if (ret)
4621 		return ret;
4622 
4623 	return ret;
4624 }
4625 
4626 /**
4627  * i40e_vsi_enable_rx - Start a VSI's rings
4628  * @vsi: the VSI being configured
4629  **/
4630 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4631 {
4632 	struct i40e_pf *pf = vsi->back;
4633 	int i, pf_q, ret = 0;
4634 
4635 	pf_q = vsi->base_queue;
4636 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4637 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4638 		if (ret) {
4639 			dev_info(&pf->pdev->dev,
4640 				 "VSI seid %d Rx ring %d enable timeout\n",
4641 				 vsi->seid, pf_q);
4642 			break;
4643 		}
4644 	}
4645 
4646 	return ret;
4647 }
4648 
4649 /**
4650  * i40e_vsi_start_rings - Start a VSI's rings
4651  * @vsi: the VSI being configured
4652  **/
4653 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4654 {
4655 	int ret = 0;
4656 
4657 	/* do rx first for enable and last for disable */
4658 	ret = i40e_vsi_enable_rx(vsi);
4659 	if (ret)
4660 		return ret;
4661 	ret = i40e_vsi_enable_tx(vsi);
4662 
4663 	return ret;
4664 }
4665 
4666 #define I40E_DISABLE_TX_GAP_MSEC	50
4667 
4668 /**
4669  * i40e_vsi_stop_rings - Stop a VSI's rings
4670  * @vsi: the VSI being configured
4671  **/
4672 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4673 {
4674 	struct i40e_pf *pf = vsi->back;
4675 	int pf_q, err, q_end;
4676 
4677 	/* When port TX is suspended, don't wait */
4678 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4679 		return i40e_vsi_stop_rings_no_wait(vsi);
4680 
4681 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4682 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4683 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4684 
4685 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4686 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4687 		if (err)
4688 			dev_info(&pf->pdev->dev,
4689 				 "VSI seid %d Rx ring %d disable timeout\n",
4690 				 vsi->seid, pf_q);
4691 	}
4692 
4693 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4694 	pf_q = vsi->base_queue;
4695 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4696 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4697 
4698 	i40e_vsi_wait_queues_disabled(vsi);
4699 }
4700 
4701 /**
4702  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4703  * @vsi: the VSI being shutdown
4704  *
4705  * This function stops all the rings for a VSI but does not delay to verify
4706  * that rings have been disabled. It is expected that the caller is shutting
4707  * down multiple VSIs at once and will delay together for all the VSIs after
4708  * initiating the shutdown. This is particularly useful for shutting down lots
4709  * of VFs together. Otherwise, a large delay can be incurred while configuring
4710  * each VSI in serial.
4711  **/
4712 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4713 {
4714 	struct i40e_pf *pf = vsi->back;
4715 	int i, pf_q;
4716 
4717 	pf_q = vsi->base_queue;
4718 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4719 		i40e_control_tx_q(pf, pf_q, false);
4720 		i40e_control_rx_q(pf, pf_q, false);
4721 	}
4722 }
4723 
4724 /**
4725  * i40e_vsi_free_irq - Free the irq association with the OS
4726  * @vsi: the VSI being configured
4727  **/
4728 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4729 {
4730 	struct i40e_pf *pf = vsi->back;
4731 	struct i40e_hw *hw = &pf->hw;
4732 	int base = vsi->base_vector;
4733 	u32 val, qp;
4734 	int i;
4735 
4736 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4737 		if (!vsi->q_vectors)
4738 			return;
4739 
4740 		if (!vsi->irqs_ready)
4741 			return;
4742 
4743 		vsi->irqs_ready = false;
4744 		for (i = 0; i < vsi->num_q_vectors; i++) {
4745 			int irq_num;
4746 			u16 vector;
4747 
4748 			vector = i + base;
4749 			irq_num = pf->msix_entries[vector].vector;
4750 
4751 			/* free only the irqs that were actually requested */
4752 			if (!vsi->q_vectors[i] ||
4753 			    !vsi->q_vectors[i]->num_ringpairs)
4754 				continue;
4755 
4756 			/* clear the affinity notifier in the IRQ descriptor */
4757 			irq_set_affinity_notifier(irq_num, NULL);
4758 			/* remove our suggested affinity mask for this IRQ */
4759 			irq_update_affinity_hint(irq_num, NULL);
4760 			synchronize_irq(irq_num);
4761 			free_irq(irq_num, vsi->q_vectors[i]);
4762 
4763 			/* Tear down the interrupt queue link list
4764 			 *
4765 			 * We know that they come in pairs and always
4766 			 * the Rx first, then the Tx.  To clear the
4767 			 * link list, stick the EOL value into the
4768 			 * next_q field of the registers.
4769 			 */
4770 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4771 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4772 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4773 			val |= I40E_QUEUE_END_OF_LIST
4774 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4775 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4776 
4777 			while (qp != I40E_QUEUE_END_OF_LIST) {
4778 				u32 next;
4779 
4780 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4781 
4782 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4783 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4784 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4785 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4786 
4787 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4788 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4789 
4790 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4791 
4792 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4793 
4794 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4795 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4796 
4797 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4798 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4799 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4800 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4801 
4802 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4803 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4804 
4805 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4806 				qp = next;
4807 			}
4808 		}
4809 	} else {
4810 		free_irq(pf->pdev->irq, pf);
4811 
4812 		val = rd32(hw, I40E_PFINT_LNKLST0);
4813 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4814 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4815 		val |= I40E_QUEUE_END_OF_LIST
4816 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4817 		wr32(hw, I40E_PFINT_LNKLST0, val);
4818 
4819 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4820 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4821 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4822 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4823 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4824 
4825 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4826 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4827 
4828 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4829 
4830 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4831 
4832 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4833 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4834 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4835 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4836 
4837 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4838 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4839 
4840 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4841 	}
4842 }
4843 
4844 /**
4845  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4846  * @vsi: the VSI being configured
4847  * @v_idx: Index of vector to be freed
4848  *
4849  * This function frees the memory allocated to the q_vector.  In addition if
4850  * NAPI is enabled it will delete any references to the NAPI struct prior
4851  * to freeing the q_vector.
4852  **/
4853 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4854 {
4855 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4856 	struct i40e_ring *ring;
4857 
4858 	if (!q_vector)
4859 		return;
4860 
4861 	/* disassociate q_vector from rings */
4862 	i40e_for_each_ring(ring, q_vector->tx)
4863 		ring->q_vector = NULL;
4864 
4865 	i40e_for_each_ring(ring, q_vector->rx)
4866 		ring->q_vector = NULL;
4867 
4868 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4869 	if (vsi->netdev)
4870 		netif_napi_del(&q_vector->napi);
4871 
4872 	vsi->q_vectors[v_idx] = NULL;
4873 
4874 	kfree_rcu(q_vector, rcu);
4875 }
4876 
4877 /**
4878  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4879  * @vsi: the VSI being un-configured
4880  *
4881  * This frees the memory allocated to the q_vectors and
4882  * deletes references to the NAPI struct.
4883  **/
4884 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4885 {
4886 	int v_idx;
4887 
4888 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4889 		i40e_free_q_vector(vsi, v_idx);
4890 }
4891 
4892 /**
4893  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4894  * @pf: board private structure
4895  **/
4896 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4897 {
4898 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4899 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4900 		pci_disable_msix(pf->pdev);
4901 		kfree(pf->msix_entries);
4902 		pf->msix_entries = NULL;
4903 		kfree(pf->irq_pile);
4904 		pf->irq_pile = NULL;
4905 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4906 		pci_disable_msi(pf->pdev);
4907 	}
4908 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4909 }
4910 
4911 /**
4912  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4913  * @pf: board private structure
4914  *
4915  * We go through and clear interrupt specific resources and reset the structure
4916  * to pre-load conditions
4917  **/
4918 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4919 {
4920 	int i;
4921 
4922 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4923 		i40e_free_misc_vector(pf);
4924 
4925 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4926 		      I40E_IWARP_IRQ_PILE_ID);
4927 
4928 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4929 	for (i = 0; i < pf->num_alloc_vsi; i++)
4930 		if (pf->vsi[i])
4931 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4932 	i40e_reset_interrupt_capability(pf);
4933 }
4934 
4935 /**
4936  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4937  * @vsi: the VSI being configured
4938  **/
4939 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4940 {
4941 	int q_idx;
4942 
4943 	if (!vsi->netdev)
4944 		return;
4945 
4946 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4947 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4948 
4949 		if (q_vector->rx.ring || q_vector->tx.ring)
4950 			napi_enable(&q_vector->napi);
4951 	}
4952 }
4953 
4954 /**
4955  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4956  * @vsi: the VSI being configured
4957  **/
4958 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4959 {
4960 	int q_idx;
4961 
4962 	if (!vsi->netdev)
4963 		return;
4964 
4965 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4966 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4967 
4968 		if (q_vector->rx.ring || q_vector->tx.ring)
4969 			napi_disable(&q_vector->napi);
4970 	}
4971 }
4972 
4973 /**
4974  * i40e_vsi_close - Shut down a VSI
4975  * @vsi: the vsi to be quelled
4976  **/
4977 static void i40e_vsi_close(struct i40e_vsi *vsi)
4978 {
4979 	struct i40e_pf *pf = vsi->back;
4980 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4981 		i40e_down(vsi);
4982 	i40e_vsi_free_irq(vsi);
4983 	i40e_vsi_free_tx_resources(vsi);
4984 	i40e_vsi_free_rx_resources(vsi);
4985 	vsi->current_netdev_flags = 0;
4986 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4987 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4988 		set_bit(__I40E_CLIENT_RESET, pf->state);
4989 }
4990 
4991 /**
4992  * i40e_quiesce_vsi - Pause a given VSI
4993  * @vsi: the VSI being paused
4994  **/
4995 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4996 {
4997 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4998 		return;
4999 
5000 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5001 	if (vsi->netdev && netif_running(vsi->netdev))
5002 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5003 	else
5004 		i40e_vsi_close(vsi);
5005 }
5006 
5007 /**
5008  * i40e_unquiesce_vsi - Resume a given VSI
5009  * @vsi: the VSI being resumed
5010  **/
5011 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5012 {
5013 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5014 		return;
5015 
5016 	if (vsi->netdev && netif_running(vsi->netdev))
5017 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5018 	else
5019 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5020 }
5021 
5022 /**
5023  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5024  * @pf: the PF
5025  **/
5026 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5027 {
5028 	int v;
5029 
5030 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5031 		if (pf->vsi[v])
5032 			i40e_quiesce_vsi(pf->vsi[v]);
5033 	}
5034 }
5035 
5036 /**
5037  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5038  * @pf: the PF
5039  **/
5040 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5041 {
5042 	int v;
5043 
5044 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5045 		if (pf->vsi[v])
5046 			i40e_unquiesce_vsi(pf->vsi[v]);
5047 	}
5048 }
5049 
5050 /**
5051  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5052  * @vsi: the VSI being configured
5053  *
5054  * Wait until all queues on a given VSI have been disabled.
5055  **/
5056 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5057 {
5058 	struct i40e_pf *pf = vsi->back;
5059 	int i, pf_q, ret;
5060 
5061 	pf_q = vsi->base_queue;
5062 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5063 		/* Check and wait for the Tx queue */
5064 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5065 		if (ret) {
5066 			dev_info(&pf->pdev->dev,
5067 				 "VSI seid %d Tx ring %d disable timeout\n",
5068 				 vsi->seid, pf_q);
5069 			return ret;
5070 		}
5071 
5072 		if (!i40e_enabled_xdp_vsi(vsi))
5073 			goto wait_rx;
5074 
5075 		/* Check and wait for the XDP Tx queue */
5076 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5077 				       false);
5078 		if (ret) {
5079 			dev_info(&pf->pdev->dev,
5080 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5081 				 vsi->seid, pf_q);
5082 			return ret;
5083 		}
5084 wait_rx:
5085 		/* Check and wait for the Rx queue */
5086 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5087 		if (ret) {
5088 			dev_info(&pf->pdev->dev,
5089 				 "VSI seid %d Rx ring %d disable timeout\n",
5090 				 vsi->seid, pf_q);
5091 			return ret;
5092 		}
5093 	}
5094 
5095 	return 0;
5096 }
5097 
5098 #ifdef CONFIG_I40E_DCB
5099 /**
5100  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5101  * @pf: the PF
5102  *
5103  * This function waits for the queues to be in disabled state for all the
5104  * VSIs that are managed by this PF.
5105  **/
5106 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5107 {
5108 	int v, ret = 0;
5109 
5110 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5111 		if (pf->vsi[v]) {
5112 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5113 			if (ret)
5114 				break;
5115 		}
5116 	}
5117 
5118 	return ret;
5119 }
5120 
5121 #endif
5122 
5123 /**
5124  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5125  * @pf: pointer to PF
5126  *
5127  * Get TC map for ISCSI PF type that will include iSCSI TC
5128  * and LAN TC.
5129  **/
5130 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5131 {
5132 	struct i40e_dcb_app_priority_table app;
5133 	struct i40e_hw *hw = &pf->hw;
5134 	u8 enabled_tc = 1; /* TC0 is always enabled */
5135 	u8 tc, i;
5136 	/* Get the iSCSI APP TLV */
5137 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5138 
5139 	for (i = 0; i < dcbcfg->numapps; i++) {
5140 		app = dcbcfg->app[i];
5141 		if (app.selector == I40E_APP_SEL_TCPIP &&
5142 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5143 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5144 			enabled_tc |= BIT(tc);
5145 			break;
5146 		}
5147 	}
5148 
5149 	return enabled_tc;
5150 }
5151 
5152 /**
5153  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5154  * @dcbcfg: the corresponding DCBx configuration structure
5155  *
5156  * Return the number of TCs from given DCBx configuration
5157  **/
5158 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5159 {
5160 	int i, tc_unused = 0;
5161 	u8 num_tc = 0;
5162 	u8 ret = 0;
5163 
5164 	/* Scan the ETS Config Priority Table to find
5165 	 * traffic class enabled for a given priority
5166 	 * and create a bitmask of enabled TCs
5167 	 */
5168 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5169 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5170 
5171 	/* Now scan the bitmask to check for
5172 	 * contiguous TCs starting with TC0
5173 	 */
5174 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5175 		if (num_tc & BIT(i)) {
5176 			if (!tc_unused) {
5177 				ret++;
5178 			} else {
5179 				pr_err("Non-contiguous TC - Disabling DCB\n");
5180 				return 1;
5181 			}
5182 		} else {
5183 			tc_unused = 1;
5184 		}
5185 	}
5186 
5187 	/* There is always at least TC0 */
5188 	if (!ret)
5189 		ret = 1;
5190 
5191 	return ret;
5192 }
5193 
5194 /**
5195  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5196  * @dcbcfg: the corresponding DCBx configuration structure
5197  *
5198  * Query the current DCB configuration and return the number of
5199  * traffic classes enabled from the given DCBX config
5200  **/
5201 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5202 {
5203 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5204 	u8 enabled_tc = 1;
5205 	u8 i;
5206 
5207 	for (i = 0; i < num_tc; i++)
5208 		enabled_tc |= BIT(i);
5209 
5210 	return enabled_tc;
5211 }
5212 
5213 /**
5214  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5215  * @pf: PF being queried
5216  *
5217  * Query the current MQPRIO configuration and return the number of
5218  * traffic classes enabled.
5219  **/
5220 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5221 {
5222 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5223 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5224 	u8 enabled_tc = 1, i;
5225 
5226 	for (i = 1; i < num_tc; i++)
5227 		enabled_tc |= BIT(i);
5228 	return enabled_tc;
5229 }
5230 
5231 /**
5232  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5233  * @pf: PF being queried
5234  *
5235  * Return number of traffic classes enabled for the given PF
5236  **/
5237 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5238 {
5239 	struct i40e_hw *hw = &pf->hw;
5240 	u8 i, enabled_tc = 1;
5241 	u8 num_tc = 0;
5242 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5243 
5244 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5245 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5246 
5247 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5248 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5249 		return 1;
5250 
5251 	/* SFP mode will be enabled for all TCs on port */
5252 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5253 		return i40e_dcb_get_num_tc(dcbcfg);
5254 
5255 	/* MFP mode return count of enabled TCs for this PF */
5256 	if (pf->hw.func_caps.iscsi)
5257 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5258 	else
5259 		return 1; /* Only TC0 */
5260 
5261 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5262 		if (enabled_tc & BIT(i))
5263 			num_tc++;
5264 	}
5265 	return num_tc;
5266 }
5267 
5268 /**
5269  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5270  * @pf: PF being queried
5271  *
5272  * Return a bitmap for enabled traffic classes for this PF.
5273  **/
5274 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5275 {
5276 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5277 		return i40e_mqprio_get_enabled_tc(pf);
5278 
5279 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5280 	 * default TC
5281 	 */
5282 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5283 		return I40E_DEFAULT_TRAFFIC_CLASS;
5284 
5285 	/* SFP mode we want PF to be enabled for all TCs */
5286 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5287 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5288 
5289 	/* MFP enabled and iSCSI PF type */
5290 	if (pf->hw.func_caps.iscsi)
5291 		return i40e_get_iscsi_tc_map(pf);
5292 	else
5293 		return I40E_DEFAULT_TRAFFIC_CLASS;
5294 }
5295 
5296 /**
5297  * i40e_vsi_get_bw_info - Query VSI BW Information
5298  * @vsi: the VSI being queried
5299  *
5300  * Returns 0 on success, negative value on failure
5301  **/
5302 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5303 {
5304 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5305 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5306 	struct i40e_pf *pf = vsi->back;
5307 	struct i40e_hw *hw = &pf->hw;
5308 	i40e_status ret;
5309 	u32 tc_bw_max;
5310 	int i;
5311 
5312 	/* Get the VSI level BW configuration */
5313 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5314 	if (ret) {
5315 		dev_info(&pf->pdev->dev,
5316 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5317 			 i40e_stat_str(&pf->hw, ret),
5318 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5319 		return -EINVAL;
5320 	}
5321 
5322 	/* Get the VSI level BW configuration per TC */
5323 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5324 					       NULL);
5325 	if (ret) {
5326 		dev_info(&pf->pdev->dev,
5327 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5328 			 i40e_stat_str(&pf->hw, ret),
5329 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5330 		return -EINVAL;
5331 	}
5332 
5333 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5334 		dev_info(&pf->pdev->dev,
5335 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5336 			 bw_config.tc_valid_bits,
5337 			 bw_ets_config.tc_valid_bits);
5338 		/* Still continuing */
5339 	}
5340 
5341 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5342 	vsi->bw_max_quanta = bw_config.max_bw;
5343 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5344 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5345 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5346 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5347 		vsi->bw_ets_limit_credits[i] =
5348 					le16_to_cpu(bw_ets_config.credits[i]);
5349 		/* 3 bits out of 4 for each TC */
5350 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5351 	}
5352 
5353 	return 0;
5354 }
5355 
5356 /**
5357  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5358  * @vsi: the VSI being configured
5359  * @enabled_tc: TC bitmap
5360  * @bw_share: BW shared credits per TC
5361  *
5362  * Returns 0 on success, negative value on failure
5363  **/
5364 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5365 				       u8 *bw_share)
5366 {
5367 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5368 	struct i40e_pf *pf = vsi->back;
5369 	i40e_status ret;
5370 	int i;
5371 
5372 	/* There is no need to reset BW when mqprio mode is on.  */
5373 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5374 		return 0;
5375 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5376 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5377 		if (ret)
5378 			dev_info(&pf->pdev->dev,
5379 				 "Failed to reset tx rate for vsi->seid %u\n",
5380 				 vsi->seid);
5381 		return ret;
5382 	}
5383 	memset(&bw_data, 0, sizeof(bw_data));
5384 	bw_data.tc_valid_bits = enabled_tc;
5385 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5386 		bw_data.tc_bw_credits[i] = bw_share[i];
5387 
5388 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5389 	if (ret) {
5390 		dev_info(&pf->pdev->dev,
5391 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5392 			 pf->hw.aq.asq_last_status);
5393 		return -EINVAL;
5394 	}
5395 
5396 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5397 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5398 
5399 	return 0;
5400 }
5401 
5402 /**
5403  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5404  * @vsi: the VSI being configured
5405  * @enabled_tc: TC map to be enabled
5406  *
5407  **/
5408 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5409 {
5410 	struct net_device *netdev = vsi->netdev;
5411 	struct i40e_pf *pf = vsi->back;
5412 	struct i40e_hw *hw = &pf->hw;
5413 	u8 netdev_tc = 0;
5414 	int i;
5415 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5416 
5417 	if (!netdev)
5418 		return;
5419 
5420 	if (!enabled_tc) {
5421 		netdev_reset_tc(netdev);
5422 		return;
5423 	}
5424 
5425 	/* Set up actual enabled TCs on the VSI */
5426 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5427 		return;
5428 
5429 	/* set per TC queues for the VSI */
5430 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5431 		/* Only set TC queues for enabled tcs
5432 		 *
5433 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5434 		 * enabled_tc bitmap would be 0x00001001; the driver
5435 		 * will set the numtc for netdev as 2 that will be
5436 		 * referenced by the netdev layer as TC 0 and 1.
5437 		 */
5438 		if (vsi->tc_config.enabled_tc & BIT(i))
5439 			netdev_set_tc_queue(netdev,
5440 					vsi->tc_config.tc_info[i].netdev_tc,
5441 					vsi->tc_config.tc_info[i].qcount,
5442 					vsi->tc_config.tc_info[i].qoffset);
5443 	}
5444 
5445 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5446 		return;
5447 
5448 	/* Assign UP2TC map for the VSI */
5449 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5450 		/* Get the actual TC# for the UP */
5451 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5452 		/* Get the mapped netdev TC# for the UP */
5453 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5454 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5455 	}
5456 }
5457 
5458 /**
5459  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5460  * @vsi: the VSI being configured
5461  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5462  **/
5463 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5464 				      struct i40e_vsi_context *ctxt)
5465 {
5466 	/* copy just the sections touched not the entire info
5467 	 * since not all sections are valid as returned by
5468 	 * update vsi params
5469 	 */
5470 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5471 	memcpy(&vsi->info.queue_mapping,
5472 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5473 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5474 	       sizeof(vsi->info.tc_mapping));
5475 }
5476 
5477 /**
5478  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5479  * @vsi: the VSI being reconfigured
5480  * @vsi_offset: offset from main VF VSI
5481  */
5482 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5483 {
5484 	struct i40e_vsi_context ctxt = {};
5485 	struct i40e_pf *pf;
5486 	struct i40e_hw *hw;
5487 	int ret;
5488 
5489 	if (!vsi)
5490 		return I40E_ERR_PARAM;
5491 	pf = vsi->back;
5492 	hw = &pf->hw;
5493 
5494 	ctxt.seid = vsi->seid;
5495 	ctxt.pf_num = hw->pf_id;
5496 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5497 	ctxt.uplink_seid = vsi->uplink_seid;
5498 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5499 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5500 	ctxt.info = vsi->info;
5501 
5502 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5503 				 false);
5504 	if (vsi->reconfig_rss) {
5505 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5506 				      vsi->num_queue_pairs);
5507 		ret = i40e_vsi_config_rss(vsi);
5508 		if (ret) {
5509 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5510 			return ret;
5511 		}
5512 		vsi->reconfig_rss = false;
5513 	}
5514 
5515 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5516 	if (ret) {
5517 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5518 			 i40e_stat_str(hw, ret),
5519 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5520 		return ret;
5521 	}
5522 	/* update the local VSI info with updated queue map */
5523 	i40e_vsi_update_queue_map(vsi, &ctxt);
5524 	vsi->info.valid_sections = 0;
5525 
5526 	return ret;
5527 }
5528 
5529 /**
5530  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5531  * @vsi: VSI to be configured
5532  * @enabled_tc: TC bitmap
5533  *
5534  * This configures a particular VSI for TCs that are mapped to the
5535  * given TC bitmap. It uses default bandwidth share for TCs across
5536  * VSIs to configure TC for a particular VSI.
5537  *
5538  * NOTE:
5539  * It is expected that the VSI queues have been quisced before calling
5540  * this function.
5541  **/
5542 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5543 {
5544 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5545 	struct i40e_pf *pf = vsi->back;
5546 	struct i40e_hw *hw = &pf->hw;
5547 	struct i40e_vsi_context ctxt;
5548 	int ret = 0;
5549 	int i;
5550 
5551 	/* Check if enabled_tc is same as existing or new TCs */
5552 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5553 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5554 		return ret;
5555 
5556 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5557 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5558 		if (enabled_tc & BIT(i))
5559 			bw_share[i] = 1;
5560 	}
5561 
5562 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5563 	if (ret) {
5564 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5565 
5566 		dev_info(&pf->pdev->dev,
5567 			 "Failed configuring TC map %d for VSI %d\n",
5568 			 enabled_tc, vsi->seid);
5569 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5570 						  &bw_config, NULL);
5571 		if (ret) {
5572 			dev_info(&pf->pdev->dev,
5573 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5574 				 i40e_stat_str(hw, ret),
5575 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5576 			goto out;
5577 		}
5578 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5579 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5580 
5581 			if (!valid_tc)
5582 				valid_tc = bw_config.tc_valid_bits;
5583 			/* Always enable TC0, no matter what */
5584 			valid_tc |= 1;
5585 			dev_info(&pf->pdev->dev,
5586 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5587 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5588 			enabled_tc = valid_tc;
5589 		}
5590 
5591 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5592 		if (ret) {
5593 			dev_err(&pf->pdev->dev,
5594 				"Unable to  configure TC map %d for VSI %d\n",
5595 				enabled_tc, vsi->seid);
5596 			goto out;
5597 		}
5598 	}
5599 
5600 	/* Update Queue Pairs Mapping for currently enabled UPs */
5601 	ctxt.seid = vsi->seid;
5602 	ctxt.pf_num = vsi->back->hw.pf_id;
5603 	ctxt.vf_num = 0;
5604 	ctxt.uplink_seid = vsi->uplink_seid;
5605 	ctxt.info = vsi->info;
5606 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5607 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5608 		if (ret)
5609 			goto out;
5610 	} else {
5611 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5612 	}
5613 
5614 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5615 	 * queues changed.
5616 	 */
5617 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5618 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5619 				      vsi->num_queue_pairs);
5620 		ret = i40e_vsi_config_rss(vsi);
5621 		if (ret) {
5622 			dev_info(&vsi->back->pdev->dev,
5623 				 "Failed to reconfig rss for num_queues\n");
5624 			return ret;
5625 		}
5626 		vsi->reconfig_rss = false;
5627 	}
5628 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5629 		ctxt.info.valid_sections |=
5630 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5631 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5632 	}
5633 
5634 	/* Update the VSI after updating the VSI queue-mapping
5635 	 * information
5636 	 */
5637 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5638 	if (ret) {
5639 		dev_info(&pf->pdev->dev,
5640 			 "Update vsi tc config failed, err %s aq_err %s\n",
5641 			 i40e_stat_str(hw, ret),
5642 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5643 		goto out;
5644 	}
5645 	/* update the local VSI info with updated queue map */
5646 	i40e_vsi_update_queue_map(vsi, &ctxt);
5647 	vsi->info.valid_sections = 0;
5648 
5649 	/* Update current VSI BW information */
5650 	ret = i40e_vsi_get_bw_info(vsi);
5651 	if (ret) {
5652 		dev_info(&pf->pdev->dev,
5653 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5654 			 i40e_stat_str(hw, ret),
5655 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5656 		goto out;
5657 	}
5658 
5659 	/* Update the netdev TC setup */
5660 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5661 out:
5662 	return ret;
5663 }
5664 
5665 /**
5666  * i40e_get_link_speed - Returns link speed for the interface
5667  * @vsi: VSI to be configured
5668  *
5669  **/
5670 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5671 {
5672 	struct i40e_pf *pf = vsi->back;
5673 
5674 	switch (pf->hw.phy.link_info.link_speed) {
5675 	case I40E_LINK_SPEED_40GB:
5676 		return 40000;
5677 	case I40E_LINK_SPEED_25GB:
5678 		return 25000;
5679 	case I40E_LINK_SPEED_20GB:
5680 		return 20000;
5681 	case I40E_LINK_SPEED_10GB:
5682 		return 10000;
5683 	case I40E_LINK_SPEED_1GB:
5684 		return 1000;
5685 	default:
5686 		return -EINVAL;
5687 	}
5688 }
5689 
5690 /**
5691  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5692  * @vsi: VSI to be configured
5693  * @seid: seid of the channel/VSI
5694  * @max_tx_rate: max TX rate to be configured as BW limit
5695  *
5696  * Helper function to set BW limit for a given VSI
5697  **/
5698 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5699 {
5700 	struct i40e_pf *pf = vsi->back;
5701 	u64 credits = 0;
5702 	int speed = 0;
5703 	int ret = 0;
5704 
5705 	speed = i40e_get_link_speed(vsi);
5706 	if (max_tx_rate > speed) {
5707 		dev_err(&pf->pdev->dev,
5708 			"Invalid max tx rate %llu specified for VSI seid %d.",
5709 			max_tx_rate, seid);
5710 		return -EINVAL;
5711 	}
5712 	if (max_tx_rate && max_tx_rate < 50) {
5713 		dev_warn(&pf->pdev->dev,
5714 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5715 		max_tx_rate = 50;
5716 	}
5717 
5718 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5719 	credits = max_tx_rate;
5720 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5721 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5722 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5723 	if (ret)
5724 		dev_err(&pf->pdev->dev,
5725 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5726 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5727 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5728 	return ret;
5729 }
5730 
5731 /**
5732  * i40e_remove_queue_channels - Remove queue channels for the TCs
5733  * @vsi: VSI to be configured
5734  *
5735  * Remove queue channels for the TCs
5736  **/
5737 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5738 {
5739 	enum i40e_admin_queue_err last_aq_status;
5740 	struct i40e_cloud_filter *cfilter;
5741 	struct i40e_channel *ch, *ch_tmp;
5742 	struct i40e_pf *pf = vsi->back;
5743 	struct hlist_node *node;
5744 	int ret, i;
5745 
5746 	/* Reset rss size that was stored when reconfiguring rss for
5747 	 * channel VSIs with non-power-of-2 queue count.
5748 	 */
5749 	vsi->current_rss_size = 0;
5750 
5751 	/* perform cleanup for channels if they exist */
5752 	if (list_empty(&vsi->ch_list))
5753 		return;
5754 
5755 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5756 		struct i40e_vsi *p_vsi;
5757 
5758 		list_del(&ch->list);
5759 		p_vsi = ch->parent_vsi;
5760 		if (!p_vsi || !ch->initialized) {
5761 			kfree(ch);
5762 			continue;
5763 		}
5764 		/* Reset queue contexts */
5765 		for (i = 0; i < ch->num_queue_pairs; i++) {
5766 			struct i40e_ring *tx_ring, *rx_ring;
5767 			u16 pf_q;
5768 
5769 			pf_q = ch->base_queue + i;
5770 			tx_ring = vsi->tx_rings[pf_q];
5771 			tx_ring->ch = NULL;
5772 
5773 			rx_ring = vsi->rx_rings[pf_q];
5774 			rx_ring->ch = NULL;
5775 		}
5776 
5777 		/* Reset BW configured for this VSI via mqprio */
5778 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5779 		if (ret)
5780 			dev_info(&vsi->back->pdev->dev,
5781 				 "Failed to reset tx rate for ch->seid %u\n",
5782 				 ch->seid);
5783 
5784 		/* delete cloud filters associated with this channel */
5785 		hlist_for_each_entry_safe(cfilter, node,
5786 					  &pf->cloud_filter_list, cloud_node) {
5787 			if (cfilter->seid != ch->seid)
5788 				continue;
5789 
5790 			hash_del(&cfilter->cloud_node);
5791 			if (cfilter->dst_port)
5792 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5793 									cfilter,
5794 									false);
5795 			else
5796 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5797 								false);
5798 			last_aq_status = pf->hw.aq.asq_last_status;
5799 			if (ret)
5800 				dev_info(&pf->pdev->dev,
5801 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5802 					 i40e_stat_str(&pf->hw, ret),
5803 					 i40e_aq_str(&pf->hw, last_aq_status));
5804 			kfree(cfilter);
5805 		}
5806 
5807 		/* delete VSI from FW */
5808 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5809 					     NULL);
5810 		if (ret)
5811 			dev_err(&vsi->back->pdev->dev,
5812 				"unable to remove channel (%d) for parent VSI(%d)\n",
5813 				ch->seid, p_vsi->seid);
5814 		kfree(ch);
5815 	}
5816 	INIT_LIST_HEAD(&vsi->ch_list);
5817 }
5818 
5819 /**
5820  * i40e_get_max_queues_for_channel
5821  * @vsi: ptr to VSI to which channels are associated with
5822  *
5823  * Helper function which returns max value among the queue counts set on the
5824  * channels/TCs created.
5825  **/
5826 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5827 {
5828 	struct i40e_channel *ch, *ch_tmp;
5829 	int max = 0;
5830 
5831 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5832 		if (!ch->initialized)
5833 			continue;
5834 		if (ch->num_queue_pairs > max)
5835 			max = ch->num_queue_pairs;
5836 	}
5837 
5838 	return max;
5839 }
5840 
5841 /**
5842  * i40e_validate_num_queues - validate num_queues w.r.t channel
5843  * @pf: ptr to PF device
5844  * @num_queues: number of queues
5845  * @vsi: the parent VSI
5846  * @reconfig_rss: indicates should the RSS be reconfigured or not
5847  *
5848  * This function validates number of queues in the context of new channel
5849  * which is being established and determines if RSS should be reconfigured
5850  * or not for parent VSI.
5851  **/
5852 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5853 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5854 {
5855 	int max_ch_queues;
5856 
5857 	if (!reconfig_rss)
5858 		return -EINVAL;
5859 
5860 	*reconfig_rss = false;
5861 	if (vsi->current_rss_size) {
5862 		if (num_queues > vsi->current_rss_size) {
5863 			dev_dbg(&pf->pdev->dev,
5864 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5865 				num_queues, vsi->current_rss_size);
5866 			return -EINVAL;
5867 		} else if ((num_queues < vsi->current_rss_size) &&
5868 			   (!is_power_of_2(num_queues))) {
5869 			dev_dbg(&pf->pdev->dev,
5870 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5871 				num_queues, vsi->current_rss_size);
5872 			return -EINVAL;
5873 		}
5874 	}
5875 
5876 	if (!is_power_of_2(num_queues)) {
5877 		/* Find the max num_queues configured for channel if channel
5878 		 * exist.
5879 		 * if channel exist, then enforce 'num_queues' to be more than
5880 		 * max ever queues configured for channel.
5881 		 */
5882 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5883 		if (num_queues < max_ch_queues) {
5884 			dev_dbg(&pf->pdev->dev,
5885 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5886 				num_queues, max_ch_queues);
5887 			return -EINVAL;
5888 		}
5889 		*reconfig_rss = true;
5890 	}
5891 
5892 	return 0;
5893 }
5894 
5895 /**
5896  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5897  * @vsi: the VSI being setup
5898  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5899  *
5900  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5901  **/
5902 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5903 {
5904 	struct i40e_pf *pf = vsi->back;
5905 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5906 	struct i40e_hw *hw = &pf->hw;
5907 	int local_rss_size;
5908 	u8 *lut;
5909 	int ret;
5910 
5911 	if (!vsi->rss_size)
5912 		return -EINVAL;
5913 
5914 	if (rss_size > vsi->rss_size)
5915 		return -EINVAL;
5916 
5917 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5918 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5919 	if (!lut)
5920 		return -ENOMEM;
5921 
5922 	/* Ignoring user configured lut if there is one */
5923 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5924 
5925 	/* Use user configured hash key if there is one, otherwise
5926 	 * use default.
5927 	 */
5928 	if (vsi->rss_hkey_user)
5929 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5930 	else
5931 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5932 
5933 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5934 	if (ret) {
5935 		dev_info(&pf->pdev->dev,
5936 			 "Cannot set RSS lut, err %s aq_err %s\n",
5937 			 i40e_stat_str(hw, ret),
5938 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5939 		kfree(lut);
5940 		return ret;
5941 	}
5942 	kfree(lut);
5943 
5944 	/* Do the update w.r.t. storing rss_size */
5945 	if (!vsi->orig_rss_size)
5946 		vsi->orig_rss_size = vsi->rss_size;
5947 	vsi->current_rss_size = local_rss_size;
5948 
5949 	return ret;
5950 }
5951 
5952 /**
5953  * i40e_channel_setup_queue_map - Setup a channel queue map
5954  * @pf: ptr to PF device
5955  * @ctxt: VSI context structure
5956  * @ch: ptr to channel structure
5957  *
5958  * Setup queue map for a specific channel
5959  **/
5960 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5961 					 struct i40e_vsi_context *ctxt,
5962 					 struct i40e_channel *ch)
5963 {
5964 	u16 qcount, qmap, sections = 0;
5965 	u8 offset = 0;
5966 	int pow;
5967 
5968 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5969 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5970 
5971 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5972 	ch->num_queue_pairs = qcount;
5973 
5974 	/* find the next higher power-of-2 of num queue pairs */
5975 	pow = ilog2(qcount);
5976 	if (!is_power_of_2(qcount))
5977 		pow++;
5978 
5979 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5980 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5981 
5982 	/* Setup queue TC[0].qmap for given VSI context */
5983 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5984 
5985 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5986 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5987 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5988 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5989 }
5990 
5991 /**
5992  * i40e_add_channel - add a channel by adding VSI
5993  * @pf: ptr to PF device
5994  * @uplink_seid: underlying HW switching element (VEB) ID
5995  * @ch: ptr to channel structure
5996  *
5997  * Add a channel (VSI) using add_vsi and queue_map
5998  **/
5999 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6000 			    struct i40e_channel *ch)
6001 {
6002 	struct i40e_hw *hw = &pf->hw;
6003 	struct i40e_vsi_context ctxt;
6004 	u8 enabled_tc = 0x1; /* TC0 enabled */
6005 	int ret;
6006 
6007 	if (ch->type != I40E_VSI_VMDQ2) {
6008 		dev_info(&pf->pdev->dev,
6009 			 "add new vsi failed, ch->type %d\n", ch->type);
6010 		return -EINVAL;
6011 	}
6012 
6013 	memset(&ctxt, 0, sizeof(ctxt));
6014 	ctxt.pf_num = hw->pf_id;
6015 	ctxt.vf_num = 0;
6016 	ctxt.uplink_seid = uplink_seid;
6017 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6018 	if (ch->type == I40E_VSI_VMDQ2)
6019 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6020 
6021 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6022 		ctxt.info.valid_sections |=
6023 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6024 		ctxt.info.switch_id =
6025 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6026 	}
6027 
6028 	/* Set queue map for a given VSI context */
6029 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6030 
6031 	/* Now time to create VSI */
6032 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6033 	if (ret) {
6034 		dev_info(&pf->pdev->dev,
6035 			 "add new vsi failed, err %s aq_err %s\n",
6036 			 i40e_stat_str(&pf->hw, ret),
6037 			 i40e_aq_str(&pf->hw,
6038 				     pf->hw.aq.asq_last_status));
6039 		return -ENOENT;
6040 	}
6041 
6042 	/* Success, update channel, set enabled_tc only if the channel
6043 	 * is not a macvlan
6044 	 */
6045 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6046 	ch->seid = ctxt.seid;
6047 	ch->vsi_number = ctxt.vsi_number;
6048 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6049 
6050 	/* copy just the sections touched not the entire info
6051 	 * since not all sections are valid as returned by
6052 	 * update vsi params
6053 	 */
6054 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6055 	memcpy(&ch->info.queue_mapping,
6056 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6057 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6058 	       sizeof(ctxt.info.tc_mapping));
6059 
6060 	return 0;
6061 }
6062 
6063 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6064 				  u8 *bw_share)
6065 {
6066 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6067 	i40e_status ret;
6068 	int i;
6069 
6070 	memset(&bw_data, 0, sizeof(bw_data));
6071 	bw_data.tc_valid_bits = ch->enabled_tc;
6072 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6073 		bw_data.tc_bw_credits[i] = bw_share[i];
6074 
6075 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6076 				       &bw_data, NULL);
6077 	if (ret) {
6078 		dev_info(&vsi->back->pdev->dev,
6079 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6080 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6081 		return -EINVAL;
6082 	}
6083 
6084 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6085 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6086 
6087 	return 0;
6088 }
6089 
6090 /**
6091  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6092  * @pf: ptr to PF device
6093  * @vsi: the VSI being setup
6094  * @ch: ptr to channel structure
6095  *
6096  * Configure TX rings associated with channel (VSI) since queues are being
6097  * from parent VSI.
6098  **/
6099 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6100 				       struct i40e_vsi *vsi,
6101 				       struct i40e_channel *ch)
6102 {
6103 	i40e_status ret;
6104 	int i;
6105 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6106 
6107 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6108 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6109 		if (ch->enabled_tc & BIT(i))
6110 			bw_share[i] = 1;
6111 	}
6112 
6113 	/* configure BW for new VSI */
6114 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6115 	if (ret) {
6116 		dev_info(&vsi->back->pdev->dev,
6117 			 "Failed configuring TC map %d for channel (seid %u)\n",
6118 			 ch->enabled_tc, ch->seid);
6119 		return ret;
6120 	}
6121 
6122 	for (i = 0; i < ch->num_queue_pairs; i++) {
6123 		struct i40e_ring *tx_ring, *rx_ring;
6124 		u16 pf_q;
6125 
6126 		pf_q = ch->base_queue + i;
6127 
6128 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6129 		 * context
6130 		 */
6131 		tx_ring = vsi->tx_rings[pf_q];
6132 		tx_ring->ch = ch;
6133 
6134 		/* Get the RX ring ptr */
6135 		rx_ring = vsi->rx_rings[pf_q];
6136 		rx_ring->ch = ch;
6137 	}
6138 
6139 	return 0;
6140 }
6141 
6142 /**
6143  * i40e_setup_hw_channel - setup new channel
6144  * @pf: ptr to PF device
6145  * @vsi: the VSI being setup
6146  * @ch: ptr to channel structure
6147  * @uplink_seid: underlying HW switching element (VEB) ID
6148  * @type: type of channel to be created (VMDq2/VF)
6149  *
6150  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6151  * and configures TX rings accordingly
6152  **/
6153 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6154 					struct i40e_vsi *vsi,
6155 					struct i40e_channel *ch,
6156 					u16 uplink_seid, u8 type)
6157 {
6158 	int ret;
6159 
6160 	ch->initialized = false;
6161 	ch->base_queue = vsi->next_base_queue;
6162 	ch->type = type;
6163 
6164 	/* Proceed with creation of channel (VMDq2) VSI */
6165 	ret = i40e_add_channel(pf, uplink_seid, ch);
6166 	if (ret) {
6167 		dev_info(&pf->pdev->dev,
6168 			 "failed to add_channel using uplink_seid %u\n",
6169 			 uplink_seid);
6170 		return ret;
6171 	}
6172 
6173 	/* Mark the successful creation of channel */
6174 	ch->initialized = true;
6175 
6176 	/* Reconfigure TX queues using QTX_CTL register */
6177 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6178 	if (ret) {
6179 		dev_info(&pf->pdev->dev,
6180 			 "failed to configure TX rings for channel %u\n",
6181 			 ch->seid);
6182 		return ret;
6183 	}
6184 
6185 	/* update 'next_base_queue' */
6186 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6187 	dev_dbg(&pf->pdev->dev,
6188 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6189 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6190 		ch->num_queue_pairs,
6191 		vsi->next_base_queue);
6192 	return ret;
6193 }
6194 
6195 /**
6196  * i40e_setup_channel - setup new channel using uplink element
6197  * @pf: ptr to PF device
6198  * @vsi: pointer to the VSI to set up the channel within
6199  * @ch: ptr to channel structure
6200  *
6201  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6202  * and uplink switching element (uplink_seid)
6203  **/
6204 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6205 			       struct i40e_channel *ch)
6206 {
6207 	u8 vsi_type;
6208 	u16 seid;
6209 	int ret;
6210 
6211 	if (vsi->type == I40E_VSI_MAIN) {
6212 		vsi_type = I40E_VSI_VMDQ2;
6213 	} else {
6214 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6215 			vsi->type);
6216 		return false;
6217 	}
6218 
6219 	/* underlying switching element */
6220 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6221 
6222 	/* create channel (VSI), configure TX rings */
6223 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6224 	if (ret) {
6225 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6226 		return false;
6227 	}
6228 
6229 	return ch->initialized ? true : false;
6230 }
6231 
6232 /**
6233  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6234  * @vsi: ptr to VSI which has PF backing
6235  *
6236  * Sets up switch mode correctly if it needs to be changed and perform
6237  * what are allowed modes.
6238  **/
6239 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6240 {
6241 	u8 mode;
6242 	struct i40e_pf *pf = vsi->back;
6243 	struct i40e_hw *hw = &pf->hw;
6244 	int ret;
6245 
6246 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6247 	if (ret)
6248 		return -EINVAL;
6249 
6250 	if (hw->dev_caps.switch_mode) {
6251 		/* if switch mode is set, support mode2 (non-tunneled for
6252 		 * cloud filter) for now
6253 		 */
6254 		u32 switch_mode = hw->dev_caps.switch_mode &
6255 				  I40E_SWITCH_MODE_MASK;
6256 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6257 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6258 				return 0;
6259 			dev_err(&pf->pdev->dev,
6260 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6261 				hw->dev_caps.switch_mode);
6262 			return -EINVAL;
6263 		}
6264 	}
6265 
6266 	/* Set Bit 7 to be valid */
6267 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6268 
6269 	/* Set L4type for TCP support */
6270 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6271 
6272 	/* Set cloud filter mode */
6273 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6274 
6275 	/* Prep mode field for set_switch_config */
6276 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6277 					pf->last_sw_conf_valid_flags,
6278 					mode, NULL);
6279 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6280 		dev_err(&pf->pdev->dev,
6281 			"couldn't set switch config bits, err %s aq_err %s\n",
6282 			i40e_stat_str(hw, ret),
6283 			i40e_aq_str(hw,
6284 				    hw->aq.asq_last_status));
6285 
6286 	return ret;
6287 }
6288 
6289 /**
6290  * i40e_create_queue_channel - function to create channel
6291  * @vsi: VSI to be configured
6292  * @ch: ptr to channel (it contains channel specific params)
6293  *
6294  * This function creates channel (VSI) using num_queues specified by user,
6295  * reconfigs RSS if needed.
6296  **/
6297 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6298 			      struct i40e_channel *ch)
6299 {
6300 	struct i40e_pf *pf = vsi->back;
6301 	bool reconfig_rss;
6302 	int err;
6303 
6304 	if (!ch)
6305 		return -EINVAL;
6306 
6307 	if (!ch->num_queue_pairs) {
6308 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6309 			ch->num_queue_pairs);
6310 		return -EINVAL;
6311 	}
6312 
6313 	/* validate user requested num_queues for channel */
6314 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6315 				       &reconfig_rss);
6316 	if (err) {
6317 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6318 			 ch->num_queue_pairs);
6319 		return -EINVAL;
6320 	}
6321 
6322 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6323 	 * VSI to be added switch to VEB mode.
6324 	 */
6325 
6326 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6327 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6328 
6329 		if (vsi->type == I40E_VSI_MAIN) {
6330 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6331 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6332 			else
6333 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6334 		}
6335 		/* now onwards for main VSI, number of queues will be value
6336 		 * of TC0's queue count
6337 		 */
6338 	}
6339 
6340 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6341 	 * it should be more than num_queues
6342 	 */
6343 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6344 		dev_dbg(&pf->pdev->dev,
6345 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6346 			vsi->cnt_q_avail, ch->num_queue_pairs);
6347 		return -EINVAL;
6348 	}
6349 
6350 	/* reconfig_rss only if vsi type is MAIN_VSI */
6351 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6352 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6353 		if (err) {
6354 			dev_info(&pf->pdev->dev,
6355 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6356 				 ch->num_queue_pairs);
6357 			return -EINVAL;
6358 		}
6359 	}
6360 
6361 	if (!i40e_setup_channel(pf, vsi, ch)) {
6362 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6363 		return -EINVAL;
6364 	}
6365 
6366 	dev_info(&pf->pdev->dev,
6367 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6368 		 ch->seid, ch->num_queue_pairs);
6369 
6370 	/* configure VSI for BW limit */
6371 	if (ch->max_tx_rate) {
6372 		u64 credits = ch->max_tx_rate;
6373 
6374 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6375 			return -EINVAL;
6376 
6377 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6378 		dev_dbg(&pf->pdev->dev,
6379 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6380 			ch->max_tx_rate,
6381 			credits,
6382 			ch->seid);
6383 	}
6384 
6385 	/* in case of VF, this will be main SRIOV VSI */
6386 	ch->parent_vsi = vsi;
6387 
6388 	/* and update main_vsi's count for queue_available to use */
6389 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6390 
6391 	return 0;
6392 }
6393 
6394 /**
6395  * i40e_configure_queue_channels - Add queue channel for the given TCs
6396  * @vsi: VSI to be configured
6397  *
6398  * Configures queue channel mapping to the given TCs
6399  **/
6400 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6401 {
6402 	struct i40e_channel *ch;
6403 	u64 max_rate = 0;
6404 	int ret = 0, i;
6405 
6406 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6407 	vsi->tc_seid_map[0] = vsi->seid;
6408 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6409 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6410 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6411 			if (!ch) {
6412 				ret = -ENOMEM;
6413 				goto err_free;
6414 			}
6415 
6416 			INIT_LIST_HEAD(&ch->list);
6417 			ch->num_queue_pairs =
6418 				vsi->tc_config.tc_info[i].qcount;
6419 			ch->base_queue =
6420 				vsi->tc_config.tc_info[i].qoffset;
6421 
6422 			/* Bandwidth limit through tc interface is in bytes/s,
6423 			 * change to Mbit/s
6424 			 */
6425 			max_rate = vsi->mqprio_qopt.max_rate[i];
6426 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6427 			ch->max_tx_rate = max_rate;
6428 
6429 			list_add_tail(&ch->list, &vsi->ch_list);
6430 
6431 			ret = i40e_create_queue_channel(vsi, ch);
6432 			if (ret) {
6433 				dev_err(&vsi->back->pdev->dev,
6434 					"Failed creating queue channel with TC%d: queues %d\n",
6435 					i, ch->num_queue_pairs);
6436 				goto err_free;
6437 			}
6438 			vsi->tc_seid_map[i] = ch->seid;
6439 		}
6440 	}
6441 	return ret;
6442 
6443 err_free:
6444 	i40e_remove_queue_channels(vsi);
6445 	return ret;
6446 }
6447 
6448 /**
6449  * i40e_veb_config_tc - Configure TCs for given VEB
6450  * @veb: given VEB
6451  * @enabled_tc: TC bitmap
6452  *
6453  * Configures given TC bitmap for VEB (switching) element
6454  **/
6455 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6456 {
6457 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6458 	struct i40e_pf *pf = veb->pf;
6459 	int ret = 0;
6460 	int i;
6461 
6462 	/* No TCs or already enabled TCs just return */
6463 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6464 		return ret;
6465 
6466 	bw_data.tc_valid_bits = enabled_tc;
6467 	/* bw_data.absolute_credits is not set (relative) */
6468 
6469 	/* Enable ETS TCs with equal BW Share for now */
6470 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6471 		if (enabled_tc & BIT(i))
6472 			bw_data.tc_bw_share_credits[i] = 1;
6473 	}
6474 
6475 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6476 						   &bw_data, NULL);
6477 	if (ret) {
6478 		dev_info(&pf->pdev->dev,
6479 			 "VEB bw config failed, err %s aq_err %s\n",
6480 			 i40e_stat_str(&pf->hw, ret),
6481 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6482 		goto out;
6483 	}
6484 
6485 	/* Update the BW information */
6486 	ret = i40e_veb_get_bw_info(veb);
6487 	if (ret) {
6488 		dev_info(&pf->pdev->dev,
6489 			 "Failed getting veb bw config, err %s aq_err %s\n",
6490 			 i40e_stat_str(&pf->hw, ret),
6491 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6492 	}
6493 
6494 out:
6495 	return ret;
6496 }
6497 
6498 #ifdef CONFIG_I40E_DCB
6499 /**
6500  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6501  * @pf: PF struct
6502  *
6503  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6504  * the caller would've quiesce all the VSIs before calling
6505  * this function
6506  **/
6507 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6508 {
6509 	u8 tc_map = 0;
6510 	int ret;
6511 	u8 v;
6512 
6513 	/* Enable the TCs available on PF to all VEBs */
6514 	tc_map = i40e_pf_get_tc_map(pf);
6515 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6516 		return;
6517 
6518 	for (v = 0; v < I40E_MAX_VEB; v++) {
6519 		if (!pf->veb[v])
6520 			continue;
6521 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6522 		if (ret) {
6523 			dev_info(&pf->pdev->dev,
6524 				 "Failed configuring TC for VEB seid=%d\n",
6525 				 pf->veb[v]->seid);
6526 			/* Will try to configure as many components */
6527 		}
6528 	}
6529 
6530 	/* Update each VSI */
6531 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6532 		if (!pf->vsi[v])
6533 			continue;
6534 
6535 		/* - Enable all TCs for the LAN VSI
6536 		 * - For all others keep them at TC0 for now
6537 		 */
6538 		if (v == pf->lan_vsi)
6539 			tc_map = i40e_pf_get_tc_map(pf);
6540 		else
6541 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6542 
6543 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6544 		if (ret) {
6545 			dev_info(&pf->pdev->dev,
6546 				 "Failed configuring TC for VSI seid=%d\n",
6547 				 pf->vsi[v]->seid);
6548 			/* Will try to configure as many components */
6549 		} else {
6550 			/* Re-configure VSI vectors based on updated TC map */
6551 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6552 			if (pf->vsi[v]->netdev)
6553 				i40e_dcbnl_set_all(pf->vsi[v]);
6554 		}
6555 	}
6556 }
6557 
6558 /**
6559  * i40e_resume_port_tx - Resume port Tx
6560  * @pf: PF struct
6561  *
6562  * Resume a port's Tx and issue a PF reset in case of failure to
6563  * resume.
6564  **/
6565 static int i40e_resume_port_tx(struct i40e_pf *pf)
6566 {
6567 	struct i40e_hw *hw = &pf->hw;
6568 	int ret;
6569 
6570 	ret = i40e_aq_resume_port_tx(hw, NULL);
6571 	if (ret) {
6572 		dev_info(&pf->pdev->dev,
6573 			 "Resume Port Tx failed, err %s aq_err %s\n",
6574 			  i40e_stat_str(&pf->hw, ret),
6575 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6576 		/* Schedule PF reset to recover */
6577 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6578 		i40e_service_event_schedule(pf);
6579 	}
6580 
6581 	return ret;
6582 }
6583 
6584 /**
6585  * i40e_suspend_port_tx - Suspend port Tx
6586  * @pf: PF struct
6587  *
6588  * Suspend a port's Tx and issue a PF reset in case of failure.
6589  **/
6590 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6591 {
6592 	struct i40e_hw *hw = &pf->hw;
6593 	int ret;
6594 
6595 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6596 	if (ret) {
6597 		dev_info(&pf->pdev->dev,
6598 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6599 			 i40e_stat_str(&pf->hw, ret),
6600 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6601 		/* Schedule PF reset to recover */
6602 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6603 		i40e_service_event_schedule(pf);
6604 	}
6605 
6606 	return ret;
6607 }
6608 
6609 /**
6610  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6611  * @pf: PF being configured
6612  * @new_cfg: New DCBX configuration
6613  *
6614  * Program DCB settings into HW and reconfigure VEB/VSIs on
6615  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6616  **/
6617 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6618 				  struct i40e_dcbx_config *new_cfg)
6619 {
6620 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6621 	int ret;
6622 
6623 	/* Check if need reconfiguration */
6624 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6625 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6626 		return 0;
6627 	}
6628 
6629 	/* Config change disable all VSIs */
6630 	i40e_pf_quiesce_all_vsi(pf);
6631 
6632 	/* Copy the new config to the current config */
6633 	*old_cfg = *new_cfg;
6634 	old_cfg->etsrec = old_cfg->etscfg;
6635 	ret = i40e_set_dcb_config(&pf->hw);
6636 	if (ret) {
6637 		dev_info(&pf->pdev->dev,
6638 			 "Set DCB Config failed, err %s aq_err %s\n",
6639 			 i40e_stat_str(&pf->hw, ret),
6640 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6641 		goto out;
6642 	}
6643 
6644 	/* Changes in configuration update VEB/VSI */
6645 	i40e_dcb_reconfigure(pf);
6646 out:
6647 	/* In case of reset do not try to resume anything */
6648 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6649 		/* Re-start the VSIs if disabled */
6650 		ret = i40e_resume_port_tx(pf);
6651 		/* In case of error no point in resuming VSIs */
6652 		if (ret)
6653 			goto err;
6654 		i40e_pf_unquiesce_all_vsi(pf);
6655 	}
6656 err:
6657 	return ret;
6658 }
6659 
6660 /**
6661  * i40e_hw_dcb_config - Program new DCBX settings into HW
6662  * @pf: PF being configured
6663  * @new_cfg: New DCBX configuration
6664  *
6665  * Program DCB settings into HW and reconfigure VEB/VSIs on
6666  * given PF
6667  **/
6668 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6669 {
6670 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6671 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6672 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6673 	struct i40e_dcbx_config *old_cfg;
6674 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6675 	struct i40e_rx_pb_config pb_cfg;
6676 	struct i40e_hw *hw = &pf->hw;
6677 	u8 num_ports = hw->num_ports;
6678 	bool need_reconfig;
6679 	int ret = -EINVAL;
6680 	u8 lltc_map = 0;
6681 	u8 tc_map = 0;
6682 	u8 new_numtc;
6683 	u8 i;
6684 
6685 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6686 	/* Un-pack information to Program ETS HW via shared API
6687 	 * numtc, tcmap
6688 	 * LLTC map
6689 	 * ETS/NON-ETS arbiter mode
6690 	 * max exponent (credit refills)
6691 	 * Total number of ports
6692 	 * PFC priority bit-map
6693 	 * Priority Table
6694 	 * BW % per TC
6695 	 * Arbiter mode between UPs sharing same TC
6696 	 * TSA table (ETS or non-ETS)
6697 	 * EEE enabled or not
6698 	 * MFS TC table
6699 	 */
6700 
6701 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6702 
6703 	memset(&ets_data, 0, sizeof(ets_data));
6704 	for (i = 0; i < new_numtc; i++) {
6705 		tc_map |= BIT(i);
6706 		switch (new_cfg->etscfg.tsatable[i]) {
6707 		case I40E_IEEE_TSA_ETS:
6708 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6709 			ets_data.tc_bw_share_credits[i] =
6710 					new_cfg->etscfg.tcbwtable[i];
6711 			break;
6712 		case I40E_IEEE_TSA_STRICT:
6713 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6714 			lltc_map |= BIT(i);
6715 			ets_data.tc_bw_share_credits[i] =
6716 					I40E_DCB_STRICT_PRIO_CREDITS;
6717 			break;
6718 		default:
6719 			/* Invalid TSA type */
6720 			need_reconfig = false;
6721 			goto out;
6722 		}
6723 	}
6724 
6725 	old_cfg = &hw->local_dcbx_config;
6726 	/* Check if need reconfiguration */
6727 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6728 
6729 	/* If needed, enable/disable frame tagging, disable all VSIs
6730 	 * and suspend port tx
6731 	 */
6732 	if (need_reconfig) {
6733 		/* Enable DCB tagging only when more than one TC */
6734 		if (new_numtc > 1)
6735 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6736 		else
6737 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6738 
6739 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6740 		/* Reconfiguration needed quiesce all VSIs */
6741 		i40e_pf_quiesce_all_vsi(pf);
6742 		ret = i40e_suspend_port_tx(pf);
6743 		if (ret)
6744 			goto err;
6745 	}
6746 
6747 	/* Configure Port ETS Tx Scheduler */
6748 	ets_data.tc_valid_bits = tc_map;
6749 	ets_data.tc_strict_priority_flags = lltc_map;
6750 	ret = i40e_aq_config_switch_comp_ets
6751 		(hw, pf->mac_seid, &ets_data,
6752 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6753 	if (ret) {
6754 		dev_info(&pf->pdev->dev,
6755 			 "Modify Port ETS failed, err %s aq_err %s\n",
6756 			 i40e_stat_str(&pf->hw, ret),
6757 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6758 		goto out;
6759 	}
6760 
6761 	/* Configure Rx ETS HW */
6762 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6763 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6764 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6765 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6766 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6767 				   lltc_map);
6768 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6769 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6770 				     prio_type);
6771 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6772 			       new_cfg->etscfg.prioritytable);
6773 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6774 
6775 	/* Configure Rx Packet Buffers in HW */
6776 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6777 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6778 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
6779 	}
6780 
6781 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
6782 					 false, new_cfg->pfc.pfcenable,
6783 					 mfs_tc, &pb_cfg);
6784 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
6785 
6786 	/* Update the local Rx Packet buffer config */
6787 	pf->pb_cfg = pb_cfg;
6788 
6789 	/* Inform the FW about changes to DCB configuration */
6790 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
6791 	if (ret) {
6792 		dev_info(&pf->pdev->dev,
6793 			 "DCB Updated failed, err %s aq_err %s\n",
6794 			 i40e_stat_str(&pf->hw, ret),
6795 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6796 		goto out;
6797 	}
6798 
6799 	/* Update the port DCBx configuration */
6800 	*old_cfg = *new_cfg;
6801 
6802 	/* Changes in configuration update VEB/VSI */
6803 	i40e_dcb_reconfigure(pf);
6804 out:
6805 	/* Re-start the VSIs if disabled */
6806 	if (need_reconfig) {
6807 		ret = i40e_resume_port_tx(pf);
6808 
6809 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
6810 		/* In case of error no point in resuming VSIs */
6811 		if (ret)
6812 			goto err;
6813 
6814 		/* Wait for the PF's queues to be disabled */
6815 		ret = i40e_pf_wait_queues_disabled(pf);
6816 		if (ret) {
6817 			/* Schedule PF reset to recover */
6818 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6819 			i40e_service_event_schedule(pf);
6820 			goto err;
6821 		} else {
6822 			i40e_pf_unquiesce_all_vsi(pf);
6823 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6824 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
6825 		}
6826 		/* registers are set, lets apply */
6827 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
6828 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
6829 	}
6830 
6831 err:
6832 	return ret;
6833 }
6834 
6835 /**
6836  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
6837  * @pf: PF being queried
6838  *
6839  * Set default DCB configuration in case DCB is to be done in SW.
6840  **/
6841 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
6842 {
6843 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
6844 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6845 	struct i40e_hw *hw = &pf->hw;
6846 	int err;
6847 
6848 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
6849 		/* Update the local cached instance with TC0 ETS */
6850 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
6851 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6852 		pf->tmp_cfg.etscfg.maxtcs = 0;
6853 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6854 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
6855 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
6856 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
6857 		/* FW needs one App to configure HW */
6858 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
6859 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
6860 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
6861 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
6862 
6863 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
6864 	}
6865 
6866 	memset(&ets_data, 0, sizeof(ets_data));
6867 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
6868 	ets_data.tc_strict_priority_flags = 0; /* ETS */
6869 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
6870 
6871 	/* Enable ETS on the Physical port */
6872 	err = i40e_aq_config_switch_comp_ets
6873 		(hw, pf->mac_seid, &ets_data,
6874 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
6875 	if (err) {
6876 		dev_info(&pf->pdev->dev,
6877 			 "Enable Port ETS failed, err %s aq_err %s\n",
6878 			 i40e_stat_str(&pf->hw, err),
6879 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6880 		err = -ENOENT;
6881 		goto out;
6882 	}
6883 
6884 	/* Update the local cached instance with TC0 ETS */
6885 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
6886 	dcb_cfg->etscfg.cbs = 0;
6887 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
6888 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
6889 
6890 out:
6891 	return err;
6892 }
6893 
6894 /**
6895  * i40e_init_pf_dcb - Initialize DCB configuration
6896  * @pf: PF being configured
6897  *
6898  * Query the current DCB configuration and cache it
6899  * in the hardware structure
6900  **/
6901 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6902 {
6903 	struct i40e_hw *hw = &pf->hw;
6904 	int err;
6905 
6906 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6907 	 * Also do not enable DCBx if FW LLDP agent is disabled
6908 	 */
6909 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
6910 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
6911 		err = I40E_NOT_SUPPORTED;
6912 		goto out;
6913 	}
6914 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
6915 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
6916 		err = i40e_dcb_sw_default_config(pf);
6917 		if (err) {
6918 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
6919 			goto out;
6920 		}
6921 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
6922 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
6923 			       DCB_CAP_DCBX_VER_IEEE;
6924 		/* at init capable but disabled */
6925 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
6926 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6927 		goto out;
6928 	}
6929 	err = i40e_init_dcb(hw, true);
6930 	if (!err) {
6931 		/* Device/Function is not DCBX capable */
6932 		if ((!hw->func_caps.dcb) ||
6933 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6934 			dev_info(&pf->pdev->dev,
6935 				 "DCBX offload is not supported or is disabled for this PF.\n");
6936 		} else {
6937 			/* When status is not DISABLED then DCBX in FW */
6938 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6939 				       DCB_CAP_DCBX_VER_IEEE;
6940 
6941 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6942 			/* Enable DCB tagging only when more than one TC
6943 			 * or explicitly disable if only one TC
6944 			 */
6945 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6946 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6947 			else
6948 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6949 			dev_dbg(&pf->pdev->dev,
6950 				"DCBX offload is supported for this PF.\n");
6951 		}
6952 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6953 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6954 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6955 	} else {
6956 		dev_info(&pf->pdev->dev,
6957 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6958 			 i40e_stat_str(&pf->hw, err),
6959 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6960 	}
6961 
6962 out:
6963 	return err;
6964 }
6965 #endif /* CONFIG_I40E_DCB */
6966 
6967 /**
6968  * i40e_print_link_message - print link up or down
6969  * @vsi: the VSI for which link needs a message
6970  * @isup: true of link is up, false otherwise
6971  */
6972 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6973 {
6974 	enum i40e_aq_link_speed new_speed;
6975 	struct i40e_pf *pf = vsi->back;
6976 	char *speed = "Unknown";
6977 	char *fc = "Unknown";
6978 	char *fec = "";
6979 	char *req_fec = "";
6980 	char *an = "";
6981 
6982 	if (isup)
6983 		new_speed = pf->hw.phy.link_info.link_speed;
6984 	else
6985 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6986 
6987 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6988 		return;
6989 	vsi->current_isup = isup;
6990 	vsi->current_speed = new_speed;
6991 	if (!isup) {
6992 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6993 		return;
6994 	}
6995 
6996 	/* Warn user if link speed on NPAR enabled partition is not at
6997 	 * least 10GB
6998 	 */
6999 	if (pf->hw.func_caps.npar_enable &&
7000 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7001 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7002 		netdev_warn(vsi->netdev,
7003 			    "The partition detected link speed that is less than 10Gbps\n");
7004 
7005 	switch (pf->hw.phy.link_info.link_speed) {
7006 	case I40E_LINK_SPEED_40GB:
7007 		speed = "40 G";
7008 		break;
7009 	case I40E_LINK_SPEED_20GB:
7010 		speed = "20 G";
7011 		break;
7012 	case I40E_LINK_SPEED_25GB:
7013 		speed = "25 G";
7014 		break;
7015 	case I40E_LINK_SPEED_10GB:
7016 		speed = "10 G";
7017 		break;
7018 	case I40E_LINK_SPEED_5GB:
7019 		speed = "5 G";
7020 		break;
7021 	case I40E_LINK_SPEED_2_5GB:
7022 		speed = "2.5 G";
7023 		break;
7024 	case I40E_LINK_SPEED_1GB:
7025 		speed = "1000 M";
7026 		break;
7027 	case I40E_LINK_SPEED_100MB:
7028 		speed = "100 M";
7029 		break;
7030 	default:
7031 		break;
7032 	}
7033 
7034 	switch (pf->hw.fc.current_mode) {
7035 	case I40E_FC_FULL:
7036 		fc = "RX/TX";
7037 		break;
7038 	case I40E_FC_TX_PAUSE:
7039 		fc = "TX";
7040 		break;
7041 	case I40E_FC_RX_PAUSE:
7042 		fc = "RX";
7043 		break;
7044 	default:
7045 		fc = "None";
7046 		break;
7047 	}
7048 
7049 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7050 		req_fec = "None";
7051 		fec = "None";
7052 		an = "False";
7053 
7054 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7055 			an = "True";
7056 
7057 		if (pf->hw.phy.link_info.fec_info &
7058 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7059 			fec = "CL74 FC-FEC/BASE-R";
7060 		else if (pf->hw.phy.link_info.fec_info &
7061 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7062 			fec = "CL108 RS-FEC";
7063 
7064 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7065 		 * both RS and FC are requested
7066 		 */
7067 		if (vsi->back->hw.phy.link_info.req_fec_info &
7068 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7069 			if (vsi->back->hw.phy.link_info.req_fec_info &
7070 			    I40E_AQ_REQUEST_FEC_RS)
7071 				req_fec = "CL108 RS-FEC";
7072 			else
7073 				req_fec = "CL74 FC-FEC/BASE-R";
7074 		}
7075 		netdev_info(vsi->netdev,
7076 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7077 			    speed, req_fec, fec, an, fc);
7078 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7079 		req_fec = "None";
7080 		fec = "None";
7081 		an = "False";
7082 
7083 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7084 			an = "True";
7085 
7086 		if (pf->hw.phy.link_info.fec_info &
7087 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7088 			fec = "CL74 FC-FEC/BASE-R";
7089 
7090 		if (pf->hw.phy.link_info.req_fec_info &
7091 		    I40E_AQ_REQUEST_FEC_KR)
7092 			req_fec = "CL74 FC-FEC/BASE-R";
7093 
7094 		netdev_info(vsi->netdev,
7095 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7096 			    speed, req_fec, fec, an, fc);
7097 	} else {
7098 		netdev_info(vsi->netdev,
7099 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7100 			    speed, fc);
7101 	}
7102 
7103 }
7104 
7105 /**
7106  * i40e_up_complete - Finish the last steps of bringing up a connection
7107  * @vsi: the VSI being configured
7108  **/
7109 static int i40e_up_complete(struct i40e_vsi *vsi)
7110 {
7111 	struct i40e_pf *pf = vsi->back;
7112 	int err;
7113 
7114 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7115 		i40e_vsi_configure_msix(vsi);
7116 	else
7117 		i40e_configure_msi_and_legacy(vsi);
7118 
7119 	/* start rings */
7120 	err = i40e_vsi_start_rings(vsi);
7121 	if (err)
7122 		return err;
7123 
7124 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7125 	i40e_napi_enable_all(vsi);
7126 	i40e_vsi_enable_irq(vsi);
7127 
7128 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7129 	    (vsi->netdev)) {
7130 		i40e_print_link_message(vsi, true);
7131 		netif_tx_start_all_queues(vsi->netdev);
7132 		netif_carrier_on(vsi->netdev);
7133 	}
7134 
7135 	/* replay FDIR SB filters */
7136 	if (vsi->type == I40E_VSI_FDIR) {
7137 		/* reset fd counters */
7138 		pf->fd_add_err = 0;
7139 		pf->fd_atr_cnt = 0;
7140 		i40e_fdir_filter_restore(vsi);
7141 	}
7142 
7143 	/* On the next run of the service_task, notify any clients of the new
7144 	 * opened netdev
7145 	 */
7146 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7147 	i40e_service_event_schedule(pf);
7148 
7149 	return 0;
7150 }
7151 
7152 /**
7153  * i40e_vsi_reinit_locked - Reset the VSI
7154  * @vsi: the VSI being configured
7155  *
7156  * Rebuild the ring structs after some configuration
7157  * has changed, e.g. MTU size.
7158  **/
7159 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7160 {
7161 	struct i40e_pf *pf = vsi->back;
7162 
7163 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7164 		usleep_range(1000, 2000);
7165 	i40e_down(vsi);
7166 
7167 	i40e_up(vsi);
7168 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7169 }
7170 
7171 /**
7172  * i40e_force_link_state - Force the link status
7173  * @pf: board private structure
7174  * @is_up: whether the link state should be forced up or down
7175  **/
7176 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7177 {
7178 	struct i40e_aq_get_phy_abilities_resp abilities;
7179 	struct i40e_aq_set_phy_config config = {0};
7180 	bool non_zero_phy_type = is_up;
7181 	struct i40e_hw *hw = &pf->hw;
7182 	i40e_status err;
7183 	u64 mask;
7184 	u8 speed;
7185 
7186 	/* Card might've been put in an unstable state by other drivers
7187 	 * and applications, which causes incorrect speed values being
7188 	 * set on startup. In order to clear speed registers, we call
7189 	 * get_phy_capabilities twice, once to get initial state of
7190 	 * available speeds, and once to get current PHY config.
7191 	 */
7192 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7193 					   NULL);
7194 	if (err) {
7195 		dev_err(&pf->pdev->dev,
7196 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7197 			i40e_stat_str(hw, err),
7198 			i40e_aq_str(hw, hw->aq.asq_last_status));
7199 		return err;
7200 	}
7201 	speed = abilities.link_speed;
7202 
7203 	/* Get the current phy config */
7204 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7205 					   NULL);
7206 	if (err) {
7207 		dev_err(&pf->pdev->dev,
7208 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7209 			i40e_stat_str(hw, err),
7210 			i40e_aq_str(hw, hw->aq.asq_last_status));
7211 		return err;
7212 	}
7213 
7214 	/* If link needs to go up, but was not forced to go down,
7215 	 * and its speed values are OK, no need for a flap
7216 	 * if non_zero_phy_type was set, still need to force up
7217 	 */
7218 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7219 		non_zero_phy_type = true;
7220 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7221 		return I40E_SUCCESS;
7222 
7223 	/* To force link we need to set bits for all supported PHY types,
7224 	 * but there are now more than 32, so we need to split the bitmap
7225 	 * across two fields.
7226 	 */
7227 	mask = I40E_PHY_TYPES_BITMASK;
7228 	config.phy_type =
7229 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7230 	config.phy_type_ext =
7231 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7232 	/* Copy the old settings, except of phy_type */
7233 	config.abilities = abilities.abilities;
7234 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7235 		if (is_up)
7236 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7237 		else
7238 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7239 	}
7240 	if (abilities.link_speed != 0)
7241 		config.link_speed = abilities.link_speed;
7242 	else
7243 		config.link_speed = speed;
7244 	config.eee_capability = abilities.eee_capability;
7245 	config.eeer = abilities.eeer_val;
7246 	config.low_power_ctrl = abilities.d3_lpan;
7247 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7248 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7249 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7250 
7251 	if (err) {
7252 		dev_err(&pf->pdev->dev,
7253 			"set phy config ret =  %s last_status =  %s\n",
7254 			i40e_stat_str(&pf->hw, err),
7255 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7256 		return err;
7257 	}
7258 
7259 	/* Update the link info */
7260 	err = i40e_update_link_info(hw);
7261 	if (err) {
7262 		/* Wait a little bit (on 40G cards it sometimes takes a really
7263 		 * long time for link to come back from the atomic reset)
7264 		 * and try once more
7265 		 */
7266 		msleep(1000);
7267 		i40e_update_link_info(hw);
7268 	}
7269 
7270 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7271 
7272 	return I40E_SUCCESS;
7273 }
7274 
7275 /**
7276  * i40e_up - Bring the connection back up after being down
7277  * @vsi: the VSI being configured
7278  **/
7279 int i40e_up(struct i40e_vsi *vsi)
7280 {
7281 	int err;
7282 
7283 	if (vsi->type == I40E_VSI_MAIN &&
7284 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7285 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7286 		i40e_force_link_state(vsi->back, true);
7287 
7288 	err = i40e_vsi_configure(vsi);
7289 	if (!err)
7290 		err = i40e_up_complete(vsi);
7291 
7292 	return err;
7293 }
7294 
7295 /**
7296  * i40e_down - Shutdown the connection processing
7297  * @vsi: the VSI being stopped
7298  **/
7299 void i40e_down(struct i40e_vsi *vsi)
7300 {
7301 	int i;
7302 
7303 	/* It is assumed that the caller of this function
7304 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7305 	 */
7306 	if (vsi->netdev) {
7307 		netif_carrier_off(vsi->netdev);
7308 		netif_tx_disable(vsi->netdev);
7309 	}
7310 	i40e_vsi_disable_irq(vsi);
7311 	i40e_vsi_stop_rings(vsi);
7312 	if (vsi->type == I40E_VSI_MAIN &&
7313 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7314 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7315 		i40e_force_link_state(vsi->back, false);
7316 	i40e_napi_disable_all(vsi);
7317 
7318 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7319 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7320 		if (i40e_enabled_xdp_vsi(vsi)) {
7321 			/* Make sure that in-progress ndo_xdp_xmit and
7322 			 * ndo_xsk_wakeup calls are completed.
7323 			 */
7324 			synchronize_rcu();
7325 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7326 		}
7327 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7328 	}
7329 
7330 }
7331 
7332 /**
7333  * i40e_validate_mqprio_qopt- validate queue mapping info
7334  * @vsi: the VSI being configured
7335  * @mqprio_qopt: queue parametrs
7336  **/
7337 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7338 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7339 {
7340 	u64 sum_max_rate = 0;
7341 	u64 max_rate = 0;
7342 	int i;
7343 
7344 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7345 	    mqprio_qopt->qopt.num_tc < 1 ||
7346 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7347 		return -EINVAL;
7348 	for (i = 0; ; i++) {
7349 		if (!mqprio_qopt->qopt.count[i])
7350 			return -EINVAL;
7351 		if (mqprio_qopt->min_rate[i]) {
7352 			dev_err(&vsi->back->pdev->dev,
7353 				"Invalid min tx rate (greater than 0) specified\n");
7354 			return -EINVAL;
7355 		}
7356 		max_rate = mqprio_qopt->max_rate[i];
7357 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7358 		sum_max_rate += max_rate;
7359 
7360 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7361 			break;
7362 		if (mqprio_qopt->qopt.offset[i + 1] !=
7363 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7364 			return -EINVAL;
7365 	}
7366 	if (vsi->num_queue_pairs <
7367 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7368 		dev_err(&vsi->back->pdev->dev,
7369 			"Failed to create traffic channel, insufficient number of queues.\n");
7370 		return -EINVAL;
7371 	}
7372 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7373 		dev_err(&vsi->back->pdev->dev,
7374 			"Invalid max tx rate specified\n");
7375 		return -EINVAL;
7376 	}
7377 	return 0;
7378 }
7379 
7380 /**
7381  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7382  * @vsi: the VSI being configured
7383  **/
7384 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7385 {
7386 	u16 qcount;
7387 	int i;
7388 
7389 	/* Only TC0 is enabled */
7390 	vsi->tc_config.numtc = 1;
7391 	vsi->tc_config.enabled_tc = 1;
7392 	qcount = min_t(int, vsi->alloc_queue_pairs,
7393 		       i40e_pf_get_max_q_per_tc(vsi->back));
7394 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7395 		/* For the TC that is not enabled set the offset to default
7396 		 * queue and allocate one queue for the given TC.
7397 		 */
7398 		vsi->tc_config.tc_info[i].qoffset = 0;
7399 		if (i == 0)
7400 			vsi->tc_config.tc_info[i].qcount = qcount;
7401 		else
7402 			vsi->tc_config.tc_info[i].qcount = 1;
7403 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7404 	}
7405 }
7406 
7407 /**
7408  * i40e_del_macvlan_filter
7409  * @hw: pointer to the HW structure
7410  * @seid: seid of the channel VSI
7411  * @macaddr: the mac address to apply as a filter
7412  * @aq_err: store the admin Q error
7413  *
7414  * This function deletes a mac filter on the channel VSI which serves as the
7415  * macvlan. Returns 0 on success.
7416  **/
7417 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7418 					   const u8 *macaddr, int *aq_err)
7419 {
7420 	struct i40e_aqc_remove_macvlan_element_data element;
7421 	i40e_status status;
7422 
7423 	memset(&element, 0, sizeof(element));
7424 	ether_addr_copy(element.mac_addr, macaddr);
7425 	element.vlan_tag = 0;
7426 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7427 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7428 	*aq_err = hw->aq.asq_last_status;
7429 
7430 	return status;
7431 }
7432 
7433 /**
7434  * i40e_add_macvlan_filter
7435  * @hw: pointer to the HW structure
7436  * @seid: seid of the channel VSI
7437  * @macaddr: the mac address to apply as a filter
7438  * @aq_err: store the admin Q error
7439  *
7440  * This function adds a mac filter on the channel VSI which serves as the
7441  * macvlan. Returns 0 on success.
7442  **/
7443 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7444 					   const u8 *macaddr, int *aq_err)
7445 {
7446 	struct i40e_aqc_add_macvlan_element_data element;
7447 	i40e_status status;
7448 	u16 cmd_flags = 0;
7449 
7450 	ether_addr_copy(element.mac_addr, macaddr);
7451 	element.vlan_tag = 0;
7452 	element.queue_number = 0;
7453 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7454 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7455 	element.flags = cpu_to_le16(cmd_flags);
7456 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7457 	*aq_err = hw->aq.asq_last_status;
7458 
7459 	return status;
7460 }
7461 
7462 /**
7463  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7464  * @vsi: the VSI we want to access
7465  * @ch: the channel we want to access
7466  */
7467 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7468 {
7469 	struct i40e_ring *tx_ring, *rx_ring;
7470 	u16 pf_q;
7471 	int i;
7472 
7473 	for (i = 0; i < ch->num_queue_pairs; i++) {
7474 		pf_q = ch->base_queue + i;
7475 		tx_ring = vsi->tx_rings[pf_q];
7476 		tx_ring->ch = NULL;
7477 		rx_ring = vsi->rx_rings[pf_q];
7478 		rx_ring->ch = NULL;
7479 	}
7480 }
7481 
7482 /**
7483  * i40e_free_macvlan_channels
7484  * @vsi: the VSI we want to access
7485  *
7486  * This function frees the Qs of the channel VSI from
7487  * the stack and also deletes the channel VSIs which
7488  * serve as macvlans.
7489  */
7490 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7491 {
7492 	struct i40e_channel *ch, *ch_tmp;
7493 	int ret;
7494 
7495 	if (list_empty(&vsi->macvlan_list))
7496 		return;
7497 
7498 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7499 		struct i40e_vsi *parent_vsi;
7500 
7501 		if (i40e_is_channel_macvlan(ch)) {
7502 			i40e_reset_ch_rings(vsi, ch);
7503 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7504 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7505 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7506 			kfree(ch->fwd);
7507 			ch->fwd = NULL;
7508 		}
7509 
7510 		list_del(&ch->list);
7511 		parent_vsi = ch->parent_vsi;
7512 		if (!parent_vsi || !ch->initialized) {
7513 			kfree(ch);
7514 			continue;
7515 		}
7516 
7517 		/* remove the VSI */
7518 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7519 					     NULL);
7520 		if (ret)
7521 			dev_err(&vsi->back->pdev->dev,
7522 				"unable to remove channel (%d) for parent VSI(%d)\n",
7523 				ch->seid, parent_vsi->seid);
7524 		kfree(ch);
7525 	}
7526 	vsi->macvlan_cnt = 0;
7527 }
7528 
7529 /**
7530  * i40e_fwd_ring_up - bring the macvlan device up
7531  * @vsi: the VSI we want to access
7532  * @vdev: macvlan netdevice
7533  * @fwd: the private fwd structure
7534  */
7535 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7536 			    struct i40e_fwd_adapter *fwd)
7537 {
7538 	int ret = 0, num_tc = 1,  i, aq_err;
7539 	struct i40e_channel *ch, *ch_tmp;
7540 	struct i40e_pf *pf = vsi->back;
7541 	struct i40e_hw *hw = &pf->hw;
7542 
7543 	if (list_empty(&vsi->macvlan_list))
7544 		return -EINVAL;
7545 
7546 	/* Go through the list and find an available channel */
7547 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7548 		if (!i40e_is_channel_macvlan(ch)) {
7549 			ch->fwd = fwd;
7550 			/* record configuration for macvlan interface in vdev */
7551 			for (i = 0; i < num_tc; i++)
7552 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7553 							     i,
7554 							     ch->num_queue_pairs,
7555 							     ch->base_queue);
7556 			for (i = 0; i < ch->num_queue_pairs; i++) {
7557 				struct i40e_ring *tx_ring, *rx_ring;
7558 				u16 pf_q;
7559 
7560 				pf_q = ch->base_queue + i;
7561 
7562 				/* Get to TX ring ptr */
7563 				tx_ring = vsi->tx_rings[pf_q];
7564 				tx_ring->ch = ch;
7565 
7566 				/* Get the RX ring ptr */
7567 				rx_ring = vsi->rx_rings[pf_q];
7568 				rx_ring->ch = ch;
7569 			}
7570 			break;
7571 		}
7572 	}
7573 
7574 	/* Guarantee all rings are updated before we update the
7575 	 * MAC address filter.
7576 	 */
7577 	wmb();
7578 
7579 	/* Add a mac filter */
7580 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7581 	if (ret) {
7582 		/* if we cannot add the MAC rule then disable the offload */
7583 		macvlan_release_l2fw_offload(vdev);
7584 		for (i = 0; i < ch->num_queue_pairs; i++) {
7585 			struct i40e_ring *rx_ring;
7586 			u16 pf_q;
7587 
7588 			pf_q = ch->base_queue + i;
7589 			rx_ring = vsi->rx_rings[pf_q];
7590 			rx_ring->netdev = NULL;
7591 		}
7592 		dev_info(&pf->pdev->dev,
7593 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7594 			  i40e_stat_str(hw, ret),
7595 			  i40e_aq_str(hw, aq_err));
7596 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7597 	}
7598 
7599 	return ret;
7600 }
7601 
7602 /**
7603  * i40e_setup_macvlans - create the channels which will be macvlans
7604  * @vsi: the VSI we want to access
7605  * @macvlan_cnt: no. of macvlans to be setup
7606  * @qcnt: no. of Qs per macvlan
7607  * @vdev: macvlan netdevice
7608  */
7609 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7610 			       struct net_device *vdev)
7611 {
7612 	struct i40e_pf *pf = vsi->back;
7613 	struct i40e_hw *hw = &pf->hw;
7614 	struct i40e_vsi_context ctxt;
7615 	u16 sections, qmap, num_qps;
7616 	struct i40e_channel *ch;
7617 	int i, pow, ret = 0;
7618 	u8 offset = 0;
7619 
7620 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7621 		return -EINVAL;
7622 
7623 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7624 
7625 	/* find the next higher power-of-2 of num queue pairs */
7626 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7627 
7628 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7629 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7630 
7631 	/* Setup context bits for the main VSI */
7632 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7633 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7634 	memset(&ctxt, 0, sizeof(ctxt));
7635 	ctxt.seid = vsi->seid;
7636 	ctxt.pf_num = vsi->back->hw.pf_id;
7637 	ctxt.vf_num = 0;
7638 	ctxt.uplink_seid = vsi->uplink_seid;
7639 	ctxt.info = vsi->info;
7640 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7641 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7642 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7643 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7644 
7645 	/* Reconfigure RSS for main VSI with new max queue count */
7646 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7647 	ret = i40e_vsi_config_rss(vsi);
7648 	if (ret) {
7649 		dev_info(&pf->pdev->dev,
7650 			 "Failed to reconfig RSS for num_queues (%u)\n",
7651 			 vsi->rss_size);
7652 		return ret;
7653 	}
7654 	vsi->reconfig_rss = true;
7655 	dev_dbg(&vsi->back->pdev->dev,
7656 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7657 	vsi->next_base_queue = num_qps;
7658 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7659 
7660 	/* Update the VSI after updating the VSI queue-mapping
7661 	 * information
7662 	 */
7663 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7664 	if (ret) {
7665 		dev_info(&pf->pdev->dev,
7666 			 "Update vsi tc config failed, err %s aq_err %s\n",
7667 			 i40e_stat_str(hw, ret),
7668 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7669 		return ret;
7670 	}
7671 	/* update the local VSI info with updated queue map */
7672 	i40e_vsi_update_queue_map(vsi, &ctxt);
7673 	vsi->info.valid_sections = 0;
7674 
7675 	/* Create channels for macvlans */
7676 	INIT_LIST_HEAD(&vsi->macvlan_list);
7677 	for (i = 0; i < macvlan_cnt; i++) {
7678 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7679 		if (!ch) {
7680 			ret = -ENOMEM;
7681 			goto err_free;
7682 		}
7683 		INIT_LIST_HEAD(&ch->list);
7684 		ch->num_queue_pairs = qcnt;
7685 		if (!i40e_setup_channel(pf, vsi, ch)) {
7686 			ret = -EINVAL;
7687 			kfree(ch);
7688 			goto err_free;
7689 		}
7690 		ch->parent_vsi = vsi;
7691 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7692 		vsi->macvlan_cnt++;
7693 		list_add_tail(&ch->list, &vsi->macvlan_list);
7694 	}
7695 
7696 	return ret;
7697 
7698 err_free:
7699 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7700 	i40e_free_macvlan_channels(vsi);
7701 
7702 	return ret;
7703 }
7704 
7705 /**
7706  * i40e_fwd_add - configure macvlans
7707  * @netdev: net device to configure
7708  * @vdev: macvlan netdevice
7709  **/
7710 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7711 {
7712 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7713 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7714 	struct i40e_vsi *vsi = np->vsi;
7715 	struct i40e_pf *pf = vsi->back;
7716 	struct i40e_fwd_adapter *fwd;
7717 	int avail_macvlan, ret;
7718 
7719 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7720 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7721 		return ERR_PTR(-EINVAL);
7722 	}
7723 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7724 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7725 		return ERR_PTR(-EINVAL);
7726 	}
7727 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7728 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7729 		return ERR_PTR(-EINVAL);
7730 	}
7731 
7732 	/* The macvlan device has to be a single Q device so that the
7733 	 * tc_to_txq field can be reused to pick the tx queue.
7734 	 */
7735 	if (netif_is_multiqueue(vdev))
7736 		return ERR_PTR(-ERANGE);
7737 
7738 	if (!vsi->macvlan_cnt) {
7739 		/* reserve bit 0 for the pf device */
7740 		set_bit(0, vsi->fwd_bitmask);
7741 
7742 		/* Try to reserve as many queues as possible for macvlans. First
7743 		 * reserve 3/4th of max vectors, then half, then quarter and
7744 		 * calculate Qs per macvlan as you go
7745 		 */
7746 		vectors = pf->num_lan_msix;
7747 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7748 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7749 			q_per_macvlan = 4;
7750 			macvlan_cnt = (vectors - 32) / 4;
7751 		} else if (vectors <= 64 && vectors > 32) {
7752 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7753 			q_per_macvlan = 2;
7754 			macvlan_cnt = (vectors - 16) / 2;
7755 		} else if (vectors <= 32 && vectors > 16) {
7756 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7757 			q_per_macvlan = 1;
7758 			macvlan_cnt = vectors - 16;
7759 		} else if (vectors <= 16 && vectors > 8) {
7760 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7761 			q_per_macvlan = 1;
7762 			macvlan_cnt = vectors - 8;
7763 		} else {
7764 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7765 			q_per_macvlan = 1;
7766 			macvlan_cnt = vectors - 1;
7767 		}
7768 
7769 		if (macvlan_cnt == 0)
7770 			return ERR_PTR(-EBUSY);
7771 
7772 		/* Quiesce VSI queues */
7773 		i40e_quiesce_vsi(vsi);
7774 
7775 		/* sets up the macvlans but does not "enable" them */
7776 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7777 					  vdev);
7778 		if (ret)
7779 			return ERR_PTR(ret);
7780 
7781 		/* Unquiesce VSI */
7782 		i40e_unquiesce_vsi(vsi);
7783 	}
7784 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7785 					    vsi->macvlan_cnt);
7786 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7787 		return ERR_PTR(-EBUSY);
7788 
7789 	/* create the fwd struct */
7790 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7791 	if (!fwd)
7792 		return ERR_PTR(-ENOMEM);
7793 
7794 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7795 	fwd->bit_no = avail_macvlan;
7796 	netdev_set_sb_channel(vdev, avail_macvlan);
7797 	fwd->netdev = vdev;
7798 
7799 	if (!netif_running(netdev))
7800 		return fwd;
7801 
7802 	/* Set fwd ring up */
7803 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7804 	if (ret) {
7805 		/* unbind the queues and drop the subordinate channel config */
7806 		netdev_unbind_sb_channel(netdev, vdev);
7807 		netdev_set_sb_channel(vdev, 0);
7808 
7809 		kfree(fwd);
7810 		return ERR_PTR(-EINVAL);
7811 	}
7812 
7813 	return fwd;
7814 }
7815 
7816 /**
7817  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7818  * @vsi: the VSI we want to access
7819  */
7820 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7821 {
7822 	struct i40e_channel *ch, *ch_tmp;
7823 	struct i40e_pf *pf = vsi->back;
7824 	struct i40e_hw *hw = &pf->hw;
7825 	int aq_err, ret = 0;
7826 
7827 	if (list_empty(&vsi->macvlan_list))
7828 		return;
7829 
7830 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7831 		if (i40e_is_channel_macvlan(ch)) {
7832 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7833 						      i40e_channel_mac(ch),
7834 						      &aq_err);
7835 			if (!ret) {
7836 				/* Reset queue contexts */
7837 				i40e_reset_ch_rings(vsi, ch);
7838 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7839 				netdev_unbind_sb_channel(vsi->netdev,
7840 							 ch->fwd->netdev);
7841 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7842 				kfree(ch->fwd);
7843 				ch->fwd = NULL;
7844 			}
7845 		}
7846 	}
7847 }
7848 
7849 /**
7850  * i40e_fwd_del - delete macvlan interfaces
7851  * @netdev: net device to configure
7852  * @vdev: macvlan netdevice
7853  */
7854 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7855 {
7856 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7857 	struct i40e_fwd_adapter *fwd = vdev;
7858 	struct i40e_channel *ch, *ch_tmp;
7859 	struct i40e_vsi *vsi = np->vsi;
7860 	struct i40e_pf *pf = vsi->back;
7861 	struct i40e_hw *hw = &pf->hw;
7862 	int aq_err, ret = 0;
7863 
7864 	/* Find the channel associated with the macvlan and del mac filter */
7865 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7866 		if (i40e_is_channel_macvlan(ch) &&
7867 		    ether_addr_equal(i40e_channel_mac(ch),
7868 				     fwd->netdev->dev_addr)) {
7869 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7870 						      i40e_channel_mac(ch),
7871 						      &aq_err);
7872 			if (!ret) {
7873 				/* Reset queue contexts */
7874 				i40e_reset_ch_rings(vsi, ch);
7875 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7876 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7877 				netdev_set_sb_channel(fwd->netdev, 0);
7878 				kfree(ch->fwd);
7879 				ch->fwd = NULL;
7880 			} else {
7881 				dev_info(&pf->pdev->dev,
7882 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7883 					  i40e_stat_str(hw, ret),
7884 					  i40e_aq_str(hw, aq_err));
7885 			}
7886 			break;
7887 		}
7888 	}
7889 }
7890 
7891 /**
7892  * i40e_setup_tc - configure multiple traffic classes
7893  * @netdev: net device to configure
7894  * @type_data: tc offload data
7895  **/
7896 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7897 {
7898 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7899 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7900 	struct i40e_vsi *vsi = np->vsi;
7901 	struct i40e_pf *pf = vsi->back;
7902 	u8 enabled_tc = 0, num_tc, hw;
7903 	bool need_reset = false;
7904 	int old_queue_pairs;
7905 	int ret = -EINVAL;
7906 	u16 mode;
7907 	int i;
7908 
7909 	old_queue_pairs = vsi->num_queue_pairs;
7910 	num_tc = mqprio_qopt->qopt.num_tc;
7911 	hw = mqprio_qopt->qopt.hw;
7912 	mode = mqprio_qopt->mode;
7913 	if (!hw) {
7914 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7915 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7916 		goto config_tc;
7917 	}
7918 
7919 	/* Check if MFP enabled */
7920 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7921 		netdev_info(netdev,
7922 			    "Configuring TC not supported in MFP mode\n");
7923 		return ret;
7924 	}
7925 	switch (mode) {
7926 	case TC_MQPRIO_MODE_DCB:
7927 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7928 
7929 		/* Check if DCB enabled to continue */
7930 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7931 			netdev_info(netdev,
7932 				    "DCB is not enabled for adapter\n");
7933 			return ret;
7934 		}
7935 
7936 		/* Check whether tc count is within enabled limit */
7937 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7938 			netdev_info(netdev,
7939 				    "TC count greater than enabled on link for adapter\n");
7940 			return ret;
7941 		}
7942 		break;
7943 	case TC_MQPRIO_MODE_CHANNEL:
7944 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7945 			netdev_info(netdev,
7946 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7947 			return ret;
7948 		}
7949 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7950 			return ret;
7951 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7952 		if (ret)
7953 			return ret;
7954 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7955 		       sizeof(*mqprio_qopt));
7956 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7957 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7958 		break;
7959 	default:
7960 		return -EINVAL;
7961 	}
7962 
7963 config_tc:
7964 	/* Generate TC map for number of tc requested */
7965 	for (i = 0; i < num_tc; i++)
7966 		enabled_tc |= BIT(i);
7967 
7968 	/* Requesting same TC configuration as already enabled */
7969 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7970 	    mode != TC_MQPRIO_MODE_CHANNEL)
7971 		return 0;
7972 
7973 	/* Quiesce VSI queues */
7974 	i40e_quiesce_vsi(vsi);
7975 
7976 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7977 		i40e_remove_queue_channels(vsi);
7978 
7979 	/* Configure VSI for enabled TCs */
7980 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7981 	if (ret) {
7982 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7983 			    vsi->seid);
7984 		need_reset = true;
7985 		goto exit;
7986 	} else if (enabled_tc &&
7987 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
7988 		netdev_info(netdev,
7989 			    "Failed to create channel. Override queues (%u) not power of 2\n",
7990 			    vsi->tc_config.tc_info[0].qcount);
7991 		ret = -EINVAL;
7992 		need_reset = true;
7993 		goto exit;
7994 	}
7995 
7996 	dev_info(&vsi->back->pdev->dev,
7997 		 "Setup channel (id:%u) utilizing num_queues %d\n",
7998 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7999 
8000 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
8001 		if (vsi->mqprio_qopt.max_rate[0]) {
8002 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8003 
8004 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8005 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8006 			if (!ret) {
8007 				u64 credits = max_tx_rate;
8008 
8009 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8010 				dev_dbg(&vsi->back->pdev->dev,
8011 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8012 					max_tx_rate,
8013 					credits,
8014 					vsi->seid);
8015 			} else {
8016 				need_reset = true;
8017 				goto exit;
8018 			}
8019 		}
8020 		ret = i40e_configure_queue_channels(vsi);
8021 		if (ret) {
8022 			vsi->num_queue_pairs = old_queue_pairs;
8023 			netdev_info(netdev,
8024 				    "Failed configuring queue channels\n");
8025 			need_reset = true;
8026 			goto exit;
8027 		}
8028 	}
8029 
8030 exit:
8031 	/* Reset the configuration data to defaults, only TC0 is enabled */
8032 	if (need_reset) {
8033 		i40e_vsi_set_default_tc_config(vsi);
8034 		need_reset = false;
8035 	}
8036 
8037 	/* Unquiesce VSI */
8038 	i40e_unquiesce_vsi(vsi);
8039 	return ret;
8040 }
8041 
8042 /**
8043  * i40e_set_cld_element - sets cloud filter element data
8044  * @filter: cloud filter rule
8045  * @cld: ptr to cloud filter element data
8046  *
8047  * This is helper function to copy data into cloud filter element
8048  **/
8049 static inline void
8050 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8051 		     struct i40e_aqc_cloud_filters_element_data *cld)
8052 {
8053 	u32 ipa;
8054 	int i;
8055 
8056 	memset(cld, 0, sizeof(*cld));
8057 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8058 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8059 
8060 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8061 		return;
8062 
8063 	if (filter->n_proto == ETH_P_IPV6) {
8064 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8065 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8066 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8067 
8068 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8069 		}
8070 	} else {
8071 		ipa = be32_to_cpu(filter->dst_ipv4);
8072 
8073 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8074 	}
8075 
8076 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8077 
8078 	/* tenant_id is not supported by FW now, once the support is enabled
8079 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8080 	 */
8081 	if (filter->tenant_id)
8082 		return;
8083 }
8084 
8085 /**
8086  * i40e_add_del_cloud_filter - Add/del cloud filter
8087  * @vsi: pointer to VSI
8088  * @filter: cloud filter rule
8089  * @add: if true, add, if false, delete
8090  *
8091  * Add or delete a cloud filter for a specific flow spec.
8092  * Returns 0 if the filter were successfully added.
8093  **/
8094 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8095 			      struct i40e_cloud_filter *filter, bool add)
8096 {
8097 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8098 	struct i40e_pf *pf = vsi->back;
8099 	int ret;
8100 	static const u16 flag_table[128] = {
8101 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8102 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8103 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8104 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8105 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8106 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8107 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8108 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8109 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8110 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8111 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8112 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8113 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8114 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8115 	};
8116 
8117 	if (filter->flags >= ARRAY_SIZE(flag_table))
8118 		return I40E_ERR_CONFIG;
8119 
8120 	memset(&cld_filter, 0, sizeof(cld_filter));
8121 
8122 	/* copy element needed to add cloud filter from filter */
8123 	i40e_set_cld_element(filter, &cld_filter);
8124 
8125 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8126 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8127 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8128 
8129 	if (filter->n_proto == ETH_P_IPV6)
8130 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8131 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8132 	else
8133 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8134 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8135 
8136 	if (add)
8137 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8138 						&cld_filter, 1);
8139 	else
8140 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8141 						&cld_filter, 1);
8142 	if (ret)
8143 		dev_dbg(&pf->pdev->dev,
8144 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8145 			add ? "add" : "delete", filter->dst_port, ret,
8146 			pf->hw.aq.asq_last_status);
8147 	else
8148 		dev_info(&pf->pdev->dev,
8149 			 "%s cloud filter for VSI: %d\n",
8150 			 add ? "Added" : "Deleted", filter->seid);
8151 	return ret;
8152 }
8153 
8154 /**
8155  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8156  * @vsi: pointer to VSI
8157  * @filter: cloud filter rule
8158  * @add: if true, add, if false, delete
8159  *
8160  * Add or delete a cloud filter for a specific flow spec using big buffer.
8161  * Returns 0 if the filter were successfully added.
8162  **/
8163 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8164 				      struct i40e_cloud_filter *filter,
8165 				      bool add)
8166 {
8167 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8168 	struct i40e_pf *pf = vsi->back;
8169 	int ret;
8170 
8171 	/* Both (src/dst) valid mac_addr are not supported */
8172 	if ((is_valid_ether_addr(filter->dst_mac) &&
8173 	     is_valid_ether_addr(filter->src_mac)) ||
8174 	    (is_multicast_ether_addr(filter->dst_mac) &&
8175 	     is_multicast_ether_addr(filter->src_mac)))
8176 		return -EOPNOTSUPP;
8177 
8178 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8179 	 * ports are not supported via big buffer now.
8180 	 */
8181 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8182 		return -EOPNOTSUPP;
8183 
8184 	/* adding filter using src_port/src_ip is not supported at this stage */
8185 	if (filter->src_port ||
8186 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8187 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8188 		return -EOPNOTSUPP;
8189 
8190 	memset(&cld_filter, 0, sizeof(cld_filter));
8191 
8192 	/* copy element needed to add cloud filter from filter */
8193 	i40e_set_cld_element(filter, &cld_filter.element);
8194 
8195 	if (is_valid_ether_addr(filter->dst_mac) ||
8196 	    is_valid_ether_addr(filter->src_mac) ||
8197 	    is_multicast_ether_addr(filter->dst_mac) ||
8198 	    is_multicast_ether_addr(filter->src_mac)) {
8199 		/* MAC + IP : unsupported mode */
8200 		if (filter->dst_ipv4)
8201 			return -EOPNOTSUPP;
8202 
8203 		/* since we validated that L4 port must be valid before
8204 		 * we get here, start with respective "flags" value
8205 		 * and update if vlan is present or not
8206 		 */
8207 		cld_filter.element.flags =
8208 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8209 
8210 		if (filter->vlan_id) {
8211 			cld_filter.element.flags =
8212 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8213 		}
8214 
8215 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8216 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8217 		cld_filter.element.flags =
8218 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8219 		if (filter->n_proto == ETH_P_IPV6)
8220 			cld_filter.element.flags |=
8221 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8222 		else
8223 			cld_filter.element.flags |=
8224 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8225 	} else {
8226 		dev_err(&pf->pdev->dev,
8227 			"either mac or ip has to be valid for cloud filter\n");
8228 		return -EINVAL;
8229 	}
8230 
8231 	/* Now copy L4 port in Byte 6..7 in general fields */
8232 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8233 						be16_to_cpu(filter->dst_port);
8234 
8235 	if (add) {
8236 		/* Validate current device switch mode, change if necessary */
8237 		ret = i40e_validate_and_set_switch_mode(vsi);
8238 		if (ret) {
8239 			dev_err(&pf->pdev->dev,
8240 				"failed to set switch mode, ret %d\n",
8241 				ret);
8242 			return ret;
8243 		}
8244 
8245 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8246 						   &cld_filter, 1);
8247 	} else {
8248 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8249 						   &cld_filter, 1);
8250 	}
8251 
8252 	if (ret)
8253 		dev_dbg(&pf->pdev->dev,
8254 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8255 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8256 	else
8257 		dev_info(&pf->pdev->dev,
8258 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8259 			 add ? "add" : "delete", filter->seid,
8260 			 ntohs(filter->dst_port));
8261 	return ret;
8262 }
8263 
8264 /**
8265  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8266  * @vsi: Pointer to VSI
8267  * @f: Pointer to struct flow_cls_offload
8268  * @filter: Pointer to cloud filter structure
8269  *
8270  **/
8271 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8272 				 struct flow_cls_offload *f,
8273 				 struct i40e_cloud_filter *filter)
8274 {
8275 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8276 	struct flow_dissector *dissector = rule->match.dissector;
8277 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8278 	struct i40e_pf *pf = vsi->back;
8279 	u8 field_flags = 0;
8280 
8281 	if (dissector->used_keys &
8282 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8283 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8284 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8285 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8286 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8287 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8288 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8289 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8290 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8291 			dissector->used_keys);
8292 		return -EOPNOTSUPP;
8293 	}
8294 
8295 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8296 		struct flow_match_enc_keyid match;
8297 
8298 		flow_rule_match_enc_keyid(rule, &match);
8299 		if (match.mask->keyid != 0)
8300 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8301 
8302 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8303 	}
8304 
8305 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8306 		struct flow_match_basic match;
8307 
8308 		flow_rule_match_basic(rule, &match);
8309 		n_proto_key = ntohs(match.key->n_proto);
8310 		n_proto_mask = ntohs(match.mask->n_proto);
8311 
8312 		if (n_proto_key == ETH_P_ALL) {
8313 			n_proto_key = 0;
8314 			n_proto_mask = 0;
8315 		}
8316 		filter->n_proto = n_proto_key & n_proto_mask;
8317 		filter->ip_proto = match.key->ip_proto;
8318 	}
8319 
8320 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8321 		struct flow_match_eth_addrs match;
8322 
8323 		flow_rule_match_eth_addrs(rule, &match);
8324 
8325 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8326 		if (!is_zero_ether_addr(match.mask->dst)) {
8327 			if (is_broadcast_ether_addr(match.mask->dst)) {
8328 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8329 			} else {
8330 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8331 					match.mask->dst);
8332 				return I40E_ERR_CONFIG;
8333 			}
8334 		}
8335 
8336 		if (!is_zero_ether_addr(match.mask->src)) {
8337 			if (is_broadcast_ether_addr(match.mask->src)) {
8338 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8339 			} else {
8340 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8341 					match.mask->src);
8342 				return I40E_ERR_CONFIG;
8343 			}
8344 		}
8345 		ether_addr_copy(filter->dst_mac, match.key->dst);
8346 		ether_addr_copy(filter->src_mac, match.key->src);
8347 	}
8348 
8349 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8350 		struct flow_match_vlan match;
8351 
8352 		flow_rule_match_vlan(rule, &match);
8353 		if (match.mask->vlan_id) {
8354 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8355 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8356 
8357 			} else {
8358 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8359 					match.mask->vlan_id);
8360 				return I40E_ERR_CONFIG;
8361 			}
8362 		}
8363 
8364 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8365 	}
8366 
8367 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8368 		struct flow_match_control match;
8369 
8370 		flow_rule_match_control(rule, &match);
8371 		addr_type = match.key->addr_type;
8372 	}
8373 
8374 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8375 		struct flow_match_ipv4_addrs match;
8376 
8377 		flow_rule_match_ipv4_addrs(rule, &match);
8378 		if (match.mask->dst) {
8379 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8380 				field_flags |= I40E_CLOUD_FIELD_IIP;
8381 			} else {
8382 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8383 					&match.mask->dst);
8384 				return I40E_ERR_CONFIG;
8385 			}
8386 		}
8387 
8388 		if (match.mask->src) {
8389 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8390 				field_flags |= I40E_CLOUD_FIELD_IIP;
8391 			} else {
8392 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8393 					&match.mask->src);
8394 				return I40E_ERR_CONFIG;
8395 			}
8396 		}
8397 
8398 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8399 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8400 			return I40E_ERR_CONFIG;
8401 		}
8402 		filter->dst_ipv4 = match.key->dst;
8403 		filter->src_ipv4 = match.key->src;
8404 	}
8405 
8406 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8407 		struct flow_match_ipv6_addrs match;
8408 
8409 		flow_rule_match_ipv6_addrs(rule, &match);
8410 
8411 		/* src and dest IPV6 address should not be LOOPBACK
8412 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8413 		 */
8414 		if (ipv6_addr_loopback(&match.key->dst) ||
8415 		    ipv6_addr_loopback(&match.key->src)) {
8416 			dev_err(&pf->pdev->dev,
8417 				"Bad ipv6, addr is LOOPBACK\n");
8418 			return I40E_ERR_CONFIG;
8419 		}
8420 		if (!ipv6_addr_any(&match.mask->dst) ||
8421 		    !ipv6_addr_any(&match.mask->src))
8422 			field_flags |= I40E_CLOUD_FIELD_IIP;
8423 
8424 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8425 		       sizeof(filter->src_ipv6));
8426 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8427 		       sizeof(filter->dst_ipv6));
8428 	}
8429 
8430 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8431 		struct flow_match_ports match;
8432 
8433 		flow_rule_match_ports(rule, &match);
8434 		if (match.mask->src) {
8435 			if (match.mask->src == cpu_to_be16(0xffff)) {
8436 				field_flags |= I40E_CLOUD_FIELD_IIP;
8437 			} else {
8438 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8439 					be16_to_cpu(match.mask->src));
8440 				return I40E_ERR_CONFIG;
8441 			}
8442 		}
8443 
8444 		if (match.mask->dst) {
8445 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8446 				field_flags |= I40E_CLOUD_FIELD_IIP;
8447 			} else {
8448 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8449 					be16_to_cpu(match.mask->dst));
8450 				return I40E_ERR_CONFIG;
8451 			}
8452 		}
8453 
8454 		filter->dst_port = match.key->dst;
8455 		filter->src_port = match.key->src;
8456 
8457 		switch (filter->ip_proto) {
8458 		case IPPROTO_TCP:
8459 		case IPPROTO_UDP:
8460 			break;
8461 		default:
8462 			dev_err(&pf->pdev->dev,
8463 				"Only UDP and TCP transport are supported\n");
8464 			return -EINVAL;
8465 		}
8466 	}
8467 	filter->flags = field_flags;
8468 	return 0;
8469 }
8470 
8471 /**
8472  * i40e_handle_tclass: Forward to a traffic class on the device
8473  * @vsi: Pointer to VSI
8474  * @tc: traffic class index on the device
8475  * @filter: Pointer to cloud filter structure
8476  *
8477  **/
8478 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8479 			      struct i40e_cloud_filter *filter)
8480 {
8481 	struct i40e_channel *ch, *ch_tmp;
8482 
8483 	/* direct to a traffic class on the same device */
8484 	if (tc == 0) {
8485 		filter->seid = vsi->seid;
8486 		return 0;
8487 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8488 		if (!filter->dst_port) {
8489 			dev_err(&vsi->back->pdev->dev,
8490 				"Specify destination port to direct to traffic class that is not default\n");
8491 			return -EINVAL;
8492 		}
8493 		if (list_empty(&vsi->ch_list))
8494 			return -EINVAL;
8495 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8496 					 list) {
8497 			if (ch->seid == vsi->tc_seid_map[tc])
8498 				filter->seid = ch->seid;
8499 		}
8500 		return 0;
8501 	}
8502 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8503 	return -EINVAL;
8504 }
8505 
8506 /**
8507  * i40e_configure_clsflower - Configure tc flower filters
8508  * @vsi: Pointer to VSI
8509  * @cls_flower: Pointer to struct flow_cls_offload
8510  *
8511  **/
8512 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8513 				    struct flow_cls_offload *cls_flower)
8514 {
8515 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8516 	struct i40e_cloud_filter *filter = NULL;
8517 	struct i40e_pf *pf = vsi->back;
8518 	int err = 0;
8519 
8520 	if (tc < 0) {
8521 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8522 		return -EOPNOTSUPP;
8523 	}
8524 
8525 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8526 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8527 		return -EBUSY;
8528 
8529 	if (pf->fdir_pf_active_filters ||
8530 	    (!hlist_empty(&pf->fdir_filter_list))) {
8531 		dev_err(&vsi->back->pdev->dev,
8532 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8533 		return -EINVAL;
8534 	}
8535 
8536 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8537 		dev_err(&vsi->back->pdev->dev,
8538 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8539 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8540 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8541 	}
8542 
8543 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8544 	if (!filter)
8545 		return -ENOMEM;
8546 
8547 	filter->cookie = cls_flower->cookie;
8548 
8549 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8550 	if (err < 0)
8551 		goto err;
8552 
8553 	err = i40e_handle_tclass(vsi, tc, filter);
8554 	if (err < 0)
8555 		goto err;
8556 
8557 	/* Add cloud filter */
8558 	if (filter->dst_port)
8559 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8560 	else
8561 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8562 
8563 	if (err) {
8564 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8565 			err);
8566 		goto err;
8567 	}
8568 
8569 	/* add filter to the ordered list */
8570 	INIT_HLIST_NODE(&filter->cloud_node);
8571 
8572 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8573 
8574 	pf->num_cloud_filters++;
8575 
8576 	return err;
8577 err:
8578 	kfree(filter);
8579 	return err;
8580 }
8581 
8582 /**
8583  * i40e_find_cloud_filter - Find the could filter in the list
8584  * @vsi: Pointer to VSI
8585  * @cookie: filter specific cookie
8586  *
8587  **/
8588 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8589 							unsigned long *cookie)
8590 {
8591 	struct i40e_cloud_filter *filter = NULL;
8592 	struct hlist_node *node2;
8593 
8594 	hlist_for_each_entry_safe(filter, node2,
8595 				  &vsi->back->cloud_filter_list, cloud_node)
8596 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8597 			return filter;
8598 	return NULL;
8599 }
8600 
8601 /**
8602  * i40e_delete_clsflower - Remove tc flower filters
8603  * @vsi: Pointer to VSI
8604  * @cls_flower: Pointer to struct flow_cls_offload
8605  *
8606  **/
8607 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8608 				 struct flow_cls_offload *cls_flower)
8609 {
8610 	struct i40e_cloud_filter *filter = NULL;
8611 	struct i40e_pf *pf = vsi->back;
8612 	int err = 0;
8613 
8614 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8615 
8616 	if (!filter)
8617 		return -EINVAL;
8618 
8619 	hash_del(&filter->cloud_node);
8620 
8621 	if (filter->dst_port)
8622 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8623 	else
8624 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8625 
8626 	kfree(filter);
8627 	if (err) {
8628 		dev_err(&pf->pdev->dev,
8629 			"Failed to delete cloud filter, err %s\n",
8630 			i40e_stat_str(&pf->hw, err));
8631 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8632 	}
8633 
8634 	pf->num_cloud_filters--;
8635 	if (!pf->num_cloud_filters)
8636 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8637 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8638 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8639 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8640 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8641 		}
8642 	return 0;
8643 }
8644 
8645 /**
8646  * i40e_setup_tc_cls_flower - flower classifier offloads
8647  * @np: net device to configure
8648  * @cls_flower: offload data
8649  **/
8650 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8651 				    struct flow_cls_offload *cls_flower)
8652 {
8653 	struct i40e_vsi *vsi = np->vsi;
8654 
8655 	switch (cls_flower->command) {
8656 	case FLOW_CLS_REPLACE:
8657 		return i40e_configure_clsflower(vsi, cls_flower);
8658 	case FLOW_CLS_DESTROY:
8659 		return i40e_delete_clsflower(vsi, cls_flower);
8660 	case FLOW_CLS_STATS:
8661 		return -EOPNOTSUPP;
8662 	default:
8663 		return -EOPNOTSUPP;
8664 	}
8665 }
8666 
8667 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8668 				  void *cb_priv)
8669 {
8670 	struct i40e_netdev_priv *np = cb_priv;
8671 
8672 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8673 		return -EOPNOTSUPP;
8674 
8675 	switch (type) {
8676 	case TC_SETUP_CLSFLOWER:
8677 		return i40e_setup_tc_cls_flower(np, type_data);
8678 
8679 	default:
8680 		return -EOPNOTSUPP;
8681 	}
8682 }
8683 
8684 static LIST_HEAD(i40e_block_cb_list);
8685 
8686 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8687 			   void *type_data)
8688 {
8689 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8690 
8691 	switch (type) {
8692 	case TC_SETUP_QDISC_MQPRIO:
8693 		return i40e_setup_tc(netdev, type_data);
8694 	case TC_SETUP_BLOCK:
8695 		return flow_block_cb_setup_simple(type_data,
8696 						  &i40e_block_cb_list,
8697 						  i40e_setup_tc_block_cb,
8698 						  np, np, true);
8699 	default:
8700 		return -EOPNOTSUPP;
8701 	}
8702 }
8703 
8704 /**
8705  * i40e_open - Called when a network interface is made active
8706  * @netdev: network interface device structure
8707  *
8708  * The open entry point is called when a network interface is made
8709  * active by the system (IFF_UP).  At this point all resources needed
8710  * for transmit and receive operations are allocated, the interrupt
8711  * handler is registered with the OS, the netdev watchdog subtask is
8712  * enabled, and the stack is notified that the interface is ready.
8713  *
8714  * Returns 0 on success, negative value on failure
8715  **/
8716 int i40e_open(struct net_device *netdev)
8717 {
8718 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8719 	struct i40e_vsi *vsi = np->vsi;
8720 	struct i40e_pf *pf = vsi->back;
8721 	int err;
8722 
8723 	/* disallow open during test or if eeprom is broken */
8724 	if (test_bit(__I40E_TESTING, pf->state) ||
8725 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8726 		return -EBUSY;
8727 
8728 	netif_carrier_off(netdev);
8729 
8730 	if (i40e_force_link_state(pf, true))
8731 		return -EAGAIN;
8732 
8733 	err = i40e_vsi_open(vsi);
8734 	if (err)
8735 		return err;
8736 
8737 	/* configure global TSO hardware offload settings */
8738 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8739 						       TCP_FLAG_FIN) >> 16);
8740 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8741 						       TCP_FLAG_FIN |
8742 						       TCP_FLAG_CWR) >> 16);
8743 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8744 	udp_tunnel_get_rx_info(netdev);
8745 
8746 	return 0;
8747 }
8748 
8749 /**
8750  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8751  * @vsi: vsi structure
8752  *
8753  * This updates netdev's number of tx/rx queues
8754  *
8755  * Returns status of setting tx/rx queues
8756  **/
8757 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8758 {
8759 	int ret;
8760 
8761 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8762 					   vsi->num_queue_pairs);
8763 	if (ret)
8764 		return ret;
8765 
8766 	return netif_set_real_num_tx_queues(vsi->netdev,
8767 					    vsi->num_queue_pairs);
8768 }
8769 
8770 /**
8771  * i40e_vsi_open -
8772  * @vsi: the VSI to open
8773  *
8774  * Finish initialization of the VSI.
8775  *
8776  * Returns 0 on success, negative value on failure
8777  *
8778  * Note: expects to be called while under rtnl_lock()
8779  **/
8780 int i40e_vsi_open(struct i40e_vsi *vsi)
8781 {
8782 	struct i40e_pf *pf = vsi->back;
8783 	char int_name[I40E_INT_NAME_STR_LEN];
8784 	int err;
8785 
8786 	/* allocate descriptors */
8787 	err = i40e_vsi_setup_tx_resources(vsi);
8788 	if (err)
8789 		goto err_setup_tx;
8790 	err = i40e_vsi_setup_rx_resources(vsi);
8791 	if (err)
8792 		goto err_setup_rx;
8793 
8794 	err = i40e_vsi_configure(vsi);
8795 	if (err)
8796 		goto err_setup_rx;
8797 
8798 	if (vsi->netdev) {
8799 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8800 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8801 		err = i40e_vsi_request_irq(vsi, int_name);
8802 		if (err)
8803 			goto err_setup_rx;
8804 
8805 		/* Notify the stack of the actual queue counts. */
8806 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8807 		if (err)
8808 			goto err_set_queues;
8809 
8810 	} else if (vsi->type == I40E_VSI_FDIR) {
8811 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8812 			 dev_driver_string(&pf->pdev->dev),
8813 			 dev_name(&pf->pdev->dev));
8814 		err = i40e_vsi_request_irq(vsi, int_name);
8815 		if (err)
8816 			goto err_setup_rx;
8817 
8818 	} else {
8819 		err = -EINVAL;
8820 		goto err_setup_rx;
8821 	}
8822 
8823 	err = i40e_up_complete(vsi);
8824 	if (err)
8825 		goto err_up_complete;
8826 
8827 	return 0;
8828 
8829 err_up_complete:
8830 	i40e_down(vsi);
8831 err_set_queues:
8832 	i40e_vsi_free_irq(vsi);
8833 err_setup_rx:
8834 	i40e_vsi_free_rx_resources(vsi);
8835 err_setup_tx:
8836 	i40e_vsi_free_tx_resources(vsi);
8837 	if (vsi == pf->vsi[pf->lan_vsi])
8838 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8839 
8840 	return err;
8841 }
8842 
8843 /**
8844  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8845  * @pf: Pointer to PF
8846  *
8847  * This function destroys the hlist where all the Flow Director
8848  * filters were saved.
8849  **/
8850 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8851 {
8852 	struct i40e_fdir_filter *filter;
8853 	struct i40e_flex_pit *pit_entry, *tmp;
8854 	struct hlist_node *node2;
8855 
8856 	hlist_for_each_entry_safe(filter, node2,
8857 				  &pf->fdir_filter_list, fdir_node) {
8858 		hlist_del(&filter->fdir_node);
8859 		kfree(filter);
8860 	}
8861 
8862 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8863 		list_del(&pit_entry->list);
8864 		kfree(pit_entry);
8865 	}
8866 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8867 
8868 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8869 		list_del(&pit_entry->list);
8870 		kfree(pit_entry);
8871 	}
8872 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8873 
8874 	pf->fdir_pf_active_filters = 0;
8875 	i40e_reset_fdir_filter_cnt(pf);
8876 
8877 	/* Reprogram the default input set for TCP/IPv4 */
8878 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8879 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8880 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8881 
8882 	/* Reprogram the default input set for TCP/IPv6 */
8883 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
8884 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8885 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8886 
8887 	/* Reprogram the default input set for UDP/IPv4 */
8888 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8889 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8890 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8891 
8892 	/* Reprogram the default input set for UDP/IPv6 */
8893 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
8894 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8895 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8896 
8897 	/* Reprogram the default input set for SCTP/IPv4 */
8898 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8899 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8900 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8901 
8902 	/* Reprogram the default input set for SCTP/IPv6 */
8903 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
8904 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
8905 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8906 
8907 	/* Reprogram the default input set for Other/IPv4 */
8908 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8909 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8910 
8911 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8912 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8913 
8914 	/* Reprogram the default input set for Other/IPv6 */
8915 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
8916 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8917 
8918 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
8919 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8920 }
8921 
8922 /**
8923  * i40e_cloud_filter_exit - Cleans up the cloud filters
8924  * @pf: Pointer to PF
8925  *
8926  * This function destroys the hlist where all the cloud filters
8927  * were saved.
8928  **/
8929 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8930 {
8931 	struct i40e_cloud_filter *cfilter;
8932 	struct hlist_node *node;
8933 
8934 	hlist_for_each_entry_safe(cfilter, node,
8935 				  &pf->cloud_filter_list, cloud_node) {
8936 		hlist_del(&cfilter->cloud_node);
8937 		kfree(cfilter);
8938 	}
8939 	pf->num_cloud_filters = 0;
8940 
8941 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8942 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8943 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8944 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8945 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8946 	}
8947 }
8948 
8949 /**
8950  * i40e_close - Disables a network interface
8951  * @netdev: network interface device structure
8952  *
8953  * The close entry point is called when an interface is de-activated
8954  * by the OS.  The hardware is still under the driver's control, but
8955  * this netdev interface is disabled.
8956  *
8957  * Returns 0, this is not allowed to fail
8958  **/
8959 int i40e_close(struct net_device *netdev)
8960 {
8961 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8962 	struct i40e_vsi *vsi = np->vsi;
8963 
8964 	i40e_vsi_close(vsi);
8965 
8966 	return 0;
8967 }
8968 
8969 /**
8970  * i40e_do_reset - Start a PF or Core Reset sequence
8971  * @pf: board private structure
8972  * @reset_flags: which reset is requested
8973  * @lock_acquired: indicates whether or not the lock has been acquired
8974  * before this function was called.
8975  *
8976  * The essential difference in resets is that the PF Reset
8977  * doesn't clear the packet buffers, doesn't reset the PE
8978  * firmware, and doesn't bother the other PFs on the chip.
8979  **/
8980 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8981 {
8982 	u32 val;
8983 
8984 	/* do the biggest reset indicated */
8985 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8986 
8987 		/* Request a Global Reset
8988 		 *
8989 		 * This will start the chip's countdown to the actual full
8990 		 * chip reset event, and a warning interrupt to be sent
8991 		 * to all PFs, including the requestor.  Our handler
8992 		 * for the warning interrupt will deal with the shutdown
8993 		 * and recovery of the switch setup.
8994 		 */
8995 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8996 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8997 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8998 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8999 
9000 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9001 
9002 		/* Request a Core Reset
9003 		 *
9004 		 * Same as Global Reset, except does *not* include the MAC/PHY
9005 		 */
9006 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9007 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9008 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9009 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9010 		i40e_flush(&pf->hw);
9011 
9012 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9013 
9014 		/* Request a PF Reset
9015 		 *
9016 		 * Resets only the PF-specific registers
9017 		 *
9018 		 * This goes directly to the tear-down and rebuild of
9019 		 * the switch, since we need to do all the recovery as
9020 		 * for the Core Reset.
9021 		 */
9022 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9023 		i40e_handle_reset_warning(pf, lock_acquired);
9024 
9025 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9026 		/* Request a PF Reset
9027 		 *
9028 		 * Resets PF and reinitializes PFs VSI.
9029 		 */
9030 		i40e_prep_for_reset(pf);
9031 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9032 		dev_info(&pf->pdev->dev,
9033 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9034 			 "FW LLDP is disabled\n" :
9035 			 "FW LLDP is enabled\n");
9036 
9037 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9038 		int v;
9039 
9040 		/* Find the VSI(s) that requested a re-init */
9041 		dev_info(&pf->pdev->dev,
9042 			 "VSI reinit requested\n");
9043 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9044 			struct i40e_vsi *vsi = pf->vsi[v];
9045 
9046 			if (vsi != NULL &&
9047 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9048 					       vsi->state))
9049 				i40e_vsi_reinit_locked(pf->vsi[v]);
9050 		}
9051 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9052 		int v;
9053 
9054 		/* Find the VSI(s) that needs to be brought down */
9055 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9056 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9057 			struct i40e_vsi *vsi = pf->vsi[v];
9058 
9059 			if (vsi != NULL &&
9060 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9061 					       vsi->state)) {
9062 				set_bit(__I40E_VSI_DOWN, vsi->state);
9063 				i40e_down(vsi);
9064 			}
9065 		}
9066 	} else {
9067 		dev_info(&pf->pdev->dev,
9068 			 "bad reset request 0x%08x\n", reset_flags);
9069 	}
9070 }
9071 
9072 #ifdef CONFIG_I40E_DCB
9073 /**
9074  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9075  * @pf: board private structure
9076  * @old_cfg: current DCB config
9077  * @new_cfg: new DCB config
9078  **/
9079 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9080 			    struct i40e_dcbx_config *old_cfg,
9081 			    struct i40e_dcbx_config *new_cfg)
9082 {
9083 	bool need_reconfig = false;
9084 
9085 	/* Check if ETS configuration has changed */
9086 	if (memcmp(&new_cfg->etscfg,
9087 		   &old_cfg->etscfg,
9088 		   sizeof(new_cfg->etscfg))) {
9089 		/* If Priority Table has changed reconfig is needed */
9090 		if (memcmp(&new_cfg->etscfg.prioritytable,
9091 			   &old_cfg->etscfg.prioritytable,
9092 			   sizeof(new_cfg->etscfg.prioritytable))) {
9093 			need_reconfig = true;
9094 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9095 		}
9096 
9097 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9098 			   &old_cfg->etscfg.tcbwtable,
9099 			   sizeof(new_cfg->etscfg.tcbwtable)))
9100 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9101 
9102 		if (memcmp(&new_cfg->etscfg.tsatable,
9103 			   &old_cfg->etscfg.tsatable,
9104 			   sizeof(new_cfg->etscfg.tsatable)))
9105 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9106 	}
9107 
9108 	/* Check if PFC configuration has changed */
9109 	if (memcmp(&new_cfg->pfc,
9110 		   &old_cfg->pfc,
9111 		   sizeof(new_cfg->pfc))) {
9112 		need_reconfig = true;
9113 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9114 	}
9115 
9116 	/* Check if APP Table has changed */
9117 	if (memcmp(&new_cfg->app,
9118 		   &old_cfg->app,
9119 		   sizeof(new_cfg->app))) {
9120 		need_reconfig = true;
9121 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9122 	}
9123 
9124 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9125 	return need_reconfig;
9126 }
9127 
9128 /**
9129  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9130  * @pf: board private structure
9131  * @e: event info posted on ARQ
9132  **/
9133 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9134 				  struct i40e_arq_event_info *e)
9135 {
9136 	struct i40e_aqc_lldp_get_mib *mib =
9137 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9138 	struct i40e_hw *hw = &pf->hw;
9139 	struct i40e_dcbx_config tmp_dcbx_cfg;
9140 	bool need_reconfig = false;
9141 	int ret = 0;
9142 	u8 type;
9143 
9144 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9145 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9146 	    (hw->phy.link_info.link_speed &
9147 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9148 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9149 		/* let firmware decide if the DCB should be disabled */
9150 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9151 
9152 	/* Not DCB capable or capability disabled */
9153 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9154 		return ret;
9155 
9156 	/* Ignore if event is not for Nearest Bridge */
9157 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9158 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9159 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9160 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9161 		return ret;
9162 
9163 	/* Check MIB Type and return if event for Remote MIB update */
9164 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9165 	dev_dbg(&pf->pdev->dev,
9166 		"LLDP event mib type %s\n", type ? "remote" : "local");
9167 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9168 		/* Update the remote cached instance and return */
9169 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9170 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9171 				&hw->remote_dcbx_config);
9172 		goto exit;
9173 	}
9174 
9175 	/* Store the old configuration */
9176 	tmp_dcbx_cfg = hw->local_dcbx_config;
9177 
9178 	/* Reset the old DCBx configuration data */
9179 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9180 	/* Get updated DCBX data from firmware */
9181 	ret = i40e_get_dcb_config(&pf->hw);
9182 	if (ret) {
9183 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9184 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9185 		    (hw->phy.link_info.link_speed &
9186 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9187 			dev_warn(&pf->pdev->dev,
9188 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9189 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9190 		} else {
9191 			dev_info(&pf->pdev->dev,
9192 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9193 				 i40e_stat_str(&pf->hw, ret),
9194 				 i40e_aq_str(&pf->hw,
9195 					     pf->hw.aq.asq_last_status));
9196 		}
9197 		goto exit;
9198 	}
9199 
9200 	/* No change detected in DCBX configs */
9201 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9202 		    sizeof(tmp_dcbx_cfg))) {
9203 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9204 		goto exit;
9205 	}
9206 
9207 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9208 					       &hw->local_dcbx_config);
9209 
9210 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9211 
9212 	if (!need_reconfig)
9213 		goto exit;
9214 
9215 	/* Enable DCB tagging only when more than one TC */
9216 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9217 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9218 	else
9219 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9220 
9221 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9222 	/* Reconfiguration needed quiesce all VSIs */
9223 	i40e_pf_quiesce_all_vsi(pf);
9224 
9225 	/* Changes in configuration update VEB/VSI */
9226 	i40e_dcb_reconfigure(pf);
9227 
9228 	ret = i40e_resume_port_tx(pf);
9229 
9230 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9231 	/* In case of error no point in resuming VSIs */
9232 	if (ret)
9233 		goto exit;
9234 
9235 	/* Wait for the PF's queues to be disabled */
9236 	ret = i40e_pf_wait_queues_disabled(pf);
9237 	if (ret) {
9238 		/* Schedule PF reset to recover */
9239 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9240 		i40e_service_event_schedule(pf);
9241 	} else {
9242 		i40e_pf_unquiesce_all_vsi(pf);
9243 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9244 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9245 	}
9246 
9247 exit:
9248 	return ret;
9249 }
9250 #endif /* CONFIG_I40E_DCB */
9251 
9252 /**
9253  * i40e_do_reset_safe - Protected reset path for userland calls.
9254  * @pf: board private structure
9255  * @reset_flags: which reset is requested
9256  *
9257  **/
9258 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9259 {
9260 	rtnl_lock();
9261 	i40e_do_reset(pf, reset_flags, true);
9262 	rtnl_unlock();
9263 }
9264 
9265 /**
9266  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9267  * @pf: board private structure
9268  * @e: event info posted on ARQ
9269  *
9270  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9271  * and VF queues
9272  **/
9273 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9274 					   struct i40e_arq_event_info *e)
9275 {
9276 	struct i40e_aqc_lan_overflow *data =
9277 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9278 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9279 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9280 	struct i40e_hw *hw = &pf->hw;
9281 	struct i40e_vf *vf;
9282 	u16 vf_id;
9283 
9284 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9285 		queue, qtx_ctl);
9286 
9287 	/* Queue belongs to VF, find the VF and issue VF reset */
9288 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9289 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9290 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9291 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9292 		vf_id -= hw->func_caps.vf_base_id;
9293 		vf = &pf->vf[vf_id];
9294 		i40e_vc_notify_vf_reset(vf);
9295 		/* Allow VF to process pending reset notification */
9296 		msleep(20);
9297 		i40e_reset_vf(vf, false);
9298 	}
9299 }
9300 
9301 /**
9302  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9303  * @pf: board private structure
9304  **/
9305 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9306 {
9307 	u32 val, fcnt_prog;
9308 
9309 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9310 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9311 	return fcnt_prog;
9312 }
9313 
9314 /**
9315  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9316  * @pf: board private structure
9317  **/
9318 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9319 {
9320 	u32 val, fcnt_prog;
9321 
9322 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9323 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9324 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9325 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9326 	return fcnt_prog;
9327 }
9328 
9329 /**
9330  * i40e_get_global_fd_count - Get total FD filters programmed on device
9331  * @pf: board private structure
9332  **/
9333 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9334 {
9335 	u32 val, fcnt_prog;
9336 
9337 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9338 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9339 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9340 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9341 	return fcnt_prog;
9342 }
9343 
9344 /**
9345  * i40e_reenable_fdir_sb - Restore FDir SB capability
9346  * @pf: board private structure
9347  **/
9348 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9349 {
9350 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9351 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9352 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9353 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9354 }
9355 
9356 /**
9357  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9358  * @pf: board private structure
9359  **/
9360 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9361 {
9362 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9363 		/* ATR uses the same filtering logic as SB rules. It only
9364 		 * functions properly if the input set mask is at the default
9365 		 * settings. It is safe to restore the default input set
9366 		 * because there are no active TCPv4 filter rules.
9367 		 */
9368 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9369 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9370 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9371 
9372 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9373 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9374 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9375 	}
9376 }
9377 
9378 /**
9379  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9380  * @pf: board private structure
9381  * @filter: FDir filter to remove
9382  */
9383 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9384 				       struct i40e_fdir_filter *filter)
9385 {
9386 	/* Update counters */
9387 	pf->fdir_pf_active_filters--;
9388 	pf->fd_inv = 0;
9389 
9390 	switch (filter->flow_type) {
9391 	case TCP_V4_FLOW:
9392 		pf->fd_tcp4_filter_cnt--;
9393 		break;
9394 	case UDP_V4_FLOW:
9395 		pf->fd_udp4_filter_cnt--;
9396 		break;
9397 	case SCTP_V4_FLOW:
9398 		pf->fd_sctp4_filter_cnt--;
9399 		break;
9400 	case TCP_V6_FLOW:
9401 		pf->fd_tcp6_filter_cnt--;
9402 		break;
9403 	case UDP_V6_FLOW:
9404 		pf->fd_udp6_filter_cnt--;
9405 		break;
9406 	case SCTP_V6_FLOW:
9407 		pf->fd_udp6_filter_cnt--;
9408 		break;
9409 	case IP_USER_FLOW:
9410 		switch (filter->ipl4_proto) {
9411 		case IPPROTO_TCP:
9412 			pf->fd_tcp4_filter_cnt--;
9413 			break;
9414 		case IPPROTO_UDP:
9415 			pf->fd_udp4_filter_cnt--;
9416 			break;
9417 		case IPPROTO_SCTP:
9418 			pf->fd_sctp4_filter_cnt--;
9419 			break;
9420 		case IPPROTO_IP:
9421 			pf->fd_ip4_filter_cnt--;
9422 			break;
9423 		}
9424 		break;
9425 	case IPV6_USER_FLOW:
9426 		switch (filter->ipl4_proto) {
9427 		case IPPROTO_TCP:
9428 			pf->fd_tcp6_filter_cnt--;
9429 			break;
9430 		case IPPROTO_UDP:
9431 			pf->fd_udp6_filter_cnt--;
9432 			break;
9433 		case IPPROTO_SCTP:
9434 			pf->fd_sctp6_filter_cnt--;
9435 			break;
9436 		case IPPROTO_IP:
9437 			pf->fd_ip6_filter_cnt--;
9438 			break;
9439 		}
9440 		break;
9441 	}
9442 
9443 	/* Remove the filter from the list and free memory */
9444 	hlist_del(&filter->fdir_node);
9445 	kfree(filter);
9446 }
9447 
9448 /**
9449  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9450  * @pf: board private structure
9451  **/
9452 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9453 {
9454 	struct i40e_fdir_filter *filter;
9455 	u32 fcnt_prog, fcnt_avail;
9456 	struct hlist_node *node;
9457 
9458 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9459 		return;
9460 
9461 	/* Check if we have enough room to re-enable FDir SB capability. */
9462 	fcnt_prog = i40e_get_global_fd_count(pf);
9463 	fcnt_avail = pf->fdir_pf_filter_count;
9464 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9465 	    (pf->fd_add_err == 0) ||
9466 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9467 		i40e_reenable_fdir_sb(pf);
9468 
9469 	/* We should wait for even more space before re-enabling ATR.
9470 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9471 	 * rules active.
9472 	 */
9473 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9474 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9475 		i40e_reenable_fdir_atr(pf);
9476 
9477 	/* if hw had a problem adding a filter, delete it */
9478 	if (pf->fd_inv > 0) {
9479 		hlist_for_each_entry_safe(filter, node,
9480 					  &pf->fdir_filter_list, fdir_node)
9481 			if (filter->fd_id == pf->fd_inv)
9482 				i40e_delete_invalid_filter(pf, filter);
9483 	}
9484 }
9485 
9486 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9487 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9488 /**
9489  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9490  * @pf: board private structure
9491  **/
9492 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9493 {
9494 	unsigned long min_flush_time;
9495 	int flush_wait_retry = 50;
9496 	bool disable_atr = false;
9497 	int fd_room;
9498 	int reg;
9499 
9500 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9501 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9502 		return;
9503 
9504 	/* If the flush is happening too quick and we have mostly SB rules we
9505 	 * should not re-enable ATR for some time.
9506 	 */
9507 	min_flush_time = pf->fd_flush_timestamp +
9508 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9509 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9510 
9511 	if (!(time_after(jiffies, min_flush_time)) &&
9512 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9513 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9514 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9515 		disable_atr = true;
9516 	}
9517 
9518 	pf->fd_flush_timestamp = jiffies;
9519 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9520 	/* flush all filters */
9521 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9522 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9523 	i40e_flush(&pf->hw);
9524 	pf->fd_flush_cnt++;
9525 	pf->fd_add_err = 0;
9526 	do {
9527 		/* Check FD flush status every 5-6msec */
9528 		usleep_range(5000, 6000);
9529 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9530 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9531 			break;
9532 	} while (flush_wait_retry--);
9533 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9534 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9535 	} else {
9536 		/* replay sideband filters */
9537 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9538 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9539 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9540 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9541 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9542 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9543 	}
9544 }
9545 
9546 /**
9547  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9548  * @pf: board private structure
9549  **/
9550 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9551 {
9552 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9553 }
9554 
9555 /**
9556  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9557  * @pf: board private structure
9558  **/
9559 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9560 {
9561 
9562 	/* if interface is down do nothing */
9563 	if (test_bit(__I40E_DOWN, pf->state))
9564 		return;
9565 
9566 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9567 		i40e_fdir_flush_and_replay(pf);
9568 
9569 	i40e_fdir_check_and_reenable(pf);
9570 
9571 }
9572 
9573 /**
9574  * i40e_vsi_link_event - notify VSI of a link event
9575  * @vsi: vsi to be notified
9576  * @link_up: link up or down
9577  **/
9578 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9579 {
9580 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9581 		return;
9582 
9583 	switch (vsi->type) {
9584 	case I40E_VSI_MAIN:
9585 		if (!vsi->netdev || !vsi->netdev_registered)
9586 			break;
9587 
9588 		if (link_up) {
9589 			netif_carrier_on(vsi->netdev);
9590 			netif_tx_wake_all_queues(vsi->netdev);
9591 		} else {
9592 			netif_carrier_off(vsi->netdev);
9593 			netif_tx_stop_all_queues(vsi->netdev);
9594 		}
9595 		break;
9596 
9597 	case I40E_VSI_SRIOV:
9598 	case I40E_VSI_VMDQ2:
9599 	case I40E_VSI_CTRL:
9600 	case I40E_VSI_IWARP:
9601 	case I40E_VSI_MIRROR:
9602 	default:
9603 		/* there is no notification for other VSIs */
9604 		break;
9605 	}
9606 }
9607 
9608 /**
9609  * i40e_veb_link_event - notify elements on the veb of a link event
9610  * @veb: veb to be notified
9611  * @link_up: link up or down
9612  **/
9613 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9614 {
9615 	struct i40e_pf *pf;
9616 	int i;
9617 
9618 	if (!veb || !veb->pf)
9619 		return;
9620 	pf = veb->pf;
9621 
9622 	/* depth first... */
9623 	for (i = 0; i < I40E_MAX_VEB; i++)
9624 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9625 			i40e_veb_link_event(pf->veb[i], link_up);
9626 
9627 	/* ... now the local VSIs */
9628 	for (i = 0; i < pf->num_alloc_vsi; i++)
9629 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9630 			i40e_vsi_link_event(pf->vsi[i], link_up);
9631 }
9632 
9633 /**
9634  * i40e_link_event - Update netif_carrier status
9635  * @pf: board private structure
9636  **/
9637 static void i40e_link_event(struct i40e_pf *pf)
9638 {
9639 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9640 	u8 new_link_speed, old_link_speed;
9641 	i40e_status status;
9642 	bool new_link, old_link;
9643 #ifdef CONFIG_I40E_DCB
9644 	int err;
9645 #endif /* CONFIG_I40E_DCB */
9646 
9647 	/* set this to force the get_link_status call to refresh state */
9648 	pf->hw.phy.get_link_info = true;
9649 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9650 	status = i40e_get_link_status(&pf->hw, &new_link);
9651 
9652 	/* On success, disable temp link polling */
9653 	if (status == I40E_SUCCESS) {
9654 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9655 	} else {
9656 		/* Enable link polling temporarily until i40e_get_link_status
9657 		 * returns I40E_SUCCESS
9658 		 */
9659 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9660 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9661 			status);
9662 		return;
9663 	}
9664 
9665 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9666 	new_link_speed = pf->hw.phy.link_info.link_speed;
9667 
9668 	if (new_link == old_link &&
9669 	    new_link_speed == old_link_speed &&
9670 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9671 	     new_link == netif_carrier_ok(vsi->netdev)))
9672 		return;
9673 
9674 	i40e_print_link_message(vsi, new_link);
9675 
9676 	/* Notify the base of the switch tree connected to
9677 	 * the link.  Floating VEBs are not notified.
9678 	 */
9679 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9680 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9681 	else
9682 		i40e_vsi_link_event(vsi, new_link);
9683 
9684 	if (pf->vf)
9685 		i40e_vc_notify_link_state(pf);
9686 
9687 	if (pf->flags & I40E_FLAG_PTP)
9688 		i40e_ptp_set_increment(pf);
9689 #ifdef CONFIG_I40E_DCB
9690 	if (new_link == old_link)
9691 		return;
9692 	/* Not SW DCB so firmware will take care of default settings */
9693 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9694 		return;
9695 
9696 	/* We cover here only link down, as after link up in case of SW DCB
9697 	 * SW LLDP agent will take care of setting it up
9698 	 */
9699 	if (!new_link) {
9700 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9701 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9702 		err = i40e_dcb_sw_default_config(pf);
9703 		if (err) {
9704 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9705 				       I40E_FLAG_DCB_ENABLED);
9706 		} else {
9707 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9708 				       DCB_CAP_DCBX_VER_IEEE;
9709 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9710 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9711 		}
9712 	}
9713 #endif /* CONFIG_I40E_DCB */
9714 }
9715 
9716 /**
9717  * i40e_watchdog_subtask - periodic checks not using event driven response
9718  * @pf: board private structure
9719  **/
9720 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9721 {
9722 	int i;
9723 
9724 	/* if interface is down do nothing */
9725 	if (test_bit(__I40E_DOWN, pf->state) ||
9726 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9727 		return;
9728 
9729 	/* make sure we don't do these things too often */
9730 	if (time_before(jiffies, (pf->service_timer_previous +
9731 				  pf->service_timer_period)))
9732 		return;
9733 	pf->service_timer_previous = jiffies;
9734 
9735 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9736 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9737 		i40e_link_event(pf);
9738 
9739 	/* Update the stats for active netdevs so the network stack
9740 	 * can look at updated numbers whenever it cares to
9741 	 */
9742 	for (i = 0; i < pf->num_alloc_vsi; i++)
9743 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9744 			i40e_update_stats(pf->vsi[i]);
9745 
9746 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9747 		/* Update the stats for the active switching components */
9748 		for (i = 0; i < I40E_MAX_VEB; i++)
9749 			if (pf->veb[i])
9750 				i40e_update_veb_stats(pf->veb[i]);
9751 	}
9752 
9753 	i40e_ptp_rx_hang(pf);
9754 	i40e_ptp_tx_hang(pf);
9755 }
9756 
9757 /**
9758  * i40e_reset_subtask - Set up for resetting the device and driver
9759  * @pf: board private structure
9760  **/
9761 static void i40e_reset_subtask(struct i40e_pf *pf)
9762 {
9763 	u32 reset_flags = 0;
9764 
9765 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9766 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9767 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9768 	}
9769 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9770 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9771 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9772 	}
9773 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9774 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9775 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9776 	}
9777 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9778 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9779 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9780 	}
9781 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9782 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9783 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9784 	}
9785 
9786 	/* If there's a recovery already waiting, it takes
9787 	 * precedence before starting a new reset sequence.
9788 	 */
9789 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9790 		i40e_prep_for_reset(pf);
9791 		i40e_reset(pf);
9792 		i40e_rebuild(pf, false, false);
9793 	}
9794 
9795 	/* If we're already down or resetting, just bail */
9796 	if (reset_flags &&
9797 	    !test_bit(__I40E_DOWN, pf->state) &&
9798 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9799 		i40e_do_reset(pf, reset_flags, false);
9800 	}
9801 }
9802 
9803 /**
9804  * i40e_handle_link_event - Handle link event
9805  * @pf: board private structure
9806  * @e: event info posted on ARQ
9807  **/
9808 static void i40e_handle_link_event(struct i40e_pf *pf,
9809 				   struct i40e_arq_event_info *e)
9810 {
9811 	struct i40e_aqc_get_link_status *status =
9812 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9813 
9814 	/* Do a new status request to re-enable LSE reporting
9815 	 * and load new status information into the hw struct
9816 	 * This completely ignores any state information
9817 	 * in the ARQ event info, instead choosing to always
9818 	 * issue the AQ update link status command.
9819 	 */
9820 	i40e_link_event(pf);
9821 
9822 	/* Check if module meets thermal requirements */
9823 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9824 		dev_err(&pf->pdev->dev,
9825 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9826 		dev_err(&pf->pdev->dev,
9827 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9828 	} else {
9829 		/* check for unqualified module, if link is down, suppress
9830 		 * the message if link was forced to be down.
9831 		 */
9832 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9833 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9834 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9835 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9836 			dev_err(&pf->pdev->dev,
9837 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9838 			dev_err(&pf->pdev->dev,
9839 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9840 		}
9841 	}
9842 }
9843 
9844 /**
9845  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9846  * @pf: board private structure
9847  **/
9848 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9849 {
9850 	struct i40e_arq_event_info event;
9851 	struct i40e_hw *hw = &pf->hw;
9852 	u16 pending, i = 0;
9853 	i40e_status ret;
9854 	u16 opcode;
9855 	u32 oldval;
9856 	u32 val;
9857 
9858 	/* Do not run clean AQ when PF reset fails */
9859 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9860 		return;
9861 
9862 	/* check for error indications */
9863 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9864 	oldval = val;
9865 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9866 		if (hw->debug_mask & I40E_DEBUG_AQ)
9867 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9868 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9869 	}
9870 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9871 		if (hw->debug_mask & I40E_DEBUG_AQ)
9872 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9873 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9874 		pf->arq_overflows++;
9875 	}
9876 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9877 		if (hw->debug_mask & I40E_DEBUG_AQ)
9878 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9879 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9880 	}
9881 	if (oldval != val)
9882 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9883 
9884 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9885 	oldval = val;
9886 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9887 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9888 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9889 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9890 	}
9891 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9892 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9893 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9894 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9895 	}
9896 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9897 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9898 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9899 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9900 	}
9901 	if (oldval != val)
9902 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9903 
9904 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9905 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9906 	if (!event.msg_buf)
9907 		return;
9908 
9909 	do {
9910 		ret = i40e_clean_arq_element(hw, &event, &pending);
9911 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9912 			break;
9913 		else if (ret) {
9914 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9915 			break;
9916 		}
9917 
9918 		opcode = le16_to_cpu(event.desc.opcode);
9919 		switch (opcode) {
9920 
9921 		case i40e_aqc_opc_get_link_status:
9922 			rtnl_lock();
9923 			i40e_handle_link_event(pf, &event);
9924 			rtnl_unlock();
9925 			break;
9926 		case i40e_aqc_opc_send_msg_to_pf:
9927 			ret = i40e_vc_process_vf_msg(pf,
9928 					le16_to_cpu(event.desc.retval),
9929 					le32_to_cpu(event.desc.cookie_high),
9930 					le32_to_cpu(event.desc.cookie_low),
9931 					event.msg_buf,
9932 					event.msg_len);
9933 			break;
9934 		case i40e_aqc_opc_lldp_update_mib:
9935 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9936 #ifdef CONFIG_I40E_DCB
9937 			rtnl_lock();
9938 			i40e_handle_lldp_event(pf, &event);
9939 			rtnl_unlock();
9940 #endif /* CONFIG_I40E_DCB */
9941 			break;
9942 		case i40e_aqc_opc_event_lan_overflow:
9943 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9944 			i40e_handle_lan_overflow_event(pf, &event);
9945 			break;
9946 		case i40e_aqc_opc_send_msg_to_peer:
9947 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9948 			break;
9949 		case i40e_aqc_opc_nvm_erase:
9950 		case i40e_aqc_opc_nvm_update:
9951 		case i40e_aqc_opc_oem_post_update:
9952 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9953 				   "ARQ NVM operation 0x%04x completed\n",
9954 				   opcode);
9955 			break;
9956 		default:
9957 			dev_info(&pf->pdev->dev,
9958 				 "ARQ: Unknown event 0x%04x ignored\n",
9959 				 opcode);
9960 			break;
9961 		}
9962 	} while (i++ < pf->adminq_work_limit);
9963 
9964 	if (i < pf->adminq_work_limit)
9965 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9966 
9967 	/* re-enable Admin queue interrupt cause */
9968 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9969 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9970 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9971 	i40e_flush(hw);
9972 
9973 	kfree(event.msg_buf);
9974 }
9975 
9976 /**
9977  * i40e_verify_eeprom - make sure eeprom is good to use
9978  * @pf: board private structure
9979  **/
9980 static void i40e_verify_eeprom(struct i40e_pf *pf)
9981 {
9982 	int err;
9983 
9984 	err = i40e_diag_eeprom_test(&pf->hw);
9985 	if (err) {
9986 		/* retry in case of garbage read */
9987 		err = i40e_diag_eeprom_test(&pf->hw);
9988 		if (err) {
9989 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9990 				 err);
9991 			set_bit(__I40E_BAD_EEPROM, pf->state);
9992 		}
9993 	}
9994 
9995 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9996 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9997 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9998 	}
9999 }
10000 
10001 /**
10002  * i40e_enable_pf_switch_lb
10003  * @pf: pointer to the PF structure
10004  *
10005  * enable switch loop back or die - no point in a return value
10006  **/
10007 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10008 {
10009 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10010 	struct i40e_vsi_context ctxt;
10011 	int ret;
10012 
10013 	ctxt.seid = pf->main_vsi_seid;
10014 	ctxt.pf_num = pf->hw.pf_id;
10015 	ctxt.vf_num = 0;
10016 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10017 	if (ret) {
10018 		dev_info(&pf->pdev->dev,
10019 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10020 			 i40e_stat_str(&pf->hw, ret),
10021 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10022 		return;
10023 	}
10024 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10025 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10026 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10027 
10028 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10029 	if (ret) {
10030 		dev_info(&pf->pdev->dev,
10031 			 "update vsi switch failed, err %s aq_err %s\n",
10032 			 i40e_stat_str(&pf->hw, ret),
10033 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10034 	}
10035 }
10036 
10037 /**
10038  * i40e_disable_pf_switch_lb
10039  * @pf: pointer to the PF structure
10040  *
10041  * disable switch loop back or die - no point in a return value
10042  **/
10043 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10044 {
10045 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10046 	struct i40e_vsi_context ctxt;
10047 	int ret;
10048 
10049 	ctxt.seid = pf->main_vsi_seid;
10050 	ctxt.pf_num = pf->hw.pf_id;
10051 	ctxt.vf_num = 0;
10052 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10053 	if (ret) {
10054 		dev_info(&pf->pdev->dev,
10055 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10056 			 i40e_stat_str(&pf->hw, ret),
10057 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10058 		return;
10059 	}
10060 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10061 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10062 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10063 
10064 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10065 	if (ret) {
10066 		dev_info(&pf->pdev->dev,
10067 			 "update vsi switch failed, err %s aq_err %s\n",
10068 			 i40e_stat_str(&pf->hw, ret),
10069 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10070 	}
10071 }
10072 
10073 /**
10074  * i40e_config_bridge_mode - Configure the HW bridge mode
10075  * @veb: pointer to the bridge instance
10076  *
10077  * Configure the loop back mode for the LAN VSI that is downlink to the
10078  * specified HW bridge instance. It is expected this function is called
10079  * when a new HW bridge is instantiated.
10080  **/
10081 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10082 {
10083 	struct i40e_pf *pf = veb->pf;
10084 
10085 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10086 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10087 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10088 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10089 		i40e_disable_pf_switch_lb(pf);
10090 	else
10091 		i40e_enable_pf_switch_lb(pf);
10092 }
10093 
10094 /**
10095  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10096  * @veb: pointer to the VEB instance
10097  *
10098  * This is a recursive function that first builds the attached VSIs then
10099  * recurses in to build the next layer of VEB.  We track the connections
10100  * through our own index numbers because the seid's from the HW could
10101  * change across the reset.
10102  **/
10103 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10104 {
10105 	struct i40e_vsi *ctl_vsi = NULL;
10106 	struct i40e_pf *pf = veb->pf;
10107 	int v, veb_idx;
10108 	int ret;
10109 
10110 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10111 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10112 		if (pf->vsi[v] &&
10113 		    pf->vsi[v]->veb_idx == veb->idx &&
10114 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10115 			ctl_vsi = pf->vsi[v];
10116 			break;
10117 		}
10118 	}
10119 	if (!ctl_vsi) {
10120 		dev_info(&pf->pdev->dev,
10121 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10122 		ret = -ENOENT;
10123 		goto end_reconstitute;
10124 	}
10125 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10126 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10127 	ret = i40e_add_vsi(ctl_vsi);
10128 	if (ret) {
10129 		dev_info(&pf->pdev->dev,
10130 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10131 			 veb->idx, ret);
10132 		goto end_reconstitute;
10133 	}
10134 	i40e_vsi_reset_stats(ctl_vsi);
10135 
10136 	/* create the VEB in the switch and move the VSI onto the VEB */
10137 	ret = i40e_add_veb(veb, ctl_vsi);
10138 	if (ret)
10139 		goto end_reconstitute;
10140 
10141 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10142 		veb->bridge_mode = BRIDGE_MODE_VEB;
10143 	else
10144 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10145 	i40e_config_bridge_mode(veb);
10146 
10147 	/* create the remaining VSIs attached to this VEB */
10148 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10149 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10150 			continue;
10151 
10152 		if (pf->vsi[v]->veb_idx == veb->idx) {
10153 			struct i40e_vsi *vsi = pf->vsi[v];
10154 
10155 			vsi->uplink_seid = veb->seid;
10156 			ret = i40e_add_vsi(vsi);
10157 			if (ret) {
10158 				dev_info(&pf->pdev->dev,
10159 					 "rebuild of vsi_idx %d failed: %d\n",
10160 					 v, ret);
10161 				goto end_reconstitute;
10162 			}
10163 			i40e_vsi_reset_stats(vsi);
10164 		}
10165 	}
10166 
10167 	/* create any VEBs attached to this VEB - RECURSION */
10168 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10169 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10170 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10171 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10172 			if (ret)
10173 				break;
10174 		}
10175 	}
10176 
10177 end_reconstitute:
10178 	return ret;
10179 }
10180 
10181 /**
10182  * i40e_get_capabilities - get info about the HW
10183  * @pf: the PF struct
10184  * @list_type: AQ capability to be queried
10185  **/
10186 static int i40e_get_capabilities(struct i40e_pf *pf,
10187 				 enum i40e_admin_queue_opc list_type)
10188 {
10189 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10190 	u16 data_size;
10191 	int buf_len;
10192 	int err;
10193 
10194 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10195 	do {
10196 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10197 		if (!cap_buf)
10198 			return -ENOMEM;
10199 
10200 		/* this loads the data into the hw struct for us */
10201 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10202 						    &data_size, list_type,
10203 						    NULL);
10204 		/* data loaded, buffer no longer needed */
10205 		kfree(cap_buf);
10206 
10207 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10208 			/* retry with a larger buffer */
10209 			buf_len = data_size;
10210 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10211 			dev_info(&pf->pdev->dev,
10212 				 "capability discovery failed, err %s aq_err %s\n",
10213 				 i40e_stat_str(&pf->hw, err),
10214 				 i40e_aq_str(&pf->hw,
10215 					     pf->hw.aq.asq_last_status));
10216 			return -ENODEV;
10217 		}
10218 	} while (err);
10219 
10220 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10221 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10222 			dev_info(&pf->pdev->dev,
10223 				 "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",
10224 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10225 				 pf->hw.func_caps.num_msix_vectors,
10226 				 pf->hw.func_caps.num_msix_vectors_vf,
10227 				 pf->hw.func_caps.fd_filters_guaranteed,
10228 				 pf->hw.func_caps.fd_filters_best_effort,
10229 				 pf->hw.func_caps.num_tx_qp,
10230 				 pf->hw.func_caps.num_vsis);
10231 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10232 			dev_info(&pf->pdev->dev,
10233 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10234 				 pf->hw.dev_caps.switch_mode,
10235 				 pf->hw.dev_caps.valid_functions);
10236 			dev_info(&pf->pdev->dev,
10237 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10238 				 pf->hw.dev_caps.sr_iov_1_1,
10239 				 pf->hw.dev_caps.num_vfs);
10240 			dev_info(&pf->pdev->dev,
10241 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10242 				 pf->hw.dev_caps.num_vsis,
10243 				 pf->hw.dev_caps.num_rx_qp,
10244 				 pf->hw.dev_caps.num_tx_qp);
10245 		}
10246 	}
10247 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10248 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10249 		       + pf->hw.func_caps.num_vfs)
10250 		if (pf->hw.revision_id == 0 &&
10251 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10252 			dev_info(&pf->pdev->dev,
10253 				 "got num_vsis %d, setting num_vsis to %d\n",
10254 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10255 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10256 		}
10257 	}
10258 	return 0;
10259 }
10260 
10261 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10262 
10263 /**
10264  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10265  * @pf: board private structure
10266  **/
10267 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10268 {
10269 	struct i40e_vsi *vsi;
10270 
10271 	/* quick workaround for an NVM issue that leaves a critical register
10272 	 * uninitialized
10273 	 */
10274 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10275 		static const u32 hkey[] = {
10276 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10277 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10278 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10279 			0x95b3a76d};
10280 		int i;
10281 
10282 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10283 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10284 	}
10285 
10286 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10287 		return;
10288 
10289 	/* find existing VSI and see if it needs configuring */
10290 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10291 
10292 	/* create a new VSI if none exists */
10293 	if (!vsi) {
10294 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10295 				     pf->vsi[pf->lan_vsi]->seid, 0);
10296 		if (!vsi) {
10297 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10298 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10299 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10300 			return;
10301 		}
10302 	}
10303 
10304 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10305 }
10306 
10307 /**
10308  * i40e_fdir_teardown - release the Flow Director resources
10309  * @pf: board private structure
10310  **/
10311 static void i40e_fdir_teardown(struct i40e_pf *pf)
10312 {
10313 	struct i40e_vsi *vsi;
10314 
10315 	i40e_fdir_filter_exit(pf);
10316 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10317 	if (vsi)
10318 		i40e_vsi_release(vsi);
10319 }
10320 
10321 /**
10322  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10323  * @vsi: PF main vsi
10324  * @seid: seid of main or channel VSIs
10325  *
10326  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10327  * existed before reset
10328  **/
10329 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10330 {
10331 	struct i40e_cloud_filter *cfilter;
10332 	struct i40e_pf *pf = vsi->back;
10333 	struct hlist_node *node;
10334 	i40e_status ret;
10335 
10336 	/* Add cloud filters back if they exist */
10337 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10338 				  cloud_node) {
10339 		if (cfilter->seid != seid)
10340 			continue;
10341 
10342 		if (cfilter->dst_port)
10343 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10344 								true);
10345 		else
10346 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10347 
10348 		if (ret) {
10349 			dev_dbg(&pf->pdev->dev,
10350 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10351 				i40e_stat_str(&pf->hw, ret),
10352 				i40e_aq_str(&pf->hw,
10353 					    pf->hw.aq.asq_last_status));
10354 			return ret;
10355 		}
10356 	}
10357 	return 0;
10358 }
10359 
10360 /**
10361  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10362  * @vsi: PF main vsi
10363  *
10364  * Rebuilds channel VSIs if they existed before reset
10365  **/
10366 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10367 {
10368 	struct i40e_channel *ch, *ch_tmp;
10369 	i40e_status ret;
10370 
10371 	if (list_empty(&vsi->ch_list))
10372 		return 0;
10373 
10374 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10375 		if (!ch->initialized)
10376 			break;
10377 		/* Proceed with creation of channel (VMDq2) VSI */
10378 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10379 		if (ret) {
10380 			dev_info(&vsi->back->pdev->dev,
10381 				 "failed to rebuild channels using uplink_seid %u\n",
10382 				 vsi->uplink_seid);
10383 			return ret;
10384 		}
10385 		/* Reconfigure TX queues using QTX_CTL register */
10386 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10387 		if (ret) {
10388 			dev_info(&vsi->back->pdev->dev,
10389 				 "failed to configure TX rings for channel %u\n",
10390 				 ch->seid);
10391 			return ret;
10392 		}
10393 		/* update 'next_base_queue' */
10394 		vsi->next_base_queue = vsi->next_base_queue +
10395 							ch->num_queue_pairs;
10396 		if (ch->max_tx_rate) {
10397 			u64 credits = ch->max_tx_rate;
10398 
10399 			if (i40e_set_bw_limit(vsi, ch->seid,
10400 					      ch->max_tx_rate))
10401 				return -EINVAL;
10402 
10403 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10404 			dev_dbg(&vsi->back->pdev->dev,
10405 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10406 				ch->max_tx_rate,
10407 				credits,
10408 				ch->seid);
10409 		}
10410 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10411 		if (ret) {
10412 			dev_dbg(&vsi->back->pdev->dev,
10413 				"Failed to rebuild cloud filters for channel VSI %u\n",
10414 				ch->seid);
10415 			return ret;
10416 		}
10417 	}
10418 	return 0;
10419 }
10420 
10421 /**
10422  * i40e_prep_for_reset - prep for the core to reset
10423  * @pf: board private structure
10424  *
10425  * Close up the VFs and other things in prep for PF Reset.
10426   **/
10427 static void i40e_prep_for_reset(struct i40e_pf *pf)
10428 {
10429 	struct i40e_hw *hw = &pf->hw;
10430 	i40e_status ret = 0;
10431 	u32 v;
10432 
10433 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10434 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10435 		return;
10436 	if (i40e_check_asq_alive(&pf->hw))
10437 		i40e_vc_notify_reset(pf);
10438 
10439 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10440 
10441 	/* quiesce the VSIs and their queues that are not already DOWN */
10442 	i40e_pf_quiesce_all_vsi(pf);
10443 
10444 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10445 		if (pf->vsi[v])
10446 			pf->vsi[v]->seid = 0;
10447 	}
10448 
10449 	i40e_shutdown_adminq(&pf->hw);
10450 
10451 	/* call shutdown HMC */
10452 	if (hw->hmc.hmc_obj) {
10453 		ret = i40e_shutdown_lan_hmc(hw);
10454 		if (ret)
10455 			dev_warn(&pf->pdev->dev,
10456 				 "shutdown_lan_hmc failed: %d\n", ret);
10457 	}
10458 
10459 	/* Save the current PTP time so that we can restore the time after the
10460 	 * reset completes.
10461 	 */
10462 	i40e_ptp_save_hw_time(pf);
10463 }
10464 
10465 /**
10466  * i40e_send_version - update firmware with driver version
10467  * @pf: PF struct
10468  */
10469 static void i40e_send_version(struct i40e_pf *pf)
10470 {
10471 	struct i40e_driver_version dv;
10472 
10473 	dv.major_version = 0xff;
10474 	dv.minor_version = 0xff;
10475 	dv.build_version = 0xff;
10476 	dv.subbuild_version = 0;
10477 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10478 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10479 }
10480 
10481 /**
10482  * i40e_get_oem_version - get OEM specific version information
10483  * @hw: pointer to the hardware structure
10484  **/
10485 static void i40e_get_oem_version(struct i40e_hw *hw)
10486 {
10487 	u16 block_offset = 0xffff;
10488 	u16 block_length = 0;
10489 	u16 capabilities = 0;
10490 	u16 gen_snap = 0;
10491 	u16 release = 0;
10492 
10493 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10494 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10495 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10496 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10497 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10498 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10499 #define I40E_NVM_OEM_LENGTH			3
10500 
10501 	/* Check if pointer to OEM version block is valid. */
10502 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10503 	if (block_offset == 0xffff)
10504 		return;
10505 
10506 	/* Check if OEM version block has correct length. */
10507 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10508 			   &block_length);
10509 	if (block_length < I40E_NVM_OEM_LENGTH)
10510 		return;
10511 
10512 	/* Check if OEM version format is as expected. */
10513 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10514 			   &capabilities);
10515 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10516 		return;
10517 
10518 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10519 			   &gen_snap);
10520 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10521 			   &release);
10522 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10523 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10524 }
10525 
10526 /**
10527  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10528  * @pf: board private structure
10529  **/
10530 static int i40e_reset(struct i40e_pf *pf)
10531 {
10532 	struct i40e_hw *hw = &pf->hw;
10533 	i40e_status ret;
10534 
10535 	ret = i40e_pf_reset(hw);
10536 	if (ret) {
10537 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10538 		set_bit(__I40E_RESET_FAILED, pf->state);
10539 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10540 	} else {
10541 		pf->pfr_count++;
10542 	}
10543 	return ret;
10544 }
10545 
10546 /**
10547  * i40e_rebuild - rebuild using a saved config
10548  * @pf: board private structure
10549  * @reinit: if the Main VSI needs to re-initialized.
10550  * @lock_acquired: indicates whether or not the lock has been acquired
10551  * before this function was called.
10552  **/
10553 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10554 {
10555 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10556 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10557 	struct i40e_hw *hw = &pf->hw;
10558 	i40e_status ret;
10559 	u32 val;
10560 	int v;
10561 
10562 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10563 	    i40e_check_recovery_mode(pf)) {
10564 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10565 	}
10566 
10567 	if (test_bit(__I40E_DOWN, pf->state) &&
10568 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10569 	    !old_recovery_mode_bit)
10570 		goto clear_recovery;
10571 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10572 
10573 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10574 	ret = i40e_init_adminq(&pf->hw);
10575 	if (ret) {
10576 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10577 			 i40e_stat_str(&pf->hw, ret),
10578 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10579 		goto clear_recovery;
10580 	}
10581 	i40e_get_oem_version(&pf->hw);
10582 
10583 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10584 		/* The following delay is necessary for firmware update. */
10585 		mdelay(1000);
10586 	}
10587 
10588 	/* re-verify the eeprom if we just had an EMP reset */
10589 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10590 		i40e_verify_eeprom(pf);
10591 
10592 	/* if we are going out of or into recovery mode we have to act
10593 	 * accordingly with regard to resources initialization
10594 	 * and deinitialization
10595 	 */
10596 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10597 	    old_recovery_mode_bit) {
10598 		if (i40e_get_capabilities(pf,
10599 					  i40e_aqc_opc_list_func_capabilities))
10600 			goto end_unlock;
10601 
10602 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10603 			/* we're staying in recovery mode so we'll reinitialize
10604 			 * misc vector here
10605 			 */
10606 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10607 				goto end_unlock;
10608 		} else {
10609 			if (!lock_acquired)
10610 				rtnl_lock();
10611 			/* we're going out of recovery mode so we'll free
10612 			 * the IRQ allocated specifically for recovery mode
10613 			 * and restore the interrupt scheme
10614 			 */
10615 			free_irq(pf->pdev->irq, pf);
10616 			i40e_clear_interrupt_scheme(pf);
10617 			if (i40e_restore_interrupt_scheme(pf))
10618 				goto end_unlock;
10619 		}
10620 
10621 		/* tell the firmware that we're starting */
10622 		i40e_send_version(pf);
10623 
10624 		/* bail out in case recovery mode was detected, as there is
10625 		 * no need for further configuration.
10626 		 */
10627 		goto end_unlock;
10628 	}
10629 
10630 	i40e_clear_pxe_mode(hw);
10631 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10632 	if (ret)
10633 		goto end_core_reset;
10634 
10635 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10636 				hw->func_caps.num_rx_qp, 0, 0);
10637 	if (ret) {
10638 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10639 		goto end_core_reset;
10640 	}
10641 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10642 	if (ret) {
10643 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10644 		goto end_core_reset;
10645 	}
10646 
10647 #ifdef CONFIG_I40E_DCB
10648 	/* Enable FW to write a default DCB config on link-up
10649 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10650 	 * is not supported with new link speed
10651 	 */
10652 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
10653 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10654 	} else {
10655 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10656 		    (hw->phy.link_info.link_speed &
10657 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10658 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10659 			dev_warn(&pf->pdev->dev,
10660 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10661 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10662 		} else {
10663 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10664 			ret = i40e_init_pf_dcb(pf);
10665 			if (ret) {
10666 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10667 					 ret);
10668 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10669 				/* Continue without DCB enabled */
10670 			}
10671 		}
10672 	}
10673 
10674 #endif /* CONFIG_I40E_DCB */
10675 	if (!lock_acquired)
10676 		rtnl_lock();
10677 	ret = i40e_setup_pf_switch(pf, reinit, true);
10678 	if (ret)
10679 		goto end_unlock;
10680 
10681 	/* The driver only wants link up/down and module qualification
10682 	 * reports from firmware.  Note the negative logic.
10683 	 */
10684 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10685 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10686 					 I40E_AQ_EVENT_MEDIA_NA |
10687 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10688 	if (ret)
10689 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10690 			 i40e_stat_str(&pf->hw, ret),
10691 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10692 
10693 	/* Rebuild the VSIs and VEBs that existed before reset.
10694 	 * They are still in our local switch element arrays, so only
10695 	 * need to rebuild the switch model in the HW.
10696 	 *
10697 	 * If there were VEBs but the reconstitution failed, we'll try
10698 	 * to recover minimal use by getting the basic PF VSI working.
10699 	 */
10700 	if (vsi->uplink_seid != pf->mac_seid) {
10701 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10702 		/* find the one VEB connected to the MAC, and find orphans */
10703 		for (v = 0; v < I40E_MAX_VEB; v++) {
10704 			if (!pf->veb[v])
10705 				continue;
10706 
10707 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10708 			    pf->veb[v]->uplink_seid == 0) {
10709 				ret = i40e_reconstitute_veb(pf->veb[v]);
10710 
10711 				if (!ret)
10712 					continue;
10713 
10714 				/* If Main VEB failed, we're in deep doodoo,
10715 				 * so give up rebuilding the switch and set up
10716 				 * for minimal rebuild of PF VSI.
10717 				 * If orphan failed, we'll report the error
10718 				 * but try to keep going.
10719 				 */
10720 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10721 					dev_info(&pf->pdev->dev,
10722 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10723 						 ret);
10724 					vsi->uplink_seid = pf->mac_seid;
10725 					break;
10726 				} else if (pf->veb[v]->uplink_seid == 0) {
10727 					dev_info(&pf->pdev->dev,
10728 						 "rebuild of orphan VEB failed: %d\n",
10729 						 ret);
10730 				}
10731 			}
10732 		}
10733 	}
10734 
10735 	if (vsi->uplink_seid == pf->mac_seid) {
10736 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10737 		/* no VEB, so rebuild only the Main VSI */
10738 		ret = i40e_add_vsi(vsi);
10739 		if (ret) {
10740 			dev_info(&pf->pdev->dev,
10741 				 "rebuild of Main VSI failed: %d\n", ret);
10742 			goto end_unlock;
10743 		}
10744 	}
10745 
10746 	if (vsi->mqprio_qopt.max_rate[0]) {
10747 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10748 		u64 credits = 0;
10749 
10750 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10751 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10752 		if (ret)
10753 			goto end_unlock;
10754 
10755 		credits = max_tx_rate;
10756 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10757 		dev_dbg(&vsi->back->pdev->dev,
10758 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10759 			max_tx_rate,
10760 			credits,
10761 			vsi->seid);
10762 	}
10763 
10764 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10765 	if (ret)
10766 		goto end_unlock;
10767 
10768 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10769 	 * for this main VSI if they exist
10770 	 */
10771 	ret = i40e_rebuild_channels(vsi);
10772 	if (ret)
10773 		goto end_unlock;
10774 
10775 	/* Reconfigure hardware for allowing smaller MSS in the case
10776 	 * of TSO, so that we avoid the MDD being fired and causing
10777 	 * a reset in the case of small MSS+TSO.
10778 	 */
10779 #define I40E_REG_MSS          0x000E64DC
10780 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10781 #define I40E_64BYTE_MSS       0x400000
10782 	val = rd32(hw, I40E_REG_MSS);
10783 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10784 		val &= ~I40E_REG_MSS_MIN_MASK;
10785 		val |= I40E_64BYTE_MSS;
10786 		wr32(hw, I40E_REG_MSS, val);
10787 	}
10788 
10789 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10790 		msleep(75);
10791 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10792 		if (ret)
10793 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10794 				 i40e_stat_str(&pf->hw, ret),
10795 				 i40e_aq_str(&pf->hw,
10796 					     pf->hw.aq.asq_last_status));
10797 	}
10798 	/* reinit the misc interrupt */
10799 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10800 		ret = i40e_setup_misc_vector(pf);
10801 
10802 	/* Add a filter to drop all Flow control frames from any VSI from being
10803 	 * transmitted. By doing so we stop a malicious VF from sending out
10804 	 * PAUSE or PFC frames and potentially controlling traffic for other
10805 	 * PF/VF VSIs.
10806 	 * The FW can still send Flow control frames if enabled.
10807 	 */
10808 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10809 						       pf->main_vsi_seid);
10810 
10811 	/* restart the VSIs that were rebuilt and running before the reset */
10812 	i40e_pf_unquiesce_all_vsi(pf);
10813 
10814 	/* Release the RTNL lock before we start resetting VFs */
10815 	if (!lock_acquired)
10816 		rtnl_unlock();
10817 
10818 	/* Restore promiscuous settings */
10819 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10820 	if (ret)
10821 		dev_warn(&pf->pdev->dev,
10822 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10823 			 pf->cur_promisc ? "on" : "off",
10824 			 i40e_stat_str(&pf->hw, ret),
10825 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10826 
10827 	i40e_reset_all_vfs(pf, true);
10828 
10829 	/* tell the firmware that we're starting */
10830 	i40e_send_version(pf);
10831 
10832 	/* We've already released the lock, so don't do it again */
10833 	goto end_core_reset;
10834 
10835 end_unlock:
10836 	if (!lock_acquired)
10837 		rtnl_unlock();
10838 end_core_reset:
10839 	clear_bit(__I40E_RESET_FAILED, pf->state);
10840 clear_recovery:
10841 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10842 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10843 }
10844 
10845 /**
10846  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10847  * @pf: board private structure
10848  * @reinit: if the Main VSI needs to re-initialized.
10849  * @lock_acquired: indicates whether or not the lock has been acquired
10850  * before this function was called.
10851  **/
10852 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10853 				   bool lock_acquired)
10854 {
10855 	int ret;
10856 
10857 	if (test_bit(__I40E_IN_REMOVE, pf->state))
10858 		return;
10859 	/* Now we wait for GRST to settle out.
10860 	 * We don't have to delete the VEBs or VSIs from the hw switch
10861 	 * because the reset will make them disappear.
10862 	 */
10863 	ret = i40e_reset(pf);
10864 	if (!ret)
10865 		i40e_rebuild(pf, reinit, lock_acquired);
10866 }
10867 
10868 /**
10869  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10870  * @pf: board private structure
10871  *
10872  * Close up the VFs and other things in prep for a Core Reset,
10873  * then get ready to rebuild the world.
10874  * @lock_acquired: indicates whether or not the lock has been acquired
10875  * before this function was called.
10876  **/
10877 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10878 {
10879 	i40e_prep_for_reset(pf);
10880 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10881 }
10882 
10883 /**
10884  * i40e_handle_mdd_event
10885  * @pf: pointer to the PF structure
10886  *
10887  * Called from the MDD irq handler to identify possibly malicious vfs
10888  **/
10889 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10890 {
10891 	struct i40e_hw *hw = &pf->hw;
10892 	bool mdd_detected = false;
10893 	struct i40e_vf *vf;
10894 	u32 reg;
10895 	int i;
10896 
10897 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10898 		return;
10899 
10900 	/* find what triggered the MDD event */
10901 	reg = rd32(hw, I40E_GL_MDET_TX);
10902 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10903 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10904 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10905 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10906 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10907 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10908 				I40E_GL_MDET_TX_EVENT_SHIFT;
10909 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10910 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10911 				pf->hw.func_caps.base_queue;
10912 		if (netif_msg_tx_err(pf))
10913 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10914 				 event, queue, pf_num, vf_num);
10915 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10916 		mdd_detected = true;
10917 	}
10918 	reg = rd32(hw, I40E_GL_MDET_RX);
10919 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10920 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10921 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10922 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10923 				I40E_GL_MDET_RX_EVENT_SHIFT;
10924 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10925 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10926 				pf->hw.func_caps.base_queue;
10927 		if (netif_msg_rx_err(pf))
10928 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10929 				 event, queue, func);
10930 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10931 		mdd_detected = true;
10932 	}
10933 
10934 	if (mdd_detected) {
10935 		reg = rd32(hw, I40E_PF_MDET_TX);
10936 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10937 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10938 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10939 		}
10940 		reg = rd32(hw, I40E_PF_MDET_RX);
10941 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10942 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10943 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10944 		}
10945 	}
10946 
10947 	/* see if one of the VFs needs its hand slapped */
10948 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10949 		vf = &(pf->vf[i]);
10950 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10951 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10952 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10953 			vf->num_mdd_events++;
10954 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10955 				 i);
10956 			dev_info(&pf->pdev->dev,
10957 				 "Use PF Control I/F to re-enable the VF\n");
10958 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10959 		}
10960 
10961 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10962 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10963 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10964 			vf->num_mdd_events++;
10965 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10966 				 i);
10967 			dev_info(&pf->pdev->dev,
10968 				 "Use PF Control I/F to re-enable the VF\n");
10969 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10970 		}
10971 	}
10972 
10973 	/* re-enable mdd interrupt cause */
10974 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10975 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10976 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10977 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10978 	i40e_flush(hw);
10979 }
10980 
10981 /**
10982  * i40e_service_task - Run the driver's async subtasks
10983  * @work: pointer to work_struct containing our data
10984  **/
10985 static void i40e_service_task(struct work_struct *work)
10986 {
10987 	struct i40e_pf *pf = container_of(work,
10988 					  struct i40e_pf,
10989 					  service_task);
10990 	unsigned long start_time = jiffies;
10991 
10992 	/* don't bother with service tasks if a reset is in progress */
10993 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10994 	    test_bit(__I40E_SUSPENDED, pf->state))
10995 		return;
10996 
10997 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10998 		return;
10999 
11000 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11001 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11002 		i40e_sync_filters_subtask(pf);
11003 		i40e_reset_subtask(pf);
11004 		i40e_handle_mdd_event(pf);
11005 		i40e_vc_process_vflr_event(pf);
11006 		i40e_watchdog_subtask(pf);
11007 		i40e_fdir_reinit_subtask(pf);
11008 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11009 			/* Client subtask will reopen next time through. */
11010 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11011 							   true);
11012 		} else {
11013 			i40e_client_subtask(pf);
11014 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11015 					       pf->state))
11016 				i40e_notify_client_of_l2_param_changes(
11017 								pf->vsi[pf->lan_vsi]);
11018 		}
11019 		i40e_sync_filters_subtask(pf);
11020 	} else {
11021 		i40e_reset_subtask(pf);
11022 	}
11023 
11024 	i40e_clean_adminq_subtask(pf);
11025 
11026 	/* flush memory to make sure state is correct before next watchdog */
11027 	smp_mb__before_atomic();
11028 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11029 
11030 	/* If the tasks have taken longer than one timer cycle or there
11031 	 * is more work to be done, reschedule the service task now
11032 	 * rather than wait for the timer to tick again.
11033 	 */
11034 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11035 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11036 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11037 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11038 		i40e_service_event_schedule(pf);
11039 }
11040 
11041 /**
11042  * i40e_service_timer - timer callback
11043  * @t: timer list pointer
11044  **/
11045 static void i40e_service_timer(struct timer_list *t)
11046 {
11047 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11048 
11049 	mod_timer(&pf->service_timer,
11050 		  round_jiffies(jiffies + pf->service_timer_period));
11051 	i40e_service_event_schedule(pf);
11052 }
11053 
11054 /**
11055  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11056  * @vsi: the VSI being configured
11057  **/
11058 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11059 {
11060 	struct i40e_pf *pf = vsi->back;
11061 
11062 	switch (vsi->type) {
11063 	case I40E_VSI_MAIN:
11064 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11065 		if (!vsi->num_tx_desc)
11066 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11067 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11068 		if (!vsi->num_rx_desc)
11069 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11070 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11071 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11072 			vsi->num_q_vectors = pf->num_lan_msix;
11073 		else
11074 			vsi->num_q_vectors = 1;
11075 
11076 		break;
11077 
11078 	case I40E_VSI_FDIR:
11079 		vsi->alloc_queue_pairs = 1;
11080 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11081 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11082 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11083 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11084 		vsi->num_q_vectors = pf->num_fdsb_msix;
11085 		break;
11086 
11087 	case I40E_VSI_VMDQ2:
11088 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11089 		if (!vsi->num_tx_desc)
11090 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11091 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11092 		if (!vsi->num_rx_desc)
11093 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11094 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11095 		vsi->num_q_vectors = pf->num_vmdq_msix;
11096 		break;
11097 
11098 	case I40E_VSI_SRIOV:
11099 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11100 		if (!vsi->num_tx_desc)
11101 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11102 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11103 		if (!vsi->num_rx_desc)
11104 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11105 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11106 		break;
11107 
11108 	default:
11109 		WARN_ON(1);
11110 		return -ENODATA;
11111 	}
11112 
11113 	if (is_kdump_kernel()) {
11114 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11115 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11116 	}
11117 
11118 	return 0;
11119 }
11120 
11121 /**
11122  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11123  * @vsi: VSI pointer
11124  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11125  *
11126  * On error: returns error code (negative)
11127  * On success: returns 0
11128  **/
11129 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11130 {
11131 	struct i40e_ring **next_rings;
11132 	int size;
11133 	int ret = 0;
11134 
11135 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11136 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11137 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11138 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11139 	if (!vsi->tx_rings)
11140 		return -ENOMEM;
11141 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11142 	if (i40e_enabled_xdp_vsi(vsi)) {
11143 		vsi->xdp_rings = next_rings;
11144 		next_rings += vsi->alloc_queue_pairs;
11145 	}
11146 	vsi->rx_rings = next_rings;
11147 
11148 	if (alloc_qvectors) {
11149 		/* allocate memory for q_vector pointers */
11150 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11151 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11152 		if (!vsi->q_vectors) {
11153 			ret = -ENOMEM;
11154 			goto err_vectors;
11155 		}
11156 	}
11157 	return ret;
11158 
11159 err_vectors:
11160 	kfree(vsi->tx_rings);
11161 	return ret;
11162 }
11163 
11164 /**
11165  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11166  * @pf: board private structure
11167  * @type: type of VSI
11168  *
11169  * On error: returns error code (negative)
11170  * On success: returns vsi index in PF (positive)
11171  **/
11172 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11173 {
11174 	int ret = -ENODEV;
11175 	struct i40e_vsi *vsi;
11176 	int vsi_idx;
11177 	int i;
11178 
11179 	/* Need to protect the allocation of the VSIs at the PF level */
11180 	mutex_lock(&pf->switch_mutex);
11181 
11182 	/* VSI list may be fragmented if VSI creation/destruction has
11183 	 * been happening.  We can afford to do a quick scan to look
11184 	 * for any free VSIs in the list.
11185 	 *
11186 	 * find next empty vsi slot, looping back around if necessary
11187 	 */
11188 	i = pf->next_vsi;
11189 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11190 		i++;
11191 	if (i >= pf->num_alloc_vsi) {
11192 		i = 0;
11193 		while (i < pf->next_vsi && pf->vsi[i])
11194 			i++;
11195 	}
11196 
11197 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11198 		vsi_idx = i;             /* Found one! */
11199 	} else {
11200 		ret = -ENODEV;
11201 		goto unlock_pf;  /* out of VSI slots! */
11202 	}
11203 	pf->next_vsi = ++i;
11204 
11205 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11206 	if (!vsi) {
11207 		ret = -ENOMEM;
11208 		goto unlock_pf;
11209 	}
11210 	vsi->type = type;
11211 	vsi->back = pf;
11212 	set_bit(__I40E_VSI_DOWN, vsi->state);
11213 	vsi->flags = 0;
11214 	vsi->idx = vsi_idx;
11215 	vsi->int_rate_limit = 0;
11216 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11217 				pf->rss_table_size : 64;
11218 	vsi->netdev_registered = false;
11219 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11220 	hash_init(vsi->mac_filter_hash);
11221 	vsi->irqs_ready = false;
11222 
11223 	if (type == I40E_VSI_MAIN) {
11224 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11225 		if (!vsi->af_xdp_zc_qps)
11226 			goto err_rings;
11227 	}
11228 
11229 	ret = i40e_set_num_rings_in_vsi(vsi);
11230 	if (ret)
11231 		goto err_rings;
11232 
11233 	ret = i40e_vsi_alloc_arrays(vsi, true);
11234 	if (ret)
11235 		goto err_rings;
11236 
11237 	/* Setup default MSIX irq handler for VSI */
11238 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11239 
11240 	/* Initialize VSI lock */
11241 	spin_lock_init(&vsi->mac_filter_hash_lock);
11242 	pf->vsi[vsi_idx] = vsi;
11243 	ret = vsi_idx;
11244 	goto unlock_pf;
11245 
11246 err_rings:
11247 	bitmap_free(vsi->af_xdp_zc_qps);
11248 	pf->next_vsi = i - 1;
11249 	kfree(vsi);
11250 unlock_pf:
11251 	mutex_unlock(&pf->switch_mutex);
11252 	return ret;
11253 }
11254 
11255 /**
11256  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11257  * @vsi: VSI pointer
11258  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11259  *
11260  * On error: returns error code (negative)
11261  * On success: returns 0
11262  **/
11263 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11264 {
11265 	/* free the ring and vector containers */
11266 	if (free_qvectors) {
11267 		kfree(vsi->q_vectors);
11268 		vsi->q_vectors = NULL;
11269 	}
11270 	kfree(vsi->tx_rings);
11271 	vsi->tx_rings = NULL;
11272 	vsi->rx_rings = NULL;
11273 	vsi->xdp_rings = NULL;
11274 }
11275 
11276 /**
11277  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11278  * and lookup table
11279  * @vsi: Pointer to VSI structure
11280  */
11281 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11282 {
11283 	if (!vsi)
11284 		return;
11285 
11286 	kfree(vsi->rss_hkey_user);
11287 	vsi->rss_hkey_user = NULL;
11288 
11289 	kfree(vsi->rss_lut_user);
11290 	vsi->rss_lut_user = NULL;
11291 }
11292 
11293 /**
11294  * i40e_vsi_clear - Deallocate the VSI provided
11295  * @vsi: the VSI being un-configured
11296  **/
11297 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11298 {
11299 	struct i40e_pf *pf;
11300 
11301 	if (!vsi)
11302 		return 0;
11303 
11304 	if (!vsi->back)
11305 		goto free_vsi;
11306 	pf = vsi->back;
11307 
11308 	mutex_lock(&pf->switch_mutex);
11309 	if (!pf->vsi[vsi->idx]) {
11310 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11311 			vsi->idx, vsi->idx, vsi->type);
11312 		goto unlock_vsi;
11313 	}
11314 
11315 	if (pf->vsi[vsi->idx] != vsi) {
11316 		dev_err(&pf->pdev->dev,
11317 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11318 			pf->vsi[vsi->idx]->idx,
11319 			pf->vsi[vsi->idx]->type,
11320 			vsi->idx, vsi->type);
11321 		goto unlock_vsi;
11322 	}
11323 
11324 	/* updates the PF for this cleared vsi */
11325 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11326 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11327 
11328 	bitmap_free(vsi->af_xdp_zc_qps);
11329 	i40e_vsi_free_arrays(vsi, true);
11330 	i40e_clear_rss_config_user(vsi);
11331 
11332 	pf->vsi[vsi->idx] = NULL;
11333 	if (vsi->idx < pf->next_vsi)
11334 		pf->next_vsi = vsi->idx;
11335 
11336 unlock_vsi:
11337 	mutex_unlock(&pf->switch_mutex);
11338 free_vsi:
11339 	kfree(vsi);
11340 
11341 	return 0;
11342 }
11343 
11344 /**
11345  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11346  * @vsi: the VSI being cleaned
11347  **/
11348 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11349 {
11350 	int i;
11351 
11352 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11353 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11354 			kfree_rcu(vsi->tx_rings[i], rcu);
11355 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11356 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11357 			if (vsi->xdp_rings)
11358 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11359 		}
11360 	}
11361 }
11362 
11363 /**
11364  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11365  * @vsi: the VSI being configured
11366  **/
11367 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11368 {
11369 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11370 	struct i40e_pf *pf = vsi->back;
11371 	struct i40e_ring *ring;
11372 
11373 	/* Set basic values in the rings to be used later during open() */
11374 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11375 		/* allocate space for both Tx and Rx in one shot */
11376 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11377 		if (!ring)
11378 			goto err_out;
11379 
11380 		ring->queue_index = i;
11381 		ring->reg_idx = vsi->base_queue + i;
11382 		ring->ring_active = false;
11383 		ring->vsi = vsi;
11384 		ring->netdev = vsi->netdev;
11385 		ring->dev = &pf->pdev->dev;
11386 		ring->count = vsi->num_tx_desc;
11387 		ring->size = 0;
11388 		ring->dcb_tc = 0;
11389 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11390 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11391 		ring->itr_setting = pf->tx_itr_default;
11392 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11393 
11394 		if (!i40e_enabled_xdp_vsi(vsi))
11395 			goto setup_rx;
11396 
11397 		ring->queue_index = vsi->alloc_queue_pairs + i;
11398 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11399 		ring->ring_active = false;
11400 		ring->vsi = vsi;
11401 		ring->netdev = NULL;
11402 		ring->dev = &pf->pdev->dev;
11403 		ring->count = vsi->num_tx_desc;
11404 		ring->size = 0;
11405 		ring->dcb_tc = 0;
11406 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11407 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11408 		set_ring_xdp(ring);
11409 		ring->itr_setting = pf->tx_itr_default;
11410 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11411 
11412 setup_rx:
11413 		ring->queue_index = i;
11414 		ring->reg_idx = vsi->base_queue + i;
11415 		ring->ring_active = false;
11416 		ring->vsi = vsi;
11417 		ring->netdev = vsi->netdev;
11418 		ring->dev = &pf->pdev->dev;
11419 		ring->count = vsi->num_rx_desc;
11420 		ring->size = 0;
11421 		ring->dcb_tc = 0;
11422 		ring->itr_setting = pf->rx_itr_default;
11423 		WRITE_ONCE(vsi->rx_rings[i], ring);
11424 	}
11425 
11426 	return 0;
11427 
11428 err_out:
11429 	i40e_vsi_clear_rings(vsi);
11430 	return -ENOMEM;
11431 }
11432 
11433 /**
11434  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11435  * @pf: board private structure
11436  * @vectors: the number of MSI-X vectors to request
11437  *
11438  * Returns the number of vectors reserved, or error
11439  **/
11440 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11441 {
11442 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11443 					I40E_MIN_MSIX, vectors);
11444 	if (vectors < 0) {
11445 		dev_info(&pf->pdev->dev,
11446 			 "MSI-X vector reservation failed: %d\n", vectors);
11447 		vectors = 0;
11448 	}
11449 
11450 	return vectors;
11451 }
11452 
11453 /**
11454  * i40e_init_msix - Setup the MSIX capability
11455  * @pf: board private structure
11456  *
11457  * Work with the OS to set up the MSIX vectors needed.
11458  *
11459  * Returns the number of vectors reserved or negative on failure
11460  **/
11461 static int i40e_init_msix(struct i40e_pf *pf)
11462 {
11463 	struct i40e_hw *hw = &pf->hw;
11464 	int cpus, extra_vectors;
11465 	int vectors_left;
11466 	int v_budget, i;
11467 	int v_actual;
11468 	int iwarp_requested = 0;
11469 
11470 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11471 		return -ENODEV;
11472 
11473 	/* The number of vectors we'll request will be comprised of:
11474 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11475 	 *   - The number of LAN queue pairs
11476 	 *	- Queues being used for RSS.
11477 	 *		We don't need as many as max_rss_size vectors.
11478 	 *		use rss_size instead in the calculation since that
11479 	 *		is governed by number of cpus in the system.
11480 	 *	- assumes symmetric Tx/Rx pairing
11481 	 *   - The number of VMDq pairs
11482 	 *   - The CPU count within the NUMA node if iWARP is enabled
11483 	 * Once we count this up, try the request.
11484 	 *
11485 	 * If we can't get what we want, we'll simplify to nearly nothing
11486 	 * and try again.  If that still fails, we punt.
11487 	 */
11488 	vectors_left = hw->func_caps.num_msix_vectors;
11489 	v_budget = 0;
11490 
11491 	/* reserve one vector for miscellaneous handler */
11492 	if (vectors_left) {
11493 		v_budget++;
11494 		vectors_left--;
11495 	}
11496 
11497 	/* reserve some vectors for the main PF traffic queues. Initially we
11498 	 * only reserve at most 50% of the available vectors, in the case that
11499 	 * the number of online CPUs is large. This ensures that we can enable
11500 	 * extra features as well. Once we've enabled the other features, we
11501 	 * will use any remaining vectors to reach as close as we can to the
11502 	 * number of online CPUs.
11503 	 */
11504 	cpus = num_online_cpus();
11505 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11506 	vectors_left -= pf->num_lan_msix;
11507 
11508 	/* reserve one vector for sideband flow director */
11509 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11510 		if (vectors_left) {
11511 			pf->num_fdsb_msix = 1;
11512 			v_budget++;
11513 			vectors_left--;
11514 		} else {
11515 			pf->num_fdsb_msix = 0;
11516 		}
11517 	}
11518 
11519 	/* can we reserve enough for iWARP? */
11520 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11521 		iwarp_requested = pf->num_iwarp_msix;
11522 
11523 		if (!vectors_left)
11524 			pf->num_iwarp_msix = 0;
11525 		else if (vectors_left < pf->num_iwarp_msix)
11526 			pf->num_iwarp_msix = 1;
11527 		v_budget += pf->num_iwarp_msix;
11528 		vectors_left -= pf->num_iwarp_msix;
11529 	}
11530 
11531 	/* any vectors left over go for VMDq support */
11532 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11533 		if (!vectors_left) {
11534 			pf->num_vmdq_msix = 0;
11535 			pf->num_vmdq_qps = 0;
11536 		} else {
11537 			int vmdq_vecs_wanted =
11538 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11539 			int vmdq_vecs =
11540 				min_t(int, vectors_left, vmdq_vecs_wanted);
11541 
11542 			/* if we're short on vectors for what's desired, we limit
11543 			 * the queues per vmdq.  If this is still more than are
11544 			 * available, the user will need to change the number of
11545 			 * queues/vectors used by the PF later with the ethtool
11546 			 * channels command
11547 			 */
11548 			if (vectors_left < vmdq_vecs_wanted) {
11549 				pf->num_vmdq_qps = 1;
11550 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11551 				vmdq_vecs = min_t(int,
11552 						  vectors_left,
11553 						  vmdq_vecs_wanted);
11554 			}
11555 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11556 
11557 			v_budget += vmdq_vecs;
11558 			vectors_left -= vmdq_vecs;
11559 		}
11560 	}
11561 
11562 	/* On systems with a large number of SMP cores, we previously limited
11563 	 * the number of vectors for num_lan_msix to be at most 50% of the
11564 	 * available vectors, to allow for other features. Now, we add back
11565 	 * the remaining vectors. However, we ensure that the total
11566 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11567 	 * calculate the number of vectors we can add without going over the
11568 	 * cap of CPUs. For systems with a small number of CPUs this will be
11569 	 * zero.
11570 	 */
11571 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11572 	pf->num_lan_msix += extra_vectors;
11573 	vectors_left -= extra_vectors;
11574 
11575 	WARN(vectors_left < 0,
11576 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11577 
11578 	v_budget += pf->num_lan_msix;
11579 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11580 				   GFP_KERNEL);
11581 	if (!pf->msix_entries)
11582 		return -ENOMEM;
11583 
11584 	for (i = 0; i < v_budget; i++)
11585 		pf->msix_entries[i].entry = i;
11586 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11587 
11588 	if (v_actual < I40E_MIN_MSIX) {
11589 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11590 		kfree(pf->msix_entries);
11591 		pf->msix_entries = NULL;
11592 		pci_disable_msix(pf->pdev);
11593 		return -ENODEV;
11594 
11595 	} else if (v_actual == I40E_MIN_MSIX) {
11596 		/* Adjust for minimal MSIX use */
11597 		pf->num_vmdq_vsis = 0;
11598 		pf->num_vmdq_qps = 0;
11599 		pf->num_lan_qps = 1;
11600 		pf->num_lan_msix = 1;
11601 
11602 	} else if (v_actual != v_budget) {
11603 		/* If we have limited resources, we will start with no vectors
11604 		 * for the special features and then allocate vectors to some
11605 		 * of these features based on the policy and at the end disable
11606 		 * the features that did not get any vectors.
11607 		 */
11608 		int vec;
11609 
11610 		dev_info(&pf->pdev->dev,
11611 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11612 			 v_actual, v_budget);
11613 		/* reserve the misc vector */
11614 		vec = v_actual - 1;
11615 
11616 		/* Scale vector usage down */
11617 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11618 		pf->num_vmdq_vsis = 1;
11619 		pf->num_vmdq_qps = 1;
11620 
11621 		/* partition out the remaining vectors */
11622 		switch (vec) {
11623 		case 2:
11624 			pf->num_lan_msix = 1;
11625 			break;
11626 		case 3:
11627 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11628 				pf->num_lan_msix = 1;
11629 				pf->num_iwarp_msix = 1;
11630 			} else {
11631 				pf->num_lan_msix = 2;
11632 			}
11633 			break;
11634 		default:
11635 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11636 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11637 						 iwarp_requested);
11638 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11639 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11640 			} else {
11641 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11642 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11643 			}
11644 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11645 				pf->num_fdsb_msix = 1;
11646 				vec--;
11647 			}
11648 			pf->num_lan_msix = min_t(int,
11649 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11650 							      pf->num_lan_msix);
11651 			pf->num_lan_qps = pf->num_lan_msix;
11652 			break;
11653 		}
11654 	}
11655 
11656 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11657 	    (pf->num_fdsb_msix == 0)) {
11658 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11659 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11660 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11661 	}
11662 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11663 	    (pf->num_vmdq_msix == 0)) {
11664 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11665 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11666 	}
11667 
11668 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11669 	    (pf->num_iwarp_msix == 0)) {
11670 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11671 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11672 	}
11673 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11674 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11675 		   pf->num_lan_msix,
11676 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11677 		   pf->num_fdsb_msix,
11678 		   pf->num_iwarp_msix);
11679 
11680 	return v_actual;
11681 }
11682 
11683 /**
11684  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11685  * @vsi: the VSI being configured
11686  * @v_idx: index of the vector in the vsi struct
11687  *
11688  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11689  **/
11690 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11691 {
11692 	struct i40e_q_vector *q_vector;
11693 
11694 	/* allocate q_vector */
11695 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11696 	if (!q_vector)
11697 		return -ENOMEM;
11698 
11699 	q_vector->vsi = vsi;
11700 	q_vector->v_idx = v_idx;
11701 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11702 
11703 	if (vsi->netdev)
11704 		netif_napi_add(vsi->netdev, &q_vector->napi,
11705 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11706 
11707 	/* tie q_vector and vsi together */
11708 	vsi->q_vectors[v_idx] = q_vector;
11709 
11710 	return 0;
11711 }
11712 
11713 /**
11714  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11715  * @vsi: the VSI being configured
11716  *
11717  * We allocate one q_vector per queue interrupt.  If allocation fails we
11718  * return -ENOMEM.
11719  **/
11720 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11721 {
11722 	struct i40e_pf *pf = vsi->back;
11723 	int err, v_idx, num_q_vectors;
11724 
11725 	/* if not MSIX, give the one vector only to the LAN VSI */
11726 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11727 		num_q_vectors = vsi->num_q_vectors;
11728 	else if (vsi == pf->vsi[pf->lan_vsi])
11729 		num_q_vectors = 1;
11730 	else
11731 		return -EINVAL;
11732 
11733 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11734 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11735 		if (err)
11736 			goto err_out;
11737 	}
11738 
11739 	return 0;
11740 
11741 err_out:
11742 	while (v_idx--)
11743 		i40e_free_q_vector(vsi, v_idx);
11744 
11745 	return err;
11746 }
11747 
11748 /**
11749  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11750  * @pf: board private structure to initialize
11751  **/
11752 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11753 {
11754 	int vectors = 0;
11755 	ssize_t size;
11756 
11757 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11758 		vectors = i40e_init_msix(pf);
11759 		if (vectors < 0) {
11760 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11761 				       I40E_FLAG_IWARP_ENABLED	|
11762 				       I40E_FLAG_RSS_ENABLED	|
11763 				       I40E_FLAG_DCB_CAPABLE	|
11764 				       I40E_FLAG_DCB_ENABLED	|
11765 				       I40E_FLAG_SRIOV_ENABLED	|
11766 				       I40E_FLAG_FD_SB_ENABLED	|
11767 				       I40E_FLAG_FD_ATR_ENABLED	|
11768 				       I40E_FLAG_VMDQ_ENABLED);
11769 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11770 
11771 			/* rework the queue expectations without MSIX */
11772 			i40e_determine_queue_usage(pf);
11773 		}
11774 	}
11775 
11776 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11777 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11778 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11779 		vectors = pci_enable_msi(pf->pdev);
11780 		if (vectors < 0) {
11781 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11782 				 vectors);
11783 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11784 		}
11785 		vectors = 1;  /* one MSI or Legacy vector */
11786 	}
11787 
11788 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11789 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11790 
11791 	/* set up vector assignment tracking */
11792 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11793 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11794 	if (!pf->irq_pile)
11795 		return -ENOMEM;
11796 
11797 	pf->irq_pile->num_entries = vectors;
11798 
11799 	/* track first vector for misc interrupts, ignore return */
11800 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11801 
11802 	return 0;
11803 }
11804 
11805 /**
11806  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11807  * @pf: private board data structure
11808  *
11809  * Restore the interrupt scheme that was cleared when we suspended the
11810  * device. This should be called during resume to re-allocate the q_vectors
11811  * and reacquire IRQs.
11812  */
11813 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11814 {
11815 	int err, i;
11816 
11817 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11818 	 * scheme. We need to re-enabled them here in order to attempt to
11819 	 * re-acquire the MSI or MSI-X vectors
11820 	 */
11821 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11822 
11823 	err = i40e_init_interrupt_scheme(pf);
11824 	if (err)
11825 		return err;
11826 
11827 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11828 	 * rings together again.
11829 	 */
11830 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11831 		if (pf->vsi[i]) {
11832 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11833 			if (err)
11834 				goto err_unwind;
11835 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11836 		}
11837 	}
11838 
11839 	err = i40e_setup_misc_vector(pf);
11840 	if (err)
11841 		goto err_unwind;
11842 
11843 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11844 		i40e_client_update_msix_info(pf);
11845 
11846 	return 0;
11847 
11848 err_unwind:
11849 	while (i--) {
11850 		if (pf->vsi[i])
11851 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11852 	}
11853 
11854 	return err;
11855 }
11856 
11857 /**
11858  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11859  * non queue events in recovery mode
11860  * @pf: board private structure
11861  *
11862  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11863  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11864  * This is handled differently than in recovery mode since no Tx/Rx resources
11865  * are being allocated.
11866  **/
11867 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11868 {
11869 	int err;
11870 
11871 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11872 		err = i40e_setup_misc_vector(pf);
11873 
11874 		if (err) {
11875 			dev_info(&pf->pdev->dev,
11876 				 "MSI-X misc vector request failed, error %d\n",
11877 				 err);
11878 			return err;
11879 		}
11880 	} else {
11881 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11882 
11883 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11884 				  pf->int_name, pf);
11885 
11886 		if (err) {
11887 			dev_info(&pf->pdev->dev,
11888 				 "MSI/legacy misc vector request failed, error %d\n",
11889 				 err);
11890 			return err;
11891 		}
11892 		i40e_enable_misc_int_causes(pf);
11893 		i40e_irq_dynamic_enable_icr0(pf);
11894 	}
11895 
11896 	return 0;
11897 }
11898 
11899 /**
11900  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11901  * @pf: board private structure
11902  *
11903  * This sets up the handler for MSIX 0, which is used to manage the
11904  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11905  * when in MSI or Legacy interrupt mode.
11906  **/
11907 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11908 {
11909 	struct i40e_hw *hw = &pf->hw;
11910 	int err = 0;
11911 
11912 	/* Only request the IRQ once, the first time through. */
11913 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11914 		err = request_irq(pf->msix_entries[0].vector,
11915 				  i40e_intr, 0, pf->int_name, pf);
11916 		if (err) {
11917 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11918 			dev_info(&pf->pdev->dev,
11919 				 "request_irq for %s failed: %d\n",
11920 				 pf->int_name, err);
11921 			return -EFAULT;
11922 		}
11923 	}
11924 
11925 	i40e_enable_misc_int_causes(pf);
11926 
11927 	/* associate no queues to the misc vector */
11928 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11929 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11930 
11931 	i40e_flush(hw);
11932 
11933 	i40e_irq_dynamic_enable_icr0(pf);
11934 
11935 	return err;
11936 }
11937 
11938 /**
11939  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11940  * @vsi: Pointer to vsi structure
11941  * @seed: Buffter to store the hash keys
11942  * @lut: Buffer to store the lookup table entries
11943  * @lut_size: Size of buffer to store the lookup table entries
11944  *
11945  * Return 0 on success, negative on failure
11946  */
11947 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11948 			   u8 *lut, u16 lut_size)
11949 {
11950 	struct i40e_pf *pf = vsi->back;
11951 	struct i40e_hw *hw = &pf->hw;
11952 	int ret = 0;
11953 
11954 	if (seed) {
11955 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11956 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11957 		if (ret) {
11958 			dev_info(&pf->pdev->dev,
11959 				 "Cannot get RSS key, err %s aq_err %s\n",
11960 				 i40e_stat_str(&pf->hw, ret),
11961 				 i40e_aq_str(&pf->hw,
11962 					     pf->hw.aq.asq_last_status));
11963 			return ret;
11964 		}
11965 	}
11966 
11967 	if (lut) {
11968 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11969 
11970 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11971 		if (ret) {
11972 			dev_info(&pf->pdev->dev,
11973 				 "Cannot get RSS lut, err %s aq_err %s\n",
11974 				 i40e_stat_str(&pf->hw, ret),
11975 				 i40e_aq_str(&pf->hw,
11976 					     pf->hw.aq.asq_last_status));
11977 			return ret;
11978 		}
11979 	}
11980 
11981 	return ret;
11982 }
11983 
11984 /**
11985  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11986  * @vsi: Pointer to vsi structure
11987  * @seed: RSS hash seed
11988  * @lut: Lookup table
11989  * @lut_size: Lookup table size
11990  *
11991  * Returns 0 on success, negative on failure
11992  **/
11993 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11994 			       const u8 *lut, u16 lut_size)
11995 {
11996 	struct i40e_pf *pf = vsi->back;
11997 	struct i40e_hw *hw = &pf->hw;
11998 	u16 vf_id = vsi->vf_id;
11999 	u8 i;
12000 
12001 	/* Fill out hash function seed */
12002 	if (seed) {
12003 		u32 *seed_dw = (u32 *)seed;
12004 
12005 		if (vsi->type == I40E_VSI_MAIN) {
12006 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12007 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12008 		} else if (vsi->type == I40E_VSI_SRIOV) {
12009 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12010 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12011 		} else {
12012 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12013 		}
12014 	}
12015 
12016 	if (lut) {
12017 		u32 *lut_dw = (u32 *)lut;
12018 
12019 		if (vsi->type == I40E_VSI_MAIN) {
12020 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12021 				return -EINVAL;
12022 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12023 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12024 		} else if (vsi->type == I40E_VSI_SRIOV) {
12025 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12026 				return -EINVAL;
12027 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12028 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12029 		} else {
12030 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12031 		}
12032 	}
12033 	i40e_flush(hw);
12034 
12035 	return 0;
12036 }
12037 
12038 /**
12039  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12040  * @vsi: Pointer to VSI structure
12041  * @seed: Buffer to store the keys
12042  * @lut: Buffer to store the lookup table entries
12043  * @lut_size: Size of buffer to store the lookup table entries
12044  *
12045  * Returns 0 on success, negative on failure
12046  */
12047 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12048 			    u8 *lut, u16 lut_size)
12049 {
12050 	struct i40e_pf *pf = vsi->back;
12051 	struct i40e_hw *hw = &pf->hw;
12052 	u16 i;
12053 
12054 	if (seed) {
12055 		u32 *seed_dw = (u32 *)seed;
12056 
12057 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12058 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12059 	}
12060 	if (lut) {
12061 		u32 *lut_dw = (u32 *)lut;
12062 
12063 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12064 			return -EINVAL;
12065 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12066 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12067 	}
12068 
12069 	return 0;
12070 }
12071 
12072 /**
12073  * i40e_config_rss - Configure RSS keys and lut
12074  * @vsi: Pointer to VSI structure
12075  * @seed: RSS hash seed
12076  * @lut: Lookup table
12077  * @lut_size: Lookup table size
12078  *
12079  * Returns 0 on success, negative on failure
12080  */
12081 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12082 {
12083 	struct i40e_pf *pf = vsi->back;
12084 
12085 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12086 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12087 	else
12088 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12089 }
12090 
12091 /**
12092  * i40e_get_rss - Get RSS keys and lut
12093  * @vsi: Pointer to VSI structure
12094  * @seed: Buffer to store the keys
12095  * @lut: Buffer to store the lookup table entries
12096  * @lut_size: Size of buffer to store the lookup table entries
12097  *
12098  * Returns 0 on success, negative on failure
12099  */
12100 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12101 {
12102 	struct i40e_pf *pf = vsi->back;
12103 
12104 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12105 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12106 	else
12107 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12108 }
12109 
12110 /**
12111  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12112  * @pf: Pointer to board private structure
12113  * @lut: Lookup table
12114  * @rss_table_size: Lookup table size
12115  * @rss_size: Range of queue number for hashing
12116  */
12117 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12118 		       u16 rss_table_size, u16 rss_size)
12119 {
12120 	u16 i;
12121 
12122 	for (i = 0; i < rss_table_size; i++)
12123 		lut[i] = i % rss_size;
12124 }
12125 
12126 /**
12127  * i40e_pf_config_rss - Prepare for RSS if used
12128  * @pf: board private structure
12129  **/
12130 static int i40e_pf_config_rss(struct i40e_pf *pf)
12131 {
12132 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12133 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12134 	u8 *lut;
12135 	struct i40e_hw *hw = &pf->hw;
12136 	u32 reg_val;
12137 	u64 hena;
12138 	int ret;
12139 
12140 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12141 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12142 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12143 	hena |= i40e_pf_get_default_rss_hena(pf);
12144 
12145 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12146 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12147 
12148 	/* Determine the RSS table size based on the hardware capabilities */
12149 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12150 	reg_val = (pf->rss_table_size == 512) ?
12151 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12152 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12153 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12154 
12155 	/* Determine the RSS size of the VSI */
12156 	if (!vsi->rss_size) {
12157 		u16 qcount;
12158 		/* If the firmware does something weird during VSI init, we
12159 		 * could end up with zero TCs. Check for that to avoid
12160 		 * divide-by-zero. It probably won't pass traffic, but it also
12161 		 * won't panic.
12162 		 */
12163 		qcount = vsi->num_queue_pairs /
12164 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12165 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12166 	}
12167 	if (!vsi->rss_size)
12168 		return -EINVAL;
12169 
12170 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12171 	if (!lut)
12172 		return -ENOMEM;
12173 
12174 	/* Use user configured lut if there is one, otherwise use default */
12175 	if (vsi->rss_lut_user)
12176 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12177 	else
12178 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12179 
12180 	/* Use user configured hash key if there is one, otherwise
12181 	 * use default.
12182 	 */
12183 	if (vsi->rss_hkey_user)
12184 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12185 	else
12186 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12187 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12188 	kfree(lut);
12189 
12190 	return ret;
12191 }
12192 
12193 /**
12194  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12195  * @pf: board private structure
12196  * @queue_count: the requested queue count for rss.
12197  *
12198  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12199  * count which may be different from the requested queue count.
12200  * Note: expects to be called while under rtnl_lock()
12201  **/
12202 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12203 {
12204 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12205 	int new_rss_size;
12206 
12207 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12208 		return 0;
12209 
12210 	queue_count = min_t(int, queue_count, num_online_cpus());
12211 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12212 
12213 	if (queue_count != vsi->num_queue_pairs) {
12214 		u16 qcount;
12215 
12216 		vsi->req_queue_pairs = queue_count;
12217 		i40e_prep_for_reset(pf);
12218 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12219 			return pf->alloc_rss_size;
12220 
12221 		pf->alloc_rss_size = new_rss_size;
12222 
12223 		i40e_reset_and_rebuild(pf, true, true);
12224 
12225 		/* Discard the user configured hash keys and lut, if less
12226 		 * queues are enabled.
12227 		 */
12228 		if (queue_count < vsi->rss_size) {
12229 			i40e_clear_rss_config_user(vsi);
12230 			dev_dbg(&pf->pdev->dev,
12231 				"discard user configured hash keys and lut\n");
12232 		}
12233 
12234 		/* Reset vsi->rss_size, as number of enabled queues changed */
12235 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12236 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12237 
12238 		i40e_pf_config_rss(pf);
12239 	}
12240 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12241 		 vsi->req_queue_pairs, pf->rss_size_max);
12242 	return pf->alloc_rss_size;
12243 }
12244 
12245 /**
12246  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12247  * @pf: board private structure
12248  **/
12249 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12250 {
12251 	i40e_status status;
12252 	bool min_valid, max_valid;
12253 	u32 max_bw, min_bw;
12254 
12255 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12256 					   &min_valid, &max_valid);
12257 
12258 	if (!status) {
12259 		if (min_valid)
12260 			pf->min_bw = min_bw;
12261 		if (max_valid)
12262 			pf->max_bw = max_bw;
12263 	}
12264 
12265 	return status;
12266 }
12267 
12268 /**
12269  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12270  * @pf: board private structure
12271  **/
12272 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12273 {
12274 	struct i40e_aqc_configure_partition_bw_data bw_data;
12275 	i40e_status status;
12276 
12277 	memset(&bw_data, 0, sizeof(bw_data));
12278 
12279 	/* Set the valid bit for this PF */
12280 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12281 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12282 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12283 
12284 	/* Set the new bandwidths */
12285 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12286 
12287 	return status;
12288 }
12289 
12290 /**
12291  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12292  * @pf: board private structure
12293  **/
12294 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12295 {
12296 	/* Commit temporary BW setting to permanent NVM image */
12297 	enum i40e_admin_queue_err last_aq_status;
12298 	i40e_status ret;
12299 	u16 nvm_word;
12300 
12301 	if (pf->hw.partition_id != 1) {
12302 		dev_info(&pf->pdev->dev,
12303 			 "Commit BW only works on partition 1! This is partition %d",
12304 			 pf->hw.partition_id);
12305 		ret = I40E_NOT_SUPPORTED;
12306 		goto bw_commit_out;
12307 	}
12308 
12309 	/* Acquire NVM for read access */
12310 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12311 	last_aq_status = pf->hw.aq.asq_last_status;
12312 	if (ret) {
12313 		dev_info(&pf->pdev->dev,
12314 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12315 			 i40e_stat_str(&pf->hw, ret),
12316 			 i40e_aq_str(&pf->hw, last_aq_status));
12317 		goto bw_commit_out;
12318 	}
12319 
12320 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12321 	ret = i40e_aq_read_nvm(&pf->hw,
12322 			       I40E_SR_NVM_CONTROL_WORD,
12323 			       0x10, sizeof(nvm_word), &nvm_word,
12324 			       false, NULL);
12325 	/* Save off last admin queue command status before releasing
12326 	 * the NVM
12327 	 */
12328 	last_aq_status = pf->hw.aq.asq_last_status;
12329 	i40e_release_nvm(&pf->hw);
12330 	if (ret) {
12331 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12332 			 i40e_stat_str(&pf->hw, ret),
12333 			 i40e_aq_str(&pf->hw, last_aq_status));
12334 		goto bw_commit_out;
12335 	}
12336 
12337 	/* Wait a bit for NVM release to complete */
12338 	msleep(50);
12339 
12340 	/* Acquire NVM for write access */
12341 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12342 	last_aq_status = pf->hw.aq.asq_last_status;
12343 	if (ret) {
12344 		dev_info(&pf->pdev->dev,
12345 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12346 			 i40e_stat_str(&pf->hw, ret),
12347 			 i40e_aq_str(&pf->hw, last_aq_status));
12348 		goto bw_commit_out;
12349 	}
12350 	/* Write it back out unchanged to initiate update NVM,
12351 	 * which will force a write of the shadow (alt) RAM to
12352 	 * the NVM - thus storing the bandwidth values permanently.
12353 	 */
12354 	ret = i40e_aq_update_nvm(&pf->hw,
12355 				 I40E_SR_NVM_CONTROL_WORD,
12356 				 0x10, sizeof(nvm_word),
12357 				 &nvm_word, true, 0, NULL);
12358 	/* Save off last admin queue command status before releasing
12359 	 * the NVM
12360 	 */
12361 	last_aq_status = pf->hw.aq.asq_last_status;
12362 	i40e_release_nvm(&pf->hw);
12363 	if (ret)
12364 		dev_info(&pf->pdev->dev,
12365 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12366 			 i40e_stat_str(&pf->hw, ret),
12367 			 i40e_aq_str(&pf->hw, last_aq_status));
12368 bw_commit_out:
12369 
12370 	return ret;
12371 }
12372 
12373 /**
12374  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12375  * if total port shutdown feature is enabled for this PF
12376  * @pf: board private structure
12377  **/
12378 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12379 {
12380 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12381 #define I40E_FEATURES_ENABLE_PTR		0x2A
12382 #define I40E_CURRENT_SETTING_PTR		0x2B
12383 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12384 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12385 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12386 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12387 	i40e_status read_status = I40E_SUCCESS;
12388 	u16 sr_emp_sr_settings_ptr = 0;
12389 	u16 features_enable = 0;
12390 	u16 link_behavior = 0;
12391 	bool ret = false;
12392 
12393 	read_status = i40e_read_nvm_word(&pf->hw,
12394 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12395 					 &sr_emp_sr_settings_ptr);
12396 	if (read_status)
12397 		goto err_nvm;
12398 	read_status = i40e_read_nvm_word(&pf->hw,
12399 					 sr_emp_sr_settings_ptr +
12400 					 I40E_FEATURES_ENABLE_PTR,
12401 					 &features_enable);
12402 	if (read_status)
12403 		goto err_nvm;
12404 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12405 		read_status = i40e_read_nvm_module_data(&pf->hw,
12406 							I40E_SR_EMP_SR_SETTINGS_PTR,
12407 							I40E_CURRENT_SETTING_PTR,
12408 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12409 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12410 							&link_behavior);
12411 		if (read_status)
12412 			goto err_nvm;
12413 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12414 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12415 	}
12416 	return ret;
12417 
12418 err_nvm:
12419 	dev_warn(&pf->pdev->dev,
12420 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12421 		 i40e_stat_str(&pf->hw, read_status));
12422 	return ret;
12423 }
12424 
12425 /**
12426  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12427  * @pf: board private structure to initialize
12428  *
12429  * i40e_sw_init initializes the Adapter private data structure.
12430  * Fields are initialized based on PCI device information and
12431  * OS network device settings (MTU size).
12432  **/
12433 static int i40e_sw_init(struct i40e_pf *pf)
12434 {
12435 	int err = 0;
12436 	int size;
12437 	u16 pow;
12438 
12439 	/* Set default capability flags */
12440 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12441 		    I40E_FLAG_MSI_ENABLED     |
12442 		    I40E_FLAG_MSIX_ENABLED;
12443 
12444 	/* Set default ITR */
12445 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12446 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12447 
12448 	/* Depending on PF configurations, it is possible that the RSS
12449 	 * maximum might end up larger than the available queues
12450 	 */
12451 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12452 	pf->alloc_rss_size = 1;
12453 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12454 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12455 				 pf->hw.func_caps.num_tx_qp);
12456 
12457 	/* find the next higher power-of-2 of num cpus */
12458 	pow = roundup_pow_of_two(num_online_cpus());
12459 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12460 
12461 	if (pf->hw.func_caps.rss) {
12462 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12463 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12464 					   num_online_cpus());
12465 	}
12466 
12467 	/* MFP mode enabled */
12468 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12469 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12470 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12471 		if (i40e_get_partition_bw_setting(pf)) {
12472 			dev_warn(&pf->pdev->dev,
12473 				 "Could not get partition bw settings\n");
12474 		} else {
12475 			dev_info(&pf->pdev->dev,
12476 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12477 				 pf->min_bw, pf->max_bw);
12478 
12479 			/* nudge the Tx scheduler */
12480 			i40e_set_partition_bw_setting(pf);
12481 		}
12482 	}
12483 
12484 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12485 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12486 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12487 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12488 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12489 		    pf->hw.num_partitions > 1)
12490 			dev_info(&pf->pdev->dev,
12491 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12492 		else
12493 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12494 		pf->fdir_pf_filter_count =
12495 				 pf->hw.func_caps.fd_filters_guaranteed;
12496 		pf->hw.fdir_shared_filter_count =
12497 				 pf->hw.func_caps.fd_filters_best_effort;
12498 	}
12499 
12500 	if (pf->hw.mac.type == I40E_MAC_X722) {
12501 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12502 				    I40E_HW_128_QP_RSS_CAPABLE |
12503 				    I40E_HW_ATR_EVICT_CAPABLE |
12504 				    I40E_HW_WB_ON_ITR_CAPABLE |
12505 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12506 				    I40E_HW_NO_PCI_LINK_CHECK |
12507 				    I40E_HW_USE_SET_LLDP_MIB |
12508 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12509 				    I40E_HW_PTP_L4_CAPABLE |
12510 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12511 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12512 
12513 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12514 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12515 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12516 			dev_warn(&pf->pdev->dev,
12517 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12518 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12519 		}
12520 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12521 		   ((pf->hw.aq.api_maj_ver == 1) &&
12522 		    (pf->hw.aq.api_min_ver > 4))) {
12523 		/* Supported in FW API version higher than 1.4 */
12524 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12525 	}
12526 
12527 	/* Enable HW ATR eviction if possible */
12528 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12529 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12530 
12531 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12532 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12533 	    (pf->hw.aq.fw_maj_ver < 4))) {
12534 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12535 		/* No DCB support  for FW < v4.33 */
12536 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12537 	}
12538 
12539 	/* Disable FW LLDP if FW < v4.3 */
12540 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12541 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12542 	    (pf->hw.aq.fw_maj_ver < 4)))
12543 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12544 
12545 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12546 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12547 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12548 	    (pf->hw.aq.fw_maj_ver >= 5)))
12549 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12550 
12551 	/* Enable PTP L4 if FW > v6.0 */
12552 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12553 	    pf->hw.aq.fw_maj_ver >= 6)
12554 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12555 
12556 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12557 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12558 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12559 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12560 	}
12561 
12562 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12563 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12564 		/* IWARP needs one extra vector for CQP just like MISC.*/
12565 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12566 	}
12567 	/* Stopping FW LLDP engine is supported on XL710 and X722
12568 	 * starting from FW versions determined in i40e_init_adminq.
12569 	 * Stopping the FW LLDP engine is not supported on XL710
12570 	 * if NPAR is functioning so unset this hw flag in this case.
12571 	 */
12572 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12573 	    pf->hw.func_caps.npar_enable &&
12574 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12575 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12576 
12577 #ifdef CONFIG_PCI_IOV
12578 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12579 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12580 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12581 		pf->num_req_vfs = min_t(int,
12582 					pf->hw.func_caps.num_vfs,
12583 					I40E_MAX_VF_COUNT);
12584 	}
12585 #endif /* CONFIG_PCI_IOV */
12586 	pf->eeprom_version = 0xDEAD;
12587 	pf->lan_veb = I40E_NO_VEB;
12588 	pf->lan_vsi = I40E_NO_VSI;
12589 
12590 	/* By default FW has this off for performance reasons */
12591 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12592 
12593 	/* set up queue assignment tracking */
12594 	size = sizeof(struct i40e_lump_tracking)
12595 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12596 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12597 	if (!pf->qp_pile) {
12598 		err = -ENOMEM;
12599 		goto sw_init_done;
12600 	}
12601 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12602 
12603 	pf->tx_timeout_recovery_level = 1;
12604 
12605 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12606 	    i40e_is_total_port_shutdown_enabled(pf)) {
12607 		/* Link down on close must be on when total port shutdown
12608 		 * is enabled for a given port
12609 		 */
12610 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12611 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12612 		dev_info(&pf->pdev->dev,
12613 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12614 	}
12615 	mutex_init(&pf->switch_mutex);
12616 
12617 sw_init_done:
12618 	return err;
12619 }
12620 
12621 /**
12622  * i40e_set_ntuple - set the ntuple feature flag and take action
12623  * @pf: board private structure to initialize
12624  * @features: the feature set that the stack is suggesting
12625  *
12626  * returns a bool to indicate if reset needs to happen
12627  **/
12628 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12629 {
12630 	bool need_reset = false;
12631 
12632 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12633 	 * the state changed, we need to reset.
12634 	 */
12635 	if (features & NETIF_F_NTUPLE) {
12636 		/* Enable filters and mark for reset */
12637 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12638 			need_reset = true;
12639 		/* enable FD_SB only if there is MSI-X vector and no cloud
12640 		 * filters exist
12641 		 */
12642 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12643 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12644 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12645 		}
12646 	} else {
12647 		/* turn off filters, mark for reset and clear SW filter list */
12648 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12649 			need_reset = true;
12650 			i40e_fdir_filter_exit(pf);
12651 		}
12652 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12653 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12654 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12655 
12656 		/* reset fd counters */
12657 		pf->fd_add_err = 0;
12658 		pf->fd_atr_cnt = 0;
12659 		/* if ATR was auto disabled it can be re-enabled. */
12660 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12661 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12662 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12663 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12664 	}
12665 	return need_reset;
12666 }
12667 
12668 /**
12669  * i40e_clear_rss_lut - clear the rx hash lookup table
12670  * @vsi: the VSI being configured
12671  **/
12672 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12673 {
12674 	struct i40e_pf *pf = vsi->back;
12675 	struct i40e_hw *hw = &pf->hw;
12676 	u16 vf_id = vsi->vf_id;
12677 	u8 i;
12678 
12679 	if (vsi->type == I40E_VSI_MAIN) {
12680 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12681 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12682 	} else if (vsi->type == I40E_VSI_SRIOV) {
12683 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12684 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12685 	} else {
12686 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12687 	}
12688 }
12689 
12690 /**
12691  * i40e_set_features - set the netdev feature flags
12692  * @netdev: ptr to the netdev being adjusted
12693  * @features: the feature set that the stack is suggesting
12694  * Note: expects to be called while under rtnl_lock()
12695  **/
12696 static int i40e_set_features(struct net_device *netdev,
12697 			     netdev_features_t features)
12698 {
12699 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12700 	struct i40e_vsi *vsi = np->vsi;
12701 	struct i40e_pf *pf = vsi->back;
12702 	bool need_reset;
12703 
12704 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12705 		i40e_pf_config_rss(pf);
12706 	else if (!(features & NETIF_F_RXHASH) &&
12707 		 netdev->features & NETIF_F_RXHASH)
12708 		i40e_clear_rss_lut(vsi);
12709 
12710 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12711 		i40e_vlan_stripping_enable(vsi);
12712 	else
12713 		i40e_vlan_stripping_disable(vsi);
12714 
12715 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12716 		dev_err(&pf->pdev->dev,
12717 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12718 		return -EINVAL;
12719 	}
12720 
12721 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12722 		i40e_del_all_macvlans(vsi);
12723 
12724 	need_reset = i40e_set_ntuple(pf, features);
12725 
12726 	if (need_reset)
12727 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12728 
12729 	return 0;
12730 }
12731 
12732 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12733 				    unsigned int table, unsigned int idx,
12734 				    struct udp_tunnel_info *ti)
12735 {
12736 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12737 	struct i40e_hw *hw = &np->vsi->back->hw;
12738 	u8 type, filter_index;
12739 	i40e_status ret;
12740 
12741 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12742 						   I40E_AQC_TUNNEL_TYPE_NGE;
12743 
12744 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12745 				     NULL);
12746 	if (ret) {
12747 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12748 			    i40e_stat_str(hw, ret),
12749 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12750 		return -EIO;
12751 	}
12752 
12753 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12754 	return 0;
12755 }
12756 
12757 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12758 				      unsigned int table, unsigned int idx,
12759 				      struct udp_tunnel_info *ti)
12760 {
12761 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12762 	struct i40e_hw *hw = &np->vsi->back->hw;
12763 	i40e_status ret;
12764 
12765 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12766 	if (ret) {
12767 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12768 			    i40e_stat_str(hw, ret),
12769 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12770 		return -EIO;
12771 	}
12772 
12773 	return 0;
12774 }
12775 
12776 static int i40e_get_phys_port_id(struct net_device *netdev,
12777 				 struct netdev_phys_item_id *ppid)
12778 {
12779 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12780 	struct i40e_pf *pf = np->vsi->back;
12781 	struct i40e_hw *hw = &pf->hw;
12782 
12783 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12784 		return -EOPNOTSUPP;
12785 
12786 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12787 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12788 
12789 	return 0;
12790 }
12791 
12792 /**
12793  * i40e_ndo_fdb_add - add an entry to the hardware database
12794  * @ndm: the input from the stack
12795  * @tb: pointer to array of nladdr (unused)
12796  * @dev: the net device pointer
12797  * @addr: the MAC address entry being added
12798  * @vid: VLAN ID
12799  * @flags: instructions from stack about fdb operation
12800  * @extack: netlink extended ack, unused currently
12801  */
12802 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12803 			    struct net_device *dev,
12804 			    const unsigned char *addr, u16 vid,
12805 			    u16 flags,
12806 			    struct netlink_ext_ack *extack)
12807 {
12808 	struct i40e_netdev_priv *np = netdev_priv(dev);
12809 	struct i40e_pf *pf = np->vsi->back;
12810 	int err = 0;
12811 
12812 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12813 		return -EOPNOTSUPP;
12814 
12815 	if (vid) {
12816 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12817 		return -EINVAL;
12818 	}
12819 
12820 	/* Hardware does not support aging addresses so if a
12821 	 * ndm_state is given only allow permanent addresses
12822 	 */
12823 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12824 		netdev_info(dev, "FDB only supports static addresses\n");
12825 		return -EINVAL;
12826 	}
12827 
12828 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12829 		err = dev_uc_add_excl(dev, addr);
12830 	else if (is_multicast_ether_addr(addr))
12831 		err = dev_mc_add_excl(dev, addr);
12832 	else
12833 		err = -EINVAL;
12834 
12835 	/* Only return duplicate errors if NLM_F_EXCL is set */
12836 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12837 		err = 0;
12838 
12839 	return err;
12840 }
12841 
12842 /**
12843  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12844  * @dev: the netdev being configured
12845  * @nlh: RTNL message
12846  * @flags: bridge flags
12847  * @extack: netlink extended ack
12848  *
12849  * Inserts a new hardware bridge if not already created and
12850  * enables the bridging mode requested (VEB or VEPA). If the
12851  * hardware bridge has already been inserted and the request
12852  * is to change the mode then that requires a PF reset to
12853  * allow rebuild of the components with required hardware
12854  * bridge mode enabled.
12855  *
12856  * Note: expects to be called while under rtnl_lock()
12857  **/
12858 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12859 				   struct nlmsghdr *nlh,
12860 				   u16 flags,
12861 				   struct netlink_ext_ack *extack)
12862 {
12863 	struct i40e_netdev_priv *np = netdev_priv(dev);
12864 	struct i40e_vsi *vsi = np->vsi;
12865 	struct i40e_pf *pf = vsi->back;
12866 	struct i40e_veb *veb = NULL;
12867 	struct nlattr *attr, *br_spec;
12868 	int i, rem;
12869 
12870 	/* Only for PF VSI for now */
12871 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12872 		return -EOPNOTSUPP;
12873 
12874 	/* Find the HW bridge for PF VSI */
12875 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12876 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12877 			veb = pf->veb[i];
12878 	}
12879 
12880 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12881 
12882 	nla_for_each_nested(attr, br_spec, rem) {
12883 		__u16 mode;
12884 
12885 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12886 			continue;
12887 
12888 		mode = nla_get_u16(attr);
12889 		if ((mode != BRIDGE_MODE_VEPA) &&
12890 		    (mode != BRIDGE_MODE_VEB))
12891 			return -EINVAL;
12892 
12893 		/* Insert a new HW bridge */
12894 		if (!veb) {
12895 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12896 					     vsi->tc_config.enabled_tc);
12897 			if (veb) {
12898 				veb->bridge_mode = mode;
12899 				i40e_config_bridge_mode(veb);
12900 			} else {
12901 				/* No Bridge HW offload available */
12902 				return -ENOENT;
12903 			}
12904 			break;
12905 		} else if (mode != veb->bridge_mode) {
12906 			/* Existing HW bridge but different mode needs reset */
12907 			veb->bridge_mode = mode;
12908 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12909 			if (mode == BRIDGE_MODE_VEB)
12910 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12911 			else
12912 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12913 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12914 			break;
12915 		}
12916 	}
12917 
12918 	return 0;
12919 }
12920 
12921 /**
12922  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12923  * @skb: skb buff
12924  * @pid: process id
12925  * @seq: RTNL message seq #
12926  * @dev: the netdev being configured
12927  * @filter_mask: unused
12928  * @nlflags: netlink flags passed in
12929  *
12930  * Return the mode in which the hardware bridge is operating in
12931  * i.e VEB or VEPA.
12932  **/
12933 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12934 				   struct net_device *dev,
12935 				   u32 __always_unused filter_mask,
12936 				   int nlflags)
12937 {
12938 	struct i40e_netdev_priv *np = netdev_priv(dev);
12939 	struct i40e_vsi *vsi = np->vsi;
12940 	struct i40e_pf *pf = vsi->back;
12941 	struct i40e_veb *veb = NULL;
12942 	int i;
12943 
12944 	/* Only for PF VSI for now */
12945 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12946 		return -EOPNOTSUPP;
12947 
12948 	/* Find the HW bridge for the PF VSI */
12949 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12950 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12951 			veb = pf->veb[i];
12952 	}
12953 
12954 	if (!veb)
12955 		return 0;
12956 
12957 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12958 				       0, 0, nlflags, filter_mask, NULL);
12959 }
12960 
12961 /**
12962  * i40e_features_check - Validate encapsulated packet conforms to limits
12963  * @skb: skb buff
12964  * @dev: This physical port's netdev
12965  * @features: Offload features that the stack believes apply
12966  **/
12967 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12968 					     struct net_device *dev,
12969 					     netdev_features_t features)
12970 {
12971 	size_t len;
12972 
12973 	/* No point in doing any of this if neither checksum nor GSO are
12974 	 * being requested for this frame.  We can rule out both by just
12975 	 * checking for CHECKSUM_PARTIAL
12976 	 */
12977 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12978 		return features;
12979 
12980 	/* We cannot support GSO if the MSS is going to be less than
12981 	 * 64 bytes.  If it is then we need to drop support for GSO.
12982 	 */
12983 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12984 		features &= ~NETIF_F_GSO_MASK;
12985 
12986 	/* MACLEN can support at most 63 words */
12987 	len = skb_network_header(skb) - skb->data;
12988 	if (len & ~(63 * 2))
12989 		goto out_err;
12990 
12991 	/* IPLEN and EIPLEN can support at most 127 dwords */
12992 	len = skb_transport_header(skb) - skb_network_header(skb);
12993 	if (len & ~(127 * 4))
12994 		goto out_err;
12995 
12996 	if (skb->encapsulation) {
12997 		/* L4TUNLEN can support 127 words */
12998 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12999 		if (len & ~(127 * 2))
13000 			goto out_err;
13001 
13002 		/* IPLEN can support at most 127 dwords */
13003 		len = skb_inner_transport_header(skb) -
13004 		      skb_inner_network_header(skb);
13005 		if (len & ~(127 * 4))
13006 			goto out_err;
13007 	}
13008 
13009 	/* No need to validate L4LEN as TCP is the only protocol with a
13010 	 * a flexible value and we support all possible values supported
13011 	 * by TCP, which is at most 15 dwords
13012 	 */
13013 
13014 	return features;
13015 out_err:
13016 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13017 }
13018 
13019 /**
13020  * i40e_xdp_setup - add/remove an XDP program
13021  * @vsi: VSI to changed
13022  * @prog: XDP program
13023  * @extack: netlink extended ack
13024  **/
13025 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13026 			  struct netlink_ext_ack *extack)
13027 {
13028 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13029 	struct i40e_pf *pf = vsi->back;
13030 	struct bpf_prog *old_prog;
13031 	bool need_reset;
13032 	int i;
13033 
13034 	/* Don't allow frames that span over multiple buffers */
13035 	if (frame_size > vsi->rx_buf_len) {
13036 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13037 		return -EINVAL;
13038 	}
13039 
13040 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13041 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13042 
13043 	if (need_reset)
13044 		i40e_prep_for_reset(pf);
13045 
13046 	/* VSI shall be deleted in a moment, just return EINVAL */
13047 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13048 		return -EINVAL;
13049 
13050 	old_prog = xchg(&vsi->xdp_prog, prog);
13051 
13052 	if (need_reset) {
13053 		if (!prog)
13054 			/* Wait until ndo_xsk_wakeup completes. */
13055 			synchronize_rcu();
13056 		i40e_reset_and_rebuild(pf, true, true);
13057 	}
13058 
13059 	for (i = 0; i < vsi->num_queue_pairs; i++)
13060 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13061 
13062 	if (old_prog)
13063 		bpf_prog_put(old_prog);
13064 
13065 	/* Kick start the NAPI context if there is an AF_XDP socket open
13066 	 * on that queue id. This so that receiving will start.
13067 	 */
13068 	if (need_reset && prog)
13069 		for (i = 0; i < vsi->num_queue_pairs; i++)
13070 			if (vsi->xdp_rings[i]->xsk_pool)
13071 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13072 						      XDP_WAKEUP_RX);
13073 
13074 	return 0;
13075 }
13076 
13077 /**
13078  * i40e_enter_busy_conf - Enters busy config state
13079  * @vsi: vsi
13080  *
13081  * Returns 0 on success, <0 for failure.
13082  **/
13083 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13084 {
13085 	struct i40e_pf *pf = vsi->back;
13086 	int timeout = 50;
13087 
13088 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13089 		timeout--;
13090 		if (!timeout)
13091 			return -EBUSY;
13092 		usleep_range(1000, 2000);
13093 	}
13094 
13095 	return 0;
13096 }
13097 
13098 /**
13099  * i40e_exit_busy_conf - Exits busy config state
13100  * @vsi: vsi
13101  **/
13102 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13103 {
13104 	struct i40e_pf *pf = vsi->back;
13105 
13106 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13107 }
13108 
13109 /**
13110  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13111  * @vsi: vsi
13112  * @queue_pair: queue pair
13113  **/
13114 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13115 {
13116 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13117 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13118 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13119 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13120 	if (i40e_enabled_xdp_vsi(vsi)) {
13121 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13122 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13123 	}
13124 }
13125 
13126 /**
13127  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13128  * @vsi: vsi
13129  * @queue_pair: queue pair
13130  **/
13131 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13132 {
13133 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13134 	if (i40e_enabled_xdp_vsi(vsi)) {
13135 		/* Make sure that in-progress ndo_xdp_xmit calls are
13136 		 * completed.
13137 		 */
13138 		synchronize_rcu();
13139 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13140 	}
13141 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13142 }
13143 
13144 /**
13145  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13146  * @vsi: vsi
13147  * @queue_pair: queue pair
13148  * @enable: true for enable, false for disable
13149  **/
13150 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13151 					bool enable)
13152 {
13153 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13154 	struct i40e_q_vector *q_vector = rxr->q_vector;
13155 
13156 	if (!vsi->netdev)
13157 		return;
13158 
13159 	/* All rings in a qp belong to the same qvector. */
13160 	if (q_vector->rx.ring || q_vector->tx.ring) {
13161 		if (enable)
13162 			napi_enable(&q_vector->napi);
13163 		else
13164 			napi_disable(&q_vector->napi);
13165 	}
13166 }
13167 
13168 /**
13169  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13170  * @vsi: vsi
13171  * @queue_pair: queue pair
13172  * @enable: true for enable, false for disable
13173  *
13174  * Returns 0 on success, <0 on failure.
13175  **/
13176 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13177 					bool enable)
13178 {
13179 	struct i40e_pf *pf = vsi->back;
13180 	int pf_q, ret = 0;
13181 
13182 	pf_q = vsi->base_queue + queue_pair;
13183 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13184 				     false /*is xdp*/, enable);
13185 	if (ret) {
13186 		dev_info(&pf->pdev->dev,
13187 			 "VSI seid %d Tx ring %d %sable timeout\n",
13188 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13189 		return ret;
13190 	}
13191 
13192 	i40e_control_rx_q(pf, pf_q, enable);
13193 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13194 	if (ret) {
13195 		dev_info(&pf->pdev->dev,
13196 			 "VSI seid %d Rx ring %d %sable timeout\n",
13197 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13198 		return ret;
13199 	}
13200 
13201 	/* Due to HW errata, on Rx disable only, the register can
13202 	 * indicate done before it really is. Needs 50ms to be sure
13203 	 */
13204 	if (!enable)
13205 		mdelay(50);
13206 
13207 	if (!i40e_enabled_xdp_vsi(vsi))
13208 		return ret;
13209 
13210 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13211 				     pf_q + vsi->alloc_queue_pairs,
13212 				     true /*is xdp*/, enable);
13213 	if (ret) {
13214 		dev_info(&pf->pdev->dev,
13215 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13216 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13217 	}
13218 
13219 	return ret;
13220 }
13221 
13222 /**
13223  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13224  * @vsi: vsi
13225  * @queue_pair: queue_pair
13226  **/
13227 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13228 {
13229 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13230 	struct i40e_pf *pf = vsi->back;
13231 	struct i40e_hw *hw = &pf->hw;
13232 
13233 	/* All rings in a qp belong to the same qvector. */
13234 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13235 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13236 	else
13237 		i40e_irq_dynamic_enable_icr0(pf);
13238 
13239 	i40e_flush(hw);
13240 }
13241 
13242 /**
13243  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13244  * @vsi: vsi
13245  * @queue_pair: queue_pair
13246  **/
13247 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13248 {
13249 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13250 	struct i40e_pf *pf = vsi->back;
13251 	struct i40e_hw *hw = &pf->hw;
13252 
13253 	/* For simplicity, instead of removing the qp interrupt causes
13254 	 * from the interrupt linked list, we simply disable the interrupt, and
13255 	 * leave the list intact.
13256 	 *
13257 	 * All rings in a qp belong to the same qvector.
13258 	 */
13259 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13260 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13261 
13262 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13263 		i40e_flush(hw);
13264 		synchronize_irq(pf->msix_entries[intpf].vector);
13265 	} else {
13266 		/* Legacy and MSI mode - this stops all interrupt handling */
13267 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13268 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13269 		i40e_flush(hw);
13270 		synchronize_irq(pf->pdev->irq);
13271 	}
13272 }
13273 
13274 /**
13275  * i40e_queue_pair_disable - Disables a queue pair
13276  * @vsi: vsi
13277  * @queue_pair: queue pair
13278  *
13279  * Returns 0 on success, <0 on failure.
13280  **/
13281 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13282 {
13283 	int err;
13284 
13285 	err = i40e_enter_busy_conf(vsi);
13286 	if (err)
13287 		return err;
13288 
13289 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13290 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13291 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13292 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13293 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13294 
13295 	return err;
13296 }
13297 
13298 /**
13299  * i40e_queue_pair_enable - Enables a queue pair
13300  * @vsi: vsi
13301  * @queue_pair: queue pair
13302  *
13303  * Returns 0 on success, <0 on failure.
13304  **/
13305 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13306 {
13307 	int err;
13308 
13309 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13310 	if (err)
13311 		return err;
13312 
13313 	if (i40e_enabled_xdp_vsi(vsi)) {
13314 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13315 		if (err)
13316 			return err;
13317 	}
13318 
13319 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13320 	if (err)
13321 		return err;
13322 
13323 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13324 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13325 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13326 
13327 	i40e_exit_busy_conf(vsi);
13328 
13329 	return err;
13330 }
13331 
13332 /**
13333  * i40e_xdp - implements ndo_bpf for i40e
13334  * @dev: netdevice
13335  * @xdp: XDP command
13336  **/
13337 static int i40e_xdp(struct net_device *dev,
13338 		    struct netdev_bpf *xdp)
13339 {
13340 	struct i40e_netdev_priv *np = netdev_priv(dev);
13341 	struct i40e_vsi *vsi = np->vsi;
13342 
13343 	if (vsi->type != I40E_VSI_MAIN)
13344 		return -EINVAL;
13345 
13346 	switch (xdp->command) {
13347 	case XDP_SETUP_PROG:
13348 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13349 	case XDP_SETUP_XSK_POOL:
13350 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13351 					   xdp->xsk.queue_id);
13352 	default:
13353 		return -EINVAL;
13354 	}
13355 }
13356 
13357 static const struct net_device_ops i40e_netdev_ops = {
13358 	.ndo_open		= i40e_open,
13359 	.ndo_stop		= i40e_close,
13360 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13361 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13362 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13363 	.ndo_validate_addr	= eth_validate_addr,
13364 	.ndo_set_mac_address	= i40e_set_mac,
13365 	.ndo_change_mtu		= i40e_change_mtu,
13366 	.ndo_eth_ioctl		= i40e_ioctl,
13367 	.ndo_tx_timeout		= i40e_tx_timeout,
13368 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13369 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13370 #ifdef CONFIG_NET_POLL_CONTROLLER
13371 	.ndo_poll_controller	= i40e_netpoll,
13372 #endif
13373 	.ndo_setup_tc		= __i40e_setup_tc,
13374 	.ndo_select_queue	= i40e_lan_select_queue,
13375 	.ndo_set_features	= i40e_set_features,
13376 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13377 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13378 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13379 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13380 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13381 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13382 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13383 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13384 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13385 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13386 	.ndo_features_check	= i40e_features_check,
13387 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13388 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13389 	.ndo_bpf		= i40e_xdp,
13390 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13391 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13392 	.ndo_dfwd_add_station	= i40e_fwd_add,
13393 	.ndo_dfwd_del_station	= i40e_fwd_del,
13394 };
13395 
13396 /**
13397  * i40e_config_netdev - Setup the netdev flags
13398  * @vsi: the VSI being configured
13399  *
13400  * Returns 0 on success, negative value on failure
13401  **/
13402 static int i40e_config_netdev(struct i40e_vsi *vsi)
13403 {
13404 	struct i40e_pf *pf = vsi->back;
13405 	struct i40e_hw *hw = &pf->hw;
13406 	struct i40e_netdev_priv *np;
13407 	struct net_device *netdev;
13408 	u8 broadcast[ETH_ALEN];
13409 	u8 mac_addr[ETH_ALEN];
13410 	int etherdev_size;
13411 	netdev_features_t hw_enc_features;
13412 	netdev_features_t hw_features;
13413 
13414 	etherdev_size = sizeof(struct i40e_netdev_priv);
13415 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13416 	if (!netdev)
13417 		return -ENOMEM;
13418 
13419 	vsi->netdev = netdev;
13420 	np = netdev_priv(netdev);
13421 	np->vsi = vsi;
13422 
13423 	hw_enc_features = NETIF_F_SG			|
13424 			  NETIF_F_IP_CSUM		|
13425 			  NETIF_F_IPV6_CSUM		|
13426 			  NETIF_F_HIGHDMA		|
13427 			  NETIF_F_SOFT_FEATURES		|
13428 			  NETIF_F_TSO			|
13429 			  NETIF_F_TSO_ECN		|
13430 			  NETIF_F_TSO6			|
13431 			  NETIF_F_GSO_GRE		|
13432 			  NETIF_F_GSO_GRE_CSUM		|
13433 			  NETIF_F_GSO_PARTIAL		|
13434 			  NETIF_F_GSO_IPXIP4		|
13435 			  NETIF_F_GSO_IPXIP6		|
13436 			  NETIF_F_GSO_UDP_TUNNEL	|
13437 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13438 			  NETIF_F_GSO_UDP_L4		|
13439 			  NETIF_F_SCTP_CRC		|
13440 			  NETIF_F_RXHASH		|
13441 			  NETIF_F_RXCSUM		|
13442 			  0;
13443 
13444 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13445 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13446 
13447 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13448 
13449 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13450 
13451 	netdev->hw_enc_features |= hw_enc_features;
13452 
13453 	/* record features VLANs can make use of */
13454 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13455 
13456 	/* enable macvlan offloads */
13457 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13458 
13459 	hw_features = hw_enc_features		|
13460 		      NETIF_F_HW_VLAN_CTAG_TX	|
13461 		      NETIF_F_HW_VLAN_CTAG_RX;
13462 
13463 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13464 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13465 
13466 	netdev->hw_features |= hw_features;
13467 
13468 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13469 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13470 
13471 	if (vsi->type == I40E_VSI_MAIN) {
13472 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13473 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13474 		/* The following steps are necessary for two reasons. First,
13475 		 * some older NVM configurations load a default MAC-VLAN
13476 		 * filter that will accept any tagged packet, and we want to
13477 		 * replace this with a normal filter. Additionally, it is
13478 		 * possible our MAC address was provided by the platform using
13479 		 * Open Firmware or similar.
13480 		 *
13481 		 * Thus, we need to remove the default filter and install one
13482 		 * specific to the MAC address.
13483 		 */
13484 		i40e_rm_default_mac_filter(vsi, mac_addr);
13485 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13486 		i40e_add_mac_filter(vsi, mac_addr);
13487 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13488 	} else {
13489 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13490 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13491 		 * the end, which is 4 bytes long, so force truncation of the
13492 		 * original name by IFNAMSIZ - 4
13493 		 */
13494 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13495 			 IFNAMSIZ - 4,
13496 			 pf->vsi[pf->lan_vsi]->netdev->name);
13497 		eth_random_addr(mac_addr);
13498 
13499 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13500 		i40e_add_mac_filter(vsi, mac_addr);
13501 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13502 	}
13503 
13504 	/* Add the broadcast filter so that we initially will receive
13505 	 * broadcast packets. Note that when a new VLAN is first added the
13506 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13507 	 * specific filters as part of transitioning into "vlan" operation.
13508 	 * When more VLANs are added, the driver will copy each existing MAC
13509 	 * filter and add it for the new VLAN.
13510 	 *
13511 	 * Broadcast filters are handled specially by
13512 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13513 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13514 	 * filter. The subtask will update the correct broadcast promiscuous
13515 	 * bits as VLANs become active or inactive.
13516 	 */
13517 	eth_broadcast_addr(broadcast);
13518 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13519 	i40e_add_mac_filter(vsi, broadcast);
13520 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13521 
13522 	eth_hw_addr_set(netdev, mac_addr);
13523 	ether_addr_copy(netdev->perm_addr, mac_addr);
13524 
13525 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13526 	netdev->neigh_priv_len = sizeof(u32) * 4;
13527 
13528 	netdev->priv_flags |= IFF_UNICAST_FLT;
13529 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13530 	/* Setup netdev TC information */
13531 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13532 
13533 	netdev->netdev_ops = &i40e_netdev_ops;
13534 	netdev->watchdog_timeo = 5 * HZ;
13535 	i40e_set_ethtool_ops(netdev);
13536 
13537 	/* MTU range: 68 - 9706 */
13538 	netdev->min_mtu = ETH_MIN_MTU;
13539 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13540 
13541 	return 0;
13542 }
13543 
13544 /**
13545  * i40e_vsi_delete - Delete a VSI from the switch
13546  * @vsi: the VSI being removed
13547  *
13548  * Returns 0 on success, negative value on failure
13549  **/
13550 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13551 {
13552 	/* remove default VSI is not allowed */
13553 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13554 		return;
13555 
13556 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13557 }
13558 
13559 /**
13560  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13561  * @vsi: the VSI being queried
13562  *
13563  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13564  **/
13565 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13566 {
13567 	struct i40e_veb *veb;
13568 	struct i40e_pf *pf = vsi->back;
13569 
13570 	/* Uplink is not a bridge so default to VEB */
13571 	if (vsi->veb_idx >= I40E_MAX_VEB)
13572 		return 1;
13573 
13574 	veb = pf->veb[vsi->veb_idx];
13575 	if (!veb) {
13576 		dev_info(&pf->pdev->dev,
13577 			 "There is no veb associated with the bridge\n");
13578 		return -ENOENT;
13579 	}
13580 
13581 	/* Uplink is a bridge in VEPA mode */
13582 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13583 		return 0;
13584 	} else {
13585 		/* Uplink is a bridge in VEB mode */
13586 		return 1;
13587 	}
13588 
13589 	/* VEPA is now default bridge, so return 0 */
13590 	return 0;
13591 }
13592 
13593 /**
13594  * i40e_add_vsi - Add a VSI to the switch
13595  * @vsi: the VSI being configured
13596  *
13597  * This initializes a VSI context depending on the VSI type to be added and
13598  * passes it down to the add_vsi aq command.
13599  **/
13600 static int i40e_add_vsi(struct i40e_vsi *vsi)
13601 {
13602 	int ret = -ENODEV;
13603 	struct i40e_pf *pf = vsi->back;
13604 	struct i40e_hw *hw = &pf->hw;
13605 	struct i40e_vsi_context ctxt;
13606 	struct i40e_mac_filter *f;
13607 	struct hlist_node *h;
13608 	int bkt;
13609 
13610 	u8 enabled_tc = 0x1; /* TC0 enabled */
13611 	int f_count = 0;
13612 
13613 	memset(&ctxt, 0, sizeof(ctxt));
13614 	switch (vsi->type) {
13615 	case I40E_VSI_MAIN:
13616 		/* The PF's main VSI is already setup as part of the
13617 		 * device initialization, so we'll not bother with
13618 		 * the add_vsi call, but we will retrieve the current
13619 		 * VSI context.
13620 		 */
13621 		ctxt.seid = pf->main_vsi_seid;
13622 		ctxt.pf_num = pf->hw.pf_id;
13623 		ctxt.vf_num = 0;
13624 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13625 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13626 		if (ret) {
13627 			dev_info(&pf->pdev->dev,
13628 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13629 				 i40e_stat_str(&pf->hw, ret),
13630 				 i40e_aq_str(&pf->hw,
13631 					     pf->hw.aq.asq_last_status));
13632 			return -ENOENT;
13633 		}
13634 		vsi->info = ctxt.info;
13635 		vsi->info.valid_sections = 0;
13636 
13637 		vsi->seid = ctxt.seid;
13638 		vsi->id = ctxt.vsi_number;
13639 
13640 		enabled_tc = i40e_pf_get_tc_map(pf);
13641 
13642 		/* Source pruning is enabled by default, so the flag is
13643 		 * negative logic - if it's set, we need to fiddle with
13644 		 * the VSI to disable source pruning.
13645 		 */
13646 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13647 			memset(&ctxt, 0, sizeof(ctxt));
13648 			ctxt.seid = pf->main_vsi_seid;
13649 			ctxt.pf_num = pf->hw.pf_id;
13650 			ctxt.vf_num = 0;
13651 			ctxt.info.valid_sections |=
13652 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13653 			ctxt.info.switch_id =
13654 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13655 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13656 			if (ret) {
13657 				dev_info(&pf->pdev->dev,
13658 					 "update vsi failed, err %s aq_err %s\n",
13659 					 i40e_stat_str(&pf->hw, ret),
13660 					 i40e_aq_str(&pf->hw,
13661 						     pf->hw.aq.asq_last_status));
13662 				ret = -ENOENT;
13663 				goto err;
13664 			}
13665 		}
13666 
13667 		/* MFP mode setup queue map and update VSI */
13668 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13669 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13670 			memset(&ctxt, 0, sizeof(ctxt));
13671 			ctxt.seid = pf->main_vsi_seid;
13672 			ctxt.pf_num = pf->hw.pf_id;
13673 			ctxt.vf_num = 0;
13674 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13675 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13676 			if (ret) {
13677 				dev_info(&pf->pdev->dev,
13678 					 "update vsi failed, err %s aq_err %s\n",
13679 					 i40e_stat_str(&pf->hw, ret),
13680 					 i40e_aq_str(&pf->hw,
13681 						    pf->hw.aq.asq_last_status));
13682 				ret = -ENOENT;
13683 				goto err;
13684 			}
13685 			/* update the local VSI info queue map */
13686 			i40e_vsi_update_queue_map(vsi, &ctxt);
13687 			vsi->info.valid_sections = 0;
13688 		} else {
13689 			/* Default/Main VSI is only enabled for TC0
13690 			 * reconfigure it to enable all TCs that are
13691 			 * available on the port in SFP mode.
13692 			 * For MFP case the iSCSI PF would use this
13693 			 * flow to enable LAN+iSCSI TC.
13694 			 */
13695 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13696 			if (ret) {
13697 				/* Single TC condition is not fatal,
13698 				 * message and continue
13699 				 */
13700 				dev_info(&pf->pdev->dev,
13701 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13702 					 enabled_tc,
13703 					 i40e_stat_str(&pf->hw, ret),
13704 					 i40e_aq_str(&pf->hw,
13705 						    pf->hw.aq.asq_last_status));
13706 			}
13707 		}
13708 		break;
13709 
13710 	case I40E_VSI_FDIR:
13711 		ctxt.pf_num = hw->pf_id;
13712 		ctxt.vf_num = 0;
13713 		ctxt.uplink_seid = vsi->uplink_seid;
13714 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13715 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13716 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13717 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13718 			ctxt.info.valid_sections |=
13719 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13720 			ctxt.info.switch_id =
13721 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13722 		}
13723 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13724 		break;
13725 
13726 	case I40E_VSI_VMDQ2:
13727 		ctxt.pf_num = hw->pf_id;
13728 		ctxt.vf_num = 0;
13729 		ctxt.uplink_seid = vsi->uplink_seid;
13730 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13731 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13732 
13733 		/* This VSI is connected to VEB so the switch_id
13734 		 * should be set to zero by default.
13735 		 */
13736 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13737 			ctxt.info.valid_sections |=
13738 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13739 			ctxt.info.switch_id =
13740 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13741 		}
13742 
13743 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13744 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13745 		break;
13746 
13747 	case I40E_VSI_SRIOV:
13748 		ctxt.pf_num = hw->pf_id;
13749 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13750 		ctxt.uplink_seid = vsi->uplink_seid;
13751 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13752 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13753 
13754 		/* This VSI is connected to VEB so the switch_id
13755 		 * should be set to zero by default.
13756 		 */
13757 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13758 			ctxt.info.valid_sections |=
13759 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13760 			ctxt.info.switch_id =
13761 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13762 		}
13763 
13764 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13765 			ctxt.info.valid_sections |=
13766 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13767 			ctxt.info.queueing_opt_flags |=
13768 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13769 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13770 		}
13771 
13772 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13773 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13774 		if (pf->vf[vsi->vf_id].spoofchk) {
13775 			ctxt.info.valid_sections |=
13776 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13777 			ctxt.info.sec_flags |=
13778 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13779 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13780 		}
13781 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13782 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13783 		break;
13784 
13785 	case I40E_VSI_IWARP:
13786 		/* send down message to iWARP */
13787 		break;
13788 
13789 	default:
13790 		return -ENODEV;
13791 	}
13792 
13793 	if (vsi->type != I40E_VSI_MAIN) {
13794 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13795 		if (ret) {
13796 			dev_info(&vsi->back->pdev->dev,
13797 				 "add vsi failed, err %s aq_err %s\n",
13798 				 i40e_stat_str(&pf->hw, ret),
13799 				 i40e_aq_str(&pf->hw,
13800 					     pf->hw.aq.asq_last_status));
13801 			ret = -ENOENT;
13802 			goto err;
13803 		}
13804 		vsi->info = ctxt.info;
13805 		vsi->info.valid_sections = 0;
13806 		vsi->seid = ctxt.seid;
13807 		vsi->id = ctxt.vsi_number;
13808 	}
13809 
13810 	vsi->active_filters = 0;
13811 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13812 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13813 	/* If macvlan filters already exist, force them to get loaded */
13814 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13815 		f->state = I40E_FILTER_NEW;
13816 		f_count++;
13817 	}
13818 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13819 
13820 	if (f_count) {
13821 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13822 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13823 	}
13824 
13825 	/* Update VSI BW information */
13826 	ret = i40e_vsi_get_bw_info(vsi);
13827 	if (ret) {
13828 		dev_info(&pf->pdev->dev,
13829 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13830 			 i40e_stat_str(&pf->hw, ret),
13831 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13832 		/* VSI is already added so not tearing that up */
13833 		ret = 0;
13834 	}
13835 
13836 err:
13837 	return ret;
13838 }
13839 
13840 /**
13841  * i40e_vsi_release - Delete a VSI and free its resources
13842  * @vsi: the VSI being removed
13843  *
13844  * Returns 0 on success or < 0 on error
13845  **/
13846 int i40e_vsi_release(struct i40e_vsi *vsi)
13847 {
13848 	struct i40e_mac_filter *f;
13849 	struct hlist_node *h;
13850 	struct i40e_veb *veb = NULL;
13851 	struct i40e_pf *pf;
13852 	u16 uplink_seid;
13853 	int i, n, bkt;
13854 
13855 	pf = vsi->back;
13856 
13857 	/* release of a VEB-owner or last VSI is not allowed */
13858 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13859 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13860 			 vsi->seid, vsi->uplink_seid);
13861 		return -ENODEV;
13862 	}
13863 	if (vsi == pf->vsi[pf->lan_vsi] &&
13864 	    !test_bit(__I40E_DOWN, pf->state)) {
13865 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13866 		return -ENODEV;
13867 	}
13868 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13869 	uplink_seid = vsi->uplink_seid;
13870 	if (vsi->type != I40E_VSI_SRIOV) {
13871 		if (vsi->netdev_registered) {
13872 			vsi->netdev_registered = false;
13873 			if (vsi->netdev) {
13874 				/* results in a call to i40e_close() */
13875 				unregister_netdev(vsi->netdev);
13876 			}
13877 		} else {
13878 			i40e_vsi_close(vsi);
13879 		}
13880 		i40e_vsi_disable_irq(vsi);
13881 	}
13882 
13883 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13884 
13885 	/* clear the sync flag on all filters */
13886 	if (vsi->netdev) {
13887 		__dev_uc_unsync(vsi->netdev, NULL);
13888 		__dev_mc_unsync(vsi->netdev, NULL);
13889 	}
13890 
13891 	/* make sure any remaining filters are marked for deletion */
13892 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13893 		__i40e_del_filter(vsi, f);
13894 
13895 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13896 
13897 	i40e_sync_vsi_filters(vsi);
13898 
13899 	i40e_vsi_delete(vsi);
13900 	i40e_vsi_free_q_vectors(vsi);
13901 	if (vsi->netdev) {
13902 		free_netdev(vsi->netdev);
13903 		vsi->netdev = NULL;
13904 	}
13905 	i40e_vsi_clear_rings(vsi);
13906 	i40e_vsi_clear(vsi);
13907 
13908 	/* If this was the last thing on the VEB, except for the
13909 	 * controlling VSI, remove the VEB, which puts the controlling
13910 	 * VSI onto the next level down in the switch.
13911 	 *
13912 	 * Well, okay, there's one more exception here: don't remove
13913 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13914 	 * from up the network stack.
13915 	 */
13916 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13917 		if (pf->vsi[i] &&
13918 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13919 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13920 			n++;      /* count the VSIs */
13921 		}
13922 	}
13923 	for (i = 0; i < I40E_MAX_VEB; i++) {
13924 		if (!pf->veb[i])
13925 			continue;
13926 		if (pf->veb[i]->uplink_seid == uplink_seid)
13927 			n++;     /* count the VEBs */
13928 		if (pf->veb[i]->seid == uplink_seid)
13929 			veb = pf->veb[i];
13930 	}
13931 	if (n == 0 && veb && veb->uplink_seid != 0)
13932 		i40e_veb_release(veb);
13933 
13934 	return 0;
13935 }
13936 
13937 /**
13938  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13939  * @vsi: ptr to the VSI
13940  *
13941  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13942  * corresponding SW VSI structure and initializes num_queue_pairs for the
13943  * newly allocated VSI.
13944  *
13945  * Returns 0 on success or negative on failure
13946  **/
13947 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13948 {
13949 	int ret = -ENOENT;
13950 	struct i40e_pf *pf = vsi->back;
13951 
13952 	if (vsi->q_vectors[0]) {
13953 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13954 			 vsi->seid);
13955 		return -EEXIST;
13956 	}
13957 
13958 	if (vsi->base_vector) {
13959 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13960 			 vsi->seid, vsi->base_vector);
13961 		return -EEXIST;
13962 	}
13963 
13964 	ret = i40e_vsi_alloc_q_vectors(vsi);
13965 	if (ret) {
13966 		dev_info(&pf->pdev->dev,
13967 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13968 			 vsi->num_q_vectors, vsi->seid, ret);
13969 		vsi->num_q_vectors = 0;
13970 		goto vector_setup_out;
13971 	}
13972 
13973 	/* In Legacy mode, we do not have to get any other vector since we
13974 	 * piggyback on the misc/ICR0 for queue interrupts.
13975 	*/
13976 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13977 		return ret;
13978 	if (vsi->num_q_vectors)
13979 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13980 						 vsi->num_q_vectors, vsi->idx);
13981 	if (vsi->base_vector < 0) {
13982 		dev_info(&pf->pdev->dev,
13983 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13984 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13985 		i40e_vsi_free_q_vectors(vsi);
13986 		ret = -ENOENT;
13987 		goto vector_setup_out;
13988 	}
13989 
13990 vector_setup_out:
13991 	return ret;
13992 }
13993 
13994 /**
13995  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13996  * @vsi: pointer to the vsi.
13997  *
13998  * This re-allocates a vsi's queue resources.
13999  *
14000  * Returns pointer to the successfully allocated and configured VSI sw struct
14001  * on success, otherwise returns NULL on failure.
14002  **/
14003 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14004 {
14005 	u16 alloc_queue_pairs;
14006 	struct i40e_pf *pf;
14007 	u8 enabled_tc;
14008 	int ret;
14009 
14010 	if (!vsi)
14011 		return NULL;
14012 
14013 	pf = vsi->back;
14014 
14015 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14016 	i40e_vsi_clear_rings(vsi);
14017 
14018 	i40e_vsi_free_arrays(vsi, false);
14019 	i40e_set_num_rings_in_vsi(vsi);
14020 	ret = i40e_vsi_alloc_arrays(vsi, false);
14021 	if (ret)
14022 		goto err_vsi;
14023 
14024 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14025 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14026 
14027 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14028 	if (ret < 0) {
14029 		dev_info(&pf->pdev->dev,
14030 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14031 			 alloc_queue_pairs, vsi->seid, ret);
14032 		goto err_vsi;
14033 	}
14034 	vsi->base_queue = ret;
14035 
14036 	/* Update the FW view of the VSI. Force a reset of TC and queue
14037 	 * layout configurations.
14038 	 */
14039 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14040 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14041 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14042 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14043 	if (vsi->type == I40E_VSI_MAIN)
14044 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14045 
14046 	/* assign it some queues */
14047 	ret = i40e_alloc_rings(vsi);
14048 	if (ret)
14049 		goto err_rings;
14050 
14051 	/* map all of the rings to the q_vectors */
14052 	i40e_vsi_map_rings_to_vectors(vsi);
14053 	return vsi;
14054 
14055 err_rings:
14056 	i40e_vsi_free_q_vectors(vsi);
14057 	if (vsi->netdev_registered) {
14058 		vsi->netdev_registered = false;
14059 		unregister_netdev(vsi->netdev);
14060 		free_netdev(vsi->netdev);
14061 		vsi->netdev = NULL;
14062 	}
14063 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14064 err_vsi:
14065 	i40e_vsi_clear(vsi);
14066 	return NULL;
14067 }
14068 
14069 /**
14070  * i40e_vsi_setup - Set up a VSI by a given type
14071  * @pf: board private structure
14072  * @type: VSI type
14073  * @uplink_seid: the switch element to link to
14074  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14075  *
14076  * This allocates the sw VSI structure and its queue resources, then add a VSI
14077  * to the identified VEB.
14078  *
14079  * Returns pointer to the successfully allocated and configure VSI sw struct on
14080  * success, otherwise returns NULL on failure.
14081  **/
14082 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14083 				u16 uplink_seid, u32 param1)
14084 {
14085 	struct i40e_vsi *vsi = NULL;
14086 	struct i40e_veb *veb = NULL;
14087 	u16 alloc_queue_pairs;
14088 	int ret, i;
14089 	int v_idx;
14090 
14091 	/* The requested uplink_seid must be either
14092 	 *     - the PF's port seid
14093 	 *              no VEB is needed because this is the PF
14094 	 *              or this is a Flow Director special case VSI
14095 	 *     - seid of an existing VEB
14096 	 *     - seid of a VSI that owns an existing VEB
14097 	 *     - seid of a VSI that doesn't own a VEB
14098 	 *              a new VEB is created and the VSI becomes the owner
14099 	 *     - seid of the PF VSI, which is what creates the first VEB
14100 	 *              this is a special case of the previous
14101 	 *
14102 	 * Find which uplink_seid we were given and create a new VEB if needed
14103 	 */
14104 	for (i = 0; i < I40E_MAX_VEB; i++) {
14105 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14106 			veb = pf->veb[i];
14107 			break;
14108 		}
14109 	}
14110 
14111 	if (!veb && uplink_seid != pf->mac_seid) {
14112 
14113 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14114 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14115 				vsi = pf->vsi[i];
14116 				break;
14117 			}
14118 		}
14119 		if (!vsi) {
14120 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14121 				 uplink_seid);
14122 			return NULL;
14123 		}
14124 
14125 		if (vsi->uplink_seid == pf->mac_seid)
14126 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14127 					     vsi->tc_config.enabled_tc);
14128 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14129 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14130 					     vsi->tc_config.enabled_tc);
14131 		if (veb) {
14132 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14133 				dev_info(&vsi->back->pdev->dev,
14134 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14135 				return NULL;
14136 			}
14137 			/* We come up by default in VEPA mode if SRIOV is not
14138 			 * already enabled, in which case we can't force VEPA
14139 			 * mode.
14140 			 */
14141 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14142 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14143 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14144 			}
14145 			i40e_config_bridge_mode(veb);
14146 		}
14147 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14148 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14149 				veb = pf->veb[i];
14150 		}
14151 		if (!veb) {
14152 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14153 			return NULL;
14154 		}
14155 
14156 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14157 		uplink_seid = veb->seid;
14158 	}
14159 
14160 	/* get vsi sw struct */
14161 	v_idx = i40e_vsi_mem_alloc(pf, type);
14162 	if (v_idx < 0)
14163 		goto err_alloc;
14164 	vsi = pf->vsi[v_idx];
14165 	if (!vsi)
14166 		goto err_alloc;
14167 	vsi->type = type;
14168 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14169 
14170 	if (type == I40E_VSI_MAIN)
14171 		pf->lan_vsi = v_idx;
14172 	else if (type == I40E_VSI_SRIOV)
14173 		vsi->vf_id = param1;
14174 	/* assign it some queues */
14175 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14176 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14177 
14178 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14179 	if (ret < 0) {
14180 		dev_info(&pf->pdev->dev,
14181 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14182 			 alloc_queue_pairs, vsi->seid, ret);
14183 		goto err_vsi;
14184 	}
14185 	vsi->base_queue = ret;
14186 
14187 	/* get a VSI from the hardware */
14188 	vsi->uplink_seid = uplink_seid;
14189 	ret = i40e_add_vsi(vsi);
14190 	if (ret)
14191 		goto err_vsi;
14192 
14193 	switch (vsi->type) {
14194 	/* setup the netdev if needed */
14195 	case I40E_VSI_MAIN:
14196 	case I40E_VSI_VMDQ2:
14197 		ret = i40e_config_netdev(vsi);
14198 		if (ret)
14199 			goto err_netdev;
14200 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14201 		if (ret)
14202 			goto err_netdev;
14203 		ret = register_netdev(vsi->netdev);
14204 		if (ret)
14205 			goto err_netdev;
14206 		vsi->netdev_registered = true;
14207 		netif_carrier_off(vsi->netdev);
14208 #ifdef CONFIG_I40E_DCB
14209 		/* Setup DCB netlink interface */
14210 		i40e_dcbnl_setup(vsi);
14211 #endif /* CONFIG_I40E_DCB */
14212 		fallthrough;
14213 	case I40E_VSI_FDIR:
14214 		/* set up vectors and rings if needed */
14215 		ret = i40e_vsi_setup_vectors(vsi);
14216 		if (ret)
14217 			goto err_msix;
14218 
14219 		ret = i40e_alloc_rings(vsi);
14220 		if (ret)
14221 			goto err_rings;
14222 
14223 		/* map all of the rings to the q_vectors */
14224 		i40e_vsi_map_rings_to_vectors(vsi);
14225 
14226 		i40e_vsi_reset_stats(vsi);
14227 		break;
14228 	default:
14229 		/* no netdev or rings for the other VSI types */
14230 		break;
14231 	}
14232 
14233 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14234 	    (vsi->type == I40E_VSI_VMDQ2)) {
14235 		ret = i40e_vsi_config_rss(vsi);
14236 	}
14237 	return vsi;
14238 
14239 err_rings:
14240 	i40e_vsi_free_q_vectors(vsi);
14241 err_msix:
14242 	if (vsi->netdev_registered) {
14243 		vsi->netdev_registered = false;
14244 		unregister_netdev(vsi->netdev);
14245 		free_netdev(vsi->netdev);
14246 		vsi->netdev = NULL;
14247 	}
14248 err_netdev:
14249 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14250 err_vsi:
14251 	i40e_vsi_clear(vsi);
14252 err_alloc:
14253 	return NULL;
14254 }
14255 
14256 /**
14257  * i40e_veb_get_bw_info - Query VEB BW information
14258  * @veb: the veb to query
14259  *
14260  * Query the Tx scheduler BW configuration data for given VEB
14261  **/
14262 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14263 {
14264 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14265 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14266 	struct i40e_pf *pf = veb->pf;
14267 	struct i40e_hw *hw = &pf->hw;
14268 	u32 tc_bw_max;
14269 	int ret = 0;
14270 	int i;
14271 
14272 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14273 						  &bw_data, NULL);
14274 	if (ret) {
14275 		dev_info(&pf->pdev->dev,
14276 			 "query veb bw config failed, err %s aq_err %s\n",
14277 			 i40e_stat_str(&pf->hw, ret),
14278 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14279 		goto out;
14280 	}
14281 
14282 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14283 						   &ets_data, NULL);
14284 	if (ret) {
14285 		dev_info(&pf->pdev->dev,
14286 			 "query veb bw ets config failed, err %s aq_err %s\n",
14287 			 i40e_stat_str(&pf->hw, ret),
14288 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14289 		goto out;
14290 	}
14291 
14292 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14293 	veb->bw_max_quanta = ets_data.tc_bw_max;
14294 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14295 	veb->enabled_tc = ets_data.tc_valid_bits;
14296 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14297 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14298 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14299 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14300 		veb->bw_tc_limit_credits[i] =
14301 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14302 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14303 	}
14304 
14305 out:
14306 	return ret;
14307 }
14308 
14309 /**
14310  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14311  * @pf: board private structure
14312  *
14313  * On error: returns error code (negative)
14314  * On success: returns vsi index in PF (positive)
14315  **/
14316 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14317 {
14318 	int ret = -ENOENT;
14319 	struct i40e_veb *veb;
14320 	int i;
14321 
14322 	/* Need to protect the allocation of switch elements at the PF level */
14323 	mutex_lock(&pf->switch_mutex);
14324 
14325 	/* VEB list may be fragmented if VEB creation/destruction has
14326 	 * been happening.  We can afford to do a quick scan to look
14327 	 * for any free slots in the list.
14328 	 *
14329 	 * find next empty veb slot, looping back around if necessary
14330 	 */
14331 	i = 0;
14332 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14333 		i++;
14334 	if (i >= I40E_MAX_VEB) {
14335 		ret = -ENOMEM;
14336 		goto err_alloc_veb;  /* out of VEB slots! */
14337 	}
14338 
14339 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14340 	if (!veb) {
14341 		ret = -ENOMEM;
14342 		goto err_alloc_veb;
14343 	}
14344 	veb->pf = pf;
14345 	veb->idx = i;
14346 	veb->enabled_tc = 1;
14347 
14348 	pf->veb[i] = veb;
14349 	ret = i;
14350 err_alloc_veb:
14351 	mutex_unlock(&pf->switch_mutex);
14352 	return ret;
14353 }
14354 
14355 /**
14356  * i40e_switch_branch_release - Delete a branch of the switch tree
14357  * @branch: where to start deleting
14358  *
14359  * This uses recursion to find the tips of the branch to be
14360  * removed, deleting until we get back to and can delete this VEB.
14361  **/
14362 static void i40e_switch_branch_release(struct i40e_veb *branch)
14363 {
14364 	struct i40e_pf *pf = branch->pf;
14365 	u16 branch_seid = branch->seid;
14366 	u16 veb_idx = branch->idx;
14367 	int i;
14368 
14369 	/* release any VEBs on this VEB - RECURSION */
14370 	for (i = 0; i < I40E_MAX_VEB; i++) {
14371 		if (!pf->veb[i])
14372 			continue;
14373 		if (pf->veb[i]->uplink_seid == branch->seid)
14374 			i40e_switch_branch_release(pf->veb[i]);
14375 	}
14376 
14377 	/* Release the VSIs on this VEB, but not the owner VSI.
14378 	 *
14379 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14380 	 *       the VEB itself, so don't use (*branch) after this loop.
14381 	 */
14382 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14383 		if (!pf->vsi[i])
14384 			continue;
14385 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14386 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14387 			i40e_vsi_release(pf->vsi[i]);
14388 		}
14389 	}
14390 
14391 	/* There's one corner case where the VEB might not have been
14392 	 * removed, so double check it here and remove it if needed.
14393 	 * This case happens if the veb was created from the debugfs
14394 	 * commands and no VSIs were added to it.
14395 	 */
14396 	if (pf->veb[veb_idx])
14397 		i40e_veb_release(pf->veb[veb_idx]);
14398 }
14399 
14400 /**
14401  * i40e_veb_clear - remove veb struct
14402  * @veb: the veb to remove
14403  **/
14404 static void i40e_veb_clear(struct i40e_veb *veb)
14405 {
14406 	if (!veb)
14407 		return;
14408 
14409 	if (veb->pf) {
14410 		struct i40e_pf *pf = veb->pf;
14411 
14412 		mutex_lock(&pf->switch_mutex);
14413 		if (pf->veb[veb->idx] == veb)
14414 			pf->veb[veb->idx] = NULL;
14415 		mutex_unlock(&pf->switch_mutex);
14416 	}
14417 
14418 	kfree(veb);
14419 }
14420 
14421 /**
14422  * i40e_veb_release - Delete a VEB and free its resources
14423  * @veb: the VEB being removed
14424  **/
14425 void i40e_veb_release(struct i40e_veb *veb)
14426 {
14427 	struct i40e_vsi *vsi = NULL;
14428 	struct i40e_pf *pf;
14429 	int i, n = 0;
14430 
14431 	pf = veb->pf;
14432 
14433 	/* find the remaining VSI and check for extras */
14434 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14435 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14436 			n++;
14437 			vsi = pf->vsi[i];
14438 		}
14439 	}
14440 	if (n != 1) {
14441 		dev_info(&pf->pdev->dev,
14442 			 "can't remove VEB %d with %d VSIs left\n",
14443 			 veb->seid, n);
14444 		return;
14445 	}
14446 
14447 	/* move the remaining VSI to uplink veb */
14448 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14449 	if (veb->uplink_seid) {
14450 		vsi->uplink_seid = veb->uplink_seid;
14451 		if (veb->uplink_seid == pf->mac_seid)
14452 			vsi->veb_idx = I40E_NO_VEB;
14453 		else
14454 			vsi->veb_idx = veb->veb_idx;
14455 	} else {
14456 		/* floating VEB */
14457 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14458 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14459 	}
14460 
14461 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14462 	i40e_veb_clear(veb);
14463 }
14464 
14465 /**
14466  * i40e_add_veb - create the VEB in the switch
14467  * @veb: the VEB to be instantiated
14468  * @vsi: the controlling VSI
14469  **/
14470 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14471 {
14472 	struct i40e_pf *pf = veb->pf;
14473 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14474 	int ret;
14475 
14476 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14477 			      veb->enabled_tc, false,
14478 			      &veb->seid, enable_stats, NULL);
14479 
14480 	/* get a VEB from the hardware */
14481 	if (ret) {
14482 		dev_info(&pf->pdev->dev,
14483 			 "couldn't add VEB, err %s aq_err %s\n",
14484 			 i40e_stat_str(&pf->hw, ret),
14485 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14486 		return -EPERM;
14487 	}
14488 
14489 	/* get statistics counter */
14490 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14491 					 &veb->stats_idx, NULL, NULL, NULL);
14492 	if (ret) {
14493 		dev_info(&pf->pdev->dev,
14494 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14495 			 i40e_stat_str(&pf->hw, ret),
14496 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14497 		return -EPERM;
14498 	}
14499 	ret = i40e_veb_get_bw_info(veb);
14500 	if (ret) {
14501 		dev_info(&pf->pdev->dev,
14502 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14503 			 i40e_stat_str(&pf->hw, ret),
14504 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14505 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14506 		return -ENOENT;
14507 	}
14508 
14509 	vsi->uplink_seid = veb->seid;
14510 	vsi->veb_idx = veb->idx;
14511 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14512 
14513 	return 0;
14514 }
14515 
14516 /**
14517  * i40e_veb_setup - Set up a VEB
14518  * @pf: board private structure
14519  * @flags: VEB setup flags
14520  * @uplink_seid: the switch element to link to
14521  * @vsi_seid: the initial VSI seid
14522  * @enabled_tc: Enabled TC bit-map
14523  *
14524  * This allocates the sw VEB structure and links it into the switch
14525  * It is possible and legal for this to be a duplicate of an already
14526  * existing VEB.  It is also possible for both uplink and vsi seids
14527  * to be zero, in order to create a floating VEB.
14528  *
14529  * Returns pointer to the successfully allocated VEB sw struct on
14530  * success, otherwise returns NULL on failure.
14531  **/
14532 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14533 				u16 uplink_seid, u16 vsi_seid,
14534 				u8 enabled_tc)
14535 {
14536 	struct i40e_veb *veb, *uplink_veb = NULL;
14537 	int vsi_idx, veb_idx;
14538 	int ret;
14539 
14540 	/* if one seid is 0, the other must be 0 to create a floating relay */
14541 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14542 	    (uplink_seid + vsi_seid != 0)) {
14543 		dev_info(&pf->pdev->dev,
14544 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14545 			 uplink_seid, vsi_seid);
14546 		return NULL;
14547 	}
14548 
14549 	/* make sure there is such a vsi and uplink */
14550 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14551 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14552 			break;
14553 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14554 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14555 			 vsi_seid);
14556 		return NULL;
14557 	}
14558 
14559 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14560 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14561 			if (pf->veb[veb_idx] &&
14562 			    pf->veb[veb_idx]->seid == uplink_seid) {
14563 				uplink_veb = pf->veb[veb_idx];
14564 				break;
14565 			}
14566 		}
14567 		if (!uplink_veb) {
14568 			dev_info(&pf->pdev->dev,
14569 				 "uplink seid %d not found\n", uplink_seid);
14570 			return NULL;
14571 		}
14572 	}
14573 
14574 	/* get veb sw struct */
14575 	veb_idx = i40e_veb_mem_alloc(pf);
14576 	if (veb_idx < 0)
14577 		goto err_alloc;
14578 	veb = pf->veb[veb_idx];
14579 	veb->flags = flags;
14580 	veb->uplink_seid = uplink_seid;
14581 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14582 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14583 
14584 	/* create the VEB in the switch */
14585 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14586 	if (ret)
14587 		goto err_veb;
14588 	if (vsi_idx == pf->lan_vsi)
14589 		pf->lan_veb = veb->idx;
14590 
14591 	return veb;
14592 
14593 err_veb:
14594 	i40e_veb_clear(veb);
14595 err_alloc:
14596 	return NULL;
14597 }
14598 
14599 /**
14600  * i40e_setup_pf_switch_element - set PF vars based on switch type
14601  * @pf: board private structure
14602  * @ele: element we are building info from
14603  * @num_reported: total number of elements
14604  * @printconfig: should we print the contents
14605  *
14606  * helper function to assist in extracting a few useful SEID values.
14607  **/
14608 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14609 				struct i40e_aqc_switch_config_element_resp *ele,
14610 				u16 num_reported, bool printconfig)
14611 {
14612 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14613 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14614 	u8 element_type = ele->element_type;
14615 	u16 seid = le16_to_cpu(ele->seid);
14616 
14617 	if (printconfig)
14618 		dev_info(&pf->pdev->dev,
14619 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14620 			 element_type, seid, uplink_seid, downlink_seid);
14621 
14622 	switch (element_type) {
14623 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14624 		pf->mac_seid = seid;
14625 		break;
14626 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14627 		/* Main VEB? */
14628 		if (uplink_seid != pf->mac_seid)
14629 			break;
14630 		if (pf->lan_veb >= I40E_MAX_VEB) {
14631 			int v;
14632 
14633 			/* find existing or else empty VEB */
14634 			for (v = 0; v < I40E_MAX_VEB; v++) {
14635 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14636 					pf->lan_veb = v;
14637 					break;
14638 				}
14639 			}
14640 			if (pf->lan_veb >= I40E_MAX_VEB) {
14641 				v = i40e_veb_mem_alloc(pf);
14642 				if (v < 0)
14643 					break;
14644 				pf->lan_veb = v;
14645 			}
14646 		}
14647 		if (pf->lan_veb >= I40E_MAX_VEB)
14648 			break;
14649 
14650 		pf->veb[pf->lan_veb]->seid = seid;
14651 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14652 		pf->veb[pf->lan_veb]->pf = pf;
14653 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14654 		break;
14655 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14656 		if (num_reported != 1)
14657 			break;
14658 		/* This is immediately after a reset so we can assume this is
14659 		 * the PF's VSI
14660 		 */
14661 		pf->mac_seid = uplink_seid;
14662 		pf->pf_seid = downlink_seid;
14663 		pf->main_vsi_seid = seid;
14664 		if (printconfig)
14665 			dev_info(&pf->pdev->dev,
14666 				 "pf_seid=%d main_vsi_seid=%d\n",
14667 				 pf->pf_seid, pf->main_vsi_seid);
14668 		break;
14669 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14670 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14671 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14672 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14673 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14674 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14675 		/* ignore these for now */
14676 		break;
14677 	default:
14678 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14679 			 element_type, seid);
14680 		break;
14681 	}
14682 }
14683 
14684 /**
14685  * i40e_fetch_switch_configuration - Get switch config from firmware
14686  * @pf: board private structure
14687  * @printconfig: should we print the contents
14688  *
14689  * Get the current switch configuration from the device and
14690  * extract a few useful SEID values.
14691  **/
14692 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14693 {
14694 	struct i40e_aqc_get_switch_config_resp *sw_config;
14695 	u16 next_seid = 0;
14696 	int ret = 0;
14697 	u8 *aq_buf;
14698 	int i;
14699 
14700 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14701 	if (!aq_buf)
14702 		return -ENOMEM;
14703 
14704 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14705 	do {
14706 		u16 num_reported, num_total;
14707 
14708 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14709 						I40E_AQ_LARGE_BUF,
14710 						&next_seid, NULL);
14711 		if (ret) {
14712 			dev_info(&pf->pdev->dev,
14713 				 "get switch config failed err %s aq_err %s\n",
14714 				 i40e_stat_str(&pf->hw, ret),
14715 				 i40e_aq_str(&pf->hw,
14716 					     pf->hw.aq.asq_last_status));
14717 			kfree(aq_buf);
14718 			return -ENOENT;
14719 		}
14720 
14721 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14722 		num_total = le16_to_cpu(sw_config->header.num_total);
14723 
14724 		if (printconfig)
14725 			dev_info(&pf->pdev->dev,
14726 				 "header: %d reported %d total\n",
14727 				 num_reported, num_total);
14728 
14729 		for (i = 0; i < num_reported; i++) {
14730 			struct i40e_aqc_switch_config_element_resp *ele =
14731 				&sw_config->element[i];
14732 
14733 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14734 						     printconfig);
14735 		}
14736 	} while (next_seid != 0);
14737 
14738 	kfree(aq_buf);
14739 	return ret;
14740 }
14741 
14742 /**
14743  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14744  * @pf: board private structure
14745  * @reinit: if the Main VSI needs to re-initialized.
14746  * @lock_acquired: indicates whether or not the lock has been acquired
14747  *
14748  * Returns 0 on success, negative value on failure
14749  **/
14750 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14751 {
14752 	u16 flags = 0;
14753 	int ret;
14754 
14755 	/* find out what's out there already */
14756 	ret = i40e_fetch_switch_configuration(pf, false);
14757 	if (ret) {
14758 		dev_info(&pf->pdev->dev,
14759 			 "couldn't fetch switch config, err %s aq_err %s\n",
14760 			 i40e_stat_str(&pf->hw, ret),
14761 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14762 		return ret;
14763 	}
14764 	i40e_pf_reset_stats(pf);
14765 
14766 	/* set the switch config bit for the whole device to
14767 	 * support limited promisc or true promisc
14768 	 * when user requests promisc. The default is limited
14769 	 * promisc.
14770 	*/
14771 
14772 	if ((pf->hw.pf_id == 0) &&
14773 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14774 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14775 		pf->last_sw_conf_flags = flags;
14776 	}
14777 
14778 	if (pf->hw.pf_id == 0) {
14779 		u16 valid_flags;
14780 
14781 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14782 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14783 						NULL);
14784 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14785 			dev_info(&pf->pdev->dev,
14786 				 "couldn't set switch config bits, err %s aq_err %s\n",
14787 				 i40e_stat_str(&pf->hw, ret),
14788 				 i40e_aq_str(&pf->hw,
14789 					     pf->hw.aq.asq_last_status));
14790 			/* not a fatal problem, just keep going */
14791 		}
14792 		pf->last_sw_conf_valid_flags = valid_flags;
14793 	}
14794 
14795 	/* first time setup */
14796 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14797 		struct i40e_vsi *vsi = NULL;
14798 		u16 uplink_seid;
14799 
14800 		/* Set up the PF VSI associated with the PF's main VSI
14801 		 * that is already in the HW switch
14802 		 */
14803 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14804 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14805 		else
14806 			uplink_seid = pf->mac_seid;
14807 		if (pf->lan_vsi == I40E_NO_VSI)
14808 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14809 		else if (reinit)
14810 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14811 		if (!vsi) {
14812 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14813 			i40e_cloud_filter_exit(pf);
14814 			i40e_fdir_teardown(pf);
14815 			return -EAGAIN;
14816 		}
14817 	} else {
14818 		/* force a reset of TC and queue layout configurations */
14819 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14820 
14821 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14822 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14823 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14824 	}
14825 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14826 
14827 	i40e_fdir_sb_setup(pf);
14828 
14829 	/* Setup static PF queue filter control settings */
14830 	ret = i40e_setup_pf_filter_control(pf);
14831 	if (ret) {
14832 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14833 			 ret);
14834 		/* Failure here should not stop continuing other steps */
14835 	}
14836 
14837 	/* enable RSS in the HW, even for only one queue, as the stack can use
14838 	 * the hash
14839 	 */
14840 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14841 		i40e_pf_config_rss(pf);
14842 
14843 	/* fill in link information and enable LSE reporting */
14844 	i40e_link_event(pf);
14845 
14846 	/* Initialize user-specific link properties */
14847 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14848 				  I40E_AQ_AN_COMPLETED) ? true : false);
14849 
14850 	i40e_ptp_init(pf);
14851 
14852 	if (!lock_acquired)
14853 		rtnl_lock();
14854 
14855 	/* repopulate tunnel port filters */
14856 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14857 
14858 	if (!lock_acquired)
14859 		rtnl_unlock();
14860 
14861 	return ret;
14862 }
14863 
14864 /**
14865  * i40e_determine_queue_usage - Work out queue distribution
14866  * @pf: board private structure
14867  **/
14868 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14869 {
14870 	int queues_left;
14871 	int q_max;
14872 
14873 	pf->num_lan_qps = 0;
14874 
14875 	/* Find the max queues to be put into basic use.  We'll always be
14876 	 * using TC0, whether or not DCB is running, and TC0 will get the
14877 	 * big RSS set.
14878 	 */
14879 	queues_left = pf->hw.func_caps.num_tx_qp;
14880 
14881 	if ((queues_left == 1) ||
14882 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14883 		/* one qp for PF, no queues for anything else */
14884 		queues_left = 0;
14885 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14886 
14887 		/* make sure all the fancies are disabled */
14888 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14889 			       I40E_FLAG_IWARP_ENABLED	|
14890 			       I40E_FLAG_FD_SB_ENABLED	|
14891 			       I40E_FLAG_FD_ATR_ENABLED	|
14892 			       I40E_FLAG_DCB_CAPABLE	|
14893 			       I40E_FLAG_DCB_ENABLED	|
14894 			       I40E_FLAG_SRIOV_ENABLED	|
14895 			       I40E_FLAG_VMDQ_ENABLED);
14896 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14897 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14898 				  I40E_FLAG_FD_SB_ENABLED |
14899 				  I40E_FLAG_FD_ATR_ENABLED |
14900 				  I40E_FLAG_DCB_CAPABLE))) {
14901 		/* one qp for PF */
14902 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14903 		queues_left -= pf->num_lan_qps;
14904 
14905 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14906 			       I40E_FLAG_IWARP_ENABLED	|
14907 			       I40E_FLAG_FD_SB_ENABLED	|
14908 			       I40E_FLAG_FD_ATR_ENABLED	|
14909 			       I40E_FLAG_DCB_ENABLED	|
14910 			       I40E_FLAG_VMDQ_ENABLED);
14911 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14912 	} else {
14913 		/* Not enough queues for all TCs */
14914 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14915 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14916 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14917 					I40E_FLAG_DCB_ENABLED);
14918 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14919 		}
14920 
14921 		/* limit lan qps to the smaller of qps, cpus or msix */
14922 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14923 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14924 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14925 		pf->num_lan_qps = q_max;
14926 
14927 		queues_left -= pf->num_lan_qps;
14928 	}
14929 
14930 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14931 		if (queues_left > 1) {
14932 			queues_left -= 1; /* save 1 queue for FD */
14933 		} else {
14934 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14935 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14936 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14937 		}
14938 	}
14939 
14940 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14941 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14942 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14943 					(queues_left / pf->num_vf_qps));
14944 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14945 	}
14946 
14947 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14948 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14949 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14950 					  (queues_left / pf->num_vmdq_qps));
14951 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14952 	}
14953 
14954 	pf->queues_left = queues_left;
14955 	dev_dbg(&pf->pdev->dev,
14956 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14957 		pf->hw.func_caps.num_tx_qp,
14958 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14959 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14960 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14961 		queues_left);
14962 }
14963 
14964 /**
14965  * i40e_setup_pf_filter_control - Setup PF static filter control
14966  * @pf: PF to be setup
14967  *
14968  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14969  * settings. If PE/FCoE are enabled then it will also set the per PF
14970  * based filter sizes required for them. It also enables Flow director,
14971  * ethertype and macvlan type filter settings for the pf.
14972  *
14973  * Returns 0 on success, negative on failure
14974  **/
14975 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14976 {
14977 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14978 
14979 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14980 
14981 	/* Flow Director is enabled */
14982 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14983 		settings->enable_fdir = true;
14984 
14985 	/* Ethtype and MACVLAN filters enabled for PF */
14986 	settings->enable_ethtype = true;
14987 	settings->enable_macvlan = true;
14988 
14989 	if (i40e_set_filter_control(&pf->hw, settings))
14990 		return -ENOENT;
14991 
14992 	return 0;
14993 }
14994 
14995 #define INFO_STRING_LEN 255
14996 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
14997 static void i40e_print_features(struct i40e_pf *pf)
14998 {
14999 	struct i40e_hw *hw = &pf->hw;
15000 	char *buf;
15001 	int i;
15002 
15003 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15004 	if (!buf)
15005 		return;
15006 
15007 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15008 #ifdef CONFIG_PCI_IOV
15009 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15010 #endif
15011 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15012 		      pf->hw.func_caps.num_vsis,
15013 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15014 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15015 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15016 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15017 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15018 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15019 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15020 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15021 	}
15022 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15023 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15024 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15025 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15026 	if (pf->flags & I40E_FLAG_PTP)
15027 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15028 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15029 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15030 	else
15031 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15032 
15033 	dev_info(&pf->pdev->dev, "%s\n", buf);
15034 	kfree(buf);
15035 	WARN_ON(i > INFO_STRING_LEN);
15036 }
15037 
15038 /**
15039  * i40e_get_platform_mac_addr - get platform-specific MAC address
15040  * @pdev: PCI device information struct
15041  * @pf: board private structure
15042  *
15043  * Look up the MAC address for the device. First we'll try
15044  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15045  * specific fallback. Otherwise, we'll default to the stored value in
15046  * firmware.
15047  **/
15048 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15049 {
15050 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15051 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15052 }
15053 
15054 /**
15055  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15056  * @fec_cfg: FEC option to set in flags
15057  * @flags: ptr to flags in which we set FEC option
15058  **/
15059 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15060 {
15061 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15062 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15063 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15064 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15065 		*flags |= I40E_FLAG_RS_FEC;
15066 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15067 	}
15068 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15069 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15070 		*flags |= I40E_FLAG_BASE_R_FEC;
15071 		*flags &= ~I40E_FLAG_RS_FEC;
15072 	}
15073 	if (fec_cfg == 0)
15074 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15075 }
15076 
15077 /**
15078  * i40e_check_recovery_mode - check if we are running transition firmware
15079  * @pf: board private structure
15080  *
15081  * Check registers indicating the firmware runs in recovery mode. Sets the
15082  * appropriate driver state.
15083  *
15084  * Returns true if the recovery mode was detected, false otherwise
15085  **/
15086 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15087 {
15088 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15089 
15090 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15091 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15092 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15093 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15094 
15095 		return true;
15096 	}
15097 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15098 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15099 
15100 	return false;
15101 }
15102 
15103 /**
15104  * i40e_pf_loop_reset - perform reset in a loop.
15105  * @pf: board private structure
15106  *
15107  * This function is useful when a NIC is about to enter recovery mode.
15108  * When a NIC's internal data structures are corrupted the NIC's
15109  * firmware is going to enter recovery mode.
15110  * Right after a POR it takes about 7 minutes for firmware to enter
15111  * recovery mode. Until that time a NIC is in some kind of intermediate
15112  * state. After that time period the NIC almost surely enters
15113  * recovery mode. The only way for a driver to detect intermediate
15114  * state is to issue a series of pf-resets and check a return value.
15115  * If a PF reset returns success then the firmware could be in recovery
15116  * mode so the caller of this code needs to check for recovery mode
15117  * if this function returns success. There is a little chance that
15118  * firmware will hang in intermediate state forever.
15119  * Since waiting 7 minutes is quite a lot of time this function waits
15120  * 10 seconds and then gives up by returning an error.
15121  *
15122  * Return 0 on success, negative on failure.
15123  **/
15124 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15125 {
15126 	/* wait max 10 seconds for PF reset to succeed */
15127 	const unsigned long time_end = jiffies + 10 * HZ;
15128 
15129 	struct i40e_hw *hw = &pf->hw;
15130 	i40e_status ret;
15131 
15132 	ret = i40e_pf_reset(hw);
15133 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15134 		usleep_range(10000, 20000);
15135 		ret = i40e_pf_reset(hw);
15136 	}
15137 
15138 	if (ret == I40E_SUCCESS)
15139 		pf->pfr_count++;
15140 	else
15141 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15142 
15143 	return ret;
15144 }
15145 
15146 /**
15147  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15148  * @pf: board private structure
15149  *
15150  * Check FW registers to determine if FW issued unexpected EMP Reset.
15151  * Every time when unexpected EMP Reset occurs the FW increments
15152  * a counter of unexpected EMP Resets. When the counter reaches 10
15153  * the FW should enter the Recovery mode
15154  *
15155  * Returns true if FW issued unexpected EMP Reset
15156  **/
15157 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15158 {
15159 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15160 			   I40E_GL_FWSTS_FWS1B_MASK;
15161 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15162 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15163 }
15164 
15165 /**
15166  * i40e_handle_resets - handle EMP resets and PF resets
15167  * @pf: board private structure
15168  *
15169  * Handle both EMP resets and PF resets and conclude whether there are
15170  * any issues regarding these resets. If there are any issues then
15171  * generate log entry.
15172  *
15173  * Return 0 if NIC is healthy or negative value when there are issues
15174  * with resets
15175  **/
15176 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15177 {
15178 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15179 	const bool is_empr = i40e_check_fw_empr(pf);
15180 
15181 	if (is_empr || pfr != I40E_SUCCESS)
15182 		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");
15183 
15184 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15185 }
15186 
15187 /**
15188  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15189  * @pf: board private structure
15190  * @hw: ptr to the hardware info
15191  *
15192  * This function does a minimal setup of all subsystems needed for running
15193  * recovery mode.
15194  *
15195  * Returns 0 on success, negative on failure
15196  **/
15197 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15198 {
15199 	struct i40e_vsi *vsi;
15200 	int err;
15201 	int v_idx;
15202 
15203 	pci_save_state(pf->pdev);
15204 
15205 	/* set up periodic task facility */
15206 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15207 	pf->service_timer_period = HZ;
15208 
15209 	INIT_WORK(&pf->service_task, i40e_service_task);
15210 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15211 
15212 	err = i40e_init_interrupt_scheme(pf);
15213 	if (err)
15214 		goto err_switch_setup;
15215 
15216 	/* The number of VSIs reported by the FW is the minimum guaranteed
15217 	 * to us; HW supports far more and we share the remaining pool with
15218 	 * the other PFs. We allocate space for more than the guarantee with
15219 	 * the understanding that we might not get them all later.
15220 	 */
15221 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15222 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15223 	else
15224 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15225 
15226 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15227 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15228 			  GFP_KERNEL);
15229 	if (!pf->vsi) {
15230 		err = -ENOMEM;
15231 		goto err_switch_setup;
15232 	}
15233 
15234 	/* We allocate one VSI which is needed as absolute minimum
15235 	 * in order to register the netdev
15236 	 */
15237 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15238 	if (v_idx < 0) {
15239 		err = v_idx;
15240 		goto err_switch_setup;
15241 	}
15242 	pf->lan_vsi = v_idx;
15243 	vsi = pf->vsi[v_idx];
15244 	if (!vsi) {
15245 		err = -EFAULT;
15246 		goto err_switch_setup;
15247 	}
15248 	vsi->alloc_queue_pairs = 1;
15249 	err = i40e_config_netdev(vsi);
15250 	if (err)
15251 		goto err_switch_setup;
15252 	err = register_netdev(vsi->netdev);
15253 	if (err)
15254 		goto err_switch_setup;
15255 	vsi->netdev_registered = true;
15256 	i40e_dbg_pf_init(pf);
15257 
15258 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15259 	if (err)
15260 		goto err_switch_setup;
15261 
15262 	/* tell the firmware that we're starting */
15263 	i40e_send_version(pf);
15264 
15265 	/* since everything's happy, start the service_task timer */
15266 	mod_timer(&pf->service_timer,
15267 		  round_jiffies(jiffies + pf->service_timer_period));
15268 
15269 	return 0;
15270 
15271 err_switch_setup:
15272 	i40e_reset_interrupt_capability(pf);
15273 	del_timer_sync(&pf->service_timer);
15274 	i40e_shutdown_adminq(hw);
15275 	iounmap(hw->hw_addr);
15276 	pci_disable_pcie_error_reporting(pf->pdev);
15277 	pci_release_mem_regions(pf->pdev);
15278 	pci_disable_device(pf->pdev);
15279 	kfree(pf);
15280 
15281 	return err;
15282 }
15283 
15284 /**
15285  * i40e_set_subsystem_device_id - set subsystem device id
15286  * @hw: pointer to the hardware info
15287  *
15288  * Set PCI subsystem device id either from a pci_dev structure or
15289  * a specific FW register.
15290  **/
15291 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15292 {
15293 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15294 
15295 	hw->subsystem_device_id = pdev->subsystem_device ?
15296 		pdev->subsystem_device :
15297 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15298 }
15299 
15300 /**
15301  * i40e_probe - Device initialization routine
15302  * @pdev: PCI device information struct
15303  * @ent: entry in i40e_pci_tbl
15304  *
15305  * i40e_probe initializes a PF identified by a pci_dev structure.
15306  * The OS initialization, configuring of the PF private structure,
15307  * and a hardware reset occur.
15308  *
15309  * Returns 0 on success, negative on failure
15310  **/
15311 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15312 {
15313 	struct i40e_aq_get_phy_abilities_resp abilities;
15314 #ifdef CONFIG_I40E_DCB
15315 	enum i40e_get_fw_lldp_status_resp lldp_status;
15316 	i40e_status status;
15317 #endif /* CONFIG_I40E_DCB */
15318 	struct i40e_pf *pf;
15319 	struct i40e_hw *hw;
15320 	static u16 pfs_found;
15321 	u16 wol_nvm_bits;
15322 	u16 link_status;
15323 	int err;
15324 	u32 val;
15325 	u32 i;
15326 
15327 	err = pci_enable_device_mem(pdev);
15328 	if (err)
15329 		return err;
15330 
15331 	/* set up for high or low dma */
15332 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15333 	if (err) {
15334 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
15335 		if (err) {
15336 			dev_err(&pdev->dev,
15337 				"DMA configuration failed: 0x%x\n", err);
15338 			goto err_dma;
15339 		}
15340 	}
15341 
15342 	/* set up pci connections */
15343 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15344 	if (err) {
15345 		dev_info(&pdev->dev,
15346 			 "pci_request_selected_regions failed %d\n", err);
15347 		goto err_pci_reg;
15348 	}
15349 
15350 	pci_enable_pcie_error_reporting(pdev);
15351 	pci_set_master(pdev);
15352 
15353 	/* Now that we have a PCI connection, we need to do the
15354 	 * low level device setup.  This is primarily setting up
15355 	 * the Admin Queue structures and then querying for the
15356 	 * device's current profile information.
15357 	 */
15358 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15359 	if (!pf) {
15360 		err = -ENOMEM;
15361 		goto err_pf_alloc;
15362 	}
15363 	pf->next_vsi = 0;
15364 	pf->pdev = pdev;
15365 	set_bit(__I40E_DOWN, pf->state);
15366 
15367 	hw = &pf->hw;
15368 	hw->back = pf;
15369 
15370 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15371 				I40E_MAX_CSR_SPACE);
15372 	/* We believe that the highest register to read is
15373 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15374 	 * is not less than that before mapping to prevent a
15375 	 * kernel panic.
15376 	 */
15377 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15378 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15379 			pf->ioremap_len);
15380 		err = -ENOMEM;
15381 		goto err_ioremap;
15382 	}
15383 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15384 	if (!hw->hw_addr) {
15385 		err = -EIO;
15386 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15387 			 (unsigned int)pci_resource_start(pdev, 0),
15388 			 pf->ioremap_len, err);
15389 		goto err_ioremap;
15390 	}
15391 	hw->vendor_id = pdev->vendor;
15392 	hw->device_id = pdev->device;
15393 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15394 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15395 	i40e_set_subsystem_device_id(hw);
15396 	hw->bus.device = PCI_SLOT(pdev->devfn);
15397 	hw->bus.func = PCI_FUNC(pdev->devfn);
15398 	hw->bus.bus_id = pdev->bus->number;
15399 	pf->instance = pfs_found;
15400 
15401 	/* Select something other than the 802.1ad ethertype for the
15402 	 * switch to use internally and drop on ingress.
15403 	 */
15404 	hw->switch_tag = 0xffff;
15405 	hw->first_tag = ETH_P_8021AD;
15406 	hw->second_tag = ETH_P_8021Q;
15407 
15408 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15409 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15410 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15411 
15412 	/* set up the locks for the AQ, do this only once in probe
15413 	 * and destroy them only once in remove
15414 	 */
15415 	mutex_init(&hw->aq.asq_mutex);
15416 	mutex_init(&hw->aq.arq_mutex);
15417 
15418 	pf->msg_enable = netif_msg_init(debug,
15419 					NETIF_MSG_DRV |
15420 					NETIF_MSG_PROBE |
15421 					NETIF_MSG_LINK);
15422 	if (debug < -1)
15423 		pf->hw.debug_mask = debug;
15424 
15425 	/* do a special CORER for clearing PXE mode once at init */
15426 	if (hw->revision_id == 0 &&
15427 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15428 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15429 		i40e_flush(hw);
15430 		msleep(200);
15431 		pf->corer_count++;
15432 
15433 		i40e_clear_pxe_mode(hw);
15434 	}
15435 
15436 	/* Reset here to make sure all is clean and to define PF 'n' */
15437 	i40e_clear_hw(hw);
15438 
15439 	err = i40e_set_mac_type(hw);
15440 	if (err) {
15441 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15442 			 err);
15443 		goto err_pf_reset;
15444 	}
15445 
15446 	err = i40e_handle_resets(pf);
15447 	if (err)
15448 		goto err_pf_reset;
15449 
15450 	i40e_check_recovery_mode(pf);
15451 
15452 	if (is_kdump_kernel()) {
15453 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15454 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15455 	} else {
15456 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15457 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15458 	}
15459 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15460 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15461 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15462 
15463 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15464 		 "%s-%s:misc",
15465 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15466 
15467 	err = i40e_init_shared_code(hw);
15468 	if (err) {
15469 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15470 			 err);
15471 		goto err_pf_reset;
15472 	}
15473 
15474 	/* set up a default setting for link flow control */
15475 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15476 
15477 	err = i40e_init_adminq(hw);
15478 	if (err) {
15479 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15480 			dev_info(&pdev->dev,
15481 				 "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",
15482 				 hw->aq.api_maj_ver,
15483 				 hw->aq.api_min_ver,
15484 				 I40E_FW_API_VERSION_MAJOR,
15485 				 I40E_FW_MINOR_VERSION(hw));
15486 		else
15487 			dev_info(&pdev->dev,
15488 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15489 
15490 		goto err_pf_reset;
15491 	}
15492 	i40e_get_oem_version(hw);
15493 
15494 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15495 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15496 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15497 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15498 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15499 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15500 
15501 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15502 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15503 		dev_dbg(&pdev->dev,
15504 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15505 			 hw->aq.api_maj_ver,
15506 			 hw->aq.api_min_ver,
15507 			 I40E_FW_API_VERSION_MAJOR,
15508 			 I40E_FW_MINOR_VERSION(hw));
15509 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15510 		dev_info(&pdev->dev,
15511 			 "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",
15512 			 hw->aq.api_maj_ver,
15513 			 hw->aq.api_min_ver,
15514 			 I40E_FW_API_VERSION_MAJOR,
15515 			 I40E_FW_MINOR_VERSION(hw));
15516 
15517 	i40e_verify_eeprom(pf);
15518 
15519 	/* Rev 0 hardware was never productized */
15520 	if (hw->revision_id < 1)
15521 		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");
15522 
15523 	i40e_clear_pxe_mode(hw);
15524 
15525 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15526 	if (err)
15527 		goto err_adminq_setup;
15528 
15529 	err = i40e_sw_init(pf);
15530 	if (err) {
15531 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15532 		goto err_sw_init;
15533 	}
15534 
15535 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15536 		return i40e_init_recovery_mode(pf, hw);
15537 
15538 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15539 				hw->func_caps.num_rx_qp, 0, 0);
15540 	if (err) {
15541 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15542 		goto err_init_lan_hmc;
15543 	}
15544 
15545 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15546 	if (err) {
15547 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15548 		err = -ENOENT;
15549 		goto err_configure_lan_hmc;
15550 	}
15551 
15552 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15553 	 * Ignore error return codes because if it was already disabled via
15554 	 * hardware settings this will fail
15555 	 */
15556 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15557 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15558 		i40e_aq_stop_lldp(hw, true, false, NULL);
15559 	}
15560 
15561 	/* allow a platform config to override the HW addr */
15562 	i40e_get_platform_mac_addr(pdev, pf);
15563 
15564 	if (!is_valid_ether_addr(hw->mac.addr)) {
15565 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15566 		err = -EIO;
15567 		goto err_mac_addr;
15568 	}
15569 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15570 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15571 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15572 	if (is_valid_ether_addr(hw->mac.port_addr))
15573 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15574 
15575 	i40e_ptp_alloc_pins(pf);
15576 	pci_set_drvdata(pdev, pf);
15577 	pci_save_state(pdev);
15578 
15579 #ifdef CONFIG_I40E_DCB
15580 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15581 	(!status &&
15582 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15583 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15584 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15585 	dev_info(&pdev->dev,
15586 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15587 			"FW LLDP is disabled\n" :
15588 			"FW LLDP is enabled\n");
15589 
15590 	/* Enable FW to write default DCB config on link-up */
15591 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15592 
15593 	err = i40e_init_pf_dcb(pf);
15594 	if (err) {
15595 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15596 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15597 		/* Continue without DCB enabled */
15598 	}
15599 #endif /* CONFIG_I40E_DCB */
15600 
15601 	/* set up periodic task facility */
15602 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15603 	pf->service_timer_period = HZ;
15604 
15605 	INIT_WORK(&pf->service_task, i40e_service_task);
15606 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15607 
15608 	/* NVM bit on means WoL disabled for the port */
15609 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15610 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15611 		pf->wol_en = false;
15612 	else
15613 		pf->wol_en = true;
15614 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15615 
15616 	/* set up the main switch operations */
15617 	i40e_determine_queue_usage(pf);
15618 	err = i40e_init_interrupt_scheme(pf);
15619 	if (err)
15620 		goto err_switch_setup;
15621 
15622 	/* Reduce Tx and Rx pairs for kdump
15623 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15624 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15625 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15626 	 */
15627 	if (is_kdump_kernel())
15628 		pf->num_lan_msix = 1;
15629 
15630 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15631 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15632 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15633 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15634 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15635 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15636 						    UDP_TUNNEL_TYPE_GENEVE;
15637 
15638 	/* The number of VSIs reported by the FW is the minimum guaranteed
15639 	 * to us; HW supports far more and we share the remaining pool with
15640 	 * the other PFs. We allocate space for more than the guarantee with
15641 	 * the understanding that we might not get them all later.
15642 	 */
15643 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15644 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15645 	else
15646 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15647 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15648 		dev_warn(&pf->pdev->dev,
15649 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15650 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15651 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15652 	}
15653 
15654 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15655 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15656 			  GFP_KERNEL);
15657 	if (!pf->vsi) {
15658 		err = -ENOMEM;
15659 		goto err_switch_setup;
15660 	}
15661 
15662 #ifdef CONFIG_PCI_IOV
15663 	/* prep for VF support */
15664 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15665 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15666 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15667 		if (pci_num_vf(pdev))
15668 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15669 	}
15670 #endif
15671 	err = i40e_setup_pf_switch(pf, false, false);
15672 	if (err) {
15673 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15674 		goto err_vsis;
15675 	}
15676 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15677 
15678 	/* if FDIR VSI was set up, start it now */
15679 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15680 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15681 			i40e_vsi_open(pf->vsi[i]);
15682 			break;
15683 		}
15684 	}
15685 
15686 	/* The driver only wants link up/down and module qualification
15687 	 * reports from firmware.  Note the negative logic.
15688 	 */
15689 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15690 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15691 					 I40E_AQ_EVENT_MEDIA_NA |
15692 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15693 	if (err)
15694 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15695 			 i40e_stat_str(&pf->hw, err),
15696 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15697 
15698 	/* Reconfigure hardware for allowing smaller MSS in the case
15699 	 * of TSO, so that we avoid the MDD being fired and causing
15700 	 * a reset in the case of small MSS+TSO.
15701 	 */
15702 	val = rd32(hw, I40E_REG_MSS);
15703 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15704 		val &= ~I40E_REG_MSS_MIN_MASK;
15705 		val |= I40E_64BYTE_MSS;
15706 		wr32(hw, I40E_REG_MSS, val);
15707 	}
15708 
15709 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15710 		msleep(75);
15711 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15712 		if (err)
15713 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15714 				 i40e_stat_str(&pf->hw, err),
15715 				 i40e_aq_str(&pf->hw,
15716 					     pf->hw.aq.asq_last_status));
15717 	}
15718 	/* The main driver is (mostly) up and happy. We need to set this state
15719 	 * before setting up the misc vector or we get a race and the vector
15720 	 * ends up disabled forever.
15721 	 */
15722 	clear_bit(__I40E_DOWN, pf->state);
15723 
15724 	/* In case of MSIX we are going to setup the misc vector right here
15725 	 * to handle admin queue events etc. In case of legacy and MSI
15726 	 * the misc functionality and queue processing is combined in
15727 	 * the same vector and that gets setup at open.
15728 	 */
15729 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15730 		err = i40e_setup_misc_vector(pf);
15731 		if (err) {
15732 			dev_info(&pdev->dev,
15733 				 "setup of misc vector failed: %d\n", err);
15734 			i40e_cloud_filter_exit(pf);
15735 			i40e_fdir_teardown(pf);
15736 			goto err_vsis;
15737 		}
15738 	}
15739 
15740 #ifdef CONFIG_PCI_IOV
15741 	/* prep for VF support */
15742 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15743 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15744 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15745 		/* disable link interrupts for VFs */
15746 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15747 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15748 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15749 		i40e_flush(hw);
15750 
15751 		if (pci_num_vf(pdev)) {
15752 			dev_info(&pdev->dev,
15753 				 "Active VFs found, allocating resources.\n");
15754 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15755 			if (err)
15756 				dev_info(&pdev->dev,
15757 					 "Error %d allocating resources for existing VFs\n",
15758 					 err);
15759 		}
15760 	}
15761 #endif /* CONFIG_PCI_IOV */
15762 
15763 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15764 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15765 						      pf->num_iwarp_msix,
15766 						      I40E_IWARP_IRQ_PILE_ID);
15767 		if (pf->iwarp_base_vector < 0) {
15768 			dev_info(&pdev->dev,
15769 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15770 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15771 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15772 		}
15773 	}
15774 
15775 	i40e_dbg_pf_init(pf);
15776 
15777 	/* tell the firmware that we're starting */
15778 	i40e_send_version(pf);
15779 
15780 	/* since everything's happy, start the service_task timer */
15781 	mod_timer(&pf->service_timer,
15782 		  round_jiffies(jiffies + pf->service_timer_period));
15783 
15784 	/* add this PF to client device list and launch a client service task */
15785 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15786 		err = i40e_lan_add_device(pf);
15787 		if (err)
15788 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15789 				 err);
15790 	}
15791 
15792 #define PCI_SPEED_SIZE 8
15793 #define PCI_WIDTH_SIZE 8
15794 	/* Devices on the IOSF bus do not have this information
15795 	 * and will report PCI Gen 1 x 1 by default so don't bother
15796 	 * checking them.
15797 	 */
15798 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15799 		char speed[PCI_SPEED_SIZE] = "Unknown";
15800 		char width[PCI_WIDTH_SIZE] = "Unknown";
15801 
15802 		/* Get the negotiated link width and speed from PCI config
15803 		 * space
15804 		 */
15805 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15806 					  &link_status);
15807 
15808 		i40e_set_pci_config_data(hw, link_status);
15809 
15810 		switch (hw->bus.speed) {
15811 		case i40e_bus_speed_8000:
15812 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15813 		case i40e_bus_speed_5000:
15814 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15815 		case i40e_bus_speed_2500:
15816 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15817 		default:
15818 			break;
15819 		}
15820 		switch (hw->bus.width) {
15821 		case i40e_bus_width_pcie_x8:
15822 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15823 		case i40e_bus_width_pcie_x4:
15824 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15825 		case i40e_bus_width_pcie_x2:
15826 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15827 		case i40e_bus_width_pcie_x1:
15828 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15829 		default:
15830 			break;
15831 		}
15832 
15833 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15834 			 speed, width);
15835 
15836 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15837 		    hw->bus.speed < i40e_bus_speed_8000) {
15838 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15839 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15840 		}
15841 	}
15842 
15843 	/* get the requested speeds from the fw */
15844 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15845 	if (err)
15846 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15847 			i40e_stat_str(&pf->hw, err),
15848 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15849 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15850 
15851 	/* set the FEC config due to the board capabilities */
15852 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15853 
15854 	/* get the supported phy types from the fw */
15855 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15856 	if (err)
15857 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15858 			i40e_stat_str(&pf->hw, err),
15859 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15860 
15861 	/* make sure the MFS hasn't been set lower than the default */
15862 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15863 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15864 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15865 	if (val < MAX_FRAME_SIZE_DEFAULT)
15866 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15867 			 i, val);
15868 
15869 	/* Add a filter to drop all Flow control frames from any VSI from being
15870 	 * transmitted. By doing so we stop a malicious VF from sending out
15871 	 * PAUSE or PFC frames and potentially controlling traffic for other
15872 	 * PF/VF VSIs.
15873 	 * The FW can still send Flow control frames if enabled.
15874 	 */
15875 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15876 						       pf->main_vsi_seid);
15877 
15878 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15879 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15880 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15881 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15882 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15883 	/* print a string summarizing features */
15884 	i40e_print_features(pf);
15885 
15886 	return 0;
15887 
15888 	/* Unwind what we've done if something failed in the setup */
15889 err_vsis:
15890 	set_bit(__I40E_DOWN, pf->state);
15891 	i40e_clear_interrupt_scheme(pf);
15892 	kfree(pf->vsi);
15893 err_switch_setup:
15894 	i40e_reset_interrupt_capability(pf);
15895 	del_timer_sync(&pf->service_timer);
15896 err_mac_addr:
15897 err_configure_lan_hmc:
15898 	(void)i40e_shutdown_lan_hmc(hw);
15899 err_init_lan_hmc:
15900 	kfree(pf->qp_pile);
15901 err_sw_init:
15902 err_adminq_setup:
15903 err_pf_reset:
15904 	iounmap(hw->hw_addr);
15905 err_ioremap:
15906 	kfree(pf);
15907 err_pf_alloc:
15908 	pci_disable_pcie_error_reporting(pdev);
15909 	pci_release_mem_regions(pdev);
15910 err_pci_reg:
15911 err_dma:
15912 	pci_disable_device(pdev);
15913 	return err;
15914 }
15915 
15916 /**
15917  * i40e_remove - Device removal routine
15918  * @pdev: PCI device information struct
15919  *
15920  * i40e_remove is called by the PCI subsystem to alert the driver
15921  * that is should release a PCI device.  This could be caused by a
15922  * Hot-Plug event, or because the driver is going to be removed from
15923  * memory.
15924  **/
15925 static void i40e_remove(struct pci_dev *pdev)
15926 {
15927 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15928 	struct i40e_hw *hw = &pf->hw;
15929 	i40e_status ret_code;
15930 	int i;
15931 
15932 	i40e_dbg_pf_exit(pf);
15933 
15934 	i40e_ptp_stop(pf);
15935 
15936 	/* Disable RSS in hw */
15937 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15938 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15939 
15940 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
15941 	 * flags, once they are set, i40e_rebuild should not be called as
15942 	 * i40e_prep_for_reset always returns early.
15943 	 */
15944 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15945 		usleep_range(1000, 2000);
15946 	set_bit(__I40E_IN_REMOVE, pf->state);
15947 
15948 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15949 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15950 		i40e_free_vfs(pf);
15951 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15952 	}
15953 	/* no more scheduling of any task */
15954 	set_bit(__I40E_SUSPENDED, pf->state);
15955 	set_bit(__I40E_DOWN, pf->state);
15956 	if (pf->service_timer.function)
15957 		del_timer_sync(&pf->service_timer);
15958 	if (pf->service_task.func)
15959 		cancel_work_sync(&pf->service_task);
15960 
15961 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15962 		struct i40e_vsi *vsi = pf->vsi[0];
15963 
15964 		/* We know that we have allocated only one vsi for this PF,
15965 		 * it was just for registering netdevice, so the interface
15966 		 * could be visible in the 'ifconfig' output
15967 		 */
15968 		unregister_netdev(vsi->netdev);
15969 		free_netdev(vsi->netdev);
15970 
15971 		goto unmap;
15972 	}
15973 
15974 	/* Client close must be called explicitly here because the timer
15975 	 * has been stopped.
15976 	 */
15977 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15978 
15979 	i40e_fdir_teardown(pf);
15980 
15981 	/* If there is a switch structure or any orphans, remove them.
15982 	 * This will leave only the PF's VSI remaining.
15983 	 */
15984 	for (i = 0; i < I40E_MAX_VEB; i++) {
15985 		if (!pf->veb[i])
15986 			continue;
15987 
15988 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15989 		    pf->veb[i]->uplink_seid == 0)
15990 			i40e_switch_branch_release(pf->veb[i]);
15991 	}
15992 
15993 	/* Now we can shutdown the PF's VSI, just before we kill
15994 	 * adminq and hmc.
15995 	 */
15996 	if (pf->vsi[pf->lan_vsi])
15997 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15998 
15999 	i40e_cloud_filter_exit(pf);
16000 
16001 	/* remove attached clients */
16002 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16003 		ret_code = i40e_lan_del_device(pf);
16004 		if (ret_code)
16005 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16006 				 ret_code);
16007 	}
16008 
16009 	/* shutdown and destroy the HMC */
16010 	if (hw->hmc.hmc_obj) {
16011 		ret_code = i40e_shutdown_lan_hmc(hw);
16012 		if (ret_code)
16013 			dev_warn(&pdev->dev,
16014 				 "Failed to destroy the HMC resources: %d\n",
16015 				 ret_code);
16016 	}
16017 
16018 unmap:
16019 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16020 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16021 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16022 		free_irq(pf->pdev->irq, pf);
16023 
16024 	/* shutdown the adminq */
16025 	i40e_shutdown_adminq(hw);
16026 
16027 	/* destroy the locks only once, here */
16028 	mutex_destroy(&hw->aq.arq_mutex);
16029 	mutex_destroy(&hw->aq.asq_mutex);
16030 
16031 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16032 	rtnl_lock();
16033 	i40e_clear_interrupt_scheme(pf);
16034 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16035 		if (pf->vsi[i]) {
16036 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16037 				i40e_vsi_clear_rings(pf->vsi[i]);
16038 			i40e_vsi_clear(pf->vsi[i]);
16039 			pf->vsi[i] = NULL;
16040 		}
16041 	}
16042 	rtnl_unlock();
16043 
16044 	for (i = 0; i < I40E_MAX_VEB; i++) {
16045 		kfree(pf->veb[i]);
16046 		pf->veb[i] = NULL;
16047 	}
16048 
16049 	kfree(pf->qp_pile);
16050 	kfree(pf->vsi);
16051 
16052 	iounmap(hw->hw_addr);
16053 	kfree(pf);
16054 	pci_release_mem_regions(pdev);
16055 
16056 	pci_disable_pcie_error_reporting(pdev);
16057 	pci_disable_device(pdev);
16058 }
16059 
16060 /**
16061  * i40e_pci_error_detected - warning that something funky happened in PCI land
16062  * @pdev: PCI device information struct
16063  * @error: the type of PCI error
16064  *
16065  * Called to warn that something happened and the error handling steps
16066  * are in progress.  Allows the driver to quiesce things, be ready for
16067  * remediation.
16068  **/
16069 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16070 						pci_channel_state_t error)
16071 {
16072 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16073 
16074 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16075 
16076 	if (!pf) {
16077 		dev_info(&pdev->dev,
16078 			 "Cannot recover - error happened during device probe\n");
16079 		return PCI_ERS_RESULT_DISCONNECT;
16080 	}
16081 
16082 	/* shutdown all operations */
16083 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16084 		i40e_prep_for_reset(pf);
16085 
16086 	/* Request a slot reset */
16087 	return PCI_ERS_RESULT_NEED_RESET;
16088 }
16089 
16090 /**
16091  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16092  * @pdev: PCI device information struct
16093  *
16094  * Called to find if the driver can work with the device now that
16095  * the pci slot has been reset.  If a basic connection seems good
16096  * (registers are readable and have sane content) then return a
16097  * happy little PCI_ERS_RESULT_xxx.
16098  **/
16099 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16100 {
16101 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16102 	pci_ers_result_t result;
16103 	u32 reg;
16104 
16105 	dev_dbg(&pdev->dev, "%s\n", __func__);
16106 	if (pci_enable_device_mem(pdev)) {
16107 		dev_info(&pdev->dev,
16108 			 "Cannot re-enable PCI device after reset.\n");
16109 		result = PCI_ERS_RESULT_DISCONNECT;
16110 	} else {
16111 		pci_set_master(pdev);
16112 		pci_restore_state(pdev);
16113 		pci_save_state(pdev);
16114 		pci_wake_from_d3(pdev, false);
16115 
16116 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16117 		if (reg == 0)
16118 			result = PCI_ERS_RESULT_RECOVERED;
16119 		else
16120 			result = PCI_ERS_RESULT_DISCONNECT;
16121 	}
16122 
16123 	return result;
16124 }
16125 
16126 /**
16127  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16128  * @pdev: PCI device information struct
16129  */
16130 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16131 {
16132 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16133 
16134 	i40e_prep_for_reset(pf);
16135 }
16136 
16137 /**
16138  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16139  * @pdev: PCI device information struct
16140  */
16141 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16142 {
16143 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16144 
16145 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16146 		return;
16147 
16148 	i40e_reset_and_rebuild(pf, false, false);
16149 }
16150 
16151 /**
16152  * i40e_pci_error_resume - restart operations after PCI error recovery
16153  * @pdev: PCI device information struct
16154  *
16155  * Called to allow the driver to bring things back up after PCI error
16156  * and/or reset recovery has finished.
16157  **/
16158 static void i40e_pci_error_resume(struct pci_dev *pdev)
16159 {
16160 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16161 
16162 	dev_dbg(&pdev->dev, "%s\n", __func__);
16163 	if (test_bit(__I40E_SUSPENDED, pf->state))
16164 		return;
16165 
16166 	i40e_handle_reset_warning(pf, false);
16167 }
16168 
16169 /**
16170  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16171  * using the mac_address_write admin q function
16172  * @pf: pointer to i40e_pf struct
16173  **/
16174 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16175 {
16176 	struct i40e_hw *hw = &pf->hw;
16177 	i40e_status ret;
16178 	u8 mac_addr[6];
16179 	u16 flags = 0;
16180 
16181 	/* Get current MAC address in case it's an LAA */
16182 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16183 		ether_addr_copy(mac_addr,
16184 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16185 	} else {
16186 		dev_err(&pf->pdev->dev,
16187 			"Failed to retrieve MAC address; using default\n");
16188 		ether_addr_copy(mac_addr, hw->mac.addr);
16189 	}
16190 
16191 	/* The FW expects the mac address write cmd to first be called with
16192 	 * one of these flags before calling it again with the multicast
16193 	 * enable flags.
16194 	 */
16195 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16196 
16197 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16198 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16199 
16200 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16201 	if (ret) {
16202 		dev_err(&pf->pdev->dev,
16203 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16204 		return;
16205 	}
16206 
16207 	flags = I40E_AQC_MC_MAG_EN
16208 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16209 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16210 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16211 	if (ret)
16212 		dev_err(&pf->pdev->dev,
16213 			"Failed to enable Multicast Magic Packet wake up\n");
16214 }
16215 
16216 /**
16217  * i40e_shutdown - PCI callback for shutting down
16218  * @pdev: PCI device information struct
16219  **/
16220 static void i40e_shutdown(struct pci_dev *pdev)
16221 {
16222 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16223 	struct i40e_hw *hw = &pf->hw;
16224 
16225 	set_bit(__I40E_SUSPENDED, pf->state);
16226 	set_bit(__I40E_DOWN, pf->state);
16227 
16228 	del_timer_sync(&pf->service_timer);
16229 	cancel_work_sync(&pf->service_task);
16230 	i40e_cloud_filter_exit(pf);
16231 	i40e_fdir_teardown(pf);
16232 
16233 	/* Client close must be called explicitly here because the timer
16234 	 * has been stopped.
16235 	 */
16236 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16237 
16238 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16239 		i40e_enable_mc_magic_wake(pf);
16240 
16241 	i40e_prep_for_reset(pf);
16242 
16243 	wr32(hw, I40E_PFPM_APM,
16244 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16245 	wr32(hw, I40E_PFPM_WUFC,
16246 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16247 
16248 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16249 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16250 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16251 		free_irq(pf->pdev->irq, pf);
16252 
16253 	/* Since we're going to destroy queues during the
16254 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16255 	 * whole section
16256 	 */
16257 	rtnl_lock();
16258 	i40e_clear_interrupt_scheme(pf);
16259 	rtnl_unlock();
16260 
16261 	if (system_state == SYSTEM_POWER_OFF) {
16262 		pci_wake_from_d3(pdev, pf->wol_en);
16263 		pci_set_power_state(pdev, PCI_D3hot);
16264 	}
16265 }
16266 
16267 /**
16268  * i40e_suspend - PM callback for moving to D3
16269  * @dev: generic device information structure
16270  **/
16271 static int __maybe_unused i40e_suspend(struct device *dev)
16272 {
16273 	struct i40e_pf *pf = dev_get_drvdata(dev);
16274 	struct i40e_hw *hw = &pf->hw;
16275 
16276 	/* If we're already suspended, then there is nothing to do */
16277 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16278 		return 0;
16279 
16280 	set_bit(__I40E_DOWN, pf->state);
16281 
16282 	/* Ensure service task will not be running */
16283 	del_timer_sync(&pf->service_timer);
16284 	cancel_work_sync(&pf->service_task);
16285 
16286 	/* Client close must be called explicitly here because the timer
16287 	 * has been stopped.
16288 	 */
16289 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16290 
16291 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16292 		i40e_enable_mc_magic_wake(pf);
16293 
16294 	/* Since we're going to destroy queues during the
16295 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16296 	 * whole section
16297 	 */
16298 	rtnl_lock();
16299 
16300 	i40e_prep_for_reset(pf);
16301 
16302 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16303 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16304 
16305 	/* Clear the interrupt scheme and release our IRQs so that the system
16306 	 * can safely hibernate even when there are a large number of CPUs.
16307 	 * Otherwise hibernation might fail when mapping all the vectors back
16308 	 * to CPU0.
16309 	 */
16310 	i40e_clear_interrupt_scheme(pf);
16311 
16312 	rtnl_unlock();
16313 
16314 	return 0;
16315 }
16316 
16317 /**
16318  * i40e_resume - PM callback for waking up from D3
16319  * @dev: generic device information structure
16320  **/
16321 static int __maybe_unused i40e_resume(struct device *dev)
16322 {
16323 	struct i40e_pf *pf = dev_get_drvdata(dev);
16324 	int err;
16325 
16326 	/* If we're not suspended, then there is nothing to do */
16327 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16328 		return 0;
16329 
16330 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16331 	 * since we're going to be restoring queues
16332 	 */
16333 	rtnl_lock();
16334 
16335 	/* We cleared the interrupt scheme when we suspended, so we need to
16336 	 * restore it now to resume device functionality.
16337 	 */
16338 	err = i40e_restore_interrupt_scheme(pf);
16339 	if (err) {
16340 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16341 			err);
16342 	}
16343 
16344 	clear_bit(__I40E_DOWN, pf->state);
16345 	i40e_reset_and_rebuild(pf, false, true);
16346 
16347 	rtnl_unlock();
16348 
16349 	/* Clear suspended state last after everything is recovered */
16350 	clear_bit(__I40E_SUSPENDED, pf->state);
16351 
16352 	/* Restart the service task */
16353 	mod_timer(&pf->service_timer,
16354 		  round_jiffies(jiffies + pf->service_timer_period));
16355 
16356 	return 0;
16357 }
16358 
16359 static const struct pci_error_handlers i40e_err_handler = {
16360 	.error_detected = i40e_pci_error_detected,
16361 	.slot_reset = i40e_pci_error_slot_reset,
16362 	.reset_prepare = i40e_pci_error_reset_prepare,
16363 	.reset_done = i40e_pci_error_reset_done,
16364 	.resume = i40e_pci_error_resume,
16365 };
16366 
16367 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16368 
16369 static struct pci_driver i40e_driver = {
16370 	.name     = i40e_driver_name,
16371 	.id_table = i40e_pci_tbl,
16372 	.probe    = i40e_probe,
16373 	.remove   = i40e_remove,
16374 	.driver   = {
16375 		.pm = &i40e_pm_ops,
16376 	},
16377 	.shutdown = i40e_shutdown,
16378 	.err_handler = &i40e_err_handler,
16379 	.sriov_configure = i40e_pci_sriov_configure,
16380 };
16381 
16382 /**
16383  * i40e_init_module - Driver registration routine
16384  *
16385  * i40e_init_module is the first routine called when the driver is
16386  * loaded. All it does is register with the PCI subsystem.
16387  **/
16388 static int __init i40e_init_module(void)
16389 {
16390 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16391 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16392 
16393 	/* There is no need to throttle the number of active tasks because
16394 	 * each device limits its own task using a state bit for scheduling
16395 	 * the service task, and the device tasks do not interfere with each
16396 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16397 	 * since we need to be able to guarantee forward progress even under
16398 	 * memory pressure.
16399 	 */
16400 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16401 	if (!i40e_wq) {
16402 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16403 		return -ENOMEM;
16404 	}
16405 
16406 	i40e_dbg_init();
16407 	return pci_register_driver(&i40e_driver);
16408 }
16409 module_init(i40e_init_module);
16410 
16411 /**
16412  * i40e_exit_module - Driver exit cleanup routine
16413  *
16414  * i40e_exit_module is called just before the driver is removed
16415  * from memory.
16416  **/
16417 static void __exit i40e_exit_module(void)
16418 {
16419 	pci_unregister_driver(&i40e_driver);
16420 	destroy_workqueue(i40e_wq);
16421 	ida_destroy(&i40e_client_ida);
16422 	i40e_dbg_exit();
16423 }
16424 module_exit(i40e_exit_module);
16425