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_1G_BASE_T_BC), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
88 	/* required last entry */
89 	{0, }
90 };
91 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
92 
93 #define I40E_MAX_VF_COUNT 128
94 static int debug = -1;
95 module_param(debug, uint, 0);
96 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
97 
98 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
99 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
100 MODULE_LICENSE("GPL v2");
101 
102 static struct workqueue_struct *i40e_wq;
103 
104 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
105 				  struct net_device *netdev, int delta)
106 {
107 	struct netdev_hw_addr *ha;
108 
109 	if (!f || !netdev)
110 		return;
111 
112 	netdev_for_each_mc_addr(ha, netdev) {
113 		if (ether_addr_equal(ha->addr, f->macaddr)) {
114 			ha->refcount += delta;
115 			if (ha->refcount <= 0)
116 				ha->refcount = 1;
117 			break;
118 		}
119 	}
120 }
121 
122 /**
123  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
124  * @hw:   pointer to the HW structure
125  * @mem:  ptr to mem struct to fill out
126  * @size: size of memory requested
127  * @alignment: what to align the allocation to
128  **/
129 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
130 			    u64 size, u32 alignment)
131 {
132 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
133 
134 	mem->size = ALIGN(size, alignment);
135 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
136 				     GFP_KERNEL);
137 	if (!mem->va)
138 		return -ENOMEM;
139 
140 	return 0;
141 }
142 
143 /**
144  * i40e_free_dma_mem_d - OS specific memory free for shared code
145  * @hw:   pointer to the HW structure
146  * @mem:  ptr to mem struct to free
147  **/
148 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
149 {
150 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
151 
152 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
153 	mem->va = NULL;
154 	mem->pa = 0;
155 	mem->size = 0;
156 
157 	return 0;
158 }
159 
160 /**
161  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
162  * @hw:   pointer to the HW structure
163  * @mem:  ptr to mem struct to fill out
164  * @size: size of memory requested
165  **/
166 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
167 			     u32 size)
168 {
169 	mem->size = size;
170 	mem->va = kzalloc(size, GFP_KERNEL);
171 
172 	if (!mem->va)
173 		return -ENOMEM;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_free_virt_mem_d - OS specific memory free for shared code
180  * @hw:   pointer to the HW structure
181  * @mem:  ptr to mem struct to free
182  **/
183 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
184 {
185 	/* it's ok to kfree a NULL pointer */
186 	kfree(mem->va);
187 	mem->va = NULL;
188 	mem->size = 0;
189 
190 	return 0;
191 }
192 
193 /**
194  * i40e_get_lump - find a lump of free generic resource
195  * @pf: board private structure
196  * @pile: the pile of resource to search
197  * @needed: the number of items needed
198  * @id: an owner id to stick on the items assigned
199  *
200  * Returns the base item index of the lump, or negative for error
201  **/
202 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
203 			 u16 needed, u16 id)
204 {
205 	int ret = -ENOMEM;
206 	int i, j;
207 
208 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
209 		dev_info(&pf->pdev->dev,
210 			 "param err: pile=%s needed=%d id=0x%04x\n",
211 			 pile ? "<valid>" : "<null>", needed, id);
212 		return -EINVAL;
213 	}
214 
215 	/* Allocate last queue in the pile for FDIR VSI queue
216 	 * so it doesn't fragment the qp_pile
217 	 */
218 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
219 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
220 			dev_err(&pf->pdev->dev,
221 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
222 				pile->num_entries - 1);
223 			return -ENOMEM;
224 		}
225 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
226 		return pile->num_entries - 1;
227 	}
228 
229 	i = 0;
230 	while (i < pile->num_entries) {
231 		/* skip already allocated entries */
232 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
233 			i++;
234 			continue;
235 		}
236 
237 		/* do we have enough in this lump? */
238 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
239 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
240 				break;
241 		}
242 
243 		if (j == needed) {
244 			/* there was enough, so assign it to the requestor */
245 			for (j = 0; j < needed; j++)
246 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
247 			ret = i;
248 			break;
249 		}
250 
251 		/* not enough, so skip over it and continue looking */
252 		i += j;
253 	}
254 
255 	return ret;
256 }
257 
258 /**
259  * i40e_put_lump - return a lump of generic resource
260  * @pile: the pile of resource to search
261  * @index: the base item index
262  * @id: the owner id of the items assigned
263  *
264  * Returns the count of items in the lump
265  **/
266 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
267 {
268 	int valid_id = (id | I40E_PILE_VALID_BIT);
269 	int count = 0;
270 	u16 i;
271 
272 	if (!pile || index >= pile->num_entries)
273 		return -EINVAL;
274 
275 	for (i = index;
276 	     i < pile->num_entries && pile->list[i] == valid_id;
277 	     i++) {
278 		pile->list[i] = 0;
279 		count++;
280 	}
281 
282 
283 	return count;
284 }
285 
286 /**
287  * i40e_find_vsi_from_id - searches for the vsi with the given id
288  * @pf: the pf structure to search for the vsi
289  * @id: id of the vsi it is searching for
290  **/
291 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
292 {
293 	int i;
294 
295 	for (i = 0; i < pf->num_alloc_vsi; i++)
296 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
297 			return pf->vsi[i];
298 
299 	return NULL;
300 }
301 
302 /**
303  * i40e_service_event_schedule - Schedule the service task to wake up
304  * @pf: board private structure
305  *
306  * If not already scheduled, this puts the task into the work queue
307  **/
308 void i40e_service_event_schedule(struct i40e_pf *pf)
309 {
310 	if ((!test_bit(__I40E_DOWN, pf->state) &&
311 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
312 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
313 		queue_work(i40e_wq, &pf->service_task);
314 }
315 
316 /**
317  * i40e_tx_timeout - Respond to a Tx Hang
318  * @netdev: network interface device structure
319  * @txqueue: queue number timing out
320  *
321  * If any port has noticed a Tx timeout, it is likely that the whole
322  * device is munged, not just the one netdev port, so go for the full
323  * reset.
324  **/
325 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
326 {
327 	struct i40e_netdev_priv *np = netdev_priv(netdev);
328 	struct i40e_vsi *vsi = np->vsi;
329 	struct i40e_pf *pf = vsi->back;
330 	struct i40e_ring *tx_ring = NULL;
331 	unsigned int i;
332 	u32 head, val;
333 
334 	pf->tx_timeout_count++;
335 
336 	/* with txqueue index, find the tx_ring struct */
337 	for (i = 0; i < vsi->num_queue_pairs; i++) {
338 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 			if (txqueue ==
340 			    vsi->tx_rings[i]->queue_index) {
341 				tx_ring = vsi->tx_rings[i];
342 				break;
343 			}
344 		}
345 	}
346 
347 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
348 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
349 	else if (time_before(jiffies,
350 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 		return;   /* don't do any new action before the next timeout */
352 
353 	/* don't kick off another recovery if one is already pending */
354 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
355 		return;
356 
357 	if (tx_ring) {
358 		head = i40e_get_head(tx_ring);
359 		/* Read interrupt register */
360 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
361 			val = rd32(&pf->hw,
362 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
363 						tx_ring->vsi->base_vector - 1));
364 		else
365 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
366 
367 		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",
368 			    vsi->seid, txqueue, tx_ring->next_to_clean,
369 			    head, tx_ring->next_to_use,
370 			    readl(tx_ring->tail), val);
371 	}
372 
373 	pf->tx_timeout_last_recovery = jiffies;
374 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
375 		    pf->tx_timeout_recovery_level, txqueue);
376 
377 	switch (pf->tx_timeout_recovery_level) {
378 	case 1:
379 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
380 		break;
381 	case 2:
382 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
383 		break;
384 	case 3:
385 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
386 		break;
387 	default:
388 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
389 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
390 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
391 		break;
392 	}
393 
394 	i40e_service_event_schedule(pf);
395 	pf->tx_timeout_recovery_level++;
396 }
397 
398 /**
399  * i40e_get_vsi_stats_struct - Get System Network Statistics
400  * @vsi: the VSI we care about
401  *
402  * Returns the address of the device statistics structure.
403  * The statistics are actually updated from the service task.
404  **/
405 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406 {
407 	return &vsi->net_stats;
408 }
409 
410 /**
411  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412  * @ring: Tx ring to get statistics from
413  * @stats: statistics entry to be updated
414  **/
415 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 					    struct rtnl_link_stats64 *stats)
417 {
418 	u64 bytes, packets;
419 	unsigned int start;
420 
421 	do {
422 		start = u64_stats_fetch_begin_irq(&ring->syncp);
423 		packets = ring->stats.packets;
424 		bytes   = ring->stats.bytes;
425 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
426 
427 	stats->tx_packets += packets;
428 	stats->tx_bytes   += bytes;
429 }
430 
431 /**
432  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433  * @netdev: network interface device structure
434  * @stats: data structure to store statistics
435  *
436  * Returns the address of the device statistics structure.
437  * The statistics are actually updated from the service task.
438  **/
439 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
440 				  struct rtnl_link_stats64 *stats)
441 {
442 	struct i40e_netdev_priv *np = netdev_priv(netdev);
443 	struct i40e_vsi *vsi = np->vsi;
444 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 	struct i40e_ring *ring;
446 	int i;
447 
448 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
449 		return;
450 
451 	if (!vsi->tx_rings)
452 		return;
453 
454 	rcu_read_lock();
455 	for (i = 0; i < vsi->num_queue_pairs; i++) {
456 		u64 bytes, packets;
457 		unsigned int start;
458 
459 		ring = READ_ONCE(vsi->tx_rings[i]);
460 		if (!ring)
461 			continue;
462 		i40e_get_netdev_stats_struct_tx(ring, stats);
463 
464 		if (i40e_enabled_xdp_vsi(vsi)) {
465 			ring = READ_ONCE(vsi->xdp_rings[i]);
466 			if (!ring)
467 				continue;
468 			i40e_get_netdev_stats_struct_tx(ring, stats);
469 		}
470 
471 		ring = READ_ONCE(vsi->rx_rings[i]);
472 		if (!ring)
473 			continue;
474 		do {
475 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
476 			packets = ring->stats.packets;
477 			bytes   = ring->stats.bytes;
478 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
479 
480 		stats->rx_packets += packets;
481 		stats->rx_bytes   += bytes;
482 
483 	}
484 	rcu_read_unlock();
485 
486 	/* following stats updated by i40e_watchdog_subtask() */
487 	stats->multicast	= vsi_stats->multicast;
488 	stats->tx_errors	= vsi_stats->tx_errors;
489 	stats->tx_dropped	= vsi_stats->tx_dropped;
490 	stats->rx_errors	= vsi_stats->rx_errors;
491 	stats->rx_dropped	= vsi_stats->rx_dropped;
492 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
493 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
494 }
495 
496 /**
497  * i40e_vsi_reset_stats - Resets all stats of the given vsi
498  * @vsi: the VSI to have its stats reset
499  **/
500 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
501 {
502 	struct rtnl_link_stats64 *ns;
503 	int i;
504 
505 	if (!vsi)
506 		return;
507 
508 	ns = i40e_get_vsi_stats_struct(vsi);
509 	memset(ns, 0, sizeof(*ns));
510 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
511 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
512 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
513 	if (vsi->rx_rings && vsi->rx_rings[0]) {
514 		for (i = 0; i < vsi->num_queue_pairs; i++) {
515 			memset(&vsi->rx_rings[i]->stats, 0,
516 			       sizeof(vsi->rx_rings[i]->stats));
517 			memset(&vsi->rx_rings[i]->rx_stats, 0,
518 			       sizeof(vsi->rx_rings[i]->rx_stats));
519 			memset(&vsi->tx_rings[i]->stats, 0,
520 			       sizeof(vsi->tx_rings[i]->stats));
521 			memset(&vsi->tx_rings[i]->tx_stats, 0,
522 			       sizeof(vsi->tx_rings[i]->tx_stats));
523 		}
524 	}
525 	vsi->stat_offsets_loaded = false;
526 }
527 
528 /**
529  * i40e_pf_reset_stats - Reset all of the stats for the given PF
530  * @pf: the PF to be reset
531  **/
532 void i40e_pf_reset_stats(struct i40e_pf *pf)
533 {
534 	int i;
535 
536 	memset(&pf->stats, 0, sizeof(pf->stats));
537 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
538 	pf->stat_offsets_loaded = false;
539 
540 	for (i = 0; i < I40E_MAX_VEB; i++) {
541 		if (pf->veb[i]) {
542 			memset(&pf->veb[i]->stats, 0,
543 			       sizeof(pf->veb[i]->stats));
544 			memset(&pf->veb[i]->stats_offsets, 0,
545 			       sizeof(pf->veb[i]->stats_offsets));
546 			memset(&pf->veb[i]->tc_stats, 0,
547 			       sizeof(pf->veb[i]->tc_stats));
548 			memset(&pf->veb[i]->tc_stats_offsets, 0,
549 			       sizeof(pf->veb[i]->tc_stats_offsets));
550 			pf->veb[i]->stat_offsets_loaded = false;
551 		}
552 	}
553 	pf->hw_csum_rx_error = 0;
554 }
555 
556 /**
557  * i40e_compute_pci_to_hw_id - compute index form PCI function.
558  * @vsi: ptr to the VSI to read from.
559  * @hw: ptr to the hardware info.
560  **/
561 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
562 {
563 	int pf_count = i40e_get_pf_count(hw);
564 
565 	if (vsi->type == I40E_VSI_SRIOV)
566 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
567 
568 	return hw->port + BIT(7);
569 }
570 
571 /**
572  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
573  * @hw: ptr to the hardware info.
574  * @hireg: the high 32 bit reg to read.
575  * @loreg: the low 32 bit reg to read.
576  * @offset_loaded: has the initial offset been loaded yet.
577  * @offset: ptr to current offset value.
578  * @stat: ptr to the stat.
579  *
580  * Since the device stats are not reset at PFReset, they will not
581  * be zeroed when the driver starts.  We'll save the first values read
582  * and use them as offsets to be subtracted from the raw values in order
583  * to report stats that count from zero.
584  **/
585 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
586 			       bool offset_loaded, u64 *offset, u64 *stat)
587 {
588 	u64 new_data;
589 
590 	new_data = rd64(hw, loreg);
591 
592 	if (!offset_loaded || new_data < *offset)
593 		*offset = new_data;
594 	*stat = new_data - *offset;
595 }
596 
597 /**
598  * i40e_stat_update48 - read and update a 48 bit stat from the chip
599  * @hw: ptr to the hardware info
600  * @hireg: the high 32 bit reg to read
601  * @loreg: the low 32 bit reg to read
602  * @offset_loaded: has the initial offset been loaded yet
603  * @offset: ptr to current offset value
604  * @stat: ptr to the stat
605  *
606  * Since the device stats are not reset at PFReset, they likely will not
607  * be zeroed when the driver starts.  We'll save the first values read
608  * and use them as offsets to be subtracted from the raw values in order
609  * to report stats that count from zero.  In the process, we also manage
610  * the potential roll-over.
611  **/
612 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
613 			       bool offset_loaded, u64 *offset, u64 *stat)
614 {
615 	u64 new_data;
616 
617 	if (hw->device_id == I40E_DEV_ID_QEMU) {
618 		new_data = rd32(hw, loreg);
619 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
620 	} else {
621 		new_data = rd64(hw, loreg);
622 	}
623 	if (!offset_loaded)
624 		*offset = new_data;
625 	if (likely(new_data >= *offset))
626 		*stat = new_data - *offset;
627 	else
628 		*stat = (new_data + BIT_ULL(48)) - *offset;
629 	*stat &= 0xFFFFFFFFFFFFULL;
630 }
631 
632 /**
633  * i40e_stat_update32 - read and update a 32 bit stat from the chip
634  * @hw: ptr to the hardware info
635  * @reg: the hw reg to read
636  * @offset_loaded: has the initial offset been loaded yet
637  * @offset: ptr to current offset value
638  * @stat: ptr to the stat
639  **/
640 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
641 			       bool offset_loaded, u64 *offset, u64 *stat)
642 {
643 	u32 new_data;
644 
645 	new_data = rd32(hw, reg);
646 	if (!offset_loaded)
647 		*offset = new_data;
648 	if (likely(new_data >= *offset))
649 		*stat = (u32)(new_data - *offset);
650 	else
651 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
652 }
653 
654 /**
655  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
656  * @hw: ptr to the hardware info
657  * @reg: the hw reg to read and clear
658  * @stat: ptr to the stat
659  **/
660 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
661 {
662 	u32 new_data = rd32(hw, reg);
663 
664 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
665 	*stat += new_data;
666 }
667 
668 /**
669  * i40e_stats_update_rx_discards - update rx_discards.
670  * @vsi: ptr to the VSI to be updated.
671  * @hw: ptr to the hardware info.
672  * @stat_idx: VSI's stat_counter_idx.
673  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
674  * @stat_offset: ptr to stat_offset to store first read of specific register.
675  * @stat: ptr to VSI's stat to be updated.
676  **/
677 static void
678 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
679 			      int stat_idx, bool offset_loaded,
680 			      struct i40e_eth_stats *stat_offset,
681 			      struct i40e_eth_stats *stat)
682 {
683 	u64 rx_rdpc, rx_rxerr;
684 
685 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
686 			   &stat_offset->rx_discards, &rx_rdpc);
687 	i40e_stat_update64(hw,
688 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
689 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
690 			   offset_loaded, &stat_offset->rx_discards_other,
691 			   &rx_rxerr);
692 
693 	stat->rx_discards = rx_rdpc + rx_rxerr;
694 }
695 
696 /**
697  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
698  * @vsi: the VSI to be updated
699  **/
700 void i40e_update_eth_stats(struct i40e_vsi *vsi)
701 {
702 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
703 	struct i40e_pf *pf = vsi->back;
704 	struct i40e_hw *hw = &pf->hw;
705 	struct i40e_eth_stats *oes;
706 	struct i40e_eth_stats *es;     /* device's eth stats */
707 
708 	es = &vsi->eth_stats;
709 	oes = &vsi->eth_stats_offsets;
710 
711 	/* Gather up the stats that the hw collects */
712 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
713 			   vsi->stat_offsets_loaded,
714 			   &oes->tx_errors, &es->tx_errors);
715 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
716 			   vsi->stat_offsets_loaded,
717 			   &oes->rx_discards, &es->rx_discards);
718 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
719 			   vsi->stat_offsets_loaded,
720 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
721 
722 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
723 			   I40E_GLV_GORCL(stat_idx),
724 			   vsi->stat_offsets_loaded,
725 			   &oes->rx_bytes, &es->rx_bytes);
726 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
727 			   I40E_GLV_UPRCL(stat_idx),
728 			   vsi->stat_offsets_loaded,
729 			   &oes->rx_unicast, &es->rx_unicast);
730 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
731 			   I40E_GLV_MPRCL(stat_idx),
732 			   vsi->stat_offsets_loaded,
733 			   &oes->rx_multicast, &es->rx_multicast);
734 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
735 			   I40E_GLV_BPRCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_broadcast, &es->rx_broadcast);
738 
739 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
740 			   I40E_GLV_GOTCL(stat_idx),
741 			   vsi->stat_offsets_loaded,
742 			   &oes->tx_bytes, &es->tx_bytes);
743 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
744 			   I40E_GLV_UPTCL(stat_idx),
745 			   vsi->stat_offsets_loaded,
746 			   &oes->tx_unicast, &es->tx_unicast);
747 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
748 			   I40E_GLV_MPTCL(stat_idx),
749 			   vsi->stat_offsets_loaded,
750 			   &oes->tx_multicast, &es->tx_multicast);
751 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
752 			   I40E_GLV_BPTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_broadcast, &es->tx_broadcast);
755 
756 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
757 				      vsi->stat_offsets_loaded, oes, es);
758 
759 	vsi->stat_offsets_loaded = true;
760 }
761 
762 /**
763  * i40e_update_veb_stats - Update Switch component statistics
764  * @veb: the VEB being updated
765  **/
766 void i40e_update_veb_stats(struct i40e_veb *veb)
767 {
768 	struct i40e_pf *pf = veb->pf;
769 	struct i40e_hw *hw = &pf->hw;
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	struct i40e_veb_tc_stats *veb_oes;
773 	struct i40e_veb_tc_stats *veb_es;
774 	int i, idx = 0;
775 
776 	idx = veb->stats_idx;
777 	es = &veb->stats;
778 	oes = &veb->stats_offsets;
779 	veb_es = &veb->tc_stats;
780 	veb_oes = &veb->tc_stats_offsets;
781 
782 	/* Gather up the stats that the hw collects */
783 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
784 			   veb->stat_offsets_loaded,
785 			   &oes->tx_discards, &es->tx_discards);
786 	if (hw->revision_id > 0)
787 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
788 				   veb->stat_offsets_loaded,
789 				   &oes->rx_unknown_protocol,
790 				   &es->rx_unknown_protocol);
791 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->rx_bytes, &es->rx_bytes);
794 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->rx_unicast, &es->rx_unicast);
797 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_multicast, &es->rx_multicast);
800 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
801 			   veb->stat_offsets_loaded,
802 			   &oes->rx_broadcast, &es->rx_broadcast);
803 
804 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->tx_bytes, &es->tx_bytes);
807 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->tx_unicast, &es->tx_unicast);
810 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_multicast, &es->tx_multicast);
813 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->tx_broadcast, &es->tx_broadcast);
816 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
817 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
818 				   I40E_GLVEBTC_RPCL(i, idx),
819 				   veb->stat_offsets_loaded,
820 				   &veb_oes->tc_rx_packets[i],
821 				   &veb_es->tc_rx_packets[i]);
822 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
823 				   I40E_GLVEBTC_RBCL(i, idx),
824 				   veb->stat_offsets_loaded,
825 				   &veb_oes->tc_rx_bytes[i],
826 				   &veb_es->tc_rx_bytes[i]);
827 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
828 				   I40E_GLVEBTC_TPCL(i, idx),
829 				   veb->stat_offsets_loaded,
830 				   &veb_oes->tc_tx_packets[i],
831 				   &veb_es->tc_tx_packets[i]);
832 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
833 				   I40E_GLVEBTC_TBCL(i, idx),
834 				   veb->stat_offsets_loaded,
835 				   &veb_oes->tc_tx_bytes[i],
836 				   &veb_es->tc_tx_bytes[i]);
837 	}
838 	veb->stat_offsets_loaded = true;
839 }
840 
841 /**
842  * i40e_update_vsi_stats - Update the vsi statistics counters.
843  * @vsi: the VSI to be updated
844  *
845  * There are a few instances where we store the same stat in a
846  * couple of different structs.  This is partly because we have
847  * the netdev stats that need to be filled out, which is slightly
848  * different from the "eth_stats" defined by the chip and used in
849  * VF communications.  We sort it out here.
850  **/
851 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
852 {
853 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
854 	struct i40e_pf *pf = vsi->back;
855 	struct rtnl_link_stats64 *ons;
856 	struct rtnl_link_stats64 *ns;   /* netdev stats */
857 	struct i40e_eth_stats *oes;
858 	struct i40e_eth_stats *es;     /* device's eth stats */
859 	u64 tx_restart, tx_busy;
860 	struct i40e_ring *p;
861 	u64 bytes, packets;
862 	unsigned int start;
863 	u64 tx_linearize;
864 	u64 tx_force_wb;
865 	u64 tx_stopped;
866 	u64 rx_p, rx_b;
867 	u64 tx_p, tx_b;
868 	u16 q;
869 
870 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
871 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
872 		return;
873 
874 	ns = i40e_get_vsi_stats_struct(vsi);
875 	ons = &vsi->net_stats_offsets;
876 	es = &vsi->eth_stats;
877 	oes = &vsi->eth_stats_offsets;
878 
879 	/* Gather up the netdev and vsi stats that the driver collects
880 	 * on the fly during packet processing
881 	 */
882 	rx_b = rx_p = 0;
883 	tx_b = tx_p = 0;
884 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
885 	tx_stopped = 0;
886 	rx_page = 0;
887 	rx_buf = 0;
888 	rx_reuse = 0;
889 	rx_alloc = 0;
890 	rx_waive = 0;
891 	rx_busy = 0;
892 	rcu_read_lock();
893 	for (q = 0; q < vsi->num_queue_pairs; q++) {
894 		/* locate Tx ring */
895 		p = READ_ONCE(vsi->tx_rings[q]);
896 		if (!p)
897 			continue;
898 
899 		do {
900 			start = u64_stats_fetch_begin_irq(&p->syncp);
901 			packets = p->stats.packets;
902 			bytes = p->stats.bytes;
903 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
904 		tx_b += bytes;
905 		tx_p += packets;
906 		tx_restart += p->tx_stats.restart_queue;
907 		tx_busy += p->tx_stats.tx_busy;
908 		tx_linearize += p->tx_stats.tx_linearize;
909 		tx_force_wb += p->tx_stats.tx_force_wb;
910 		tx_stopped += p->tx_stats.tx_stopped;
911 
912 		/* locate Rx ring */
913 		p = READ_ONCE(vsi->rx_rings[q]);
914 		if (!p)
915 			continue;
916 
917 		do {
918 			start = u64_stats_fetch_begin_irq(&p->syncp);
919 			packets = p->stats.packets;
920 			bytes = p->stats.bytes;
921 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
922 		rx_b += bytes;
923 		rx_p += packets;
924 		rx_buf += p->rx_stats.alloc_buff_failed;
925 		rx_page += p->rx_stats.alloc_page_failed;
926 		rx_reuse += p->rx_stats.page_reuse_count;
927 		rx_alloc += p->rx_stats.page_alloc_count;
928 		rx_waive += p->rx_stats.page_waive_count;
929 		rx_busy += p->rx_stats.page_busy_count;
930 
931 		if (i40e_enabled_xdp_vsi(vsi)) {
932 			/* locate XDP ring */
933 			p = READ_ONCE(vsi->xdp_rings[q]);
934 			if (!p)
935 				continue;
936 
937 			do {
938 				start = u64_stats_fetch_begin_irq(&p->syncp);
939 				packets = p->stats.packets;
940 				bytes = p->stats.bytes;
941 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
942 			tx_b += bytes;
943 			tx_p += packets;
944 			tx_restart += p->tx_stats.restart_queue;
945 			tx_busy += p->tx_stats.tx_busy;
946 			tx_linearize += p->tx_stats.tx_linearize;
947 			tx_force_wb += p->tx_stats.tx_force_wb;
948 		}
949 	}
950 	rcu_read_unlock();
951 	vsi->tx_restart = tx_restart;
952 	vsi->tx_busy = tx_busy;
953 	vsi->tx_linearize = tx_linearize;
954 	vsi->tx_force_wb = tx_force_wb;
955 	vsi->tx_stopped = tx_stopped;
956 	vsi->rx_page_failed = rx_page;
957 	vsi->rx_buf_failed = rx_buf;
958 	vsi->rx_page_reuse = rx_reuse;
959 	vsi->rx_page_alloc = rx_alloc;
960 	vsi->rx_page_waive = rx_waive;
961 	vsi->rx_page_busy = rx_busy;
962 
963 	ns->rx_packets = rx_p;
964 	ns->rx_bytes = rx_b;
965 	ns->tx_packets = tx_p;
966 	ns->tx_bytes = tx_b;
967 
968 	/* update netdev stats from eth stats */
969 	i40e_update_eth_stats(vsi);
970 	ons->tx_errors = oes->tx_errors;
971 	ns->tx_errors = es->tx_errors;
972 	ons->multicast = oes->rx_multicast;
973 	ns->multicast = es->rx_multicast;
974 	ons->rx_dropped = oes->rx_discards;
975 	ns->rx_dropped = es->rx_discards;
976 	ons->tx_dropped = oes->tx_discards;
977 	ns->tx_dropped = es->tx_discards;
978 
979 	/* pull in a couple PF stats if this is the main vsi */
980 	if (vsi == pf->vsi[pf->lan_vsi]) {
981 		ns->rx_crc_errors = pf->stats.crc_errors;
982 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
983 		ns->rx_length_errors = pf->stats.rx_length_errors;
984 	}
985 }
986 
987 /**
988  * i40e_update_pf_stats - Update the PF statistics counters.
989  * @pf: the PF to be updated
990  **/
991 static void i40e_update_pf_stats(struct i40e_pf *pf)
992 {
993 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
994 	struct i40e_hw_port_stats *nsd = &pf->stats;
995 	struct i40e_hw *hw = &pf->hw;
996 	u32 val;
997 	int i;
998 
999 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1000 			   I40E_GLPRT_GORCL(hw->port),
1001 			   pf->stat_offsets_loaded,
1002 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1003 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1004 			   I40E_GLPRT_GOTCL(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1007 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1008 			   pf->stat_offsets_loaded,
1009 			   &osd->eth.rx_discards,
1010 			   &nsd->eth.rx_discards);
1011 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1012 			   I40E_GLPRT_UPRCL(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->eth.rx_unicast,
1015 			   &nsd->eth.rx_unicast);
1016 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1017 			   I40E_GLPRT_MPRCL(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->eth.rx_multicast,
1020 			   &nsd->eth.rx_multicast);
1021 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1022 			   I40E_GLPRT_BPRCL(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->eth.rx_broadcast,
1025 			   &nsd->eth.rx_broadcast);
1026 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1027 			   I40E_GLPRT_UPTCL(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.tx_unicast,
1030 			   &nsd->eth.tx_unicast);
1031 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1032 			   I40E_GLPRT_MPTCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.tx_multicast,
1035 			   &nsd->eth.tx_multicast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1037 			   I40E_GLPRT_BPTCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.tx_broadcast,
1040 			   &nsd->eth.tx_broadcast);
1041 
1042 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->tx_dropped_link_down,
1045 			   &nsd->tx_dropped_link_down);
1046 
1047 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->crc_errors, &nsd->crc_errors);
1050 
1051 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1054 
1055 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->mac_local_faults,
1058 			   &nsd->mac_local_faults);
1059 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->mac_remote_faults,
1062 			   &nsd->mac_remote_faults);
1063 
1064 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->rx_length_errors,
1067 			   &nsd->rx_length_errors);
1068 
1069 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1072 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1078 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1081 
1082 	for (i = 0; i < 8; i++) {
1083 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1084 				   pf->stat_offsets_loaded,
1085 				   &osd->priority_xoff_rx[i],
1086 				   &nsd->priority_xoff_rx[i]);
1087 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1088 				   pf->stat_offsets_loaded,
1089 				   &osd->priority_xon_rx[i],
1090 				   &nsd->priority_xon_rx[i]);
1091 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1092 				   pf->stat_offsets_loaded,
1093 				   &osd->priority_xon_tx[i],
1094 				   &nsd->priority_xon_tx[i]);
1095 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1096 				   pf->stat_offsets_loaded,
1097 				   &osd->priority_xoff_tx[i],
1098 				   &nsd->priority_xoff_tx[i]);
1099 		i40e_stat_update32(hw,
1100 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1101 				   pf->stat_offsets_loaded,
1102 				   &osd->priority_xon_2_xoff[i],
1103 				   &nsd->priority_xon_2_xoff[i]);
1104 	}
1105 
1106 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1107 			   I40E_GLPRT_PRC64L(hw->port),
1108 			   pf->stat_offsets_loaded,
1109 			   &osd->rx_size_64, &nsd->rx_size_64);
1110 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1111 			   I40E_GLPRT_PRC127L(hw->port),
1112 			   pf->stat_offsets_loaded,
1113 			   &osd->rx_size_127, &nsd->rx_size_127);
1114 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1115 			   I40E_GLPRT_PRC255L(hw->port),
1116 			   pf->stat_offsets_loaded,
1117 			   &osd->rx_size_255, &nsd->rx_size_255);
1118 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1119 			   I40E_GLPRT_PRC511L(hw->port),
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->rx_size_511, &nsd->rx_size_511);
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1123 			   I40E_GLPRT_PRC1023L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1127 			   I40E_GLPRT_PRC1522L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1131 			   I40E_GLPRT_PRC9522L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_big, &nsd->rx_size_big);
1134 
1135 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1136 			   I40E_GLPRT_PTC64L(hw->port),
1137 			   pf->stat_offsets_loaded,
1138 			   &osd->tx_size_64, &nsd->tx_size_64);
1139 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1140 			   I40E_GLPRT_PTC127L(hw->port),
1141 			   pf->stat_offsets_loaded,
1142 			   &osd->tx_size_127, &nsd->tx_size_127);
1143 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1144 			   I40E_GLPRT_PTC255L(hw->port),
1145 			   pf->stat_offsets_loaded,
1146 			   &osd->tx_size_255, &nsd->tx_size_255);
1147 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1148 			   I40E_GLPRT_PTC511L(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->tx_size_511, &nsd->tx_size_511);
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1152 			   I40E_GLPRT_PTC1023L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1156 			   I40E_GLPRT_PTC1522L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1160 			   I40E_GLPRT_PTC9522L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_big, &nsd->tx_size_big);
1163 
1164 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->rx_undersize, &nsd->rx_undersize);
1167 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1168 			   pf->stat_offsets_loaded,
1169 			   &osd->rx_fragments, &nsd->rx_fragments);
1170 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_oversize, &nsd->rx_oversize);
1173 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->rx_jabber, &nsd->rx_jabber);
1176 
1177 	/* FDIR stats */
1178 	i40e_stat_update_and_clear32(hw,
1179 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1180 			&nsd->fd_atr_match);
1181 	i40e_stat_update_and_clear32(hw,
1182 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1183 			&nsd->fd_sb_match);
1184 	i40e_stat_update_and_clear32(hw,
1185 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1186 			&nsd->fd_atr_tunnel_match);
1187 
1188 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1189 	nsd->tx_lpi_status =
1190 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1191 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1192 	nsd->rx_lpi_status =
1193 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1194 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1195 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1196 			   pf->stat_offsets_loaded,
1197 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1198 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1199 			   pf->stat_offsets_loaded,
1200 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1201 
1202 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1203 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1204 		nsd->fd_sb_status = true;
1205 	else
1206 		nsd->fd_sb_status = false;
1207 
1208 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1209 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1210 		nsd->fd_atr_status = true;
1211 	else
1212 		nsd->fd_atr_status = false;
1213 
1214 	pf->stat_offsets_loaded = true;
1215 }
1216 
1217 /**
1218  * i40e_update_stats - Update the various statistics counters.
1219  * @vsi: the VSI to be updated
1220  *
1221  * Update the various stats for this VSI and its related entities.
1222  **/
1223 void i40e_update_stats(struct i40e_vsi *vsi)
1224 {
1225 	struct i40e_pf *pf = vsi->back;
1226 
1227 	if (vsi == pf->vsi[pf->lan_vsi])
1228 		i40e_update_pf_stats(pf);
1229 
1230 	i40e_update_vsi_stats(vsi);
1231 }
1232 
1233 /**
1234  * i40e_count_filters - counts VSI mac filters
1235  * @vsi: the VSI to be searched
1236  *
1237  * Returns count of mac filters
1238  **/
1239 int i40e_count_filters(struct i40e_vsi *vsi)
1240 {
1241 	struct i40e_mac_filter *f;
1242 	struct hlist_node *h;
1243 	int bkt;
1244 	int cnt = 0;
1245 
1246 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1247 		++cnt;
1248 
1249 	return cnt;
1250 }
1251 
1252 /**
1253  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1254  * @vsi: the VSI to be searched
1255  * @macaddr: the MAC address
1256  * @vlan: the vlan
1257  *
1258  * Returns ptr to the filter object or NULL
1259  **/
1260 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1261 						const u8 *macaddr, s16 vlan)
1262 {
1263 	struct i40e_mac_filter *f;
1264 	u64 key;
1265 
1266 	if (!vsi || !macaddr)
1267 		return NULL;
1268 
1269 	key = i40e_addr_to_hkey(macaddr);
1270 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1271 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1272 		    (vlan == f->vlan))
1273 			return f;
1274 	}
1275 	return NULL;
1276 }
1277 
1278 /**
1279  * i40e_find_mac - Find a mac addr in the macvlan filters list
1280  * @vsi: the VSI to be searched
1281  * @macaddr: the MAC address we are searching for
1282  *
1283  * Returns the first filter with the provided MAC address or NULL if
1284  * MAC address was not found
1285  **/
1286 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1287 {
1288 	struct i40e_mac_filter *f;
1289 	u64 key;
1290 
1291 	if (!vsi || !macaddr)
1292 		return NULL;
1293 
1294 	key = i40e_addr_to_hkey(macaddr);
1295 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1296 		if ((ether_addr_equal(macaddr, f->macaddr)))
1297 			return f;
1298 	}
1299 	return NULL;
1300 }
1301 
1302 /**
1303  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1304  * @vsi: the VSI to be searched
1305  *
1306  * Returns true if VSI is in vlan mode or false otherwise
1307  **/
1308 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1309 {
1310 	/* If we have a PVID, always operate in VLAN mode */
1311 	if (vsi->info.pvid)
1312 		return true;
1313 
1314 	/* We need to operate in VLAN mode whenever we have any filters with
1315 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1316 	 * time, incurring search cost repeatedly. However, we can notice two
1317 	 * things:
1318 	 *
1319 	 * 1) the only place where we can gain a VLAN filter is in
1320 	 *    i40e_add_filter.
1321 	 *
1322 	 * 2) the only place where filters are actually removed is in
1323 	 *    i40e_sync_filters_subtask.
1324 	 *
1325 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1326 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1327 	 * we have to perform the full search after deleting filters in
1328 	 * i40e_sync_filters_subtask, but we already have to search
1329 	 * filters here and can perform the check at the same time. This
1330 	 * results in avoiding embedding a loop for VLAN mode inside another
1331 	 * loop over all the filters, and should maintain correctness as noted
1332 	 * above.
1333 	 */
1334 	return vsi->has_vlan_filter;
1335 }
1336 
1337 /**
1338  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1339  * @vsi: the VSI to configure
1340  * @tmp_add_list: list of filters ready to be added
1341  * @tmp_del_list: list of filters ready to be deleted
1342  * @vlan_filters: the number of active VLAN filters
1343  *
1344  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1345  * behave as expected. If we have any active VLAN filters remaining or about
1346  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1347  * so that they only match against untagged traffic. If we no longer have any
1348  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1349  * so that they match against both tagged and untagged traffic. In this way,
1350  * we ensure that we correctly receive the desired traffic. This ensures that
1351  * when we have an active VLAN we will receive only untagged traffic and
1352  * traffic matching active VLANs. If we have no active VLANs then we will
1353  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1354  *
1355  * Finally, in a similar fashion, this function also corrects filters when
1356  * there is an active PVID assigned to this VSI.
1357  *
1358  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1359  *
1360  * This function is only expected to be called from within
1361  * i40e_sync_vsi_filters.
1362  *
1363  * NOTE: This function expects to be called while under the
1364  * mac_filter_hash_lock
1365  */
1366 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1367 					 struct hlist_head *tmp_add_list,
1368 					 struct hlist_head *tmp_del_list,
1369 					 int vlan_filters)
1370 {
1371 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1372 	struct i40e_mac_filter *f, *add_head;
1373 	struct i40e_new_mac_filter *new;
1374 	struct hlist_node *h;
1375 	int bkt, new_vlan;
1376 
1377 	/* To determine if a particular filter needs to be replaced we
1378 	 * have the three following conditions:
1379 	 *
1380 	 * a) if we have a PVID assigned, then all filters which are
1381 	 *    not marked as VLAN=PVID must be replaced with filters that
1382 	 *    are.
1383 	 * b) otherwise, if we have any active VLANS, all filters
1384 	 *    which are marked as VLAN=-1 must be replaced with
1385 	 *    filters marked as VLAN=0
1386 	 * c) finally, if we do not have any active VLANS, all filters
1387 	 *    which are marked as VLAN=0 must be replaced with filters
1388 	 *    marked as VLAN=-1
1389 	 */
1390 
1391 	/* Update the filters about to be added in place */
1392 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1393 		if (pvid && new->f->vlan != pvid)
1394 			new->f->vlan = pvid;
1395 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1396 			new->f->vlan = 0;
1397 		else if (!vlan_filters && new->f->vlan == 0)
1398 			new->f->vlan = I40E_VLAN_ANY;
1399 	}
1400 
1401 	/* Update the remaining active filters */
1402 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1403 		/* Combine the checks for whether a filter needs to be changed
1404 		 * and then determine the new VLAN inside the if block, in
1405 		 * order to avoid duplicating code for adding the new filter
1406 		 * then deleting the old filter.
1407 		 */
1408 		if ((pvid && f->vlan != pvid) ||
1409 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1410 		    (!vlan_filters && f->vlan == 0)) {
1411 			/* Determine the new vlan we will be adding */
1412 			if (pvid)
1413 				new_vlan = pvid;
1414 			else if (vlan_filters)
1415 				new_vlan = 0;
1416 			else
1417 				new_vlan = I40E_VLAN_ANY;
1418 
1419 			/* Create the new filter */
1420 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1421 			if (!add_head)
1422 				return -ENOMEM;
1423 
1424 			/* Create a temporary i40e_new_mac_filter */
1425 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1426 			if (!new)
1427 				return -ENOMEM;
1428 
1429 			new->f = add_head;
1430 			new->state = add_head->state;
1431 
1432 			/* Add the new filter to the tmp list */
1433 			hlist_add_head(&new->hlist, tmp_add_list);
1434 
1435 			/* Put the original filter into the delete list */
1436 			f->state = I40E_FILTER_REMOVE;
1437 			hash_del(&f->hlist);
1438 			hlist_add_head(&f->hlist, tmp_del_list);
1439 		}
1440 	}
1441 
1442 	vsi->has_vlan_filter = !!vlan_filters;
1443 
1444 	return 0;
1445 }
1446 
1447 /**
1448  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1449  * @vsi: the vsi to configure
1450  * @new_mac: new mac filter to be added
1451  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1452  * @vlan_filters: the number of active VLAN filters
1453  * @trusted: flag if the VF is trusted
1454  *
1455  * Get new VLAN id based on current VLAN filters, trust, PVID
1456  * and vf-vlan-prune-disable flag.
1457  *
1458  * Returns the value of the new vlan filter or
1459  * the old value if no new filter is needed.
1460  */
1461 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1462 				struct i40e_new_mac_filter *new_mac,
1463 				struct i40e_mac_filter *f,
1464 				int vlan_filters,
1465 				bool trusted)
1466 {
1467 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1468 	struct i40e_pf *pf = vsi->back;
1469 	bool is_any;
1470 
1471 	if (new_mac)
1472 		f = new_mac->f;
1473 
1474 	if (pvid && f->vlan != pvid)
1475 		return pvid;
1476 
1477 	is_any = (trusted ||
1478 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1479 
1480 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1481 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1482 	    (is_any && !vlan_filters && f->vlan == 0)) {
1483 		if (is_any)
1484 			return I40E_VLAN_ANY;
1485 		else
1486 			return 0;
1487 	}
1488 
1489 	return f->vlan;
1490 }
1491 
1492 /**
1493  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1494  * @vsi: the vsi to configure
1495  * @tmp_add_list: list of filters ready to be added
1496  * @tmp_del_list: list of filters ready to be deleted
1497  * @vlan_filters: the number of active VLAN filters
1498  * @trusted: flag if the VF is trusted
1499  *
1500  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1501  * and vf-vlan-prune-disable flag.
1502  *
1503  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1504  *
1505  * This function is only expected to be called from within
1506  * i40e_sync_vsi_filters.
1507  *
1508  * NOTE: This function expects to be called while under the
1509  * mac_filter_hash_lock
1510  */
1511 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1512 					    struct hlist_head *tmp_add_list,
1513 					    struct hlist_head *tmp_del_list,
1514 					    int vlan_filters,
1515 					    bool trusted)
1516 {
1517 	struct i40e_mac_filter *f, *add_head;
1518 	struct i40e_new_mac_filter *new_mac;
1519 	struct hlist_node *h;
1520 	int bkt, new_vlan;
1521 
1522 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1523 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1524 							vlan_filters, trusted);
1525 	}
1526 
1527 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1528 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1529 						trusted);
1530 		if (new_vlan != f->vlan) {
1531 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1532 			if (!add_head)
1533 				return -ENOMEM;
1534 			/* Create a temporary i40e_new_mac_filter */
1535 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1536 			if (!new_mac)
1537 				return -ENOMEM;
1538 			new_mac->f = add_head;
1539 			new_mac->state = add_head->state;
1540 
1541 			/* Add the new filter to the tmp list */
1542 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1543 
1544 			/* Put the original filter into the delete list */
1545 			f->state = I40E_FILTER_REMOVE;
1546 			hash_del(&f->hlist);
1547 			hlist_add_head(&f->hlist, tmp_del_list);
1548 		}
1549 	}
1550 
1551 	vsi->has_vlan_filter = !!vlan_filters;
1552 	return 0;
1553 }
1554 
1555 /**
1556  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1557  * @vsi: the PF Main VSI - inappropriate for any other VSI
1558  * @macaddr: the MAC address
1559  *
1560  * Remove whatever filter the firmware set up so the driver can manage
1561  * its own filtering intelligently.
1562  **/
1563 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1564 {
1565 	struct i40e_aqc_remove_macvlan_element_data element;
1566 	struct i40e_pf *pf = vsi->back;
1567 
1568 	/* Only appropriate for the PF main VSI */
1569 	if (vsi->type != I40E_VSI_MAIN)
1570 		return;
1571 
1572 	memset(&element, 0, sizeof(element));
1573 	ether_addr_copy(element.mac_addr, macaddr);
1574 	element.vlan_tag = 0;
1575 	/* Ignore error returns, some firmware does it this way... */
1576 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1577 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1578 
1579 	memset(&element, 0, sizeof(element));
1580 	ether_addr_copy(element.mac_addr, macaddr);
1581 	element.vlan_tag = 0;
1582 	/* ...and some firmware does it this way. */
1583 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1584 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1585 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1586 }
1587 
1588 /**
1589  * i40e_add_filter - Add a mac/vlan filter to the VSI
1590  * @vsi: the VSI to be searched
1591  * @macaddr: the MAC address
1592  * @vlan: the vlan
1593  *
1594  * Returns ptr to the filter object or NULL when no memory available.
1595  *
1596  * NOTE: This function is expected to be called with mac_filter_hash_lock
1597  * being held.
1598  **/
1599 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1600 					const u8 *macaddr, s16 vlan)
1601 {
1602 	struct i40e_mac_filter *f;
1603 	u64 key;
1604 
1605 	if (!vsi || !macaddr)
1606 		return NULL;
1607 
1608 	f = i40e_find_filter(vsi, macaddr, vlan);
1609 	if (!f) {
1610 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1611 		if (!f)
1612 			return NULL;
1613 
1614 		/* Update the boolean indicating if we need to function in
1615 		 * VLAN mode.
1616 		 */
1617 		if (vlan >= 0)
1618 			vsi->has_vlan_filter = true;
1619 
1620 		ether_addr_copy(f->macaddr, macaddr);
1621 		f->vlan = vlan;
1622 		f->state = I40E_FILTER_NEW;
1623 		INIT_HLIST_NODE(&f->hlist);
1624 
1625 		key = i40e_addr_to_hkey(macaddr);
1626 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1627 
1628 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1629 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1630 	}
1631 
1632 	/* If we're asked to add a filter that has been marked for removal, it
1633 	 * is safe to simply restore it to active state. __i40e_del_filter
1634 	 * will have simply deleted any filters which were previously marked
1635 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1636 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1637 	 * task, just restore this filter to the ACTIVE state so that the
1638 	 * sync task leaves it in place
1639 	 */
1640 	if (f->state == I40E_FILTER_REMOVE)
1641 		f->state = I40E_FILTER_ACTIVE;
1642 
1643 	return f;
1644 }
1645 
1646 /**
1647  * __i40e_del_filter - Remove a specific filter from the VSI
1648  * @vsi: VSI to remove from
1649  * @f: the filter to remove from the list
1650  *
1651  * This function should be called instead of i40e_del_filter only if you know
1652  * the exact filter you will remove already, such as via i40e_find_filter or
1653  * i40e_find_mac.
1654  *
1655  * NOTE: This function is expected to be called with mac_filter_hash_lock
1656  * being held.
1657  * ANOTHER NOTE: This function MUST be called from within the context of
1658  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1659  * instead of list_for_each_entry().
1660  **/
1661 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1662 {
1663 	if (!f)
1664 		return;
1665 
1666 	/* If the filter was never added to firmware then we can just delete it
1667 	 * directly and we don't want to set the status to remove or else an
1668 	 * admin queue command will unnecessarily fire.
1669 	 */
1670 	if ((f->state == I40E_FILTER_FAILED) ||
1671 	    (f->state == I40E_FILTER_NEW)) {
1672 		hash_del(&f->hlist);
1673 		kfree(f);
1674 	} else {
1675 		f->state = I40E_FILTER_REMOVE;
1676 	}
1677 
1678 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1679 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1680 }
1681 
1682 /**
1683  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1684  * @vsi: the VSI to be searched
1685  * @macaddr: the MAC address
1686  * @vlan: the VLAN
1687  *
1688  * NOTE: This function is expected to be called with mac_filter_hash_lock
1689  * being held.
1690  * ANOTHER NOTE: This function MUST be called from within the context of
1691  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1692  * instead of list_for_each_entry().
1693  **/
1694 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1695 {
1696 	struct i40e_mac_filter *f;
1697 
1698 	if (!vsi || !macaddr)
1699 		return;
1700 
1701 	f = i40e_find_filter(vsi, macaddr, vlan);
1702 	__i40e_del_filter(vsi, f);
1703 }
1704 
1705 /**
1706  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1707  * @vsi: the VSI to be searched
1708  * @macaddr: the mac address to be filtered
1709  *
1710  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1711  * go through all the macvlan filters and add a macvlan filter for each
1712  * unique vlan that already exists. If a PVID has been assigned, instead only
1713  * add the macaddr to that VLAN.
1714  *
1715  * Returns last filter added on success, else NULL
1716  **/
1717 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1718 					    const u8 *macaddr)
1719 {
1720 	struct i40e_mac_filter *f, *add = NULL;
1721 	struct hlist_node *h;
1722 	int bkt;
1723 
1724 	if (vsi->info.pvid)
1725 		return i40e_add_filter(vsi, macaddr,
1726 				       le16_to_cpu(vsi->info.pvid));
1727 
1728 	if (!i40e_is_vsi_in_vlan(vsi))
1729 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1730 
1731 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1732 		if (f->state == I40E_FILTER_REMOVE)
1733 			continue;
1734 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1735 		if (!add)
1736 			return NULL;
1737 	}
1738 
1739 	return add;
1740 }
1741 
1742 /**
1743  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1744  * @vsi: the VSI to be searched
1745  * @macaddr: the mac address to be removed
1746  *
1747  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1748  * associated with.
1749  *
1750  * Returns 0 for success, or error
1751  **/
1752 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1753 {
1754 	struct i40e_mac_filter *f;
1755 	struct hlist_node *h;
1756 	bool found = false;
1757 	int bkt;
1758 
1759 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1760 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1761 		if (ether_addr_equal(macaddr, f->macaddr)) {
1762 			__i40e_del_filter(vsi, f);
1763 			found = true;
1764 		}
1765 	}
1766 
1767 	if (found)
1768 		return 0;
1769 	else
1770 		return -ENOENT;
1771 }
1772 
1773 /**
1774  * i40e_set_mac - NDO callback to set mac address
1775  * @netdev: network interface device structure
1776  * @p: pointer to an address structure
1777  *
1778  * Returns 0 on success, negative on failure
1779  **/
1780 static int i40e_set_mac(struct net_device *netdev, void *p)
1781 {
1782 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1783 	struct i40e_vsi *vsi = np->vsi;
1784 	struct i40e_pf *pf = vsi->back;
1785 	struct i40e_hw *hw = &pf->hw;
1786 	struct sockaddr *addr = p;
1787 
1788 	if (!is_valid_ether_addr(addr->sa_data))
1789 		return -EADDRNOTAVAIL;
1790 
1791 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1792 		netdev_info(netdev, "already using mac address %pM\n",
1793 			    addr->sa_data);
1794 		return 0;
1795 	}
1796 
1797 	if (test_bit(__I40E_DOWN, pf->state) ||
1798 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1799 		return -EADDRNOTAVAIL;
1800 
1801 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1802 		netdev_info(netdev, "returning to hw mac address %pM\n",
1803 			    hw->mac.addr);
1804 	else
1805 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1806 
1807 	/* Copy the address first, so that we avoid a possible race with
1808 	 * .set_rx_mode().
1809 	 * - Remove old address from MAC filter
1810 	 * - Copy new address
1811 	 * - Add new address to MAC filter
1812 	 */
1813 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1814 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1815 	eth_hw_addr_set(netdev, addr->sa_data);
1816 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1817 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1818 
1819 	if (vsi->type == I40E_VSI_MAIN) {
1820 		i40e_status ret;
1821 
1822 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1823 						addr->sa_data, NULL);
1824 		if (ret)
1825 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1826 				    i40e_stat_str(hw, ret),
1827 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1828 	}
1829 
1830 	/* schedule our worker thread which will take care of
1831 	 * applying the new filter changes
1832 	 */
1833 	i40e_service_event_schedule(pf);
1834 	return 0;
1835 }
1836 
1837 /**
1838  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1839  * @vsi: vsi structure
1840  * @seed: RSS hash seed
1841  * @lut: pointer to lookup table of lut_size
1842  * @lut_size: size of the lookup table
1843  **/
1844 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1845 			      u8 *lut, u16 lut_size)
1846 {
1847 	struct i40e_pf *pf = vsi->back;
1848 	struct i40e_hw *hw = &pf->hw;
1849 	int ret = 0;
1850 
1851 	if (seed) {
1852 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1853 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1854 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1855 		if (ret) {
1856 			dev_info(&pf->pdev->dev,
1857 				 "Cannot set RSS key, err %s aq_err %s\n",
1858 				 i40e_stat_str(hw, ret),
1859 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1860 			return ret;
1861 		}
1862 	}
1863 	if (lut) {
1864 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1865 
1866 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1867 		if (ret) {
1868 			dev_info(&pf->pdev->dev,
1869 				 "Cannot set RSS lut, err %s aq_err %s\n",
1870 				 i40e_stat_str(hw, ret),
1871 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1872 			return ret;
1873 		}
1874 	}
1875 	return ret;
1876 }
1877 
1878 /**
1879  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1880  * @vsi: VSI structure
1881  **/
1882 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1883 {
1884 	struct i40e_pf *pf = vsi->back;
1885 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1886 	u8 *lut;
1887 	int ret;
1888 
1889 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1890 		return 0;
1891 	if (!vsi->rss_size)
1892 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1893 				      vsi->num_queue_pairs);
1894 	if (!vsi->rss_size)
1895 		return -EINVAL;
1896 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1897 	if (!lut)
1898 		return -ENOMEM;
1899 
1900 	/* Use the user configured hash keys and lookup table if there is one,
1901 	 * otherwise use default
1902 	 */
1903 	if (vsi->rss_lut_user)
1904 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1905 	else
1906 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1907 	if (vsi->rss_hkey_user)
1908 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1909 	else
1910 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1911 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1912 	kfree(lut);
1913 	return ret;
1914 }
1915 
1916 /**
1917  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1918  * @vsi: the VSI being configured,
1919  * @ctxt: VSI context structure
1920  * @enabled_tc: number of traffic classes to enable
1921  *
1922  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1923  **/
1924 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1925 					   struct i40e_vsi_context *ctxt,
1926 					   u8 enabled_tc)
1927 {
1928 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1929 	int i, override_q, pow, num_qps, ret;
1930 	u8 netdev_tc = 0, offset = 0;
1931 
1932 	if (vsi->type != I40E_VSI_MAIN)
1933 		return -EINVAL;
1934 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1935 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1936 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1937 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1938 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1939 
1940 	/* find the next higher power-of-2 of num queue pairs */
1941 	pow = ilog2(num_qps);
1942 	if (!is_power_of_2(num_qps))
1943 		pow++;
1944 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1945 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1946 
1947 	/* Setup queue offset/count for all TCs for given VSI */
1948 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1949 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1950 		/* See if the given TC is enabled for the given VSI */
1951 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1952 			offset = vsi->mqprio_qopt.qopt.offset[i];
1953 			qcount = vsi->mqprio_qopt.qopt.count[i];
1954 			if (qcount > max_qcount)
1955 				max_qcount = qcount;
1956 			vsi->tc_config.tc_info[i].qoffset = offset;
1957 			vsi->tc_config.tc_info[i].qcount = qcount;
1958 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1959 		} else {
1960 			/* TC is not enabled so set the offset to
1961 			 * default queue and allocate one queue
1962 			 * for the given TC.
1963 			 */
1964 			vsi->tc_config.tc_info[i].qoffset = 0;
1965 			vsi->tc_config.tc_info[i].qcount = 1;
1966 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1967 		}
1968 	}
1969 
1970 	/* Set actual Tx/Rx queue pairs */
1971 	vsi->num_queue_pairs = offset + qcount;
1972 
1973 	/* Setup queue TC[0].qmap for given VSI context */
1974 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1975 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1976 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1977 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1978 
1979 	/* Reconfigure RSS for main VSI with max queue count */
1980 	vsi->rss_size = max_qcount;
1981 	ret = i40e_vsi_config_rss(vsi);
1982 	if (ret) {
1983 		dev_info(&vsi->back->pdev->dev,
1984 			 "Failed to reconfig rss for num_queues (%u)\n",
1985 			 max_qcount);
1986 		return ret;
1987 	}
1988 	vsi->reconfig_rss = true;
1989 	dev_dbg(&vsi->back->pdev->dev,
1990 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1991 
1992 	/* Find queue count available for channel VSIs and starting offset
1993 	 * for channel VSIs
1994 	 */
1995 	override_q = vsi->mqprio_qopt.qopt.count[0];
1996 	if (override_q && override_q < vsi->num_queue_pairs) {
1997 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1998 		vsi->next_base_queue = override_q;
1999 	}
2000 	return 0;
2001 }
2002 
2003 /**
2004  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2005  * @vsi: the VSI being setup
2006  * @ctxt: VSI context structure
2007  * @enabled_tc: Enabled TCs bitmap
2008  * @is_add: True if called before Add VSI
2009  *
2010  * Setup VSI queue mapping for enabled traffic classes.
2011  **/
2012 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2013 				     struct i40e_vsi_context *ctxt,
2014 				     u8 enabled_tc,
2015 				     bool is_add)
2016 {
2017 	struct i40e_pf *pf = vsi->back;
2018 	u16 num_tc_qps = 0;
2019 	u16 sections = 0;
2020 	u8 netdev_tc = 0;
2021 	u16 numtc = 1;
2022 	u16 qcount;
2023 	u8 offset;
2024 	u16 qmap;
2025 	int i;
2026 
2027 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2028 	offset = 0;
2029 	/* zero out queue mapping, it will get updated on the end of the function */
2030 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2031 
2032 	if (vsi->type == I40E_VSI_MAIN) {
2033 		/* This code helps add more queue to the VSI if we have
2034 		 * more cores than RSS can support, the higher cores will
2035 		 * be served by ATR or other filters. Furthermore, the
2036 		 * non-zero req_queue_pairs says that user requested a new
2037 		 * queue count via ethtool's set_channels, so use this
2038 		 * value for queues distribution across traffic classes
2039 		 * We need at least one queue pair for the interface
2040 		 * to be usable as we see in else statement.
2041 		 */
2042 		if (vsi->req_queue_pairs > 0)
2043 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2044 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2045 			vsi->num_queue_pairs = pf->num_lan_msix;
2046 		else
2047 			vsi->num_queue_pairs = 1;
2048 	}
2049 
2050 	/* Number of queues per enabled TC */
2051 	if (vsi->type == I40E_VSI_MAIN ||
2052 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2053 		num_tc_qps = vsi->num_queue_pairs;
2054 	else
2055 		num_tc_qps = vsi->alloc_queue_pairs;
2056 
2057 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2058 		/* Find numtc from enabled TC bitmap */
2059 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2060 			if (enabled_tc & BIT(i)) /* TC is enabled */
2061 				numtc++;
2062 		}
2063 		if (!numtc) {
2064 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2065 			numtc = 1;
2066 		}
2067 		num_tc_qps = num_tc_qps / numtc;
2068 		num_tc_qps = min_t(int, num_tc_qps,
2069 				   i40e_pf_get_max_q_per_tc(pf));
2070 	}
2071 
2072 	vsi->tc_config.numtc = numtc;
2073 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2074 
2075 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2076 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2077 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2078 
2079 	/* Setup queue offset/count for all TCs for given VSI */
2080 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2081 		/* See if the given TC is enabled for the given VSI */
2082 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2083 			/* TC is enabled */
2084 			int pow, num_qps;
2085 
2086 			switch (vsi->type) {
2087 			case I40E_VSI_MAIN:
2088 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2089 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2090 				    vsi->tc_config.enabled_tc != 1) {
2091 					qcount = min_t(int, pf->alloc_rss_size,
2092 						       num_tc_qps);
2093 					break;
2094 				}
2095 				fallthrough;
2096 			case I40E_VSI_FDIR:
2097 			case I40E_VSI_SRIOV:
2098 			case I40E_VSI_VMDQ2:
2099 			default:
2100 				qcount = num_tc_qps;
2101 				WARN_ON(i != 0);
2102 				break;
2103 			}
2104 			vsi->tc_config.tc_info[i].qoffset = offset;
2105 			vsi->tc_config.tc_info[i].qcount = qcount;
2106 
2107 			/* find the next higher power-of-2 of num queue pairs */
2108 			num_qps = qcount;
2109 			pow = 0;
2110 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2111 				pow++;
2112 				num_qps >>= 1;
2113 			}
2114 
2115 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2116 			qmap =
2117 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2118 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2119 
2120 			offset += qcount;
2121 		} else {
2122 			/* TC is not enabled so set the offset to
2123 			 * default queue and allocate one queue
2124 			 * for the given TC.
2125 			 */
2126 			vsi->tc_config.tc_info[i].qoffset = 0;
2127 			vsi->tc_config.tc_info[i].qcount = 1;
2128 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2129 
2130 			qmap = 0;
2131 		}
2132 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2133 	}
2134 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2135 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2136 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2137 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2138 		vsi->num_queue_pairs = offset;
2139 
2140 	/* Scheduler section valid can only be set for ADD VSI */
2141 	if (is_add) {
2142 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2143 
2144 		ctxt->info.up_enable_bits = enabled_tc;
2145 	}
2146 	if (vsi->type == I40E_VSI_SRIOV) {
2147 		ctxt->info.mapping_flags |=
2148 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2149 		for (i = 0; i < vsi->num_queue_pairs; i++)
2150 			ctxt->info.queue_mapping[i] =
2151 					       cpu_to_le16(vsi->base_queue + i);
2152 	} else {
2153 		ctxt->info.mapping_flags |=
2154 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2155 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2156 	}
2157 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2158 }
2159 
2160 /**
2161  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2162  * @netdev: the netdevice
2163  * @addr: address to add
2164  *
2165  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2166  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2167  */
2168 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2169 {
2170 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2171 	struct i40e_vsi *vsi = np->vsi;
2172 
2173 	if (i40e_add_mac_filter(vsi, addr))
2174 		return 0;
2175 	else
2176 		return -ENOMEM;
2177 }
2178 
2179 /**
2180  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2181  * @netdev: the netdevice
2182  * @addr: address to add
2183  *
2184  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2185  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2186  */
2187 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2188 {
2189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2190 	struct i40e_vsi *vsi = np->vsi;
2191 
2192 	/* Under some circumstances, we might receive a request to delete
2193 	 * our own device address from our uc list. Because we store the
2194 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2195 	 * such requests and not delete our device address from this list.
2196 	 */
2197 	if (ether_addr_equal(addr, netdev->dev_addr))
2198 		return 0;
2199 
2200 	i40e_del_mac_filter(vsi, addr);
2201 
2202 	return 0;
2203 }
2204 
2205 /**
2206  * i40e_set_rx_mode - NDO callback to set the netdev filters
2207  * @netdev: network interface device structure
2208  **/
2209 static void i40e_set_rx_mode(struct net_device *netdev)
2210 {
2211 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2212 	struct i40e_vsi *vsi = np->vsi;
2213 
2214 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2215 
2216 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2217 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2218 
2219 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2220 
2221 	/* check for other flag changes */
2222 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2223 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2224 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2225 	}
2226 }
2227 
2228 /**
2229  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2230  * @vsi: Pointer to VSI struct
2231  * @from: Pointer to list which contains MAC filter entries - changes to
2232  *        those entries needs to be undone.
2233  *
2234  * MAC filter entries from this list were slated for deletion.
2235  **/
2236 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2237 					 struct hlist_head *from)
2238 {
2239 	struct i40e_mac_filter *f;
2240 	struct hlist_node *h;
2241 
2242 	hlist_for_each_entry_safe(f, h, from, hlist) {
2243 		u64 key = i40e_addr_to_hkey(f->macaddr);
2244 
2245 		/* Move the element back into MAC filter list*/
2246 		hlist_del(&f->hlist);
2247 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2248 	}
2249 }
2250 
2251 /**
2252  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2253  * @vsi: Pointer to vsi struct
2254  * @from: Pointer to list which contains MAC filter entries - changes to
2255  *        those entries needs to be undone.
2256  *
2257  * MAC filter entries from this list were slated for addition.
2258  **/
2259 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2260 					 struct hlist_head *from)
2261 {
2262 	struct i40e_new_mac_filter *new;
2263 	struct hlist_node *h;
2264 
2265 	hlist_for_each_entry_safe(new, h, from, hlist) {
2266 		/* We can simply free the wrapper structure */
2267 		hlist_del(&new->hlist);
2268 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2269 		kfree(new);
2270 	}
2271 }
2272 
2273 /**
2274  * i40e_next_filter - Get the next non-broadcast filter from a list
2275  * @next: pointer to filter in list
2276  *
2277  * Returns the next non-broadcast filter in the list. Required so that we
2278  * ignore broadcast filters within the list, since these are not handled via
2279  * the normal firmware update path.
2280  */
2281 static
2282 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2283 {
2284 	hlist_for_each_entry_continue(next, hlist) {
2285 		if (!is_broadcast_ether_addr(next->f->macaddr))
2286 			return next;
2287 	}
2288 
2289 	return NULL;
2290 }
2291 
2292 /**
2293  * i40e_update_filter_state - Update filter state based on return data
2294  * from firmware
2295  * @count: Number of filters added
2296  * @add_list: return data from fw
2297  * @add_head: pointer to first filter in current batch
2298  *
2299  * MAC filter entries from list were slated to be added to device. Returns
2300  * number of successful filters. Note that 0 does NOT mean success!
2301  **/
2302 static int
2303 i40e_update_filter_state(int count,
2304 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2305 			 struct i40e_new_mac_filter *add_head)
2306 {
2307 	int retval = 0;
2308 	int i;
2309 
2310 	for (i = 0; i < count; i++) {
2311 		/* Always check status of each filter. We don't need to check
2312 		 * the firmware return status because we pre-set the filter
2313 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2314 		 * request to the adminq. Thus, if it no longer matches then
2315 		 * we know the filter is active.
2316 		 */
2317 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2318 			add_head->state = I40E_FILTER_FAILED;
2319 		} else {
2320 			add_head->state = I40E_FILTER_ACTIVE;
2321 			retval++;
2322 		}
2323 
2324 		add_head = i40e_next_filter(add_head);
2325 		if (!add_head)
2326 			break;
2327 	}
2328 
2329 	return retval;
2330 }
2331 
2332 /**
2333  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2334  * @vsi: ptr to the VSI
2335  * @vsi_name: name to display in messages
2336  * @list: the list of filters to send to firmware
2337  * @num_del: the number of filters to delete
2338  * @retval: Set to -EIO on failure to delete
2339  *
2340  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2341  * *retval instead of a return value so that success does not force ret_val to
2342  * be set to 0. This ensures that a sequence of calls to this function
2343  * preserve the previous value of *retval on successful delete.
2344  */
2345 static
2346 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2347 			  struct i40e_aqc_remove_macvlan_element_data *list,
2348 			  int num_del, int *retval)
2349 {
2350 	struct i40e_hw *hw = &vsi->back->hw;
2351 	enum i40e_admin_queue_err aq_status;
2352 	i40e_status aq_ret;
2353 
2354 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2355 					   &aq_status);
2356 
2357 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2358 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2359 		*retval = -EIO;
2360 		dev_info(&vsi->back->pdev->dev,
2361 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2362 			 vsi_name, i40e_stat_str(hw, aq_ret),
2363 			 i40e_aq_str(hw, aq_status));
2364 	}
2365 }
2366 
2367 /**
2368  * i40e_aqc_add_filters - Request firmware to add a set of filters
2369  * @vsi: ptr to the VSI
2370  * @vsi_name: name to display in messages
2371  * @list: the list of filters to send to firmware
2372  * @add_head: Position in the add hlist
2373  * @num_add: the number of filters to add
2374  *
2375  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2376  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2377  * space for more filters.
2378  */
2379 static
2380 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2381 			  struct i40e_aqc_add_macvlan_element_data *list,
2382 			  struct i40e_new_mac_filter *add_head,
2383 			  int num_add)
2384 {
2385 	struct i40e_hw *hw = &vsi->back->hw;
2386 	enum i40e_admin_queue_err aq_status;
2387 	int fcnt;
2388 
2389 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2390 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2391 
2392 	if (fcnt != num_add) {
2393 		if (vsi->type == I40E_VSI_MAIN) {
2394 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2395 			dev_warn(&vsi->back->pdev->dev,
2396 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2397 				 i40e_aq_str(hw, aq_status), vsi_name);
2398 		} else if (vsi->type == I40E_VSI_SRIOV ||
2399 			   vsi->type == I40E_VSI_VMDQ1 ||
2400 			   vsi->type == I40E_VSI_VMDQ2) {
2401 			dev_warn(&vsi->back->pdev->dev,
2402 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2403 				 i40e_aq_str(hw, aq_status), vsi_name,
2404 					     vsi_name);
2405 		} else {
2406 			dev_warn(&vsi->back->pdev->dev,
2407 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2408 				 i40e_aq_str(hw, aq_status), vsi_name,
2409 					     vsi->type);
2410 		}
2411 	}
2412 }
2413 
2414 /**
2415  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2416  * @vsi: pointer to the VSI
2417  * @vsi_name: the VSI name
2418  * @f: filter data
2419  *
2420  * This function sets or clears the promiscuous broadcast flags for VLAN
2421  * filters in order to properly receive broadcast frames. Assumes that only
2422  * broadcast filters are passed.
2423  *
2424  * Returns status indicating success or failure;
2425  **/
2426 static i40e_status
2427 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2428 			  struct i40e_mac_filter *f)
2429 {
2430 	bool enable = f->state == I40E_FILTER_NEW;
2431 	struct i40e_hw *hw = &vsi->back->hw;
2432 	i40e_status aq_ret;
2433 
2434 	if (f->vlan == I40E_VLAN_ANY) {
2435 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2436 						   vsi->seid,
2437 						   enable,
2438 						   NULL);
2439 	} else {
2440 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2441 							    vsi->seid,
2442 							    enable,
2443 							    f->vlan,
2444 							    NULL);
2445 	}
2446 
2447 	if (aq_ret) {
2448 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2449 		dev_warn(&vsi->back->pdev->dev,
2450 			 "Error %s, forcing overflow promiscuous on %s\n",
2451 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2452 			 vsi_name);
2453 	}
2454 
2455 	return aq_ret;
2456 }
2457 
2458 /**
2459  * i40e_set_promiscuous - set promiscuous mode
2460  * @pf: board private structure
2461  * @promisc: promisc on or off
2462  *
2463  * There are different ways of setting promiscuous mode on a PF depending on
2464  * what state/environment we're in.  This identifies and sets it appropriately.
2465  * Returns 0 on success.
2466  **/
2467 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2468 {
2469 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2470 	struct i40e_hw *hw = &pf->hw;
2471 	i40e_status aq_ret;
2472 
2473 	if (vsi->type == I40E_VSI_MAIN &&
2474 	    pf->lan_veb != I40E_NO_VEB &&
2475 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2476 		/* set defport ON for Main VSI instead of true promisc
2477 		 * this way we will get all unicast/multicast and VLAN
2478 		 * promisc behavior but will not get VF or VMDq traffic
2479 		 * replicated on the Main VSI.
2480 		 */
2481 		if (promisc)
2482 			aq_ret = i40e_aq_set_default_vsi(hw,
2483 							 vsi->seid,
2484 							 NULL);
2485 		else
2486 			aq_ret = i40e_aq_clear_default_vsi(hw,
2487 							   vsi->seid,
2488 							   NULL);
2489 		if (aq_ret) {
2490 			dev_info(&pf->pdev->dev,
2491 				 "Set default VSI failed, err %s, aq_err %s\n",
2492 				 i40e_stat_str(hw, aq_ret),
2493 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2494 		}
2495 	} else {
2496 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2497 						  hw,
2498 						  vsi->seid,
2499 						  promisc, NULL,
2500 						  true);
2501 		if (aq_ret) {
2502 			dev_info(&pf->pdev->dev,
2503 				 "set unicast promisc failed, err %s, aq_err %s\n",
2504 				 i40e_stat_str(hw, aq_ret),
2505 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2506 		}
2507 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2508 						  hw,
2509 						  vsi->seid,
2510 						  promisc, NULL);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "set multicast promisc failed, err %s, aq_err %s\n",
2514 				 i40e_stat_str(hw, aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 	}
2518 
2519 	if (!aq_ret)
2520 		pf->cur_promisc = promisc;
2521 
2522 	return aq_ret;
2523 }
2524 
2525 /**
2526  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2527  * @vsi: ptr to the VSI
2528  *
2529  * Push any outstanding VSI filter changes through the AdminQ.
2530  *
2531  * Returns 0 or error value
2532  **/
2533 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2534 {
2535 	struct hlist_head tmp_add_list, tmp_del_list;
2536 	struct i40e_mac_filter *f;
2537 	struct i40e_new_mac_filter *new, *add_head = NULL;
2538 	struct i40e_hw *hw = &vsi->back->hw;
2539 	bool old_overflow, new_overflow;
2540 	unsigned int failed_filters = 0;
2541 	unsigned int vlan_filters = 0;
2542 	char vsi_name[16] = "PF";
2543 	int filter_list_len = 0;
2544 	i40e_status aq_ret = 0;
2545 	u32 changed_flags = 0;
2546 	struct hlist_node *h;
2547 	struct i40e_pf *pf;
2548 	int num_add = 0;
2549 	int num_del = 0;
2550 	int retval = 0;
2551 	u16 cmd_flags;
2552 	int list_size;
2553 	int bkt;
2554 
2555 	/* empty array typed pointers, kcalloc later */
2556 	struct i40e_aqc_add_macvlan_element_data *add_list;
2557 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2558 
2559 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2560 		usleep_range(1000, 2000);
2561 	pf = vsi->back;
2562 
2563 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2564 
2565 	if (vsi->netdev) {
2566 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2567 		vsi->current_netdev_flags = vsi->netdev->flags;
2568 	}
2569 
2570 	INIT_HLIST_HEAD(&tmp_add_list);
2571 	INIT_HLIST_HEAD(&tmp_del_list);
2572 
2573 	if (vsi->type == I40E_VSI_SRIOV)
2574 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2575 	else if (vsi->type != I40E_VSI_MAIN)
2576 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2577 
2578 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2579 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2580 
2581 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2582 		/* Create a list of filters to delete. */
2583 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2584 			if (f->state == I40E_FILTER_REMOVE) {
2585 				/* Move the element into temporary del_list */
2586 				hash_del(&f->hlist);
2587 				hlist_add_head(&f->hlist, &tmp_del_list);
2588 
2589 				/* Avoid counting removed filters */
2590 				continue;
2591 			}
2592 			if (f->state == I40E_FILTER_NEW) {
2593 				/* Create a temporary i40e_new_mac_filter */
2594 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2595 				if (!new)
2596 					goto err_no_memory_locked;
2597 
2598 				/* Store pointer to the real filter */
2599 				new->f = f;
2600 				new->state = f->state;
2601 
2602 				/* Add it to the hash list */
2603 				hlist_add_head(&new->hlist, &tmp_add_list);
2604 			}
2605 
2606 			/* Count the number of active (current and new) VLAN
2607 			 * filters we have now. Does not count filters which
2608 			 * are marked for deletion.
2609 			 */
2610 			if (f->vlan > 0)
2611 				vlan_filters++;
2612 		}
2613 
2614 		if (vsi->type != I40E_VSI_SRIOV)
2615 			retval = i40e_correct_mac_vlan_filters
2616 				(vsi, &tmp_add_list, &tmp_del_list,
2617 				 vlan_filters);
2618 		else
2619 			retval = i40e_correct_vf_mac_vlan_filters
2620 				(vsi, &tmp_add_list, &tmp_del_list,
2621 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2622 
2623 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2624 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2625 
2626 		if (retval)
2627 			goto err_no_memory_locked;
2628 
2629 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2630 	}
2631 
2632 	/* Now process 'del_list' outside the lock */
2633 	if (!hlist_empty(&tmp_del_list)) {
2634 		filter_list_len = hw->aq.asq_buf_size /
2635 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2636 		list_size = filter_list_len *
2637 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2638 		del_list = kzalloc(list_size, GFP_ATOMIC);
2639 		if (!del_list)
2640 			goto err_no_memory;
2641 
2642 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2643 			cmd_flags = 0;
2644 
2645 			/* handle broadcast filters by updating the broadcast
2646 			 * promiscuous flag and release filter list.
2647 			 */
2648 			if (is_broadcast_ether_addr(f->macaddr)) {
2649 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2650 
2651 				hlist_del(&f->hlist);
2652 				kfree(f);
2653 				continue;
2654 			}
2655 
2656 			/* add to delete list */
2657 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2658 			if (f->vlan == I40E_VLAN_ANY) {
2659 				del_list[num_del].vlan_tag = 0;
2660 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2661 			} else {
2662 				del_list[num_del].vlan_tag =
2663 					cpu_to_le16((u16)(f->vlan));
2664 			}
2665 
2666 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2667 			del_list[num_del].flags = cmd_flags;
2668 			num_del++;
2669 
2670 			/* flush a full buffer */
2671 			if (num_del == filter_list_len) {
2672 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2673 						     num_del, &retval);
2674 				memset(del_list, 0, list_size);
2675 				num_del = 0;
2676 			}
2677 			/* Release memory for MAC filter entries which were
2678 			 * synced up with HW.
2679 			 */
2680 			hlist_del(&f->hlist);
2681 			kfree(f);
2682 		}
2683 
2684 		if (num_del) {
2685 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2686 					     num_del, &retval);
2687 		}
2688 
2689 		kfree(del_list);
2690 		del_list = NULL;
2691 	}
2692 
2693 	if (!hlist_empty(&tmp_add_list)) {
2694 		/* Do all the adds now. */
2695 		filter_list_len = hw->aq.asq_buf_size /
2696 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2697 		list_size = filter_list_len *
2698 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2699 		add_list = kzalloc(list_size, GFP_ATOMIC);
2700 		if (!add_list)
2701 			goto err_no_memory;
2702 
2703 		num_add = 0;
2704 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2705 			/* handle broadcast filters by updating the broadcast
2706 			 * promiscuous flag instead of adding a MAC filter.
2707 			 */
2708 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2709 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2710 							      new->f))
2711 					new->state = I40E_FILTER_FAILED;
2712 				else
2713 					new->state = I40E_FILTER_ACTIVE;
2714 				continue;
2715 			}
2716 
2717 			/* add to add array */
2718 			if (num_add == 0)
2719 				add_head = new;
2720 			cmd_flags = 0;
2721 			ether_addr_copy(add_list[num_add].mac_addr,
2722 					new->f->macaddr);
2723 			if (new->f->vlan == I40E_VLAN_ANY) {
2724 				add_list[num_add].vlan_tag = 0;
2725 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2726 			} else {
2727 				add_list[num_add].vlan_tag =
2728 					cpu_to_le16((u16)(new->f->vlan));
2729 			}
2730 			add_list[num_add].queue_number = 0;
2731 			/* set invalid match method for later detection */
2732 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2733 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2734 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2735 			num_add++;
2736 
2737 			/* flush a full buffer */
2738 			if (num_add == filter_list_len) {
2739 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2740 						     add_head, num_add);
2741 				memset(add_list, 0, list_size);
2742 				num_add = 0;
2743 			}
2744 		}
2745 		if (num_add) {
2746 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2747 					     num_add);
2748 		}
2749 		/* Now move all of the filters from the temp add list back to
2750 		 * the VSI's list.
2751 		 */
2752 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2753 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2754 			/* Only update the state if we're still NEW */
2755 			if (new->f->state == I40E_FILTER_NEW)
2756 				new->f->state = new->state;
2757 			hlist_del(&new->hlist);
2758 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2759 			kfree(new);
2760 		}
2761 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2762 		kfree(add_list);
2763 		add_list = NULL;
2764 	}
2765 
2766 	/* Determine the number of active and failed filters. */
2767 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2768 	vsi->active_filters = 0;
2769 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2770 		if (f->state == I40E_FILTER_ACTIVE)
2771 			vsi->active_filters++;
2772 		else if (f->state == I40E_FILTER_FAILED)
2773 			failed_filters++;
2774 	}
2775 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2776 
2777 	/* Check if we are able to exit overflow promiscuous mode. We can
2778 	 * safely exit if we didn't just enter, we no longer have any failed
2779 	 * filters, and we have reduced filters below the threshold value.
2780 	 */
2781 	if (old_overflow && !failed_filters &&
2782 	    vsi->active_filters < vsi->promisc_threshold) {
2783 		dev_info(&pf->pdev->dev,
2784 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2785 			 vsi_name);
2786 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2787 		vsi->promisc_threshold = 0;
2788 	}
2789 
2790 	/* if the VF is not trusted do not do promisc */
2791 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2792 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 		goto out;
2794 	}
2795 
2796 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2797 
2798 	/* If we are entering overflow promiscuous, we need to calculate a new
2799 	 * threshold for when we are safe to exit
2800 	 */
2801 	if (!old_overflow && new_overflow)
2802 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2803 
2804 	/* check for changes in promiscuous modes */
2805 	if (changed_flags & IFF_ALLMULTI) {
2806 		bool cur_multipromisc;
2807 
2808 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2809 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2810 							       vsi->seid,
2811 							       cur_multipromisc,
2812 							       NULL);
2813 		if (aq_ret) {
2814 			retval = i40e_aq_rc_to_posix(aq_ret,
2815 						     hw->aq.asq_last_status);
2816 			dev_info(&pf->pdev->dev,
2817 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2818 				 vsi_name,
2819 				 i40e_stat_str(hw, aq_ret),
2820 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2821 		} else {
2822 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2823 				 cur_multipromisc ? "entering" : "leaving");
2824 		}
2825 	}
2826 
2827 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2828 		bool cur_promisc;
2829 
2830 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2831 			       new_overflow);
2832 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2833 		if (aq_ret) {
2834 			retval = i40e_aq_rc_to_posix(aq_ret,
2835 						     hw->aq.asq_last_status);
2836 			dev_info(&pf->pdev->dev,
2837 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2838 				 cur_promisc ? "on" : "off",
2839 				 vsi_name,
2840 				 i40e_stat_str(hw, aq_ret),
2841 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2842 		}
2843 	}
2844 out:
2845 	/* if something went wrong then set the changed flag so we try again */
2846 	if (retval)
2847 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2848 
2849 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2850 	return retval;
2851 
2852 err_no_memory:
2853 	/* Restore elements on the temporary add and delete lists */
2854 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2855 err_no_memory_locked:
2856 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2857 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2858 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2859 
2860 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2861 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2862 	return -ENOMEM;
2863 }
2864 
2865 /**
2866  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2867  * @pf: board private structure
2868  **/
2869 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2870 {
2871 	int v;
2872 
2873 	if (!pf)
2874 		return;
2875 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2876 		return;
2877 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2878 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2879 		return;
2880 	}
2881 
2882 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2883 		if (pf->vsi[v] &&
2884 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2885 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2886 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2887 
2888 			if (ret) {
2889 				/* come back and try again later */
2890 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2891 					pf->state);
2892 				break;
2893 			}
2894 		}
2895 	}
2896 }
2897 
2898 /**
2899  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2900  * @vsi: the vsi
2901  **/
2902 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2903 {
2904 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2905 		return I40E_RXBUFFER_2048;
2906 	else
2907 		return I40E_RXBUFFER_3072;
2908 }
2909 
2910 /**
2911  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2912  * @netdev: network interface device structure
2913  * @new_mtu: new value for maximum frame size
2914  *
2915  * Returns 0 on success, negative on failure
2916  **/
2917 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2918 {
2919 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2920 	struct i40e_vsi *vsi = np->vsi;
2921 	struct i40e_pf *pf = vsi->back;
2922 
2923 	if (i40e_enabled_xdp_vsi(vsi)) {
2924 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2925 
2926 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2927 			return -EINVAL;
2928 	}
2929 
2930 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2931 		   netdev->mtu, new_mtu);
2932 	netdev->mtu = new_mtu;
2933 	if (netif_running(netdev))
2934 		i40e_vsi_reinit_locked(vsi);
2935 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2936 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2937 	return 0;
2938 }
2939 
2940 /**
2941  * i40e_ioctl - Access the hwtstamp interface
2942  * @netdev: network interface device structure
2943  * @ifr: interface request data
2944  * @cmd: ioctl command
2945  **/
2946 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2947 {
2948 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2949 	struct i40e_pf *pf = np->vsi->back;
2950 
2951 	switch (cmd) {
2952 	case SIOCGHWTSTAMP:
2953 		return i40e_ptp_get_ts_config(pf, ifr);
2954 	case SIOCSHWTSTAMP:
2955 		return i40e_ptp_set_ts_config(pf, ifr);
2956 	default:
2957 		return -EOPNOTSUPP;
2958 	}
2959 }
2960 
2961 /**
2962  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2963  * @vsi: the vsi being adjusted
2964  **/
2965 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2966 {
2967 	struct i40e_vsi_context ctxt;
2968 	i40e_status ret;
2969 
2970 	/* Don't modify stripping options if a port VLAN is active */
2971 	if (vsi->info.pvid)
2972 		return;
2973 
2974 	if ((vsi->info.valid_sections &
2975 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2976 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2977 		return;  /* already enabled */
2978 
2979 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2980 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2981 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2982 
2983 	ctxt.seid = vsi->seid;
2984 	ctxt.info = vsi->info;
2985 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2986 	if (ret) {
2987 		dev_info(&vsi->back->pdev->dev,
2988 			 "update vlan stripping failed, err %s aq_err %s\n",
2989 			 i40e_stat_str(&vsi->back->hw, ret),
2990 			 i40e_aq_str(&vsi->back->hw,
2991 				     vsi->back->hw.aq.asq_last_status));
2992 	}
2993 }
2994 
2995 /**
2996  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2997  * @vsi: the vsi being adjusted
2998  **/
2999 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3000 {
3001 	struct i40e_vsi_context ctxt;
3002 	i40e_status ret;
3003 
3004 	/* Don't modify stripping options if a port VLAN is active */
3005 	if (vsi->info.pvid)
3006 		return;
3007 
3008 	if ((vsi->info.valid_sections &
3009 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3010 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3011 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3012 		return;  /* already disabled */
3013 
3014 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3015 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3016 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3017 
3018 	ctxt.seid = vsi->seid;
3019 	ctxt.info = vsi->info;
3020 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3021 	if (ret) {
3022 		dev_info(&vsi->back->pdev->dev,
3023 			 "update vlan stripping failed, err %s aq_err %s\n",
3024 			 i40e_stat_str(&vsi->back->hw, ret),
3025 			 i40e_aq_str(&vsi->back->hw,
3026 				     vsi->back->hw.aq.asq_last_status));
3027 	}
3028 }
3029 
3030 /**
3031  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3032  * @vsi: the vsi being configured
3033  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3034  *
3035  * This is a helper function for adding a new MAC/VLAN filter with the
3036  * specified VLAN for each existing MAC address already in the hash table.
3037  * This function does *not* perform any accounting to update filters based on
3038  * VLAN mode.
3039  *
3040  * NOTE: this function expects to be called while under the
3041  * mac_filter_hash_lock
3042  **/
3043 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3044 {
3045 	struct i40e_mac_filter *f, *add_f;
3046 	struct hlist_node *h;
3047 	int bkt;
3048 
3049 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3050 		/* If we're asked to add a filter that has been marked for
3051 		 * removal, it is safe to simply restore it to active state.
3052 		 * __i40e_del_filter will have simply deleted any filters which
3053 		 * were previously marked NEW or FAILED, so if it is currently
3054 		 * marked REMOVE it must have previously been ACTIVE. Since we
3055 		 * haven't yet run the sync filters task, just restore this
3056 		 * filter to the ACTIVE state so that the sync task leaves it
3057 		 * in place.
3058 		 */
3059 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3060 			f->state = I40E_FILTER_ACTIVE;
3061 			continue;
3062 		} else if (f->state == I40E_FILTER_REMOVE) {
3063 			continue;
3064 		}
3065 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3066 		if (!add_f) {
3067 			dev_info(&vsi->back->pdev->dev,
3068 				 "Could not add vlan filter %d for %pM\n",
3069 				 vid, f->macaddr);
3070 			return -ENOMEM;
3071 		}
3072 	}
3073 
3074 	return 0;
3075 }
3076 
3077 /**
3078  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3079  * @vsi: the VSI being configured
3080  * @vid: VLAN id to be added
3081  **/
3082 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3083 {
3084 	int err;
3085 
3086 	if (vsi->info.pvid)
3087 		return -EINVAL;
3088 
3089 	/* The network stack will attempt to add VID=0, with the intention to
3090 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3091 	 * these packets by default when configured to receive untagged
3092 	 * packets, so we don't need to add a filter for this case.
3093 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3094 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3095 	 * Thus, we do not want to actually add a filter for VID=0
3096 	 */
3097 	if (!vid)
3098 		return 0;
3099 
3100 	/* Locked once because all functions invoked below iterates list*/
3101 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3102 	err = i40e_add_vlan_all_mac(vsi, vid);
3103 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3104 	if (err)
3105 		return err;
3106 
3107 	/* schedule our worker thread which will take care of
3108 	 * applying the new filter changes
3109 	 */
3110 	i40e_service_event_schedule(vsi->back);
3111 	return 0;
3112 }
3113 
3114 /**
3115  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3116  * @vsi: the vsi being configured
3117  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3118  *
3119  * This function should be used to remove all VLAN filters which match the
3120  * given VID. It does not schedule the service event and does not take the
3121  * mac_filter_hash_lock so it may be combined with other operations under
3122  * a single invocation of the mac_filter_hash_lock.
3123  *
3124  * NOTE: this function expects to be called while under the
3125  * mac_filter_hash_lock
3126  */
3127 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3128 {
3129 	struct i40e_mac_filter *f;
3130 	struct hlist_node *h;
3131 	int bkt;
3132 
3133 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3134 		if (f->vlan == vid)
3135 			__i40e_del_filter(vsi, f);
3136 	}
3137 }
3138 
3139 /**
3140  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3141  * @vsi: the VSI being configured
3142  * @vid: VLAN id to be removed
3143  **/
3144 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3145 {
3146 	if (!vid || vsi->info.pvid)
3147 		return;
3148 
3149 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3150 	i40e_rm_vlan_all_mac(vsi, vid);
3151 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3152 
3153 	/* schedule our worker thread which will take care of
3154 	 * applying the new filter changes
3155 	 */
3156 	i40e_service_event_schedule(vsi->back);
3157 }
3158 
3159 /**
3160  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3161  * @netdev: network interface to be adjusted
3162  * @proto: unused protocol value
3163  * @vid: vlan id to be added
3164  *
3165  * net_device_ops implementation for adding vlan ids
3166  **/
3167 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3168 				__always_unused __be16 proto, u16 vid)
3169 {
3170 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3171 	struct i40e_vsi *vsi = np->vsi;
3172 	int ret = 0;
3173 
3174 	if (vid >= VLAN_N_VID)
3175 		return -EINVAL;
3176 
3177 	ret = i40e_vsi_add_vlan(vsi, vid);
3178 	if (!ret)
3179 		set_bit(vid, vsi->active_vlans);
3180 
3181 	return ret;
3182 }
3183 
3184 /**
3185  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3186  * @netdev: network interface to be adjusted
3187  * @proto: unused protocol value
3188  * @vid: vlan id to be added
3189  **/
3190 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3191 				    __always_unused __be16 proto, u16 vid)
3192 {
3193 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3194 	struct i40e_vsi *vsi = np->vsi;
3195 
3196 	if (vid >= VLAN_N_VID)
3197 		return;
3198 	set_bit(vid, vsi->active_vlans);
3199 }
3200 
3201 /**
3202  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3203  * @netdev: network interface to be adjusted
3204  * @proto: unused protocol value
3205  * @vid: vlan id to be removed
3206  *
3207  * net_device_ops implementation for removing vlan ids
3208  **/
3209 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3210 				 __always_unused __be16 proto, u16 vid)
3211 {
3212 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3213 	struct i40e_vsi *vsi = np->vsi;
3214 
3215 	/* return code is ignored as there is nothing a user
3216 	 * can do about failure to remove and a log message was
3217 	 * already printed from the other function
3218 	 */
3219 	i40e_vsi_kill_vlan(vsi, vid);
3220 
3221 	clear_bit(vid, vsi->active_vlans);
3222 
3223 	return 0;
3224 }
3225 
3226 /**
3227  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3228  * @vsi: the vsi being brought back up
3229  **/
3230 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3231 {
3232 	u16 vid;
3233 
3234 	if (!vsi->netdev)
3235 		return;
3236 
3237 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3238 		i40e_vlan_stripping_enable(vsi);
3239 	else
3240 		i40e_vlan_stripping_disable(vsi);
3241 
3242 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3243 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3244 					vid);
3245 }
3246 
3247 /**
3248  * i40e_vsi_add_pvid - Add pvid for the VSI
3249  * @vsi: the vsi being adjusted
3250  * @vid: the vlan id to set as a PVID
3251  **/
3252 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3253 {
3254 	struct i40e_vsi_context ctxt;
3255 	i40e_status ret;
3256 
3257 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3258 	vsi->info.pvid = cpu_to_le16(vid);
3259 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3260 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3261 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3262 
3263 	ctxt.seid = vsi->seid;
3264 	ctxt.info = vsi->info;
3265 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3266 	if (ret) {
3267 		dev_info(&vsi->back->pdev->dev,
3268 			 "add pvid failed, err %s aq_err %s\n",
3269 			 i40e_stat_str(&vsi->back->hw, ret),
3270 			 i40e_aq_str(&vsi->back->hw,
3271 				     vsi->back->hw.aq.asq_last_status));
3272 		return -ENOENT;
3273 	}
3274 
3275 	return 0;
3276 }
3277 
3278 /**
3279  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3280  * @vsi: the vsi being adjusted
3281  *
3282  * Just use the vlan_rx_register() service to put it back to normal
3283  **/
3284 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3285 {
3286 	vsi->info.pvid = 0;
3287 
3288 	i40e_vlan_stripping_disable(vsi);
3289 }
3290 
3291 /**
3292  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3293  * @vsi: ptr to the VSI
3294  *
3295  * If this function returns with an error, then it's possible one or
3296  * more of the rings is populated (while the rest are not).  It is the
3297  * callers duty to clean those orphaned rings.
3298  *
3299  * Return 0 on success, negative on failure
3300  **/
3301 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3302 {
3303 	int i, err = 0;
3304 
3305 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3306 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3307 
3308 	if (!i40e_enabled_xdp_vsi(vsi))
3309 		return err;
3310 
3311 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3312 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3313 
3314 	return err;
3315 }
3316 
3317 /**
3318  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3319  * @vsi: ptr to the VSI
3320  *
3321  * Free VSI's transmit software resources
3322  **/
3323 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3324 {
3325 	int i;
3326 
3327 	if (vsi->tx_rings) {
3328 		for (i = 0; i < vsi->num_queue_pairs; i++)
3329 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3330 				i40e_free_tx_resources(vsi->tx_rings[i]);
3331 	}
3332 
3333 	if (vsi->xdp_rings) {
3334 		for (i = 0; i < vsi->num_queue_pairs; i++)
3335 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3336 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3337 	}
3338 }
3339 
3340 /**
3341  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3342  * @vsi: ptr to the VSI
3343  *
3344  * If this function returns with an error, then it's possible one or
3345  * more of the rings is populated (while the rest are not).  It is the
3346  * callers duty to clean those orphaned rings.
3347  *
3348  * Return 0 on success, negative on failure
3349  **/
3350 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3351 {
3352 	int i, err = 0;
3353 
3354 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3355 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3356 	return err;
3357 }
3358 
3359 /**
3360  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3361  * @vsi: ptr to the VSI
3362  *
3363  * Free all receive software resources
3364  **/
3365 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3366 {
3367 	int i;
3368 
3369 	if (!vsi->rx_rings)
3370 		return;
3371 
3372 	for (i = 0; i < vsi->num_queue_pairs; i++)
3373 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3374 			i40e_free_rx_resources(vsi->rx_rings[i]);
3375 }
3376 
3377 /**
3378  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3379  * @ring: The Tx ring to configure
3380  *
3381  * This enables/disables XPS for a given Tx descriptor ring
3382  * based on the TCs enabled for the VSI that ring belongs to.
3383  **/
3384 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3385 {
3386 	int cpu;
3387 
3388 	if (!ring->q_vector || !ring->netdev || ring->ch)
3389 		return;
3390 
3391 	/* We only initialize XPS once, so as not to overwrite user settings */
3392 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3393 		return;
3394 
3395 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3396 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3397 			    ring->queue_index);
3398 }
3399 
3400 /**
3401  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3402  * @ring: The Tx or Rx ring
3403  *
3404  * Returns the AF_XDP buffer pool or NULL.
3405  **/
3406 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3407 {
3408 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3409 	int qid = ring->queue_index;
3410 
3411 	if (ring_is_xdp(ring))
3412 		qid -= ring->vsi->alloc_queue_pairs;
3413 
3414 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3415 		return NULL;
3416 
3417 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3418 }
3419 
3420 /**
3421  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3422  * @ring: The Tx ring to configure
3423  *
3424  * Configure the Tx descriptor ring in the HMC context.
3425  **/
3426 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3427 {
3428 	struct i40e_vsi *vsi = ring->vsi;
3429 	u16 pf_q = vsi->base_queue + ring->queue_index;
3430 	struct i40e_hw *hw = &vsi->back->hw;
3431 	struct i40e_hmc_obj_txq tx_ctx;
3432 	i40e_status err = 0;
3433 	u32 qtx_ctl = 0;
3434 
3435 	if (ring_is_xdp(ring))
3436 		ring->xsk_pool = i40e_xsk_pool(ring);
3437 
3438 	/* some ATR related tx ring init */
3439 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3440 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3441 		ring->atr_count = 0;
3442 	} else {
3443 		ring->atr_sample_rate = 0;
3444 	}
3445 
3446 	/* configure XPS */
3447 	i40e_config_xps_tx_ring(ring);
3448 
3449 	/* clear the context structure first */
3450 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3451 
3452 	tx_ctx.new_context = 1;
3453 	tx_ctx.base = (ring->dma / 128);
3454 	tx_ctx.qlen = ring->count;
3455 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3456 					       I40E_FLAG_FD_ATR_ENABLED));
3457 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3458 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3459 	if (vsi->type != I40E_VSI_FDIR)
3460 		tx_ctx.head_wb_ena = 1;
3461 	tx_ctx.head_wb_addr = ring->dma +
3462 			      (ring->count * sizeof(struct i40e_tx_desc));
3463 
3464 	/* As part of VSI creation/update, FW allocates certain
3465 	 * Tx arbitration queue sets for each TC enabled for
3466 	 * the VSI. The FW returns the handles to these queue
3467 	 * sets as part of the response buffer to Add VSI,
3468 	 * Update VSI, etc. AQ commands. It is expected that
3469 	 * these queue set handles be associated with the Tx
3470 	 * queues by the driver as part of the TX queue context
3471 	 * initialization. This has to be done regardless of
3472 	 * DCB as by default everything is mapped to TC0.
3473 	 */
3474 
3475 	if (ring->ch)
3476 		tx_ctx.rdylist =
3477 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3478 
3479 	else
3480 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3481 
3482 	tx_ctx.rdylist_act = 0;
3483 
3484 	/* clear the context in the HMC */
3485 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3486 	if (err) {
3487 		dev_info(&vsi->back->pdev->dev,
3488 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3489 			 ring->queue_index, pf_q, err);
3490 		return -ENOMEM;
3491 	}
3492 
3493 	/* set the context in the HMC */
3494 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3495 	if (err) {
3496 		dev_info(&vsi->back->pdev->dev,
3497 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3498 			 ring->queue_index, pf_q, err);
3499 		return -ENOMEM;
3500 	}
3501 
3502 	/* Now associate this queue with this PCI function */
3503 	if (ring->ch) {
3504 		if (ring->ch->type == I40E_VSI_VMDQ2)
3505 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3506 		else
3507 			return -EINVAL;
3508 
3509 		qtx_ctl |= (ring->ch->vsi_number <<
3510 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3511 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3512 	} else {
3513 		if (vsi->type == I40E_VSI_VMDQ2) {
3514 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3515 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3516 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3517 		} else {
3518 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3519 		}
3520 	}
3521 
3522 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3523 		    I40E_QTX_CTL_PF_INDX_MASK);
3524 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3525 	i40e_flush(hw);
3526 
3527 	/* cache tail off for easier writes later */
3528 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3529 
3530 	return 0;
3531 }
3532 
3533 /**
3534  * i40e_rx_offset - Return expected offset into page to access data
3535  * @rx_ring: Ring we are requesting offset of
3536  *
3537  * Returns the offset value for ring into the data buffer.
3538  */
3539 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3540 {
3541 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3542 }
3543 
3544 /**
3545  * i40e_configure_rx_ring - Configure a receive ring context
3546  * @ring: The Rx ring to configure
3547  *
3548  * Configure the Rx descriptor ring in the HMC context.
3549  **/
3550 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3551 {
3552 	struct i40e_vsi *vsi = ring->vsi;
3553 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3554 	u16 pf_q = vsi->base_queue + ring->queue_index;
3555 	struct i40e_hw *hw = &vsi->back->hw;
3556 	struct i40e_hmc_obj_rxq rx_ctx;
3557 	i40e_status err = 0;
3558 	bool ok;
3559 	int ret;
3560 
3561 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3562 
3563 	/* clear the context structure first */
3564 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3565 
3566 	if (ring->vsi->type == I40E_VSI_MAIN)
3567 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3568 
3569 	ring->xsk_pool = i40e_xsk_pool(ring);
3570 	if (ring->xsk_pool) {
3571 		ring->rx_buf_len =
3572 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3573 		/* For AF_XDP ZC, we disallow packets to span on
3574 		 * multiple buffers, thus letting us skip that
3575 		 * handling in the fast-path.
3576 		 */
3577 		chain_len = 1;
3578 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3579 						 MEM_TYPE_XSK_BUFF_POOL,
3580 						 NULL);
3581 		if (ret)
3582 			return ret;
3583 		dev_info(&vsi->back->pdev->dev,
3584 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3585 			 ring->queue_index);
3586 
3587 	} else {
3588 		ring->rx_buf_len = vsi->rx_buf_len;
3589 		if (ring->vsi->type == I40E_VSI_MAIN) {
3590 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3591 							 MEM_TYPE_PAGE_SHARED,
3592 							 NULL);
3593 			if (ret)
3594 				return ret;
3595 		}
3596 	}
3597 
3598 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3599 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3600 
3601 	rx_ctx.base = (ring->dma / 128);
3602 	rx_ctx.qlen = ring->count;
3603 
3604 	/* use 16 byte descriptors */
3605 	rx_ctx.dsize = 0;
3606 
3607 	/* descriptor type is always zero
3608 	 * rx_ctx.dtype = 0;
3609 	 */
3610 	rx_ctx.hsplit_0 = 0;
3611 
3612 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3613 	if (hw->revision_id == 0)
3614 		rx_ctx.lrxqthresh = 0;
3615 	else
3616 		rx_ctx.lrxqthresh = 1;
3617 	rx_ctx.crcstrip = 1;
3618 	rx_ctx.l2tsel = 1;
3619 	/* this controls whether VLAN is stripped from inner headers */
3620 	rx_ctx.showiv = 0;
3621 	/* set the prefena field to 1 because the manual says to */
3622 	rx_ctx.prefena = 1;
3623 
3624 	/* clear the context in the HMC */
3625 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3626 	if (err) {
3627 		dev_info(&vsi->back->pdev->dev,
3628 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3629 			 ring->queue_index, pf_q, err);
3630 		return -ENOMEM;
3631 	}
3632 
3633 	/* set the context in the HMC */
3634 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3635 	if (err) {
3636 		dev_info(&vsi->back->pdev->dev,
3637 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3638 			 ring->queue_index, pf_q, err);
3639 		return -ENOMEM;
3640 	}
3641 
3642 	/* configure Rx buffer alignment */
3643 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3644 		clear_ring_build_skb_enabled(ring);
3645 	else
3646 		set_ring_build_skb_enabled(ring);
3647 
3648 	ring->rx_offset = i40e_rx_offset(ring);
3649 
3650 	/* cache tail for quicker writes, and clear the reg before use */
3651 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3652 	writel(0, ring->tail);
3653 
3654 	if (ring->xsk_pool) {
3655 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3656 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3657 	} else {
3658 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3659 	}
3660 	if (!ok) {
3661 		/* Log this in case the user has forgotten to give the kernel
3662 		 * any buffers, even later in the application.
3663 		 */
3664 		dev_info(&vsi->back->pdev->dev,
3665 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3666 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3667 			 ring->queue_index, pf_q);
3668 	}
3669 
3670 	return 0;
3671 }
3672 
3673 /**
3674  * i40e_vsi_configure_tx - Configure the VSI for Tx
3675  * @vsi: VSI structure describing this set of rings and resources
3676  *
3677  * Configure the Tx VSI for operation.
3678  **/
3679 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3680 {
3681 	int err = 0;
3682 	u16 i;
3683 
3684 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3685 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3686 
3687 	if (err || !i40e_enabled_xdp_vsi(vsi))
3688 		return err;
3689 
3690 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3691 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3692 
3693 	return err;
3694 }
3695 
3696 /**
3697  * i40e_vsi_configure_rx - Configure the VSI for Rx
3698  * @vsi: the VSI being configured
3699  *
3700  * Configure the Rx VSI for operation.
3701  **/
3702 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3703 {
3704 	int err = 0;
3705 	u16 i;
3706 
3707 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3708 		vsi->max_frame = I40E_MAX_RXBUFFER;
3709 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3710 #if (PAGE_SIZE < 8192)
3711 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3712 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3713 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3714 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3715 #endif
3716 	} else {
3717 		vsi->max_frame = I40E_MAX_RXBUFFER;
3718 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3719 						       I40E_RXBUFFER_2048;
3720 	}
3721 
3722 	/* set up individual rings */
3723 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3724 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3725 
3726 	return err;
3727 }
3728 
3729 /**
3730  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3731  * @vsi: ptr to the VSI
3732  **/
3733 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3734 {
3735 	struct i40e_ring *tx_ring, *rx_ring;
3736 	u16 qoffset, qcount;
3737 	int i, n;
3738 
3739 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3740 		/* Reset the TC information */
3741 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3742 			rx_ring = vsi->rx_rings[i];
3743 			tx_ring = vsi->tx_rings[i];
3744 			rx_ring->dcb_tc = 0;
3745 			tx_ring->dcb_tc = 0;
3746 		}
3747 		return;
3748 	}
3749 
3750 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3751 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3752 			continue;
3753 
3754 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3755 		qcount = vsi->tc_config.tc_info[n].qcount;
3756 		for (i = qoffset; i < (qoffset + qcount); i++) {
3757 			rx_ring = vsi->rx_rings[i];
3758 			tx_ring = vsi->tx_rings[i];
3759 			rx_ring->dcb_tc = n;
3760 			tx_ring->dcb_tc = n;
3761 		}
3762 	}
3763 }
3764 
3765 /**
3766  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3767  * @vsi: ptr to the VSI
3768  **/
3769 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3770 {
3771 	if (vsi->netdev)
3772 		i40e_set_rx_mode(vsi->netdev);
3773 }
3774 
3775 /**
3776  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3777  * @pf: Pointer to the targeted PF
3778  *
3779  * Set all flow director counters to 0.
3780  */
3781 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3782 {
3783 	pf->fd_tcp4_filter_cnt = 0;
3784 	pf->fd_udp4_filter_cnt = 0;
3785 	pf->fd_sctp4_filter_cnt = 0;
3786 	pf->fd_ip4_filter_cnt = 0;
3787 	pf->fd_tcp6_filter_cnt = 0;
3788 	pf->fd_udp6_filter_cnt = 0;
3789 	pf->fd_sctp6_filter_cnt = 0;
3790 	pf->fd_ip6_filter_cnt = 0;
3791 }
3792 
3793 /**
3794  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3795  * @vsi: Pointer to the targeted VSI
3796  *
3797  * This function replays the hlist on the hw where all the SB Flow Director
3798  * filters were saved.
3799  **/
3800 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3801 {
3802 	struct i40e_fdir_filter *filter;
3803 	struct i40e_pf *pf = vsi->back;
3804 	struct hlist_node *node;
3805 
3806 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3807 		return;
3808 
3809 	/* Reset FDir counters as we're replaying all existing filters */
3810 	i40e_reset_fdir_filter_cnt(pf);
3811 
3812 	hlist_for_each_entry_safe(filter, node,
3813 				  &pf->fdir_filter_list, fdir_node) {
3814 		i40e_add_del_fdir(vsi, filter, true);
3815 	}
3816 }
3817 
3818 /**
3819  * i40e_vsi_configure - Set up the VSI for action
3820  * @vsi: the VSI being configured
3821  **/
3822 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3823 {
3824 	int err;
3825 
3826 	i40e_set_vsi_rx_mode(vsi);
3827 	i40e_restore_vlan(vsi);
3828 	i40e_vsi_config_dcb_rings(vsi);
3829 	err = i40e_vsi_configure_tx(vsi);
3830 	if (!err)
3831 		err = i40e_vsi_configure_rx(vsi);
3832 
3833 	return err;
3834 }
3835 
3836 /**
3837  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3838  * @vsi: the VSI being configured
3839  **/
3840 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3841 {
3842 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3843 	struct i40e_pf *pf = vsi->back;
3844 	struct i40e_hw *hw = &pf->hw;
3845 	u16 vector;
3846 	int i, q;
3847 	u32 qp;
3848 
3849 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3850 	 * and PFINT_LNKLSTn registers, e.g.:
3851 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3852 	 */
3853 	qp = vsi->base_queue;
3854 	vector = vsi->base_vector;
3855 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3856 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3857 
3858 		q_vector->rx.next_update = jiffies + 1;
3859 		q_vector->rx.target_itr =
3860 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3861 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3862 		     q_vector->rx.target_itr >> 1);
3863 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3864 
3865 		q_vector->tx.next_update = jiffies + 1;
3866 		q_vector->tx.target_itr =
3867 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3868 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3869 		     q_vector->tx.target_itr >> 1);
3870 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3871 
3872 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3873 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3874 
3875 		/* begin of linked list for RX queue assigned to this vector */
3876 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3877 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3878 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3879 			u32 val;
3880 
3881 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3882 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3883 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3884 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3885 			      (I40E_QUEUE_TYPE_TX <<
3886 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3887 
3888 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3889 
3890 			if (has_xdp) {
3891 				/* TX queue with next queue set to TX */
3892 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3893 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3894 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3895 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3896 				      (I40E_QUEUE_TYPE_TX <<
3897 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3898 
3899 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3900 			}
3901 			/* TX queue with next RX or end of linked list */
3902 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3903 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3904 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3905 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3906 			      (I40E_QUEUE_TYPE_RX <<
3907 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3908 
3909 			/* Terminate the linked list */
3910 			if (q == (q_vector->num_ringpairs - 1))
3911 				val |= (I40E_QUEUE_END_OF_LIST <<
3912 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3913 
3914 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3915 			qp++;
3916 		}
3917 	}
3918 
3919 	i40e_flush(hw);
3920 }
3921 
3922 /**
3923  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3924  * @pf: pointer to private device data structure
3925  **/
3926 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3927 {
3928 	struct i40e_hw *hw = &pf->hw;
3929 	u32 val;
3930 
3931 	/* clear things first */
3932 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3933 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3934 
3935 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3936 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3937 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3938 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3939 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3940 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3941 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3942 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3943 
3944 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3945 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3946 
3947 	if (pf->flags & I40E_FLAG_PTP)
3948 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3949 
3950 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3951 
3952 	/* SW_ITR_IDX = 0, but don't change INTENA */
3953 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3954 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3955 
3956 	/* OTHER_ITR_IDX = 0 */
3957 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3958 }
3959 
3960 /**
3961  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3962  * @vsi: the VSI being configured
3963  **/
3964 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3965 {
3966 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3967 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3968 	struct i40e_pf *pf = vsi->back;
3969 	struct i40e_hw *hw = &pf->hw;
3970 
3971 	/* set the ITR configuration */
3972 	q_vector->rx.next_update = jiffies + 1;
3973 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3974 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3975 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3976 	q_vector->tx.next_update = jiffies + 1;
3977 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3978 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3979 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3980 
3981 	i40e_enable_misc_int_causes(pf);
3982 
3983 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3984 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3985 
3986 	/* Associate the queue pair to the vector and enable the queue
3987 	 * interrupt RX queue in linked list with next queue set to TX
3988 	 */
3989 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
3990 
3991 	if (i40e_enabled_xdp_vsi(vsi)) {
3992 		/* TX queue in linked list with next queue set to TX */
3993 		wr32(hw, I40E_QINT_TQCTL(nextqp),
3994 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
3995 	}
3996 
3997 	/* last TX queue so the next RX queue doesn't matter */
3998 	wr32(hw, I40E_QINT_TQCTL(0),
3999 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4000 	i40e_flush(hw);
4001 }
4002 
4003 /**
4004  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4005  * @pf: board private structure
4006  **/
4007 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4008 {
4009 	struct i40e_hw *hw = &pf->hw;
4010 
4011 	wr32(hw, I40E_PFINT_DYN_CTL0,
4012 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4013 	i40e_flush(hw);
4014 }
4015 
4016 /**
4017  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4018  * @pf: board private structure
4019  **/
4020 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4021 {
4022 	struct i40e_hw *hw = &pf->hw;
4023 	u32 val;
4024 
4025 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4026 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4027 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4028 
4029 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4030 	i40e_flush(hw);
4031 }
4032 
4033 /**
4034  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4035  * @irq: interrupt number
4036  * @data: pointer to a q_vector
4037  **/
4038 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4039 {
4040 	struct i40e_q_vector *q_vector = data;
4041 
4042 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4043 		return IRQ_HANDLED;
4044 
4045 	napi_schedule_irqoff(&q_vector->napi);
4046 
4047 	return IRQ_HANDLED;
4048 }
4049 
4050 /**
4051  * i40e_irq_affinity_notify - Callback for affinity changes
4052  * @notify: context as to what irq was changed
4053  * @mask: the new affinity mask
4054  *
4055  * This is a callback function used by the irq_set_affinity_notifier function
4056  * so that we may register to receive changes to the irq affinity masks.
4057  **/
4058 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4059 				     const cpumask_t *mask)
4060 {
4061 	struct i40e_q_vector *q_vector =
4062 		container_of(notify, struct i40e_q_vector, affinity_notify);
4063 
4064 	cpumask_copy(&q_vector->affinity_mask, mask);
4065 }
4066 
4067 /**
4068  * i40e_irq_affinity_release - Callback for affinity notifier release
4069  * @ref: internal core kernel usage
4070  *
4071  * This is a callback function used by the irq_set_affinity_notifier function
4072  * to inform the current notification subscriber that they will no longer
4073  * receive notifications.
4074  **/
4075 static void i40e_irq_affinity_release(struct kref *ref) {}
4076 
4077 /**
4078  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4079  * @vsi: the VSI being configured
4080  * @basename: name for the vector
4081  *
4082  * Allocates MSI-X vectors and requests interrupts from the kernel.
4083  **/
4084 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4085 {
4086 	int q_vectors = vsi->num_q_vectors;
4087 	struct i40e_pf *pf = vsi->back;
4088 	int base = vsi->base_vector;
4089 	int rx_int_idx = 0;
4090 	int tx_int_idx = 0;
4091 	int vector, err;
4092 	int irq_num;
4093 	int cpu;
4094 
4095 	for (vector = 0; vector < q_vectors; vector++) {
4096 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4097 
4098 		irq_num = pf->msix_entries[base + vector].vector;
4099 
4100 		if (q_vector->tx.ring && q_vector->rx.ring) {
4101 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4102 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4103 			tx_int_idx++;
4104 		} else if (q_vector->rx.ring) {
4105 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4106 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4107 		} else if (q_vector->tx.ring) {
4108 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4109 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4110 		} else {
4111 			/* skip this unused q_vector */
4112 			continue;
4113 		}
4114 		err = request_irq(irq_num,
4115 				  vsi->irq_handler,
4116 				  0,
4117 				  q_vector->name,
4118 				  q_vector);
4119 		if (err) {
4120 			dev_info(&pf->pdev->dev,
4121 				 "MSIX request_irq failed, error: %d\n", err);
4122 			goto free_queue_irqs;
4123 		}
4124 
4125 		/* register for affinity change notifications */
4126 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4127 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4128 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4129 		/* Spread affinity hints out across online CPUs.
4130 		 *
4131 		 * get_cpu_mask returns a static constant mask with
4132 		 * a permanent lifetime so it's ok to pass to
4133 		 * irq_update_affinity_hint without making a copy.
4134 		 */
4135 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4136 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4137 	}
4138 
4139 	vsi->irqs_ready = true;
4140 	return 0;
4141 
4142 free_queue_irqs:
4143 	while (vector) {
4144 		vector--;
4145 		irq_num = pf->msix_entries[base + vector].vector;
4146 		irq_set_affinity_notifier(irq_num, NULL);
4147 		irq_update_affinity_hint(irq_num, NULL);
4148 		free_irq(irq_num, &vsi->q_vectors[vector]);
4149 	}
4150 	return err;
4151 }
4152 
4153 /**
4154  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4155  * @vsi: the VSI being un-configured
4156  **/
4157 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4158 {
4159 	struct i40e_pf *pf = vsi->back;
4160 	struct i40e_hw *hw = &pf->hw;
4161 	int base = vsi->base_vector;
4162 	int i;
4163 
4164 	/* disable interrupt causation from each queue */
4165 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4166 		u32 val;
4167 
4168 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4169 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4170 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4171 
4172 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4173 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4174 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4175 
4176 		if (!i40e_enabled_xdp_vsi(vsi))
4177 			continue;
4178 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4179 	}
4180 
4181 	/* disable each interrupt */
4182 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4183 		for (i = vsi->base_vector;
4184 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4185 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4186 
4187 		i40e_flush(hw);
4188 		for (i = 0; i < vsi->num_q_vectors; i++)
4189 			synchronize_irq(pf->msix_entries[i + base].vector);
4190 	} else {
4191 		/* Legacy and MSI mode - this stops all interrupt handling */
4192 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4193 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4194 		i40e_flush(hw);
4195 		synchronize_irq(pf->pdev->irq);
4196 	}
4197 }
4198 
4199 /**
4200  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4201  * @vsi: the VSI being configured
4202  **/
4203 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4204 {
4205 	struct i40e_pf *pf = vsi->back;
4206 	int i;
4207 
4208 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4209 		for (i = 0; i < vsi->num_q_vectors; i++)
4210 			i40e_irq_dynamic_enable(vsi, i);
4211 	} else {
4212 		i40e_irq_dynamic_enable_icr0(pf);
4213 	}
4214 
4215 	i40e_flush(&pf->hw);
4216 	return 0;
4217 }
4218 
4219 /**
4220  * i40e_free_misc_vector - Free the vector that handles non-queue events
4221  * @pf: board private structure
4222  **/
4223 static void i40e_free_misc_vector(struct i40e_pf *pf)
4224 {
4225 	/* Disable ICR 0 */
4226 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4227 	i40e_flush(&pf->hw);
4228 
4229 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4230 		free_irq(pf->msix_entries[0].vector, pf);
4231 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4232 	}
4233 }
4234 
4235 /**
4236  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4237  * @irq: interrupt number
4238  * @data: pointer to a q_vector
4239  *
4240  * This is the handler used for all MSI/Legacy interrupts, and deals
4241  * with both queue and non-queue interrupts.  This is also used in
4242  * MSIX mode to handle the non-queue interrupts.
4243  **/
4244 static irqreturn_t i40e_intr(int irq, void *data)
4245 {
4246 	struct i40e_pf *pf = (struct i40e_pf *)data;
4247 	struct i40e_hw *hw = &pf->hw;
4248 	irqreturn_t ret = IRQ_NONE;
4249 	u32 icr0, icr0_remaining;
4250 	u32 val, ena_mask;
4251 
4252 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4253 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4254 
4255 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4256 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4257 		goto enable_intr;
4258 
4259 	/* if interrupt but no bits showing, must be SWINT */
4260 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4261 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4262 		pf->sw_int_count++;
4263 
4264 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4265 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4266 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4267 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4268 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4269 	}
4270 
4271 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4272 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4273 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4274 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4275 
4276 		/* We do not have a way to disarm Queue causes while leaving
4277 		 * interrupt enabled for all other causes, ideally
4278 		 * interrupt should be disabled while we are in NAPI but
4279 		 * this is not a performance path and napi_schedule()
4280 		 * can deal with rescheduling.
4281 		 */
4282 		if (!test_bit(__I40E_DOWN, pf->state))
4283 			napi_schedule_irqoff(&q_vector->napi);
4284 	}
4285 
4286 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4287 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4288 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4289 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4290 	}
4291 
4292 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4293 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4294 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4295 	}
4296 
4297 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4298 		/* disable any further VFLR event notifications */
4299 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4300 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4301 
4302 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4303 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4304 		} else {
4305 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4306 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4307 		}
4308 	}
4309 
4310 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4311 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4312 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4313 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4314 		val = rd32(hw, I40E_GLGEN_RSTAT);
4315 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4316 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4317 		if (val == I40E_RESET_CORER) {
4318 			pf->corer_count++;
4319 		} else if (val == I40E_RESET_GLOBR) {
4320 			pf->globr_count++;
4321 		} else if (val == I40E_RESET_EMPR) {
4322 			pf->empr_count++;
4323 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4324 		}
4325 	}
4326 
4327 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4328 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4329 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4330 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4331 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4332 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4333 	}
4334 
4335 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4336 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4337 
4338 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4339 			schedule_work(&pf->ptp_extts0_work);
4340 
4341 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4342 			i40e_ptp_tx_hwtstamp(pf);
4343 
4344 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4345 	}
4346 
4347 	/* If a critical error is pending we have no choice but to reset the
4348 	 * device.
4349 	 * Report and mask out any remaining unexpected interrupts.
4350 	 */
4351 	icr0_remaining = icr0 & ena_mask;
4352 	if (icr0_remaining) {
4353 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4354 			 icr0_remaining);
4355 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4356 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4357 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4358 			dev_info(&pf->pdev->dev, "device will be reset\n");
4359 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4360 			i40e_service_event_schedule(pf);
4361 		}
4362 		ena_mask &= ~icr0_remaining;
4363 	}
4364 	ret = IRQ_HANDLED;
4365 
4366 enable_intr:
4367 	/* re-enable interrupt causes */
4368 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4369 	if (!test_bit(__I40E_DOWN, pf->state) ||
4370 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4371 		i40e_service_event_schedule(pf);
4372 		i40e_irq_dynamic_enable_icr0(pf);
4373 	}
4374 
4375 	return ret;
4376 }
4377 
4378 /**
4379  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4380  * @tx_ring:  tx ring to clean
4381  * @budget:   how many cleans we're allowed
4382  *
4383  * Returns true if there's any budget left (e.g. the clean is finished)
4384  **/
4385 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4386 {
4387 	struct i40e_vsi *vsi = tx_ring->vsi;
4388 	u16 i = tx_ring->next_to_clean;
4389 	struct i40e_tx_buffer *tx_buf;
4390 	struct i40e_tx_desc *tx_desc;
4391 
4392 	tx_buf = &tx_ring->tx_bi[i];
4393 	tx_desc = I40E_TX_DESC(tx_ring, i);
4394 	i -= tx_ring->count;
4395 
4396 	do {
4397 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4398 
4399 		/* if next_to_watch is not set then there is no work pending */
4400 		if (!eop_desc)
4401 			break;
4402 
4403 		/* prevent any other reads prior to eop_desc */
4404 		smp_rmb();
4405 
4406 		/* if the descriptor isn't done, no work yet to do */
4407 		if (!(eop_desc->cmd_type_offset_bsz &
4408 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4409 			break;
4410 
4411 		/* clear next_to_watch to prevent false hangs */
4412 		tx_buf->next_to_watch = NULL;
4413 
4414 		tx_desc->buffer_addr = 0;
4415 		tx_desc->cmd_type_offset_bsz = 0;
4416 		/* move past filter desc */
4417 		tx_buf++;
4418 		tx_desc++;
4419 		i++;
4420 		if (unlikely(!i)) {
4421 			i -= tx_ring->count;
4422 			tx_buf = tx_ring->tx_bi;
4423 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4424 		}
4425 		/* unmap skb header data */
4426 		dma_unmap_single(tx_ring->dev,
4427 				 dma_unmap_addr(tx_buf, dma),
4428 				 dma_unmap_len(tx_buf, len),
4429 				 DMA_TO_DEVICE);
4430 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4431 			kfree(tx_buf->raw_buf);
4432 
4433 		tx_buf->raw_buf = NULL;
4434 		tx_buf->tx_flags = 0;
4435 		tx_buf->next_to_watch = NULL;
4436 		dma_unmap_len_set(tx_buf, len, 0);
4437 		tx_desc->buffer_addr = 0;
4438 		tx_desc->cmd_type_offset_bsz = 0;
4439 
4440 		/* move us past the eop_desc for start of next FD desc */
4441 		tx_buf++;
4442 		tx_desc++;
4443 		i++;
4444 		if (unlikely(!i)) {
4445 			i -= tx_ring->count;
4446 			tx_buf = tx_ring->tx_bi;
4447 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4448 		}
4449 
4450 		/* update budget accounting */
4451 		budget--;
4452 	} while (likely(budget));
4453 
4454 	i += tx_ring->count;
4455 	tx_ring->next_to_clean = i;
4456 
4457 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4458 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4459 
4460 	return budget > 0;
4461 }
4462 
4463 /**
4464  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4465  * @irq: interrupt number
4466  * @data: pointer to a q_vector
4467  **/
4468 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4469 {
4470 	struct i40e_q_vector *q_vector = data;
4471 	struct i40e_vsi *vsi;
4472 
4473 	if (!q_vector->tx.ring)
4474 		return IRQ_HANDLED;
4475 
4476 	vsi = q_vector->tx.ring->vsi;
4477 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4478 
4479 	return IRQ_HANDLED;
4480 }
4481 
4482 /**
4483  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4484  * @vsi: the VSI being configured
4485  * @v_idx: vector index
4486  * @qp_idx: queue pair index
4487  **/
4488 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4489 {
4490 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4491 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4492 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4493 
4494 	tx_ring->q_vector = q_vector;
4495 	tx_ring->next = q_vector->tx.ring;
4496 	q_vector->tx.ring = tx_ring;
4497 	q_vector->tx.count++;
4498 
4499 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4500 	if (i40e_enabled_xdp_vsi(vsi)) {
4501 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4502 
4503 		xdp_ring->q_vector = q_vector;
4504 		xdp_ring->next = q_vector->tx.ring;
4505 		q_vector->tx.ring = xdp_ring;
4506 		q_vector->tx.count++;
4507 	}
4508 
4509 	rx_ring->q_vector = q_vector;
4510 	rx_ring->next = q_vector->rx.ring;
4511 	q_vector->rx.ring = rx_ring;
4512 	q_vector->rx.count++;
4513 }
4514 
4515 /**
4516  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4517  * @vsi: the VSI being configured
4518  *
4519  * This function maps descriptor rings to the queue-specific vectors
4520  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4521  * one vector per queue pair, but on a constrained vector budget, we
4522  * group the queue pairs as "efficiently" as possible.
4523  **/
4524 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4525 {
4526 	int qp_remaining = vsi->num_queue_pairs;
4527 	int q_vectors = vsi->num_q_vectors;
4528 	int num_ringpairs;
4529 	int v_start = 0;
4530 	int qp_idx = 0;
4531 
4532 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4533 	 * group them so there are multiple queues per vector.
4534 	 * It is also important to go through all the vectors available to be
4535 	 * sure that if we don't use all the vectors, that the remaining vectors
4536 	 * are cleared. This is especially important when decreasing the
4537 	 * number of queues in use.
4538 	 */
4539 	for (; v_start < q_vectors; v_start++) {
4540 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4541 
4542 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4543 
4544 		q_vector->num_ringpairs = num_ringpairs;
4545 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4546 
4547 		q_vector->rx.count = 0;
4548 		q_vector->tx.count = 0;
4549 		q_vector->rx.ring = NULL;
4550 		q_vector->tx.ring = NULL;
4551 
4552 		while (num_ringpairs--) {
4553 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4554 			qp_idx++;
4555 			qp_remaining--;
4556 		}
4557 	}
4558 }
4559 
4560 /**
4561  * i40e_vsi_request_irq - Request IRQ from the OS
4562  * @vsi: the VSI being configured
4563  * @basename: name for the vector
4564  **/
4565 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4566 {
4567 	struct i40e_pf *pf = vsi->back;
4568 	int err;
4569 
4570 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4571 		err = i40e_vsi_request_irq_msix(vsi, basename);
4572 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4573 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4574 				  pf->int_name, pf);
4575 	else
4576 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4577 				  pf->int_name, pf);
4578 
4579 	if (err)
4580 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4581 
4582 	return err;
4583 }
4584 
4585 #ifdef CONFIG_NET_POLL_CONTROLLER
4586 /**
4587  * i40e_netpoll - A Polling 'interrupt' handler
4588  * @netdev: network interface device structure
4589  *
4590  * This is used by netconsole to send skbs without having to re-enable
4591  * interrupts.  It's not called while the normal interrupt routine is executing.
4592  **/
4593 static void i40e_netpoll(struct net_device *netdev)
4594 {
4595 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4596 	struct i40e_vsi *vsi = np->vsi;
4597 	struct i40e_pf *pf = vsi->back;
4598 	int i;
4599 
4600 	/* if interface is down do nothing */
4601 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4602 		return;
4603 
4604 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4605 		for (i = 0; i < vsi->num_q_vectors; i++)
4606 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4607 	} else {
4608 		i40e_intr(pf->pdev->irq, netdev);
4609 	}
4610 }
4611 #endif
4612 
4613 #define I40E_QTX_ENA_WAIT_COUNT 50
4614 
4615 /**
4616  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4617  * @pf: the PF being configured
4618  * @pf_q: the PF queue
4619  * @enable: enable or disable state of the queue
4620  *
4621  * This routine will wait for the given Tx queue of the PF to reach the
4622  * enabled or disabled state.
4623  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4624  * multiple retries; else will return 0 in case of success.
4625  **/
4626 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4627 {
4628 	int i;
4629 	u32 tx_reg;
4630 
4631 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4632 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4633 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4634 			break;
4635 
4636 		usleep_range(10, 20);
4637 	}
4638 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4639 		return -ETIMEDOUT;
4640 
4641 	return 0;
4642 }
4643 
4644 /**
4645  * i40e_control_tx_q - Start or stop a particular Tx queue
4646  * @pf: the PF structure
4647  * @pf_q: the PF queue to configure
4648  * @enable: start or stop the queue
4649  *
4650  * This function enables or disables a single queue. Note that any delay
4651  * required after the operation is expected to be handled by the caller of
4652  * this function.
4653  **/
4654 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4655 {
4656 	struct i40e_hw *hw = &pf->hw;
4657 	u32 tx_reg;
4658 	int i;
4659 
4660 	/* warn the TX unit of coming changes */
4661 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4662 	if (!enable)
4663 		usleep_range(10, 20);
4664 
4665 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4666 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4667 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4668 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4669 			break;
4670 		usleep_range(1000, 2000);
4671 	}
4672 
4673 	/* Skip if the queue is already in the requested state */
4674 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4675 		return;
4676 
4677 	/* turn on/off the queue */
4678 	if (enable) {
4679 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4680 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4681 	} else {
4682 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4683 	}
4684 
4685 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4686 }
4687 
4688 /**
4689  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4690  * @seid: VSI SEID
4691  * @pf: the PF structure
4692  * @pf_q: the PF queue to configure
4693  * @is_xdp: true if the queue is used for XDP
4694  * @enable: start or stop the queue
4695  **/
4696 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4697 			   bool is_xdp, bool enable)
4698 {
4699 	int ret;
4700 
4701 	i40e_control_tx_q(pf, pf_q, enable);
4702 
4703 	/* wait for the change to finish */
4704 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4705 	if (ret) {
4706 		dev_info(&pf->pdev->dev,
4707 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4708 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4709 			 (enable ? "en" : "dis"));
4710 	}
4711 
4712 	return ret;
4713 }
4714 
4715 /**
4716  * i40e_vsi_enable_tx - Start a VSI's rings
4717  * @vsi: the VSI being configured
4718  **/
4719 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4720 {
4721 	struct i40e_pf *pf = vsi->back;
4722 	int i, pf_q, ret = 0;
4723 
4724 	pf_q = vsi->base_queue;
4725 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4726 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4727 					     pf_q,
4728 					     false /*is xdp*/, true);
4729 		if (ret)
4730 			break;
4731 
4732 		if (!i40e_enabled_xdp_vsi(vsi))
4733 			continue;
4734 
4735 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4736 					     pf_q + vsi->alloc_queue_pairs,
4737 					     true /*is xdp*/, true);
4738 		if (ret)
4739 			break;
4740 	}
4741 	return ret;
4742 }
4743 
4744 /**
4745  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4746  * @pf: the PF being configured
4747  * @pf_q: the PF queue
4748  * @enable: enable or disable state of the queue
4749  *
4750  * This routine will wait for the given Rx queue of the PF to reach the
4751  * enabled or disabled state.
4752  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4753  * multiple retries; else will return 0 in case of success.
4754  **/
4755 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4756 {
4757 	int i;
4758 	u32 rx_reg;
4759 
4760 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4761 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4762 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4763 			break;
4764 
4765 		usleep_range(10, 20);
4766 	}
4767 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4768 		return -ETIMEDOUT;
4769 
4770 	return 0;
4771 }
4772 
4773 /**
4774  * i40e_control_rx_q - Start or stop a particular Rx queue
4775  * @pf: the PF structure
4776  * @pf_q: the PF queue to configure
4777  * @enable: start or stop the queue
4778  *
4779  * This function enables or disables a single queue. Note that
4780  * any delay required after the operation is expected to be
4781  * handled by the caller of this function.
4782  **/
4783 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4784 {
4785 	struct i40e_hw *hw = &pf->hw;
4786 	u32 rx_reg;
4787 	int i;
4788 
4789 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4790 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4791 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4792 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4793 			break;
4794 		usleep_range(1000, 2000);
4795 	}
4796 
4797 	/* Skip if the queue is already in the requested state */
4798 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4799 		return;
4800 
4801 	/* turn on/off the queue */
4802 	if (enable)
4803 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4804 	else
4805 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4806 
4807 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4808 }
4809 
4810 /**
4811  * i40e_control_wait_rx_q
4812  * @pf: the PF structure
4813  * @pf_q: queue being configured
4814  * @enable: start or stop the rings
4815  *
4816  * This function enables or disables a single queue along with waiting
4817  * for the change to finish. The caller of this function should handle
4818  * the delays needed in the case of disabling queues.
4819  **/
4820 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4821 {
4822 	int ret = 0;
4823 
4824 	i40e_control_rx_q(pf, pf_q, enable);
4825 
4826 	/* wait for the change to finish */
4827 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4828 	if (ret)
4829 		return ret;
4830 
4831 	return ret;
4832 }
4833 
4834 /**
4835  * i40e_vsi_enable_rx - Start a VSI's rings
4836  * @vsi: the VSI being configured
4837  **/
4838 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4839 {
4840 	struct i40e_pf *pf = vsi->back;
4841 	int i, pf_q, ret = 0;
4842 
4843 	pf_q = vsi->base_queue;
4844 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4845 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4846 		if (ret) {
4847 			dev_info(&pf->pdev->dev,
4848 				 "VSI seid %d Rx ring %d enable timeout\n",
4849 				 vsi->seid, pf_q);
4850 			break;
4851 		}
4852 	}
4853 
4854 	return ret;
4855 }
4856 
4857 /**
4858  * i40e_vsi_start_rings - Start a VSI's rings
4859  * @vsi: the VSI being configured
4860  **/
4861 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4862 {
4863 	int ret = 0;
4864 
4865 	/* do rx first for enable and last for disable */
4866 	ret = i40e_vsi_enable_rx(vsi);
4867 	if (ret)
4868 		return ret;
4869 	ret = i40e_vsi_enable_tx(vsi);
4870 
4871 	return ret;
4872 }
4873 
4874 #define I40E_DISABLE_TX_GAP_MSEC	50
4875 
4876 /**
4877  * i40e_vsi_stop_rings - Stop a VSI's rings
4878  * @vsi: the VSI being configured
4879  **/
4880 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4881 {
4882 	struct i40e_pf *pf = vsi->back;
4883 	int pf_q, err, q_end;
4884 
4885 	/* When port TX is suspended, don't wait */
4886 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4887 		return i40e_vsi_stop_rings_no_wait(vsi);
4888 
4889 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4890 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4891 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4892 
4893 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4894 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4895 		if (err)
4896 			dev_info(&pf->pdev->dev,
4897 				 "VSI seid %d Rx ring %d disable timeout\n",
4898 				 vsi->seid, pf_q);
4899 	}
4900 
4901 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4902 	pf_q = vsi->base_queue;
4903 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4904 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4905 
4906 	i40e_vsi_wait_queues_disabled(vsi);
4907 }
4908 
4909 /**
4910  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4911  * @vsi: the VSI being shutdown
4912  *
4913  * This function stops all the rings for a VSI but does not delay to verify
4914  * that rings have been disabled. It is expected that the caller is shutting
4915  * down multiple VSIs at once and will delay together for all the VSIs after
4916  * initiating the shutdown. This is particularly useful for shutting down lots
4917  * of VFs together. Otherwise, a large delay can be incurred while configuring
4918  * each VSI in serial.
4919  **/
4920 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4921 {
4922 	struct i40e_pf *pf = vsi->back;
4923 	int i, pf_q;
4924 
4925 	pf_q = vsi->base_queue;
4926 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4927 		i40e_control_tx_q(pf, pf_q, false);
4928 		i40e_control_rx_q(pf, pf_q, false);
4929 	}
4930 }
4931 
4932 /**
4933  * i40e_vsi_free_irq - Free the irq association with the OS
4934  * @vsi: the VSI being configured
4935  **/
4936 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4937 {
4938 	struct i40e_pf *pf = vsi->back;
4939 	struct i40e_hw *hw = &pf->hw;
4940 	int base = vsi->base_vector;
4941 	u32 val, qp;
4942 	int i;
4943 
4944 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4945 		if (!vsi->q_vectors)
4946 			return;
4947 
4948 		if (!vsi->irqs_ready)
4949 			return;
4950 
4951 		vsi->irqs_ready = false;
4952 		for (i = 0; i < vsi->num_q_vectors; i++) {
4953 			int irq_num;
4954 			u16 vector;
4955 
4956 			vector = i + base;
4957 			irq_num = pf->msix_entries[vector].vector;
4958 
4959 			/* free only the irqs that were actually requested */
4960 			if (!vsi->q_vectors[i] ||
4961 			    !vsi->q_vectors[i]->num_ringpairs)
4962 				continue;
4963 
4964 			/* clear the affinity notifier in the IRQ descriptor */
4965 			irq_set_affinity_notifier(irq_num, NULL);
4966 			/* remove our suggested affinity mask for this IRQ */
4967 			irq_update_affinity_hint(irq_num, NULL);
4968 			free_irq(irq_num, vsi->q_vectors[i]);
4969 
4970 			/* Tear down the interrupt queue link list
4971 			 *
4972 			 * We know that they come in pairs and always
4973 			 * the Rx first, then the Tx.  To clear the
4974 			 * link list, stick the EOL value into the
4975 			 * next_q field of the registers.
4976 			 */
4977 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4978 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4979 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4980 			val |= I40E_QUEUE_END_OF_LIST
4981 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4982 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4983 
4984 			while (qp != I40E_QUEUE_END_OF_LIST) {
4985 				u32 next;
4986 
4987 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4988 
4989 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4990 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4991 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4992 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4993 
4994 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4995 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4996 
4997 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4998 
4999 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5000 
5001 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5002 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5003 
5004 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5005 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5006 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5007 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5008 
5009 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5010 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5011 
5012 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5013 				qp = next;
5014 			}
5015 		}
5016 	} else {
5017 		free_irq(pf->pdev->irq, pf);
5018 
5019 		val = rd32(hw, I40E_PFINT_LNKLST0);
5020 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5021 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5022 		val |= I40E_QUEUE_END_OF_LIST
5023 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5024 		wr32(hw, I40E_PFINT_LNKLST0, val);
5025 
5026 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5027 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5028 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5029 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5030 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5031 
5032 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5033 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5034 
5035 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5036 
5037 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5038 
5039 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5040 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5041 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5042 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5043 
5044 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5045 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5046 
5047 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5048 	}
5049 }
5050 
5051 /**
5052  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5053  * @vsi: the VSI being configured
5054  * @v_idx: Index of vector to be freed
5055  *
5056  * This function frees the memory allocated to the q_vector.  In addition if
5057  * NAPI is enabled it will delete any references to the NAPI struct prior
5058  * to freeing the q_vector.
5059  **/
5060 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5061 {
5062 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5063 	struct i40e_ring *ring;
5064 
5065 	if (!q_vector)
5066 		return;
5067 
5068 	/* disassociate q_vector from rings */
5069 	i40e_for_each_ring(ring, q_vector->tx)
5070 		ring->q_vector = NULL;
5071 
5072 	i40e_for_each_ring(ring, q_vector->rx)
5073 		ring->q_vector = NULL;
5074 
5075 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5076 	if (vsi->netdev)
5077 		netif_napi_del(&q_vector->napi);
5078 
5079 	vsi->q_vectors[v_idx] = NULL;
5080 
5081 	kfree_rcu(q_vector, rcu);
5082 }
5083 
5084 /**
5085  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5086  * @vsi: the VSI being un-configured
5087  *
5088  * This frees the memory allocated to the q_vectors and
5089  * deletes references to the NAPI struct.
5090  **/
5091 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5092 {
5093 	int v_idx;
5094 
5095 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5096 		i40e_free_q_vector(vsi, v_idx);
5097 }
5098 
5099 /**
5100  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5101  * @pf: board private structure
5102  **/
5103 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5104 {
5105 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5106 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5107 		pci_disable_msix(pf->pdev);
5108 		kfree(pf->msix_entries);
5109 		pf->msix_entries = NULL;
5110 		kfree(pf->irq_pile);
5111 		pf->irq_pile = NULL;
5112 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5113 		pci_disable_msi(pf->pdev);
5114 	}
5115 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5116 }
5117 
5118 /**
5119  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5120  * @pf: board private structure
5121  *
5122  * We go through and clear interrupt specific resources and reset the structure
5123  * to pre-load conditions
5124  **/
5125 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5126 {
5127 	int i;
5128 
5129 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5130 		i40e_free_misc_vector(pf);
5131 
5132 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5133 		      I40E_IWARP_IRQ_PILE_ID);
5134 
5135 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5136 	for (i = 0; i < pf->num_alloc_vsi; i++)
5137 		if (pf->vsi[i])
5138 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5139 	i40e_reset_interrupt_capability(pf);
5140 }
5141 
5142 /**
5143  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5144  * @vsi: the VSI being configured
5145  **/
5146 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5147 {
5148 	int q_idx;
5149 
5150 	if (!vsi->netdev)
5151 		return;
5152 
5153 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5154 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5155 
5156 		if (q_vector->rx.ring || q_vector->tx.ring)
5157 			napi_enable(&q_vector->napi);
5158 	}
5159 }
5160 
5161 /**
5162  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5163  * @vsi: the VSI being configured
5164  **/
5165 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5166 {
5167 	int q_idx;
5168 
5169 	if (!vsi->netdev)
5170 		return;
5171 
5172 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5173 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5174 
5175 		if (q_vector->rx.ring || q_vector->tx.ring)
5176 			napi_disable(&q_vector->napi);
5177 	}
5178 }
5179 
5180 /**
5181  * i40e_vsi_close - Shut down a VSI
5182  * @vsi: the vsi to be quelled
5183  **/
5184 static void i40e_vsi_close(struct i40e_vsi *vsi)
5185 {
5186 	struct i40e_pf *pf = vsi->back;
5187 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5188 		i40e_down(vsi);
5189 	i40e_vsi_free_irq(vsi);
5190 	i40e_vsi_free_tx_resources(vsi);
5191 	i40e_vsi_free_rx_resources(vsi);
5192 	vsi->current_netdev_flags = 0;
5193 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5194 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5195 		set_bit(__I40E_CLIENT_RESET, pf->state);
5196 }
5197 
5198 /**
5199  * i40e_quiesce_vsi - Pause a given VSI
5200  * @vsi: the VSI being paused
5201  **/
5202 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5203 {
5204 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5205 		return;
5206 
5207 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5208 	if (vsi->netdev && netif_running(vsi->netdev))
5209 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5210 	else
5211 		i40e_vsi_close(vsi);
5212 }
5213 
5214 /**
5215  * i40e_unquiesce_vsi - Resume a given VSI
5216  * @vsi: the VSI being resumed
5217  **/
5218 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5219 {
5220 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5221 		return;
5222 
5223 	if (vsi->netdev && netif_running(vsi->netdev))
5224 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5225 	else
5226 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5227 }
5228 
5229 /**
5230  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5231  * @pf: the PF
5232  **/
5233 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5234 {
5235 	int v;
5236 
5237 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5238 		if (pf->vsi[v])
5239 			i40e_quiesce_vsi(pf->vsi[v]);
5240 	}
5241 }
5242 
5243 /**
5244  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5245  * @pf: the PF
5246  **/
5247 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5248 {
5249 	int v;
5250 
5251 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5252 		if (pf->vsi[v])
5253 			i40e_unquiesce_vsi(pf->vsi[v]);
5254 	}
5255 }
5256 
5257 /**
5258  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5259  * @vsi: the VSI being configured
5260  *
5261  * Wait until all queues on a given VSI have been disabled.
5262  **/
5263 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5264 {
5265 	struct i40e_pf *pf = vsi->back;
5266 	int i, pf_q, ret;
5267 
5268 	pf_q = vsi->base_queue;
5269 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5270 		/* Check and wait for the Tx queue */
5271 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5272 		if (ret) {
5273 			dev_info(&pf->pdev->dev,
5274 				 "VSI seid %d Tx ring %d disable timeout\n",
5275 				 vsi->seid, pf_q);
5276 			return ret;
5277 		}
5278 
5279 		if (!i40e_enabled_xdp_vsi(vsi))
5280 			goto wait_rx;
5281 
5282 		/* Check and wait for the XDP Tx queue */
5283 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5284 				       false);
5285 		if (ret) {
5286 			dev_info(&pf->pdev->dev,
5287 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5288 				 vsi->seid, pf_q);
5289 			return ret;
5290 		}
5291 wait_rx:
5292 		/* Check and wait for the Rx queue */
5293 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5294 		if (ret) {
5295 			dev_info(&pf->pdev->dev,
5296 				 "VSI seid %d Rx ring %d disable timeout\n",
5297 				 vsi->seid, pf_q);
5298 			return ret;
5299 		}
5300 	}
5301 
5302 	return 0;
5303 }
5304 
5305 #ifdef CONFIG_I40E_DCB
5306 /**
5307  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5308  * @pf: the PF
5309  *
5310  * This function waits for the queues to be in disabled state for all the
5311  * VSIs that are managed by this PF.
5312  **/
5313 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5314 {
5315 	int v, ret = 0;
5316 
5317 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5318 		if (pf->vsi[v]) {
5319 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5320 			if (ret)
5321 				break;
5322 		}
5323 	}
5324 
5325 	return ret;
5326 }
5327 
5328 #endif
5329 
5330 /**
5331  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5332  * @pf: pointer to PF
5333  *
5334  * Get TC map for ISCSI PF type that will include iSCSI TC
5335  * and LAN TC.
5336  **/
5337 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5338 {
5339 	struct i40e_dcb_app_priority_table app;
5340 	struct i40e_hw *hw = &pf->hw;
5341 	u8 enabled_tc = 1; /* TC0 is always enabled */
5342 	u8 tc, i;
5343 	/* Get the iSCSI APP TLV */
5344 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5345 
5346 	for (i = 0; i < dcbcfg->numapps; i++) {
5347 		app = dcbcfg->app[i];
5348 		if (app.selector == I40E_APP_SEL_TCPIP &&
5349 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5350 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5351 			enabled_tc |= BIT(tc);
5352 			break;
5353 		}
5354 	}
5355 
5356 	return enabled_tc;
5357 }
5358 
5359 /**
5360  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5361  * @dcbcfg: the corresponding DCBx configuration structure
5362  *
5363  * Return the number of TCs from given DCBx configuration
5364  **/
5365 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5366 {
5367 	int i, tc_unused = 0;
5368 	u8 num_tc = 0;
5369 	u8 ret = 0;
5370 
5371 	/* Scan the ETS Config Priority Table to find
5372 	 * traffic class enabled for a given priority
5373 	 * and create a bitmask of enabled TCs
5374 	 */
5375 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5376 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5377 
5378 	/* Now scan the bitmask to check for
5379 	 * contiguous TCs starting with TC0
5380 	 */
5381 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5382 		if (num_tc & BIT(i)) {
5383 			if (!tc_unused) {
5384 				ret++;
5385 			} else {
5386 				pr_err("Non-contiguous TC - Disabling DCB\n");
5387 				return 1;
5388 			}
5389 		} else {
5390 			tc_unused = 1;
5391 		}
5392 	}
5393 
5394 	/* There is always at least TC0 */
5395 	if (!ret)
5396 		ret = 1;
5397 
5398 	return ret;
5399 }
5400 
5401 /**
5402  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5403  * @dcbcfg: the corresponding DCBx configuration structure
5404  *
5405  * Query the current DCB configuration and return the number of
5406  * traffic classes enabled from the given DCBX config
5407  **/
5408 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5409 {
5410 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5411 	u8 enabled_tc = 1;
5412 	u8 i;
5413 
5414 	for (i = 0; i < num_tc; i++)
5415 		enabled_tc |= BIT(i);
5416 
5417 	return enabled_tc;
5418 }
5419 
5420 /**
5421  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5422  * @pf: PF being queried
5423  *
5424  * Query the current MQPRIO configuration and return the number of
5425  * traffic classes enabled.
5426  **/
5427 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5428 {
5429 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5430 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5431 	u8 enabled_tc = 1, i;
5432 
5433 	for (i = 1; i < num_tc; i++)
5434 		enabled_tc |= BIT(i);
5435 	return enabled_tc;
5436 }
5437 
5438 /**
5439  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5440  * @pf: PF being queried
5441  *
5442  * Return number of traffic classes enabled for the given PF
5443  **/
5444 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5445 {
5446 	struct i40e_hw *hw = &pf->hw;
5447 	u8 i, enabled_tc = 1;
5448 	u8 num_tc = 0;
5449 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5450 
5451 	if (i40e_is_tc_mqprio_enabled(pf))
5452 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5453 
5454 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5455 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5456 		return 1;
5457 
5458 	/* SFP mode will be enabled for all TCs on port */
5459 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5460 		return i40e_dcb_get_num_tc(dcbcfg);
5461 
5462 	/* MFP mode return count of enabled TCs for this PF */
5463 	if (pf->hw.func_caps.iscsi)
5464 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5465 	else
5466 		return 1; /* Only TC0 */
5467 
5468 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5469 		if (enabled_tc & BIT(i))
5470 			num_tc++;
5471 	}
5472 	return num_tc;
5473 }
5474 
5475 /**
5476  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5477  * @pf: PF being queried
5478  *
5479  * Return a bitmap for enabled traffic classes for this PF.
5480  **/
5481 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5482 {
5483 	if (i40e_is_tc_mqprio_enabled(pf))
5484 		return i40e_mqprio_get_enabled_tc(pf);
5485 
5486 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5487 	 * default TC
5488 	 */
5489 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5490 		return I40E_DEFAULT_TRAFFIC_CLASS;
5491 
5492 	/* SFP mode we want PF to be enabled for all TCs */
5493 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5494 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5495 
5496 	/* MFP enabled and iSCSI PF type */
5497 	if (pf->hw.func_caps.iscsi)
5498 		return i40e_get_iscsi_tc_map(pf);
5499 	else
5500 		return I40E_DEFAULT_TRAFFIC_CLASS;
5501 }
5502 
5503 /**
5504  * i40e_vsi_get_bw_info - Query VSI BW Information
5505  * @vsi: the VSI being queried
5506  *
5507  * Returns 0 on success, negative value on failure
5508  **/
5509 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5510 {
5511 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5512 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5513 	struct i40e_pf *pf = vsi->back;
5514 	struct i40e_hw *hw = &pf->hw;
5515 	i40e_status ret;
5516 	u32 tc_bw_max;
5517 	int i;
5518 
5519 	/* Get the VSI level BW configuration */
5520 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5521 	if (ret) {
5522 		dev_info(&pf->pdev->dev,
5523 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5524 			 i40e_stat_str(&pf->hw, ret),
5525 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5526 		return -EINVAL;
5527 	}
5528 
5529 	/* Get the VSI level BW configuration per TC */
5530 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5531 					       NULL);
5532 	if (ret) {
5533 		dev_info(&pf->pdev->dev,
5534 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5535 			 i40e_stat_str(&pf->hw, ret),
5536 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5537 		return -EINVAL;
5538 	}
5539 
5540 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5541 		dev_info(&pf->pdev->dev,
5542 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5543 			 bw_config.tc_valid_bits,
5544 			 bw_ets_config.tc_valid_bits);
5545 		/* Still continuing */
5546 	}
5547 
5548 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5549 	vsi->bw_max_quanta = bw_config.max_bw;
5550 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5551 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5552 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5553 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5554 		vsi->bw_ets_limit_credits[i] =
5555 					le16_to_cpu(bw_ets_config.credits[i]);
5556 		/* 3 bits out of 4 for each TC */
5557 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5558 	}
5559 
5560 	return 0;
5561 }
5562 
5563 /**
5564  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5565  * @vsi: the VSI being configured
5566  * @enabled_tc: TC bitmap
5567  * @bw_share: BW shared credits per TC
5568  *
5569  * Returns 0 on success, negative value on failure
5570  **/
5571 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5572 				       u8 *bw_share)
5573 {
5574 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5575 	struct i40e_pf *pf = vsi->back;
5576 	i40e_status ret;
5577 	int i;
5578 
5579 	/* There is no need to reset BW when mqprio mode is on.  */
5580 	if (i40e_is_tc_mqprio_enabled(pf))
5581 		return 0;
5582 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5583 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5584 		if (ret)
5585 			dev_info(&pf->pdev->dev,
5586 				 "Failed to reset tx rate for vsi->seid %u\n",
5587 				 vsi->seid);
5588 		return ret;
5589 	}
5590 	memset(&bw_data, 0, sizeof(bw_data));
5591 	bw_data.tc_valid_bits = enabled_tc;
5592 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5593 		bw_data.tc_bw_credits[i] = bw_share[i];
5594 
5595 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5596 	if (ret) {
5597 		dev_info(&pf->pdev->dev,
5598 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5599 			 pf->hw.aq.asq_last_status);
5600 		return -EINVAL;
5601 	}
5602 
5603 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5604 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5605 
5606 	return 0;
5607 }
5608 
5609 /**
5610  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5611  * @vsi: the VSI being configured
5612  * @enabled_tc: TC map to be enabled
5613  *
5614  **/
5615 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5616 {
5617 	struct net_device *netdev = vsi->netdev;
5618 	struct i40e_pf *pf = vsi->back;
5619 	struct i40e_hw *hw = &pf->hw;
5620 	u8 netdev_tc = 0;
5621 	int i;
5622 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5623 
5624 	if (!netdev)
5625 		return;
5626 
5627 	if (!enabled_tc) {
5628 		netdev_reset_tc(netdev);
5629 		return;
5630 	}
5631 
5632 	/* Set up actual enabled TCs on the VSI */
5633 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5634 		return;
5635 
5636 	/* set per TC queues for the VSI */
5637 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5638 		/* Only set TC queues for enabled tcs
5639 		 *
5640 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5641 		 * enabled_tc bitmap would be 0x00001001; the driver
5642 		 * will set the numtc for netdev as 2 that will be
5643 		 * referenced by the netdev layer as TC 0 and 1.
5644 		 */
5645 		if (vsi->tc_config.enabled_tc & BIT(i))
5646 			netdev_set_tc_queue(netdev,
5647 					vsi->tc_config.tc_info[i].netdev_tc,
5648 					vsi->tc_config.tc_info[i].qcount,
5649 					vsi->tc_config.tc_info[i].qoffset);
5650 	}
5651 
5652 	if (i40e_is_tc_mqprio_enabled(pf))
5653 		return;
5654 
5655 	/* Assign UP2TC map for the VSI */
5656 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5657 		/* Get the actual TC# for the UP */
5658 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5659 		/* Get the mapped netdev TC# for the UP */
5660 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5661 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5662 	}
5663 }
5664 
5665 /**
5666  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5667  * @vsi: the VSI being configured
5668  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5669  **/
5670 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5671 				      struct i40e_vsi_context *ctxt)
5672 {
5673 	/* copy just the sections touched not the entire info
5674 	 * since not all sections are valid as returned by
5675 	 * update vsi params
5676 	 */
5677 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5678 	memcpy(&vsi->info.queue_mapping,
5679 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5680 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5681 	       sizeof(vsi->info.tc_mapping));
5682 }
5683 
5684 /**
5685  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5686  * @vsi: the VSI being reconfigured
5687  * @vsi_offset: offset from main VF VSI
5688  */
5689 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5690 {
5691 	struct i40e_vsi_context ctxt = {};
5692 	struct i40e_pf *pf;
5693 	struct i40e_hw *hw;
5694 	int ret;
5695 
5696 	if (!vsi)
5697 		return I40E_ERR_PARAM;
5698 	pf = vsi->back;
5699 	hw = &pf->hw;
5700 
5701 	ctxt.seid = vsi->seid;
5702 	ctxt.pf_num = hw->pf_id;
5703 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5704 	ctxt.uplink_seid = vsi->uplink_seid;
5705 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5706 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5707 	ctxt.info = vsi->info;
5708 
5709 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5710 				 false);
5711 	if (vsi->reconfig_rss) {
5712 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5713 				      vsi->num_queue_pairs);
5714 		ret = i40e_vsi_config_rss(vsi);
5715 		if (ret) {
5716 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5717 			return ret;
5718 		}
5719 		vsi->reconfig_rss = false;
5720 	}
5721 
5722 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5723 	if (ret) {
5724 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5725 			 i40e_stat_str(hw, ret),
5726 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5727 		return ret;
5728 	}
5729 	/* update the local VSI info with updated queue map */
5730 	i40e_vsi_update_queue_map(vsi, &ctxt);
5731 	vsi->info.valid_sections = 0;
5732 
5733 	return ret;
5734 }
5735 
5736 /**
5737  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5738  * @vsi: VSI to be configured
5739  * @enabled_tc: TC bitmap
5740  *
5741  * This configures a particular VSI for TCs that are mapped to the
5742  * given TC bitmap. It uses default bandwidth share for TCs across
5743  * VSIs to configure TC for a particular VSI.
5744  *
5745  * NOTE:
5746  * It is expected that the VSI queues have been quisced before calling
5747  * this function.
5748  **/
5749 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5750 {
5751 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5752 	struct i40e_pf *pf = vsi->back;
5753 	struct i40e_hw *hw = &pf->hw;
5754 	struct i40e_vsi_context ctxt;
5755 	int ret = 0;
5756 	int i;
5757 
5758 	/* Check if enabled_tc is same as existing or new TCs */
5759 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5760 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5761 		return ret;
5762 
5763 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5764 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5765 		if (enabled_tc & BIT(i))
5766 			bw_share[i] = 1;
5767 	}
5768 
5769 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5770 	if (ret) {
5771 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5772 
5773 		dev_info(&pf->pdev->dev,
5774 			 "Failed configuring TC map %d for VSI %d\n",
5775 			 enabled_tc, vsi->seid);
5776 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5777 						  &bw_config, NULL);
5778 		if (ret) {
5779 			dev_info(&pf->pdev->dev,
5780 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5781 				 i40e_stat_str(hw, ret),
5782 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5783 			goto out;
5784 		}
5785 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5786 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5787 
5788 			if (!valid_tc)
5789 				valid_tc = bw_config.tc_valid_bits;
5790 			/* Always enable TC0, no matter what */
5791 			valid_tc |= 1;
5792 			dev_info(&pf->pdev->dev,
5793 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5794 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5795 			enabled_tc = valid_tc;
5796 		}
5797 
5798 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5799 		if (ret) {
5800 			dev_err(&pf->pdev->dev,
5801 				"Unable to  configure TC map %d for VSI %d\n",
5802 				enabled_tc, vsi->seid);
5803 			goto out;
5804 		}
5805 	}
5806 
5807 	/* Update Queue Pairs Mapping for currently enabled UPs */
5808 	ctxt.seid = vsi->seid;
5809 	ctxt.pf_num = vsi->back->hw.pf_id;
5810 	ctxt.vf_num = 0;
5811 	ctxt.uplink_seid = vsi->uplink_seid;
5812 	ctxt.info = vsi->info;
5813 	if (i40e_is_tc_mqprio_enabled(pf)) {
5814 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5815 		if (ret)
5816 			goto out;
5817 	} else {
5818 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5819 	}
5820 
5821 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5822 	 * queues changed.
5823 	 */
5824 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5825 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5826 				      vsi->num_queue_pairs);
5827 		ret = i40e_vsi_config_rss(vsi);
5828 		if (ret) {
5829 			dev_info(&vsi->back->pdev->dev,
5830 				 "Failed to reconfig rss for num_queues\n");
5831 			return ret;
5832 		}
5833 		vsi->reconfig_rss = false;
5834 	}
5835 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5836 		ctxt.info.valid_sections |=
5837 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5838 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5839 	}
5840 
5841 	/* Update the VSI after updating the VSI queue-mapping
5842 	 * information
5843 	 */
5844 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5845 	if (ret) {
5846 		dev_info(&pf->pdev->dev,
5847 			 "Update vsi tc config failed, err %s aq_err %s\n",
5848 			 i40e_stat_str(hw, ret),
5849 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5850 		goto out;
5851 	}
5852 	/* update the local VSI info with updated queue map */
5853 	i40e_vsi_update_queue_map(vsi, &ctxt);
5854 	vsi->info.valid_sections = 0;
5855 
5856 	/* Update current VSI BW information */
5857 	ret = i40e_vsi_get_bw_info(vsi);
5858 	if (ret) {
5859 		dev_info(&pf->pdev->dev,
5860 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5861 			 i40e_stat_str(hw, ret),
5862 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5863 		goto out;
5864 	}
5865 
5866 	/* Update the netdev TC setup */
5867 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5868 out:
5869 	return ret;
5870 }
5871 
5872 /**
5873  * i40e_get_link_speed - Returns link speed for the interface
5874  * @vsi: VSI to be configured
5875  *
5876  **/
5877 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5878 {
5879 	struct i40e_pf *pf = vsi->back;
5880 
5881 	switch (pf->hw.phy.link_info.link_speed) {
5882 	case I40E_LINK_SPEED_40GB:
5883 		return 40000;
5884 	case I40E_LINK_SPEED_25GB:
5885 		return 25000;
5886 	case I40E_LINK_SPEED_20GB:
5887 		return 20000;
5888 	case I40E_LINK_SPEED_10GB:
5889 		return 10000;
5890 	case I40E_LINK_SPEED_1GB:
5891 		return 1000;
5892 	default:
5893 		return -EINVAL;
5894 	}
5895 }
5896 
5897 /**
5898  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5899  * @vsi: Pointer to vsi structure
5900  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5901  *
5902  * Helper function to convert units before send to set BW limit
5903  **/
5904 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5905 {
5906 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5907 		dev_warn(&vsi->back->pdev->dev,
5908 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5909 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5910 	} else {
5911 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5912 	}
5913 
5914 	return max_tx_rate;
5915 }
5916 
5917 /**
5918  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5919  * @vsi: VSI to be configured
5920  * @seid: seid of the channel/VSI
5921  * @max_tx_rate: max TX rate to be configured as BW limit
5922  *
5923  * Helper function to set BW limit for a given VSI
5924  **/
5925 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5926 {
5927 	struct i40e_pf *pf = vsi->back;
5928 	u64 credits = 0;
5929 	int speed = 0;
5930 	int ret = 0;
5931 
5932 	speed = i40e_get_link_speed(vsi);
5933 	if (max_tx_rate > speed) {
5934 		dev_err(&pf->pdev->dev,
5935 			"Invalid max tx rate %llu specified for VSI seid %d.",
5936 			max_tx_rate, seid);
5937 		return -EINVAL;
5938 	}
5939 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5940 		dev_warn(&pf->pdev->dev,
5941 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5942 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5943 	}
5944 
5945 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5946 	credits = max_tx_rate;
5947 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5948 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5949 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5950 	if (ret)
5951 		dev_err(&pf->pdev->dev,
5952 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5953 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5954 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5955 	return ret;
5956 }
5957 
5958 /**
5959  * i40e_remove_queue_channels - Remove queue channels for the TCs
5960  * @vsi: VSI to be configured
5961  *
5962  * Remove queue channels for the TCs
5963  **/
5964 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5965 {
5966 	enum i40e_admin_queue_err last_aq_status;
5967 	struct i40e_cloud_filter *cfilter;
5968 	struct i40e_channel *ch, *ch_tmp;
5969 	struct i40e_pf *pf = vsi->back;
5970 	struct hlist_node *node;
5971 	int ret, i;
5972 
5973 	/* Reset rss size that was stored when reconfiguring rss for
5974 	 * channel VSIs with non-power-of-2 queue count.
5975 	 */
5976 	vsi->current_rss_size = 0;
5977 
5978 	/* perform cleanup for channels if they exist */
5979 	if (list_empty(&vsi->ch_list))
5980 		return;
5981 
5982 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5983 		struct i40e_vsi *p_vsi;
5984 
5985 		list_del(&ch->list);
5986 		p_vsi = ch->parent_vsi;
5987 		if (!p_vsi || !ch->initialized) {
5988 			kfree(ch);
5989 			continue;
5990 		}
5991 		/* Reset queue contexts */
5992 		for (i = 0; i < ch->num_queue_pairs; i++) {
5993 			struct i40e_ring *tx_ring, *rx_ring;
5994 			u16 pf_q;
5995 
5996 			pf_q = ch->base_queue + i;
5997 			tx_ring = vsi->tx_rings[pf_q];
5998 			tx_ring->ch = NULL;
5999 
6000 			rx_ring = vsi->rx_rings[pf_q];
6001 			rx_ring->ch = NULL;
6002 		}
6003 
6004 		/* Reset BW configured for this VSI via mqprio */
6005 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6006 		if (ret)
6007 			dev_info(&vsi->back->pdev->dev,
6008 				 "Failed to reset tx rate for ch->seid %u\n",
6009 				 ch->seid);
6010 
6011 		/* delete cloud filters associated with this channel */
6012 		hlist_for_each_entry_safe(cfilter, node,
6013 					  &pf->cloud_filter_list, cloud_node) {
6014 			if (cfilter->seid != ch->seid)
6015 				continue;
6016 
6017 			hash_del(&cfilter->cloud_node);
6018 			if (cfilter->dst_port)
6019 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6020 									cfilter,
6021 									false);
6022 			else
6023 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6024 								false);
6025 			last_aq_status = pf->hw.aq.asq_last_status;
6026 			if (ret)
6027 				dev_info(&pf->pdev->dev,
6028 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6029 					 i40e_stat_str(&pf->hw, ret),
6030 					 i40e_aq_str(&pf->hw, last_aq_status));
6031 			kfree(cfilter);
6032 		}
6033 
6034 		/* delete VSI from FW */
6035 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6036 					     NULL);
6037 		if (ret)
6038 			dev_err(&vsi->back->pdev->dev,
6039 				"unable to remove channel (%d) for parent VSI(%d)\n",
6040 				ch->seid, p_vsi->seid);
6041 		kfree(ch);
6042 	}
6043 	INIT_LIST_HEAD(&vsi->ch_list);
6044 }
6045 
6046 /**
6047  * i40e_get_max_queues_for_channel
6048  * @vsi: ptr to VSI to which channels are associated with
6049  *
6050  * Helper function which returns max value among the queue counts set on the
6051  * channels/TCs created.
6052  **/
6053 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6054 {
6055 	struct i40e_channel *ch, *ch_tmp;
6056 	int max = 0;
6057 
6058 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6059 		if (!ch->initialized)
6060 			continue;
6061 		if (ch->num_queue_pairs > max)
6062 			max = ch->num_queue_pairs;
6063 	}
6064 
6065 	return max;
6066 }
6067 
6068 /**
6069  * i40e_validate_num_queues - validate num_queues w.r.t channel
6070  * @pf: ptr to PF device
6071  * @num_queues: number of queues
6072  * @vsi: the parent VSI
6073  * @reconfig_rss: indicates should the RSS be reconfigured or not
6074  *
6075  * This function validates number of queues in the context of new channel
6076  * which is being established and determines if RSS should be reconfigured
6077  * or not for parent VSI.
6078  **/
6079 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6080 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6081 {
6082 	int max_ch_queues;
6083 
6084 	if (!reconfig_rss)
6085 		return -EINVAL;
6086 
6087 	*reconfig_rss = false;
6088 	if (vsi->current_rss_size) {
6089 		if (num_queues > vsi->current_rss_size) {
6090 			dev_dbg(&pf->pdev->dev,
6091 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6092 				num_queues, vsi->current_rss_size);
6093 			return -EINVAL;
6094 		} else if ((num_queues < vsi->current_rss_size) &&
6095 			   (!is_power_of_2(num_queues))) {
6096 			dev_dbg(&pf->pdev->dev,
6097 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6098 				num_queues, vsi->current_rss_size);
6099 			return -EINVAL;
6100 		}
6101 	}
6102 
6103 	if (!is_power_of_2(num_queues)) {
6104 		/* Find the max num_queues configured for channel if channel
6105 		 * exist.
6106 		 * if channel exist, then enforce 'num_queues' to be more than
6107 		 * max ever queues configured for channel.
6108 		 */
6109 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6110 		if (num_queues < max_ch_queues) {
6111 			dev_dbg(&pf->pdev->dev,
6112 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6113 				num_queues, max_ch_queues);
6114 			return -EINVAL;
6115 		}
6116 		*reconfig_rss = true;
6117 	}
6118 
6119 	return 0;
6120 }
6121 
6122 /**
6123  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6124  * @vsi: the VSI being setup
6125  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6126  *
6127  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6128  **/
6129 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6130 {
6131 	struct i40e_pf *pf = vsi->back;
6132 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6133 	struct i40e_hw *hw = &pf->hw;
6134 	int local_rss_size;
6135 	u8 *lut;
6136 	int ret;
6137 
6138 	if (!vsi->rss_size)
6139 		return -EINVAL;
6140 
6141 	if (rss_size > vsi->rss_size)
6142 		return -EINVAL;
6143 
6144 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6145 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6146 	if (!lut)
6147 		return -ENOMEM;
6148 
6149 	/* Ignoring user configured lut if there is one */
6150 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6151 
6152 	/* Use user configured hash key if there is one, otherwise
6153 	 * use default.
6154 	 */
6155 	if (vsi->rss_hkey_user)
6156 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6157 	else
6158 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6159 
6160 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6161 	if (ret) {
6162 		dev_info(&pf->pdev->dev,
6163 			 "Cannot set RSS lut, err %s aq_err %s\n",
6164 			 i40e_stat_str(hw, ret),
6165 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6166 		kfree(lut);
6167 		return ret;
6168 	}
6169 	kfree(lut);
6170 
6171 	/* Do the update w.r.t. storing rss_size */
6172 	if (!vsi->orig_rss_size)
6173 		vsi->orig_rss_size = vsi->rss_size;
6174 	vsi->current_rss_size = local_rss_size;
6175 
6176 	return ret;
6177 }
6178 
6179 /**
6180  * i40e_channel_setup_queue_map - Setup a channel queue map
6181  * @pf: ptr to PF device
6182  * @ctxt: VSI context structure
6183  * @ch: ptr to channel structure
6184  *
6185  * Setup queue map for a specific channel
6186  **/
6187 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6188 					 struct i40e_vsi_context *ctxt,
6189 					 struct i40e_channel *ch)
6190 {
6191 	u16 qcount, qmap, sections = 0;
6192 	u8 offset = 0;
6193 	int pow;
6194 
6195 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6196 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6197 
6198 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6199 	ch->num_queue_pairs = qcount;
6200 
6201 	/* find the next higher power-of-2 of num queue pairs */
6202 	pow = ilog2(qcount);
6203 	if (!is_power_of_2(qcount))
6204 		pow++;
6205 
6206 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6207 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6208 
6209 	/* Setup queue TC[0].qmap for given VSI context */
6210 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6211 
6212 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6213 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6214 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6215 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6216 }
6217 
6218 /**
6219  * i40e_add_channel - add a channel by adding VSI
6220  * @pf: ptr to PF device
6221  * @uplink_seid: underlying HW switching element (VEB) ID
6222  * @ch: ptr to channel structure
6223  *
6224  * Add a channel (VSI) using add_vsi and queue_map
6225  **/
6226 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6227 			    struct i40e_channel *ch)
6228 {
6229 	struct i40e_hw *hw = &pf->hw;
6230 	struct i40e_vsi_context ctxt;
6231 	u8 enabled_tc = 0x1; /* TC0 enabled */
6232 	int ret;
6233 
6234 	if (ch->type != I40E_VSI_VMDQ2) {
6235 		dev_info(&pf->pdev->dev,
6236 			 "add new vsi failed, ch->type %d\n", ch->type);
6237 		return -EINVAL;
6238 	}
6239 
6240 	memset(&ctxt, 0, sizeof(ctxt));
6241 	ctxt.pf_num = hw->pf_id;
6242 	ctxt.vf_num = 0;
6243 	ctxt.uplink_seid = uplink_seid;
6244 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6245 	if (ch->type == I40E_VSI_VMDQ2)
6246 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6247 
6248 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6249 		ctxt.info.valid_sections |=
6250 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6251 		ctxt.info.switch_id =
6252 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6253 	}
6254 
6255 	/* Set queue map for a given VSI context */
6256 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6257 
6258 	/* Now time to create VSI */
6259 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6260 	if (ret) {
6261 		dev_info(&pf->pdev->dev,
6262 			 "add new vsi failed, err %s aq_err %s\n",
6263 			 i40e_stat_str(&pf->hw, ret),
6264 			 i40e_aq_str(&pf->hw,
6265 				     pf->hw.aq.asq_last_status));
6266 		return -ENOENT;
6267 	}
6268 
6269 	/* Success, update channel, set enabled_tc only if the channel
6270 	 * is not a macvlan
6271 	 */
6272 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6273 	ch->seid = ctxt.seid;
6274 	ch->vsi_number = ctxt.vsi_number;
6275 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6276 
6277 	/* copy just the sections touched not the entire info
6278 	 * since not all sections are valid as returned by
6279 	 * update vsi params
6280 	 */
6281 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6282 	memcpy(&ch->info.queue_mapping,
6283 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6284 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6285 	       sizeof(ctxt.info.tc_mapping));
6286 
6287 	return 0;
6288 }
6289 
6290 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6291 				  u8 *bw_share)
6292 {
6293 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6294 	i40e_status ret;
6295 	int i;
6296 
6297 	memset(&bw_data, 0, sizeof(bw_data));
6298 	bw_data.tc_valid_bits = ch->enabled_tc;
6299 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6300 		bw_data.tc_bw_credits[i] = bw_share[i];
6301 
6302 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6303 				       &bw_data, NULL);
6304 	if (ret) {
6305 		dev_info(&vsi->back->pdev->dev,
6306 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6307 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6308 		return -EINVAL;
6309 	}
6310 
6311 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6312 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6313 
6314 	return 0;
6315 }
6316 
6317 /**
6318  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6319  * @pf: ptr to PF device
6320  * @vsi: the VSI being setup
6321  * @ch: ptr to channel structure
6322  *
6323  * Configure TX rings associated with channel (VSI) since queues are being
6324  * from parent VSI.
6325  **/
6326 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6327 				       struct i40e_vsi *vsi,
6328 				       struct i40e_channel *ch)
6329 {
6330 	i40e_status ret;
6331 	int i;
6332 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6333 
6334 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6335 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6336 		if (ch->enabled_tc & BIT(i))
6337 			bw_share[i] = 1;
6338 	}
6339 
6340 	/* configure BW for new VSI */
6341 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6342 	if (ret) {
6343 		dev_info(&vsi->back->pdev->dev,
6344 			 "Failed configuring TC map %d for channel (seid %u)\n",
6345 			 ch->enabled_tc, ch->seid);
6346 		return ret;
6347 	}
6348 
6349 	for (i = 0; i < ch->num_queue_pairs; i++) {
6350 		struct i40e_ring *tx_ring, *rx_ring;
6351 		u16 pf_q;
6352 
6353 		pf_q = ch->base_queue + i;
6354 
6355 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6356 		 * context
6357 		 */
6358 		tx_ring = vsi->tx_rings[pf_q];
6359 		tx_ring->ch = ch;
6360 
6361 		/* Get the RX ring ptr */
6362 		rx_ring = vsi->rx_rings[pf_q];
6363 		rx_ring->ch = ch;
6364 	}
6365 
6366 	return 0;
6367 }
6368 
6369 /**
6370  * i40e_setup_hw_channel - setup new channel
6371  * @pf: ptr to PF device
6372  * @vsi: the VSI being setup
6373  * @ch: ptr to channel structure
6374  * @uplink_seid: underlying HW switching element (VEB) ID
6375  * @type: type of channel to be created (VMDq2/VF)
6376  *
6377  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6378  * and configures TX rings accordingly
6379  **/
6380 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6381 					struct i40e_vsi *vsi,
6382 					struct i40e_channel *ch,
6383 					u16 uplink_seid, u8 type)
6384 {
6385 	int ret;
6386 
6387 	ch->initialized = false;
6388 	ch->base_queue = vsi->next_base_queue;
6389 	ch->type = type;
6390 
6391 	/* Proceed with creation of channel (VMDq2) VSI */
6392 	ret = i40e_add_channel(pf, uplink_seid, ch);
6393 	if (ret) {
6394 		dev_info(&pf->pdev->dev,
6395 			 "failed to add_channel using uplink_seid %u\n",
6396 			 uplink_seid);
6397 		return ret;
6398 	}
6399 
6400 	/* Mark the successful creation of channel */
6401 	ch->initialized = true;
6402 
6403 	/* Reconfigure TX queues using QTX_CTL register */
6404 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6405 	if (ret) {
6406 		dev_info(&pf->pdev->dev,
6407 			 "failed to configure TX rings for channel %u\n",
6408 			 ch->seid);
6409 		return ret;
6410 	}
6411 
6412 	/* update 'next_base_queue' */
6413 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6414 	dev_dbg(&pf->pdev->dev,
6415 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6416 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6417 		ch->num_queue_pairs,
6418 		vsi->next_base_queue);
6419 	return ret;
6420 }
6421 
6422 /**
6423  * i40e_setup_channel - setup new channel using uplink element
6424  * @pf: ptr to PF device
6425  * @vsi: pointer to the VSI to set up the channel within
6426  * @ch: ptr to channel structure
6427  *
6428  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6429  * and uplink switching element (uplink_seid)
6430  **/
6431 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6432 			       struct i40e_channel *ch)
6433 {
6434 	u8 vsi_type;
6435 	u16 seid;
6436 	int ret;
6437 
6438 	if (vsi->type == I40E_VSI_MAIN) {
6439 		vsi_type = I40E_VSI_VMDQ2;
6440 	} else {
6441 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6442 			vsi->type);
6443 		return false;
6444 	}
6445 
6446 	/* underlying switching element */
6447 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6448 
6449 	/* create channel (VSI), configure TX rings */
6450 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6451 	if (ret) {
6452 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6453 		return false;
6454 	}
6455 
6456 	return ch->initialized ? true : false;
6457 }
6458 
6459 /**
6460  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6461  * @vsi: ptr to VSI which has PF backing
6462  *
6463  * Sets up switch mode correctly if it needs to be changed and perform
6464  * what are allowed modes.
6465  **/
6466 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6467 {
6468 	u8 mode;
6469 	struct i40e_pf *pf = vsi->back;
6470 	struct i40e_hw *hw = &pf->hw;
6471 	int ret;
6472 
6473 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6474 	if (ret)
6475 		return -EINVAL;
6476 
6477 	if (hw->dev_caps.switch_mode) {
6478 		/* if switch mode is set, support mode2 (non-tunneled for
6479 		 * cloud filter) for now
6480 		 */
6481 		u32 switch_mode = hw->dev_caps.switch_mode &
6482 				  I40E_SWITCH_MODE_MASK;
6483 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6484 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6485 				return 0;
6486 			dev_err(&pf->pdev->dev,
6487 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6488 				hw->dev_caps.switch_mode);
6489 			return -EINVAL;
6490 		}
6491 	}
6492 
6493 	/* Set Bit 7 to be valid */
6494 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6495 
6496 	/* Set L4type for TCP support */
6497 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6498 
6499 	/* Set cloud filter mode */
6500 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6501 
6502 	/* Prep mode field for set_switch_config */
6503 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6504 					pf->last_sw_conf_valid_flags,
6505 					mode, NULL);
6506 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6507 		dev_err(&pf->pdev->dev,
6508 			"couldn't set switch config bits, err %s aq_err %s\n",
6509 			i40e_stat_str(hw, ret),
6510 			i40e_aq_str(hw,
6511 				    hw->aq.asq_last_status));
6512 
6513 	return ret;
6514 }
6515 
6516 /**
6517  * i40e_create_queue_channel - function to create channel
6518  * @vsi: VSI to be configured
6519  * @ch: ptr to channel (it contains channel specific params)
6520  *
6521  * This function creates channel (VSI) using num_queues specified by user,
6522  * reconfigs RSS if needed.
6523  **/
6524 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6525 			      struct i40e_channel *ch)
6526 {
6527 	struct i40e_pf *pf = vsi->back;
6528 	bool reconfig_rss;
6529 	int err;
6530 
6531 	if (!ch)
6532 		return -EINVAL;
6533 
6534 	if (!ch->num_queue_pairs) {
6535 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6536 			ch->num_queue_pairs);
6537 		return -EINVAL;
6538 	}
6539 
6540 	/* validate user requested num_queues for channel */
6541 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6542 				       &reconfig_rss);
6543 	if (err) {
6544 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6545 			 ch->num_queue_pairs);
6546 		return -EINVAL;
6547 	}
6548 
6549 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6550 	 * VSI to be added switch to VEB mode.
6551 	 */
6552 
6553 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6554 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6555 
6556 		if (vsi->type == I40E_VSI_MAIN) {
6557 			if (i40e_is_tc_mqprio_enabled(pf))
6558 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6559 			else
6560 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6561 		}
6562 		/* now onwards for main VSI, number of queues will be value
6563 		 * of TC0's queue count
6564 		 */
6565 	}
6566 
6567 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6568 	 * it should be more than num_queues
6569 	 */
6570 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6571 		dev_dbg(&pf->pdev->dev,
6572 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6573 			vsi->cnt_q_avail, ch->num_queue_pairs);
6574 		return -EINVAL;
6575 	}
6576 
6577 	/* reconfig_rss only if vsi type is MAIN_VSI */
6578 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6579 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6580 		if (err) {
6581 			dev_info(&pf->pdev->dev,
6582 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6583 				 ch->num_queue_pairs);
6584 			return -EINVAL;
6585 		}
6586 	}
6587 
6588 	if (!i40e_setup_channel(pf, vsi, ch)) {
6589 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6590 		return -EINVAL;
6591 	}
6592 
6593 	dev_info(&pf->pdev->dev,
6594 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6595 		 ch->seid, ch->num_queue_pairs);
6596 
6597 	/* configure VSI for BW limit */
6598 	if (ch->max_tx_rate) {
6599 		u64 credits = ch->max_tx_rate;
6600 
6601 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6602 			return -EINVAL;
6603 
6604 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6605 		dev_dbg(&pf->pdev->dev,
6606 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6607 			ch->max_tx_rate,
6608 			credits,
6609 			ch->seid);
6610 	}
6611 
6612 	/* in case of VF, this will be main SRIOV VSI */
6613 	ch->parent_vsi = vsi;
6614 
6615 	/* and update main_vsi's count for queue_available to use */
6616 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6617 
6618 	return 0;
6619 }
6620 
6621 /**
6622  * i40e_configure_queue_channels - Add queue channel for the given TCs
6623  * @vsi: VSI to be configured
6624  *
6625  * Configures queue channel mapping to the given TCs
6626  **/
6627 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6628 {
6629 	struct i40e_channel *ch;
6630 	u64 max_rate = 0;
6631 	int ret = 0, i;
6632 
6633 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6634 	vsi->tc_seid_map[0] = vsi->seid;
6635 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6636 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6637 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6638 			if (!ch) {
6639 				ret = -ENOMEM;
6640 				goto err_free;
6641 			}
6642 
6643 			INIT_LIST_HEAD(&ch->list);
6644 			ch->num_queue_pairs =
6645 				vsi->tc_config.tc_info[i].qcount;
6646 			ch->base_queue =
6647 				vsi->tc_config.tc_info[i].qoffset;
6648 
6649 			/* Bandwidth limit through tc interface is in bytes/s,
6650 			 * change to Mbit/s
6651 			 */
6652 			max_rate = vsi->mqprio_qopt.max_rate[i];
6653 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6654 			ch->max_tx_rate = max_rate;
6655 
6656 			list_add_tail(&ch->list, &vsi->ch_list);
6657 
6658 			ret = i40e_create_queue_channel(vsi, ch);
6659 			if (ret) {
6660 				dev_err(&vsi->back->pdev->dev,
6661 					"Failed creating queue channel with TC%d: queues %d\n",
6662 					i, ch->num_queue_pairs);
6663 				goto err_free;
6664 			}
6665 			vsi->tc_seid_map[i] = ch->seid;
6666 		}
6667 	}
6668 
6669 	/* reset to reconfigure TX queue contexts */
6670 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6671 	return ret;
6672 
6673 err_free:
6674 	i40e_remove_queue_channels(vsi);
6675 	return ret;
6676 }
6677 
6678 /**
6679  * i40e_veb_config_tc - Configure TCs for given VEB
6680  * @veb: given VEB
6681  * @enabled_tc: TC bitmap
6682  *
6683  * Configures given TC bitmap for VEB (switching) element
6684  **/
6685 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6686 {
6687 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6688 	struct i40e_pf *pf = veb->pf;
6689 	int ret = 0;
6690 	int i;
6691 
6692 	/* No TCs or already enabled TCs just return */
6693 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6694 		return ret;
6695 
6696 	bw_data.tc_valid_bits = enabled_tc;
6697 	/* bw_data.absolute_credits is not set (relative) */
6698 
6699 	/* Enable ETS TCs with equal BW Share for now */
6700 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6701 		if (enabled_tc & BIT(i))
6702 			bw_data.tc_bw_share_credits[i] = 1;
6703 	}
6704 
6705 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6706 						   &bw_data, NULL);
6707 	if (ret) {
6708 		dev_info(&pf->pdev->dev,
6709 			 "VEB bw config failed, err %s aq_err %s\n",
6710 			 i40e_stat_str(&pf->hw, ret),
6711 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6712 		goto out;
6713 	}
6714 
6715 	/* Update the BW information */
6716 	ret = i40e_veb_get_bw_info(veb);
6717 	if (ret) {
6718 		dev_info(&pf->pdev->dev,
6719 			 "Failed getting veb bw config, err %s aq_err %s\n",
6720 			 i40e_stat_str(&pf->hw, ret),
6721 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6722 	}
6723 
6724 out:
6725 	return ret;
6726 }
6727 
6728 #ifdef CONFIG_I40E_DCB
6729 /**
6730  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6731  * @pf: PF struct
6732  *
6733  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6734  * the caller would've quiesce all the VSIs before calling
6735  * this function
6736  **/
6737 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6738 {
6739 	u8 tc_map = 0;
6740 	int ret;
6741 	u8 v;
6742 
6743 	/* Enable the TCs available on PF to all VEBs */
6744 	tc_map = i40e_pf_get_tc_map(pf);
6745 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6746 		return;
6747 
6748 	for (v = 0; v < I40E_MAX_VEB; v++) {
6749 		if (!pf->veb[v])
6750 			continue;
6751 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6752 		if (ret) {
6753 			dev_info(&pf->pdev->dev,
6754 				 "Failed configuring TC for VEB seid=%d\n",
6755 				 pf->veb[v]->seid);
6756 			/* Will try to configure as many components */
6757 		}
6758 	}
6759 
6760 	/* Update each VSI */
6761 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6762 		if (!pf->vsi[v])
6763 			continue;
6764 
6765 		/* - Enable all TCs for the LAN VSI
6766 		 * - For all others keep them at TC0 for now
6767 		 */
6768 		if (v == pf->lan_vsi)
6769 			tc_map = i40e_pf_get_tc_map(pf);
6770 		else
6771 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6772 
6773 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6774 		if (ret) {
6775 			dev_info(&pf->pdev->dev,
6776 				 "Failed configuring TC for VSI seid=%d\n",
6777 				 pf->vsi[v]->seid);
6778 			/* Will try to configure as many components */
6779 		} else {
6780 			/* Re-configure VSI vectors based on updated TC map */
6781 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6782 			if (pf->vsi[v]->netdev)
6783 				i40e_dcbnl_set_all(pf->vsi[v]);
6784 		}
6785 	}
6786 }
6787 
6788 /**
6789  * i40e_resume_port_tx - Resume port Tx
6790  * @pf: PF struct
6791  *
6792  * Resume a port's Tx and issue a PF reset in case of failure to
6793  * resume.
6794  **/
6795 static int i40e_resume_port_tx(struct i40e_pf *pf)
6796 {
6797 	struct i40e_hw *hw = &pf->hw;
6798 	int ret;
6799 
6800 	ret = i40e_aq_resume_port_tx(hw, NULL);
6801 	if (ret) {
6802 		dev_info(&pf->pdev->dev,
6803 			 "Resume Port Tx failed, err %s aq_err %s\n",
6804 			  i40e_stat_str(&pf->hw, ret),
6805 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6806 		/* Schedule PF reset to recover */
6807 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6808 		i40e_service_event_schedule(pf);
6809 	}
6810 
6811 	return ret;
6812 }
6813 
6814 /**
6815  * i40e_suspend_port_tx - Suspend port Tx
6816  * @pf: PF struct
6817  *
6818  * Suspend a port's Tx and issue a PF reset in case of failure.
6819  **/
6820 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6821 {
6822 	struct i40e_hw *hw = &pf->hw;
6823 	int ret;
6824 
6825 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6826 	if (ret) {
6827 		dev_info(&pf->pdev->dev,
6828 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6829 			 i40e_stat_str(&pf->hw, ret),
6830 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6831 		/* Schedule PF reset to recover */
6832 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6833 		i40e_service_event_schedule(pf);
6834 	}
6835 
6836 	return ret;
6837 }
6838 
6839 /**
6840  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6841  * @pf: PF being configured
6842  * @new_cfg: New DCBX configuration
6843  *
6844  * Program DCB settings into HW and reconfigure VEB/VSIs on
6845  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6846  **/
6847 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6848 				  struct i40e_dcbx_config *new_cfg)
6849 {
6850 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6851 	int ret;
6852 
6853 	/* Check if need reconfiguration */
6854 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6855 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6856 		return 0;
6857 	}
6858 
6859 	/* Config change disable all VSIs */
6860 	i40e_pf_quiesce_all_vsi(pf);
6861 
6862 	/* Copy the new config to the current config */
6863 	*old_cfg = *new_cfg;
6864 	old_cfg->etsrec = old_cfg->etscfg;
6865 	ret = i40e_set_dcb_config(&pf->hw);
6866 	if (ret) {
6867 		dev_info(&pf->pdev->dev,
6868 			 "Set DCB Config failed, err %s aq_err %s\n",
6869 			 i40e_stat_str(&pf->hw, ret),
6870 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6871 		goto out;
6872 	}
6873 
6874 	/* Changes in configuration update VEB/VSI */
6875 	i40e_dcb_reconfigure(pf);
6876 out:
6877 	/* In case of reset do not try to resume anything */
6878 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6879 		/* Re-start the VSIs if disabled */
6880 		ret = i40e_resume_port_tx(pf);
6881 		/* In case of error no point in resuming VSIs */
6882 		if (ret)
6883 			goto err;
6884 		i40e_pf_unquiesce_all_vsi(pf);
6885 	}
6886 err:
6887 	return ret;
6888 }
6889 
6890 /**
6891  * i40e_hw_dcb_config - Program new DCBX settings into HW
6892  * @pf: PF being configured
6893  * @new_cfg: New DCBX configuration
6894  *
6895  * Program DCB settings into HW and reconfigure VEB/VSIs on
6896  * given PF
6897  **/
6898 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6899 {
6900 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6901 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6902 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6903 	struct i40e_dcbx_config *old_cfg;
6904 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6905 	struct i40e_rx_pb_config pb_cfg;
6906 	struct i40e_hw *hw = &pf->hw;
6907 	u8 num_ports = hw->num_ports;
6908 	bool need_reconfig;
6909 	int ret = -EINVAL;
6910 	u8 lltc_map = 0;
6911 	u8 tc_map = 0;
6912 	u8 new_numtc;
6913 	u8 i;
6914 
6915 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6916 	/* Un-pack information to Program ETS HW via shared API
6917 	 * numtc, tcmap
6918 	 * LLTC map
6919 	 * ETS/NON-ETS arbiter mode
6920 	 * max exponent (credit refills)
6921 	 * Total number of ports
6922 	 * PFC priority bit-map
6923 	 * Priority Table
6924 	 * BW % per TC
6925 	 * Arbiter mode between UPs sharing same TC
6926 	 * TSA table (ETS or non-ETS)
6927 	 * EEE enabled or not
6928 	 * MFS TC table
6929 	 */
6930 
6931 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6932 
6933 	memset(&ets_data, 0, sizeof(ets_data));
6934 	for (i = 0; i < new_numtc; i++) {
6935 		tc_map |= BIT(i);
6936 		switch (new_cfg->etscfg.tsatable[i]) {
6937 		case I40E_IEEE_TSA_ETS:
6938 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6939 			ets_data.tc_bw_share_credits[i] =
6940 					new_cfg->etscfg.tcbwtable[i];
6941 			break;
6942 		case I40E_IEEE_TSA_STRICT:
6943 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6944 			lltc_map |= BIT(i);
6945 			ets_data.tc_bw_share_credits[i] =
6946 					I40E_DCB_STRICT_PRIO_CREDITS;
6947 			break;
6948 		default:
6949 			/* Invalid TSA type */
6950 			need_reconfig = false;
6951 			goto out;
6952 		}
6953 	}
6954 
6955 	old_cfg = &hw->local_dcbx_config;
6956 	/* Check if need reconfiguration */
6957 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6958 
6959 	/* If needed, enable/disable frame tagging, disable all VSIs
6960 	 * and suspend port tx
6961 	 */
6962 	if (need_reconfig) {
6963 		/* Enable DCB tagging only when more than one TC */
6964 		if (new_numtc > 1)
6965 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6966 		else
6967 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6968 
6969 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6970 		/* Reconfiguration needed quiesce all VSIs */
6971 		i40e_pf_quiesce_all_vsi(pf);
6972 		ret = i40e_suspend_port_tx(pf);
6973 		if (ret)
6974 			goto err;
6975 	}
6976 
6977 	/* Configure Port ETS Tx Scheduler */
6978 	ets_data.tc_valid_bits = tc_map;
6979 	ets_data.tc_strict_priority_flags = lltc_map;
6980 	ret = i40e_aq_config_switch_comp_ets
6981 		(hw, pf->mac_seid, &ets_data,
6982 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6983 	if (ret) {
6984 		dev_info(&pf->pdev->dev,
6985 			 "Modify Port ETS failed, err %s aq_err %s\n",
6986 			 i40e_stat_str(&pf->hw, ret),
6987 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6988 		goto out;
6989 	}
6990 
6991 	/* Configure Rx ETS HW */
6992 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6993 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6994 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6995 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6996 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6997 				   lltc_map);
6998 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6999 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7000 				     prio_type);
7001 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7002 			       new_cfg->etscfg.prioritytable);
7003 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7004 
7005 	/* Configure Rx Packet Buffers in HW */
7006 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7007 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7008 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7009 	}
7010 
7011 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7012 					 false, new_cfg->pfc.pfcenable,
7013 					 mfs_tc, &pb_cfg);
7014 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7015 
7016 	/* Update the local Rx Packet buffer config */
7017 	pf->pb_cfg = pb_cfg;
7018 
7019 	/* Inform the FW about changes to DCB configuration */
7020 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7021 	if (ret) {
7022 		dev_info(&pf->pdev->dev,
7023 			 "DCB Updated failed, err %s aq_err %s\n",
7024 			 i40e_stat_str(&pf->hw, ret),
7025 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7026 		goto out;
7027 	}
7028 
7029 	/* Update the port DCBx configuration */
7030 	*old_cfg = *new_cfg;
7031 
7032 	/* Changes in configuration update VEB/VSI */
7033 	i40e_dcb_reconfigure(pf);
7034 out:
7035 	/* Re-start the VSIs if disabled */
7036 	if (need_reconfig) {
7037 		ret = i40e_resume_port_tx(pf);
7038 
7039 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7040 		/* In case of error no point in resuming VSIs */
7041 		if (ret)
7042 			goto err;
7043 
7044 		/* Wait for the PF's queues to be disabled */
7045 		ret = i40e_pf_wait_queues_disabled(pf);
7046 		if (ret) {
7047 			/* Schedule PF reset to recover */
7048 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7049 			i40e_service_event_schedule(pf);
7050 			goto err;
7051 		} else {
7052 			i40e_pf_unquiesce_all_vsi(pf);
7053 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7054 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7055 		}
7056 		/* registers are set, lets apply */
7057 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7058 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7059 	}
7060 
7061 err:
7062 	return ret;
7063 }
7064 
7065 /**
7066  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7067  * @pf: PF being queried
7068  *
7069  * Set default DCB configuration in case DCB is to be done in SW.
7070  **/
7071 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7072 {
7073 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7074 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7075 	struct i40e_hw *hw = &pf->hw;
7076 	int err;
7077 
7078 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7079 		/* Update the local cached instance with TC0 ETS */
7080 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7081 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7082 		pf->tmp_cfg.etscfg.maxtcs = 0;
7083 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7084 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7085 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7086 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7087 		/* FW needs one App to configure HW */
7088 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7089 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7090 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7091 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7092 
7093 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7094 	}
7095 
7096 	memset(&ets_data, 0, sizeof(ets_data));
7097 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7098 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7099 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7100 
7101 	/* Enable ETS on the Physical port */
7102 	err = i40e_aq_config_switch_comp_ets
7103 		(hw, pf->mac_seid, &ets_data,
7104 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7105 	if (err) {
7106 		dev_info(&pf->pdev->dev,
7107 			 "Enable Port ETS failed, err %s aq_err %s\n",
7108 			 i40e_stat_str(&pf->hw, err),
7109 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7110 		err = -ENOENT;
7111 		goto out;
7112 	}
7113 
7114 	/* Update the local cached instance with TC0 ETS */
7115 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7116 	dcb_cfg->etscfg.cbs = 0;
7117 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7118 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7119 
7120 out:
7121 	return err;
7122 }
7123 
7124 /**
7125  * i40e_init_pf_dcb - Initialize DCB configuration
7126  * @pf: PF being configured
7127  *
7128  * Query the current DCB configuration and cache it
7129  * in the hardware structure
7130  **/
7131 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7132 {
7133 	struct i40e_hw *hw = &pf->hw;
7134 	int err;
7135 
7136 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7137 	 * Also do not enable DCBx if FW LLDP agent is disabled
7138 	 */
7139 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7140 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7141 		err = I40E_NOT_SUPPORTED;
7142 		goto out;
7143 	}
7144 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7145 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7146 		err = i40e_dcb_sw_default_config(pf);
7147 		if (err) {
7148 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7149 			goto out;
7150 		}
7151 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7152 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7153 			       DCB_CAP_DCBX_VER_IEEE;
7154 		/* at init capable but disabled */
7155 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7156 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7157 		goto out;
7158 	}
7159 	err = i40e_init_dcb(hw, true);
7160 	if (!err) {
7161 		/* Device/Function is not DCBX capable */
7162 		if ((!hw->func_caps.dcb) ||
7163 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7164 			dev_info(&pf->pdev->dev,
7165 				 "DCBX offload is not supported or is disabled for this PF.\n");
7166 		} else {
7167 			/* When status is not DISABLED then DCBX in FW */
7168 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7169 				       DCB_CAP_DCBX_VER_IEEE;
7170 
7171 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7172 			/* Enable DCB tagging only when more than one TC
7173 			 * or explicitly disable if only one TC
7174 			 */
7175 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7176 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7177 			else
7178 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7179 			dev_dbg(&pf->pdev->dev,
7180 				"DCBX offload is supported for this PF.\n");
7181 		}
7182 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7183 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7184 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7185 	} else {
7186 		dev_info(&pf->pdev->dev,
7187 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7188 			 i40e_stat_str(&pf->hw, err),
7189 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7190 	}
7191 
7192 out:
7193 	return err;
7194 }
7195 #endif /* CONFIG_I40E_DCB */
7196 
7197 /**
7198  * i40e_print_link_message - print link up or down
7199  * @vsi: the VSI for which link needs a message
7200  * @isup: true of link is up, false otherwise
7201  */
7202 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7203 {
7204 	enum i40e_aq_link_speed new_speed;
7205 	struct i40e_pf *pf = vsi->back;
7206 	char *speed = "Unknown";
7207 	char *fc = "Unknown";
7208 	char *fec = "";
7209 	char *req_fec = "";
7210 	char *an = "";
7211 
7212 	if (isup)
7213 		new_speed = pf->hw.phy.link_info.link_speed;
7214 	else
7215 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7216 
7217 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7218 		return;
7219 	vsi->current_isup = isup;
7220 	vsi->current_speed = new_speed;
7221 	if (!isup) {
7222 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7223 		return;
7224 	}
7225 
7226 	/* Warn user if link speed on NPAR enabled partition is not at
7227 	 * least 10GB
7228 	 */
7229 	if (pf->hw.func_caps.npar_enable &&
7230 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7231 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7232 		netdev_warn(vsi->netdev,
7233 			    "The partition detected link speed that is less than 10Gbps\n");
7234 
7235 	switch (pf->hw.phy.link_info.link_speed) {
7236 	case I40E_LINK_SPEED_40GB:
7237 		speed = "40 G";
7238 		break;
7239 	case I40E_LINK_SPEED_20GB:
7240 		speed = "20 G";
7241 		break;
7242 	case I40E_LINK_SPEED_25GB:
7243 		speed = "25 G";
7244 		break;
7245 	case I40E_LINK_SPEED_10GB:
7246 		speed = "10 G";
7247 		break;
7248 	case I40E_LINK_SPEED_5GB:
7249 		speed = "5 G";
7250 		break;
7251 	case I40E_LINK_SPEED_2_5GB:
7252 		speed = "2.5 G";
7253 		break;
7254 	case I40E_LINK_SPEED_1GB:
7255 		speed = "1000 M";
7256 		break;
7257 	case I40E_LINK_SPEED_100MB:
7258 		speed = "100 M";
7259 		break;
7260 	default:
7261 		break;
7262 	}
7263 
7264 	switch (pf->hw.fc.current_mode) {
7265 	case I40E_FC_FULL:
7266 		fc = "RX/TX";
7267 		break;
7268 	case I40E_FC_TX_PAUSE:
7269 		fc = "TX";
7270 		break;
7271 	case I40E_FC_RX_PAUSE:
7272 		fc = "RX";
7273 		break;
7274 	default:
7275 		fc = "None";
7276 		break;
7277 	}
7278 
7279 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7280 		req_fec = "None";
7281 		fec = "None";
7282 		an = "False";
7283 
7284 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7285 			an = "True";
7286 
7287 		if (pf->hw.phy.link_info.fec_info &
7288 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7289 			fec = "CL74 FC-FEC/BASE-R";
7290 		else if (pf->hw.phy.link_info.fec_info &
7291 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7292 			fec = "CL108 RS-FEC";
7293 
7294 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7295 		 * both RS and FC are requested
7296 		 */
7297 		if (vsi->back->hw.phy.link_info.req_fec_info &
7298 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7299 			if (vsi->back->hw.phy.link_info.req_fec_info &
7300 			    I40E_AQ_REQUEST_FEC_RS)
7301 				req_fec = "CL108 RS-FEC";
7302 			else
7303 				req_fec = "CL74 FC-FEC/BASE-R";
7304 		}
7305 		netdev_info(vsi->netdev,
7306 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7307 			    speed, req_fec, fec, an, fc);
7308 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7309 		req_fec = "None";
7310 		fec = "None";
7311 		an = "False";
7312 
7313 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7314 			an = "True";
7315 
7316 		if (pf->hw.phy.link_info.fec_info &
7317 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7318 			fec = "CL74 FC-FEC/BASE-R";
7319 
7320 		if (pf->hw.phy.link_info.req_fec_info &
7321 		    I40E_AQ_REQUEST_FEC_KR)
7322 			req_fec = "CL74 FC-FEC/BASE-R";
7323 
7324 		netdev_info(vsi->netdev,
7325 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7326 			    speed, req_fec, fec, an, fc);
7327 	} else {
7328 		netdev_info(vsi->netdev,
7329 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7330 			    speed, fc);
7331 	}
7332 
7333 }
7334 
7335 /**
7336  * i40e_up_complete - Finish the last steps of bringing up a connection
7337  * @vsi: the VSI being configured
7338  **/
7339 static int i40e_up_complete(struct i40e_vsi *vsi)
7340 {
7341 	struct i40e_pf *pf = vsi->back;
7342 	int err;
7343 
7344 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7345 		i40e_vsi_configure_msix(vsi);
7346 	else
7347 		i40e_configure_msi_and_legacy(vsi);
7348 
7349 	/* start rings */
7350 	err = i40e_vsi_start_rings(vsi);
7351 	if (err)
7352 		return err;
7353 
7354 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7355 	i40e_napi_enable_all(vsi);
7356 	i40e_vsi_enable_irq(vsi);
7357 
7358 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7359 	    (vsi->netdev)) {
7360 		i40e_print_link_message(vsi, true);
7361 		netif_tx_start_all_queues(vsi->netdev);
7362 		netif_carrier_on(vsi->netdev);
7363 	}
7364 
7365 	/* replay FDIR SB filters */
7366 	if (vsi->type == I40E_VSI_FDIR) {
7367 		/* reset fd counters */
7368 		pf->fd_add_err = 0;
7369 		pf->fd_atr_cnt = 0;
7370 		i40e_fdir_filter_restore(vsi);
7371 	}
7372 
7373 	/* On the next run of the service_task, notify any clients of the new
7374 	 * opened netdev
7375 	 */
7376 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7377 	i40e_service_event_schedule(pf);
7378 
7379 	return 0;
7380 }
7381 
7382 /**
7383  * i40e_vsi_reinit_locked - Reset the VSI
7384  * @vsi: the VSI being configured
7385  *
7386  * Rebuild the ring structs after some configuration
7387  * has changed, e.g. MTU size.
7388  **/
7389 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7390 {
7391 	struct i40e_pf *pf = vsi->back;
7392 
7393 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7394 		usleep_range(1000, 2000);
7395 	i40e_down(vsi);
7396 
7397 	i40e_up(vsi);
7398 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7399 }
7400 
7401 /**
7402  * i40e_force_link_state - Force the link status
7403  * @pf: board private structure
7404  * @is_up: whether the link state should be forced up or down
7405  **/
7406 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7407 {
7408 	struct i40e_aq_get_phy_abilities_resp abilities;
7409 	struct i40e_aq_set_phy_config config = {0};
7410 	bool non_zero_phy_type = is_up;
7411 	struct i40e_hw *hw = &pf->hw;
7412 	i40e_status err;
7413 	u64 mask;
7414 	u8 speed;
7415 
7416 	/* Card might've been put in an unstable state by other drivers
7417 	 * and applications, which causes incorrect speed values being
7418 	 * set on startup. In order to clear speed registers, we call
7419 	 * get_phy_capabilities twice, once to get initial state of
7420 	 * available speeds, and once to get current PHY config.
7421 	 */
7422 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7423 					   NULL);
7424 	if (err) {
7425 		dev_err(&pf->pdev->dev,
7426 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7427 			i40e_stat_str(hw, err),
7428 			i40e_aq_str(hw, hw->aq.asq_last_status));
7429 		return err;
7430 	}
7431 	speed = abilities.link_speed;
7432 
7433 	/* Get the current phy config */
7434 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7435 					   NULL);
7436 	if (err) {
7437 		dev_err(&pf->pdev->dev,
7438 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7439 			i40e_stat_str(hw, err),
7440 			i40e_aq_str(hw, hw->aq.asq_last_status));
7441 		return err;
7442 	}
7443 
7444 	/* If link needs to go up, but was not forced to go down,
7445 	 * and its speed values are OK, no need for a flap
7446 	 * if non_zero_phy_type was set, still need to force up
7447 	 */
7448 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7449 		non_zero_phy_type = true;
7450 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7451 		return I40E_SUCCESS;
7452 
7453 	/* To force link we need to set bits for all supported PHY types,
7454 	 * but there are now more than 32, so we need to split the bitmap
7455 	 * across two fields.
7456 	 */
7457 	mask = I40E_PHY_TYPES_BITMASK;
7458 	config.phy_type =
7459 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7460 	config.phy_type_ext =
7461 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7462 	/* Copy the old settings, except of phy_type */
7463 	config.abilities = abilities.abilities;
7464 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7465 		if (is_up)
7466 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7467 		else
7468 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7469 	}
7470 	if (abilities.link_speed != 0)
7471 		config.link_speed = abilities.link_speed;
7472 	else
7473 		config.link_speed = speed;
7474 	config.eee_capability = abilities.eee_capability;
7475 	config.eeer = abilities.eeer_val;
7476 	config.low_power_ctrl = abilities.d3_lpan;
7477 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7478 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7479 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7480 
7481 	if (err) {
7482 		dev_err(&pf->pdev->dev,
7483 			"set phy config ret =  %s last_status =  %s\n",
7484 			i40e_stat_str(&pf->hw, err),
7485 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7486 		return err;
7487 	}
7488 
7489 	/* Update the link info */
7490 	err = i40e_update_link_info(hw);
7491 	if (err) {
7492 		/* Wait a little bit (on 40G cards it sometimes takes a really
7493 		 * long time for link to come back from the atomic reset)
7494 		 * and try once more
7495 		 */
7496 		msleep(1000);
7497 		i40e_update_link_info(hw);
7498 	}
7499 
7500 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7501 
7502 	return I40E_SUCCESS;
7503 }
7504 
7505 /**
7506  * i40e_up - Bring the connection back up after being down
7507  * @vsi: the VSI being configured
7508  **/
7509 int i40e_up(struct i40e_vsi *vsi)
7510 {
7511 	int err;
7512 
7513 	if (vsi->type == I40E_VSI_MAIN &&
7514 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7515 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7516 		i40e_force_link_state(vsi->back, true);
7517 
7518 	err = i40e_vsi_configure(vsi);
7519 	if (!err)
7520 		err = i40e_up_complete(vsi);
7521 
7522 	return err;
7523 }
7524 
7525 /**
7526  * i40e_down - Shutdown the connection processing
7527  * @vsi: the VSI being stopped
7528  **/
7529 void i40e_down(struct i40e_vsi *vsi)
7530 {
7531 	int i;
7532 
7533 	/* It is assumed that the caller of this function
7534 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7535 	 */
7536 	if (vsi->netdev) {
7537 		netif_carrier_off(vsi->netdev);
7538 		netif_tx_disable(vsi->netdev);
7539 	}
7540 	i40e_vsi_disable_irq(vsi);
7541 	i40e_vsi_stop_rings(vsi);
7542 	if (vsi->type == I40E_VSI_MAIN &&
7543 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7544 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7545 		i40e_force_link_state(vsi->back, false);
7546 	i40e_napi_disable_all(vsi);
7547 
7548 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7549 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7550 		if (i40e_enabled_xdp_vsi(vsi)) {
7551 			/* Make sure that in-progress ndo_xdp_xmit and
7552 			 * ndo_xsk_wakeup calls are completed.
7553 			 */
7554 			synchronize_rcu();
7555 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7556 		}
7557 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7558 	}
7559 
7560 }
7561 
7562 /**
7563  * i40e_validate_mqprio_qopt- validate queue mapping info
7564  * @vsi: the VSI being configured
7565  * @mqprio_qopt: queue parametrs
7566  **/
7567 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7568 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7569 {
7570 	u64 sum_max_rate = 0;
7571 	u64 max_rate = 0;
7572 	int i;
7573 
7574 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7575 	    mqprio_qopt->qopt.num_tc < 1 ||
7576 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7577 		return -EINVAL;
7578 	for (i = 0; ; i++) {
7579 		if (!mqprio_qopt->qopt.count[i])
7580 			return -EINVAL;
7581 		if (mqprio_qopt->min_rate[i]) {
7582 			dev_err(&vsi->back->pdev->dev,
7583 				"Invalid min tx rate (greater than 0) specified\n");
7584 			return -EINVAL;
7585 		}
7586 		max_rate = mqprio_qopt->max_rate[i];
7587 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7588 		sum_max_rate += max_rate;
7589 
7590 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7591 			break;
7592 		if (mqprio_qopt->qopt.offset[i + 1] !=
7593 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7594 			return -EINVAL;
7595 	}
7596 	if (vsi->num_queue_pairs <
7597 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7598 		dev_err(&vsi->back->pdev->dev,
7599 			"Failed to create traffic channel, insufficient number of queues.\n");
7600 		return -EINVAL;
7601 	}
7602 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7603 		dev_err(&vsi->back->pdev->dev,
7604 			"Invalid max tx rate specified\n");
7605 		return -EINVAL;
7606 	}
7607 	return 0;
7608 }
7609 
7610 /**
7611  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7612  * @vsi: the VSI being configured
7613  **/
7614 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7615 {
7616 	u16 qcount;
7617 	int i;
7618 
7619 	/* Only TC0 is enabled */
7620 	vsi->tc_config.numtc = 1;
7621 	vsi->tc_config.enabled_tc = 1;
7622 	qcount = min_t(int, vsi->alloc_queue_pairs,
7623 		       i40e_pf_get_max_q_per_tc(vsi->back));
7624 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7625 		/* For the TC that is not enabled set the offset to default
7626 		 * queue and allocate one queue for the given TC.
7627 		 */
7628 		vsi->tc_config.tc_info[i].qoffset = 0;
7629 		if (i == 0)
7630 			vsi->tc_config.tc_info[i].qcount = qcount;
7631 		else
7632 			vsi->tc_config.tc_info[i].qcount = 1;
7633 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7634 	}
7635 }
7636 
7637 /**
7638  * i40e_del_macvlan_filter
7639  * @hw: pointer to the HW structure
7640  * @seid: seid of the channel VSI
7641  * @macaddr: the mac address to apply as a filter
7642  * @aq_err: store the admin Q error
7643  *
7644  * This function deletes a mac filter on the channel VSI which serves as the
7645  * macvlan. Returns 0 on success.
7646  **/
7647 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7648 					   const u8 *macaddr, int *aq_err)
7649 {
7650 	struct i40e_aqc_remove_macvlan_element_data element;
7651 	i40e_status status;
7652 
7653 	memset(&element, 0, sizeof(element));
7654 	ether_addr_copy(element.mac_addr, macaddr);
7655 	element.vlan_tag = 0;
7656 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7657 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7658 	*aq_err = hw->aq.asq_last_status;
7659 
7660 	return status;
7661 }
7662 
7663 /**
7664  * i40e_add_macvlan_filter
7665  * @hw: pointer to the HW structure
7666  * @seid: seid of the channel VSI
7667  * @macaddr: the mac address to apply as a filter
7668  * @aq_err: store the admin Q error
7669  *
7670  * This function adds a mac filter on the channel VSI which serves as the
7671  * macvlan. Returns 0 on success.
7672  **/
7673 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7674 					   const u8 *macaddr, int *aq_err)
7675 {
7676 	struct i40e_aqc_add_macvlan_element_data element;
7677 	i40e_status status;
7678 	u16 cmd_flags = 0;
7679 
7680 	ether_addr_copy(element.mac_addr, macaddr);
7681 	element.vlan_tag = 0;
7682 	element.queue_number = 0;
7683 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7684 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7685 	element.flags = cpu_to_le16(cmd_flags);
7686 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7687 	*aq_err = hw->aq.asq_last_status;
7688 
7689 	return status;
7690 }
7691 
7692 /**
7693  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7694  * @vsi: the VSI we want to access
7695  * @ch: the channel we want to access
7696  */
7697 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7698 {
7699 	struct i40e_ring *tx_ring, *rx_ring;
7700 	u16 pf_q;
7701 	int i;
7702 
7703 	for (i = 0; i < ch->num_queue_pairs; i++) {
7704 		pf_q = ch->base_queue + i;
7705 		tx_ring = vsi->tx_rings[pf_q];
7706 		tx_ring->ch = NULL;
7707 		rx_ring = vsi->rx_rings[pf_q];
7708 		rx_ring->ch = NULL;
7709 	}
7710 }
7711 
7712 /**
7713  * i40e_free_macvlan_channels
7714  * @vsi: the VSI we want to access
7715  *
7716  * This function frees the Qs of the channel VSI from
7717  * the stack and also deletes the channel VSIs which
7718  * serve as macvlans.
7719  */
7720 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7721 {
7722 	struct i40e_channel *ch, *ch_tmp;
7723 	int ret;
7724 
7725 	if (list_empty(&vsi->macvlan_list))
7726 		return;
7727 
7728 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7729 		struct i40e_vsi *parent_vsi;
7730 
7731 		if (i40e_is_channel_macvlan(ch)) {
7732 			i40e_reset_ch_rings(vsi, ch);
7733 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7734 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7735 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7736 			kfree(ch->fwd);
7737 			ch->fwd = NULL;
7738 		}
7739 
7740 		list_del(&ch->list);
7741 		parent_vsi = ch->parent_vsi;
7742 		if (!parent_vsi || !ch->initialized) {
7743 			kfree(ch);
7744 			continue;
7745 		}
7746 
7747 		/* remove the VSI */
7748 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7749 					     NULL);
7750 		if (ret)
7751 			dev_err(&vsi->back->pdev->dev,
7752 				"unable to remove channel (%d) for parent VSI(%d)\n",
7753 				ch->seid, parent_vsi->seid);
7754 		kfree(ch);
7755 	}
7756 	vsi->macvlan_cnt = 0;
7757 }
7758 
7759 /**
7760  * i40e_fwd_ring_up - bring the macvlan device up
7761  * @vsi: the VSI we want to access
7762  * @vdev: macvlan netdevice
7763  * @fwd: the private fwd structure
7764  */
7765 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7766 			    struct i40e_fwd_adapter *fwd)
7767 {
7768 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7769 	int ret = 0, num_tc = 1,  i, aq_err;
7770 	struct i40e_pf *pf = vsi->back;
7771 	struct i40e_hw *hw = &pf->hw;
7772 
7773 	/* Go through the list and find an available channel */
7774 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7775 		if (!i40e_is_channel_macvlan(iter)) {
7776 			iter->fwd = fwd;
7777 			/* record configuration for macvlan interface in vdev */
7778 			for (i = 0; i < num_tc; i++)
7779 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7780 							     i,
7781 							     iter->num_queue_pairs,
7782 							     iter->base_queue);
7783 			for (i = 0; i < iter->num_queue_pairs; i++) {
7784 				struct i40e_ring *tx_ring, *rx_ring;
7785 				u16 pf_q;
7786 
7787 				pf_q = iter->base_queue + i;
7788 
7789 				/* Get to TX ring ptr */
7790 				tx_ring = vsi->tx_rings[pf_q];
7791 				tx_ring->ch = iter;
7792 
7793 				/* Get the RX ring ptr */
7794 				rx_ring = vsi->rx_rings[pf_q];
7795 				rx_ring->ch = iter;
7796 			}
7797 			ch = iter;
7798 			break;
7799 		}
7800 	}
7801 
7802 	if (!ch)
7803 		return -EINVAL;
7804 
7805 	/* Guarantee all rings are updated before we update the
7806 	 * MAC address filter.
7807 	 */
7808 	wmb();
7809 
7810 	/* Add a mac filter */
7811 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7812 	if (ret) {
7813 		/* if we cannot add the MAC rule then disable the offload */
7814 		macvlan_release_l2fw_offload(vdev);
7815 		for (i = 0; i < ch->num_queue_pairs; i++) {
7816 			struct i40e_ring *rx_ring;
7817 			u16 pf_q;
7818 
7819 			pf_q = ch->base_queue + i;
7820 			rx_ring = vsi->rx_rings[pf_q];
7821 			rx_ring->netdev = NULL;
7822 		}
7823 		dev_info(&pf->pdev->dev,
7824 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7825 			  i40e_stat_str(hw, ret),
7826 			  i40e_aq_str(hw, aq_err));
7827 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7828 	}
7829 
7830 	return ret;
7831 }
7832 
7833 /**
7834  * i40e_setup_macvlans - create the channels which will be macvlans
7835  * @vsi: the VSI we want to access
7836  * @macvlan_cnt: no. of macvlans to be setup
7837  * @qcnt: no. of Qs per macvlan
7838  * @vdev: macvlan netdevice
7839  */
7840 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7841 			       struct net_device *vdev)
7842 {
7843 	struct i40e_pf *pf = vsi->back;
7844 	struct i40e_hw *hw = &pf->hw;
7845 	struct i40e_vsi_context ctxt;
7846 	u16 sections, qmap, num_qps;
7847 	struct i40e_channel *ch;
7848 	int i, pow, ret = 0;
7849 	u8 offset = 0;
7850 
7851 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7852 		return -EINVAL;
7853 
7854 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7855 
7856 	/* find the next higher power-of-2 of num queue pairs */
7857 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7858 
7859 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7860 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7861 
7862 	/* Setup context bits for the main VSI */
7863 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7864 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7865 	memset(&ctxt, 0, sizeof(ctxt));
7866 	ctxt.seid = vsi->seid;
7867 	ctxt.pf_num = vsi->back->hw.pf_id;
7868 	ctxt.vf_num = 0;
7869 	ctxt.uplink_seid = vsi->uplink_seid;
7870 	ctxt.info = vsi->info;
7871 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7872 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7873 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7874 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7875 
7876 	/* Reconfigure RSS for main VSI with new max queue count */
7877 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7878 	ret = i40e_vsi_config_rss(vsi);
7879 	if (ret) {
7880 		dev_info(&pf->pdev->dev,
7881 			 "Failed to reconfig RSS for num_queues (%u)\n",
7882 			 vsi->rss_size);
7883 		return ret;
7884 	}
7885 	vsi->reconfig_rss = true;
7886 	dev_dbg(&vsi->back->pdev->dev,
7887 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7888 	vsi->next_base_queue = num_qps;
7889 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7890 
7891 	/* Update the VSI after updating the VSI queue-mapping
7892 	 * information
7893 	 */
7894 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7895 	if (ret) {
7896 		dev_info(&pf->pdev->dev,
7897 			 "Update vsi tc config failed, err %s aq_err %s\n",
7898 			 i40e_stat_str(hw, ret),
7899 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7900 		return ret;
7901 	}
7902 	/* update the local VSI info with updated queue map */
7903 	i40e_vsi_update_queue_map(vsi, &ctxt);
7904 	vsi->info.valid_sections = 0;
7905 
7906 	/* Create channels for macvlans */
7907 	INIT_LIST_HEAD(&vsi->macvlan_list);
7908 	for (i = 0; i < macvlan_cnt; i++) {
7909 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7910 		if (!ch) {
7911 			ret = -ENOMEM;
7912 			goto err_free;
7913 		}
7914 		INIT_LIST_HEAD(&ch->list);
7915 		ch->num_queue_pairs = qcnt;
7916 		if (!i40e_setup_channel(pf, vsi, ch)) {
7917 			ret = -EINVAL;
7918 			kfree(ch);
7919 			goto err_free;
7920 		}
7921 		ch->parent_vsi = vsi;
7922 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7923 		vsi->macvlan_cnt++;
7924 		list_add_tail(&ch->list, &vsi->macvlan_list);
7925 	}
7926 
7927 	return ret;
7928 
7929 err_free:
7930 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7931 	i40e_free_macvlan_channels(vsi);
7932 
7933 	return ret;
7934 }
7935 
7936 /**
7937  * i40e_fwd_add - configure macvlans
7938  * @netdev: net device to configure
7939  * @vdev: macvlan netdevice
7940  **/
7941 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7942 {
7943 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7944 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7945 	struct i40e_vsi *vsi = np->vsi;
7946 	struct i40e_pf *pf = vsi->back;
7947 	struct i40e_fwd_adapter *fwd;
7948 	int avail_macvlan, ret;
7949 
7950 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7951 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7952 		return ERR_PTR(-EINVAL);
7953 	}
7954 	if (i40e_is_tc_mqprio_enabled(pf)) {
7955 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7956 		return ERR_PTR(-EINVAL);
7957 	}
7958 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7959 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7960 		return ERR_PTR(-EINVAL);
7961 	}
7962 
7963 	/* The macvlan device has to be a single Q device so that the
7964 	 * tc_to_txq field can be reused to pick the tx queue.
7965 	 */
7966 	if (netif_is_multiqueue(vdev))
7967 		return ERR_PTR(-ERANGE);
7968 
7969 	if (!vsi->macvlan_cnt) {
7970 		/* reserve bit 0 for the pf device */
7971 		set_bit(0, vsi->fwd_bitmask);
7972 
7973 		/* Try to reserve as many queues as possible for macvlans. First
7974 		 * reserve 3/4th of max vectors, then half, then quarter and
7975 		 * calculate Qs per macvlan as you go
7976 		 */
7977 		vectors = pf->num_lan_msix;
7978 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7979 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7980 			q_per_macvlan = 4;
7981 			macvlan_cnt = (vectors - 32) / 4;
7982 		} else if (vectors <= 64 && vectors > 32) {
7983 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7984 			q_per_macvlan = 2;
7985 			macvlan_cnt = (vectors - 16) / 2;
7986 		} else if (vectors <= 32 && vectors > 16) {
7987 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7988 			q_per_macvlan = 1;
7989 			macvlan_cnt = vectors - 16;
7990 		} else if (vectors <= 16 && vectors > 8) {
7991 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7992 			q_per_macvlan = 1;
7993 			macvlan_cnt = vectors - 8;
7994 		} else {
7995 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7996 			q_per_macvlan = 1;
7997 			macvlan_cnt = vectors - 1;
7998 		}
7999 
8000 		if (macvlan_cnt == 0)
8001 			return ERR_PTR(-EBUSY);
8002 
8003 		/* Quiesce VSI queues */
8004 		i40e_quiesce_vsi(vsi);
8005 
8006 		/* sets up the macvlans but does not "enable" them */
8007 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8008 					  vdev);
8009 		if (ret)
8010 			return ERR_PTR(ret);
8011 
8012 		/* Unquiesce VSI */
8013 		i40e_unquiesce_vsi(vsi);
8014 	}
8015 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8016 					    vsi->macvlan_cnt);
8017 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8018 		return ERR_PTR(-EBUSY);
8019 
8020 	/* create the fwd struct */
8021 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8022 	if (!fwd)
8023 		return ERR_PTR(-ENOMEM);
8024 
8025 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8026 	fwd->bit_no = avail_macvlan;
8027 	netdev_set_sb_channel(vdev, avail_macvlan);
8028 	fwd->netdev = vdev;
8029 
8030 	if (!netif_running(netdev))
8031 		return fwd;
8032 
8033 	/* Set fwd ring up */
8034 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8035 	if (ret) {
8036 		/* unbind the queues and drop the subordinate channel config */
8037 		netdev_unbind_sb_channel(netdev, vdev);
8038 		netdev_set_sb_channel(vdev, 0);
8039 
8040 		kfree(fwd);
8041 		return ERR_PTR(-EINVAL);
8042 	}
8043 
8044 	return fwd;
8045 }
8046 
8047 /**
8048  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8049  * @vsi: the VSI we want to access
8050  */
8051 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8052 {
8053 	struct i40e_channel *ch, *ch_tmp;
8054 	struct i40e_pf *pf = vsi->back;
8055 	struct i40e_hw *hw = &pf->hw;
8056 	int aq_err, ret = 0;
8057 
8058 	if (list_empty(&vsi->macvlan_list))
8059 		return;
8060 
8061 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8062 		if (i40e_is_channel_macvlan(ch)) {
8063 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8064 						      i40e_channel_mac(ch),
8065 						      &aq_err);
8066 			if (!ret) {
8067 				/* Reset queue contexts */
8068 				i40e_reset_ch_rings(vsi, ch);
8069 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8070 				netdev_unbind_sb_channel(vsi->netdev,
8071 							 ch->fwd->netdev);
8072 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8073 				kfree(ch->fwd);
8074 				ch->fwd = NULL;
8075 			}
8076 		}
8077 	}
8078 }
8079 
8080 /**
8081  * i40e_fwd_del - delete macvlan interfaces
8082  * @netdev: net device to configure
8083  * @vdev: macvlan netdevice
8084  */
8085 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8086 {
8087 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8088 	struct i40e_fwd_adapter *fwd = vdev;
8089 	struct i40e_channel *ch, *ch_tmp;
8090 	struct i40e_vsi *vsi = np->vsi;
8091 	struct i40e_pf *pf = vsi->back;
8092 	struct i40e_hw *hw = &pf->hw;
8093 	int aq_err, ret = 0;
8094 
8095 	/* Find the channel associated with the macvlan and del mac filter */
8096 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8097 		if (i40e_is_channel_macvlan(ch) &&
8098 		    ether_addr_equal(i40e_channel_mac(ch),
8099 				     fwd->netdev->dev_addr)) {
8100 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8101 						      i40e_channel_mac(ch),
8102 						      &aq_err);
8103 			if (!ret) {
8104 				/* Reset queue contexts */
8105 				i40e_reset_ch_rings(vsi, ch);
8106 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8107 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8108 				netdev_set_sb_channel(fwd->netdev, 0);
8109 				kfree(ch->fwd);
8110 				ch->fwd = NULL;
8111 			} else {
8112 				dev_info(&pf->pdev->dev,
8113 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8114 					  i40e_stat_str(hw, ret),
8115 					  i40e_aq_str(hw, aq_err));
8116 			}
8117 			break;
8118 		}
8119 	}
8120 }
8121 
8122 /**
8123  * i40e_setup_tc - configure multiple traffic classes
8124  * @netdev: net device to configure
8125  * @type_data: tc offload data
8126  **/
8127 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8128 {
8129 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8130 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8131 	struct i40e_vsi *vsi = np->vsi;
8132 	struct i40e_pf *pf = vsi->back;
8133 	u8 enabled_tc = 0, num_tc, hw;
8134 	bool need_reset = false;
8135 	int old_queue_pairs;
8136 	int ret = -EINVAL;
8137 	u16 mode;
8138 	int i;
8139 
8140 	old_queue_pairs = vsi->num_queue_pairs;
8141 	num_tc = mqprio_qopt->qopt.num_tc;
8142 	hw = mqprio_qopt->qopt.hw;
8143 	mode = mqprio_qopt->mode;
8144 	if (!hw) {
8145 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8146 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8147 		goto config_tc;
8148 	}
8149 
8150 	/* Check if MFP enabled */
8151 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8152 		netdev_info(netdev,
8153 			    "Configuring TC not supported in MFP mode\n");
8154 		return ret;
8155 	}
8156 	switch (mode) {
8157 	case TC_MQPRIO_MODE_DCB:
8158 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8159 
8160 		/* Check if DCB enabled to continue */
8161 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8162 			netdev_info(netdev,
8163 				    "DCB is not enabled for adapter\n");
8164 			return ret;
8165 		}
8166 
8167 		/* Check whether tc count is within enabled limit */
8168 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8169 			netdev_info(netdev,
8170 				    "TC count greater than enabled on link for adapter\n");
8171 			return ret;
8172 		}
8173 		break;
8174 	case TC_MQPRIO_MODE_CHANNEL:
8175 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8176 			netdev_info(netdev,
8177 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8178 			return ret;
8179 		}
8180 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8181 			return ret;
8182 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8183 		if (ret)
8184 			return ret;
8185 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8186 		       sizeof(*mqprio_qopt));
8187 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8188 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8189 		break;
8190 	default:
8191 		return -EINVAL;
8192 	}
8193 
8194 config_tc:
8195 	/* Generate TC map for number of tc requested */
8196 	for (i = 0; i < num_tc; i++)
8197 		enabled_tc |= BIT(i);
8198 
8199 	/* Requesting same TC configuration as already enabled */
8200 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8201 	    mode != TC_MQPRIO_MODE_CHANNEL)
8202 		return 0;
8203 
8204 	/* Quiesce VSI queues */
8205 	i40e_quiesce_vsi(vsi);
8206 
8207 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8208 		i40e_remove_queue_channels(vsi);
8209 
8210 	/* Configure VSI for enabled TCs */
8211 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8212 	if (ret) {
8213 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8214 			    vsi->seid);
8215 		need_reset = true;
8216 		goto exit;
8217 	} else if (enabled_tc &&
8218 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8219 		netdev_info(netdev,
8220 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8221 			    vsi->tc_config.tc_info[0].qcount);
8222 		ret = -EINVAL;
8223 		need_reset = true;
8224 		goto exit;
8225 	}
8226 
8227 	dev_info(&vsi->back->pdev->dev,
8228 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8229 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8230 
8231 	if (i40e_is_tc_mqprio_enabled(pf)) {
8232 		if (vsi->mqprio_qopt.max_rate[0]) {
8233 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8234 						  vsi->mqprio_qopt.max_rate[0]);
8235 
8236 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8237 			if (!ret) {
8238 				u64 credits = max_tx_rate;
8239 
8240 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8241 				dev_dbg(&vsi->back->pdev->dev,
8242 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8243 					max_tx_rate,
8244 					credits,
8245 					vsi->seid);
8246 			} else {
8247 				need_reset = true;
8248 				goto exit;
8249 			}
8250 		}
8251 		ret = i40e_configure_queue_channels(vsi);
8252 		if (ret) {
8253 			vsi->num_queue_pairs = old_queue_pairs;
8254 			netdev_info(netdev,
8255 				    "Failed configuring queue channels\n");
8256 			need_reset = true;
8257 			goto exit;
8258 		}
8259 	}
8260 
8261 exit:
8262 	/* Reset the configuration data to defaults, only TC0 is enabled */
8263 	if (need_reset) {
8264 		i40e_vsi_set_default_tc_config(vsi);
8265 		need_reset = false;
8266 	}
8267 
8268 	/* Unquiesce VSI */
8269 	i40e_unquiesce_vsi(vsi);
8270 	return ret;
8271 }
8272 
8273 /**
8274  * i40e_set_cld_element - sets cloud filter element data
8275  * @filter: cloud filter rule
8276  * @cld: ptr to cloud filter element data
8277  *
8278  * This is helper function to copy data into cloud filter element
8279  **/
8280 static inline void
8281 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8282 		     struct i40e_aqc_cloud_filters_element_data *cld)
8283 {
8284 	u32 ipa;
8285 	int i;
8286 
8287 	memset(cld, 0, sizeof(*cld));
8288 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8289 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8290 
8291 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8292 		return;
8293 
8294 	if (filter->n_proto == ETH_P_IPV6) {
8295 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8296 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8297 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8298 
8299 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8300 		}
8301 	} else {
8302 		ipa = be32_to_cpu(filter->dst_ipv4);
8303 
8304 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8305 	}
8306 
8307 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8308 
8309 	/* tenant_id is not supported by FW now, once the support is enabled
8310 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8311 	 */
8312 	if (filter->tenant_id)
8313 		return;
8314 }
8315 
8316 /**
8317  * i40e_add_del_cloud_filter - Add/del cloud filter
8318  * @vsi: pointer to VSI
8319  * @filter: cloud filter rule
8320  * @add: if true, add, if false, delete
8321  *
8322  * Add or delete a cloud filter for a specific flow spec.
8323  * Returns 0 if the filter were successfully added.
8324  **/
8325 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8326 			      struct i40e_cloud_filter *filter, bool add)
8327 {
8328 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8329 	struct i40e_pf *pf = vsi->back;
8330 	int ret;
8331 	static const u16 flag_table[128] = {
8332 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8333 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8334 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8335 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8336 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8337 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8338 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8339 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8340 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8341 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8342 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8343 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8344 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8345 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8346 	};
8347 
8348 	if (filter->flags >= ARRAY_SIZE(flag_table))
8349 		return I40E_ERR_CONFIG;
8350 
8351 	memset(&cld_filter, 0, sizeof(cld_filter));
8352 
8353 	/* copy element needed to add cloud filter from filter */
8354 	i40e_set_cld_element(filter, &cld_filter);
8355 
8356 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8357 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8358 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8359 
8360 	if (filter->n_proto == ETH_P_IPV6)
8361 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8362 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8363 	else
8364 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8365 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8366 
8367 	if (add)
8368 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8369 						&cld_filter, 1);
8370 	else
8371 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8372 						&cld_filter, 1);
8373 	if (ret)
8374 		dev_dbg(&pf->pdev->dev,
8375 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8376 			add ? "add" : "delete", filter->dst_port, ret,
8377 			pf->hw.aq.asq_last_status);
8378 	else
8379 		dev_info(&pf->pdev->dev,
8380 			 "%s cloud filter for VSI: %d\n",
8381 			 add ? "Added" : "Deleted", filter->seid);
8382 	return ret;
8383 }
8384 
8385 /**
8386  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8387  * @vsi: pointer to VSI
8388  * @filter: cloud filter rule
8389  * @add: if true, add, if false, delete
8390  *
8391  * Add or delete a cloud filter for a specific flow spec using big buffer.
8392  * Returns 0 if the filter were successfully added.
8393  **/
8394 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8395 				      struct i40e_cloud_filter *filter,
8396 				      bool add)
8397 {
8398 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8399 	struct i40e_pf *pf = vsi->back;
8400 	int ret;
8401 
8402 	/* Both (src/dst) valid mac_addr are not supported */
8403 	if ((is_valid_ether_addr(filter->dst_mac) &&
8404 	     is_valid_ether_addr(filter->src_mac)) ||
8405 	    (is_multicast_ether_addr(filter->dst_mac) &&
8406 	     is_multicast_ether_addr(filter->src_mac)))
8407 		return -EOPNOTSUPP;
8408 
8409 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8410 	 * ports are not supported via big buffer now.
8411 	 */
8412 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8413 		return -EOPNOTSUPP;
8414 
8415 	/* adding filter using src_port/src_ip is not supported at this stage */
8416 	if (filter->src_port ||
8417 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8418 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8419 		return -EOPNOTSUPP;
8420 
8421 	memset(&cld_filter, 0, sizeof(cld_filter));
8422 
8423 	/* copy element needed to add cloud filter from filter */
8424 	i40e_set_cld_element(filter, &cld_filter.element);
8425 
8426 	if (is_valid_ether_addr(filter->dst_mac) ||
8427 	    is_valid_ether_addr(filter->src_mac) ||
8428 	    is_multicast_ether_addr(filter->dst_mac) ||
8429 	    is_multicast_ether_addr(filter->src_mac)) {
8430 		/* MAC + IP : unsupported mode */
8431 		if (filter->dst_ipv4)
8432 			return -EOPNOTSUPP;
8433 
8434 		/* since we validated that L4 port must be valid before
8435 		 * we get here, start with respective "flags" value
8436 		 * and update if vlan is present or not
8437 		 */
8438 		cld_filter.element.flags =
8439 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8440 
8441 		if (filter->vlan_id) {
8442 			cld_filter.element.flags =
8443 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8444 		}
8445 
8446 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8447 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8448 		cld_filter.element.flags =
8449 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8450 		if (filter->n_proto == ETH_P_IPV6)
8451 			cld_filter.element.flags |=
8452 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8453 		else
8454 			cld_filter.element.flags |=
8455 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8456 	} else {
8457 		dev_err(&pf->pdev->dev,
8458 			"either mac or ip has to be valid for cloud filter\n");
8459 		return -EINVAL;
8460 	}
8461 
8462 	/* Now copy L4 port in Byte 6..7 in general fields */
8463 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8464 						be16_to_cpu(filter->dst_port);
8465 
8466 	if (add) {
8467 		/* Validate current device switch mode, change if necessary */
8468 		ret = i40e_validate_and_set_switch_mode(vsi);
8469 		if (ret) {
8470 			dev_err(&pf->pdev->dev,
8471 				"failed to set switch mode, ret %d\n",
8472 				ret);
8473 			return ret;
8474 		}
8475 
8476 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8477 						   &cld_filter, 1);
8478 	} else {
8479 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8480 						   &cld_filter, 1);
8481 	}
8482 
8483 	if (ret)
8484 		dev_dbg(&pf->pdev->dev,
8485 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8486 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8487 	else
8488 		dev_info(&pf->pdev->dev,
8489 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8490 			 add ? "add" : "delete", filter->seid,
8491 			 ntohs(filter->dst_port));
8492 	return ret;
8493 }
8494 
8495 /**
8496  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8497  * @vsi: Pointer to VSI
8498  * @f: Pointer to struct flow_cls_offload
8499  * @filter: Pointer to cloud filter structure
8500  *
8501  **/
8502 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8503 				 struct flow_cls_offload *f,
8504 				 struct i40e_cloud_filter *filter)
8505 {
8506 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8507 	struct flow_dissector *dissector = rule->match.dissector;
8508 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8509 	struct i40e_pf *pf = vsi->back;
8510 	u8 field_flags = 0;
8511 
8512 	if (dissector->used_keys &
8513 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8514 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8515 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8516 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8517 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8518 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8519 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8520 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8521 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8522 			dissector->used_keys);
8523 		return -EOPNOTSUPP;
8524 	}
8525 
8526 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8527 		struct flow_match_enc_keyid match;
8528 
8529 		flow_rule_match_enc_keyid(rule, &match);
8530 		if (match.mask->keyid != 0)
8531 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8532 
8533 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8534 	}
8535 
8536 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8537 		struct flow_match_basic match;
8538 
8539 		flow_rule_match_basic(rule, &match);
8540 		n_proto_key = ntohs(match.key->n_proto);
8541 		n_proto_mask = ntohs(match.mask->n_proto);
8542 
8543 		if (n_proto_key == ETH_P_ALL) {
8544 			n_proto_key = 0;
8545 			n_proto_mask = 0;
8546 		}
8547 		filter->n_proto = n_proto_key & n_proto_mask;
8548 		filter->ip_proto = match.key->ip_proto;
8549 	}
8550 
8551 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8552 		struct flow_match_eth_addrs match;
8553 
8554 		flow_rule_match_eth_addrs(rule, &match);
8555 
8556 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8557 		if (!is_zero_ether_addr(match.mask->dst)) {
8558 			if (is_broadcast_ether_addr(match.mask->dst)) {
8559 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8560 			} else {
8561 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8562 					match.mask->dst);
8563 				return I40E_ERR_CONFIG;
8564 			}
8565 		}
8566 
8567 		if (!is_zero_ether_addr(match.mask->src)) {
8568 			if (is_broadcast_ether_addr(match.mask->src)) {
8569 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8570 			} else {
8571 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8572 					match.mask->src);
8573 				return I40E_ERR_CONFIG;
8574 			}
8575 		}
8576 		ether_addr_copy(filter->dst_mac, match.key->dst);
8577 		ether_addr_copy(filter->src_mac, match.key->src);
8578 	}
8579 
8580 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8581 		struct flow_match_vlan match;
8582 
8583 		flow_rule_match_vlan(rule, &match);
8584 		if (match.mask->vlan_id) {
8585 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8586 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8587 
8588 			} else {
8589 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8590 					match.mask->vlan_id);
8591 				return I40E_ERR_CONFIG;
8592 			}
8593 		}
8594 
8595 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8596 	}
8597 
8598 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8599 		struct flow_match_control match;
8600 
8601 		flow_rule_match_control(rule, &match);
8602 		addr_type = match.key->addr_type;
8603 	}
8604 
8605 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8606 		struct flow_match_ipv4_addrs match;
8607 
8608 		flow_rule_match_ipv4_addrs(rule, &match);
8609 		if (match.mask->dst) {
8610 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8611 				field_flags |= I40E_CLOUD_FIELD_IIP;
8612 			} else {
8613 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8614 					&match.mask->dst);
8615 				return I40E_ERR_CONFIG;
8616 			}
8617 		}
8618 
8619 		if (match.mask->src) {
8620 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8621 				field_flags |= I40E_CLOUD_FIELD_IIP;
8622 			} else {
8623 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8624 					&match.mask->src);
8625 				return I40E_ERR_CONFIG;
8626 			}
8627 		}
8628 
8629 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8630 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8631 			return I40E_ERR_CONFIG;
8632 		}
8633 		filter->dst_ipv4 = match.key->dst;
8634 		filter->src_ipv4 = match.key->src;
8635 	}
8636 
8637 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8638 		struct flow_match_ipv6_addrs match;
8639 
8640 		flow_rule_match_ipv6_addrs(rule, &match);
8641 
8642 		/* src and dest IPV6 address should not be LOOPBACK
8643 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8644 		 */
8645 		if (ipv6_addr_loopback(&match.key->dst) ||
8646 		    ipv6_addr_loopback(&match.key->src)) {
8647 			dev_err(&pf->pdev->dev,
8648 				"Bad ipv6, addr is LOOPBACK\n");
8649 			return I40E_ERR_CONFIG;
8650 		}
8651 		if (!ipv6_addr_any(&match.mask->dst) ||
8652 		    !ipv6_addr_any(&match.mask->src))
8653 			field_flags |= I40E_CLOUD_FIELD_IIP;
8654 
8655 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8656 		       sizeof(filter->src_ipv6));
8657 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8658 		       sizeof(filter->dst_ipv6));
8659 	}
8660 
8661 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8662 		struct flow_match_ports match;
8663 
8664 		flow_rule_match_ports(rule, &match);
8665 		if (match.mask->src) {
8666 			if (match.mask->src == cpu_to_be16(0xffff)) {
8667 				field_flags |= I40E_CLOUD_FIELD_IIP;
8668 			} else {
8669 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8670 					be16_to_cpu(match.mask->src));
8671 				return I40E_ERR_CONFIG;
8672 			}
8673 		}
8674 
8675 		if (match.mask->dst) {
8676 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8677 				field_flags |= I40E_CLOUD_FIELD_IIP;
8678 			} else {
8679 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8680 					be16_to_cpu(match.mask->dst));
8681 				return I40E_ERR_CONFIG;
8682 			}
8683 		}
8684 
8685 		filter->dst_port = match.key->dst;
8686 		filter->src_port = match.key->src;
8687 
8688 		switch (filter->ip_proto) {
8689 		case IPPROTO_TCP:
8690 		case IPPROTO_UDP:
8691 			break;
8692 		default:
8693 			dev_err(&pf->pdev->dev,
8694 				"Only UDP and TCP transport are supported\n");
8695 			return -EINVAL;
8696 		}
8697 	}
8698 	filter->flags = field_flags;
8699 	return 0;
8700 }
8701 
8702 /**
8703  * i40e_handle_tclass: Forward to a traffic class on the device
8704  * @vsi: Pointer to VSI
8705  * @tc: traffic class index on the device
8706  * @filter: Pointer to cloud filter structure
8707  *
8708  **/
8709 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8710 			      struct i40e_cloud_filter *filter)
8711 {
8712 	struct i40e_channel *ch, *ch_tmp;
8713 
8714 	/* direct to a traffic class on the same device */
8715 	if (tc == 0) {
8716 		filter->seid = vsi->seid;
8717 		return 0;
8718 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8719 		if (!filter->dst_port) {
8720 			dev_err(&vsi->back->pdev->dev,
8721 				"Specify destination port to direct to traffic class that is not default\n");
8722 			return -EINVAL;
8723 		}
8724 		if (list_empty(&vsi->ch_list))
8725 			return -EINVAL;
8726 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8727 					 list) {
8728 			if (ch->seid == vsi->tc_seid_map[tc])
8729 				filter->seid = ch->seid;
8730 		}
8731 		return 0;
8732 	}
8733 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8734 	return -EINVAL;
8735 }
8736 
8737 /**
8738  * i40e_configure_clsflower - Configure tc flower filters
8739  * @vsi: Pointer to VSI
8740  * @cls_flower: Pointer to struct flow_cls_offload
8741  *
8742  **/
8743 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8744 				    struct flow_cls_offload *cls_flower)
8745 {
8746 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8747 	struct i40e_cloud_filter *filter = NULL;
8748 	struct i40e_pf *pf = vsi->back;
8749 	int err = 0;
8750 
8751 	if (tc < 0) {
8752 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8753 		return -EOPNOTSUPP;
8754 	}
8755 
8756 	if (!tc) {
8757 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8758 		return -EINVAL;
8759 	}
8760 
8761 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8762 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8763 		return -EBUSY;
8764 
8765 	if (pf->fdir_pf_active_filters ||
8766 	    (!hlist_empty(&pf->fdir_filter_list))) {
8767 		dev_err(&vsi->back->pdev->dev,
8768 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8769 		return -EINVAL;
8770 	}
8771 
8772 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8773 		dev_err(&vsi->back->pdev->dev,
8774 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8775 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8776 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8777 	}
8778 
8779 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8780 	if (!filter)
8781 		return -ENOMEM;
8782 
8783 	filter->cookie = cls_flower->cookie;
8784 
8785 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8786 	if (err < 0)
8787 		goto err;
8788 
8789 	err = i40e_handle_tclass(vsi, tc, filter);
8790 	if (err < 0)
8791 		goto err;
8792 
8793 	/* Add cloud filter */
8794 	if (filter->dst_port)
8795 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8796 	else
8797 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8798 
8799 	if (err) {
8800 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8801 			err);
8802 		goto err;
8803 	}
8804 
8805 	/* add filter to the ordered list */
8806 	INIT_HLIST_NODE(&filter->cloud_node);
8807 
8808 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8809 
8810 	pf->num_cloud_filters++;
8811 
8812 	return err;
8813 err:
8814 	kfree(filter);
8815 	return err;
8816 }
8817 
8818 /**
8819  * i40e_find_cloud_filter - Find the could filter in the list
8820  * @vsi: Pointer to VSI
8821  * @cookie: filter specific cookie
8822  *
8823  **/
8824 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8825 							unsigned long *cookie)
8826 {
8827 	struct i40e_cloud_filter *filter = NULL;
8828 	struct hlist_node *node2;
8829 
8830 	hlist_for_each_entry_safe(filter, node2,
8831 				  &vsi->back->cloud_filter_list, cloud_node)
8832 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8833 			return filter;
8834 	return NULL;
8835 }
8836 
8837 /**
8838  * i40e_delete_clsflower - Remove tc flower filters
8839  * @vsi: Pointer to VSI
8840  * @cls_flower: Pointer to struct flow_cls_offload
8841  *
8842  **/
8843 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8844 				 struct flow_cls_offload *cls_flower)
8845 {
8846 	struct i40e_cloud_filter *filter = NULL;
8847 	struct i40e_pf *pf = vsi->back;
8848 	int err = 0;
8849 
8850 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8851 
8852 	if (!filter)
8853 		return -EINVAL;
8854 
8855 	hash_del(&filter->cloud_node);
8856 
8857 	if (filter->dst_port)
8858 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8859 	else
8860 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8861 
8862 	kfree(filter);
8863 	if (err) {
8864 		dev_err(&pf->pdev->dev,
8865 			"Failed to delete cloud filter, err %s\n",
8866 			i40e_stat_str(&pf->hw, err));
8867 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8868 	}
8869 
8870 	pf->num_cloud_filters--;
8871 	if (!pf->num_cloud_filters)
8872 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8873 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8874 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8875 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8876 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8877 		}
8878 	return 0;
8879 }
8880 
8881 /**
8882  * i40e_setup_tc_cls_flower - flower classifier offloads
8883  * @np: net device to configure
8884  * @cls_flower: offload data
8885  **/
8886 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8887 				    struct flow_cls_offload *cls_flower)
8888 {
8889 	struct i40e_vsi *vsi = np->vsi;
8890 
8891 	switch (cls_flower->command) {
8892 	case FLOW_CLS_REPLACE:
8893 		return i40e_configure_clsflower(vsi, cls_flower);
8894 	case FLOW_CLS_DESTROY:
8895 		return i40e_delete_clsflower(vsi, cls_flower);
8896 	case FLOW_CLS_STATS:
8897 		return -EOPNOTSUPP;
8898 	default:
8899 		return -EOPNOTSUPP;
8900 	}
8901 }
8902 
8903 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8904 				  void *cb_priv)
8905 {
8906 	struct i40e_netdev_priv *np = cb_priv;
8907 
8908 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8909 		return -EOPNOTSUPP;
8910 
8911 	switch (type) {
8912 	case TC_SETUP_CLSFLOWER:
8913 		return i40e_setup_tc_cls_flower(np, type_data);
8914 
8915 	default:
8916 		return -EOPNOTSUPP;
8917 	}
8918 }
8919 
8920 static LIST_HEAD(i40e_block_cb_list);
8921 
8922 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8923 			   void *type_data)
8924 {
8925 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8926 
8927 	switch (type) {
8928 	case TC_SETUP_QDISC_MQPRIO:
8929 		return i40e_setup_tc(netdev, type_data);
8930 	case TC_SETUP_BLOCK:
8931 		return flow_block_cb_setup_simple(type_data,
8932 						  &i40e_block_cb_list,
8933 						  i40e_setup_tc_block_cb,
8934 						  np, np, true);
8935 	default:
8936 		return -EOPNOTSUPP;
8937 	}
8938 }
8939 
8940 /**
8941  * i40e_open - Called when a network interface is made active
8942  * @netdev: network interface device structure
8943  *
8944  * The open entry point is called when a network interface is made
8945  * active by the system (IFF_UP).  At this point all resources needed
8946  * for transmit and receive operations are allocated, the interrupt
8947  * handler is registered with the OS, the netdev watchdog subtask is
8948  * enabled, and the stack is notified that the interface is ready.
8949  *
8950  * Returns 0 on success, negative value on failure
8951  **/
8952 int i40e_open(struct net_device *netdev)
8953 {
8954 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8955 	struct i40e_vsi *vsi = np->vsi;
8956 	struct i40e_pf *pf = vsi->back;
8957 	int err;
8958 
8959 	/* disallow open during test or if eeprom is broken */
8960 	if (test_bit(__I40E_TESTING, pf->state) ||
8961 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8962 		return -EBUSY;
8963 
8964 	netif_carrier_off(netdev);
8965 
8966 	if (i40e_force_link_state(pf, true))
8967 		return -EAGAIN;
8968 
8969 	err = i40e_vsi_open(vsi);
8970 	if (err)
8971 		return err;
8972 
8973 	/* configure global TSO hardware offload settings */
8974 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8975 						       TCP_FLAG_FIN) >> 16);
8976 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8977 						       TCP_FLAG_FIN |
8978 						       TCP_FLAG_CWR) >> 16);
8979 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8980 	udp_tunnel_get_rx_info(netdev);
8981 
8982 	return 0;
8983 }
8984 
8985 /**
8986  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8987  * @vsi: vsi structure
8988  *
8989  * This updates netdev's number of tx/rx queues
8990  *
8991  * Returns status of setting tx/rx queues
8992  **/
8993 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8994 {
8995 	int ret;
8996 
8997 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8998 					   vsi->num_queue_pairs);
8999 	if (ret)
9000 		return ret;
9001 
9002 	return netif_set_real_num_tx_queues(vsi->netdev,
9003 					    vsi->num_queue_pairs);
9004 }
9005 
9006 /**
9007  * i40e_vsi_open -
9008  * @vsi: the VSI to open
9009  *
9010  * Finish initialization of the VSI.
9011  *
9012  * Returns 0 on success, negative value on failure
9013  *
9014  * Note: expects to be called while under rtnl_lock()
9015  **/
9016 int i40e_vsi_open(struct i40e_vsi *vsi)
9017 {
9018 	struct i40e_pf *pf = vsi->back;
9019 	char int_name[I40E_INT_NAME_STR_LEN];
9020 	int err;
9021 
9022 	/* allocate descriptors */
9023 	err = i40e_vsi_setup_tx_resources(vsi);
9024 	if (err)
9025 		goto err_setup_tx;
9026 	err = i40e_vsi_setup_rx_resources(vsi);
9027 	if (err)
9028 		goto err_setup_rx;
9029 
9030 	err = i40e_vsi_configure(vsi);
9031 	if (err)
9032 		goto err_setup_rx;
9033 
9034 	if (vsi->netdev) {
9035 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9036 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9037 		err = i40e_vsi_request_irq(vsi, int_name);
9038 		if (err)
9039 			goto err_setup_rx;
9040 
9041 		/* Notify the stack of the actual queue counts. */
9042 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9043 		if (err)
9044 			goto err_set_queues;
9045 
9046 	} else if (vsi->type == I40E_VSI_FDIR) {
9047 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9048 			 dev_driver_string(&pf->pdev->dev),
9049 			 dev_name(&pf->pdev->dev));
9050 		err = i40e_vsi_request_irq(vsi, int_name);
9051 		if (err)
9052 			goto err_setup_rx;
9053 
9054 	} else {
9055 		err = -EINVAL;
9056 		goto err_setup_rx;
9057 	}
9058 
9059 	err = i40e_up_complete(vsi);
9060 	if (err)
9061 		goto err_up_complete;
9062 
9063 	return 0;
9064 
9065 err_up_complete:
9066 	i40e_down(vsi);
9067 err_set_queues:
9068 	i40e_vsi_free_irq(vsi);
9069 err_setup_rx:
9070 	i40e_vsi_free_rx_resources(vsi);
9071 err_setup_tx:
9072 	i40e_vsi_free_tx_resources(vsi);
9073 	if (vsi == pf->vsi[pf->lan_vsi])
9074 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9075 
9076 	return err;
9077 }
9078 
9079 /**
9080  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9081  * @pf: Pointer to PF
9082  *
9083  * This function destroys the hlist where all the Flow Director
9084  * filters were saved.
9085  **/
9086 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9087 {
9088 	struct i40e_fdir_filter *filter;
9089 	struct i40e_flex_pit *pit_entry, *tmp;
9090 	struct hlist_node *node2;
9091 
9092 	hlist_for_each_entry_safe(filter, node2,
9093 				  &pf->fdir_filter_list, fdir_node) {
9094 		hlist_del(&filter->fdir_node);
9095 		kfree(filter);
9096 	}
9097 
9098 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9099 		list_del(&pit_entry->list);
9100 		kfree(pit_entry);
9101 	}
9102 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9103 
9104 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9105 		list_del(&pit_entry->list);
9106 		kfree(pit_entry);
9107 	}
9108 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9109 
9110 	pf->fdir_pf_active_filters = 0;
9111 	i40e_reset_fdir_filter_cnt(pf);
9112 
9113 	/* Reprogram the default input set for TCP/IPv4 */
9114 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9115 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9116 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9117 
9118 	/* Reprogram the default input set for TCP/IPv6 */
9119 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9120 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9121 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9122 
9123 	/* Reprogram the default input set for UDP/IPv4 */
9124 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9125 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9126 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9127 
9128 	/* Reprogram the default input set for UDP/IPv6 */
9129 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9130 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9131 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9132 
9133 	/* Reprogram the default input set for SCTP/IPv4 */
9134 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9135 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9136 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9137 
9138 	/* Reprogram the default input set for SCTP/IPv6 */
9139 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9140 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9141 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9142 
9143 	/* Reprogram the default input set for Other/IPv4 */
9144 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9145 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9146 
9147 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9148 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9149 
9150 	/* Reprogram the default input set for Other/IPv6 */
9151 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9152 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9153 
9154 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9155 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9156 }
9157 
9158 /**
9159  * i40e_cloud_filter_exit - Cleans up the cloud filters
9160  * @pf: Pointer to PF
9161  *
9162  * This function destroys the hlist where all the cloud filters
9163  * were saved.
9164  **/
9165 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9166 {
9167 	struct i40e_cloud_filter *cfilter;
9168 	struct hlist_node *node;
9169 
9170 	hlist_for_each_entry_safe(cfilter, node,
9171 				  &pf->cloud_filter_list, cloud_node) {
9172 		hlist_del(&cfilter->cloud_node);
9173 		kfree(cfilter);
9174 	}
9175 	pf->num_cloud_filters = 0;
9176 
9177 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9178 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9179 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9180 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9181 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9182 	}
9183 }
9184 
9185 /**
9186  * i40e_close - Disables a network interface
9187  * @netdev: network interface device structure
9188  *
9189  * The close entry point is called when an interface is de-activated
9190  * by the OS.  The hardware is still under the driver's control, but
9191  * this netdev interface is disabled.
9192  *
9193  * Returns 0, this is not allowed to fail
9194  **/
9195 int i40e_close(struct net_device *netdev)
9196 {
9197 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9198 	struct i40e_vsi *vsi = np->vsi;
9199 
9200 	i40e_vsi_close(vsi);
9201 
9202 	return 0;
9203 }
9204 
9205 /**
9206  * i40e_do_reset - Start a PF or Core Reset sequence
9207  * @pf: board private structure
9208  * @reset_flags: which reset is requested
9209  * @lock_acquired: indicates whether or not the lock has been acquired
9210  * before this function was called.
9211  *
9212  * The essential difference in resets is that the PF Reset
9213  * doesn't clear the packet buffers, doesn't reset the PE
9214  * firmware, and doesn't bother the other PFs on the chip.
9215  **/
9216 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9217 {
9218 	u32 val;
9219 
9220 	/* do the biggest reset indicated */
9221 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9222 
9223 		/* Request a Global Reset
9224 		 *
9225 		 * This will start the chip's countdown to the actual full
9226 		 * chip reset event, and a warning interrupt to be sent
9227 		 * to all PFs, including the requestor.  Our handler
9228 		 * for the warning interrupt will deal with the shutdown
9229 		 * and recovery of the switch setup.
9230 		 */
9231 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9232 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9233 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9234 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9235 
9236 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9237 
9238 		/* Request a Core Reset
9239 		 *
9240 		 * Same as Global Reset, except does *not* include the MAC/PHY
9241 		 */
9242 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9243 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9244 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9245 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9246 		i40e_flush(&pf->hw);
9247 
9248 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9249 
9250 		/* Request a PF Reset
9251 		 *
9252 		 * Resets only the PF-specific registers
9253 		 *
9254 		 * This goes directly to the tear-down and rebuild of
9255 		 * the switch, since we need to do all the recovery as
9256 		 * for the Core Reset.
9257 		 */
9258 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9259 		i40e_handle_reset_warning(pf, lock_acquired);
9260 
9261 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9262 		/* Request a PF Reset
9263 		 *
9264 		 * Resets PF and reinitializes PFs VSI.
9265 		 */
9266 		i40e_prep_for_reset(pf);
9267 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9268 		dev_info(&pf->pdev->dev,
9269 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9270 			 "FW LLDP is disabled\n" :
9271 			 "FW LLDP is enabled\n");
9272 
9273 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9274 		int v;
9275 
9276 		/* Find the VSI(s) that requested a re-init */
9277 		dev_info(&pf->pdev->dev,
9278 			 "VSI reinit requested\n");
9279 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9280 			struct i40e_vsi *vsi = pf->vsi[v];
9281 
9282 			if (vsi != NULL &&
9283 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9284 					       vsi->state))
9285 				i40e_vsi_reinit_locked(pf->vsi[v]);
9286 		}
9287 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9288 		int v;
9289 
9290 		/* Find the VSI(s) that needs to be brought down */
9291 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9292 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9293 			struct i40e_vsi *vsi = pf->vsi[v];
9294 
9295 			if (vsi != NULL &&
9296 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9297 					       vsi->state)) {
9298 				set_bit(__I40E_VSI_DOWN, vsi->state);
9299 				i40e_down(vsi);
9300 			}
9301 		}
9302 	} else {
9303 		dev_info(&pf->pdev->dev,
9304 			 "bad reset request 0x%08x\n", reset_flags);
9305 	}
9306 }
9307 
9308 #ifdef CONFIG_I40E_DCB
9309 /**
9310  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9311  * @pf: board private structure
9312  * @old_cfg: current DCB config
9313  * @new_cfg: new DCB config
9314  **/
9315 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9316 			    struct i40e_dcbx_config *old_cfg,
9317 			    struct i40e_dcbx_config *new_cfg)
9318 {
9319 	bool need_reconfig = false;
9320 
9321 	/* Check if ETS configuration has changed */
9322 	if (memcmp(&new_cfg->etscfg,
9323 		   &old_cfg->etscfg,
9324 		   sizeof(new_cfg->etscfg))) {
9325 		/* If Priority Table has changed reconfig is needed */
9326 		if (memcmp(&new_cfg->etscfg.prioritytable,
9327 			   &old_cfg->etscfg.prioritytable,
9328 			   sizeof(new_cfg->etscfg.prioritytable))) {
9329 			need_reconfig = true;
9330 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9331 		}
9332 
9333 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9334 			   &old_cfg->etscfg.tcbwtable,
9335 			   sizeof(new_cfg->etscfg.tcbwtable)))
9336 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9337 
9338 		if (memcmp(&new_cfg->etscfg.tsatable,
9339 			   &old_cfg->etscfg.tsatable,
9340 			   sizeof(new_cfg->etscfg.tsatable)))
9341 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9342 	}
9343 
9344 	/* Check if PFC configuration has changed */
9345 	if (memcmp(&new_cfg->pfc,
9346 		   &old_cfg->pfc,
9347 		   sizeof(new_cfg->pfc))) {
9348 		need_reconfig = true;
9349 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9350 	}
9351 
9352 	/* Check if APP Table has changed */
9353 	if (memcmp(&new_cfg->app,
9354 		   &old_cfg->app,
9355 		   sizeof(new_cfg->app))) {
9356 		need_reconfig = true;
9357 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9358 	}
9359 
9360 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9361 	return need_reconfig;
9362 }
9363 
9364 /**
9365  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9366  * @pf: board private structure
9367  * @e: event info posted on ARQ
9368  **/
9369 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9370 				  struct i40e_arq_event_info *e)
9371 {
9372 	struct i40e_aqc_lldp_get_mib *mib =
9373 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9374 	struct i40e_hw *hw = &pf->hw;
9375 	struct i40e_dcbx_config tmp_dcbx_cfg;
9376 	bool need_reconfig = false;
9377 	int ret = 0;
9378 	u8 type;
9379 
9380 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9381 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9382 	    (hw->phy.link_info.link_speed &
9383 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9384 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9385 		/* let firmware decide if the DCB should be disabled */
9386 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9387 
9388 	/* Not DCB capable or capability disabled */
9389 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9390 		return ret;
9391 
9392 	/* Ignore if event is not for Nearest Bridge */
9393 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9394 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9395 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9396 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9397 		return ret;
9398 
9399 	/* Check MIB Type and return if event for Remote MIB update */
9400 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9401 	dev_dbg(&pf->pdev->dev,
9402 		"LLDP event mib type %s\n", type ? "remote" : "local");
9403 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9404 		/* Update the remote cached instance and return */
9405 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9406 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9407 				&hw->remote_dcbx_config);
9408 		goto exit;
9409 	}
9410 
9411 	/* Store the old configuration */
9412 	tmp_dcbx_cfg = hw->local_dcbx_config;
9413 
9414 	/* Reset the old DCBx configuration data */
9415 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9416 	/* Get updated DCBX data from firmware */
9417 	ret = i40e_get_dcb_config(&pf->hw);
9418 	if (ret) {
9419 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9420 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9421 		    (hw->phy.link_info.link_speed &
9422 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9423 			dev_warn(&pf->pdev->dev,
9424 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9425 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9426 		} else {
9427 			dev_info(&pf->pdev->dev,
9428 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9429 				 i40e_stat_str(&pf->hw, ret),
9430 				 i40e_aq_str(&pf->hw,
9431 					     pf->hw.aq.asq_last_status));
9432 		}
9433 		goto exit;
9434 	}
9435 
9436 	/* No change detected in DCBX configs */
9437 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9438 		    sizeof(tmp_dcbx_cfg))) {
9439 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9440 		goto exit;
9441 	}
9442 
9443 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9444 					       &hw->local_dcbx_config);
9445 
9446 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9447 
9448 	if (!need_reconfig)
9449 		goto exit;
9450 
9451 	/* Enable DCB tagging only when more than one TC */
9452 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9453 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9454 	else
9455 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9456 
9457 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9458 	/* Reconfiguration needed quiesce all VSIs */
9459 	i40e_pf_quiesce_all_vsi(pf);
9460 
9461 	/* Changes in configuration update VEB/VSI */
9462 	i40e_dcb_reconfigure(pf);
9463 
9464 	ret = i40e_resume_port_tx(pf);
9465 
9466 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9467 	/* In case of error no point in resuming VSIs */
9468 	if (ret)
9469 		goto exit;
9470 
9471 	/* Wait for the PF's queues to be disabled */
9472 	ret = i40e_pf_wait_queues_disabled(pf);
9473 	if (ret) {
9474 		/* Schedule PF reset to recover */
9475 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9476 		i40e_service_event_schedule(pf);
9477 	} else {
9478 		i40e_pf_unquiesce_all_vsi(pf);
9479 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9480 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9481 	}
9482 
9483 exit:
9484 	return ret;
9485 }
9486 #endif /* CONFIG_I40E_DCB */
9487 
9488 /**
9489  * i40e_do_reset_safe - Protected reset path for userland calls.
9490  * @pf: board private structure
9491  * @reset_flags: which reset is requested
9492  *
9493  **/
9494 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9495 {
9496 	rtnl_lock();
9497 	i40e_do_reset(pf, reset_flags, true);
9498 	rtnl_unlock();
9499 }
9500 
9501 /**
9502  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9503  * @pf: board private structure
9504  * @e: event info posted on ARQ
9505  *
9506  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9507  * and VF queues
9508  **/
9509 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9510 					   struct i40e_arq_event_info *e)
9511 {
9512 	struct i40e_aqc_lan_overflow *data =
9513 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9514 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9515 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9516 	struct i40e_hw *hw = &pf->hw;
9517 	struct i40e_vf *vf;
9518 	u16 vf_id;
9519 
9520 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9521 		queue, qtx_ctl);
9522 
9523 	/* Queue belongs to VF, find the VF and issue VF reset */
9524 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9525 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9526 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9527 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9528 		vf_id -= hw->func_caps.vf_base_id;
9529 		vf = &pf->vf[vf_id];
9530 		i40e_vc_notify_vf_reset(vf);
9531 		/* Allow VF to process pending reset notification */
9532 		msleep(20);
9533 		i40e_reset_vf(vf, false);
9534 	}
9535 }
9536 
9537 /**
9538  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9539  * @pf: board private structure
9540  **/
9541 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9542 {
9543 	u32 val, fcnt_prog;
9544 
9545 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9546 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9547 	return fcnt_prog;
9548 }
9549 
9550 /**
9551  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9552  * @pf: board private structure
9553  **/
9554 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9555 {
9556 	u32 val, fcnt_prog;
9557 
9558 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9559 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9560 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9561 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9562 	return fcnt_prog;
9563 }
9564 
9565 /**
9566  * i40e_get_global_fd_count - Get total FD filters programmed on device
9567  * @pf: board private structure
9568  **/
9569 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9570 {
9571 	u32 val, fcnt_prog;
9572 
9573 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9574 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9575 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9576 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9577 	return fcnt_prog;
9578 }
9579 
9580 /**
9581  * i40e_reenable_fdir_sb - Restore FDir SB capability
9582  * @pf: board private structure
9583  **/
9584 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9585 {
9586 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9587 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9588 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9589 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9590 }
9591 
9592 /**
9593  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9594  * @pf: board private structure
9595  **/
9596 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9597 {
9598 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9599 		/* ATR uses the same filtering logic as SB rules. It only
9600 		 * functions properly if the input set mask is at the default
9601 		 * settings. It is safe to restore the default input set
9602 		 * because there are no active TCPv4 filter rules.
9603 		 */
9604 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9605 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9606 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9607 
9608 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9609 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9610 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9611 	}
9612 }
9613 
9614 /**
9615  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9616  * @pf: board private structure
9617  * @filter: FDir filter to remove
9618  */
9619 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9620 				       struct i40e_fdir_filter *filter)
9621 {
9622 	/* Update counters */
9623 	pf->fdir_pf_active_filters--;
9624 	pf->fd_inv = 0;
9625 
9626 	switch (filter->flow_type) {
9627 	case TCP_V4_FLOW:
9628 		pf->fd_tcp4_filter_cnt--;
9629 		break;
9630 	case UDP_V4_FLOW:
9631 		pf->fd_udp4_filter_cnt--;
9632 		break;
9633 	case SCTP_V4_FLOW:
9634 		pf->fd_sctp4_filter_cnt--;
9635 		break;
9636 	case TCP_V6_FLOW:
9637 		pf->fd_tcp6_filter_cnt--;
9638 		break;
9639 	case UDP_V6_FLOW:
9640 		pf->fd_udp6_filter_cnt--;
9641 		break;
9642 	case SCTP_V6_FLOW:
9643 		pf->fd_udp6_filter_cnt--;
9644 		break;
9645 	case IP_USER_FLOW:
9646 		switch (filter->ipl4_proto) {
9647 		case IPPROTO_TCP:
9648 			pf->fd_tcp4_filter_cnt--;
9649 			break;
9650 		case IPPROTO_UDP:
9651 			pf->fd_udp4_filter_cnt--;
9652 			break;
9653 		case IPPROTO_SCTP:
9654 			pf->fd_sctp4_filter_cnt--;
9655 			break;
9656 		case IPPROTO_IP:
9657 			pf->fd_ip4_filter_cnt--;
9658 			break;
9659 		}
9660 		break;
9661 	case IPV6_USER_FLOW:
9662 		switch (filter->ipl4_proto) {
9663 		case IPPROTO_TCP:
9664 			pf->fd_tcp6_filter_cnt--;
9665 			break;
9666 		case IPPROTO_UDP:
9667 			pf->fd_udp6_filter_cnt--;
9668 			break;
9669 		case IPPROTO_SCTP:
9670 			pf->fd_sctp6_filter_cnt--;
9671 			break;
9672 		case IPPROTO_IP:
9673 			pf->fd_ip6_filter_cnt--;
9674 			break;
9675 		}
9676 		break;
9677 	}
9678 
9679 	/* Remove the filter from the list and free memory */
9680 	hlist_del(&filter->fdir_node);
9681 	kfree(filter);
9682 }
9683 
9684 /**
9685  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9686  * @pf: board private structure
9687  **/
9688 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9689 {
9690 	struct i40e_fdir_filter *filter;
9691 	u32 fcnt_prog, fcnt_avail;
9692 	struct hlist_node *node;
9693 
9694 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9695 		return;
9696 
9697 	/* Check if we have enough room to re-enable FDir SB capability. */
9698 	fcnt_prog = i40e_get_global_fd_count(pf);
9699 	fcnt_avail = pf->fdir_pf_filter_count;
9700 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9701 	    (pf->fd_add_err == 0) ||
9702 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9703 		i40e_reenable_fdir_sb(pf);
9704 
9705 	/* We should wait for even more space before re-enabling ATR.
9706 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9707 	 * rules active.
9708 	 */
9709 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9710 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9711 		i40e_reenable_fdir_atr(pf);
9712 
9713 	/* if hw had a problem adding a filter, delete it */
9714 	if (pf->fd_inv > 0) {
9715 		hlist_for_each_entry_safe(filter, node,
9716 					  &pf->fdir_filter_list, fdir_node)
9717 			if (filter->fd_id == pf->fd_inv)
9718 				i40e_delete_invalid_filter(pf, filter);
9719 	}
9720 }
9721 
9722 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9723 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9724 /**
9725  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9726  * @pf: board private structure
9727  **/
9728 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9729 {
9730 	unsigned long min_flush_time;
9731 	int flush_wait_retry = 50;
9732 	bool disable_atr = false;
9733 	int fd_room;
9734 	int reg;
9735 
9736 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9737 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9738 		return;
9739 
9740 	/* If the flush is happening too quick and we have mostly SB rules we
9741 	 * should not re-enable ATR for some time.
9742 	 */
9743 	min_flush_time = pf->fd_flush_timestamp +
9744 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9745 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9746 
9747 	if (!(time_after(jiffies, min_flush_time)) &&
9748 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9749 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9750 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9751 		disable_atr = true;
9752 	}
9753 
9754 	pf->fd_flush_timestamp = jiffies;
9755 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9756 	/* flush all filters */
9757 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9758 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9759 	i40e_flush(&pf->hw);
9760 	pf->fd_flush_cnt++;
9761 	pf->fd_add_err = 0;
9762 	do {
9763 		/* Check FD flush status every 5-6msec */
9764 		usleep_range(5000, 6000);
9765 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9766 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9767 			break;
9768 	} while (flush_wait_retry--);
9769 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9770 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9771 	} else {
9772 		/* replay sideband filters */
9773 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9774 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9775 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9776 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9777 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9778 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9779 	}
9780 }
9781 
9782 /**
9783  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9784  * @pf: board private structure
9785  **/
9786 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9787 {
9788 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9789 }
9790 
9791 /**
9792  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9793  * @pf: board private structure
9794  **/
9795 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9796 {
9797 
9798 	/* if interface is down do nothing */
9799 	if (test_bit(__I40E_DOWN, pf->state))
9800 		return;
9801 
9802 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9803 		i40e_fdir_flush_and_replay(pf);
9804 
9805 	i40e_fdir_check_and_reenable(pf);
9806 
9807 }
9808 
9809 /**
9810  * i40e_vsi_link_event - notify VSI of a link event
9811  * @vsi: vsi to be notified
9812  * @link_up: link up or down
9813  **/
9814 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9815 {
9816 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9817 		return;
9818 
9819 	switch (vsi->type) {
9820 	case I40E_VSI_MAIN:
9821 		if (!vsi->netdev || !vsi->netdev_registered)
9822 			break;
9823 
9824 		if (link_up) {
9825 			netif_carrier_on(vsi->netdev);
9826 			netif_tx_wake_all_queues(vsi->netdev);
9827 		} else {
9828 			netif_carrier_off(vsi->netdev);
9829 			netif_tx_stop_all_queues(vsi->netdev);
9830 		}
9831 		break;
9832 
9833 	case I40E_VSI_SRIOV:
9834 	case I40E_VSI_VMDQ2:
9835 	case I40E_VSI_CTRL:
9836 	case I40E_VSI_IWARP:
9837 	case I40E_VSI_MIRROR:
9838 	default:
9839 		/* there is no notification for other VSIs */
9840 		break;
9841 	}
9842 }
9843 
9844 /**
9845  * i40e_veb_link_event - notify elements on the veb of a link event
9846  * @veb: veb to be notified
9847  * @link_up: link up or down
9848  **/
9849 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9850 {
9851 	struct i40e_pf *pf;
9852 	int i;
9853 
9854 	if (!veb || !veb->pf)
9855 		return;
9856 	pf = veb->pf;
9857 
9858 	/* depth first... */
9859 	for (i = 0; i < I40E_MAX_VEB; i++)
9860 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9861 			i40e_veb_link_event(pf->veb[i], link_up);
9862 
9863 	/* ... now the local VSIs */
9864 	for (i = 0; i < pf->num_alloc_vsi; i++)
9865 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9866 			i40e_vsi_link_event(pf->vsi[i], link_up);
9867 }
9868 
9869 /**
9870  * i40e_link_event - Update netif_carrier status
9871  * @pf: board private structure
9872  **/
9873 static void i40e_link_event(struct i40e_pf *pf)
9874 {
9875 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9876 	u8 new_link_speed, old_link_speed;
9877 	i40e_status status;
9878 	bool new_link, old_link;
9879 #ifdef CONFIG_I40E_DCB
9880 	int err;
9881 #endif /* CONFIG_I40E_DCB */
9882 
9883 	/* set this to force the get_link_status call to refresh state */
9884 	pf->hw.phy.get_link_info = true;
9885 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9886 	status = i40e_get_link_status(&pf->hw, &new_link);
9887 
9888 	/* On success, disable temp link polling */
9889 	if (status == I40E_SUCCESS) {
9890 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9891 	} else {
9892 		/* Enable link polling temporarily until i40e_get_link_status
9893 		 * returns I40E_SUCCESS
9894 		 */
9895 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9896 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9897 			status);
9898 		return;
9899 	}
9900 
9901 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9902 	new_link_speed = pf->hw.phy.link_info.link_speed;
9903 
9904 	if (new_link == old_link &&
9905 	    new_link_speed == old_link_speed &&
9906 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9907 	     new_link == netif_carrier_ok(vsi->netdev)))
9908 		return;
9909 
9910 	i40e_print_link_message(vsi, new_link);
9911 
9912 	/* Notify the base of the switch tree connected to
9913 	 * the link.  Floating VEBs are not notified.
9914 	 */
9915 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9916 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9917 	else
9918 		i40e_vsi_link_event(vsi, new_link);
9919 
9920 	if (pf->vf)
9921 		i40e_vc_notify_link_state(pf);
9922 
9923 	if (pf->flags & I40E_FLAG_PTP)
9924 		i40e_ptp_set_increment(pf);
9925 #ifdef CONFIG_I40E_DCB
9926 	if (new_link == old_link)
9927 		return;
9928 	/* Not SW DCB so firmware will take care of default settings */
9929 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9930 		return;
9931 
9932 	/* We cover here only link down, as after link up in case of SW DCB
9933 	 * SW LLDP agent will take care of setting it up
9934 	 */
9935 	if (!new_link) {
9936 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9937 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9938 		err = i40e_dcb_sw_default_config(pf);
9939 		if (err) {
9940 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9941 				       I40E_FLAG_DCB_ENABLED);
9942 		} else {
9943 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9944 				       DCB_CAP_DCBX_VER_IEEE;
9945 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9946 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9947 		}
9948 	}
9949 #endif /* CONFIG_I40E_DCB */
9950 }
9951 
9952 /**
9953  * i40e_watchdog_subtask - periodic checks not using event driven response
9954  * @pf: board private structure
9955  **/
9956 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9957 {
9958 	int i;
9959 
9960 	/* if interface is down do nothing */
9961 	if (test_bit(__I40E_DOWN, pf->state) ||
9962 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9963 		return;
9964 
9965 	/* make sure we don't do these things too often */
9966 	if (time_before(jiffies, (pf->service_timer_previous +
9967 				  pf->service_timer_period)))
9968 		return;
9969 	pf->service_timer_previous = jiffies;
9970 
9971 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9972 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9973 		i40e_link_event(pf);
9974 
9975 	/* Update the stats for active netdevs so the network stack
9976 	 * can look at updated numbers whenever it cares to
9977 	 */
9978 	for (i = 0; i < pf->num_alloc_vsi; i++)
9979 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9980 			i40e_update_stats(pf->vsi[i]);
9981 
9982 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9983 		/* Update the stats for the active switching components */
9984 		for (i = 0; i < I40E_MAX_VEB; i++)
9985 			if (pf->veb[i])
9986 				i40e_update_veb_stats(pf->veb[i]);
9987 	}
9988 
9989 	i40e_ptp_rx_hang(pf);
9990 	i40e_ptp_tx_hang(pf);
9991 }
9992 
9993 /**
9994  * i40e_reset_subtask - Set up for resetting the device and driver
9995  * @pf: board private structure
9996  **/
9997 static void i40e_reset_subtask(struct i40e_pf *pf)
9998 {
9999 	u32 reset_flags = 0;
10000 
10001 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10002 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10003 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10004 	}
10005 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10006 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10007 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10008 	}
10009 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10010 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10011 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10012 	}
10013 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10014 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10015 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10016 	}
10017 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10018 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10019 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10020 	}
10021 
10022 	/* If there's a recovery already waiting, it takes
10023 	 * precedence before starting a new reset sequence.
10024 	 */
10025 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10026 		i40e_prep_for_reset(pf);
10027 		i40e_reset(pf);
10028 		i40e_rebuild(pf, false, false);
10029 	}
10030 
10031 	/* If we're already down or resetting, just bail */
10032 	if (reset_flags &&
10033 	    !test_bit(__I40E_DOWN, pf->state) &&
10034 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10035 		i40e_do_reset(pf, reset_flags, false);
10036 	}
10037 }
10038 
10039 /**
10040  * i40e_handle_link_event - Handle link event
10041  * @pf: board private structure
10042  * @e: event info posted on ARQ
10043  **/
10044 static void i40e_handle_link_event(struct i40e_pf *pf,
10045 				   struct i40e_arq_event_info *e)
10046 {
10047 	struct i40e_aqc_get_link_status *status =
10048 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10049 
10050 	/* Do a new status request to re-enable LSE reporting
10051 	 * and load new status information into the hw struct
10052 	 * This completely ignores any state information
10053 	 * in the ARQ event info, instead choosing to always
10054 	 * issue the AQ update link status command.
10055 	 */
10056 	i40e_link_event(pf);
10057 
10058 	/* Check if module meets thermal requirements */
10059 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10060 		dev_err(&pf->pdev->dev,
10061 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10062 		dev_err(&pf->pdev->dev,
10063 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10064 	} else {
10065 		/* check for unqualified module, if link is down, suppress
10066 		 * the message if link was forced to be down.
10067 		 */
10068 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10069 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10070 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10071 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10072 			dev_err(&pf->pdev->dev,
10073 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10074 			dev_err(&pf->pdev->dev,
10075 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10076 		}
10077 	}
10078 }
10079 
10080 /**
10081  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10082  * @pf: board private structure
10083  **/
10084 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10085 {
10086 	struct i40e_arq_event_info event;
10087 	struct i40e_hw *hw = &pf->hw;
10088 	u16 pending, i = 0;
10089 	i40e_status ret;
10090 	u16 opcode;
10091 	u32 oldval;
10092 	u32 val;
10093 
10094 	/* Do not run clean AQ when PF reset fails */
10095 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10096 		return;
10097 
10098 	/* check for error indications */
10099 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10100 	oldval = val;
10101 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10102 		if (hw->debug_mask & I40E_DEBUG_AQ)
10103 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10104 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10105 	}
10106 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10107 		if (hw->debug_mask & I40E_DEBUG_AQ)
10108 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10109 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10110 		pf->arq_overflows++;
10111 	}
10112 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10113 		if (hw->debug_mask & I40E_DEBUG_AQ)
10114 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10115 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10116 	}
10117 	if (oldval != val)
10118 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10119 
10120 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10121 	oldval = val;
10122 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10123 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10124 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10125 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10126 	}
10127 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10128 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10129 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10130 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10131 	}
10132 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10133 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10134 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10135 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10136 	}
10137 	if (oldval != val)
10138 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10139 
10140 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10141 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10142 	if (!event.msg_buf)
10143 		return;
10144 
10145 	do {
10146 		ret = i40e_clean_arq_element(hw, &event, &pending);
10147 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10148 			break;
10149 		else if (ret) {
10150 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10151 			break;
10152 		}
10153 
10154 		opcode = le16_to_cpu(event.desc.opcode);
10155 		switch (opcode) {
10156 
10157 		case i40e_aqc_opc_get_link_status:
10158 			rtnl_lock();
10159 			i40e_handle_link_event(pf, &event);
10160 			rtnl_unlock();
10161 			break;
10162 		case i40e_aqc_opc_send_msg_to_pf:
10163 			ret = i40e_vc_process_vf_msg(pf,
10164 					le16_to_cpu(event.desc.retval),
10165 					le32_to_cpu(event.desc.cookie_high),
10166 					le32_to_cpu(event.desc.cookie_low),
10167 					event.msg_buf,
10168 					event.msg_len);
10169 			break;
10170 		case i40e_aqc_opc_lldp_update_mib:
10171 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10172 #ifdef CONFIG_I40E_DCB
10173 			rtnl_lock();
10174 			i40e_handle_lldp_event(pf, &event);
10175 			rtnl_unlock();
10176 #endif /* CONFIG_I40E_DCB */
10177 			break;
10178 		case i40e_aqc_opc_event_lan_overflow:
10179 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10180 			i40e_handle_lan_overflow_event(pf, &event);
10181 			break;
10182 		case i40e_aqc_opc_send_msg_to_peer:
10183 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10184 			break;
10185 		case i40e_aqc_opc_nvm_erase:
10186 		case i40e_aqc_opc_nvm_update:
10187 		case i40e_aqc_opc_oem_post_update:
10188 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10189 				   "ARQ NVM operation 0x%04x completed\n",
10190 				   opcode);
10191 			break;
10192 		default:
10193 			dev_info(&pf->pdev->dev,
10194 				 "ARQ: Unknown event 0x%04x ignored\n",
10195 				 opcode);
10196 			break;
10197 		}
10198 	} while (i++ < pf->adminq_work_limit);
10199 
10200 	if (i < pf->adminq_work_limit)
10201 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10202 
10203 	/* re-enable Admin queue interrupt cause */
10204 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10205 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10206 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10207 	i40e_flush(hw);
10208 
10209 	kfree(event.msg_buf);
10210 }
10211 
10212 /**
10213  * i40e_verify_eeprom - make sure eeprom is good to use
10214  * @pf: board private structure
10215  **/
10216 static void i40e_verify_eeprom(struct i40e_pf *pf)
10217 {
10218 	int err;
10219 
10220 	err = i40e_diag_eeprom_test(&pf->hw);
10221 	if (err) {
10222 		/* retry in case of garbage read */
10223 		err = i40e_diag_eeprom_test(&pf->hw);
10224 		if (err) {
10225 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10226 				 err);
10227 			set_bit(__I40E_BAD_EEPROM, pf->state);
10228 		}
10229 	}
10230 
10231 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10232 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10233 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10234 	}
10235 }
10236 
10237 /**
10238  * i40e_enable_pf_switch_lb
10239  * @pf: pointer to the PF structure
10240  *
10241  * enable switch loop back or die - no point in a return value
10242  **/
10243 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10244 {
10245 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10246 	struct i40e_vsi_context ctxt;
10247 	int ret;
10248 
10249 	ctxt.seid = pf->main_vsi_seid;
10250 	ctxt.pf_num = pf->hw.pf_id;
10251 	ctxt.vf_num = 0;
10252 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10253 	if (ret) {
10254 		dev_info(&pf->pdev->dev,
10255 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10256 			 i40e_stat_str(&pf->hw, ret),
10257 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10258 		return;
10259 	}
10260 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10261 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10262 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10263 
10264 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10265 	if (ret) {
10266 		dev_info(&pf->pdev->dev,
10267 			 "update vsi switch failed, err %s aq_err %s\n",
10268 			 i40e_stat_str(&pf->hw, ret),
10269 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10270 	}
10271 }
10272 
10273 /**
10274  * i40e_disable_pf_switch_lb
10275  * @pf: pointer to the PF structure
10276  *
10277  * disable switch loop back or die - no point in a return value
10278  **/
10279 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10280 {
10281 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10282 	struct i40e_vsi_context ctxt;
10283 	int ret;
10284 
10285 	ctxt.seid = pf->main_vsi_seid;
10286 	ctxt.pf_num = pf->hw.pf_id;
10287 	ctxt.vf_num = 0;
10288 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10289 	if (ret) {
10290 		dev_info(&pf->pdev->dev,
10291 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10292 			 i40e_stat_str(&pf->hw, ret),
10293 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10294 		return;
10295 	}
10296 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10297 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10298 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10299 
10300 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10301 	if (ret) {
10302 		dev_info(&pf->pdev->dev,
10303 			 "update vsi switch failed, err %s aq_err %s\n",
10304 			 i40e_stat_str(&pf->hw, ret),
10305 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10306 	}
10307 }
10308 
10309 /**
10310  * i40e_config_bridge_mode - Configure the HW bridge mode
10311  * @veb: pointer to the bridge instance
10312  *
10313  * Configure the loop back mode for the LAN VSI that is downlink to the
10314  * specified HW bridge instance. It is expected this function is called
10315  * when a new HW bridge is instantiated.
10316  **/
10317 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10318 {
10319 	struct i40e_pf *pf = veb->pf;
10320 
10321 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10322 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10323 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10324 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10325 		i40e_disable_pf_switch_lb(pf);
10326 	else
10327 		i40e_enable_pf_switch_lb(pf);
10328 }
10329 
10330 /**
10331  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10332  * @veb: pointer to the VEB instance
10333  *
10334  * This is a recursive function that first builds the attached VSIs then
10335  * recurses in to build the next layer of VEB.  We track the connections
10336  * through our own index numbers because the seid's from the HW could
10337  * change across the reset.
10338  **/
10339 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10340 {
10341 	struct i40e_vsi *ctl_vsi = NULL;
10342 	struct i40e_pf *pf = veb->pf;
10343 	int v, veb_idx;
10344 	int ret;
10345 
10346 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10347 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10348 		if (pf->vsi[v] &&
10349 		    pf->vsi[v]->veb_idx == veb->idx &&
10350 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10351 			ctl_vsi = pf->vsi[v];
10352 			break;
10353 		}
10354 	}
10355 	if (!ctl_vsi) {
10356 		dev_info(&pf->pdev->dev,
10357 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10358 		ret = -ENOENT;
10359 		goto end_reconstitute;
10360 	}
10361 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10362 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10363 	ret = i40e_add_vsi(ctl_vsi);
10364 	if (ret) {
10365 		dev_info(&pf->pdev->dev,
10366 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10367 			 veb->idx, ret);
10368 		goto end_reconstitute;
10369 	}
10370 	i40e_vsi_reset_stats(ctl_vsi);
10371 
10372 	/* create the VEB in the switch and move the VSI onto the VEB */
10373 	ret = i40e_add_veb(veb, ctl_vsi);
10374 	if (ret)
10375 		goto end_reconstitute;
10376 
10377 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10378 		veb->bridge_mode = BRIDGE_MODE_VEB;
10379 	else
10380 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10381 	i40e_config_bridge_mode(veb);
10382 
10383 	/* create the remaining VSIs attached to this VEB */
10384 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10385 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10386 			continue;
10387 
10388 		if (pf->vsi[v]->veb_idx == veb->idx) {
10389 			struct i40e_vsi *vsi = pf->vsi[v];
10390 
10391 			vsi->uplink_seid = veb->seid;
10392 			ret = i40e_add_vsi(vsi);
10393 			if (ret) {
10394 				dev_info(&pf->pdev->dev,
10395 					 "rebuild of vsi_idx %d failed: %d\n",
10396 					 v, ret);
10397 				goto end_reconstitute;
10398 			}
10399 			i40e_vsi_reset_stats(vsi);
10400 		}
10401 	}
10402 
10403 	/* create any VEBs attached to this VEB - RECURSION */
10404 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10405 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10406 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10407 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10408 			if (ret)
10409 				break;
10410 		}
10411 	}
10412 
10413 end_reconstitute:
10414 	return ret;
10415 }
10416 
10417 /**
10418  * i40e_get_capabilities - get info about the HW
10419  * @pf: the PF struct
10420  * @list_type: AQ capability to be queried
10421  **/
10422 static int i40e_get_capabilities(struct i40e_pf *pf,
10423 				 enum i40e_admin_queue_opc list_type)
10424 {
10425 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10426 	u16 data_size;
10427 	int buf_len;
10428 	int err;
10429 
10430 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10431 	do {
10432 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10433 		if (!cap_buf)
10434 			return -ENOMEM;
10435 
10436 		/* this loads the data into the hw struct for us */
10437 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10438 						    &data_size, list_type,
10439 						    NULL);
10440 		/* data loaded, buffer no longer needed */
10441 		kfree(cap_buf);
10442 
10443 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10444 			/* retry with a larger buffer */
10445 			buf_len = data_size;
10446 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10447 			dev_info(&pf->pdev->dev,
10448 				 "capability discovery failed, err %s aq_err %s\n",
10449 				 i40e_stat_str(&pf->hw, err),
10450 				 i40e_aq_str(&pf->hw,
10451 					     pf->hw.aq.asq_last_status));
10452 			return -ENODEV;
10453 		}
10454 	} while (err);
10455 
10456 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10457 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10458 			dev_info(&pf->pdev->dev,
10459 				 "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",
10460 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10461 				 pf->hw.func_caps.num_msix_vectors,
10462 				 pf->hw.func_caps.num_msix_vectors_vf,
10463 				 pf->hw.func_caps.fd_filters_guaranteed,
10464 				 pf->hw.func_caps.fd_filters_best_effort,
10465 				 pf->hw.func_caps.num_tx_qp,
10466 				 pf->hw.func_caps.num_vsis);
10467 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10468 			dev_info(&pf->pdev->dev,
10469 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10470 				 pf->hw.dev_caps.switch_mode,
10471 				 pf->hw.dev_caps.valid_functions);
10472 			dev_info(&pf->pdev->dev,
10473 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10474 				 pf->hw.dev_caps.sr_iov_1_1,
10475 				 pf->hw.dev_caps.num_vfs);
10476 			dev_info(&pf->pdev->dev,
10477 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10478 				 pf->hw.dev_caps.num_vsis,
10479 				 pf->hw.dev_caps.num_rx_qp,
10480 				 pf->hw.dev_caps.num_tx_qp);
10481 		}
10482 	}
10483 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10484 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10485 		       + pf->hw.func_caps.num_vfs)
10486 		if (pf->hw.revision_id == 0 &&
10487 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10488 			dev_info(&pf->pdev->dev,
10489 				 "got num_vsis %d, setting num_vsis to %d\n",
10490 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10491 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10492 		}
10493 	}
10494 	return 0;
10495 }
10496 
10497 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10498 
10499 /**
10500  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10501  * @pf: board private structure
10502  **/
10503 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10504 {
10505 	struct i40e_vsi *vsi;
10506 
10507 	/* quick workaround for an NVM issue that leaves a critical register
10508 	 * uninitialized
10509 	 */
10510 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10511 		static const u32 hkey[] = {
10512 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10513 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10514 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10515 			0x95b3a76d};
10516 		int i;
10517 
10518 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10519 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10520 	}
10521 
10522 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10523 		return;
10524 
10525 	/* find existing VSI and see if it needs configuring */
10526 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10527 
10528 	/* create a new VSI if none exists */
10529 	if (!vsi) {
10530 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10531 				     pf->vsi[pf->lan_vsi]->seid, 0);
10532 		if (!vsi) {
10533 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10534 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10535 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10536 			return;
10537 		}
10538 	}
10539 
10540 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10541 }
10542 
10543 /**
10544  * i40e_fdir_teardown - release the Flow Director resources
10545  * @pf: board private structure
10546  **/
10547 static void i40e_fdir_teardown(struct i40e_pf *pf)
10548 {
10549 	struct i40e_vsi *vsi;
10550 
10551 	i40e_fdir_filter_exit(pf);
10552 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10553 	if (vsi)
10554 		i40e_vsi_release(vsi);
10555 }
10556 
10557 /**
10558  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10559  * @vsi: PF main vsi
10560  * @seid: seid of main or channel VSIs
10561  *
10562  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10563  * existed before reset
10564  **/
10565 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10566 {
10567 	struct i40e_cloud_filter *cfilter;
10568 	struct i40e_pf *pf = vsi->back;
10569 	struct hlist_node *node;
10570 	i40e_status ret;
10571 
10572 	/* Add cloud filters back if they exist */
10573 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10574 				  cloud_node) {
10575 		if (cfilter->seid != seid)
10576 			continue;
10577 
10578 		if (cfilter->dst_port)
10579 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10580 								true);
10581 		else
10582 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10583 
10584 		if (ret) {
10585 			dev_dbg(&pf->pdev->dev,
10586 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10587 				i40e_stat_str(&pf->hw, ret),
10588 				i40e_aq_str(&pf->hw,
10589 					    pf->hw.aq.asq_last_status));
10590 			return ret;
10591 		}
10592 	}
10593 	return 0;
10594 }
10595 
10596 /**
10597  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10598  * @vsi: PF main vsi
10599  *
10600  * Rebuilds channel VSIs if they existed before reset
10601  **/
10602 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10603 {
10604 	struct i40e_channel *ch, *ch_tmp;
10605 	i40e_status ret;
10606 
10607 	if (list_empty(&vsi->ch_list))
10608 		return 0;
10609 
10610 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10611 		if (!ch->initialized)
10612 			break;
10613 		/* Proceed with creation of channel (VMDq2) VSI */
10614 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10615 		if (ret) {
10616 			dev_info(&vsi->back->pdev->dev,
10617 				 "failed to rebuild channels using uplink_seid %u\n",
10618 				 vsi->uplink_seid);
10619 			return ret;
10620 		}
10621 		/* Reconfigure TX queues using QTX_CTL register */
10622 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10623 		if (ret) {
10624 			dev_info(&vsi->back->pdev->dev,
10625 				 "failed to configure TX rings for channel %u\n",
10626 				 ch->seid);
10627 			return ret;
10628 		}
10629 		/* update 'next_base_queue' */
10630 		vsi->next_base_queue = vsi->next_base_queue +
10631 							ch->num_queue_pairs;
10632 		if (ch->max_tx_rate) {
10633 			u64 credits = ch->max_tx_rate;
10634 
10635 			if (i40e_set_bw_limit(vsi, ch->seid,
10636 					      ch->max_tx_rate))
10637 				return -EINVAL;
10638 
10639 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10640 			dev_dbg(&vsi->back->pdev->dev,
10641 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10642 				ch->max_tx_rate,
10643 				credits,
10644 				ch->seid);
10645 		}
10646 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10647 		if (ret) {
10648 			dev_dbg(&vsi->back->pdev->dev,
10649 				"Failed to rebuild cloud filters for channel VSI %u\n",
10650 				ch->seid);
10651 			return ret;
10652 		}
10653 	}
10654 	return 0;
10655 }
10656 
10657 /**
10658  * i40e_prep_for_reset - prep for the core to reset
10659  * @pf: board private structure
10660  *
10661  * Close up the VFs and other things in prep for PF Reset.
10662   **/
10663 static void i40e_prep_for_reset(struct i40e_pf *pf)
10664 {
10665 	struct i40e_hw *hw = &pf->hw;
10666 	i40e_status ret = 0;
10667 	u32 v;
10668 
10669 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10670 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10671 		return;
10672 	if (i40e_check_asq_alive(&pf->hw))
10673 		i40e_vc_notify_reset(pf);
10674 
10675 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10676 
10677 	/* quiesce the VSIs and their queues that are not already DOWN */
10678 	i40e_pf_quiesce_all_vsi(pf);
10679 
10680 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10681 		if (pf->vsi[v])
10682 			pf->vsi[v]->seid = 0;
10683 	}
10684 
10685 	i40e_shutdown_adminq(&pf->hw);
10686 
10687 	/* call shutdown HMC */
10688 	if (hw->hmc.hmc_obj) {
10689 		ret = i40e_shutdown_lan_hmc(hw);
10690 		if (ret)
10691 			dev_warn(&pf->pdev->dev,
10692 				 "shutdown_lan_hmc failed: %d\n", ret);
10693 	}
10694 
10695 	/* Save the current PTP time so that we can restore the time after the
10696 	 * reset completes.
10697 	 */
10698 	i40e_ptp_save_hw_time(pf);
10699 }
10700 
10701 /**
10702  * i40e_send_version - update firmware with driver version
10703  * @pf: PF struct
10704  */
10705 static void i40e_send_version(struct i40e_pf *pf)
10706 {
10707 	struct i40e_driver_version dv;
10708 
10709 	dv.major_version = 0xff;
10710 	dv.minor_version = 0xff;
10711 	dv.build_version = 0xff;
10712 	dv.subbuild_version = 0;
10713 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10714 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10715 }
10716 
10717 /**
10718  * i40e_get_oem_version - get OEM specific version information
10719  * @hw: pointer to the hardware structure
10720  **/
10721 static void i40e_get_oem_version(struct i40e_hw *hw)
10722 {
10723 	u16 block_offset = 0xffff;
10724 	u16 block_length = 0;
10725 	u16 capabilities = 0;
10726 	u16 gen_snap = 0;
10727 	u16 release = 0;
10728 
10729 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10730 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10731 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10732 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10733 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10734 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10735 #define I40E_NVM_OEM_LENGTH			3
10736 
10737 	/* Check if pointer to OEM version block is valid. */
10738 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10739 	if (block_offset == 0xffff)
10740 		return;
10741 
10742 	/* Check if OEM version block has correct length. */
10743 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10744 			   &block_length);
10745 	if (block_length < I40E_NVM_OEM_LENGTH)
10746 		return;
10747 
10748 	/* Check if OEM version format is as expected. */
10749 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10750 			   &capabilities);
10751 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10752 		return;
10753 
10754 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10755 			   &gen_snap);
10756 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10757 			   &release);
10758 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10759 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10760 }
10761 
10762 /**
10763  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10764  * @pf: board private structure
10765  **/
10766 static int i40e_reset(struct i40e_pf *pf)
10767 {
10768 	struct i40e_hw *hw = &pf->hw;
10769 	i40e_status ret;
10770 
10771 	ret = i40e_pf_reset(hw);
10772 	if (ret) {
10773 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10774 		set_bit(__I40E_RESET_FAILED, pf->state);
10775 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10776 	} else {
10777 		pf->pfr_count++;
10778 	}
10779 	return ret;
10780 }
10781 
10782 /**
10783  * i40e_rebuild - rebuild using a saved config
10784  * @pf: board private structure
10785  * @reinit: if the Main VSI needs to re-initialized.
10786  * @lock_acquired: indicates whether or not the lock has been acquired
10787  * before this function was called.
10788  **/
10789 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10790 {
10791 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10792 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10793 	struct i40e_hw *hw = &pf->hw;
10794 	i40e_status ret;
10795 	u32 val;
10796 	int v;
10797 
10798 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10799 	    is_recovery_mode_reported)
10800 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10801 
10802 	if (test_bit(__I40E_DOWN, pf->state) &&
10803 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10804 		goto clear_recovery;
10805 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10806 
10807 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10808 	ret = i40e_init_adminq(&pf->hw);
10809 	if (ret) {
10810 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10811 			 i40e_stat_str(&pf->hw, ret),
10812 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10813 		goto clear_recovery;
10814 	}
10815 	i40e_get_oem_version(&pf->hw);
10816 
10817 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10818 		/* The following delay is necessary for firmware update. */
10819 		mdelay(1000);
10820 	}
10821 
10822 	/* re-verify the eeprom if we just had an EMP reset */
10823 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10824 		i40e_verify_eeprom(pf);
10825 
10826 	/* if we are going out of or into recovery mode we have to act
10827 	 * accordingly with regard to resources initialization
10828 	 * and deinitialization
10829 	 */
10830 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10831 		if (i40e_get_capabilities(pf,
10832 					  i40e_aqc_opc_list_func_capabilities))
10833 			goto end_unlock;
10834 
10835 		if (is_recovery_mode_reported) {
10836 			/* we're staying in recovery mode so we'll reinitialize
10837 			 * misc vector here
10838 			 */
10839 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10840 				goto end_unlock;
10841 		} else {
10842 			if (!lock_acquired)
10843 				rtnl_lock();
10844 			/* we're going out of recovery mode so we'll free
10845 			 * the IRQ allocated specifically for recovery mode
10846 			 * and restore the interrupt scheme
10847 			 */
10848 			free_irq(pf->pdev->irq, pf);
10849 			i40e_clear_interrupt_scheme(pf);
10850 			if (i40e_restore_interrupt_scheme(pf))
10851 				goto end_unlock;
10852 		}
10853 
10854 		/* tell the firmware that we're starting */
10855 		i40e_send_version(pf);
10856 
10857 		/* bail out in case recovery mode was detected, as there is
10858 		 * no need for further configuration.
10859 		 */
10860 		goto end_unlock;
10861 	}
10862 
10863 	i40e_clear_pxe_mode(hw);
10864 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10865 	if (ret)
10866 		goto end_core_reset;
10867 
10868 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10869 				hw->func_caps.num_rx_qp, 0, 0);
10870 	if (ret) {
10871 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10872 		goto end_core_reset;
10873 	}
10874 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10875 	if (ret) {
10876 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10877 		goto end_core_reset;
10878 	}
10879 
10880 #ifdef CONFIG_I40E_DCB
10881 	/* Enable FW to write a default DCB config on link-up
10882 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10883 	 * is not supported with new link speed
10884 	 */
10885 	if (i40e_is_tc_mqprio_enabled(pf)) {
10886 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10887 	} else {
10888 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10889 		    (hw->phy.link_info.link_speed &
10890 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10891 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10892 			dev_warn(&pf->pdev->dev,
10893 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10894 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10895 		} else {
10896 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10897 			ret = i40e_init_pf_dcb(pf);
10898 			if (ret) {
10899 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10900 					 ret);
10901 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10902 				/* Continue without DCB enabled */
10903 			}
10904 		}
10905 	}
10906 
10907 #endif /* CONFIG_I40E_DCB */
10908 	if (!lock_acquired)
10909 		rtnl_lock();
10910 	ret = i40e_setup_pf_switch(pf, reinit, true);
10911 	if (ret)
10912 		goto end_unlock;
10913 
10914 	/* The driver only wants link up/down and module qualification
10915 	 * reports from firmware.  Note the negative logic.
10916 	 */
10917 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10918 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10919 					 I40E_AQ_EVENT_MEDIA_NA |
10920 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10921 	if (ret)
10922 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10923 			 i40e_stat_str(&pf->hw, ret),
10924 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10925 
10926 	/* Rebuild the VSIs and VEBs that existed before reset.
10927 	 * They are still in our local switch element arrays, so only
10928 	 * need to rebuild the switch model in the HW.
10929 	 *
10930 	 * If there were VEBs but the reconstitution failed, we'll try
10931 	 * to recover minimal use by getting the basic PF VSI working.
10932 	 */
10933 	if (vsi->uplink_seid != pf->mac_seid) {
10934 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10935 		/* find the one VEB connected to the MAC, and find orphans */
10936 		for (v = 0; v < I40E_MAX_VEB; v++) {
10937 			if (!pf->veb[v])
10938 				continue;
10939 
10940 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10941 			    pf->veb[v]->uplink_seid == 0) {
10942 				ret = i40e_reconstitute_veb(pf->veb[v]);
10943 
10944 				if (!ret)
10945 					continue;
10946 
10947 				/* If Main VEB failed, we're in deep doodoo,
10948 				 * so give up rebuilding the switch and set up
10949 				 * for minimal rebuild of PF VSI.
10950 				 * If orphan failed, we'll report the error
10951 				 * but try to keep going.
10952 				 */
10953 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10954 					dev_info(&pf->pdev->dev,
10955 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10956 						 ret);
10957 					vsi->uplink_seid = pf->mac_seid;
10958 					break;
10959 				} else if (pf->veb[v]->uplink_seid == 0) {
10960 					dev_info(&pf->pdev->dev,
10961 						 "rebuild of orphan VEB failed: %d\n",
10962 						 ret);
10963 				}
10964 			}
10965 		}
10966 	}
10967 
10968 	if (vsi->uplink_seid == pf->mac_seid) {
10969 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10970 		/* no VEB, so rebuild only the Main VSI */
10971 		ret = i40e_add_vsi(vsi);
10972 		if (ret) {
10973 			dev_info(&pf->pdev->dev,
10974 				 "rebuild of Main VSI failed: %d\n", ret);
10975 			goto end_unlock;
10976 		}
10977 	}
10978 
10979 	if (vsi->mqprio_qopt.max_rate[0]) {
10980 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
10981 						  vsi->mqprio_qopt.max_rate[0]);
10982 		u64 credits = 0;
10983 
10984 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10985 		if (ret)
10986 			goto end_unlock;
10987 
10988 		credits = max_tx_rate;
10989 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10990 		dev_dbg(&vsi->back->pdev->dev,
10991 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10992 			max_tx_rate,
10993 			credits,
10994 			vsi->seid);
10995 	}
10996 
10997 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10998 	if (ret)
10999 		goto end_unlock;
11000 
11001 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11002 	 * for this main VSI if they exist
11003 	 */
11004 	ret = i40e_rebuild_channels(vsi);
11005 	if (ret)
11006 		goto end_unlock;
11007 
11008 	/* Reconfigure hardware for allowing smaller MSS in the case
11009 	 * of TSO, so that we avoid the MDD being fired and causing
11010 	 * a reset in the case of small MSS+TSO.
11011 	 */
11012 #define I40E_REG_MSS          0x000E64DC
11013 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11014 #define I40E_64BYTE_MSS       0x400000
11015 	val = rd32(hw, I40E_REG_MSS);
11016 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11017 		val &= ~I40E_REG_MSS_MIN_MASK;
11018 		val |= I40E_64BYTE_MSS;
11019 		wr32(hw, I40E_REG_MSS, val);
11020 	}
11021 
11022 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11023 		msleep(75);
11024 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11025 		if (ret)
11026 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11027 				 i40e_stat_str(&pf->hw, ret),
11028 				 i40e_aq_str(&pf->hw,
11029 					     pf->hw.aq.asq_last_status));
11030 	}
11031 	/* reinit the misc interrupt */
11032 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11033 		ret = i40e_setup_misc_vector(pf);
11034 
11035 	/* Add a filter to drop all Flow control frames from any VSI from being
11036 	 * transmitted. By doing so we stop a malicious VF from sending out
11037 	 * PAUSE or PFC frames and potentially controlling traffic for other
11038 	 * PF/VF VSIs.
11039 	 * The FW can still send Flow control frames if enabled.
11040 	 */
11041 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11042 						       pf->main_vsi_seid);
11043 
11044 	/* restart the VSIs that were rebuilt and running before the reset */
11045 	i40e_pf_unquiesce_all_vsi(pf);
11046 
11047 	/* Release the RTNL lock before we start resetting VFs */
11048 	if (!lock_acquired)
11049 		rtnl_unlock();
11050 
11051 	/* Restore promiscuous settings */
11052 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11053 	if (ret)
11054 		dev_warn(&pf->pdev->dev,
11055 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11056 			 pf->cur_promisc ? "on" : "off",
11057 			 i40e_stat_str(&pf->hw, ret),
11058 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11059 
11060 	i40e_reset_all_vfs(pf, true);
11061 
11062 	/* tell the firmware that we're starting */
11063 	i40e_send_version(pf);
11064 
11065 	/* We've already released the lock, so don't do it again */
11066 	goto end_core_reset;
11067 
11068 end_unlock:
11069 	if (!lock_acquired)
11070 		rtnl_unlock();
11071 end_core_reset:
11072 	clear_bit(__I40E_RESET_FAILED, pf->state);
11073 clear_recovery:
11074 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11075 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11076 }
11077 
11078 /**
11079  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11080  * @pf: board private structure
11081  * @reinit: if the Main VSI needs to re-initialized.
11082  * @lock_acquired: indicates whether or not the lock has been acquired
11083  * before this function was called.
11084  **/
11085 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11086 				   bool lock_acquired)
11087 {
11088 	int ret;
11089 
11090 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11091 		return;
11092 	/* Now we wait for GRST to settle out.
11093 	 * We don't have to delete the VEBs or VSIs from the hw switch
11094 	 * because the reset will make them disappear.
11095 	 */
11096 	ret = i40e_reset(pf);
11097 	if (!ret)
11098 		i40e_rebuild(pf, reinit, lock_acquired);
11099 }
11100 
11101 /**
11102  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11103  * @pf: board private structure
11104  *
11105  * Close up the VFs and other things in prep for a Core Reset,
11106  * then get ready to rebuild the world.
11107  * @lock_acquired: indicates whether or not the lock has been acquired
11108  * before this function was called.
11109  **/
11110 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11111 {
11112 	i40e_prep_for_reset(pf);
11113 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11114 }
11115 
11116 /**
11117  * i40e_handle_mdd_event
11118  * @pf: pointer to the PF structure
11119  *
11120  * Called from the MDD irq handler to identify possibly malicious vfs
11121  **/
11122 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11123 {
11124 	struct i40e_hw *hw = &pf->hw;
11125 	bool mdd_detected = false;
11126 	struct i40e_vf *vf;
11127 	u32 reg;
11128 	int i;
11129 
11130 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11131 		return;
11132 
11133 	/* find what triggered the MDD event */
11134 	reg = rd32(hw, I40E_GL_MDET_TX);
11135 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11136 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11137 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11138 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11139 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11140 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11141 				I40E_GL_MDET_TX_EVENT_SHIFT;
11142 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11143 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11144 				pf->hw.func_caps.base_queue;
11145 		if (netif_msg_tx_err(pf))
11146 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11147 				 event, queue, pf_num, vf_num);
11148 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11149 		mdd_detected = true;
11150 	}
11151 	reg = rd32(hw, I40E_GL_MDET_RX);
11152 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11153 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11154 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11155 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11156 				I40E_GL_MDET_RX_EVENT_SHIFT;
11157 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11158 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11159 				pf->hw.func_caps.base_queue;
11160 		if (netif_msg_rx_err(pf))
11161 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11162 				 event, queue, func);
11163 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11164 		mdd_detected = true;
11165 	}
11166 
11167 	if (mdd_detected) {
11168 		reg = rd32(hw, I40E_PF_MDET_TX);
11169 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11170 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11171 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11172 		}
11173 		reg = rd32(hw, I40E_PF_MDET_RX);
11174 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11175 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11176 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11177 		}
11178 	}
11179 
11180 	/* see if one of the VFs needs its hand slapped */
11181 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11182 		vf = &(pf->vf[i]);
11183 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11184 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11185 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11186 			vf->num_mdd_events++;
11187 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11188 				 i);
11189 			dev_info(&pf->pdev->dev,
11190 				 "Use PF Control I/F to re-enable the VF\n");
11191 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11192 		}
11193 
11194 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11195 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11196 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11197 			vf->num_mdd_events++;
11198 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11199 				 i);
11200 			dev_info(&pf->pdev->dev,
11201 				 "Use PF Control I/F to re-enable the VF\n");
11202 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11203 		}
11204 	}
11205 
11206 	/* re-enable mdd interrupt cause */
11207 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11208 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11209 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11210 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11211 	i40e_flush(hw);
11212 }
11213 
11214 /**
11215  * i40e_service_task - Run the driver's async subtasks
11216  * @work: pointer to work_struct containing our data
11217  **/
11218 static void i40e_service_task(struct work_struct *work)
11219 {
11220 	struct i40e_pf *pf = container_of(work,
11221 					  struct i40e_pf,
11222 					  service_task);
11223 	unsigned long start_time = jiffies;
11224 
11225 	/* don't bother with service tasks if a reset is in progress */
11226 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11227 	    test_bit(__I40E_SUSPENDED, pf->state))
11228 		return;
11229 
11230 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11231 		return;
11232 
11233 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11234 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11235 		i40e_sync_filters_subtask(pf);
11236 		i40e_reset_subtask(pf);
11237 		i40e_handle_mdd_event(pf);
11238 		i40e_vc_process_vflr_event(pf);
11239 		i40e_watchdog_subtask(pf);
11240 		i40e_fdir_reinit_subtask(pf);
11241 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11242 			/* Client subtask will reopen next time through. */
11243 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11244 							   true);
11245 		} else {
11246 			i40e_client_subtask(pf);
11247 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11248 					       pf->state))
11249 				i40e_notify_client_of_l2_param_changes(
11250 								pf->vsi[pf->lan_vsi]);
11251 		}
11252 		i40e_sync_filters_subtask(pf);
11253 	} else {
11254 		i40e_reset_subtask(pf);
11255 	}
11256 
11257 	i40e_clean_adminq_subtask(pf);
11258 
11259 	/* flush memory to make sure state is correct before next watchdog */
11260 	smp_mb__before_atomic();
11261 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11262 
11263 	/* If the tasks have taken longer than one timer cycle or there
11264 	 * is more work to be done, reschedule the service task now
11265 	 * rather than wait for the timer to tick again.
11266 	 */
11267 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11268 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11269 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11270 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11271 		i40e_service_event_schedule(pf);
11272 }
11273 
11274 /**
11275  * i40e_service_timer - timer callback
11276  * @t: timer list pointer
11277  **/
11278 static void i40e_service_timer(struct timer_list *t)
11279 {
11280 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11281 
11282 	mod_timer(&pf->service_timer,
11283 		  round_jiffies(jiffies + pf->service_timer_period));
11284 	i40e_service_event_schedule(pf);
11285 }
11286 
11287 /**
11288  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11289  * @vsi: the VSI being configured
11290  **/
11291 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11292 {
11293 	struct i40e_pf *pf = vsi->back;
11294 
11295 	switch (vsi->type) {
11296 	case I40E_VSI_MAIN:
11297 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11298 		if (!vsi->num_tx_desc)
11299 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11300 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11301 		if (!vsi->num_rx_desc)
11302 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11303 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11304 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11305 			vsi->num_q_vectors = pf->num_lan_msix;
11306 		else
11307 			vsi->num_q_vectors = 1;
11308 
11309 		break;
11310 
11311 	case I40E_VSI_FDIR:
11312 		vsi->alloc_queue_pairs = 1;
11313 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11314 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11315 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11316 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11317 		vsi->num_q_vectors = pf->num_fdsb_msix;
11318 		break;
11319 
11320 	case I40E_VSI_VMDQ2:
11321 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11322 		if (!vsi->num_tx_desc)
11323 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11324 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11325 		if (!vsi->num_rx_desc)
11326 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11327 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11328 		vsi->num_q_vectors = pf->num_vmdq_msix;
11329 		break;
11330 
11331 	case I40E_VSI_SRIOV:
11332 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11333 		if (!vsi->num_tx_desc)
11334 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11335 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11336 		if (!vsi->num_rx_desc)
11337 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11338 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11339 		break;
11340 
11341 	default:
11342 		WARN_ON(1);
11343 		return -ENODATA;
11344 	}
11345 
11346 	if (is_kdump_kernel()) {
11347 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11348 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11349 	}
11350 
11351 	return 0;
11352 }
11353 
11354 /**
11355  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11356  * @vsi: VSI pointer
11357  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11358  *
11359  * On error: returns error code (negative)
11360  * On success: returns 0
11361  **/
11362 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11363 {
11364 	struct i40e_ring **next_rings;
11365 	int size;
11366 	int ret = 0;
11367 
11368 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11369 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11370 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11371 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11372 	if (!vsi->tx_rings)
11373 		return -ENOMEM;
11374 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11375 	if (i40e_enabled_xdp_vsi(vsi)) {
11376 		vsi->xdp_rings = next_rings;
11377 		next_rings += vsi->alloc_queue_pairs;
11378 	}
11379 	vsi->rx_rings = next_rings;
11380 
11381 	if (alloc_qvectors) {
11382 		/* allocate memory for q_vector pointers */
11383 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11384 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11385 		if (!vsi->q_vectors) {
11386 			ret = -ENOMEM;
11387 			goto err_vectors;
11388 		}
11389 	}
11390 	return ret;
11391 
11392 err_vectors:
11393 	kfree(vsi->tx_rings);
11394 	return ret;
11395 }
11396 
11397 /**
11398  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11399  * @pf: board private structure
11400  * @type: type of VSI
11401  *
11402  * On error: returns error code (negative)
11403  * On success: returns vsi index in PF (positive)
11404  **/
11405 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11406 {
11407 	int ret = -ENODEV;
11408 	struct i40e_vsi *vsi;
11409 	int vsi_idx;
11410 	int i;
11411 
11412 	/* Need to protect the allocation of the VSIs at the PF level */
11413 	mutex_lock(&pf->switch_mutex);
11414 
11415 	/* VSI list may be fragmented if VSI creation/destruction has
11416 	 * been happening.  We can afford to do a quick scan to look
11417 	 * for any free VSIs in the list.
11418 	 *
11419 	 * find next empty vsi slot, looping back around if necessary
11420 	 */
11421 	i = pf->next_vsi;
11422 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11423 		i++;
11424 	if (i >= pf->num_alloc_vsi) {
11425 		i = 0;
11426 		while (i < pf->next_vsi && pf->vsi[i])
11427 			i++;
11428 	}
11429 
11430 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11431 		vsi_idx = i;             /* Found one! */
11432 	} else {
11433 		ret = -ENODEV;
11434 		goto unlock_pf;  /* out of VSI slots! */
11435 	}
11436 	pf->next_vsi = ++i;
11437 
11438 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11439 	if (!vsi) {
11440 		ret = -ENOMEM;
11441 		goto unlock_pf;
11442 	}
11443 	vsi->type = type;
11444 	vsi->back = pf;
11445 	set_bit(__I40E_VSI_DOWN, vsi->state);
11446 	vsi->flags = 0;
11447 	vsi->idx = vsi_idx;
11448 	vsi->int_rate_limit = 0;
11449 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11450 				pf->rss_table_size : 64;
11451 	vsi->netdev_registered = false;
11452 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11453 	hash_init(vsi->mac_filter_hash);
11454 	vsi->irqs_ready = false;
11455 
11456 	if (type == I40E_VSI_MAIN) {
11457 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11458 		if (!vsi->af_xdp_zc_qps)
11459 			goto err_rings;
11460 	}
11461 
11462 	ret = i40e_set_num_rings_in_vsi(vsi);
11463 	if (ret)
11464 		goto err_rings;
11465 
11466 	ret = i40e_vsi_alloc_arrays(vsi, true);
11467 	if (ret)
11468 		goto err_rings;
11469 
11470 	/* Setup default MSIX irq handler for VSI */
11471 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11472 
11473 	/* Initialize VSI lock */
11474 	spin_lock_init(&vsi->mac_filter_hash_lock);
11475 	pf->vsi[vsi_idx] = vsi;
11476 	ret = vsi_idx;
11477 	goto unlock_pf;
11478 
11479 err_rings:
11480 	bitmap_free(vsi->af_xdp_zc_qps);
11481 	pf->next_vsi = i - 1;
11482 	kfree(vsi);
11483 unlock_pf:
11484 	mutex_unlock(&pf->switch_mutex);
11485 	return ret;
11486 }
11487 
11488 /**
11489  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11490  * @vsi: VSI pointer
11491  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11492  *
11493  * On error: returns error code (negative)
11494  * On success: returns 0
11495  **/
11496 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11497 {
11498 	/* free the ring and vector containers */
11499 	if (free_qvectors) {
11500 		kfree(vsi->q_vectors);
11501 		vsi->q_vectors = NULL;
11502 	}
11503 	kfree(vsi->tx_rings);
11504 	vsi->tx_rings = NULL;
11505 	vsi->rx_rings = NULL;
11506 	vsi->xdp_rings = NULL;
11507 }
11508 
11509 /**
11510  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11511  * and lookup table
11512  * @vsi: Pointer to VSI structure
11513  */
11514 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11515 {
11516 	if (!vsi)
11517 		return;
11518 
11519 	kfree(vsi->rss_hkey_user);
11520 	vsi->rss_hkey_user = NULL;
11521 
11522 	kfree(vsi->rss_lut_user);
11523 	vsi->rss_lut_user = NULL;
11524 }
11525 
11526 /**
11527  * i40e_vsi_clear - Deallocate the VSI provided
11528  * @vsi: the VSI being un-configured
11529  **/
11530 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11531 {
11532 	struct i40e_pf *pf;
11533 
11534 	if (!vsi)
11535 		return 0;
11536 
11537 	if (!vsi->back)
11538 		goto free_vsi;
11539 	pf = vsi->back;
11540 
11541 	mutex_lock(&pf->switch_mutex);
11542 	if (!pf->vsi[vsi->idx]) {
11543 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11544 			vsi->idx, vsi->idx, vsi->type);
11545 		goto unlock_vsi;
11546 	}
11547 
11548 	if (pf->vsi[vsi->idx] != vsi) {
11549 		dev_err(&pf->pdev->dev,
11550 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11551 			pf->vsi[vsi->idx]->idx,
11552 			pf->vsi[vsi->idx]->type,
11553 			vsi->idx, vsi->type);
11554 		goto unlock_vsi;
11555 	}
11556 
11557 	/* updates the PF for this cleared vsi */
11558 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11559 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11560 
11561 	bitmap_free(vsi->af_xdp_zc_qps);
11562 	i40e_vsi_free_arrays(vsi, true);
11563 	i40e_clear_rss_config_user(vsi);
11564 
11565 	pf->vsi[vsi->idx] = NULL;
11566 	if (vsi->idx < pf->next_vsi)
11567 		pf->next_vsi = vsi->idx;
11568 
11569 unlock_vsi:
11570 	mutex_unlock(&pf->switch_mutex);
11571 free_vsi:
11572 	kfree(vsi);
11573 
11574 	return 0;
11575 }
11576 
11577 /**
11578  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11579  * @vsi: the VSI being cleaned
11580  **/
11581 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11582 {
11583 	int i;
11584 
11585 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11586 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11587 			kfree_rcu(vsi->tx_rings[i], rcu);
11588 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11589 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11590 			if (vsi->xdp_rings)
11591 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11592 		}
11593 	}
11594 }
11595 
11596 /**
11597  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11598  * @vsi: the VSI being configured
11599  **/
11600 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11601 {
11602 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11603 	struct i40e_pf *pf = vsi->back;
11604 	struct i40e_ring *ring;
11605 
11606 	/* Set basic values in the rings to be used later during open() */
11607 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11608 		/* allocate space for both Tx and Rx in one shot */
11609 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11610 		if (!ring)
11611 			goto err_out;
11612 
11613 		ring->queue_index = i;
11614 		ring->reg_idx = vsi->base_queue + i;
11615 		ring->ring_active = false;
11616 		ring->vsi = vsi;
11617 		ring->netdev = vsi->netdev;
11618 		ring->dev = &pf->pdev->dev;
11619 		ring->count = vsi->num_tx_desc;
11620 		ring->size = 0;
11621 		ring->dcb_tc = 0;
11622 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11623 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11624 		ring->itr_setting = pf->tx_itr_default;
11625 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11626 
11627 		if (!i40e_enabled_xdp_vsi(vsi))
11628 			goto setup_rx;
11629 
11630 		ring->queue_index = vsi->alloc_queue_pairs + i;
11631 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11632 		ring->ring_active = false;
11633 		ring->vsi = vsi;
11634 		ring->netdev = NULL;
11635 		ring->dev = &pf->pdev->dev;
11636 		ring->count = vsi->num_tx_desc;
11637 		ring->size = 0;
11638 		ring->dcb_tc = 0;
11639 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11640 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11641 		set_ring_xdp(ring);
11642 		ring->itr_setting = pf->tx_itr_default;
11643 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11644 
11645 setup_rx:
11646 		ring->queue_index = i;
11647 		ring->reg_idx = vsi->base_queue + i;
11648 		ring->ring_active = false;
11649 		ring->vsi = vsi;
11650 		ring->netdev = vsi->netdev;
11651 		ring->dev = &pf->pdev->dev;
11652 		ring->count = vsi->num_rx_desc;
11653 		ring->size = 0;
11654 		ring->dcb_tc = 0;
11655 		ring->itr_setting = pf->rx_itr_default;
11656 		WRITE_ONCE(vsi->rx_rings[i], ring);
11657 	}
11658 
11659 	return 0;
11660 
11661 err_out:
11662 	i40e_vsi_clear_rings(vsi);
11663 	return -ENOMEM;
11664 }
11665 
11666 /**
11667  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11668  * @pf: board private structure
11669  * @vectors: the number of MSI-X vectors to request
11670  *
11671  * Returns the number of vectors reserved, or error
11672  **/
11673 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11674 {
11675 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11676 					I40E_MIN_MSIX, vectors);
11677 	if (vectors < 0) {
11678 		dev_info(&pf->pdev->dev,
11679 			 "MSI-X vector reservation failed: %d\n", vectors);
11680 		vectors = 0;
11681 	}
11682 
11683 	return vectors;
11684 }
11685 
11686 /**
11687  * i40e_init_msix - Setup the MSIX capability
11688  * @pf: board private structure
11689  *
11690  * Work with the OS to set up the MSIX vectors needed.
11691  *
11692  * Returns the number of vectors reserved or negative on failure
11693  **/
11694 static int i40e_init_msix(struct i40e_pf *pf)
11695 {
11696 	struct i40e_hw *hw = &pf->hw;
11697 	int cpus, extra_vectors;
11698 	int vectors_left;
11699 	int v_budget, i;
11700 	int v_actual;
11701 	int iwarp_requested = 0;
11702 
11703 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11704 		return -ENODEV;
11705 
11706 	/* The number of vectors we'll request will be comprised of:
11707 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11708 	 *   - The number of LAN queue pairs
11709 	 *	- Queues being used for RSS.
11710 	 *		We don't need as many as max_rss_size vectors.
11711 	 *		use rss_size instead in the calculation since that
11712 	 *		is governed by number of cpus in the system.
11713 	 *	- assumes symmetric Tx/Rx pairing
11714 	 *   - The number of VMDq pairs
11715 	 *   - The CPU count within the NUMA node if iWARP is enabled
11716 	 * Once we count this up, try the request.
11717 	 *
11718 	 * If we can't get what we want, we'll simplify to nearly nothing
11719 	 * and try again.  If that still fails, we punt.
11720 	 */
11721 	vectors_left = hw->func_caps.num_msix_vectors;
11722 	v_budget = 0;
11723 
11724 	/* reserve one vector for miscellaneous handler */
11725 	if (vectors_left) {
11726 		v_budget++;
11727 		vectors_left--;
11728 	}
11729 
11730 	/* reserve some vectors for the main PF traffic queues. Initially we
11731 	 * only reserve at most 50% of the available vectors, in the case that
11732 	 * the number of online CPUs is large. This ensures that we can enable
11733 	 * extra features as well. Once we've enabled the other features, we
11734 	 * will use any remaining vectors to reach as close as we can to the
11735 	 * number of online CPUs.
11736 	 */
11737 	cpus = num_online_cpus();
11738 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11739 	vectors_left -= pf->num_lan_msix;
11740 
11741 	/* reserve one vector for sideband flow director */
11742 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11743 		if (vectors_left) {
11744 			pf->num_fdsb_msix = 1;
11745 			v_budget++;
11746 			vectors_left--;
11747 		} else {
11748 			pf->num_fdsb_msix = 0;
11749 		}
11750 	}
11751 
11752 	/* can we reserve enough for iWARP? */
11753 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11754 		iwarp_requested = pf->num_iwarp_msix;
11755 
11756 		if (!vectors_left)
11757 			pf->num_iwarp_msix = 0;
11758 		else if (vectors_left < pf->num_iwarp_msix)
11759 			pf->num_iwarp_msix = 1;
11760 		v_budget += pf->num_iwarp_msix;
11761 		vectors_left -= pf->num_iwarp_msix;
11762 	}
11763 
11764 	/* any vectors left over go for VMDq support */
11765 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11766 		if (!vectors_left) {
11767 			pf->num_vmdq_msix = 0;
11768 			pf->num_vmdq_qps = 0;
11769 		} else {
11770 			int vmdq_vecs_wanted =
11771 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11772 			int vmdq_vecs =
11773 				min_t(int, vectors_left, vmdq_vecs_wanted);
11774 
11775 			/* if we're short on vectors for what's desired, we limit
11776 			 * the queues per vmdq.  If this is still more than are
11777 			 * available, the user will need to change the number of
11778 			 * queues/vectors used by the PF later with the ethtool
11779 			 * channels command
11780 			 */
11781 			if (vectors_left < vmdq_vecs_wanted) {
11782 				pf->num_vmdq_qps = 1;
11783 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11784 				vmdq_vecs = min_t(int,
11785 						  vectors_left,
11786 						  vmdq_vecs_wanted);
11787 			}
11788 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11789 
11790 			v_budget += vmdq_vecs;
11791 			vectors_left -= vmdq_vecs;
11792 		}
11793 	}
11794 
11795 	/* On systems with a large number of SMP cores, we previously limited
11796 	 * the number of vectors for num_lan_msix to be at most 50% of the
11797 	 * available vectors, to allow for other features. Now, we add back
11798 	 * the remaining vectors. However, we ensure that the total
11799 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11800 	 * calculate the number of vectors we can add without going over the
11801 	 * cap of CPUs. For systems with a small number of CPUs this will be
11802 	 * zero.
11803 	 */
11804 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11805 	pf->num_lan_msix += extra_vectors;
11806 	vectors_left -= extra_vectors;
11807 
11808 	WARN(vectors_left < 0,
11809 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11810 
11811 	v_budget += pf->num_lan_msix;
11812 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11813 				   GFP_KERNEL);
11814 	if (!pf->msix_entries)
11815 		return -ENOMEM;
11816 
11817 	for (i = 0; i < v_budget; i++)
11818 		pf->msix_entries[i].entry = i;
11819 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11820 
11821 	if (v_actual < I40E_MIN_MSIX) {
11822 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11823 		kfree(pf->msix_entries);
11824 		pf->msix_entries = NULL;
11825 		pci_disable_msix(pf->pdev);
11826 		return -ENODEV;
11827 
11828 	} else if (v_actual == I40E_MIN_MSIX) {
11829 		/* Adjust for minimal MSIX use */
11830 		pf->num_vmdq_vsis = 0;
11831 		pf->num_vmdq_qps = 0;
11832 		pf->num_lan_qps = 1;
11833 		pf->num_lan_msix = 1;
11834 
11835 	} else if (v_actual != v_budget) {
11836 		/* If we have limited resources, we will start with no vectors
11837 		 * for the special features and then allocate vectors to some
11838 		 * of these features based on the policy and at the end disable
11839 		 * the features that did not get any vectors.
11840 		 */
11841 		int vec;
11842 
11843 		dev_info(&pf->pdev->dev,
11844 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11845 			 v_actual, v_budget);
11846 		/* reserve the misc vector */
11847 		vec = v_actual - 1;
11848 
11849 		/* Scale vector usage down */
11850 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11851 		pf->num_vmdq_vsis = 1;
11852 		pf->num_vmdq_qps = 1;
11853 
11854 		/* partition out the remaining vectors */
11855 		switch (vec) {
11856 		case 2:
11857 			pf->num_lan_msix = 1;
11858 			break;
11859 		case 3:
11860 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11861 				pf->num_lan_msix = 1;
11862 				pf->num_iwarp_msix = 1;
11863 			} else {
11864 				pf->num_lan_msix = 2;
11865 			}
11866 			break;
11867 		default:
11868 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11869 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11870 						 iwarp_requested);
11871 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11872 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11873 			} else {
11874 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11875 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11876 			}
11877 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11878 				pf->num_fdsb_msix = 1;
11879 				vec--;
11880 			}
11881 			pf->num_lan_msix = min_t(int,
11882 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11883 							      pf->num_lan_msix);
11884 			pf->num_lan_qps = pf->num_lan_msix;
11885 			break;
11886 		}
11887 	}
11888 
11889 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11890 	    (pf->num_fdsb_msix == 0)) {
11891 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11892 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11893 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11894 	}
11895 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11896 	    (pf->num_vmdq_msix == 0)) {
11897 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11898 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11899 	}
11900 
11901 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11902 	    (pf->num_iwarp_msix == 0)) {
11903 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11904 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11905 	}
11906 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11907 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11908 		   pf->num_lan_msix,
11909 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11910 		   pf->num_fdsb_msix,
11911 		   pf->num_iwarp_msix);
11912 
11913 	return v_actual;
11914 }
11915 
11916 /**
11917  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11918  * @vsi: the VSI being configured
11919  * @v_idx: index of the vector in the vsi struct
11920  *
11921  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11922  **/
11923 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11924 {
11925 	struct i40e_q_vector *q_vector;
11926 
11927 	/* allocate q_vector */
11928 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11929 	if (!q_vector)
11930 		return -ENOMEM;
11931 
11932 	q_vector->vsi = vsi;
11933 	q_vector->v_idx = v_idx;
11934 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11935 
11936 	if (vsi->netdev)
11937 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11938 
11939 	/* tie q_vector and vsi together */
11940 	vsi->q_vectors[v_idx] = q_vector;
11941 
11942 	return 0;
11943 }
11944 
11945 /**
11946  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11947  * @vsi: the VSI being configured
11948  *
11949  * We allocate one q_vector per queue interrupt.  If allocation fails we
11950  * return -ENOMEM.
11951  **/
11952 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11953 {
11954 	struct i40e_pf *pf = vsi->back;
11955 	int err, v_idx, num_q_vectors;
11956 
11957 	/* if not MSIX, give the one vector only to the LAN VSI */
11958 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11959 		num_q_vectors = vsi->num_q_vectors;
11960 	else if (vsi == pf->vsi[pf->lan_vsi])
11961 		num_q_vectors = 1;
11962 	else
11963 		return -EINVAL;
11964 
11965 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11966 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11967 		if (err)
11968 			goto err_out;
11969 	}
11970 
11971 	return 0;
11972 
11973 err_out:
11974 	while (v_idx--)
11975 		i40e_free_q_vector(vsi, v_idx);
11976 
11977 	return err;
11978 }
11979 
11980 /**
11981  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11982  * @pf: board private structure to initialize
11983  **/
11984 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11985 {
11986 	int vectors = 0;
11987 	ssize_t size;
11988 
11989 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11990 		vectors = i40e_init_msix(pf);
11991 		if (vectors < 0) {
11992 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11993 				       I40E_FLAG_IWARP_ENABLED	|
11994 				       I40E_FLAG_RSS_ENABLED	|
11995 				       I40E_FLAG_DCB_CAPABLE	|
11996 				       I40E_FLAG_DCB_ENABLED	|
11997 				       I40E_FLAG_SRIOV_ENABLED	|
11998 				       I40E_FLAG_FD_SB_ENABLED	|
11999 				       I40E_FLAG_FD_ATR_ENABLED	|
12000 				       I40E_FLAG_VMDQ_ENABLED);
12001 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12002 
12003 			/* rework the queue expectations without MSIX */
12004 			i40e_determine_queue_usage(pf);
12005 		}
12006 	}
12007 
12008 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12009 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12010 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12011 		vectors = pci_enable_msi(pf->pdev);
12012 		if (vectors < 0) {
12013 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12014 				 vectors);
12015 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12016 		}
12017 		vectors = 1;  /* one MSI or Legacy vector */
12018 	}
12019 
12020 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12021 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12022 
12023 	/* set up vector assignment tracking */
12024 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12025 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12026 	if (!pf->irq_pile)
12027 		return -ENOMEM;
12028 
12029 	pf->irq_pile->num_entries = vectors;
12030 
12031 	/* track first vector for misc interrupts, ignore return */
12032 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12033 
12034 	return 0;
12035 }
12036 
12037 /**
12038  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12039  * @pf: private board data structure
12040  *
12041  * Restore the interrupt scheme that was cleared when we suspended the
12042  * device. This should be called during resume to re-allocate the q_vectors
12043  * and reacquire IRQs.
12044  */
12045 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12046 {
12047 	int err, i;
12048 
12049 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12050 	 * scheme. We need to re-enabled them here in order to attempt to
12051 	 * re-acquire the MSI or MSI-X vectors
12052 	 */
12053 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12054 
12055 	err = i40e_init_interrupt_scheme(pf);
12056 	if (err)
12057 		return err;
12058 
12059 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12060 	 * rings together again.
12061 	 */
12062 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12063 		if (pf->vsi[i]) {
12064 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12065 			if (err)
12066 				goto err_unwind;
12067 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12068 		}
12069 	}
12070 
12071 	err = i40e_setup_misc_vector(pf);
12072 	if (err)
12073 		goto err_unwind;
12074 
12075 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12076 		i40e_client_update_msix_info(pf);
12077 
12078 	return 0;
12079 
12080 err_unwind:
12081 	while (i--) {
12082 		if (pf->vsi[i])
12083 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12084 	}
12085 
12086 	return err;
12087 }
12088 
12089 /**
12090  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12091  * non queue events in recovery mode
12092  * @pf: board private structure
12093  *
12094  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12095  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12096  * This is handled differently than in recovery mode since no Tx/Rx resources
12097  * are being allocated.
12098  **/
12099 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12100 {
12101 	int err;
12102 
12103 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12104 		err = i40e_setup_misc_vector(pf);
12105 
12106 		if (err) {
12107 			dev_info(&pf->pdev->dev,
12108 				 "MSI-X misc vector request failed, error %d\n",
12109 				 err);
12110 			return err;
12111 		}
12112 	} else {
12113 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12114 
12115 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12116 				  pf->int_name, pf);
12117 
12118 		if (err) {
12119 			dev_info(&pf->pdev->dev,
12120 				 "MSI/legacy misc vector request failed, error %d\n",
12121 				 err);
12122 			return err;
12123 		}
12124 		i40e_enable_misc_int_causes(pf);
12125 		i40e_irq_dynamic_enable_icr0(pf);
12126 	}
12127 
12128 	return 0;
12129 }
12130 
12131 /**
12132  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12133  * @pf: board private structure
12134  *
12135  * This sets up the handler for MSIX 0, which is used to manage the
12136  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12137  * when in MSI or Legacy interrupt mode.
12138  **/
12139 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12140 {
12141 	struct i40e_hw *hw = &pf->hw;
12142 	int err = 0;
12143 
12144 	/* Only request the IRQ once, the first time through. */
12145 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12146 		err = request_irq(pf->msix_entries[0].vector,
12147 				  i40e_intr, 0, pf->int_name, pf);
12148 		if (err) {
12149 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12150 			dev_info(&pf->pdev->dev,
12151 				 "request_irq for %s failed: %d\n",
12152 				 pf->int_name, err);
12153 			return -EFAULT;
12154 		}
12155 	}
12156 
12157 	i40e_enable_misc_int_causes(pf);
12158 
12159 	/* associate no queues to the misc vector */
12160 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12161 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12162 
12163 	i40e_flush(hw);
12164 
12165 	i40e_irq_dynamic_enable_icr0(pf);
12166 
12167 	return err;
12168 }
12169 
12170 /**
12171  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12172  * @vsi: Pointer to vsi structure
12173  * @seed: Buffter to store the hash keys
12174  * @lut: Buffer to store the lookup table entries
12175  * @lut_size: Size of buffer to store the lookup table entries
12176  *
12177  * Return 0 on success, negative on failure
12178  */
12179 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12180 			   u8 *lut, u16 lut_size)
12181 {
12182 	struct i40e_pf *pf = vsi->back;
12183 	struct i40e_hw *hw = &pf->hw;
12184 	int ret = 0;
12185 
12186 	if (seed) {
12187 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12188 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12189 		if (ret) {
12190 			dev_info(&pf->pdev->dev,
12191 				 "Cannot get RSS key, err %s aq_err %s\n",
12192 				 i40e_stat_str(&pf->hw, ret),
12193 				 i40e_aq_str(&pf->hw,
12194 					     pf->hw.aq.asq_last_status));
12195 			return ret;
12196 		}
12197 	}
12198 
12199 	if (lut) {
12200 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12201 
12202 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12203 		if (ret) {
12204 			dev_info(&pf->pdev->dev,
12205 				 "Cannot get RSS lut, err %s aq_err %s\n",
12206 				 i40e_stat_str(&pf->hw, ret),
12207 				 i40e_aq_str(&pf->hw,
12208 					     pf->hw.aq.asq_last_status));
12209 			return ret;
12210 		}
12211 	}
12212 
12213 	return ret;
12214 }
12215 
12216 /**
12217  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12218  * @vsi: Pointer to vsi structure
12219  * @seed: RSS hash seed
12220  * @lut: Lookup table
12221  * @lut_size: Lookup table size
12222  *
12223  * Returns 0 on success, negative on failure
12224  **/
12225 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12226 			       const u8 *lut, u16 lut_size)
12227 {
12228 	struct i40e_pf *pf = vsi->back;
12229 	struct i40e_hw *hw = &pf->hw;
12230 	u16 vf_id = vsi->vf_id;
12231 	u8 i;
12232 
12233 	/* Fill out hash function seed */
12234 	if (seed) {
12235 		u32 *seed_dw = (u32 *)seed;
12236 
12237 		if (vsi->type == I40E_VSI_MAIN) {
12238 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12239 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12240 		} else if (vsi->type == I40E_VSI_SRIOV) {
12241 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12242 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12243 		} else {
12244 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12245 		}
12246 	}
12247 
12248 	if (lut) {
12249 		u32 *lut_dw = (u32 *)lut;
12250 
12251 		if (vsi->type == I40E_VSI_MAIN) {
12252 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12253 				return -EINVAL;
12254 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12255 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12256 		} else if (vsi->type == I40E_VSI_SRIOV) {
12257 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12258 				return -EINVAL;
12259 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12260 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12261 		} else {
12262 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12263 		}
12264 	}
12265 	i40e_flush(hw);
12266 
12267 	return 0;
12268 }
12269 
12270 /**
12271  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12272  * @vsi: Pointer to VSI structure
12273  * @seed: Buffer to store the keys
12274  * @lut: Buffer to store the lookup table entries
12275  * @lut_size: Size of buffer to store the lookup table entries
12276  *
12277  * Returns 0 on success, negative on failure
12278  */
12279 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12280 			    u8 *lut, u16 lut_size)
12281 {
12282 	struct i40e_pf *pf = vsi->back;
12283 	struct i40e_hw *hw = &pf->hw;
12284 	u16 i;
12285 
12286 	if (seed) {
12287 		u32 *seed_dw = (u32 *)seed;
12288 
12289 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12290 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12291 	}
12292 	if (lut) {
12293 		u32 *lut_dw = (u32 *)lut;
12294 
12295 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12296 			return -EINVAL;
12297 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12298 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12299 	}
12300 
12301 	return 0;
12302 }
12303 
12304 /**
12305  * i40e_config_rss - Configure RSS keys and lut
12306  * @vsi: Pointer to VSI structure
12307  * @seed: RSS hash seed
12308  * @lut: Lookup table
12309  * @lut_size: Lookup table size
12310  *
12311  * Returns 0 on success, negative on failure
12312  */
12313 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12314 {
12315 	struct i40e_pf *pf = vsi->back;
12316 
12317 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12318 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12319 	else
12320 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12321 }
12322 
12323 /**
12324  * i40e_get_rss - Get RSS keys and lut
12325  * @vsi: Pointer to VSI structure
12326  * @seed: Buffer to store the keys
12327  * @lut: Buffer to store the lookup table entries
12328  * @lut_size: Size of buffer to store the lookup table entries
12329  *
12330  * Returns 0 on success, negative on failure
12331  */
12332 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12333 {
12334 	struct i40e_pf *pf = vsi->back;
12335 
12336 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12337 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12338 	else
12339 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12340 }
12341 
12342 /**
12343  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12344  * @pf: Pointer to board private structure
12345  * @lut: Lookup table
12346  * @rss_table_size: Lookup table size
12347  * @rss_size: Range of queue number for hashing
12348  */
12349 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12350 		       u16 rss_table_size, u16 rss_size)
12351 {
12352 	u16 i;
12353 
12354 	for (i = 0; i < rss_table_size; i++)
12355 		lut[i] = i % rss_size;
12356 }
12357 
12358 /**
12359  * i40e_pf_config_rss - Prepare for RSS if used
12360  * @pf: board private structure
12361  **/
12362 static int i40e_pf_config_rss(struct i40e_pf *pf)
12363 {
12364 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12365 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12366 	u8 *lut;
12367 	struct i40e_hw *hw = &pf->hw;
12368 	u32 reg_val;
12369 	u64 hena;
12370 	int ret;
12371 
12372 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12373 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12374 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12375 	hena |= i40e_pf_get_default_rss_hena(pf);
12376 
12377 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12378 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12379 
12380 	/* Determine the RSS table size based on the hardware capabilities */
12381 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12382 	reg_val = (pf->rss_table_size == 512) ?
12383 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12384 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12385 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12386 
12387 	/* Determine the RSS size of the VSI */
12388 	if (!vsi->rss_size) {
12389 		u16 qcount;
12390 		/* If the firmware does something weird during VSI init, we
12391 		 * could end up with zero TCs. Check for that to avoid
12392 		 * divide-by-zero. It probably won't pass traffic, but it also
12393 		 * won't panic.
12394 		 */
12395 		qcount = vsi->num_queue_pairs /
12396 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12397 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12398 	}
12399 	if (!vsi->rss_size)
12400 		return -EINVAL;
12401 
12402 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12403 	if (!lut)
12404 		return -ENOMEM;
12405 
12406 	/* Use user configured lut if there is one, otherwise use default */
12407 	if (vsi->rss_lut_user)
12408 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12409 	else
12410 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12411 
12412 	/* Use user configured hash key if there is one, otherwise
12413 	 * use default.
12414 	 */
12415 	if (vsi->rss_hkey_user)
12416 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12417 	else
12418 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12419 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12420 	kfree(lut);
12421 
12422 	return ret;
12423 }
12424 
12425 /**
12426  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12427  * @pf: board private structure
12428  * @queue_count: the requested queue count for rss.
12429  *
12430  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12431  * count which may be different from the requested queue count.
12432  * Note: expects to be called while under rtnl_lock()
12433  **/
12434 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12435 {
12436 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12437 	int new_rss_size;
12438 
12439 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12440 		return 0;
12441 
12442 	queue_count = min_t(int, queue_count, num_online_cpus());
12443 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12444 
12445 	if (queue_count != vsi->num_queue_pairs) {
12446 		u16 qcount;
12447 
12448 		vsi->req_queue_pairs = queue_count;
12449 		i40e_prep_for_reset(pf);
12450 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12451 			return pf->alloc_rss_size;
12452 
12453 		pf->alloc_rss_size = new_rss_size;
12454 
12455 		i40e_reset_and_rebuild(pf, true, true);
12456 
12457 		/* Discard the user configured hash keys and lut, if less
12458 		 * queues are enabled.
12459 		 */
12460 		if (queue_count < vsi->rss_size) {
12461 			i40e_clear_rss_config_user(vsi);
12462 			dev_dbg(&pf->pdev->dev,
12463 				"discard user configured hash keys and lut\n");
12464 		}
12465 
12466 		/* Reset vsi->rss_size, as number of enabled queues changed */
12467 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12468 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12469 
12470 		i40e_pf_config_rss(pf);
12471 	}
12472 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12473 		 vsi->req_queue_pairs, pf->rss_size_max);
12474 	return pf->alloc_rss_size;
12475 }
12476 
12477 /**
12478  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12479  * @pf: board private structure
12480  **/
12481 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12482 {
12483 	i40e_status status;
12484 	bool min_valid, max_valid;
12485 	u32 max_bw, min_bw;
12486 
12487 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12488 					   &min_valid, &max_valid);
12489 
12490 	if (!status) {
12491 		if (min_valid)
12492 			pf->min_bw = min_bw;
12493 		if (max_valid)
12494 			pf->max_bw = max_bw;
12495 	}
12496 
12497 	return status;
12498 }
12499 
12500 /**
12501  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12502  * @pf: board private structure
12503  **/
12504 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12505 {
12506 	struct i40e_aqc_configure_partition_bw_data bw_data;
12507 	i40e_status status;
12508 
12509 	memset(&bw_data, 0, sizeof(bw_data));
12510 
12511 	/* Set the valid bit for this PF */
12512 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12513 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12514 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12515 
12516 	/* Set the new bandwidths */
12517 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12518 
12519 	return status;
12520 }
12521 
12522 /**
12523  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12524  * @pf: board private structure
12525  **/
12526 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12527 {
12528 	/* Commit temporary BW setting to permanent NVM image */
12529 	enum i40e_admin_queue_err last_aq_status;
12530 	i40e_status ret;
12531 	u16 nvm_word;
12532 
12533 	if (pf->hw.partition_id != 1) {
12534 		dev_info(&pf->pdev->dev,
12535 			 "Commit BW only works on partition 1! This is partition %d",
12536 			 pf->hw.partition_id);
12537 		ret = I40E_NOT_SUPPORTED;
12538 		goto bw_commit_out;
12539 	}
12540 
12541 	/* Acquire NVM for read access */
12542 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12543 	last_aq_status = pf->hw.aq.asq_last_status;
12544 	if (ret) {
12545 		dev_info(&pf->pdev->dev,
12546 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12547 			 i40e_stat_str(&pf->hw, ret),
12548 			 i40e_aq_str(&pf->hw, last_aq_status));
12549 		goto bw_commit_out;
12550 	}
12551 
12552 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12553 	ret = i40e_aq_read_nvm(&pf->hw,
12554 			       I40E_SR_NVM_CONTROL_WORD,
12555 			       0x10, sizeof(nvm_word), &nvm_word,
12556 			       false, NULL);
12557 	/* Save off last admin queue command status before releasing
12558 	 * the NVM
12559 	 */
12560 	last_aq_status = pf->hw.aq.asq_last_status;
12561 	i40e_release_nvm(&pf->hw);
12562 	if (ret) {
12563 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12564 			 i40e_stat_str(&pf->hw, ret),
12565 			 i40e_aq_str(&pf->hw, last_aq_status));
12566 		goto bw_commit_out;
12567 	}
12568 
12569 	/* Wait a bit for NVM release to complete */
12570 	msleep(50);
12571 
12572 	/* Acquire NVM for write access */
12573 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12574 	last_aq_status = pf->hw.aq.asq_last_status;
12575 	if (ret) {
12576 		dev_info(&pf->pdev->dev,
12577 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12578 			 i40e_stat_str(&pf->hw, ret),
12579 			 i40e_aq_str(&pf->hw, last_aq_status));
12580 		goto bw_commit_out;
12581 	}
12582 	/* Write it back out unchanged to initiate update NVM,
12583 	 * which will force a write of the shadow (alt) RAM to
12584 	 * the NVM - thus storing the bandwidth values permanently.
12585 	 */
12586 	ret = i40e_aq_update_nvm(&pf->hw,
12587 				 I40E_SR_NVM_CONTROL_WORD,
12588 				 0x10, sizeof(nvm_word),
12589 				 &nvm_word, true, 0, NULL);
12590 	/* Save off last admin queue command status before releasing
12591 	 * the NVM
12592 	 */
12593 	last_aq_status = pf->hw.aq.asq_last_status;
12594 	i40e_release_nvm(&pf->hw);
12595 	if (ret)
12596 		dev_info(&pf->pdev->dev,
12597 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12598 			 i40e_stat_str(&pf->hw, ret),
12599 			 i40e_aq_str(&pf->hw, last_aq_status));
12600 bw_commit_out:
12601 
12602 	return ret;
12603 }
12604 
12605 /**
12606  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12607  * if total port shutdown feature is enabled for this PF
12608  * @pf: board private structure
12609  **/
12610 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12611 {
12612 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12613 #define I40E_FEATURES_ENABLE_PTR		0x2A
12614 #define I40E_CURRENT_SETTING_PTR		0x2B
12615 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12616 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12617 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12618 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12619 	i40e_status read_status = I40E_SUCCESS;
12620 	u16 sr_emp_sr_settings_ptr = 0;
12621 	u16 features_enable = 0;
12622 	u16 link_behavior = 0;
12623 	bool ret = false;
12624 
12625 	read_status = i40e_read_nvm_word(&pf->hw,
12626 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12627 					 &sr_emp_sr_settings_ptr);
12628 	if (read_status)
12629 		goto err_nvm;
12630 	read_status = i40e_read_nvm_word(&pf->hw,
12631 					 sr_emp_sr_settings_ptr +
12632 					 I40E_FEATURES_ENABLE_PTR,
12633 					 &features_enable);
12634 	if (read_status)
12635 		goto err_nvm;
12636 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12637 		read_status = i40e_read_nvm_module_data(&pf->hw,
12638 							I40E_SR_EMP_SR_SETTINGS_PTR,
12639 							I40E_CURRENT_SETTING_PTR,
12640 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12641 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12642 							&link_behavior);
12643 		if (read_status)
12644 			goto err_nvm;
12645 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12646 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12647 	}
12648 	return ret;
12649 
12650 err_nvm:
12651 	dev_warn(&pf->pdev->dev,
12652 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12653 		 i40e_stat_str(&pf->hw, read_status));
12654 	return ret;
12655 }
12656 
12657 /**
12658  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12659  * @pf: board private structure to initialize
12660  *
12661  * i40e_sw_init initializes the Adapter private data structure.
12662  * Fields are initialized based on PCI device information and
12663  * OS network device settings (MTU size).
12664  **/
12665 static int i40e_sw_init(struct i40e_pf *pf)
12666 {
12667 	int err = 0;
12668 	int size;
12669 	u16 pow;
12670 
12671 	/* Set default capability flags */
12672 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12673 		    I40E_FLAG_MSI_ENABLED     |
12674 		    I40E_FLAG_MSIX_ENABLED;
12675 
12676 	/* Set default ITR */
12677 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12678 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12679 
12680 	/* Depending on PF configurations, it is possible that the RSS
12681 	 * maximum might end up larger than the available queues
12682 	 */
12683 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12684 	pf->alloc_rss_size = 1;
12685 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12686 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12687 				 pf->hw.func_caps.num_tx_qp);
12688 
12689 	/* find the next higher power-of-2 of num cpus */
12690 	pow = roundup_pow_of_two(num_online_cpus());
12691 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12692 
12693 	if (pf->hw.func_caps.rss) {
12694 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12695 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12696 					   num_online_cpus());
12697 	}
12698 
12699 	/* MFP mode enabled */
12700 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12701 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12702 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12703 		if (i40e_get_partition_bw_setting(pf)) {
12704 			dev_warn(&pf->pdev->dev,
12705 				 "Could not get partition bw settings\n");
12706 		} else {
12707 			dev_info(&pf->pdev->dev,
12708 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12709 				 pf->min_bw, pf->max_bw);
12710 
12711 			/* nudge the Tx scheduler */
12712 			i40e_set_partition_bw_setting(pf);
12713 		}
12714 	}
12715 
12716 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12717 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12718 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12719 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12720 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12721 		    pf->hw.num_partitions > 1)
12722 			dev_info(&pf->pdev->dev,
12723 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12724 		else
12725 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12726 		pf->fdir_pf_filter_count =
12727 				 pf->hw.func_caps.fd_filters_guaranteed;
12728 		pf->hw.fdir_shared_filter_count =
12729 				 pf->hw.func_caps.fd_filters_best_effort;
12730 	}
12731 
12732 	if (pf->hw.mac.type == I40E_MAC_X722) {
12733 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12734 				    I40E_HW_128_QP_RSS_CAPABLE |
12735 				    I40E_HW_ATR_EVICT_CAPABLE |
12736 				    I40E_HW_WB_ON_ITR_CAPABLE |
12737 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12738 				    I40E_HW_NO_PCI_LINK_CHECK |
12739 				    I40E_HW_USE_SET_LLDP_MIB |
12740 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12741 				    I40E_HW_PTP_L4_CAPABLE |
12742 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12743 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12744 
12745 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12746 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12747 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12748 			dev_warn(&pf->pdev->dev,
12749 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12750 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12751 		}
12752 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12753 		   ((pf->hw.aq.api_maj_ver == 1) &&
12754 		    (pf->hw.aq.api_min_ver > 4))) {
12755 		/* Supported in FW API version higher than 1.4 */
12756 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12757 	}
12758 
12759 	/* Enable HW ATR eviction if possible */
12760 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12761 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12762 
12763 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12764 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12765 	    (pf->hw.aq.fw_maj_ver < 4))) {
12766 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12767 		/* No DCB support  for FW < v4.33 */
12768 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12769 	}
12770 
12771 	/* Disable FW LLDP if FW < v4.3 */
12772 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12773 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12774 	    (pf->hw.aq.fw_maj_ver < 4)))
12775 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12776 
12777 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12778 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12779 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12780 	    (pf->hw.aq.fw_maj_ver >= 5)))
12781 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12782 
12783 	/* Enable PTP L4 if FW > v6.0 */
12784 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12785 	    pf->hw.aq.fw_maj_ver >= 6)
12786 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12787 
12788 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12789 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12790 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12791 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12792 	}
12793 
12794 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12795 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12796 		/* IWARP needs one extra vector for CQP just like MISC.*/
12797 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12798 	}
12799 	/* Stopping FW LLDP engine is supported on XL710 and X722
12800 	 * starting from FW versions determined in i40e_init_adminq.
12801 	 * Stopping the FW LLDP engine is not supported on XL710
12802 	 * if NPAR is functioning so unset this hw flag in this case.
12803 	 */
12804 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12805 	    pf->hw.func_caps.npar_enable &&
12806 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12807 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12808 
12809 #ifdef CONFIG_PCI_IOV
12810 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12811 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12812 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12813 		pf->num_req_vfs = min_t(int,
12814 					pf->hw.func_caps.num_vfs,
12815 					I40E_MAX_VF_COUNT);
12816 	}
12817 #endif /* CONFIG_PCI_IOV */
12818 	pf->eeprom_version = 0xDEAD;
12819 	pf->lan_veb = I40E_NO_VEB;
12820 	pf->lan_vsi = I40E_NO_VSI;
12821 
12822 	/* By default FW has this off for performance reasons */
12823 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12824 
12825 	/* set up queue assignment tracking */
12826 	size = sizeof(struct i40e_lump_tracking)
12827 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12828 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12829 	if (!pf->qp_pile) {
12830 		err = -ENOMEM;
12831 		goto sw_init_done;
12832 	}
12833 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12834 
12835 	pf->tx_timeout_recovery_level = 1;
12836 
12837 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12838 	    i40e_is_total_port_shutdown_enabled(pf)) {
12839 		/* Link down on close must be on when total port shutdown
12840 		 * is enabled for a given port
12841 		 */
12842 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12843 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12844 		dev_info(&pf->pdev->dev,
12845 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12846 	}
12847 	mutex_init(&pf->switch_mutex);
12848 
12849 sw_init_done:
12850 	return err;
12851 }
12852 
12853 /**
12854  * i40e_set_ntuple - set the ntuple feature flag and take action
12855  * @pf: board private structure to initialize
12856  * @features: the feature set that the stack is suggesting
12857  *
12858  * returns a bool to indicate if reset needs to happen
12859  **/
12860 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12861 {
12862 	bool need_reset = false;
12863 
12864 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12865 	 * the state changed, we need to reset.
12866 	 */
12867 	if (features & NETIF_F_NTUPLE) {
12868 		/* Enable filters and mark for reset */
12869 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12870 			need_reset = true;
12871 		/* enable FD_SB only if there is MSI-X vector and no cloud
12872 		 * filters exist
12873 		 */
12874 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12875 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12876 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12877 		}
12878 	} else {
12879 		/* turn off filters, mark for reset and clear SW filter list */
12880 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12881 			need_reset = true;
12882 			i40e_fdir_filter_exit(pf);
12883 		}
12884 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12885 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12886 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12887 
12888 		/* reset fd counters */
12889 		pf->fd_add_err = 0;
12890 		pf->fd_atr_cnt = 0;
12891 		/* if ATR was auto disabled it can be re-enabled. */
12892 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12893 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12894 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12895 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12896 	}
12897 	return need_reset;
12898 }
12899 
12900 /**
12901  * i40e_clear_rss_lut - clear the rx hash lookup table
12902  * @vsi: the VSI being configured
12903  **/
12904 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12905 {
12906 	struct i40e_pf *pf = vsi->back;
12907 	struct i40e_hw *hw = &pf->hw;
12908 	u16 vf_id = vsi->vf_id;
12909 	u8 i;
12910 
12911 	if (vsi->type == I40E_VSI_MAIN) {
12912 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12913 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12914 	} else if (vsi->type == I40E_VSI_SRIOV) {
12915 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12916 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12917 	} else {
12918 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12919 	}
12920 }
12921 
12922 /**
12923  * i40e_set_features - set the netdev feature flags
12924  * @netdev: ptr to the netdev being adjusted
12925  * @features: the feature set that the stack is suggesting
12926  * Note: expects to be called while under rtnl_lock()
12927  **/
12928 static int i40e_set_features(struct net_device *netdev,
12929 			     netdev_features_t features)
12930 {
12931 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12932 	struct i40e_vsi *vsi = np->vsi;
12933 	struct i40e_pf *pf = vsi->back;
12934 	bool need_reset;
12935 
12936 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12937 		i40e_pf_config_rss(pf);
12938 	else if (!(features & NETIF_F_RXHASH) &&
12939 		 netdev->features & NETIF_F_RXHASH)
12940 		i40e_clear_rss_lut(vsi);
12941 
12942 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12943 		i40e_vlan_stripping_enable(vsi);
12944 	else
12945 		i40e_vlan_stripping_disable(vsi);
12946 
12947 	if (!(features & NETIF_F_HW_TC) &&
12948 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12949 		dev_err(&pf->pdev->dev,
12950 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12951 		return -EINVAL;
12952 	}
12953 
12954 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12955 		i40e_del_all_macvlans(vsi);
12956 
12957 	need_reset = i40e_set_ntuple(pf, features);
12958 
12959 	if (need_reset)
12960 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12961 
12962 	return 0;
12963 }
12964 
12965 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12966 				    unsigned int table, unsigned int idx,
12967 				    struct udp_tunnel_info *ti)
12968 {
12969 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12970 	struct i40e_hw *hw = &np->vsi->back->hw;
12971 	u8 type, filter_index;
12972 	i40e_status ret;
12973 
12974 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12975 						   I40E_AQC_TUNNEL_TYPE_NGE;
12976 
12977 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12978 				     NULL);
12979 	if (ret) {
12980 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12981 			    i40e_stat_str(hw, ret),
12982 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12983 		return -EIO;
12984 	}
12985 
12986 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12987 	return 0;
12988 }
12989 
12990 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12991 				      unsigned int table, unsigned int idx,
12992 				      struct udp_tunnel_info *ti)
12993 {
12994 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12995 	struct i40e_hw *hw = &np->vsi->back->hw;
12996 	i40e_status ret;
12997 
12998 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12999 	if (ret) {
13000 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
13001 			    i40e_stat_str(hw, ret),
13002 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13003 		return -EIO;
13004 	}
13005 
13006 	return 0;
13007 }
13008 
13009 static int i40e_get_phys_port_id(struct net_device *netdev,
13010 				 struct netdev_phys_item_id *ppid)
13011 {
13012 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13013 	struct i40e_pf *pf = np->vsi->back;
13014 	struct i40e_hw *hw = &pf->hw;
13015 
13016 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13017 		return -EOPNOTSUPP;
13018 
13019 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13020 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13021 
13022 	return 0;
13023 }
13024 
13025 /**
13026  * i40e_ndo_fdb_add - add an entry to the hardware database
13027  * @ndm: the input from the stack
13028  * @tb: pointer to array of nladdr (unused)
13029  * @dev: the net device pointer
13030  * @addr: the MAC address entry being added
13031  * @vid: VLAN ID
13032  * @flags: instructions from stack about fdb operation
13033  * @extack: netlink extended ack, unused currently
13034  */
13035 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13036 			    struct net_device *dev,
13037 			    const unsigned char *addr, u16 vid,
13038 			    u16 flags,
13039 			    struct netlink_ext_ack *extack)
13040 {
13041 	struct i40e_netdev_priv *np = netdev_priv(dev);
13042 	struct i40e_pf *pf = np->vsi->back;
13043 	int err = 0;
13044 
13045 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13046 		return -EOPNOTSUPP;
13047 
13048 	if (vid) {
13049 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13050 		return -EINVAL;
13051 	}
13052 
13053 	/* Hardware does not support aging addresses so if a
13054 	 * ndm_state is given only allow permanent addresses
13055 	 */
13056 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13057 		netdev_info(dev, "FDB only supports static addresses\n");
13058 		return -EINVAL;
13059 	}
13060 
13061 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13062 		err = dev_uc_add_excl(dev, addr);
13063 	else if (is_multicast_ether_addr(addr))
13064 		err = dev_mc_add_excl(dev, addr);
13065 	else
13066 		err = -EINVAL;
13067 
13068 	/* Only return duplicate errors if NLM_F_EXCL is set */
13069 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13070 		err = 0;
13071 
13072 	return err;
13073 }
13074 
13075 /**
13076  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13077  * @dev: the netdev being configured
13078  * @nlh: RTNL message
13079  * @flags: bridge flags
13080  * @extack: netlink extended ack
13081  *
13082  * Inserts a new hardware bridge if not already created and
13083  * enables the bridging mode requested (VEB or VEPA). If the
13084  * hardware bridge has already been inserted and the request
13085  * is to change the mode then that requires a PF reset to
13086  * allow rebuild of the components with required hardware
13087  * bridge mode enabled.
13088  *
13089  * Note: expects to be called while under rtnl_lock()
13090  **/
13091 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13092 				   struct nlmsghdr *nlh,
13093 				   u16 flags,
13094 				   struct netlink_ext_ack *extack)
13095 {
13096 	struct i40e_netdev_priv *np = netdev_priv(dev);
13097 	struct i40e_vsi *vsi = np->vsi;
13098 	struct i40e_pf *pf = vsi->back;
13099 	struct i40e_veb *veb = NULL;
13100 	struct nlattr *attr, *br_spec;
13101 	int i, rem;
13102 
13103 	/* Only for PF VSI for now */
13104 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13105 		return -EOPNOTSUPP;
13106 
13107 	/* Find the HW bridge for PF VSI */
13108 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13109 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13110 			veb = pf->veb[i];
13111 	}
13112 
13113 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13114 
13115 	nla_for_each_nested(attr, br_spec, rem) {
13116 		__u16 mode;
13117 
13118 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13119 			continue;
13120 
13121 		mode = nla_get_u16(attr);
13122 		if ((mode != BRIDGE_MODE_VEPA) &&
13123 		    (mode != BRIDGE_MODE_VEB))
13124 			return -EINVAL;
13125 
13126 		/* Insert a new HW bridge */
13127 		if (!veb) {
13128 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13129 					     vsi->tc_config.enabled_tc);
13130 			if (veb) {
13131 				veb->bridge_mode = mode;
13132 				i40e_config_bridge_mode(veb);
13133 			} else {
13134 				/* No Bridge HW offload available */
13135 				return -ENOENT;
13136 			}
13137 			break;
13138 		} else if (mode != veb->bridge_mode) {
13139 			/* Existing HW bridge but different mode needs reset */
13140 			veb->bridge_mode = mode;
13141 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13142 			if (mode == BRIDGE_MODE_VEB)
13143 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13144 			else
13145 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13146 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13147 			break;
13148 		}
13149 	}
13150 
13151 	return 0;
13152 }
13153 
13154 /**
13155  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13156  * @skb: skb buff
13157  * @pid: process id
13158  * @seq: RTNL message seq #
13159  * @dev: the netdev being configured
13160  * @filter_mask: unused
13161  * @nlflags: netlink flags passed in
13162  *
13163  * Return the mode in which the hardware bridge is operating in
13164  * i.e VEB or VEPA.
13165  **/
13166 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13167 				   struct net_device *dev,
13168 				   u32 __always_unused filter_mask,
13169 				   int nlflags)
13170 {
13171 	struct i40e_netdev_priv *np = netdev_priv(dev);
13172 	struct i40e_vsi *vsi = np->vsi;
13173 	struct i40e_pf *pf = vsi->back;
13174 	struct i40e_veb *veb = NULL;
13175 	int i;
13176 
13177 	/* Only for PF VSI for now */
13178 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13179 		return -EOPNOTSUPP;
13180 
13181 	/* Find the HW bridge for the PF VSI */
13182 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13183 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13184 			veb = pf->veb[i];
13185 	}
13186 
13187 	if (!veb)
13188 		return 0;
13189 
13190 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13191 				       0, 0, nlflags, filter_mask, NULL);
13192 }
13193 
13194 /**
13195  * i40e_features_check - Validate encapsulated packet conforms to limits
13196  * @skb: skb buff
13197  * @dev: This physical port's netdev
13198  * @features: Offload features that the stack believes apply
13199  **/
13200 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13201 					     struct net_device *dev,
13202 					     netdev_features_t features)
13203 {
13204 	size_t len;
13205 
13206 	/* No point in doing any of this if neither checksum nor GSO are
13207 	 * being requested for this frame.  We can rule out both by just
13208 	 * checking for CHECKSUM_PARTIAL
13209 	 */
13210 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13211 		return features;
13212 
13213 	/* We cannot support GSO if the MSS is going to be less than
13214 	 * 64 bytes.  If it is then we need to drop support for GSO.
13215 	 */
13216 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13217 		features &= ~NETIF_F_GSO_MASK;
13218 
13219 	/* MACLEN can support at most 63 words */
13220 	len = skb_network_header(skb) - skb->data;
13221 	if (len & ~(63 * 2))
13222 		goto out_err;
13223 
13224 	/* IPLEN and EIPLEN can support at most 127 dwords */
13225 	len = skb_transport_header(skb) - skb_network_header(skb);
13226 	if (len & ~(127 * 4))
13227 		goto out_err;
13228 
13229 	if (skb->encapsulation) {
13230 		/* L4TUNLEN can support 127 words */
13231 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13232 		if (len & ~(127 * 2))
13233 			goto out_err;
13234 
13235 		/* IPLEN can support at most 127 dwords */
13236 		len = skb_inner_transport_header(skb) -
13237 		      skb_inner_network_header(skb);
13238 		if (len & ~(127 * 4))
13239 			goto out_err;
13240 	}
13241 
13242 	/* No need to validate L4LEN as TCP is the only protocol with a
13243 	 * flexible value and we support all possible values supported
13244 	 * by TCP, which is at most 15 dwords
13245 	 */
13246 
13247 	return features;
13248 out_err:
13249 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13250 }
13251 
13252 /**
13253  * i40e_xdp_setup - add/remove an XDP program
13254  * @vsi: VSI to changed
13255  * @prog: XDP program
13256  * @extack: netlink extended ack
13257  **/
13258 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13259 			  struct netlink_ext_ack *extack)
13260 {
13261 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13262 	struct i40e_pf *pf = vsi->back;
13263 	struct bpf_prog *old_prog;
13264 	bool need_reset;
13265 	int i;
13266 
13267 	/* Don't allow frames that span over multiple buffers */
13268 	if (frame_size > vsi->rx_buf_len) {
13269 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13270 		return -EINVAL;
13271 	}
13272 
13273 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13274 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13275 
13276 	if (need_reset)
13277 		i40e_prep_for_reset(pf);
13278 
13279 	/* VSI shall be deleted in a moment, just return EINVAL */
13280 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13281 		return -EINVAL;
13282 
13283 	old_prog = xchg(&vsi->xdp_prog, prog);
13284 
13285 	if (need_reset) {
13286 		if (!prog)
13287 			/* Wait until ndo_xsk_wakeup completes. */
13288 			synchronize_rcu();
13289 		i40e_reset_and_rebuild(pf, true, true);
13290 	}
13291 
13292 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13293 		if (i40e_realloc_rx_bi_zc(vsi, true))
13294 			return -ENOMEM;
13295 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13296 		if (i40e_realloc_rx_bi_zc(vsi, false))
13297 			return -ENOMEM;
13298 	}
13299 
13300 	for (i = 0; i < vsi->num_queue_pairs; i++)
13301 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13302 
13303 	if (old_prog)
13304 		bpf_prog_put(old_prog);
13305 
13306 	/* Kick start the NAPI context if there is an AF_XDP socket open
13307 	 * on that queue id. This so that receiving will start.
13308 	 */
13309 	if (need_reset && prog)
13310 		for (i = 0; i < vsi->num_queue_pairs; i++)
13311 			if (vsi->xdp_rings[i]->xsk_pool)
13312 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13313 						      XDP_WAKEUP_RX);
13314 
13315 	return 0;
13316 }
13317 
13318 /**
13319  * i40e_enter_busy_conf - Enters busy config state
13320  * @vsi: vsi
13321  *
13322  * Returns 0 on success, <0 for failure.
13323  **/
13324 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13325 {
13326 	struct i40e_pf *pf = vsi->back;
13327 	int timeout = 50;
13328 
13329 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13330 		timeout--;
13331 		if (!timeout)
13332 			return -EBUSY;
13333 		usleep_range(1000, 2000);
13334 	}
13335 
13336 	return 0;
13337 }
13338 
13339 /**
13340  * i40e_exit_busy_conf - Exits busy config state
13341  * @vsi: vsi
13342  **/
13343 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13344 {
13345 	struct i40e_pf *pf = vsi->back;
13346 
13347 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13348 }
13349 
13350 /**
13351  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13352  * @vsi: vsi
13353  * @queue_pair: queue pair
13354  **/
13355 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13356 {
13357 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13358 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13359 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13360 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13361 	if (i40e_enabled_xdp_vsi(vsi)) {
13362 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13363 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13364 	}
13365 }
13366 
13367 /**
13368  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13369  * @vsi: vsi
13370  * @queue_pair: queue pair
13371  **/
13372 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13373 {
13374 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13375 	if (i40e_enabled_xdp_vsi(vsi)) {
13376 		/* Make sure that in-progress ndo_xdp_xmit calls are
13377 		 * completed.
13378 		 */
13379 		synchronize_rcu();
13380 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13381 	}
13382 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13383 }
13384 
13385 /**
13386  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13387  * @vsi: vsi
13388  * @queue_pair: queue pair
13389  * @enable: true for enable, false for disable
13390  **/
13391 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13392 					bool enable)
13393 {
13394 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13395 	struct i40e_q_vector *q_vector = rxr->q_vector;
13396 
13397 	if (!vsi->netdev)
13398 		return;
13399 
13400 	/* All rings in a qp belong to the same qvector. */
13401 	if (q_vector->rx.ring || q_vector->tx.ring) {
13402 		if (enable)
13403 			napi_enable(&q_vector->napi);
13404 		else
13405 			napi_disable(&q_vector->napi);
13406 	}
13407 }
13408 
13409 /**
13410  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13411  * @vsi: vsi
13412  * @queue_pair: queue pair
13413  * @enable: true for enable, false for disable
13414  *
13415  * Returns 0 on success, <0 on failure.
13416  **/
13417 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13418 					bool enable)
13419 {
13420 	struct i40e_pf *pf = vsi->back;
13421 	int pf_q, ret = 0;
13422 
13423 	pf_q = vsi->base_queue + queue_pair;
13424 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13425 				     false /*is xdp*/, enable);
13426 	if (ret) {
13427 		dev_info(&pf->pdev->dev,
13428 			 "VSI seid %d Tx ring %d %sable timeout\n",
13429 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13430 		return ret;
13431 	}
13432 
13433 	i40e_control_rx_q(pf, pf_q, enable);
13434 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13435 	if (ret) {
13436 		dev_info(&pf->pdev->dev,
13437 			 "VSI seid %d Rx ring %d %sable timeout\n",
13438 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13439 		return ret;
13440 	}
13441 
13442 	/* Due to HW errata, on Rx disable only, the register can
13443 	 * indicate done before it really is. Needs 50ms to be sure
13444 	 */
13445 	if (!enable)
13446 		mdelay(50);
13447 
13448 	if (!i40e_enabled_xdp_vsi(vsi))
13449 		return ret;
13450 
13451 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13452 				     pf_q + vsi->alloc_queue_pairs,
13453 				     true /*is xdp*/, enable);
13454 	if (ret) {
13455 		dev_info(&pf->pdev->dev,
13456 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13457 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13458 	}
13459 
13460 	return ret;
13461 }
13462 
13463 /**
13464  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13465  * @vsi: vsi
13466  * @queue_pair: queue_pair
13467  **/
13468 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13469 {
13470 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13471 	struct i40e_pf *pf = vsi->back;
13472 	struct i40e_hw *hw = &pf->hw;
13473 
13474 	/* All rings in a qp belong to the same qvector. */
13475 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13476 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13477 	else
13478 		i40e_irq_dynamic_enable_icr0(pf);
13479 
13480 	i40e_flush(hw);
13481 }
13482 
13483 /**
13484  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13485  * @vsi: vsi
13486  * @queue_pair: queue_pair
13487  **/
13488 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13489 {
13490 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13491 	struct i40e_pf *pf = vsi->back;
13492 	struct i40e_hw *hw = &pf->hw;
13493 
13494 	/* For simplicity, instead of removing the qp interrupt causes
13495 	 * from the interrupt linked list, we simply disable the interrupt, and
13496 	 * leave the list intact.
13497 	 *
13498 	 * All rings in a qp belong to the same qvector.
13499 	 */
13500 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13501 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13502 
13503 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13504 		i40e_flush(hw);
13505 		synchronize_irq(pf->msix_entries[intpf].vector);
13506 	} else {
13507 		/* Legacy and MSI mode - this stops all interrupt handling */
13508 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13509 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13510 		i40e_flush(hw);
13511 		synchronize_irq(pf->pdev->irq);
13512 	}
13513 }
13514 
13515 /**
13516  * i40e_queue_pair_disable - Disables a queue pair
13517  * @vsi: vsi
13518  * @queue_pair: queue pair
13519  *
13520  * Returns 0 on success, <0 on failure.
13521  **/
13522 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13523 {
13524 	int err;
13525 
13526 	err = i40e_enter_busy_conf(vsi);
13527 	if (err)
13528 		return err;
13529 
13530 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13531 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13532 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13533 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13534 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13535 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13536 
13537 	return err;
13538 }
13539 
13540 /**
13541  * i40e_queue_pair_enable - Enables a queue pair
13542  * @vsi: vsi
13543  * @queue_pair: queue pair
13544  *
13545  * Returns 0 on success, <0 on failure.
13546  **/
13547 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13548 {
13549 	int err;
13550 
13551 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13552 	if (err)
13553 		return err;
13554 
13555 	if (i40e_enabled_xdp_vsi(vsi)) {
13556 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13557 		if (err)
13558 			return err;
13559 	}
13560 
13561 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13562 	if (err)
13563 		return err;
13564 
13565 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13566 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13567 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13568 
13569 	i40e_exit_busy_conf(vsi);
13570 
13571 	return err;
13572 }
13573 
13574 /**
13575  * i40e_xdp - implements ndo_bpf for i40e
13576  * @dev: netdevice
13577  * @xdp: XDP command
13578  **/
13579 static int i40e_xdp(struct net_device *dev,
13580 		    struct netdev_bpf *xdp)
13581 {
13582 	struct i40e_netdev_priv *np = netdev_priv(dev);
13583 	struct i40e_vsi *vsi = np->vsi;
13584 
13585 	if (vsi->type != I40E_VSI_MAIN)
13586 		return -EINVAL;
13587 
13588 	switch (xdp->command) {
13589 	case XDP_SETUP_PROG:
13590 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13591 	case XDP_SETUP_XSK_POOL:
13592 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13593 					   xdp->xsk.queue_id);
13594 	default:
13595 		return -EINVAL;
13596 	}
13597 }
13598 
13599 static const struct net_device_ops i40e_netdev_ops = {
13600 	.ndo_open		= i40e_open,
13601 	.ndo_stop		= i40e_close,
13602 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13603 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13604 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13605 	.ndo_validate_addr	= eth_validate_addr,
13606 	.ndo_set_mac_address	= i40e_set_mac,
13607 	.ndo_change_mtu		= i40e_change_mtu,
13608 	.ndo_eth_ioctl		= i40e_ioctl,
13609 	.ndo_tx_timeout		= i40e_tx_timeout,
13610 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13611 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13612 #ifdef CONFIG_NET_POLL_CONTROLLER
13613 	.ndo_poll_controller	= i40e_netpoll,
13614 #endif
13615 	.ndo_setup_tc		= __i40e_setup_tc,
13616 	.ndo_select_queue	= i40e_lan_select_queue,
13617 	.ndo_set_features	= i40e_set_features,
13618 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13619 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13620 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13621 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13622 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13623 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13624 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13625 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13626 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13627 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13628 	.ndo_features_check	= i40e_features_check,
13629 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13630 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13631 	.ndo_bpf		= i40e_xdp,
13632 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13633 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13634 	.ndo_dfwd_add_station	= i40e_fwd_add,
13635 	.ndo_dfwd_del_station	= i40e_fwd_del,
13636 };
13637 
13638 /**
13639  * i40e_config_netdev - Setup the netdev flags
13640  * @vsi: the VSI being configured
13641  *
13642  * Returns 0 on success, negative value on failure
13643  **/
13644 static int i40e_config_netdev(struct i40e_vsi *vsi)
13645 {
13646 	struct i40e_pf *pf = vsi->back;
13647 	struct i40e_hw *hw = &pf->hw;
13648 	struct i40e_netdev_priv *np;
13649 	struct net_device *netdev;
13650 	u8 broadcast[ETH_ALEN];
13651 	u8 mac_addr[ETH_ALEN];
13652 	int etherdev_size;
13653 	netdev_features_t hw_enc_features;
13654 	netdev_features_t hw_features;
13655 
13656 	etherdev_size = sizeof(struct i40e_netdev_priv);
13657 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13658 	if (!netdev)
13659 		return -ENOMEM;
13660 
13661 	vsi->netdev = netdev;
13662 	np = netdev_priv(netdev);
13663 	np->vsi = vsi;
13664 
13665 	hw_enc_features = NETIF_F_SG			|
13666 			  NETIF_F_HW_CSUM		|
13667 			  NETIF_F_HIGHDMA		|
13668 			  NETIF_F_SOFT_FEATURES		|
13669 			  NETIF_F_TSO			|
13670 			  NETIF_F_TSO_ECN		|
13671 			  NETIF_F_TSO6			|
13672 			  NETIF_F_GSO_GRE		|
13673 			  NETIF_F_GSO_GRE_CSUM		|
13674 			  NETIF_F_GSO_PARTIAL		|
13675 			  NETIF_F_GSO_IPXIP4		|
13676 			  NETIF_F_GSO_IPXIP6		|
13677 			  NETIF_F_GSO_UDP_TUNNEL	|
13678 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13679 			  NETIF_F_GSO_UDP_L4		|
13680 			  NETIF_F_SCTP_CRC		|
13681 			  NETIF_F_RXHASH		|
13682 			  NETIF_F_RXCSUM		|
13683 			  0;
13684 
13685 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13686 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13687 
13688 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13689 
13690 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13691 
13692 	netdev->hw_enc_features |= hw_enc_features;
13693 
13694 	/* record features VLANs can make use of */
13695 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13696 
13697 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13698 				   NETIF_F_GSO_GRE_CSUM |	\
13699 				   NETIF_F_GSO_IPXIP4 |		\
13700 				   NETIF_F_GSO_IPXIP6 |		\
13701 				   NETIF_F_GSO_UDP_TUNNEL |	\
13702 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13703 
13704 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13705 	netdev->features |= NETIF_F_GSO_PARTIAL |
13706 			    I40E_GSO_PARTIAL_FEATURES;
13707 
13708 	netdev->mpls_features |= NETIF_F_SG;
13709 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13710 	netdev->mpls_features |= NETIF_F_TSO;
13711 	netdev->mpls_features |= NETIF_F_TSO6;
13712 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13713 
13714 	/* enable macvlan offloads */
13715 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13716 
13717 	hw_features = hw_enc_features		|
13718 		      NETIF_F_HW_VLAN_CTAG_TX	|
13719 		      NETIF_F_HW_VLAN_CTAG_RX;
13720 
13721 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13722 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13723 
13724 	netdev->hw_features |= hw_features;
13725 
13726 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13727 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13728 
13729 	netdev->features &= ~NETIF_F_HW_TC;
13730 
13731 	if (vsi->type == I40E_VSI_MAIN) {
13732 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13733 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13734 		/* The following steps are necessary for two reasons. First,
13735 		 * some older NVM configurations load a default MAC-VLAN
13736 		 * filter that will accept any tagged packet, and we want to
13737 		 * replace this with a normal filter. Additionally, it is
13738 		 * possible our MAC address was provided by the platform using
13739 		 * Open Firmware or similar.
13740 		 *
13741 		 * Thus, we need to remove the default filter and install one
13742 		 * specific to the MAC address.
13743 		 */
13744 		i40e_rm_default_mac_filter(vsi, mac_addr);
13745 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13746 		i40e_add_mac_filter(vsi, mac_addr);
13747 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13748 	} else {
13749 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13750 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13751 		 * the end, which is 4 bytes long, so force truncation of the
13752 		 * original name by IFNAMSIZ - 4
13753 		 */
13754 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13755 			 IFNAMSIZ - 4,
13756 			 pf->vsi[pf->lan_vsi]->netdev->name);
13757 		eth_random_addr(mac_addr);
13758 
13759 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13760 		i40e_add_mac_filter(vsi, mac_addr);
13761 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13762 	}
13763 
13764 	/* Add the broadcast filter so that we initially will receive
13765 	 * broadcast packets. Note that when a new VLAN is first added the
13766 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13767 	 * specific filters as part of transitioning into "vlan" operation.
13768 	 * When more VLANs are added, the driver will copy each existing MAC
13769 	 * filter and add it for the new VLAN.
13770 	 *
13771 	 * Broadcast filters are handled specially by
13772 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13773 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13774 	 * filter. The subtask will update the correct broadcast promiscuous
13775 	 * bits as VLANs become active or inactive.
13776 	 */
13777 	eth_broadcast_addr(broadcast);
13778 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13779 	i40e_add_mac_filter(vsi, broadcast);
13780 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13781 
13782 	eth_hw_addr_set(netdev, mac_addr);
13783 	ether_addr_copy(netdev->perm_addr, mac_addr);
13784 
13785 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13786 	netdev->neigh_priv_len = sizeof(u32) * 4;
13787 
13788 	netdev->priv_flags |= IFF_UNICAST_FLT;
13789 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13790 	/* Setup netdev TC information */
13791 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13792 
13793 	netdev->netdev_ops = &i40e_netdev_ops;
13794 	netdev->watchdog_timeo = 5 * HZ;
13795 	i40e_set_ethtool_ops(netdev);
13796 
13797 	/* MTU range: 68 - 9706 */
13798 	netdev->min_mtu = ETH_MIN_MTU;
13799 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13800 
13801 	return 0;
13802 }
13803 
13804 /**
13805  * i40e_vsi_delete - Delete a VSI from the switch
13806  * @vsi: the VSI being removed
13807  *
13808  * Returns 0 on success, negative value on failure
13809  **/
13810 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13811 {
13812 	/* remove default VSI is not allowed */
13813 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13814 		return;
13815 
13816 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13817 }
13818 
13819 /**
13820  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13821  * @vsi: the VSI being queried
13822  *
13823  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13824  **/
13825 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13826 {
13827 	struct i40e_veb *veb;
13828 	struct i40e_pf *pf = vsi->back;
13829 
13830 	/* Uplink is not a bridge so default to VEB */
13831 	if (vsi->veb_idx >= I40E_MAX_VEB)
13832 		return 1;
13833 
13834 	veb = pf->veb[vsi->veb_idx];
13835 	if (!veb) {
13836 		dev_info(&pf->pdev->dev,
13837 			 "There is no veb associated with the bridge\n");
13838 		return -ENOENT;
13839 	}
13840 
13841 	/* Uplink is a bridge in VEPA mode */
13842 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13843 		return 0;
13844 	} else {
13845 		/* Uplink is a bridge in VEB mode */
13846 		return 1;
13847 	}
13848 
13849 	/* VEPA is now default bridge, so return 0 */
13850 	return 0;
13851 }
13852 
13853 /**
13854  * i40e_add_vsi - Add a VSI to the switch
13855  * @vsi: the VSI being configured
13856  *
13857  * This initializes a VSI context depending on the VSI type to be added and
13858  * passes it down to the add_vsi aq command.
13859  **/
13860 static int i40e_add_vsi(struct i40e_vsi *vsi)
13861 {
13862 	int ret = -ENODEV;
13863 	struct i40e_pf *pf = vsi->back;
13864 	struct i40e_hw *hw = &pf->hw;
13865 	struct i40e_vsi_context ctxt;
13866 	struct i40e_mac_filter *f;
13867 	struct hlist_node *h;
13868 	int bkt;
13869 
13870 	u8 enabled_tc = 0x1; /* TC0 enabled */
13871 	int f_count = 0;
13872 
13873 	memset(&ctxt, 0, sizeof(ctxt));
13874 	switch (vsi->type) {
13875 	case I40E_VSI_MAIN:
13876 		/* The PF's main VSI is already setup as part of the
13877 		 * device initialization, so we'll not bother with
13878 		 * the add_vsi call, but we will retrieve the current
13879 		 * VSI context.
13880 		 */
13881 		ctxt.seid = pf->main_vsi_seid;
13882 		ctxt.pf_num = pf->hw.pf_id;
13883 		ctxt.vf_num = 0;
13884 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13885 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13886 		if (ret) {
13887 			dev_info(&pf->pdev->dev,
13888 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13889 				 i40e_stat_str(&pf->hw, ret),
13890 				 i40e_aq_str(&pf->hw,
13891 					     pf->hw.aq.asq_last_status));
13892 			return -ENOENT;
13893 		}
13894 		vsi->info = ctxt.info;
13895 		vsi->info.valid_sections = 0;
13896 
13897 		vsi->seid = ctxt.seid;
13898 		vsi->id = ctxt.vsi_number;
13899 
13900 		enabled_tc = i40e_pf_get_tc_map(pf);
13901 
13902 		/* Source pruning is enabled by default, so the flag is
13903 		 * negative logic - if it's set, we need to fiddle with
13904 		 * the VSI to disable source pruning.
13905 		 */
13906 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13907 			memset(&ctxt, 0, sizeof(ctxt));
13908 			ctxt.seid = pf->main_vsi_seid;
13909 			ctxt.pf_num = pf->hw.pf_id;
13910 			ctxt.vf_num = 0;
13911 			ctxt.info.valid_sections |=
13912 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13913 			ctxt.info.switch_id =
13914 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13915 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13916 			if (ret) {
13917 				dev_info(&pf->pdev->dev,
13918 					 "update vsi failed, err %s aq_err %s\n",
13919 					 i40e_stat_str(&pf->hw, ret),
13920 					 i40e_aq_str(&pf->hw,
13921 						     pf->hw.aq.asq_last_status));
13922 				ret = -ENOENT;
13923 				goto err;
13924 			}
13925 		}
13926 
13927 		/* MFP mode setup queue map and update VSI */
13928 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13929 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13930 			memset(&ctxt, 0, sizeof(ctxt));
13931 			ctxt.seid = pf->main_vsi_seid;
13932 			ctxt.pf_num = pf->hw.pf_id;
13933 			ctxt.vf_num = 0;
13934 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13935 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13936 			if (ret) {
13937 				dev_info(&pf->pdev->dev,
13938 					 "update vsi failed, err %s aq_err %s\n",
13939 					 i40e_stat_str(&pf->hw, ret),
13940 					 i40e_aq_str(&pf->hw,
13941 						    pf->hw.aq.asq_last_status));
13942 				ret = -ENOENT;
13943 				goto err;
13944 			}
13945 			/* update the local VSI info queue map */
13946 			i40e_vsi_update_queue_map(vsi, &ctxt);
13947 			vsi->info.valid_sections = 0;
13948 		} else {
13949 			/* Default/Main VSI is only enabled for TC0
13950 			 * reconfigure it to enable all TCs that are
13951 			 * available on the port in SFP mode.
13952 			 * For MFP case the iSCSI PF would use this
13953 			 * flow to enable LAN+iSCSI TC.
13954 			 */
13955 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13956 			if (ret) {
13957 				/* Single TC condition is not fatal,
13958 				 * message and continue
13959 				 */
13960 				dev_info(&pf->pdev->dev,
13961 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13962 					 enabled_tc,
13963 					 i40e_stat_str(&pf->hw, ret),
13964 					 i40e_aq_str(&pf->hw,
13965 						    pf->hw.aq.asq_last_status));
13966 			}
13967 		}
13968 		break;
13969 
13970 	case I40E_VSI_FDIR:
13971 		ctxt.pf_num = hw->pf_id;
13972 		ctxt.vf_num = 0;
13973 		ctxt.uplink_seid = vsi->uplink_seid;
13974 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13975 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13976 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13977 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13978 			ctxt.info.valid_sections |=
13979 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13980 			ctxt.info.switch_id =
13981 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13982 		}
13983 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13984 		break;
13985 
13986 	case I40E_VSI_VMDQ2:
13987 		ctxt.pf_num = hw->pf_id;
13988 		ctxt.vf_num = 0;
13989 		ctxt.uplink_seid = vsi->uplink_seid;
13990 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13991 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13992 
13993 		/* This VSI is connected to VEB so the switch_id
13994 		 * should be set to zero by default.
13995 		 */
13996 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13997 			ctxt.info.valid_sections |=
13998 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13999 			ctxt.info.switch_id =
14000 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14001 		}
14002 
14003 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14004 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14005 		break;
14006 
14007 	case I40E_VSI_SRIOV:
14008 		ctxt.pf_num = hw->pf_id;
14009 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14010 		ctxt.uplink_seid = vsi->uplink_seid;
14011 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14012 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14013 
14014 		/* This VSI is connected to VEB so the switch_id
14015 		 * should be set to zero by default.
14016 		 */
14017 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14018 			ctxt.info.valid_sections |=
14019 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14020 			ctxt.info.switch_id =
14021 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14022 		}
14023 
14024 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14025 			ctxt.info.valid_sections |=
14026 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14027 			ctxt.info.queueing_opt_flags |=
14028 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14029 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14030 		}
14031 
14032 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14033 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14034 		if (pf->vf[vsi->vf_id].spoofchk) {
14035 			ctxt.info.valid_sections |=
14036 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14037 			ctxt.info.sec_flags |=
14038 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14039 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14040 		}
14041 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14042 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14043 		break;
14044 
14045 	case I40E_VSI_IWARP:
14046 		/* send down message to iWARP */
14047 		break;
14048 
14049 	default:
14050 		return -ENODEV;
14051 	}
14052 
14053 	if (vsi->type != I40E_VSI_MAIN) {
14054 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14055 		if (ret) {
14056 			dev_info(&vsi->back->pdev->dev,
14057 				 "add vsi failed, err %s aq_err %s\n",
14058 				 i40e_stat_str(&pf->hw, ret),
14059 				 i40e_aq_str(&pf->hw,
14060 					     pf->hw.aq.asq_last_status));
14061 			ret = -ENOENT;
14062 			goto err;
14063 		}
14064 		vsi->info = ctxt.info;
14065 		vsi->info.valid_sections = 0;
14066 		vsi->seid = ctxt.seid;
14067 		vsi->id = ctxt.vsi_number;
14068 	}
14069 
14070 	vsi->active_filters = 0;
14071 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14072 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14073 	/* If macvlan filters already exist, force them to get loaded */
14074 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14075 		f->state = I40E_FILTER_NEW;
14076 		f_count++;
14077 	}
14078 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14079 
14080 	if (f_count) {
14081 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14082 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14083 	}
14084 
14085 	/* Update VSI BW information */
14086 	ret = i40e_vsi_get_bw_info(vsi);
14087 	if (ret) {
14088 		dev_info(&pf->pdev->dev,
14089 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14090 			 i40e_stat_str(&pf->hw, ret),
14091 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14092 		/* VSI is already added so not tearing that up */
14093 		ret = 0;
14094 	}
14095 
14096 err:
14097 	return ret;
14098 }
14099 
14100 /**
14101  * i40e_vsi_release - Delete a VSI and free its resources
14102  * @vsi: the VSI being removed
14103  *
14104  * Returns 0 on success or < 0 on error
14105  **/
14106 int i40e_vsi_release(struct i40e_vsi *vsi)
14107 {
14108 	struct i40e_mac_filter *f;
14109 	struct hlist_node *h;
14110 	struct i40e_veb *veb = NULL;
14111 	struct i40e_pf *pf;
14112 	u16 uplink_seid;
14113 	int i, n, bkt;
14114 
14115 	pf = vsi->back;
14116 
14117 	/* release of a VEB-owner or last VSI is not allowed */
14118 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14119 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14120 			 vsi->seid, vsi->uplink_seid);
14121 		return -ENODEV;
14122 	}
14123 	if (vsi == pf->vsi[pf->lan_vsi] &&
14124 	    !test_bit(__I40E_DOWN, pf->state)) {
14125 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14126 		return -ENODEV;
14127 	}
14128 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14129 	uplink_seid = vsi->uplink_seid;
14130 	if (vsi->type != I40E_VSI_SRIOV) {
14131 		if (vsi->netdev_registered) {
14132 			vsi->netdev_registered = false;
14133 			if (vsi->netdev) {
14134 				/* results in a call to i40e_close() */
14135 				unregister_netdev(vsi->netdev);
14136 			}
14137 		} else {
14138 			i40e_vsi_close(vsi);
14139 		}
14140 		i40e_vsi_disable_irq(vsi);
14141 	}
14142 
14143 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14144 
14145 	/* clear the sync flag on all filters */
14146 	if (vsi->netdev) {
14147 		__dev_uc_unsync(vsi->netdev, NULL);
14148 		__dev_mc_unsync(vsi->netdev, NULL);
14149 	}
14150 
14151 	/* make sure any remaining filters are marked for deletion */
14152 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14153 		__i40e_del_filter(vsi, f);
14154 
14155 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14156 
14157 	i40e_sync_vsi_filters(vsi);
14158 
14159 	i40e_vsi_delete(vsi);
14160 	i40e_vsi_free_q_vectors(vsi);
14161 	if (vsi->netdev) {
14162 		free_netdev(vsi->netdev);
14163 		vsi->netdev = NULL;
14164 	}
14165 	i40e_vsi_clear_rings(vsi);
14166 	i40e_vsi_clear(vsi);
14167 
14168 	/* If this was the last thing on the VEB, except for the
14169 	 * controlling VSI, remove the VEB, which puts the controlling
14170 	 * VSI onto the next level down in the switch.
14171 	 *
14172 	 * Well, okay, there's one more exception here: don't remove
14173 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14174 	 * from up the network stack.
14175 	 */
14176 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14177 		if (pf->vsi[i] &&
14178 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14179 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14180 			n++;      /* count the VSIs */
14181 		}
14182 	}
14183 	for (i = 0; i < I40E_MAX_VEB; i++) {
14184 		if (!pf->veb[i])
14185 			continue;
14186 		if (pf->veb[i]->uplink_seid == uplink_seid)
14187 			n++;     /* count the VEBs */
14188 		if (pf->veb[i]->seid == uplink_seid)
14189 			veb = pf->veb[i];
14190 	}
14191 	if (n == 0 && veb && veb->uplink_seid != 0)
14192 		i40e_veb_release(veb);
14193 
14194 	return 0;
14195 }
14196 
14197 /**
14198  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14199  * @vsi: ptr to the VSI
14200  *
14201  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14202  * corresponding SW VSI structure and initializes num_queue_pairs for the
14203  * newly allocated VSI.
14204  *
14205  * Returns 0 on success or negative on failure
14206  **/
14207 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14208 {
14209 	int ret = -ENOENT;
14210 	struct i40e_pf *pf = vsi->back;
14211 
14212 	if (vsi->q_vectors[0]) {
14213 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14214 			 vsi->seid);
14215 		return -EEXIST;
14216 	}
14217 
14218 	if (vsi->base_vector) {
14219 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14220 			 vsi->seid, vsi->base_vector);
14221 		return -EEXIST;
14222 	}
14223 
14224 	ret = i40e_vsi_alloc_q_vectors(vsi);
14225 	if (ret) {
14226 		dev_info(&pf->pdev->dev,
14227 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14228 			 vsi->num_q_vectors, vsi->seid, ret);
14229 		vsi->num_q_vectors = 0;
14230 		goto vector_setup_out;
14231 	}
14232 
14233 	/* In Legacy mode, we do not have to get any other vector since we
14234 	 * piggyback on the misc/ICR0 for queue interrupts.
14235 	*/
14236 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14237 		return ret;
14238 	if (vsi->num_q_vectors)
14239 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14240 						 vsi->num_q_vectors, vsi->idx);
14241 	if (vsi->base_vector < 0) {
14242 		dev_info(&pf->pdev->dev,
14243 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14244 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14245 		i40e_vsi_free_q_vectors(vsi);
14246 		ret = -ENOENT;
14247 		goto vector_setup_out;
14248 	}
14249 
14250 vector_setup_out:
14251 	return ret;
14252 }
14253 
14254 /**
14255  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14256  * @vsi: pointer to the vsi.
14257  *
14258  * This re-allocates a vsi's queue resources.
14259  *
14260  * Returns pointer to the successfully allocated and configured VSI sw struct
14261  * on success, otherwise returns NULL on failure.
14262  **/
14263 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14264 {
14265 	u16 alloc_queue_pairs;
14266 	struct i40e_pf *pf;
14267 	u8 enabled_tc;
14268 	int ret;
14269 
14270 	if (!vsi)
14271 		return NULL;
14272 
14273 	pf = vsi->back;
14274 
14275 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14276 	i40e_vsi_clear_rings(vsi);
14277 
14278 	i40e_vsi_free_arrays(vsi, false);
14279 	i40e_set_num_rings_in_vsi(vsi);
14280 	ret = i40e_vsi_alloc_arrays(vsi, false);
14281 	if (ret)
14282 		goto err_vsi;
14283 
14284 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14285 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14286 
14287 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14288 	if (ret < 0) {
14289 		dev_info(&pf->pdev->dev,
14290 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14291 			 alloc_queue_pairs, vsi->seid, ret);
14292 		goto err_vsi;
14293 	}
14294 	vsi->base_queue = ret;
14295 
14296 	/* Update the FW view of the VSI. Force a reset of TC and queue
14297 	 * layout configurations.
14298 	 */
14299 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14300 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14301 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14302 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14303 	if (vsi->type == I40E_VSI_MAIN)
14304 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14305 
14306 	/* assign it some queues */
14307 	ret = i40e_alloc_rings(vsi);
14308 	if (ret)
14309 		goto err_rings;
14310 
14311 	/* map all of the rings to the q_vectors */
14312 	i40e_vsi_map_rings_to_vectors(vsi);
14313 	return vsi;
14314 
14315 err_rings:
14316 	i40e_vsi_free_q_vectors(vsi);
14317 	if (vsi->netdev_registered) {
14318 		vsi->netdev_registered = false;
14319 		unregister_netdev(vsi->netdev);
14320 		free_netdev(vsi->netdev);
14321 		vsi->netdev = NULL;
14322 	}
14323 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14324 err_vsi:
14325 	i40e_vsi_clear(vsi);
14326 	return NULL;
14327 }
14328 
14329 /**
14330  * i40e_vsi_setup - Set up a VSI by a given type
14331  * @pf: board private structure
14332  * @type: VSI type
14333  * @uplink_seid: the switch element to link to
14334  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14335  *
14336  * This allocates the sw VSI structure and its queue resources, then add a VSI
14337  * to the identified VEB.
14338  *
14339  * Returns pointer to the successfully allocated and configure VSI sw struct on
14340  * success, otherwise returns NULL on failure.
14341  **/
14342 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14343 				u16 uplink_seid, u32 param1)
14344 {
14345 	struct i40e_vsi *vsi = NULL;
14346 	struct i40e_veb *veb = NULL;
14347 	u16 alloc_queue_pairs;
14348 	int ret, i;
14349 	int v_idx;
14350 
14351 	/* The requested uplink_seid must be either
14352 	 *     - the PF's port seid
14353 	 *              no VEB is needed because this is the PF
14354 	 *              or this is a Flow Director special case VSI
14355 	 *     - seid of an existing VEB
14356 	 *     - seid of a VSI that owns an existing VEB
14357 	 *     - seid of a VSI that doesn't own a VEB
14358 	 *              a new VEB is created and the VSI becomes the owner
14359 	 *     - seid of the PF VSI, which is what creates the first VEB
14360 	 *              this is a special case of the previous
14361 	 *
14362 	 * Find which uplink_seid we were given and create a new VEB if needed
14363 	 */
14364 	for (i = 0; i < I40E_MAX_VEB; i++) {
14365 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14366 			veb = pf->veb[i];
14367 			break;
14368 		}
14369 	}
14370 
14371 	if (!veb && uplink_seid != pf->mac_seid) {
14372 
14373 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14374 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14375 				vsi = pf->vsi[i];
14376 				break;
14377 			}
14378 		}
14379 		if (!vsi) {
14380 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14381 				 uplink_seid);
14382 			return NULL;
14383 		}
14384 
14385 		if (vsi->uplink_seid == pf->mac_seid)
14386 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14387 					     vsi->tc_config.enabled_tc);
14388 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14389 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14390 					     vsi->tc_config.enabled_tc);
14391 		if (veb) {
14392 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14393 				dev_info(&vsi->back->pdev->dev,
14394 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14395 				return NULL;
14396 			}
14397 			/* We come up by default in VEPA mode if SRIOV is not
14398 			 * already enabled, in which case we can't force VEPA
14399 			 * mode.
14400 			 */
14401 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14402 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14403 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14404 			}
14405 			i40e_config_bridge_mode(veb);
14406 		}
14407 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14408 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14409 				veb = pf->veb[i];
14410 		}
14411 		if (!veb) {
14412 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14413 			return NULL;
14414 		}
14415 
14416 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14417 		uplink_seid = veb->seid;
14418 	}
14419 
14420 	/* get vsi sw struct */
14421 	v_idx = i40e_vsi_mem_alloc(pf, type);
14422 	if (v_idx < 0)
14423 		goto err_alloc;
14424 	vsi = pf->vsi[v_idx];
14425 	if (!vsi)
14426 		goto err_alloc;
14427 	vsi->type = type;
14428 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14429 
14430 	if (type == I40E_VSI_MAIN)
14431 		pf->lan_vsi = v_idx;
14432 	else if (type == I40E_VSI_SRIOV)
14433 		vsi->vf_id = param1;
14434 	/* assign it some queues */
14435 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14436 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14437 
14438 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14439 	if (ret < 0) {
14440 		dev_info(&pf->pdev->dev,
14441 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14442 			 alloc_queue_pairs, vsi->seid, ret);
14443 		goto err_vsi;
14444 	}
14445 	vsi->base_queue = ret;
14446 
14447 	/* get a VSI from the hardware */
14448 	vsi->uplink_seid = uplink_seid;
14449 	ret = i40e_add_vsi(vsi);
14450 	if (ret)
14451 		goto err_vsi;
14452 
14453 	switch (vsi->type) {
14454 	/* setup the netdev if needed */
14455 	case I40E_VSI_MAIN:
14456 	case I40E_VSI_VMDQ2:
14457 		ret = i40e_config_netdev(vsi);
14458 		if (ret)
14459 			goto err_netdev;
14460 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14461 		if (ret)
14462 			goto err_netdev;
14463 		ret = register_netdev(vsi->netdev);
14464 		if (ret)
14465 			goto err_netdev;
14466 		vsi->netdev_registered = true;
14467 		netif_carrier_off(vsi->netdev);
14468 #ifdef CONFIG_I40E_DCB
14469 		/* Setup DCB netlink interface */
14470 		i40e_dcbnl_setup(vsi);
14471 #endif /* CONFIG_I40E_DCB */
14472 		fallthrough;
14473 	case I40E_VSI_FDIR:
14474 		/* set up vectors and rings if needed */
14475 		ret = i40e_vsi_setup_vectors(vsi);
14476 		if (ret)
14477 			goto err_msix;
14478 
14479 		ret = i40e_alloc_rings(vsi);
14480 		if (ret)
14481 			goto err_rings;
14482 
14483 		/* map all of the rings to the q_vectors */
14484 		i40e_vsi_map_rings_to_vectors(vsi);
14485 
14486 		i40e_vsi_reset_stats(vsi);
14487 		break;
14488 	default:
14489 		/* no netdev or rings for the other VSI types */
14490 		break;
14491 	}
14492 
14493 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14494 	    (vsi->type == I40E_VSI_VMDQ2)) {
14495 		ret = i40e_vsi_config_rss(vsi);
14496 	}
14497 	return vsi;
14498 
14499 err_rings:
14500 	i40e_vsi_free_q_vectors(vsi);
14501 err_msix:
14502 	if (vsi->netdev_registered) {
14503 		vsi->netdev_registered = false;
14504 		unregister_netdev(vsi->netdev);
14505 		free_netdev(vsi->netdev);
14506 		vsi->netdev = NULL;
14507 	}
14508 err_netdev:
14509 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14510 err_vsi:
14511 	i40e_vsi_clear(vsi);
14512 err_alloc:
14513 	return NULL;
14514 }
14515 
14516 /**
14517  * i40e_veb_get_bw_info - Query VEB BW information
14518  * @veb: the veb to query
14519  *
14520  * Query the Tx scheduler BW configuration data for given VEB
14521  **/
14522 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14523 {
14524 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14525 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14526 	struct i40e_pf *pf = veb->pf;
14527 	struct i40e_hw *hw = &pf->hw;
14528 	u32 tc_bw_max;
14529 	int ret = 0;
14530 	int i;
14531 
14532 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14533 						  &bw_data, NULL);
14534 	if (ret) {
14535 		dev_info(&pf->pdev->dev,
14536 			 "query veb bw config failed, err %s aq_err %s\n",
14537 			 i40e_stat_str(&pf->hw, ret),
14538 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14539 		goto out;
14540 	}
14541 
14542 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14543 						   &ets_data, NULL);
14544 	if (ret) {
14545 		dev_info(&pf->pdev->dev,
14546 			 "query veb bw ets config failed, err %s aq_err %s\n",
14547 			 i40e_stat_str(&pf->hw, ret),
14548 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14549 		goto out;
14550 	}
14551 
14552 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14553 	veb->bw_max_quanta = ets_data.tc_bw_max;
14554 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14555 	veb->enabled_tc = ets_data.tc_valid_bits;
14556 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14557 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14558 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14559 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14560 		veb->bw_tc_limit_credits[i] =
14561 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14562 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14563 	}
14564 
14565 out:
14566 	return ret;
14567 }
14568 
14569 /**
14570  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14571  * @pf: board private structure
14572  *
14573  * On error: returns error code (negative)
14574  * On success: returns vsi index in PF (positive)
14575  **/
14576 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14577 {
14578 	int ret = -ENOENT;
14579 	struct i40e_veb *veb;
14580 	int i;
14581 
14582 	/* Need to protect the allocation of switch elements at the PF level */
14583 	mutex_lock(&pf->switch_mutex);
14584 
14585 	/* VEB list may be fragmented if VEB creation/destruction has
14586 	 * been happening.  We can afford to do a quick scan to look
14587 	 * for any free slots in the list.
14588 	 *
14589 	 * find next empty veb slot, looping back around if necessary
14590 	 */
14591 	i = 0;
14592 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14593 		i++;
14594 	if (i >= I40E_MAX_VEB) {
14595 		ret = -ENOMEM;
14596 		goto err_alloc_veb;  /* out of VEB slots! */
14597 	}
14598 
14599 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14600 	if (!veb) {
14601 		ret = -ENOMEM;
14602 		goto err_alloc_veb;
14603 	}
14604 	veb->pf = pf;
14605 	veb->idx = i;
14606 	veb->enabled_tc = 1;
14607 
14608 	pf->veb[i] = veb;
14609 	ret = i;
14610 err_alloc_veb:
14611 	mutex_unlock(&pf->switch_mutex);
14612 	return ret;
14613 }
14614 
14615 /**
14616  * i40e_switch_branch_release - Delete a branch of the switch tree
14617  * @branch: where to start deleting
14618  *
14619  * This uses recursion to find the tips of the branch to be
14620  * removed, deleting until we get back to and can delete this VEB.
14621  **/
14622 static void i40e_switch_branch_release(struct i40e_veb *branch)
14623 {
14624 	struct i40e_pf *pf = branch->pf;
14625 	u16 branch_seid = branch->seid;
14626 	u16 veb_idx = branch->idx;
14627 	int i;
14628 
14629 	/* release any VEBs on this VEB - RECURSION */
14630 	for (i = 0; i < I40E_MAX_VEB; i++) {
14631 		if (!pf->veb[i])
14632 			continue;
14633 		if (pf->veb[i]->uplink_seid == branch->seid)
14634 			i40e_switch_branch_release(pf->veb[i]);
14635 	}
14636 
14637 	/* Release the VSIs on this VEB, but not the owner VSI.
14638 	 *
14639 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14640 	 *       the VEB itself, so don't use (*branch) after this loop.
14641 	 */
14642 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14643 		if (!pf->vsi[i])
14644 			continue;
14645 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14646 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14647 			i40e_vsi_release(pf->vsi[i]);
14648 		}
14649 	}
14650 
14651 	/* There's one corner case where the VEB might not have been
14652 	 * removed, so double check it here and remove it if needed.
14653 	 * This case happens if the veb was created from the debugfs
14654 	 * commands and no VSIs were added to it.
14655 	 */
14656 	if (pf->veb[veb_idx])
14657 		i40e_veb_release(pf->veb[veb_idx]);
14658 }
14659 
14660 /**
14661  * i40e_veb_clear - remove veb struct
14662  * @veb: the veb to remove
14663  **/
14664 static void i40e_veb_clear(struct i40e_veb *veb)
14665 {
14666 	if (!veb)
14667 		return;
14668 
14669 	if (veb->pf) {
14670 		struct i40e_pf *pf = veb->pf;
14671 
14672 		mutex_lock(&pf->switch_mutex);
14673 		if (pf->veb[veb->idx] == veb)
14674 			pf->veb[veb->idx] = NULL;
14675 		mutex_unlock(&pf->switch_mutex);
14676 	}
14677 
14678 	kfree(veb);
14679 }
14680 
14681 /**
14682  * i40e_veb_release - Delete a VEB and free its resources
14683  * @veb: the VEB being removed
14684  **/
14685 void i40e_veb_release(struct i40e_veb *veb)
14686 {
14687 	struct i40e_vsi *vsi = NULL;
14688 	struct i40e_pf *pf;
14689 	int i, n = 0;
14690 
14691 	pf = veb->pf;
14692 
14693 	/* find the remaining VSI and check for extras */
14694 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14695 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14696 			n++;
14697 			vsi = pf->vsi[i];
14698 		}
14699 	}
14700 	if (n != 1) {
14701 		dev_info(&pf->pdev->dev,
14702 			 "can't remove VEB %d with %d VSIs left\n",
14703 			 veb->seid, n);
14704 		return;
14705 	}
14706 
14707 	/* move the remaining VSI to uplink veb */
14708 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14709 	if (veb->uplink_seid) {
14710 		vsi->uplink_seid = veb->uplink_seid;
14711 		if (veb->uplink_seid == pf->mac_seid)
14712 			vsi->veb_idx = I40E_NO_VEB;
14713 		else
14714 			vsi->veb_idx = veb->veb_idx;
14715 	} else {
14716 		/* floating VEB */
14717 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14718 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14719 	}
14720 
14721 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14722 	i40e_veb_clear(veb);
14723 }
14724 
14725 /**
14726  * i40e_add_veb - create the VEB in the switch
14727  * @veb: the VEB to be instantiated
14728  * @vsi: the controlling VSI
14729  **/
14730 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14731 {
14732 	struct i40e_pf *pf = veb->pf;
14733 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14734 	int ret;
14735 
14736 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14737 			      veb->enabled_tc, false,
14738 			      &veb->seid, enable_stats, NULL);
14739 
14740 	/* get a VEB from the hardware */
14741 	if (ret) {
14742 		dev_info(&pf->pdev->dev,
14743 			 "couldn't add VEB, err %s aq_err %s\n",
14744 			 i40e_stat_str(&pf->hw, ret),
14745 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14746 		return -EPERM;
14747 	}
14748 
14749 	/* get statistics counter */
14750 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14751 					 &veb->stats_idx, NULL, NULL, NULL);
14752 	if (ret) {
14753 		dev_info(&pf->pdev->dev,
14754 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14755 			 i40e_stat_str(&pf->hw, ret),
14756 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14757 		return -EPERM;
14758 	}
14759 	ret = i40e_veb_get_bw_info(veb);
14760 	if (ret) {
14761 		dev_info(&pf->pdev->dev,
14762 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14763 			 i40e_stat_str(&pf->hw, ret),
14764 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14765 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14766 		return -ENOENT;
14767 	}
14768 
14769 	vsi->uplink_seid = veb->seid;
14770 	vsi->veb_idx = veb->idx;
14771 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14772 
14773 	return 0;
14774 }
14775 
14776 /**
14777  * i40e_veb_setup - Set up a VEB
14778  * @pf: board private structure
14779  * @flags: VEB setup flags
14780  * @uplink_seid: the switch element to link to
14781  * @vsi_seid: the initial VSI seid
14782  * @enabled_tc: Enabled TC bit-map
14783  *
14784  * This allocates the sw VEB structure and links it into the switch
14785  * It is possible and legal for this to be a duplicate of an already
14786  * existing VEB.  It is also possible for both uplink and vsi seids
14787  * to be zero, in order to create a floating VEB.
14788  *
14789  * Returns pointer to the successfully allocated VEB sw struct on
14790  * success, otherwise returns NULL on failure.
14791  **/
14792 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14793 				u16 uplink_seid, u16 vsi_seid,
14794 				u8 enabled_tc)
14795 {
14796 	struct i40e_veb *veb, *uplink_veb = NULL;
14797 	int vsi_idx, veb_idx;
14798 	int ret;
14799 
14800 	/* if one seid is 0, the other must be 0 to create a floating relay */
14801 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14802 	    (uplink_seid + vsi_seid != 0)) {
14803 		dev_info(&pf->pdev->dev,
14804 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14805 			 uplink_seid, vsi_seid);
14806 		return NULL;
14807 	}
14808 
14809 	/* make sure there is such a vsi and uplink */
14810 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14811 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14812 			break;
14813 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14814 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14815 			 vsi_seid);
14816 		return NULL;
14817 	}
14818 
14819 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14820 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14821 			if (pf->veb[veb_idx] &&
14822 			    pf->veb[veb_idx]->seid == uplink_seid) {
14823 				uplink_veb = pf->veb[veb_idx];
14824 				break;
14825 			}
14826 		}
14827 		if (!uplink_veb) {
14828 			dev_info(&pf->pdev->dev,
14829 				 "uplink seid %d not found\n", uplink_seid);
14830 			return NULL;
14831 		}
14832 	}
14833 
14834 	/* get veb sw struct */
14835 	veb_idx = i40e_veb_mem_alloc(pf);
14836 	if (veb_idx < 0)
14837 		goto err_alloc;
14838 	veb = pf->veb[veb_idx];
14839 	veb->flags = flags;
14840 	veb->uplink_seid = uplink_seid;
14841 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14842 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14843 
14844 	/* create the VEB in the switch */
14845 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14846 	if (ret)
14847 		goto err_veb;
14848 	if (vsi_idx == pf->lan_vsi)
14849 		pf->lan_veb = veb->idx;
14850 
14851 	return veb;
14852 
14853 err_veb:
14854 	i40e_veb_clear(veb);
14855 err_alloc:
14856 	return NULL;
14857 }
14858 
14859 /**
14860  * i40e_setup_pf_switch_element - set PF vars based on switch type
14861  * @pf: board private structure
14862  * @ele: element we are building info from
14863  * @num_reported: total number of elements
14864  * @printconfig: should we print the contents
14865  *
14866  * helper function to assist in extracting a few useful SEID values.
14867  **/
14868 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14869 				struct i40e_aqc_switch_config_element_resp *ele,
14870 				u16 num_reported, bool printconfig)
14871 {
14872 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14873 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14874 	u8 element_type = ele->element_type;
14875 	u16 seid = le16_to_cpu(ele->seid);
14876 
14877 	if (printconfig)
14878 		dev_info(&pf->pdev->dev,
14879 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14880 			 element_type, seid, uplink_seid, downlink_seid);
14881 
14882 	switch (element_type) {
14883 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14884 		pf->mac_seid = seid;
14885 		break;
14886 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14887 		/* Main VEB? */
14888 		if (uplink_seid != pf->mac_seid)
14889 			break;
14890 		if (pf->lan_veb >= I40E_MAX_VEB) {
14891 			int v;
14892 
14893 			/* find existing or else empty VEB */
14894 			for (v = 0; v < I40E_MAX_VEB; v++) {
14895 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14896 					pf->lan_veb = v;
14897 					break;
14898 				}
14899 			}
14900 			if (pf->lan_veb >= I40E_MAX_VEB) {
14901 				v = i40e_veb_mem_alloc(pf);
14902 				if (v < 0)
14903 					break;
14904 				pf->lan_veb = v;
14905 			}
14906 		}
14907 		if (pf->lan_veb >= I40E_MAX_VEB)
14908 			break;
14909 
14910 		pf->veb[pf->lan_veb]->seid = seid;
14911 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14912 		pf->veb[pf->lan_veb]->pf = pf;
14913 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14914 		break;
14915 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14916 		if (num_reported != 1)
14917 			break;
14918 		/* This is immediately after a reset so we can assume this is
14919 		 * the PF's VSI
14920 		 */
14921 		pf->mac_seid = uplink_seid;
14922 		pf->pf_seid = downlink_seid;
14923 		pf->main_vsi_seid = seid;
14924 		if (printconfig)
14925 			dev_info(&pf->pdev->dev,
14926 				 "pf_seid=%d main_vsi_seid=%d\n",
14927 				 pf->pf_seid, pf->main_vsi_seid);
14928 		break;
14929 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14930 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14931 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14932 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14933 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14934 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14935 		/* ignore these for now */
14936 		break;
14937 	default:
14938 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14939 			 element_type, seid);
14940 		break;
14941 	}
14942 }
14943 
14944 /**
14945  * i40e_fetch_switch_configuration - Get switch config from firmware
14946  * @pf: board private structure
14947  * @printconfig: should we print the contents
14948  *
14949  * Get the current switch configuration from the device and
14950  * extract a few useful SEID values.
14951  **/
14952 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14953 {
14954 	struct i40e_aqc_get_switch_config_resp *sw_config;
14955 	u16 next_seid = 0;
14956 	int ret = 0;
14957 	u8 *aq_buf;
14958 	int i;
14959 
14960 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14961 	if (!aq_buf)
14962 		return -ENOMEM;
14963 
14964 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14965 	do {
14966 		u16 num_reported, num_total;
14967 
14968 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14969 						I40E_AQ_LARGE_BUF,
14970 						&next_seid, NULL);
14971 		if (ret) {
14972 			dev_info(&pf->pdev->dev,
14973 				 "get switch config failed err %s aq_err %s\n",
14974 				 i40e_stat_str(&pf->hw, ret),
14975 				 i40e_aq_str(&pf->hw,
14976 					     pf->hw.aq.asq_last_status));
14977 			kfree(aq_buf);
14978 			return -ENOENT;
14979 		}
14980 
14981 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14982 		num_total = le16_to_cpu(sw_config->header.num_total);
14983 
14984 		if (printconfig)
14985 			dev_info(&pf->pdev->dev,
14986 				 "header: %d reported %d total\n",
14987 				 num_reported, num_total);
14988 
14989 		for (i = 0; i < num_reported; i++) {
14990 			struct i40e_aqc_switch_config_element_resp *ele =
14991 				&sw_config->element[i];
14992 
14993 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14994 						     printconfig);
14995 		}
14996 	} while (next_seid != 0);
14997 
14998 	kfree(aq_buf);
14999 	return ret;
15000 }
15001 
15002 /**
15003  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15004  * @pf: board private structure
15005  * @reinit: if the Main VSI needs to re-initialized.
15006  * @lock_acquired: indicates whether or not the lock has been acquired
15007  *
15008  * Returns 0 on success, negative value on failure
15009  **/
15010 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15011 {
15012 	u16 flags = 0;
15013 	int ret;
15014 
15015 	/* find out what's out there already */
15016 	ret = i40e_fetch_switch_configuration(pf, false);
15017 	if (ret) {
15018 		dev_info(&pf->pdev->dev,
15019 			 "couldn't fetch switch config, err %s aq_err %s\n",
15020 			 i40e_stat_str(&pf->hw, ret),
15021 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15022 		return ret;
15023 	}
15024 	i40e_pf_reset_stats(pf);
15025 
15026 	/* set the switch config bit for the whole device to
15027 	 * support limited promisc or true promisc
15028 	 * when user requests promisc. The default is limited
15029 	 * promisc.
15030 	*/
15031 
15032 	if ((pf->hw.pf_id == 0) &&
15033 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15034 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15035 		pf->last_sw_conf_flags = flags;
15036 	}
15037 
15038 	if (pf->hw.pf_id == 0) {
15039 		u16 valid_flags;
15040 
15041 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15042 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15043 						NULL);
15044 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15045 			dev_info(&pf->pdev->dev,
15046 				 "couldn't set switch config bits, err %s aq_err %s\n",
15047 				 i40e_stat_str(&pf->hw, ret),
15048 				 i40e_aq_str(&pf->hw,
15049 					     pf->hw.aq.asq_last_status));
15050 			/* not a fatal problem, just keep going */
15051 		}
15052 		pf->last_sw_conf_valid_flags = valid_flags;
15053 	}
15054 
15055 	/* first time setup */
15056 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15057 		struct i40e_vsi *vsi = NULL;
15058 		u16 uplink_seid;
15059 
15060 		/* Set up the PF VSI associated with the PF's main VSI
15061 		 * that is already in the HW switch
15062 		 */
15063 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15064 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15065 		else
15066 			uplink_seid = pf->mac_seid;
15067 		if (pf->lan_vsi == I40E_NO_VSI)
15068 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15069 		else if (reinit)
15070 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15071 		if (!vsi) {
15072 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15073 			i40e_cloud_filter_exit(pf);
15074 			i40e_fdir_teardown(pf);
15075 			return -EAGAIN;
15076 		}
15077 	} else {
15078 		/* force a reset of TC and queue layout configurations */
15079 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15080 
15081 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15082 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15083 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15084 	}
15085 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15086 
15087 	i40e_fdir_sb_setup(pf);
15088 
15089 	/* Setup static PF queue filter control settings */
15090 	ret = i40e_setup_pf_filter_control(pf);
15091 	if (ret) {
15092 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15093 			 ret);
15094 		/* Failure here should not stop continuing other steps */
15095 	}
15096 
15097 	/* enable RSS in the HW, even for only one queue, as the stack can use
15098 	 * the hash
15099 	 */
15100 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15101 		i40e_pf_config_rss(pf);
15102 
15103 	/* fill in link information and enable LSE reporting */
15104 	i40e_link_event(pf);
15105 
15106 	/* Initialize user-specific link properties */
15107 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15108 				  I40E_AQ_AN_COMPLETED) ? true : false);
15109 
15110 	i40e_ptp_init(pf);
15111 
15112 	if (!lock_acquired)
15113 		rtnl_lock();
15114 
15115 	/* repopulate tunnel port filters */
15116 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15117 
15118 	if (!lock_acquired)
15119 		rtnl_unlock();
15120 
15121 	return ret;
15122 }
15123 
15124 /**
15125  * i40e_determine_queue_usage - Work out queue distribution
15126  * @pf: board private structure
15127  **/
15128 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15129 {
15130 	int queues_left;
15131 	int q_max;
15132 
15133 	pf->num_lan_qps = 0;
15134 
15135 	/* Find the max queues to be put into basic use.  We'll always be
15136 	 * using TC0, whether or not DCB is running, and TC0 will get the
15137 	 * big RSS set.
15138 	 */
15139 	queues_left = pf->hw.func_caps.num_tx_qp;
15140 
15141 	if ((queues_left == 1) ||
15142 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15143 		/* one qp for PF, no queues for anything else */
15144 		queues_left = 0;
15145 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15146 
15147 		/* make sure all the fancies are disabled */
15148 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15149 			       I40E_FLAG_IWARP_ENABLED	|
15150 			       I40E_FLAG_FD_SB_ENABLED	|
15151 			       I40E_FLAG_FD_ATR_ENABLED	|
15152 			       I40E_FLAG_DCB_CAPABLE	|
15153 			       I40E_FLAG_DCB_ENABLED	|
15154 			       I40E_FLAG_SRIOV_ENABLED	|
15155 			       I40E_FLAG_VMDQ_ENABLED);
15156 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15157 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15158 				  I40E_FLAG_FD_SB_ENABLED |
15159 				  I40E_FLAG_FD_ATR_ENABLED |
15160 				  I40E_FLAG_DCB_CAPABLE))) {
15161 		/* one qp for PF */
15162 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15163 		queues_left -= pf->num_lan_qps;
15164 
15165 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15166 			       I40E_FLAG_IWARP_ENABLED	|
15167 			       I40E_FLAG_FD_SB_ENABLED	|
15168 			       I40E_FLAG_FD_ATR_ENABLED	|
15169 			       I40E_FLAG_DCB_ENABLED	|
15170 			       I40E_FLAG_VMDQ_ENABLED);
15171 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15172 	} else {
15173 		/* Not enough queues for all TCs */
15174 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15175 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15176 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15177 					I40E_FLAG_DCB_ENABLED);
15178 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15179 		}
15180 
15181 		/* limit lan qps to the smaller of qps, cpus or msix */
15182 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15183 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15184 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15185 		pf->num_lan_qps = q_max;
15186 
15187 		queues_left -= pf->num_lan_qps;
15188 	}
15189 
15190 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15191 		if (queues_left > 1) {
15192 			queues_left -= 1; /* save 1 queue for FD */
15193 		} else {
15194 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15195 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15196 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15197 		}
15198 	}
15199 
15200 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15201 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15202 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15203 					(queues_left / pf->num_vf_qps));
15204 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15205 	}
15206 
15207 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15208 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15209 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15210 					  (queues_left / pf->num_vmdq_qps));
15211 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15212 	}
15213 
15214 	pf->queues_left = queues_left;
15215 	dev_dbg(&pf->pdev->dev,
15216 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15217 		pf->hw.func_caps.num_tx_qp,
15218 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15219 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15220 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15221 		queues_left);
15222 }
15223 
15224 /**
15225  * i40e_setup_pf_filter_control - Setup PF static filter control
15226  * @pf: PF to be setup
15227  *
15228  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15229  * settings. If PE/FCoE are enabled then it will also set the per PF
15230  * based filter sizes required for them. It also enables Flow director,
15231  * ethertype and macvlan type filter settings for the pf.
15232  *
15233  * Returns 0 on success, negative on failure
15234  **/
15235 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15236 {
15237 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15238 
15239 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15240 
15241 	/* Flow Director is enabled */
15242 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15243 		settings->enable_fdir = true;
15244 
15245 	/* Ethtype and MACVLAN filters enabled for PF */
15246 	settings->enable_ethtype = true;
15247 	settings->enable_macvlan = true;
15248 
15249 	if (i40e_set_filter_control(&pf->hw, settings))
15250 		return -ENOENT;
15251 
15252 	return 0;
15253 }
15254 
15255 #define INFO_STRING_LEN 255
15256 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15257 static void i40e_print_features(struct i40e_pf *pf)
15258 {
15259 	struct i40e_hw *hw = &pf->hw;
15260 	char *buf;
15261 	int i;
15262 
15263 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15264 	if (!buf)
15265 		return;
15266 
15267 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15268 #ifdef CONFIG_PCI_IOV
15269 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15270 #endif
15271 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15272 		      pf->hw.func_caps.num_vsis,
15273 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15274 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15275 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15276 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15277 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15278 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15279 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15280 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15281 	}
15282 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15283 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15284 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15285 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15286 	if (pf->flags & I40E_FLAG_PTP)
15287 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15288 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15289 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15290 	else
15291 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15292 
15293 	dev_info(&pf->pdev->dev, "%s\n", buf);
15294 	kfree(buf);
15295 	WARN_ON(i > INFO_STRING_LEN);
15296 }
15297 
15298 /**
15299  * i40e_get_platform_mac_addr - get platform-specific MAC address
15300  * @pdev: PCI device information struct
15301  * @pf: board private structure
15302  *
15303  * Look up the MAC address for the device. First we'll try
15304  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15305  * specific fallback. Otherwise, we'll default to the stored value in
15306  * firmware.
15307  **/
15308 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15309 {
15310 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15311 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15312 }
15313 
15314 /**
15315  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15316  * @fec_cfg: FEC option to set in flags
15317  * @flags: ptr to flags in which we set FEC option
15318  **/
15319 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15320 {
15321 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15322 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15323 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15324 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15325 		*flags |= I40E_FLAG_RS_FEC;
15326 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15327 	}
15328 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15329 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15330 		*flags |= I40E_FLAG_BASE_R_FEC;
15331 		*flags &= ~I40E_FLAG_RS_FEC;
15332 	}
15333 	if (fec_cfg == 0)
15334 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15335 }
15336 
15337 /**
15338  * i40e_check_recovery_mode - check if we are running transition firmware
15339  * @pf: board private structure
15340  *
15341  * Check registers indicating the firmware runs in recovery mode. Sets the
15342  * appropriate driver state.
15343  *
15344  * Returns true if the recovery mode was detected, false otherwise
15345  **/
15346 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15347 {
15348 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15349 
15350 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15351 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15352 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15353 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15354 
15355 		return true;
15356 	}
15357 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15358 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15359 
15360 	return false;
15361 }
15362 
15363 /**
15364  * i40e_pf_loop_reset - perform reset in a loop.
15365  * @pf: board private structure
15366  *
15367  * This function is useful when a NIC is about to enter recovery mode.
15368  * When a NIC's internal data structures are corrupted the NIC's
15369  * firmware is going to enter recovery mode.
15370  * Right after a POR it takes about 7 minutes for firmware to enter
15371  * recovery mode. Until that time a NIC is in some kind of intermediate
15372  * state. After that time period the NIC almost surely enters
15373  * recovery mode. The only way for a driver to detect intermediate
15374  * state is to issue a series of pf-resets and check a return value.
15375  * If a PF reset returns success then the firmware could be in recovery
15376  * mode so the caller of this code needs to check for recovery mode
15377  * if this function returns success. There is a little chance that
15378  * firmware will hang in intermediate state forever.
15379  * Since waiting 7 minutes is quite a lot of time this function waits
15380  * 10 seconds and then gives up by returning an error.
15381  *
15382  * Return 0 on success, negative on failure.
15383  **/
15384 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15385 {
15386 	/* wait max 10 seconds for PF reset to succeed */
15387 	const unsigned long time_end = jiffies + 10 * HZ;
15388 
15389 	struct i40e_hw *hw = &pf->hw;
15390 	i40e_status ret;
15391 
15392 	ret = i40e_pf_reset(hw);
15393 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15394 		usleep_range(10000, 20000);
15395 		ret = i40e_pf_reset(hw);
15396 	}
15397 
15398 	if (ret == I40E_SUCCESS)
15399 		pf->pfr_count++;
15400 	else
15401 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15402 
15403 	return ret;
15404 }
15405 
15406 /**
15407  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15408  * @pf: board private structure
15409  *
15410  * Check FW registers to determine if FW issued unexpected EMP Reset.
15411  * Every time when unexpected EMP Reset occurs the FW increments
15412  * a counter of unexpected EMP Resets. When the counter reaches 10
15413  * the FW should enter the Recovery mode
15414  *
15415  * Returns true if FW issued unexpected EMP Reset
15416  **/
15417 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15418 {
15419 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15420 			   I40E_GL_FWSTS_FWS1B_MASK;
15421 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15422 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15423 }
15424 
15425 /**
15426  * i40e_handle_resets - handle EMP resets and PF resets
15427  * @pf: board private structure
15428  *
15429  * Handle both EMP resets and PF resets and conclude whether there are
15430  * any issues regarding these resets. If there are any issues then
15431  * generate log entry.
15432  *
15433  * Return 0 if NIC is healthy or negative value when there are issues
15434  * with resets
15435  **/
15436 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15437 {
15438 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15439 	const bool is_empr = i40e_check_fw_empr(pf);
15440 
15441 	if (is_empr || pfr != I40E_SUCCESS)
15442 		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");
15443 
15444 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15445 }
15446 
15447 /**
15448  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15449  * @pf: board private structure
15450  * @hw: ptr to the hardware info
15451  *
15452  * This function does a minimal setup of all subsystems needed for running
15453  * recovery mode.
15454  *
15455  * Returns 0 on success, negative on failure
15456  **/
15457 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15458 {
15459 	struct i40e_vsi *vsi;
15460 	int err;
15461 	int v_idx;
15462 
15463 	pci_save_state(pf->pdev);
15464 
15465 	/* set up periodic task facility */
15466 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15467 	pf->service_timer_period = HZ;
15468 
15469 	INIT_WORK(&pf->service_task, i40e_service_task);
15470 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15471 
15472 	err = i40e_init_interrupt_scheme(pf);
15473 	if (err)
15474 		goto err_switch_setup;
15475 
15476 	/* The number of VSIs reported by the FW is the minimum guaranteed
15477 	 * to us; HW supports far more and we share the remaining pool with
15478 	 * the other PFs. We allocate space for more than the guarantee with
15479 	 * the understanding that we might not get them all later.
15480 	 */
15481 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15482 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15483 	else
15484 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15485 
15486 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15487 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15488 			  GFP_KERNEL);
15489 	if (!pf->vsi) {
15490 		err = -ENOMEM;
15491 		goto err_switch_setup;
15492 	}
15493 
15494 	/* We allocate one VSI which is needed as absolute minimum
15495 	 * in order to register the netdev
15496 	 */
15497 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15498 	if (v_idx < 0) {
15499 		err = v_idx;
15500 		goto err_switch_setup;
15501 	}
15502 	pf->lan_vsi = v_idx;
15503 	vsi = pf->vsi[v_idx];
15504 	if (!vsi) {
15505 		err = -EFAULT;
15506 		goto err_switch_setup;
15507 	}
15508 	vsi->alloc_queue_pairs = 1;
15509 	err = i40e_config_netdev(vsi);
15510 	if (err)
15511 		goto err_switch_setup;
15512 	err = register_netdev(vsi->netdev);
15513 	if (err)
15514 		goto err_switch_setup;
15515 	vsi->netdev_registered = true;
15516 	i40e_dbg_pf_init(pf);
15517 
15518 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15519 	if (err)
15520 		goto err_switch_setup;
15521 
15522 	/* tell the firmware that we're starting */
15523 	i40e_send_version(pf);
15524 
15525 	/* since everything's happy, start the service_task timer */
15526 	mod_timer(&pf->service_timer,
15527 		  round_jiffies(jiffies + pf->service_timer_period));
15528 
15529 	return 0;
15530 
15531 err_switch_setup:
15532 	i40e_reset_interrupt_capability(pf);
15533 	del_timer_sync(&pf->service_timer);
15534 	i40e_shutdown_adminq(hw);
15535 	iounmap(hw->hw_addr);
15536 	pci_disable_pcie_error_reporting(pf->pdev);
15537 	pci_release_mem_regions(pf->pdev);
15538 	pci_disable_device(pf->pdev);
15539 	kfree(pf);
15540 
15541 	return err;
15542 }
15543 
15544 /**
15545  * i40e_set_subsystem_device_id - set subsystem device id
15546  * @hw: pointer to the hardware info
15547  *
15548  * Set PCI subsystem device id either from a pci_dev structure or
15549  * a specific FW register.
15550  **/
15551 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15552 {
15553 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15554 
15555 	hw->subsystem_device_id = pdev->subsystem_device ?
15556 		pdev->subsystem_device :
15557 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15558 }
15559 
15560 /**
15561  * i40e_probe - Device initialization routine
15562  * @pdev: PCI device information struct
15563  * @ent: entry in i40e_pci_tbl
15564  *
15565  * i40e_probe initializes a PF identified by a pci_dev structure.
15566  * The OS initialization, configuring of the PF private structure,
15567  * and a hardware reset occur.
15568  *
15569  * Returns 0 on success, negative on failure
15570  **/
15571 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15572 {
15573 	struct i40e_aq_get_phy_abilities_resp abilities;
15574 #ifdef CONFIG_I40E_DCB
15575 	enum i40e_get_fw_lldp_status_resp lldp_status;
15576 	i40e_status status;
15577 #endif /* CONFIG_I40E_DCB */
15578 	struct i40e_pf *pf;
15579 	struct i40e_hw *hw;
15580 	static u16 pfs_found;
15581 	u16 wol_nvm_bits;
15582 	u16 link_status;
15583 	int err;
15584 	u32 val;
15585 	u32 i;
15586 
15587 	err = pci_enable_device_mem(pdev);
15588 	if (err)
15589 		return err;
15590 
15591 	/* set up for high or low dma */
15592 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15593 	if (err) {
15594 		dev_err(&pdev->dev,
15595 			"DMA configuration failed: 0x%x\n", err);
15596 		goto err_dma;
15597 	}
15598 
15599 	/* set up pci connections */
15600 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15601 	if (err) {
15602 		dev_info(&pdev->dev,
15603 			 "pci_request_selected_regions failed %d\n", err);
15604 		goto err_pci_reg;
15605 	}
15606 
15607 	pci_enable_pcie_error_reporting(pdev);
15608 	pci_set_master(pdev);
15609 
15610 	/* Now that we have a PCI connection, we need to do the
15611 	 * low level device setup.  This is primarily setting up
15612 	 * the Admin Queue structures and then querying for the
15613 	 * device's current profile information.
15614 	 */
15615 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15616 	if (!pf) {
15617 		err = -ENOMEM;
15618 		goto err_pf_alloc;
15619 	}
15620 	pf->next_vsi = 0;
15621 	pf->pdev = pdev;
15622 	set_bit(__I40E_DOWN, pf->state);
15623 
15624 	hw = &pf->hw;
15625 	hw->back = pf;
15626 
15627 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15628 				I40E_MAX_CSR_SPACE);
15629 	/* We believe that the highest register to read is
15630 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15631 	 * is not less than that before mapping to prevent a
15632 	 * kernel panic.
15633 	 */
15634 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15635 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15636 			pf->ioremap_len);
15637 		err = -ENOMEM;
15638 		goto err_ioremap;
15639 	}
15640 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15641 	if (!hw->hw_addr) {
15642 		err = -EIO;
15643 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15644 			 (unsigned int)pci_resource_start(pdev, 0),
15645 			 pf->ioremap_len, err);
15646 		goto err_ioremap;
15647 	}
15648 	hw->vendor_id = pdev->vendor;
15649 	hw->device_id = pdev->device;
15650 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15651 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15652 	i40e_set_subsystem_device_id(hw);
15653 	hw->bus.device = PCI_SLOT(pdev->devfn);
15654 	hw->bus.func = PCI_FUNC(pdev->devfn);
15655 	hw->bus.bus_id = pdev->bus->number;
15656 	pf->instance = pfs_found;
15657 
15658 	/* Select something other than the 802.1ad ethertype for the
15659 	 * switch to use internally and drop on ingress.
15660 	 */
15661 	hw->switch_tag = 0xffff;
15662 	hw->first_tag = ETH_P_8021AD;
15663 	hw->second_tag = ETH_P_8021Q;
15664 
15665 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15666 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15667 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15668 
15669 	/* set up the locks for the AQ, do this only once in probe
15670 	 * and destroy them only once in remove
15671 	 */
15672 	mutex_init(&hw->aq.asq_mutex);
15673 	mutex_init(&hw->aq.arq_mutex);
15674 
15675 	pf->msg_enable = netif_msg_init(debug,
15676 					NETIF_MSG_DRV |
15677 					NETIF_MSG_PROBE |
15678 					NETIF_MSG_LINK);
15679 	if (debug < -1)
15680 		pf->hw.debug_mask = debug;
15681 
15682 	/* do a special CORER for clearing PXE mode once at init */
15683 	if (hw->revision_id == 0 &&
15684 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15685 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15686 		i40e_flush(hw);
15687 		msleep(200);
15688 		pf->corer_count++;
15689 
15690 		i40e_clear_pxe_mode(hw);
15691 	}
15692 
15693 	/* Reset here to make sure all is clean and to define PF 'n' */
15694 	i40e_clear_hw(hw);
15695 
15696 	err = i40e_set_mac_type(hw);
15697 	if (err) {
15698 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15699 			 err);
15700 		goto err_pf_reset;
15701 	}
15702 
15703 	err = i40e_handle_resets(pf);
15704 	if (err)
15705 		goto err_pf_reset;
15706 
15707 	i40e_check_recovery_mode(pf);
15708 
15709 	if (is_kdump_kernel()) {
15710 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15711 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15712 	} else {
15713 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15714 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15715 	}
15716 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15717 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15718 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15719 
15720 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15721 		 "%s-%s:misc",
15722 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15723 
15724 	err = i40e_init_shared_code(hw);
15725 	if (err) {
15726 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15727 			 err);
15728 		goto err_pf_reset;
15729 	}
15730 
15731 	/* set up a default setting for link flow control */
15732 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15733 
15734 	err = i40e_init_adminq(hw);
15735 	if (err) {
15736 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15737 			dev_info(&pdev->dev,
15738 				 "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",
15739 				 hw->aq.api_maj_ver,
15740 				 hw->aq.api_min_ver,
15741 				 I40E_FW_API_VERSION_MAJOR,
15742 				 I40E_FW_MINOR_VERSION(hw));
15743 		else
15744 			dev_info(&pdev->dev,
15745 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15746 
15747 		goto err_pf_reset;
15748 	}
15749 	i40e_get_oem_version(hw);
15750 
15751 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15752 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15753 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15754 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15755 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15756 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15757 
15758 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15759 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15760 		dev_dbg(&pdev->dev,
15761 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15762 			 hw->aq.api_maj_ver,
15763 			 hw->aq.api_min_ver,
15764 			 I40E_FW_API_VERSION_MAJOR,
15765 			 I40E_FW_MINOR_VERSION(hw));
15766 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15767 		dev_info(&pdev->dev,
15768 			 "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",
15769 			 hw->aq.api_maj_ver,
15770 			 hw->aq.api_min_ver,
15771 			 I40E_FW_API_VERSION_MAJOR,
15772 			 I40E_FW_MINOR_VERSION(hw));
15773 
15774 	i40e_verify_eeprom(pf);
15775 
15776 	/* Rev 0 hardware was never productized */
15777 	if (hw->revision_id < 1)
15778 		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");
15779 
15780 	i40e_clear_pxe_mode(hw);
15781 
15782 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15783 	if (err)
15784 		goto err_adminq_setup;
15785 
15786 	err = i40e_sw_init(pf);
15787 	if (err) {
15788 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15789 		goto err_sw_init;
15790 	}
15791 
15792 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15793 		return i40e_init_recovery_mode(pf, hw);
15794 
15795 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15796 				hw->func_caps.num_rx_qp, 0, 0);
15797 	if (err) {
15798 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15799 		goto err_init_lan_hmc;
15800 	}
15801 
15802 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15803 	if (err) {
15804 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15805 		err = -ENOENT;
15806 		goto err_configure_lan_hmc;
15807 	}
15808 
15809 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15810 	 * Ignore error return codes because if it was already disabled via
15811 	 * hardware settings this will fail
15812 	 */
15813 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15814 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15815 		i40e_aq_stop_lldp(hw, true, false, NULL);
15816 	}
15817 
15818 	/* allow a platform config to override the HW addr */
15819 	i40e_get_platform_mac_addr(pdev, pf);
15820 
15821 	if (!is_valid_ether_addr(hw->mac.addr)) {
15822 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15823 		err = -EIO;
15824 		goto err_mac_addr;
15825 	}
15826 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15827 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15828 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15829 	if (is_valid_ether_addr(hw->mac.port_addr))
15830 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15831 
15832 	i40e_ptp_alloc_pins(pf);
15833 	pci_set_drvdata(pdev, pf);
15834 	pci_save_state(pdev);
15835 
15836 #ifdef CONFIG_I40E_DCB
15837 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15838 	(!status &&
15839 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15840 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15841 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15842 	dev_info(&pdev->dev,
15843 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15844 			"FW LLDP is disabled\n" :
15845 			"FW LLDP is enabled\n");
15846 
15847 	/* Enable FW to write default DCB config on link-up */
15848 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15849 
15850 	err = i40e_init_pf_dcb(pf);
15851 	if (err) {
15852 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15853 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15854 		/* Continue without DCB enabled */
15855 	}
15856 #endif /* CONFIG_I40E_DCB */
15857 
15858 	/* set up periodic task facility */
15859 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15860 	pf->service_timer_period = HZ;
15861 
15862 	INIT_WORK(&pf->service_task, i40e_service_task);
15863 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15864 
15865 	/* NVM bit on means WoL disabled for the port */
15866 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15867 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15868 		pf->wol_en = false;
15869 	else
15870 		pf->wol_en = true;
15871 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15872 
15873 	/* set up the main switch operations */
15874 	i40e_determine_queue_usage(pf);
15875 	err = i40e_init_interrupt_scheme(pf);
15876 	if (err)
15877 		goto err_switch_setup;
15878 
15879 	/* Reduce Tx and Rx pairs for kdump
15880 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15881 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15882 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15883 	 */
15884 	if (is_kdump_kernel())
15885 		pf->num_lan_msix = 1;
15886 
15887 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15888 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15889 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15890 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15891 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15892 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15893 						    UDP_TUNNEL_TYPE_GENEVE;
15894 
15895 	/* The number of VSIs reported by the FW is the minimum guaranteed
15896 	 * to us; HW supports far more and we share the remaining pool with
15897 	 * the other PFs. We allocate space for more than the guarantee with
15898 	 * the understanding that we might not get them all later.
15899 	 */
15900 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15901 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15902 	else
15903 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15904 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15905 		dev_warn(&pf->pdev->dev,
15906 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15907 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15908 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15909 	}
15910 
15911 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15912 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15913 			  GFP_KERNEL);
15914 	if (!pf->vsi) {
15915 		err = -ENOMEM;
15916 		goto err_switch_setup;
15917 	}
15918 
15919 #ifdef CONFIG_PCI_IOV
15920 	/* prep for VF support */
15921 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15922 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15923 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15924 		if (pci_num_vf(pdev))
15925 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15926 	}
15927 #endif
15928 	err = i40e_setup_pf_switch(pf, false, false);
15929 	if (err) {
15930 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15931 		goto err_vsis;
15932 	}
15933 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15934 
15935 	/* if FDIR VSI was set up, start it now */
15936 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15937 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15938 			i40e_vsi_open(pf->vsi[i]);
15939 			break;
15940 		}
15941 	}
15942 
15943 	/* The driver only wants link up/down and module qualification
15944 	 * reports from firmware.  Note the negative logic.
15945 	 */
15946 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15947 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15948 					 I40E_AQ_EVENT_MEDIA_NA |
15949 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15950 	if (err)
15951 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15952 			 i40e_stat_str(&pf->hw, err),
15953 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15954 
15955 	/* Reconfigure hardware for allowing smaller MSS in the case
15956 	 * of TSO, so that we avoid the MDD being fired and causing
15957 	 * a reset in the case of small MSS+TSO.
15958 	 */
15959 	val = rd32(hw, I40E_REG_MSS);
15960 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15961 		val &= ~I40E_REG_MSS_MIN_MASK;
15962 		val |= I40E_64BYTE_MSS;
15963 		wr32(hw, I40E_REG_MSS, val);
15964 	}
15965 
15966 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15967 		msleep(75);
15968 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15969 		if (err)
15970 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15971 				 i40e_stat_str(&pf->hw, err),
15972 				 i40e_aq_str(&pf->hw,
15973 					     pf->hw.aq.asq_last_status));
15974 	}
15975 	/* The main driver is (mostly) up and happy. We need to set this state
15976 	 * before setting up the misc vector or we get a race and the vector
15977 	 * ends up disabled forever.
15978 	 */
15979 	clear_bit(__I40E_DOWN, pf->state);
15980 
15981 	/* In case of MSIX we are going to setup the misc vector right here
15982 	 * to handle admin queue events etc. In case of legacy and MSI
15983 	 * the misc functionality and queue processing is combined in
15984 	 * the same vector and that gets setup at open.
15985 	 */
15986 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15987 		err = i40e_setup_misc_vector(pf);
15988 		if (err) {
15989 			dev_info(&pdev->dev,
15990 				 "setup of misc vector failed: %d\n", err);
15991 			i40e_cloud_filter_exit(pf);
15992 			i40e_fdir_teardown(pf);
15993 			goto err_vsis;
15994 		}
15995 	}
15996 
15997 #ifdef CONFIG_PCI_IOV
15998 	/* prep for VF support */
15999 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16000 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16001 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16002 		/* disable link interrupts for VFs */
16003 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16004 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16005 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16006 		i40e_flush(hw);
16007 
16008 		if (pci_num_vf(pdev)) {
16009 			dev_info(&pdev->dev,
16010 				 "Active VFs found, allocating resources.\n");
16011 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16012 			if (err)
16013 				dev_info(&pdev->dev,
16014 					 "Error %d allocating resources for existing VFs\n",
16015 					 err);
16016 		}
16017 	}
16018 #endif /* CONFIG_PCI_IOV */
16019 
16020 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16021 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16022 						      pf->num_iwarp_msix,
16023 						      I40E_IWARP_IRQ_PILE_ID);
16024 		if (pf->iwarp_base_vector < 0) {
16025 			dev_info(&pdev->dev,
16026 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16027 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16028 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16029 		}
16030 	}
16031 
16032 	i40e_dbg_pf_init(pf);
16033 
16034 	/* tell the firmware that we're starting */
16035 	i40e_send_version(pf);
16036 
16037 	/* since everything's happy, start the service_task timer */
16038 	mod_timer(&pf->service_timer,
16039 		  round_jiffies(jiffies + pf->service_timer_period));
16040 
16041 	/* add this PF to client device list and launch a client service task */
16042 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16043 		err = i40e_lan_add_device(pf);
16044 		if (err)
16045 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16046 				 err);
16047 	}
16048 
16049 #define PCI_SPEED_SIZE 8
16050 #define PCI_WIDTH_SIZE 8
16051 	/* Devices on the IOSF bus do not have this information
16052 	 * and will report PCI Gen 1 x 1 by default so don't bother
16053 	 * checking them.
16054 	 */
16055 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16056 		char speed[PCI_SPEED_SIZE] = "Unknown";
16057 		char width[PCI_WIDTH_SIZE] = "Unknown";
16058 
16059 		/* Get the negotiated link width and speed from PCI config
16060 		 * space
16061 		 */
16062 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16063 					  &link_status);
16064 
16065 		i40e_set_pci_config_data(hw, link_status);
16066 
16067 		switch (hw->bus.speed) {
16068 		case i40e_bus_speed_8000:
16069 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16070 		case i40e_bus_speed_5000:
16071 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16072 		case i40e_bus_speed_2500:
16073 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16074 		default:
16075 			break;
16076 		}
16077 		switch (hw->bus.width) {
16078 		case i40e_bus_width_pcie_x8:
16079 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16080 		case i40e_bus_width_pcie_x4:
16081 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16082 		case i40e_bus_width_pcie_x2:
16083 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16084 		case i40e_bus_width_pcie_x1:
16085 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16086 		default:
16087 			break;
16088 		}
16089 
16090 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16091 			 speed, width);
16092 
16093 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16094 		    hw->bus.speed < i40e_bus_speed_8000) {
16095 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16096 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16097 		}
16098 	}
16099 
16100 	/* get the requested speeds from the fw */
16101 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16102 	if (err)
16103 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16104 			i40e_stat_str(&pf->hw, err),
16105 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16106 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16107 
16108 	/* set the FEC config due to the board capabilities */
16109 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16110 
16111 	/* get the supported phy types from the fw */
16112 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16113 	if (err)
16114 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16115 			i40e_stat_str(&pf->hw, err),
16116 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16117 
16118 	/* make sure the MFS hasn't been set lower than the default */
16119 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16120 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16121 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16122 	if (val < MAX_FRAME_SIZE_DEFAULT)
16123 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16124 			 i, val);
16125 
16126 	/* Add a filter to drop all Flow control frames from any VSI from being
16127 	 * transmitted. By doing so we stop a malicious VF from sending out
16128 	 * PAUSE or PFC frames and potentially controlling traffic for other
16129 	 * PF/VF VSIs.
16130 	 * The FW can still send Flow control frames if enabled.
16131 	 */
16132 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16133 						       pf->main_vsi_seid);
16134 
16135 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16136 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16137 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16138 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16139 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16140 	/* print a string summarizing features */
16141 	i40e_print_features(pf);
16142 
16143 	return 0;
16144 
16145 	/* Unwind what we've done if something failed in the setup */
16146 err_vsis:
16147 	set_bit(__I40E_DOWN, pf->state);
16148 	i40e_clear_interrupt_scheme(pf);
16149 	kfree(pf->vsi);
16150 err_switch_setup:
16151 	i40e_reset_interrupt_capability(pf);
16152 	del_timer_sync(&pf->service_timer);
16153 err_mac_addr:
16154 err_configure_lan_hmc:
16155 	(void)i40e_shutdown_lan_hmc(hw);
16156 err_init_lan_hmc:
16157 	kfree(pf->qp_pile);
16158 err_sw_init:
16159 err_adminq_setup:
16160 err_pf_reset:
16161 	iounmap(hw->hw_addr);
16162 err_ioremap:
16163 	kfree(pf);
16164 err_pf_alloc:
16165 	pci_disable_pcie_error_reporting(pdev);
16166 	pci_release_mem_regions(pdev);
16167 err_pci_reg:
16168 err_dma:
16169 	pci_disable_device(pdev);
16170 	return err;
16171 }
16172 
16173 /**
16174  * i40e_remove - Device removal routine
16175  * @pdev: PCI device information struct
16176  *
16177  * i40e_remove is called by the PCI subsystem to alert the driver
16178  * that is should release a PCI device.  This could be caused by a
16179  * Hot-Plug event, or because the driver is going to be removed from
16180  * memory.
16181  **/
16182 static void i40e_remove(struct pci_dev *pdev)
16183 {
16184 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16185 	struct i40e_hw *hw = &pf->hw;
16186 	i40e_status ret_code;
16187 	int i;
16188 
16189 	i40e_dbg_pf_exit(pf);
16190 
16191 	i40e_ptp_stop(pf);
16192 
16193 	/* Disable RSS in hw */
16194 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16195 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16196 
16197 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16198 	 * flags, once they are set, i40e_rebuild should not be called as
16199 	 * i40e_prep_for_reset always returns early.
16200 	 */
16201 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16202 		usleep_range(1000, 2000);
16203 	set_bit(__I40E_IN_REMOVE, pf->state);
16204 
16205 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16206 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16207 		i40e_free_vfs(pf);
16208 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16209 	}
16210 	/* no more scheduling of any task */
16211 	set_bit(__I40E_SUSPENDED, pf->state);
16212 	set_bit(__I40E_DOWN, pf->state);
16213 	if (pf->service_timer.function)
16214 		del_timer_sync(&pf->service_timer);
16215 	if (pf->service_task.func)
16216 		cancel_work_sync(&pf->service_task);
16217 
16218 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16219 		struct i40e_vsi *vsi = pf->vsi[0];
16220 
16221 		/* We know that we have allocated only one vsi for this PF,
16222 		 * it was just for registering netdevice, so the interface
16223 		 * could be visible in the 'ifconfig' output
16224 		 */
16225 		unregister_netdev(vsi->netdev);
16226 		free_netdev(vsi->netdev);
16227 
16228 		goto unmap;
16229 	}
16230 
16231 	/* Client close must be called explicitly here because the timer
16232 	 * has been stopped.
16233 	 */
16234 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16235 
16236 	i40e_fdir_teardown(pf);
16237 
16238 	/* If there is a switch structure or any orphans, remove them.
16239 	 * This will leave only the PF's VSI remaining.
16240 	 */
16241 	for (i = 0; i < I40E_MAX_VEB; i++) {
16242 		if (!pf->veb[i])
16243 			continue;
16244 
16245 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16246 		    pf->veb[i]->uplink_seid == 0)
16247 			i40e_switch_branch_release(pf->veb[i]);
16248 	}
16249 
16250 	/* Now we can shutdown the PF's VSI, just before we kill
16251 	 * adminq and hmc.
16252 	 */
16253 	if (pf->vsi[pf->lan_vsi])
16254 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16255 
16256 	i40e_cloud_filter_exit(pf);
16257 
16258 	/* remove attached clients */
16259 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16260 		ret_code = i40e_lan_del_device(pf);
16261 		if (ret_code)
16262 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16263 				 ret_code);
16264 	}
16265 
16266 	/* shutdown and destroy the HMC */
16267 	if (hw->hmc.hmc_obj) {
16268 		ret_code = i40e_shutdown_lan_hmc(hw);
16269 		if (ret_code)
16270 			dev_warn(&pdev->dev,
16271 				 "Failed to destroy the HMC resources: %d\n",
16272 				 ret_code);
16273 	}
16274 
16275 unmap:
16276 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16277 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16278 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16279 		free_irq(pf->pdev->irq, pf);
16280 
16281 	/* shutdown the adminq */
16282 	i40e_shutdown_adminq(hw);
16283 
16284 	/* destroy the locks only once, here */
16285 	mutex_destroy(&hw->aq.arq_mutex);
16286 	mutex_destroy(&hw->aq.asq_mutex);
16287 
16288 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16289 	rtnl_lock();
16290 	i40e_clear_interrupt_scheme(pf);
16291 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16292 		if (pf->vsi[i]) {
16293 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16294 				i40e_vsi_clear_rings(pf->vsi[i]);
16295 			i40e_vsi_clear(pf->vsi[i]);
16296 			pf->vsi[i] = NULL;
16297 		}
16298 	}
16299 	rtnl_unlock();
16300 
16301 	for (i = 0; i < I40E_MAX_VEB; i++) {
16302 		kfree(pf->veb[i]);
16303 		pf->veb[i] = NULL;
16304 	}
16305 
16306 	kfree(pf->qp_pile);
16307 	kfree(pf->vsi);
16308 
16309 	iounmap(hw->hw_addr);
16310 	kfree(pf);
16311 	pci_release_mem_regions(pdev);
16312 
16313 	pci_disable_pcie_error_reporting(pdev);
16314 	pci_disable_device(pdev);
16315 }
16316 
16317 /**
16318  * i40e_pci_error_detected - warning that something funky happened in PCI land
16319  * @pdev: PCI device information struct
16320  * @error: the type of PCI error
16321  *
16322  * Called to warn that something happened and the error handling steps
16323  * are in progress.  Allows the driver to quiesce things, be ready for
16324  * remediation.
16325  **/
16326 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16327 						pci_channel_state_t error)
16328 {
16329 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16330 
16331 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16332 
16333 	if (!pf) {
16334 		dev_info(&pdev->dev,
16335 			 "Cannot recover - error happened during device probe\n");
16336 		return PCI_ERS_RESULT_DISCONNECT;
16337 	}
16338 
16339 	/* shutdown all operations */
16340 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16341 		i40e_prep_for_reset(pf);
16342 
16343 	/* Request a slot reset */
16344 	return PCI_ERS_RESULT_NEED_RESET;
16345 }
16346 
16347 /**
16348  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16349  * @pdev: PCI device information struct
16350  *
16351  * Called to find if the driver can work with the device now that
16352  * the pci slot has been reset.  If a basic connection seems good
16353  * (registers are readable and have sane content) then return a
16354  * happy little PCI_ERS_RESULT_xxx.
16355  **/
16356 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16357 {
16358 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16359 	pci_ers_result_t result;
16360 	u32 reg;
16361 
16362 	dev_dbg(&pdev->dev, "%s\n", __func__);
16363 	if (pci_enable_device_mem(pdev)) {
16364 		dev_info(&pdev->dev,
16365 			 "Cannot re-enable PCI device after reset.\n");
16366 		result = PCI_ERS_RESULT_DISCONNECT;
16367 	} else {
16368 		pci_set_master(pdev);
16369 		pci_restore_state(pdev);
16370 		pci_save_state(pdev);
16371 		pci_wake_from_d3(pdev, false);
16372 
16373 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16374 		if (reg == 0)
16375 			result = PCI_ERS_RESULT_RECOVERED;
16376 		else
16377 			result = PCI_ERS_RESULT_DISCONNECT;
16378 	}
16379 
16380 	return result;
16381 }
16382 
16383 /**
16384  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16385  * @pdev: PCI device information struct
16386  */
16387 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16388 {
16389 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16390 
16391 	i40e_prep_for_reset(pf);
16392 }
16393 
16394 /**
16395  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16396  * @pdev: PCI device information struct
16397  */
16398 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16399 {
16400 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16401 
16402 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16403 		return;
16404 
16405 	i40e_reset_and_rebuild(pf, false, false);
16406 }
16407 
16408 /**
16409  * i40e_pci_error_resume - restart operations after PCI error recovery
16410  * @pdev: PCI device information struct
16411  *
16412  * Called to allow the driver to bring things back up after PCI error
16413  * and/or reset recovery has finished.
16414  **/
16415 static void i40e_pci_error_resume(struct pci_dev *pdev)
16416 {
16417 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16418 
16419 	dev_dbg(&pdev->dev, "%s\n", __func__);
16420 	if (test_bit(__I40E_SUSPENDED, pf->state))
16421 		return;
16422 
16423 	i40e_handle_reset_warning(pf, false);
16424 }
16425 
16426 /**
16427  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16428  * using the mac_address_write admin q function
16429  * @pf: pointer to i40e_pf struct
16430  **/
16431 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16432 {
16433 	struct i40e_hw *hw = &pf->hw;
16434 	i40e_status ret;
16435 	u8 mac_addr[6];
16436 	u16 flags = 0;
16437 
16438 	/* Get current MAC address in case it's an LAA */
16439 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16440 		ether_addr_copy(mac_addr,
16441 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16442 	} else {
16443 		dev_err(&pf->pdev->dev,
16444 			"Failed to retrieve MAC address; using default\n");
16445 		ether_addr_copy(mac_addr, hw->mac.addr);
16446 	}
16447 
16448 	/* The FW expects the mac address write cmd to first be called with
16449 	 * one of these flags before calling it again with the multicast
16450 	 * enable flags.
16451 	 */
16452 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16453 
16454 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16455 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16456 
16457 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16458 	if (ret) {
16459 		dev_err(&pf->pdev->dev,
16460 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16461 		return;
16462 	}
16463 
16464 	flags = I40E_AQC_MC_MAG_EN
16465 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16466 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16467 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16468 	if (ret)
16469 		dev_err(&pf->pdev->dev,
16470 			"Failed to enable Multicast Magic Packet wake up\n");
16471 }
16472 
16473 /**
16474  * i40e_shutdown - PCI callback for shutting down
16475  * @pdev: PCI device information struct
16476  **/
16477 static void i40e_shutdown(struct pci_dev *pdev)
16478 {
16479 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16480 	struct i40e_hw *hw = &pf->hw;
16481 
16482 	set_bit(__I40E_SUSPENDED, pf->state);
16483 	set_bit(__I40E_DOWN, pf->state);
16484 
16485 	del_timer_sync(&pf->service_timer);
16486 	cancel_work_sync(&pf->service_task);
16487 	i40e_cloud_filter_exit(pf);
16488 	i40e_fdir_teardown(pf);
16489 
16490 	/* Client close must be called explicitly here because the timer
16491 	 * has been stopped.
16492 	 */
16493 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16494 
16495 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16496 		i40e_enable_mc_magic_wake(pf);
16497 
16498 	i40e_prep_for_reset(pf);
16499 
16500 	wr32(hw, I40E_PFPM_APM,
16501 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16502 	wr32(hw, I40E_PFPM_WUFC,
16503 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16504 
16505 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16506 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16507 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16508 		free_irq(pf->pdev->irq, pf);
16509 
16510 	/* Since we're going to destroy queues during the
16511 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16512 	 * whole section
16513 	 */
16514 	rtnl_lock();
16515 	i40e_clear_interrupt_scheme(pf);
16516 	rtnl_unlock();
16517 
16518 	if (system_state == SYSTEM_POWER_OFF) {
16519 		pci_wake_from_d3(pdev, pf->wol_en);
16520 		pci_set_power_state(pdev, PCI_D3hot);
16521 	}
16522 }
16523 
16524 /**
16525  * i40e_suspend - PM callback for moving to D3
16526  * @dev: generic device information structure
16527  **/
16528 static int __maybe_unused i40e_suspend(struct device *dev)
16529 {
16530 	struct i40e_pf *pf = dev_get_drvdata(dev);
16531 	struct i40e_hw *hw = &pf->hw;
16532 
16533 	/* If we're already suspended, then there is nothing to do */
16534 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16535 		return 0;
16536 
16537 	set_bit(__I40E_DOWN, pf->state);
16538 
16539 	/* Ensure service task will not be running */
16540 	del_timer_sync(&pf->service_timer);
16541 	cancel_work_sync(&pf->service_task);
16542 
16543 	/* Client close must be called explicitly here because the timer
16544 	 * has been stopped.
16545 	 */
16546 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16547 
16548 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16549 		i40e_enable_mc_magic_wake(pf);
16550 
16551 	/* Since we're going to destroy queues during the
16552 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16553 	 * whole section
16554 	 */
16555 	rtnl_lock();
16556 
16557 	i40e_prep_for_reset(pf);
16558 
16559 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16560 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16561 
16562 	/* Clear the interrupt scheme and release our IRQs so that the system
16563 	 * can safely hibernate even when there are a large number of CPUs.
16564 	 * Otherwise hibernation might fail when mapping all the vectors back
16565 	 * to CPU0.
16566 	 */
16567 	i40e_clear_interrupt_scheme(pf);
16568 
16569 	rtnl_unlock();
16570 
16571 	return 0;
16572 }
16573 
16574 /**
16575  * i40e_resume - PM callback for waking up from D3
16576  * @dev: generic device information structure
16577  **/
16578 static int __maybe_unused i40e_resume(struct device *dev)
16579 {
16580 	struct i40e_pf *pf = dev_get_drvdata(dev);
16581 	int err;
16582 
16583 	/* If we're not suspended, then there is nothing to do */
16584 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16585 		return 0;
16586 
16587 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16588 	 * since we're going to be restoring queues
16589 	 */
16590 	rtnl_lock();
16591 
16592 	/* We cleared the interrupt scheme when we suspended, so we need to
16593 	 * restore it now to resume device functionality.
16594 	 */
16595 	err = i40e_restore_interrupt_scheme(pf);
16596 	if (err) {
16597 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16598 			err);
16599 	}
16600 
16601 	clear_bit(__I40E_DOWN, pf->state);
16602 	i40e_reset_and_rebuild(pf, false, true);
16603 
16604 	rtnl_unlock();
16605 
16606 	/* Clear suspended state last after everything is recovered */
16607 	clear_bit(__I40E_SUSPENDED, pf->state);
16608 
16609 	/* Restart the service task */
16610 	mod_timer(&pf->service_timer,
16611 		  round_jiffies(jiffies + pf->service_timer_period));
16612 
16613 	return 0;
16614 }
16615 
16616 static const struct pci_error_handlers i40e_err_handler = {
16617 	.error_detected = i40e_pci_error_detected,
16618 	.slot_reset = i40e_pci_error_slot_reset,
16619 	.reset_prepare = i40e_pci_error_reset_prepare,
16620 	.reset_done = i40e_pci_error_reset_done,
16621 	.resume = i40e_pci_error_resume,
16622 };
16623 
16624 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16625 
16626 static struct pci_driver i40e_driver = {
16627 	.name     = i40e_driver_name,
16628 	.id_table = i40e_pci_tbl,
16629 	.probe    = i40e_probe,
16630 	.remove   = i40e_remove,
16631 	.driver   = {
16632 		.pm = &i40e_pm_ops,
16633 	},
16634 	.shutdown = i40e_shutdown,
16635 	.err_handler = &i40e_err_handler,
16636 	.sriov_configure = i40e_pci_sriov_configure,
16637 };
16638 
16639 /**
16640  * i40e_init_module - Driver registration routine
16641  *
16642  * i40e_init_module is the first routine called when the driver is
16643  * loaded. All it does is register with the PCI subsystem.
16644  **/
16645 static int __init i40e_init_module(void)
16646 {
16647 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16648 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16649 
16650 	/* There is no need to throttle the number of active tasks because
16651 	 * each device limits its own task using a state bit for scheduling
16652 	 * the service task, and the device tasks do not interfere with each
16653 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16654 	 * since we need to be able to guarantee forward progress even under
16655 	 * memory pressure.
16656 	 */
16657 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16658 	if (!i40e_wq) {
16659 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16660 		return -ENOMEM;
16661 	}
16662 
16663 	i40e_dbg_init();
16664 	return pci_register_driver(&i40e_driver);
16665 }
16666 module_init(i40e_init_module);
16667 
16668 /**
16669  * i40e_exit_module - Driver exit cleanup routine
16670  *
16671  * i40e_exit_module is called just before the driver is removed
16672  * from memory.
16673  **/
16674 static void __exit i40e_exit_module(void)
16675 {
16676 	pci_unregister_driver(&i40e_driver);
16677 	destroy_workqueue(i40e_wq);
16678 	ida_destroy(&i40e_client_ida);
16679 	i40e_dbg_exit();
16680 }
16681 module_exit(i40e_exit_module);
16682