xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/net/intel/libie/pctype.h>
7 #include <linux/if_bridge.h>
8 #include <linux/if_macvlan.h>
9 #include <linux/module.h>
10 #include <net/pkt_cls.h>
11 #include <net/xdp_sock_drv.h>
12 
13 /* Local includes */
14 #include "i40e.h"
15 #include "i40e_devids.h"
16 #include "i40e_diag.h"
17 #include "i40e_lan_hmc.h"
18 #include "i40e_virtchnl_pf.h"
19 #include "i40e_xsk.h"
20 
21 /* All i40e tracepoints are defined by the include below, which
22  * must be included exactly once across the whole kernel with
23  * CREATE_TRACE_POINTS defined
24  */
25 #define CREATE_TRACE_POINTS
26 #include "i40e_trace.h"
27 
28 const char i40e_driver_name[] = "i40e";
29 static const char i40e_driver_string[] =
30 			"Intel(R) Ethernet Connection XL710 Network Driver";
31 
32 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
33 
34 /* a bit of forward declarations */
35 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
36 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
37 static int i40e_add_vsi(struct i40e_vsi *vsi);
38 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
39 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
40 static int i40e_setup_misc_vector(struct i40e_pf *pf);
41 static void i40e_determine_queue_usage(struct i40e_pf *pf);
42 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
43 static void i40e_prep_for_reset(struct i40e_pf *pf);
44 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
45 				   bool lock_acquired);
46 static int i40e_reset(struct i40e_pf *pf);
47 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
48 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
49 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
50 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
51 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
52 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
53 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
54 static int i40e_get_capabilities(struct i40e_pf *pf,
55 				 enum i40e_admin_queue_opc list_type);
56 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
57 
58 /* i40e_pci_tbl - PCI Device ID Table
59  *
60  * Last entry must be all 0s
61  *
62  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
63  *   Class, Class Mask, private data (not used) }
64  */
65 static const struct pci_device_id i40e_pci_tbl[] = {
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
78 	/*
79 	 * This ID conflicts with ipw2200, but the devices can be differentiated
80 	 * because i40e devices use PCI_CLASS_NETWORK_ETHERNET and ipw2200
81 	 * devices use PCI_CLASS_NETWORK_OTHER.
82 	 */
83 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, I40E_DEV_ID_10G_B),
84 		PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
91 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
92 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
93 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
94 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
95 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
96 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
97 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
98 	/* required last entry */
99 	{0, }
100 };
101 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
102 
103 #define I40E_MAX_VF_COUNT 128
104 static int debug = -1;
105 module_param(debug, uint, 0);
106 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
107 
108 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
109 MODULE_IMPORT_NS("LIBIE");
110 MODULE_IMPORT_NS("LIBIE_ADMINQ");
111 MODULE_LICENSE("GPL v2");
112 
113 static struct workqueue_struct *i40e_wq;
114 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)115 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
116 				  struct net_device *netdev, int delta)
117 {
118 	struct netdev_hw_addr_list *ha_list;
119 	struct netdev_hw_addr *ha;
120 
121 	if (!f || !netdev)
122 		return;
123 
124 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
125 		ha_list = &netdev->uc;
126 	else
127 		ha_list = &netdev->mc;
128 
129 	netdev_hw_addr_list_for_each(ha, ha_list) {
130 		if (ether_addr_equal(ha->addr, f->macaddr)) {
131 			ha->refcount += delta;
132 			if (ha->refcount <= 0)
133 				ha->refcount = 1;
134 			break;
135 		}
136 	}
137 }
138 
139 /**
140  * i40e_hw_to_dev - get device pointer from the hardware structure
141  * @hw: pointer to the device HW structure
142  **/
i40e_hw_to_dev(struct i40e_hw * hw)143 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
144 {
145 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
146 
147 	return &pf->pdev->dev;
148 }
149 
150 /**
151  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
152  * @hw:   pointer to the HW structure
153  * @mem:  ptr to mem struct to fill out
154  * @size: size of memory requested
155  * @alignment: what to align the allocation to
156  **/
i40e_allocate_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)157 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
158 			  u64 size, u32 alignment)
159 {
160 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
161 
162 	mem->size = ALIGN(size, alignment);
163 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
164 				     GFP_KERNEL);
165 	if (!mem->va)
166 		return -ENOMEM;
167 
168 	return 0;
169 }
170 
171 /**
172  * i40e_free_dma_mem - OS specific memory free for shared code
173  * @hw:   pointer to the HW structure
174  * @mem:  ptr to mem struct to free
175  **/
i40e_free_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem)176 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
177 {
178 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
179 
180 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
181 	mem->va = NULL;
182 	mem->pa = 0;
183 	mem->size = 0;
184 
185 	return 0;
186 }
187 
188 /**
189  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
190  * @hw:   pointer to the HW structure
191  * @mem:  ptr to mem struct to fill out
192  * @size: size of memory requested
193  **/
i40e_allocate_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)194 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
195 			   u32 size)
196 {
197 	mem->size = size;
198 	mem->va = kzalloc(size, GFP_KERNEL);
199 
200 	if (!mem->va)
201 		return -ENOMEM;
202 
203 	return 0;
204 }
205 
206 /**
207  * i40e_free_virt_mem - OS specific memory free for shared code
208  * @hw:   pointer to the HW structure
209  * @mem:  ptr to mem struct to free
210  **/
i40e_free_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem)211 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
212 {
213 	/* it's ok to kfree a NULL pointer */
214 	kfree(mem->va);
215 	mem->va = NULL;
216 	mem->size = 0;
217 
218 	return 0;
219 }
220 
221 /**
222  * i40e_get_lump - find a lump of free generic resource
223  * @pf: board private structure
224  * @pile: the pile of resource to search
225  * @needed: the number of items needed
226  * @id: an owner id to stick on the items assigned
227  *
228  * Returns the base item index of the lump, or negative for error
229  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)230 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
231 			 u16 needed, u16 id)
232 {
233 	int ret = -ENOMEM;
234 	int i, j;
235 
236 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
237 		dev_info(&pf->pdev->dev,
238 			 "param err: pile=%s needed=%d id=0x%04x\n",
239 			 pile ? "<valid>" : "<null>", needed, id);
240 		return -EINVAL;
241 	}
242 
243 	/* Allocate last queue in the pile for FDIR VSI queue
244 	 * so it doesn't fragment the qp_pile
245 	 */
246 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
247 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
248 			dev_err(&pf->pdev->dev,
249 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
250 				pile->num_entries - 1);
251 			return -ENOMEM;
252 		}
253 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
254 		return pile->num_entries - 1;
255 	}
256 
257 	i = 0;
258 	while (i < pile->num_entries) {
259 		/* skip already allocated entries */
260 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
261 			i++;
262 			continue;
263 		}
264 
265 		/* do we have enough in this lump? */
266 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
267 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
268 				break;
269 		}
270 
271 		if (j == needed) {
272 			/* there was enough, so assign it to the requestor */
273 			for (j = 0; j < needed; j++)
274 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
275 			ret = i;
276 			break;
277 		}
278 
279 		/* not enough, so skip over it and continue looking */
280 		i += j;
281 	}
282 
283 	return ret;
284 }
285 
286 /**
287  * i40e_put_lump - return a lump of generic resource
288  * @pile: the pile of resource to search
289  * @index: the base item index
290  * @id: the owner id of the items assigned
291  *
292  * Returns the count of items in the lump
293  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)294 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
295 {
296 	int valid_id = (id | I40E_PILE_VALID_BIT);
297 	int count = 0;
298 	u16 i;
299 
300 	if (!pile || index >= pile->num_entries)
301 		return -EINVAL;
302 
303 	for (i = index;
304 	     i < pile->num_entries && pile->list[i] == valid_id;
305 	     i++) {
306 		pile->list[i] = 0;
307 		count++;
308 	}
309 
310 
311 	return count;
312 }
313 
314 /**
315  * i40e_find_vsi_from_id - searches for the vsi with the given id
316  * @pf: the pf structure to search for the vsi
317  * @id: id of the vsi it is searching for
318  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)319 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
320 {
321 	struct i40e_vsi *vsi;
322 	int i;
323 
324 	i40e_pf_for_each_vsi(pf, i, vsi)
325 		if (vsi->id == id)
326 			return vsi;
327 
328 	return NULL;
329 }
330 
331 /**
332  * i40e_service_event_schedule - Schedule the service task to wake up
333  * @pf: board private structure
334  *
335  * If not already scheduled, this puts the task into the work queue
336  **/
i40e_service_event_schedule(struct i40e_pf * pf)337 void i40e_service_event_schedule(struct i40e_pf *pf)
338 {
339 	if ((!test_bit(__I40E_DOWN, pf->state) &&
340 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
341 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
342 		queue_work(i40e_wq, &pf->service_task);
343 }
344 
345 /**
346  * i40e_tx_timeout - Respond to a Tx Hang
347  * @netdev: network interface device structure
348  * @txqueue: queue number timing out
349  *
350  * If any port has noticed a Tx timeout, it is likely that the whole
351  * device is munged, not just the one netdev port, so go for the full
352  * reset.
353  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)354 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
355 {
356 	struct i40e_netdev_priv *np = netdev_priv(netdev);
357 	struct i40e_vsi *vsi = np->vsi;
358 	struct i40e_pf *pf = vsi->back;
359 	struct i40e_ring *tx_ring = NULL;
360 	unsigned int i;
361 	u32 head, val;
362 
363 	pf->tx_timeout_count++;
364 
365 	/* with txqueue index, find the tx_ring struct */
366 	for (i = 0; i < vsi->num_queue_pairs; i++) {
367 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
368 			if (txqueue ==
369 			    vsi->tx_rings[i]->queue_index) {
370 				tx_ring = vsi->tx_rings[i];
371 				break;
372 			}
373 		}
374 	}
375 
376 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
377 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
378 	else if (time_before(jiffies,
379 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
380 		return;   /* don't do any new action before the next timeout */
381 
382 	/* don't kick off another recovery if one is already pending */
383 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
384 		return;
385 
386 	if (tx_ring) {
387 		head = i40e_get_head(tx_ring);
388 		/* Read interrupt register */
389 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
390 			val = rd32(&pf->hw,
391 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
392 						tx_ring->vsi->base_vector - 1));
393 		else
394 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
395 
396 		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",
397 			    vsi->seid, txqueue, tx_ring->next_to_clean,
398 			    head, tx_ring->next_to_use,
399 			    readl(tx_ring->tail), val);
400 	}
401 
402 	pf->tx_timeout_last_recovery = jiffies;
403 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
404 		    pf->tx_timeout_recovery_level, txqueue);
405 
406 	switch (pf->tx_timeout_recovery_level) {
407 	case 1:
408 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
409 		break;
410 	case 2:
411 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
412 		break;
413 	case 3:
414 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
415 		break;
416 	default:
417 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
418 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
419 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
420 		break;
421 	}
422 
423 	i40e_service_event_schedule(pf);
424 	pf->tx_timeout_recovery_level++;
425 }
426 
427 /**
428  * i40e_get_vsi_stats_struct - Get System Network Statistics
429  * @vsi: the VSI we care about
430  *
431  * Returns the address of the device statistics structure.
432  * The statistics are actually updated from the service task.
433  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)434 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
435 {
436 	return &vsi->net_stats;
437 }
438 
439 /**
440  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
441  * @ring: Tx ring to get statistics from
442  * @stats: statistics entry to be updated
443  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)444 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
445 					    struct rtnl_link_stats64 *stats)
446 {
447 	u64 bytes, packets;
448 	unsigned int start;
449 
450 	do {
451 		start = u64_stats_fetch_begin(&ring->syncp);
452 		packets = ring->stats.packets;
453 		bytes   = ring->stats.bytes;
454 	} while (u64_stats_fetch_retry(&ring->syncp, start));
455 
456 	stats->tx_packets += packets;
457 	stats->tx_bytes   += bytes;
458 }
459 
460 /**
461  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
462  * @netdev: network interface device structure
463  * @stats: data structure to store statistics
464  *
465  * Returns the address of the device statistics structure.
466  * The statistics are actually updated from the service task.
467  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)468 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
469 				  struct rtnl_link_stats64 *stats)
470 {
471 	struct i40e_netdev_priv *np = netdev_priv(netdev);
472 	struct i40e_vsi *vsi = np->vsi;
473 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
474 	struct i40e_ring *ring;
475 	int i;
476 
477 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
478 		return;
479 
480 	if (!vsi->tx_rings)
481 		return;
482 
483 	rcu_read_lock();
484 	for (i = 0; i < vsi->num_queue_pairs; i++) {
485 		u64 bytes, packets;
486 		unsigned int start;
487 
488 		ring = READ_ONCE(vsi->tx_rings[i]);
489 		if (!ring)
490 			continue;
491 		i40e_get_netdev_stats_struct_tx(ring, stats);
492 
493 		if (i40e_enabled_xdp_vsi(vsi)) {
494 			ring = READ_ONCE(vsi->xdp_rings[i]);
495 			if (!ring)
496 				continue;
497 			i40e_get_netdev_stats_struct_tx(ring, stats);
498 		}
499 
500 		ring = READ_ONCE(vsi->rx_rings[i]);
501 		if (!ring)
502 			continue;
503 		do {
504 			start   = u64_stats_fetch_begin(&ring->syncp);
505 			packets = ring->stats.packets;
506 			bytes   = ring->stats.bytes;
507 		} while (u64_stats_fetch_retry(&ring->syncp, start));
508 
509 		stats->rx_packets += packets;
510 		stats->rx_bytes   += bytes;
511 
512 	}
513 	rcu_read_unlock();
514 
515 	/* following stats updated by i40e_watchdog_subtask() */
516 	stats->multicast	= vsi_stats->multicast;
517 	stats->tx_errors	= vsi_stats->tx_errors;
518 	stats->tx_dropped	= vsi_stats->tx_dropped;
519 	stats->rx_errors	= vsi_stats->rx_errors;
520 	stats->rx_dropped	= vsi_stats->rx_dropped;
521 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
522 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
523 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
524 }
525 
526 /**
527  * i40e_vsi_reset_stats - Resets all stats of the given vsi
528  * @vsi: the VSI to have its stats reset
529  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)530 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
531 {
532 	struct rtnl_link_stats64 *ns;
533 	int i;
534 
535 	if (!vsi)
536 		return;
537 
538 	ns = i40e_get_vsi_stats_struct(vsi);
539 	memset(ns, 0, sizeof(*ns));
540 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
541 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
542 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
543 	if (vsi->rx_rings && vsi->rx_rings[0]) {
544 		for (i = 0; i < vsi->num_queue_pairs; i++) {
545 			memset(&vsi->rx_rings[i]->stats, 0,
546 			       sizeof(vsi->rx_rings[i]->stats));
547 			memset(&vsi->rx_rings[i]->rx_stats, 0,
548 			       sizeof(vsi->rx_rings[i]->rx_stats));
549 			memset(&vsi->tx_rings[i]->stats, 0,
550 			       sizeof(vsi->tx_rings[i]->stats));
551 			memset(&vsi->tx_rings[i]->tx_stats, 0,
552 			       sizeof(vsi->tx_rings[i]->tx_stats));
553 		}
554 	}
555 	vsi->stat_offsets_loaded = false;
556 }
557 
558 /**
559  * i40e_pf_reset_stats - Reset all of the stats for the given PF
560  * @pf: the PF to be reset
561  **/
i40e_pf_reset_stats(struct i40e_pf * pf)562 void i40e_pf_reset_stats(struct i40e_pf *pf)
563 {
564 	struct i40e_veb *veb;
565 	int i;
566 
567 	memset(&pf->stats, 0, sizeof(pf->stats));
568 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
569 	pf->stat_offsets_loaded = false;
570 
571 	i40e_pf_for_each_veb(pf, i, veb) {
572 		memset(&veb->stats, 0, sizeof(veb->stats));
573 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
574 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
575 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
576 		veb->stat_offsets_loaded = false;
577 	}
578 	pf->hw_csum_rx_error = 0;
579 }
580 
581 /**
582  * i40e_compute_pci_to_hw_id - compute index form PCI function.
583  * @vsi: ptr to the VSI to read from.
584  * @hw: ptr to the hardware info.
585  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)586 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
587 {
588 	int pf_count = i40e_get_pf_count(hw);
589 
590 	if (vsi->type == I40E_VSI_SRIOV)
591 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
592 
593 	return hw->port + BIT(7);
594 }
595 
596 /**
597  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
598  * @hw: ptr to the hardware info.
599  * @hireg: the high 32 bit reg to read.
600  * @loreg: the low 32 bit reg to read.
601  * @offset_loaded: has the initial offset been loaded yet.
602  * @offset: ptr to current offset value.
603  * @stat: ptr to the stat.
604  *
605  * Since the device stats are not reset at PFReset, they will not
606  * be zeroed when the driver starts.  We'll save the first values read
607  * and use them as offsets to be subtracted from the raw values in order
608  * to report stats that count from zero.
609  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)610 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
611 			       bool offset_loaded, u64 *offset, u64 *stat)
612 {
613 	u64 new_data;
614 
615 	new_data = rd64(hw, loreg);
616 
617 	if (!offset_loaded || new_data < *offset)
618 		*offset = new_data;
619 	*stat = new_data - *offset;
620 }
621 
622 /**
623  * i40e_stat_update48 - read and update a 48 bit stat from the chip
624  * @hw: ptr to the hardware info
625  * @hireg: the high 32 bit reg to read
626  * @loreg: the low 32 bit reg to read
627  * @offset_loaded: has the initial offset been loaded yet
628  * @offset: ptr to current offset value
629  * @stat: ptr to the stat
630  *
631  * Since the device stats are not reset at PFReset, they likely will not
632  * be zeroed when the driver starts.  We'll save the first values read
633  * and use them as offsets to be subtracted from the raw values in order
634  * to report stats that count from zero.  In the process, we also manage
635  * the potential roll-over.
636  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)637 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
638 			       bool offset_loaded, u64 *offset, u64 *stat)
639 {
640 	u64 new_data;
641 
642 	if (hw->device_id == I40E_DEV_ID_QEMU) {
643 		new_data = rd32(hw, loreg);
644 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
645 	} else {
646 		new_data = rd64(hw, loreg);
647 	}
648 	if (!offset_loaded)
649 		*offset = new_data;
650 	if (likely(new_data >= *offset))
651 		*stat = new_data - *offset;
652 	else
653 		*stat = (new_data + BIT_ULL(48)) - *offset;
654 	*stat &= 0xFFFFFFFFFFFFULL;
655 }
656 
657 /**
658  * i40e_stat_update32 - read and update a 32 bit stat from the chip
659  * @hw: ptr to the hardware info
660  * @reg: the hw reg to read
661  * @offset_loaded: has the initial offset been loaded yet
662  * @offset: ptr to current offset value
663  * @stat: ptr to the stat
664  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)665 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
666 			       bool offset_loaded, u64 *offset, u64 *stat)
667 {
668 	u32 new_data;
669 
670 	new_data = rd32(hw, reg);
671 	if (!offset_loaded)
672 		*offset = new_data;
673 	if (likely(new_data >= *offset))
674 		*stat = (u32)(new_data - *offset);
675 	else
676 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
677 }
678 
679 /**
680  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
681  * @hw: ptr to the hardware info
682  * @reg: the hw reg to read and clear
683  * @stat: ptr to the stat
684  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)685 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
686 {
687 	u32 new_data = rd32(hw, reg);
688 
689 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
690 	*stat += new_data;
691 }
692 
693 /**
694  * i40e_stats_update_rx_discards - update rx_discards.
695  * @vsi: ptr to the VSI to be updated.
696  * @hw: ptr to the hardware info.
697  * @stat_idx: VSI's stat_counter_idx.
698  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
699  * @stat_offset: ptr to stat_offset to store first read of specific register.
700  * @stat: ptr to VSI's stat to be updated.
701  **/
702 static void
i40e_stats_update_rx_discards(struct i40e_vsi * vsi,struct i40e_hw * hw,int stat_idx,bool offset_loaded,struct i40e_eth_stats * stat_offset,struct i40e_eth_stats * stat)703 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
704 			      int stat_idx, bool offset_loaded,
705 			      struct i40e_eth_stats *stat_offset,
706 			      struct i40e_eth_stats *stat)
707 {
708 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
709 			   &stat_offset->rx_discards, &stat->rx_discards);
710 	i40e_stat_update64(hw,
711 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
712 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
713 			   offset_loaded, &stat_offset->rx_discards_other,
714 			   &stat->rx_discards_other);
715 }
716 
717 /**
718  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
719  * @vsi: the VSI to be updated
720  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)721 void i40e_update_eth_stats(struct i40e_vsi *vsi)
722 {
723 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
724 	struct i40e_pf *pf = vsi->back;
725 	struct i40e_hw *hw = &pf->hw;
726 	struct i40e_eth_stats *oes;
727 	struct i40e_eth_stats *es;     /* device's eth stats */
728 
729 	es = &vsi->eth_stats;
730 	oes = &vsi->eth_stats_offsets;
731 
732 	/* Gather up the stats that the hw collects */
733 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
734 			   vsi->stat_offsets_loaded,
735 			   &oes->tx_errors, &es->tx_errors);
736 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
737 			   vsi->stat_offsets_loaded,
738 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
739 
740 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
741 			   I40E_GLV_GORCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_bytes, &es->rx_bytes);
744 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
745 			   I40E_GLV_UPRCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_unicast, &es->rx_unicast);
748 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
749 			   I40E_GLV_MPRCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->rx_multicast, &es->rx_multicast);
752 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
753 			   I40E_GLV_BPRCL(stat_idx),
754 			   vsi->stat_offsets_loaded,
755 			   &oes->rx_broadcast, &es->rx_broadcast);
756 
757 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
758 			   I40E_GLV_GOTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_bytes, &es->tx_bytes);
761 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
762 			   I40E_GLV_UPTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_unicast, &es->tx_unicast);
765 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
766 			   I40E_GLV_MPTCL(stat_idx),
767 			   vsi->stat_offsets_loaded,
768 			   &oes->tx_multicast, &es->tx_multicast);
769 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
770 			   I40E_GLV_BPTCL(stat_idx),
771 			   vsi->stat_offsets_loaded,
772 			   &oes->tx_broadcast, &es->tx_broadcast);
773 
774 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
775 				      vsi->stat_offsets_loaded, oes, es);
776 
777 	vsi->stat_offsets_loaded = true;
778 }
779 
780 /**
781  * i40e_update_veb_stats - Update Switch component statistics
782  * @veb: the VEB being updated
783  **/
i40e_update_veb_stats(struct i40e_veb * veb)784 void i40e_update_veb_stats(struct i40e_veb *veb)
785 {
786 	struct i40e_pf *pf = veb->pf;
787 	struct i40e_hw *hw = &pf->hw;
788 	struct i40e_eth_stats *oes;
789 	struct i40e_eth_stats *es;     /* device's eth stats */
790 	struct i40e_veb_tc_stats *veb_oes;
791 	struct i40e_veb_tc_stats *veb_es;
792 	int i, idx = 0;
793 
794 	idx = veb->stats_idx;
795 	es = &veb->stats;
796 	oes = &veb->stats_offsets;
797 	veb_es = &veb->tc_stats;
798 	veb_oes = &veb->tc_stats_offsets;
799 
800 	/* Gather up the stats that the hw collects */
801 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
802 			   veb->stat_offsets_loaded,
803 			   &oes->tx_discards, &es->tx_discards);
804 	if (hw->revision_id > 0)
805 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
806 				   veb->stat_offsets_loaded,
807 				   &oes->rx_unknown_protocol,
808 				   &es->rx_unknown_protocol);
809 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
810 			   veb->stat_offsets_loaded,
811 			   &oes->rx_bytes, &es->rx_bytes);
812 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
813 			   veb->stat_offsets_loaded,
814 			   &oes->rx_unicast, &es->rx_unicast);
815 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
816 			   veb->stat_offsets_loaded,
817 			   &oes->rx_multicast, &es->rx_multicast);
818 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
819 			   veb->stat_offsets_loaded,
820 			   &oes->rx_broadcast, &es->rx_broadcast);
821 
822 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
823 			   veb->stat_offsets_loaded,
824 			   &oes->tx_bytes, &es->tx_bytes);
825 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
826 			   veb->stat_offsets_loaded,
827 			   &oes->tx_unicast, &es->tx_unicast);
828 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
829 			   veb->stat_offsets_loaded,
830 			   &oes->tx_multicast, &es->tx_multicast);
831 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
832 			   veb->stat_offsets_loaded,
833 			   &oes->tx_broadcast, &es->tx_broadcast);
834 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
835 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
836 				   I40E_GLVEBTC_RPCL(i, idx),
837 				   veb->stat_offsets_loaded,
838 				   &veb_oes->tc_rx_packets[i],
839 				   &veb_es->tc_rx_packets[i]);
840 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
841 				   I40E_GLVEBTC_RBCL(i, idx),
842 				   veb->stat_offsets_loaded,
843 				   &veb_oes->tc_rx_bytes[i],
844 				   &veb_es->tc_rx_bytes[i]);
845 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
846 				   I40E_GLVEBTC_TPCL(i, idx),
847 				   veb->stat_offsets_loaded,
848 				   &veb_oes->tc_tx_packets[i],
849 				   &veb_es->tc_tx_packets[i]);
850 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
851 				   I40E_GLVEBTC_TBCL(i, idx),
852 				   veb->stat_offsets_loaded,
853 				   &veb_oes->tc_tx_bytes[i],
854 				   &veb_es->tc_tx_bytes[i]);
855 	}
856 	veb->stat_offsets_loaded = true;
857 }
858 
859 /**
860  * i40e_update_vsi_stats - Update the vsi statistics counters.
861  * @vsi: the VSI to be updated
862  *
863  * There are a few instances where we store the same stat in a
864  * couple of different structs.  This is partly because we have
865  * the netdev stats that need to be filled out, which is slightly
866  * different from the "eth_stats" defined by the chip and used in
867  * VF communications.  We sort it out here.
868  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)869 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
870 {
871 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
872 	struct i40e_pf *pf = vsi->back;
873 	struct rtnl_link_stats64 *ons;
874 	struct rtnl_link_stats64 *ns;   /* netdev stats */
875 	struct i40e_eth_stats *oes;
876 	struct i40e_eth_stats *es;     /* device's eth stats */
877 	u64 tx_restart, tx_busy;
878 	struct i40e_ring *p;
879 	u64 bytes, packets;
880 	unsigned int start;
881 	u64 tx_linearize;
882 	u64 tx_force_wb;
883 	u64 tx_stopped;
884 	u64 rx_p, rx_b;
885 	u64 tx_p, tx_b;
886 	u16 q;
887 
888 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
889 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
890 		return;
891 
892 	ns = i40e_get_vsi_stats_struct(vsi);
893 	ons = &vsi->net_stats_offsets;
894 	es = &vsi->eth_stats;
895 	oes = &vsi->eth_stats_offsets;
896 
897 	/* Gather up the netdev and vsi stats that the driver collects
898 	 * on the fly during packet processing
899 	 */
900 	rx_b = rx_p = 0;
901 	tx_b = tx_p = 0;
902 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
903 	tx_stopped = 0;
904 	rx_page = 0;
905 	rx_buf = 0;
906 	rx_reuse = 0;
907 	rx_alloc = 0;
908 	rx_waive = 0;
909 	rx_busy = 0;
910 	rcu_read_lock();
911 	for (q = 0; q < vsi->num_queue_pairs; q++) {
912 		/* locate Tx ring */
913 		p = READ_ONCE(vsi->tx_rings[q]);
914 		if (!p)
915 			continue;
916 
917 		do {
918 			start = u64_stats_fetch_begin(&p->syncp);
919 			packets = p->stats.packets;
920 			bytes = p->stats.bytes;
921 		} while (u64_stats_fetch_retry(&p->syncp, start));
922 		tx_b += bytes;
923 		tx_p += packets;
924 		tx_restart += p->tx_stats.restart_queue;
925 		tx_busy += p->tx_stats.tx_busy;
926 		tx_linearize += p->tx_stats.tx_linearize;
927 		tx_force_wb += p->tx_stats.tx_force_wb;
928 		tx_stopped += p->tx_stats.tx_stopped;
929 
930 		/* locate Rx ring */
931 		p = READ_ONCE(vsi->rx_rings[q]);
932 		if (!p)
933 			continue;
934 
935 		do {
936 			start = u64_stats_fetch_begin(&p->syncp);
937 			packets = p->stats.packets;
938 			bytes = p->stats.bytes;
939 		} while (u64_stats_fetch_retry(&p->syncp, start));
940 		rx_b += bytes;
941 		rx_p += packets;
942 		rx_buf += p->rx_stats.alloc_buff_failed;
943 		rx_page += p->rx_stats.alloc_page_failed;
944 		rx_reuse += p->rx_stats.page_reuse_count;
945 		rx_alloc += p->rx_stats.page_alloc_count;
946 		rx_waive += p->rx_stats.page_waive_count;
947 		rx_busy += p->rx_stats.page_busy_count;
948 
949 		if (i40e_enabled_xdp_vsi(vsi)) {
950 			/* locate XDP ring */
951 			p = READ_ONCE(vsi->xdp_rings[q]);
952 			if (!p)
953 				continue;
954 
955 			do {
956 				start = u64_stats_fetch_begin(&p->syncp);
957 				packets = p->stats.packets;
958 				bytes = p->stats.bytes;
959 			} while (u64_stats_fetch_retry(&p->syncp, start));
960 			tx_b += bytes;
961 			tx_p += packets;
962 			tx_restart += p->tx_stats.restart_queue;
963 			tx_busy += p->tx_stats.tx_busy;
964 			tx_linearize += p->tx_stats.tx_linearize;
965 			tx_force_wb += p->tx_stats.tx_force_wb;
966 		}
967 	}
968 	rcu_read_unlock();
969 	vsi->tx_restart = tx_restart;
970 	vsi->tx_busy = tx_busy;
971 	vsi->tx_linearize = tx_linearize;
972 	vsi->tx_force_wb = tx_force_wb;
973 	vsi->tx_stopped = tx_stopped;
974 	vsi->rx_page_failed = rx_page;
975 	vsi->rx_buf_failed = rx_buf;
976 	vsi->rx_page_reuse = rx_reuse;
977 	vsi->rx_page_alloc = rx_alloc;
978 	vsi->rx_page_waive = rx_waive;
979 	vsi->rx_page_busy = rx_busy;
980 
981 	ns->rx_packets = rx_p;
982 	ns->rx_bytes = rx_b;
983 	ns->tx_packets = tx_p;
984 	ns->tx_bytes = tx_b;
985 
986 	/* update netdev stats from eth stats */
987 	i40e_update_eth_stats(vsi);
988 	ons->tx_errors = oes->tx_errors;
989 	ns->tx_errors = es->tx_errors;
990 	ons->multicast = oes->rx_multicast;
991 	ns->multicast = es->rx_multicast;
992 	ons->rx_dropped = oes->rx_discards_other;
993 	ns->rx_dropped = es->rx_discards_other;
994 	ons->rx_missed_errors = oes->rx_discards;
995 	ns->rx_missed_errors = es->rx_discards;
996 	ons->tx_dropped = oes->tx_discards;
997 	ns->tx_dropped = es->tx_discards;
998 
999 	/* pull in a couple PF stats if this is the main vsi */
1000 	if (vsi->type == I40E_VSI_MAIN) {
1001 		ns->rx_crc_errors = pf->stats.crc_errors;
1002 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
1003 		ns->rx_length_errors = pf->stats.rx_length_errors;
1004 	}
1005 }
1006 
1007 /**
1008  * i40e_update_pf_stats - Update the PF statistics counters.
1009  * @pf: the PF to be updated
1010  **/
i40e_update_pf_stats(struct i40e_pf * pf)1011 static void i40e_update_pf_stats(struct i40e_pf *pf)
1012 {
1013 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1014 	struct i40e_hw_port_stats *nsd = &pf->stats;
1015 	struct i40e_hw *hw = &pf->hw;
1016 	u32 val;
1017 	int i;
1018 
1019 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1020 			   I40E_GLPRT_GORCL(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1023 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1024 			   I40E_GLPRT_GOTCL(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1027 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.rx_discards,
1030 			   &nsd->eth.rx_discards);
1031 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1032 			   I40E_GLPRT_UPRCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.rx_unicast,
1035 			   &nsd->eth.rx_unicast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1037 			   I40E_GLPRT_MPRCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.rx_multicast,
1040 			   &nsd->eth.rx_multicast);
1041 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1042 			   I40E_GLPRT_BPRCL(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->eth.rx_broadcast,
1045 			   &nsd->eth.rx_broadcast);
1046 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1047 			   I40E_GLPRT_UPTCL(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->eth.tx_unicast,
1050 			   &nsd->eth.tx_unicast);
1051 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1052 			   I40E_GLPRT_MPTCL(hw->port),
1053 			   pf->stat_offsets_loaded,
1054 			   &osd->eth.tx_multicast,
1055 			   &nsd->eth.tx_multicast);
1056 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1057 			   I40E_GLPRT_BPTCL(hw->port),
1058 			   pf->stat_offsets_loaded,
1059 			   &osd->eth.tx_broadcast,
1060 			   &nsd->eth.tx_broadcast);
1061 
1062 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1063 			   pf->stat_offsets_loaded,
1064 			   &osd->tx_dropped_link_down,
1065 			   &nsd->tx_dropped_link_down);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->crc_errors, &nsd->crc_errors);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1074 
1075 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->mac_local_faults,
1078 			   &nsd->mac_local_faults);
1079 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->mac_remote_faults,
1082 			   &nsd->mac_remote_faults);
1083 
1084 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->rx_length_errors,
1087 			   &nsd->rx_length_errors);
1088 
1089 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1090 			   pf->stat_offsets_loaded,
1091 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1092 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1095 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1096 			   pf->stat_offsets_loaded,
1097 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1098 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1099 			   pf->stat_offsets_loaded,
1100 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1101 
1102 	for (i = 0; i < 8; i++) {
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xoff_rx[i],
1106 				   &nsd->priority_xoff_rx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xon_rx[i],
1110 				   &nsd->priority_xon_rx[i]);
1111 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1112 				   pf->stat_offsets_loaded,
1113 				   &osd->priority_xon_tx[i],
1114 				   &nsd->priority_xon_tx[i]);
1115 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1116 				   pf->stat_offsets_loaded,
1117 				   &osd->priority_xoff_tx[i],
1118 				   &nsd->priority_xoff_tx[i]);
1119 		i40e_stat_update32(hw,
1120 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1121 				   pf->stat_offsets_loaded,
1122 				   &osd->priority_xon_2_xoff[i],
1123 				   &nsd->priority_xon_2_xoff[i]);
1124 	}
1125 
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1127 			   I40E_GLPRT_PRC64L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_64, &nsd->rx_size_64);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1131 			   I40E_GLPRT_PRC127L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_127, &nsd->rx_size_127);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1135 			   I40E_GLPRT_PRC255L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_255, &nsd->rx_size_255);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1139 			   I40E_GLPRT_PRC511L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_511, &nsd->rx_size_511);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1143 			   I40E_GLPRT_PRC1023L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1147 			   I40E_GLPRT_PRC1522L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1150 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1151 			   I40E_GLPRT_PRC9522L(hw->port),
1152 			   pf->stat_offsets_loaded,
1153 			   &osd->rx_size_big, &nsd->rx_size_big);
1154 
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1156 			   I40E_GLPRT_PTC64L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_64, &nsd->tx_size_64);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1160 			   I40E_GLPRT_PTC127L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_127, &nsd->tx_size_127);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1164 			   I40E_GLPRT_PTC255L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_255, &nsd->tx_size_255);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1168 			   I40E_GLPRT_PTC511L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_511, &nsd->tx_size_511);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1172 			   I40E_GLPRT_PTC1023L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1175 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1176 			   I40E_GLPRT_PTC1522L(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1179 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1180 			   I40E_GLPRT_PTC9522L(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->tx_size_big, &nsd->tx_size_big);
1183 
1184 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1185 			   pf->stat_offsets_loaded,
1186 			   &osd->rx_undersize, &nsd->rx_undersize);
1187 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1188 			   pf->stat_offsets_loaded,
1189 			   &osd->rx_fragments, &nsd->rx_fragments);
1190 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1191 			   pf->stat_offsets_loaded,
1192 			   &osd->rx_oversize, &nsd->rx_oversize);
1193 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1194 			   pf->stat_offsets_loaded,
1195 			   &osd->rx_jabber, &nsd->rx_jabber);
1196 
1197 	/* FDIR stats */
1198 	i40e_stat_update_and_clear32(hw,
1199 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1200 			&nsd->fd_atr_match);
1201 	i40e_stat_update_and_clear32(hw,
1202 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1203 			&nsd->fd_sb_match);
1204 	i40e_stat_update_and_clear32(hw,
1205 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1206 			&nsd->fd_atr_tunnel_match);
1207 
1208 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1209 	nsd->tx_lpi_status =
1210 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1211 	nsd->rx_lpi_status =
1212 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1213 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1214 			   pf->stat_offsets_loaded,
1215 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1216 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1217 			   pf->stat_offsets_loaded,
1218 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1219 
1220 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1221 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1222 		nsd->fd_sb_status = true;
1223 	else
1224 		nsd->fd_sb_status = false;
1225 
1226 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1227 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1228 		nsd->fd_atr_status = true;
1229 	else
1230 		nsd->fd_atr_status = false;
1231 
1232 	pf->stat_offsets_loaded = true;
1233 }
1234 
1235 /**
1236  * i40e_update_stats - Update the various statistics counters.
1237  * @vsi: the VSI to be updated
1238  *
1239  * Update the various stats for this VSI and its related entities.
1240  **/
i40e_update_stats(struct i40e_vsi * vsi)1241 void i40e_update_stats(struct i40e_vsi *vsi)
1242 {
1243 	struct i40e_pf *pf = vsi->back;
1244 
1245 	if (vsi->type == I40E_VSI_MAIN)
1246 		i40e_update_pf_stats(pf);
1247 
1248 	i40e_update_vsi_stats(vsi);
1249 }
1250 
1251 /**
1252  * i40e_count_all_filters - counts VSI MAC filters
1253  * @vsi: the VSI to be searched
1254  *
1255  * Return: count of MAC filters in any state.
1256  */
i40e_count_all_filters(struct i40e_vsi * vsi)1257 int i40e_count_all_filters(struct i40e_vsi *vsi)
1258 {
1259 	struct i40e_mac_filter *f;
1260 	struct hlist_node *h;
1261 	int bkt, cnt = 0;
1262 
1263 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1264 		cnt++;
1265 
1266 	return cnt;
1267 }
1268 
1269 /**
1270  * i40e_count_active_filters - counts VSI MAC filters
1271  * @vsi: the VSI to be searched
1272  *
1273  * Return: count of active MAC filters.
1274  */
i40e_count_active_filters(struct i40e_vsi * vsi)1275 int i40e_count_active_filters(struct i40e_vsi *vsi)
1276 {
1277 	struct i40e_mac_filter *f;
1278 	struct hlist_node *h;
1279 	int bkt;
1280 	int cnt = 0;
1281 
1282 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1283 		if (f->state == I40E_FILTER_NEW ||
1284 		    f->state == I40E_FILTER_NEW_SYNC ||
1285 		    f->state == I40E_FILTER_ACTIVE)
1286 			++cnt;
1287 	}
1288 
1289 	return cnt;
1290 }
1291 
1292 /**
1293  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1294  * @vsi: the VSI to be searched
1295  * @macaddr: the MAC address
1296  * @vlan: the vlan
1297  *
1298  * Returns ptr to the filter object or NULL
1299  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1300 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1301 						const u8 *macaddr, s16 vlan)
1302 {
1303 	struct i40e_mac_filter *f;
1304 	u64 key;
1305 
1306 	if (!vsi || !macaddr)
1307 		return NULL;
1308 
1309 	key = i40e_addr_to_hkey(macaddr);
1310 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1311 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1312 		    (vlan == f->vlan))
1313 			return f;
1314 	}
1315 	return NULL;
1316 }
1317 
1318 /**
1319  * i40e_find_mac - Find a mac addr in the macvlan filters list
1320  * @vsi: the VSI to be searched
1321  * @macaddr: the MAC address we are searching for
1322  *
1323  * Returns the first filter with the provided MAC address or NULL if
1324  * MAC address was not found
1325  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1326 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1327 {
1328 	struct i40e_mac_filter *f;
1329 	u64 key;
1330 
1331 	if (!vsi || !macaddr)
1332 		return NULL;
1333 
1334 	key = i40e_addr_to_hkey(macaddr);
1335 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1336 		if ((ether_addr_equal(macaddr, f->macaddr)))
1337 			return f;
1338 	}
1339 	return NULL;
1340 }
1341 
1342 /**
1343  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1344  * @vsi: the VSI to be searched
1345  *
1346  * Returns true if VSI is in vlan mode or false otherwise
1347  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1348 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1349 {
1350 	/* If we have a PVID, always operate in VLAN mode */
1351 	if (vsi->info.pvid)
1352 		return true;
1353 
1354 	/* We need to operate in VLAN mode whenever we have any filters with
1355 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1356 	 * time, incurring search cost repeatedly. However, we can notice two
1357 	 * things:
1358 	 *
1359 	 * 1) the only place where we can gain a VLAN filter is in
1360 	 *    i40e_add_filter.
1361 	 *
1362 	 * 2) the only place where filters are actually removed is in
1363 	 *    i40e_sync_filters_subtask.
1364 	 *
1365 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1366 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1367 	 * we have to perform the full search after deleting filters in
1368 	 * i40e_sync_filters_subtask, but we already have to search
1369 	 * filters here and can perform the check at the same time. This
1370 	 * results in avoiding embedding a loop for VLAN mode inside another
1371 	 * loop over all the filters, and should maintain correctness as noted
1372 	 * above.
1373 	 */
1374 	return vsi->has_vlan_filter;
1375 }
1376 
1377 /**
1378  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1379  * @vsi: the VSI to configure
1380  * @tmp_add_list: list of filters ready to be added
1381  * @tmp_del_list: list of filters ready to be deleted
1382  * @vlan_filters: the number of active VLAN filters
1383  *
1384  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1385  * behave as expected. If we have any active VLAN filters remaining or about
1386  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1387  * so that they only match against untagged traffic. If we no longer have any
1388  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1389  * so that they match against both tagged and untagged traffic. In this way,
1390  * we ensure that we correctly receive the desired traffic. This ensures that
1391  * when we have an active VLAN we will receive only untagged traffic and
1392  * traffic matching active VLANs. If we have no active VLANs then we will
1393  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1394  *
1395  * Finally, in a similar fashion, this function also corrects filters when
1396  * there is an active PVID assigned to this VSI.
1397  *
1398  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1399  *
1400  * This function is only expected to be called from within
1401  * i40e_sync_vsi_filters.
1402  *
1403  * NOTE: This function expects to be called while under the
1404  * mac_filter_hash_lock
1405  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1406 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1407 					 struct hlist_head *tmp_add_list,
1408 					 struct hlist_head *tmp_del_list,
1409 					 int vlan_filters)
1410 {
1411 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1412 	struct i40e_mac_filter *f, *add_head;
1413 	struct i40e_new_mac_filter *new;
1414 	struct hlist_node *h;
1415 	int bkt, new_vlan;
1416 
1417 	/* To determine if a particular filter needs to be replaced we
1418 	 * have the three following conditions:
1419 	 *
1420 	 * a) if we have a PVID assigned, then all filters which are
1421 	 *    not marked as VLAN=PVID must be replaced with filters that
1422 	 *    are.
1423 	 * b) otherwise, if we have any active VLANS, all filters
1424 	 *    which are marked as VLAN=-1 must be replaced with
1425 	 *    filters marked as VLAN=0
1426 	 * c) finally, if we do not have any active VLANS, all filters
1427 	 *    which are marked as VLAN=0 must be replaced with filters
1428 	 *    marked as VLAN=-1
1429 	 */
1430 
1431 	/* Update the filters about to be added in place */
1432 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1433 		if (pvid && new->f->vlan != pvid)
1434 			new->f->vlan = pvid;
1435 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1436 			new->f->vlan = 0;
1437 		else if (!vlan_filters && new->f->vlan == 0)
1438 			new->f->vlan = I40E_VLAN_ANY;
1439 	}
1440 
1441 	/* Update the remaining active filters */
1442 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1443 		/* Combine the checks for whether a filter needs to be changed
1444 		 * and then determine the new VLAN inside the if block, in
1445 		 * order to avoid duplicating code for adding the new filter
1446 		 * then deleting the old filter.
1447 		 */
1448 		if ((pvid && f->vlan != pvid) ||
1449 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1450 		    (!vlan_filters && f->vlan == 0)) {
1451 			/* Determine the new vlan we will be adding */
1452 			if (pvid)
1453 				new_vlan = pvid;
1454 			else if (vlan_filters)
1455 				new_vlan = 0;
1456 			else
1457 				new_vlan = I40E_VLAN_ANY;
1458 
1459 			/* Create the new filter */
1460 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1461 			if (!add_head)
1462 				return -ENOMEM;
1463 
1464 			/* Create a temporary i40e_new_mac_filter */
1465 			new = kzalloc_obj(*new, GFP_ATOMIC);
1466 			if (!new)
1467 				return -ENOMEM;
1468 
1469 			new->f = add_head;
1470 			new->state = add_head->state;
1471 			if (add_head->state == I40E_FILTER_NEW)
1472 				add_head->state = I40E_FILTER_NEW_SYNC;
1473 
1474 			/* Add the new filter to the tmp list */
1475 			hlist_add_head(&new->hlist, tmp_add_list);
1476 
1477 			/* Put the original filter into the delete list */
1478 			f->state = I40E_FILTER_REMOVE;
1479 			hash_del(&f->hlist);
1480 			hlist_add_head(&f->hlist, tmp_del_list);
1481 		}
1482 	}
1483 
1484 	vsi->has_vlan_filter = !!vlan_filters;
1485 
1486 	return 0;
1487 }
1488 
1489 /**
1490  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1491  * @vsi: the vsi to configure
1492  * @new_mac: new mac filter to be added
1493  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1494  * @vlan_filters: the number of active VLAN filters
1495  * @trusted: flag if the VF is trusted
1496  *
1497  * Get new VLAN id based on current VLAN filters, trust, PVID
1498  * and vf-vlan-prune-disable flag.
1499  *
1500  * Returns the value of the new vlan filter or
1501  * the old value if no new filter is needed.
1502  */
i40e_get_vf_new_vlan(struct i40e_vsi * vsi,struct i40e_new_mac_filter * new_mac,struct i40e_mac_filter * f,int vlan_filters,bool trusted)1503 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1504 				struct i40e_new_mac_filter *new_mac,
1505 				struct i40e_mac_filter *f,
1506 				int vlan_filters,
1507 				bool trusted)
1508 {
1509 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1510 	struct i40e_pf *pf = vsi->back;
1511 	bool is_any;
1512 
1513 	if (new_mac)
1514 		f = new_mac->f;
1515 
1516 	if (pvid && f->vlan != pvid)
1517 		return pvid;
1518 
1519 	is_any = (trusted ||
1520 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1521 
1522 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1523 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1524 	    (is_any && !vlan_filters && f->vlan == 0)) {
1525 		if (is_any)
1526 			return I40E_VLAN_ANY;
1527 		else
1528 			return 0;
1529 	}
1530 
1531 	return f->vlan;
1532 }
1533 
1534 /**
1535  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1536  * @vsi: the vsi to configure
1537  * @tmp_add_list: list of filters ready to be added
1538  * @tmp_del_list: list of filters ready to be deleted
1539  * @vlan_filters: the number of active VLAN filters
1540  * @trusted: flag if the VF is trusted
1541  *
1542  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1543  * and vf-vlan-prune-disable flag.
1544  *
1545  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1546  *
1547  * This function is only expected to be called from within
1548  * i40e_sync_vsi_filters.
1549  *
1550  * NOTE: This function expects to be called while under the
1551  * mac_filter_hash_lock
1552  */
i40e_correct_vf_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters,bool trusted)1553 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1554 					    struct hlist_head *tmp_add_list,
1555 					    struct hlist_head *tmp_del_list,
1556 					    int vlan_filters,
1557 					    bool trusted)
1558 {
1559 	struct i40e_mac_filter *f, *add_head;
1560 	struct i40e_new_mac_filter *new_mac;
1561 	struct hlist_node *h;
1562 	int bkt, new_vlan;
1563 
1564 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1565 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1566 							vlan_filters, trusted);
1567 	}
1568 
1569 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1570 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1571 						trusted);
1572 		if (new_vlan != f->vlan) {
1573 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1574 			if (!add_head)
1575 				return -ENOMEM;
1576 			/* Create a temporary i40e_new_mac_filter */
1577 			new_mac = kzalloc_obj(*new_mac, GFP_ATOMIC);
1578 			if (!new_mac)
1579 				return -ENOMEM;
1580 			new_mac->f = add_head;
1581 			new_mac->state = add_head->state;
1582 			if (add_head->state == I40E_FILTER_NEW)
1583 				add_head->state = I40E_FILTER_NEW_SYNC;
1584 
1585 			/* Add the new filter to the tmp list */
1586 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1587 
1588 			/* Put the original filter into the delete list */
1589 			f->state = I40E_FILTER_REMOVE;
1590 			hash_del(&f->hlist);
1591 			hlist_add_head(&f->hlist, tmp_del_list);
1592 		}
1593 	}
1594 
1595 	vsi->has_vlan_filter = !!vlan_filters;
1596 	return 0;
1597 }
1598 
1599 /**
1600  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1601  * @vsi: the PF Main VSI - inappropriate for any other VSI
1602  * @macaddr: the MAC address
1603  *
1604  * Remove whatever filter the firmware set up so the driver can manage
1605  * its own filtering intelligently.
1606  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1607 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1608 {
1609 	struct i40e_aqc_remove_macvlan_element_data element;
1610 	struct i40e_pf *pf = vsi->back;
1611 
1612 	/* Only appropriate for the PF main VSI */
1613 	if (vsi->type != I40E_VSI_MAIN)
1614 		return;
1615 
1616 	memset(&element, 0, sizeof(element));
1617 	ether_addr_copy(element.mac_addr, macaddr);
1618 	element.vlan_tag = 0;
1619 	/* Ignore error returns, some firmware does it this way... */
1620 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1621 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1622 
1623 	memset(&element, 0, sizeof(element));
1624 	ether_addr_copy(element.mac_addr, macaddr);
1625 	element.vlan_tag = 0;
1626 	/* ...and some firmware does it this way. */
1627 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1628 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1629 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1630 }
1631 
1632 /**
1633  * i40e_add_filter - Add a mac/vlan filter to the VSI
1634  * @vsi: the VSI to be searched
1635  * @macaddr: the MAC address
1636  * @vlan: the vlan
1637  *
1638  * Returns ptr to the filter object or NULL when no memory available.
1639  *
1640  * NOTE: This function is expected to be called with mac_filter_hash_lock
1641  * being held.
1642  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1643 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1644 					const u8 *macaddr, s16 vlan)
1645 {
1646 	struct i40e_mac_filter *f;
1647 	u64 key;
1648 
1649 	if (!vsi || !macaddr)
1650 		return NULL;
1651 
1652 	f = i40e_find_filter(vsi, macaddr, vlan);
1653 	if (!f) {
1654 		f = kzalloc_obj(*f, GFP_ATOMIC);
1655 		if (!f)
1656 			return NULL;
1657 
1658 		/* Update the boolean indicating if we need to function in
1659 		 * VLAN mode.
1660 		 */
1661 		if (vlan >= 0)
1662 			vsi->has_vlan_filter = true;
1663 
1664 		ether_addr_copy(f->macaddr, macaddr);
1665 		f->vlan = vlan;
1666 		f->state = I40E_FILTER_NEW;
1667 		INIT_HLIST_NODE(&f->hlist);
1668 
1669 		key = i40e_addr_to_hkey(macaddr);
1670 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1671 
1672 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1673 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1674 	}
1675 
1676 	/* If we're asked to add a filter that has been marked for removal, it
1677 	 * is safe to simply restore it to active state. __i40e_del_filter
1678 	 * will have simply deleted any filters which were previously marked
1679 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1680 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1681 	 * task, just restore this filter to the ACTIVE state so that the
1682 	 * sync task leaves it in place
1683 	 */
1684 	if (f->state == I40E_FILTER_REMOVE)
1685 		f->state = I40E_FILTER_ACTIVE;
1686 
1687 	return f;
1688 }
1689 
1690 /**
1691  * __i40e_del_filter - Remove a specific filter from the VSI
1692  * @vsi: VSI to remove from
1693  * @f: the filter to remove from the list
1694  *
1695  * This function requires you've found * the exact filter you will remove
1696  * already, such as via i40e_find_filter or i40e_find_mac.
1697  *
1698  * NOTE: This function is expected to be called with mac_filter_hash_lock
1699  * being held.
1700  * ANOTHER NOTE: This function MUST be called from within the context of
1701  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1702  * instead of list_for_each_entry().
1703  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1704 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1705 {
1706 	if (!f)
1707 		return;
1708 
1709 	/* If the filter was never added to firmware then we can just delete it
1710 	 * directly and we don't want to set the status to remove or else an
1711 	 * admin queue command will unnecessarily fire.
1712 	 */
1713 	if ((f->state == I40E_FILTER_FAILED) ||
1714 	    (f->state == I40E_FILTER_NEW)) {
1715 		hash_del(&f->hlist);
1716 		kfree(f);
1717 	} else {
1718 		f->state = I40E_FILTER_REMOVE;
1719 	}
1720 
1721 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1722 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1723 }
1724 
1725 /**
1726  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1727  * @vsi: the VSI to be searched
1728  * @macaddr: the mac address to be filtered
1729  *
1730  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1731  * go through all the macvlan filters and add a macvlan filter for each
1732  * unique vlan that already exists. If a PVID has been assigned, instead only
1733  * add the macaddr to that VLAN.
1734  *
1735  * Returns last filter added on success, else NULL
1736  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1737 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1738 					    const u8 *macaddr)
1739 {
1740 	struct i40e_mac_filter *f, *add = NULL;
1741 	struct hlist_node *h;
1742 	int bkt;
1743 
1744 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1745 	if (vsi->info.pvid)
1746 		return i40e_add_filter(vsi, macaddr,
1747 				       le16_to_cpu(vsi->info.pvid));
1748 
1749 	if (!i40e_is_vsi_in_vlan(vsi))
1750 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1751 
1752 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1753 		if (f->state == I40E_FILTER_REMOVE)
1754 			continue;
1755 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1756 		if (!add)
1757 			return NULL;
1758 	}
1759 
1760 	return add;
1761 }
1762 
1763 /**
1764  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1765  * @vsi: the VSI to be searched
1766  * @macaddr: the mac address to be removed
1767  *
1768  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1769  * associated with.
1770  *
1771  * Returns 0 for success, or error
1772  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1773 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1774 {
1775 	struct i40e_mac_filter *f;
1776 	struct hlist_node *h;
1777 	bool found = false;
1778 	int bkt;
1779 
1780 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1781 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1782 		if (ether_addr_equal(macaddr, f->macaddr)) {
1783 			__i40e_del_filter(vsi, f);
1784 			found = true;
1785 		}
1786 	}
1787 
1788 	if (found)
1789 		return 0;
1790 	else
1791 		return -ENOENT;
1792 }
1793 
1794 /**
1795  * i40e_set_mac - NDO callback to set mac address
1796  * @netdev: network interface device structure
1797  * @p: pointer to an address structure
1798  *
1799  * Returns 0 on success, negative on failure
1800  **/
i40e_set_mac(struct net_device * netdev,void * p)1801 static int i40e_set_mac(struct net_device *netdev, void *p)
1802 {
1803 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1804 	struct i40e_vsi *vsi = np->vsi;
1805 	struct i40e_pf *pf = vsi->back;
1806 	struct i40e_hw *hw = &pf->hw;
1807 	struct sockaddr *addr = p;
1808 
1809 	if (!is_valid_ether_addr(addr->sa_data))
1810 		return -EADDRNOTAVAIL;
1811 
1812 	if (test_bit(__I40E_DOWN, pf->state) ||
1813 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1814 		return -EADDRNOTAVAIL;
1815 
1816 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1817 		netdev_info(netdev, "returning to hw mac address %pM\n",
1818 			    hw->mac.addr);
1819 	else
1820 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1821 
1822 	/* Copy the address first, so that we avoid a possible race with
1823 	 * .set_rx_mode().
1824 	 * - Remove old address from MAC filter
1825 	 * - Copy new address
1826 	 * - Add new address to MAC filter
1827 	 */
1828 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1829 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1830 	eth_hw_addr_set(netdev, addr->sa_data);
1831 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1832 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1833 
1834 	if (vsi->type == I40E_VSI_MAIN) {
1835 		int ret;
1836 
1837 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1838 						addr->sa_data, NULL);
1839 		if (ret)
1840 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1841 				    ERR_PTR(ret),
1842 				    libie_aq_str(hw->aq.asq_last_status));
1843 	}
1844 
1845 	/* schedule our worker thread which will take care of
1846 	 * applying the new filter changes
1847 	 */
1848 	i40e_service_event_schedule(pf);
1849 	return 0;
1850 }
1851 
1852 /**
1853  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1854  * @vsi: vsi structure
1855  * @seed: RSS hash seed
1856  * @lut: pointer to lookup table of lut_size
1857  * @lut_size: size of the lookup table
1858  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1859 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1860 			      u8 *lut, u16 lut_size)
1861 {
1862 	struct i40e_pf *pf = vsi->back;
1863 	struct i40e_hw *hw = &pf->hw;
1864 	int ret = 0;
1865 
1866 	if (seed) {
1867 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1868 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1869 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1870 		if (ret) {
1871 			dev_info(&pf->pdev->dev,
1872 				 "Cannot set RSS key, err %pe aq_err %s\n",
1873 				 ERR_PTR(ret),
1874 				 libie_aq_str(hw->aq.asq_last_status));
1875 			return ret;
1876 		}
1877 	}
1878 	if (lut) {
1879 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1880 
1881 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1882 		if (ret) {
1883 			dev_info(&pf->pdev->dev,
1884 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1885 				 ERR_PTR(ret),
1886 				 libie_aq_str(hw->aq.asq_last_status));
1887 			return ret;
1888 		}
1889 	}
1890 	return ret;
1891 }
1892 
1893 /**
1894  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1895  * @vsi: VSI structure
1896  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1897 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1898 {
1899 	struct i40e_pf *pf = vsi->back;
1900 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1901 	u8 *lut;
1902 	int ret;
1903 
1904 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1905 		return 0;
1906 	if (!vsi->rss_size)
1907 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1908 				      vsi->num_queue_pairs);
1909 	if (!vsi->rss_size)
1910 		return -EINVAL;
1911 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1912 	if (!lut)
1913 		return -ENOMEM;
1914 
1915 	/* Use the user configured hash keys and lookup table if there is one,
1916 	 * otherwise use default
1917 	 */
1918 	if (vsi->rss_lut_user)
1919 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1920 	else
1921 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1922 	if (vsi->rss_hkey_user)
1923 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1924 	else
1925 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1926 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1927 	kfree(lut);
1928 	return ret;
1929 }
1930 
1931 /**
1932  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1933  * @vsi: the VSI being configured,
1934  * @ctxt: VSI context structure
1935  * @enabled_tc: number of traffic classes to enable
1936  *
1937  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1938  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1939 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1940 					   struct i40e_vsi_context *ctxt,
1941 					   u8 enabled_tc)
1942 {
1943 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1944 	int i, override_q, pow, num_qps, ret;
1945 	u8 netdev_tc = 0, offset = 0;
1946 
1947 	if (vsi->type != I40E_VSI_MAIN)
1948 		return -EINVAL;
1949 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1950 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1951 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1952 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1953 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1954 
1955 	/* find the next higher power-of-2 of num queue pairs */
1956 	pow = ilog2(num_qps);
1957 	if (!is_power_of_2(num_qps))
1958 		pow++;
1959 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1960 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1961 
1962 	/* Setup queue offset/count for all TCs for given VSI */
1963 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1964 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1965 		/* See if the given TC is enabled for the given VSI */
1966 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1967 			offset = vsi->mqprio_qopt.qopt.offset[i];
1968 			qcount = vsi->mqprio_qopt.qopt.count[i];
1969 			if (qcount > max_qcount)
1970 				max_qcount = qcount;
1971 			vsi->tc_config.tc_info[i].qoffset = offset;
1972 			vsi->tc_config.tc_info[i].qcount = qcount;
1973 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1974 		} else {
1975 			/* TC is not enabled so set the offset to
1976 			 * default queue and allocate one queue
1977 			 * for the given TC.
1978 			 */
1979 			vsi->tc_config.tc_info[i].qoffset = 0;
1980 			vsi->tc_config.tc_info[i].qcount = 1;
1981 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1982 		}
1983 	}
1984 
1985 	/* Set actual Tx/Rx queue pairs */
1986 	vsi->num_queue_pairs = offset + qcount;
1987 
1988 	/* Setup queue TC[0].qmap for given VSI context */
1989 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1990 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1991 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1992 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1993 
1994 	/* Reconfigure RSS for main VSI with max queue count */
1995 	vsi->rss_size = max_qcount;
1996 	ret = i40e_vsi_config_rss(vsi);
1997 	if (ret) {
1998 		dev_info(&vsi->back->pdev->dev,
1999 			 "Failed to reconfig rss for num_queues (%u)\n",
2000 			 max_qcount);
2001 		return ret;
2002 	}
2003 	vsi->reconfig_rss = true;
2004 	dev_dbg(&vsi->back->pdev->dev,
2005 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2006 
2007 	/* Find queue count available for channel VSIs and starting offset
2008 	 * for channel VSIs
2009 	 */
2010 	override_q = vsi->mqprio_qopt.qopt.count[0];
2011 	if (override_q && override_q < vsi->num_queue_pairs) {
2012 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2013 		vsi->next_base_queue = override_q;
2014 	}
2015 	return 0;
2016 }
2017 
2018 /**
2019  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2020  * @vsi: the VSI being setup
2021  * @ctxt: VSI context structure
2022  * @enabled_tc: Enabled TCs bitmap
2023  * @is_add: True if called before Add VSI
2024  *
2025  * Setup VSI queue mapping for enabled traffic classes.
2026  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2027 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2028 				     struct i40e_vsi_context *ctxt,
2029 				     u8 enabled_tc,
2030 				     bool is_add)
2031 {
2032 	struct i40e_pf *pf = vsi->back;
2033 	u16 num_tc_qps = 0;
2034 	u16 sections = 0;
2035 	u8 netdev_tc = 0;
2036 	u16 numtc = 1;
2037 	u16 qcount;
2038 	u8 offset;
2039 	u16 qmap;
2040 	int i;
2041 
2042 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2043 	offset = 0;
2044 	/* zero out queue mapping, it will get updated on the end of the function */
2045 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2046 
2047 	if (vsi->type == I40E_VSI_MAIN) {
2048 		/* This code helps add more queue to the VSI if we have
2049 		 * more cores than RSS can support, the higher cores will
2050 		 * be served by ATR or other filters. Furthermore, the
2051 		 * non-zero req_queue_pairs says that user requested a new
2052 		 * queue count via ethtool's set_channels, so use this
2053 		 * value for queues distribution across traffic classes
2054 		 * We need at least one queue pair for the interface
2055 		 * to be usable as we see in else statement.
2056 		 */
2057 		if (vsi->req_queue_pairs > 0)
2058 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2059 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2060 			vsi->num_queue_pairs = pf->num_lan_msix;
2061 		else
2062 			vsi->num_queue_pairs = 1;
2063 	}
2064 
2065 	/* Number of queues per enabled TC */
2066 	if (vsi->type == I40E_VSI_MAIN ||
2067 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2068 		num_tc_qps = vsi->num_queue_pairs;
2069 	else
2070 		num_tc_qps = vsi->alloc_queue_pairs;
2071 
2072 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2073 		/* Find numtc from enabled TC bitmap */
2074 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2075 			if (enabled_tc & BIT(i)) /* TC is enabled */
2076 				numtc++;
2077 		}
2078 		if (!numtc) {
2079 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2080 			numtc = 1;
2081 		}
2082 		num_tc_qps = num_tc_qps / numtc;
2083 		num_tc_qps = min_t(int, num_tc_qps,
2084 				   i40e_pf_get_max_q_per_tc(pf));
2085 	}
2086 
2087 	vsi->tc_config.numtc = numtc;
2088 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2089 
2090 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2091 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2092 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2093 
2094 	/* Setup queue offset/count for all TCs for given VSI */
2095 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2096 		/* See if the given TC is enabled for the given VSI */
2097 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2098 			/* TC is enabled */
2099 			int pow, num_qps;
2100 
2101 			switch (vsi->type) {
2102 			case I40E_VSI_MAIN:
2103 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2104 					       pf->flags) &&
2105 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2106 					       pf->flags)) ||
2107 				    vsi->tc_config.enabled_tc != 1) {
2108 					qcount = min_t(int, pf->alloc_rss_size,
2109 						       num_tc_qps);
2110 					break;
2111 				}
2112 				fallthrough;
2113 			case I40E_VSI_FDIR:
2114 			case I40E_VSI_SRIOV:
2115 			case I40E_VSI_VMDQ2:
2116 			default:
2117 				qcount = num_tc_qps;
2118 				WARN_ON(i != 0);
2119 				break;
2120 			}
2121 			vsi->tc_config.tc_info[i].qoffset = offset;
2122 			vsi->tc_config.tc_info[i].qcount = qcount;
2123 
2124 			/* find the next higher power-of-2 of num queue pairs */
2125 			num_qps = qcount;
2126 			pow = 0;
2127 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2128 				pow++;
2129 				num_qps >>= 1;
2130 			}
2131 
2132 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2133 			qmap =
2134 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2135 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2136 
2137 			offset += qcount;
2138 		} else {
2139 			/* TC is not enabled so set the offset to
2140 			 * default queue and allocate one queue
2141 			 * for the given TC.
2142 			 */
2143 			vsi->tc_config.tc_info[i].qoffset = 0;
2144 			vsi->tc_config.tc_info[i].qcount = 1;
2145 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2146 
2147 			qmap = 0;
2148 		}
2149 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2150 	}
2151 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2152 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2153 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2154 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2155 		vsi->num_queue_pairs = offset;
2156 
2157 	/* Scheduler section valid can only be set for ADD VSI */
2158 	if (is_add) {
2159 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2160 
2161 		ctxt->info.up_enable_bits = enabled_tc;
2162 	}
2163 	if (vsi->type == I40E_VSI_SRIOV) {
2164 		ctxt->info.mapping_flags |=
2165 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2166 		for (i = 0; i < vsi->num_queue_pairs; i++)
2167 			ctxt->info.queue_mapping[i] =
2168 					       cpu_to_le16(vsi->base_queue + i);
2169 	} else {
2170 		ctxt->info.mapping_flags |=
2171 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2172 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2173 	}
2174 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2175 }
2176 
2177 /**
2178  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2179  * @netdev: the netdevice
2180  * @addr: address to add
2181  *
2182  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2183  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2184  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2185 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2186 {
2187 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2188 	struct i40e_vsi *vsi = np->vsi;
2189 
2190 	if (i40e_add_mac_filter(vsi, addr))
2191 		return 0;
2192 	else
2193 		return -ENOMEM;
2194 }
2195 
2196 /**
2197  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2198  * @netdev: the netdevice
2199  * @addr: address to add
2200  *
2201  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2202  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2203  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2204 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2205 {
2206 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2207 	struct i40e_vsi *vsi = np->vsi;
2208 
2209 	/* Under some circumstances, we might receive a request to delete
2210 	 * our own device address from our uc list. Because we store the
2211 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2212 	 * such requests and not delete our device address from this list.
2213 	 */
2214 	if (ether_addr_equal(addr, netdev->dev_addr))
2215 		return 0;
2216 
2217 	i40e_del_mac_filter(vsi, addr);
2218 
2219 	return 0;
2220 }
2221 
2222 /**
2223  * i40e_set_rx_mode - NDO callback to set the netdev filters
2224  * @netdev: network interface device structure
2225  **/
i40e_set_rx_mode(struct net_device * netdev)2226 static void i40e_set_rx_mode(struct net_device *netdev)
2227 {
2228 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2229 	struct i40e_vsi *vsi = np->vsi;
2230 
2231 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2232 
2233 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2234 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2235 
2236 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2237 
2238 	/* check for other flag changes */
2239 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2240 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2241 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2242 	}
2243 	i40e_service_event_schedule(vsi->back);
2244 }
2245 
2246 /**
2247  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2248  * @vsi: Pointer to VSI struct
2249  * @from: Pointer to list which contains MAC filter entries - changes to
2250  *        those entries needs to be undone.
2251  *
2252  * MAC filter entries from this list were slated for deletion.
2253  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2254 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2255 					 struct hlist_head *from)
2256 {
2257 	struct i40e_mac_filter *f;
2258 	struct hlist_node *h;
2259 
2260 	hlist_for_each_entry_safe(f, h, from, hlist) {
2261 		u64 key = i40e_addr_to_hkey(f->macaddr);
2262 
2263 		/* Move the element back into MAC filter list*/
2264 		hlist_del(&f->hlist);
2265 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2266 	}
2267 }
2268 
2269 /**
2270  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2271  * @vsi: Pointer to vsi struct
2272  * @from: Pointer to list which contains MAC filter entries - changes to
2273  *        those entries needs to be undone.
2274  *
2275  * MAC filter entries from this list were slated for addition.
2276  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2277 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2278 					 struct hlist_head *from)
2279 {
2280 	struct i40e_new_mac_filter *new;
2281 	struct hlist_node *h;
2282 
2283 	hlist_for_each_entry_safe(new, h, from, hlist) {
2284 		/* We can simply free the wrapper structure */
2285 		hlist_del(&new->hlist);
2286 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2287 		kfree(new);
2288 	}
2289 }
2290 
2291 /**
2292  * i40e_next_filter - Get the next non-broadcast filter from a list
2293  * @next: pointer to filter in list
2294  *
2295  * Returns the next non-broadcast filter in the list. Required so that we
2296  * ignore broadcast filters within the list, since these are not handled via
2297  * the normal firmware update path.
2298  */
2299 static
i40e_next_filter(struct i40e_new_mac_filter * next)2300 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2301 {
2302 	hlist_for_each_entry_continue(next, hlist) {
2303 		if (!is_broadcast_ether_addr(next->f->macaddr))
2304 			return next;
2305 	}
2306 
2307 	return NULL;
2308 }
2309 
2310 /**
2311  * i40e_update_filter_state - Update filter state based on return data
2312  * from firmware
2313  * @count: Number of filters added
2314  * @add_list: return data from fw
2315  * @add_head: pointer to first filter in current batch
2316  *
2317  * MAC filter entries from list were slated to be added to device. Returns
2318  * number of successful filters. Note that 0 does NOT mean success!
2319  **/
2320 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2321 i40e_update_filter_state(int count,
2322 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2323 			 struct i40e_new_mac_filter *add_head)
2324 {
2325 	int retval = 0;
2326 	int i;
2327 
2328 	for (i = 0; i < count; i++) {
2329 		/* Always check status of each filter. We don't need to check
2330 		 * the firmware return status because we pre-set the filter
2331 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2332 		 * request to the adminq. Thus, if it no longer matches then
2333 		 * we know the filter is active.
2334 		 */
2335 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2336 			add_head->state = I40E_FILTER_FAILED;
2337 		} else {
2338 			add_head->state = I40E_FILTER_ACTIVE;
2339 			retval++;
2340 		}
2341 
2342 		add_head = i40e_next_filter(add_head);
2343 		if (!add_head)
2344 			break;
2345 	}
2346 
2347 	return retval;
2348 }
2349 
2350 /**
2351  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2352  * @vsi: ptr to the VSI
2353  * @vsi_name: name to display in messages
2354  * @list: the list of filters to send to firmware
2355  * @num_del: the number of filters to delete
2356  * @retval: Set to -EIO on failure to delete
2357  *
2358  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2359  * *retval instead of a return value so that success does not force ret_val to
2360  * be set to 0. This ensures that a sequence of calls to this function
2361  * preserve the previous value of *retval on successful delete.
2362  */
2363 static
i40e_aqc_del_filters(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_aqc_remove_macvlan_element_data * list,int num_del,int * retval)2364 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2365 			  struct i40e_aqc_remove_macvlan_element_data *list,
2366 			  int num_del, int *retval)
2367 {
2368 	struct i40e_hw *hw = &vsi->back->hw;
2369 	enum libie_aq_err aq_status;
2370 	int aq_ret;
2371 
2372 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2373 					   &aq_status);
2374 
2375 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2376 	if (aq_ret && !(aq_status == LIBIE_AQ_RC_ENOENT)) {
2377 		*retval = -EIO;
2378 		dev_info(&vsi->back->pdev->dev,
2379 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2380 			 vsi_name, ERR_PTR(aq_ret), libie_aq_str(aq_status));
2381 	}
2382 }
2383 
2384 /**
2385  * i40e_aqc_add_filters - Request firmware to add a set of filters
2386  * @vsi: ptr to the VSI
2387  * @vsi_name: name to display in messages
2388  * @list: the list of filters to send to firmware
2389  * @add_head: Position in the add hlist
2390  * @num_add: the number of filters to add
2391  *
2392  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2393  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2394  * space for more filters.
2395  */
2396 static
i40e_aqc_add_filters(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_aqc_add_macvlan_element_data * list,struct i40e_new_mac_filter * add_head,int num_add)2397 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2398 			  struct i40e_aqc_add_macvlan_element_data *list,
2399 			  struct i40e_new_mac_filter *add_head,
2400 			  int num_add)
2401 {
2402 	struct i40e_hw *hw = &vsi->back->hw;
2403 	enum libie_aq_err aq_status;
2404 	int fcnt;
2405 
2406 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2407 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2408 
2409 	if (fcnt != num_add) {
2410 		if (vsi->type == I40E_VSI_MAIN) {
2411 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2412 			dev_warn(&vsi->back->pdev->dev,
2413 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2414 				 libie_aq_str(aq_status), vsi_name);
2415 		} else if (vsi->type == I40E_VSI_SRIOV ||
2416 			   vsi->type == I40E_VSI_VMDQ1 ||
2417 			   vsi->type == I40E_VSI_VMDQ2) {
2418 			dev_warn(&vsi->back->pdev->dev,
2419 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2420 				 libie_aq_str(aq_status), vsi_name, vsi_name);
2421 		} else {
2422 			dev_warn(&vsi->back->pdev->dev,
2423 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2424 				 libie_aq_str(aq_status), vsi_name, vsi->type);
2425 		}
2426 	}
2427 }
2428 
2429 /**
2430  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2431  * @vsi: pointer to the VSI
2432  * @vsi_name: the VSI name
2433  * @f: filter data
2434  *
2435  * This function sets or clears the promiscuous broadcast flags for VLAN
2436  * filters in order to properly receive broadcast frames. Assumes that only
2437  * broadcast filters are passed.
2438  *
2439  * Returns status indicating success or failure;
2440  **/
2441 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2442 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2443 			  struct i40e_mac_filter *f)
2444 {
2445 	bool enable = f->state == I40E_FILTER_NEW ||
2446 		      f->state == I40E_FILTER_NEW_SYNC;
2447 	struct i40e_hw *hw = &vsi->back->hw;
2448 	int aq_ret;
2449 
2450 	if (f->vlan == I40E_VLAN_ANY) {
2451 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2452 						   vsi->seid,
2453 						   enable,
2454 						   NULL);
2455 	} else {
2456 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2457 							    vsi->seid,
2458 							    enable,
2459 							    f->vlan,
2460 							    NULL);
2461 	}
2462 
2463 	if (aq_ret) {
2464 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2465 		dev_warn(&vsi->back->pdev->dev,
2466 			 "Error %s, forcing overflow promiscuous on %s\n",
2467 			 libie_aq_str(hw->aq.asq_last_status), vsi_name);
2468 	}
2469 
2470 	return aq_ret;
2471 }
2472 
2473 /**
2474  * i40e_set_promiscuous - set promiscuous mode
2475  * @pf: board private structure
2476  * @promisc: promisc on or off
2477  *
2478  * There are different ways of setting promiscuous mode on a PF depending on
2479  * what state/environment we're in.  This identifies and sets it appropriately.
2480  * Returns 0 on success.
2481  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2482 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2483 {
2484 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2485 	struct i40e_hw *hw = &pf->hw;
2486 	int aq_ret;
2487 
2488 	if (vsi->type == I40E_VSI_MAIN &&
2489 	    i40e_pf_get_main_veb(pf) &&
2490 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2491 		/* set defport ON for Main VSI instead of true promisc
2492 		 * this way we will get all unicast/multicast and VLAN
2493 		 * promisc behavior but will not get VF or VMDq traffic
2494 		 * replicated on the Main VSI.
2495 		 */
2496 		if (promisc)
2497 			aq_ret = i40e_aq_set_default_vsi(hw,
2498 							 vsi->seid,
2499 							 NULL);
2500 		else
2501 			aq_ret = i40e_aq_clear_default_vsi(hw,
2502 							   vsi->seid,
2503 							   NULL);
2504 		if (aq_ret) {
2505 			dev_info(&pf->pdev->dev,
2506 				 "Set default VSI failed, err %pe, aq_err %s\n",
2507 				 ERR_PTR(aq_ret),
2508 				 libie_aq_str(hw->aq.asq_last_status));
2509 		}
2510 	} else {
2511 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2512 						  hw,
2513 						  vsi->seid,
2514 						  promisc, NULL,
2515 						  true);
2516 		if (aq_ret) {
2517 			dev_info(&pf->pdev->dev,
2518 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2519 				 ERR_PTR(aq_ret),
2520 				 libie_aq_str(hw->aq.asq_last_status));
2521 		}
2522 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2523 						  hw,
2524 						  vsi->seid,
2525 						  promisc, NULL);
2526 		if (aq_ret) {
2527 			dev_info(&pf->pdev->dev,
2528 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2529 				 ERR_PTR(aq_ret),
2530 				 libie_aq_str(hw->aq.asq_last_status));
2531 		}
2532 	}
2533 
2534 	if (!aq_ret)
2535 		pf->cur_promisc = promisc;
2536 
2537 	return aq_ret;
2538 }
2539 
2540 /**
2541  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2542  * @vsi: ptr to the VSI
2543  *
2544  * Push any outstanding VSI filter changes through the AdminQ.
2545  *
2546  * Returns 0 or error value
2547  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2548 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2549 {
2550 	struct hlist_head tmp_add_list, tmp_del_list;
2551 	struct i40e_mac_filter *f;
2552 	struct i40e_new_mac_filter *new, *add_head = NULL;
2553 	struct i40e_hw *hw = &vsi->back->hw;
2554 	bool old_overflow, new_overflow;
2555 	unsigned int failed_filters = 0;
2556 	unsigned int vlan_filters = 0;
2557 	char vsi_name[16] = "PF";
2558 	int filter_list_len = 0;
2559 	u32 changed_flags = 0;
2560 	struct hlist_node *h;
2561 	struct i40e_pf *pf;
2562 	int num_add = 0;
2563 	int num_del = 0;
2564 	int aq_ret = 0;
2565 	int retval = 0;
2566 	u16 cmd_flags;
2567 	int list_size;
2568 	int bkt;
2569 
2570 	/* empty array typed pointers, kcalloc later */
2571 	struct i40e_aqc_add_macvlan_element_data *add_list;
2572 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2573 
2574 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2575 		usleep_range(1000, 2000);
2576 	pf = vsi->back;
2577 
2578 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2579 
2580 	if (vsi->netdev) {
2581 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2582 		vsi->current_netdev_flags = vsi->netdev->flags;
2583 	}
2584 
2585 	INIT_HLIST_HEAD(&tmp_add_list);
2586 	INIT_HLIST_HEAD(&tmp_del_list);
2587 
2588 	if (vsi->type == I40E_VSI_SRIOV)
2589 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2590 	else if (vsi->type != I40E_VSI_MAIN)
2591 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2592 
2593 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2594 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2595 
2596 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2597 		/* Create a list of filters to delete. */
2598 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2599 			if (f->state == I40E_FILTER_REMOVE) {
2600 				/* Move the element into temporary del_list */
2601 				hash_del(&f->hlist);
2602 				hlist_add_head(&f->hlist, &tmp_del_list);
2603 
2604 				/* Avoid counting removed filters */
2605 				continue;
2606 			}
2607 			if (f->state == I40E_FILTER_NEW) {
2608 				/* Create a temporary i40e_new_mac_filter */
2609 				new = kzalloc_obj(*new, GFP_ATOMIC);
2610 				if (!new)
2611 					goto err_no_memory_locked;
2612 
2613 				/* Store pointer to the real filter */
2614 				new->f = f;
2615 				new->state = f->state;
2616 
2617 				/* Add it to the hash list */
2618 				hlist_add_head(&new->hlist, &tmp_add_list);
2619 				f->state = I40E_FILTER_NEW_SYNC;
2620 			}
2621 
2622 			/* Count the number of active (current and new) VLAN
2623 			 * filters we have now. Does not count filters which
2624 			 * are marked for deletion.
2625 			 */
2626 			if (f->vlan > 0)
2627 				vlan_filters++;
2628 		}
2629 
2630 		if (vsi->type != I40E_VSI_SRIOV)
2631 			retval = i40e_correct_mac_vlan_filters
2632 				(vsi, &tmp_add_list, &tmp_del_list,
2633 				 vlan_filters);
2634 		else if (pf->vf)
2635 			retval = i40e_correct_vf_mac_vlan_filters
2636 				(vsi, &tmp_add_list, &tmp_del_list,
2637 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2638 
2639 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2640 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2641 
2642 		if (retval)
2643 			goto err_no_memory_locked;
2644 
2645 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2646 	}
2647 
2648 	/* Now process 'del_list' outside the lock */
2649 	if (!hlist_empty(&tmp_del_list)) {
2650 		filter_list_len = hw->aq.asq_buf_size /
2651 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2652 		list_size = filter_list_len *
2653 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2654 		del_list = kzalloc(list_size, GFP_ATOMIC);
2655 		if (!del_list)
2656 			goto err_no_memory;
2657 
2658 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2659 			cmd_flags = 0;
2660 
2661 			/* handle broadcast filters by updating the broadcast
2662 			 * promiscuous flag and release filter list.
2663 			 */
2664 			if (is_broadcast_ether_addr(f->macaddr)) {
2665 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2666 
2667 				hlist_del(&f->hlist);
2668 				kfree(f);
2669 				continue;
2670 			}
2671 
2672 			/* add to delete list */
2673 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2674 			if (f->vlan == I40E_VLAN_ANY) {
2675 				del_list[num_del].vlan_tag = 0;
2676 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2677 			} else {
2678 				del_list[num_del].vlan_tag =
2679 					cpu_to_le16((u16)(f->vlan));
2680 			}
2681 
2682 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2683 			del_list[num_del].flags = cmd_flags;
2684 			num_del++;
2685 
2686 			/* flush a full buffer */
2687 			if (num_del == filter_list_len) {
2688 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2689 						     num_del, &retval);
2690 				memset(del_list, 0, list_size);
2691 				num_del = 0;
2692 			}
2693 			/* Release memory for MAC filter entries which were
2694 			 * synced up with HW.
2695 			 */
2696 			hlist_del(&f->hlist);
2697 			kfree(f);
2698 		}
2699 
2700 		if (num_del) {
2701 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2702 					     num_del, &retval);
2703 		}
2704 
2705 		kfree(del_list);
2706 		del_list = NULL;
2707 	}
2708 
2709 	if (!hlist_empty(&tmp_add_list)) {
2710 		/* Do all the adds now. */
2711 		filter_list_len = hw->aq.asq_buf_size /
2712 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2713 		list_size = filter_list_len *
2714 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2715 		add_list = kzalloc(list_size, GFP_ATOMIC);
2716 		if (!add_list)
2717 			goto err_no_memory;
2718 
2719 		num_add = 0;
2720 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2721 			/* handle broadcast filters by updating the broadcast
2722 			 * promiscuous flag instead of adding a MAC filter.
2723 			 */
2724 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2725 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2726 							      new->f))
2727 					new->state = I40E_FILTER_FAILED;
2728 				else
2729 					new->state = I40E_FILTER_ACTIVE;
2730 				continue;
2731 			}
2732 
2733 			/* add to add array */
2734 			if (num_add == 0)
2735 				add_head = new;
2736 			cmd_flags = 0;
2737 			ether_addr_copy(add_list[num_add].mac_addr,
2738 					new->f->macaddr);
2739 			if (new->f->vlan == I40E_VLAN_ANY) {
2740 				add_list[num_add].vlan_tag = 0;
2741 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2742 			} else {
2743 				add_list[num_add].vlan_tag =
2744 					cpu_to_le16((u16)(new->f->vlan));
2745 			}
2746 			add_list[num_add].queue_number = 0;
2747 			/* set invalid match method for later detection */
2748 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2749 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2750 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2751 			num_add++;
2752 
2753 			/* flush a full buffer */
2754 			if (num_add == filter_list_len) {
2755 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2756 						     add_head, num_add);
2757 				memset(add_list, 0, list_size);
2758 				num_add = 0;
2759 			}
2760 		}
2761 		if (num_add) {
2762 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2763 					     num_add);
2764 		}
2765 		/* Now move all of the filters from the temp add list back to
2766 		 * the VSI's list.
2767 		 */
2768 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2769 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2770 			/* Only update the state if we're still NEW */
2771 			if (new->f->state == I40E_FILTER_NEW ||
2772 			    new->f->state == I40E_FILTER_NEW_SYNC)
2773 				new->f->state = new->state;
2774 			hlist_del(&new->hlist);
2775 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2776 			kfree(new);
2777 		}
2778 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2779 		kfree(add_list);
2780 		add_list = NULL;
2781 	}
2782 
2783 	/* Determine the number of active and failed filters. */
2784 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2785 	vsi->active_filters = 0;
2786 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2787 		if (f->state == I40E_FILTER_ACTIVE)
2788 			vsi->active_filters++;
2789 		else if (f->state == I40E_FILTER_FAILED)
2790 			failed_filters++;
2791 	}
2792 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2793 
2794 	/* Check if we are able to exit overflow promiscuous mode. We can
2795 	 * safely exit if we didn't just enter, we no longer have any failed
2796 	 * filters, and we have reduced filters below the threshold value.
2797 	 */
2798 	if (old_overflow && !failed_filters &&
2799 	    vsi->active_filters < vsi->promisc_threshold) {
2800 		dev_info(&pf->pdev->dev,
2801 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2802 			 vsi_name);
2803 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 		vsi->promisc_threshold = 0;
2805 	}
2806 
2807 	/* if the VF is not trusted do not do promisc */
2808 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2809 	    !pf->vf[vsi->vf_id].trusted) {
2810 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2811 		goto out;
2812 	}
2813 
2814 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2815 
2816 	/* If we are entering overflow promiscuous, we need to calculate a new
2817 	 * threshold for when we are safe to exit
2818 	 */
2819 	if (!old_overflow && new_overflow)
2820 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2821 
2822 	/* check for changes in promiscuous modes */
2823 	if (changed_flags & IFF_ALLMULTI) {
2824 		bool cur_multipromisc;
2825 
2826 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2827 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2828 							       vsi->seid,
2829 							       cur_multipromisc,
2830 							       NULL);
2831 		if (aq_ret) {
2832 			retval = i40e_aq_rc_to_posix(aq_ret,
2833 						     hw->aq.asq_last_status);
2834 			dev_info(&pf->pdev->dev,
2835 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2836 				 vsi_name,
2837 				 ERR_PTR(aq_ret),
2838 				 libie_aq_str(hw->aq.asq_last_status));
2839 		} else {
2840 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2841 				 cur_multipromisc ? "entering" : "leaving");
2842 		}
2843 	}
2844 
2845 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2846 		bool cur_promisc;
2847 
2848 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2849 			       new_overflow);
2850 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2851 		if (aq_ret) {
2852 			retval = i40e_aq_rc_to_posix(aq_ret,
2853 						     hw->aq.asq_last_status);
2854 			dev_info(&pf->pdev->dev,
2855 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2856 				 cur_promisc ? "on" : "off",
2857 				 vsi_name,
2858 				 ERR_PTR(aq_ret),
2859 				 libie_aq_str(hw->aq.asq_last_status));
2860 		}
2861 	}
2862 out:
2863 	/* if something went wrong then set the changed flag so we try again */
2864 	if (retval)
2865 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2866 
2867 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2868 	return retval;
2869 
2870 err_no_memory:
2871 	/* Restore elements on the temporary add and delete lists */
2872 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2873 err_no_memory_locked:
2874 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2875 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2876 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2877 
2878 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2879 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2880 	return -ENOMEM;
2881 }
2882 
2883 /**
2884  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2885  * @pf: board private structure
2886  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2887 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2888 {
2889 	struct i40e_vsi *vsi;
2890 	int v;
2891 
2892 	if (!pf)
2893 		return;
2894 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2895 		return;
2896 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2897 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2898 		return;
2899 	}
2900 
2901 	i40e_pf_for_each_vsi(pf, v, vsi) {
2902 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2903 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2904 			int ret = i40e_sync_vsi_filters(vsi);
2905 
2906 			if (ret) {
2907 				/* come back and try again later */
2908 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2909 					pf->state);
2910 				break;
2911 			}
2912 		}
2913 	}
2914 }
2915 
2916 /**
2917  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2918  *
2919  * @vsi: VSI to calculate rx_buf_len from
2920  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)2921 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2922 {
2923 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2924 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2925 
2926 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2927 }
2928 
2929 /**
2930  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2931  * @vsi: the vsi
2932  * @xdp_prog: XDP program
2933  **/
i40e_max_vsi_frame_size(struct i40e_vsi * vsi,struct bpf_prog * xdp_prog)2934 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2935 				   struct bpf_prog *xdp_prog)
2936 {
2937 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2938 	u16 chain_len;
2939 
2940 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2941 		chain_len = 1;
2942 	else
2943 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2944 
2945 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2946 }
2947 
2948 /**
2949  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2950  * @netdev: network interface device structure
2951  * @new_mtu: new value for maximum frame size
2952  *
2953  * Returns 0 on success, negative on failure
2954  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2955 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2956 {
2957 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2958 	struct i40e_vsi *vsi = np->vsi;
2959 	struct i40e_pf *pf = vsi->back;
2960 	int frame_size;
2961 
2962 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2963 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2964 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2965 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2966 		return -EINVAL;
2967 	}
2968 
2969 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2970 		   netdev->mtu, new_mtu);
2971 	WRITE_ONCE(netdev->mtu, new_mtu);
2972 	if (netif_running(netdev))
2973 		i40e_vsi_reinit_locked(vsi);
2974 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2975 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2976 	return 0;
2977 }
2978 
2979 /**
2980  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2981  * @vsi: the vsi being adjusted
2982  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2983 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2984 {
2985 	struct i40e_vsi_context ctxt;
2986 	int ret;
2987 
2988 	/* Don't modify stripping options if a port VLAN is active */
2989 	if (vsi->info.pvid)
2990 		return;
2991 
2992 	if ((vsi->info.valid_sections &
2993 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2994 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2995 		return;  /* already enabled */
2996 
2997 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2998 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2999 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3000 
3001 	ctxt.seid = vsi->seid;
3002 	ctxt.info = vsi->info;
3003 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3004 	if (ret) {
3005 		dev_info(&vsi->back->pdev->dev,
3006 			 "update vlan stripping failed, err %pe aq_err %s\n",
3007 			 ERR_PTR(ret),
3008 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3009 	}
3010 }
3011 
3012 /**
3013  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3014  * @vsi: the vsi being adjusted
3015  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)3016 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3017 {
3018 	struct i40e_vsi_context ctxt;
3019 	int ret;
3020 
3021 	/* Don't modify stripping options if a port VLAN is active */
3022 	if (vsi->info.pvid)
3023 		return;
3024 
3025 	if ((vsi->info.valid_sections &
3026 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3027 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3028 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3029 		return;  /* already disabled */
3030 
3031 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3032 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3033 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3034 
3035 	ctxt.seid = vsi->seid;
3036 	ctxt.info = vsi->info;
3037 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3038 	if (ret) {
3039 		dev_info(&vsi->back->pdev->dev,
3040 			 "update vlan stripping failed, err %pe aq_err %s\n",
3041 			 ERR_PTR(ret),
3042 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3043 	}
3044 }
3045 
3046 /**
3047  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3048  * @vsi: the vsi being configured
3049  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3050  *
3051  * This is a helper function for adding a new MAC/VLAN filter with the
3052  * specified VLAN for each existing MAC address already in the hash table.
3053  * This function does *not* perform any accounting to update filters based on
3054  * VLAN mode.
3055  *
3056  * NOTE: this function expects to be called while under the
3057  * mac_filter_hash_lock
3058  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3059 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3060 {
3061 	struct i40e_mac_filter *f, *add_f;
3062 	struct hlist_node *h;
3063 	int bkt;
3064 
3065 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3066 		/* If we're asked to add a filter that has been marked for
3067 		 * removal, it is safe to simply restore it to active state.
3068 		 * __i40e_del_filter will have simply deleted any filters which
3069 		 * were previously marked NEW or FAILED, so if it is currently
3070 		 * marked REMOVE it must have previously been ACTIVE. Since we
3071 		 * haven't yet run the sync filters task, just restore this
3072 		 * filter to the ACTIVE state so that the sync task leaves it
3073 		 * in place.
3074 		 */
3075 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3076 			f->state = I40E_FILTER_ACTIVE;
3077 			continue;
3078 		} else if (f->state == I40E_FILTER_REMOVE) {
3079 			continue;
3080 		}
3081 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3082 		if (!add_f) {
3083 			dev_info(&vsi->back->pdev->dev,
3084 				 "Could not add vlan filter %d for %pM\n",
3085 				 vid, f->macaddr);
3086 			return -ENOMEM;
3087 		}
3088 	}
3089 
3090 	return 0;
3091 }
3092 
3093 /**
3094  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3095  * @vsi: the VSI being configured
3096  * @vid: VLAN id to be added
3097  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3098 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3099 {
3100 	int err;
3101 
3102 	if (vsi->info.pvid)
3103 		return -EINVAL;
3104 
3105 	/* The network stack will attempt to add VID=0, with the intention to
3106 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3107 	 * these packets by default when configured to receive untagged
3108 	 * packets, so we don't need to add a filter for this case.
3109 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3110 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3111 	 * Thus, we do not want to actually add a filter for VID=0
3112 	 */
3113 	if (!vid)
3114 		return 0;
3115 
3116 	/* Locked once because all functions invoked below iterates list*/
3117 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3118 	err = i40e_add_vlan_all_mac(vsi, vid);
3119 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3120 	if (err)
3121 		return err;
3122 
3123 	/* schedule our worker thread which will take care of
3124 	 * applying the new filter changes
3125 	 */
3126 	i40e_service_event_schedule(vsi->back);
3127 	return 0;
3128 }
3129 
3130 /**
3131  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3132  * @vsi: the vsi being configured
3133  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3134  *
3135  * This function should be used to remove all VLAN filters which match the
3136  * given VID. It does not schedule the service event and does not take the
3137  * mac_filter_hash_lock so it may be combined with other operations under
3138  * a single invocation of the mac_filter_hash_lock.
3139  *
3140  * NOTE: this function expects to be called while under the
3141  * mac_filter_hash_lock
3142  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3143 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3144 {
3145 	struct i40e_mac_filter *f;
3146 	struct hlist_node *h;
3147 	int bkt;
3148 
3149 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3150 		if (f->vlan == vid)
3151 			__i40e_del_filter(vsi, f);
3152 	}
3153 }
3154 
3155 /**
3156  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3157  * @vsi: the VSI being configured
3158  * @vid: VLAN id to be removed
3159  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3160 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3161 {
3162 	if (!vid || vsi->info.pvid)
3163 		return;
3164 
3165 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3166 	i40e_rm_vlan_all_mac(vsi, vid);
3167 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3168 
3169 	/* schedule our worker thread which will take care of
3170 	 * applying the new filter changes
3171 	 */
3172 	i40e_service_event_schedule(vsi->back);
3173 }
3174 
3175 /**
3176  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3177  * @netdev: network interface to be adjusted
3178  * @proto: unused protocol value
3179  * @vid: vlan id to be added
3180  *
3181  * net_device_ops implementation for adding vlan ids
3182  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3183 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3184 				__always_unused __be16 proto, u16 vid)
3185 {
3186 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3187 	struct i40e_vsi *vsi = np->vsi;
3188 	int ret = 0;
3189 
3190 	if (vid >= VLAN_N_VID)
3191 		return -EINVAL;
3192 
3193 	ret = i40e_vsi_add_vlan(vsi, vid);
3194 	if (!ret)
3195 		set_bit(vid, vsi->active_vlans);
3196 
3197 	return ret;
3198 }
3199 
3200 /**
3201  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3202  * @netdev: network interface to be adjusted
3203  * @proto: unused protocol value
3204  * @vid: vlan id to be added
3205  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3206 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3207 				    __always_unused __be16 proto, u16 vid)
3208 {
3209 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3210 	struct i40e_vsi *vsi = np->vsi;
3211 
3212 	if (vid >= VLAN_N_VID)
3213 		return;
3214 	set_bit(vid, vsi->active_vlans);
3215 }
3216 
3217 /**
3218  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3219  * @netdev: network interface to be adjusted
3220  * @proto: unused protocol value
3221  * @vid: vlan id to be removed
3222  *
3223  * net_device_ops implementation for removing vlan ids
3224  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3225 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3226 				 __always_unused __be16 proto, u16 vid)
3227 {
3228 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3229 	struct i40e_vsi *vsi = np->vsi;
3230 
3231 	/* return code is ignored as there is nothing a user
3232 	 * can do about failure to remove and a log message was
3233 	 * already printed from the other function
3234 	 */
3235 	i40e_vsi_kill_vlan(vsi, vid);
3236 
3237 	clear_bit(vid, vsi->active_vlans);
3238 
3239 	return 0;
3240 }
3241 
3242 /**
3243  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3244  * @vsi: the vsi being brought back up
3245  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3246 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3247 {
3248 	u16 vid;
3249 
3250 	if (!vsi->netdev)
3251 		return;
3252 
3253 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3254 		i40e_vlan_stripping_enable(vsi);
3255 	else
3256 		i40e_vlan_stripping_disable(vsi);
3257 
3258 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3259 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3260 					vid);
3261 }
3262 
3263 /**
3264  * i40e_vsi_add_pvid - Add pvid for the VSI
3265  * @vsi: the vsi being adjusted
3266  * @vid: the vlan id to set as a PVID
3267  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3268 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3269 {
3270 	struct i40e_vsi_context ctxt;
3271 	int ret;
3272 
3273 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3274 	vsi->info.pvid = cpu_to_le16(vid);
3275 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3276 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3277 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3278 
3279 	ctxt.seid = vsi->seid;
3280 	ctxt.info = vsi->info;
3281 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3282 	if (ret) {
3283 		dev_info(&vsi->back->pdev->dev,
3284 			 "add pvid failed, err %pe aq_err %s\n",
3285 			 ERR_PTR(ret),
3286 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3287 		return -ENOENT;
3288 	}
3289 
3290 	return 0;
3291 }
3292 
3293 /**
3294  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3295  * @vsi: the vsi being adjusted
3296  *
3297  * Just use the vlan_rx_register() service to put it back to normal
3298  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3299 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3300 {
3301 	vsi->info.pvid = 0;
3302 
3303 	i40e_vlan_stripping_disable(vsi);
3304 }
3305 
3306 /**
3307  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3308  * @vsi: ptr to the VSI
3309  *
3310  * If this function returns with an error, then it's possible one or
3311  * more of the rings is populated (while the rest are not).  It is the
3312  * callers duty to clean those orphaned rings.
3313  *
3314  * Return 0 on success, negative on failure
3315  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3316 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3317 {
3318 	int i, err = 0;
3319 
3320 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3321 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3322 
3323 	if (!i40e_enabled_xdp_vsi(vsi))
3324 		return err;
3325 
3326 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3327 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3328 
3329 	return err;
3330 }
3331 
3332 /**
3333  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3334  * @vsi: ptr to the VSI
3335  *
3336  * Free VSI's transmit software resources
3337  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3338 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3339 {
3340 	int i;
3341 
3342 	if (vsi->tx_rings) {
3343 		for (i = 0; i < vsi->num_queue_pairs; i++)
3344 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3345 				i40e_free_tx_resources(vsi->tx_rings[i]);
3346 	}
3347 
3348 	if (vsi->xdp_rings) {
3349 		for (i = 0; i < vsi->num_queue_pairs; i++)
3350 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3351 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3352 	}
3353 }
3354 
3355 /**
3356  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3357  * @vsi: ptr to the VSI
3358  *
3359  * If this function returns with an error, then it's possible one or
3360  * more of the rings is populated (while the rest are not).  It is the
3361  * callers duty to clean those orphaned rings.
3362  *
3363  * Return 0 on success, negative on failure
3364  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3365 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3366 {
3367 	int i, err = 0;
3368 
3369 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3370 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3371 	return err;
3372 }
3373 
3374 /**
3375  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3376  * @vsi: ptr to the VSI
3377  *
3378  * Free all receive software resources
3379  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3380 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3381 {
3382 	int i;
3383 
3384 	if (!vsi->rx_rings)
3385 		return;
3386 
3387 	for (i = 0; i < vsi->num_queue_pairs; i++)
3388 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3389 			i40e_free_rx_resources(vsi->rx_rings[i]);
3390 }
3391 
3392 /**
3393  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3394  * @ring: The Tx ring to configure
3395  *
3396  * This enables/disables XPS for a given Tx descriptor ring
3397  * based on the TCs enabled for the VSI that ring belongs to.
3398  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3399 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3400 {
3401 	int cpu;
3402 
3403 	if (!ring->q_vector || !ring->netdev || ring->ch)
3404 		return;
3405 
3406 	/* We only initialize XPS once, so as not to overwrite user settings */
3407 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3408 		return;
3409 
3410 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3411 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3412 			    ring->queue_index);
3413 }
3414 
3415 /**
3416  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3417  * @ring: The Tx or Rx ring
3418  *
3419  * Returns the AF_XDP buffer pool or NULL.
3420  **/
i40e_xsk_pool(struct i40e_ring * ring)3421 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3422 {
3423 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3424 	int qid = ring->queue_index;
3425 
3426 	if (ring_is_xdp(ring))
3427 		qid -= ring->vsi->alloc_queue_pairs;
3428 
3429 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3430 		return NULL;
3431 
3432 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3433 }
3434 
3435 /**
3436  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3437  * @ring: The Tx ring to configure
3438  *
3439  * Configure the Tx descriptor ring in the HMC context.
3440  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3441 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3442 {
3443 	struct i40e_vsi *vsi = ring->vsi;
3444 	u16 pf_q = vsi->base_queue + ring->queue_index;
3445 	struct i40e_hw *hw = &vsi->back->hw;
3446 	struct i40e_hmc_obj_txq tx_ctx;
3447 	u32 qtx_ctl = 0;
3448 	int err = 0;
3449 
3450 	if (ring_is_xdp(ring))
3451 		ring->xsk_pool = i40e_xsk_pool(ring);
3452 
3453 	/* some ATR related tx ring init */
3454 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3455 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3456 		ring->atr_count = 0;
3457 	} else {
3458 		ring->atr_sample_rate = 0;
3459 	}
3460 
3461 	/* configure XPS */
3462 	i40e_config_xps_tx_ring(ring);
3463 
3464 	/* clear the context structure first */
3465 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3466 
3467 	tx_ctx.new_context = 1;
3468 	tx_ctx.base = (ring->dma / 128);
3469 	tx_ctx.qlen = ring->count;
3470 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3471 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3472 		tx_ctx.fd_ena = 1;
3473 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3474 		tx_ctx.timesync_ena = 1;
3475 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3476 	if (vsi->type != I40E_VSI_FDIR)
3477 		tx_ctx.head_wb_ena = 1;
3478 	tx_ctx.head_wb_addr = ring->dma +
3479 			      (ring->count * sizeof(struct i40e_tx_desc));
3480 
3481 	/* As part of VSI creation/update, FW allocates certain
3482 	 * Tx arbitration queue sets for each TC enabled for
3483 	 * the VSI. The FW returns the handles to these queue
3484 	 * sets as part of the response buffer to Add VSI,
3485 	 * Update VSI, etc. AQ commands. It is expected that
3486 	 * these queue set handles be associated with the Tx
3487 	 * queues by the driver as part of the TX queue context
3488 	 * initialization. This has to be done regardless of
3489 	 * DCB as by default everything is mapped to TC0.
3490 	 */
3491 
3492 	if (ring->ch)
3493 		tx_ctx.rdylist =
3494 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3495 
3496 	else
3497 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3498 
3499 	tx_ctx.rdylist_act = 0;
3500 
3501 	/* clear the context in the HMC */
3502 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3503 	if (err) {
3504 		dev_info(&vsi->back->pdev->dev,
3505 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3506 			 ring->queue_index, pf_q, err);
3507 		return -ENOMEM;
3508 	}
3509 
3510 	/* set the context in the HMC */
3511 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3512 	if (err) {
3513 		dev_info(&vsi->back->pdev->dev,
3514 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3515 			 ring->queue_index, pf_q, err);
3516 		return -ENOMEM;
3517 	}
3518 
3519 	/* Now associate this queue with this PCI function */
3520 	if (ring->ch) {
3521 		if (ring->ch->type == I40E_VSI_VMDQ2)
3522 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3523 		else
3524 			return -EINVAL;
3525 
3526 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3527 				      ring->ch->vsi_number);
3528 	} else {
3529 		if (vsi->type == I40E_VSI_VMDQ2) {
3530 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3531 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3532 					      vsi->id);
3533 		} else {
3534 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3535 		}
3536 	}
3537 
3538 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3539 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3540 	i40e_flush(hw);
3541 
3542 	/* cache tail off for easier writes later */
3543 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3544 
3545 	return 0;
3546 }
3547 
3548 /**
3549  * i40e_rx_offset - Return expected offset into page to access data
3550  * @rx_ring: Ring we are requesting offset of
3551  *
3552  * Returns the offset value for ring into the data buffer.
3553  */
i40e_rx_offset(struct i40e_ring * rx_ring)3554 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3555 {
3556 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3557 }
3558 
3559 /**
3560  * i40e_configure_rx_ring - Configure a receive ring context
3561  * @ring: The Rx ring to configure
3562  *
3563  * Configure the Rx descriptor ring in the HMC context.
3564  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3565 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3566 {
3567 	struct i40e_vsi *vsi = ring->vsi;
3568 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3569 	u16 pf_q = vsi->base_queue + ring->queue_index;
3570 	struct i40e_hw *hw = &vsi->back->hw;
3571 	struct i40e_hmc_obj_rxq rx_ctx;
3572 	int err = 0;
3573 	bool ok;
3574 
3575 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3576 
3577 	/* clear the context structure first */
3578 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3579 
3580 	ring->rx_buf_len = vsi->rx_buf_len;
3581 
3582 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3583 	if (ring->vsi->type != I40E_VSI_MAIN)
3584 		goto skip;
3585 
3586 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3587 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3588 					 ring->queue_index,
3589 					 ring->q_vector->napi.napi_id,
3590 					 ring->rx_buf_len);
3591 		if (err)
3592 			return err;
3593 	}
3594 
3595 	ring->xsk_pool = i40e_xsk_pool(ring);
3596 	if (ring->xsk_pool) {
3597 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3598 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3599 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3600 					 ring->queue_index,
3601 					 ring->q_vector->napi.napi_id,
3602 					 ring->rx_buf_len);
3603 		if (err)
3604 			return err;
3605 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3606 						 MEM_TYPE_XSK_BUFF_POOL,
3607 						 NULL);
3608 		if (err)
3609 			return err;
3610 		dev_info(&vsi->back->pdev->dev,
3611 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3612 			 ring->queue_index);
3613 
3614 	} else {
3615 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3616 						 MEM_TYPE_PAGE_SHARED,
3617 						 NULL);
3618 		if (err)
3619 			return err;
3620 	}
3621 
3622 skip:
3623 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3624 
3625 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3626 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3627 
3628 	rx_ctx.base = (ring->dma / 128);
3629 	rx_ctx.qlen = ring->count;
3630 
3631 	/* use 16 byte descriptors */
3632 	rx_ctx.dsize = 0;
3633 
3634 	/* descriptor type is always zero
3635 	 * rx_ctx.dtype = 0;
3636 	 */
3637 	rx_ctx.hsplit_0 = 0;
3638 
3639 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3640 	if (hw->revision_id == 0)
3641 		rx_ctx.lrxqthresh = 0;
3642 	else
3643 		rx_ctx.lrxqthresh = 1;
3644 	rx_ctx.crcstrip = 1;
3645 	rx_ctx.l2tsel = 1;
3646 	/* this controls whether VLAN is stripped from inner headers */
3647 	rx_ctx.showiv = 0;
3648 	/* set the prefena field to 1 because the manual says to */
3649 	rx_ctx.prefena = 1;
3650 
3651 	/* clear the context in the HMC */
3652 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3653 	if (err) {
3654 		dev_info(&vsi->back->pdev->dev,
3655 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3656 			 ring->queue_index, pf_q, err);
3657 		return -ENOMEM;
3658 	}
3659 
3660 	/* set the context in the HMC */
3661 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3662 	if (err) {
3663 		dev_info(&vsi->back->pdev->dev,
3664 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3665 			 ring->queue_index, pf_q, err);
3666 		return -ENOMEM;
3667 	}
3668 
3669 	/* configure Rx buffer alignment */
3670 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3671 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3672 			dev_info(&vsi->back->pdev->dev,
3673 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3674 			return -EOPNOTSUPP;
3675 		}
3676 		clear_ring_build_skb_enabled(ring);
3677 	} else {
3678 		set_ring_build_skb_enabled(ring);
3679 	}
3680 
3681 	ring->rx_offset = i40e_rx_offset(ring);
3682 
3683 	/* cache tail for quicker writes, and clear the reg before use */
3684 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3685 	writel(0, ring->tail);
3686 
3687 	if (ring->xsk_pool) {
3688 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3689 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3690 	} else {
3691 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3692 	}
3693 	if (!ok) {
3694 		/* Log this in case the user has forgotten to give the kernel
3695 		 * any buffers, even later in the application.
3696 		 */
3697 		dev_info(&vsi->back->pdev->dev,
3698 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3699 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3700 			 ring->queue_index, pf_q);
3701 	}
3702 
3703 	return 0;
3704 }
3705 
3706 /**
3707  * i40e_vsi_configure_tx - Configure the VSI for Tx
3708  * @vsi: VSI structure describing this set of rings and resources
3709  *
3710  * Configure the Tx VSI for operation.
3711  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3712 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3713 {
3714 	int err = 0;
3715 	u16 i;
3716 
3717 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3718 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3719 
3720 	if (err || !i40e_enabled_xdp_vsi(vsi))
3721 		return err;
3722 
3723 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3724 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3725 
3726 	return err;
3727 }
3728 
3729 /**
3730  * i40e_vsi_configure_rx - Configure the VSI for Rx
3731  * @vsi: the VSI being configured
3732  *
3733  * Configure the Rx VSI for operation.
3734  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3735 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3736 {
3737 	int err = 0;
3738 	u16 i;
3739 
3740 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3741 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3742 
3743 #if (PAGE_SIZE < 8192)
3744 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3745 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3746 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3747 		vsi->max_frame = vsi->rx_buf_len;
3748 	}
3749 #endif
3750 
3751 	/* set up individual rings */
3752 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3753 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3754 
3755 	return err;
3756 }
3757 
3758 /**
3759  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3760  * @vsi: ptr to the VSI
3761  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3762 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3763 {
3764 	struct i40e_ring *tx_ring, *rx_ring;
3765 	u16 qoffset, qcount;
3766 	int i, n;
3767 
3768 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3769 		/* Reset the TC information */
3770 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3771 			rx_ring = vsi->rx_rings[i];
3772 			tx_ring = vsi->tx_rings[i];
3773 			rx_ring->dcb_tc = 0;
3774 			tx_ring->dcb_tc = 0;
3775 		}
3776 		return;
3777 	}
3778 
3779 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3780 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3781 			continue;
3782 
3783 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3784 		qcount = vsi->tc_config.tc_info[n].qcount;
3785 		for (i = qoffset; i < (qoffset + qcount); i++) {
3786 			rx_ring = vsi->rx_rings[i];
3787 			tx_ring = vsi->tx_rings[i];
3788 			rx_ring->dcb_tc = n;
3789 			tx_ring->dcb_tc = n;
3790 		}
3791 	}
3792 }
3793 
3794 /**
3795  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3796  * @vsi: ptr to the VSI
3797  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3798 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3799 {
3800 	if (vsi->netdev)
3801 		i40e_set_rx_mode(vsi->netdev);
3802 }
3803 
3804 /**
3805  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3806  * @pf: Pointer to the targeted PF
3807  *
3808  * Set all flow director counters to 0.
3809  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3810 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3811 {
3812 	pf->fd_tcp4_filter_cnt = 0;
3813 	pf->fd_udp4_filter_cnt = 0;
3814 	pf->fd_sctp4_filter_cnt = 0;
3815 	pf->fd_ip4_filter_cnt = 0;
3816 	pf->fd_tcp6_filter_cnt = 0;
3817 	pf->fd_udp6_filter_cnt = 0;
3818 	pf->fd_sctp6_filter_cnt = 0;
3819 	pf->fd_ip6_filter_cnt = 0;
3820 }
3821 
3822 /**
3823  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3824  * @vsi: Pointer to the targeted VSI
3825  *
3826  * This function replays the hlist on the hw where all the SB Flow Director
3827  * filters were saved.
3828  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3829 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3830 {
3831 	struct i40e_fdir_filter *filter;
3832 	struct i40e_pf *pf = vsi->back;
3833 	struct hlist_node *node;
3834 
3835 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3836 		return;
3837 
3838 	/* Reset FDir counters as we're replaying all existing filters */
3839 	i40e_reset_fdir_filter_cnt(pf);
3840 
3841 	hlist_for_each_entry_safe(filter, node,
3842 				  &pf->fdir_filter_list, fdir_node) {
3843 		i40e_add_del_fdir(vsi, filter, true);
3844 	}
3845 }
3846 
3847 /**
3848  * i40e_vsi_configure - Set up the VSI for action
3849  * @vsi: the VSI being configured
3850  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3851 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3852 {
3853 	int err;
3854 
3855 	i40e_set_vsi_rx_mode(vsi);
3856 	i40e_restore_vlan(vsi);
3857 	i40e_vsi_config_dcb_rings(vsi);
3858 	err = i40e_vsi_configure_tx(vsi);
3859 	if (!err)
3860 		err = i40e_vsi_configure_rx(vsi);
3861 
3862 	return err;
3863 }
3864 
3865 /**
3866  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3867  * @vsi: the VSI being configured
3868  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3869 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3870 {
3871 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3872 	struct i40e_pf *pf = vsi->back;
3873 	struct i40e_hw *hw = &pf->hw;
3874 	u16 vector;
3875 	int i, q;
3876 	u32 qp;
3877 
3878 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3879 	 * and PFINT_LNKLSTn registers, e.g.:
3880 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3881 	 */
3882 	qp = vsi->base_queue;
3883 	vector = vsi->base_vector;
3884 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3885 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3886 
3887 		q_vector->rx.next_update = jiffies + 1;
3888 		q_vector->rx.target_itr =
3889 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3890 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3891 		     q_vector->rx.target_itr >> 1);
3892 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3893 
3894 		q_vector->tx.next_update = jiffies + 1;
3895 		q_vector->tx.target_itr =
3896 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3897 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3898 		     q_vector->tx.target_itr >> 1);
3899 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3900 
3901 		/* Set ITR for software interrupts triggered after exiting
3902 		 * busy-loop polling.
3903 		 */
3904 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3905 		     I40E_ITR_20K);
3906 
3907 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3908 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3909 
3910 		/* begin of linked list for RX queue assigned to this vector */
3911 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3912 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3913 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3914 			u32 val;
3915 
3916 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3917 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3918 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3919 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3920 			      (I40E_QUEUE_TYPE_TX <<
3921 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3922 
3923 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3924 
3925 			if (has_xdp) {
3926 				/* TX queue with next queue set to TX */
3927 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3928 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3929 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3930 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3931 				      (I40E_QUEUE_TYPE_TX <<
3932 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3933 
3934 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3935 			}
3936 			/* TX queue with next RX or end of linked list */
3937 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3938 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3939 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3940 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3941 			      (I40E_QUEUE_TYPE_RX <<
3942 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3943 
3944 			/* Terminate the linked list */
3945 			if (q == (q_vector->num_ringpairs - 1))
3946 				val |= (I40E_QUEUE_END_OF_LIST <<
3947 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3948 
3949 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3950 			qp++;
3951 		}
3952 	}
3953 
3954 	i40e_flush(hw);
3955 }
3956 
3957 /**
3958  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3959  * @pf: pointer to private device data structure
3960  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3961 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3962 {
3963 	struct i40e_hw *hw = &pf->hw;
3964 	u32 val;
3965 
3966 	/* clear things first */
3967 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3968 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3969 
3970 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3971 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3972 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3973 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3974 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3975 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3976 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3977 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3978 
3979 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3980 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3981 
3982 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3983 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3984 
3985 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3986 
3987 	/* SW_ITR_IDX = 0, but don't change INTENA */
3988 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3989 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3990 
3991 	/* OTHER_ITR_IDX = 0 */
3992 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3993 }
3994 
3995 /**
3996  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3997  * @vsi: the VSI being configured
3998  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3999 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4000 {
4001 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4002 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4003 	struct i40e_pf *pf = vsi->back;
4004 	struct i40e_hw *hw = &pf->hw;
4005 
4006 	/* set the ITR configuration */
4007 	q_vector->rx.next_update = jiffies + 1;
4008 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4009 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4010 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4011 	q_vector->tx.next_update = jiffies + 1;
4012 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4013 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4014 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4015 
4016 	i40e_enable_misc_int_causes(pf);
4017 
4018 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4019 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4020 
4021 	/* Associate the queue pair to the vector and enable the queue
4022 	 * interrupt RX queue in linked list with next queue set to TX
4023 	 */
4024 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4025 
4026 	if (i40e_enabled_xdp_vsi(vsi)) {
4027 		/* TX queue in linked list with next queue set to TX */
4028 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4029 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4030 	}
4031 
4032 	/* last TX queue so the next RX queue doesn't matter */
4033 	wr32(hw, I40E_QINT_TQCTL(0),
4034 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4035 	i40e_flush(hw);
4036 }
4037 
4038 /**
4039  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4040  * @pf: board private structure
4041  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4042 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4043 {
4044 	struct i40e_hw *hw = &pf->hw;
4045 
4046 	wr32(hw, I40E_PFINT_DYN_CTL0,
4047 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4048 	i40e_flush(hw);
4049 }
4050 
4051 /**
4052  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4053  * @pf: board private structure
4054  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4055 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4056 {
4057 	struct i40e_hw *hw = &pf->hw;
4058 	u32 val;
4059 
4060 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4061 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4062 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4063 
4064 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4065 	i40e_flush(hw);
4066 }
4067 
4068 /**
4069  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4070  * @irq: interrupt number
4071  * @data: pointer to a q_vector
4072  **/
i40e_msix_clean_rings(int irq,void * data)4073 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4074 {
4075 	struct i40e_q_vector *q_vector = data;
4076 
4077 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4078 		return IRQ_HANDLED;
4079 
4080 	napi_schedule_irqoff(&q_vector->napi);
4081 
4082 	return IRQ_HANDLED;
4083 }
4084 
4085 /**
4086  * i40e_irq_affinity_notify - Callback for affinity changes
4087  * @notify: context as to what irq was changed
4088  * @mask: the new affinity mask
4089  *
4090  * This is a callback function used by the irq_set_affinity_notifier function
4091  * so that we may register to receive changes to the irq affinity masks.
4092  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4093 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4094 				     const cpumask_t *mask)
4095 {
4096 	struct i40e_q_vector *q_vector =
4097 		container_of(notify, struct i40e_q_vector, affinity_notify);
4098 
4099 	cpumask_copy(&q_vector->affinity_mask, mask);
4100 }
4101 
4102 /**
4103  * i40e_irq_affinity_release - Callback for affinity notifier release
4104  * @ref: internal core kernel usage
4105  *
4106  * This is a callback function used by the irq_set_affinity_notifier function
4107  * to inform the current notification subscriber that they will no longer
4108  * receive notifications.
4109  **/
i40e_irq_affinity_release(struct kref * ref)4110 static void i40e_irq_affinity_release(struct kref *ref) {}
4111 
4112 /**
4113  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4114  * @vsi: the VSI being configured
4115  * @basename: name for the vector
4116  *
4117  * Allocates MSI-X vectors and requests interrupts from the kernel.
4118  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4119 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4120 {
4121 	int q_vectors = vsi->num_q_vectors;
4122 	struct i40e_pf *pf = vsi->back;
4123 	int base = vsi->base_vector;
4124 	int rx_int_idx = 0;
4125 	int tx_int_idx = 0;
4126 	int vector, err;
4127 	int irq_num;
4128 	int cpu;
4129 
4130 	for (vector = 0; vector < q_vectors; vector++) {
4131 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4132 
4133 		irq_num = pf->msix_entries[base + vector].vector;
4134 
4135 		if (q_vector->tx.ring && q_vector->rx.ring) {
4136 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4137 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4138 			tx_int_idx++;
4139 		} else if (q_vector->rx.ring) {
4140 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4141 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4142 		} else if (q_vector->tx.ring) {
4143 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4144 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4145 		} else {
4146 			/* skip this unused q_vector */
4147 			continue;
4148 		}
4149 		err = request_irq(irq_num,
4150 				  vsi->irq_handler,
4151 				  0,
4152 				  q_vector->name,
4153 				  q_vector);
4154 		if (err) {
4155 			dev_info(&pf->pdev->dev,
4156 				 "MSIX request_irq failed, error: %d\n", err);
4157 			goto free_queue_irqs;
4158 		}
4159 
4160 		/* register for affinity change notifications */
4161 		q_vector->irq_num = irq_num;
4162 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4163 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4164 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4165 		/* Spread affinity hints out across online CPUs.
4166 		 *
4167 		 * get_cpu_mask returns a static constant mask with
4168 		 * a permanent lifetime so it's ok to pass to
4169 		 * irq_update_affinity_hint without making a copy.
4170 		 */
4171 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4172 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4173 	}
4174 
4175 	vsi->irqs_ready = true;
4176 	return 0;
4177 
4178 free_queue_irqs:
4179 	while (vector) {
4180 		vector--;
4181 		irq_num = pf->msix_entries[base + vector].vector;
4182 		irq_set_affinity_notifier(irq_num, NULL);
4183 		irq_update_affinity_hint(irq_num, NULL);
4184 		free_irq(irq_num, vsi->q_vectors[vector]);
4185 	}
4186 	return err;
4187 }
4188 
4189 /**
4190  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4191  * @vsi: the VSI being un-configured
4192  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4193 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4194 {
4195 	struct i40e_pf *pf = vsi->back;
4196 	struct i40e_hw *hw = &pf->hw;
4197 	int base = vsi->base_vector;
4198 	int i;
4199 
4200 	/* disable interrupt causation from each queue */
4201 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4202 		u32 val;
4203 
4204 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4205 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4206 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4207 
4208 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4209 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4210 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4211 
4212 		if (!i40e_enabled_xdp_vsi(vsi))
4213 			continue;
4214 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4215 	}
4216 
4217 	/* disable each interrupt */
4218 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4219 		for (i = vsi->base_vector;
4220 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4221 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4222 
4223 		i40e_flush(hw);
4224 		for (i = 0; i < vsi->num_q_vectors; i++)
4225 			synchronize_irq(pf->msix_entries[i + base].vector);
4226 	} else {
4227 		/* Legacy and MSI mode - this stops all interrupt handling */
4228 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4229 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4230 		i40e_flush(hw);
4231 		synchronize_irq(pf->pdev->irq);
4232 	}
4233 }
4234 
4235 /**
4236  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4237  * @vsi: the VSI being configured
4238  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4239 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4240 {
4241 	struct i40e_pf *pf = vsi->back;
4242 	int i;
4243 
4244 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4245 		for (i = 0; i < vsi->num_q_vectors; i++)
4246 			i40e_irq_dynamic_enable(vsi, i);
4247 	} else {
4248 		i40e_irq_dynamic_enable_icr0(pf);
4249 	}
4250 
4251 	i40e_flush(&pf->hw);
4252 	return 0;
4253 }
4254 
4255 /**
4256  * i40e_free_misc_vector - Free the vector that handles non-queue events
4257  * @pf: board private structure
4258  **/
i40e_free_misc_vector(struct i40e_pf * pf)4259 static void i40e_free_misc_vector(struct i40e_pf *pf)
4260 {
4261 	/* Disable ICR 0 */
4262 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4263 	i40e_flush(&pf->hw);
4264 
4265 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4266 		free_irq(pf->msix_entries[0].vector, pf);
4267 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4268 	}
4269 }
4270 
4271 /**
4272  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4273  * @irq: interrupt number
4274  * @data: pointer to a q_vector
4275  *
4276  * This is the handler used for all MSI/Legacy interrupts, and deals
4277  * with both queue and non-queue interrupts.  This is also used in
4278  * MSIX mode to handle the non-queue interrupts.
4279  **/
i40e_intr(int irq,void * data)4280 static irqreturn_t i40e_intr(int irq, void *data)
4281 {
4282 	struct i40e_pf *pf = (struct i40e_pf *)data;
4283 	struct i40e_hw *hw = &pf->hw;
4284 	irqreturn_t ret = IRQ_NONE;
4285 	u32 icr0, icr0_remaining;
4286 	u32 val, ena_mask;
4287 
4288 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4289 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4290 
4291 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4292 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4293 		goto enable_intr;
4294 
4295 	/* if interrupt but no bits showing, must be SWINT */
4296 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4297 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4298 		pf->sw_int_count++;
4299 
4300 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4301 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4302 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4303 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4304 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4305 	}
4306 
4307 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4308 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4309 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4310 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4311 
4312 		/* We do not have a way to disarm Queue causes while leaving
4313 		 * interrupt enabled for all other causes, ideally
4314 		 * interrupt should be disabled while we are in NAPI but
4315 		 * this is not a performance path and napi_schedule()
4316 		 * can deal with rescheduling.
4317 		 */
4318 		if (!test_bit(__I40E_DOWN, pf->state))
4319 			napi_schedule_irqoff(&q_vector->napi);
4320 	}
4321 
4322 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4323 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4324 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4325 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4326 	}
4327 
4328 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4329 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4330 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4331 	}
4332 
4333 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4334 		/* disable any further VFLR event notifications */
4335 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4336 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4337 
4338 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4339 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4340 		} else {
4341 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4342 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4343 		}
4344 	}
4345 
4346 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4347 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4348 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4349 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4350 		val = rd32(hw, I40E_GLGEN_RSTAT);
4351 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4352 		if (val == I40E_RESET_CORER) {
4353 			pf->corer_count++;
4354 		} else if (val == I40E_RESET_GLOBR) {
4355 			pf->globr_count++;
4356 		} else if (val == I40E_RESET_EMPR) {
4357 			pf->empr_count++;
4358 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4359 		}
4360 	}
4361 
4362 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4363 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4364 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4365 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4366 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4367 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4368 	}
4369 
4370 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4371 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4372 
4373 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4374 			schedule_work(&pf->ptp_extts0_work);
4375 
4376 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4377 			i40e_ptp_tx_hwtstamp(pf);
4378 
4379 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4380 	}
4381 
4382 	/* If a critical error is pending we have no choice but to reset the
4383 	 * device.
4384 	 * Report and mask out any remaining unexpected interrupts.
4385 	 */
4386 	icr0_remaining = icr0 & ena_mask;
4387 	if (icr0_remaining) {
4388 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4389 			 icr0_remaining);
4390 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4391 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4392 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4393 			dev_info(&pf->pdev->dev, "device will be reset\n");
4394 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4395 			i40e_service_event_schedule(pf);
4396 		}
4397 		ena_mask &= ~icr0_remaining;
4398 	}
4399 	ret = IRQ_HANDLED;
4400 
4401 enable_intr:
4402 	/* re-enable interrupt causes */
4403 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4404 	if (!test_bit(__I40E_DOWN, pf->state) ||
4405 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4406 		i40e_service_event_schedule(pf);
4407 		i40e_irq_dynamic_enable_icr0(pf);
4408 	}
4409 
4410 	return ret;
4411 }
4412 
4413 /**
4414  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4415  * @tx_ring:  tx ring to clean
4416  * @budget:   how many cleans we're allowed
4417  *
4418  * Returns true if there's any budget left (e.g. the clean is finished)
4419  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4420 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4421 {
4422 	struct i40e_vsi *vsi = tx_ring->vsi;
4423 	u16 i = tx_ring->next_to_clean;
4424 	struct i40e_tx_buffer *tx_buf;
4425 	struct i40e_tx_desc *tx_desc;
4426 
4427 	tx_buf = &tx_ring->tx_bi[i];
4428 	tx_desc = I40E_TX_DESC(tx_ring, i);
4429 	i -= tx_ring->count;
4430 
4431 	do {
4432 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4433 
4434 		/* if next_to_watch is not set then there is no work pending */
4435 		if (!eop_desc)
4436 			break;
4437 
4438 		/* prevent any other reads prior to eop_desc */
4439 		smp_rmb();
4440 
4441 		/* if the descriptor isn't done, no work yet to do */
4442 		if (!(eop_desc->cmd_type_offset_bsz &
4443 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4444 			break;
4445 
4446 		/* clear next_to_watch to prevent false hangs */
4447 		tx_buf->next_to_watch = NULL;
4448 
4449 		tx_desc->buffer_addr = 0;
4450 		tx_desc->cmd_type_offset_bsz = 0;
4451 		/* move past filter desc */
4452 		tx_buf++;
4453 		tx_desc++;
4454 		i++;
4455 		if (unlikely(!i)) {
4456 			i -= tx_ring->count;
4457 			tx_buf = tx_ring->tx_bi;
4458 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4459 		}
4460 		/* unmap skb header data */
4461 		dma_unmap_single(tx_ring->dev,
4462 				 dma_unmap_addr(tx_buf, dma),
4463 				 dma_unmap_len(tx_buf, len),
4464 				 DMA_TO_DEVICE);
4465 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4466 			kfree(tx_buf->raw_buf);
4467 
4468 		tx_buf->raw_buf = NULL;
4469 		tx_buf->tx_flags = 0;
4470 		tx_buf->next_to_watch = NULL;
4471 		dma_unmap_len_set(tx_buf, len, 0);
4472 		tx_desc->buffer_addr = 0;
4473 		tx_desc->cmd_type_offset_bsz = 0;
4474 
4475 		/* move us past the eop_desc for start of next FD desc */
4476 		tx_buf++;
4477 		tx_desc++;
4478 		i++;
4479 		if (unlikely(!i)) {
4480 			i -= tx_ring->count;
4481 			tx_buf = tx_ring->tx_bi;
4482 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4483 		}
4484 
4485 		/* update budget accounting */
4486 		budget--;
4487 	} while (likely(budget));
4488 
4489 	i += tx_ring->count;
4490 	tx_ring->next_to_clean = i;
4491 
4492 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4493 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4494 
4495 	return budget > 0;
4496 }
4497 
4498 /**
4499  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4500  * @irq: interrupt number
4501  * @data: pointer to a q_vector
4502  **/
i40e_fdir_clean_ring(int irq,void * data)4503 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4504 {
4505 	struct i40e_q_vector *q_vector = data;
4506 	struct i40e_vsi *vsi;
4507 
4508 	if (!q_vector->tx.ring)
4509 		return IRQ_HANDLED;
4510 
4511 	vsi = q_vector->tx.ring->vsi;
4512 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4513 
4514 	return IRQ_HANDLED;
4515 }
4516 
4517 /**
4518  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4519  * @vsi: the VSI being configured
4520  * @v_idx: vector index
4521  * @qp_idx: queue pair index
4522  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4523 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4524 {
4525 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4526 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4527 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4528 
4529 	tx_ring->q_vector = q_vector;
4530 	tx_ring->next = q_vector->tx.ring;
4531 	q_vector->tx.ring = tx_ring;
4532 	q_vector->tx.count++;
4533 
4534 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4535 	if (i40e_enabled_xdp_vsi(vsi)) {
4536 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4537 
4538 		xdp_ring->q_vector = q_vector;
4539 		xdp_ring->next = q_vector->tx.ring;
4540 		q_vector->tx.ring = xdp_ring;
4541 		q_vector->tx.count++;
4542 	}
4543 
4544 	rx_ring->q_vector = q_vector;
4545 	rx_ring->next = q_vector->rx.ring;
4546 	q_vector->rx.ring = rx_ring;
4547 	q_vector->rx.count++;
4548 }
4549 
4550 /**
4551  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4552  * @vsi: the VSI being configured
4553  *
4554  * This function maps descriptor rings to the queue-specific vectors
4555  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4556  * one vector per queue pair, but on a constrained vector budget, we
4557  * group the queue pairs as "efficiently" as possible.
4558  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4559 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4560 {
4561 	int qp_remaining = vsi->num_queue_pairs;
4562 	int q_vectors = vsi->num_q_vectors;
4563 	int num_ringpairs;
4564 	int v_start = 0;
4565 	int qp_idx = 0;
4566 
4567 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4568 	 * group them so there are multiple queues per vector.
4569 	 * It is also important to go through all the vectors available to be
4570 	 * sure that if we don't use all the vectors, that the remaining vectors
4571 	 * are cleared. This is especially important when decreasing the
4572 	 * number of queues in use.
4573 	 */
4574 	for (; v_start < q_vectors; v_start++) {
4575 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4576 
4577 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4578 
4579 		q_vector->num_ringpairs = num_ringpairs;
4580 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4581 
4582 		q_vector->rx.count = 0;
4583 		q_vector->tx.count = 0;
4584 		q_vector->rx.ring = NULL;
4585 		q_vector->tx.ring = NULL;
4586 
4587 		while (num_ringpairs--) {
4588 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4589 			qp_idx++;
4590 			qp_remaining--;
4591 		}
4592 	}
4593 }
4594 
4595 /**
4596  * i40e_vsi_request_irq - Request IRQ from the OS
4597  * @vsi: the VSI being configured
4598  * @basename: name for the vector
4599  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4600 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4601 {
4602 	struct i40e_pf *pf = vsi->back;
4603 	int err;
4604 
4605 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4606 		err = i40e_vsi_request_irq_msix(vsi, basename);
4607 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4608 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4609 				  pf->int_name, pf);
4610 	else
4611 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4612 				  pf->int_name, pf);
4613 
4614 	if (err)
4615 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4616 
4617 	return err;
4618 }
4619 
4620 #ifdef CONFIG_NET_POLL_CONTROLLER
4621 /**
4622  * i40e_netpoll - A Polling 'interrupt' handler
4623  * @netdev: network interface device structure
4624  *
4625  * This is used by netconsole to send skbs without having to re-enable
4626  * interrupts.  It's not called while the normal interrupt routine is executing.
4627  **/
i40e_netpoll(struct net_device * netdev)4628 static void i40e_netpoll(struct net_device *netdev)
4629 {
4630 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4631 	struct i40e_vsi *vsi = np->vsi;
4632 	struct i40e_pf *pf = vsi->back;
4633 	int i;
4634 
4635 	/* if interface is down do nothing */
4636 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4637 		return;
4638 
4639 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4640 		for (i = 0; i < vsi->num_q_vectors; i++)
4641 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4642 	} else {
4643 		i40e_intr(pf->pdev->irq, netdev);
4644 	}
4645 }
4646 #endif
4647 
4648 #define I40E_QTX_ENA_WAIT_COUNT 50
4649 
4650 /**
4651  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4652  * @pf: the PF being configured
4653  * @pf_q: the PF queue
4654  * @enable: enable or disable state of the queue
4655  *
4656  * This routine will wait for the given Tx queue of the PF to reach the
4657  * enabled or disabled state.
4658  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4659  * multiple retries; else will return 0 in case of success.
4660  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4661 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4662 {
4663 	int i;
4664 	u32 tx_reg;
4665 
4666 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4667 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4668 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4669 			break;
4670 
4671 		usleep_range(10, 20);
4672 	}
4673 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4674 		return -ETIMEDOUT;
4675 
4676 	return 0;
4677 }
4678 
4679 /**
4680  * i40e_control_tx_q - Start or stop a particular Tx queue
4681  * @pf: the PF structure
4682  * @pf_q: the PF queue to configure
4683  * @enable: start or stop the queue
4684  *
4685  * This function enables or disables a single queue. Note that any delay
4686  * required after the operation is expected to be handled by the caller of
4687  * this function.
4688  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4689 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4690 {
4691 	struct i40e_hw *hw = &pf->hw;
4692 	u32 tx_reg;
4693 	int i;
4694 
4695 	/* warn the TX unit of coming changes */
4696 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4697 	if (!enable)
4698 		usleep_range(10, 20);
4699 
4700 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4701 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4702 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4703 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4704 			break;
4705 		usleep_range(1000, 2000);
4706 	}
4707 
4708 	/* Skip if the queue is already in the requested state */
4709 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4710 		return;
4711 
4712 	/* turn on/off the queue */
4713 	if (enable) {
4714 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4715 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4716 	} else {
4717 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4718 	}
4719 
4720 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4721 }
4722 
4723 /**
4724  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4725  * @seid: VSI SEID
4726  * @pf: the PF structure
4727  * @pf_q: the PF queue to configure
4728  * @is_xdp: true if the queue is used for XDP
4729  * @enable: start or stop the queue
4730  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4731 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4732 			   bool is_xdp, bool enable)
4733 {
4734 	int ret;
4735 
4736 	i40e_control_tx_q(pf, pf_q, enable);
4737 
4738 	/* wait for the change to finish */
4739 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4740 	if (ret) {
4741 		dev_info(&pf->pdev->dev,
4742 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4743 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4744 			 (enable ? "en" : "dis"));
4745 	}
4746 
4747 	return ret;
4748 }
4749 
4750 /**
4751  * i40e_vsi_enable_tx - Start a VSI's rings
4752  * @vsi: the VSI being configured
4753  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4754 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4755 {
4756 	struct i40e_pf *pf = vsi->back;
4757 	int i, pf_q, ret = 0;
4758 
4759 	pf_q = vsi->base_queue;
4760 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4761 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4762 					     pf_q,
4763 					     false /*is xdp*/, true);
4764 		if (ret)
4765 			break;
4766 
4767 		if (!i40e_enabled_xdp_vsi(vsi))
4768 			continue;
4769 
4770 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4771 					     pf_q + vsi->alloc_queue_pairs,
4772 					     true /*is xdp*/, true);
4773 		if (ret)
4774 			break;
4775 	}
4776 	return ret;
4777 }
4778 
4779 /**
4780  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4781  * @pf: the PF being configured
4782  * @pf_q: the PF queue
4783  * @enable: enable or disable state of the queue
4784  *
4785  * This routine will wait for the given Rx queue of the PF to reach the
4786  * enabled or disabled state.
4787  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4788  * multiple retries; else will return 0 in case of success.
4789  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4790 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4791 {
4792 	int i;
4793 	u32 rx_reg;
4794 
4795 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4796 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4797 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4798 			break;
4799 
4800 		usleep_range(10, 20);
4801 	}
4802 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4803 		return -ETIMEDOUT;
4804 
4805 	return 0;
4806 }
4807 
4808 /**
4809  * i40e_control_rx_q - Start or stop a particular Rx queue
4810  * @pf: the PF structure
4811  * @pf_q: the PF queue to configure
4812  * @enable: start or stop the queue
4813  *
4814  * This function enables or disables a single queue. Note that
4815  * any delay required after the operation is expected to be
4816  * handled by the caller of this function.
4817  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4818 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4819 {
4820 	struct i40e_hw *hw = &pf->hw;
4821 	u32 rx_reg;
4822 	int i;
4823 
4824 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4825 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4826 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4827 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4828 			break;
4829 		usleep_range(1000, 2000);
4830 	}
4831 
4832 	/* Skip if the queue is already in the requested state */
4833 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4834 		return;
4835 
4836 	/* turn on/off the queue */
4837 	if (enable)
4838 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4839 	else
4840 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4841 
4842 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4843 }
4844 
4845 /**
4846  * i40e_control_wait_rx_q
4847  * @pf: the PF structure
4848  * @pf_q: queue being configured
4849  * @enable: start or stop the rings
4850  *
4851  * This function enables or disables a single queue along with waiting
4852  * for the change to finish. The caller of this function should handle
4853  * the delays needed in the case of disabling queues.
4854  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4855 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4856 {
4857 	int ret = 0;
4858 
4859 	i40e_control_rx_q(pf, pf_q, enable);
4860 
4861 	/* wait for the change to finish */
4862 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4863 	if (ret)
4864 		return ret;
4865 
4866 	return ret;
4867 }
4868 
4869 /**
4870  * i40e_vsi_enable_rx - Start a VSI's rings
4871  * @vsi: the VSI being configured
4872  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4873 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4874 {
4875 	struct i40e_pf *pf = vsi->back;
4876 	int i, pf_q, ret = 0;
4877 
4878 	pf_q = vsi->base_queue;
4879 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4880 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4881 		if (ret) {
4882 			dev_info(&pf->pdev->dev,
4883 				 "VSI seid %d Rx ring %d enable timeout\n",
4884 				 vsi->seid, pf_q);
4885 			break;
4886 		}
4887 	}
4888 
4889 	return ret;
4890 }
4891 
4892 /**
4893  * i40e_vsi_start_rings - Start a VSI's rings
4894  * @vsi: the VSI being configured
4895  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4896 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4897 {
4898 	int ret = 0;
4899 
4900 	/* do rx first for enable and last for disable */
4901 	ret = i40e_vsi_enable_rx(vsi);
4902 	if (ret)
4903 		return ret;
4904 	ret = i40e_vsi_enable_tx(vsi);
4905 
4906 	return ret;
4907 }
4908 
4909 #define I40E_DISABLE_TX_GAP_MSEC	50
4910 
4911 /**
4912  * i40e_vsi_stop_rings - Stop a VSI's rings
4913  * @vsi: the VSI being configured
4914  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4915 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4916 {
4917 	struct i40e_pf *pf = vsi->back;
4918 	u32 pf_q, tx_q_end, rx_q_end;
4919 
4920 	/* When port TX is suspended, don't wait */
4921 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4922 		return i40e_vsi_stop_rings_no_wait(vsi);
4923 
4924 	tx_q_end = vsi->base_queue +
4925 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4926 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4927 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4928 
4929 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4930 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4931 		i40e_control_rx_q(pf, pf_q, false);
4932 
4933 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4934 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4935 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4936 
4937 	i40e_vsi_wait_queues_disabled(vsi);
4938 }
4939 
4940 /**
4941  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4942  * @vsi: the VSI being shutdown
4943  *
4944  * This function stops all the rings for a VSI but does not delay to verify
4945  * that rings have been disabled. It is expected that the caller is shutting
4946  * down multiple VSIs at once and will delay together for all the VSIs after
4947  * initiating the shutdown. This is particularly useful for shutting down lots
4948  * of VFs together. Otherwise, a large delay can be incurred while configuring
4949  * each VSI in serial.
4950  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4951 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4952 {
4953 	struct i40e_pf *pf = vsi->back;
4954 	int i, pf_q;
4955 
4956 	pf_q = vsi->base_queue;
4957 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4958 		i40e_control_tx_q(pf, pf_q, false);
4959 		i40e_control_rx_q(pf, pf_q, false);
4960 	}
4961 }
4962 
4963 /**
4964  * i40e_vsi_free_irq - Free the irq association with the OS
4965  * @vsi: the VSI being configured
4966  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4967 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4968 {
4969 	struct i40e_pf *pf = vsi->back;
4970 	struct i40e_hw *hw = &pf->hw;
4971 	int base = vsi->base_vector;
4972 	u32 val, qp;
4973 	int i;
4974 
4975 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4976 		if (!vsi->q_vectors)
4977 			return;
4978 
4979 		if (!vsi->irqs_ready)
4980 			return;
4981 
4982 		vsi->irqs_ready = false;
4983 		for (i = 0; i < vsi->num_q_vectors; i++) {
4984 			int irq_num;
4985 			u16 vector;
4986 
4987 			vector = i + base;
4988 			irq_num = pf->msix_entries[vector].vector;
4989 
4990 			/* free only the irqs that were actually requested */
4991 			if (!vsi->q_vectors[i] ||
4992 			    !vsi->q_vectors[i]->num_ringpairs)
4993 				continue;
4994 
4995 			/* clear the affinity notifier in the IRQ descriptor */
4996 			irq_set_affinity_notifier(irq_num, NULL);
4997 			/* remove our suggested affinity mask for this IRQ */
4998 			irq_update_affinity_hint(irq_num, NULL);
4999 			free_irq(irq_num, vsi->q_vectors[i]);
5000 
5001 			/* Tear down the interrupt queue link list
5002 			 *
5003 			 * We know that they come in pairs and always
5004 			 * the Rx first, then the Tx.  To clear the
5005 			 * link list, stick the EOL value into the
5006 			 * next_q field of the registers.
5007 			 */
5008 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5009 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5010 				       val);
5011 			val |= I40E_QUEUE_END_OF_LIST
5012 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5013 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5014 
5015 			while (qp != I40E_QUEUE_END_OF_LIST) {
5016 				u32 next;
5017 
5018 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5019 
5020 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5021 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5022 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5023 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5024 
5025 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5026 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5027 
5028 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5029 
5030 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5031 
5032 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5033 						 val);
5034 
5035 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5036 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5037 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5038 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5039 
5040 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5041 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5042 
5043 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5044 				qp = next;
5045 			}
5046 		}
5047 	} else {
5048 		free_irq(pf->pdev->irq, pf);
5049 
5050 		val = rd32(hw, I40E_PFINT_LNKLST0);
5051 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5052 		val |= I40E_QUEUE_END_OF_LIST
5053 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5054 		wr32(hw, I40E_PFINT_LNKLST0, val);
5055 
5056 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5057 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5058 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5059 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5060 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5061 
5062 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5063 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5064 
5065 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5066 
5067 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5068 
5069 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5070 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5071 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5072 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5073 
5074 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5075 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5076 
5077 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5078 	}
5079 }
5080 
5081 /**
5082  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5083  * @vsi: the VSI being configured
5084  * @v_idx: Index of vector to be freed
5085  *
5086  * This function frees the memory allocated to the q_vector.  In addition if
5087  * NAPI is enabled it will delete any references to the NAPI struct prior
5088  * to freeing the q_vector.
5089  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5090 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5091 {
5092 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5093 	struct i40e_ring *ring;
5094 
5095 	if (!q_vector)
5096 		return;
5097 
5098 	/* disassociate q_vector from rings */
5099 	i40e_for_each_ring(ring, q_vector->tx)
5100 		ring->q_vector = NULL;
5101 
5102 	i40e_for_each_ring(ring, q_vector->rx)
5103 		ring->q_vector = NULL;
5104 
5105 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5106 	if (vsi->netdev)
5107 		netif_napi_del(&q_vector->napi);
5108 
5109 	vsi->q_vectors[v_idx] = NULL;
5110 
5111 	kfree_rcu(q_vector, rcu);
5112 }
5113 
5114 /**
5115  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5116  * @vsi: the VSI being un-configured
5117  *
5118  * This frees the memory allocated to the q_vectors and
5119  * deletes references to the NAPI struct.
5120  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5121 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5122 {
5123 	int v_idx;
5124 
5125 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5126 		i40e_free_q_vector(vsi, v_idx);
5127 }
5128 
5129 /**
5130  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5131  * @pf: board private structure
5132  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5133 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5134 {
5135 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5136 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5137 		pci_disable_msix(pf->pdev);
5138 		kfree(pf->msix_entries);
5139 		pf->msix_entries = NULL;
5140 		kfree(pf->irq_pile);
5141 		pf->irq_pile = NULL;
5142 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5143 		pci_disable_msi(pf->pdev);
5144 	}
5145 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5146 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5147 }
5148 
5149 /**
5150  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5151  * @pf: board private structure
5152  *
5153  * We go through and clear interrupt specific resources and reset the structure
5154  * to pre-load conditions
5155  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5156 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5157 {
5158 	struct i40e_vsi *vsi;
5159 	int i;
5160 
5161 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5162 		i40e_free_misc_vector(pf);
5163 
5164 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5165 		      I40E_IWARP_IRQ_PILE_ID);
5166 
5167 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5168 
5169 	i40e_pf_for_each_vsi(pf, i, vsi)
5170 		i40e_vsi_free_q_vectors(vsi);
5171 
5172 	i40e_reset_interrupt_capability(pf);
5173 }
5174 
5175 /**
5176  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5177  * @vsi: the VSI being configured
5178  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5179 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5180 {
5181 	int q_idx;
5182 
5183 	if (!vsi->netdev)
5184 		return;
5185 
5186 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5187 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5188 
5189 		if (q_vector->rx.ring || q_vector->tx.ring)
5190 			napi_enable(&q_vector->napi);
5191 	}
5192 }
5193 
5194 /**
5195  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5196  * @vsi: the VSI being configured
5197  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5198 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5199 {
5200 	int q_idx;
5201 
5202 	if (!vsi->netdev)
5203 		return;
5204 
5205 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5206 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5207 
5208 		if (q_vector->rx.ring || q_vector->tx.ring)
5209 			napi_disable(&q_vector->napi);
5210 	}
5211 }
5212 
5213 /**
5214  * i40e_vsi_close - Shut down a VSI
5215  * @vsi: the vsi to be quelled
5216  **/
i40e_vsi_close(struct i40e_vsi * vsi)5217 static void i40e_vsi_close(struct i40e_vsi *vsi)
5218 {
5219 	struct i40e_pf *pf = vsi->back;
5220 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5221 		i40e_down(vsi);
5222 	i40e_vsi_free_irq(vsi);
5223 	i40e_vsi_free_tx_resources(vsi);
5224 	i40e_vsi_free_rx_resources(vsi);
5225 	vsi->current_netdev_flags = 0;
5226 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5227 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5228 		set_bit(__I40E_CLIENT_RESET, pf->state);
5229 }
5230 
5231 /**
5232  * i40e_quiesce_vsi - Pause a given VSI
5233  * @vsi: the VSI being paused
5234  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5235 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5236 {
5237 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5238 		return;
5239 
5240 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5241 	if (vsi->netdev && netif_running(vsi->netdev))
5242 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5243 	else
5244 		i40e_vsi_close(vsi);
5245 }
5246 
5247 /**
5248  * i40e_unquiesce_vsi - Resume a given VSI
5249  * @vsi: the VSI being resumed
5250  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5251 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5252 {
5253 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5254 		return;
5255 
5256 	if (vsi->netdev && netif_running(vsi->netdev))
5257 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5258 	else
5259 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5260 }
5261 
5262 /**
5263  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5264  * @pf: the PF
5265  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5266 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5267 {
5268 	struct i40e_vsi *vsi;
5269 	int v;
5270 
5271 	i40e_pf_for_each_vsi(pf, v, vsi)
5272 		i40e_quiesce_vsi(vsi);
5273 }
5274 
5275 /**
5276  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5277  * @pf: the PF
5278  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5279 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5280 {
5281 	struct i40e_vsi *vsi;
5282 	int v;
5283 
5284 	i40e_pf_for_each_vsi(pf, v, vsi)
5285 		i40e_unquiesce_vsi(vsi);
5286 }
5287 
5288 /**
5289  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5290  * @vsi: the VSI being configured
5291  *
5292  * Wait until all queues on a given VSI have been disabled.
5293  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5294 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5295 {
5296 	struct i40e_pf *pf = vsi->back;
5297 	int i, pf_q, ret;
5298 
5299 	pf_q = vsi->base_queue;
5300 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5301 		/* Check and wait for the Tx queue */
5302 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5303 		if (ret) {
5304 			dev_info(&pf->pdev->dev,
5305 				 "VSI seid %d Tx ring %d disable timeout\n",
5306 				 vsi->seid, pf_q);
5307 			return ret;
5308 		}
5309 
5310 		if (!i40e_enabled_xdp_vsi(vsi))
5311 			goto wait_rx;
5312 
5313 		/* Check and wait for the XDP Tx queue */
5314 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5315 				       false);
5316 		if (ret) {
5317 			dev_info(&pf->pdev->dev,
5318 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5319 				 vsi->seid, pf_q);
5320 			return ret;
5321 		}
5322 wait_rx:
5323 		/* Check and wait for the Rx queue */
5324 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5325 		if (ret) {
5326 			dev_info(&pf->pdev->dev,
5327 				 "VSI seid %d Rx ring %d disable timeout\n",
5328 				 vsi->seid, pf_q);
5329 			return ret;
5330 		}
5331 	}
5332 
5333 	return 0;
5334 }
5335 
5336 #ifdef CONFIG_I40E_DCB
5337 /**
5338  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5339  * @pf: the PF
5340  *
5341  * This function waits for the queues to be in disabled state for all the
5342  * VSIs that are managed by this PF.
5343  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5344 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5345 {
5346 	struct i40e_vsi *vsi;
5347 	int v, ret = 0;
5348 
5349 	i40e_pf_for_each_vsi(pf, v, vsi) {
5350 		ret = i40e_vsi_wait_queues_disabled(vsi);
5351 		if (ret)
5352 			break;
5353 	}
5354 
5355 	return ret;
5356 }
5357 
5358 #endif
5359 
5360 /**
5361  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5362  * @pf: pointer to PF
5363  *
5364  * Get TC map for ISCSI PF type that will include iSCSI TC
5365  * and LAN TC.
5366  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5367 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5368 {
5369 	struct i40e_dcb_app_priority_table app;
5370 	struct i40e_hw *hw = &pf->hw;
5371 	u8 enabled_tc = 1; /* TC0 is always enabled */
5372 	u8 tc, i;
5373 	/* Get the iSCSI APP TLV */
5374 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5375 
5376 	for (i = 0; i < dcbcfg->numapps; i++) {
5377 		app = dcbcfg->app[i];
5378 		if (app.selector == I40E_APP_SEL_TCPIP &&
5379 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5380 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5381 			enabled_tc |= BIT(tc);
5382 			break;
5383 		}
5384 	}
5385 
5386 	return enabled_tc;
5387 }
5388 
5389 /**
5390  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5391  * @dcbcfg: the corresponding DCBx configuration structure
5392  *
5393  * Return the number of TCs from given DCBx configuration
5394  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5395 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5396 {
5397 	int i, tc_unused = 0;
5398 	u8 num_tc = 0;
5399 	u8 ret = 0;
5400 
5401 	/* Scan the ETS Config Priority Table to find
5402 	 * traffic class enabled for a given priority
5403 	 * and create a bitmask of enabled TCs
5404 	 */
5405 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5406 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5407 
5408 	/* Now scan the bitmask to check for
5409 	 * contiguous TCs starting with TC0
5410 	 */
5411 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5412 		if (num_tc & BIT(i)) {
5413 			if (!tc_unused) {
5414 				ret++;
5415 			} else {
5416 				pr_err("Non-contiguous TC - Disabling DCB\n");
5417 				return 1;
5418 			}
5419 		} else {
5420 			tc_unused = 1;
5421 		}
5422 	}
5423 
5424 	/* There is always at least TC0 */
5425 	if (!ret)
5426 		ret = 1;
5427 
5428 	return ret;
5429 }
5430 
5431 /**
5432  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5433  * @dcbcfg: the corresponding DCBx configuration structure
5434  *
5435  * Query the current DCB configuration and return the number of
5436  * traffic classes enabled from the given DCBX config
5437  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5438 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5439 {
5440 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5441 	u8 enabled_tc = 1;
5442 	u8 i;
5443 
5444 	for (i = 0; i < num_tc; i++)
5445 		enabled_tc |= BIT(i);
5446 
5447 	return enabled_tc;
5448 }
5449 
5450 /**
5451  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5452  * @pf: PF being queried
5453  *
5454  * Query the current MQPRIO configuration and return the number of
5455  * traffic classes enabled.
5456  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5457 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5458 {
5459 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5460 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5461 	u8 enabled_tc = 1, i;
5462 
5463 	for (i = 1; i < num_tc; i++)
5464 		enabled_tc |= BIT(i);
5465 	return enabled_tc;
5466 }
5467 
5468 /**
5469  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5470  * @pf: PF being queried
5471  *
5472  * Return number of traffic classes enabled for the given PF
5473  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5474 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5475 {
5476 	u8 i, enabled_tc = 1;
5477 	u8 num_tc = 0;
5478 
5479 	if (i40e_is_tc_mqprio_enabled(pf)) {
5480 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5481 
5482 		return vsi->mqprio_qopt.qopt.num_tc;
5483 	}
5484 
5485 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5486 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5487 		return 1;
5488 
5489 	/* SFP mode will be enabled for all TCs on port */
5490 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5491 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5492 
5493 	/* MFP mode return count of enabled TCs for this PF */
5494 	if (pf->hw.func_caps.iscsi)
5495 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5496 	else
5497 		return 1; /* Only TC0 */
5498 
5499 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5500 		if (enabled_tc & BIT(i))
5501 			num_tc++;
5502 	}
5503 	return num_tc;
5504 }
5505 
5506 /**
5507  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5508  * @pf: PF being queried
5509  *
5510  * Return a bitmap for enabled traffic classes for this PF.
5511  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5512 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5513 {
5514 	if (i40e_is_tc_mqprio_enabled(pf))
5515 		return i40e_mqprio_get_enabled_tc(pf);
5516 
5517 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5518 	 * default TC
5519 	 */
5520 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5521 		return I40E_DEFAULT_TRAFFIC_CLASS;
5522 
5523 	/* SFP mode we want PF to be enabled for all TCs */
5524 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5525 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5526 
5527 	/* MFP enabled and iSCSI PF type */
5528 	if (pf->hw.func_caps.iscsi)
5529 		return i40e_get_iscsi_tc_map(pf);
5530 	else
5531 		return I40E_DEFAULT_TRAFFIC_CLASS;
5532 }
5533 
5534 /**
5535  * i40e_vsi_get_bw_info - Query VSI BW Information
5536  * @vsi: the VSI being queried
5537  *
5538  * Returns 0 on success, negative value on failure
5539  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5540 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5541 {
5542 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5543 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5544 	struct i40e_pf *pf = vsi->back;
5545 	struct i40e_hw *hw = &pf->hw;
5546 	u32 tc_bw_max;
5547 	int ret;
5548 	int i;
5549 
5550 	/* Get the VSI level BW configuration */
5551 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5552 	if (ret) {
5553 		dev_info(&pf->pdev->dev,
5554 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5555 			 ERR_PTR(ret),
5556 			 libie_aq_str(pf->hw.aq.asq_last_status));
5557 		return -EINVAL;
5558 	}
5559 
5560 	/* Get the VSI level BW configuration per TC */
5561 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5562 					       NULL);
5563 	if (ret) {
5564 		dev_info(&pf->pdev->dev,
5565 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5566 			 ERR_PTR(ret),
5567 			 libie_aq_str(pf->hw.aq.asq_last_status));
5568 		return -EINVAL;
5569 	}
5570 
5571 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5572 		dev_info(&pf->pdev->dev,
5573 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5574 			 bw_config.tc_valid_bits,
5575 			 bw_ets_config.tc_valid_bits);
5576 		/* Still continuing */
5577 	}
5578 
5579 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5580 	vsi->bw_max_quanta = bw_config.max_bw;
5581 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5582 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5583 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5584 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5585 		vsi->bw_ets_limit_credits[i] =
5586 					le16_to_cpu(bw_ets_config.credits[i]);
5587 		/* 3 bits out of 4 for each TC */
5588 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5589 	}
5590 
5591 	return 0;
5592 }
5593 
5594 /**
5595  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5596  * @vsi: the VSI being configured
5597  * @enabled_tc: TC bitmap
5598  * @bw_share: BW shared credits per TC
5599  *
5600  * Returns 0 on success, negative value on failure
5601  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5602 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5603 				       u8 *bw_share)
5604 {
5605 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5606 	struct i40e_pf *pf = vsi->back;
5607 	int ret;
5608 	int i;
5609 
5610 	/* There is no need to reset BW when mqprio mode is on.  */
5611 	if (i40e_is_tc_mqprio_enabled(pf))
5612 		return 0;
5613 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5614 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5615 		if (ret)
5616 			dev_info(&pf->pdev->dev,
5617 				 "Failed to reset tx rate for vsi->seid %u\n",
5618 				 vsi->seid);
5619 		return ret;
5620 	}
5621 	memset(&bw_data, 0, sizeof(bw_data));
5622 	bw_data.tc_valid_bits = enabled_tc;
5623 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5624 		bw_data.tc_bw_credits[i] = bw_share[i];
5625 
5626 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5627 	if (ret) {
5628 		dev_info(&pf->pdev->dev,
5629 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5630 			 pf->hw.aq.asq_last_status);
5631 		return -EINVAL;
5632 	}
5633 
5634 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5635 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5636 
5637 	return 0;
5638 }
5639 
5640 /**
5641  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5642  * @vsi: the VSI being configured
5643  * @enabled_tc: TC map to be enabled
5644  *
5645  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5646 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5647 {
5648 	struct net_device *netdev = vsi->netdev;
5649 	struct i40e_pf *pf = vsi->back;
5650 	struct i40e_hw *hw = &pf->hw;
5651 	u8 netdev_tc = 0;
5652 	int i;
5653 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5654 
5655 	if (!netdev)
5656 		return;
5657 
5658 	if (!enabled_tc) {
5659 		netdev_reset_tc(netdev);
5660 		return;
5661 	}
5662 
5663 	/* Set up actual enabled TCs on the VSI */
5664 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5665 		return;
5666 
5667 	/* set per TC queues for the VSI */
5668 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5669 		/* Only set TC queues for enabled tcs
5670 		 *
5671 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5672 		 * enabled_tc bitmap would be 0x00001001; the driver
5673 		 * will set the numtc for netdev as 2 that will be
5674 		 * referenced by the netdev layer as TC 0 and 1.
5675 		 */
5676 		if (vsi->tc_config.enabled_tc & BIT(i))
5677 			netdev_set_tc_queue(netdev,
5678 					vsi->tc_config.tc_info[i].netdev_tc,
5679 					vsi->tc_config.tc_info[i].qcount,
5680 					vsi->tc_config.tc_info[i].qoffset);
5681 	}
5682 
5683 	if (i40e_is_tc_mqprio_enabled(pf))
5684 		return;
5685 
5686 	/* Assign UP2TC map for the VSI */
5687 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5688 		/* Get the actual TC# for the UP */
5689 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5690 		/* Get the mapped netdev TC# for the UP */
5691 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5692 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5693 	}
5694 }
5695 
5696 /**
5697  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5698  * @vsi: the VSI being configured
5699  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5700  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5701 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5702 				      struct i40e_vsi_context *ctxt)
5703 {
5704 	/* copy just the sections touched not the entire info
5705 	 * since not all sections are valid as returned by
5706 	 * update vsi params
5707 	 */
5708 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5709 	memcpy(&vsi->info.queue_mapping,
5710 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5711 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5712 	       sizeof(vsi->info.tc_mapping));
5713 }
5714 
5715 /**
5716  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5717  * @vsi: the VSI being reconfigured
5718  * @vsi_offset: offset from main VF VSI
5719  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5720 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5721 {
5722 	struct i40e_vsi_context ctxt = {};
5723 	struct i40e_pf *pf;
5724 	struct i40e_hw *hw;
5725 	int ret;
5726 
5727 	if (!vsi)
5728 		return -EINVAL;
5729 	pf = vsi->back;
5730 	hw = &pf->hw;
5731 
5732 	ctxt.seid = vsi->seid;
5733 	ctxt.pf_num = hw->pf_id;
5734 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5735 	ctxt.uplink_seid = vsi->uplink_seid;
5736 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5737 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5738 	ctxt.info = vsi->info;
5739 
5740 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5741 				 false);
5742 	if (vsi->reconfig_rss) {
5743 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5744 				      vsi->num_queue_pairs);
5745 		ret = i40e_vsi_config_rss(vsi);
5746 		if (ret) {
5747 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5748 			return ret;
5749 		}
5750 		vsi->reconfig_rss = false;
5751 	}
5752 
5753 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5754 	if (ret) {
5755 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5756 			 ERR_PTR(ret),
5757 			 libie_aq_str(hw->aq.asq_last_status));
5758 		return ret;
5759 	}
5760 	/* update the local VSI info with updated queue map */
5761 	i40e_vsi_update_queue_map(vsi, &ctxt);
5762 	vsi->info.valid_sections = 0;
5763 
5764 	return ret;
5765 }
5766 
5767 /**
5768  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5769  * @vsi: VSI to be configured
5770  * @enabled_tc: TC bitmap
5771  *
5772  * This configures a particular VSI for TCs that are mapped to the
5773  * given TC bitmap. It uses default bandwidth share for TCs across
5774  * VSIs to configure TC for a particular VSI.
5775  *
5776  * NOTE:
5777  * It is expected that the VSI queues have been quisced before calling
5778  * this function.
5779  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5780 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5781 {
5782 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5783 	struct i40e_pf *pf = vsi->back;
5784 	struct i40e_hw *hw = &pf->hw;
5785 	struct i40e_vsi_context ctxt;
5786 	int ret = 0;
5787 	int i;
5788 
5789 	/* Check if enabled_tc is same as existing or new TCs */
5790 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5791 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5792 		return ret;
5793 
5794 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5795 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5796 		if (enabled_tc & BIT(i))
5797 			bw_share[i] = 1;
5798 	}
5799 
5800 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5801 	if (ret) {
5802 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5803 
5804 		dev_info(&pf->pdev->dev,
5805 			 "Failed configuring TC map %d for VSI %d\n",
5806 			 enabled_tc, vsi->seid);
5807 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5808 						  &bw_config, NULL);
5809 		if (ret) {
5810 			dev_info(&pf->pdev->dev,
5811 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5812 				 ERR_PTR(ret),
5813 				 libie_aq_str(hw->aq.asq_last_status));
5814 			goto out;
5815 		}
5816 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5817 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5818 
5819 			if (!valid_tc)
5820 				valid_tc = bw_config.tc_valid_bits;
5821 			/* Always enable TC0, no matter what */
5822 			valid_tc |= 1;
5823 			dev_info(&pf->pdev->dev,
5824 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5825 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5826 			enabled_tc = valid_tc;
5827 		}
5828 
5829 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5830 		if (ret) {
5831 			dev_err(&pf->pdev->dev,
5832 				"Unable to  configure TC map %d for VSI %d\n",
5833 				enabled_tc, vsi->seid);
5834 			goto out;
5835 		}
5836 	}
5837 
5838 	/* Update Queue Pairs Mapping for currently enabled UPs */
5839 	ctxt.seid = vsi->seid;
5840 	ctxt.pf_num = vsi->back->hw.pf_id;
5841 	ctxt.vf_num = 0;
5842 	ctxt.uplink_seid = vsi->uplink_seid;
5843 	ctxt.info = vsi->info;
5844 	if (i40e_is_tc_mqprio_enabled(pf)) {
5845 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5846 		if (ret)
5847 			goto out;
5848 	} else {
5849 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5850 	}
5851 
5852 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5853 	 * queues changed.
5854 	 */
5855 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5856 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5857 				      vsi->num_queue_pairs);
5858 		ret = i40e_vsi_config_rss(vsi);
5859 		if (ret) {
5860 			dev_info(&vsi->back->pdev->dev,
5861 				 "Failed to reconfig rss for num_queues\n");
5862 			return ret;
5863 		}
5864 		vsi->reconfig_rss = false;
5865 	}
5866 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5867 		ctxt.info.valid_sections |=
5868 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5869 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5870 	}
5871 
5872 	/* Update the VSI after updating the VSI queue-mapping
5873 	 * information
5874 	 */
5875 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5876 	if (ret) {
5877 		dev_info(&pf->pdev->dev,
5878 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5879 			 ERR_PTR(ret),
5880 			 libie_aq_str(hw->aq.asq_last_status));
5881 		goto out;
5882 	}
5883 	/* update the local VSI info with updated queue map */
5884 	i40e_vsi_update_queue_map(vsi, &ctxt);
5885 	vsi->info.valid_sections = 0;
5886 
5887 	/* Update current VSI BW information */
5888 	ret = i40e_vsi_get_bw_info(vsi);
5889 	if (ret) {
5890 		dev_info(&pf->pdev->dev,
5891 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5892 			 ERR_PTR(ret),
5893 			 libie_aq_str(hw->aq.asq_last_status));
5894 		goto out;
5895 	}
5896 
5897 	/* Update the netdev TC setup */
5898 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5899 out:
5900 	return ret;
5901 }
5902 
5903 /**
5904  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5905  * @vsi: VSI to be reconfigured
5906  *
5907  * This reconfigures a particular VSI for TCs that are mapped to the
5908  * TC bitmap stored previously for the VSI.
5909  *
5910  * Context: It is expected that the VSI queues have been quisced before
5911  *          calling this function.
5912  *
5913  * Return: 0 on success, negative value on failure
5914  **/
i40e_vsi_reconfig_tc(struct i40e_vsi * vsi)5915 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5916 {
5917 	u8 enabled_tc;
5918 
5919 	enabled_tc = vsi->tc_config.enabled_tc;
5920 	vsi->tc_config.enabled_tc = 0;
5921 
5922 	return i40e_vsi_config_tc(vsi, enabled_tc);
5923 }
5924 
5925 /**
5926  * i40e_get_link_speed - Returns link speed for the interface
5927  * @vsi: VSI to be configured
5928  *
5929  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5930 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5931 {
5932 	struct i40e_pf *pf = vsi->back;
5933 
5934 	switch (pf->hw.phy.link_info.link_speed) {
5935 	case I40E_LINK_SPEED_40GB:
5936 		return 40000;
5937 	case I40E_LINK_SPEED_25GB:
5938 		return 25000;
5939 	case I40E_LINK_SPEED_20GB:
5940 		return 20000;
5941 	case I40E_LINK_SPEED_10GB:
5942 		return 10000;
5943 	case I40E_LINK_SPEED_1GB:
5944 		return 1000;
5945 	default:
5946 		return -EINVAL;
5947 	}
5948 }
5949 
5950 /**
5951  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5952  * @vsi: Pointer to vsi structure
5953  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5954  *
5955  * Helper function to convert units before send to set BW limit
5956  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5957 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5958 {
5959 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5960 		dev_warn(&vsi->back->pdev->dev,
5961 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5962 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5963 	} else {
5964 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5965 	}
5966 
5967 	return max_tx_rate;
5968 }
5969 
5970 /**
5971  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5972  * @vsi: VSI to be configured
5973  * @seid: seid of the channel/VSI
5974  * @max_tx_rate: max TX rate to be configured as BW limit
5975  *
5976  * Helper function to set BW limit for a given VSI
5977  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5978 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5979 {
5980 	struct i40e_pf *pf = vsi->back;
5981 	u64 credits = 0;
5982 	int speed = 0;
5983 	int ret = 0;
5984 
5985 	speed = i40e_get_link_speed(vsi);
5986 	if (max_tx_rate > speed) {
5987 		dev_err(&pf->pdev->dev,
5988 			"Invalid max tx rate %llu specified for VSI seid %d.",
5989 			max_tx_rate, seid);
5990 		return -EINVAL;
5991 	}
5992 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5993 		dev_warn(&pf->pdev->dev,
5994 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5995 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5996 	}
5997 
5998 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5999 	credits = max_tx_rate;
6000 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
6001 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
6002 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6003 	if (ret)
6004 		dev_err(&pf->pdev->dev,
6005 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6006 			max_tx_rate, seid, ERR_PTR(ret),
6007 			libie_aq_str(pf->hw.aq.asq_last_status));
6008 	return ret;
6009 }
6010 
6011 /**
6012  * i40e_remove_queue_channels - Remove queue channels for the TCs
6013  * @vsi: VSI to be configured
6014  *
6015  * Remove queue channels for the TCs
6016  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)6017 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6018 {
6019 	struct i40e_cloud_filter *cfilter;
6020 	enum libie_aq_err last_aq_status;
6021 	struct i40e_channel *ch, *ch_tmp;
6022 	struct i40e_pf *pf = vsi->back;
6023 	struct hlist_node *node;
6024 	int ret, i;
6025 
6026 	/* Reset rss size that was stored when reconfiguring rss for
6027 	 * channel VSIs with non-power-of-2 queue count.
6028 	 */
6029 	vsi->current_rss_size = 0;
6030 
6031 	/* perform cleanup for channels if they exist */
6032 	if (list_empty(&vsi->ch_list))
6033 		return;
6034 
6035 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6036 		struct i40e_vsi *p_vsi;
6037 
6038 		list_del(&ch->list);
6039 		p_vsi = ch->parent_vsi;
6040 		if (!p_vsi || !ch->initialized) {
6041 			kfree(ch);
6042 			continue;
6043 		}
6044 		/* Reset queue contexts */
6045 		for (i = 0; i < ch->num_queue_pairs; i++) {
6046 			struct i40e_ring *tx_ring, *rx_ring;
6047 			u16 pf_q;
6048 
6049 			pf_q = ch->base_queue + i;
6050 			tx_ring = vsi->tx_rings[pf_q];
6051 			tx_ring->ch = NULL;
6052 
6053 			rx_ring = vsi->rx_rings[pf_q];
6054 			rx_ring->ch = NULL;
6055 		}
6056 
6057 		/* Reset BW configured for this VSI via mqprio */
6058 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6059 		if (ret)
6060 			dev_info(&vsi->back->pdev->dev,
6061 				 "Failed to reset tx rate for ch->seid %u\n",
6062 				 ch->seid);
6063 
6064 		/* delete cloud filters associated with this channel */
6065 		hlist_for_each_entry_safe(cfilter, node,
6066 					  &pf->cloud_filter_list, cloud_node) {
6067 			if (cfilter->seid != ch->seid)
6068 				continue;
6069 
6070 			hash_del(&cfilter->cloud_node);
6071 			if (cfilter->dst_port)
6072 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6073 									cfilter,
6074 									false);
6075 			else
6076 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6077 								false);
6078 			last_aq_status = pf->hw.aq.asq_last_status;
6079 			if (ret)
6080 				dev_info(&pf->pdev->dev,
6081 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6082 					 ERR_PTR(ret),
6083 					 libie_aq_str(last_aq_status));
6084 			kfree(cfilter);
6085 		}
6086 
6087 		/* delete VSI from FW */
6088 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6089 					     NULL);
6090 		if (ret)
6091 			dev_err(&vsi->back->pdev->dev,
6092 				"unable to remove channel (%d) for parent VSI(%d)\n",
6093 				ch->seid, p_vsi->seid);
6094 		kfree(ch);
6095 	}
6096 	INIT_LIST_HEAD(&vsi->ch_list);
6097 }
6098 
6099 /**
6100  * i40e_get_max_queues_for_channel
6101  * @vsi: ptr to VSI to which channels are associated with
6102  *
6103  * Helper function which returns max value among the queue counts set on the
6104  * channels/TCs created.
6105  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6106 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6107 {
6108 	struct i40e_channel *ch, *ch_tmp;
6109 	int max = 0;
6110 
6111 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6112 		if (!ch->initialized)
6113 			continue;
6114 		if (ch->num_queue_pairs > max)
6115 			max = ch->num_queue_pairs;
6116 	}
6117 
6118 	return max;
6119 }
6120 
6121 /**
6122  * i40e_validate_num_queues - validate num_queues w.r.t channel
6123  * @pf: ptr to PF device
6124  * @num_queues: number of queues
6125  * @vsi: the parent VSI
6126  * @reconfig_rss: indicates should the RSS be reconfigured or not
6127  *
6128  * This function validates number of queues in the context of new channel
6129  * which is being established and determines if RSS should be reconfigured
6130  * or not for parent VSI.
6131  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6132 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6133 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6134 {
6135 	int max_ch_queues;
6136 
6137 	if (!reconfig_rss)
6138 		return -EINVAL;
6139 
6140 	*reconfig_rss = false;
6141 	if (vsi->current_rss_size) {
6142 		if (num_queues > vsi->current_rss_size) {
6143 			dev_dbg(&pf->pdev->dev,
6144 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6145 				num_queues, vsi->current_rss_size);
6146 			return -EINVAL;
6147 		} else if ((num_queues < vsi->current_rss_size) &&
6148 			   (!is_power_of_2(num_queues))) {
6149 			dev_dbg(&pf->pdev->dev,
6150 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6151 				num_queues, vsi->current_rss_size);
6152 			return -EINVAL;
6153 		}
6154 	}
6155 
6156 	if (!is_power_of_2(num_queues)) {
6157 		/* Find the max num_queues configured for channel if channel
6158 		 * exist.
6159 		 * if channel exist, then enforce 'num_queues' to be more than
6160 		 * max ever queues configured for channel.
6161 		 */
6162 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6163 		if (num_queues < max_ch_queues) {
6164 			dev_dbg(&pf->pdev->dev,
6165 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6166 				num_queues, max_ch_queues);
6167 			return -EINVAL;
6168 		}
6169 		*reconfig_rss = true;
6170 	}
6171 
6172 	return 0;
6173 }
6174 
6175 /**
6176  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6177  * @vsi: the VSI being setup
6178  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6179  *
6180  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6181  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6182 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6183 {
6184 	struct i40e_pf *pf = vsi->back;
6185 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6186 	struct i40e_hw *hw = &pf->hw;
6187 	int local_rss_size;
6188 	u8 *lut;
6189 	int ret;
6190 
6191 	if (!vsi->rss_size)
6192 		return -EINVAL;
6193 
6194 	if (rss_size > vsi->rss_size)
6195 		return -EINVAL;
6196 
6197 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6198 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6199 	if (!lut)
6200 		return -ENOMEM;
6201 
6202 	/* Ignoring user configured lut if there is one */
6203 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6204 
6205 	/* Use user configured hash key if there is one, otherwise
6206 	 * use default.
6207 	 */
6208 	if (vsi->rss_hkey_user)
6209 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6210 	else
6211 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6212 
6213 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6214 	if (ret) {
6215 		dev_info(&pf->pdev->dev,
6216 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6217 			 ERR_PTR(ret),
6218 			 libie_aq_str(hw->aq.asq_last_status));
6219 		kfree(lut);
6220 		return ret;
6221 	}
6222 	kfree(lut);
6223 
6224 	/* Do the update w.r.t. storing rss_size */
6225 	if (!vsi->orig_rss_size)
6226 		vsi->orig_rss_size = vsi->rss_size;
6227 	vsi->current_rss_size = local_rss_size;
6228 
6229 	return ret;
6230 }
6231 
6232 /**
6233  * i40e_channel_setup_queue_map - Setup a channel queue map
6234  * @pf: ptr to PF device
6235  * @ctxt: VSI context structure
6236  * @ch: ptr to channel structure
6237  *
6238  * Setup queue map for a specific channel
6239  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6240 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6241 					 struct i40e_vsi_context *ctxt,
6242 					 struct i40e_channel *ch)
6243 {
6244 	u16 qcount, qmap, sections = 0;
6245 	u8 offset = 0;
6246 	int pow;
6247 
6248 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6249 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6250 
6251 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6252 	ch->num_queue_pairs = qcount;
6253 
6254 	/* find the next higher power-of-2 of num queue pairs */
6255 	pow = ilog2(qcount);
6256 	if (!is_power_of_2(qcount))
6257 		pow++;
6258 
6259 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6260 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6261 
6262 	/* Setup queue TC[0].qmap for given VSI context */
6263 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6264 
6265 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6266 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6267 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6268 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6269 }
6270 
6271 /**
6272  * i40e_add_channel - add a channel by adding VSI
6273  * @pf: ptr to PF device
6274  * @uplink_seid: underlying HW switching element (VEB) ID
6275  * @ch: ptr to channel structure
6276  *
6277  * Add a channel (VSI) using add_vsi and queue_map
6278  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6279 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6280 			    struct i40e_channel *ch)
6281 {
6282 	struct i40e_hw *hw = &pf->hw;
6283 	struct i40e_vsi_context ctxt;
6284 	u8 enabled_tc = 0x1; /* TC0 enabled */
6285 	int ret;
6286 
6287 	if (ch->type != I40E_VSI_VMDQ2) {
6288 		dev_info(&pf->pdev->dev,
6289 			 "add new vsi failed, ch->type %d\n", ch->type);
6290 		return -EINVAL;
6291 	}
6292 
6293 	memset(&ctxt, 0, sizeof(ctxt));
6294 	ctxt.pf_num = hw->pf_id;
6295 	ctxt.vf_num = 0;
6296 	ctxt.uplink_seid = uplink_seid;
6297 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6298 	if (ch->type == I40E_VSI_VMDQ2)
6299 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6300 
6301 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6302 		ctxt.info.valid_sections |=
6303 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6304 		ctxt.info.switch_id =
6305 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6306 	}
6307 
6308 	/* Set queue map for a given VSI context */
6309 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6310 
6311 	/* Now time to create VSI */
6312 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6313 	if (ret) {
6314 		dev_info(&pf->pdev->dev,
6315 			 "add new vsi failed, err %pe aq_err %s\n",
6316 			 ERR_PTR(ret),
6317 			 libie_aq_str(pf->hw.aq.asq_last_status));
6318 		return -ENOENT;
6319 	}
6320 
6321 	/* Success, update channel, set enabled_tc only if the channel
6322 	 * is not a macvlan
6323 	 */
6324 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6325 	ch->seid = ctxt.seid;
6326 	ch->vsi_number = ctxt.vsi_number;
6327 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6328 
6329 	/* copy just the sections touched not the entire info
6330 	 * since not all sections are valid as returned by
6331 	 * update vsi params
6332 	 */
6333 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6334 	memcpy(&ch->info.queue_mapping,
6335 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6336 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6337 	       sizeof(ctxt.info.tc_mapping));
6338 
6339 	return 0;
6340 }
6341 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6342 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6343 				  u8 *bw_share)
6344 {
6345 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6346 	int ret;
6347 	int i;
6348 
6349 	memset(&bw_data, 0, sizeof(bw_data));
6350 	bw_data.tc_valid_bits = ch->enabled_tc;
6351 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6352 		bw_data.tc_bw_credits[i] = bw_share[i];
6353 
6354 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6355 				       &bw_data, NULL);
6356 	if (ret) {
6357 		dev_info(&vsi->back->pdev->dev,
6358 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6359 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6360 		return -EINVAL;
6361 	}
6362 
6363 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6364 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6365 
6366 	return 0;
6367 }
6368 
6369 /**
6370  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6371  * @pf: ptr to PF device
6372  * @vsi: the VSI being setup
6373  * @ch: ptr to channel structure
6374  *
6375  * Configure TX rings associated with channel (VSI) since queues are being
6376  * from parent VSI.
6377  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6378 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6379 				       struct i40e_vsi *vsi,
6380 				       struct i40e_channel *ch)
6381 {
6382 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6383 	int ret;
6384 	int i;
6385 
6386 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6387 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6388 		if (ch->enabled_tc & BIT(i))
6389 			bw_share[i] = 1;
6390 	}
6391 
6392 	/* configure BW for new VSI */
6393 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6394 	if (ret) {
6395 		dev_info(&vsi->back->pdev->dev,
6396 			 "Failed configuring TC map %d for channel (seid %u)\n",
6397 			 ch->enabled_tc, ch->seid);
6398 		return ret;
6399 	}
6400 
6401 	for (i = 0; i < ch->num_queue_pairs; i++) {
6402 		struct i40e_ring *tx_ring, *rx_ring;
6403 		u16 pf_q;
6404 
6405 		pf_q = ch->base_queue + i;
6406 
6407 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6408 		 * context
6409 		 */
6410 		tx_ring = vsi->tx_rings[pf_q];
6411 		tx_ring->ch = ch;
6412 
6413 		/* Get the RX ring ptr */
6414 		rx_ring = vsi->rx_rings[pf_q];
6415 		rx_ring->ch = ch;
6416 	}
6417 
6418 	return 0;
6419 }
6420 
6421 /**
6422  * i40e_setup_hw_channel - setup new channel
6423  * @pf: ptr to PF device
6424  * @vsi: the VSI being setup
6425  * @ch: ptr to channel structure
6426  * @uplink_seid: underlying HW switching element (VEB) ID
6427  * @type: type of channel to be created (VMDq2/VF)
6428  *
6429  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6430  * and configures TX rings accordingly
6431  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6432 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6433 					struct i40e_vsi *vsi,
6434 					struct i40e_channel *ch,
6435 					u16 uplink_seid, u8 type)
6436 {
6437 	int ret;
6438 
6439 	ch->initialized = false;
6440 	ch->base_queue = vsi->next_base_queue;
6441 	ch->type = type;
6442 
6443 	/* Proceed with creation of channel (VMDq2) VSI */
6444 	ret = i40e_add_channel(pf, uplink_seid, ch);
6445 	if (ret) {
6446 		dev_info(&pf->pdev->dev,
6447 			 "failed to add_channel using uplink_seid %u\n",
6448 			 uplink_seid);
6449 		return ret;
6450 	}
6451 
6452 	/* Mark the successful creation of channel */
6453 	ch->initialized = true;
6454 
6455 	/* Reconfigure TX queues using QTX_CTL register */
6456 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6457 	if (ret) {
6458 		dev_info(&pf->pdev->dev,
6459 			 "failed to configure TX rings for channel %u\n",
6460 			 ch->seid);
6461 		return ret;
6462 	}
6463 
6464 	/* update 'next_base_queue' */
6465 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6466 	dev_dbg(&pf->pdev->dev,
6467 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6468 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6469 		ch->num_queue_pairs,
6470 		vsi->next_base_queue);
6471 	return ret;
6472 }
6473 
6474 /**
6475  * i40e_setup_channel - setup new channel using uplink element
6476  * @pf: ptr to PF device
6477  * @vsi: pointer to the VSI to set up the channel within
6478  * @ch: ptr to channel structure
6479  *
6480  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6481  * and uplink switching element (uplink_seid)
6482  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6483 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6484 			       struct i40e_channel *ch)
6485 {
6486 	struct i40e_vsi *main_vsi;
6487 	u8 vsi_type;
6488 	u16 seid;
6489 	int ret;
6490 
6491 	if (vsi->type == I40E_VSI_MAIN) {
6492 		vsi_type = I40E_VSI_VMDQ2;
6493 	} else {
6494 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6495 			vsi->type);
6496 		return false;
6497 	}
6498 
6499 	/* underlying switching element */
6500 	main_vsi = i40e_pf_get_main_vsi(pf);
6501 	seid = main_vsi->uplink_seid;
6502 
6503 	/* create channel (VSI), configure TX rings */
6504 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6505 	if (ret) {
6506 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6507 		return false;
6508 	}
6509 
6510 	return ch->initialized ? true : false;
6511 }
6512 
6513 /**
6514  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6515  * @vsi: ptr to VSI which has PF backing
6516  *
6517  * Sets up switch mode correctly if it needs to be changed and perform
6518  * what are allowed modes.
6519  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6520 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6521 {
6522 	u8 mode;
6523 	struct i40e_pf *pf = vsi->back;
6524 	struct i40e_hw *hw = &pf->hw;
6525 	int ret;
6526 
6527 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6528 	if (ret)
6529 		return -EINVAL;
6530 
6531 	if (hw->dev_caps.switch_mode) {
6532 		/* if switch mode is set, support mode2 (non-tunneled for
6533 		 * cloud filter) for now
6534 		 */
6535 		u32 switch_mode = hw->dev_caps.switch_mode &
6536 				  I40E_SWITCH_MODE_MASK;
6537 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6538 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6539 				return 0;
6540 			dev_err(&pf->pdev->dev,
6541 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6542 				hw->dev_caps.switch_mode);
6543 			return -EINVAL;
6544 		}
6545 	}
6546 
6547 	/* Set Bit 7 to be valid */
6548 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6549 
6550 	/* Set L4type for TCP support */
6551 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6552 
6553 	/* Set cloud filter mode */
6554 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6555 
6556 	/* Prep mode field for set_switch_config */
6557 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6558 					pf->last_sw_conf_valid_flags,
6559 					mode, NULL);
6560 	if (ret && hw->aq.asq_last_status != LIBIE_AQ_RC_ESRCH)
6561 		dev_err(&pf->pdev->dev,
6562 			"couldn't set switch config bits, err %pe aq_err %s\n",
6563 			ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
6564 
6565 	return ret;
6566 }
6567 
6568 /**
6569  * i40e_create_queue_channel - function to create channel
6570  * @vsi: VSI to be configured
6571  * @ch: ptr to channel (it contains channel specific params)
6572  *
6573  * This function creates channel (VSI) using num_queues specified by user,
6574  * reconfigs RSS if needed.
6575  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6576 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6577 			      struct i40e_channel *ch)
6578 {
6579 	struct i40e_pf *pf = vsi->back;
6580 	bool reconfig_rss;
6581 	int err;
6582 
6583 	if (!ch)
6584 		return -EINVAL;
6585 
6586 	if (!ch->num_queue_pairs) {
6587 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6588 			ch->num_queue_pairs);
6589 		return -EINVAL;
6590 	}
6591 
6592 	/* validate user requested num_queues for channel */
6593 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6594 				       &reconfig_rss);
6595 	if (err) {
6596 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6597 			 ch->num_queue_pairs);
6598 		return -EINVAL;
6599 	}
6600 
6601 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6602 	 * VSI to be added switch to VEB mode.
6603 	 */
6604 
6605 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6606 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6607 
6608 		if (vsi->type == I40E_VSI_MAIN) {
6609 			if (i40e_is_tc_mqprio_enabled(pf))
6610 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6611 			else
6612 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6613 		}
6614 		/* now onwards for main VSI, number of queues will be value
6615 		 * of TC0's queue count
6616 		 */
6617 	}
6618 
6619 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6620 	 * it should be more than num_queues
6621 	 */
6622 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6623 		dev_dbg(&pf->pdev->dev,
6624 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6625 			vsi->cnt_q_avail, ch->num_queue_pairs);
6626 		return -EINVAL;
6627 	}
6628 
6629 	/* reconfig_rss only if vsi type is MAIN_VSI */
6630 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6631 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6632 		if (err) {
6633 			dev_info(&pf->pdev->dev,
6634 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6635 				 ch->num_queue_pairs);
6636 			return -EINVAL;
6637 		}
6638 	}
6639 
6640 	if (!i40e_setup_channel(pf, vsi, ch)) {
6641 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6642 		return -EINVAL;
6643 	}
6644 
6645 	dev_info(&pf->pdev->dev,
6646 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6647 		 ch->seid, ch->num_queue_pairs);
6648 
6649 	/* configure VSI for BW limit */
6650 	if (ch->max_tx_rate) {
6651 		u64 credits = ch->max_tx_rate;
6652 
6653 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6654 			return -EINVAL;
6655 
6656 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6657 		dev_dbg(&pf->pdev->dev,
6658 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6659 			ch->max_tx_rate,
6660 			credits,
6661 			ch->seid);
6662 	}
6663 
6664 	/* in case of VF, this will be main SRIOV VSI */
6665 	ch->parent_vsi = vsi;
6666 
6667 	/* and update main_vsi's count for queue_available to use */
6668 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6669 
6670 	return 0;
6671 }
6672 
6673 /**
6674  * i40e_configure_queue_channels - Add queue channel for the given TCs
6675  * @vsi: VSI to be configured
6676  *
6677  * Configures queue channel mapping to the given TCs
6678  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6679 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6680 {
6681 	struct i40e_channel *ch;
6682 	u64 max_rate = 0;
6683 	int ret = 0, i;
6684 
6685 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6686 	vsi->tc_seid_map[0] = vsi->seid;
6687 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6688 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6689 			ch = kzalloc_obj(*ch);
6690 			if (!ch) {
6691 				ret = -ENOMEM;
6692 				goto err_free;
6693 			}
6694 
6695 			INIT_LIST_HEAD(&ch->list);
6696 			ch->num_queue_pairs =
6697 				vsi->tc_config.tc_info[i].qcount;
6698 			ch->base_queue =
6699 				vsi->tc_config.tc_info[i].qoffset;
6700 
6701 			/* Bandwidth limit through tc interface is in bytes/s,
6702 			 * change to Mbit/s
6703 			 */
6704 			max_rate = vsi->mqprio_qopt.max_rate[i];
6705 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6706 			ch->max_tx_rate = max_rate;
6707 
6708 			list_add_tail(&ch->list, &vsi->ch_list);
6709 
6710 			ret = i40e_create_queue_channel(vsi, ch);
6711 			if (ret) {
6712 				dev_err(&vsi->back->pdev->dev,
6713 					"Failed creating queue channel with TC%d: queues %d\n",
6714 					i, ch->num_queue_pairs);
6715 				goto err_free;
6716 			}
6717 			vsi->tc_seid_map[i] = ch->seid;
6718 		}
6719 	}
6720 
6721 	/* reset to reconfigure TX queue contexts */
6722 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6723 	return ret;
6724 
6725 err_free:
6726 	i40e_remove_queue_channels(vsi);
6727 	return ret;
6728 }
6729 
6730 /**
6731  * i40e_veb_config_tc - Configure TCs for given VEB
6732  * @veb: given VEB
6733  * @enabled_tc: TC bitmap
6734  *
6735  * Configures given TC bitmap for VEB (switching) element
6736  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6737 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6738 {
6739 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6740 	struct i40e_pf *pf = veb->pf;
6741 	int ret = 0;
6742 	int i;
6743 
6744 	/* No TCs or already enabled TCs just return */
6745 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6746 		return ret;
6747 
6748 	bw_data.tc_valid_bits = enabled_tc;
6749 	/* bw_data.absolute_credits is not set (relative) */
6750 
6751 	/* Enable ETS TCs with equal BW Share for now */
6752 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6753 		if (enabled_tc & BIT(i))
6754 			bw_data.tc_bw_share_credits[i] = 1;
6755 	}
6756 
6757 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6758 						   &bw_data, NULL);
6759 	if (ret) {
6760 		dev_info(&pf->pdev->dev,
6761 			 "VEB bw config failed, err %pe aq_err %s\n",
6762 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6763 		goto out;
6764 	}
6765 
6766 	/* Update the BW information */
6767 	ret = i40e_veb_get_bw_info(veb);
6768 	if (ret) {
6769 		dev_info(&pf->pdev->dev,
6770 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6771 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6772 	}
6773 
6774 out:
6775 	return ret;
6776 }
6777 
6778 #ifdef CONFIG_I40E_DCB
6779 /**
6780  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6781  * @pf: PF struct
6782  *
6783  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6784  * the caller would've quiesce all the VSIs before calling
6785  * this function
6786  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6787 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6788 {
6789 	struct i40e_vsi *vsi;
6790 	struct i40e_veb *veb;
6791 	u8 tc_map = 0;
6792 	int ret;
6793 	int v;
6794 
6795 	/* Enable the TCs available on PF to all VEBs */
6796 	tc_map = i40e_pf_get_tc_map(pf);
6797 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6798 		return;
6799 
6800 	i40e_pf_for_each_veb(pf, v, veb) {
6801 		ret = i40e_veb_config_tc(veb, tc_map);
6802 		if (ret) {
6803 			dev_info(&pf->pdev->dev,
6804 				 "Failed configuring TC for VEB seid=%d\n",
6805 				 veb->seid);
6806 			/* Will try to configure as many components */
6807 		}
6808 	}
6809 
6810 	/* Update each VSI */
6811 	i40e_pf_for_each_vsi(pf, v, vsi) {
6812 		/* - Enable all TCs for the LAN VSI
6813 		 * - For all others keep them at TC0 for now
6814 		 */
6815 		if (vsi->type == I40E_VSI_MAIN)
6816 			tc_map = i40e_pf_get_tc_map(pf);
6817 		else
6818 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6819 
6820 		ret = i40e_vsi_config_tc(vsi, tc_map);
6821 		if (ret) {
6822 			dev_info(&pf->pdev->dev,
6823 				 "Failed configuring TC for VSI seid=%d\n",
6824 				 vsi->seid);
6825 			/* Will try to configure as many components */
6826 		} else {
6827 			/* Re-configure VSI vectors based on updated TC map */
6828 			i40e_vsi_map_rings_to_vectors(vsi);
6829 			if (vsi->netdev)
6830 				i40e_dcbnl_set_all(vsi);
6831 		}
6832 	}
6833 }
6834 
6835 /**
6836  * i40e_resume_port_tx - Resume port Tx
6837  * @pf: PF struct
6838  *
6839  * Resume a port's Tx and issue a PF reset in case of failure to
6840  * resume.
6841  **/
i40e_resume_port_tx(struct i40e_pf * pf)6842 static int i40e_resume_port_tx(struct i40e_pf *pf)
6843 {
6844 	struct i40e_hw *hw = &pf->hw;
6845 	int ret;
6846 
6847 	ret = i40e_aq_resume_port_tx(hw, NULL);
6848 	if (ret) {
6849 		dev_info(&pf->pdev->dev,
6850 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6851 			  ERR_PTR(ret),
6852 			  libie_aq_str(pf->hw.aq.asq_last_status));
6853 		/* Schedule PF reset to recover */
6854 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6855 		i40e_service_event_schedule(pf);
6856 	}
6857 
6858 	return ret;
6859 }
6860 
6861 /**
6862  * i40e_suspend_port_tx - Suspend port Tx
6863  * @pf: PF struct
6864  *
6865  * Suspend a port's Tx and issue a PF reset in case of failure.
6866  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6867 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6868 {
6869 	struct i40e_hw *hw = &pf->hw;
6870 	int ret;
6871 
6872 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6873 	if (ret) {
6874 		dev_info(&pf->pdev->dev,
6875 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6876 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6877 		/* Schedule PF reset to recover */
6878 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6879 		i40e_service_event_schedule(pf);
6880 	}
6881 
6882 	return ret;
6883 }
6884 
6885 /**
6886  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6887  * @pf: PF being configured
6888  * @new_cfg: New DCBX configuration
6889  *
6890  * Program DCB settings into HW and reconfigure VEB/VSIs on
6891  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6892  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6893 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6894 				  struct i40e_dcbx_config *new_cfg)
6895 {
6896 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6897 	int ret;
6898 
6899 	/* Check if need reconfiguration */
6900 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6901 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6902 		return 0;
6903 	}
6904 
6905 	/* Config change disable all VSIs */
6906 	i40e_pf_quiesce_all_vsi(pf);
6907 
6908 	/* Copy the new config to the current config */
6909 	*old_cfg = *new_cfg;
6910 	old_cfg->etsrec = old_cfg->etscfg;
6911 	ret = i40e_set_dcb_config(&pf->hw);
6912 	if (ret) {
6913 		dev_info(&pf->pdev->dev,
6914 			 "Set DCB Config failed, err %pe aq_err %s\n",
6915 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6916 		goto out;
6917 	}
6918 
6919 	/* Changes in configuration update VEB/VSI */
6920 	i40e_dcb_reconfigure(pf);
6921 out:
6922 	/* In case of reset do not try to resume anything */
6923 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6924 		/* Re-start the VSIs if disabled */
6925 		ret = i40e_resume_port_tx(pf);
6926 		/* In case of error no point in resuming VSIs */
6927 		if (ret)
6928 			goto err;
6929 		i40e_pf_unquiesce_all_vsi(pf);
6930 	}
6931 err:
6932 	return ret;
6933 }
6934 
6935 /**
6936  * i40e_hw_dcb_config - Program new DCBX settings into HW
6937  * @pf: PF being configured
6938  * @new_cfg: New DCBX configuration
6939  *
6940  * Program DCB settings into HW and reconfigure VEB/VSIs on
6941  * given PF
6942  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6943 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6944 {
6945 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6946 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6947 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6948 	struct i40e_dcbx_config *old_cfg;
6949 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6950 	struct i40e_rx_pb_config pb_cfg;
6951 	struct i40e_hw *hw = &pf->hw;
6952 	u8 num_ports = hw->num_ports;
6953 	bool need_reconfig;
6954 	int ret = -EINVAL;
6955 	u8 lltc_map = 0;
6956 	u8 tc_map = 0;
6957 	u8 new_numtc;
6958 	u8 i;
6959 
6960 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6961 	/* Un-pack information to Program ETS HW via shared API
6962 	 * numtc, tcmap
6963 	 * LLTC map
6964 	 * ETS/NON-ETS arbiter mode
6965 	 * max exponent (credit refills)
6966 	 * Total number of ports
6967 	 * PFC priority bit-map
6968 	 * Priority Table
6969 	 * BW % per TC
6970 	 * Arbiter mode between UPs sharing same TC
6971 	 * TSA table (ETS or non-ETS)
6972 	 * EEE enabled or not
6973 	 * MFS TC table
6974 	 */
6975 
6976 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6977 
6978 	memset(&ets_data, 0, sizeof(ets_data));
6979 	for (i = 0; i < new_numtc; i++) {
6980 		tc_map |= BIT(i);
6981 		switch (new_cfg->etscfg.tsatable[i]) {
6982 		case I40E_IEEE_TSA_ETS:
6983 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6984 			ets_data.tc_bw_share_credits[i] =
6985 					new_cfg->etscfg.tcbwtable[i];
6986 			break;
6987 		case I40E_IEEE_TSA_STRICT:
6988 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6989 			lltc_map |= BIT(i);
6990 			ets_data.tc_bw_share_credits[i] =
6991 					I40E_DCB_STRICT_PRIO_CREDITS;
6992 			break;
6993 		default:
6994 			/* Invalid TSA type */
6995 			need_reconfig = false;
6996 			goto out;
6997 		}
6998 	}
6999 
7000 	old_cfg = &hw->local_dcbx_config;
7001 	/* Check if need reconfiguration */
7002 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7003 
7004 	/* If needed, enable/disable frame tagging, disable all VSIs
7005 	 * and suspend port tx
7006 	 */
7007 	if (need_reconfig) {
7008 		/* Enable DCB tagging only when more than one TC */
7009 		if (new_numtc > 1)
7010 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7011 		else
7012 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7013 
7014 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7015 		/* Reconfiguration needed quiesce all VSIs */
7016 		i40e_pf_quiesce_all_vsi(pf);
7017 		ret = i40e_suspend_port_tx(pf);
7018 		if (ret)
7019 			goto err;
7020 	}
7021 
7022 	/* Configure Port ETS Tx Scheduler */
7023 	ets_data.tc_valid_bits = tc_map;
7024 	ets_data.tc_strict_priority_flags = lltc_map;
7025 	ret = i40e_aq_config_switch_comp_ets
7026 		(hw, pf->mac_seid, &ets_data,
7027 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7028 	if (ret) {
7029 		dev_info(&pf->pdev->dev,
7030 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7031 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7032 		goto out;
7033 	}
7034 
7035 	/* Configure Rx ETS HW */
7036 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7037 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7038 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7039 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7040 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7041 				   lltc_map);
7042 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7043 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7044 				     prio_type);
7045 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7046 			       new_cfg->etscfg.prioritytable);
7047 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7048 
7049 	/* Configure Rx Packet Buffers in HW */
7050 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7051 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7052 
7053 		mfs_tc[i] = main_vsi->netdev->mtu;
7054 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7055 	}
7056 
7057 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7058 					 false, new_cfg->pfc.pfcenable,
7059 					 mfs_tc, &pb_cfg);
7060 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7061 
7062 	/* Update the local Rx Packet buffer config */
7063 	pf->pb_cfg = pb_cfg;
7064 
7065 	/* Inform the FW about changes to DCB configuration */
7066 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7067 	if (ret) {
7068 		dev_info(&pf->pdev->dev,
7069 			 "DCB Updated failed, err %pe aq_err %s\n",
7070 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7071 		goto out;
7072 	}
7073 
7074 	/* Update the port DCBx configuration */
7075 	*old_cfg = *new_cfg;
7076 
7077 	/* Changes in configuration update VEB/VSI */
7078 	i40e_dcb_reconfigure(pf);
7079 out:
7080 	/* Re-start the VSIs if disabled */
7081 	if (need_reconfig) {
7082 		ret = i40e_resume_port_tx(pf);
7083 
7084 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7085 		/* In case of error no point in resuming VSIs */
7086 		if (ret)
7087 			goto err;
7088 
7089 		/* Wait for the PF's queues to be disabled */
7090 		ret = i40e_pf_wait_queues_disabled(pf);
7091 		if (ret) {
7092 			/* Schedule PF reset to recover */
7093 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7094 			i40e_service_event_schedule(pf);
7095 			goto err;
7096 		} else {
7097 			i40e_pf_unquiesce_all_vsi(pf);
7098 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7099 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7100 		}
7101 		/* registers are set, lets apply */
7102 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7103 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7104 	}
7105 
7106 err:
7107 	return ret;
7108 }
7109 
7110 /**
7111  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7112  * @pf: PF being queried
7113  *
7114  * Set default DCB configuration in case DCB is to be done in SW.
7115  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7116 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7117 {
7118 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7119 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7120 	struct i40e_hw *hw = &pf->hw;
7121 	int err;
7122 
7123 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7124 		/* Update the local cached instance with TC0 ETS */
7125 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7126 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7127 		pf->tmp_cfg.etscfg.maxtcs = 0;
7128 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7129 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7130 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7131 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7132 		/* FW needs one App to configure HW */
7133 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7134 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7135 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7136 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7137 
7138 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7139 	}
7140 
7141 	memset(&ets_data, 0, sizeof(ets_data));
7142 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7143 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7144 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7145 
7146 	/* Enable ETS on the Physical port */
7147 	err = i40e_aq_config_switch_comp_ets
7148 		(hw, pf->mac_seid, &ets_data,
7149 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7150 	if (err) {
7151 		dev_info(&pf->pdev->dev,
7152 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7153 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7154 		err = -ENOENT;
7155 		goto out;
7156 	}
7157 
7158 	/* Update the local cached instance with TC0 ETS */
7159 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7160 	dcb_cfg->etscfg.cbs = 0;
7161 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7162 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7163 
7164 out:
7165 	return err;
7166 }
7167 
7168 /**
7169  * i40e_init_pf_dcb - Initialize DCB configuration
7170  * @pf: PF being configured
7171  *
7172  * Query the current DCB configuration and cache it
7173  * in the hardware structure
7174  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7175 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7176 {
7177 	struct i40e_hw *hw = &pf->hw;
7178 	int err;
7179 
7180 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7181 	 * Also do not enable DCBx if FW LLDP agent is disabled
7182 	 */
7183 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7184 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7185 		err = -EOPNOTSUPP;
7186 		goto out;
7187 	}
7188 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7189 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7190 		err = i40e_dcb_sw_default_config(pf);
7191 		if (err) {
7192 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7193 			goto out;
7194 		}
7195 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7196 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7197 			       DCB_CAP_DCBX_VER_IEEE;
7198 		/* at init capable but disabled */
7199 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7200 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7201 		goto out;
7202 	}
7203 	err = i40e_init_dcb(hw, true);
7204 	if (!err) {
7205 		/* Device/Function is not DCBX capable */
7206 		if ((!hw->func_caps.dcb) ||
7207 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7208 			dev_info(&pf->pdev->dev,
7209 				 "DCBX offload is not supported or is disabled for this PF.\n");
7210 		} else {
7211 			/* When status is not DISABLED then DCBX in FW */
7212 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7213 				       DCB_CAP_DCBX_VER_IEEE;
7214 
7215 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7216 			/* Enable DCB tagging only when more than one TC
7217 			 * or explicitly disable if only one TC
7218 			 */
7219 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7220 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7221 			else
7222 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7223 			dev_dbg(&pf->pdev->dev,
7224 				"DCBX offload is supported for this PF.\n");
7225 		}
7226 	} else if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_EPERM) {
7227 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7228 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7229 	} else {
7230 		dev_info(&pf->pdev->dev,
7231 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7232 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7233 	}
7234 
7235 out:
7236 	return err;
7237 }
7238 #endif /* CONFIG_I40E_DCB */
7239 
i40e_print_link_message_eee(struct i40e_vsi * vsi,const char * speed,const char * fc)7240 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7241 					const char *speed, const char *fc)
7242 {
7243 	struct ethtool_keee kedata;
7244 
7245 	memzero_explicit(&kedata, sizeof(kedata));
7246 	if (vsi->netdev->ethtool_ops->get_eee)
7247 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7248 
7249 	if (!linkmode_empty(kedata.supported))
7250 		netdev_info(vsi->netdev,
7251 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7252 			    speed, fc,
7253 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7254 	else
7255 		netdev_info(vsi->netdev,
7256 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7257 			    speed, fc);
7258 }
7259 
7260 /**
7261  * i40e_print_link_message - print link up or down
7262  * @vsi: the VSI for which link needs a message
7263  * @isup: true of link is up, false otherwise
7264  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7265 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7266 {
7267 	enum i40e_aq_link_speed new_speed;
7268 	struct i40e_pf *pf = vsi->back;
7269 	char *speed = "Unknown";
7270 	char *fc = "Unknown";
7271 	char *fec = "";
7272 	char *req_fec = "";
7273 	char *an = "";
7274 
7275 	if (isup)
7276 		new_speed = pf->hw.phy.link_info.link_speed;
7277 	else
7278 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7279 
7280 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7281 		return;
7282 	vsi->current_isup = isup;
7283 	vsi->current_speed = new_speed;
7284 	if (!isup) {
7285 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7286 		return;
7287 	}
7288 
7289 	/* Warn user if link speed on NPAR enabled partition is not at
7290 	 * least 10GB
7291 	 */
7292 	if (pf->hw.func_caps.npar_enable &&
7293 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7294 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7295 		netdev_warn(vsi->netdev,
7296 			    "The partition detected link speed that is less than 10Gbps\n");
7297 
7298 	switch (pf->hw.phy.link_info.link_speed) {
7299 	case I40E_LINK_SPEED_40GB:
7300 		speed = "40 G";
7301 		break;
7302 	case I40E_LINK_SPEED_20GB:
7303 		speed = "20 G";
7304 		break;
7305 	case I40E_LINK_SPEED_25GB:
7306 		speed = "25 G";
7307 		break;
7308 	case I40E_LINK_SPEED_10GB:
7309 		speed = "10 G";
7310 		break;
7311 	case I40E_LINK_SPEED_5GB:
7312 		speed = "5 G";
7313 		break;
7314 	case I40E_LINK_SPEED_2_5GB:
7315 		speed = "2.5 G";
7316 		break;
7317 	case I40E_LINK_SPEED_1GB:
7318 		speed = "1000 M";
7319 		break;
7320 	case I40E_LINK_SPEED_100MB:
7321 		speed = "100 M";
7322 		break;
7323 	default:
7324 		break;
7325 	}
7326 
7327 	switch (pf->hw.fc.current_mode) {
7328 	case I40E_FC_FULL:
7329 		fc = "RX/TX";
7330 		break;
7331 	case I40E_FC_TX_PAUSE:
7332 		fc = "TX";
7333 		break;
7334 	case I40E_FC_RX_PAUSE:
7335 		fc = "RX";
7336 		break;
7337 	default:
7338 		fc = "None";
7339 		break;
7340 	}
7341 
7342 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7343 		req_fec = "None";
7344 		fec = "None";
7345 		an = "False";
7346 
7347 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7348 			an = "True";
7349 
7350 		if (pf->hw.phy.link_info.fec_info &
7351 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7352 			fec = "CL74 FC-FEC/BASE-R";
7353 		else if (pf->hw.phy.link_info.fec_info &
7354 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7355 			fec = "CL108 RS-FEC";
7356 
7357 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7358 		 * both RS and FC are requested
7359 		 */
7360 		if (vsi->back->hw.phy.link_info.req_fec_info &
7361 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7362 			if (vsi->back->hw.phy.link_info.req_fec_info &
7363 			    I40E_AQ_REQUEST_FEC_RS)
7364 				req_fec = "CL108 RS-FEC";
7365 			else
7366 				req_fec = "CL74 FC-FEC/BASE-R";
7367 		}
7368 		netdev_info(vsi->netdev,
7369 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7370 			    speed, req_fec, fec, an, fc);
7371 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7372 		req_fec = "None";
7373 		fec = "None";
7374 		an = "False";
7375 
7376 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7377 			an = "True";
7378 
7379 		if (pf->hw.phy.link_info.fec_info &
7380 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7381 			fec = "CL74 FC-FEC/BASE-R";
7382 
7383 		if (pf->hw.phy.link_info.req_fec_info &
7384 		    I40E_AQ_REQUEST_FEC_KR)
7385 			req_fec = "CL74 FC-FEC/BASE-R";
7386 
7387 		netdev_info(vsi->netdev,
7388 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7389 			    speed, req_fec, fec, an, fc);
7390 	} else {
7391 		i40e_print_link_message_eee(vsi, speed, fc);
7392 	}
7393 
7394 }
7395 
7396 /**
7397  * i40e_up_complete - Finish the last steps of bringing up a connection
7398  * @vsi: the VSI being configured
7399  **/
i40e_up_complete(struct i40e_vsi * vsi)7400 static int i40e_up_complete(struct i40e_vsi *vsi)
7401 {
7402 	struct i40e_pf *pf = vsi->back;
7403 	int err;
7404 
7405 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7406 		i40e_vsi_configure_msix(vsi);
7407 	else
7408 		i40e_configure_msi_and_legacy(vsi);
7409 
7410 	/* start rings */
7411 	err = i40e_vsi_start_rings(vsi);
7412 	if (err)
7413 		return err;
7414 
7415 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7416 	i40e_napi_enable_all(vsi);
7417 	i40e_vsi_enable_irq(vsi);
7418 
7419 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7420 	    (vsi->netdev)) {
7421 		i40e_print_link_message(vsi, true);
7422 		netif_tx_start_all_queues(vsi->netdev);
7423 		netif_carrier_on(vsi->netdev);
7424 	}
7425 
7426 	/* replay FDIR SB filters */
7427 	if (vsi->type == I40E_VSI_FDIR) {
7428 		/* reset fd counters */
7429 		pf->fd_add_err = 0;
7430 		pf->fd_atr_cnt = 0;
7431 		i40e_fdir_filter_restore(vsi);
7432 	}
7433 
7434 	/* On the next run of the service_task, notify any clients of the new
7435 	 * opened netdev
7436 	 */
7437 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7438 	i40e_service_event_schedule(pf);
7439 
7440 	return 0;
7441 }
7442 
7443 /**
7444  * i40e_vsi_reinit_locked - Reset the VSI
7445  * @vsi: the VSI being configured
7446  *
7447  * Rebuild the ring structs after some configuration
7448  * has changed, e.g. MTU size.
7449  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7450 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7451 {
7452 	struct i40e_pf *pf = vsi->back;
7453 
7454 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7455 		usleep_range(1000, 2000);
7456 	i40e_down(vsi);
7457 
7458 	i40e_up(vsi);
7459 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7460 }
7461 
7462 /**
7463  * i40e_force_link_state - Force the link status
7464  * @pf: board private structure
7465  * @is_up: whether the link state should be forced up or down
7466  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7467 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7468 {
7469 	struct i40e_aq_get_phy_abilities_resp abilities;
7470 	struct i40e_aq_set_phy_config config = {0};
7471 	bool non_zero_phy_type = is_up;
7472 	struct i40e_hw *hw = &pf->hw;
7473 	u64 mask;
7474 	u8 speed;
7475 	int err;
7476 
7477 	/* Card might've been put in an unstable state by other drivers
7478 	 * and applications, which causes incorrect speed values being
7479 	 * set on startup. In order to clear speed registers, we call
7480 	 * get_phy_capabilities twice, once to get initial state of
7481 	 * available speeds, and once to get current PHY config.
7482 	 */
7483 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7484 					   NULL);
7485 	if (err) {
7486 		dev_err(&pf->pdev->dev,
7487 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7488 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7489 		return err;
7490 	}
7491 	speed = abilities.link_speed;
7492 
7493 	/* Get the current phy config */
7494 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7495 					   NULL);
7496 	if (err) {
7497 		dev_err(&pf->pdev->dev,
7498 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7499 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7500 		return err;
7501 	}
7502 
7503 	/* If link needs to go up, but was not forced to go down,
7504 	 * and its speed values are OK, no need for a flap
7505 	 * if non_zero_phy_type was set, still need to force up
7506 	 */
7507 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7508 		non_zero_phy_type = true;
7509 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7510 		return 0;
7511 
7512 	/* To force link we need to set bits for all supported PHY types,
7513 	 * but there are now more than 32, so we need to split the bitmap
7514 	 * across two fields.
7515 	 */
7516 	mask = I40E_PHY_TYPES_BITMASK;
7517 	config.phy_type =
7518 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7519 	config.phy_type_ext =
7520 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7521 	/* Copy the old settings, except of phy_type */
7522 	config.abilities = abilities.abilities;
7523 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7524 		if (is_up)
7525 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7526 		else
7527 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7528 	}
7529 	if (abilities.link_speed != 0)
7530 		config.link_speed = abilities.link_speed;
7531 	else
7532 		config.link_speed = speed;
7533 	config.eee_capability = abilities.eee_capability;
7534 	config.eeer = abilities.eeer_val;
7535 	config.low_power_ctrl = abilities.d3_lpan;
7536 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7537 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7538 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7539 
7540 	if (err) {
7541 		dev_err(&pf->pdev->dev,
7542 			"set phy config ret =  %pe last_status =  %s\n",
7543 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7544 		return err;
7545 	}
7546 
7547 	/* Update the link info */
7548 	err = i40e_update_link_info(hw);
7549 	if (err) {
7550 		/* Wait a little bit (on 40G cards it sometimes takes a really
7551 		 * long time for link to come back from the atomic reset)
7552 		 * and try once more
7553 		 */
7554 		msleep(1000);
7555 		i40e_update_link_info(hw);
7556 	}
7557 
7558 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7559 
7560 	return 0;
7561 }
7562 
7563 /**
7564  * i40e_up - Bring the connection back up after being down
7565  * @vsi: the VSI being configured
7566  **/
i40e_up(struct i40e_vsi * vsi)7567 int i40e_up(struct i40e_vsi *vsi)
7568 {
7569 	int err;
7570 
7571 	if (vsi->type == I40E_VSI_MAIN &&
7572 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7573 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7574 		i40e_force_link_state(vsi->back, true);
7575 
7576 	err = i40e_vsi_configure(vsi);
7577 	if (!err)
7578 		err = i40e_up_complete(vsi);
7579 
7580 	return err;
7581 }
7582 
7583 /**
7584  * i40e_down - Shutdown the connection processing
7585  * @vsi: the VSI being stopped
7586  **/
i40e_down(struct i40e_vsi * vsi)7587 void i40e_down(struct i40e_vsi *vsi)
7588 {
7589 	int i;
7590 
7591 	/* It is assumed that the caller of this function
7592 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7593 	 */
7594 	if (vsi->netdev) {
7595 		netif_carrier_off(vsi->netdev);
7596 		netif_tx_disable(vsi->netdev);
7597 	}
7598 	i40e_vsi_disable_irq(vsi);
7599 	i40e_vsi_stop_rings(vsi);
7600 	if (vsi->type == I40E_VSI_MAIN &&
7601 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7602 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7603 		i40e_force_link_state(vsi->back, false);
7604 	i40e_napi_disable_all(vsi);
7605 
7606 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7607 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7608 		if (i40e_enabled_xdp_vsi(vsi)) {
7609 			/* Make sure that in-progress ndo_xdp_xmit and
7610 			 * ndo_xsk_wakeup calls are completed.
7611 			 */
7612 			synchronize_rcu();
7613 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7614 		}
7615 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7616 	}
7617 
7618 }
7619 
7620 /**
7621  * i40e_validate_mqprio_qopt- validate queue mapping info
7622  * @vsi: the VSI being configured
7623  * @mqprio_qopt: queue parametrs
7624  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7625 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7626 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7627 {
7628 	u64 sum_max_rate = 0;
7629 	u64 max_rate = 0;
7630 	int i;
7631 
7632 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7633 	    mqprio_qopt->qopt.num_tc < 1 ||
7634 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7635 		return -EINVAL;
7636 	for (i = 0; ; i++) {
7637 		if (!mqprio_qopt->qopt.count[i])
7638 			return -EINVAL;
7639 		if (mqprio_qopt->min_rate[i]) {
7640 			dev_err(&vsi->back->pdev->dev,
7641 				"Invalid min tx rate (greater than 0) specified\n");
7642 			return -EINVAL;
7643 		}
7644 		max_rate = mqprio_qopt->max_rate[i];
7645 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7646 		sum_max_rate += max_rate;
7647 
7648 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7649 			break;
7650 		if (mqprio_qopt->qopt.offset[i + 1] !=
7651 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7652 			return -EINVAL;
7653 	}
7654 	if (vsi->num_queue_pairs <
7655 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7656 		dev_err(&vsi->back->pdev->dev,
7657 			"Failed to create traffic channel, insufficient number of queues.\n");
7658 		return -EINVAL;
7659 	}
7660 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7661 		dev_err(&vsi->back->pdev->dev,
7662 			"Invalid max tx rate specified\n");
7663 		return -EINVAL;
7664 	}
7665 	return 0;
7666 }
7667 
7668 /**
7669  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7670  * @vsi: the VSI being configured
7671  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7672 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7673 {
7674 	u16 qcount;
7675 	int i;
7676 
7677 	/* Only TC0 is enabled */
7678 	vsi->tc_config.numtc = 1;
7679 	vsi->tc_config.enabled_tc = 1;
7680 	qcount = min_t(int, vsi->alloc_queue_pairs,
7681 		       i40e_pf_get_max_q_per_tc(vsi->back));
7682 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7683 		/* For the TC that is not enabled set the offset to default
7684 		 * queue and allocate one queue for the given TC.
7685 		 */
7686 		vsi->tc_config.tc_info[i].qoffset = 0;
7687 		if (i == 0)
7688 			vsi->tc_config.tc_info[i].qcount = qcount;
7689 		else
7690 			vsi->tc_config.tc_info[i].qcount = 1;
7691 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7692 	}
7693 }
7694 
7695 /**
7696  * i40e_del_macvlan_filter
7697  * @hw: pointer to the HW structure
7698  * @seid: seid of the channel VSI
7699  * @macaddr: the mac address to apply as a filter
7700  * @aq_err: store the admin Q error
7701  *
7702  * This function deletes a mac filter on the channel VSI which serves as the
7703  * macvlan. Returns 0 on success.
7704  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7705 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7706 				   const u8 *macaddr, int *aq_err)
7707 {
7708 	struct i40e_aqc_remove_macvlan_element_data element;
7709 	int status;
7710 
7711 	memset(&element, 0, sizeof(element));
7712 	ether_addr_copy(element.mac_addr, macaddr);
7713 	element.vlan_tag = 0;
7714 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7715 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7716 	*aq_err = hw->aq.asq_last_status;
7717 
7718 	return status;
7719 }
7720 
7721 /**
7722  * i40e_add_macvlan_filter
7723  * @hw: pointer to the HW structure
7724  * @seid: seid of the channel VSI
7725  * @macaddr: the mac address to apply as a filter
7726  * @aq_err: store the admin Q error
7727  *
7728  * This function adds a mac filter on the channel VSI which serves as the
7729  * macvlan. Returns 0 on success.
7730  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7731 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7732 				   const u8 *macaddr, int *aq_err)
7733 {
7734 	struct i40e_aqc_add_macvlan_element_data element;
7735 	u16 cmd_flags = 0;
7736 	int status;
7737 
7738 	ether_addr_copy(element.mac_addr, macaddr);
7739 	element.vlan_tag = 0;
7740 	element.queue_number = 0;
7741 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7742 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7743 	element.flags = cpu_to_le16(cmd_flags);
7744 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7745 	*aq_err = hw->aq.asq_last_status;
7746 
7747 	return status;
7748 }
7749 
7750 /**
7751  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7752  * @vsi: the VSI we want to access
7753  * @ch: the channel we want to access
7754  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7755 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7756 {
7757 	struct i40e_ring *tx_ring, *rx_ring;
7758 	u16 pf_q;
7759 	int i;
7760 
7761 	for (i = 0; i < ch->num_queue_pairs; i++) {
7762 		pf_q = ch->base_queue + i;
7763 		tx_ring = vsi->tx_rings[pf_q];
7764 		tx_ring->ch = NULL;
7765 		rx_ring = vsi->rx_rings[pf_q];
7766 		rx_ring->ch = NULL;
7767 	}
7768 }
7769 
7770 /**
7771  * i40e_free_macvlan_channels
7772  * @vsi: the VSI we want to access
7773  *
7774  * This function frees the Qs of the channel VSI from
7775  * the stack and also deletes the channel VSIs which
7776  * serve as macvlans.
7777  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7778 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7779 {
7780 	struct i40e_channel *ch, *ch_tmp;
7781 	int ret;
7782 
7783 	if (list_empty(&vsi->macvlan_list))
7784 		return;
7785 
7786 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7787 		struct i40e_vsi *parent_vsi;
7788 
7789 		if (i40e_is_channel_macvlan(ch)) {
7790 			i40e_reset_ch_rings(vsi, ch);
7791 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7792 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7793 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7794 			kfree(ch->fwd);
7795 			ch->fwd = NULL;
7796 		}
7797 
7798 		list_del(&ch->list);
7799 		parent_vsi = ch->parent_vsi;
7800 		if (!parent_vsi || !ch->initialized) {
7801 			kfree(ch);
7802 			continue;
7803 		}
7804 
7805 		/* remove the VSI */
7806 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7807 					     NULL);
7808 		if (ret)
7809 			dev_err(&vsi->back->pdev->dev,
7810 				"unable to remove channel (%d) for parent VSI(%d)\n",
7811 				ch->seid, parent_vsi->seid);
7812 		kfree(ch);
7813 	}
7814 	vsi->macvlan_cnt = 0;
7815 }
7816 
7817 /**
7818  * i40e_fwd_ring_up - bring the macvlan device up
7819  * @vsi: the VSI we want to access
7820  * @vdev: macvlan netdevice
7821  * @fwd: the private fwd structure
7822  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7823 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7824 			    struct i40e_fwd_adapter *fwd)
7825 {
7826 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7827 	int ret = 0, num_tc = 1,  i, aq_err;
7828 	struct i40e_pf *pf = vsi->back;
7829 	struct i40e_hw *hw = &pf->hw;
7830 
7831 	/* Go through the list and find an available channel */
7832 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7833 		if (!i40e_is_channel_macvlan(iter)) {
7834 			iter->fwd = fwd;
7835 			/* record configuration for macvlan interface in vdev */
7836 			for (i = 0; i < num_tc; i++)
7837 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7838 							     i,
7839 							     iter->num_queue_pairs,
7840 							     iter->base_queue);
7841 			for (i = 0; i < iter->num_queue_pairs; i++) {
7842 				struct i40e_ring *tx_ring, *rx_ring;
7843 				u16 pf_q;
7844 
7845 				pf_q = iter->base_queue + i;
7846 
7847 				/* Get to TX ring ptr */
7848 				tx_ring = vsi->tx_rings[pf_q];
7849 				tx_ring->ch = iter;
7850 
7851 				/* Get the RX ring ptr */
7852 				rx_ring = vsi->rx_rings[pf_q];
7853 				rx_ring->ch = iter;
7854 			}
7855 			ch = iter;
7856 			break;
7857 		}
7858 	}
7859 
7860 	if (!ch)
7861 		return -EINVAL;
7862 
7863 	/* Guarantee all rings are updated before we update the
7864 	 * MAC address filter.
7865 	 */
7866 	wmb();
7867 
7868 	/* Add a mac filter */
7869 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7870 	if (ret) {
7871 		/* if we cannot add the MAC rule then disable the offload */
7872 		macvlan_release_l2fw_offload(vdev);
7873 		for (i = 0; i < ch->num_queue_pairs; i++) {
7874 			struct i40e_ring *rx_ring;
7875 			u16 pf_q;
7876 
7877 			pf_q = ch->base_queue + i;
7878 			rx_ring = vsi->rx_rings[pf_q];
7879 			rx_ring->netdev = NULL;
7880 		}
7881 		dev_info(&pf->pdev->dev,
7882 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7883 			  ERR_PTR(ret), libie_aq_str(aq_err));
7884 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7885 	}
7886 
7887 	return ret;
7888 }
7889 
7890 /**
7891  * i40e_setup_macvlans - create the channels which will be macvlans
7892  * @vsi: the VSI we want to access
7893  * @macvlan_cnt: no. of macvlans to be setup
7894  * @qcnt: no. of Qs per macvlan
7895  * @vdev: macvlan netdevice
7896  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7897 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7898 			       struct net_device *vdev)
7899 {
7900 	struct i40e_pf *pf = vsi->back;
7901 	struct i40e_hw *hw = &pf->hw;
7902 	struct i40e_vsi_context ctxt;
7903 	u16 sections, qmap, num_qps;
7904 	struct i40e_channel *ch;
7905 	int i, pow, ret = 0;
7906 	u8 offset = 0;
7907 
7908 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7909 		return -EINVAL;
7910 
7911 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7912 
7913 	/* find the next higher power-of-2 of num queue pairs */
7914 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7915 
7916 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7917 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7918 
7919 	/* Setup context bits for the main VSI */
7920 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7921 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7922 	memset(&ctxt, 0, sizeof(ctxt));
7923 	ctxt.seid = vsi->seid;
7924 	ctxt.pf_num = vsi->back->hw.pf_id;
7925 	ctxt.vf_num = 0;
7926 	ctxt.uplink_seid = vsi->uplink_seid;
7927 	ctxt.info = vsi->info;
7928 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7929 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7930 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7931 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7932 
7933 	/* Reconfigure RSS for main VSI with new max queue count */
7934 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7935 	ret = i40e_vsi_config_rss(vsi);
7936 	if (ret) {
7937 		dev_info(&pf->pdev->dev,
7938 			 "Failed to reconfig RSS for num_queues (%u)\n",
7939 			 vsi->rss_size);
7940 		return ret;
7941 	}
7942 	vsi->reconfig_rss = true;
7943 	dev_dbg(&vsi->back->pdev->dev,
7944 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7945 	vsi->next_base_queue = num_qps;
7946 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7947 
7948 	/* Update the VSI after updating the VSI queue-mapping
7949 	 * information
7950 	 */
7951 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7952 	if (ret) {
7953 		dev_info(&pf->pdev->dev,
7954 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7955 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
7956 		return ret;
7957 	}
7958 	/* update the local VSI info with updated queue map */
7959 	i40e_vsi_update_queue_map(vsi, &ctxt);
7960 	vsi->info.valid_sections = 0;
7961 
7962 	/* Create channels for macvlans */
7963 	INIT_LIST_HEAD(&vsi->macvlan_list);
7964 	for (i = 0; i < macvlan_cnt; i++) {
7965 		ch = kzalloc_obj(*ch);
7966 		if (!ch) {
7967 			ret = -ENOMEM;
7968 			goto err_free;
7969 		}
7970 		INIT_LIST_HEAD(&ch->list);
7971 		ch->num_queue_pairs = qcnt;
7972 		if (!i40e_setup_channel(pf, vsi, ch)) {
7973 			ret = -EINVAL;
7974 			kfree(ch);
7975 			goto err_free;
7976 		}
7977 		ch->parent_vsi = vsi;
7978 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7979 		vsi->macvlan_cnt++;
7980 		list_add_tail(&ch->list, &vsi->macvlan_list);
7981 	}
7982 
7983 	return ret;
7984 
7985 err_free:
7986 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7987 	i40e_free_macvlan_channels(vsi);
7988 
7989 	return ret;
7990 }
7991 
7992 /**
7993  * i40e_fwd_add - configure macvlans
7994  * @netdev: net device to configure
7995  * @vdev: macvlan netdevice
7996  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7997 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7998 {
7999 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8000 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
8001 	struct i40e_vsi *vsi = np->vsi;
8002 	struct i40e_pf *pf = vsi->back;
8003 	struct i40e_fwd_adapter *fwd;
8004 	int avail_macvlan, ret;
8005 
8006 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8007 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8008 		return ERR_PTR(-EINVAL);
8009 	}
8010 	if (i40e_is_tc_mqprio_enabled(pf)) {
8011 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8012 		return ERR_PTR(-EINVAL);
8013 	}
8014 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8015 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8016 		return ERR_PTR(-EINVAL);
8017 	}
8018 
8019 	/* The macvlan device has to be a single Q device so that the
8020 	 * tc_to_txq field can be reused to pick the tx queue.
8021 	 */
8022 	if (netif_is_multiqueue(vdev))
8023 		return ERR_PTR(-ERANGE);
8024 
8025 	if (!vsi->macvlan_cnt) {
8026 		/* reserve bit 0 for the pf device */
8027 		set_bit(0, vsi->fwd_bitmask);
8028 
8029 		/* Try to reserve as many queues as possible for macvlans. First
8030 		 * reserve 3/4th of max vectors, then half, then quarter and
8031 		 * calculate Qs per macvlan as you go
8032 		 */
8033 		vectors = pf->num_lan_msix;
8034 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8035 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8036 			q_per_macvlan = 4;
8037 			macvlan_cnt = (vectors - 32) / 4;
8038 		} else if (vectors <= 64 && vectors > 32) {
8039 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8040 			q_per_macvlan = 2;
8041 			macvlan_cnt = (vectors - 16) / 2;
8042 		} else if (vectors <= 32 && vectors > 16) {
8043 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8044 			q_per_macvlan = 1;
8045 			macvlan_cnt = vectors - 16;
8046 		} else if (vectors <= 16 && vectors > 8) {
8047 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8048 			q_per_macvlan = 1;
8049 			macvlan_cnt = vectors - 8;
8050 		} else {
8051 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8052 			q_per_macvlan = 1;
8053 			macvlan_cnt = vectors - 1;
8054 		}
8055 
8056 		if (macvlan_cnt == 0)
8057 			return ERR_PTR(-EBUSY);
8058 
8059 		/* Quiesce VSI queues */
8060 		i40e_quiesce_vsi(vsi);
8061 
8062 		/* sets up the macvlans but does not "enable" them */
8063 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8064 					  vdev);
8065 		if (ret)
8066 			return ERR_PTR(ret);
8067 
8068 		/* Unquiesce VSI */
8069 		i40e_unquiesce_vsi(vsi);
8070 	}
8071 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8072 					    vsi->macvlan_cnt);
8073 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8074 		return ERR_PTR(-EBUSY);
8075 
8076 	/* create the fwd struct */
8077 	fwd = kzalloc_obj(*fwd);
8078 	if (!fwd)
8079 		return ERR_PTR(-ENOMEM);
8080 
8081 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8082 	fwd->bit_no = avail_macvlan;
8083 	netdev_set_sb_channel(vdev, avail_macvlan);
8084 	fwd->netdev = vdev;
8085 
8086 	if (!netif_running(netdev))
8087 		return fwd;
8088 
8089 	/* Set fwd ring up */
8090 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8091 	if (ret) {
8092 		/* unbind the queues and drop the subordinate channel config */
8093 		netdev_unbind_sb_channel(netdev, vdev);
8094 		netdev_set_sb_channel(vdev, 0);
8095 
8096 		kfree(fwd);
8097 		return ERR_PTR(-EINVAL);
8098 	}
8099 
8100 	return fwd;
8101 }
8102 
8103 /**
8104  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8105  * @vsi: the VSI we want to access
8106  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8107 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8108 {
8109 	struct i40e_channel *ch, *ch_tmp;
8110 	struct i40e_pf *pf = vsi->back;
8111 	struct i40e_hw *hw = &pf->hw;
8112 	int aq_err, ret = 0;
8113 
8114 	if (list_empty(&vsi->macvlan_list))
8115 		return;
8116 
8117 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8118 		if (i40e_is_channel_macvlan(ch)) {
8119 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8120 						      i40e_channel_mac(ch),
8121 						      &aq_err);
8122 			if (!ret) {
8123 				/* Reset queue contexts */
8124 				i40e_reset_ch_rings(vsi, ch);
8125 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8126 				netdev_unbind_sb_channel(vsi->netdev,
8127 							 ch->fwd->netdev);
8128 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8129 				kfree(ch->fwd);
8130 				ch->fwd = NULL;
8131 			}
8132 		}
8133 	}
8134 }
8135 
8136 /**
8137  * i40e_fwd_del - delete macvlan interfaces
8138  * @netdev: net device to configure
8139  * @vdev: macvlan netdevice
8140  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8141 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8142 {
8143 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8144 	struct i40e_fwd_adapter *fwd = vdev;
8145 	struct i40e_channel *ch, *ch_tmp;
8146 	struct i40e_vsi *vsi = np->vsi;
8147 	struct i40e_pf *pf = vsi->back;
8148 	struct i40e_hw *hw = &pf->hw;
8149 	int aq_err, ret = 0;
8150 
8151 	/* Find the channel associated with the macvlan and del mac filter */
8152 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8153 		if (i40e_is_channel_macvlan(ch) &&
8154 		    ether_addr_equal(i40e_channel_mac(ch),
8155 				     fwd->netdev->dev_addr)) {
8156 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8157 						      i40e_channel_mac(ch),
8158 						      &aq_err);
8159 			if (!ret) {
8160 				/* Reset queue contexts */
8161 				i40e_reset_ch_rings(vsi, ch);
8162 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8163 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8164 				netdev_set_sb_channel(fwd->netdev, 0);
8165 				kfree(ch->fwd);
8166 				ch->fwd = NULL;
8167 			} else {
8168 				dev_info(&pf->pdev->dev,
8169 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8170 					  ERR_PTR(ret), libie_aq_str(aq_err));
8171 			}
8172 			break;
8173 		}
8174 	}
8175 }
8176 
8177 /**
8178  * i40e_setup_tc - configure multiple traffic classes
8179  * @netdev: net device to configure
8180  * @type_data: tc offload data
8181  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8182 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8183 {
8184 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8185 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8186 	struct i40e_vsi *vsi = np->vsi;
8187 	struct i40e_pf *pf = vsi->back;
8188 	u8 enabled_tc = 0, num_tc, hw;
8189 	bool need_reset = false;
8190 	int old_queue_pairs;
8191 	int ret = -EINVAL;
8192 	u16 mode;
8193 	int i;
8194 
8195 	old_queue_pairs = vsi->num_queue_pairs;
8196 	num_tc = mqprio_qopt->qopt.num_tc;
8197 	hw = mqprio_qopt->qopt.hw;
8198 	mode = mqprio_qopt->mode;
8199 	if (!hw) {
8200 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8201 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8202 		goto config_tc;
8203 	}
8204 
8205 	/* Check if MFP enabled */
8206 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8207 		netdev_info(netdev,
8208 			    "Configuring TC not supported in MFP mode\n");
8209 		return ret;
8210 	}
8211 	switch (mode) {
8212 	case TC_MQPRIO_MODE_DCB:
8213 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8214 
8215 		/* Check if DCB enabled to continue */
8216 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8217 			netdev_info(netdev,
8218 				    "DCB is not enabled for adapter\n");
8219 			return ret;
8220 		}
8221 
8222 		/* Check whether tc count is within enabled limit */
8223 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8224 			netdev_info(netdev,
8225 				    "TC count greater than enabled on link for adapter\n");
8226 			return ret;
8227 		}
8228 		break;
8229 	case TC_MQPRIO_MODE_CHANNEL:
8230 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8231 			netdev_info(netdev,
8232 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8233 			return ret;
8234 		}
8235 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8236 			return ret;
8237 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8238 		if (ret)
8239 			return ret;
8240 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8241 		       sizeof(*mqprio_qopt));
8242 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8243 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8244 		break;
8245 	default:
8246 		return -EINVAL;
8247 	}
8248 
8249 config_tc:
8250 	/* Generate TC map for number of tc requested */
8251 	for (i = 0; i < num_tc; i++)
8252 		enabled_tc |= BIT(i);
8253 
8254 	/* Requesting same TC configuration as already enabled */
8255 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8256 	    mode != TC_MQPRIO_MODE_CHANNEL)
8257 		return 0;
8258 
8259 	/* Quiesce VSI queues */
8260 	i40e_quiesce_vsi(vsi);
8261 
8262 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8263 		i40e_remove_queue_channels(vsi);
8264 
8265 	/* Configure VSI for enabled TCs */
8266 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8267 	if (ret) {
8268 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8269 			    vsi->seid);
8270 		need_reset = true;
8271 		goto exit;
8272 	} else if (enabled_tc &&
8273 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8274 		netdev_info(netdev,
8275 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8276 			    vsi->tc_config.tc_info[0].qcount);
8277 		ret = -EINVAL;
8278 		need_reset = true;
8279 		goto exit;
8280 	}
8281 
8282 	dev_info(&vsi->back->pdev->dev,
8283 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8284 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8285 
8286 	if (i40e_is_tc_mqprio_enabled(pf)) {
8287 		if (vsi->mqprio_qopt.max_rate[0]) {
8288 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8289 						  vsi->mqprio_qopt.max_rate[0]);
8290 
8291 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8292 			if (!ret) {
8293 				u64 credits = max_tx_rate;
8294 
8295 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8296 				dev_dbg(&vsi->back->pdev->dev,
8297 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8298 					max_tx_rate,
8299 					credits,
8300 					vsi->seid);
8301 			} else {
8302 				need_reset = true;
8303 				goto exit;
8304 			}
8305 		}
8306 		ret = i40e_configure_queue_channels(vsi);
8307 		if (ret) {
8308 			vsi->num_queue_pairs = old_queue_pairs;
8309 			netdev_info(netdev,
8310 				    "Failed configuring queue channels\n");
8311 			need_reset = true;
8312 			goto exit;
8313 		}
8314 	}
8315 
8316 exit:
8317 	/* Reset the configuration data to defaults, only TC0 is enabled */
8318 	if (need_reset) {
8319 		i40e_vsi_set_default_tc_config(vsi);
8320 		need_reset = false;
8321 	}
8322 
8323 	/* Unquiesce VSI */
8324 	i40e_unquiesce_vsi(vsi);
8325 	return ret;
8326 }
8327 
8328 /**
8329  * i40e_set_cld_element - sets cloud filter element data
8330  * @filter: cloud filter rule
8331  * @cld: ptr to cloud filter element data
8332  *
8333  * This is helper function to copy data into cloud filter element
8334  **/
8335 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8336 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8337 		     struct i40e_aqc_cloud_filters_element_data *cld)
8338 {
8339 	u32 ipa;
8340 	int i;
8341 
8342 	memset(cld, 0, sizeof(*cld));
8343 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8344 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8345 
8346 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8347 		return;
8348 
8349 	if (filter->n_proto == ETH_P_IPV6) {
8350 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8351 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8352 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8353 
8354 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8355 		}
8356 	} else {
8357 		ipa = be32_to_cpu(filter->dst_ipv4);
8358 
8359 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8360 	}
8361 
8362 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8363 
8364 	/* tenant_id is not supported by FW now, once the support is enabled
8365 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8366 	 */
8367 	if (filter->tenant_id)
8368 		return;
8369 }
8370 
8371 /**
8372  * i40e_add_del_cloud_filter - Add/del cloud filter
8373  * @vsi: pointer to VSI
8374  * @filter: cloud filter rule
8375  * @add: if true, add, if false, delete
8376  *
8377  * Add or delete a cloud filter for a specific flow spec.
8378  * Returns 0 if the filter were successfully added.
8379  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8380 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8381 			      struct i40e_cloud_filter *filter, bool add)
8382 {
8383 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8384 	struct i40e_pf *pf = vsi->back;
8385 	int ret;
8386 	static const u16 flag_table[128] = {
8387 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8388 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8389 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8390 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8391 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8392 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8393 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8394 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8395 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8396 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8397 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8398 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8399 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8400 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8401 	};
8402 
8403 	if (filter->flags >= ARRAY_SIZE(flag_table))
8404 		return -EIO;
8405 
8406 	memset(&cld_filter, 0, sizeof(cld_filter));
8407 
8408 	/* copy element needed to add cloud filter from filter */
8409 	i40e_set_cld_element(filter, &cld_filter);
8410 
8411 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8412 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8413 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8414 
8415 	if (filter->n_proto == ETH_P_IPV6)
8416 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8417 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8418 	else
8419 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8420 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8421 
8422 	if (add)
8423 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8424 						&cld_filter, 1);
8425 	else
8426 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8427 						&cld_filter, 1);
8428 	if (ret)
8429 		dev_dbg(&pf->pdev->dev,
8430 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8431 			add ? "add" : "delete", filter->dst_port, ret,
8432 			pf->hw.aq.asq_last_status);
8433 	else
8434 		dev_info(&pf->pdev->dev,
8435 			 "%s cloud filter for VSI: %d\n",
8436 			 add ? "Added" : "Deleted", filter->seid);
8437 	return ret;
8438 }
8439 
8440 /**
8441  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8442  * @vsi: pointer to VSI
8443  * @filter: cloud filter rule
8444  * @add: if true, add, if false, delete
8445  *
8446  * Add or delete a cloud filter for a specific flow spec using big buffer.
8447  * Returns 0 if the filter were successfully added.
8448  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8449 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8450 				      struct i40e_cloud_filter *filter,
8451 				      bool add)
8452 {
8453 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8454 	struct i40e_pf *pf = vsi->back;
8455 	int ret;
8456 
8457 	/* Both (src/dst) valid mac_addr are not supported */
8458 	if ((is_valid_ether_addr(filter->dst_mac) &&
8459 	     is_valid_ether_addr(filter->src_mac)) ||
8460 	    (is_multicast_ether_addr(filter->dst_mac) &&
8461 	     is_multicast_ether_addr(filter->src_mac)))
8462 		return -EOPNOTSUPP;
8463 
8464 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8465 	 * ports are not supported via big buffer now.
8466 	 */
8467 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8468 		return -EOPNOTSUPP;
8469 
8470 	/* adding filter using src_port/src_ip is not supported at this stage */
8471 	if (filter->src_port ||
8472 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8473 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8474 		return -EOPNOTSUPP;
8475 
8476 	memset(&cld_filter, 0, sizeof(cld_filter));
8477 
8478 	/* copy element needed to add cloud filter from filter */
8479 	i40e_set_cld_element(filter, &cld_filter.element);
8480 
8481 	if (is_valid_ether_addr(filter->dst_mac) ||
8482 	    is_valid_ether_addr(filter->src_mac) ||
8483 	    is_multicast_ether_addr(filter->dst_mac) ||
8484 	    is_multicast_ether_addr(filter->src_mac)) {
8485 		/* MAC + IP : unsupported mode */
8486 		if (filter->dst_ipv4)
8487 			return -EOPNOTSUPP;
8488 
8489 		/* since we validated that L4 port must be valid before
8490 		 * we get here, start with respective "flags" value
8491 		 * and update if vlan is present or not
8492 		 */
8493 		cld_filter.element.flags =
8494 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8495 
8496 		if (filter->vlan_id) {
8497 			cld_filter.element.flags =
8498 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8499 		}
8500 
8501 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8502 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8503 		cld_filter.element.flags =
8504 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8505 		if (filter->n_proto == ETH_P_IPV6)
8506 			cld_filter.element.flags |=
8507 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8508 		else
8509 			cld_filter.element.flags |=
8510 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8511 	} else {
8512 		dev_err(&pf->pdev->dev,
8513 			"either mac or ip has to be valid for cloud filter\n");
8514 		return -EINVAL;
8515 	}
8516 
8517 	/* Now copy L4 port in Byte 6..7 in general fields */
8518 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8519 						be16_to_cpu(filter->dst_port);
8520 
8521 	if (add) {
8522 		/* Validate current device switch mode, change if necessary */
8523 		ret = i40e_validate_and_set_switch_mode(vsi);
8524 		if (ret) {
8525 			dev_err(&pf->pdev->dev,
8526 				"failed to set switch mode, ret %d\n",
8527 				ret);
8528 			return ret;
8529 		}
8530 
8531 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8532 						   &cld_filter, 1);
8533 	} else {
8534 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8535 						   &cld_filter, 1);
8536 	}
8537 
8538 	if (ret)
8539 		dev_dbg(&pf->pdev->dev,
8540 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8541 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8542 	else
8543 		dev_info(&pf->pdev->dev,
8544 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8545 			 add ? "add" : "delete", filter->seid,
8546 			 ntohs(filter->dst_port));
8547 	return ret;
8548 }
8549 
8550 /**
8551  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8552  * @vsi: Pointer to VSI
8553  * @f: Pointer to struct flow_cls_offload
8554  * @filter: Pointer to cloud filter structure
8555  *
8556  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8557 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8558 				 struct flow_cls_offload *f,
8559 				 struct i40e_cloud_filter *filter)
8560 {
8561 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8562 	struct flow_dissector *dissector = rule->match.dissector;
8563 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8564 	struct i40e_pf *pf = vsi->back;
8565 	u8 field_flags = 0;
8566 
8567 	if (dissector->used_keys &
8568 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8569 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8570 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8571 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8572 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8573 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8574 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8575 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8576 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8577 			dissector->used_keys);
8578 		return -EOPNOTSUPP;
8579 	}
8580 
8581 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8582 		struct flow_match_enc_keyid match;
8583 
8584 		flow_rule_match_enc_keyid(rule, &match);
8585 		if (match.mask->keyid != 0)
8586 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8587 
8588 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8589 	}
8590 
8591 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8592 		struct flow_match_basic match;
8593 
8594 		flow_rule_match_basic(rule, &match);
8595 		n_proto_key = ntohs(match.key->n_proto);
8596 		n_proto_mask = ntohs(match.mask->n_proto);
8597 
8598 		if (n_proto_key == ETH_P_ALL) {
8599 			n_proto_key = 0;
8600 			n_proto_mask = 0;
8601 		}
8602 		filter->n_proto = n_proto_key & n_proto_mask;
8603 		filter->ip_proto = match.key->ip_proto;
8604 	}
8605 
8606 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8607 		struct flow_match_eth_addrs match;
8608 
8609 		flow_rule_match_eth_addrs(rule, &match);
8610 
8611 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8612 		if (!is_zero_ether_addr(match.mask->dst)) {
8613 			if (is_broadcast_ether_addr(match.mask->dst)) {
8614 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8615 			} else {
8616 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8617 					match.mask->dst);
8618 				return -EIO;
8619 			}
8620 		}
8621 
8622 		if (!is_zero_ether_addr(match.mask->src)) {
8623 			if (is_broadcast_ether_addr(match.mask->src)) {
8624 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8625 			} else {
8626 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8627 					match.mask->src);
8628 				return -EIO;
8629 			}
8630 		}
8631 		ether_addr_copy(filter->dst_mac, match.key->dst);
8632 		ether_addr_copy(filter->src_mac, match.key->src);
8633 	}
8634 
8635 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8636 		struct flow_match_vlan match;
8637 
8638 		flow_rule_match_vlan(rule, &match);
8639 		if (match.mask->vlan_id) {
8640 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8641 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8642 
8643 			} else {
8644 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8645 					match.mask->vlan_id);
8646 				return -EIO;
8647 			}
8648 		}
8649 
8650 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8651 	}
8652 
8653 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8654 		struct flow_match_control match;
8655 
8656 		flow_rule_match_control(rule, &match);
8657 		addr_type = match.key->addr_type;
8658 
8659 		if (flow_rule_has_control_flags(match.mask->flags,
8660 						f->common.extack))
8661 			return -EOPNOTSUPP;
8662 	}
8663 
8664 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8665 		struct flow_match_ipv4_addrs match;
8666 
8667 		flow_rule_match_ipv4_addrs(rule, &match);
8668 		if (match.mask->dst) {
8669 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8670 				field_flags |= I40E_CLOUD_FIELD_IIP;
8671 			} else {
8672 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8673 					&match.mask->dst);
8674 				return -EIO;
8675 			}
8676 		}
8677 
8678 		if (match.mask->src) {
8679 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8680 				field_flags |= I40E_CLOUD_FIELD_IIP;
8681 			} else {
8682 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8683 					&match.mask->src);
8684 				return -EIO;
8685 			}
8686 		}
8687 
8688 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8689 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8690 			return -EIO;
8691 		}
8692 		filter->dst_ipv4 = match.key->dst;
8693 		filter->src_ipv4 = match.key->src;
8694 	}
8695 
8696 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8697 		struct flow_match_ipv6_addrs match;
8698 
8699 		flow_rule_match_ipv6_addrs(rule, &match);
8700 
8701 		/* src and dest IPV6 address should not be LOOPBACK
8702 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8703 		 */
8704 		if (ipv6_addr_loopback(&match.key->dst) ||
8705 		    ipv6_addr_loopback(&match.key->src)) {
8706 			dev_err(&pf->pdev->dev,
8707 				"Bad ipv6, addr is LOOPBACK\n");
8708 			return -EIO;
8709 		}
8710 		if (!ipv6_addr_any(&match.mask->dst) ||
8711 		    !ipv6_addr_any(&match.mask->src))
8712 			field_flags |= I40E_CLOUD_FIELD_IIP;
8713 
8714 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8715 		       sizeof(filter->src_ipv6));
8716 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8717 		       sizeof(filter->dst_ipv6));
8718 	}
8719 
8720 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8721 		struct flow_match_ports match;
8722 
8723 		flow_rule_match_ports(rule, &match);
8724 		if (match.mask->src) {
8725 			if (match.mask->src == cpu_to_be16(0xffff)) {
8726 				field_flags |= I40E_CLOUD_FIELD_IIP;
8727 			} else {
8728 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8729 					be16_to_cpu(match.mask->src));
8730 				return -EIO;
8731 			}
8732 		}
8733 
8734 		if (match.mask->dst) {
8735 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8736 				field_flags |= I40E_CLOUD_FIELD_IIP;
8737 			} else {
8738 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8739 					be16_to_cpu(match.mask->dst));
8740 				return -EIO;
8741 			}
8742 		}
8743 
8744 		filter->dst_port = match.key->dst;
8745 		filter->src_port = match.key->src;
8746 
8747 		switch (filter->ip_proto) {
8748 		case IPPROTO_TCP:
8749 		case IPPROTO_UDP:
8750 			break;
8751 		default:
8752 			dev_err(&pf->pdev->dev,
8753 				"Only UDP and TCP transport are supported\n");
8754 			return -EINVAL;
8755 		}
8756 	}
8757 	filter->flags = field_flags;
8758 	return 0;
8759 }
8760 
8761 /**
8762  * i40e_handle_tclass: Forward to a traffic class on the device
8763  * @vsi: Pointer to VSI
8764  * @tc: traffic class index on the device
8765  * @filter: Pointer to cloud filter structure
8766  *
8767  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8768 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8769 			      struct i40e_cloud_filter *filter)
8770 {
8771 	struct i40e_channel *ch, *ch_tmp;
8772 
8773 	/* direct to a traffic class on the same device */
8774 	if (tc == 0) {
8775 		filter->seid = vsi->seid;
8776 		return 0;
8777 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8778 		if (!filter->dst_port) {
8779 			dev_err(&vsi->back->pdev->dev,
8780 				"Specify destination port to direct to traffic class that is not default\n");
8781 			return -EINVAL;
8782 		}
8783 		if (list_empty(&vsi->ch_list))
8784 			return -EINVAL;
8785 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8786 					 list) {
8787 			if (ch->seid == vsi->tc_seid_map[tc])
8788 				filter->seid = ch->seid;
8789 		}
8790 		return 0;
8791 	}
8792 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8793 	return -EINVAL;
8794 }
8795 
8796 /**
8797  * i40e_configure_clsflower - Configure tc flower filters
8798  * @vsi: Pointer to VSI
8799  * @cls_flower: Pointer to struct flow_cls_offload
8800  *
8801  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8802 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8803 				    struct flow_cls_offload *cls_flower)
8804 {
8805 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8806 	struct i40e_cloud_filter *filter = NULL;
8807 	struct i40e_pf *pf = vsi->back;
8808 	int err = 0;
8809 
8810 	if (tc < 0) {
8811 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8812 		return -EOPNOTSUPP;
8813 	}
8814 
8815 	if (!tc) {
8816 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8817 		return -EINVAL;
8818 	}
8819 
8820 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8821 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8822 		return -EBUSY;
8823 
8824 	if (pf->fdir_pf_active_filters ||
8825 	    (!hlist_empty(&pf->fdir_filter_list))) {
8826 		dev_err(&vsi->back->pdev->dev,
8827 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8828 		return -EINVAL;
8829 	}
8830 
8831 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8832 		dev_err(&vsi->back->pdev->dev,
8833 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8834 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8835 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8836 	}
8837 
8838 	filter = kzalloc_obj(*filter);
8839 	if (!filter)
8840 		return -ENOMEM;
8841 
8842 	filter->cookie = cls_flower->cookie;
8843 
8844 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8845 	if (err < 0)
8846 		goto err;
8847 
8848 	err = i40e_handle_tclass(vsi, tc, filter);
8849 	if (err < 0)
8850 		goto err;
8851 
8852 	/* Add cloud filter */
8853 	if (filter->dst_port)
8854 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8855 	else
8856 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8857 
8858 	if (err) {
8859 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8860 			err);
8861 		goto err;
8862 	}
8863 
8864 	/* add filter to the ordered list */
8865 	INIT_HLIST_NODE(&filter->cloud_node);
8866 
8867 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8868 
8869 	pf->num_cloud_filters++;
8870 
8871 	return err;
8872 err:
8873 	kfree(filter);
8874 	return err;
8875 }
8876 
8877 /**
8878  * i40e_find_cloud_filter - Find the could filter in the list
8879  * @vsi: Pointer to VSI
8880  * @cookie: filter specific cookie
8881  *
8882  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8883 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8884 							unsigned long *cookie)
8885 {
8886 	struct i40e_cloud_filter *filter = NULL;
8887 	struct hlist_node *node2;
8888 
8889 	hlist_for_each_entry_safe(filter, node2,
8890 				  &vsi->back->cloud_filter_list, cloud_node)
8891 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8892 			return filter;
8893 	return NULL;
8894 }
8895 
8896 /**
8897  * i40e_delete_clsflower - Remove tc flower filters
8898  * @vsi: Pointer to VSI
8899  * @cls_flower: Pointer to struct flow_cls_offload
8900  *
8901  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8902 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8903 				 struct flow_cls_offload *cls_flower)
8904 {
8905 	struct i40e_cloud_filter *filter = NULL;
8906 	struct i40e_pf *pf = vsi->back;
8907 	int err = 0;
8908 
8909 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8910 
8911 	if (!filter)
8912 		return -EINVAL;
8913 
8914 	hash_del(&filter->cloud_node);
8915 
8916 	if (filter->dst_port)
8917 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8918 	else
8919 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8920 
8921 	kfree(filter);
8922 	if (err) {
8923 		dev_err(&pf->pdev->dev,
8924 			"Failed to delete cloud filter, err %pe\n",
8925 			ERR_PTR(err));
8926 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8927 	}
8928 
8929 	pf->num_cloud_filters--;
8930 	if (!pf->num_cloud_filters)
8931 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8932 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8933 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8934 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8935 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8936 		}
8937 	return 0;
8938 }
8939 
8940 /**
8941  * i40e_setup_tc_cls_flower - flower classifier offloads
8942  * @np: net device to configure
8943  * @cls_flower: offload data
8944  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8945 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8946 				    struct flow_cls_offload *cls_flower)
8947 {
8948 	struct i40e_vsi *vsi = np->vsi;
8949 
8950 	switch (cls_flower->command) {
8951 	case FLOW_CLS_REPLACE:
8952 		return i40e_configure_clsflower(vsi, cls_flower);
8953 	case FLOW_CLS_DESTROY:
8954 		return i40e_delete_clsflower(vsi, cls_flower);
8955 	case FLOW_CLS_STATS:
8956 		return -EOPNOTSUPP;
8957 	default:
8958 		return -EOPNOTSUPP;
8959 	}
8960 }
8961 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8962 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8963 				  void *cb_priv)
8964 {
8965 	struct i40e_netdev_priv *np = cb_priv;
8966 
8967 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8968 		return -EOPNOTSUPP;
8969 
8970 	switch (type) {
8971 	case TC_SETUP_CLSFLOWER:
8972 		return i40e_setup_tc_cls_flower(np, type_data);
8973 
8974 	default:
8975 		return -EOPNOTSUPP;
8976 	}
8977 }
8978 
8979 static LIST_HEAD(i40e_block_cb_list);
8980 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8981 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8982 			   void *type_data)
8983 {
8984 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8985 
8986 	switch (type) {
8987 	case TC_SETUP_QDISC_MQPRIO:
8988 		return i40e_setup_tc(netdev, type_data);
8989 	case TC_SETUP_BLOCK:
8990 		return flow_block_cb_setup_simple(type_data,
8991 						  &i40e_block_cb_list,
8992 						  i40e_setup_tc_block_cb,
8993 						  np, np, true);
8994 	default:
8995 		return -EOPNOTSUPP;
8996 	}
8997 }
8998 
8999 /**
9000  * i40e_open - Called when a network interface is made active
9001  * @netdev: network interface device structure
9002  *
9003  * The open entry point is called when a network interface is made
9004  * active by the system (IFF_UP).  At this point all resources needed
9005  * for transmit and receive operations are allocated, the interrupt
9006  * handler is registered with the OS, the netdev watchdog subtask is
9007  * enabled, and the stack is notified that the interface is ready.
9008  *
9009  * Returns 0 on success, negative value on failure
9010  **/
i40e_open(struct net_device * netdev)9011 int i40e_open(struct net_device *netdev)
9012 {
9013 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9014 	struct i40e_vsi *vsi = np->vsi;
9015 	struct i40e_pf *pf = vsi->back;
9016 	int err;
9017 
9018 	/* disallow open during test or if eeprom is broken */
9019 	if (test_bit(__I40E_TESTING, pf->state) ||
9020 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9021 		return -EBUSY;
9022 
9023 	netif_carrier_off(netdev);
9024 
9025 	if (i40e_force_link_state(pf, true))
9026 		return -EAGAIN;
9027 
9028 	err = i40e_vsi_open(vsi);
9029 	if (err)
9030 		return err;
9031 
9032 	/* configure global TSO hardware offload settings */
9033 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9034 						       TCP_FLAG_FIN) >> 16);
9035 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9036 						       TCP_FLAG_FIN |
9037 						       TCP_FLAG_CWR) >> 16);
9038 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9039 
9040 	return 0;
9041 }
9042 
9043 /**
9044  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9045  * @vsi: vsi structure
9046  *
9047  * This updates netdev's number of tx/rx queues
9048  *
9049  * Returns status of setting tx/rx queues
9050  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9051 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9052 {
9053 	int ret;
9054 
9055 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9056 					   vsi->num_queue_pairs);
9057 	if (ret)
9058 		return ret;
9059 
9060 	return netif_set_real_num_tx_queues(vsi->netdev,
9061 					    vsi->num_queue_pairs);
9062 }
9063 
9064 /**
9065  * i40e_vsi_open -
9066  * @vsi: the VSI to open
9067  *
9068  * Finish initialization of the VSI.
9069  *
9070  * Returns 0 on success, negative value on failure
9071  *
9072  * Note: expects to be called while under rtnl_lock()
9073  **/
i40e_vsi_open(struct i40e_vsi * vsi)9074 int i40e_vsi_open(struct i40e_vsi *vsi)
9075 {
9076 	struct i40e_pf *pf = vsi->back;
9077 	char int_name[I40E_INT_NAME_STR_LEN];
9078 	int err;
9079 
9080 	/* allocate descriptors */
9081 	err = i40e_vsi_setup_tx_resources(vsi);
9082 	if (err)
9083 		goto err_setup_tx;
9084 	err = i40e_vsi_setup_rx_resources(vsi);
9085 	if (err)
9086 		goto err_setup_rx;
9087 
9088 	err = i40e_vsi_configure(vsi);
9089 	if (err)
9090 		goto err_setup_rx;
9091 
9092 	if (vsi->netdev) {
9093 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9094 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9095 		err = i40e_vsi_request_irq(vsi, int_name);
9096 		if (err)
9097 			goto err_setup_rx;
9098 
9099 		/* Notify the stack of the actual queue counts. */
9100 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9101 		if (err)
9102 			goto err_set_queues;
9103 
9104 	} else if (vsi->type == I40E_VSI_FDIR) {
9105 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9106 			 dev_driver_string(&pf->pdev->dev),
9107 			 dev_name(&pf->pdev->dev));
9108 		err = i40e_vsi_request_irq(vsi, int_name);
9109 		if (err)
9110 			goto err_setup_rx;
9111 
9112 	} else {
9113 		err = -EINVAL;
9114 		goto err_setup_rx;
9115 	}
9116 
9117 	err = i40e_up_complete(vsi);
9118 	if (err)
9119 		goto err_up_complete;
9120 
9121 	return 0;
9122 
9123 err_up_complete:
9124 	i40e_down(vsi);
9125 err_set_queues:
9126 	i40e_vsi_free_irq(vsi);
9127 err_setup_rx:
9128 	i40e_vsi_free_rx_resources(vsi);
9129 err_setup_tx:
9130 	i40e_vsi_free_tx_resources(vsi);
9131 	if (vsi->type == I40E_VSI_MAIN)
9132 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9133 
9134 	return err;
9135 }
9136 
9137 /**
9138  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9139  * @pf: Pointer to PF
9140  *
9141  * This function destroys the hlist where all the Flow Director
9142  * filters were saved.
9143  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9144 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9145 {
9146 	struct i40e_fdir_filter *filter;
9147 	struct i40e_flex_pit *pit_entry, *tmp;
9148 	struct hlist_node *node2;
9149 
9150 	hlist_for_each_entry_safe(filter, node2,
9151 				  &pf->fdir_filter_list, fdir_node) {
9152 		hlist_del(&filter->fdir_node);
9153 		kfree(filter);
9154 	}
9155 
9156 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9157 		list_del(&pit_entry->list);
9158 		kfree(pit_entry);
9159 	}
9160 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9161 
9162 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9163 		list_del(&pit_entry->list);
9164 		kfree(pit_entry);
9165 	}
9166 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9167 
9168 	pf->fdir_pf_active_filters = 0;
9169 	i40e_reset_fdir_filter_cnt(pf);
9170 
9171 	/* Reprogram the default input set for TCP/IPv4 */
9172 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9173 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9174 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9175 
9176 	/* Reprogram the default input set for TCP/IPv6 */
9177 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP,
9178 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9179 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9180 
9181 	/* Reprogram the default input set for UDP/IPv4 */
9182 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_UDP,
9183 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9184 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9185 
9186 	/* Reprogram the default input set for UDP/IPv6 */
9187 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_UDP,
9188 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9189 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9190 
9191 	/* Reprogram the default input set for SCTP/IPv4 */
9192 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP,
9193 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9194 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9195 
9196 	/* Reprogram the default input set for SCTP/IPv6 */
9197 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP,
9198 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9199 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9200 
9201 	/* Reprogram the default input set for Other/IPv4 */
9202 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_OTHER,
9203 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9204 
9205 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV4,
9206 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9207 
9208 	/* Reprogram the default input set for Other/IPv6 */
9209 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_OTHER,
9210 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9211 
9212 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV6,
9213 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9214 }
9215 
9216 /**
9217  * i40e_cloud_filter_exit - Cleans up the cloud filters
9218  * @pf: Pointer to PF
9219  *
9220  * This function destroys the hlist where all the cloud filters
9221  * were saved.
9222  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9223 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9224 {
9225 	struct i40e_cloud_filter *cfilter;
9226 	struct hlist_node *node;
9227 
9228 	hlist_for_each_entry_safe(cfilter, node,
9229 				  &pf->cloud_filter_list, cloud_node) {
9230 		hlist_del(&cfilter->cloud_node);
9231 		kfree(cfilter);
9232 	}
9233 	pf->num_cloud_filters = 0;
9234 
9235 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9236 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9237 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9238 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9239 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9240 	}
9241 }
9242 
9243 /**
9244  * i40e_close - Disables a network interface
9245  * @netdev: network interface device structure
9246  *
9247  * The close entry point is called when an interface is de-activated
9248  * by the OS.  The hardware is still under the driver's control, but
9249  * this netdev interface is disabled.
9250  *
9251  * Returns 0, this is not allowed to fail
9252  **/
i40e_close(struct net_device * netdev)9253 int i40e_close(struct net_device *netdev)
9254 {
9255 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9256 	struct i40e_vsi *vsi = np->vsi;
9257 
9258 	i40e_vsi_close(vsi);
9259 
9260 	return 0;
9261 }
9262 
9263 /**
9264  * i40e_do_reset - Start a PF or Core Reset sequence
9265  * @pf: board private structure
9266  * @reset_flags: which reset is requested
9267  * @lock_acquired: indicates whether or not the lock has been acquired
9268  * before this function was called.
9269  *
9270  * The essential difference in resets is that the PF Reset
9271  * doesn't clear the packet buffers, doesn't reset the PE
9272  * firmware, and doesn't bother the other PFs on the chip.
9273  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9274 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9275 {
9276 	struct i40e_vsi *vsi;
9277 	u32 val;
9278 	int i;
9279 
9280 	/* do the biggest reset indicated */
9281 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9282 
9283 		/* Request a Global Reset
9284 		 *
9285 		 * This will start the chip's countdown to the actual full
9286 		 * chip reset event, and a warning interrupt to be sent
9287 		 * to all PFs, including the requestor.  Our handler
9288 		 * for the warning interrupt will deal with the shutdown
9289 		 * and recovery of the switch setup.
9290 		 */
9291 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9292 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9293 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9294 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9295 
9296 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9297 
9298 		/* Request a Core Reset
9299 		 *
9300 		 * Same as Global Reset, except does *not* include the MAC/PHY
9301 		 */
9302 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9303 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9304 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9305 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9306 		i40e_flush(&pf->hw);
9307 
9308 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9309 
9310 		/* Request a PF Reset
9311 		 *
9312 		 * Resets only the PF-specific registers
9313 		 *
9314 		 * This goes directly to the tear-down and rebuild of
9315 		 * the switch, since we need to do all the recovery as
9316 		 * for the Core Reset.
9317 		 */
9318 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9319 		i40e_handle_reset_warning(pf, lock_acquired);
9320 
9321 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9322 		/* Request a PF Reset
9323 		 *
9324 		 * Resets PF and reinitializes PFs VSI.
9325 		 */
9326 		i40e_prep_for_reset(pf);
9327 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9328 		dev_info(&pf->pdev->dev,
9329 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9330 			 "FW LLDP is disabled\n" :
9331 			 "FW LLDP is enabled\n");
9332 
9333 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9334 		/* Find the VSI(s) that requested a re-init */
9335 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9336 
9337 		i40e_pf_for_each_vsi(pf, i, vsi) {
9338 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9339 					       vsi->state))
9340 				i40e_vsi_reinit_locked(vsi);
9341 		}
9342 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9343 		/* Find the VSI(s) that needs to be brought down */
9344 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9345 
9346 		i40e_pf_for_each_vsi(pf, i, vsi) {
9347 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9348 					       vsi->state)) {
9349 				set_bit(__I40E_VSI_DOWN, vsi->state);
9350 				i40e_down(vsi);
9351 			}
9352 		}
9353 	} else {
9354 		dev_info(&pf->pdev->dev,
9355 			 "bad reset request 0x%08x\n", reset_flags);
9356 	}
9357 }
9358 
9359 #ifdef CONFIG_I40E_DCB
9360 /**
9361  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9362  * @pf: board private structure
9363  * @old_cfg: current DCB config
9364  * @new_cfg: new DCB config
9365  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9366 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9367 			    struct i40e_dcbx_config *old_cfg,
9368 			    struct i40e_dcbx_config *new_cfg)
9369 {
9370 	bool need_reconfig = false;
9371 
9372 	/* Check if ETS configuration has changed */
9373 	if (memcmp(&new_cfg->etscfg,
9374 		   &old_cfg->etscfg,
9375 		   sizeof(new_cfg->etscfg))) {
9376 		/* If Priority Table has changed reconfig is needed */
9377 		if (memcmp(&new_cfg->etscfg.prioritytable,
9378 			   &old_cfg->etscfg.prioritytable,
9379 			   sizeof(new_cfg->etscfg.prioritytable))) {
9380 			need_reconfig = true;
9381 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9382 		}
9383 
9384 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9385 			   &old_cfg->etscfg.tcbwtable,
9386 			   sizeof(new_cfg->etscfg.tcbwtable)))
9387 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9388 
9389 		if (memcmp(&new_cfg->etscfg.tsatable,
9390 			   &old_cfg->etscfg.tsatable,
9391 			   sizeof(new_cfg->etscfg.tsatable)))
9392 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9393 	}
9394 
9395 	/* Check if PFC configuration has changed */
9396 	if (memcmp(&new_cfg->pfc,
9397 		   &old_cfg->pfc,
9398 		   sizeof(new_cfg->pfc))) {
9399 		need_reconfig = true;
9400 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9401 	}
9402 
9403 	/* Check if APP Table has changed */
9404 	if (memcmp(&new_cfg->app,
9405 		   &old_cfg->app,
9406 		   sizeof(new_cfg->app))) {
9407 		need_reconfig = true;
9408 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9409 	}
9410 
9411 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9412 	return need_reconfig;
9413 }
9414 
9415 /**
9416  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9417  * @pf: board private structure
9418  * @e: event info posted on ARQ
9419  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9420 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9421 				  struct i40e_arq_event_info *e)
9422 {
9423 	struct i40e_aqc_lldp_get_mib *mib = libie_aq_raw(&e->desc);
9424 	struct i40e_hw *hw = &pf->hw;
9425 	struct i40e_dcbx_config tmp_dcbx_cfg;
9426 	bool need_reconfig = false;
9427 	int ret = 0;
9428 	u8 type;
9429 
9430 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9431 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9432 	    (hw->phy.link_info.link_speed &
9433 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9434 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9435 		/* let firmware decide if the DCB should be disabled */
9436 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9437 
9438 	/* Not DCB capable or capability disabled */
9439 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9440 		return ret;
9441 
9442 	/* Ignore if event is not for Nearest Bridge */
9443 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9444 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9445 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9446 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9447 		return ret;
9448 
9449 	/* Check MIB Type and return if event for Remote MIB update */
9450 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9451 	dev_dbg(&pf->pdev->dev,
9452 		"LLDP event mib type %s\n", type ? "remote" : "local");
9453 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9454 		/* Update the remote cached instance and return */
9455 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9456 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9457 				&hw->remote_dcbx_config);
9458 		goto exit;
9459 	}
9460 
9461 	/* Store the old configuration */
9462 	tmp_dcbx_cfg = hw->local_dcbx_config;
9463 
9464 	/* Reset the old DCBx configuration data */
9465 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9466 	/* Get updated DCBX data from firmware */
9467 	ret = i40e_get_dcb_config(&pf->hw);
9468 	if (ret) {
9469 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9470 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9471 		    (hw->phy.link_info.link_speed &
9472 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9473 			dev_warn(&pf->pdev->dev,
9474 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9475 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9476 		} else {
9477 			dev_info(&pf->pdev->dev,
9478 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9479 				 ERR_PTR(ret),
9480 				 libie_aq_str(pf->hw.aq.asq_last_status));
9481 		}
9482 		goto exit;
9483 	}
9484 
9485 	/* No change detected in DCBX configs */
9486 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9487 		    sizeof(tmp_dcbx_cfg))) {
9488 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9489 		goto exit;
9490 	}
9491 
9492 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9493 					       &hw->local_dcbx_config);
9494 
9495 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9496 
9497 	if (!need_reconfig)
9498 		goto exit;
9499 
9500 	/* Enable DCB tagging only when more than one TC */
9501 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9502 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9503 	else
9504 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9505 
9506 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9507 	/* Reconfiguration needed quiesce all VSIs */
9508 	i40e_pf_quiesce_all_vsi(pf);
9509 
9510 	/* Changes in configuration update VEB/VSI */
9511 	i40e_dcb_reconfigure(pf);
9512 
9513 	ret = i40e_resume_port_tx(pf);
9514 
9515 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9516 	/* In case of error no point in resuming VSIs */
9517 	if (ret)
9518 		goto exit;
9519 
9520 	/* Wait for the PF's queues to be disabled */
9521 	ret = i40e_pf_wait_queues_disabled(pf);
9522 	if (ret) {
9523 		/* Schedule PF reset to recover */
9524 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9525 		i40e_service_event_schedule(pf);
9526 	} else {
9527 		i40e_pf_unquiesce_all_vsi(pf);
9528 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9529 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9530 	}
9531 
9532 exit:
9533 	return ret;
9534 }
9535 #endif /* CONFIG_I40E_DCB */
9536 
9537 /**
9538  * i40e_do_reset_safe - Protected reset path for userland calls.
9539  * @pf: board private structure
9540  * @reset_flags: which reset is requested
9541  *
9542  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9543 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9544 {
9545 	rtnl_lock();
9546 	i40e_do_reset(pf, reset_flags, true);
9547 	rtnl_unlock();
9548 }
9549 
9550 /**
9551  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9552  * @pf: board private structure
9553  * @e: event info posted on ARQ
9554  *
9555  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9556  * and VF queues
9557  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9558 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9559 					   struct i40e_arq_event_info *e)
9560 {
9561 	struct i40e_aqc_lan_overflow *data = libie_aq_raw(&e->desc);
9562 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9563 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9564 	struct i40e_hw *hw = &pf->hw;
9565 	struct i40e_vf *vf;
9566 	u16 vf_id;
9567 
9568 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9569 		queue, qtx_ctl);
9570 
9571 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9572 	    I40E_QTX_CTL_VF_QUEUE)
9573 		return;
9574 
9575 	/* Queue belongs to VF, find the VF and issue VF reset */
9576 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9577 	vf_id -= hw->func_caps.vf_base_id;
9578 	vf = &pf->vf[vf_id];
9579 	i40e_vc_notify_vf_reset(vf);
9580 	/* Allow VF to process pending reset notification */
9581 	msleep(20);
9582 	i40e_reset_vf(vf, false);
9583 }
9584 
9585 /**
9586  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9587  * @pf: board private structure
9588  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9589 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9590 {
9591 	u32 val, fcnt_prog;
9592 
9593 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9594 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9595 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9596 	return fcnt_prog;
9597 }
9598 
9599 /**
9600  * i40e_get_global_fd_count - Get total FD filters programmed on device
9601  * @pf: board private structure
9602  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9603 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9604 {
9605 	u32 val, fcnt_prog;
9606 
9607 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9608 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9609 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9610 	return fcnt_prog;
9611 }
9612 
9613 /**
9614  * i40e_reenable_fdir_sb - Restore FDir SB capability
9615  * @pf: board private structure
9616  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9617 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9618 {
9619 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9620 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9621 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9622 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9623 }
9624 
9625 /**
9626  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9627  * @pf: board private structure
9628  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9629 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9630 {
9631 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9632 		/* ATR uses the same filtering logic as SB rules. It only
9633 		 * functions properly if the input set mask is at the default
9634 		 * settings. It is safe to restore the default input set
9635 		 * because there are no active TCPv4 filter rules.
9636 		 */
9637 		i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9638 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9639 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9640 
9641 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9642 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9643 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9644 	}
9645 }
9646 
9647 /**
9648  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9649  * @pf: board private structure
9650  * @filter: FDir filter to remove
9651  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9652 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9653 				       struct i40e_fdir_filter *filter)
9654 {
9655 	/* Update counters */
9656 	pf->fdir_pf_active_filters--;
9657 	pf->fd_inv = 0;
9658 
9659 	switch (filter->flow_type) {
9660 	case TCP_V4_FLOW:
9661 		pf->fd_tcp4_filter_cnt--;
9662 		break;
9663 	case UDP_V4_FLOW:
9664 		pf->fd_udp4_filter_cnt--;
9665 		break;
9666 	case SCTP_V4_FLOW:
9667 		pf->fd_sctp4_filter_cnt--;
9668 		break;
9669 	case TCP_V6_FLOW:
9670 		pf->fd_tcp6_filter_cnt--;
9671 		break;
9672 	case UDP_V6_FLOW:
9673 		pf->fd_udp6_filter_cnt--;
9674 		break;
9675 	case SCTP_V6_FLOW:
9676 		pf->fd_udp6_filter_cnt--;
9677 		break;
9678 	case IP_USER_FLOW:
9679 		switch (filter->ipl4_proto) {
9680 		case IPPROTO_TCP:
9681 			pf->fd_tcp4_filter_cnt--;
9682 			break;
9683 		case IPPROTO_UDP:
9684 			pf->fd_udp4_filter_cnt--;
9685 			break;
9686 		case IPPROTO_SCTP:
9687 			pf->fd_sctp4_filter_cnt--;
9688 			break;
9689 		case IPPROTO_IP:
9690 			pf->fd_ip4_filter_cnt--;
9691 			break;
9692 		}
9693 		break;
9694 	case IPV6_USER_FLOW:
9695 		switch (filter->ipl4_proto) {
9696 		case IPPROTO_TCP:
9697 			pf->fd_tcp6_filter_cnt--;
9698 			break;
9699 		case IPPROTO_UDP:
9700 			pf->fd_udp6_filter_cnt--;
9701 			break;
9702 		case IPPROTO_SCTP:
9703 			pf->fd_sctp6_filter_cnt--;
9704 			break;
9705 		case IPPROTO_IP:
9706 			pf->fd_ip6_filter_cnt--;
9707 			break;
9708 		}
9709 		break;
9710 	}
9711 
9712 	/* Remove the filter from the list and free memory */
9713 	hlist_del(&filter->fdir_node);
9714 	kfree(filter);
9715 }
9716 
9717 /**
9718  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9719  * @pf: board private structure
9720  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9721 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9722 {
9723 	struct i40e_fdir_filter *filter;
9724 	u32 fcnt_prog, fcnt_avail;
9725 	struct hlist_node *node;
9726 
9727 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9728 		return;
9729 
9730 	/* Check if we have enough room to re-enable FDir SB capability. */
9731 	fcnt_prog = i40e_get_global_fd_count(pf);
9732 	fcnt_avail = pf->fdir_pf_filter_count;
9733 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9734 	    (pf->fd_add_err == 0) ||
9735 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9736 		i40e_reenable_fdir_sb(pf);
9737 
9738 	/* We should wait for even more space before re-enabling ATR.
9739 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9740 	 * rules active.
9741 	 */
9742 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9743 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9744 		i40e_reenable_fdir_atr(pf);
9745 
9746 	/* if hw had a problem adding a filter, delete it */
9747 	if (pf->fd_inv > 0) {
9748 		hlist_for_each_entry_safe(filter, node,
9749 					  &pf->fdir_filter_list, fdir_node)
9750 			if (filter->fd_id == pf->fd_inv)
9751 				i40e_delete_invalid_filter(pf, filter);
9752 	}
9753 }
9754 
9755 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9756 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9757 /**
9758  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9759  * @pf: board private structure
9760  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9761 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9762 {
9763 	unsigned long min_flush_time;
9764 	int flush_wait_retry = 50;
9765 	bool disable_atr = false;
9766 	int fd_room;
9767 	int reg;
9768 
9769 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9770 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9771 		return;
9772 
9773 	/* If the flush is happening too quick and we have mostly SB rules we
9774 	 * should not re-enable ATR for some time.
9775 	 */
9776 	min_flush_time = pf->fd_flush_timestamp +
9777 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9778 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9779 
9780 	if (!(time_after(jiffies, min_flush_time)) &&
9781 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9782 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9783 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9784 		disable_atr = true;
9785 	}
9786 
9787 	pf->fd_flush_timestamp = jiffies;
9788 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9789 	/* flush all filters */
9790 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9791 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9792 	i40e_flush(&pf->hw);
9793 	pf->fd_flush_cnt++;
9794 	pf->fd_add_err = 0;
9795 	do {
9796 		/* Check FD flush status every 5-6msec */
9797 		usleep_range(5000, 6000);
9798 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9799 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9800 			break;
9801 	} while (flush_wait_retry--);
9802 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9803 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9804 	} else {
9805 		/* replay sideband filters */
9806 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9807 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9808 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9809 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9810 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9811 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9812 	}
9813 }
9814 
9815 /**
9816  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9817  * @pf: board private structure
9818  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9819 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9820 {
9821 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9822 }
9823 
9824 /**
9825  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9826  * @pf: board private structure
9827  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9828 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9829 {
9830 
9831 	/* if interface is down do nothing */
9832 	if (test_bit(__I40E_DOWN, pf->state))
9833 		return;
9834 
9835 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9836 		i40e_fdir_flush_and_replay(pf);
9837 
9838 	i40e_fdir_check_and_reenable(pf);
9839 
9840 }
9841 
9842 /**
9843  * i40e_vsi_link_event - notify VSI of a link event
9844  * @vsi: vsi to be notified
9845  * @link_up: link up or down
9846  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9847 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9848 {
9849 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9850 		return;
9851 
9852 	switch (vsi->type) {
9853 	case I40E_VSI_MAIN:
9854 		if (!vsi->netdev || !vsi->netdev_registered)
9855 			break;
9856 
9857 		if (link_up) {
9858 			netif_carrier_on(vsi->netdev);
9859 			netif_tx_wake_all_queues(vsi->netdev);
9860 		} else {
9861 			netif_carrier_off(vsi->netdev);
9862 			netif_tx_stop_all_queues(vsi->netdev);
9863 		}
9864 		break;
9865 
9866 	case I40E_VSI_SRIOV:
9867 	case I40E_VSI_VMDQ2:
9868 	case I40E_VSI_CTRL:
9869 	case I40E_VSI_IWARP:
9870 	case I40E_VSI_MIRROR:
9871 	default:
9872 		/* there is no notification for other VSIs */
9873 		break;
9874 	}
9875 }
9876 
9877 /**
9878  * i40e_veb_link_event - notify elements on the veb of a link event
9879  * @veb: veb to be notified
9880  * @link_up: link up or down
9881  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9882 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9883 {
9884 	struct i40e_vsi *vsi;
9885 	struct i40e_pf *pf;
9886 	int i;
9887 
9888 	if (!veb || !veb->pf)
9889 		return;
9890 	pf = veb->pf;
9891 
9892 	/* Send link event to contained VSIs */
9893 	i40e_pf_for_each_vsi(pf, i, vsi)
9894 		if (vsi->uplink_seid == veb->seid)
9895 			i40e_vsi_link_event(vsi, link_up);
9896 }
9897 
9898 /**
9899  * i40e_link_event - Update netif_carrier status
9900  * @pf: board private structure
9901  **/
i40e_link_event(struct i40e_pf * pf)9902 static void i40e_link_event(struct i40e_pf *pf)
9903 {
9904 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9905 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9906 	u8 new_link_speed, old_link_speed;
9907 	bool new_link, old_link;
9908 	int status;
9909 #ifdef CONFIG_I40E_DCB
9910 	int err;
9911 #endif /* CONFIG_I40E_DCB */
9912 
9913 	/* set this to force the get_link_status call to refresh state */
9914 	pf->hw.phy.get_link_info = true;
9915 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9916 	status = i40e_get_link_status(&pf->hw, &new_link);
9917 
9918 	/* On success, disable temp link polling */
9919 	if (status == 0) {
9920 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9921 	} else {
9922 		/* Enable link polling temporarily until i40e_get_link_status
9923 		 * returns 0
9924 		 */
9925 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9926 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9927 			status);
9928 		return;
9929 	}
9930 
9931 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9932 	new_link_speed = pf->hw.phy.link_info.link_speed;
9933 
9934 	if (new_link == old_link &&
9935 	    new_link_speed == old_link_speed &&
9936 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9937 	     new_link == netif_carrier_ok(vsi->netdev)))
9938 		return;
9939 
9940 	if (!new_link && old_link)
9941 		pf->link_down_events++;
9942 
9943 	i40e_print_link_message(vsi, new_link);
9944 
9945 	/* Notify the base of the switch tree connected to
9946 	 * the link.  Floating VEBs are not notified.
9947 	 */
9948 	if (veb)
9949 		i40e_veb_link_event(veb, new_link);
9950 	else
9951 		i40e_vsi_link_event(vsi, new_link);
9952 
9953 	if (pf->vf)
9954 		i40e_vc_notify_link_state(pf);
9955 
9956 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9957 		i40e_ptp_set_increment(pf);
9958 #ifdef CONFIG_I40E_DCB
9959 	if (new_link == old_link)
9960 		return;
9961 	/* Not SW DCB so firmware will take care of default settings */
9962 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9963 		return;
9964 
9965 	/* We cover here only link down, as after link up in case of SW DCB
9966 	 * SW LLDP agent will take care of setting it up
9967 	 */
9968 	if (!new_link) {
9969 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9970 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9971 		err = i40e_dcb_sw_default_config(pf);
9972 		if (err) {
9973 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9974 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9975 		} else {
9976 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9977 				       DCB_CAP_DCBX_VER_IEEE;
9978 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9979 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9980 		}
9981 	}
9982 #endif /* CONFIG_I40E_DCB */
9983 }
9984 
9985 /**
9986  * i40e_watchdog_subtask - periodic checks not using event driven response
9987  * @pf: board private structure
9988  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9989 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9990 {
9991 	struct i40e_vsi *vsi;
9992 	struct i40e_veb *veb;
9993 	int i;
9994 
9995 	/* if interface is down do nothing */
9996 	if (test_bit(__I40E_DOWN, pf->state) ||
9997 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9998 		return;
9999 
10000 	/* make sure we don't do these things too often */
10001 	if (time_before(jiffies, (pf->service_timer_previous +
10002 				  pf->service_timer_period)))
10003 		return;
10004 	pf->service_timer_previous = jiffies;
10005 
10006 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10007 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10008 		i40e_link_event(pf);
10009 
10010 	/* Update the stats for active netdevs so the network stack
10011 	 * can look at updated numbers whenever it cares to
10012 	 */
10013 	i40e_pf_for_each_vsi(pf, i, vsi)
10014 		if (vsi->netdev)
10015 			i40e_update_stats(vsi);
10016 
10017 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10018 		/* Update the stats for the active switching components */
10019 		i40e_pf_for_each_veb(pf, i, veb)
10020 			i40e_update_veb_stats(veb);
10021 	}
10022 
10023 	i40e_ptp_rx_hang(pf);
10024 	i40e_ptp_tx_hang(pf);
10025 }
10026 
10027 /**
10028  * i40e_reset_subtask - Set up for resetting the device and driver
10029  * @pf: board private structure
10030  **/
i40e_reset_subtask(struct i40e_pf * pf)10031 static void i40e_reset_subtask(struct i40e_pf *pf)
10032 {
10033 	u32 reset_flags = 0;
10034 
10035 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10036 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10037 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10038 	}
10039 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10040 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10041 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10042 	}
10043 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10044 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10045 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10046 	}
10047 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10048 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10049 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10050 	}
10051 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10052 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10053 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10054 	}
10055 
10056 	/* If there's a recovery already waiting, it takes
10057 	 * precedence before starting a new reset sequence.
10058 	 */
10059 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10060 		i40e_prep_for_reset(pf);
10061 		i40e_reset(pf);
10062 		i40e_rebuild(pf, false, false);
10063 	}
10064 
10065 	/* If we're already down or resetting, just bail */
10066 	if (reset_flags &&
10067 	    !test_bit(__I40E_DOWN, pf->state) &&
10068 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10069 		i40e_do_reset(pf, reset_flags, false);
10070 	}
10071 }
10072 
10073 /**
10074  * i40e_handle_link_event - Handle link event
10075  * @pf: board private structure
10076  * @e: event info posted on ARQ
10077  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10078 static void i40e_handle_link_event(struct i40e_pf *pf,
10079 				   struct i40e_arq_event_info *e)
10080 {
10081 	struct i40e_aqc_get_link_status *status = libie_aq_raw(&e->desc);
10082 
10083 	/* Do a new status request to re-enable LSE reporting
10084 	 * and load new status information into the hw struct
10085 	 * This completely ignores any state information
10086 	 * in the ARQ event info, instead choosing to always
10087 	 * issue the AQ update link status command.
10088 	 */
10089 	i40e_link_event(pf);
10090 
10091 	/* Check if module meets thermal requirements */
10092 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10093 		dev_err(&pf->pdev->dev,
10094 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10095 		dev_err(&pf->pdev->dev,
10096 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10097 	} else {
10098 		/* check for unqualified module, if link is down, suppress
10099 		 * the message if link was forced to be down.
10100 		 */
10101 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10102 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10103 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10104 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10105 			dev_err(&pf->pdev->dev,
10106 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10107 			dev_err(&pf->pdev->dev,
10108 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10109 		}
10110 	}
10111 }
10112 
10113 /**
10114  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10115  * @pf: board private structure
10116  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10117 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10118 {
10119 	struct i40e_arq_event_info event;
10120 	struct i40e_hw *hw = &pf->hw;
10121 	u16 pending, i = 0;
10122 	u16 opcode;
10123 	u32 oldval;
10124 	int ret;
10125 	u32 val;
10126 
10127 	/* Do not run clean AQ when PF reset fails */
10128 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10129 		return;
10130 
10131 	/* check for error indications */
10132 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10133 	oldval = val;
10134 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10135 		if (hw->debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10137 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10138 	}
10139 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10140 		if (hw->debug_mask & I40E_DEBUG_AQ)
10141 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10142 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10143 		pf->arq_overflows++;
10144 	}
10145 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10146 		if (hw->debug_mask & I40E_DEBUG_AQ)
10147 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10148 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10149 	}
10150 	if (oldval != val)
10151 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10152 
10153 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10154 	oldval = val;
10155 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10156 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10157 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10158 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10159 	}
10160 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10161 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10162 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10163 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10164 	}
10165 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10166 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10167 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10168 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10169 	}
10170 	if (oldval != val)
10171 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10172 
10173 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10174 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10175 	if (!event.msg_buf)
10176 		return;
10177 
10178 	do {
10179 		ret = i40e_clean_arq_element(hw, &event, &pending);
10180 		if (ret == -EALREADY)
10181 			break;
10182 		else if (ret) {
10183 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10184 			break;
10185 		}
10186 
10187 		opcode = le16_to_cpu(event.desc.opcode);
10188 		switch (opcode) {
10189 
10190 		case i40e_aqc_opc_get_link_status:
10191 			rtnl_lock();
10192 			i40e_handle_link_event(pf, &event);
10193 			rtnl_unlock();
10194 			break;
10195 		case i40e_aqc_opc_send_msg_to_pf:
10196 			ret = i40e_vc_process_vf_msg(pf,
10197 					le16_to_cpu(event.desc.retval),
10198 					le32_to_cpu(event.desc.cookie_high),
10199 					le32_to_cpu(event.desc.cookie_low),
10200 					event.msg_buf,
10201 					event.msg_len);
10202 			break;
10203 		case i40e_aqc_opc_lldp_update_mib:
10204 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10205 #ifdef CONFIG_I40E_DCB
10206 			rtnl_lock();
10207 			i40e_handle_lldp_event(pf, &event);
10208 			rtnl_unlock();
10209 #endif /* CONFIG_I40E_DCB */
10210 			break;
10211 		case i40e_aqc_opc_event_lan_overflow:
10212 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10213 			i40e_handle_lan_overflow_event(pf, &event);
10214 			break;
10215 		case i40e_aqc_opc_send_msg_to_peer:
10216 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10217 			break;
10218 		case i40e_aqc_opc_nvm_erase:
10219 		case i40e_aqc_opc_nvm_update:
10220 		case i40e_aqc_opc_oem_post_update:
10221 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10222 				   "ARQ NVM operation 0x%04x completed\n",
10223 				   opcode);
10224 			break;
10225 		default:
10226 			dev_info(&pf->pdev->dev,
10227 				 "ARQ: Unknown event 0x%04x ignored\n",
10228 				 opcode);
10229 			break;
10230 		}
10231 	} while (i++ < I40E_AQ_WORK_LIMIT);
10232 
10233 	if (i < I40E_AQ_WORK_LIMIT)
10234 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10235 
10236 	/* re-enable Admin queue interrupt cause */
10237 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10238 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10239 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10240 	i40e_flush(hw);
10241 
10242 	kfree(event.msg_buf);
10243 }
10244 
10245 /**
10246  * i40e_verify_eeprom - make sure eeprom is good to use
10247  * @pf: board private structure
10248  **/
i40e_verify_eeprom(struct i40e_pf * pf)10249 static void i40e_verify_eeprom(struct i40e_pf *pf)
10250 {
10251 	int err;
10252 
10253 	err = i40e_diag_eeprom_test(&pf->hw);
10254 	if (err) {
10255 		/* retry in case of garbage read */
10256 		err = i40e_diag_eeprom_test(&pf->hw);
10257 		if (err) {
10258 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10259 				 err);
10260 			set_bit(__I40E_BAD_EEPROM, pf->state);
10261 		}
10262 	}
10263 
10264 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10265 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10266 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10267 	}
10268 }
10269 
10270 /**
10271  * i40e_enable_pf_switch_lb
10272  * @pf: pointer to the PF structure
10273  *
10274  * enable switch loop back or die - no point in a return value
10275  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10276 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10277 {
10278 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10279 	struct i40e_vsi_context ctxt;
10280 	int ret;
10281 
10282 	ctxt.seid = pf->main_vsi_seid;
10283 	ctxt.pf_num = pf->hw.pf_id;
10284 	ctxt.vf_num = 0;
10285 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10286 	if (ret) {
10287 		dev_info(&pf->pdev->dev,
10288 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10289 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10290 		return;
10291 	}
10292 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10293 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10294 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10295 
10296 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10297 	if (ret) {
10298 		dev_info(&pf->pdev->dev,
10299 			 "update vsi switch failed, err %pe aq_err %s\n",
10300 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10301 	}
10302 }
10303 
10304 /**
10305  * i40e_disable_pf_switch_lb
10306  * @pf: pointer to the PF structure
10307  *
10308  * disable switch loop back or die - no point in a return value
10309  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10310 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10311 {
10312 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10313 	struct i40e_vsi_context ctxt;
10314 	int ret;
10315 
10316 	ctxt.seid = pf->main_vsi_seid;
10317 	ctxt.pf_num = pf->hw.pf_id;
10318 	ctxt.vf_num = 0;
10319 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10320 	if (ret) {
10321 		dev_info(&pf->pdev->dev,
10322 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10323 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10324 		return;
10325 	}
10326 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10327 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10328 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10329 
10330 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10331 	if (ret) {
10332 		dev_info(&pf->pdev->dev,
10333 			 "update vsi switch failed, err %pe aq_err %s\n",
10334 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10335 	}
10336 }
10337 
10338 /**
10339  * i40e_config_bridge_mode - Configure the HW bridge mode
10340  * @veb: pointer to the bridge instance
10341  *
10342  * Configure the loop back mode for the LAN VSI that is downlink to the
10343  * specified HW bridge instance. It is expected this function is called
10344  * when a new HW bridge is instantiated.
10345  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10346 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10347 {
10348 	struct i40e_pf *pf = veb->pf;
10349 
10350 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10351 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10352 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10353 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10354 		i40e_disable_pf_switch_lb(pf);
10355 	else
10356 		i40e_enable_pf_switch_lb(pf);
10357 }
10358 
10359 /**
10360  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10361  * @veb: pointer to the VEB instance
10362  *
10363  * This is a function that builds the attached VSIs. We track the connections
10364  * through our own index numbers because the seid's from the HW could change
10365  * across the reset.
10366  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10367 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10368 {
10369 	struct i40e_vsi *ctl_vsi = NULL;
10370 	struct i40e_pf *pf = veb->pf;
10371 	struct i40e_vsi *vsi;
10372 	int v, ret;
10373 
10374 	/* As we do not maintain PV (port virtualizer) switch element then
10375 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10376 	 * and its control VSI is the main one.
10377 	 */
10378 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10379 		dev_err(&pf->pdev->dev,
10380 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10381 		return -ENOENT;
10382 	}
10383 
10384 	if (veb->uplink_seid == pf->mac_seid) {
10385 		/* Check that the LAN VSI has VEB owning flag set */
10386 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10387 
10388 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10389 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10390 			dev_err(&pf->pdev->dev,
10391 				"Invalid control VSI for VEB %d\n", veb->idx);
10392 			return -ENOENT;
10393 		}
10394 
10395 		/* Add the control VSI to switch */
10396 		ret = i40e_add_vsi(ctl_vsi);
10397 		if (ret) {
10398 			dev_err(&pf->pdev->dev,
10399 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10400 				veb->idx, ret);
10401 			return ret;
10402 		}
10403 
10404 		i40e_vsi_reset_stats(ctl_vsi);
10405 	}
10406 
10407 	/* create the VEB in the switch and move the VSI onto the VEB */
10408 	ret = i40e_add_veb(veb, ctl_vsi);
10409 	if (ret)
10410 		return ret;
10411 
10412 	if (veb->uplink_seid) {
10413 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10414 			veb->bridge_mode = BRIDGE_MODE_VEB;
10415 		else
10416 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10417 		i40e_config_bridge_mode(veb);
10418 	}
10419 
10420 	/* create the remaining VSIs attached to this VEB */
10421 	i40e_pf_for_each_vsi(pf, v, vsi) {
10422 		if (vsi == ctl_vsi)
10423 			continue;
10424 
10425 		if (vsi->veb_idx == veb->idx) {
10426 			vsi->uplink_seid = veb->seid;
10427 			ret = i40e_add_vsi(vsi);
10428 			if (ret) {
10429 				dev_info(&pf->pdev->dev,
10430 					 "rebuild of vsi_idx %d failed: %d\n",
10431 					 v, ret);
10432 				return ret;
10433 			}
10434 			i40e_vsi_reset_stats(vsi);
10435 		}
10436 	}
10437 
10438 	return ret;
10439 }
10440 
10441 /**
10442  * i40e_get_capabilities - get info about the HW
10443  * @pf: the PF struct
10444  * @list_type: AQ capability to be queried
10445  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10446 static int i40e_get_capabilities(struct i40e_pf *pf,
10447 				 enum i40e_admin_queue_opc list_type)
10448 {
10449 	struct libie_aqc_list_caps_elem *cap_buf;
10450 	u16 data_size;
10451 	int buf_len;
10452 	int err;
10453 
10454 	buf_len = 40 * sizeof(struct libie_aqc_list_caps_elem);
10455 	do {
10456 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10457 		if (!cap_buf)
10458 			return -ENOMEM;
10459 
10460 		/* this loads the data into the hw struct for us */
10461 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10462 						    &data_size, list_type,
10463 						    NULL);
10464 		/* data loaded, buffer no longer needed */
10465 		kfree(cap_buf);
10466 
10467 		if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_ENOMEM) {
10468 			/* retry with a larger buffer */
10469 			buf_len = data_size;
10470 		} else if (pf->hw.aq.asq_last_status != LIBIE_AQ_RC_OK || err) {
10471 			dev_info(&pf->pdev->dev,
10472 				 "capability discovery failed, err %pe aq_err %s\n",
10473 				 ERR_PTR(err),
10474 				 libie_aq_str(pf->hw.aq.asq_last_status));
10475 			return -ENODEV;
10476 		}
10477 	} while (err);
10478 
10479 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10480 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10481 			dev_info(&pf->pdev->dev,
10482 				 "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",
10483 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10484 				 pf->hw.func_caps.num_msix_vectors,
10485 				 pf->hw.func_caps.num_msix_vectors_vf,
10486 				 pf->hw.func_caps.fd_filters_guaranteed,
10487 				 pf->hw.func_caps.fd_filters_best_effort,
10488 				 pf->hw.func_caps.num_tx_qp,
10489 				 pf->hw.func_caps.num_vsis);
10490 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10491 			dev_info(&pf->pdev->dev,
10492 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10493 				 pf->hw.dev_caps.switch_mode,
10494 				 pf->hw.dev_caps.valid_functions);
10495 			dev_info(&pf->pdev->dev,
10496 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10497 				 pf->hw.dev_caps.sr_iov_1_1,
10498 				 pf->hw.dev_caps.num_vfs);
10499 			dev_info(&pf->pdev->dev,
10500 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10501 				 pf->hw.dev_caps.num_vsis,
10502 				 pf->hw.dev_caps.num_rx_qp,
10503 				 pf->hw.dev_caps.num_tx_qp);
10504 		}
10505 	}
10506 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10507 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10508 		       + pf->hw.func_caps.num_vfs)
10509 		if (pf->hw.revision_id == 0 &&
10510 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10511 			dev_info(&pf->pdev->dev,
10512 				 "got num_vsis %d, setting num_vsis to %d\n",
10513 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10514 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10515 		}
10516 	}
10517 	return 0;
10518 }
10519 
10520 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10521 
10522 /**
10523  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10524  * @pf: board private structure
10525  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10526 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10527 {
10528 	struct i40e_vsi *main_vsi, *vsi;
10529 
10530 	/* quick workaround for an NVM issue that leaves a critical register
10531 	 * uninitialized
10532 	 */
10533 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10534 		static const u32 hkey[] = {
10535 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10536 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10537 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10538 			0x95b3a76d};
10539 		int i;
10540 
10541 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10542 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10543 	}
10544 
10545 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10546 		return;
10547 
10548 	/* find existing VSI and see if it needs configuring */
10549 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10550 
10551 	/* create a new VSI if none exists */
10552 	if (!vsi) {
10553 		main_vsi = i40e_pf_get_main_vsi(pf);
10554 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10555 		if (!vsi) {
10556 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10557 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10558 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10559 			return;
10560 		}
10561 	}
10562 
10563 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10564 }
10565 
10566 /**
10567  * i40e_fdir_teardown - release the Flow Director resources
10568  * @pf: board private structure
10569  **/
i40e_fdir_teardown(struct i40e_pf * pf)10570 static void i40e_fdir_teardown(struct i40e_pf *pf)
10571 {
10572 	struct i40e_vsi *vsi;
10573 
10574 	i40e_fdir_filter_exit(pf);
10575 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10576 	if (vsi)
10577 		i40e_vsi_release(vsi);
10578 }
10579 
10580 /**
10581  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10582  * @vsi: PF main vsi
10583  * @seid: seid of main or channel VSIs
10584  *
10585  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10586  * existed before reset
10587  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10588 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10589 {
10590 	struct i40e_cloud_filter *cfilter;
10591 	struct i40e_pf *pf = vsi->back;
10592 	struct hlist_node *node;
10593 	int ret;
10594 
10595 	/* Add cloud filters back if they exist */
10596 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10597 				  cloud_node) {
10598 		if (cfilter->seid != seid)
10599 			continue;
10600 
10601 		if (cfilter->dst_port)
10602 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10603 								true);
10604 		else
10605 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10606 
10607 		if (ret) {
10608 			dev_dbg(&pf->pdev->dev,
10609 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10610 				ERR_PTR(ret),
10611 				libie_aq_str(pf->hw.aq.asq_last_status));
10612 			return ret;
10613 		}
10614 	}
10615 	return 0;
10616 }
10617 
10618 /**
10619  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10620  * @vsi: PF main vsi
10621  *
10622  * Rebuilds channel VSIs if they existed before reset
10623  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10624 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10625 {
10626 	struct i40e_channel *ch, *ch_tmp;
10627 	int ret;
10628 
10629 	if (list_empty(&vsi->ch_list))
10630 		return 0;
10631 
10632 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10633 		if (!ch->initialized)
10634 			break;
10635 		/* Proceed with creation of channel (VMDq2) VSI */
10636 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10637 		if (ret) {
10638 			dev_info(&vsi->back->pdev->dev,
10639 				 "failed to rebuild channels using uplink_seid %u\n",
10640 				 vsi->uplink_seid);
10641 			return ret;
10642 		}
10643 		/* Reconfigure TX queues using QTX_CTL register */
10644 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10645 		if (ret) {
10646 			dev_info(&vsi->back->pdev->dev,
10647 				 "failed to configure TX rings for channel %u\n",
10648 				 ch->seid);
10649 			return ret;
10650 		}
10651 		/* update 'next_base_queue' */
10652 		vsi->next_base_queue = vsi->next_base_queue +
10653 							ch->num_queue_pairs;
10654 		if (ch->max_tx_rate) {
10655 			u64 credits = ch->max_tx_rate;
10656 
10657 			if (i40e_set_bw_limit(vsi, ch->seid,
10658 					      ch->max_tx_rate))
10659 				return -EINVAL;
10660 
10661 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10662 			dev_dbg(&vsi->back->pdev->dev,
10663 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10664 				ch->max_tx_rate,
10665 				credits,
10666 				ch->seid);
10667 		}
10668 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10669 		if (ret) {
10670 			dev_dbg(&vsi->back->pdev->dev,
10671 				"Failed to rebuild cloud filters for channel VSI %u\n",
10672 				ch->seid);
10673 			return ret;
10674 		}
10675 	}
10676 	return 0;
10677 }
10678 
10679 /**
10680  * i40e_clean_xps_state - clean xps state for every tx_ring
10681  * @vsi: ptr to the VSI
10682  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10683 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10684 {
10685 	int i;
10686 
10687 	if (vsi->tx_rings)
10688 		for (i = 0; i < vsi->num_queue_pairs; i++)
10689 			if (vsi->tx_rings[i])
10690 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10691 					  vsi->tx_rings[i]->state);
10692 }
10693 
10694 /**
10695  * i40e_prep_for_reset - prep for the core to reset
10696  * @pf: board private structure
10697  *
10698  * Close up the VFs and other things in prep for PF Reset.
10699   **/
i40e_prep_for_reset(struct i40e_pf * pf)10700 static void i40e_prep_for_reset(struct i40e_pf *pf)
10701 {
10702 	struct i40e_hw *hw = &pf->hw;
10703 	struct i40e_vsi *vsi;
10704 	int ret = 0;
10705 	u32 v;
10706 
10707 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10708 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10709 		return;
10710 	if (i40e_check_asq_alive(&pf->hw))
10711 		i40e_vc_notify_reset(pf);
10712 
10713 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10714 
10715 	/* quiesce the VSIs and their queues that are not already DOWN */
10716 	i40e_pf_quiesce_all_vsi(pf);
10717 
10718 	i40e_pf_for_each_vsi(pf, v, vsi) {
10719 		i40e_clean_xps_state(vsi);
10720 		vsi->seid = 0;
10721 	}
10722 
10723 	i40e_shutdown_adminq(&pf->hw);
10724 
10725 	/* call shutdown HMC */
10726 	if (hw->hmc.hmc_obj) {
10727 		ret = i40e_shutdown_lan_hmc(hw);
10728 		if (ret)
10729 			dev_warn(&pf->pdev->dev,
10730 				 "shutdown_lan_hmc failed: %d\n", ret);
10731 	}
10732 
10733 	/* Save the current PTP time so that we can restore the time after the
10734 	 * reset completes.
10735 	 */
10736 	i40e_ptp_save_hw_time(pf);
10737 }
10738 
10739 /**
10740  * i40e_send_version - update firmware with driver version
10741  * @pf: PF struct
10742  */
i40e_send_version(struct i40e_pf * pf)10743 static void i40e_send_version(struct i40e_pf *pf)
10744 {
10745 	struct i40e_driver_version dv;
10746 
10747 	dv.major_version = 0xff;
10748 	dv.minor_version = 0xff;
10749 	dv.build_version = 0xff;
10750 	dv.subbuild_version = 0;
10751 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10752 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10753 }
10754 
10755 /**
10756  * i40e_get_oem_version - get OEM specific version information
10757  * @hw: pointer to the hardware structure
10758  **/
i40e_get_oem_version(struct i40e_hw * hw)10759 static void i40e_get_oem_version(struct i40e_hw *hw)
10760 {
10761 	u16 block_offset = 0xffff;
10762 	u16 block_length = 0;
10763 	u16 capabilities = 0;
10764 	u16 gen_snap = 0;
10765 	u16 release = 0;
10766 
10767 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10768 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10769 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10770 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10771 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10772 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10773 #define I40E_NVM_OEM_LENGTH			3
10774 
10775 	/* Check if pointer to OEM version block is valid. */
10776 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10777 	if (block_offset == 0xffff)
10778 		return;
10779 
10780 	/* Check if OEM version block has correct length. */
10781 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10782 			   &block_length);
10783 	if (block_length < I40E_NVM_OEM_LENGTH)
10784 		return;
10785 
10786 	/* Check if OEM version format is as expected. */
10787 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10788 			   &capabilities);
10789 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10790 		return;
10791 
10792 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10793 			   &gen_snap);
10794 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10795 			   &release);
10796 	hw->nvm.oem_ver =
10797 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10798 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10799 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10800 }
10801 
10802 /**
10803  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10804  * @pf: board private structure
10805  **/
i40e_reset(struct i40e_pf * pf)10806 static int i40e_reset(struct i40e_pf *pf)
10807 {
10808 	struct i40e_hw *hw = &pf->hw;
10809 	int ret;
10810 
10811 	ret = i40e_pf_reset(hw);
10812 	if (ret) {
10813 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10814 		set_bit(__I40E_RESET_FAILED, pf->state);
10815 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10816 	} else {
10817 		pf->pfr_count++;
10818 	}
10819 	return ret;
10820 }
10821 
10822 /**
10823  * i40e_rebuild - rebuild using a saved config
10824  * @pf: board private structure
10825  * @reinit: if the Main VSI needs to re-initialized.
10826  * @lock_acquired: indicates whether or not the lock has been acquired
10827  * before this function was called.
10828  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10829 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10830 {
10831 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10832 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10833 	struct i40e_hw *hw = &pf->hw;
10834 	struct i40e_veb *veb;
10835 	int ret;
10836 	u32 val;
10837 	int v;
10838 
10839 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10840 	    is_recovery_mode_reported)
10841 		i40e_set_ethtool_ops(vsi->netdev);
10842 
10843 	if (test_bit(__I40E_DOWN, pf->state) &&
10844 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10845 		goto clear_recovery;
10846 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10847 
10848 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10849 	ret = i40e_init_adminq(&pf->hw);
10850 	if (ret) {
10851 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10852 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10853 		goto clear_recovery;
10854 	}
10855 	i40e_get_oem_version(&pf->hw);
10856 
10857 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10858 		/* The following delay is necessary for firmware update. */
10859 		mdelay(1000);
10860 	}
10861 
10862 	/* re-verify the eeprom if we just had an EMP reset */
10863 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10864 		i40e_verify_eeprom(pf);
10865 
10866 	/* if we are going out of or into recovery mode we have to act
10867 	 * accordingly with regard to resources initialization
10868 	 * and deinitialization
10869 	 */
10870 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10871 		if (i40e_get_capabilities(pf,
10872 					  i40e_aqc_opc_list_func_capabilities))
10873 			goto end_unlock;
10874 
10875 		if (is_recovery_mode_reported) {
10876 			/* we're staying in recovery mode so we'll reinitialize
10877 			 * misc vector here
10878 			 */
10879 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10880 				goto end_unlock;
10881 		} else {
10882 			if (!lock_acquired)
10883 				rtnl_lock();
10884 			/* we're going out of recovery mode so we'll free
10885 			 * the IRQ allocated specifically for recovery mode
10886 			 * and restore the interrupt scheme
10887 			 */
10888 			free_irq(pf->pdev->irq, pf);
10889 			i40e_clear_interrupt_scheme(pf);
10890 			if (i40e_restore_interrupt_scheme(pf))
10891 				goto end_unlock;
10892 		}
10893 
10894 		/* tell the firmware that we're starting */
10895 		i40e_send_version(pf);
10896 
10897 		/* bail out in case recovery mode was detected, as there is
10898 		 * no need for further configuration.
10899 		 */
10900 		goto end_unlock;
10901 	}
10902 
10903 	i40e_clear_pxe_mode(hw);
10904 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10905 	if (ret)
10906 		goto end_core_reset;
10907 
10908 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10909 				hw->func_caps.num_rx_qp, 0, 0);
10910 	if (ret) {
10911 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10912 		goto end_core_reset;
10913 	}
10914 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10915 	if (ret) {
10916 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10917 		goto end_core_reset;
10918 	}
10919 
10920 #ifdef CONFIG_I40E_DCB
10921 	/* Enable FW to write a default DCB config on link-up
10922 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10923 	 * is not supported with new link speed
10924 	 */
10925 	if (i40e_is_tc_mqprio_enabled(pf)) {
10926 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10927 	} else {
10928 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10929 		    (hw->phy.link_info.link_speed &
10930 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10931 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10932 			dev_warn(&pf->pdev->dev,
10933 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10934 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10935 		} else {
10936 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10937 			ret = i40e_init_pf_dcb(pf);
10938 			if (ret) {
10939 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10940 					 ret);
10941 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10942 				/* Continue without DCB enabled */
10943 			}
10944 		}
10945 	}
10946 
10947 #endif /* CONFIG_I40E_DCB */
10948 	if (!lock_acquired)
10949 		rtnl_lock();
10950 	ret = i40e_setup_pf_switch(pf, reinit, true);
10951 	if (ret)
10952 		goto end_unlock;
10953 
10954 	/* The driver only wants link up/down and module qualification
10955 	 * reports from firmware.  Note the negative logic.
10956 	 */
10957 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10958 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10959 					 I40E_AQ_EVENT_MEDIA_NA |
10960 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10961 	if (ret)
10962 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10963 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10964 
10965 	/* Rebuild the VSIs and VEBs that existed before reset.
10966 	 * They are still in our local switch element arrays, so only
10967 	 * need to rebuild the switch model in the HW.
10968 	 *
10969 	 * If there were VEBs but the reconstitution failed, we'll try
10970 	 * to recover minimal use by getting the basic PF VSI working.
10971 	 */
10972 	if (vsi->uplink_seid != pf->mac_seid) {
10973 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10974 
10975 		/* Rebuild VEBs */
10976 		i40e_pf_for_each_veb(pf, v, veb) {
10977 			ret = i40e_reconstitute_veb(veb);
10978 			if (!ret)
10979 				continue;
10980 
10981 			/* If Main VEB failed, we're in deep doodoo,
10982 			 * so give up rebuilding the switch and set up
10983 			 * for minimal rebuild of PF VSI.
10984 			 * If orphan failed, we'll report the error
10985 			 * but try to keep going.
10986 			 */
10987 			if (veb->uplink_seid == pf->mac_seid) {
10988 				dev_info(&pf->pdev->dev,
10989 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10990 					 ret);
10991 				vsi->uplink_seid = pf->mac_seid;
10992 				break;
10993 			} else if (veb->uplink_seid == 0) {
10994 				dev_info(&pf->pdev->dev,
10995 					 "rebuild of orphan VEB failed: %d\n",
10996 					 ret);
10997 			}
10998 		}
10999 	}
11000 
11001 	if (vsi->uplink_seid == pf->mac_seid) {
11002 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11003 		/* no VEB, so rebuild only the Main VSI */
11004 		ret = i40e_add_vsi(vsi);
11005 		if (ret) {
11006 			dev_info(&pf->pdev->dev,
11007 				 "rebuild of Main VSI failed: %d\n", ret);
11008 			goto end_unlock;
11009 		}
11010 	}
11011 
11012 	if (vsi->mqprio_qopt.max_rate[0]) {
11013 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11014 						  vsi->mqprio_qopt.max_rate[0]);
11015 		u64 credits = 0;
11016 
11017 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11018 		if (ret)
11019 			goto end_unlock;
11020 
11021 		credits = max_tx_rate;
11022 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11023 		dev_dbg(&vsi->back->pdev->dev,
11024 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11025 			max_tx_rate,
11026 			credits,
11027 			vsi->seid);
11028 	}
11029 
11030 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11031 	if (ret)
11032 		goto end_unlock;
11033 
11034 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11035 	 * for this main VSI if they exist
11036 	 */
11037 	ret = i40e_rebuild_channels(vsi);
11038 	if (ret)
11039 		goto end_unlock;
11040 
11041 	/* Reconfigure hardware for allowing smaller MSS in the case
11042 	 * of TSO, so that we avoid the MDD being fired and causing
11043 	 * a reset in the case of small MSS+TSO.
11044 	 */
11045 #define I40E_REG_MSS          0x000E64DC
11046 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11047 #define I40E_64BYTE_MSS       0x400000
11048 	val = rd32(hw, I40E_REG_MSS);
11049 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11050 		val &= ~I40E_REG_MSS_MIN_MASK;
11051 		val |= I40E_64BYTE_MSS;
11052 		wr32(hw, I40E_REG_MSS, val);
11053 	}
11054 
11055 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11056 		msleep(75);
11057 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11058 		if (ret)
11059 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11060 				 ERR_PTR(ret),
11061 				 libie_aq_str(pf->hw.aq.asq_last_status));
11062 	}
11063 	/* reinit the misc interrupt */
11064 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11065 		ret = i40e_setup_misc_vector(pf);
11066 		if (ret)
11067 			goto end_unlock;
11068 	}
11069 
11070 	/* Add a filter to drop all Flow control frames from any VSI from being
11071 	 * transmitted. By doing so we stop a malicious VF from sending out
11072 	 * PAUSE or PFC frames and potentially controlling traffic for other
11073 	 * PF/VF VSIs.
11074 	 * The FW can still send Flow control frames if enabled.
11075 	 */
11076 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11077 						       pf->main_vsi_seid);
11078 
11079 	/* restart the VSIs that were rebuilt and running before the reset */
11080 	i40e_pf_unquiesce_all_vsi(pf);
11081 
11082 	/* Release the RTNL lock before we start resetting VFs */
11083 	if (!lock_acquired)
11084 		rtnl_unlock();
11085 
11086 	/* Restore promiscuous settings */
11087 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11088 	if (ret)
11089 		dev_warn(&pf->pdev->dev,
11090 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11091 			 pf->cur_promisc ? "on" : "off",
11092 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
11093 
11094 	i40e_reset_all_vfs(pf, true);
11095 
11096 	/* tell the firmware that we're starting */
11097 	i40e_send_version(pf);
11098 
11099 	/* We've already released the lock, so don't do it again */
11100 	goto end_core_reset;
11101 
11102 end_unlock:
11103 	if (!lock_acquired)
11104 		rtnl_unlock();
11105 end_core_reset:
11106 	clear_bit(__I40E_RESET_FAILED, pf->state);
11107 clear_recovery:
11108 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11109 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11110 }
11111 
11112 /**
11113  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11114  * @pf: board private structure
11115  * @reinit: if the Main VSI needs to re-initialized.
11116  * @lock_acquired: indicates whether or not the lock has been acquired
11117  * before this function was called.
11118  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11119 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11120 				   bool lock_acquired)
11121 {
11122 	int ret;
11123 
11124 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11125 		return;
11126 	/* Now we wait for GRST to settle out.
11127 	 * We don't have to delete the VEBs or VSIs from the hw switch
11128 	 * because the reset will make them disappear.
11129 	 */
11130 	ret = i40e_reset(pf);
11131 	if (!ret)
11132 		i40e_rebuild(pf, reinit, lock_acquired);
11133 	else
11134 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11135 }
11136 
11137 /**
11138  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11139  * @pf: board private structure
11140  *
11141  * Close up the VFs and other things in prep for a Core Reset,
11142  * then get ready to rebuild the world.
11143  * @lock_acquired: indicates whether or not the lock has been acquired
11144  * before this function was called.
11145  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11146 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11147 {
11148 	i40e_prep_for_reset(pf);
11149 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11150 }
11151 
11152 /**
11153  * i40e_print_vf_mdd_event - print VF Tx/Rx malicious driver detect event
11154  * @pf: board private structure
11155  * @vf: pointer to the VF structure
11156  * @is_tx: true - for Tx event, false - for  Rx
11157  */
i40e_print_vf_mdd_event(struct i40e_pf * pf,struct i40e_vf * vf,bool is_tx)11158 static void i40e_print_vf_mdd_event(struct i40e_pf *pf, struct i40e_vf *vf,
11159 				    bool is_tx)
11160 {
11161 	dev_err(&pf->pdev->dev, is_tx ?
11162 		"%lld Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n" :
11163 		"%lld Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n",
11164 		is_tx ? vf->mdd_tx_events.count : vf->mdd_rx_events.count,
11165 		pf->hw.pf_id,
11166 		vf->vf_id,
11167 		vf->default_lan_addr.addr,
11168 		str_on_off(test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)));
11169 }
11170 
11171 /**
11172  * i40e_print_vfs_mdd_events - print VFs malicious driver detect event
11173  * @pf: pointer to the PF structure
11174  *
11175  * Called from i40e_handle_mdd_event to rate limit and print VFs MDD events.
11176  */
i40e_print_vfs_mdd_events(struct i40e_pf * pf)11177 static void i40e_print_vfs_mdd_events(struct i40e_pf *pf)
11178 {
11179 	unsigned int i;
11180 
11181 	/* check that there are pending MDD events to print */
11182 	if (!test_and_clear_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state))
11183 		return;
11184 
11185 	if (!__ratelimit(&pf->mdd_message_rate_limit))
11186 		return;
11187 
11188 	for (i = 0; i < pf->num_alloc_vfs; i++) {
11189 		struct i40e_vf *vf = &pf->vf[i];
11190 		bool is_printed = false;
11191 
11192 		/* only print Rx MDD event message if there are new events */
11193 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
11194 			vf->mdd_rx_events.last_printed = vf->mdd_rx_events.count;
11195 			i40e_print_vf_mdd_event(pf, vf, false);
11196 			is_printed = true;
11197 		}
11198 
11199 		/* only print Tx MDD event message if there are new events */
11200 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
11201 			vf->mdd_tx_events.last_printed = vf->mdd_tx_events.count;
11202 			i40e_print_vf_mdd_event(pf, vf, true);
11203 			is_printed = true;
11204 		}
11205 
11206 		if (is_printed && !test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags))
11207 			dev_info(&pf->pdev->dev,
11208 				 "Use PF Control I/F to re-enable the VF #%d\n",
11209 				 i);
11210 	}
11211 }
11212 
11213 /**
11214  * i40e_handle_mdd_event
11215  * @pf: pointer to the PF structure
11216  *
11217  * Called from the MDD irq handler to identify possibly malicious vfs
11218  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11219 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11220 {
11221 	struct i40e_hw *hw = &pf->hw;
11222 	bool mdd_detected = false;
11223 	struct i40e_vf *vf;
11224 	u32 reg;
11225 	int i;
11226 
11227 	if (!test_and_clear_bit(__I40E_MDD_EVENT_PENDING, pf->state)) {
11228 		/* Since the VF MDD event logging is rate limited, check if
11229 		 * there are pending MDD events.
11230 		 */
11231 		i40e_print_vfs_mdd_events(pf);
11232 		return;
11233 	}
11234 
11235 	/* find what triggered the MDD event */
11236 	reg = rd32(hw, I40E_GL_MDET_TX);
11237 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11238 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11239 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11240 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11241 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11242 				pf->hw.func_caps.base_queue;
11243 		if (netif_msg_tx_err(pf))
11244 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11245 				 event, queue, pf_num, vf_num);
11246 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11247 		mdd_detected = true;
11248 	}
11249 	reg = rd32(hw, I40E_GL_MDET_RX);
11250 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11251 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11252 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11253 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11254 				pf->hw.func_caps.base_queue;
11255 		if (netif_msg_rx_err(pf))
11256 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11257 				 event, queue, func);
11258 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11259 		mdd_detected = true;
11260 	}
11261 
11262 	if (mdd_detected) {
11263 		reg = rd32(hw, I40E_PF_MDET_TX);
11264 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11265 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11266 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11267 		}
11268 		reg = rd32(hw, I40E_PF_MDET_RX);
11269 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11270 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11271 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11272 		}
11273 	}
11274 
11275 	/* see if one of the VFs needs its hand slapped */
11276 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11277 		bool is_mdd_on_tx = false;
11278 		bool is_mdd_on_rx = false;
11279 
11280 		vf = &(pf->vf[i]);
11281 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11282 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11283 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11284 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11285 			vf->mdd_tx_events.count++;
11286 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11287 			is_mdd_on_tx = true;
11288 		}
11289 
11290 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11291 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11292 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11293 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11294 			vf->mdd_rx_events.count++;
11295 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11296 			is_mdd_on_rx = true;
11297 		}
11298 
11299 		if ((is_mdd_on_tx || is_mdd_on_rx) &&
11300 		    test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
11301 			/* VF MDD event counters will be cleared by
11302 			 * reset, so print the event prior to reset.
11303 			 */
11304 			if (is_mdd_on_rx)
11305 				i40e_print_vf_mdd_event(pf, vf, false);
11306 			if (is_mdd_on_tx)
11307 				i40e_print_vf_mdd_event(pf, vf, true);
11308 
11309 			i40e_vc_reset_vf(vf, true);
11310 		}
11311 	}
11312 
11313 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11314 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11315 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11316 	i40e_flush(hw);
11317 
11318 	i40e_print_vfs_mdd_events(pf);
11319 }
11320 
11321 /**
11322  * i40e_service_task - Run the driver's async subtasks
11323  * @work: pointer to work_struct containing our data
11324  **/
i40e_service_task(struct work_struct * work)11325 static void i40e_service_task(struct work_struct *work)
11326 {
11327 	struct i40e_pf *pf = container_of(work,
11328 					  struct i40e_pf,
11329 					  service_task);
11330 	unsigned long start_time = jiffies;
11331 
11332 	/* don't bother with service tasks if a reset is in progress */
11333 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11334 	    test_bit(__I40E_SUSPENDED, pf->state))
11335 		return;
11336 
11337 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11338 		return;
11339 
11340 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11341 		i40e_detect_recover_hung(pf);
11342 		i40e_sync_filters_subtask(pf);
11343 		i40e_reset_subtask(pf);
11344 		i40e_handle_mdd_event(pf);
11345 		i40e_vc_process_vflr_event(pf);
11346 		i40e_watchdog_subtask(pf);
11347 		i40e_fdir_reinit_subtask(pf);
11348 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11349 			/* Client subtask will reopen next time through. */
11350 			i40e_notify_client_of_netdev_close(pf, true);
11351 		} else {
11352 			i40e_client_subtask(pf);
11353 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11354 					       pf->state))
11355 				i40e_notify_client_of_l2_param_changes(pf);
11356 		}
11357 		i40e_sync_filters_subtask(pf);
11358 	} else {
11359 		i40e_reset_subtask(pf);
11360 	}
11361 
11362 	i40e_clean_adminq_subtask(pf);
11363 
11364 	/* flush memory to make sure state is correct before next watchdog */
11365 	smp_mb__before_atomic();
11366 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11367 
11368 	/* If the tasks have taken longer than one timer cycle or there
11369 	 * is more work to be done, reschedule the service task now
11370 	 * rather than wait for the timer to tick again.
11371 	 */
11372 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11373 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11374 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11375 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11376 		i40e_service_event_schedule(pf);
11377 }
11378 
11379 /**
11380  * i40e_service_timer - timer callback
11381  * @t: timer list pointer
11382  **/
i40e_service_timer(struct timer_list * t)11383 static void i40e_service_timer(struct timer_list *t)
11384 {
11385 	struct i40e_pf *pf = timer_container_of(pf, t, service_timer);
11386 
11387 	mod_timer(&pf->service_timer,
11388 		  round_jiffies(jiffies + pf->service_timer_period));
11389 	i40e_service_event_schedule(pf);
11390 }
11391 
11392 /**
11393  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11394  * @vsi: the VSI being configured
11395  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11396 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11397 {
11398 	struct i40e_pf *pf = vsi->back;
11399 
11400 	switch (vsi->type) {
11401 	case I40E_VSI_MAIN:
11402 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11403 		if (!vsi->num_tx_desc)
11404 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11405 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11406 		if (!vsi->num_rx_desc)
11407 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11408 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11409 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11410 			vsi->num_q_vectors = pf->num_lan_msix;
11411 		else
11412 			vsi->num_q_vectors = 1;
11413 
11414 		break;
11415 
11416 	case I40E_VSI_FDIR:
11417 		vsi->alloc_queue_pairs = 1;
11418 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11419 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11420 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11421 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11422 		vsi->num_q_vectors = pf->num_fdsb_msix;
11423 		break;
11424 
11425 	case I40E_VSI_VMDQ2:
11426 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11427 		if (!vsi->num_tx_desc)
11428 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11429 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11430 		if (!vsi->num_rx_desc)
11431 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11432 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11433 		vsi->num_q_vectors = pf->num_vmdq_msix;
11434 		break;
11435 
11436 	case I40E_VSI_SRIOV:
11437 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11438 		if (!vsi->num_tx_desc)
11439 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11440 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11441 		if (!vsi->num_rx_desc)
11442 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11443 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11444 		break;
11445 
11446 	default:
11447 		WARN_ON(1);
11448 		return -ENODATA;
11449 	}
11450 
11451 	if (is_kdump_kernel()) {
11452 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11453 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11454 	}
11455 
11456 	return 0;
11457 }
11458 
11459 /**
11460  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11461  * @vsi: VSI pointer
11462  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11463  *
11464  * On error: returns error code (negative)
11465  * On success: returns 0
11466  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11467 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11468 {
11469 	struct i40e_ring **next_rings;
11470 	int size;
11471 	int ret = 0;
11472 
11473 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11474 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11475 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11476 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11477 	if (!vsi->tx_rings)
11478 		return -ENOMEM;
11479 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11480 	if (i40e_enabled_xdp_vsi(vsi)) {
11481 		vsi->xdp_rings = next_rings;
11482 		next_rings += vsi->alloc_queue_pairs;
11483 	}
11484 	vsi->rx_rings = next_rings;
11485 
11486 	if (alloc_qvectors) {
11487 		/* allocate memory for q_vector pointers */
11488 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11489 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11490 		if (!vsi->q_vectors) {
11491 			ret = -ENOMEM;
11492 			goto err_vectors;
11493 		}
11494 	}
11495 	return ret;
11496 
11497 err_vectors:
11498 	kfree(vsi->tx_rings);
11499 	return ret;
11500 }
11501 
11502 /**
11503  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11504  * @pf: board private structure
11505  * @type: type of VSI
11506  *
11507  * On error: returns error code (negative)
11508  * On success: returns vsi index in PF (positive)
11509  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11510 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11511 {
11512 	int ret = -ENODEV;
11513 	struct i40e_vsi *vsi;
11514 	int vsi_idx;
11515 	int i;
11516 
11517 	/* Need to protect the allocation of the VSIs at the PF level */
11518 	mutex_lock(&pf->switch_mutex);
11519 
11520 	/* VSI list may be fragmented if VSI creation/destruction has
11521 	 * been happening.  We can afford to do a quick scan to look
11522 	 * for any free VSIs in the list.
11523 	 *
11524 	 * find next empty vsi slot, looping back around if necessary
11525 	 */
11526 	i = pf->next_vsi;
11527 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11528 		i++;
11529 	if (i >= pf->num_alloc_vsi) {
11530 		i = 0;
11531 		while (i < pf->next_vsi && pf->vsi[i])
11532 			i++;
11533 	}
11534 
11535 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11536 		vsi_idx = i;             /* Found one! */
11537 	} else {
11538 		ret = -ENODEV;
11539 		goto unlock_pf;  /* out of VSI slots! */
11540 	}
11541 	pf->next_vsi = ++i;
11542 
11543 	vsi = kzalloc_obj(*vsi);
11544 	if (!vsi) {
11545 		ret = -ENOMEM;
11546 		goto unlock_pf;
11547 	}
11548 	vsi->type = type;
11549 	vsi->back = pf;
11550 	set_bit(__I40E_VSI_DOWN, vsi->state);
11551 	vsi->flags = 0;
11552 	vsi->idx = vsi_idx;
11553 	vsi->int_rate_limit = 0;
11554 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11555 				pf->rss_table_size : 64;
11556 	vsi->netdev_registered = false;
11557 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11558 	hash_init(vsi->mac_filter_hash);
11559 	vsi->irqs_ready = false;
11560 
11561 	if (type == I40E_VSI_MAIN) {
11562 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11563 		if (!vsi->af_xdp_zc_qps)
11564 			goto err_rings;
11565 	}
11566 
11567 	ret = i40e_set_num_rings_in_vsi(vsi);
11568 	if (ret)
11569 		goto err_rings;
11570 
11571 	ret = i40e_vsi_alloc_arrays(vsi, true);
11572 	if (ret)
11573 		goto err_rings;
11574 
11575 	/* Setup default MSIX irq handler for VSI */
11576 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11577 
11578 	/* Initialize VSI lock */
11579 	spin_lock_init(&vsi->mac_filter_hash_lock);
11580 	pf->vsi[vsi_idx] = vsi;
11581 	ret = vsi_idx;
11582 	goto unlock_pf;
11583 
11584 err_rings:
11585 	bitmap_free(vsi->af_xdp_zc_qps);
11586 	pf->next_vsi = i - 1;
11587 	kfree(vsi);
11588 unlock_pf:
11589 	mutex_unlock(&pf->switch_mutex);
11590 	return ret;
11591 }
11592 
11593 /**
11594  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11595  * @vsi: VSI pointer
11596  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11597  *
11598  * On error: returns error code (negative)
11599  * On success: returns 0
11600  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11601 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11602 {
11603 	/* free the ring and vector containers */
11604 	if (free_qvectors) {
11605 		kfree(vsi->q_vectors);
11606 		vsi->q_vectors = NULL;
11607 	}
11608 	kfree(vsi->tx_rings);
11609 	vsi->tx_rings = NULL;
11610 	vsi->rx_rings = NULL;
11611 	vsi->xdp_rings = NULL;
11612 }
11613 
11614 /**
11615  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11616  * and lookup table
11617  * @vsi: Pointer to VSI structure
11618  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11619 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11620 {
11621 	if (!vsi)
11622 		return;
11623 
11624 	kfree(vsi->rss_hkey_user);
11625 	vsi->rss_hkey_user = NULL;
11626 
11627 	kfree(vsi->rss_lut_user);
11628 	vsi->rss_lut_user = NULL;
11629 }
11630 
11631 /**
11632  * i40e_vsi_clear - Deallocate the VSI provided
11633  * @vsi: the VSI being un-configured
11634  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11635 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11636 {
11637 	struct i40e_pf *pf;
11638 
11639 	if (!vsi)
11640 		return 0;
11641 
11642 	if (!vsi->back)
11643 		goto free_vsi;
11644 	pf = vsi->back;
11645 
11646 	mutex_lock(&pf->switch_mutex);
11647 	if (!pf->vsi[vsi->idx]) {
11648 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11649 			vsi->idx, vsi->idx, vsi->type);
11650 		goto unlock_vsi;
11651 	}
11652 
11653 	if (pf->vsi[vsi->idx] != vsi) {
11654 		dev_err(&pf->pdev->dev,
11655 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11656 			pf->vsi[vsi->idx]->idx,
11657 			pf->vsi[vsi->idx]->type,
11658 			vsi->idx, vsi->type);
11659 		goto unlock_vsi;
11660 	}
11661 
11662 	/* updates the PF for this cleared vsi */
11663 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11664 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11665 
11666 	bitmap_free(vsi->af_xdp_zc_qps);
11667 	i40e_vsi_free_arrays(vsi, true);
11668 	i40e_clear_rss_config_user(vsi);
11669 
11670 	pf->vsi[vsi->idx] = NULL;
11671 	if (vsi->idx < pf->next_vsi)
11672 		pf->next_vsi = vsi->idx;
11673 
11674 unlock_vsi:
11675 	mutex_unlock(&pf->switch_mutex);
11676 free_vsi:
11677 	kfree(vsi);
11678 
11679 	return 0;
11680 }
11681 
11682 /**
11683  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11684  * @vsi: the VSI being cleaned
11685  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11686 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11687 {
11688 	int i;
11689 
11690 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11691 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11692 			kfree_rcu(vsi->tx_rings[i], rcu);
11693 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11694 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11695 			if (vsi->xdp_rings)
11696 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11697 		}
11698 	}
11699 }
11700 
11701 /**
11702  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11703  * @vsi: the VSI being configured
11704  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11705 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11706 {
11707 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11708 	struct i40e_pf *pf = vsi->back;
11709 	struct i40e_ring *ring;
11710 
11711 	/* Set basic values in the rings to be used later during open() */
11712 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11713 		/* allocate space for both Tx and Rx in one shot */
11714 		ring = kzalloc_objs(struct i40e_ring, qpv);
11715 		if (!ring)
11716 			goto err_out;
11717 
11718 		ring->queue_index = i;
11719 		ring->reg_idx = vsi->base_queue + i;
11720 		ring->ring_active = false;
11721 		ring->vsi = vsi;
11722 		ring->netdev = vsi->netdev;
11723 		ring->dev = &pf->pdev->dev;
11724 		ring->count = vsi->num_tx_desc;
11725 		ring->size = 0;
11726 		ring->dcb_tc = 0;
11727 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11728 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11729 		ring->itr_setting = pf->tx_itr_default;
11730 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11731 
11732 		if (!i40e_enabled_xdp_vsi(vsi))
11733 			goto setup_rx;
11734 
11735 		ring->queue_index = vsi->alloc_queue_pairs + i;
11736 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11737 		ring->ring_active = false;
11738 		ring->vsi = vsi;
11739 		ring->netdev = NULL;
11740 		ring->dev = &pf->pdev->dev;
11741 		ring->count = vsi->num_tx_desc;
11742 		ring->size = 0;
11743 		ring->dcb_tc = 0;
11744 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11745 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11746 		set_ring_xdp(ring);
11747 		ring->itr_setting = pf->tx_itr_default;
11748 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11749 
11750 setup_rx:
11751 		ring->queue_index = i;
11752 		ring->reg_idx = vsi->base_queue + i;
11753 		ring->ring_active = false;
11754 		ring->vsi = vsi;
11755 		ring->netdev = vsi->netdev;
11756 		ring->dev = &pf->pdev->dev;
11757 		ring->count = vsi->num_rx_desc;
11758 		ring->size = 0;
11759 		ring->dcb_tc = 0;
11760 		ring->itr_setting = pf->rx_itr_default;
11761 		WRITE_ONCE(vsi->rx_rings[i], ring);
11762 	}
11763 
11764 	return 0;
11765 
11766 err_out:
11767 	i40e_vsi_clear_rings(vsi);
11768 	return -ENOMEM;
11769 }
11770 
11771 /**
11772  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11773  * @pf: board private structure
11774  * @vectors: the number of MSI-X vectors to request
11775  *
11776  * Returns the number of vectors reserved, or error
11777  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11778 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11779 {
11780 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11781 					I40E_MIN_MSIX, vectors);
11782 	if (vectors < 0) {
11783 		dev_info(&pf->pdev->dev,
11784 			 "MSI-X vector reservation failed: %d\n", vectors);
11785 		vectors = 0;
11786 	}
11787 
11788 	return vectors;
11789 }
11790 
11791 /**
11792  * i40e_init_msix - Setup the MSIX capability
11793  * @pf: board private structure
11794  *
11795  * Work with the OS to set up the MSIX vectors needed.
11796  *
11797  * Returns the number of vectors reserved or negative on failure
11798  **/
i40e_init_msix(struct i40e_pf * pf)11799 static int i40e_init_msix(struct i40e_pf *pf)
11800 {
11801 	struct i40e_hw *hw = &pf->hw;
11802 	int cpus, extra_vectors;
11803 	int vectors_left;
11804 	int v_budget, i;
11805 	int v_actual;
11806 	int iwarp_requested = 0;
11807 
11808 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11809 		return -ENODEV;
11810 
11811 	/* The number of vectors we'll request will be comprised of:
11812 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11813 	 *   - The number of LAN queue pairs
11814 	 *	- Queues being used for RSS.
11815 	 *		We don't need as many as max_rss_size vectors.
11816 	 *		use rss_size instead in the calculation since that
11817 	 *		is governed by number of cpus in the system.
11818 	 *	- assumes symmetric Tx/Rx pairing
11819 	 *   - The number of VMDq pairs
11820 	 *   - The CPU count within the NUMA node if iWARP is enabled
11821 	 * Once we count this up, try the request.
11822 	 *
11823 	 * If we can't get what we want, we'll simplify to nearly nothing
11824 	 * and try again.  If that still fails, we punt.
11825 	 */
11826 	vectors_left = hw->func_caps.num_msix_vectors;
11827 	v_budget = 0;
11828 
11829 	/* reserve one vector for miscellaneous handler */
11830 	if (vectors_left) {
11831 		v_budget++;
11832 		vectors_left--;
11833 	}
11834 
11835 	/* reserve some vectors for the main PF traffic queues. Initially we
11836 	 * only reserve at most 50% of the available vectors, in the case that
11837 	 * the number of online CPUs is large. This ensures that we can enable
11838 	 * extra features as well. Once we've enabled the other features, we
11839 	 * will use any remaining vectors to reach as close as we can to the
11840 	 * number of online CPUs.
11841 	 */
11842 	cpus = num_online_cpus();
11843 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11844 	vectors_left -= pf->num_lan_msix;
11845 
11846 	/* reserve one vector for sideband flow director */
11847 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11848 		if (vectors_left) {
11849 			pf->num_fdsb_msix = 1;
11850 			v_budget++;
11851 			vectors_left--;
11852 		} else {
11853 			pf->num_fdsb_msix = 0;
11854 		}
11855 	}
11856 
11857 	/* can we reserve enough for iWARP? */
11858 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11859 		iwarp_requested = pf->num_iwarp_msix;
11860 
11861 		if (!vectors_left)
11862 			pf->num_iwarp_msix = 0;
11863 		else if (vectors_left < pf->num_iwarp_msix)
11864 			pf->num_iwarp_msix = 1;
11865 		v_budget += pf->num_iwarp_msix;
11866 		vectors_left -= pf->num_iwarp_msix;
11867 	}
11868 
11869 	/* any vectors left over go for VMDq support */
11870 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11871 		if (!vectors_left) {
11872 			pf->num_vmdq_msix = 0;
11873 			pf->num_vmdq_qps = 0;
11874 		} else {
11875 			int vmdq_vecs_wanted =
11876 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11877 			int vmdq_vecs =
11878 				min_t(int, vectors_left, vmdq_vecs_wanted);
11879 
11880 			/* if we're short on vectors for what's desired, we limit
11881 			 * the queues per vmdq.  If this is still more than are
11882 			 * available, the user will need to change the number of
11883 			 * queues/vectors used by the PF later with the ethtool
11884 			 * channels command
11885 			 */
11886 			if (vectors_left < vmdq_vecs_wanted) {
11887 				pf->num_vmdq_qps = 1;
11888 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11889 				vmdq_vecs = min_t(int,
11890 						  vectors_left,
11891 						  vmdq_vecs_wanted);
11892 			}
11893 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11894 
11895 			v_budget += vmdq_vecs;
11896 			vectors_left -= vmdq_vecs;
11897 		}
11898 	}
11899 
11900 	/* On systems with a large number of SMP cores, we previously limited
11901 	 * the number of vectors for num_lan_msix to be at most 50% of the
11902 	 * available vectors, to allow for other features. Now, we add back
11903 	 * the remaining vectors. However, we ensure that the total
11904 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11905 	 * calculate the number of vectors we can add without going over the
11906 	 * cap of CPUs. For systems with a small number of CPUs this will be
11907 	 * zero.
11908 	 */
11909 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11910 	pf->num_lan_msix += extra_vectors;
11911 	vectors_left -= extra_vectors;
11912 
11913 	WARN(vectors_left < 0,
11914 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11915 
11916 	v_budget += pf->num_lan_msix;
11917 	pf->msix_entries = kzalloc_objs(struct msix_entry, v_budget);
11918 	if (!pf->msix_entries)
11919 		return -ENOMEM;
11920 
11921 	for (i = 0; i < v_budget; i++)
11922 		pf->msix_entries[i].entry = i;
11923 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11924 
11925 	if (v_actual < I40E_MIN_MSIX) {
11926 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11927 		kfree(pf->msix_entries);
11928 		pf->msix_entries = NULL;
11929 		pci_disable_msix(pf->pdev);
11930 		return -ENODEV;
11931 
11932 	} else if (v_actual == I40E_MIN_MSIX) {
11933 		/* Adjust for minimal MSIX use */
11934 		pf->num_vmdq_vsis = 0;
11935 		pf->num_vmdq_qps = 0;
11936 		pf->num_lan_qps = 1;
11937 		pf->num_lan_msix = 1;
11938 
11939 	} else if (v_actual != v_budget) {
11940 		/* If we have limited resources, we will start with no vectors
11941 		 * for the special features and then allocate vectors to some
11942 		 * of these features based on the policy and at the end disable
11943 		 * the features that did not get any vectors.
11944 		 */
11945 		int vec;
11946 
11947 		dev_info(&pf->pdev->dev,
11948 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11949 			 v_actual, v_budget);
11950 		/* reserve the misc vector */
11951 		vec = v_actual - 1;
11952 
11953 		/* Scale vector usage down */
11954 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11955 		pf->num_vmdq_vsis = 1;
11956 		pf->num_vmdq_qps = 1;
11957 
11958 		/* partition out the remaining vectors */
11959 		switch (vec) {
11960 		case 2:
11961 			pf->num_lan_msix = 1;
11962 			break;
11963 		case 3:
11964 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11965 				pf->num_lan_msix = 1;
11966 				pf->num_iwarp_msix = 1;
11967 			} else {
11968 				pf->num_lan_msix = 2;
11969 			}
11970 			break;
11971 		default:
11972 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11973 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11974 						 iwarp_requested);
11975 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11976 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11977 			} else {
11978 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11979 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11980 			}
11981 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11982 				pf->num_fdsb_msix = 1;
11983 				vec--;
11984 			}
11985 			pf->num_lan_msix = min_t(int,
11986 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11987 							      pf->num_lan_msix);
11988 			pf->num_lan_qps = pf->num_lan_msix;
11989 			break;
11990 		}
11991 	}
11992 
11993 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11994 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11995 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11996 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11997 	}
11998 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11999 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
12000 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12001 	}
12002 
12003 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
12004 	    pf->num_iwarp_msix == 0) {
12005 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
12006 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12007 	}
12008 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
12009 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
12010 		   pf->num_lan_msix,
12011 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
12012 		   pf->num_fdsb_msix,
12013 		   pf->num_iwarp_msix);
12014 
12015 	return v_actual;
12016 }
12017 
12018 /**
12019  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12020  * @vsi: the VSI being configured
12021  * @v_idx: index of the vector in the vsi struct
12022  *
12023  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12024  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)12025 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12026 {
12027 	struct i40e_q_vector *q_vector;
12028 
12029 	/* allocate q_vector */
12030 	q_vector = kzalloc_obj(struct i40e_q_vector);
12031 	if (!q_vector)
12032 		return -ENOMEM;
12033 
12034 	q_vector->vsi = vsi;
12035 	q_vector->v_idx = v_idx;
12036 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12037 
12038 	if (vsi->netdev)
12039 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12040 
12041 	/* tie q_vector and vsi together */
12042 	vsi->q_vectors[v_idx] = q_vector;
12043 
12044 	return 0;
12045 }
12046 
12047 /**
12048  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12049  * @vsi: the VSI being configured
12050  *
12051  * We allocate one q_vector per queue interrupt.  If allocation fails we
12052  * return -ENOMEM.
12053  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)12054 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12055 {
12056 	struct i40e_pf *pf = vsi->back;
12057 	int err, v_idx, num_q_vectors;
12058 
12059 	/* if not MSIX, give the one vector only to the LAN VSI */
12060 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12061 		num_q_vectors = vsi->num_q_vectors;
12062 	else if (vsi->type == I40E_VSI_MAIN)
12063 		num_q_vectors = 1;
12064 	else
12065 		return -EINVAL;
12066 
12067 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12068 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12069 		if (err)
12070 			goto err_out;
12071 	}
12072 
12073 	return 0;
12074 
12075 err_out:
12076 	while (v_idx--)
12077 		i40e_free_q_vector(vsi, v_idx);
12078 
12079 	return err;
12080 }
12081 
12082 /**
12083  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12084  * @pf: board private structure to initialize
12085  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12086 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12087 {
12088 	int vectors = 0;
12089 	ssize_t size;
12090 
12091 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12092 		vectors = i40e_init_msix(pf);
12093 		if (vectors < 0) {
12094 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12095 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12096 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12097 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12098 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12099 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12100 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12101 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12102 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12103 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12104 
12105 			/* rework the queue expectations without MSIX */
12106 			i40e_determine_queue_usage(pf);
12107 		}
12108 	}
12109 
12110 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12111 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12112 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12113 		vectors = pci_enable_msi(pf->pdev);
12114 		if (vectors < 0) {
12115 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12116 				 vectors);
12117 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12118 		}
12119 		vectors = 1;  /* one MSI or Legacy vector */
12120 	}
12121 
12122 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12123 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12124 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12125 
12126 	/* set up vector assignment tracking */
12127 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12128 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12129 	if (!pf->irq_pile)
12130 		return -ENOMEM;
12131 
12132 	pf->irq_pile->num_entries = vectors;
12133 
12134 	/* track first vector for misc interrupts, ignore return */
12135 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12136 
12137 	return 0;
12138 }
12139 
12140 /**
12141  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12142  * @pf: private board data structure
12143  *
12144  * Restore the interrupt scheme that was cleared when we suspended the
12145  * device. This should be called during resume to re-allocate the q_vectors
12146  * and reacquire IRQs.
12147  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12148 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12149 {
12150 	struct i40e_vsi *vsi;
12151 	int err, i;
12152 
12153 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12154 	 * scheme. We need to re-enabled them here in order to attempt to
12155 	 * re-acquire the MSI or MSI-X vectors
12156 	 */
12157 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12158 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12159 
12160 	err = i40e_init_interrupt_scheme(pf);
12161 	if (err)
12162 		return err;
12163 
12164 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12165 	 * rings together again.
12166 	 */
12167 	i40e_pf_for_each_vsi(pf, i, vsi) {
12168 		err = i40e_vsi_alloc_q_vectors(vsi);
12169 		if (err)
12170 			goto err_unwind;
12171 
12172 		i40e_vsi_map_rings_to_vectors(vsi);
12173 	}
12174 
12175 	err = i40e_setup_misc_vector(pf);
12176 	if (err)
12177 		goto err_unwind;
12178 
12179 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12180 		i40e_client_update_msix_info(pf);
12181 
12182 	return 0;
12183 
12184 err_unwind:
12185 	while (i--) {
12186 		if (pf->vsi[i])
12187 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12188 	}
12189 
12190 	return err;
12191 }
12192 
12193 /**
12194  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12195  * non queue events in recovery mode
12196  * @pf: board private structure
12197  *
12198  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12199  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12200  * This is handled differently than in recovery mode since no Tx/Rx resources
12201  * are being allocated.
12202  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12203 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12204 {
12205 	int err;
12206 
12207 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12208 		err = i40e_setup_misc_vector(pf);
12209 
12210 		if (err) {
12211 			dev_info(&pf->pdev->dev,
12212 				 "MSI-X misc vector request failed, error %d\n",
12213 				 err);
12214 			return err;
12215 		}
12216 	} else {
12217 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12218 
12219 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12220 				  pf->int_name, pf);
12221 
12222 		if (err) {
12223 			dev_info(&pf->pdev->dev,
12224 				 "MSI/legacy misc vector request failed, error %d\n",
12225 				 err);
12226 			return err;
12227 		}
12228 		i40e_enable_misc_int_causes(pf);
12229 		i40e_irq_dynamic_enable_icr0(pf);
12230 	}
12231 
12232 	return 0;
12233 }
12234 
12235 /**
12236  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12237  * @pf: board private structure
12238  *
12239  * This sets up the handler for MSIX 0, which is used to manage the
12240  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12241  * when in MSI or Legacy interrupt mode.
12242  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12243 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12244 {
12245 	struct i40e_hw *hw = &pf->hw;
12246 	int err = 0;
12247 
12248 	/* Only request the IRQ once, the first time through. */
12249 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12250 		err = request_irq(pf->msix_entries[0].vector,
12251 				  i40e_intr, 0, pf->int_name, pf);
12252 		if (err) {
12253 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12254 			dev_info(&pf->pdev->dev,
12255 				 "request_irq for %s failed: %d\n",
12256 				 pf->int_name, err);
12257 			return -EFAULT;
12258 		}
12259 	}
12260 
12261 	i40e_enable_misc_int_causes(pf);
12262 
12263 	/* associate no queues to the misc vector */
12264 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12265 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12266 
12267 	i40e_flush(hw);
12268 
12269 	i40e_irq_dynamic_enable_icr0(pf);
12270 
12271 	return err;
12272 }
12273 
12274 /**
12275  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12276  * @vsi: Pointer to vsi structure
12277  * @seed: Buffter to store the hash keys
12278  * @lut: Buffer to store the lookup table entries
12279  * @lut_size: Size of buffer to store the lookup table entries
12280  *
12281  * Return 0 on success, negative on failure
12282  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12283 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12284 			   u8 *lut, u16 lut_size)
12285 {
12286 	struct i40e_pf *pf = vsi->back;
12287 	struct i40e_hw *hw = &pf->hw;
12288 	int ret = 0;
12289 
12290 	if (seed) {
12291 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12292 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12293 		if (ret) {
12294 			dev_info(&pf->pdev->dev,
12295 				 "Cannot get RSS key, err %pe aq_err %s\n",
12296 				 ERR_PTR(ret),
12297 				 libie_aq_str(pf->hw.aq.asq_last_status));
12298 			return ret;
12299 		}
12300 	}
12301 
12302 	if (lut) {
12303 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12304 
12305 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12306 		if (ret) {
12307 			dev_info(&pf->pdev->dev,
12308 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12309 				 ERR_PTR(ret),
12310 				 libie_aq_str(pf->hw.aq.asq_last_status));
12311 			return ret;
12312 		}
12313 	}
12314 
12315 	return ret;
12316 }
12317 
12318 /**
12319  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12320  * @vsi: Pointer to vsi structure
12321  * @seed: RSS hash seed
12322  * @lut: Lookup table
12323  * @lut_size: Lookup table size
12324  *
12325  * Returns 0 on success, negative on failure
12326  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12327 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12328 			       const u8 *lut, u16 lut_size)
12329 {
12330 	struct i40e_pf *pf = vsi->back;
12331 	struct i40e_hw *hw = &pf->hw;
12332 	u16 vf_id = vsi->vf_id;
12333 	u8 i;
12334 
12335 	/* Fill out hash function seed */
12336 	if (seed) {
12337 		u32 *seed_dw = (u32 *)seed;
12338 
12339 		if (vsi->type == I40E_VSI_MAIN) {
12340 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12341 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12342 		} else if (vsi->type == I40E_VSI_SRIOV) {
12343 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12344 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12345 		} else {
12346 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12347 		}
12348 	}
12349 
12350 	if (lut) {
12351 		u32 *lut_dw = (u32 *)lut;
12352 
12353 		if (vsi->type == I40E_VSI_MAIN) {
12354 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12355 				return -EINVAL;
12356 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12357 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12358 		} else if (vsi->type == I40E_VSI_SRIOV) {
12359 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12360 				return -EINVAL;
12361 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12362 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12363 		} else {
12364 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12365 		}
12366 	}
12367 	i40e_flush(hw);
12368 
12369 	return 0;
12370 }
12371 
12372 /**
12373  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12374  * @vsi: Pointer to VSI structure
12375  * @seed: Buffer to store the keys
12376  * @lut: Buffer to store the lookup table entries
12377  * @lut_size: Size of buffer to store the lookup table entries
12378  *
12379  * Returns 0 on success, negative on failure
12380  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12381 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12382 			    u8 *lut, u16 lut_size)
12383 {
12384 	struct i40e_pf *pf = vsi->back;
12385 	struct i40e_hw *hw = &pf->hw;
12386 	u16 i;
12387 
12388 	if (seed) {
12389 		u32 *seed_dw = (u32 *)seed;
12390 
12391 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12392 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12393 	}
12394 	if (lut) {
12395 		u32 *lut_dw = (u32 *)lut;
12396 
12397 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12398 			return -EINVAL;
12399 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12400 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12401 	}
12402 
12403 	return 0;
12404 }
12405 
12406 /**
12407  * i40e_config_rss - Configure RSS keys and lut
12408  * @vsi: Pointer to VSI structure
12409  * @seed: RSS hash seed
12410  * @lut: Lookup table
12411  * @lut_size: Lookup table size
12412  *
12413  * Returns 0 on success, negative on failure
12414  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12415 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12416 {
12417 	struct i40e_pf *pf = vsi->back;
12418 
12419 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12420 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12421 	else
12422 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12423 }
12424 
12425 /**
12426  * i40e_get_rss - Get RSS keys and lut
12427  * @vsi: Pointer to VSI structure
12428  * @seed: Buffer to store the keys
12429  * @lut: Buffer to store the lookup table entries
12430  * @lut_size: Size of buffer to store the lookup table entries
12431  *
12432  * Returns 0 on success, negative on failure
12433  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12434 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12435 {
12436 	struct i40e_pf *pf = vsi->back;
12437 
12438 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12439 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12440 	else
12441 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12442 }
12443 
12444 /**
12445  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12446  * @pf: Pointer to board private structure
12447  * @lut: Lookup table
12448  * @rss_table_size: Lookup table size
12449  * @rss_size: Range of queue number for hashing
12450  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12451 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12452 		       u16 rss_table_size, u16 rss_size)
12453 {
12454 	u16 i;
12455 
12456 	for (i = 0; i < rss_table_size; i++)
12457 		lut[i] = i % rss_size;
12458 }
12459 
12460 /**
12461  * i40e_pf_config_rss - Prepare for RSS if used
12462  * @pf: board private structure
12463  **/
i40e_pf_config_rss(struct i40e_pf * pf)12464 static int i40e_pf_config_rss(struct i40e_pf *pf)
12465 {
12466 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12467 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12468 	u8 *lut;
12469 	struct i40e_hw *hw = &pf->hw;
12470 	u32 reg_val;
12471 	u64 hena;
12472 	int ret;
12473 
12474 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12475 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12476 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12477 	hena |= i40e_pf_get_default_rss_hashcfg(pf);
12478 
12479 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12480 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12481 
12482 	/* Determine the RSS table size based on the hardware capabilities */
12483 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12484 	reg_val = (pf->rss_table_size == 512) ?
12485 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12486 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12487 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12488 
12489 	/* Determine the RSS size of the VSI */
12490 	if (!vsi->rss_size) {
12491 		u16 qcount;
12492 		/* If the firmware does something weird during VSI init, we
12493 		 * could end up with zero TCs. Check for that to avoid
12494 		 * divide-by-zero. It probably won't pass traffic, but it also
12495 		 * won't panic.
12496 		 */
12497 		qcount = vsi->num_queue_pairs /
12498 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12499 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12500 	}
12501 	if (!vsi->rss_size)
12502 		return -EINVAL;
12503 
12504 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12505 	if (!lut)
12506 		return -ENOMEM;
12507 
12508 	/* Use user configured lut if there is one, otherwise use default */
12509 	if (vsi->rss_lut_user)
12510 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12511 	else
12512 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12513 
12514 	/* Use user configured hash key if there is one, otherwise
12515 	 * use default.
12516 	 */
12517 	if (vsi->rss_hkey_user)
12518 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12519 	else
12520 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12521 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12522 	kfree(lut);
12523 
12524 	return ret;
12525 }
12526 
12527 /**
12528  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12529  * @pf: board private structure
12530  * @queue_count: the requested queue count for rss.
12531  *
12532  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12533  * count which may be different from the requested queue count.
12534  * Note: expects to be called while under rtnl_lock()
12535  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12536 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12537 {
12538 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12539 	int new_rss_size;
12540 
12541 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12542 		return 0;
12543 
12544 	queue_count = min_t(int, queue_count, num_online_cpus());
12545 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12546 
12547 	if (queue_count != vsi->num_queue_pairs) {
12548 		u16 qcount;
12549 
12550 		vsi->req_queue_pairs = queue_count;
12551 		i40e_prep_for_reset(pf);
12552 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12553 			return pf->alloc_rss_size;
12554 
12555 		pf->alloc_rss_size = new_rss_size;
12556 
12557 		i40e_reset_and_rebuild(pf, true, true);
12558 
12559 		/* Discard the user configured hash keys and lut, if less
12560 		 * queues are enabled.
12561 		 */
12562 		if (queue_count < vsi->rss_size) {
12563 			i40e_clear_rss_config_user(vsi);
12564 			dev_dbg(&pf->pdev->dev,
12565 				"discard user configured hash keys and lut\n");
12566 		}
12567 
12568 		/* Reset vsi->rss_size, as number of enabled queues changed */
12569 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12570 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12571 
12572 		i40e_pf_config_rss(pf);
12573 	}
12574 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12575 		 vsi->req_queue_pairs, pf->rss_size_max);
12576 	return pf->alloc_rss_size;
12577 }
12578 
12579 /**
12580  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12581  * @pf: board private structure
12582  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12583 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12584 {
12585 	bool min_valid, max_valid;
12586 	u32 max_bw, min_bw;
12587 	int status;
12588 
12589 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12590 					   &min_valid, &max_valid);
12591 
12592 	if (!status) {
12593 		if (min_valid)
12594 			pf->min_bw = min_bw;
12595 		if (max_valid)
12596 			pf->max_bw = max_bw;
12597 	}
12598 
12599 	return status;
12600 }
12601 
12602 /**
12603  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12604  * @pf: board private structure
12605  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12606 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12607 {
12608 	struct i40e_aqc_configure_partition_bw_data bw_data;
12609 	int status;
12610 
12611 	memset(&bw_data, 0, sizeof(bw_data));
12612 
12613 	/* Set the valid bit for this PF */
12614 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12615 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12616 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12617 
12618 	/* Set the new bandwidths */
12619 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12620 
12621 	return status;
12622 }
12623 
12624 /**
12625  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12626  * if total port shutdown feature is enabled for this PF
12627  * @pf: board private structure
12628  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12629 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12630 {
12631 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12632 #define I40E_FEATURES_ENABLE_PTR		0x2A
12633 #define I40E_CURRENT_SETTING_PTR		0x2B
12634 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12635 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12636 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12637 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12638 	u16 sr_emp_sr_settings_ptr = 0;
12639 	u16 features_enable = 0;
12640 	u16 link_behavior = 0;
12641 	int read_status = 0;
12642 	bool ret = false;
12643 
12644 	read_status = i40e_read_nvm_word(&pf->hw,
12645 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12646 					 &sr_emp_sr_settings_ptr);
12647 	if (read_status)
12648 		goto err_nvm;
12649 	read_status = i40e_read_nvm_word(&pf->hw,
12650 					 sr_emp_sr_settings_ptr +
12651 					 I40E_FEATURES_ENABLE_PTR,
12652 					 &features_enable);
12653 	if (read_status)
12654 		goto err_nvm;
12655 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12656 		read_status = i40e_read_nvm_module_data(&pf->hw,
12657 							I40E_SR_EMP_SR_SETTINGS_PTR,
12658 							I40E_CURRENT_SETTING_PTR,
12659 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12660 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12661 							&link_behavior);
12662 		if (read_status)
12663 			goto err_nvm;
12664 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12665 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12666 	}
12667 	return ret;
12668 
12669 err_nvm:
12670 	dev_warn(&pf->pdev->dev,
12671 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12672 		 ERR_PTR(read_status));
12673 	return ret;
12674 }
12675 
12676 /**
12677  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12678  * @pf: board private structure to initialize
12679  *
12680  * i40e_sw_init initializes the Adapter private data structure.
12681  * Fields are initialized based on PCI device information and
12682  * OS network device settings (MTU size).
12683  **/
i40e_sw_init(struct i40e_pf * pf)12684 static int i40e_sw_init(struct i40e_pf *pf)
12685 {
12686 	int err = 0;
12687 	int size;
12688 	u16 pow;
12689 
12690 	/* Set default capability flags */
12691 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12692 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12693 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12694 
12695 	/* Set default ITR */
12696 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12697 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12698 
12699 	/* Depending on PF configurations, it is possible that the RSS
12700 	 * maximum might end up larger than the available queues
12701 	 */
12702 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12703 	pf->alloc_rss_size = 1;
12704 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12705 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12706 				 pf->hw.func_caps.num_tx_qp);
12707 
12708 	/* find the next higher power-of-2 of num cpus */
12709 	pow = roundup_pow_of_two(num_online_cpus());
12710 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12711 
12712 	if (pf->hw.func_caps.rss) {
12713 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12714 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12715 					   num_online_cpus());
12716 	}
12717 
12718 	/* MFP mode enabled */
12719 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12720 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12721 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12722 		if (i40e_get_partition_bw_setting(pf)) {
12723 			dev_warn(&pf->pdev->dev,
12724 				 "Could not get partition bw settings\n");
12725 		} else {
12726 			dev_info(&pf->pdev->dev,
12727 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12728 				 pf->min_bw, pf->max_bw);
12729 
12730 			/* nudge the Tx scheduler */
12731 			i40e_set_partition_bw_setting(pf);
12732 		}
12733 	}
12734 
12735 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12736 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12737 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12738 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12739 		    pf->hw.num_partitions > 1)
12740 			dev_info(&pf->pdev->dev,
12741 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12742 		else
12743 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12744 		pf->fdir_pf_filter_count =
12745 				 pf->hw.func_caps.fd_filters_guaranteed;
12746 		pf->hw.fdir_shared_filter_count =
12747 				 pf->hw.func_caps.fd_filters_best_effort;
12748 	}
12749 
12750 	/* Enable HW ATR eviction if possible */
12751 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12752 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12753 
12754 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12755 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12756 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12757 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12758 	}
12759 
12760 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12761 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12762 		/* IWARP needs one extra vector for CQP just like MISC.*/
12763 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12764 	}
12765 	/* Stopping FW LLDP engine is supported on XL710 and X722
12766 	 * starting from FW versions determined in i40e_init_adminq.
12767 	 * Stopping the FW LLDP engine is not supported on XL710
12768 	 * if NPAR is functioning so unset this hw flag in this case.
12769 	 */
12770 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12771 	    pf->hw.func_caps.npar_enable)
12772 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12773 
12774 #ifdef CONFIG_PCI_IOV
12775 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12776 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12777 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12778 		pf->num_req_vfs = min_t(int,
12779 					pf->hw.func_caps.num_vfs,
12780 					I40E_MAX_VF_COUNT);
12781 	}
12782 #endif /* CONFIG_PCI_IOV */
12783 	pf->lan_veb = I40E_NO_VEB;
12784 	pf->lan_vsi = I40E_NO_VSI;
12785 
12786 	/* By default FW has this off for performance reasons */
12787 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12788 
12789 	/* set up queue assignment tracking */
12790 	size = sizeof(struct i40e_lump_tracking)
12791 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12792 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12793 	if (!pf->qp_pile) {
12794 		err = -ENOMEM;
12795 		goto sw_init_done;
12796 	}
12797 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12798 
12799 	pf->tx_timeout_recovery_level = 1;
12800 
12801 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12802 	    i40e_is_total_port_shutdown_enabled(pf)) {
12803 		/* Link down on close must be on when total port shutdown
12804 		 * is enabled for a given port
12805 		 */
12806 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12807 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12808 		dev_info(&pf->pdev->dev,
12809 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12810 	}
12811 	mutex_init(&pf->switch_mutex);
12812 
12813 sw_init_done:
12814 	return err;
12815 }
12816 
12817 /**
12818  * i40e_set_ntuple - set the ntuple feature flag and take action
12819  * @pf: board private structure to initialize
12820  * @features: the feature set that the stack is suggesting
12821  *
12822  * returns a bool to indicate if reset needs to happen
12823  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12824 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12825 {
12826 	bool need_reset = false;
12827 
12828 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12829 	 * the state changed, we need to reset.
12830 	 */
12831 	if (features & NETIF_F_NTUPLE) {
12832 		/* Enable filters and mark for reset */
12833 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12834 			need_reset = true;
12835 		/* enable FD_SB only if there is MSI-X vector and no cloud
12836 		 * filters exist
12837 		 */
12838 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12839 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12840 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12841 		}
12842 	} else {
12843 		/* turn off filters, mark for reset and clear SW filter list */
12844 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12845 			need_reset = true;
12846 			i40e_fdir_filter_exit(pf);
12847 		}
12848 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12849 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12850 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12851 
12852 		/* reset fd counters */
12853 		pf->fd_add_err = 0;
12854 		pf->fd_atr_cnt = 0;
12855 		/* if ATR was auto disabled it can be re-enabled. */
12856 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12857 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12858 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12859 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12860 	}
12861 	return need_reset;
12862 }
12863 
12864 /**
12865  * i40e_clear_rss_lut - clear the rx hash lookup table
12866  * @vsi: the VSI being configured
12867  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12868 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12869 {
12870 	struct i40e_pf *pf = vsi->back;
12871 	struct i40e_hw *hw = &pf->hw;
12872 	u16 vf_id = vsi->vf_id;
12873 	u8 i;
12874 
12875 	if (vsi->type == I40E_VSI_MAIN) {
12876 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12877 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12878 	} else if (vsi->type == I40E_VSI_SRIOV) {
12879 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12880 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12881 	} else {
12882 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12883 	}
12884 }
12885 
12886 /**
12887  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12888  * @vsi: ptr to VSI
12889  * @ena: flag to indicate the on/off setting
12890  */
i40e_set_loopback(struct i40e_vsi * vsi,bool ena)12891 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12892 {
12893 	bool if_running = netif_running(vsi->netdev) &&
12894 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12895 	int ret;
12896 
12897 	if (if_running)
12898 		i40e_down(vsi);
12899 
12900 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12901 	if (ret)
12902 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12903 	if (if_running)
12904 		i40e_up(vsi);
12905 
12906 	return ret;
12907 }
12908 
12909 /**
12910  * i40e_set_features - set the netdev feature flags
12911  * @netdev: ptr to the netdev being adjusted
12912  * @features: the feature set that the stack is suggesting
12913  * Note: expects to be called while under rtnl_lock()
12914  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12915 static int i40e_set_features(struct net_device *netdev,
12916 			     netdev_features_t features)
12917 {
12918 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12919 	struct i40e_vsi *vsi = np->vsi;
12920 	struct i40e_pf *pf = vsi->back;
12921 	bool need_reset;
12922 
12923 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12924 		i40e_pf_config_rss(pf);
12925 	else if (!(features & NETIF_F_RXHASH) &&
12926 		 netdev->features & NETIF_F_RXHASH)
12927 		i40e_clear_rss_lut(vsi);
12928 
12929 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12930 		i40e_vlan_stripping_enable(vsi);
12931 	else
12932 		i40e_vlan_stripping_disable(vsi);
12933 
12934 	if (!(features & NETIF_F_HW_TC) &&
12935 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12936 		dev_err(&pf->pdev->dev,
12937 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12938 		return -EINVAL;
12939 	}
12940 
12941 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12942 		i40e_del_all_macvlans(vsi);
12943 
12944 	need_reset = i40e_set_ntuple(pf, features);
12945 
12946 	if (need_reset)
12947 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12948 
12949 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12950 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12951 
12952 	return 0;
12953 }
12954 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12955 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12956 				    unsigned int table, unsigned int idx,
12957 				    struct udp_tunnel_info *ti)
12958 {
12959 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12960 	struct i40e_hw *hw = &np->vsi->back->hw;
12961 	u8 type, filter_index;
12962 	int ret;
12963 
12964 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12965 						   I40E_AQC_TUNNEL_TYPE_NGE;
12966 
12967 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12968 				     NULL);
12969 	if (ret) {
12970 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12971 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12972 		return -EIO;
12973 	}
12974 
12975 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12976 	return 0;
12977 }
12978 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12979 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12980 				      unsigned int table, unsigned int idx,
12981 				      struct udp_tunnel_info *ti)
12982 {
12983 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12984 	struct i40e_hw *hw = &np->vsi->back->hw;
12985 	int ret;
12986 
12987 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12988 	if (ret) {
12989 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
12990 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12991 		return -EIO;
12992 	}
12993 
12994 	return 0;
12995 }
12996 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)12997 static int i40e_get_phys_port_id(struct net_device *netdev,
12998 				 struct netdev_phys_item_id *ppid)
12999 {
13000 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13001 	struct i40e_pf *pf = np->vsi->back;
13002 	struct i40e_hw *hw = &pf->hw;
13003 
13004 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13005 		return -EOPNOTSUPP;
13006 
13007 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13008 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13009 
13010 	return 0;
13011 }
13012 
13013 /**
13014  * i40e_ndo_fdb_add - add an entry to the hardware database
13015  * @ndm: the input from the stack
13016  * @tb: pointer to array of nladdr (unused)
13017  * @dev: the net device pointer
13018  * @addr: the MAC address entry being added
13019  * @vid: VLAN ID
13020  * @flags: instructions from stack about fdb operation
13021  * @notified: whether notification was emitted
13022  * @extack: netlink extended ack, unused currently
13023  */
i40e_ndo_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,bool * notified,struct netlink_ext_ack * extack)13024 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13025 			    struct net_device *dev,
13026 			    const unsigned char *addr, u16 vid,
13027 			    u16 flags, bool *notified,
13028 			    struct netlink_ext_ack *extack)
13029 {
13030 	struct i40e_netdev_priv *np = netdev_priv(dev);
13031 	struct i40e_pf *pf = np->vsi->back;
13032 	int err = 0;
13033 
13034 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13035 		return -EOPNOTSUPP;
13036 
13037 	if (vid) {
13038 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13039 		return -EINVAL;
13040 	}
13041 
13042 	/* Hardware does not support aging addresses so if a
13043 	 * ndm_state is given only allow permanent addresses
13044 	 */
13045 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13046 		netdev_info(dev, "FDB only supports static addresses\n");
13047 		return -EINVAL;
13048 	}
13049 
13050 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13051 		err = dev_uc_add_excl(dev, addr);
13052 	else if (is_multicast_ether_addr(addr))
13053 		err = dev_mc_add_excl(dev, addr);
13054 	else
13055 		err = -EINVAL;
13056 
13057 	/* Only return duplicate errors if NLM_F_EXCL is set */
13058 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13059 		err = 0;
13060 
13061 	return err;
13062 }
13063 
13064 /**
13065  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13066  * @dev: the netdev being configured
13067  * @nlh: RTNL message
13068  * @flags: bridge flags
13069  * @extack: netlink extended ack
13070  *
13071  * Inserts a new hardware bridge if not already created and
13072  * enables the bridging mode requested (VEB or VEPA). If the
13073  * hardware bridge has already been inserted and the request
13074  * is to change the mode then that requires a PF reset to
13075  * allow rebuild of the components with required hardware
13076  * bridge mode enabled.
13077  *
13078  * Note: expects to be called while under rtnl_lock()
13079  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13080 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13081 				   struct nlmsghdr *nlh,
13082 				   u16 flags,
13083 				   struct netlink_ext_ack *extack)
13084 {
13085 	struct i40e_netdev_priv *np = netdev_priv(dev);
13086 	struct i40e_vsi *vsi = np->vsi;
13087 	struct i40e_pf *pf = vsi->back;
13088 	struct nlattr *attr, *br_spec;
13089 	struct i40e_veb *veb;
13090 	int rem;
13091 
13092 	/* Only for PF VSI for now */
13093 	if (vsi->type != I40E_VSI_MAIN)
13094 		return -EOPNOTSUPP;
13095 
13096 	/* Find the HW bridge for PF VSI */
13097 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13098 
13099 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13100 	if (!br_spec)
13101 		return -EINVAL;
13102 
13103 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13104 		__u16 mode = nla_get_u16(attr);
13105 
13106 		if ((mode != BRIDGE_MODE_VEPA) &&
13107 		    (mode != BRIDGE_MODE_VEB))
13108 			return -EINVAL;
13109 
13110 		/* Insert a new HW bridge */
13111 		if (!veb) {
13112 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13113 					     vsi->tc_config.enabled_tc);
13114 			if (veb) {
13115 				veb->bridge_mode = mode;
13116 				i40e_config_bridge_mode(veb);
13117 			} else {
13118 				/* No Bridge HW offload available */
13119 				return -ENOENT;
13120 			}
13121 			break;
13122 		} else if (mode != veb->bridge_mode) {
13123 			/* Existing HW bridge but different mode needs reset */
13124 			veb->bridge_mode = mode;
13125 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13126 			if (mode == BRIDGE_MODE_VEB)
13127 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13128 			else
13129 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13130 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13131 			break;
13132 		}
13133 	}
13134 
13135 	return 0;
13136 }
13137 
13138 /**
13139  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13140  * @skb: skb buff
13141  * @pid: process id
13142  * @seq: RTNL message seq #
13143  * @dev: the netdev being configured
13144  * @filter_mask: unused
13145  * @nlflags: netlink flags passed in
13146  *
13147  * Return the mode in which the hardware bridge is operating in
13148  * i.e VEB or VEPA.
13149  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13150 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13151 				   struct net_device *dev,
13152 				   u32 __always_unused filter_mask,
13153 				   int nlflags)
13154 {
13155 	struct i40e_netdev_priv *np = netdev_priv(dev);
13156 	struct i40e_vsi *vsi = np->vsi;
13157 	struct i40e_pf *pf = vsi->back;
13158 	struct i40e_veb *veb;
13159 
13160 	/* Only for PF VSI for now */
13161 	if (vsi->type != I40E_VSI_MAIN)
13162 		return -EOPNOTSUPP;
13163 
13164 	/* Find the HW bridge for the PF VSI */
13165 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13166 	if (!veb)
13167 		return 0;
13168 
13169 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13170 				       0, 0, nlflags, filter_mask, NULL);
13171 }
13172 
13173 /**
13174  * i40e_features_check - Validate encapsulated packet conforms to limits
13175  * @skb: skb buff
13176  * @dev: This physical port's netdev
13177  * @features: Offload features that the stack believes apply
13178  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13179 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13180 					     struct net_device *dev,
13181 					     netdev_features_t features)
13182 {
13183 	size_t len;
13184 
13185 	/* No point in doing any of this if neither checksum nor GSO are
13186 	 * being requested for this frame.  We can rule out both by just
13187 	 * checking for CHECKSUM_PARTIAL
13188 	 */
13189 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13190 		return features;
13191 
13192 	/* We cannot support GSO if the MSS is going to be less than
13193 	 * 64 bytes.  If it is then we need to drop support for GSO.
13194 	 */
13195 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13196 		features &= ~NETIF_F_GSO_MASK;
13197 
13198 	/* MACLEN can support at most 63 words */
13199 	len = skb_network_offset(skb);
13200 	if (len & ~(63 * 2))
13201 		goto out_err;
13202 
13203 	/* IPLEN and EIPLEN can support at most 127 dwords */
13204 	len = skb_network_header_len(skb);
13205 	if (len & ~(127 * 4))
13206 		goto out_err;
13207 
13208 	if (skb->encapsulation) {
13209 		/* L4TUNLEN can support 127 words */
13210 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13211 		if (len & ~(127 * 2))
13212 			goto out_err;
13213 
13214 		/* IPLEN can support at most 127 dwords */
13215 		len = skb_inner_transport_header(skb) -
13216 		      skb_inner_network_header(skb);
13217 		if (len & ~(127 * 4))
13218 			goto out_err;
13219 	}
13220 
13221 	/* No need to validate L4LEN as TCP is the only protocol with a
13222 	 * flexible value and we support all possible values supported
13223 	 * by TCP, which is at most 15 dwords
13224 	 */
13225 
13226 	return features;
13227 out_err:
13228 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13229 }
13230 
13231 /**
13232  * i40e_xdp_setup - add/remove an XDP program
13233  * @vsi: VSI to changed
13234  * @prog: XDP program
13235  * @extack: netlink extended ack
13236  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13237 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13238 			  struct netlink_ext_ack *extack)
13239 {
13240 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13241 	struct i40e_pf *pf = vsi->back;
13242 	struct bpf_prog *old_prog;
13243 	bool need_reset;
13244 	int i;
13245 
13246 	/* VSI shall be deleted in a moment, block loading new programs */
13247 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13248 		return -EINVAL;
13249 
13250 	/* Don't allow frames that span over multiple buffers */
13251 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13252 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13253 		return -EINVAL;
13254 	}
13255 
13256 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13257 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13258 	if (need_reset)
13259 		i40e_prep_for_reset(pf);
13260 
13261 	old_prog = xchg(&vsi->xdp_prog, prog);
13262 
13263 	if (need_reset) {
13264 		if (!prog) {
13265 			xdp_features_clear_redirect_target(vsi->netdev);
13266 			/* Wait until ndo_xsk_wakeup completes. */
13267 			synchronize_rcu();
13268 		}
13269 		i40e_reset_and_rebuild(pf, true, true);
13270 	}
13271 
13272 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13273 		if (i40e_realloc_rx_bi_zc(vsi, true))
13274 			return -ENOMEM;
13275 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13276 		if (i40e_realloc_rx_bi_zc(vsi, false))
13277 			return -ENOMEM;
13278 	}
13279 
13280 	for (i = 0; i < vsi->num_queue_pairs; i++)
13281 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13282 
13283 	if (old_prog)
13284 		bpf_prog_put(old_prog);
13285 
13286 	/* Kick start the NAPI context if there is an AF_XDP socket open
13287 	 * on that queue id. This so that receiving will start.
13288 	 */
13289 	if (need_reset && prog) {
13290 		for (i = 0; i < vsi->num_queue_pairs; i++)
13291 			if (vsi->xdp_rings[i]->xsk_pool)
13292 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13293 						      XDP_WAKEUP_RX);
13294 		xdp_features_set_redirect_target(vsi->netdev, true);
13295 	}
13296 
13297 	return 0;
13298 }
13299 
13300 /**
13301  * i40e_enter_busy_conf - Enters busy config state
13302  * @vsi: vsi
13303  *
13304  * Returns 0 on success, <0 for failure.
13305  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13306 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13307 {
13308 	struct i40e_pf *pf = vsi->back;
13309 	int timeout = 50;
13310 
13311 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13312 		timeout--;
13313 		if (!timeout)
13314 			return -EBUSY;
13315 		usleep_range(1000, 2000);
13316 	}
13317 
13318 	return 0;
13319 }
13320 
13321 /**
13322  * i40e_exit_busy_conf - Exits busy config state
13323  * @vsi: vsi
13324  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13325 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13326 {
13327 	struct i40e_pf *pf = vsi->back;
13328 
13329 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13330 }
13331 
13332 /**
13333  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13334  * @vsi: vsi
13335  * @queue_pair: queue pair
13336  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13337 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13338 {
13339 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13340 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13341 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13342 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13343 	if (i40e_enabled_xdp_vsi(vsi)) {
13344 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13345 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13346 	}
13347 }
13348 
13349 /**
13350  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13351  * @vsi: vsi
13352  * @queue_pair: queue pair
13353  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13354 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13355 {
13356 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13357 	if (i40e_enabled_xdp_vsi(vsi)) {
13358 		/* Make sure that in-progress ndo_xdp_xmit calls are
13359 		 * completed.
13360 		 */
13361 		synchronize_rcu();
13362 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13363 	}
13364 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13365 }
13366 
13367 /**
13368  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13369  * @vsi: vsi
13370  * @queue_pair: queue pair
13371  * @enable: true for enable, false for disable
13372  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13373 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13374 					bool enable)
13375 {
13376 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13377 	struct i40e_q_vector *q_vector = rxr->q_vector;
13378 
13379 	if (!vsi->netdev)
13380 		return;
13381 
13382 	/* All rings in a qp belong to the same qvector. */
13383 	if (q_vector->rx.ring || q_vector->tx.ring) {
13384 		if (enable)
13385 			napi_enable(&q_vector->napi);
13386 		else
13387 			napi_disable(&q_vector->napi);
13388 	}
13389 }
13390 
13391 /**
13392  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13393  * @vsi: vsi
13394  * @queue_pair: queue pair
13395  * @enable: true for enable, false for disable
13396  *
13397  * Returns 0 on success, <0 on failure.
13398  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13399 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13400 					bool enable)
13401 {
13402 	struct i40e_pf *pf = vsi->back;
13403 	int pf_q, ret = 0;
13404 
13405 	pf_q = vsi->base_queue + queue_pair;
13406 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13407 				     false /*is xdp*/, enable);
13408 	if (ret) {
13409 		dev_info(&pf->pdev->dev,
13410 			 "VSI seid %d Tx ring %d %sable timeout\n",
13411 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13412 		return ret;
13413 	}
13414 
13415 	i40e_control_rx_q(pf, pf_q, enable);
13416 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13417 	if (ret) {
13418 		dev_info(&pf->pdev->dev,
13419 			 "VSI seid %d Rx ring %d %sable timeout\n",
13420 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13421 		return ret;
13422 	}
13423 
13424 	/* Due to HW errata, on Rx disable only, the register can
13425 	 * indicate done before it really is. Needs 50ms to be sure
13426 	 */
13427 	if (!enable)
13428 		mdelay(50);
13429 
13430 	if (!i40e_enabled_xdp_vsi(vsi))
13431 		return ret;
13432 
13433 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13434 				     pf_q + vsi->alloc_queue_pairs,
13435 				     true /*is xdp*/, enable);
13436 	if (ret) {
13437 		dev_info(&pf->pdev->dev,
13438 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13439 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13440 	}
13441 
13442 	return ret;
13443 }
13444 
13445 /**
13446  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13447  * @vsi: vsi
13448  * @queue_pair: queue_pair
13449  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13450 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13451 {
13452 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13453 	struct i40e_pf *pf = vsi->back;
13454 	struct i40e_hw *hw = &pf->hw;
13455 
13456 	/* All rings in a qp belong to the same qvector. */
13457 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13458 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13459 	else
13460 		i40e_irq_dynamic_enable_icr0(pf);
13461 
13462 	i40e_flush(hw);
13463 }
13464 
13465 /**
13466  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13467  * @vsi: vsi
13468  * @queue_pair: queue_pair
13469  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13470 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13471 {
13472 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13473 	struct i40e_pf *pf = vsi->back;
13474 	struct i40e_hw *hw = &pf->hw;
13475 
13476 	/* For simplicity, instead of removing the qp interrupt causes
13477 	 * from the interrupt linked list, we simply disable the interrupt, and
13478 	 * leave the list intact.
13479 	 *
13480 	 * All rings in a qp belong to the same qvector.
13481 	 */
13482 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13483 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13484 
13485 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13486 		i40e_flush(hw);
13487 		synchronize_irq(pf->msix_entries[intpf].vector);
13488 	} else {
13489 		/* Legacy and MSI mode - this stops all interrupt handling */
13490 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13491 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13492 		i40e_flush(hw);
13493 		synchronize_irq(pf->pdev->irq);
13494 	}
13495 }
13496 
13497 /**
13498  * i40e_queue_pair_disable - Disables a queue pair
13499  * @vsi: vsi
13500  * @queue_pair: queue pair
13501  *
13502  * Returns 0 on success, <0 on failure.
13503  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13504 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13505 {
13506 	int err;
13507 
13508 	err = i40e_enter_busy_conf(vsi);
13509 	if (err)
13510 		return err;
13511 
13512 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13513 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13514 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13515 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13516 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13517 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13518 
13519 	return err;
13520 }
13521 
13522 /**
13523  * i40e_queue_pair_enable - Enables a queue pair
13524  * @vsi: vsi
13525  * @queue_pair: queue pair
13526  *
13527  * Returns 0 on success, <0 on failure.
13528  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13529 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13530 {
13531 	int err;
13532 
13533 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13534 	if (err)
13535 		return err;
13536 
13537 	if (i40e_enabled_xdp_vsi(vsi)) {
13538 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13539 		if (err)
13540 			return err;
13541 	}
13542 
13543 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13544 	if (err)
13545 		return err;
13546 
13547 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13548 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13549 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13550 
13551 	i40e_exit_busy_conf(vsi);
13552 
13553 	return err;
13554 }
13555 
13556 /**
13557  * i40e_xdp - implements ndo_bpf for i40e
13558  * @dev: netdevice
13559  * @xdp: XDP command
13560  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13561 static int i40e_xdp(struct net_device *dev,
13562 		    struct netdev_bpf *xdp)
13563 {
13564 	struct i40e_netdev_priv *np = netdev_priv(dev);
13565 	struct i40e_vsi *vsi = np->vsi;
13566 
13567 	if (vsi->type != I40E_VSI_MAIN)
13568 		return -EINVAL;
13569 
13570 	switch (xdp->command) {
13571 	case XDP_SETUP_PROG:
13572 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13573 	case XDP_SETUP_XSK_POOL:
13574 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13575 					   xdp->xsk.queue_id);
13576 	default:
13577 		return -EINVAL;
13578 	}
13579 }
13580 
13581 static const struct net_device_ops i40e_netdev_ops = {
13582 	.ndo_open		= i40e_open,
13583 	.ndo_stop		= i40e_close,
13584 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13585 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13586 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13587 	.ndo_validate_addr	= eth_validate_addr,
13588 	.ndo_set_mac_address	= i40e_set_mac,
13589 	.ndo_change_mtu		= i40e_change_mtu,
13590 	.ndo_tx_timeout		= i40e_tx_timeout,
13591 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13592 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13593 #ifdef CONFIG_NET_POLL_CONTROLLER
13594 	.ndo_poll_controller	= i40e_netpoll,
13595 #endif
13596 	.ndo_setup_tc		= __i40e_setup_tc,
13597 	.ndo_select_queue	= i40e_lan_select_queue,
13598 	.ndo_set_features	= i40e_set_features,
13599 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13600 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13601 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13602 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13603 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13604 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13605 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13606 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13607 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13608 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13609 	.ndo_features_check	= i40e_features_check,
13610 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13611 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13612 	.ndo_bpf		= i40e_xdp,
13613 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13614 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13615 	.ndo_dfwd_add_station	= i40e_fwd_add,
13616 	.ndo_dfwd_del_station	= i40e_fwd_del,
13617 	.ndo_hwtstamp_get	= i40e_ptp_hwtstamp_get,
13618 	.ndo_hwtstamp_set	= i40e_ptp_hwtstamp_set,
13619 };
13620 
13621 /**
13622  * i40e_config_netdev - Setup the netdev flags
13623  * @vsi: the VSI being configured
13624  *
13625  * Returns 0 on success, negative value on failure
13626  **/
i40e_config_netdev(struct i40e_vsi * vsi)13627 static int i40e_config_netdev(struct i40e_vsi *vsi)
13628 {
13629 	struct i40e_pf *pf = vsi->back;
13630 	struct i40e_hw *hw = &pf->hw;
13631 	struct i40e_netdev_priv *np;
13632 	struct net_device *netdev;
13633 	u8 broadcast[ETH_ALEN];
13634 	u8 mac_addr[ETH_ALEN];
13635 	int etherdev_size;
13636 	netdev_features_t hw_enc_features;
13637 	netdev_features_t hw_features;
13638 
13639 	etherdev_size = sizeof(struct i40e_netdev_priv);
13640 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13641 	if (!netdev)
13642 		return -ENOMEM;
13643 
13644 	vsi->netdev = netdev;
13645 	np = netdev_priv(netdev);
13646 	np->vsi = vsi;
13647 
13648 	hw_enc_features = NETIF_F_SG			|
13649 			  NETIF_F_HW_CSUM		|
13650 			  NETIF_F_HIGHDMA		|
13651 			  NETIF_F_SOFT_FEATURES		|
13652 			  NETIF_F_TSO			|
13653 			  NETIF_F_TSO_ECN		|
13654 			  NETIF_F_TSO6			|
13655 			  NETIF_F_GSO_GRE		|
13656 			  NETIF_F_GSO_GRE_CSUM		|
13657 			  NETIF_F_GSO_PARTIAL		|
13658 			  NETIF_F_GSO_IPXIP4		|
13659 			  NETIF_F_GSO_IPXIP6		|
13660 			  NETIF_F_GSO_UDP_TUNNEL	|
13661 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13662 			  NETIF_F_GSO_UDP_L4		|
13663 			  NETIF_F_SCTP_CRC		|
13664 			  NETIF_F_RXHASH		|
13665 			  NETIF_F_RXCSUM		|
13666 			  0;
13667 
13668 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13669 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13670 
13671 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13672 
13673 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13674 
13675 	netdev->hw_enc_features |= hw_enc_features;
13676 
13677 	/* record features VLANs can make use of */
13678 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13679 
13680 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13681 				   NETIF_F_GSO_GRE_CSUM |	\
13682 				   NETIF_F_GSO_IPXIP4 |		\
13683 				   NETIF_F_GSO_IPXIP6 |		\
13684 				   NETIF_F_GSO_UDP_TUNNEL |	\
13685 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13686 
13687 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13688 	netdev->features |= NETIF_F_GSO_PARTIAL |
13689 			    I40E_GSO_PARTIAL_FEATURES;
13690 
13691 	netdev->mpls_features |= NETIF_F_SG;
13692 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13693 	netdev->mpls_features |= NETIF_F_TSO;
13694 	netdev->mpls_features |= NETIF_F_TSO6;
13695 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13696 
13697 	/* enable macvlan offloads */
13698 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13699 
13700 	hw_features = hw_enc_features		|
13701 		      NETIF_F_HW_VLAN_CTAG_TX	|
13702 		      NETIF_F_HW_VLAN_CTAG_RX;
13703 
13704 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13705 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13706 
13707 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13708 
13709 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13710 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13711 
13712 	netdev->features &= ~NETIF_F_HW_TC;
13713 
13714 	if (vsi->type == I40E_VSI_MAIN) {
13715 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13716 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13717 		/* The following steps are necessary for two reasons. First,
13718 		 * some older NVM configurations load a default MAC-VLAN
13719 		 * filter that will accept any tagged packet, and we want to
13720 		 * replace this with a normal filter. Additionally, it is
13721 		 * possible our MAC address was provided by the platform using
13722 		 * Open Firmware or similar.
13723 		 *
13724 		 * Thus, we need to remove the default filter and install one
13725 		 * specific to the MAC address.
13726 		 */
13727 		i40e_rm_default_mac_filter(vsi, mac_addr);
13728 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13729 		i40e_add_mac_filter(vsi, mac_addr);
13730 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13731 
13732 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13733 				       NETDEV_XDP_ACT_REDIRECT |
13734 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13735 				       NETDEV_XDP_ACT_RX_SG;
13736 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13737 	} else {
13738 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13739 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13740 		 * the end, which is 4 bytes long, so force truncation of the
13741 		 * original name by IFNAMSIZ - 4
13742 		 */
13743 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13744 
13745 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13746 			 main_vsi->netdev->name);
13747 		eth_random_addr(mac_addr);
13748 
13749 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13750 		i40e_add_mac_filter(vsi, mac_addr);
13751 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13752 	}
13753 
13754 	/* Add the broadcast filter so that we initially will receive
13755 	 * broadcast packets. Note that when a new VLAN is first added the
13756 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13757 	 * specific filters as part of transitioning into "vlan" operation.
13758 	 * When more VLANs are added, the driver will copy each existing MAC
13759 	 * filter and add it for the new VLAN.
13760 	 *
13761 	 * Broadcast filters are handled specially by
13762 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13763 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13764 	 * filter. The subtask will update the correct broadcast promiscuous
13765 	 * bits as VLANs become active or inactive.
13766 	 */
13767 	eth_broadcast_addr(broadcast);
13768 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13769 	i40e_add_mac_filter(vsi, broadcast);
13770 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13771 
13772 	eth_hw_addr_set(netdev, mac_addr);
13773 	ether_addr_copy(netdev->perm_addr, mac_addr);
13774 
13775 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13776 	netdev->neigh_priv_len = sizeof(u32) * 4;
13777 
13778 	netdev->priv_flags |= IFF_UNICAST_FLT;
13779 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13780 	/* Setup netdev TC information */
13781 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13782 
13783 	netdev->netdev_ops = &i40e_netdev_ops;
13784 	netdev->watchdog_timeo = 5 * HZ;
13785 	i40e_set_ethtool_ops(netdev);
13786 
13787 	/* MTU range: 68 - 9706 */
13788 	netdev->min_mtu = ETH_MIN_MTU;
13789 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13790 
13791 	return 0;
13792 }
13793 
13794 /**
13795  * i40e_vsi_delete - Delete a VSI from the switch
13796  * @vsi: the VSI being removed
13797  *
13798  * Returns 0 on success, negative value on failure
13799  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13800 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13801 {
13802 	/* remove default VSI is not allowed */
13803 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13804 		return;
13805 
13806 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13807 }
13808 
13809 /**
13810  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13811  * @vsi: the VSI being queried
13812  *
13813  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13814  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13815 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13816 {
13817 	struct i40e_veb *veb;
13818 	struct i40e_pf *pf = vsi->back;
13819 
13820 	/* Uplink is not a bridge so default to VEB */
13821 	if (vsi->veb_idx >= I40E_MAX_VEB)
13822 		return 1;
13823 
13824 	veb = pf->veb[vsi->veb_idx];
13825 	if (!veb) {
13826 		dev_info(&pf->pdev->dev,
13827 			 "There is no veb associated with the bridge\n");
13828 		return -ENOENT;
13829 	}
13830 
13831 	/* Uplink is a bridge in VEPA mode */
13832 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13833 		return 0;
13834 	} else {
13835 		/* Uplink is a bridge in VEB mode */
13836 		return 1;
13837 	}
13838 
13839 	/* VEPA is now default bridge, so return 0 */
13840 	return 0;
13841 }
13842 
13843 /**
13844  * i40e_add_vsi - Add a VSI to the switch
13845  * @vsi: the VSI being configured
13846  *
13847  * This initializes a VSI context depending on the VSI type to be added and
13848  * passes it down to the add_vsi aq command.
13849  **/
i40e_add_vsi(struct i40e_vsi * vsi)13850 static int i40e_add_vsi(struct i40e_vsi *vsi)
13851 {
13852 	int ret = -ENODEV;
13853 	struct i40e_pf *pf = vsi->back;
13854 	struct i40e_hw *hw = &pf->hw;
13855 	struct i40e_vsi_context ctxt;
13856 	struct i40e_mac_filter *f;
13857 	struct hlist_node *h;
13858 	int bkt;
13859 
13860 	u8 enabled_tc = 0x1; /* TC0 enabled */
13861 	int f_count = 0;
13862 
13863 	memset(&ctxt, 0, sizeof(ctxt));
13864 	switch (vsi->type) {
13865 	case I40E_VSI_MAIN:
13866 		/* The PF's main VSI is already setup as part of the
13867 		 * device initialization, so we'll not bother with
13868 		 * the add_vsi call, but we will retrieve the current
13869 		 * VSI context.
13870 		 */
13871 		ctxt.seid = pf->main_vsi_seid;
13872 		ctxt.pf_num = pf->hw.pf_id;
13873 		ctxt.vf_num = 0;
13874 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13875 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13876 		if (ret) {
13877 			dev_info(&pf->pdev->dev,
13878 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13879 				 ERR_PTR(ret),
13880 				 libie_aq_str(pf->hw.aq.asq_last_status));
13881 			return -ENOENT;
13882 		}
13883 		vsi->info = ctxt.info;
13884 		vsi->info.valid_sections = 0;
13885 
13886 		vsi->seid = ctxt.seid;
13887 		vsi->id = ctxt.vsi_number;
13888 
13889 		enabled_tc = i40e_pf_get_tc_map(pf);
13890 
13891 		/* Source pruning is enabled by default, so the flag is
13892 		 * negative logic - if it's set, we need to fiddle with
13893 		 * the VSI to disable source pruning.
13894 		 */
13895 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13896 			memset(&ctxt, 0, sizeof(ctxt));
13897 			ctxt.seid = pf->main_vsi_seid;
13898 			ctxt.pf_num = pf->hw.pf_id;
13899 			ctxt.vf_num = 0;
13900 			ctxt.info.valid_sections |=
13901 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13902 			ctxt.info.switch_id =
13903 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13904 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13905 			if (ret) {
13906 				dev_info(&pf->pdev->dev,
13907 					 "update vsi failed, err %d aq_err %s\n",
13908 					 ret,
13909 					 libie_aq_str(pf->hw.aq.asq_last_status));
13910 				ret = -ENOENT;
13911 				goto err;
13912 			}
13913 		}
13914 
13915 		/* MFP mode setup queue map and update VSI */
13916 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13917 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13918 			memset(&ctxt, 0, sizeof(ctxt));
13919 			ctxt.seid = pf->main_vsi_seid;
13920 			ctxt.pf_num = pf->hw.pf_id;
13921 			ctxt.vf_num = 0;
13922 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13923 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13924 			if (ret) {
13925 				dev_info(&pf->pdev->dev,
13926 					 "update vsi failed, err %pe aq_err %s\n",
13927 					 ERR_PTR(ret),
13928 					 libie_aq_str(pf->hw.aq.asq_last_status));
13929 				ret = -ENOENT;
13930 				goto err;
13931 			}
13932 			/* update the local VSI info queue map */
13933 			i40e_vsi_update_queue_map(vsi, &ctxt);
13934 			vsi->info.valid_sections = 0;
13935 		} else {
13936 			/* Default/Main VSI is only enabled for TC0
13937 			 * reconfigure it to enable all TCs that are
13938 			 * available on the port in SFP mode.
13939 			 * For MFP case the iSCSI PF would use this
13940 			 * flow to enable LAN+iSCSI TC.
13941 			 */
13942 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13943 			if (ret) {
13944 				/* Single TC condition is not fatal,
13945 				 * message and continue
13946 				 */
13947 				dev_info(&pf->pdev->dev,
13948 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13949 					 enabled_tc,
13950 					 ERR_PTR(ret),
13951 					 libie_aq_str(pf->hw.aq.asq_last_status));
13952 			}
13953 		}
13954 		break;
13955 
13956 	case I40E_VSI_FDIR:
13957 		ctxt.pf_num = hw->pf_id;
13958 		ctxt.vf_num = 0;
13959 		ctxt.uplink_seid = vsi->uplink_seid;
13960 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13961 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13962 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
13963 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13964 			ctxt.info.valid_sections |=
13965 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13966 			ctxt.info.switch_id =
13967 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13968 		}
13969 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13970 		break;
13971 
13972 	case I40E_VSI_VMDQ2:
13973 		ctxt.pf_num = hw->pf_id;
13974 		ctxt.vf_num = 0;
13975 		ctxt.uplink_seid = vsi->uplink_seid;
13976 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13977 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13978 
13979 		/* This VSI is connected to VEB so the switch_id
13980 		 * should be set to zero by default.
13981 		 */
13982 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13983 			ctxt.info.valid_sections |=
13984 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13985 			ctxt.info.switch_id =
13986 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13987 		}
13988 
13989 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13990 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13991 		break;
13992 
13993 	case I40E_VSI_SRIOV:
13994 		ctxt.pf_num = hw->pf_id;
13995 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13996 		ctxt.uplink_seid = vsi->uplink_seid;
13997 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13998 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13999 
14000 		/* This VSI is connected to VEB so the switch_id
14001 		 * should be set to zero by default.
14002 		 */
14003 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14004 			ctxt.info.valid_sections |=
14005 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14006 			ctxt.info.switch_id =
14007 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14008 		}
14009 
14010 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14011 			ctxt.info.valid_sections |=
14012 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14013 			ctxt.info.queueing_opt_flags |=
14014 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14015 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14016 		}
14017 
14018 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14019 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14020 		if (pf->vf[vsi->vf_id].spoofchk) {
14021 			ctxt.info.valid_sections |=
14022 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14023 			ctxt.info.sec_flags |=
14024 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14025 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14026 		}
14027 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14028 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14029 		break;
14030 
14031 	case I40E_VSI_IWARP:
14032 		/* send down message to iWARP */
14033 		break;
14034 
14035 	default:
14036 		return -ENODEV;
14037 	}
14038 
14039 	if (vsi->type != I40E_VSI_MAIN) {
14040 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14041 		if (ret) {
14042 			dev_info(&vsi->back->pdev->dev,
14043 				 "add vsi failed, err %pe aq_err %s\n",
14044 				 ERR_PTR(ret),
14045 				 libie_aq_str(pf->hw.aq.asq_last_status));
14046 			ret = -ENOENT;
14047 			goto err;
14048 		}
14049 		vsi->info = ctxt.info;
14050 		vsi->info.valid_sections = 0;
14051 		vsi->seid = ctxt.seid;
14052 		vsi->id = ctxt.vsi_number;
14053 	}
14054 
14055 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14056 	vsi->active_filters = 0;
14057 	/* If macvlan filters already exist, force them to get loaded */
14058 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14059 		f->state = I40E_FILTER_NEW;
14060 		f_count++;
14061 	}
14062 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14063 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14064 
14065 	if (f_count) {
14066 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14067 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14068 	}
14069 
14070 	/* Update VSI BW information */
14071 	ret = i40e_vsi_get_bw_info(vsi);
14072 	if (ret) {
14073 		dev_info(&pf->pdev->dev,
14074 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14075 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14076 		/* VSI is already added so not tearing that up */
14077 		ret = 0;
14078 	}
14079 
14080 err:
14081 	return ret;
14082 }
14083 
14084 /**
14085  * i40e_vsi_release - Delete a VSI and free its resources
14086  * @vsi: the VSI being removed
14087  *
14088  * Returns 0 on success or < 0 on error
14089  **/
i40e_vsi_release(struct i40e_vsi * vsi)14090 int i40e_vsi_release(struct i40e_vsi *vsi)
14091 {
14092 	struct i40e_mac_filter *f;
14093 	struct hlist_node *h;
14094 	struct i40e_veb *veb;
14095 	struct i40e_pf *pf;
14096 	u16 uplink_seid;
14097 	int i, n, bkt;
14098 
14099 	pf = vsi->back;
14100 
14101 	/* release of a VEB-owner or last VSI is not allowed */
14102 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14103 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14104 			 vsi->seid, vsi->uplink_seid);
14105 		return -ENODEV;
14106 	}
14107 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14108 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14109 		return -ENODEV;
14110 	}
14111 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14112 	uplink_seid = vsi->uplink_seid;
14113 
14114 	if (vsi->type != I40E_VSI_SRIOV) {
14115 		if (vsi->netdev_registered) {
14116 			vsi->netdev_registered = false;
14117 			if (vsi->netdev) {
14118 				/* results in a call to i40e_close() */
14119 				unregister_netdev(vsi->netdev);
14120 			}
14121 		} else {
14122 			i40e_vsi_close(vsi);
14123 		}
14124 		i40e_vsi_disable_irq(vsi);
14125 	}
14126 
14127 	if (vsi->type == I40E_VSI_MAIN)
14128 		i40e_devlink_destroy_port(pf);
14129 
14130 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14131 
14132 	/* clear the sync flag on all filters */
14133 	if (vsi->netdev) {
14134 		__dev_uc_unsync(vsi->netdev, NULL);
14135 		__dev_mc_unsync(vsi->netdev, NULL);
14136 	}
14137 
14138 	/* make sure any remaining filters are marked for deletion */
14139 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14140 		__i40e_del_filter(vsi, f);
14141 
14142 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14143 
14144 	i40e_sync_vsi_filters(vsi);
14145 
14146 	i40e_vsi_delete(vsi);
14147 	i40e_vsi_free_q_vectors(vsi);
14148 	if (vsi->netdev) {
14149 		free_netdev(vsi->netdev);
14150 		vsi->netdev = NULL;
14151 	}
14152 	i40e_vsi_clear_rings(vsi);
14153 	i40e_vsi_clear(vsi);
14154 
14155 	/* If this was the last thing on the VEB, except for the
14156 	 * controlling VSI, remove the VEB, which puts the controlling
14157 	 * VSI onto the uplink port.
14158 	 *
14159 	 * Well, okay, there's one more exception here: don't remove
14160 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14161 	 * from up the network stack.
14162 	 */
14163 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14164 	if (veb && veb->uplink_seid) {
14165 		n = 0;
14166 
14167 		/* Count non-controlling VSIs present on  the VEB */
14168 		i40e_pf_for_each_vsi(pf, i, vsi)
14169 			if (vsi->uplink_seid == uplink_seid &&
14170 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14171 				n++;
14172 
14173 		/* If there is no VSI except the control one then release
14174 		 * the VEB and put the control VSI onto VEB uplink.
14175 		 */
14176 		if (!n)
14177 			i40e_veb_release(veb);
14178 	}
14179 
14180 	return 0;
14181 }
14182 
14183 /**
14184  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14185  * @vsi: ptr to the VSI
14186  *
14187  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14188  * corresponding SW VSI structure and initializes num_queue_pairs for the
14189  * newly allocated VSI.
14190  *
14191  * Returns 0 on success or negative on failure
14192  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14193 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14194 {
14195 	int ret = -ENOENT;
14196 	struct i40e_pf *pf = vsi->back;
14197 
14198 	if (vsi->q_vectors[0]) {
14199 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14200 			 vsi->seid);
14201 		return -EEXIST;
14202 	}
14203 
14204 	if (vsi->base_vector) {
14205 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14206 			 vsi->seid, vsi->base_vector);
14207 		return -EEXIST;
14208 	}
14209 
14210 	ret = i40e_vsi_alloc_q_vectors(vsi);
14211 	if (ret) {
14212 		dev_info(&pf->pdev->dev,
14213 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14214 			 vsi->num_q_vectors, vsi->seid, ret);
14215 		vsi->num_q_vectors = 0;
14216 		goto vector_setup_out;
14217 	}
14218 
14219 	/* In Legacy mode, we do not have to get any other vector since we
14220 	 * piggyback on the misc/ICR0 for queue interrupts.
14221 	*/
14222 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14223 		return ret;
14224 	if (vsi->num_q_vectors)
14225 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14226 						 vsi->num_q_vectors, vsi->idx);
14227 	if (vsi->base_vector < 0) {
14228 		dev_info(&pf->pdev->dev,
14229 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14230 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14231 		i40e_vsi_free_q_vectors(vsi);
14232 		ret = -ENOENT;
14233 		goto vector_setup_out;
14234 	}
14235 
14236 vector_setup_out:
14237 	return ret;
14238 }
14239 
14240 /**
14241  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14242  * @vsi: pointer to the vsi.
14243  *
14244  * This re-allocates a vsi's queue resources.
14245  *
14246  * Returns pointer to the successfully allocated and configured VSI sw struct
14247  * on success, otherwise returns NULL on failure.
14248  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14249 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14250 {
14251 	struct i40e_vsi *main_vsi;
14252 	u16 alloc_queue_pairs;
14253 	struct i40e_pf *pf;
14254 	int ret;
14255 
14256 	if (!vsi)
14257 		return NULL;
14258 
14259 	pf = vsi->back;
14260 
14261 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14262 	i40e_vsi_clear_rings(vsi);
14263 
14264 	i40e_vsi_free_arrays(vsi, false);
14265 	i40e_set_num_rings_in_vsi(vsi);
14266 	ret = i40e_vsi_alloc_arrays(vsi, false);
14267 	if (ret)
14268 		goto err_vsi;
14269 
14270 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14271 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14272 
14273 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14274 	if (ret < 0) {
14275 		dev_info(&pf->pdev->dev,
14276 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14277 			 alloc_queue_pairs, vsi->seid, ret);
14278 		goto err_vsi;
14279 	}
14280 	vsi->base_queue = ret;
14281 
14282 	/* Update the FW view of the VSI. Force a reset of TC and queue
14283 	 * layout configurations.
14284 	 */
14285 	main_vsi = i40e_pf_get_main_vsi(pf);
14286 	main_vsi->seid = pf->main_vsi_seid;
14287 	i40e_vsi_reconfig_tc(main_vsi);
14288 
14289 	if (vsi->type == I40E_VSI_MAIN)
14290 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14291 
14292 	/* assign it some queues */
14293 	ret = i40e_alloc_rings(vsi);
14294 	if (ret)
14295 		goto err_rings;
14296 
14297 	/* map all of the rings to the q_vectors */
14298 	i40e_vsi_map_rings_to_vectors(vsi);
14299 	return vsi;
14300 
14301 err_rings:
14302 	i40e_vsi_free_q_vectors(vsi);
14303 	if (vsi->netdev_registered) {
14304 		vsi->netdev_registered = false;
14305 		unregister_netdev(vsi->netdev);
14306 		free_netdev(vsi->netdev);
14307 		vsi->netdev = NULL;
14308 	}
14309 	if (vsi->type == I40E_VSI_MAIN)
14310 		i40e_devlink_destroy_port(pf);
14311 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14312 err_vsi:
14313 	i40e_vsi_clear(vsi);
14314 	return NULL;
14315 }
14316 
14317 /**
14318  * i40e_vsi_setup - Set up a VSI by a given type
14319  * @pf: board private structure
14320  * @type: VSI type
14321  * @uplink_seid: the switch element to link to
14322  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14323  *
14324  * This allocates the sw VSI structure and its queue resources, then add a VSI
14325  * to the identified VEB.
14326  *
14327  * Returns pointer to the successfully allocated and configure VSI sw struct on
14328  * success, otherwise returns NULL on failure.
14329  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14330 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14331 				u16 uplink_seid, u32 param1)
14332 {
14333 	struct i40e_vsi *vsi = NULL;
14334 	struct i40e_veb *veb = NULL;
14335 	u16 alloc_queue_pairs;
14336 	int v_idx;
14337 	int ret;
14338 
14339 	/* The requested uplink_seid must be either
14340 	 *     - the PF's port seid
14341 	 *              no VEB is needed because this is the PF
14342 	 *              or this is a Flow Director special case VSI
14343 	 *     - seid of an existing VEB
14344 	 *     - seid of a VSI that owns an existing VEB
14345 	 *     - seid of a VSI that doesn't own a VEB
14346 	 *              a new VEB is created and the VSI becomes the owner
14347 	 *     - seid of the PF VSI, which is what creates the first VEB
14348 	 *              this is a special case of the previous
14349 	 *
14350 	 * Find which uplink_seid we were given and create a new VEB if needed
14351 	 */
14352 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14353 	if (!veb && uplink_seid != pf->mac_seid) {
14354 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14355 		if (!vsi) {
14356 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14357 				 uplink_seid);
14358 			return NULL;
14359 		}
14360 
14361 		if (vsi->uplink_seid == pf->mac_seid)
14362 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14363 					     vsi->tc_config.enabled_tc);
14364 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14365 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14366 					     vsi->tc_config.enabled_tc);
14367 		if (veb) {
14368 			if (vsi->type != I40E_VSI_MAIN) {
14369 				dev_info(&vsi->back->pdev->dev,
14370 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14371 				return NULL;
14372 			}
14373 			/* We come up by default in VEPA mode if SRIOV is not
14374 			 * already enabled, in which case we can't force VEPA
14375 			 * mode.
14376 			 */
14377 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14378 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14379 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14380 			}
14381 			i40e_config_bridge_mode(veb);
14382 		}
14383 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14384 		if (!veb) {
14385 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14386 			return NULL;
14387 		}
14388 
14389 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14390 		uplink_seid = veb->seid;
14391 	}
14392 
14393 	/* get vsi sw struct */
14394 	v_idx = i40e_vsi_mem_alloc(pf, type);
14395 	if (v_idx < 0)
14396 		goto err_alloc;
14397 	vsi = pf->vsi[v_idx];
14398 	if (!vsi)
14399 		goto err_alloc;
14400 	vsi->type = type;
14401 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14402 
14403 	if (type == I40E_VSI_MAIN)
14404 		pf->lan_vsi = v_idx;
14405 	else if (type == I40E_VSI_SRIOV)
14406 		vsi->vf_id = param1;
14407 	/* assign it some queues */
14408 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14409 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14410 
14411 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14412 	if (ret < 0) {
14413 		dev_info(&pf->pdev->dev,
14414 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14415 			 alloc_queue_pairs, vsi->seid, ret);
14416 		goto err_vsi;
14417 	}
14418 	vsi->base_queue = ret;
14419 
14420 	/* get a VSI from the hardware */
14421 	vsi->uplink_seid = uplink_seid;
14422 	ret = i40e_add_vsi(vsi);
14423 	if (ret)
14424 		goto err_vsi;
14425 
14426 	switch (vsi->type) {
14427 	/* setup the netdev if needed */
14428 	case I40E_VSI_MAIN:
14429 	case I40E_VSI_VMDQ2:
14430 		ret = i40e_config_netdev(vsi);
14431 		if (ret)
14432 			goto err_netdev;
14433 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14434 		if (ret)
14435 			goto err_netdev;
14436 		if (vsi->type == I40E_VSI_MAIN) {
14437 			ret = i40e_devlink_create_port(pf);
14438 			if (ret)
14439 				goto err_netdev;
14440 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14441 		}
14442 		ret = register_netdev(vsi->netdev);
14443 		if (ret)
14444 			goto err_dl_port;
14445 		vsi->netdev_registered = true;
14446 		netif_carrier_off(vsi->netdev);
14447 #ifdef CONFIG_I40E_DCB
14448 		/* Setup DCB netlink interface */
14449 		i40e_dcbnl_setup(vsi);
14450 #endif /* CONFIG_I40E_DCB */
14451 		fallthrough;
14452 	case I40E_VSI_FDIR:
14453 		/* set up vectors and rings if needed */
14454 		ret = i40e_vsi_setup_vectors(vsi);
14455 		if (ret)
14456 			goto err_msix;
14457 
14458 		ret = i40e_alloc_rings(vsi);
14459 		if (ret)
14460 			goto err_rings;
14461 
14462 		/* map all of the rings to the q_vectors */
14463 		i40e_vsi_map_rings_to_vectors(vsi);
14464 
14465 		i40e_vsi_reset_stats(vsi);
14466 		break;
14467 	default:
14468 		/* no netdev or rings for the other VSI types */
14469 		break;
14470 	}
14471 
14472 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14473 	    vsi->type == I40E_VSI_VMDQ2) {
14474 		ret = i40e_vsi_config_rss(vsi);
14475 		if (ret)
14476 			goto err_config;
14477 	}
14478 	return vsi;
14479 
14480 err_config:
14481 	i40e_vsi_clear_rings(vsi);
14482 err_rings:
14483 	i40e_vsi_free_q_vectors(vsi);
14484 err_msix:
14485 	if (vsi->netdev_registered) {
14486 		vsi->netdev_registered = false;
14487 		unregister_netdev(vsi->netdev);
14488 		free_netdev(vsi->netdev);
14489 		vsi->netdev = NULL;
14490 	}
14491 err_dl_port:
14492 	if (vsi->type == I40E_VSI_MAIN)
14493 		i40e_devlink_destroy_port(pf);
14494 err_netdev:
14495 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14496 err_vsi:
14497 	i40e_vsi_clear(vsi);
14498 err_alloc:
14499 	return NULL;
14500 }
14501 
14502 /**
14503  * i40e_veb_get_bw_info - Query VEB BW information
14504  * @veb: the veb to query
14505  *
14506  * Query the Tx scheduler BW configuration data for given VEB
14507  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14508 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14509 {
14510 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14511 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14512 	struct i40e_pf *pf = veb->pf;
14513 	struct i40e_hw *hw = &pf->hw;
14514 	u32 tc_bw_max;
14515 	int ret = 0;
14516 	int i;
14517 
14518 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14519 						  &bw_data, NULL);
14520 	if (ret) {
14521 		dev_info(&pf->pdev->dev,
14522 			 "query veb bw config failed, err %pe aq_err %s\n",
14523 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14524 		goto out;
14525 	}
14526 
14527 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14528 						   &ets_data, NULL);
14529 	if (ret) {
14530 		dev_info(&pf->pdev->dev,
14531 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14532 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14533 		goto out;
14534 	}
14535 
14536 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14537 	veb->bw_max_quanta = ets_data.tc_bw_max;
14538 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14539 	veb->enabled_tc = ets_data.tc_valid_bits;
14540 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14541 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14542 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14543 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14544 		veb->bw_tc_limit_credits[i] =
14545 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14546 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14547 	}
14548 
14549 out:
14550 	return ret;
14551 }
14552 
14553 /**
14554  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14555  * @pf: board private structure
14556  *
14557  * On error: returns error code (negative)
14558  * On success: returns vsi index in PF (positive)
14559  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14560 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14561 {
14562 	int ret = -ENOENT;
14563 	struct i40e_veb *veb;
14564 	int i;
14565 
14566 	/* Need to protect the allocation of switch elements at the PF level */
14567 	mutex_lock(&pf->switch_mutex);
14568 
14569 	/* VEB list may be fragmented if VEB creation/destruction has
14570 	 * been happening.  We can afford to do a quick scan to look
14571 	 * for any free slots in the list.
14572 	 *
14573 	 * find next empty veb slot, looping back around if necessary
14574 	 */
14575 	i = 0;
14576 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14577 		i++;
14578 	if (i >= I40E_MAX_VEB) {
14579 		ret = -ENOMEM;
14580 		goto err_alloc_veb;  /* out of VEB slots! */
14581 	}
14582 
14583 	veb = kzalloc_obj(*veb);
14584 	if (!veb) {
14585 		ret = -ENOMEM;
14586 		goto err_alloc_veb;
14587 	}
14588 	veb->pf = pf;
14589 	veb->idx = i;
14590 	veb->enabled_tc = 1;
14591 
14592 	pf->veb[i] = veb;
14593 	ret = i;
14594 err_alloc_veb:
14595 	mutex_unlock(&pf->switch_mutex);
14596 	return ret;
14597 }
14598 
14599 /**
14600  * i40e_switch_branch_release - Delete a branch of the switch tree
14601  * @branch: where to start deleting
14602  *
14603  * This uses recursion to find the tips of the branch to be
14604  * removed, deleting until we get back to and can delete this VEB.
14605  **/
i40e_switch_branch_release(struct i40e_veb * branch)14606 static void i40e_switch_branch_release(struct i40e_veb *branch)
14607 {
14608 	struct i40e_pf *pf = branch->pf;
14609 	u16 branch_seid = branch->seid;
14610 	u16 veb_idx = branch->idx;
14611 	struct i40e_vsi *vsi;
14612 	struct i40e_veb *veb;
14613 	int i;
14614 
14615 	/* release any VEBs on this VEB - RECURSION */
14616 	i40e_pf_for_each_veb(pf, i, veb)
14617 		if (veb->uplink_seid == branch->seid)
14618 			i40e_switch_branch_release(veb);
14619 
14620 	/* Release the VSIs on this VEB, but not the owner VSI.
14621 	 *
14622 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14623 	 *       the VEB itself, so don't use (*branch) after this loop.
14624 	 */
14625 	i40e_pf_for_each_vsi(pf, i, vsi)
14626 		if (vsi->uplink_seid == branch_seid &&
14627 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14628 			i40e_vsi_release(vsi);
14629 
14630 	/* There's one corner case where the VEB might not have been
14631 	 * removed, so double check it here and remove it if needed.
14632 	 * This case happens if the veb was created from the debugfs
14633 	 * commands and no VSIs were added to it.
14634 	 */
14635 	if (pf->veb[veb_idx])
14636 		i40e_veb_release(pf->veb[veb_idx]);
14637 }
14638 
14639 /**
14640  * i40e_veb_clear - remove veb struct
14641  * @veb: the veb to remove
14642  **/
i40e_veb_clear(struct i40e_veb * veb)14643 static void i40e_veb_clear(struct i40e_veb *veb)
14644 {
14645 	if (!veb)
14646 		return;
14647 
14648 	if (veb->pf) {
14649 		struct i40e_pf *pf = veb->pf;
14650 
14651 		mutex_lock(&pf->switch_mutex);
14652 		if (pf->veb[veb->idx] == veb)
14653 			pf->veb[veb->idx] = NULL;
14654 		mutex_unlock(&pf->switch_mutex);
14655 	}
14656 
14657 	kfree(veb);
14658 }
14659 
14660 /**
14661  * i40e_veb_release - Delete a VEB and free its resources
14662  * @veb: the VEB being removed
14663  **/
i40e_veb_release(struct i40e_veb * veb)14664 void i40e_veb_release(struct i40e_veb *veb)
14665 {
14666 	struct i40e_vsi *vsi, *vsi_it;
14667 	struct i40e_pf *pf;
14668 	int i, n = 0;
14669 
14670 	pf = veb->pf;
14671 
14672 	/* find the remaining VSI and check for extras */
14673 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14674 		if (vsi_it->uplink_seid == veb->seid) {
14675 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14676 				vsi = vsi_it;
14677 			n++;
14678 		}
14679 
14680 	/* Floating VEB has to be empty and regular one must have
14681 	 * single owner VSI.
14682 	 */
14683 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14684 		dev_info(&pf->pdev->dev,
14685 			 "can't remove VEB %d with %d VSIs left\n",
14686 			 veb->seid, n);
14687 		return;
14688 	}
14689 
14690 	/* For regular VEB move the owner VSI to uplink port */
14691 	if (veb->uplink_seid) {
14692 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14693 		vsi->uplink_seid = veb->uplink_seid;
14694 		vsi->veb_idx = I40E_NO_VEB;
14695 	}
14696 
14697 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14698 	i40e_veb_clear(veb);
14699 }
14700 
14701 /**
14702  * i40e_add_veb - create the VEB in the switch
14703  * @veb: the VEB to be instantiated
14704  * @vsi: the controlling VSI
14705  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14706 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14707 {
14708 	struct i40e_pf *pf = veb->pf;
14709 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14710 	int ret;
14711 
14712 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14713 			      veb->enabled_tc, vsi ? false : true,
14714 			      &veb->seid, enable_stats, NULL);
14715 
14716 	/* get a VEB from the hardware */
14717 	if (ret) {
14718 		dev_info(&pf->pdev->dev,
14719 			 "couldn't add VEB, err %pe aq_err %s\n",
14720 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14721 		return -EPERM;
14722 	}
14723 
14724 	/* get statistics counter */
14725 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14726 					 &veb->stats_idx, NULL, NULL, NULL);
14727 	if (ret) {
14728 		dev_info(&pf->pdev->dev,
14729 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14730 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14731 		return -EPERM;
14732 	}
14733 	ret = i40e_veb_get_bw_info(veb);
14734 	if (ret) {
14735 		dev_info(&pf->pdev->dev,
14736 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14737 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14738 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14739 		return -ENOENT;
14740 	}
14741 
14742 	if (vsi) {
14743 		vsi->uplink_seid = veb->seid;
14744 		vsi->veb_idx = veb->idx;
14745 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14746 	}
14747 
14748 	return 0;
14749 }
14750 
14751 /**
14752  * i40e_veb_setup - Set up a VEB
14753  * @pf: board private structure
14754  * @uplink_seid: the switch element to link to
14755  * @vsi_seid: the initial VSI seid
14756  * @enabled_tc: Enabled TC bit-map
14757  *
14758  * This allocates the sw VEB structure and links it into the switch
14759  * It is possible and legal for this to be a duplicate of an already
14760  * existing VEB.  It is also possible for both uplink and vsi seids
14761  * to be zero, in order to create a floating VEB.
14762  *
14763  * Returns pointer to the successfully allocated VEB sw struct on
14764  * success, otherwise returns NULL on failure.
14765  **/
i40e_veb_setup(struct i40e_pf * pf,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14766 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14767 				u16 vsi_seid, u8 enabled_tc)
14768 {
14769 	struct i40e_vsi *vsi = NULL;
14770 	struct i40e_veb *veb;
14771 	int veb_idx;
14772 	int ret;
14773 
14774 	/* if one seid is 0, the other must be 0 to create a floating relay */
14775 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14776 	    (uplink_seid + vsi_seid != 0)) {
14777 		dev_info(&pf->pdev->dev,
14778 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14779 			 uplink_seid, vsi_seid);
14780 		return NULL;
14781 	}
14782 
14783 	/* make sure there is such a vsi and uplink */
14784 	if (vsi_seid) {
14785 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14786 		if (!vsi) {
14787 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14788 				vsi_seid);
14789 			return NULL;
14790 		}
14791 	}
14792 
14793 	/* get veb sw struct */
14794 	veb_idx = i40e_veb_mem_alloc(pf);
14795 	if (veb_idx < 0)
14796 		goto err_alloc;
14797 	veb = pf->veb[veb_idx];
14798 	veb->uplink_seid = uplink_seid;
14799 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14800 
14801 	/* create the VEB in the switch */
14802 	ret = i40e_add_veb(veb, vsi);
14803 	if (ret)
14804 		goto err_veb;
14805 
14806 	if (vsi && vsi->idx == pf->lan_vsi)
14807 		pf->lan_veb = veb->idx;
14808 
14809 	return veb;
14810 
14811 err_veb:
14812 	i40e_veb_clear(veb);
14813 err_alloc:
14814 	return NULL;
14815 }
14816 
14817 /**
14818  * i40e_setup_pf_switch_element - set PF vars based on switch type
14819  * @pf: board private structure
14820  * @ele: element we are building info from
14821  * @num_reported: total number of elements
14822  * @printconfig: should we print the contents
14823  *
14824  * helper function to assist in extracting a few useful SEID values.
14825  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14826 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14827 				struct i40e_aqc_switch_config_element_resp *ele,
14828 				u16 num_reported, bool printconfig)
14829 {
14830 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14831 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14832 	u8 element_type = ele->element_type;
14833 	u16 seid = le16_to_cpu(ele->seid);
14834 	struct i40e_veb *veb;
14835 
14836 	if (printconfig)
14837 		dev_info(&pf->pdev->dev,
14838 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14839 			 element_type, seid, uplink_seid, downlink_seid);
14840 
14841 	switch (element_type) {
14842 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14843 		pf->mac_seid = seid;
14844 		break;
14845 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14846 		/* Main VEB? */
14847 		if (uplink_seid != pf->mac_seid)
14848 			break;
14849 		veb = i40e_pf_get_main_veb(pf);
14850 		if (!veb) {
14851 			int v;
14852 
14853 			/* find existing or else empty VEB */
14854 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14855 			if (veb) {
14856 				pf->lan_veb = veb->idx;
14857 			} else {
14858 				v = i40e_veb_mem_alloc(pf);
14859 				if (v < 0)
14860 					break;
14861 				pf->lan_veb = v;
14862 			}
14863 		}
14864 
14865 		/* Try to get again main VEB as pf->lan_veb may have changed */
14866 		veb = i40e_pf_get_main_veb(pf);
14867 		if (!veb)
14868 			break;
14869 
14870 		veb->seid = seid;
14871 		veb->uplink_seid = pf->mac_seid;
14872 		veb->pf = pf;
14873 		break;
14874 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14875 		if (num_reported != 1)
14876 			break;
14877 		/* This is immediately after a reset so we can assume this is
14878 		 * the PF's VSI
14879 		 */
14880 		pf->mac_seid = uplink_seid;
14881 		pf->main_vsi_seid = seid;
14882 		if (printconfig)
14883 			dev_info(&pf->pdev->dev,
14884 				 "pf_seid=%d main_vsi_seid=%d\n",
14885 				 downlink_seid, pf->main_vsi_seid);
14886 		break;
14887 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14888 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14889 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14890 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14891 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14892 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14893 		/* ignore these for now */
14894 		break;
14895 	default:
14896 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14897 			 element_type, seid);
14898 		break;
14899 	}
14900 }
14901 
14902 /**
14903  * i40e_fetch_switch_configuration - Get switch config from firmware
14904  * @pf: board private structure
14905  * @printconfig: should we print the contents
14906  *
14907  * Get the current switch configuration from the device and
14908  * extract a few useful SEID values.
14909  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14910 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14911 {
14912 	struct i40e_aqc_get_switch_config_resp *sw_config;
14913 	u16 next_seid = 0;
14914 	int ret = 0;
14915 	u8 *aq_buf;
14916 	int i;
14917 
14918 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14919 	if (!aq_buf)
14920 		return -ENOMEM;
14921 
14922 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14923 	do {
14924 		u16 num_reported, num_total;
14925 
14926 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14927 						I40E_AQ_LARGE_BUF,
14928 						&next_seid, NULL);
14929 		if (ret) {
14930 			dev_info(&pf->pdev->dev,
14931 				 "get switch config failed err %d aq_err %s\n",
14932 				 ret, libie_aq_str(pf->hw.aq.asq_last_status));
14933 			kfree(aq_buf);
14934 			return -ENOENT;
14935 		}
14936 
14937 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14938 		num_total = le16_to_cpu(sw_config->header.num_total);
14939 
14940 		if (printconfig)
14941 			dev_info(&pf->pdev->dev,
14942 				 "header: %d reported %d total\n",
14943 				 num_reported, num_total);
14944 
14945 		for (i = 0; i < num_reported; i++) {
14946 			struct i40e_aqc_switch_config_element_resp *ele =
14947 				&sw_config->element[i];
14948 
14949 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14950 						     printconfig);
14951 		}
14952 	} while (next_seid != 0);
14953 
14954 	kfree(aq_buf);
14955 	return ret;
14956 }
14957 
14958 /**
14959  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14960  * @pf: board private structure
14961  * @reinit: if the Main VSI needs to re-initialized.
14962  * @lock_acquired: indicates whether or not the lock has been acquired
14963  *
14964  * Returns 0 on success, negative value on failure
14965  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)14966 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14967 {
14968 	struct i40e_vsi *main_vsi;
14969 	u16 flags = 0;
14970 	int ret;
14971 
14972 	/* find out what's out there already */
14973 	ret = i40e_fetch_switch_configuration(pf, false);
14974 	if (ret) {
14975 		dev_info(&pf->pdev->dev,
14976 			 "couldn't fetch switch config, err %pe aq_err %s\n",
14977 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14978 		return ret;
14979 	}
14980 	i40e_pf_reset_stats(pf);
14981 
14982 	/* set the switch config bit for the whole device to
14983 	 * support limited promisc or true promisc
14984 	 * when user requests promisc. The default is limited
14985 	 * promisc.
14986 	*/
14987 
14988 	if ((pf->hw.pf_id == 0) &&
14989 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
14990 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14991 		pf->last_sw_conf_flags = flags;
14992 	}
14993 
14994 	if (pf->hw.pf_id == 0) {
14995 		u16 valid_flags;
14996 
14997 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14998 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14999 						NULL);
15000 		if (ret && pf->hw.aq.asq_last_status != LIBIE_AQ_RC_ESRCH) {
15001 			dev_info(&pf->pdev->dev,
15002 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15003 				 ERR_PTR(ret),
15004 				 libie_aq_str(pf->hw.aq.asq_last_status));
15005 			/* not a fatal problem, just keep going */
15006 		}
15007 		pf->last_sw_conf_valid_flags = valid_flags;
15008 	}
15009 
15010 	/* first time setup */
15011 	main_vsi = i40e_pf_get_main_vsi(pf);
15012 	if (!main_vsi || reinit) {
15013 		struct i40e_veb *veb;
15014 		u16 uplink_seid;
15015 
15016 		/* Set up the PF VSI associated with the PF's main VSI
15017 		 * that is already in the HW switch
15018 		 */
15019 		veb = i40e_pf_get_main_veb(pf);
15020 		if (veb)
15021 			uplink_seid = veb->seid;
15022 		else
15023 			uplink_seid = pf->mac_seid;
15024 		if (!main_vsi)
15025 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15026 						  uplink_seid, 0);
15027 		else if (reinit)
15028 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15029 		if (!main_vsi) {
15030 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15031 			i40e_cloud_filter_exit(pf);
15032 			i40e_fdir_teardown(pf);
15033 			return -EAGAIN;
15034 		}
15035 	} else {
15036 		/* force a reset of TC and queue layout configurations */
15037 		main_vsi->seid = pf->main_vsi_seid;
15038 		i40e_vsi_reconfig_tc(main_vsi);
15039 	}
15040 	i40e_vlan_stripping_disable(main_vsi);
15041 
15042 	i40e_fdir_sb_setup(pf);
15043 
15044 	/* Setup static PF queue filter control settings */
15045 	ret = i40e_setup_pf_filter_control(pf);
15046 	if (ret) {
15047 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15048 			 ret);
15049 		/* Failure here should not stop continuing other steps */
15050 	}
15051 
15052 	/* enable RSS in the HW, even for only one queue, as the stack can use
15053 	 * the hash
15054 	 */
15055 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15056 		i40e_pf_config_rss(pf);
15057 
15058 	/* fill in link information and enable LSE reporting */
15059 	i40e_link_event(pf);
15060 
15061 	i40e_ptp_init(pf);
15062 
15063 	if (!lock_acquired)
15064 		rtnl_lock();
15065 
15066 	/* repopulate tunnel port filters */
15067 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15068 
15069 	if (!lock_acquired)
15070 		rtnl_unlock();
15071 
15072 	return ret;
15073 }
15074 
15075 /**
15076  * i40e_determine_queue_usage - Work out queue distribution
15077  * @pf: board private structure
15078  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15079 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15080 {
15081 	int queues_left;
15082 	int q_max;
15083 
15084 	pf->num_lan_qps = 0;
15085 
15086 	/* Find the max queues to be put into basic use.  We'll always be
15087 	 * using TC0, whether or not DCB is running, and TC0 will get the
15088 	 * big RSS set.
15089 	 */
15090 	queues_left = pf->hw.func_caps.num_tx_qp;
15091 
15092 	if ((queues_left == 1) ||
15093 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15094 		/* one qp for PF, no queues for anything else */
15095 		queues_left = 0;
15096 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15097 
15098 		/* make sure all the fancies are disabled */
15099 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15100 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15101 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15102 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15103 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15104 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15105 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15106 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15107 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15108 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15109 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15110 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15111 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15112 		/* one qp for PF */
15113 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15114 		queues_left -= pf->num_lan_qps;
15115 
15116 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15117 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15118 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15119 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15120 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15121 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15122 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15123 	} else {
15124 		/* Not enough queues for all TCs */
15125 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15126 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15127 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15128 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15129 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15130 		}
15131 
15132 		/* limit lan qps to the smaller of qps, cpus or msix */
15133 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15134 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15135 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15136 		pf->num_lan_qps = q_max;
15137 
15138 		queues_left -= pf->num_lan_qps;
15139 	}
15140 
15141 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15142 		if (queues_left > 1) {
15143 			queues_left -= 1; /* save 1 queue for FD */
15144 		} else {
15145 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15146 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15147 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15148 		}
15149 	}
15150 
15151 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15152 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15153 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15154 					(queues_left / pf->num_vf_qps));
15155 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15156 	}
15157 
15158 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15159 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15160 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15161 					  (queues_left / pf->num_vmdq_qps));
15162 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15163 	}
15164 
15165 	pf->queues_left = queues_left;
15166 	dev_dbg(&pf->pdev->dev,
15167 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15168 		pf->hw.func_caps.num_tx_qp,
15169 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15170 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15171 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15172 		queues_left);
15173 }
15174 
15175 /**
15176  * i40e_setup_pf_filter_control - Setup PF static filter control
15177  * @pf: PF to be setup
15178  *
15179  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15180  * settings. If PE/FCoE are enabled then it will also set the per PF
15181  * based filter sizes required for them. It also enables Flow director,
15182  * ethertype and macvlan type filter settings for the pf.
15183  *
15184  * Returns 0 on success, negative on failure
15185  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15186 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15187 {
15188 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15189 
15190 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15191 
15192 	/* Flow Director is enabled */
15193 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15194 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15195 		settings->enable_fdir = true;
15196 
15197 	/* Ethtype and MACVLAN filters enabled for PF */
15198 	settings->enable_ethtype = true;
15199 	settings->enable_macvlan = true;
15200 
15201 	if (i40e_set_filter_control(&pf->hw, settings))
15202 		return -ENOENT;
15203 
15204 	return 0;
15205 }
15206 
15207 #define INFO_STRING_LEN 255
15208 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15209 static void i40e_print_features(struct i40e_pf *pf)
15210 {
15211 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15212 	struct i40e_hw *hw = &pf->hw;
15213 	char *buf;
15214 	int i;
15215 
15216 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15217 	if (!buf)
15218 		return;
15219 
15220 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15221 #ifdef CONFIG_PCI_IOV
15222 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15223 #endif
15224 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15225 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15226 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15227 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15228 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15229 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15230 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15231 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15232 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15233 	}
15234 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15235 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15236 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15237 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15238 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15239 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15240 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15241 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15242 	else
15243 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15244 
15245 	dev_info(&pf->pdev->dev, "%s\n", buf);
15246 	kfree(buf);
15247 	WARN_ON(i > INFO_STRING_LEN);
15248 }
15249 
15250 /**
15251  * i40e_get_platform_mac_addr - get platform-specific MAC address
15252  * @pdev: PCI device information struct
15253  * @pf: board private structure
15254  *
15255  * Look up the MAC address for the device. First we'll try
15256  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15257  * specific fallback. Otherwise, we'll default to the stored value in
15258  * firmware.
15259  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15260 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15261 {
15262 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15263 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15264 }
15265 
15266 /**
15267  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15268  * @fec_cfg: FEC option to set in flags
15269  * @flags: ptr to flags in which we set FEC option
15270  **/
i40e_set_fec_in_flags(u8 fec_cfg,unsigned long * flags)15271 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15272 {
15273 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15274 		set_bit(I40E_FLAG_RS_FEC, flags);
15275 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15276 	}
15277 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15278 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15279 		set_bit(I40E_FLAG_RS_FEC, flags);
15280 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15281 	}
15282 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15283 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15284 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15285 		clear_bit(I40E_FLAG_RS_FEC, flags);
15286 	}
15287 	if (fec_cfg == 0) {
15288 		clear_bit(I40E_FLAG_RS_FEC, flags);
15289 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15290 	}
15291 }
15292 
15293 /**
15294  * i40e_check_recovery_mode - check if we are running transition firmware
15295  * @pf: board private structure
15296  *
15297  * Check registers indicating the firmware runs in recovery mode. Sets the
15298  * appropriate driver state.
15299  *
15300  * Returns true if the recovery mode was detected, false otherwise
15301  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15302 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15303 {
15304 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15305 
15306 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15307 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15308 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15309 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15310 
15311 		return true;
15312 	}
15313 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15314 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15315 
15316 	return false;
15317 }
15318 
15319 /**
15320  * i40e_pf_loop_reset - perform reset in a loop.
15321  * @pf: board private structure
15322  *
15323  * This function is useful when a NIC is about to enter recovery mode.
15324  * When a NIC's internal data structures are corrupted the NIC's
15325  * firmware is going to enter recovery mode.
15326  * Right after a POR it takes about 7 minutes for firmware to enter
15327  * recovery mode. Until that time a NIC is in some kind of intermediate
15328  * state. After that time period the NIC almost surely enters
15329  * recovery mode. The only way for a driver to detect intermediate
15330  * state is to issue a series of pf-resets and check a return value.
15331  * If a PF reset returns success then the firmware could be in recovery
15332  * mode so the caller of this code needs to check for recovery mode
15333  * if this function returns success. There is a little chance that
15334  * firmware will hang in intermediate state forever.
15335  * Since waiting 7 minutes is quite a lot of time this function waits
15336  * 10 seconds and then gives up by returning an error.
15337  *
15338  * Return 0 on success, negative on failure.
15339  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15340 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15341 {
15342 	/* wait max 10 seconds for PF reset to succeed */
15343 	const unsigned long time_end = jiffies + 10 * HZ;
15344 	struct i40e_hw *hw = &pf->hw;
15345 	int ret;
15346 
15347 	ret = i40e_pf_reset(hw);
15348 	while (ret != 0 && time_before(jiffies, time_end)) {
15349 		usleep_range(10000, 20000);
15350 		ret = i40e_pf_reset(hw);
15351 	}
15352 
15353 	if (ret == 0)
15354 		pf->pfr_count++;
15355 	else
15356 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15357 
15358 	return ret;
15359 }
15360 
15361 /**
15362  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15363  * @pf: board private structure
15364  *
15365  * Check FW registers to determine if FW issued unexpected EMP Reset.
15366  * Every time when unexpected EMP Reset occurs the FW increments
15367  * a counter of unexpected EMP Resets. When the counter reaches 10
15368  * the FW should enter the Recovery mode
15369  *
15370  * Returns true if FW issued unexpected EMP Reset
15371  **/
i40e_check_fw_empr(struct i40e_pf * pf)15372 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15373 {
15374 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15375 			   I40E_GL_FWSTS_FWS1B_MASK;
15376 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15377 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15378 }
15379 
15380 /**
15381  * i40e_handle_resets - handle EMP resets and PF resets
15382  * @pf: board private structure
15383  *
15384  * Handle both EMP resets and PF resets and conclude whether there are
15385  * any issues regarding these resets. If there are any issues then
15386  * generate log entry.
15387  *
15388  * Return 0 if NIC is healthy or negative value when there are issues
15389  * with resets
15390  **/
i40e_handle_resets(struct i40e_pf * pf)15391 static int i40e_handle_resets(struct i40e_pf *pf)
15392 {
15393 	const int pfr = i40e_pf_loop_reset(pf);
15394 	const bool is_empr = i40e_check_fw_empr(pf);
15395 
15396 	if (is_empr || pfr != 0)
15397 		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");
15398 
15399 	return is_empr ? -EIO : pfr;
15400 }
15401 
15402 /**
15403  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15404  * @pf: board private structure
15405  * @hw: ptr to the hardware info
15406  *
15407  * This function does a minimal setup of all subsystems needed for running
15408  * recovery mode.
15409  *
15410  * Returns 0 on success, negative on failure
15411  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15412 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15413 {
15414 	struct i40e_vsi *vsi;
15415 	int err;
15416 	int v_idx;
15417 
15418 	pci_set_drvdata(pf->pdev, pf);
15419 	pci_save_state(pf->pdev);
15420 
15421 	/* set up periodic task facility */
15422 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15423 	pf->service_timer_period = HZ;
15424 
15425 	INIT_WORK(&pf->service_task, i40e_service_task);
15426 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15427 
15428 	err = i40e_init_interrupt_scheme(pf);
15429 	if (err)
15430 		goto err_switch_setup;
15431 
15432 	/* The number of VSIs reported by the FW is the minimum guaranteed
15433 	 * to us; HW supports far more and we share the remaining pool with
15434 	 * the other PFs. We allocate space for more than the guarantee with
15435 	 * the understanding that we might not get them all later.
15436 	 */
15437 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15438 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15439 	else
15440 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15441 
15442 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15443 	pf->vsi = kzalloc_objs(struct i40e_vsi *, pf->num_alloc_vsi);
15444 	if (!pf->vsi) {
15445 		err = -ENOMEM;
15446 		goto err_switch_setup;
15447 	}
15448 
15449 	/* We allocate one VSI which is needed as absolute minimum
15450 	 * in order to register the netdev
15451 	 */
15452 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15453 	if (v_idx < 0) {
15454 		err = v_idx;
15455 		goto err_switch_setup;
15456 	}
15457 	pf->lan_vsi = v_idx;
15458 	vsi = pf->vsi[v_idx];
15459 	if (!vsi) {
15460 		err = -EFAULT;
15461 		goto err_switch_setup;
15462 	}
15463 	vsi->alloc_queue_pairs = 1;
15464 	err = i40e_config_netdev(vsi);
15465 	if (err)
15466 		goto err_switch_setup;
15467 	err = register_netdev(vsi->netdev);
15468 	if (err)
15469 		goto err_switch_setup;
15470 	vsi->netdev_registered = true;
15471 	i40e_dbg_pf_init(pf);
15472 
15473 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15474 	if (err)
15475 		goto err_switch_setup;
15476 
15477 	/* tell the firmware that we're starting */
15478 	i40e_send_version(pf);
15479 
15480 	/* since everything's happy, start the service_task timer */
15481 	mod_timer(&pf->service_timer,
15482 		  round_jiffies(jiffies + pf->service_timer_period));
15483 
15484 	return 0;
15485 
15486 err_switch_setup:
15487 	i40e_reset_interrupt_capability(pf);
15488 	timer_shutdown_sync(&pf->service_timer);
15489 	i40e_shutdown_adminq(hw);
15490 	iounmap(hw->hw_addr);
15491 	pci_release_mem_regions(pf->pdev);
15492 	pci_disable_device(pf->pdev);
15493 	i40e_free_pf(pf);
15494 
15495 	return err;
15496 }
15497 
15498 /**
15499  * i40e_set_subsystem_device_id - set subsystem device id
15500  * @hw: pointer to the hardware info
15501  *
15502  * Set PCI subsystem device id either from a pci_dev structure or
15503  * a specific FW register.
15504  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15505 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15506 {
15507 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15508 
15509 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15510 		pf->pdev->subsystem_device :
15511 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15512 }
15513 
15514 /**
15515  * i40e_probe - Device initialization routine
15516  * @pdev: PCI device information struct
15517  * @ent: entry in i40e_pci_tbl
15518  *
15519  * i40e_probe initializes a PF identified by a pci_dev structure.
15520  * The OS initialization, configuring of the PF private structure,
15521  * and a hardware reset occur.
15522  *
15523  * Returns 0 on success, negative on failure
15524  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15525 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15526 {
15527 	struct i40e_aq_get_phy_abilities_resp abilities;
15528 #ifdef CONFIG_I40E_DCB
15529 	enum i40e_get_fw_lldp_status_resp lldp_status;
15530 #endif /* CONFIG_I40E_DCB */
15531 	struct i40e_vsi *vsi;
15532 	struct i40e_pf *pf;
15533 	struct i40e_hw *hw;
15534 	u16 wol_nvm_bits;
15535 	char nvm_ver[32];
15536 	u16 link_status;
15537 #ifdef CONFIG_I40E_DCB
15538 	int status;
15539 #endif /* CONFIG_I40E_DCB */
15540 	int err;
15541 	u32 val;
15542 
15543 	err = pci_enable_device_mem(pdev);
15544 	if (err)
15545 		return err;
15546 
15547 	/* set up for high or low dma */
15548 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15549 	if (err) {
15550 		dev_err(&pdev->dev,
15551 			"DMA configuration failed: 0x%x\n", err);
15552 		goto err_dma;
15553 	}
15554 
15555 	/* set up pci connections */
15556 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15557 	if (err) {
15558 		dev_info(&pdev->dev,
15559 			 "pci_request_selected_regions failed %d\n", err);
15560 		goto err_pci_reg;
15561 	}
15562 
15563 	pci_set_master(pdev);
15564 
15565 	/* Now that we have a PCI connection, we need to do the
15566 	 * low level device setup.  This is primarily setting up
15567 	 * the Admin Queue structures and then querying for the
15568 	 * device's current profile information.
15569 	 */
15570 	pf = i40e_alloc_pf(&pdev->dev);
15571 	if (!pf) {
15572 		err = -ENOMEM;
15573 		goto err_pf_alloc;
15574 	}
15575 	pf->next_vsi = 0;
15576 	pf->pdev = pdev;
15577 	set_bit(__I40E_DOWN, pf->state);
15578 
15579 	hw = &pf->hw;
15580 
15581 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15582 				I40E_MAX_CSR_SPACE);
15583 	/* We believe that the highest register to read is
15584 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15585 	 * is not less than that before mapping to prevent a
15586 	 * kernel panic.
15587 	 */
15588 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15589 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15590 			pf->ioremap_len);
15591 		err = -ENOMEM;
15592 		goto err_ioremap;
15593 	}
15594 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15595 	if (!hw->hw_addr) {
15596 		err = -EIO;
15597 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15598 			 (unsigned int)pci_resource_start(pdev, 0),
15599 			 pf->ioremap_len, err);
15600 		goto err_ioremap;
15601 	}
15602 	hw->vendor_id = pdev->vendor;
15603 	hw->device_id = pdev->device;
15604 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15605 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15606 	i40e_set_subsystem_device_id(hw);
15607 	hw->bus.device = PCI_SLOT(pdev->devfn);
15608 	hw->bus.func = PCI_FUNC(pdev->devfn);
15609 	hw->bus.bus_id = pdev->bus->number;
15610 
15611 	/* Select something other than the 802.1ad ethertype for the
15612 	 * switch to use internally and drop on ingress.
15613 	 */
15614 	hw->switch_tag = 0xffff;
15615 	hw->first_tag = ETH_P_8021AD;
15616 	hw->second_tag = ETH_P_8021Q;
15617 
15618 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15619 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15620 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15621 
15622 	/* set up the locks for the AQ, do this only once in probe
15623 	 * and destroy them only once in remove
15624 	 */
15625 	mutex_init(&hw->aq.asq_mutex);
15626 	mutex_init(&hw->aq.arq_mutex);
15627 
15628 	pf->msg_enable = netif_msg_init(debug,
15629 					NETIF_MSG_DRV |
15630 					NETIF_MSG_PROBE |
15631 					NETIF_MSG_LINK);
15632 	if (debug < -1)
15633 		pf->hw.debug_mask = debug;
15634 
15635 	/* do a special CORER for clearing PXE mode once at init */
15636 	if (hw->revision_id == 0 &&
15637 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15638 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15639 		i40e_flush(hw);
15640 		msleep(200);
15641 		pf->corer_count++;
15642 
15643 		i40e_clear_pxe_mode(hw);
15644 	}
15645 
15646 	/* Reset here to make sure all is clean and to define PF 'n' */
15647 	i40e_clear_hw(hw);
15648 
15649 	err = i40e_set_mac_type(hw);
15650 	if (err) {
15651 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15652 			 err);
15653 		goto err_pf_reset;
15654 	}
15655 
15656 	err = i40e_handle_resets(pf);
15657 	if (err)
15658 		goto err_pf_reset;
15659 
15660 	i40e_check_recovery_mode(pf);
15661 
15662 	if (is_kdump_kernel()) {
15663 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15664 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15665 	} else {
15666 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15667 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15668 	}
15669 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15670 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15671 
15672 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15673 		 "%s-%s:misc",
15674 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15675 
15676 	err = i40e_init_shared_code(hw);
15677 	if (err) {
15678 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15679 			 err);
15680 		goto err_pf_reset;
15681 	}
15682 
15683 	/* set up a default setting for link flow control */
15684 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15685 
15686 	err = i40e_init_adminq(hw);
15687 	if (err) {
15688 		if (err == -EIO)
15689 			dev_info(&pdev->dev,
15690 				 "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",
15691 				 hw->aq.api_maj_ver,
15692 				 hw->aq.api_min_ver,
15693 				 I40E_FW_API_VERSION_MAJOR,
15694 				 I40E_FW_MINOR_VERSION(hw));
15695 		else
15696 			dev_info(&pdev->dev,
15697 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15698 
15699 		goto err_pf_reset;
15700 	}
15701 	i40e_get_oem_version(hw);
15702 	i40e_get_pba_string(hw);
15703 
15704 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15705 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15706 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15707 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15708 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15709 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15710 		 hw->subsystem_device_id);
15711 
15712 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15713 				  I40E_FW_MINOR_VERSION(hw) + 1))
15714 		dev_dbg(&pdev->dev,
15715 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15716 			 hw->aq.api_maj_ver,
15717 			 hw->aq.api_min_ver,
15718 			 I40E_FW_API_VERSION_MAJOR,
15719 			 I40E_FW_MINOR_VERSION(hw));
15720 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15721 		dev_info(&pdev->dev,
15722 			 "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",
15723 			 hw->aq.api_maj_ver,
15724 			 hw->aq.api_min_ver,
15725 			 I40E_FW_API_VERSION_MAJOR,
15726 			 I40E_FW_MINOR_VERSION(hw));
15727 
15728 	i40e_verify_eeprom(pf);
15729 
15730 	/* Rev 0 hardware was never productized */
15731 	if (hw->revision_id < 1)
15732 		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");
15733 
15734 	i40e_clear_pxe_mode(hw);
15735 
15736 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15737 	if (err)
15738 		goto err_adminq_setup;
15739 
15740 	err = i40e_sw_init(pf);
15741 	if (err) {
15742 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15743 		goto err_sw_init;
15744 	}
15745 
15746 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15747 		return i40e_init_recovery_mode(pf, hw);
15748 
15749 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15750 				hw->func_caps.num_rx_qp, 0, 0);
15751 	if (err) {
15752 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15753 		goto err_init_lan_hmc;
15754 	}
15755 
15756 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15757 	if (err) {
15758 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15759 		err = -ENOENT;
15760 		goto err_configure_lan_hmc;
15761 	}
15762 
15763 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15764 	 * Ignore error return codes because if it was already disabled via
15765 	 * hardware settings this will fail
15766 	 */
15767 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15768 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15769 		i40e_aq_stop_lldp(hw, true, false, NULL);
15770 	}
15771 
15772 	/* allow a platform config to override the HW addr */
15773 	i40e_get_platform_mac_addr(pdev, pf);
15774 
15775 	if (!is_valid_ether_addr(hw->mac.addr)) {
15776 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15777 		err = -EIO;
15778 		goto err_mac_addr;
15779 	}
15780 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15781 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15782 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15783 	if (is_valid_ether_addr(hw->mac.port_addr))
15784 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15785 
15786 	i40e_ptp_alloc_pins(pf);
15787 	pci_set_drvdata(pdev, pf);
15788 	pci_save_state(pdev);
15789 
15790 #ifdef CONFIG_I40E_DCB
15791 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15792 	(!status &&
15793 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15794 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15795 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15796 	dev_info(&pdev->dev,
15797 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15798 			"FW LLDP is disabled\n" :
15799 			"FW LLDP is enabled\n");
15800 
15801 	/* Enable FW to write default DCB config on link-up */
15802 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15803 
15804 	err = i40e_init_pf_dcb(pf);
15805 	if (err) {
15806 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15807 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15808 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15809 		/* Continue without DCB enabled */
15810 	}
15811 #endif /* CONFIG_I40E_DCB */
15812 
15813 	/* set up periodic task facility */
15814 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15815 	pf->service_timer_period = HZ;
15816 
15817 	INIT_WORK(&pf->service_task, i40e_service_task);
15818 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15819 
15820 	/* NVM bit on means WoL disabled for the port */
15821 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15822 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15823 		pf->wol_en = false;
15824 	else
15825 		pf->wol_en = true;
15826 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15827 
15828 	/* set up the main switch operations */
15829 	i40e_determine_queue_usage(pf);
15830 	err = i40e_init_interrupt_scheme(pf);
15831 	if (err)
15832 		goto err_switch_setup;
15833 
15834 	/* Reduce Tx and Rx pairs for kdump
15835 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15836 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15837 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15838 	 */
15839 	if (is_kdump_kernel())
15840 		pf->num_lan_msix = 1;
15841 
15842 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15843 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15844 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15845 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15846 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15847 						    UDP_TUNNEL_TYPE_GENEVE;
15848 
15849 	/* The number of VSIs reported by the FW is the minimum guaranteed
15850 	 * to us; HW supports far more and we share the remaining pool with
15851 	 * the other PFs. We allocate space for more than the guarantee with
15852 	 * the understanding that we might not get them all later.
15853 	 */
15854 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15855 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15856 	else
15857 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15858 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15859 		dev_warn(&pf->pdev->dev,
15860 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15861 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15862 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15863 	}
15864 
15865 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15866 	pf->vsi = kzalloc_objs(struct i40e_vsi *, pf->num_alloc_vsi);
15867 	if (!pf->vsi) {
15868 		err = -ENOMEM;
15869 		goto err_switch_setup;
15870 	}
15871 
15872 #ifdef CONFIG_PCI_IOV
15873 	/* prep for VF support */
15874 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15875 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15876 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15877 		if (pci_num_vf(pdev))
15878 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15879 	}
15880 #endif
15881 	err = i40e_setup_pf_switch(pf, false, false);
15882 	if (err) {
15883 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15884 		goto err_vsis;
15885 	}
15886 
15887 	vsi = i40e_pf_get_main_vsi(pf);
15888 	INIT_LIST_HEAD(&vsi->ch_list);
15889 
15890 	/* if FDIR VSI was set up, start it now */
15891 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15892 	if (vsi)
15893 		i40e_vsi_open(vsi);
15894 
15895 	/* The driver only wants link up/down and module qualification
15896 	 * reports from firmware.  Note the negative logic.
15897 	 */
15898 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15899 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15900 					 I40E_AQ_EVENT_MEDIA_NA |
15901 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15902 	if (err)
15903 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15904 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
15905 
15906 	/* VF MDD event logs are rate limited to one second intervals */
15907 	ratelimit_state_init(&pf->mdd_message_rate_limit, 1 * HZ, 1);
15908 
15909 	/* Reconfigure hardware for allowing smaller MSS in the case
15910 	 * of TSO, so that we avoid the MDD being fired and causing
15911 	 * a reset in the case of small MSS+TSO.
15912 	 */
15913 	val = rd32(hw, I40E_REG_MSS);
15914 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15915 		val &= ~I40E_REG_MSS_MIN_MASK;
15916 		val |= I40E_64BYTE_MSS;
15917 		wr32(hw, I40E_REG_MSS, val);
15918 	}
15919 
15920 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15921 		msleep(75);
15922 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15923 		if (err)
15924 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15925 				 ERR_PTR(err),
15926 				 libie_aq_str(pf->hw.aq.asq_last_status));
15927 	}
15928 	/* The main driver is (mostly) up and happy. We need to set this state
15929 	 * before setting up the misc vector or we get a race and the vector
15930 	 * ends up disabled forever.
15931 	 */
15932 	clear_bit(__I40E_DOWN, pf->state);
15933 
15934 	/* In case of MSIX we are going to setup the misc vector right here
15935 	 * to handle admin queue events etc. In case of legacy and MSI
15936 	 * the misc functionality and queue processing is combined in
15937 	 * the same vector and that gets setup at open.
15938 	 */
15939 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15940 		err = i40e_setup_misc_vector(pf);
15941 		if (err) {
15942 			dev_info(&pdev->dev,
15943 				 "setup of misc vector failed: %d\n", err);
15944 			i40e_cloud_filter_exit(pf);
15945 			i40e_fdir_teardown(pf);
15946 			goto err_vsis;
15947 		}
15948 	}
15949 
15950 #ifdef CONFIG_PCI_IOV
15951 	/* prep for VF support */
15952 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15953 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15954 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15955 		/* disable link interrupts for VFs */
15956 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15957 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15958 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15959 		i40e_flush(hw);
15960 
15961 		if (pci_num_vf(pdev)) {
15962 			dev_info(&pdev->dev,
15963 				 "Active VFs found, allocating resources.\n");
15964 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15965 			if (err)
15966 				dev_info(&pdev->dev,
15967 					 "Error %d allocating resources for existing VFs\n",
15968 					 err);
15969 		}
15970 	}
15971 #endif /* CONFIG_PCI_IOV */
15972 
15973 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15974 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15975 						      pf->num_iwarp_msix,
15976 						      I40E_IWARP_IRQ_PILE_ID);
15977 		if (pf->iwarp_base_vector < 0) {
15978 			dev_info(&pdev->dev,
15979 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15980 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15981 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15982 		}
15983 	}
15984 
15985 	i40e_dbg_pf_init(pf);
15986 
15987 	/* tell the firmware that we're starting */
15988 	i40e_send_version(pf);
15989 
15990 	/* since everything's happy, start the service_task timer */
15991 	mod_timer(&pf->service_timer,
15992 		  round_jiffies(jiffies + pf->service_timer_period));
15993 
15994 	/* add this PF to client device list and launch a client service task */
15995 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15996 		err = i40e_lan_add_device(pf);
15997 		if (err)
15998 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15999 				 err);
16000 	}
16001 
16002 #define PCI_SPEED_SIZE 8
16003 #define PCI_WIDTH_SIZE 8
16004 	/* Devices on the IOSF bus do not have this information
16005 	 * and will report PCI Gen 1 x 1 by default so don't bother
16006 	 * checking them.
16007 	 */
16008 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16009 		char speed[PCI_SPEED_SIZE] = "Unknown";
16010 		char width[PCI_WIDTH_SIZE] = "Unknown";
16011 
16012 		/* Get the negotiated link width and speed from PCI config
16013 		 * space
16014 		 */
16015 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16016 					  &link_status);
16017 
16018 		i40e_set_pci_config_data(hw, link_status);
16019 
16020 		switch (hw->bus.speed) {
16021 		case i40e_bus_speed_8000:
16022 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16023 		case i40e_bus_speed_5000:
16024 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16025 		case i40e_bus_speed_2500:
16026 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16027 		default:
16028 			break;
16029 		}
16030 		switch (hw->bus.width) {
16031 		case i40e_bus_width_pcie_x8:
16032 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16033 		case i40e_bus_width_pcie_x4:
16034 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16035 		case i40e_bus_width_pcie_x2:
16036 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16037 		case i40e_bus_width_pcie_x1:
16038 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16039 		default:
16040 			break;
16041 		}
16042 
16043 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16044 			 speed, width);
16045 
16046 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16047 		    hw->bus.speed < i40e_bus_speed_8000) {
16048 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16049 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16050 		}
16051 	}
16052 
16053 	/* get the requested speeds from the fw */
16054 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16055 	if (err)
16056 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16057 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16058 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16059 
16060 	/* set the FEC config due to the board capabilities */
16061 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16062 
16063 	/* get the supported phy types from the fw */
16064 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16065 	if (err)
16066 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16067 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16068 
16069 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16070 
16071 	err = i40e_aq_set_mac_config(hw, MAX_FRAME_SIZE_DEFAULT, NULL);
16072 	if (err)
16073 		dev_warn(&pdev->dev, "set mac config ret = %pe last_status = %s\n",
16074 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16075 
16076 	/* Make sure the MFS is set to the expected value */
16077 	val = rd32(hw, I40E_PRTGL_SAH);
16078 	FIELD_MODIFY(I40E_PRTGL_SAH_MFS_MASK, &val, MAX_FRAME_SIZE_DEFAULT);
16079 	wr32(hw, I40E_PRTGL_SAH, val);
16080 
16081 	/* Add a filter to drop all Flow control frames from any VSI from being
16082 	 * transmitted. By doing so we stop a malicious VF from sending out
16083 	 * PAUSE or PFC frames and potentially controlling traffic for other
16084 	 * PF/VF VSIs.
16085 	 * The FW can still send Flow control frames if enabled.
16086 	 */
16087 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16088 						       pf->main_vsi_seid);
16089 
16090 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16091 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16092 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16093 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16094 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16095 	/* print a string summarizing features */
16096 	i40e_print_features(pf);
16097 
16098 	i40e_devlink_register(pf);
16099 
16100 	return 0;
16101 
16102 	/* Unwind what we've done if something failed in the setup */
16103 err_vsis:
16104 	set_bit(__I40E_DOWN, pf->state);
16105 	i40e_clear_interrupt_scheme(pf);
16106 	kfree(pf->vsi);
16107 err_switch_setup:
16108 	i40e_reset_interrupt_capability(pf);
16109 	timer_shutdown_sync(&pf->service_timer);
16110 err_mac_addr:
16111 err_configure_lan_hmc:
16112 	(void)i40e_shutdown_lan_hmc(hw);
16113 err_init_lan_hmc:
16114 	kfree(pf->qp_pile);
16115 err_sw_init:
16116 err_adminq_setup:
16117 err_pf_reset:
16118 	iounmap(hw->hw_addr);
16119 err_ioremap:
16120 	i40e_free_pf(pf);
16121 err_pf_alloc:
16122 	pci_release_mem_regions(pdev);
16123 err_pci_reg:
16124 err_dma:
16125 	pci_disable_device(pdev);
16126 	return err;
16127 }
16128 
16129 /**
16130  * i40e_remove - Device removal routine
16131  * @pdev: PCI device information struct
16132  *
16133  * i40e_remove is called by the PCI subsystem to alert the driver
16134  * that is should release a PCI device.  This could be caused by a
16135  * Hot-Plug event, or because the driver is going to be removed from
16136  * memory.
16137  **/
i40e_remove(struct pci_dev * pdev)16138 static void i40e_remove(struct pci_dev *pdev)
16139 {
16140 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16141 	struct i40e_hw *hw = &pf->hw;
16142 	struct i40e_vsi *vsi;
16143 	struct i40e_veb *veb;
16144 	int ret_code;
16145 	int i;
16146 
16147 	i40e_devlink_unregister(pf);
16148 
16149 	i40e_dbg_pf_exit(pf);
16150 
16151 	i40e_ptp_stop(pf);
16152 
16153 	/* Disable RSS in hw */
16154 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16155 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16156 
16157 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16158 	 * flags, once they are set, i40e_rebuild should not be called as
16159 	 * i40e_prep_for_reset always returns early.
16160 	 */
16161 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16162 		usleep_range(1000, 2000);
16163 	set_bit(__I40E_IN_REMOVE, pf->state);
16164 
16165 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16166 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16167 		i40e_free_vfs(pf);
16168 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16169 	}
16170 	/* no more scheduling of any task */
16171 	set_bit(__I40E_SUSPENDED, pf->state);
16172 	set_bit(__I40E_DOWN, pf->state);
16173 	if (pf->service_timer.function)
16174 		timer_shutdown_sync(&pf->service_timer);
16175 	if (pf->service_task.func)
16176 		cancel_work_sync(&pf->service_task);
16177 
16178 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16179 		struct i40e_vsi *vsi = pf->vsi[0];
16180 
16181 		/* We know that we have allocated only one vsi for this PF,
16182 		 * it was just for registering netdevice, so the interface
16183 		 * could be visible in the 'ifconfig' output
16184 		 */
16185 		unregister_netdev(vsi->netdev);
16186 		free_netdev(vsi->netdev);
16187 
16188 		goto unmap;
16189 	}
16190 
16191 	/* Client close must be called explicitly here because the timer
16192 	 * has been stopped.
16193 	 */
16194 	i40e_notify_client_of_netdev_close(pf, false);
16195 
16196 	i40e_fdir_teardown(pf);
16197 
16198 	/* If there is a switch structure or any orphans, remove them.
16199 	 * This will leave only the PF's VSI remaining.
16200 	 */
16201 	i40e_pf_for_each_veb(pf, i, veb)
16202 		if (veb->uplink_seid == pf->mac_seid ||
16203 		    veb->uplink_seid == 0)
16204 			i40e_switch_branch_release(veb);
16205 
16206 	/* Now we can shutdown the PF's VSIs, just before we kill
16207 	 * adminq and hmc.
16208 	 */
16209 	i40e_pf_for_each_vsi(pf, i, vsi) {
16210 		i40e_vsi_close(vsi);
16211 		i40e_vsi_release(vsi);
16212 		pf->vsi[i] = NULL;
16213 	}
16214 
16215 	i40e_cloud_filter_exit(pf);
16216 
16217 	/* remove attached clients */
16218 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16219 		ret_code = i40e_lan_del_device(pf);
16220 		if (ret_code)
16221 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16222 				 ret_code);
16223 	}
16224 
16225 	/* shutdown and destroy the HMC */
16226 	if (hw->hmc.hmc_obj) {
16227 		ret_code = i40e_shutdown_lan_hmc(hw);
16228 		if (ret_code)
16229 			dev_warn(&pdev->dev,
16230 				 "Failed to destroy the HMC resources: %d\n",
16231 				 ret_code);
16232 	}
16233 
16234 unmap:
16235 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16236 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16237 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16238 		free_irq(pf->pdev->irq, pf);
16239 
16240 	/* shutdown the adminq */
16241 	i40e_shutdown_adminq(hw);
16242 
16243 	/* destroy the locks only once, here */
16244 	mutex_destroy(&hw->aq.arq_mutex);
16245 	mutex_destroy(&hw->aq.asq_mutex);
16246 
16247 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16248 	rtnl_lock();
16249 	i40e_clear_interrupt_scheme(pf);
16250 	i40e_pf_for_each_vsi(pf, i, vsi) {
16251 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16252 			i40e_vsi_clear_rings(vsi);
16253 
16254 		i40e_vsi_clear(vsi);
16255 		pf->vsi[i] = NULL;
16256 	}
16257 	rtnl_unlock();
16258 
16259 	i40e_pf_for_each_veb(pf, i, veb) {
16260 		kfree(veb);
16261 		pf->veb[i] = NULL;
16262 	}
16263 
16264 	kfree(pf->qp_pile);
16265 	kfree(pf->vsi);
16266 
16267 	iounmap(hw->hw_addr);
16268 	i40e_free_pf(pf);
16269 	pci_release_mem_regions(pdev);
16270 
16271 	pci_disable_device(pdev);
16272 }
16273 
16274 /**
16275  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16276  * using the mac_address_write admin q function
16277  * @pf: pointer to i40e_pf struct
16278  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16279 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16280 {
16281 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16282 	struct i40e_hw *hw = &pf->hw;
16283 	u8 mac_addr[6];
16284 	u16 flags = 0;
16285 	int ret;
16286 
16287 	/* Get current MAC address in case it's an LAA */
16288 	if (main_vsi && main_vsi->netdev) {
16289 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16290 	} else {
16291 		dev_err(&pf->pdev->dev,
16292 			"Failed to retrieve MAC address; using default\n");
16293 		ether_addr_copy(mac_addr, hw->mac.addr);
16294 	}
16295 
16296 	/* The FW expects the mac address write cmd to first be called with
16297 	 * one of these flags before calling it again with the multicast
16298 	 * enable flags.
16299 	 */
16300 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16301 
16302 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16303 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16304 
16305 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16306 	if (ret) {
16307 		dev_err(&pf->pdev->dev,
16308 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16309 		return;
16310 	}
16311 
16312 	flags = I40E_AQC_MC_MAG_EN
16313 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16314 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16315 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16316 	if (ret)
16317 		dev_err(&pf->pdev->dev,
16318 			"Failed to enable Multicast Magic Packet wake up\n");
16319 }
16320 
16321 /**
16322  * i40e_io_suspend - suspend all IO operations
16323  * @pf: pointer to i40e_pf struct
16324  *
16325  **/
i40e_io_suspend(struct i40e_pf * pf)16326 static int i40e_io_suspend(struct i40e_pf *pf)
16327 {
16328 	struct i40e_hw *hw = &pf->hw;
16329 
16330 	set_bit(__I40E_DOWN, pf->state);
16331 
16332 	/* Ensure service task will not be running */
16333 	timer_delete_sync(&pf->service_timer);
16334 	cancel_work_sync(&pf->service_task);
16335 
16336 	/* Client close must be called explicitly here because the timer
16337 	 * has been stopped.
16338 	 */
16339 	i40e_notify_client_of_netdev_close(pf, false);
16340 
16341 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16342 	    pf->wol_en)
16343 		i40e_enable_mc_magic_wake(pf);
16344 
16345 	/* Since we're going to destroy queues during the
16346 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16347 	 * whole section
16348 	 */
16349 	rtnl_lock();
16350 
16351 	i40e_prep_for_reset(pf);
16352 
16353 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16354 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16355 
16356 	/* Clear the interrupt scheme and release our IRQs so that the system
16357 	 * can safely hibernate even when there are a large number of CPUs.
16358 	 * Otherwise hibernation might fail when mapping all the vectors back
16359 	 * to CPU0.
16360 	 */
16361 	i40e_clear_interrupt_scheme(pf);
16362 
16363 	rtnl_unlock();
16364 
16365 	return 0;
16366 }
16367 
16368 /**
16369  * i40e_io_resume - resume IO operations
16370  * @pf: pointer to i40e_pf struct
16371  *
16372  **/
i40e_io_resume(struct i40e_pf * pf)16373 static int i40e_io_resume(struct i40e_pf *pf)
16374 {
16375 	struct device *dev = &pf->pdev->dev;
16376 	int err;
16377 
16378 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16379 	 * since we're going to be restoring queues
16380 	 */
16381 	rtnl_lock();
16382 
16383 	/* We cleared the interrupt scheme when we suspended, so we need to
16384 	 * restore it now to resume device functionality.
16385 	 */
16386 	err = i40e_restore_interrupt_scheme(pf);
16387 	if (err) {
16388 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16389 			err);
16390 	}
16391 
16392 	clear_bit(__I40E_DOWN, pf->state);
16393 	i40e_reset_and_rebuild(pf, false, true);
16394 
16395 	rtnl_unlock();
16396 
16397 	/* Clear suspended state last after everything is recovered */
16398 	clear_bit(__I40E_SUSPENDED, pf->state);
16399 
16400 	/* Restart the service task */
16401 	mod_timer(&pf->service_timer,
16402 		  round_jiffies(jiffies + pf->service_timer_period));
16403 
16404 	return 0;
16405 }
16406 
16407 /**
16408  * i40e_pci_error_detected - warning that something funky happened in PCI land
16409  * @pdev: PCI device information struct
16410  * @error: the type of PCI error
16411  *
16412  * Called to warn that something happened and the error handling steps
16413  * are in progress.  Allows the driver to quiesce things, be ready for
16414  * remediation.
16415  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16416 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16417 						pci_channel_state_t error)
16418 {
16419 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16420 
16421 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16422 
16423 	if (!pf) {
16424 		dev_info(&pdev->dev,
16425 			 "Cannot recover - error happened during device probe\n");
16426 		return PCI_ERS_RESULT_DISCONNECT;
16427 	}
16428 
16429 	/* shutdown all operations */
16430 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16431 		i40e_io_suspend(pf);
16432 
16433 	/* Request a slot reset */
16434 	return PCI_ERS_RESULT_NEED_RESET;
16435 }
16436 
16437 /**
16438  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16439  * @pdev: PCI device information struct
16440  *
16441  * Called to find if the driver can work with the device now that
16442  * the pci slot has been reset.  If a basic connection seems good
16443  * (registers are readable and have sane content) then return a
16444  * happy little PCI_ERS_RESULT_xxx.
16445  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16446 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16447 {
16448 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16449 	pci_ers_result_t result;
16450 	u32 reg;
16451 
16452 	dev_dbg(&pdev->dev, "%s\n", __func__);
16453 	/* enable I/O and memory of the device  */
16454 	if (pci_enable_device(pdev)) {
16455 		dev_info(&pdev->dev,
16456 			 "Cannot re-enable PCI device after reset.\n");
16457 		result = PCI_ERS_RESULT_DISCONNECT;
16458 	} else {
16459 		pci_set_master(pdev);
16460 		pci_restore_state(pdev);
16461 		pci_wake_from_d3(pdev, false);
16462 
16463 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16464 		if (reg == 0)
16465 			result = PCI_ERS_RESULT_RECOVERED;
16466 		else
16467 			result = PCI_ERS_RESULT_DISCONNECT;
16468 	}
16469 
16470 	return result;
16471 }
16472 
16473 /**
16474  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16475  * @pdev: PCI device information struct
16476  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16477 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16478 {
16479 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16480 
16481 	i40e_prep_for_reset(pf);
16482 }
16483 
16484 /**
16485  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16486  * @pdev: PCI device information struct
16487  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16488 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16489 {
16490 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16491 
16492 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16493 		return;
16494 
16495 	i40e_reset_and_rebuild(pf, false, false);
16496 #ifdef CONFIG_PCI_IOV
16497 	i40e_restore_all_vfs_msi_state(pdev);
16498 #endif /* CONFIG_PCI_IOV */
16499 }
16500 
16501 /**
16502  * i40e_pci_error_resume - restart operations after PCI error recovery
16503  * @pdev: PCI device information struct
16504  *
16505  * Called to allow the driver to bring things back up after PCI error
16506  * and/or reset recovery has finished.
16507  **/
i40e_pci_error_resume(struct pci_dev * pdev)16508 static void i40e_pci_error_resume(struct pci_dev *pdev)
16509 {
16510 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16511 
16512 	dev_dbg(&pdev->dev, "%s\n", __func__);
16513 	if (test_bit(__I40E_SUSPENDED, pf->state))
16514 		return;
16515 
16516 	i40e_io_resume(pf);
16517 }
16518 
16519 /**
16520  * i40e_shutdown - PCI callback for shutting down
16521  * @pdev: PCI device information struct
16522  **/
i40e_shutdown(struct pci_dev * pdev)16523 static void i40e_shutdown(struct pci_dev *pdev)
16524 {
16525 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16526 	struct i40e_hw *hw = &pf->hw;
16527 
16528 	set_bit(__I40E_SUSPENDED, pf->state);
16529 	set_bit(__I40E_DOWN, pf->state);
16530 
16531 	timer_delete_sync(&pf->service_timer);
16532 	cancel_work_sync(&pf->service_task);
16533 	i40e_cloud_filter_exit(pf);
16534 	i40e_fdir_teardown(pf);
16535 
16536 	/* Client close must be called explicitly here because the timer
16537 	 * has been stopped.
16538 	 */
16539 	i40e_notify_client_of_netdev_close(pf, false);
16540 
16541 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16542 	    pf->wol_en)
16543 		i40e_enable_mc_magic_wake(pf);
16544 
16545 	i40e_prep_for_reset(pf);
16546 
16547 	wr32(hw, I40E_PFPM_APM,
16548 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16549 	wr32(hw, I40E_PFPM_WUFC,
16550 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16551 
16552 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16553 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16554 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16555 		free_irq(pf->pdev->irq, pf);
16556 
16557 	/* Since we're going to destroy queues during the
16558 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16559 	 * whole section
16560 	 */
16561 	rtnl_lock();
16562 	i40e_clear_interrupt_scheme(pf);
16563 	rtnl_unlock();
16564 
16565 	if (system_state == SYSTEM_POWER_OFF) {
16566 		pci_wake_from_d3(pdev, pf->wol_en);
16567 		pci_set_power_state(pdev, PCI_D3hot);
16568 	}
16569 }
16570 
16571 /**
16572  * i40e_suspend - PM callback for moving to D3
16573  * @dev: generic device information structure
16574  **/
i40e_suspend(struct device * dev)16575 static int i40e_suspend(struct device *dev)
16576 {
16577 	struct i40e_pf *pf = dev_get_drvdata(dev);
16578 
16579 	/* If we're already suspended, then there is nothing to do */
16580 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16581 		return 0;
16582 	return i40e_io_suspend(pf);
16583 }
16584 
16585 /**
16586  * i40e_resume - PM callback for waking up from D3
16587  * @dev: generic device information structure
16588  **/
i40e_resume(struct device * dev)16589 static int i40e_resume(struct device *dev)
16590 {
16591 	struct i40e_pf *pf = dev_get_drvdata(dev);
16592 
16593 	/* If we're not suspended, then there is nothing to do */
16594 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16595 		return 0;
16596 	return i40e_io_resume(pf);
16597 }
16598 
16599 static const struct pci_error_handlers i40e_err_handler = {
16600 	.error_detected = i40e_pci_error_detected,
16601 	.slot_reset = i40e_pci_error_slot_reset,
16602 	.reset_prepare = i40e_pci_error_reset_prepare,
16603 	.reset_done = i40e_pci_error_reset_done,
16604 	.resume = i40e_pci_error_resume,
16605 };
16606 
16607 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16608 
16609 static struct pci_driver i40e_driver = {
16610 	.name     = i40e_driver_name,
16611 	.id_table = i40e_pci_tbl,
16612 	.probe    = i40e_probe,
16613 	.remove   = i40e_remove,
16614 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16615 	.shutdown = i40e_shutdown,
16616 	.err_handler = &i40e_err_handler,
16617 	.sriov_configure = i40e_pci_sriov_configure,
16618 };
16619 
16620 /**
16621  * i40e_init_module - Driver registration routine
16622  *
16623  * i40e_init_module is the first routine called when the driver is
16624  * loaded. All it does is register with the PCI subsystem.
16625  **/
i40e_init_module(void)16626 static int __init i40e_init_module(void)
16627 {
16628 	int err;
16629 
16630 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16631 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16632 
16633 	/* There is no need to throttle the number of active tasks because
16634 	 * each device limits its own task using a state bit for scheduling
16635 	 * the service task, and the device tasks do not interfere with each
16636 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16637 	 * since we need to be able to guarantee forward progress even under
16638 	 * memory pressure.
16639 	 */
16640 	i40e_wq = alloc_workqueue("%s", WQ_PERCPU, 0, i40e_driver_name);
16641 	if (!i40e_wq) {
16642 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16643 		return -ENOMEM;
16644 	}
16645 
16646 	i40e_dbg_init();
16647 	err = pci_register_driver(&i40e_driver);
16648 	if (err) {
16649 		destroy_workqueue(i40e_wq);
16650 		i40e_dbg_exit();
16651 		return err;
16652 	}
16653 
16654 	return 0;
16655 }
16656 module_init(i40e_init_module);
16657 
16658 /**
16659  * i40e_exit_module - Driver exit cleanup routine
16660  *
16661  * i40e_exit_module is called just before the driver is removed
16662  * from memory.
16663  **/
i40e_exit_module(void)16664 static void __exit i40e_exit_module(void)
16665 {
16666 	pci_unregister_driver(&i40e_driver);
16667 	destroy_workqueue(i40e_wq);
16668 	ida_destroy(&i40e_client_ida);
16669 	i40e_dbg_exit();
16670 }
16671 module_exit(i40e_exit_module);
16672