xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
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 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
91 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
92 	/* required last entry */
93 	{0, }
94 };
95 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
96 
97 #define I40E_MAX_VF_COUNT 128
98 static int debug = -1;
99 module_param(debug, uint, 0);
100 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
101 
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_IMPORT_NS("LIBIE");
104 MODULE_IMPORT_NS("LIBIE_ADMINQ");
105 MODULE_LICENSE("GPL v2");
106 
107 static struct workqueue_struct *i40e_wq;
108 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)109 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
110 				  struct net_device *netdev, int delta)
111 {
112 	struct netdev_hw_addr_list *ha_list;
113 	struct netdev_hw_addr *ha;
114 
115 	if (!f || !netdev)
116 		return;
117 
118 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
119 		ha_list = &netdev->uc;
120 	else
121 		ha_list = &netdev->mc;
122 
123 	netdev_hw_addr_list_for_each(ha, ha_list) {
124 		if (ether_addr_equal(ha->addr, f->macaddr)) {
125 			ha->refcount += delta;
126 			if (ha->refcount <= 0)
127 				ha->refcount = 1;
128 			break;
129 		}
130 	}
131 }
132 
133 /**
134  * i40e_hw_to_dev - get device pointer from the hardware structure
135  * @hw: pointer to the device HW structure
136  **/
i40e_hw_to_dev(struct i40e_hw * hw)137 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
138 {
139 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
140 
141 	return &pf->pdev->dev;
142 }
143 
144 /**
145  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
146  * @hw:   pointer to the HW structure
147  * @mem:  ptr to mem struct to fill out
148  * @size: size of memory requested
149  * @alignment: what to align the allocation to
150  **/
i40e_allocate_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)151 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
152 			  u64 size, u32 alignment)
153 {
154 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
155 
156 	mem->size = ALIGN(size, alignment);
157 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
158 				     GFP_KERNEL);
159 	if (!mem->va)
160 		return -ENOMEM;
161 
162 	return 0;
163 }
164 
165 /**
166  * i40e_free_dma_mem - OS specific memory free for shared code
167  * @hw:   pointer to the HW structure
168  * @mem:  ptr to mem struct to free
169  **/
i40e_free_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem)170 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
171 {
172 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
173 
174 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
175 	mem->va = NULL;
176 	mem->pa = 0;
177 	mem->size = 0;
178 
179 	return 0;
180 }
181 
182 /**
183  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
184  * @hw:   pointer to the HW structure
185  * @mem:  ptr to mem struct to fill out
186  * @size: size of memory requested
187  **/
i40e_allocate_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)188 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
189 			   u32 size)
190 {
191 	mem->size = size;
192 	mem->va = kzalloc(size, GFP_KERNEL);
193 
194 	if (!mem->va)
195 		return -ENOMEM;
196 
197 	return 0;
198 }
199 
200 /**
201  * i40e_free_virt_mem - OS specific memory free for shared code
202  * @hw:   pointer to the HW structure
203  * @mem:  ptr to mem struct to free
204  **/
i40e_free_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem)205 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
206 {
207 	/* it's ok to kfree a NULL pointer */
208 	kfree(mem->va);
209 	mem->va = NULL;
210 	mem->size = 0;
211 
212 	return 0;
213 }
214 
215 /**
216  * i40e_get_lump - find a lump of free generic resource
217  * @pf: board private structure
218  * @pile: the pile of resource to search
219  * @needed: the number of items needed
220  * @id: an owner id to stick on the items assigned
221  *
222  * Returns the base item index of the lump, or negative for error
223  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)224 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
225 			 u16 needed, u16 id)
226 {
227 	int ret = -ENOMEM;
228 	int i, j;
229 
230 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
231 		dev_info(&pf->pdev->dev,
232 			 "param err: pile=%s needed=%d id=0x%04x\n",
233 			 pile ? "<valid>" : "<null>", needed, id);
234 		return -EINVAL;
235 	}
236 
237 	/* Allocate last queue in the pile for FDIR VSI queue
238 	 * so it doesn't fragment the qp_pile
239 	 */
240 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
241 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
242 			dev_err(&pf->pdev->dev,
243 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
244 				pile->num_entries - 1);
245 			return -ENOMEM;
246 		}
247 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
248 		return pile->num_entries - 1;
249 	}
250 
251 	i = 0;
252 	while (i < pile->num_entries) {
253 		/* skip already allocated entries */
254 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
255 			i++;
256 			continue;
257 		}
258 
259 		/* do we have enough in this lump? */
260 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
261 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
262 				break;
263 		}
264 
265 		if (j == needed) {
266 			/* there was enough, so assign it to the requestor */
267 			for (j = 0; j < needed; j++)
268 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
269 			ret = i;
270 			break;
271 		}
272 
273 		/* not enough, so skip over it and continue looking */
274 		i += j;
275 	}
276 
277 	return ret;
278 }
279 
280 /**
281  * i40e_put_lump - return a lump of generic resource
282  * @pile: the pile of resource to search
283  * @index: the base item index
284  * @id: the owner id of the items assigned
285  *
286  * Returns the count of items in the lump
287  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)288 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
289 {
290 	int valid_id = (id | I40E_PILE_VALID_BIT);
291 	int count = 0;
292 	u16 i;
293 
294 	if (!pile || index >= pile->num_entries)
295 		return -EINVAL;
296 
297 	for (i = index;
298 	     i < pile->num_entries && pile->list[i] == valid_id;
299 	     i++) {
300 		pile->list[i] = 0;
301 		count++;
302 	}
303 
304 
305 	return count;
306 }
307 
308 /**
309  * i40e_find_vsi_from_id - searches for the vsi with the given id
310  * @pf: the pf structure to search for the vsi
311  * @id: id of the vsi it is searching for
312  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)313 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
314 {
315 	struct i40e_vsi *vsi;
316 	int i;
317 
318 	i40e_pf_for_each_vsi(pf, i, vsi)
319 		if (vsi->id == id)
320 			return vsi;
321 
322 	return NULL;
323 }
324 
325 /**
326  * i40e_service_event_schedule - Schedule the service task to wake up
327  * @pf: board private structure
328  *
329  * If not already scheduled, this puts the task into the work queue
330  **/
i40e_service_event_schedule(struct i40e_pf * pf)331 void i40e_service_event_schedule(struct i40e_pf *pf)
332 {
333 	if ((!test_bit(__I40E_DOWN, pf->state) &&
334 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
335 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
336 		queue_work(i40e_wq, &pf->service_task);
337 }
338 
339 /**
340  * i40e_tx_timeout - Respond to a Tx Hang
341  * @netdev: network interface device structure
342  * @txqueue: queue number timing out
343  *
344  * If any port has noticed a Tx timeout, it is likely that the whole
345  * device is munged, not just the one netdev port, so go for the full
346  * reset.
347  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)348 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
349 {
350 	struct i40e_netdev_priv *np = netdev_priv(netdev);
351 	struct i40e_vsi *vsi = np->vsi;
352 	struct i40e_pf *pf = vsi->back;
353 	struct i40e_ring *tx_ring = NULL;
354 	unsigned int i;
355 	u32 head, val;
356 
357 	pf->tx_timeout_count++;
358 
359 	/* with txqueue index, find the tx_ring struct */
360 	for (i = 0; i < vsi->num_queue_pairs; i++) {
361 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
362 			if (txqueue ==
363 			    vsi->tx_rings[i]->queue_index) {
364 				tx_ring = vsi->tx_rings[i];
365 				break;
366 			}
367 		}
368 	}
369 
370 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
371 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
372 	else if (time_before(jiffies,
373 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
374 		return;   /* don't do any new action before the next timeout */
375 
376 	/* don't kick off another recovery if one is already pending */
377 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
378 		return;
379 
380 	if (tx_ring) {
381 		head = i40e_get_head(tx_ring);
382 		/* Read interrupt register */
383 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
384 			val = rd32(&pf->hw,
385 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
386 						tx_ring->vsi->base_vector - 1));
387 		else
388 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
389 
390 		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",
391 			    vsi->seid, txqueue, tx_ring->next_to_clean,
392 			    head, tx_ring->next_to_use,
393 			    readl(tx_ring->tail), val);
394 	}
395 
396 	pf->tx_timeout_last_recovery = jiffies;
397 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
398 		    pf->tx_timeout_recovery_level, txqueue);
399 
400 	switch (pf->tx_timeout_recovery_level) {
401 	case 1:
402 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
403 		break;
404 	case 2:
405 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
406 		break;
407 	case 3:
408 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
409 		break;
410 	default:
411 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
412 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
413 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
414 		break;
415 	}
416 
417 	i40e_service_event_schedule(pf);
418 	pf->tx_timeout_recovery_level++;
419 }
420 
421 /**
422  * i40e_get_vsi_stats_struct - Get System Network Statistics
423  * @vsi: the VSI we care about
424  *
425  * Returns the address of the device statistics structure.
426  * The statistics are actually updated from the service task.
427  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)428 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
429 {
430 	return &vsi->net_stats;
431 }
432 
433 /**
434  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
435  * @ring: Tx ring to get statistics from
436  * @stats: statistics entry to be updated
437  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)438 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
439 					    struct rtnl_link_stats64 *stats)
440 {
441 	u64 bytes, packets;
442 	unsigned int start;
443 
444 	do {
445 		start = u64_stats_fetch_begin(&ring->syncp);
446 		packets = ring->stats.packets;
447 		bytes   = ring->stats.bytes;
448 	} while (u64_stats_fetch_retry(&ring->syncp, start));
449 
450 	stats->tx_packets += packets;
451 	stats->tx_bytes   += bytes;
452 }
453 
454 /**
455  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
456  * @netdev: network interface device structure
457  * @stats: data structure to store statistics
458  *
459  * Returns the address of the device statistics structure.
460  * The statistics are actually updated from the service task.
461  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)462 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
463 				  struct rtnl_link_stats64 *stats)
464 {
465 	struct i40e_netdev_priv *np = netdev_priv(netdev);
466 	struct i40e_vsi *vsi = np->vsi;
467 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
468 	struct i40e_ring *ring;
469 	int i;
470 
471 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
472 		return;
473 
474 	if (!vsi->tx_rings)
475 		return;
476 
477 	rcu_read_lock();
478 	for (i = 0; i < vsi->num_queue_pairs; i++) {
479 		u64 bytes, packets;
480 		unsigned int start;
481 
482 		ring = READ_ONCE(vsi->tx_rings[i]);
483 		if (!ring)
484 			continue;
485 		i40e_get_netdev_stats_struct_tx(ring, stats);
486 
487 		if (i40e_enabled_xdp_vsi(vsi)) {
488 			ring = READ_ONCE(vsi->xdp_rings[i]);
489 			if (!ring)
490 				continue;
491 			i40e_get_netdev_stats_struct_tx(ring, stats);
492 		}
493 
494 		ring = READ_ONCE(vsi->rx_rings[i]);
495 		if (!ring)
496 			continue;
497 		do {
498 			start   = u64_stats_fetch_begin(&ring->syncp);
499 			packets = ring->stats.packets;
500 			bytes   = ring->stats.bytes;
501 		} while (u64_stats_fetch_retry(&ring->syncp, start));
502 
503 		stats->rx_packets += packets;
504 		stats->rx_bytes   += bytes;
505 
506 	}
507 	rcu_read_unlock();
508 
509 	/* following stats updated by i40e_watchdog_subtask() */
510 	stats->multicast	= vsi_stats->multicast;
511 	stats->tx_errors	= vsi_stats->tx_errors;
512 	stats->tx_dropped	= vsi_stats->tx_dropped;
513 	stats->rx_errors	= vsi_stats->rx_errors;
514 	stats->rx_dropped	= vsi_stats->rx_dropped;
515 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
516 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
517 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
518 }
519 
520 /**
521  * i40e_vsi_reset_stats - Resets all stats of the given vsi
522  * @vsi: the VSI to have its stats reset
523  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)524 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
525 {
526 	struct rtnl_link_stats64 *ns;
527 	int i;
528 
529 	if (!vsi)
530 		return;
531 
532 	ns = i40e_get_vsi_stats_struct(vsi);
533 	memset(ns, 0, sizeof(*ns));
534 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
535 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
536 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
537 	if (vsi->rx_rings && vsi->rx_rings[0]) {
538 		for (i = 0; i < vsi->num_queue_pairs; i++) {
539 			memset(&vsi->rx_rings[i]->stats, 0,
540 			       sizeof(vsi->rx_rings[i]->stats));
541 			memset(&vsi->rx_rings[i]->rx_stats, 0,
542 			       sizeof(vsi->rx_rings[i]->rx_stats));
543 			memset(&vsi->tx_rings[i]->stats, 0,
544 			       sizeof(vsi->tx_rings[i]->stats));
545 			memset(&vsi->tx_rings[i]->tx_stats, 0,
546 			       sizeof(vsi->tx_rings[i]->tx_stats));
547 		}
548 	}
549 	vsi->stat_offsets_loaded = false;
550 }
551 
552 /**
553  * i40e_pf_reset_stats - Reset all of the stats for the given PF
554  * @pf: the PF to be reset
555  **/
i40e_pf_reset_stats(struct i40e_pf * pf)556 void i40e_pf_reset_stats(struct i40e_pf *pf)
557 {
558 	struct i40e_veb *veb;
559 	int i;
560 
561 	memset(&pf->stats, 0, sizeof(pf->stats));
562 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
563 	pf->stat_offsets_loaded = false;
564 
565 	i40e_pf_for_each_veb(pf, i, veb) {
566 		memset(&veb->stats, 0, sizeof(veb->stats));
567 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
568 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
569 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
570 		veb->stat_offsets_loaded = false;
571 	}
572 	pf->hw_csum_rx_error = 0;
573 }
574 
575 /**
576  * i40e_compute_pci_to_hw_id - compute index form PCI function.
577  * @vsi: ptr to the VSI to read from.
578  * @hw: ptr to the hardware info.
579  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)580 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
581 {
582 	int pf_count = i40e_get_pf_count(hw);
583 
584 	if (vsi->type == I40E_VSI_SRIOV)
585 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
586 
587 	return hw->port + BIT(7);
588 }
589 
590 /**
591  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
592  * @hw: ptr to the hardware info.
593  * @hireg: the high 32 bit reg to read.
594  * @loreg: the low 32 bit reg to read.
595  * @offset_loaded: has the initial offset been loaded yet.
596  * @offset: ptr to current offset value.
597  * @stat: ptr to the stat.
598  *
599  * Since the device stats are not reset at PFReset, they will not
600  * be zeroed when the driver starts.  We'll save the first values read
601  * and use them as offsets to be subtracted from the raw values in order
602  * to report stats that count from zero.
603  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)604 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
605 			       bool offset_loaded, u64 *offset, u64 *stat)
606 {
607 	u64 new_data;
608 
609 	new_data = rd64(hw, loreg);
610 
611 	if (!offset_loaded || new_data < *offset)
612 		*offset = new_data;
613 	*stat = new_data - *offset;
614 }
615 
616 /**
617  * i40e_stat_update48 - read and update a 48 bit stat from the chip
618  * @hw: ptr to the hardware info
619  * @hireg: the high 32 bit reg to read
620  * @loreg: the low 32 bit reg to read
621  * @offset_loaded: has the initial offset been loaded yet
622  * @offset: ptr to current offset value
623  * @stat: ptr to the stat
624  *
625  * Since the device stats are not reset at PFReset, they likely will not
626  * be zeroed when the driver starts.  We'll save the first values read
627  * and use them as offsets to be subtracted from the raw values in order
628  * to report stats that count from zero.  In the process, we also manage
629  * the potential roll-over.
630  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)631 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
632 			       bool offset_loaded, u64 *offset, u64 *stat)
633 {
634 	u64 new_data;
635 
636 	if (hw->device_id == I40E_DEV_ID_QEMU) {
637 		new_data = rd32(hw, loreg);
638 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
639 	} else {
640 		new_data = rd64(hw, loreg);
641 	}
642 	if (!offset_loaded)
643 		*offset = new_data;
644 	if (likely(new_data >= *offset))
645 		*stat = new_data - *offset;
646 	else
647 		*stat = (new_data + BIT_ULL(48)) - *offset;
648 	*stat &= 0xFFFFFFFFFFFFULL;
649 }
650 
651 /**
652  * i40e_stat_update32 - read and update a 32 bit stat from the chip
653  * @hw: ptr to the hardware info
654  * @reg: the hw reg to read
655  * @offset_loaded: has the initial offset been loaded yet
656  * @offset: ptr to current offset value
657  * @stat: ptr to the stat
658  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)659 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
660 			       bool offset_loaded, u64 *offset, u64 *stat)
661 {
662 	u32 new_data;
663 
664 	new_data = rd32(hw, reg);
665 	if (!offset_loaded)
666 		*offset = new_data;
667 	if (likely(new_data >= *offset))
668 		*stat = (u32)(new_data - *offset);
669 	else
670 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
671 }
672 
673 /**
674  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
675  * @hw: ptr to the hardware info
676  * @reg: the hw reg to read and clear
677  * @stat: ptr to the stat
678  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)679 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
680 {
681 	u32 new_data = rd32(hw, reg);
682 
683 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
684 	*stat += new_data;
685 }
686 
687 /**
688  * i40e_stats_update_rx_discards - update rx_discards.
689  * @vsi: ptr to the VSI to be updated.
690  * @hw: ptr to the hardware info.
691  * @stat_idx: VSI's stat_counter_idx.
692  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
693  * @stat_offset: ptr to stat_offset to store first read of specific register.
694  * @stat: ptr to VSI's stat to be updated.
695  **/
696 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)697 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
698 			      int stat_idx, bool offset_loaded,
699 			      struct i40e_eth_stats *stat_offset,
700 			      struct i40e_eth_stats *stat)
701 {
702 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
703 			   &stat_offset->rx_discards, &stat->rx_discards);
704 	i40e_stat_update64(hw,
705 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
706 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
707 			   offset_loaded, &stat_offset->rx_discards_other,
708 			   &stat->rx_discards_other);
709 }
710 
711 /**
712  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
713  * @vsi: the VSI to be updated
714  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)715 void i40e_update_eth_stats(struct i40e_vsi *vsi)
716 {
717 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
718 	struct i40e_pf *pf = vsi->back;
719 	struct i40e_hw *hw = &pf->hw;
720 	struct i40e_eth_stats *oes;
721 	struct i40e_eth_stats *es;     /* device's eth stats */
722 
723 	es = &vsi->eth_stats;
724 	oes = &vsi->eth_stats_offsets;
725 
726 	/* Gather up the stats that the hw collects */
727 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
728 			   vsi->stat_offsets_loaded,
729 			   &oes->tx_errors, &es->tx_errors);
730 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
731 			   vsi->stat_offsets_loaded,
732 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
733 
734 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
735 			   I40E_GLV_GORCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_bytes, &es->rx_bytes);
738 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
739 			   I40E_GLV_UPRCL(stat_idx),
740 			   vsi->stat_offsets_loaded,
741 			   &oes->rx_unicast, &es->rx_unicast);
742 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
743 			   I40E_GLV_MPRCL(stat_idx),
744 			   vsi->stat_offsets_loaded,
745 			   &oes->rx_multicast, &es->rx_multicast);
746 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
747 			   I40E_GLV_BPRCL(stat_idx),
748 			   vsi->stat_offsets_loaded,
749 			   &oes->rx_broadcast, &es->rx_broadcast);
750 
751 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
752 			   I40E_GLV_GOTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_bytes, &es->tx_bytes);
755 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
756 			   I40E_GLV_UPTCL(stat_idx),
757 			   vsi->stat_offsets_loaded,
758 			   &oes->tx_unicast, &es->tx_unicast);
759 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
760 			   I40E_GLV_MPTCL(stat_idx),
761 			   vsi->stat_offsets_loaded,
762 			   &oes->tx_multicast, &es->tx_multicast);
763 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
764 			   I40E_GLV_BPTCL(stat_idx),
765 			   vsi->stat_offsets_loaded,
766 			   &oes->tx_broadcast, &es->tx_broadcast);
767 
768 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
769 				      vsi->stat_offsets_loaded, oes, es);
770 
771 	vsi->stat_offsets_loaded = true;
772 }
773 
774 /**
775  * i40e_update_veb_stats - Update Switch component statistics
776  * @veb: the VEB being updated
777  **/
i40e_update_veb_stats(struct i40e_veb * veb)778 void i40e_update_veb_stats(struct i40e_veb *veb)
779 {
780 	struct i40e_pf *pf = veb->pf;
781 	struct i40e_hw *hw = &pf->hw;
782 	struct i40e_eth_stats *oes;
783 	struct i40e_eth_stats *es;     /* device's eth stats */
784 	struct i40e_veb_tc_stats *veb_oes;
785 	struct i40e_veb_tc_stats *veb_es;
786 	int i, idx = 0;
787 
788 	idx = veb->stats_idx;
789 	es = &veb->stats;
790 	oes = &veb->stats_offsets;
791 	veb_es = &veb->tc_stats;
792 	veb_oes = &veb->tc_stats_offsets;
793 
794 	/* Gather up the stats that the hw collects */
795 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
796 			   veb->stat_offsets_loaded,
797 			   &oes->tx_discards, &es->tx_discards);
798 	if (hw->revision_id > 0)
799 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
800 				   veb->stat_offsets_loaded,
801 				   &oes->rx_unknown_protocol,
802 				   &es->rx_unknown_protocol);
803 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
804 			   veb->stat_offsets_loaded,
805 			   &oes->rx_bytes, &es->rx_bytes);
806 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
807 			   veb->stat_offsets_loaded,
808 			   &oes->rx_unicast, &es->rx_unicast);
809 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
810 			   veb->stat_offsets_loaded,
811 			   &oes->rx_multicast, &es->rx_multicast);
812 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
813 			   veb->stat_offsets_loaded,
814 			   &oes->rx_broadcast, &es->rx_broadcast);
815 
816 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
817 			   veb->stat_offsets_loaded,
818 			   &oes->tx_bytes, &es->tx_bytes);
819 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
820 			   veb->stat_offsets_loaded,
821 			   &oes->tx_unicast, &es->tx_unicast);
822 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
823 			   veb->stat_offsets_loaded,
824 			   &oes->tx_multicast, &es->tx_multicast);
825 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
826 			   veb->stat_offsets_loaded,
827 			   &oes->tx_broadcast, &es->tx_broadcast);
828 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
829 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
830 				   I40E_GLVEBTC_RPCL(i, idx),
831 				   veb->stat_offsets_loaded,
832 				   &veb_oes->tc_rx_packets[i],
833 				   &veb_es->tc_rx_packets[i]);
834 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
835 				   I40E_GLVEBTC_RBCL(i, idx),
836 				   veb->stat_offsets_loaded,
837 				   &veb_oes->tc_rx_bytes[i],
838 				   &veb_es->tc_rx_bytes[i]);
839 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
840 				   I40E_GLVEBTC_TPCL(i, idx),
841 				   veb->stat_offsets_loaded,
842 				   &veb_oes->tc_tx_packets[i],
843 				   &veb_es->tc_tx_packets[i]);
844 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
845 				   I40E_GLVEBTC_TBCL(i, idx),
846 				   veb->stat_offsets_loaded,
847 				   &veb_oes->tc_tx_bytes[i],
848 				   &veb_es->tc_tx_bytes[i]);
849 	}
850 	veb->stat_offsets_loaded = true;
851 }
852 
853 /**
854  * i40e_update_vsi_stats - Update the vsi statistics counters.
855  * @vsi: the VSI to be updated
856  *
857  * There are a few instances where we store the same stat in a
858  * couple of different structs.  This is partly because we have
859  * the netdev stats that need to be filled out, which is slightly
860  * different from the "eth_stats" defined by the chip and used in
861  * VF communications.  We sort it out here.
862  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)863 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
864 {
865 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
866 	struct i40e_pf *pf = vsi->back;
867 	struct rtnl_link_stats64 *ons;
868 	struct rtnl_link_stats64 *ns;   /* netdev stats */
869 	struct i40e_eth_stats *oes;
870 	struct i40e_eth_stats *es;     /* device's eth stats */
871 	u64 tx_restart, tx_busy;
872 	struct i40e_ring *p;
873 	u64 bytes, packets;
874 	unsigned int start;
875 	u64 tx_linearize;
876 	u64 tx_force_wb;
877 	u64 tx_stopped;
878 	u64 rx_p, rx_b;
879 	u64 tx_p, tx_b;
880 	u16 q;
881 
882 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
883 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
884 		return;
885 
886 	ns = i40e_get_vsi_stats_struct(vsi);
887 	ons = &vsi->net_stats_offsets;
888 	es = &vsi->eth_stats;
889 	oes = &vsi->eth_stats_offsets;
890 
891 	/* Gather up the netdev and vsi stats that the driver collects
892 	 * on the fly during packet processing
893 	 */
894 	rx_b = rx_p = 0;
895 	tx_b = tx_p = 0;
896 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
897 	tx_stopped = 0;
898 	rx_page = 0;
899 	rx_buf = 0;
900 	rx_reuse = 0;
901 	rx_alloc = 0;
902 	rx_waive = 0;
903 	rx_busy = 0;
904 	rcu_read_lock();
905 	for (q = 0; q < vsi->num_queue_pairs; q++) {
906 		/* locate Tx ring */
907 		p = READ_ONCE(vsi->tx_rings[q]);
908 		if (!p)
909 			continue;
910 
911 		do {
912 			start = u64_stats_fetch_begin(&p->syncp);
913 			packets = p->stats.packets;
914 			bytes = p->stats.bytes;
915 		} while (u64_stats_fetch_retry(&p->syncp, start));
916 		tx_b += bytes;
917 		tx_p += packets;
918 		tx_restart += p->tx_stats.restart_queue;
919 		tx_busy += p->tx_stats.tx_busy;
920 		tx_linearize += p->tx_stats.tx_linearize;
921 		tx_force_wb += p->tx_stats.tx_force_wb;
922 		tx_stopped += p->tx_stats.tx_stopped;
923 
924 		/* locate Rx ring */
925 		p = READ_ONCE(vsi->rx_rings[q]);
926 		if (!p)
927 			continue;
928 
929 		do {
930 			start = u64_stats_fetch_begin(&p->syncp);
931 			packets = p->stats.packets;
932 			bytes = p->stats.bytes;
933 		} while (u64_stats_fetch_retry(&p->syncp, start));
934 		rx_b += bytes;
935 		rx_p += packets;
936 		rx_buf += p->rx_stats.alloc_buff_failed;
937 		rx_page += p->rx_stats.alloc_page_failed;
938 		rx_reuse += p->rx_stats.page_reuse_count;
939 		rx_alloc += p->rx_stats.page_alloc_count;
940 		rx_waive += p->rx_stats.page_waive_count;
941 		rx_busy += p->rx_stats.page_busy_count;
942 
943 		if (i40e_enabled_xdp_vsi(vsi)) {
944 			/* locate XDP ring */
945 			p = READ_ONCE(vsi->xdp_rings[q]);
946 			if (!p)
947 				continue;
948 
949 			do {
950 				start = u64_stats_fetch_begin(&p->syncp);
951 				packets = p->stats.packets;
952 				bytes = p->stats.bytes;
953 			} while (u64_stats_fetch_retry(&p->syncp, start));
954 			tx_b += bytes;
955 			tx_p += packets;
956 			tx_restart += p->tx_stats.restart_queue;
957 			tx_busy += p->tx_stats.tx_busy;
958 			tx_linearize += p->tx_stats.tx_linearize;
959 			tx_force_wb += p->tx_stats.tx_force_wb;
960 		}
961 	}
962 	rcu_read_unlock();
963 	vsi->tx_restart = tx_restart;
964 	vsi->tx_busy = tx_busy;
965 	vsi->tx_linearize = tx_linearize;
966 	vsi->tx_force_wb = tx_force_wb;
967 	vsi->tx_stopped = tx_stopped;
968 	vsi->rx_page_failed = rx_page;
969 	vsi->rx_buf_failed = rx_buf;
970 	vsi->rx_page_reuse = rx_reuse;
971 	vsi->rx_page_alloc = rx_alloc;
972 	vsi->rx_page_waive = rx_waive;
973 	vsi->rx_page_busy = rx_busy;
974 
975 	ns->rx_packets = rx_p;
976 	ns->rx_bytes = rx_b;
977 	ns->tx_packets = tx_p;
978 	ns->tx_bytes = tx_b;
979 
980 	/* update netdev stats from eth stats */
981 	i40e_update_eth_stats(vsi);
982 	ons->tx_errors = oes->tx_errors;
983 	ns->tx_errors = es->tx_errors;
984 	ons->multicast = oes->rx_multicast;
985 	ns->multicast = es->rx_multicast;
986 	ons->rx_dropped = oes->rx_discards_other;
987 	ns->rx_dropped = es->rx_discards_other;
988 	ons->rx_missed_errors = oes->rx_discards;
989 	ns->rx_missed_errors = es->rx_discards;
990 	ons->tx_dropped = oes->tx_discards;
991 	ns->tx_dropped = es->tx_discards;
992 
993 	/* pull in a couple PF stats if this is the main vsi */
994 	if (vsi->type == I40E_VSI_MAIN) {
995 		ns->rx_crc_errors = pf->stats.crc_errors;
996 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
997 		ns->rx_length_errors = pf->stats.rx_length_errors;
998 	}
999 }
1000 
1001 /**
1002  * i40e_update_pf_stats - Update the PF statistics counters.
1003  * @pf: the PF to be updated
1004  **/
i40e_update_pf_stats(struct i40e_pf * pf)1005 static void i40e_update_pf_stats(struct i40e_pf *pf)
1006 {
1007 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1008 	struct i40e_hw_port_stats *nsd = &pf->stats;
1009 	struct i40e_hw *hw = &pf->hw;
1010 	u32 val;
1011 	int i;
1012 
1013 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1014 			   I40E_GLPRT_GORCL(hw->port),
1015 			   pf->stat_offsets_loaded,
1016 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1017 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1018 			   I40E_GLPRT_GOTCL(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1021 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1022 			   pf->stat_offsets_loaded,
1023 			   &osd->eth.rx_discards,
1024 			   &nsd->eth.rx_discards);
1025 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1026 			   I40E_GLPRT_UPRCL(hw->port),
1027 			   pf->stat_offsets_loaded,
1028 			   &osd->eth.rx_unicast,
1029 			   &nsd->eth.rx_unicast);
1030 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1031 			   I40E_GLPRT_MPRCL(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->eth.rx_multicast,
1034 			   &nsd->eth.rx_multicast);
1035 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1036 			   I40E_GLPRT_BPRCL(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->eth.rx_broadcast,
1039 			   &nsd->eth.rx_broadcast);
1040 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1041 			   I40E_GLPRT_UPTCL(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->eth.tx_unicast,
1044 			   &nsd->eth.tx_unicast);
1045 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1046 			   I40E_GLPRT_MPTCL(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->eth.tx_multicast,
1049 			   &nsd->eth.tx_multicast);
1050 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1051 			   I40E_GLPRT_BPTCL(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->eth.tx_broadcast,
1054 			   &nsd->eth.tx_broadcast);
1055 
1056 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->tx_dropped_link_down,
1059 			   &nsd->tx_dropped_link_down);
1060 
1061 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->crc_errors, &nsd->crc_errors);
1064 
1065 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1066 			   pf->stat_offsets_loaded,
1067 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1068 
1069 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->mac_local_faults,
1072 			   &nsd->mac_local_faults);
1073 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1074 			   pf->stat_offsets_loaded,
1075 			   &osd->mac_remote_faults,
1076 			   &nsd->mac_remote_faults);
1077 
1078 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->rx_length_errors,
1081 			   &nsd->rx_length_errors);
1082 
1083 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1084 			   pf->stat_offsets_loaded,
1085 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1086 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1087 			   pf->stat_offsets_loaded,
1088 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1089 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1090 			   pf->stat_offsets_loaded,
1091 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1092 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1095 
1096 	for (i = 0; i < 8; i++) {
1097 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1098 				   pf->stat_offsets_loaded,
1099 				   &osd->priority_xoff_rx[i],
1100 				   &nsd->priority_xoff_rx[i]);
1101 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1102 				   pf->stat_offsets_loaded,
1103 				   &osd->priority_xon_rx[i],
1104 				   &nsd->priority_xon_rx[i]);
1105 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1106 				   pf->stat_offsets_loaded,
1107 				   &osd->priority_xon_tx[i],
1108 				   &nsd->priority_xon_tx[i]);
1109 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1110 				   pf->stat_offsets_loaded,
1111 				   &osd->priority_xoff_tx[i],
1112 				   &nsd->priority_xoff_tx[i]);
1113 		i40e_stat_update32(hw,
1114 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1115 				   pf->stat_offsets_loaded,
1116 				   &osd->priority_xon_2_xoff[i],
1117 				   &nsd->priority_xon_2_xoff[i]);
1118 	}
1119 
1120 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1121 			   I40E_GLPRT_PRC64L(hw->port),
1122 			   pf->stat_offsets_loaded,
1123 			   &osd->rx_size_64, &nsd->rx_size_64);
1124 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1125 			   I40E_GLPRT_PRC127L(hw->port),
1126 			   pf->stat_offsets_loaded,
1127 			   &osd->rx_size_127, &nsd->rx_size_127);
1128 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1129 			   I40E_GLPRT_PRC255L(hw->port),
1130 			   pf->stat_offsets_loaded,
1131 			   &osd->rx_size_255, &nsd->rx_size_255);
1132 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1133 			   I40E_GLPRT_PRC511L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->rx_size_511, &nsd->rx_size_511);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1137 			   I40E_GLPRT_PRC1023L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1140 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1141 			   I40E_GLPRT_PRC1522L(hw->port),
1142 			   pf->stat_offsets_loaded,
1143 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1144 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1145 			   I40E_GLPRT_PRC9522L(hw->port),
1146 			   pf->stat_offsets_loaded,
1147 			   &osd->rx_size_big, &nsd->rx_size_big);
1148 
1149 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1150 			   I40E_GLPRT_PTC64L(hw->port),
1151 			   pf->stat_offsets_loaded,
1152 			   &osd->tx_size_64, &nsd->tx_size_64);
1153 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1154 			   I40E_GLPRT_PTC127L(hw->port),
1155 			   pf->stat_offsets_loaded,
1156 			   &osd->tx_size_127, &nsd->tx_size_127);
1157 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1158 			   I40E_GLPRT_PTC255L(hw->port),
1159 			   pf->stat_offsets_loaded,
1160 			   &osd->tx_size_255, &nsd->tx_size_255);
1161 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1162 			   I40E_GLPRT_PTC511L(hw->port),
1163 			   pf->stat_offsets_loaded,
1164 			   &osd->tx_size_511, &nsd->tx_size_511);
1165 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1166 			   I40E_GLPRT_PTC1023L(hw->port),
1167 			   pf->stat_offsets_loaded,
1168 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1169 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1170 			   I40E_GLPRT_PTC1522L(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1173 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1174 			   I40E_GLPRT_PTC9522L(hw->port),
1175 			   pf->stat_offsets_loaded,
1176 			   &osd->tx_size_big, &nsd->tx_size_big);
1177 
1178 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1179 			   pf->stat_offsets_loaded,
1180 			   &osd->rx_undersize, &nsd->rx_undersize);
1181 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1182 			   pf->stat_offsets_loaded,
1183 			   &osd->rx_fragments, &nsd->rx_fragments);
1184 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1185 			   pf->stat_offsets_loaded,
1186 			   &osd->rx_oversize, &nsd->rx_oversize);
1187 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1188 			   pf->stat_offsets_loaded,
1189 			   &osd->rx_jabber, &nsd->rx_jabber);
1190 
1191 	/* FDIR stats */
1192 	i40e_stat_update_and_clear32(hw,
1193 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1194 			&nsd->fd_atr_match);
1195 	i40e_stat_update_and_clear32(hw,
1196 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1197 			&nsd->fd_sb_match);
1198 	i40e_stat_update_and_clear32(hw,
1199 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1200 			&nsd->fd_atr_tunnel_match);
1201 
1202 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1203 	nsd->tx_lpi_status =
1204 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1205 	nsd->rx_lpi_status =
1206 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1207 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1208 			   pf->stat_offsets_loaded,
1209 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1210 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1211 			   pf->stat_offsets_loaded,
1212 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1213 
1214 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1215 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1216 		nsd->fd_sb_status = true;
1217 	else
1218 		nsd->fd_sb_status = false;
1219 
1220 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1221 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1222 		nsd->fd_atr_status = true;
1223 	else
1224 		nsd->fd_atr_status = false;
1225 
1226 	pf->stat_offsets_loaded = true;
1227 }
1228 
1229 /**
1230  * i40e_update_stats - Update the various statistics counters.
1231  * @vsi: the VSI to be updated
1232  *
1233  * Update the various stats for this VSI and its related entities.
1234  **/
i40e_update_stats(struct i40e_vsi * vsi)1235 void i40e_update_stats(struct i40e_vsi *vsi)
1236 {
1237 	struct i40e_pf *pf = vsi->back;
1238 
1239 	if (vsi->type == I40E_VSI_MAIN)
1240 		i40e_update_pf_stats(pf);
1241 
1242 	i40e_update_vsi_stats(vsi);
1243 }
1244 
1245 /**
1246  * i40e_count_filters - counts VSI mac filters
1247  * @vsi: the VSI to be searched
1248  *
1249  * Returns count of mac filters
1250  **/
i40e_count_filters(struct i40e_vsi * vsi)1251 int i40e_count_filters(struct i40e_vsi *vsi)
1252 {
1253 	struct i40e_mac_filter *f;
1254 	struct hlist_node *h;
1255 	int bkt;
1256 	int cnt = 0;
1257 
1258 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1259 		if (f->state == I40E_FILTER_NEW ||
1260 		    f->state == I40E_FILTER_NEW_SYNC ||
1261 		    f->state == I40E_FILTER_ACTIVE)
1262 			++cnt;
1263 	}
1264 
1265 	return cnt;
1266 }
1267 
1268 /**
1269  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1270  * @vsi: the VSI to be searched
1271  * @macaddr: the MAC address
1272  * @vlan: the vlan
1273  *
1274  * Returns ptr to the filter object or NULL
1275  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1276 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1277 						const u8 *macaddr, s16 vlan)
1278 {
1279 	struct i40e_mac_filter *f;
1280 	u64 key;
1281 
1282 	if (!vsi || !macaddr)
1283 		return NULL;
1284 
1285 	key = i40e_addr_to_hkey(macaddr);
1286 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1287 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1288 		    (vlan == f->vlan))
1289 			return f;
1290 	}
1291 	return NULL;
1292 }
1293 
1294 /**
1295  * i40e_find_mac - Find a mac addr in the macvlan filters list
1296  * @vsi: the VSI to be searched
1297  * @macaddr: the MAC address we are searching for
1298  *
1299  * Returns the first filter with the provided MAC address or NULL if
1300  * MAC address was not found
1301  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1302 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1303 {
1304 	struct i40e_mac_filter *f;
1305 	u64 key;
1306 
1307 	if (!vsi || !macaddr)
1308 		return NULL;
1309 
1310 	key = i40e_addr_to_hkey(macaddr);
1311 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1312 		if ((ether_addr_equal(macaddr, f->macaddr)))
1313 			return f;
1314 	}
1315 	return NULL;
1316 }
1317 
1318 /**
1319  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1320  * @vsi: the VSI to be searched
1321  *
1322  * Returns true if VSI is in vlan mode or false otherwise
1323  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1324 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1325 {
1326 	/* If we have a PVID, always operate in VLAN mode */
1327 	if (vsi->info.pvid)
1328 		return true;
1329 
1330 	/* We need to operate in VLAN mode whenever we have any filters with
1331 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1332 	 * time, incurring search cost repeatedly. However, we can notice two
1333 	 * things:
1334 	 *
1335 	 * 1) the only place where we can gain a VLAN filter is in
1336 	 *    i40e_add_filter.
1337 	 *
1338 	 * 2) the only place where filters are actually removed is in
1339 	 *    i40e_sync_filters_subtask.
1340 	 *
1341 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1342 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1343 	 * we have to perform the full search after deleting filters in
1344 	 * i40e_sync_filters_subtask, but we already have to search
1345 	 * filters here and can perform the check at the same time. This
1346 	 * results in avoiding embedding a loop for VLAN mode inside another
1347 	 * loop over all the filters, and should maintain correctness as noted
1348 	 * above.
1349 	 */
1350 	return vsi->has_vlan_filter;
1351 }
1352 
1353 /**
1354  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1355  * @vsi: the VSI to configure
1356  * @tmp_add_list: list of filters ready to be added
1357  * @tmp_del_list: list of filters ready to be deleted
1358  * @vlan_filters: the number of active VLAN filters
1359  *
1360  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1361  * behave as expected. If we have any active VLAN filters remaining or about
1362  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1363  * so that they only match against untagged traffic. If we no longer have any
1364  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1365  * so that they match against both tagged and untagged traffic. In this way,
1366  * we ensure that we correctly receive the desired traffic. This ensures that
1367  * when we have an active VLAN we will receive only untagged traffic and
1368  * traffic matching active VLANs. If we have no active VLANs then we will
1369  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1370  *
1371  * Finally, in a similar fashion, this function also corrects filters when
1372  * there is an active PVID assigned to this VSI.
1373  *
1374  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1375  *
1376  * This function is only expected to be called from within
1377  * i40e_sync_vsi_filters.
1378  *
1379  * NOTE: This function expects to be called while under the
1380  * mac_filter_hash_lock
1381  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1382 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1383 					 struct hlist_head *tmp_add_list,
1384 					 struct hlist_head *tmp_del_list,
1385 					 int vlan_filters)
1386 {
1387 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1388 	struct i40e_mac_filter *f, *add_head;
1389 	struct i40e_new_mac_filter *new;
1390 	struct hlist_node *h;
1391 	int bkt, new_vlan;
1392 
1393 	/* To determine if a particular filter needs to be replaced we
1394 	 * have the three following conditions:
1395 	 *
1396 	 * a) if we have a PVID assigned, then all filters which are
1397 	 *    not marked as VLAN=PVID must be replaced with filters that
1398 	 *    are.
1399 	 * b) otherwise, if we have any active VLANS, all filters
1400 	 *    which are marked as VLAN=-1 must be replaced with
1401 	 *    filters marked as VLAN=0
1402 	 * c) finally, if we do not have any active VLANS, all filters
1403 	 *    which are marked as VLAN=0 must be replaced with filters
1404 	 *    marked as VLAN=-1
1405 	 */
1406 
1407 	/* Update the filters about to be added in place */
1408 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1409 		if (pvid && new->f->vlan != pvid)
1410 			new->f->vlan = pvid;
1411 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1412 			new->f->vlan = 0;
1413 		else if (!vlan_filters && new->f->vlan == 0)
1414 			new->f->vlan = I40E_VLAN_ANY;
1415 	}
1416 
1417 	/* Update the remaining active filters */
1418 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1419 		/* Combine the checks for whether a filter needs to be changed
1420 		 * and then determine the new VLAN inside the if block, in
1421 		 * order to avoid duplicating code for adding the new filter
1422 		 * then deleting the old filter.
1423 		 */
1424 		if ((pvid && f->vlan != pvid) ||
1425 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1426 		    (!vlan_filters && f->vlan == 0)) {
1427 			/* Determine the new vlan we will be adding */
1428 			if (pvid)
1429 				new_vlan = pvid;
1430 			else if (vlan_filters)
1431 				new_vlan = 0;
1432 			else
1433 				new_vlan = I40E_VLAN_ANY;
1434 
1435 			/* Create the new filter */
1436 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1437 			if (!add_head)
1438 				return -ENOMEM;
1439 
1440 			/* Create a temporary i40e_new_mac_filter */
1441 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1442 			if (!new)
1443 				return -ENOMEM;
1444 
1445 			new->f = add_head;
1446 			new->state = add_head->state;
1447 			if (add_head->state == I40E_FILTER_NEW)
1448 				add_head->state = I40E_FILTER_NEW_SYNC;
1449 
1450 			/* Add the new filter to the tmp list */
1451 			hlist_add_head(&new->hlist, tmp_add_list);
1452 
1453 			/* Put the original filter into the delete list */
1454 			f->state = I40E_FILTER_REMOVE;
1455 			hash_del(&f->hlist);
1456 			hlist_add_head(&f->hlist, tmp_del_list);
1457 		}
1458 	}
1459 
1460 	vsi->has_vlan_filter = !!vlan_filters;
1461 
1462 	return 0;
1463 }
1464 
1465 /**
1466  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1467  * @vsi: the vsi to configure
1468  * @new_mac: new mac filter to be added
1469  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1470  * @vlan_filters: the number of active VLAN filters
1471  * @trusted: flag if the VF is trusted
1472  *
1473  * Get new VLAN id based on current VLAN filters, trust, PVID
1474  * and vf-vlan-prune-disable flag.
1475  *
1476  * Returns the value of the new vlan filter or
1477  * the old value if no new filter is needed.
1478  */
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)1479 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1480 				struct i40e_new_mac_filter *new_mac,
1481 				struct i40e_mac_filter *f,
1482 				int vlan_filters,
1483 				bool trusted)
1484 {
1485 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1486 	struct i40e_pf *pf = vsi->back;
1487 	bool is_any;
1488 
1489 	if (new_mac)
1490 		f = new_mac->f;
1491 
1492 	if (pvid && f->vlan != pvid)
1493 		return pvid;
1494 
1495 	is_any = (trusted ||
1496 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1497 
1498 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1499 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1500 	    (is_any && !vlan_filters && f->vlan == 0)) {
1501 		if (is_any)
1502 			return I40E_VLAN_ANY;
1503 		else
1504 			return 0;
1505 	}
1506 
1507 	return f->vlan;
1508 }
1509 
1510 /**
1511  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1512  * @vsi: the vsi to configure
1513  * @tmp_add_list: list of filters ready to be added
1514  * @tmp_del_list: list of filters ready to be deleted
1515  * @vlan_filters: the number of active VLAN filters
1516  * @trusted: flag if the VF is trusted
1517  *
1518  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1519  * and vf-vlan-prune-disable flag.
1520  *
1521  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1522  *
1523  * This function is only expected to be called from within
1524  * i40e_sync_vsi_filters.
1525  *
1526  * NOTE: This function expects to be called while under the
1527  * mac_filter_hash_lock
1528  */
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)1529 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1530 					    struct hlist_head *tmp_add_list,
1531 					    struct hlist_head *tmp_del_list,
1532 					    int vlan_filters,
1533 					    bool trusted)
1534 {
1535 	struct i40e_mac_filter *f, *add_head;
1536 	struct i40e_new_mac_filter *new_mac;
1537 	struct hlist_node *h;
1538 	int bkt, new_vlan;
1539 
1540 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1541 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1542 							vlan_filters, trusted);
1543 	}
1544 
1545 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1546 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1547 						trusted);
1548 		if (new_vlan != f->vlan) {
1549 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1550 			if (!add_head)
1551 				return -ENOMEM;
1552 			/* Create a temporary i40e_new_mac_filter */
1553 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1554 			if (!new_mac)
1555 				return -ENOMEM;
1556 			new_mac->f = add_head;
1557 			new_mac->state = add_head->state;
1558 			if (add_head->state == I40E_FILTER_NEW)
1559 				add_head->state = I40E_FILTER_NEW_SYNC;
1560 
1561 			/* Add the new filter to the tmp list */
1562 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1563 
1564 			/* Put the original filter into the delete list */
1565 			f->state = I40E_FILTER_REMOVE;
1566 			hash_del(&f->hlist);
1567 			hlist_add_head(&f->hlist, tmp_del_list);
1568 		}
1569 	}
1570 
1571 	vsi->has_vlan_filter = !!vlan_filters;
1572 	return 0;
1573 }
1574 
1575 /**
1576  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1577  * @vsi: the PF Main VSI - inappropriate for any other VSI
1578  * @macaddr: the MAC address
1579  *
1580  * Remove whatever filter the firmware set up so the driver can manage
1581  * its own filtering intelligently.
1582  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1583 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1584 {
1585 	struct i40e_aqc_remove_macvlan_element_data element;
1586 	struct i40e_pf *pf = vsi->back;
1587 
1588 	/* Only appropriate for the PF main VSI */
1589 	if (vsi->type != I40E_VSI_MAIN)
1590 		return;
1591 
1592 	memset(&element, 0, sizeof(element));
1593 	ether_addr_copy(element.mac_addr, macaddr);
1594 	element.vlan_tag = 0;
1595 	/* Ignore error returns, some firmware does it this way... */
1596 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1597 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1598 
1599 	memset(&element, 0, sizeof(element));
1600 	ether_addr_copy(element.mac_addr, macaddr);
1601 	element.vlan_tag = 0;
1602 	/* ...and some firmware does it this way. */
1603 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1604 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1605 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1606 }
1607 
1608 /**
1609  * i40e_add_filter - Add a mac/vlan filter to the VSI
1610  * @vsi: the VSI to be searched
1611  * @macaddr: the MAC address
1612  * @vlan: the vlan
1613  *
1614  * Returns ptr to the filter object or NULL when no memory available.
1615  *
1616  * NOTE: This function is expected to be called with mac_filter_hash_lock
1617  * being held.
1618  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1619 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1620 					const u8 *macaddr, s16 vlan)
1621 {
1622 	struct i40e_mac_filter *f;
1623 	u64 key;
1624 
1625 	if (!vsi || !macaddr)
1626 		return NULL;
1627 
1628 	f = i40e_find_filter(vsi, macaddr, vlan);
1629 	if (!f) {
1630 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1631 		if (!f)
1632 			return NULL;
1633 
1634 		/* Update the boolean indicating if we need to function in
1635 		 * VLAN mode.
1636 		 */
1637 		if (vlan >= 0)
1638 			vsi->has_vlan_filter = true;
1639 
1640 		ether_addr_copy(f->macaddr, macaddr);
1641 		f->vlan = vlan;
1642 		f->state = I40E_FILTER_NEW;
1643 		INIT_HLIST_NODE(&f->hlist);
1644 
1645 		key = i40e_addr_to_hkey(macaddr);
1646 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1647 
1648 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1649 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1650 	}
1651 
1652 	/* If we're asked to add a filter that has been marked for removal, it
1653 	 * is safe to simply restore it to active state. __i40e_del_filter
1654 	 * will have simply deleted any filters which were previously marked
1655 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1656 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1657 	 * task, just restore this filter to the ACTIVE state so that the
1658 	 * sync task leaves it in place
1659 	 */
1660 	if (f->state == I40E_FILTER_REMOVE)
1661 		f->state = I40E_FILTER_ACTIVE;
1662 
1663 	return f;
1664 }
1665 
1666 /**
1667  * __i40e_del_filter - Remove a specific filter from the VSI
1668  * @vsi: VSI to remove from
1669  * @f: the filter to remove from the list
1670  *
1671  * This function requires you've found * the exact filter you will remove
1672  * already, such as via i40e_find_filter or i40e_find_mac.
1673  *
1674  * NOTE: This function is expected to be called with mac_filter_hash_lock
1675  * being held.
1676  * ANOTHER NOTE: This function MUST be called from within the context of
1677  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1678  * instead of list_for_each_entry().
1679  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1680 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1681 {
1682 	if (!f)
1683 		return;
1684 
1685 	/* If the filter was never added to firmware then we can just delete it
1686 	 * directly and we don't want to set the status to remove or else an
1687 	 * admin queue command will unnecessarily fire.
1688 	 */
1689 	if ((f->state == I40E_FILTER_FAILED) ||
1690 	    (f->state == I40E_FILTER_NEW)) {
1691 		hash_del(&f->hlist);
1692 		kfree(f);
1693 	} else {
1694 		f->state = I40E_FILTER_REMOVE;
1695 	}
1696 
1697 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1698 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1699 }
1700 
1701 /**
1702  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1703  * @vsi: the VSI to be searched
1704  * @macaddr: the mac address to be filtered
1705  *
1706  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1707  * go through all the macvlan filters and add a macvlan filter for each
1708  * unique vlan that already exists. If a PVID has been assigned, instead only
1709  * add the macaddr to that VLAN.
1710  *
1711  * Returns last filter added on success, else NULL
1712  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1713 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1714 					    const u8 *macaddr)
1715 {
1716 	struct i40e_mac_filter *f, *add = NULL;
1717 	struct hlist_node *h;
1718 	int bkt;
1719 
1720 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1721 	if (vsi->info.pvid)
1722 		return i40e_add_filter(vsi, macaddr,
1723 				       le16_to_cpu(vsi->info.pvid));
1724 
1725 	if (!i40e_is_vsi_in_vlan(vsi))
1726 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1727 
1728 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1729 		if (f->state == I40E_FILTER_REMOVE)
1730 			continue;
1731 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1732 		if (!add)
1733 			return NULL;
1734 	}
1735 
1736 	return add;
1737 }
1738 
1739 /**
1740  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1741  * @vsi: the VSI to be searched
1742  * @macaddr: the mac address to be removed
1743  *
1744  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1745  * associated with.
1746  *
1747  * Returns 0 for success, or error
1748  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1749 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1750 {
1751 	struct i40e_mac_filter *f;
1752 	struct hlist_node *h;
1753 	bool found = false;
1754 	int bkt;
1755 
1756 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1757 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1758 		if (ether_addr_equal(macaddr, f->macaddr)) {
1759 			__i40e_del_filter(vsi, f);
1760 			found = true;
1761 		}
1762 	}
1763 
1764 	if (found)
1765 		return 0;
1766 	else
1767 		return -ENOENT;
1768 }
1769 
1770 /**
1771  * i40e_set_mac - NDO callback to set mac address
1772  * @netdev: network interface device structure
1773  * @p: pointer to an address structure
1774  *
1775  * Returns 0 on success, negative on failure
1776  **/
i40e_set_mac(struct net_device * netdev,void * p)1777 static int i40e_set_mac(struct net_device *netdev, void *p)
1778 {
1779 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1780 	struct i40e_vsi *vsi = np->vsi;
1781 	struct i40e_pf *pf = vsi->back;
1782 	struct i40e_hw *hw = &pf->hw;
1783 	struct sockaddr *addr = p;
1784 
1785 	if (!is_valid_ether_addr(addr->sa_data))
1786 		return -EADDRNOTAVAIL;
1787 
1788 	if (test_bit(__I40E_DOWN, pf->state) ||
1789 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1790 		return -EADDRNOTAVAIL;
1791 
1792 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1793 		netdev_info(netdev, "returning to hw mac address %pM\n",
1794 			    hw->mac.addr);
1795 	else
1796 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1797 
1798 	/* Copy the address first, so that we avoid a possible race with
1799 	 * .set_rx_mode().
1800 	 * - Remove old address from MAC filter
1801 	 * - Copy new address
1802 	 * - Add new address to MAC filter
1803 	 */
1804 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1805 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1806 	eth_hw_addr_set(netdev, addr->sa_data);
1807 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1808 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1809 
1810 	if (vsi->type == I40E_VSI_MAIN) {
1811 		int ret;
1812 
1813 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1814 						addr->sa_data, NULL);
1815 		if (ret)
1816 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1817 				    ERR_PTR(ret),
1818 				    libie_aq_str(hw->aq.asq_last_status));
1819 	}
1820 
1821 	/* schedule our worker thread which will take care of
1822 	 * applying the new filter changes
1823 	 */
1824 	i40e_service_event_schedule(pf);
1825 	return 0;
1826 }
1827 
1828 /**
1829  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1830  * @vsi: vsi structure
1831  * @seed: RSS hash seed
1832  * @lut: pointer to lookup table of lut_size
1833  * @lut_size: size of the lookup table
1834  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1835 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1836 			      u8 *lut, u16 lut_size)
1837 {
1838 	struct i40e_pf *pf = vsi->back;
1839 	struct i40e_hw *hw = &pf->hw;
1840 	int ret = 0;
1841 
1842 	if (seed) {
1843 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1844 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1845 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1846 		if (ret) {
1847 			dev_info(&pf->pdev->dev,
1848 				 "Cannot set RSS key, err %pe aq_err %s\n",
1849 				 ERR_PTR(ret),
1850 				 libie_aq_str(hw->aq.asq_last_status));
1851 			return ret;
1852 		}
1853 	}
1854 	if (lut) {
1855 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1856 
1857 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1858 		if (ret) {
1859 			dev_info(&pf->pdev->dev,
1860 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1861 				 ERR_PTR(ret),
1862 				 libie_aq_str(hw->aq.asq_last_status));
1863 			return ret;
1864 		}
1865 	}
1866 	return ret;
1867 }
1868 
1869 /**
1870  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1871  * @vsi: VSI structure
1872  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1873 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1874 {
1875 	struct i40e_pf *pf = vsi->back;
1876 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1877 	u8 *lut;
1878 	int ret;
1879 
1880 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1881 		return 0;
1882 	if (!vsi->rss_size)
1883 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1884 				      vsi->num_queue_pairs);
1885 	if (!vsi->rss_size)
1886 		return -EINVAL;
1887 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1888 	if (!lut)
1889 		return -ENOMEM;
1890 
1891 	/* Use the user configured hash keys and lookup table if there is one,
1892 	 * otherwise use default
1893 	 */
1894 	if (vsi->rss_lut_user)
1895 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1896 	else
1897 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1898 	if (vsi->rss_hkey_user)
1899 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1900 	else
1901 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1902 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1903 	kfree(lut);
1904 	return ret;
1905 }
1906 
1907 /**
1908  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1909  * @vsi: the VSI being configured,
1910  * @ctxt: VSI context structure
1911  * @enabled_tc: number of traffic classes to enable
1912  *
1913  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1914  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1915 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1916 					   struct i40e_vsi_context *ctxt,
1917 					   u8 enabled_tc)
1918 {
1919 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1920 	int i, override_q, pow, num_qps, ret;
1921 	u8 netdev_tc = 0, offset = 0;
1922 
1923 	if (vsi->type != I40E_VSI_MAIN)
1924 		return -EINVAL;
1925 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1926 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1927 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1928 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1929 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1930 
1931 	/* find the next higher power-of-2 of num queue pairs */
1932 	pow = ilog2(num_qps);
1933 	if (!is_power_of_2(num_qps))
1934 		pow++;
1935 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1936 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1937 
1938 	/* Setup queue offset/count for all TCs for given VSI */
1939 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1940 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1941 		/* See if the given TC is enabled for the given VSI */
1942 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1943 			offset = vsi->mqprio_qopt.qopt.offset[i];
1944 			qcount = vsi->mqprio_qopt.qopt.count[i];
1945 			if (qcount > max_qcount)
1946 				max_qcount = qcount;
1947 			vsi->tc_config.tc_info[i].qoffset = offset;
1948 			vsi->tc_config.tc_info[i].qcount = qcount;
1949 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1950 		} else {
1951 			/* TC is not enabled so set the offset to
1952 			 * default queue and allocate one queue
1953 			 * for the given TC.
1954 			 */
1955 			vsi->tc_config.tc_info[i].qoffset = 0;
1956 			vsi->tc_config.tc_info[i].qcount = 1;
1957 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1958 		}
1959 	}
1960 
1961 	/* Set actual Tx/Rx queue pairs */
1962 	vsi->num_queue_pairs = offset + qcount;
1963 
1964 	/* Setup queue TC[0].qmap for given VSI context */
1965 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1966 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1967 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1968 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1969 
1970 	/* Reconfigure RSS for main VSI with max queue count */
1971 	vsi->rss_size = max_qcount;
1972 	ret = i40e_vsi_config_rss(vsi);
1973 	if (ret) {
1974 		dev_info(&vsi->back->pdev->dev,
1975 			 "Failed to reconfig rss for num_queues (%u)\n",
1976 			 max_qcount);
1977 		return ret;
1978 	}
1979 	vsi->reconfig_rss = true;
1980 	dev_dbg(&vsi->back->pdev->dev,
1981 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1982 
1983 	/* Find queue count available for channel VSIs and starting offset
1984 	 * for channel VSIs
1985 	 */
1986 	override_q = vsi->mqprio_qopt.qopt.count[0];
1987 	if (override_q && override_q < vsi->num_queue_pairs) {
1988 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1989 		vsi->next_base_queue = override_q;
1990 	}
1991 	return 0;
1992 }
1993 
1994 /**
1995  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1996  * @vsi: the VSI being setup
1997  * @ctxt: VSI context structure
1998  * @enabled_tc: Enabled TCs bitmap
1999  * @is_add: True if called before Add VSI
2000  *
2001  * Setup VSI queue mapping for enabled traffic classes.
2002  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2003 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2004 				     struct i40e_vsi_context *ctxt,
2005 				     u8 enabled_tc,
2006 				     bool is_add)
2007 {
2008 	struct i40e_pf *pf = vsi->back;
2009 	u16 num_tc_qps = 0;
2010 	u16 sections = 0;
2011 	u8 netdev_tc = 0;
2012 	u16 numtc = 1;
2013 	u16 qcount;
2014 	u8 offset;
2015 	u16 qmap;
2016 	int i;
2017 
2018 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2019 	offset = 0;
2020 	/* zero out queue mapping, it will get updated on the end of the function */
2021 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2022 
2023 	if (vsi->type == I40E_VSI_MAIN) {
2024 		/* This code helps add more queue to the VSI if we have
2025 		 * more cores than RSS can support, the higher cores will
2026 		 * be served by ATR or other filters. Furthermore, the
2027 		 * non-zero req_queue_pairs says that user requested a new
2028 		 * queue count via ethtool's set_channels, so use this
2029 		 * value for queues distribution across traffic classes
2030 		 * We need at least one queue pair for the interface
2031 		 * to be usable as we see in else statement.
2032 		 */
2033 		if (vsi->req_queue_pairs > 0)
2034 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2035 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2036 			vsi->num_queue_pairs = pf->num_lan_msix;
2037 		else
2038 			vsi->num_queue_pairs = 1;
2039 	}
2040 
2041 	/* Number of queues per enabled TC */
2042 	if (vsi->type == I40E_VSI_MAIN ||
2043 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2044 		num_tc_qps = vsi->num_queue_pairs;
2045 	else
2046 		num_tc_qps = vsi->alloc_queue_pairs;
2047 
2048 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2049 		/* Find numtc from enabled TC bitmap */
2050 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2051 			if (enabled_tc & BIT(i)) /* TC is enabled */
2052 				numtc++;
2053 		}
2054 		if (!numtc) {
2055 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2056 			numtc = 1;
2057 		}
2058 		num_tc_qps = num_tc_qps / numtc;
2059 		num_tc_qps = min_t(int, num_tc_qps,
2060 				   i40e_pf_get_max_q_per_tc(pf));
2061 	}
2062 
2063 	vsi->tc_config.numtc = numtc;
2064 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2065 
2066 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2067 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2068 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2069 
2070 	/* Setup queue offset/count for all TCs for given VSI */
2071 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2072 		/* See if the given TC is enabled for the given VSI */
2073 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2074 			/* TC is enabled */
2075 			int pow, num_qps;
2076 
2077 			switch (vsi->type) {
2078 			case I40E_VSI_MAIN:
2079 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2080 					       pf->flags) &&
2081 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2082 					       pf->flags)) ||
2083 				    vsi->tc_config.enabled_tc != 1) {
2084 					qcount = min_t(int, pf->alloc_rss_size,
2085 						       num_tc_qps);
2086 					break;
2087 				}
2088 				fallthrough;
2089 			case I40E_VSI_FDIR:
2090 			case I40E_VSI_SRIOV:
2091 			case I40E_VSI_VMDQ2:
2092 			default:
2093 				qcount = num_tc_qps;
2094 				WARN_ON(i != 0);
2095 				break;
2096 			}
2097 			vsi->tc_config.tc_info[i].qoffset = offset;
2098 			vsi->tc_config.tc_info[i].qcount = qcount;
2099 
2100 			/* find the next higher power-of-2 of num queue pairs */
2101 			num_qps = qcount;
2102 			pow = 0;
2103 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2104 				pow++;
2105 				num_qps >>= 1;
2106 			}
2107 
2108 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2109 			qmap =
2110 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2111 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2112 
2113 			offset += qcount;
2114 		} else {
2115 			/* TC is not enabled so set the offset to
2116 			 * default queue and allocate one queue
2117 			 * for the given TC.
2118 			 */
2119 			vsi->tc_config.tc_info[i].qoffset = 0;
2120 			vsi->tc_config.tc_info[i].qcount = 1;
2121 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2122 
2123 			qmap = 0;
2124 		}
2125 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2126 	}
2127 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2128 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2129 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2130 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2131 		vsi->num_queue_pairs = offset;
2132 
2133 	/* Scheduler section valid can only be set for ADD VSI */
2134 	if (is_add) {
2135 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2136 
2137 		ctxt->info.up_enable_bits = enabled_tc;
2138 	}
2139 	if (vsi->type == I40E_VSI_SRIOV) {
2140 		ctxt->info.mapping_flags |=
2141 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2142 		for (i = 0; i < vsi->num_queue_pairs; i++)
2143 			ctxt->info.queue_mapping[i] =
2144 					       cpu_to_le16(vsi->base_queue + i);
2145 	} else {
2146 		ctxt->info.mapping_flags |=
2147 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2148 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2149 	}
2150 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2151 }
2152 
2153 /**
2154  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2155  * @netdev: the netdevice
2156  * @addr: address to add
2157  *
2158  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2159  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2160  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2161 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2162 {
2163 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2164 	struct i40e_vsi *vsi = np->vsi;
2165 
2166 	if (i40e_add_mac_filter(vsi, addr))
2167 		return 0;
2168 	else
2169 		return -ENOMEM;
2170 }
2171 
2172 /**
2173  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2174  * @netdev: the netdevice
2175  * @addr: address to add
2176  *
2177  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2178  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2179  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2180 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2181 {
2182 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2183 	struct i40e_vsi *vsi = np->vsi;
2184 
2185 	/* Under some circumstances, we might receive a request to delete
2186 	 * our own device address from our uc list. Because we store the
2187 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2188 	 * such requests and not delete our device address from this list.
2189 	 */
2190 	if (ether_addr_equal(addr, netdev->dev_addr))
2191 		return 0;
2192 
2193 	i40e_del_mac_filter(vsi, addr);
2194 
2195 	return 0;
2196 }
2197 
2198 /**
2199  * i40e_set_rx_mode - NDO callback to set the netdev filters
2200  * @netdev: network interface device structure
2201  **/
i40e_set_rx_mode(struct net_device * netdev)2202 static void i40e_set_rx_mode(struct net_device *netdev)
2203 {
2204 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2205 	struct i40e_vsi *vsi = np->vsi;
2206 
2207 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2208 
2209 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2210 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2211 
2212 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2213 
2214 	/* check for other flag changes */
2215 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2216 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2217 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2218 	}
2219 }
2220 
2221 /**
2222  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2223  * @vsi: Pointer to VSI struct
2224  * @from: Pointer to list which contains MAC filter entries - changes to
2225  *        those entries needs to be undone.
2226  *
2227  * MAC filter entries from this list were slated for deletion.
2228  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2229 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2230 					 struct hlist_head *from)
2231 {
2232 	struct i40e_mac_filter *f;
2233 	struct hlist_node *h;
2234 
2235 	hlist_for_each_entry_safe(f, h, from, hlist) {
2236 		u64 key = i40e_addr_to_hkey(f->macaddr);
2237 
2238 		/* Move the element back into MAC filter list*/
2239 		hlist_del(&f->hlist);
2240 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2241 	}
2242 }
2243 
2244 /**
2245  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2246  * @vsi: Pointer to vsi struct
2247  * @from: Pointer to list which contains MAC filter entries - changes to
2248  *        those entries needs to be undone.
2249  *
2250  * MAC filter entries from this list were slated for addition.
2251  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2252 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2253 					 struct hlist_head *from)
2254 {
2255 	struct i40e_new_mac_filter *new;
2256 	struct hlist_node *h;
2257 
2258 	hlist_for_each_entry_safe(new, h, from, hlist) {
2259 		/* We can simply free the wrapper structure */
2260 		hlist_del(&new->hlist);
2261 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2262 		kfree(new);
2263 	}
2264 }
2265 
2266 /**
2267  * i40e_next_filter - Get the next non-broadcast filter from a list
2268  * @next: pointer to filter in list
2269  *
2270  * Returns the next non-broadcast filter in the list. Required so that we
2271  * ignore broadcast filters within the list, since these are not handled via
2272  * the normal firmware update path.
2273  */
2274 static
i40e_next_filter(struct i40e_new_mac_filter * next)2275 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2276 {
2277 	hlist_for_each_entry_continue(next, hlist) {
2278 		if (!is_broadcast_ether_addr(next->f->macaddr))
2279 			return next;
2280 	}
2281 
2282 	return NULL;
2283 }
2284 
2285 /**
2286  * i40e_update_filter_state - Update filter state based on return data
2287  * from firmware
2288  * @count: Number of filters added
2289  * @add_list: return data from fw
2290  * @add_head: pointer to first filter in current batch
2291  *
2292  * MAC filter entries from list were slated to be added to device. Returns
2293  * number of successful filters. Note that 0 does NOT mean success!
2294  **/
2295 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2296 i40e_update_filter_state(int count,
2297 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2298 			 struct i40e_new_mac_filter *add_head)
2299 {
2300 	int retval = 0;
2301 	int i;
2302 
2303 	for (i = 0; i < count; i++) {
2304 		/* Always check status of each filter. We don't need to check
2305 		 * the firmware return status because we pre-set the filter
2306 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2307 		 * request to the adminq. Thus, if it no longer matches then
2308 		 * we know the filter is active.
2309 		 */
2310 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2311 			add_head->state = I40E_FILTER_FAILED;
2312 		} else {
2313 			add_head->state = I40E_FILTER_ACTIVE;
2314 			retval++;
2315 		}
2316 
2317 		add_head = i40e_next_filter(add_head);
2318 		if (!add_head)
2319 			break;
2320 	}
2321 
2322 	return retval;
2323 }
2324 
2325 /**
2326  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2327  * @vsi: ptr to the VSI
2328  * @vsi_name: name to display in messages
2329  * @list: the list of filters to send to firmware
2330  * @num_del: the number of filters to delete
2331  * @retval: Set to -EIO on failure to delete
2332  *
2333  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2334  * *retval instead of a return value so that success does not force ret_val to
2335  * be set to 0. This ensures that a sequence of calls to this function
2336  * preserve the previous value of *retval on successful delete.
2337  */
2338 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)2339 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2340 			  struct i40e_aqc_remove_macvlan_element_data *list,
2341 			  int num_del, int *retval)
2342 {
2343 	struct i40e_hw *hw = &vsi->back->hw;
2344 	enum libie_aq_err aq_status;
2345 	int aq_ret;
2346 
2347 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2348 					   &aq_status);
2349 
2350 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2351 	if (aq_ret && !(aq_status == LIBIE_AQ_RC_ENOENT)) {
2352 		*retval = -EIO;
2353 		dev_info(&vsi->back->pdev->dev,
2354 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2355 			 vsi_name, ERR_PTR(aq_ret), libie_aq_str(aq_status));
2356 	}
2357 }
2358 
2359 /**
2360  * i40e_aqc_add_filters - Request firmware to add a set of filters
2361  * @vsi: ptr to the VSI
2362  * @vsi_name: name to display in messages
2363  * @list: the list of filters to send to firmware
2364  * @add_head: Position in the add hlist
2365  * @num_add: the number of filters to add
2366  *
2367  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2368  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2369  * space for more filters.
2370  */
2371 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)2372 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2373 			  struct i40e_aqc_add_macvlan_element_data *list,
2374 			  struct i40e_new_mac_filter *add_head,
2375 			  int num_add)
2376 {
2377 	struct i40e_hw *hw = &vsi->back->hw;
2378 	enum libie_aq_err aq_status;
2379 	int fcnt;
2380 
2381 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2382 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2383 
2384 	if (fcnt != num_add) {
2385 		if (vsi->type == I40E_VSI_MAIN) {
2386 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2387 			dev_warn(&vsi->back->pdev->dev,
2388 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2389 				 libie_aq_str(aq_status), vsi_name);
2390 		} else if (vsi->type == I40E_VSI_SRIOV ||
2391 			   vsi->type == I40E_VSI_VMDQ1 ||
2392 			   vsi->type == I40E_VSI_VMDQ2) {
2393 			dev_warn(&vsi->back->pdev->dev,
2394 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2395 				 libie_aq_str(aq_status), vsi_name, vsi_name);
2396 		} else {
2397 			dev_warn(&vsi->back->pdev->dev,
2398 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2399 				 libie_aq_str(aq_status), vsi_name, vsi->type);
2400 		}
2401 	}
2402 }
2403 
2404 /**
2405  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2406  * @vsi: pointer to the VSI
2407  * @vsi_name: the VSI name
2408  * @f: filter data
2409  *
2410  * This function sets or clears the promiscuous broadcast flags for VLAN
2411  * filters in order to properly receive broadcast frames. Assumes that only
2412  * broadcast filters are passed.
2413  *
2414  * Returns status indicating success or failure;
2415  **/
2416 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2417 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2418 			  struct i40e_mac_filter *f)
2419 {
2420 	bool enable = f->state == I40E_FILTER_NEW ||
2421 		      f->state == I40E_FILTER_NEW_SYNC;
2422 	struct i40e_hw *hw = &vsi->back->hw;
2423 	int aq_ret;
2424 
2425 	if (f->vlan == I40E_VLAN_ANY) {
2426 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2427 						   vsi->seid,
2428 						   enable,
2429 						   NULL);
2430 	} else {
2431 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2432 							    vsi->seid,
2433 							    enable,
2434 							    f->vlan,
2435 							    NULL);
2436 	}
2437 
2438 	if (aq_ret) {
2439 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2440 		dev_warn(&vsi->back->pdev->dev,
2441 			 "Error %s, forcing overflow promiscuous on %s\n",
2442 			 libie_aq_str(hw->aq.asq_last_status), vsi_name);
2443 	}
2444 
2445 	return aq_ret;
2446 }
2447 
2448 /**
2449  * i40e_set_promiscuous - set promiscuous mode
2450  * @pf: board private structure
2451  * @promisc: promisc on or off
2452  *
2453  * There are different ways of setting promiscuous mode on a PF depending on
2454  * what state/environment we're in.  This identifies and sets it appropriately.
2455  * Returns 0 on success.
2456  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2457 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2458 {
2459 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2460 	struct i40e_hw *hw = &pf->hw;
2461 	int aq_ret;
2462 
2463 	if (vsi->type == I40E_VSI_MAIN &&
2464 	    i40e_pf_get_main_veb(pf) &&
2465 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2466 		/* set defport ON for Main VSI instead of true promisc
2467 		 * this way we will get all unicast/multicast and VLAN
2468 		 * promisc behavior but will not get VF or VMDq traffic
2469 		 * replicated on the Main VSI.
2470 		 */
2471 		if (promisc)
2472 			aq_ret = i40e_aq_set_default_vsi(hw,
2473 							 vsi->seid,
2474 							 NULL);
2475 		else
2476 			aq_ret = i40e_aq_clear_default_vsi(hw,
2477 							   vsi->seid,
2478 							   NULL);
2479 		if (aq_ret) {
2480 			dev_info(&pf->pdev->dev,
2481 				 "Set default VSI failed, err %pe, aq_err %s\n",
2482 				 ERR_PTR(aq_ret),
2483 				 libie_aq_str(hw->aq.asq_last_status));
2484 		}
2485 	} else {
2486 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2487 						  hw,
2488 						  vsi->seid,
2489 						  promisc, NULL,
2490 						  true);
2491 		if (aq_ret) {
2492 			dev_info(&pf->pdev->dev,
2493 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2494 				 ERR_PTR(aq_ret),
2495 				 libie_aq_str(hw->aq.asq_last_status));
2496 		}
2497 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2498 						  hw,
2499 						  vsi->seid,
2500 						  promisc, NULL);
2501 		if (aq_ret) {
2502 			dev_info(&pf->pdev->dev,
2503 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2504 				 ERR_PTR(aq_ret),
2505 				 libie_aq_str(hw->aq.asq_last_status));
2506 		}
2507 	}
2508 
2509 	if (!aq_ret)
2510 		pf->cur_promisc = promisc;
2511 
2512 	return aq_ret;
2513 }
2514 
2515 /**
2516  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2517  * @vsi: ptr to the VSI
2518  *
2519  * Push any outstanding VSI filter changes through the AdminQ.
2520  *
2521  * Returns 0 or error value
2522  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2523 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2524 {
2525 	struct hlist_head tmp_add_list, tmp_del_list;
2526 	struct i40e_mac_filter *f;
2527 	struct i40e_new_mac_filter *new, *add_head = NULL;
2528 	struct i40e_hw *hw = &vsi->back->hw;
2529 	bool old_overflow, new_overflow;
2530 	unsigned int failed_filters = 0;
2531 	unsigned int vlan_filters = 0;
2532 	char vsi_name[16] = "PF";
2533 	int filter_list_len = 0;
2534 	u32 changed_flags = 0;
2535 	struct hlist_node *h;
2536 	struct i40e_pf *pf;
2537 	int num_add = 0;
2538 	int num_del = 0;
2539 	int aq_ret = 0;
2540 	int retval = 0;
2541 	u16 cmd_flags;
2542 	int list_size;
2543 	int bkt;
2544 
2545 	/* empty array typed pointers, kcalloc later */
2546 	struct i40e_aqc_add_macvlan_element_data *add_list;
2547 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2548 
2549 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2550 		usleep_range(1000, 2000);
2551 	pf = vsi->back;
2552 
2553 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2554 
2555 	if (vsi->netdev) {
2556 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2557 		vsi->current_netdev_flags = vsi->netdev->flags;
2558 	}
2559 
2560 	INIT_HLIST_HEAD(&tmp_add_list);
2561 	INIT_HLIST_HEAD(&tmp_del_list);
2562 
2563 	if (vsi->type == I40E_VSI_SRIOV)
2564 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2565 	else if (vsi->type != I40E_VSI_MAIN)
2566 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2567 
2568 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2569 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2570 
2571 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2572 		/* Create a list of filters to delete. */
2573 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2574 			if (f->state == I40E_FILTER_REMOVE) {
2575 				/* Move the element into temporary del_list */
2576 				hash_del(&f->hlist);
2577 				hlist_add_head(&f->hlist, &tmp_del_list);
2578 
2579 				/* Avoid counting removed filters */
2580 				continue;
2581 			}
2582 			if (f->state == I40E_FILTER_NEW) {
2583 				/* Create a temporary i40e_new_mac_filter */
2584 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2585 				if (!new)
2586 					goto err_no_memory_locked;
2587 
2588 				/* Store pointer to the real filter */
2589 				new->f = f;
2590 				new->state = f->state;
2591 
2592 				/* Add it to the hash list */
2593 				hlist_add_head(&new->hlist, &tmp_add_list);
2594 				f->state = I40E_FILTER_NEW_SYNC;
2595 			}
2596 
2597 			/* Count the number of active (current and new) VLAN
2598 			 * filters we have now. Does not count filters which
2599 			 * are marked for deletion.
2600 			 */
2601 			if (f->vlan > 0)
2602 				vlan_filters++;
2603 		}
2604 
2605 		if (vsi->type != I40E_VSI_SRIOV)
2606 			retval = i40e_correct_mac_vlan_filters
2607 				(vsi, &tmp_add_list, &tmp_del_list,
2608 				 vlan_filters);
2609 		else if (pf->vf)
2610 			retval = i40e_correct_vf_mac_vlan_filters
2611 				(vsi, &tmp_add_list, &tmp_del_list,
2612 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2613 
2614 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2615 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2616 
2617 		if (retval)
2618 			goto err_no_memory_locked;
2619 
2620 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2621 	}
2622 
2623 	/* Now process 'del_list' outside the lock */
2624 	if (!hlist_empty(&tmp_del_list)) {
2625 		filter_list_len = hw->aq.asq_buf_size /
2626 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2627 		list_size = filter_list_len *
2628 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2629 		del_list = kzalloc(list_size, GFP_ATOMIC);
2630 		if (!del_list)
2631 			goto err_no_memory;
2632 
2633 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2634 			cmd_flags = 0;
2635 
2636 			/* handle broadcast filters by updating the broadcast
2637 			 * promiscuous flag and release filter list.
2638 			 */
2639 			if (is_broadcast_ether_addr(f->macaddr)) {
2640 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2641 
2642 				hlist_del(&f->hlist);
2643 				kfree(f);
2644 				continue;
2645 			}
2646 
2647 			/* add to delete list */
2648 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2649 			if (f->vlan == I40E_VLAN_ANY) {
2650 				del_list[num_del].vlan_tag = 0;
2651 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2652 			} else {
2653 				del_list[num_del].vlan_tag =
2654 					cpu_to_le16((u16)(f->vlan));
2655 			}
2656 
2657 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2658 			del_list[num_del].flags = cmd_flags;
2659 			num_del++;
2660 
2661 			/* flush a full buffer */
2662 			if (num_del == filter_list_len) {
2663 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2664 						     num_del, &retval);
2665 				memset(del_list, 0, list_size);
2666 				num_del = 0;
2667 			}
2668 			/* Release memory for MAC filter entries which were
2669 			 * synced up with HW.
2670 			 */
2671 			hlist_del(&f->hlist);
2672 			kfree(f);
2673 		}
2674 
2675 		if (num_del) {
2676 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2677 					     num_del, &retval);
2678 		}
2679 
2680 		kfree(del_list);
2681 		del_list = NULL;
2682 	}
2683 
2684 	if (!hlist_empty(&tmp_add_list)) {
2685 		/* Do all the adds now. */
2686 		filter_list_len = hw->aq.asq_buf_size /
2687 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2688 		list_size = filter_list_len *
2689 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2690 		add_list = kzalloc(list_size, GFP_ATOMIC);
2691 		if (!add_list)
2692 			goto err_no_memory;
2693 
2694 		num_add = 0;
2695 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2696 			/* handle broadcast filters by updating the broadcast
2697 			 * promiscuous flag instead of adding a MAC filter.
2698 			 */
2699 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2700 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2701 							      new->f))
2702 					new->state = I40E_FILTER_FAILED;
2703 				else
2704 					new->state = I40E_FILTER_ACTIVE;
2705 				continue;
2706 			}
2707 
2708 			/* add to add array */
2709 			if (num_add == 0)
2710 				add_head = new;
2711 			cmd_flags = 0;
2712 			ether_addr_copy(add_list[num_add].mac_addr,
2713 					new->f->macaddr);
2714 			if (new->f->vlan == I40E_VLAN_ANY) {
2715 				add_list[num_add].vlan_tag = 0;
2716 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2717 			} else {
2718 				add_list[num_add].vlan_tag =
2719 					cpu_to_le16((u16)(new->f->vlan));
2720 			}
2721 			add_list[num_add].queue_number = 0;
2722 			/* set invalid match method for later detection */
2723 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2724 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2725 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2726 			num_add++;
2727 
2728 			/* flush a full buffer */
2729 			if (num_add == filter_list_len) {
2730 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2731 						     add_head, num_add);
2732 				memset(add_list, 0, list_size);
2733 				num_add = 0;
2734 			}
2735 		}
2736 		if (num_add) {
2737 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2738 					     num_add);
2739 		}
2740 		/* Now move all of the filters from the temp add list back to
2741 		 * the VSI's list.
2742 		 */
2743 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2744 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2745 			/* Only update the state if we're still NEW */
2746 			if (new->f->state == I40E_FILTER_NEW ||
2747 			    new->f->state == I40E_FILTER_NEW_SYNC)
2748 				new->f->state = new->state;
2749 			hlist_del(&new->hlist);
2750 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2751 			kfree(new);
2752 		}
2753 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2754 		kfree(add_list);
2755 		add_list = NULL;
2756 	}
2757 
2758 	/* Determine the number of active and failed filters. */
2759 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2760 	vsi->active_filters = 0;
2761 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2762 		if (f->state == I40E_FILTER_ACTIVE)
2763 			vsi->active_filters++;
2764 		else if (f->state == I40E_FILTER_FAILED)
2765 			failed_filters++;
2766 	}
2767 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2768 
2769 	/* Check if we are able to exit overflow promiscuous mode. We can
2770 	 * safely exit if we didn't just enter, we no longer have any failed
2771 	 * filters, and we have reduced filters below the threshold value.
2772 	 */
2773 	if (old_overflow && !failed_filters &&
2774 	    vsi->active_filters < vsi->promisc_threshold) {
2775 		dev_info(&pf->pdev->dev,
2776 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2777 			 vsi_name);
2778 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2779 		vsi->promisc_threshold = 0;
2780 	}
2781 
2782 	/* if the VF is not trusted do not do promisc */
2783 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2784 	    !pf->vf[vsi->vf_id].trusted) {
2785 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2786 		goto out;
2787 	}
2788 
2789 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2790 
2791 	/* If we are entering overflow promiscuous, we need to calculate a new
2792 	 * threshold for when we are safe to exit
2793 	 */
2794 	if (!old_overflow && new_overflow)
2795 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2796 
2797 	/* check for changes in promiscuous modes */
2798 	if (changed_flags & IFF_ALLMULTI) {
2799 		bool cur_multipromisc;
2800 
2801 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2802 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2803 							       vsi->seid,
2804 							       cur_multipromisc,
2805 							       NULL);
2806 		if (aq_ret) {
2807 			retval = i40e_aq_rc_to_posix(aq_ret,
2808 						     hw->aq.asq_last_status);
2809 			dev_info(&pf->pdev->dev,
2810 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2811 				 vsi_name,
2812 				 ERR_PTR(aq_ret),
2813 				 libie_aq_str(hw->aq.asq_last_status));
2814 		} else {
2815 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2816 				 cur_multipromisc ? "entering" : "leaving");
2817 		}
2818 	}
2819 
2820 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2821 		bool cur_promisc;
2822 
2823 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2824 			       new_overflow);
2825 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2826 		if (aq_ret) {
2827 			retval = i40e_aq_rc_to_posix(aq_ret,
2828 						     hw->aq.asq_last_status);
2829 			dev_info(&pf->pdev->dev,
2830 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2831 				 cur_promisc ? "on" : "off",
2832 				 vsi_name,
2833 				 ERR_PTR(aq_ret),
2834 				 libie_aq_str(hw->aq.asq_last_status));
2835 		}
2836 	}
2837 out:
2838 	/* if something went wrong then set the changed flag so we try again */
2839 	if (retval)
2840 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2841 
2842 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2843 	return retval;
2844 
2845 err_no_memory:
2846 	/* Restore elements on the temporary add and delete lists */
2847 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2848 err_no_memory_locked:
2849 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2850 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2851 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2852 
2853 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2854 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2855 	return -ENOMEM;
2856 }
2857 
2858 /**
2859  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2860  * @pf: board private structure
2861  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2862 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2863 {
2864 	struct i40e_vsi *vsi;
2865 	int v;
2866 
2867 	if (!pf)
2868 		return;
2869 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2870 		return;
2871 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2872 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2873 		return;
2874 	}
2875 
2876 	i40e_pf_for_each_vsi(pf, v, vsi) {
2877 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2878 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2879 			int ret = i40e_sync_vsi_filters(vsi);
2880 
2881 			if (ret) {
2882 				/* come back and try again later */
2883 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2884 					pf->state);
2885 				break;
2886 			}
2887 		}
2888 	}
2889 }
2890 
2891 /**
2892  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2893  *
2894  * @vsi: VSI to calculate rx_buf_len from
2895  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)2896 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2897 {
2898 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2899 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2900 
2901 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2902 }
2903 
2904 /**
2905  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2906  * @vsi: the vsi
2907  * @xdp_prog: XDP program
2908  **/
i40e_max_vsi_frame_size(struct i40e_vsi * vsi,struct bpf_prog * xdp_prog)2909 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2910 				   struct bpf_prog *xdp_prog)
2911 {
2912 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2913 	u16 chain_len;
2914 
2915 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2916 		chain_len = 1;
2917 	else
2918 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2919 
2920 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2921 }
2922 
2923 /**
2924  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2925  * @netdev: network interface device structure
2926  * @new_mtu: new value for maximum frame size
2927  *
2928  * Returns 0 on success, negative on failure
2929  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2930 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2931 {
2932 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2933 	struct i40e_vsi *vsi = np->vsi;
2934 	struct i40e_pf *pf = vsi->back;
2935 	int frame_size;
2936 
2937 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2938 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2939 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2940 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2941 		return -EINVAL;
2942 	}
2943 
2944 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2945 		   netdev->mtu, new_mtu);
2946 	WRITE_ONCE(netdev->mtu, new_mtu);
2947 	if (netif_running(netdev))
2948 		i40e_vsi_reinit_locked(vsi);
2949 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2950 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2951 	return 0;
2952 }
2953 
2954 /**
2955  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2956  * @vsi: the vsi being adjusted
2957  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2958 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2959 {
2960 	struct i40e_vsi_context ctxt;
2961 	int ret;
2962 
2963 	/* Don't modify stripping options if a port VLAN is active */
2964 	if (vsi->info.pvid)
2965 		return;
2966 
2967 	if ((vsi->info.valid_sections &
2968 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2969 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2970 		return;  /* already enabled */
2971 
2972 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2973 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2974 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2975 
2976 	ctxt.seid = vsi->seid;
2977 	ctxt.info = vsi->info;
2978 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2979 	if (ret) {
2980 		dev_info(&vsi->back->pdev->dev,
2981 			 "update vlan stripping failed, err %pe aq_err %s\n",
2982 			 ERR_PTR(ret),
2983 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
2984 	}
2985 }
2986 
2987 /**
2988  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2989  * @vsi: the vsi being adjusted
2990  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)2991 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2992 {
2993 	struct i40e_vsi_context ctxt;
2994 	int ret;
2995 
2996 	/* Don't modify stripping options if a port VLAN is active */
2997 	if (vsi->info.pvid)
2998 		return;
2999 
3000 	if ((vsi->info.valid_sections &
3001 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3002 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3003 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3004 		return;  /* already disabled */
3005 
3006 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3007 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3008 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3009 
3010 	ctxt.seid = vsi->seid;
3011 	ctxt.info = vsi->info;
3012 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3013 	if (ret) {
3014 		dev_info(&vsi->back->pdev->dev,
3015 			 "update vlan stripping failed, err %pe aq_err %s\n",
3016 			 ERR_PTR(ret),
3017 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3018 	}
3019 }
3020 
3021 /**
3022  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3023  * @vsi: the vsi being configured
3024  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3025  *
3026  * This is a helper function for adding a new MAC/VLAN filter with the
3027  * specified VLAN for each existing MAC address already in the hash table.
3028  * This function does *not* perform any accounting to update filters based on
3029  * VLAN mode.
3030  *
3031  * NOTE: this function expects to be called while under the
3032  * mac_filter_hash_lock
3033  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3034 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3035 {
3036 	struct i40e_mac_filter *f, *add_f;
3037 	struct hlist_node *h;
3038 	int bkt;
3039 
3040 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3041 		/* If we're asked to add a filter that has been marked for
3042 		 * removal, it is safe to simply restore it to active state.
3043 		 * __i40e_del_filter will have simply deleted any filters which
3044 		 * were previously marked NEW or FAILED, so if it is currently
3045 		 * marked REMOVE it must have previously been ACTIVE. Since we
3046 		 * haven't yet run the sync filters task, just restore this
3047 		 * filter to the ACTIVE state so that the sync task leaves it
3048 		 * in place.
3049 		 */
3050 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3051 			f->state = I40E_FILTER_ACTIVE;
3052 			continue;
3053 		} else if (f->state == I40E_FILTER_REMOVE) {
3054 			continue;
3055 		}
3056 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3057 		if (!add_f) {
3058 			dev_info(&vsi->back->pdev->dev,
3059 				 "Could not add vlan filter %d for %pM\n",
3060 				 vid, f->macaddr);
3061 			return -ENOMEM;
3062 		}
3063 	}
3064 
3065 	return 0;
3066 }
3067 
3068 /**
3069  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3070  * @vsi: the VSI being configured
3071  * @vid: VLAN id to be added
3072  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3073 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3074 {
3075 	int err;
3076 
3077 	if (vsi->info.pvid)
3078 		return -EINVAL;
3079 
3080 	/* The network stack will attempt to add VID=0, with the intention to
3081 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3082 	 * these packets by default when configured to receive untagged
3083 	 * packets, so we don't need to add a filter for this case.
3084 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3085 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3086 	 * Thus, we do not want to actually add a filter for VID=0
3087 	 */
3088 	if (!vid)
3089 		return 0;
3090 
3091 	/* Locked once because all functions invoked below iterates list*/
3092 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3093 	err = i40e_add_vlan_all_mac(vsi, vid);
3094 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3095 	if (err)
3096 		return err;
3097 
3098 	/* schedule our worker thread which will take care of
3099 	 * applying the new filter changes
3100 	 */
3101 	i40e_service_event_schedule(vsi->back);
3102 	return 0;
3103 }
3104 
3105 /**
3106  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3107  * @vsi: the vsi being configured
3108  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3109  *
3110  * This function should be used to remove all VLAN filters which match the
3111  * given VID. It does not schedule the service event and does not take the
3112  * mac_filter_hash_lock so it may be combined with other operations under
3113  * a single invocation of the mac_filter_hash_lock.
3114  *
3115  * NOTE: this function expects to be called while under the
3116  * mac_filter_hash_lock
3117  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3118 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3119 {
3120 	struct i40e_mac_filter *f;
3121 	struct hlist_node *h;
3122 	int bkt;
3123 
3124 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3125 		if (f->vlan == vid)
3126 			__i40e_del_filter(vsi, f);
3127 	}
3128 }
3129 
3130 /**
3131  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3132  * @vsi: the VSI being configured
3133  * @vid: VLAN id to be removed
3134  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3135 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3136 {
3137 	if (!vid || vsi->info.pvid)
3138 		return;
3139 
3140 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3141 	i40e_rm_vlan_all_mac(vsi, vid);
3142 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3143 
3144 	/* schedule our worker thread which will take care of
3145 	 * applying the new filter changes
3146 	 */
3147 	i40e_service_event_schedule(vsi->back);
3148 }
3149 
3150 /**
3151  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3152  * @netdev: network interface to be adjusted
3153  * @proto: unused protocol value
3154  * @vid: vlan id to be added
3155  *
3156  * net_device_ops implementation for adding vlan ids
3157  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3158 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3159 				__always_unused __be16 proto, u16 vid)
3160 {
3161 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3162 	struct i40e_vsi *vsi = np->vsi;
3163 	int ret = 0;
3164 
3165 	if (vid >= VLAN_N_VID)
3166 		return -EINVAL;
3167 
3168 	ret = i40e_vsi_add_vlan(vsi, vid);
3169 	if (!ret)
3170 		set_bit(vid, vsi->active_vlans);
3171 
3172 	return ret;
3173 }
3174 
3175 /**
3176  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3177  * @netdev: network interface to be adjusted
3178  * @proto: unused protocol value
3179  * @vid: vlan id to be added
3180  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3181 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3182 				    __always_unused __be16 proto, u16 vid)
3183 {
3184 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3185 	struct i40e_vsi *vsi = np->vsi;
3186 
3187 	if (vid >= VLAN_N_VID)
3188 		return;
3189 	set_bit(vid, vsi->active_vlans);
3190 }
3191 
3192 /**
3193  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3194  * @netdev: network interface to be adjusted
3195  * @proto: unused protocol value
3196  * @vid: vlan id to be removed
3197  *
3198  * net_device_ops implementation for removing vlan ids
3199  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3200 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3201 				 __always_unused __be16 proto, u16 vid)
3202 {
3203 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3204 	struct i40e_vsi *vsi = np->vsi;
3205 
3206 	/* return code is ignored as there is nothing a user
3207 	 * can do about failure to remove and a log message was
3208 	 * already printed from the other function
3209 	 */
3210 	i40e_vsi_kill_vlan(vsi, vid);
3211 
3212 	clear_bit(vid, vsi->active_vlans);
3213 
3214 	return 0;
3215 }
3216 
3217 /**
3218  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3219  * @vsi: the vsi being brought back up
3220  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3221 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3222 {
3223 	u16 vid;
3224 
3225 	if (!vsi->netdev)
3226 		return;
3227 
3228 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3229 		i40e_vlan_stripping_enable(vsi);
3230 	else
3231 		i40e_vlan_stripping_disable(vsi);
3232 
3233 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3234 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3235 					vid);
3236 }
3237 
3238 /**
3239  * i40e_vsi_add_pvid - Add pvid for the VSI
3240  * @vsi: the vsi being adjusted
3241  * @vid: the vlan id to set as a PVID
3242  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3243 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3244 {
3245 	struct i40e_vsi_context ctxt;
3246 	int ret;
3247 
3248 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3249 	vsi->info.pvid = cpu_to_le16(vid);
3250 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3251 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3252 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3253 
3254 	ctxt.seid = vsi->seid;
3255 	ctxt.info = vsi->info;
3256 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3257 	if (ret) {
3258 		dev_info(&vsi->back->pdev->dev,
3259 			 "add pvid failed, err %pe aq_err %s\n",
3260 			 ERR_PTR(ret),
3261 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3262 		return -ENOENT;
3263 	}
3264 
3265 	return 0;
3266 }
3267 
3268 /**
3269  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3270  * @vsi: the vsi being adjusted
3271  *
3272  * Just use the vlan_rx_register() service to put it back to normal
3273  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3274 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3275 {
3276 	vsi->info.pvid = 0;
3277 
3278 	i40e_vlan_stripping_disable(vsi);
3279 }
3280 
3281 /**
3282  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3283  * @vsi: ptr to the VSI
3284  *
3285  * If this function returns with an error, then it's possible one or
3286  * more of the rings is populated (while the rest are not).  It is the
3287  * callers duty to clean those orphaned rings.
3288  *
3289  * Return 0 on success, negative on failure
3290  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3291 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3292 {
3293 	int i, err = 0;
3294 
3295 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3296 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3297 
3298 	if (!i40e_enabled_xdp_vsi(vsi))
3299 		return err;
3300 
3301 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3302 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3303 
3304 	return err;
3305 }
3306 
3307 /**
3308  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3309  * @vsi: ptr to the VSI
3310  *
3311  * Free VSI's transmit software resources
3312  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3313 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3314 {
3315 	int i;
3316 
3317 	if (vsi->tx_rings) {
3318 		for (i = 0; i < vsi->num_queue_pairs; i++)
3319 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3320 				i40e_free_tx_resources(vsi->tx_rings[i]);
3321 	}
3322 
3323 	if (vsi->xdp_rings) {
3324 		for (i = 0; i < vsi->num_queue_pairs; i++)
3325 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3326 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3327 	}
3328 }
3329 
3330 /**
3331  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3332  * @vsi: ptr to the VSI
3333  *
3334  * If this function returns with an error, then it's possible one or
3335  * more of the rings is populated (while the rest are not).  It is the
3336  * callers duty to clean those orphaned rings.
3337  *
3338  * Return 0 on success, negative on failure
3339  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3340 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3341 {
3342 	int i, err = 0;
3343 
3344 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3345 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3346 	return err;
3347 }
3348 
3349 /**
3350  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3351  * @vsi: ptr to the VSI
3352  *
3353  * Free all receive software resources
3354  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3355 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3356 {
3357 	int i;
3358 
3359 	if (!vsi->rx_rings)
3360 		return;
3361 
3362 	for (i = 0; i < vsi->num_queue_pairs; i++)
3363 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3364 			i40e_free_rx_resources(vsi->rx_rings[i]);
3365 }
3366 
3367 /**
3368  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3369  * @ring: The Tx ring to configure
3370  *
3371  * This enables/disables XPS for a given Tx descriptor ring
3372  * based on the TCs enabled for the VSI that ring belongs to.
3373  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3374 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3375 {
3376 	int cpu;
3377 
3378 	if (!ring->q_vector || !ring->netdev || ring->ch)
3379 		return;
3380 
3381 	/* We only initialize XPS once, so as not to overwrite user settings */
3382 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3383 		return;
3384 
3385 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3386 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3387 			    ring->queue_index);
3388 }
3389 
3390 /**
3391  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3392  * @ring: The Tx or Rx ring
3393  *
3394  * Returns the AF_XDP buffer pool or NULL.
3395  **/
i40e_xsk_pool(struct i40e_ring * ring)3396 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3397 {
3398 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3399 	int qid = ring->queue_index;
3400 
3401 	if (ring_is_xdp(ring))
3402 		qid -= ring->vsi->alloc_queue_pairs;
3403 
3404 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3405 		return NULL;
3406 
3407 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3408 }
3409 
3410 /**
3411  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3412  * @ring: The Tx ring to configure
3413  *
3414  * Configure the Tx descriptor ring in the HMC context.
3415  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3416 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3417 {
3418 	struct i40e_vsi *vsi = ring->vsi;
3419 	u16 pf_q = vsi->base_queue + ring->queue_index;
3420 	struct i40e_hw *hw = &vsi->back->hw;
3421 	struct i40e_hmc_obj_txq tx_ctx;
3422 	u32 qtx_ctl = 0;
3423 	int err = 0;
3424 
3425 	if (ring_is_xdp(ring))
3426 		ring->xsk_pool = i40e_xsk_pool(ring);
3427 
3428 	/* some ATR related tx ring init */
3429 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3430 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3431 		ring->atr_count = 0;
3432 	} else {
3433 		ring->atr_sample_rate = 0;
3434 	}
3435 
3436 	/* configure XPS */
3437 	i40e_config_xps_tx_ring(ring);
3438 
3439 	/* clear the context structure first */
3440 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3441 
3442 	tx_ctx.new_context = 1;
3443 	tx_ctx.base = (ring->dma / 128);
3444 	tx_ctx.qlen = ring->count;
3445 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3446 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3447 		tx_ctx.fd_ena = 1;
3448 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3449 		tx_ctx.timesync_ena = 1;
3450 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3451 	if (vsi->type != I40E_VSI_FDIR)
3452 		tx_ctx.head_wb_ena = 1;
3453 	tx_ctx.head_wb_addr = ring->dma +
3454 			      (ring->count * sizeof(struct i40e_tx_desc));
3455 
3456 	/* As part of VSI creation/update, FW allocates certain
3457 	 * Tx arbitration queue sets for each TC enabled for
3458 	 * the VSI. The FW returns the handles to these queue
3459 	 * sets as part of the response buffer to Add VSI,
3460 	 * Update VSI, etc. AQ commands. It is expected that
3461 	 * these queue set handles be associated with the Tx
3462 	 * queues by the driver as part of the TX queue context
3463 	 * initialization. This has to be done regardless of
3464 	 * DCB as by default everything is mapped to TC0.
3465 	 */
3466 
3467 	if (ring->ch)
3468 		tx_ctx.rdylist =
3469 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3470 
3471 	else
3472 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3473 
3474 	tx_ctx.rdylist_act = 0;
3475 
3476 	/* clear the context in the HMC */
3477 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3478 	if (err) {
3479 		dev_info(&vsi->back->pdev->dev,
3480 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3481 			 ring->queue_index, pf_q, err);
3482 		return -ENOMEM;
3483 	}
3484 
3485 	/* set the context in the HMC */
3486 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3487 	if (err) {
3488 		dev_info(&vsi->back->pdev->dev,
3489 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3490 			 ring->queue_index, pf_q, err);
3491 		return -ENOMEM;
3492 	}
3493 
3494 	/* Now associate this queue with this PCI function */
3495 	if (ring->ch) {
3496 		if (ring->ch->type == I40E_VSI_VMDQ2)
3497 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3498 		else
3499 			return -EINVAL;
3500 
3501 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3502 				      ring->ch->vsi_number);
3503 	} else {
3504 		if (vsi->type == I40E_VSI_VMDQ2) {
3505 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3506 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3507 					      vsi->id);
3508 		} else {
3509 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3510 		}
3511 	}
3512 
3513 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3514 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3515 	i40e_flush(hw);
3516 
3517 	/* cache tail off for easier writes later */
3518 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3519 
3520 	return 0;
3521 }
3522 
3523 /**
3524  * i40e_rx_offset - Return expected offset into page to access data
3525  * @rx_ring: Ring we are requesting offset of
3526  *
3527  * Returns the offset value for ring into the data buffer.
3528  */
i40e_rx_offset(struct i40e_ring * rx_ring)3529 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3530 {
3531 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3532 }
3533 
3534 /**
3535  * i40e_configure_rx_ring - Configure a receive ring context
3536  * @ring: The Rx ring to configure
3537  *
3538  * Configure the Rx descriptor ring in the HMC context.
3539  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3540 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3541 {
3542 	struct i40e_vsi *vsi = ring->vsi;
3543 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3544 	u16 pf_q = vsi->base_queue + ring->queue_index;
3545 	struct i40e_hw *hw = &vsi->back->hw;
3546 	struct i40e_hmc_obj_rxq rx_ctx;
3547 	int err = 0;
3548 	bool ok;
3549 
3550 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3551 
3552 	/* clear the context structure first */
3553 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3554 
3555 	ring->rx_buf_len = vsi->rx_buf_len;
3556 
3557 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3558 	if (ring->vsi->type != I40E_VSI_MAIN)
3559 		goto skip;
3560 
3561 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3562 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3563 					 ring->queue_index,
3564 					 ring->q_vector->napi.napi_id,
3565 					 ring->rx_buf_len);
3566 		if (err)
3567 			return err;
3568 	}
3569 
3570 	ring->xsk_pool = i40e_xsk_pool(ring);
3571 	if (ring->xsk_pool) {
3572 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3573 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3574 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3575 					 ring->queue_index,
3576 					 ring->q_vector->napi.napi_id,
3577 					 ring->rx_buf_len);
3578 		if (err)
3579 			return err;
3580 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3581 						 MEM_TYPE_XSK_BUFF_POOL,
3582 						 NULL);
3583 		if (err)
3584 			return err;
3585 		dev_info(&vsi->back->pdev->dev,
3586 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3587 			 ring->queue_index);
3588 
3589 	} else {
3590 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3591 						 MEM_TYPE_PAGE_SHARED,
3592 						 NULL);
3593 		if (err)
3594 			return err;
3595 	}
3596 
3597 skip:
3598 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3599 
3600 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3601 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3602 
3603 	rx_ctx.base = (ring->dma / 128);
3604 	rx_ctx.qlen = ring->count;
3605 
3606 	/* use 16 byte descriptors */
3607 	rx_ctx.dsize = 0;
3608 
3609 	/* descriptor type is always zero
3610 	 * rx_ctx.dtype = 0;
3611 	 */
3612 	rx_ctx.hsplit_0 = 0;
3613 
3614 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3615 	if (hw->revision_id == 0)
3616 		rx_ctx.lrxqthresh = 0;
3617 	else
3618 		rx_ctx.lrxqthresh = 1;
3619 	rx_ctx.crcstrip = 1;
3620 	rx_ctx.l2tsel = 1;
3621 	/* this controls whether VLAN is stripped from inner headers */
3622 	rx_ctx.showiv = 0;
3623 	/* set the prefena field to 1 because the manual says to */
3624 	rx_ctx.prefena = 1;
3625 
3626 	/* clear the context in the HMC */
3627 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3628 	if (err) {
3629 		dev_info(&vsi->back->pdev->dev,
3630 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3631 			 ring->queue_index, pf_q, err);
3632 		return -ENOMEM;
3633 	}
3634 
3635 	/* set the context in the HMC */
3636 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3637 	if (err) {
3638 		dev_info(&vsi->back->pdev->dev,
3639 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3640 			 ring->queue_index, pf_q, err);
3641 		return -ENOMEM;
3642 	}
3643 
3644 	/* configure Rx buffer alignment */
3645 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3646 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3647 			dev_info(&vsi->back->pdev->dev,
3648 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3649 			return -EOPNOTSUPP;
3650 		}
3651 		clear_ring_build_skb_enabled(ring);
3652 	} else {
3653 		set_ring_build_skb_enabled(ring);
3654 	}
3655 
3656 	ring->rx_offset = i40e_rx_offset(ring);
3657 
3658 	/* cache tail for quicker writes, and clear the reg before use */
3659 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3660 	writel(0, ring->tail);
3661 
3662 	if (ring->xsk_pool) {
3663 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3664 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3665 	} else {
3666 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3667 	}
3668 	if (!ok) {
3669 		/* Log this in case the user has forgotten to give the kernel
3670 		 * any buffers, even later in the application.
3671 		 */
3672 		dev_info(&vsi->back->pdev->dev,
3673 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3674 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3675 			 ring->queue_index, pf_q);
3676 	}
3677 
3678 	return 0;
3679 }
3680 
3681 /**
3682  * i40e_vsi_configure_tx - Configure the VSI for Tx
3683  * @vsi: VSI structure describing this set of rings and resources
3684  *
3685  * Configure the Tx VSI for operation.
3686  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3687 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3688 {
3689 	int err = 0;
3690 	u16 i;
3691 
3692 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3693 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3694 
3695 	if (err || !i40e_enabled_xdp_vsi(vsi))
3696 		return err;
3697 
3698 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3699 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3700 
3701 	return err;
3702 }
3703 
3704 /**
3705  * i40e_vsi_configure_rx - Configure the VSI for Rx
3706  * @vsi: the VSI being configured
3707  *
3708  * Configure the Rx VSI for operation.
3709  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3710 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3711 {
3712 	int err = 0;
3713 	u16 i;
3714 
3715 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3716 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3717 
3718 #if (PAGE_SIZE < 8192)
3719 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3720 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3721 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3722 		vsi->max_frame = vsi->rx_buf_len;
3723 	}
3724 #endif
3725 
3726 	/* set up individual rings */
3727 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3728 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3729 
3730 	return err;
3731 }
3732 
3733 /**
3734  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3735  * @vsi: ptr to the VSI
3736  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3737 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3738 {
3739 	struct i40e_ring *tx_ring, *rx_ring;
3740 	u16 qoffset, qcount;
3741 	int i, n;
3742 
3743 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3744 		/* Reset the TC information */
3745 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3746 			rx_ring = vsi->rx_rings[i];
3747 			tx_ring = vsi->tx_rings[i];
3748 			rx_ring->dcb_tc = 0;
3749 			tx_ring->dcb_tc = 0;
3750 		}
3751 		return;
3752 	}
3753 
3754 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3755 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3756 			continue;
3757 
3758 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3759 		qcount = vsi->tc_config.tc_info[n].qcount;
3760 		for (i = qoffset; i < (qoffset + qcount); i++) {
3761 			rx_ring = vsi->rx_rings[i];
3762 			tx_ring = vsi->tx_rings[i];
3763 			rx_ring->dcb_tc = n;
3764 			tx_ring->dcb_tc = n;
3765 		}
3766 	}
3767 }
3768 
3769 /**
3770  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3771  * @vsi: ptr to the VSI
3772  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3773 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3774 {
3775 	if (vsi->netdev)
3776 		i40e_set_rx_mode(vsi->netdev);
3777 }
3778 
3779 /**
3780  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3781  * @pf: Pointer to the targeted PF
3782  *
3783  * Set all flow director counters to 0.
3784  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3785 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3786 {
3787 	pf->fd_tcp4_filter_cnt = 0;
3788 	pf->fd_udp4_filter_cnt = 0;
3789 	pf->fd_sctp4_filter_cnt = 0;
3790 	pf->fd_ip4_filter_cnt = 0;
3791 	pf->fd_tcp6_filter_cnt = 0;
3792 	pf->fd_udp6_filter_cnt = 0;
3793 	pf->fd_sctp6_filter_cnt = 0;
3794 	pf->fd_ip6_filter_cnt = 0;
3795 }
3796 
3797 /**
3798  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3799  * @vsi: Pointer to the targeted VSI
3800  *
3801  * This function replays the hlist on the hw where all the SB Flow Director
3802  * filters were saved.
3803  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3804 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3805 {
3806 	struct i40e_fdir_filter *filter;
3807 	struct i40e_pf *pf = vsi->back;
3808 	struct hlist_node *node;
3809 
3810 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3811 		return;
3812 
3813 	/* Reset FDir counters as we're replaying all existing filters */
3814 	i40e_reset_fdir_filter_cnt(pf);
3815 
3816 	hlist_for_each_entry_safe(filter, node,
3817 				  &pf->fdir_filter_list, fdir_node) {
3818 		i40e_add_del_fdir(vsi, filter, true);
3819 	}
3820 }
3821 
3822 /**
3823  * i40e_vsi_configure - Set up the VSI for action
3824  * @vsi: the VSI being configured
3825  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3826 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3827 {
3828 	int err;
3829 
3830 	i40e_set_vsi_rx_mode(vsi);
3831 	i40e_restore_vlan(vsi);
3832 	i40e_vsi_config_dcb_rings(vsi);
3833 	err = i40e_vsi_configure_tx(vsi);
3834 	if (!err)
3835 		err = i40e_vsi_configure_rx(vsi);
3836 
3837 	return err;
3838 }
3839 
3840 /**
3841  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3842  * @vsi: the VSI being configured
3843  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3844 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3845 {
3846 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3847 	struct i40e_pf *pf = vsi->back;
3848 	struct i40e_hw *hw = &pf->hw;
3849 	u16 vector;
3850 	int i, q;
3851 	u32 qp;
3852 
3853 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3854 	 * and PFINT_LNKLSTn registers, e.g.:
3855 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3856 	 */
3857 	qp = vsi->base_queue;
3858 	vector = vsi->base_vector;
3859 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3860 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3861 
3862 		q_vector->rx.next_update = jiffies + 1;
3863 		q_vector->rx.target_itr =
3864 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3865 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3866 		     q_vector->rx.target_itr >> 1);
3867 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3868 
3869 		q_vector->tx.next_update = jiffies + 1;
3870 		q_vector->tx.target_itr =
3871 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3872 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3873 		     q_vector->tx.target_itr >> 1);
3874 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3875 
3876 		/* Set ITR for software interrupts triggered after exiting
3877 		 * busy-loop polling.
3878 		 */
3879 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3880 		     I40E_ITR_20K);
3881 
3882 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3883 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3884 
3885 		/* begin of linked list for RX queue assigned to this vector */
3886 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3887 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3888 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3889 			u32 val;
3890 
3891 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3892 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3893 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3894 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3895 			      (I40E_QUEUE_TYPE_TX <<
3896 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3897 
3898 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3899 
3900 			if (has_xdp) {
3901 				/* TX queue with next queue set to TX */
3902 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3903 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3904 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3905 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3906 				      (I40E_QUEUE_TYPE_TX <<
3907 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3908 
3909 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3910 			}
3911 			/* TX queue with next RX or end of linked list */
3912 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3913 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3914 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3915 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3916 			      (I40E_QUEUE_TYPE_RX <<
3917 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3918 
3919 			/* Terminate the linked list */
3920 			if (q == (q_vector->num_ringpairs - 1))
3921 				val |= (I40E_QUEUE_END_OF_LIST <<
3922 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3923 
3924 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3925 			qp++;
3926 		}
3927 	}
3928 
3929 	i40e_flush(hw);
3930 }
3931 
3932 /**
3933  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3934  * @pf: pointer to private device data structure
3935  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3936 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3937 {
3938 	struct i40e_hw *hw = &pf->hw;
3939 	u32 val;
3940 
3941 	/* clear things first */
3942 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3943 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3944 
3945 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3946 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3947 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3948 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3949 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3950 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3951 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3952 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3953 
3954 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3955 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3956 
3957 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3958 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3959 
3960 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3961 
3962 	/* SW_ITR_IDX = 0, but don't change INTENA */
3963 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3964 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3965 
3966 	/* OTHER_ITR_IDX = 0 */
3967 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3968 }
3969 
3970 /**
3971  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3972  * @vsi: the VSI being configured
3973  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3974 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3975 {
3976 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3977 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3978 	struct i40e_pf *pf = vsi->back;
3979 	struct i40e_hw *hw = &pf->hw;
3980 
3981 	/* set the ITR configuration */
3982 	q_vector->rx.next_update = jiffies + 1;
3983 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3984 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3985 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3986 	q_vector->tx.next_update = jiffies + 1;
3987 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3988 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3989 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3990 
3991 	i40e_enable_misc_int_causes(pf);
3992 
3993 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3994 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3995 
3996 	/* Associate the queue pair to the vector and enable the queue
3997 	 * interrupt RX queue in linked list with next queue set to TX
3998 	 */
3999 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4000 
4001 	if (i40e_enabled_xdp_vsi(vsi)) {
4002 		/* TX queue in linked list with next queue set to TX */
4003 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4004 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4005 	}
4006 
4007 	/* last TX queue so the next RX queue doesn't matter */
4008 	wr32(hw, I40E_QINT_TQCTL(0),
4009 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4010 	i40e_flush(hw);
4011 }
4012 
4013 /**
4014  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4015  * @pf: board private structure
4016  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4017 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4018 {
4019 	struct i40e_hw *hw = &pf->hw;
4020 
4021 	wr32(hw, I40E_PFINT_DYN_CTL0,
4022 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4023 	i40e_flush(hw);
4024 }
4025 
4026 /**
4027  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4028  * @pf: board private structure
4029  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4030 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4031 {
4032 	struct i40e_hw *hw = &pf->hw;
4033 	u32 val;
4034 
4035 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4036 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4037 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4038 
4039 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4040 	i40e_flush(hw);
4041 }
4042 
4043 /**
4044  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4045  * @irq: interrupt number
4046  * @data: pointer to a q_vector
4047  **/
i40e_msix_clean_rings(int irq,void * data)4048 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4049 {
4050 	struct i40e_q_vector *q_vector = data;
4051 
4052 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4053 		return IRQ_HANDLED;
4054 
4055 	napi_schedule_irqoff(&q_vector->napi);
4056 
4057 	return IRQ_HANDLED;
4058 }
4059 
4060 /**
4061  * i40e_irq_affinity_notify - Callback for affinity changes
4062  * @notify: context as to what irq was changed
4063  * @mask: the new affinity mask
4064  *
4065  * This is a callback function used by the irq_set_affinity_notifier function
4066  * so that we may register to receive changes to the irq affinity masks.
4067  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4068 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4069 				     const cpumask_t *mask)
4070 {
4071 	struct i40e_q_vector *q_vector =
4072 		container_of(notify, struct i40e_q_vector, affinity_notify);
4073 
4074 	cpumask_copy(&q_vector->affinity_mask, mask);
4075 }
4076 
4077 /**
4078  * i40e_irq_affinity_release - Callback for affinity notifier release
4079  * @ref: internal core kernel usage
4080  *
4081  * This is a callback function used by the irq_set_affinity_notifier function
4082  * to inform the current notification subscriber that they will no longer
4083  * receive notifications.
4084  **/
i40e_irq_affinity_release(struct kref * ref)4085 static void i40e_irq_affinity_release(struct kref *ref) {}
4086 
4087 /**
4088  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4089  * @vsi: the VSI being configured
4090  * @basename: name for the vector
4091  *
4092  * Allocates MSI-X vectors and requests interrupts from the kernel.
4093  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4094 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4095 {
4096 	int q_vectors = vsi->num_q_vectors;
4097 	struct i40e_pf *pf = vsi->back;
4098 	int base = vsi->base_vector;
4099 	int rx_int_idx = 0;
4100 	int tx_int_idx = 0;
4101 	int vector, err;
4102 	int irq_num;
4103 	int cpu;
4104 
4105 	for (vector = 0; vector < q_vectors; vector++) {
4106 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4107 
4108 		irq_num = pf->msix_entries[base + vector].vector;
4109 
4110 		if (q_vector->tx.ring && q_vector->rx.ring) {
4111 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4112 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4113 			tx_int_idx++;
4114 		} else if (q_vector->rx.ring) {
4115 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4116 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4117 		} else if (q_vector->tx.ring) {
4118 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4119 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4120 		} else {
4121 			/* skip this unused q_vector */
4122 			continue;
4123 		}
4124 		err = request_irq(irq_num,
4125 				  vsi->irq_handler,
4126 				  0,
4127 				  q_vector->name,
4128 				  q_vector);
4129 		if (err) {
4130 			dev_info(&pf->pdev->dev,
4131 				 "MSIX request_irq failed, error: %d\n", err);
4132 			goto free_queue_irqs;
4133 		}
4134 
4135 		/* register for affinity change notifications */
4136 		q_vector->irq_num = irq_num;
4137 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4138 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4139 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4140 		/* Spread affinity hints out across online CPUs.
4141 		 *
4142 		 * get_cpu_mask returns a static constant mask with
4143 		 * a permanent lifetime so it's ok to pass to
4144 		 * irq_update_affinity_hint without making a copy.
4145 		 */
4146 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4147 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4148 	}
4149 
4150 	vsi->irqs_ready = true;
4151 	return 0;
4152 
4153 free_queue_irqs:
4154 	while (vector) {
4155 		vector--;
4156 		irq_num = pf->msix_entries[base + vector].vector;
4157 		irq_set_affinity_notifier(irq_num, NULL);
4158 		irq_update_affinity_hint(irq_num, NULL);
4159 		free_irq(irq_num, &vsi->q_vectors[vector]);
4160 	}
4161 	return err;
4162 }
4163 
4164 /**
4165  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4166  * @vsi: the VSI being un-configured
4167  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4168 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4169 {
4170 	struct i40e_pf *pf = vsi->back;
4171 	struct i40e_hw *hw = &pf->hw;
4172 	int base = vsi->base_vector;
4173 	int i;
4174 
4175 	/* disable interrupt causation from each queue */
4176 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4177 		u32 val;
4178 
4179 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4180 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4181 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4182 
4183 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4184 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4185 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4186 
4187 		if (!i40e_enabled_xdp_vsi(vsi))
4188 			continue;
4189 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4190 	}
4191 
4192 	/* disable each interrupt */
4193 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4194 		for (i = vsi->base_vector;
4195 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4196 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4197 
4198 		i40e_flush(hw);
4199 		for (i = 0; i < vsi->num_q_vectors; i++)
4200 			synchronize_irq(pf->msix_entries[i + base].vector);
4201 	} else {
4202 		/* Legacy and MSI mode - this stops all interrupt handling */
4203 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4204 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4205 		i40e_flush(hw);
4206 		synchronize_irq(pf->pdev->irq);
4207 	}
4208 }
4209 
4210 /**
4211  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4212  * @vsi: the VSI being configured
4213  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4214 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4215 {
4216 	struct i40e_pf *pf = vsi->back;
4217 	int i;
4218 
4219 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4220 		for (i = 0; i < vsi->num_q_vectors; i++)
4221 			i40e_irq_dynamic_enable(vsi, i);
4222 	} else {
4223 		i40e_irq_dynamic_enable_icr0(pf);
4224 	}
4225 
4226 	i40e_flush(&pf->hw);
4227 	return 0;
4228 }
4229 
4230 /**
4231  * i40e_free_misc_vector - Free the vector that handles non-queue events
4232  * @pf: board private structure
4233  **/
i40e_free_misc_vector(struct i40e_pf * pf)4234 static void i40e_free_misc_vector(struct i40e_pf *pf)
4235 {
4236 	/* Disable ICR 0 */
4237 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4238 	i40e_flush(&pf->hw);
4239 
4240 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4241 		free_irq(pf->msix_entries[0].vector, pf);
4242 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4243 	}
4244 }
4245 
4246 /**
4247  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4248  * @irq: interrupt number
4249  * @data: pointer to a q_vector
4250  *
4251  * This is the handler used for all MSI/Legacy interrupts, and deals
4252  * with both queue and non-queue interrupts.  This is also used in
4253  * MSIX mode to handle the non-queue interrupts.
4254  **/
i40e_intr(int irq,void * data)4255 static irqreturn_t i40e_intr(int irq, void *data)
4256 {
4257 	struct i40e_pf *pf = (struct i40e_pf *)data;
4258 	struct i40e_hw *hw = &pf->hw;
4259 	irqreturn_t ret = IRQ_NONE;
4260 	u32 icr0, icr0_remaining;
4261 	u32 val, ena_mask;
4262 
4263 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4264 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4265 
4266 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4267 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4268 		goto enable_intr;
4269 
4270 	/* if interrupt but no bits showing, must be SWINT */
4271 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4272 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4273 		pf->sw_int_count++;
4274 
4275 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4276 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4277 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4278 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4279 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4280 	}
4281 
4282 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4283 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4284 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4285 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4286 
4287 		/* We do not have a way to disarm Queue causes while leaving
4288 		 * interrupt enabled for all other causes, ideally
4289 		 * interrupt should be disabled while we are in NAPI but
4290 		 * this is not a performance path and napi_schedule()
4291 		 * can deal with rescheduling.
4292 		 */
4293 		if (!test_bit(__I40E_DOWN, pf->state))
4294 			napi_schedule_irqoff(&q_vector->napi);
4295 	}
4296 
4297 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4298 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4299 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4300 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4301 	}
4302 
4303 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4304 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4305 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4306 	}
4307 
4308 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4309 		/* disable any further VFLR event notifications */
4310 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4311 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4312 
4313 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4314 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4315 		} else {
4316 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4317 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4318 		}
4319 	}
4320 
4321 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4322 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4323 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4324 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4325 		val = rd32(hw, I40E_GLGEN_RSTAT);
4326 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4327 		if (val == I40E_RESET_CORER) {
4328 			pf->corer_count++;
4329 		} else if (val == I40E_RESET_GLOBR) {
4330 			pf->globr_count++;
4331 		} else if (val == I40E_RESET_EMPR) {
4332 			pf->empr_count++;
4333 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4334 		}
4335 	}
4336 
4337 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4338 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4339 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4340 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4341 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4342 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4343 	}
4344 
4345 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4346 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4347 
4348 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4349 			schedule_work(&pf->ptp_extts0_work);
4350 
4351 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4352 			i40e_ptp_tx_hwtstamp(pf);
4353 
4354 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4355 	}
4356 
4357 	/* If a critical error is pending we have no choice but to reset the
4358 	 * device.
4359 	 * Report and mask out any remaining unexpected interrupts.
4360 	 */
4361 	icr0_remaining = icr0 & ena_mask;
4362 	if (icr0_remaining) {
4363 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4364 			 icr0_remaining);
4365 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4366 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4367 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4368 			dev_info(&pf->pdev->dev, "device will be reset\n");
4369 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4370 			i40e_service_event_schedule(pf);
4371 		}
4372 		ena_mask &= ~icr0_remaining;
4373 	}
4374 	ret = IRQ_HANDLED;
4375 
4376 enable_intr:
4377 	/* re-enable interrupt causes */
4378 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4379 	if (!test_bit(__I40E_DOWN, pf->state) ||
4380 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4381 		i40e_service_event_schedule(pf);
4382 		i40e_irq_dynamic_enable_icr0(pf);
4383 	}
4384 
4385 	return ret;
4386 }
4387 
4388 /**
4389  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4390  * @tx_ring:  tx ring to clean
4391  * @budget:   how many cleans we're allowed
4392  *
4393  * Returns true if there's any budget left (e.g. the clean is finished)
4394  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4395 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4396 {
4397 	struct i40e_vsi *vsi = tx_ring->vsi;
4398 	u16 i = tx_ring->next_to_clean;
4399 	struct i40e_tx_buffer *tx_buf;
4400 	struct i40e_tx_desc *tx_desc;
4401 
4402 	tx_buf = &tx_ring->tx_bi[i];
4403 	tx_desc = I40E_TX_DESC(tx_ring, i);
4404 	i -= tx_ring->count;
4405 
4406 	do {
4407 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4408 
4409 		/* if next_to_watch is not set then there is no work pending */
4410 		if (!eop_desc)
4411 			break;
4412 
4413 		/* prevent any other reads prior to eop_desc */
4414 		smp_rmb();
4415 
4416 		/* if the descriptor isn't done, no work yet to do */
4417 		if (!(eop_desc->cmd_type_offset_bsz &
4418 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4419 			break;
4420 
4421 		/* clear next_to_watch to prevent false hangs */
4422 		tx_buf->next_to_watch = NULL;
4423 
4424 		tx_desc->buffer_addr = 0;
4425 		tx_desc->cmd_type_offset_bsz = 0;
4426 		/* move past filter desc */
4427 		tx_buf++;
4428 		tx_desc++;
4429 		i++;
4430 		if (unlikely(!i)) {
4431 			i -= tx_ring->count;
4432 			tx_buf = tx_ring->tx_bi;
4433 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4434 		}
4435 		/* unmap skb header data */
4436 		dma_unmap_single(tx_ring->dev,
4437 				 dma_unmap_addr(tx_buf, dma),
4438 				 dma_unmap_len(tx_buf, len),
4439 				 DMA_TO_DEVICE);
4440 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4441 			kfree(tx_buf->raw_buf);
4442 
4443 		tx_buf->raw_buf = NULL;
4444 		tx_buf->tx_flags = 0;
4445 		tx_buf->next_to_watch = NULL;
4446 		dma_unmap_len_set(tx_buf, len, 0);
4447 		tx_desc->buffer_addr = 0;
4448 		tx_desc->cmd_type_offset_bsz = 0;
4449 
4450 		/* move us past the eop_desc for start of next FD desc */
4451 		tx_buf++;
4452 		tx_desc++;
4453 		i++;
4454 		if (unlikely(!i)) {
4455 			i -= tx_ring->count;
4456 			tx_buf = tx_ring->tx_bi;
4457 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4458 		}
4459 
4460 		/* update budget accounting */
4461 		budget--;
4462 	} while (likely(budget));
4463 
4464 	i += tx_ring->count;
4465 	tx_ring->next_to_clean = i;
4466 
4467 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4468 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4469 
4470 	return budget > 0;
4471 }
4472 
4473 /**
4474  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4475  * @irq: interrupt number
4476  * @data: pointer to a q_vector
4477  **/
i40e_fdir_clean_ring(int irq,void * data)4478 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4479 {
4480 	struct i40e_q_vector *q_vector = data;
4481 	struct i40e_vsi *vsi;
4482 
4483 	if (!q_vector->tx.ring)
4484 		return IRQ_HANDLED;
4485 
4486 	vsi = q_vector->tx.ring->vsi;
4487 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4488 
4489 	return IRQ_HANDLED;
4490 }
4491 
4492 /**
4493  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4494  * @vsi: the VSI being configured
4495  * @v_idx: vector index
4496  * @qp_idx: queue pair index
4497  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4498 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4499 {
4500 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4501 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4502 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4503 
4504 	tx_ring->q_vector = q_vector;
4505 	tx_ring->next = q_vector->tx.ring;
4506 	q_vector->tx.ring = tx_ring;
4507 	q_vector->tx.count++;
4508 
4509 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4510 	if (i40e_enabled_xdp_vsi(vsi)) {
4511 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4512 
4513 		xdp_ring->q_vector = q_vector;
4514 		xdp_ring->next = q_vector->tx.ring;
4515 		q_vector->tx.ring = xdp_ring;
4516 		q_vector->tx.count++;
4517 	}
4518 
4519 	rx_ring->q_vector = q_vector;
4520 	rx_ring->next = q_vector->rx.ring;
4521 	q_vector->rx.ring = rx_ring;
4522 	q_vector->rx.count++;
4523 }
4524 
4525 /**
4526  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4527  * @vsi: the VSI being configured
4528  *
4529  * This function maps descriptor rings to the queue-specific vectors
4530  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4531  * one vector per queue pair, but on a constrained vector budget, we
4532  * group the queue pairs as "efficiently" as possible.
4533  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4534 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4535 {
4536 	int qp_remaining = vsi->num_queue_pairs;
4537 	int q_vectors = vsi->num_q_vectors;
4538 	int num_ringpairs;
4539 	int v_start = 0;
4540 	int qp_idx = 0;
4541 
4542 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4543 	 * group them so there are multiple queues per vector.
4544 	 * It is also important to go through all the vectors available to be
4545 	 * sure that if we don't use all the vectors, that the remaining vectors
4546 	 * are cleared. This is especially important when decreasing the
4547 	 * number of queues in use.
4548 	 */
4549 	for (; v_start < q_vectors; v_start++) {
4550 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4551 
4552 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4553 
4554 		q_vector->num_ringpairs = num_ringpairs;
4555 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4556 
4557 		q_vector->rx.count = 0;
4558 		q_vector->tx.count = 0;
4559 		q_vector->rx.ring = NULL;
4560 		q_vector->tx.ring = NULL;
4561 
4562 		while (num_ringpairs--) {
4563 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4564 			qp_idx++;
4565 			qp_remaining--;
4566 		}
4567 	}
4568 }
4569 
4570 /**
4571  * i40e_vsi_request_irq - Request IRQ from the OS
4572  * @vsi: the VSI being configured
4573  * @basename: name for the vector
4574  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4575 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4576 {
4577 	struct i40e_pf *pf = vsi->back;
4578 	int err;
4579 
4580 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4581 		err = i40e_vsi_request_irq_msix(vsi, basename);
4582 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4583 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4584 				  pf->int_name, pf);
4585 	else
4586 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4587 				  pf->int_name, pf);
4588 
4589 	if (err)
4590 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4591 
4592 	return err;
4593 }
4594 
4595 #ifdef CONFIG_NET_POLL_CONTROLLER
4596 /**
4597  * i40e_netpoll - A Polling 'interrupt' handler
4598  * @netdev: network interface device structure
4599  *
4600  * This is used by netconsole to send skbs without having to re-enable
4601  * interrupts.  It's not called while the normal interrupt routine is executing.
4602  **/
i40e_netpoll(struct net_device * netdev)4603 static void i40e_netpoll(struct net_device *netdev)
4604 {
4605 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4606 	struct i40e_vsi *vsi = np->vsi;
4607 	struct i40e_pf *pf = vsi->back;
4608 	int i;
4609 
4610 	/* if interface is down do nothing */
4611 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4612 		return;
4613 
4614 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4615 		for (i = 0; i < vsi->num_q_vectors; i++)
4616 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4617 	} else {
4618 		i40e_intr(pf->pdev->irq, netdev);
4619 	}
4620 }
4621 #endif
4622 
4623 #define I40E_QTX_ENA_WAIT_COUNT 50
4624 
4625 /**
4626  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4627  * @pf: the PF being configured
4628  * @pf_q: the PF queue
4629  * @enable: enable or disable state of the queue
4630  *
4631  * This routine will wait for the given Tx queue of the PF to reach the
4632  * enabled or disabled state.
4633  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4634  * multiple retries; else will return 0 in case of success.
4635  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4636 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4637 {
4638 	int i;
4639 	u32 tx_reg;
4640 
4641 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4642 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4643 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4644 			break;
4645 
4646 		usleep_range(10, 20);
4647 	}
4648 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4649 		return -ETIMEDOUT;
4650 
4651 	return 0;
4652 }
4653 
4654 /**
4655  * i40e_control_tx_q - Start or stop a particular Tx queue
4656  * @pf: the PF structure
4657  * @pf_q: the PF queue to configure
4658  * @enable: start or stop the queue
4659  *
4660  * This function enables or disables a single queue. Note that any delay
4661  * required after the operation is expected to be handled by the caller of
4662  * this function.
4663  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4664 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4665 {
4666 	struct i40e_hw *hw = &pf->hw;
4667 	u32 tx_reg;
4668 	int i;
4669 
4670 	/* warn the TX unit of coming changes */
4671 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4672 	if (!enable)
4673 		usleep_range(10, 20);
4674 
4675 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4676 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4677 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4678 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4679 			break;
4680 		usleep_range(1000, 2000);
4681 	}
4682 
4683 	/* Skip if the queue is already in the requested state */
4684 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4685 		return;
4686 
4687 	/* turn on/off the queue */
4688 	if (enable) {
4689 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4690 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4691 	} else {
4692 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4693 	}
4694 
4695 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4696 }
4697 
4698 /**
4699  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4700  * @seid: VSI SEID
4701  * @pf: the PF structure
4702  * @pf_q: the PF queue to configure
4703  * @is_xdp: true if the queue is used for XDP
4704  * @enable: start or stop the queue
4705  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4706 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4707 			   bool is_xdp, bool enable)
4708 {
4709 	int ret;
4710 
4711 	i40e_control_tx_q(pf, pf_q, enable);
4712 
4713 	/* wait for the change to finish */
4714 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4715 	if (ret) {
4716 		dev_info(&pf->pdev->dev,
4717 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4718 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4719 			 (enable ? "en" : "dis"));
4720 	}
4721 
4722 	return ret;
4723 }
4724 
4725 /**
4726  * i40e_vsi_enable_tx - Start a VSI's rings
4727  * @vsi: the VSI being configured
4728  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4729 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4730 {
4731 	struct i40e_pf *pf = vsi->back;
4732 	int i, pf_q, ret = 0;
4733 
4734 	pf_q = vsi->base_queue;
4735 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4736 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4737 					     pf_q,
4738 					     false /*is xdp*/, true);
4739 		if (ret)
4740 			break;
4741 
4742 		if (!i40e_enabled_xdp_vsi(vsi))
4743 			continue;
4744 
4745 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4746 					     pf_q + vsi->alloc_queue_pairs,
4747 					     true /*is xdp*/, true);
4748 		if (ret)
4749 			break;
4750 	}
4751 	return ret;
4752 }
4753 
4754 /**
4755  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4756  * @pf: the PF being configured
4757  * @pf_q: the PF queue
4758  * @enable: enable or disable state of the queue
4759  *
4760  * This routine will wait for the given Rx queue of the PF to reach the
4761  * enabled or disabled state.
4762  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4763  * multiple retries; else will return 0 in case of success.
4764  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4765 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4766 {
4767 	int i;
4768 	u32 rx_reg;
4769 
4770 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4771 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4772 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4773 			break;
4774 
4775 		usleep_range(10, 20);
4776 	}
4777 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4778 		return -ETIMEDOUT;
4779 
4780 	return 0;
4781 }
4782 
4783 /**
4784  * i40e_control_rx_q - Start or stop a particular Rx queue
4785  * @pf: the PF structure
4786  * @pf_q: the PF queue to configure
4787  * @enable: start or stop the queue
4788  *
4789  * This function enables or disables a single queue. Note that
4790  * any delay required after the operation is expected to be
4791  * handled by the caller of this function.
4792  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4793 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4794 {
4795 	struct i40e_hw *hw = &pf->hw;
4796 	u32 rx_reg;
4797 	int i;
4798 
4799 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4800 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4801 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4802 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4803 			break;
4804 		usleep_range(1000, 2000);
4805 	}
4806 
4807 	/* Skip if the queue is already in the requested state */
4808 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4809 		return;
4810 
4811 	/* turn on/off the queue */
4812 	if (enable)
4813 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4814 	else
4815 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4816 
4817 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4818 }
4819 
4820 /**
4821  * i40e_control_wait_rx_q
4822  * @pf: the PF structure
4823  * @pf_q: queue being configured
4824  * @enable: start or stop the rings
4825  *
4826  * This function enables or disables a single queue along with waiting
4827  * for the change to finish. The caller of this function should handle
4828  * the delays needed in the case of disabling queues.
4829  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4830 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4831 {
4832 	int ret = 0;
4833 
4834 	i40e_control_rx_q(pf, pf_q, enable);
4835 
4836 	/* wait for the change to finish */
4837 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4838 	if (ret)
4839 		return ret;
4840 
4841 	return ret;
4842 }
4843 
4844 /**
4845  * i40e_vsi_enable_rx - Start a VSI's rings
4846  * @vsi: the VSI being configured
4847  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4848 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4849 {
4850 	struct i40e_pf *pf = vsi->back;
4851 	int i, pf_q, ret = 0;
4852 
4853 	pf_q = vsi->base_queue;
4854 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4855 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4856 		if (ret) {
4857 			dev_info(&pf->pdev->dev,
4858 				 "VSI seid %d Rx ring %d enable timeout\n",
4859 				 vsi->seid, pf_q);
4860 			break;
4861 		}
4862 	}
4863 
4864 	return ret;
4865 }
4866 
4867 /**
4868  * i40e_vsi_start_rings - Start a VSI's rings
4869  * @vsi: the VSI being configured
4870  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4871 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4872 {
4873 	int ret = 0;
4874 
4875 	/* do rx first for enable and last for disable */
4876 	ret = i40e_vsi_enable_rx(vsi);
4877 	if (ret)
4878 		return ret;
4879 	ret = i40e_vsi_enable_tx(vsi);
4880 
4881 	return ret;
4882 }
4883 
4884 #define I40E_DISABLE_TX_GAP_MSEC	50
4885 
4886 /**
4887  * i40e_vsi_stop_rings - Stop a VSI's rings
4888  * @vsi: the VSI being configured
4889  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4890 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4891 {
4892 	struct i40e_pf *pf = vsi->back;
4893 	u32 pf_q, tx_q_end, rx_q_end;
4894 
4895 	/* When port TX is suspended, don't wait */
4896 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4897 		return i40e_vsi_stop_rings_no_wait(vsi);
4898 
4899 	tx_q_end = vsi->base_queue +
4900 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4901 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4902 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4903 
4904 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4905 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4906 		i40e_control_rx_q(pf, pf_q, false);
4907 
4908 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4909 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4910 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4911 
4912 	i40e_vsi_wait_queues_disabled(vsi);
4913 }
4914 
4915 /**
4916  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4917  * @vsi: the VSI being shutdown
4918  *
4919  * This function stops all the rings for a VSI but does not delay to verify
4920  * that rings have been disabled. It is expected that the caller is shutting
4921  * down multiple VSIs at once and will delay together for all the VSIs after
4922  * initiating the shutdown. This is particularly useful for shutting down lots
4923  * of VFs together. Otherwise, a large delay can be incurred while configuring
4924  * each VSI in serial.
4925  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4926 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4927 {
4928 	struct i40e_pf *pf = vsi->back;
4929 	int i, pf_q;
4930 
4931 	pf_q = vsi->base_queue;
4932 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4933 		i40e_control_tx_q(pf, pf_q, false);
4934 		i40e_control_rx_q(pf, pf_q, false);
4935 	}
4936 }
4937 
4938 /**
4939  * i40e_vsi_free_irq - Free the irq association with the OS
4940  * @vsi: the VSI being configured
4941  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4942 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4943 {
4944 	struct i40e_pf *pf = vsi->back;
4945 	struct i40e_hw *hw = &pf->hw;
4946 	int base = vsi->base_vector;
4947 	u32 val, qp;
4948 	int i;
4949 
4950 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4951 		if (!vsi->q_vectors)
4952 			return;
4953 
4954 		if (!vsi->irqs_ready)
4955 			return;
4956 
4957 		vsi->irqs_ready = false;
4958 		for (i = 0; i < vsi->num_q_vectors; i++) {
4959 			int irq_num;
4960 			u16 vector;
4961 
4962 			vector = i + base;
4963 			irq_num = pf->msix_entries[vector].vector;
4964 
4965 			/* free only the irqs that were actually requested */
4966 			if (!vsi->q_vectors[i] ||
4967 			    !vsi->q_vectors[i]->num_ringpairs)
4968 				continue;
4969 
4970 			/* clear the affinity notifier in the IRQ descriptor */
4971 			irq_set_affinity_notifier(irq_num, NULL);
4972 			/* remove our suggested affinity mask for this IRQ */
4973 			irq_update_affinity_hint(irq_num, NULL);
4974 			free_irq(irq_num, vsi->q_vectors[i]);
4975 
4976 			/* Tear down the interrupt queue link list
4977 			 *
4978 			 * We know that they come in pairs and always
4979 			 * the Rx first, then the Tx.  To clear the
4980 			 * link list, stick the EOL value into the
4981 			 * next_q field of the registers.
4982 			 */
4983 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4984 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
4985 				       val);
4986 			val |= I40E_QUEUE_END_OF_LIST
4987 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4988 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4989 
4990 			while (qp != I40E_QUEUE_END_OF_LIST) {
4991 				u32 next;
4992 
4993 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4994 
4995 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4996 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4997 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4998 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4999 
5000 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5001 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5002 
5003 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5004 
5005 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5006 
5007 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5008 						 val);
5009 
5010 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5011 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5012 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5013 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5014 
5015 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5016 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5017 
5018 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5019 				qp = next;
5020 			}
5021 		}
5022 	} else {
5023 		free_irq(pf->pdev->irq, pf);
5024 
5025 		val = rd32(hw, I40E_PFINT_LNKLST0);
5026 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5027 		val |= I40E_QUEUE_END_OF_LIST
5028 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5029 		wr32(hw, I40E_PFINT_LNKLST0, val);
5030 
5031 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5032 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5033 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5034 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5035 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5036 
5037 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5038 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5039 
5040 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5041 
5042 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5043 
5044 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5045 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5046 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5047 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5048 
5049 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5050 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5051 
5052 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5053 	}
5054 }
5055 
5056 /**
5057  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5058  * @vsi: the VSI being configured
5059  * @v_idx: Index of vector to be freed
5060  *
5061  * This function frees the memory allocated to the q_vector.  In addition if
5062  * NAPI is enabled it will delete any references to the NAPI struct prior
5063  * to freeing the q_vector.
5064  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5065 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5066 {
5067 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5068 	struct i40e_ring *ring;
5069 
5070 	if (!q_vector)
5071 		return;
5072 
5073 	/* disassociate q_vector from rings */
5074 	i40e_for_each_ring(ring, q_vector->tx)
5075 		ring->q_vector = NULL;
5076 
5077 	i40e_for_each_ring(ring, q_vector->rx)
5078 		ring->q_vector = NULL;
5079 
5080 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5081 	if (vsi->netdev)
5082 		netif_napi_del(&q_vector->napi);
5083 
5084 	vsi->q_vectors[v_idx] = NULL;
5085 
5086 	kfree_rcu(q_vector, rcu);
5087 }
5088 
5089 /**
5090  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5091  * @vsi: the VSI being un-configured
5092  *
5093  * This frees the memory allocated to the q_vectors and
5094  * deletes references to the NAPI struct.
5095  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5096 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5097 {
5098 	int v_idx;
5099 
5100 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5101 		i40e_free_q_vector(vsi, v_idx);
5102 }
5103 
5104 /**
5105  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5106  * @pf: board private structure
5107  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5108 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5109 {
5110 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5111 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5112 		pci_disable_msix(pf->pdev);
5113 		kfree(pf->msix_entries);
5114 		pf->msix_entries = NULL;
5115 		kfree(pf->irq_pile);
5116 		pf->irq_pile = NULL;
5117 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5118 		pci_disable_msi(pf->pdev);
5119 	}
5120 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5121 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5122 }
5123 
5124 /**
5125  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5126  * @pf: board private structure
5127  *
5128  * We go through and clear interrupt specific resources and reset the structure
5129  * to pre-load conditions
5130  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5131 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5132 {
5133 	struct i40e_vsi *vsi;
5134 	int i;
5135 
5136 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5137 		i40e_free_misc_vector(pf);
5138 
5139 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5140 		      I40E_IWARP_IRQ_PILE_ID);
5141 
5142 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5143 
5144 	i40e_pf_for_each_vsi(pf, i, vsi)
5145 		i40e_vsi_free_q_vectors(vsi);
5146 
5147 	i40e_reset_interrupt_capability(pf);
5148 }
5149 
5150 /**
5151  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5152  * @vsi: the VSI being configured
5153  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5154 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5155 {
5156 	int q_idx;
5157 
5158 	if (!vsi->netdev)
5159 		return;
5160 
5161 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5162 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5163 
5164 		if (q_vector->rx.ring || q_vector->tx.ring)
5165 			napi_enable(&q_vector->napi);
5166 	}
5167 }
5168 
5169 /**
5170  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5171  * @vsi: the VSI being configured
5172  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5173 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5174 {
5175 	int q_idx;
5176 
5177 	if (!vsi->netdev)
5178 		return;
5179 
5180 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5181 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5182 
5183 		if (q_vector->rx.ring || q_vector->tx.ring)
5184 			napi_disable(&q_vector->napi);
5185 	}
5186 }
5187 
5188 /**
5189  * i40e_vsi_close - Shut down a VSI
5190  * @vsi: the vsi to be quelled
5191  **/
i40e_vsi_close(struct i40e_vsi * vsi)5192 static void i40e_vsi_close(struct i40e_vsi *vsi)
5193 {
5194 	struct i40e_pf *pf = vsi->back;
5195 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5196 		i40e_down(vsi);
5197 	i40e_vsi_free_irq(vsi);
5198 	i40e_vsi_free_tx_resources(vsi);
5199 	i40e_vsi_free_rx_resources(vsi);
5200 	vsi->current_netdev_flags = 0;
5201 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5202 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5203 		set_bit(__I40E_CLIENT_RESET, pf->state);
5204 }
5205 
5206 /**
5207  * i40e_quiesce_vsi - Pause a given VSI
5208  * @vsi: the VSI being paused
5209  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5210 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5211 {
5212 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5213 		return;
5214 
5215 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5216 	if (vsi->netdev && netif_running(vsi->netdev))
5217 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5218 	else
5219 		i40e_vsi_close(vsi);
5220 }
5221 
5222 /**
5223  * i40e_unquiesce_vsi - Resume a given VSI
5224  * @vsi: the VSI being resumed
5225  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5226 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5227 {
5228 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5229 		return;
5230 
5231 	if (vsi->netdev && netif_running(vsi->netdev))
5232 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5233 	else
5234 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5235 }
5236 
5237 /**
5238  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5239  * @pf: the PF
5240  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5241 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5242 {
5243 	struct i40e_vsi *vsi;
5244 	int v;
5245 
5246 	i40e_pf_for_each_vsi(pf, v, vsi)
5247 		i40e_quiesce_vsi(vsi);
5248 }
5249 
5250 /**
5251  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5252  * @pf: the PF
5253  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5254 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5255 {
5256 	struct i40e_vsi *vsi;
5257 	int v;
5258 
5259 	i40e_pf_for_each_vsi(pf, v, vsi)
5260 		i40e_unquiesce_vsi(vsi);
5261 }
5262 
5263 /**
5264  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5265  * @vsi: the VSI being configured
5266  *
5267  * Wait until all queues on a given VSI have been disabled.
5268  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5269 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5270 {
5271 	struct i40e_pf *pf = vsi->back;
5272 	int i, pf_q, ret;
5273 
5274 	pf_q = vsi->base_queue;
5275 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5276 		/* Check and wait for the Tx queue */
5277 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5278 		if (ret) {
5279 			dev_info(&pf->pdev->dev,
5280 				 "VSI seid %d Tx ring %d disable timeout\n",
5281 				 vsi->seid, pf_q);
5282 			return ret;
5283 		}
5284 
5285 		if (!i40e_enabled_xdp_vsi(vsi))
5286 			goto wait_rx;
5287 
5288 		/* Check and wait for the XDP Tx queue */
5289 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5290 				       false);
5291 		if (ret) {
5292 			dev_info(&pf->pdev->dev,
5293 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5294 				 vsi->seid, pf_q);
5295 			return ret;
5296 		}
5297 wait_rx:
5298 		/* Check and wait for the Rx queue */
5299 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5300 		if (ret) {
5301 			dev_info(&pf->pdev->dev,
5302 				 "VSI seid %d Rx ring %d disable timeout\n",
5303 				 vsi->seid, pf_q);
5304 			return ret;
5305 		}
5306 	}
5307 
5308 	return 0;
5309 }
5310 
5311 #ifdef CONFIG_I40E_DCB
5312 /**
5313  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5314  * @pf: the PF
5315  *
5316  * This function waits for the queues to be in disabled state for all the
5317  * VSIs that are managed by this PF.
5318  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5319 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5320 {
5321 	struct i40e_vsi *vsi;
5322 	int v, ret = 0;
5323 
5324 	i40e_pf_for_each_vsi(pf, v, vsi) {
5325 		ret = i40e_vsi_wait_queues_disabled(vsi);
5326 		if (ret)
5327 			break;
5328 	}
5329 
5330 	return ret;
5331 }
5332 
5333 #endif
5334 
5335 /**
5336  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5337  * @pf: pointer to PF
5338  *
5339  * Get TC map for ISCSI PF type that will include iSCSI TC
5340  * and LAN TC.
5341  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5342 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5343 {
5344 	struct i40e_dcb_app_priority_table app;
5345 	struct i40e_hw *hw = &pf->hw;
5346 	u8 enabled_tc = 1; /* TC0 is always enabled */
5347 	u8 tc, i;
5348 	/* Get the iSCSI APP TLV */
5349 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5350 
5351 	for (i = 0; i < dcbcfg->numapps; i++) {
5352 		app = dcbcfg->app[i];
5353 		if (app.selector == I40E_APP_SEL_TCPIP &&
5354 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5355 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5356 			enabled_tc |= BIT(tc);
5357 			break;
5358 		}
5359 	}
5360 
5361 	return enabled_tc;
5362 }
5363 
5364 /**
5365  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5366  * @dcbcfg: the corresponding DCBx configuration structure
5367  *
5368  * Return the number of TCs from given DCBx configuration
5369  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5370 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5371 {
5372 	int i, tc_unused = 0;
5373 	u8 num_tc = 0;
5374 	u8 ret = 0;
5375 
5376 	/* Scan the ETS Config Priority Table to find
5377 	 * traffic class enabled for a given priority
5378 	 * and create a bitmask of enabled TCs
5379 	 */
5380 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5381 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5382 
5383 	/* Now scan the bitmask to check for
5384 	 * contiguous TCs starting with TC0
5385 	 */
5386 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5387 		if (num_tc & BIT(i)) {
5388 			if (!tc_unused) {
5389 				ret++;
5390 			} else {
5391 				pr_err("Non-contiguous TC - Disabling DCB\n");
5392 				return 1;
5393 			}
5394 		} else {
5395 			tc_unused = 1;
5396 		}
5397 	}
5398 
5399 	/* There is always at least TC0 */
5400 	if (!ret)
5401 		ret = 1;
5402 
5403 	return ret;
5404 }
5405 
5406 /**
5407  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5408  * @dcbcfg: the corresponding DCBx configuration structure
5409  *
5410  * Query the current DCB configuration and return the number of
5411  * traffic classes enabled from the given DCBX config
5412  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5413 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5414 {
5415 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5416 	u8 enabled_tc = 1;
5417 	u8 i;
5418 
5419 	for (i = 0; i < num_tc; i++)
5420 		enabled_tc |= BIT(i);
5421 
5422 	return enabled_tc;
5423 }
5424 
5425 /**
5426  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5427  * @pf: PF being queried
5428  *
5429  * Query the current MQPRIO configuration and return the number of
5430  * traffic classes enabled.
5431  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5432 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5433 {
5434 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5435 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5436 	u8 enabled_tc = 1, i;
5437 
5438 	for (i = 1; i < num_tc; i++)
5439 		enabled_tc |= BIT(i);
5440 	return enabled_tc;
5441 }
5442 
5443 /**
5444  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5445  * @pf: PF being queried
5446  *
5447  * Return number of traffic classes enabled for the given PF
5448  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5449 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5450 {
5451 	u8 i, enabled_tc = 1;
5452 	u8 num_tc = 0;
5453 
5454 	if (i40e_is_tc_mqprio_enabled(pf)) {
5455 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5456 
5457 		return vsi->mqprio_qopt.qopt.num_tc;
5458 	}
5459 
5460 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5461 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5462 		return 1;
5463 
5464 	/* SFP mode will be enabled for all TCs on port */
5465 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5466 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5467 
5468 	/* MFP mode return count of enabled TCs for this PF */
5469 	if (pf->hw.func_caps.iscsi)
5470 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5471 	else
5472 		return 1; /* Only TC0 */
5473 
5474 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5475 		if (enabled_tc & BIT(i))
5476 			num_tc++;
5477 	}
5478 	return num_tc;
5479 }
5480 
5481 /**
5482  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5483  * @pf: PF being queried
5484  *
5485  * Return a bitmap for enabled traffic classes for this PF.
5486  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5487 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5488 {
5489 	if (i40e_is_tc_mqprio_enabled(pf))
5490 		return i40e_mqprio_get_enabled_tc(pf);
5491 
5492 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5493 	 * default TC
5494 	 */
5495 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5496 		return I40E_DEFAULT_TRAFFIC_CLASS;
5497 
5498 	/* SFP mode we want PF to be enabled for all TCs */
5499 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5500 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5501 
5502 	/* MFP enabled and iSCSI PF type */
5503 	if (pf->hw.func_caps.iscsi)
5504 		return i40e_get_iscsi_tc_map(pf);
5505 	else
5506 		return I40E_DEFAULT_TRAFFIC_CLASS;
5507 }
5508 
5509 /**
5510  * i40e_vsi_get_bw_info - Query VSI BW Information
5511  * @vsi: the VSI being queried
5512  *
5513  * Returns 0 on success, negative value on failure
5514  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5515 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5516 {
5517 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5518 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5519 	struct i40e_pf *pf = vsi->back;
5520 	struct i40e_hw *hw = &pf->hw;
5521 	u32 tc_bw_max;
5522 	int ret;
5523 	int i;
5524 
5525 	/* Get the VSI level BW configuration */
5526 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5527 	if (ret) {
5528 		dev_info(&pf->pdev->dev,
5529 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5530 			 ERR_PTR(ret),
5531 			 libie_aq_str(pf->hw.aq.asq_last_status));
5532 		return -EINVAL;
5533 	}
5534 
5535 	/* Get the VSI level BW configuration per TC */
5536 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5537 					       NULL);
5538 	if (ret) {
5539 		dev_info(&pf->pdev->dev,
5540 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5541 			 ERR_PTR(ret),
5542 			 libie_aq_str(pf->hw.aq.asq_last_status));
5543 		return -EINVAL;
5544 	}
5545 
5546 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5547 		dev_info(&pf->pdev->dev,
5548 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5549 			 bw_config.tc_valid_bits,
5550 			 bw_ets_config.tc_valid_bits);
5551 		/* Still continuing */
5552 	}
5553 
5554 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5555 	vsi->bw_max_quanta = bw_config.max_bw;
5556 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5557 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5558 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5559 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5560 		vsi->bw_ets_limit_credits[i] =
5561 					le16_to_cpu(bw_ets_config.credits[i]);
5562 		/* 3 bits out of 4 for each TC */
5563 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5564 	}
5565 
5566 	return 0;
5567 }
5568 
5569 /**
5570  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5571  * @vsi: the VSI being configured
5572  * @enabled_tc: TC bitmap
5573  * @bw_share: BW shared credits per TC
5574  *
5575  * Returns 0 on success, negative value on failure
5576  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5577 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5578 				       u8 *bw_share)
5579 {
5580 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5581 	struct i40e_pf *pf = vsi->back;
5582 	int ret;
5583 	int i;
5584 
5585 	/* There is no need to reset BW when mqprio mode is on.  */
5586 	if (i40e_is_tc_mqprio_enabled(pf))
5587 		return 0;
5588 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5589 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5590 		if (ret)
5591 			dev_info(&pf->pdev->dev,
5592 				 "Failed to reset tx rate for vsi->seid %u\n",
5593 				 vsi->seid);
5594 		return ret;
5595 	}
5596 	memset(&bw_data, 0, sizeof(bw_data));
5597 	bw_data.tc_valid_bits = enabled_tc;
5598 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5599 		bw_data.tc_bw_credits[i] = bw_share[i];
5600 
5601 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5602 	if (ret) {
5603 		dev_info(&pf->pdev->dev,
5604 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5605 			 pf->hw.aq.asq_last_status);
5606 		return -EINVAL;
5607 	}
5608 
5609 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5610 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5611 
5612 	return 0;
5613 }
5614 
5615 /**
5616  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5617  * @vsi: the VSI being configured
5618  * @enabled_tc: TC map to be enabled
5619  *
5620  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5621 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5622 {
5623 	struct net_device *netdev = vsi->netdev;
5624 	struct i40e_pf *pf = vsi->back;
5625 	struct i40e_hw *hw = &pf->hw;
5626 	u8 netdev_tc = 0;
5627 	int i;
5628 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5629 
5630 	if (!netdev)
5631 		return;
5632 
5633 	if (!enabled_tc) {
5634 		netdev_reset_tc(netdev);
5635 		return;
5636 	}
5637 
5638 	/* Set up actual enabled TCs on the VSI */
5639 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5640 		return;
5641 
5642 	/* set per TC queues for the VSI */
5643 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5644 		/* Only set TC queues for enabled tcs
5645 		 *
5646 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5647 		 * enabled_tc bitmap would be 0x00001001; the driver
5648 		 * will set the numtc for netdev as 2 that will be
5649 		 * referenced by the netdev layer as TC 0 and 1.
5650 		 */
5651 		if (vsi->tc_config.enabled_tc & BIT(i))
5652 			netdev_set_tc_queue(netdev,
5653 					vsi->tc_config.tc_info[i].netdev_tc,
5654 					vsi->tc_config.tc_info[i].qcount,
5655 					vsi->tc_config.tc_info[i].qoffset);
5656 	}
5657 
5658 	if (i40e_is_tc_mqprio_enabled(pf))
5659 		return;
5660 
5661 	/* Assign UP2TC map for the VSI */
5662 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5663 		/* Get the actual TC# for the UP */
5664 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5665 		/* Get the mapped netdev TC# for the UP */
5666 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5667 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5668 	}
5669 }
5670 
5671 /**
5672  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5673  * @vsi: the VSI being configured
5674  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5675  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5676 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5677 				      struct i40e_vsi_context *ctxt)
5678 {
5679 	/* copy just the sections touched not the entire info
5680 	 * since not all sections are valid as returned by
5681 	 * update vsi params
5682 	 */
5683 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5684 	memcpy(&vsi->info.queue_mapping,
5685 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5686 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5687 	       sizeof(vsi->info.tc_mapping));
5688 }
5689 
5690 /**
5691  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5692  * @vsi: the VSI being reconfigured
5693  * @vsi_offset: offset from main VF VSI
5694  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5695 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5696 {
5697 	struct i40e_vsi_context ctxt = {};
5698 	struct i40e_pf *pf;
5699 	struct i40e_hw *hw;
5700 	int ret;
5701 
5702 	if (!vsi)
5703 		return -EINVAL;
5704 	pf = vsi->back;
5705 	hw = &pf->hw;
5706 
5707 	ctxt.seid = vsi->seid;
5708 	ctxt.pf_num = hw->pf_id;
5709 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5710 	ctxt.uplink_seid = vsi->uplink_seid;
5711 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5712 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5713 	ctxt.info = vsi->info;
5714 
5715 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5716 				 false);
5717 	if (vsi->reconfig_rss) {
5718 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5719 				      vsi->num_queue_pairs);
5720 		ret = i40e_vsi_config_rss(vsi);
5721 		if (ret) {
5722 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5723 			return ret;
5724 		}
5725 		vsi->reconfig_rss = false;
5726 	}
5727 
5728 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5729 	if (ret) {
5730 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5731 			 ERR_PTR(ret),
5732 			 libie_aq_str(hw->aq.asq_last_status));
5733 		return ret;
5734 	}
5735 	/* update the local VSI info with updated queue map */
5736 	i40e_vsi_update_queue_map(vsi, &ctxt);
5737 	vsi->info.valid_sections = 0;
5738 
5739 	return ret;
5740 }
5741 
5742 /**
5743  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5744  * @vsi: VSI to be configured
5745  * @enabled_tc: TC bitmap
5746  *
5747  * This configures a particular VSI for TCs that are mapped to the
5748  * given TC bitmap. It uses default bandwidth share for TCs across
5749  * VSIs to configure TC for a particular VSI.
5750  *
5751  * NOTE:
5752  * It is expected that the VSI queues have been quisced before calling
5753  * this function.
5754  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5755 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5756 {
5757 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5758 	struct i40e_pf *pf = vsi->back;
5759 	struct i40e_hw *hw = &pf->hw;
5760 	struct i40e_vsi_context ctxt;
5761 	int ret = 0;
5762 	int i;
5763 
5764 	/* Check if enabled_tc is same as existing or new TCs */
5765 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5766 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5767 		return ret;
5768 
5769 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5770 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5771 		if (enabled_tc & BIT(i))
5772 			bw_share[i] = 1;
5773 	}
5774 
5775 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5776 	if (ret) {
5777 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5778 
5779 		dev_info(&pf->pdev->dev,
5780 			 "Failed configuring TC map %d for VSI %d\n",
5781 			 enabled_tc, vsi->seid);
5782 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5783 						  &bw_config, NULL);
5784 		if (ret) {
5785 			dev_info(&pf->pdev->dev,
5786 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5787 				 ERR_PTR(ret),
5788 				 libie_aq_str(hw->aq.asq_last_status));
5789 			goto out;
5790 		}
5791 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5792 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5793 
5794 			if (!valid_tc)
5795 				valid_tc = bw_config.tc_valid_bits;
5796 			/* Always enable TC0, no matter what */
5797 			valid_tc |= 1;
5798 			dev_info(&pf->pdev->dev,
5799 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5800 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5801 			enabled_tc = valid_tc;
5802 		}
5803 
5804 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5805 		if (ret) {
5806 			dev_err(&pf->pdev->dev,
5807 				"Unable to  configure TC map %d for VSI %d\n",
5808 				enabled_tc, vsi->seid);
5809 			goto out;
5810 		}
5811 	}
5812 
5813 	/* Update Queue Pairs Mapping for currently enabled UPs */
5814 	ctxt.seid = vsi->seid;
5815 	ctxt.pf_num = vsi->back->hw.pf_id;
5816 	ctxt.vf_num = 0;
5817 	ctxt.uplink_seid = vsi->uplink_seid;
5818 	ctxt.info = vsi->info;
5819 	if (i40e_is_tc_mqprio_enabled(pf)) {
5820 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5821 		if (ret)
5822 			goto out;
5823 	} else {
5824 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5825 	}
5826 
5827 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5828 	 * queues changed.
5829 	 */
5830 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5831 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5832 				      vsi->num_queue_pairs);
5833 		ret = i40e_vsi_config_rss(vsi);
5834 		if (ret) {
5835 			dev_info(&vsi->back->pdev->dev,
5836 				 "Failed to reconfig rss for num_queues\n");
5837 			return ret;
5838 		}
5839 		vsi->reconfig_rss = false;
5840 	}
5841 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5842 		ctxt.info.valid_sections |=
5843 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5844 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5845 	}
5846 
5847 	/* Update the VSI after updating the VSI queue-mapping
5848 	 * information
5849 	 */
5850 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5851 	if (ret) {
5852 		dev_info(&pf->pdev->dev,
5853 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5854 			 ERR_PTR(ret),
5855 			 libie_aq_str(hw->aq.asq_last_status));
5856 		goto out;
5857 	}
5858 	/* update the local VSI info with updated queue map */
5859 	i40e_vsi_update_queue_map(vsi, &ctxt);
5860 	vsi->info.valid_sections = 0;
5861 
5862 	/* Update current VSI BW information */
5863 	ret = i40e_vsi_get_bw_info(vsi);
5864 	if (ret) {
5865 		dev_info(&pf->pdev->dev,
5866 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5867 			 ERR_PTR(ret),
5868 			 libie_aq_str(hw->aq.asq_last_status));
5869 		goto out;
5870 	}
5871 
5872 	/* Update the netdev TC setup */
5873 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5874 out:
5875 	return ret;
5876 }
5877 
5878 /**
5879  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5880  * @vsi: VSI to be reconfigured
5881  *
5882  * This reconfigures a particular VSI for TCs that are mapped to the
5883  * TC bitmap stored previously for the VSI.
5884  *
5885  * Context: It is expected that the VSI queues have been quisced before
5886  *          calling this function.
5887  *
5888  * Return: 0 on success, negative value on failure
5889  **/
i40e_vsi_reconfig_tc(struct i40e_vsi * vsi)5890 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5891 {
5892 	u8 enabled_tc;
5893 
5894 	enabled_tc = vsi->tc_config.enabled_tc;
5895 	vsi->tc_config.enabled_tc = 0;
5896 
5897 	return i40e_vsi_config_tc(vsi, enabled_tc);
5898 }
5899 
5900 /**
5901  * i40e_get_link_speed - Returns link speed for the interface
5902  * @vsi: VSI to be configured
5903  *
5904  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5905 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5906 {
5907 	struct i40e_pf *pf = vsi->back;
5908 
5909 	switch (pf->hw.phy.link_info.link_speed) {
5910 	case I40E_LINK_SPEED_40GB:
5911 		return 40000;
5912 	case I40E_LINK_SPEED_25GB:
5913 		return 25000;
5914 	case I40E_LINK_SPEED_20GB:
5915 		return 20000;
5916 	case I40E_LINK_SPEED_10GB:
5917 		return 10000;
5918 	case I40E_LINK_SPEED_1GB:
5919 		return 1000;
5920 	default:
5921 		return -EINVAL;
5922 	}
5923 }
5924 
5925 /**
5926  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5927  * @vsi: Pointer to vsi structure
5928  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5929  *
5930  * Helper function to convert units before send to set BW limit
5931  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5932 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5933 {
5934 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5935 		dev_warn(&vsi->back->pdev->dev,
5936 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5937 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5938 	} else {
5939 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5940 	}
5941 
5942 	return max_tx_rate;
5943 }
5944 
5945 /**
5946  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5947  * @vsi: VSI to be configured
5948  * @seid: seid of the channel/VSI
5949  * @max_tx_rate: max TX rate to be configured as BW limit
5950  *
5951  * Helper function to set BW limit for a given VSI
5952  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5953 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5954 {
5955 	struct i40e_pf *pf = vsi->back;
5956 	u64 credits = 0;
5957 	int speed = 0;
5958 	int ret = 0;
5959 
5960 	speed = i40e_get_link_speed(vsi);
5961 	if (max_tx_rate > speed) {
5962 		dev_err(&pf->pdev->dev,
5963 			"Invalid max tx rate %llu specified for VSI seid %d.",
5964 			max_tx_rate, seid);
5965 		return -EINVAL;
5966 	}
5967 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5968 		dev_warn(&pf->pdev->dev,
5969 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5970 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5971 	}
5972 
5973 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5974 	credits = max_tx_rate;
5975 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5976 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5977 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5978 	if (ret)
5979 		dev_err(&pf->pdev->dev,
5980 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5981 			max_tx_rate, seid, ERR_PTR(ret),
5982 			libie_aq_str(pf->hw.aq.asq_last_status));
5983 	return ret;
5984 }
5985 
5986 /**
5987  * i40e_remove_queue_channels - Remove queue channels for the TCs
5988  * @vsi: VSI to be configured
5989  *
5990  * Remove queue channels for the TCs
5991  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5992 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5993 {
5994 	struct i40e_cloud_filter *cfilter;
5995 	enum libie_aq_err last_aq_status;
5996 	struct i40e_channel *ch, *ch_tmp;
5997 	struct i40e_pf *pf = vsi->back;
5998 	struct hlist_node *node;
5999 	int ret, i;
6000 
6001 	/* Reset rss size that was stored when reconfiguring rss for
6002 	 * channel VSIs with non-power-of-2 queue count.
6003 	 */
6004 	vsi->current_rss_size = 0;
6005 
6006 	/* perform cleanup for channels if they exist */
6007 	if (list_empty(&vsi->ch_list))
6008 		return;
6009 
6010 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6011 		struct i40e_vsi *p_vsi;
6012 
6013 		list_del(&ch->list);
6014 		p_vsi = ch->parent_vsi;
6015 		if (!p_vsi || !ch->initialized) {
6016 			kfree(ch);
6017 			continue;
6018 		}
6019 		/* Reset queue contexts */
6020 		for (i = 0; i < ch->num_queue_pairs; i++) {
6021 			struct i40e_ring *tx_ring, *rx_ring;
6022 			u16 pf_q;
6023 
6024 			pf_q = ch->base_queue + i;
6025 			tx_ring = vsi->tx_rings[pf_q];
6026 			tx_ring->ch = NULL;
6027 
6028 			rx_ring = vsi->rx_rings[pf_q];
6029 			rx_ring->ch = NULL;
6030 		}
6031 
6032 		/* Reset BW configured for this VSI via mqprio */
6033 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6034 		if (ret)
6035 			dev_info(&vsi->back->pdev->dev,
6036 				 "Failed to reset tx rate for ch->seid %u\n",
6037 				 ch->seid);
6038 
6039 		/* delete cloud filters associated with this channel */
6040 		hlist_for_each_entry_safe(cfilter, node,
6041 					  &pf->cloud_filter_list, cloud_node) {
6042 			if (cfilter->seid != ch->seid)
6043 				continue;
6044 
6045 			hash_del(&cfilter->cloud_node);
6046 			if (cfilter->dst_port)
6047 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6048 									cfilter,
6049 									false);
6050 			else
6051 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6052 								false);
6053 			last_aq_status = pf->hw.aq.asq_last_status;
6054 			if (ret)
6055 				dev_info(&pf->pdev->dev,
6056 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6057 					 ERR_PTR(ret),
6058 					 libie_aq_str(last_aq_status));
6059 			kfree(cfilter);
6060 		}
6061 
6062 		/* delete VSI from FW */
6063 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6064 					     NULL);
6065 		if (ret)
6066 			dev_err(&vsi->back->pdev->dev,
6067 				"unable to remove channel (%d) for parent VSI(%d)\n",
6068 				ch->seid, p_vsi->seid);
6069 		kfree(ch);
6070 	}
6071 	INIT_LIST_HEAD(&vsi->ch_list);
6072 }
6073 
6074 /**
6075  * i40e_get_max_queues_for_channel
6076  * @vsi: ptr to VSI to which channels are associated with
6077  *
6078  * Helper function which returns max value among the queue counts set on the
6079  * channels/TCs created.
6080  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6081 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6082 {
6083 	struct i40e_channel *ch, *ch_tmp;
6084 	int max = 0;
6085 
6086 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6087 		if (!ch->initialized)
6088 			continue;
6089 		if (ch->num_queue_pairs > max)
6090 			max = ch->num_queue_pairs;
6091 	}
6092 
6093 	return max;
6094 }
6095 
6096 /**
6097  * i40e_validate_num_queues - validate num_queues w.r.t channel
6098  * @pf: ptr to PF device
6099  * @num_queues: number of queues
6100  * @vsi: the parent VSI
6101  * @reconfig_rss: indicates should the RSS be reconfigured or not
6102  *
6103  * This function validates number of queues in the context of new channel
6104  * which is being established and determines if RSS should be reconfigured
6105  * or not for parent VSI.
6106  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6107 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6108 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6109 {
6110 	int max_ch_queues;
6111 
6112 	if (!reconfig_rss)
6113 		return -EINVAL;
6114 
6115 	*reconfig_rss = false;
6116 	if (vsi->current_rss_size) {
6117 		if (num_queues > vsi->current_rss_size) {
6118 			dev_dbg(&pf->pdev->dev,
6119 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6120 				num_queues, vsi->current_rss_size);
6121 			return -EINVAL;
6122 		} else if ((num_queues < vsi->current_rss_size) &&
6123 			   (!is_power_of_2(num_queues))) {
6124 			dev_dbg(&pf->pdev->dev,
6125 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6126 				num_queues, vsi->current_rss_size);
6127 			return -EINVAL;
6128 		}
6129 	}
6130 
6131 	if (!is_power_of_2(num_queues)) {
6132 		/* Find the max num_queues configured for channel if channel
6133 		 * exist.
6134 		 * if channel exist, then enforce 'num_queues' to be more than
6135 		 * max ever queues configured for channel.
6136 		 */
6137 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6138 		if (num_queues < max_ch_queues) {
6139 			dev_dbg(&pf->pdev->dev,
6140 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6141 				num_queues, max_ch_queues);
6142 			return -EINVAL;
6143 		}
6144 		*reconfig_rss = true;
6145 	}
6146 
6147 	return 0;
6148 }
6149 
6150 /**
6151  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6152  * @vsi: the VSI being setup
6153  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6154  *
6155  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6156  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6157 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6158 {
6159 	struct i40e_pf *pf = vsi->back;
6160 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6161 	struct i40e_hw *hw = &pf->hw;
6162 	int local_rss_size;
6163 	u8 *lut;
6164 	int ret;
6165 
6166 	if (!vsi->rss_size)
6167 		return -EINVAL;
6168 
6169 	if (rss_size > vsi->rss_size)
6170 		return -EINVAL;
6171 
6172 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6173 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6174 	if (!lut)
6175 		return -ENOMEM;
6176 
6177 	/* Ignoring user configured lut if there is one */
6178 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6179 
6180 	/* Use user configured hash key if there is one, otherwise
6181 	 * use default.
6182 	 */
6183 	if (vsi->rss_hkey_user)
6184 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6185 	else
6186 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6187 
6188 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6189 	if (ret) {
6190 		dev_info(&pf->pdev->dev,
6191 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6192 			 ERR_PTR(ret),
6193 			 libie_aq_str(hw->aq.asq_last_status));
6194 		kfree(lut);
6195 		return ret;
6196 	}
6197 	kfree(lut);
6198 
6199 	/* Do the update w.r.t. storing rss_size */
6200 	if (!vsi->orig_rss_size)
6201 		vsi->orig_rss_size = vsi->rss_size;
6202 	vsi->current_rss_size = local_rss_size;
6203 
6204 	return ret;
6205 }
6206 
6207 /**
6208  * i40e_channel_setup_queue_map - Setup a channel queue map
6209  * @pf: ptr to PF device
6210  * @ctxt: VSI context structure
6211  * @ch: ptr to channel structure
6212  *
6213  * Setup queue map for a specific channel
6214  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6215 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6216 					 struct i40e_vsi_context *ctxt,
6217 					 struct i40e_channel *ch)
6218 {
6219 	u16 qcount, qmap, sections = 0;
6220 	u8 offset = 0;
6221 	int pow;
6222 
6223 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6224 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6225 
6226 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6227 	ch->num_queue_pairs = qcount;
6228 
6229 	/* find the next higher power-of-2 of num queue pairs */
6230 	pow = ilog2(qcount);
6231 	if (!is_power_of_2(qcount))
6232 		pow++;
6233 
6234 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6235 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6236 
6237 	/* Setup queue TC[0].qmap for given VSI context */
6238 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6239 
6240 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6241 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6242 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6243 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6244 }
6245 
6246 /**
6247  * i40e_add_channel - add a channel by adding VSI
6248  * @pf: ptr to PF device
6249  * @uplink_seid: underlying HW switching element (VEB) ID
6250  * @ch: ptr to channel structure
6251  *
6252  * Add a channel (VSI) using add_vsi and queue_map
6253  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6254 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6255 			    struct i40e_channel *ch)
6256 {
6257 	struct i40e_hw *hw = &pf->hw;
6258 	struct i40e_vsi_context ctxt;
6259 	u8 enabled_tc = 0x1; /* TC0 enabled */
6260 	int ret;
6261 
6262 	if (ch->type != I40E_VSI_VMDQ2) {
6263 		dev_info(&pf->pdev->dev,
6264 			 "add new vsi failed, ch->type %d\n", ch->type);
6265 		return -EINVAL;
6266 	}
6267 
6268 	memset(&ctxt, 0, sizeof(ctxt));
6269 	ctxt.pf_num = hw->pf_id;
6270 	ctxt.vf_num = 0;
6271 	ctxt.uplink_seid = uplink_seid;
6272 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6273 	if (ch->type == I40E_VSI_VMDQ2)
6274 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6275 
6276 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6277 		ctxt.info.valid_sections |=
6278 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6279 		ctxt.info.switch_id =
6280 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6281 	}
6282 
6283 	/* Set queue map for a given VSI context */
6284 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6285 
6286 	/* Now time to create VSI */
6287 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6288 	if (ret) {
6289 		dev_info(&pf->pdev->dev,
6290 			 "add new vsi failed, err %pe aq_err %s\n",
6291 			 ERR_PTR(ret),
6292 			 libie_aq_str(pf->hw.aq.asq_last_status));
6293 		return -ENOENT;
6294 	}
6295 
6296 	/* Success, update channel, set enabled_tc only if the channel
6297 	 * is not a macvlan
6298 	 */
6299 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6300 	ch->seid = ctxt.seid;
6301 	ch->vsi_number = ctxt.vsi_number;
6302 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6303 
6304 	/* copy just the sections touched not the entire info
6305 	 * since not all sections are valid as returned by
6306 	 * update vsi params
6307 	 */
6308 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6309 	memcpy(&ch->info.queue_mapping,
6310 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6311 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6312 	       sizeof(ctxt.info.tc_mapping));
6313 
6314 	return 0;
6315 }
6316 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6317 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6318 				  u8 *bw_share)
6319 {
6320 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6321 	int ret;
6322 	int i;
6323 
6324 	memset(&bw_data, 0, sizeof(bw_data));
6325 	bw_data.tc_valid_bits = ch->enabled_tc;
6326 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6327 		bw_data.tc_bw_credits[i] = bw_share[i];
6328 
6329 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6330 				       &bw_data, NULL);
6331 	if (ret) {
6332 		dev_info(&vsi->back->pdev->dev,
6333 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6334 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6335 		return -EINVAL;
6336 	}
6337 
6338 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6339 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6340 
6341 	return 0;
6342 }
6343 
6344 /**
6345  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6346  * @pf: ptr to PF device
6347  * @vsi: the VSI being setup
6348  * @ch: ptr to channel structure
6349  *
6350  * Configure TX rings associated with channel (VSI) since queues are being
6351  * from parent VSI.
6352  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6353 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6354 				       struct i40e_vsi *vsi,
6355 				       struct i40e_channel *ch)
6356 {
6357 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6358 	int ret;
6359 	int i;
6360 
6361 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6362 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6363 		if (ch->enabled_tc & BIT(i))
6364 			bw_share[i] = 1;
6365 	}
6366 
6367 	/* configure BW for new VSI */
6368 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6369 	if (ret) {
6370 		dev_info(&vsi->back->pdev->dev,
6371 			 "Failed configuring TC map %d for channel (seid %u)\n",
6372 			 ch->enabled_tc, ch->seid);
6373 		return ret;
6374 	}
6375 
6376 	for (i = 0; i < ch->num_queue_pairs; i++) {
6377 		struct i40e_ring *tx_ring, *rx_ring;
6378 		u16 pf_q;
6379 
6380 		pf_q = ch->base_queue + i;
6381 
6382 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6383 		 * context
6384 		 */
6385 		tx_ring = vsi->tx_rings[pf_q];
6386 		tx_ring->ch = ch;
6387 
6388 		/* Get the RX ring ptr */
6389 		rx_ring = vsi->rx_rings[pf_q];
6390 		rx_ring->ch = ch;
6391 	}
6392 
6393 	return 0;
6394 }
6395 
6396 /**
6397  * i40e_setup_hw_channel - setup new channel
6398  * @pf: ptr to PF device
6399  * @vsi: the VSI being setup
6400  * @ch: ptr to channel structure
6401  * @uplink_seid: underlying HW switching element (VEB) ID
6402  * @type: type of channel to be created (VMDq2/VF)
6403  *
6404  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6405  * and configures TX rings accordingly
6406  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6407 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6408 					struct i40e_vsi *vsi,
6409 					struct i40e_channel *ch,
6410 					u16 uplink_seid, u8 type)
6411 {
6412 	int ret;
6413 
6414 	ch->initialized = false;
6415 	ch->base_queue = vsi->next_base_queue;
6416 	ch->type = type;
6417 
6418 	/* Proceed with creation of channel (VMDq2) VSI */
6419 	ret = i40e_add_channel(pf, uplink_seid, ch);
6420 	if (ret) {
6421 		dev_info(&pf->pdev->dev,
6422 			 "failed to add_channel using uplink_seid %u\n",
6423 			 uplink_seid);
6424 		return ret;
6425 	}
6426 
6427 	/* Mark the successful creation of channel */
6428 	ch->initialized = true;
6429 
6430 	/* Reconfigure TX queues using QTX_CTL register */
6431 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6432 	if (ret) {
6433 		dev_info(&pf->pdev->dev,
6434 			 "failed to configure TX rings for channel %u\n",
6435 			 ch->seid);
6436 		return ret;
6437 	}
6438 
6439 	/* update 'next_base_queue' */
6440 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6441 	dev_dbg(&pf->pdev->dev,
6442 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6443 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6444 		ch->num_queue_pairs,
6445 		vsi->next_base_queue);
6446 	return ret;
6447 }
6448 
6449 /**
6450  * i40e_setup_channel - setup new channel using uplink element
6451  * @pf: ptr to PF device
6452  * @vsi: pointer to the VSI to set up the channel within
6453  * @ch: ptr to channel structure
6454  *
6455  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6456  * and uplink switching element (uplink_seid)
6457  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6458 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6459 			       struct i40e_channel *ch)
6460 {
6461 	struct i40e_vsi *main_vsi;
6462 	u8 vsi_type;
6463 	u16 seid;
6464 	int ret;
6465 
6466 	if (vsi->type == I40E_VSI_MAIN) {
6467 		vsi_type = I40E_VSI_VMDQ2;
6468 	} else {
6469 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6470 			vsi->type);
6471 		return false;
6472 	}
6473 
6474 	/* underlying switching element */
6475 	main_vsi = i40e_pf_get_main_vsi(pf);
6476 	seid = main_vsi->uplink_seid;
6477 
6478 	/* create channel (VSI), configure TX rings */
6479 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6480 	if (ret) {
6481 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6482 		return false;
6483 	}
6484 
6485 	return ch->initialized ? true : false;
6486 }
6487 
6488 /**
6489  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6490  * @vsi: ptr to VSI which has PF backing
6491  *
6492  * Sets up switch mode correctly if it needs to be changed and perform
6493  * what are allowed modes.
6494  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6495 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6496 {
6497 	u8 mode;
6498 	struct i40e_pf *pf = vsi->back;
6499 	struct i40e_hw *hw = &pf->hw;
6500 	int ret;
6501 
6502 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6503 	if (ret)
6504 		return -EINVAL;
6505 
6506 	if (hw->dev_caps.switch_mode) {
6507 		/* if switch mode is set, support mode2 (non-tunneled for
6508 		 * cloud filter) for now
6509 		 */
6510 		u32 switch_mode = hw->dev_caps.switch_mode &
6511 				  I40E_SWITCH_MODE_MASK;
6512 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6513 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6514 				return 0;
6515 			dev_err(&pf->pdev->dev,
6516 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6517 				hw->dev_caps.switch_mode);
6518 			return -EINVAL;
6519 		}
6520 	}
6521 
6522 	/* Set Bit 7 to be valid */
6523 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6524 
6525 	/* Set L4type for TCP support */
6526 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6527 
6528 	/* Set cloud filter mode */
6529 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6530 
6531 	/* Prep mode field for set_switch_config */
6532 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6533 					pf->last_sw_conf_valid_flags,
6534 					mode, NULL);
6535 	if (ret && hw->aq.asq_last_status != LIBIE_AQ_RC_ESRCH)
6536 		dev_err(&pf->pdev->dev,
6537 			"couldn't set switch config bits, err %pe aq_err %s\n",
6538 			ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
6539 
6540 	return ret;
6541 }
6542 
6543 /**
6544  * i40e_create_queue_channel - function to create channel
6545  * @vsi: VSI to be configured
6546  * @ch: ptr to channel (it contains channel specific params)
6547  *
6548  * This function creates channel (VSI) using num_queues specified by user,
6549  * reconfigs RSS if needed.
6550  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6551 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6552 			      struct i40e_channel *ch)
6553 {
6554 	struct i40e_pf *pf = vsi->back;
6555 	bool reconfig_rss;
6556 	int err;
6557 
6558 	if (!ch)
6559 		return -EINVAL;
6560 
6561 	if (!ch->num_queue_pairs) {
6562 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6563 			ch->num_queue_pairs);
6564 		return -EINVAL;
6565 	}
6566 
6567 	/* validate user requested num_queues for channel */
6568 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6569 				       &reconfig_rss);
6570 	if (err) {
6571 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6572 			 ch->num_queue_pairs);
6573 		return -EINVAL;
6574 	}
6575 
6576 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6577 	 * VSI to be added switch to VEB mode.
6578 	 */
6579 
6580 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6581 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6582 
6583 		if (vsi->type == I40E_VSI_MAIN) {
6584 			if (i40e_is_tc_mqprio_enabled(pf))
6585 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6586 			else
6587 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6588 		}
6589 		/* now onwards for main VSI, number of queues will be value
6590 		 * of TC0's queue count
6591 		 */
6592 	}
6593 
6594 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6595 	 * it should be more than num_queues
6596 	 */
6597 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6598 		dev_dbg(&pf->pdev->dev,
6599 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6600 			vsi->cnt_q_avail, ch->num_queue_pairs);
6601 		return -EINVAL;
6602 	}
6603 
6604 	/* reconfig_rss only if vsi type is MAIN_VSI */
6605 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6606 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6607 		if (err) {
6608 			dev_info(&pf->pdev->dev,
6609 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6610 				 ch->num_queue_pairs);
6611 			return -EINVAL;
6612 		}
6613 	}
6614 
6615 	if (!i40e_setup_channel(pf, vsi, ch)) {
6616 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6617 		return -EINVAL;
6618 	}
6619 
6620 	dev_info(&pf->pdev->dev,
6621 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6622 		 ch->seid, ch->num_queue_pairs);
6623 
6624 	/* configure VSI for BW limit */
6625 	if (ch->max_tx_rate) {
6626 		u64 credits = ch->max_tx_rate;
6627 
6628 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6629 			return -EINVAL;
6630 
6631 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6632 		dev_dbg(&pf->pdev->dev,
6633 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6634 			ch->max_tx_rate,
6635 			credits,
6636 			ch->seid);
6637 	}
6638 
6639 	/* in case of VF, this will be main SRIOV VSI */
6640 	ch->parent_vsi = vsi;
6641 
6642 	/* and update main_vsi's count for queue_available to use */
6643 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6644 
6645 	return 0;
6646 }
6647 
6648 /**
6649  * i40e_configure_queue_channels - Add queue channel for the given TCs
6650  * @vsi: VSI to be configured
6651  *
6652  * Configures queue channel mapping to the given TCs
6653  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6654 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6655 {
6656 	struct i40e_channel *ch;
6657 	u64 max_rate = 0;
6658 	int ret = 0, i;
6659 
6660 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6661 	vsi->tc_seid_map[0] = vsi->seid;
6662 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6663 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6664 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6665 			if (!ch) {
6666 				ret = -ENOMEM;
6667 				goto err_free;
6668 			}
6669 
6670 			INIT_LIST_HEAD(&ch->list);
6671 			ch->num_queue_pairs =
6672 				vsi->tc_config.tc_info[i].qcount;
6673 			ch->base_queue =
6674 				vsi->tc_config.tc_info[i].qoffset;
6675 
6676 			/* Bandwidth limit through tc interface is in bytes/s,
6677 			 * change to Mbit/s
6678 			 */
6679 			max_rate = vsi->mqprio_qopt.max_rate[i];
6680 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6681 			ch->max_tx_rate = max_rate;
6682 
6683 			list_add_tail(&ch->list, &vsi->ch_list);
6684 
6685 			ret = i40e_create_queue_channel(vsi, ch);
6686 			if (ret) {
6687 				dev_err(&vsi->back->pdev->dev,
6688 					"Failed creating queue channel with TC%d: queues %d\n",
6689 					i, ch->num_queue_pairs);
6690 				goto err_free;
6691 			}
6692 			vsi->tc_seid_map[i] = ch->seid;
6693 		}
6694 	}
6695 
6696 	/* reset to reconfigure TX queue contexts */
6697 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6698 	return ret;
6699 
6700 err_free:
6701 	i40e_remove_queue_channels(vsi);
6702 	return ret;
6703 }
6704 
6705 /**
6706  * i40e_veb_config_tc - Configure TCs for given VEB
6707  * @veb: given VEB
6708  * @enabled_tc: TC bitmap
6709  *
6710  * Configures given TC bitmap for VEB (switching) element
6711  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6712 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6713 {
6714 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6715 	struct i40e_pf *pf = veb->pf;
6716 	int ret = 0;
6717 	int i;
6718 
6719 	/* No TCs or already enabled TCs just return */
6720 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6721 		return ret;
6722 
6723 	bw_data.tc_valid_bits = enabled_tc;
6724 	/* bw_data.absolute_credits is not set (relative) */
6725 
6726 	/* Enable ETS TCs with equal BW Share for now */
6727 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6728 		if (enabled_tc & BIT(i))
6729 			bw_data.tc_bw_share_credits[i] = 1;
6730 	}
6731 
6732 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6733 						   &bw_data, NULL);
6734 	if (ret) {
6735 		dev_info(&pf->pdev->dev,
6736 			 "VEB bw config failed, err %pe aq_err %s\n",
6737 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6738 		goto out;
6739 	}
6740 
6741 	/* Update the BW information */
6742 	ret = i40e_veb_get_bw_info(veb);
6743 	if (ret) {
6744 		dev_info(&pf->pdev->dev,
6745 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6746 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6747 	}
6748 
6749 out:
6750 	return ret;
6751 }
6752 
6753 #ifdef CONFIG_I40E_DCB
6754 /**
6755  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6756  * @pf: PF struct
6757  *
6758  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6759  * the caller would've quiesce all the VSIs before calling
6760  * this function
6761  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6762 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6763 {
6764 	struct i40e_vsi *vsi;
6765 	struct i40e_veb *veb;
6766 	u8 tc_map = 0;
6767 	int ret;
6768 	int v;
6769 
6770 	/* Enable the TCs available on PF to all VEBs */
6771 	tc_map = i40e_pf_get_tc_map(pf);
6772 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6773 		return;
6774 
6775 	i40e_pf_for_each_veb(pf, v, veb) {
6776 		ret = i40e_veb_config_tc(veb, tc_map);
6777 		if (ret) {
6778 			dev_info(&pf->pdev->dev,
6779 				 "Failed configuring TC for VEB seid=%d\n",
6780 				 veb->seid);
6781 			/* Will try to configure as many components */
6782 		}
6783 	}
6784 
6785 	/* Update each VSI */
6786 	i40e_pf_for_each_vsi(pf, v, vsi) {
6787 		/* - Enable all TCs for the LAN VSI
6788 		 * - For all others keep them at TC0 for now
6789 		 */
6790 		if (vsi->type == I40E_VSI_MAIN)
6791 			tc_map = i40e_pf_get_tc_map(pf);
6792 		else
6793 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6794 
6795 		ret = i40e_vsi_config_tc(vsi, tc_map);
6796 		if (ret) {
6797 			dev_info(&pf->pdev->dev,
6798 				 "Failed configuring TC for VSI seid=%d\n",
6799 				 vsi->seid);
6800 			/* Will try to configure as many components */
6801 		} else {
6802 			/* Re-configure VSI vectors based on updated TC map */
6803 			i40e_vsi_map_rings_to_vectors(vsi);
6804 			if (vsi->netdev)
6805 				i40e_dcbnl_set_all(vsi);
6806 		}
6807 	}
6808 }
6809 
6810 /**
6811  * i40e_resume_port_tx - Resume port Tx
6812  * @pf: PF struct
6813  *
6814  * Resume a port's Tx and issue a PF reset in case of failure to
6815  * resume.
6816  **/
i40e_resume_port_tx(struct i40e_pf * pf)6817 static int i40e_resume_port_tx(struct i40e_pf *pf)
6818 {
6819 	struct i40e_hw *hw = &pf->hw;
6820 	int ret;
6821 
6822 	ret = i40e_aq_resume_port_tx(hw, NULL);
6823 	if (ret) {
6824 		dev_info(&pf->pdev->dev,
6825 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6826 			  ERR_PTR(ret),
6827 			  libie_aq_str(pf->hw.aq.asq_last_status));
6828 		/* Schedule PF reset to recover */
6829 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6830 		i40e_service_event_schedule(pf);
6831 	}
6832 
6833 	return ret;
6834 }
6835 
6836 /**
6837  * i40e_suspend_port_tx - Suspend port Tx
6838  * @pf: PF struct
6839  *
6840  * Suspend a port's Tx and issue a PF reset in case of failure.
6841  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6842 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6843 {
6844 	struct i40e_hw *hw = &pf->hw;
6845 	int ret;
6846 
6847 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6848 	if (ret) {
6849 		dev_info(&pf->pdev->dev,
6850 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6851 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6852 		/* Schedule PF reset to recover */
6853 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6854 		i40e_service_event_schedule(pf);
6855 	}
6856 
6857 	return ret;
6858 }
6859 
6860 /**
6861  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6862  * @pf: PF being configured
6863  * @new_cfg: New DCBX configuration
6864  *
6865  * Program DCB settings into HW and reconfigure VEB/VSIs on
6866  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6867  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6868 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6869 				  struct i40e_dcbx_config *new_cfg)
6870 {
6871 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6872 	int ret;
6873 
6874 	/* Check if need reconfiguration */
6875 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6876 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6877 		return 0;
6878 	}
6879 
6880 	/* Config change disable all VSIs */
6881 	i40e_pf_quiesce_all_vsi(pf);
6882 
6883 	/* Copy the new config to the current config */
6884 	*old_cfg = *new_cfg;
6885 	old_cfg->etsrec = old_cfg->etscfg;
6886 	ret = i40e_set_dcb_config(&pf->hw);
6887 	if (ret) {
6888 		dev_info(&pf->pdev->dev,
6889 			 "Set DCB Config failed, err %pe aq_err %s\n",
6890 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6891 		goto out;
6892 	}
6893 
6894 	/* Changes in configuration update VEB/VSI */
6895 	i40e_dcb_reconfigure(pf);
6896 out:
6897 	/* In case of reset do not try to resume anything */
6898 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6899 		/* Re-start the VSIs if disabled */
6900 		ret = i40e_resume_port_tx(pf);
6901 		/* In case of error no point in resuming VSIs */
6902 		if (ret)
6903 			goto err;
6904 		i40e_pf_unquiesce_all_vsi(pf);
6905 	}
6906 err:
6907 	return ret;
6908 }
6909 
6910 /**
6911  * i40e_hw_dcb_config - Program new DCBX settings into HW
6912  * @pf: PF being configured
6913  * @new_cfg: New DCBX configuration
6914  *
6915  * Program DCB settings into HW and reconfigure VEB/VSIs on
6916  * given PF
6917  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6918 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6919 {
6920 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6921 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6922 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6923 	struct i40e_dcbx_config *old_cfg;
6924 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6925 	struct i40e_rx_pb_config pb_cfg;
6926 	struct i40e_hw *hw = &pf->hw;
6927 	u8 num_ports = hw->num_ports;
6928 	bool need_reconfig;
6929 	int ret = -EINVAL;
6930 	u8 lltc_map = 0;
6931 	u8 tc_map = 0;
6932 	u8 new_numtc;
6933 	u8 i;
6934 
6935 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6936 	/* Un-pack information to Program ETS HW via shared API
6937 	 * numtc, tcmap
6938 	 * LLTC map
6939 	 * ETS/NON-ETS arbiter mode
6940 	 * max exponent (credit refills)
6941 	 * Total number of ports
6942 	 * PFC priority bit-map
6943 	 * Priority Table
6944 	 * BW % per TC
6945 	 * Arbiter mode between UPs sharing same TC
6946 	 * TSA table (ETS or non-ETS)
6947 	 * EEE enabled or not
6948 	 * MFS TC table
6949 	 */
6950 
6951 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6952 
6953 	memset(&ets_data, 0, sizeof(ets_data));
6954 	for (i = 0; i < new_numtc; i++) {
6955 		tc_map |= BIT(i);
6956 		switch (new_cfg->etscfg.tsatable[i]) {
6957 		case I40E_IEEE_TSA_ETS:
6958 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6959 			ets_data.tc_bw_share_credits[i] =
6960 					new_cfg->etscfg.tcbwtable[i];
6961 			break;
6962 		case I40E_IEEE_TSA_STRICT:
6963 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6964 			lltc_map |= BIT(i);
6965 			ets_data.tc_bw_share_credits[i] =
6966 					I40E_DCB_STRICT_PRIO_CREDITS;
6967 			break;
6968 		default:
6969 			/* Invalid TSA type */
6970 			need_reconfig = false;
6971 			goto out;
6972 		}
6973 	}
6974 
6975 	old_cfg = &hw->local_dcbx_config;
6976 	/* Check if need reconfiguration */
6977 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6978 
6979 	/* If needed, enable/disable frame tagging, disable all VSIs
6980 	 * and suspend port tx
6981 	 */
6982 	if (need_reconfig) {
6983 		/* Enable DCB tagging only when more than one TC */
6984 		if (new_numtc > 1)
6985 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
6986 		else
6987 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
6988 
6989 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6990 		/* Reconfiguration needed quiesce all VSIs */
6991 		i40e_pf_quiesce_all_vsi(pf);
6992 		ret = i40e_suspend_port_tx(pf);
6993 		if (ret)
6994 			goto err;
6995 	}
6996 
6997 	/* Configure Port ETS Tx Scheduler */
6998 	ets_data.tc_valid_bits = tc_map;
6999 	ets_data.tc_strict_priority_flags = lltc_map;
7000 	ret = i40e_aq_config_switch_comp_ets
7001 		(hw, pf->mac_seid, &ets_data,
7002 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7003 	if (ret) {
7004 		dev_info(&pf->pdev->dev,
7005 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7006 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7007 		goto out;
7008 	}
7009 
7010 	/* Configure Rx ETS HW */
7011 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7012 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7013 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7014 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7015 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7016 				   lltc_map);
7017 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7018 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7019 				     prio_type);
7020 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7021 			       new_cfg->etscfg.prioritytable);
7022 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7023 
7024 	/* Configure Rx Packet Buffers in HW */
7025 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7026 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7027 
7028 		mfs_tc[i] = main_vsi->netdev->mtu;
7029 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7030 	}
7031 
7032 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7033 					 false, new_cfg->pfc.pfcenable,
7034 					 mfs_tc, &pb_cfg);
7035 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7036 
7037 	/* Update the local Rx Packet buffer config */
7038 	pf->pb_cfg = pb_cfg;
7039 
7040 	/* Inform the FW about changes to DCB configuration */
7041 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7042 	if (ret) {
7043 		dev_info(&pf->pdev->dev,
7044 			 "DCB Updated failed, err %pe aq_err %s\n",
7045 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7046 		goto out;
7047 	}
7048 
7049 	/* Update the port DCBx configuration */
7050 	*old_cfg = *new_cfg;
7051 
7052 	/* Changes in configuration update VEB/VSI */
7053 	i40e_dcb_reconfigure(pf);
7054 out:
7055 	/* Re-start the VSIs if disabled */
7056 	if (need_reconfig) {
7057 		ret = i40e_resume_port_tx(pf);
7058 
7059 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7060 		/* In case of error no point in resuming VSIs */
7061 		if (ret)
7062 			goto err;
7063 
7064 		/* Wait for the PF's queues to be disabled */
7065 		ret = i40e_pf_wait_queues_disabled(pf);
7066 		if (ret) {
7067 			/* Schedule PF reset to recover */
7068 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7069 			i40e_service_event_schedule(pf);
7070 			goto err;
7071 		} else {
7072 			i40e_pf_unquiesce_all_vsi(pf);
7073 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7074 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7075 		}
7076 		/* registers are set, lets apply */
7077 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7078 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7079 	}
7080 
7081 err:
7082 	return ret;
7083 }
7084 
7085 /**
7086  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7087  * @pf: PF being queried
7088  *
7089  * Set default DCB configuration in case DCB is to be done in SW.
7090  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7091 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7092 {
7093 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7094 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7095 	struct i40e_hw *hw = &pf->hw;
7096 	int err;
7097 
7098 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7099 		/* Update the local cached instance with TC0 ETS */
7100 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7101 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7102 		pf->tmp_cfg.etscfg.maxtcs = 0;
7103 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7104 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7105 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7106 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7107 		/* FW needs one App to configure HW */
7108 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7109 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7110 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7111 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7112 
7113 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7114 	}
7115 
7116 	memset(&ets_data, 0, sizeof(ets_data));
7117 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7118 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7119 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7120 
7121 	/* Enable ETS on the Physical port */
7122 	err = i40e_aq_config_switch_comp_ets
7123 		(hw, pf->mac_seid, &ets_data,
7124 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7125 	if (err) {
7126 		dev_info(&pf->pdev->dev,
7127 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7128 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7129 		err = -ENOENT;
7130 		goto out;
7131 	}
7132 
7133 	/* Update the local cached instance with TC0 ETS */
7134 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7135 	dcb_cfg->etscfg.cbs = 0;
7136 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7137 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7138 
7139 out:
7140 	return err;
7141 }
7142 
7143 /**
7144  * i40e_init_pf_dcb - Initialize DCB configuration
7145  * @pf: PF being configured
7146  *
7147  * Query the current DCB configuration and cache it
7148  * in the hardware structure
7149  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7150 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7151 {
7152 	struct i40e_hw *hw = &pf->hw;
7153 	int err;
7154 
7155 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7156 	 * Also do not enable DCBx if FW LLDP agent is disabled
7157 	 */
7158 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7159 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7160 		err = -EOPNOTSUPP;
7161 		goto out;
7162 	}
7163 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7164 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7165 		err = i40e_dcb_sw_default_config(pf);
7166 		if (err) {
7167 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7168 			goto out;
7169 		}
7170 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7171 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7172 			       DCB_CAP_DCBX_VER_IEEE;
7173 		/* at init capable but disabled */
7174 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7175 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7176 		goto out;
7177 	}
7178 	err = i40e_init_dcb(hw, true);
7179 	if (!err) {
7180 		/* Device/Function is not DCBX capable */
7181 		if ((!hw->func_caps.dcb) ||
7182 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7183 			dev_info(&pf->pdev->dev,
7184 				 "DCBX offload is not supported or is disabled for this PF.\n");
7185 		} else {
7186 			/* When status is not DISABLED then DCBX in FW */
7187 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7188 				       DCB_CAP_DCBX_VER_IEEE;
7189 
7190 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7191 			/* Enable DCB tagging only when more than one TC
7192 			 * or explicitly disable if only one TC
7193 			 */
7194 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7195 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7196 			else
7197 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7198 			dev_dbg(&pf->pdev->dev,
7199 				"DCBX offload is supported for this PF.\n");
7200 		}
7201 	} else if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_EPERM) {
7202 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7203 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7204 	} else {
7205 		dev_info(&pf->pdev->dev,
7206 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7207 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7208 	}
7209 
7210 out:
7211 	return err;
7212 }
7213 #endif /* CONFIG_I40E_DCB */
7214 
i40e_print_link_message_eee(struct i40e_vsi * vsi,const char * speed,const char * fc)7215 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7216 					const char *speed, const char *fc)
7217 {
7218 	struct ethtool_keee kedata;
7219 
7220 	memzero_explicit(&kedata, sizeof(kedata));
7221 	if (vsi->netdev->ethtool_ops->get_eee)
7222 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7223 
7224 	if (!linkmode_empty(kedata.supported))
7225 		netdev_info(vsi->netdev,
7226 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7227 			    speed, fc,
7228 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7229 	else
7230 		netdev_info(vsi->netdev,
7231 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7232 			    speed, fc);
7233 }
7234 
7235 /**
7236  * i40e_print_link_message - print link up or down
7237  * @vsi: the VSI for which link needs a message
7238  * @isup: true of link is up, false otherwise
7239  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7240 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7241 {
7242 	enum i40e_aq_link_speed new_speed;
7243 	struct i40e_pf *pf = vsi->back;
7244 	char *speed = "Unknown";
7245 	char *fc = "Unknown";
7246 	char *fec = "";
7247 	char *req_fec = "";
7248 	char *an = "";
7249 
7250 	if (isup)
7251 		new_speed = pf->hw.phy.link_info.link_speed;
7252 	else
7253 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7254 
7255 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7256 		return;
7257 	vsi->current_isup = isup;
7258 	vsi->current_speed = new_speed;
7259 	if (!isup) {
7260 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7261 		return;
7262 	}
7263 
7264 	/* Warn user if link speed on NPAR enabled partition is not at
7265 	 * least 10GB
7266 	 */
7267 	if (pf->hw.func_caps.npar_enable &&
7268 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7269 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7270 		netdev_warn(vsi->netdev,
7271 			    "The partition detected link speed that is less than 10Gbps\n");
7272 
7273 	switch (pf->hw.phy.link_info.link_speed) {
7274 	case I40E_LINK_SPEED_40GB:
7275 		speed = "40 G";
7276 		break;
7277 	case I40E_LINK_SPEED_20GB:
7278 		speed = "20 G";
7279 		break;
7280 	case I40E_LINK_SPEED_25GB:
7281 		speed = "25 G";
7282 		break;
7283 	case I40E_LINK_SPEED_10GB:
7284 		speed = "10 G";
7285 		break;
7286 	case I40E_LINK_SPEED_5GB:
7287 		speed = "5 G";
7288 		break;
7289 	case I40E_LINK_SPEED_2_5GB:
7290 		speed = "2.5 G";
7291 		break;
7292 	case I40E_LINK_SPEED_1GB:
7293 		speed = "1000 M";
7294 		break;
7295 	case I40E_LINK_SPEED_100MB:
7296 		speed = "100 M";
7297 		break;
7298 	default:
7299 		break;
7300 	}
7301 
7302 	switch (pf->hw.fc.current_mode) {
7303 	case I40E_FC_FULL:
7304 		fc = "RX/TX";
7305 		break;
7306 	case I40E_FC_TX_PAUSE:
7307 		fc = "TX";
7308 		break;
7309 	case I40E_FC_RX_PAUSE:
7310 		fc = "RX";
7311 		break;
7312 	default:
7313 		fc = "None";
7314 		break;
7315 	}
7316 
7317 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7318 		req_fec = "None";
7319 		fec = "None";
7320 		an = "False";
7321 
7322 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7323 			an = "True";
7324 
7325 		if (pf->hw.phy.link_info.fec_info &
7326 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7327 			fec = "CL74 FC-FEC/BASE-R";
7328 		else if (pf->hw.phy.link_info.fec_info &
7329 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7330 			fec = "CL108 RS-FEC";
7331 
7332 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7333 		 * both RS and FC are requested
7334 		 */
7335 		if (vsi->back->hw.phy.link_info.req_fec_info &
7336 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7337 			if (vsi->back->hw.phy.link_info.req_fec_info &
7338 			    I40E_AQ_REQUEST_FEC_RS)
7339 				req_fec = "CL108 RS-FEC";
7340 			else
7341 				req_fec = "CL74 FC-FEC/BASE-R";
7342 		}
7343 		netdev_info(vsi->netdev,
7344 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7345 			    speed, req_fec, fec, an, fc);
7346 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7347 		req_fec = "None";
7348 		fec = "None";
7349 		an = "False";
7350 
7351 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7352 			an = "True";
7353 
7354 		if (pf->hw.phy.link_info.fec_info &
7355 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7356 			fec = "CL74 FC-FEC/BASE-R";
7357 
7358 		if (pf->hw.phy.link_info.req_fec_info &
7359 		    I40E_AQ_REQUEST_FEC_KR)
7360 			req_fec = "CL74 FC-FEC/BASE-R";
7361 
7362 		netdev_info(vsi->netdev,
7363 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7364 			    speed, req_fec, fec, an, fc);
7365 	} else {
7366 		i40e_print_link_message_eee(vsi, speed, fc);
7367 	}
7368 
7369 }
7370 
7371 /**
7372  * i40e_up_complete - Finish the last steps of bringing up a connection
7373  * @vsi: the VSI being configured
7374  **/
i40e_up_complete(struct i40e_vsi * vsi)7375 static int i40e_up_complete(struct i40e_vsi *vsi)
7376 {
7377 	struct i40e_pf *pf = vsi->back;
7378 	int err;
7379 
7380 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7381 		i40e_vsi_configure_msix(vsi);
7382 	else
7383 		i40e_configure_msi_and_legacy(vsi);
7384 
7385 	/* start rings */
7386 	err = i40e_vsi_start_rings(vsi);
7387 	if (err)
7388 		return err;
7389 
7390 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7391 	i40e_napi_enable_all(vsi);
7392 	i40e_vsi_enable_irq(vsi);
7393 
7394 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7395 	    (vsi->netdev)) {
7396 		i40e_print_link_message(vsi, true);
7397 		netif_tx_start_all_queues(vsi->netdev);
7398 		netif_carrier_on(vsi->netdev);
7399 	}
7400 
7401 	/* replay FDIR SB filters */
7402 	if (vsi->type == I40E_VSI_FDIR) {
7403 		/* reset fd counters */
7404 		pf->fd_add_err = 0;
7405 		pf->fd_atr_cnt = 0;
7406 		i40e_fdir_filter_restore(vsi);
7407 	}
7408 
7409 	/* On the next run of the service_task, notify any clients of the new
7410 	 * opened netdev
7411 	 */
7412 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7413 	i40e_service_event_schedule(pf);
7414 
7415 	return 0;
7416 }
7417 
7418 /**
7419  * i40e_vsi_reinit_locked - Reset the VSI
7420  * @vsi: the VSI being configured
7421  *
7422  * Rebuild the ring structs after some configuration
7423  * has changed, e.g. MTU size.
7424  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7425 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7426 {
7427 	struct i40e_pf *pf = vsi->back;
7428 
7429 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7430 		usleep_range(1000, 2000);
7431 	i40e_down(vsi);
7432 
7433 	i40e_up(vsi);
7434 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7435 }
7436 
7437 /**
7438  * i40e_force_link_state - Force the link status
7439  * @pf: board private structure
7440  * @is_up: whether the link state should be forced up or down
7441  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7442 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7443 {
7444 	struct i40e_aq_get_phy_abilities_resp abilities;
7445 	struct i40e_aq_set_phy_config config = {0};
7446 	bool non_zero_phy_type = is_up;
7447 	struct i40e_hw *hw = &pf->hw;
7448 	u64 mask;
7449 	u8 speed;
7450 	int err;
7451 
7452 	/* Card might've been put in an unstable state by other drivers
7453 	 * and applications, which causes incorrect speed values being
7454 	 * set on startup. In order to clear speed registers, we call
7455 	 * get_phy_capabilities twice, once to get initial state of
7456 	 * available speeds, and once to get current PHY config.
7457 	 */
7458 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7459 					   NULL);
7460 	if (err) {
7461 		dev_err(&pf->pdev->dev,
7462 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7463 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7464 		return err;
7465 	}
7466 	speed = abilities.link_speed;
7467 
7468 	/* Get the current phy config */
7469 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7470 					   NULL);
7471 	if (err) {
7472 		dev_err(&pf->pdev->dev,
7473 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7474 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7475 		return err;
7476 	}
7477 
7478 	/* If link needs to go up, but was not forced to go down,
7479 	 * and its speed values are OK, no need for a flap
7480 	 * if non_zero_phy_type was set, still need to force up
7481 	 */
7482 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7483 		non_zero_phy_type = true;
7484 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7485 		return 0;
7486 
7487 	/* To force link we need to set bits for all supported PHY types,
7488 	 * but there are now more than 32, so we need to split the bitmap
7489 	 * across two fields.
7490 	 */
7491 	mask = I40E_PHY_TYPES_BITMASK;
7492 	config.phy_type =
7493 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7494 	config.phy_type_ext =
7495 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7496 	/* Copy the old settings, except of phy_type */
7497 	config.abilities = abilities.abilities;
7498 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7499 		if (is_up)
7500 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7501 		else
7502 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7503 	}
7504 	if (abilities.link_speed != 0)
7505 		config.link_speed = abilities.link_speed;
7506 	else
7507 		config.link_speed = speed;
7508 	config.eee_capability = abilities.eee_capability;
7509 	config.eeer = abilities.eeer_val;
7510 	config.low_power_ctrl = abilities.d3_lpan;
7511 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7512 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7513 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7514 
7515 	if (err) {
7516 		dev_err(&pf->pdev->dev,
7517 			"set phy config ret =  %pe last_status =  %s\n",
7518 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7519 		return err;
7520 	}
7521 
7522 	/* Update the link info */
7523 	err = i40e_update_link_info(hw);
7524 	if (err) {
7525 		/* Wait a little bit (on 40G cards it sometimes takes a really
7526 		 * long time for link to come back from the atomic reset)
7527 		 * and try once more
7528 		 */
7529 		msleep(1000);
7530 		i40e_update_link_info(hw);
7531 	}
7532 
7533 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7534 
7535 	return 0;
7536 }
7537 
7538 /**
7539  * i40e_up - Bring the connection back up after being down
7540  * @vsi: the VSI being configured
7541  **/
i40e_up(struct i40e_vsi * vsi)7542 int i40e_up(struct i40e_vsi *vsi)
7543 {
7544 	int err;
7545 
7546 	if (vsi->type == I40E_VSI_MAIN &&
7547 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7548 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7549 		i40e_force_link_state(vsi->back, true);
7550 
7551 	err = i40e_vsi_configure(vsi);
7552 	if (!err)
7553 		err = i40e_up_complete(vsi);
7554 
7555 	return err;
7556 }
7557 
7558 /**
7559  * i40e_down - Shutdown the connection processing
7560  * @vsi: the VSI being stopped
7561  **/
i40e_down(struct i40e_vsi * vsi)7562 void i40e_down(struct i40e_vsi *vsi)
7563 {
7564 	int i;
7565 
7566 	/* It is assumed that the caller of this function
7567 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7568 	 */
7569 	if (vsi->netdev) {
7570 		netif_carrier_off(vsi->netdev);
7571 		netif_tx_disable(vsi->netdev);
7572 	}
7573 	i40e_vsi_disable_irq(vsi);
7574 	i40e_vsi_stop_rings(vsi);
7575 	if (vsi->type == I40E_VSI_MAIN &&
7576 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7577 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7578 		i40e_force_link_state(vsi->back, false);
7579 	i40e_napi_disable_all(vsi);
7580 
7581 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7582 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7583 		if (i40e_enabled_xdp_vsi(vsi)) {
7584 			/* Make sure that in-progress ndo_xdp_xmit and
7585 			 * ndo_xsk_wakeup calls are completed.
7586 			 */
7587 			synchronize_rcu();
7588 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7589 		}
7590 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7591 	}
7592 
7593 }
7594 
7595 /**
7596  * i40e_validate_mqprio_qopt- validate queue mapping info
7597  * @vsi: the VSI being configured
7598  * @mqprio_qopt: queue parametrs
7599  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7600 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7601 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7602 {
7603 	u64 sum_max_rate = 0;
7604 	u64 max_rate = 0;
7605 	int i;
7606 
7607 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7608 	    mqprio_qopt->qopt.num_tc < 1 ||
7609 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7610 		return -EINVAL;
7611 	for (i = 0; ; i++) {
7612 		if (!mqprio_qopt->qopt.count[i])
7613 			return -EINVAL;
7614 		if (mqprio_qopt->min_rate[i]) {
7615 			dev_err(&vsi->back->pdev->dev,
7616 				"Invalid min tx rate (greater than 0) specified\n");
7617 			return -EINVAL;
7618 		}
7619 		max_rate = mqprio_qopt->max_rate[i];
7620 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7621 		sum_max_rate += max_rate;
7622 
7623 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7624 			break;
7625 		if (mqprio_qopt->qopt.offset[i + 1] !=
7626 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7627 			return -EINVAL;
7628 	}
7629 	if (vsi->num_queue_pairs <
7630 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7631 		dev_err(&vsi->back->pdev->dev,
7632 			"Failed to create traffic channel, insufficient number of queues.\n");
7633 		return -EINVAL;
7634 	}
7635 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7636 		dev_err(&vsi->back->pdev->dev,
7637 			"Invalid max tx rate specified\n");
7638 		return -EINVAL;
7639 	}
7640 	return 0;
7641 }
7642 
7643 /**
7644  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7645  * @vsi: the VSI being configured
7646  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7647 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7648 {
7649 	u16 qcount;
7650 	int i;
7651 
7652 	/* Only TC0 is enabled */
7653 	vsi->tc_config.numtc = 1;
7654 	vsi->tc_config.enabled_tc = 1;
7655 	qcount = min_t(int, vsi->alloc_queue_pairs,
7656 		       i40e_pf_get_max_q_per_tc(vsi->back));
7657 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7658 		/* For the TC that is not enabled set the offset to default
7659 		 * queue and allocate one queue for the given TC.
7660 		 */
7661 		vsi->tc_config.tc_info[i].qoffset = 0;
7662 		if (i == 0)
7663 			vsi->tc_config.tc_info[i].qcount = qcount;
7664 		else
7665 			vsi->tc_config.tc_info[i].qcount = 1;
7666 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7667 	}
7668 }
7669 
7670 /**
7671  * i40e_del_macvlan_filter
7672  * @hw: pointer to the HW structure
7673  * @seid: seid of the channel VSI
7674  * @macaddr: the mac address to apply as a filter
7675  * @aq_err: store the admin Q error
7676  *
7677  * This function deletes a mac filter on the channel VSI which serves as the
7678  * macvlan. Returns 0 on success.
7679  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7680 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7681 				   const u8 *macaddr, int *aq_err)
7682 {
7683 	struct i40e_aqc_remove_macvlan_element_data element;
7684 	int status;
7685 
7686 	memset(&element, 0, sizeof(element));
7687 	ether_addr_copy(element.mac_addr, macaddr);
7688 	element.vlan_tag = 0;
7689 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7690 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7691 	*aq_err = hw->aq.asq_last_status;
7692 
7693 	return status;
7694 }
7695 
7696 /**
7697  * i40e_add_macvlan_filter
7698  * @hw: pointer to the HW structure
7699  * @seid: seid of the channel VSI
7700  * @macaddr: the mac address to apply as a filter
7701  * @aq_err: store the admin Q error
7702  *
7703  * This function adds a mac filter on the channel VSI which serves as the
7704  * macvlan. Returns 0 on success.
7705  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7706 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7707 				   const u8 *macaddr, int *aq_err)
7708 {
7709 	struct i40e_aqc_add_macvlan_element_data element;
7710 	u16 cmd_flags = 0;
7711 	int status;
7712 
7713 	ether_addr_copy(element.mac_addr, macaddr);
7714 	element.vlan_tag = 0;
7715 	element.queue_number = 0;
7716 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7717 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7718 	element.flags = cpu_to_le16(cmd_flags);
7719 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7720 	*aq_err = hw->aq.asq_last_status;
7721 
7722 	return status;
7723 }
7724 
7725 /**
7726  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7727  * @vsi: the VSI we want to access
7728  * @ch: the channel we want to access
7729  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7730 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7731 {
7732 	struct i40e_ring *tx_ring, *rx_ring;
7733 	u16 pf_q;
7734 	int i;
7735 
7736 	for (i = 0; i < ch->num_queue_pairs; i++) {
7737 		pf_q = ch->base_queue + i;
7738 		tx_ring = vsi->tx_rings[pf_q];
7739 		tx_ring->ch = NULL;
7740 		rx_ring = vsi->rx_rings[pf_q];
7741 		rx_ring->ch = NULL;
7742 	}
7743 }
7744 
7745 /**
7746  * i40e_free_macvlan_channels
7747  * @vsi: the VSI we want to access
7748  *
7749  * This function frees the Qs of the channel VSI from
7750  * the stack and also deletes the channel VSIs which
7751  * serve as macvlans.
7752  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7753 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7754 {
7755 	struct i40e_channel *ch, *ch_tmp;
7756 	int ret;
7757 
7758 	if (list_empty(&vsi->macvlan_list))
7759 		return;
7760 
7761 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7762 		struct i40e_vsi *parent_vsi;
7763 
7764 		if (i40e_is_channel_macvlan(ch)) {
7765 			i40e_reset_ch_rings(vsi, ch);
7766 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7767 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7768 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7769 			kfree(ch->fwd);
7770 			ch->fwd = NULL;
7771 		}
7772 
7773 		list_del(&ch->list);
7774 		parent_vsi = ch->parent_vsi;
7775 		if (!parent_vsi || !ch->initialized) {
7776 			kfree(ch);
7777 			continue;
7778 		}
7779 
7780 		/* remove the VSI */
7781 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7782 					     NULL);
7783 		if (ret)
7784 			dev_err(&vsi->back->pdev->dev,
7785 				"unable to remove channel (%d) for parent VSI(%d)\n",
7786 				ch->seid, parent_vsi->seid);
7787 		kfree(ch);
7788 	}
7789 	vsi->macvlan_cnt = 0;
7790 }
7791 
7792 /**
7793  * i40e_fwd_ring_up - bring the macvlan device up
7794  * @vsi: the VSI we want to access
7795  * @vdev: macvlan netdevice
7796  * @fwd: the private fwd structure
7797  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7798 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7799 			    struct i40e_fwd_adapter *fwd)
7800 {
7801 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7802 	int ret = 0, num_tc = 1,  i, aq_err;
7803 	struct i40e_pf *pf = vsi->back;
7804 	struct i40e_hw *hw = &pf->hw;
7805 
7806 	/* Go through the list and find an available channel */
7807 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7808 		if (!i40e_is_channel_macvlan(iter)) {
7809 			iter->fwd = fwd;
7810 			/* record configuration for macvlan interface in vdev */
7811 			for (i = 0; i < num_tc; i++)
7812 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7813 							     i,
7814 							     iter->num_queue_pairs,
7815 							     iter->base_queue);
7816 			for (i = 0; i < iter->num_queue_pairs; i++) {
7817 				struct i40e_ring *tx_ring, *rx_ring;
7818 				u16 pf_q;
7819 
7820 				pf_q = iter->base_queue + i;
7821 
7822 				/* Get to TX ring ptr */
7823 				tx_ring = vsi->tx_rings[pf_q];
7824 				tx_ring->ch = iter;
7825 
7826 				/* Get the RX ring ptr */
7827 				rx_ring = vsi->rx_rings[pf_q];
7828 				rx_ring->ch = iter;
7829 			}
7830 			ch = iter;
7831 			break;
7832 		}
7833 	}
7834 
7835 	if (!ch)
7836 		return -EINVAL;
7837 
7838 	/* Guarantee all rings are updated before we update the
7839 	 * MAC address filter.
7840 	 */
7841 	wmb();
7842 
7843 	/* Add a mac filter */
7844 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7845 	if (ret) {
7846 		/* if we cannot add the MAC rule then disable the offload */
7847 		macvlan_release_l2fw_offload(vdev);
7848 		for (i = 0; i < ch->num_queue_pairs; i++) {
7849 			struct i40e_ring *rx_ring;
7850 			u16 pf_q;
7851 
7852 			pf_q = ch->base_queue + i;
7853 			rx_ring = vsi->rx_rings[pf_q];
7854 			rx_ring->netdev = NULL;
7855 		}
7856 		dev_info(&pf->pdev->dev,
7857 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7858 			  ERR_PTR(ret), libie_aq_str(aq_err));
7859 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7860 	}
7861 
7862 	return ret;
7863 }
7864 
7865 /**
7866  * i40e_setup_macvlans - create the channels which will be macvlans
7867  * @vsi: the VSI we want to access
7868  * @macvlan_cnt: no. of macvlans to be setup
7869  * @qcnt: no. of Qs per macvlan
7870  * @vdev: macvlan netdevice
7871  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7872 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7873 			       struct net_device *vdev)
7874 {
7875 	struct i40e_pf *pf = vsi->back;
7876 	struct i40e_hw *hw = &pf->hw;
7877 	struct i40e_vsi_context ctxt;
7878 	u16 sections, qmap, num_qps;
7879 	struct i40e_channel *ch;
7880 	int i, pow, ret = 0;
7881 	u8 offset = 0;
7882 
7883 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7884 		return -EINVAL;
7885 
7886 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7887 
7888 	/* find the next higher power-of-2 of num queue pairs */
7889 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7890 
7891 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7892 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7893 
7894 	/* Setup context bits for the main VSI */
7895 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7896 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7897 	memset(&ctxt, 0, sizeof(ctxt));
7898 	ctxt.seid = vsi->seid;
7899 	ctxt.pf_num = vsi->back->hw.pf_id;
7900 	ctxt.vf_num = 0;
7901 	ctxt.uplink_seid = vsi->uplink_seid;
7902 	ctxt.info = vsi->info;
7903 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7904 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7905 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7906 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7907 
7908 	/* Reconfigure RSS for main VSI with new max queue count */
7909 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7910 	ret = i40e_vsi_config_rss(vsi);
7911 	if (ret) {
7912 		dev_info(&pf->pdev->dev,
7913 			 "Failed to reconfig RSS for num_queues (%u)\n",
7914 			 vsi->rss_size);
7915 		return ret;
7916 	}
7917 	vsi->reconfig_rss = true;
7918 	dev_dbg(&vsi->back->pdev->dev,
7919 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7920 	vsi->next_base_queue = num_qps;
7921 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7922 
7923 	/* Update the VSI after updating the VSI queue-mapping
7924 	 * information
7925 	 */
7926 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7927 	if (ret) {
7928 		dev_info(&pf->pdev->dev,
7929 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7930 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
7931 		return ret;
7932 	}
7933 	/* update the local VSI info with updated queue map */
7934 	i40e_vsi_update_queue_map(vsi, &ctxt);
7935 	vsi->info.valid_sections = 0;
7936 
7937 	/* Create channels for macvlans */
7938 	INIT_LIST_HEAD(&vsi->macvlan_list);
7939 	for (i = 0; i < macvlan_cnt; i++) {
7940 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7941 		if (!ch) {
7942 			ret = -ENOMEM;
7943 			goto err_free;
7944 		}
7945 		INIT_LIST_HEAD(&ch->list);
7946 		ch->num_queue_pairs = qcnt;
7947 		if (!i40e_setup_channel(pf, vsi, ch)) {
7948 			ret = -EINVAL;
7949 			kfree(ch);
7950 			goto err_free;
7951 		}
7952 		ch->parent_vsi = vsi;
7953 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7954 		vsi->macvlan_cnt++;
7955 		list_add_tail(&ch->list, &vsi->macvlan_list);
7956 	}
7957 
7958 	return ret;
7959 
7960 err_free:
7961 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7962 	i40e_free_macvlan_channels(vsi);
7963 
7964 	return ret;
7965 }
7966 
7967 /**
7968  * i40e_fwd_add - configure macvlans
7969  * @netdev: net device to configure
7970  * @vdev: macvlan netdevice
7971  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7972 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7973 {
7974 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7975 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7976 	struct i40e_vsi *vsi = np->vsi;
7977 	struct i40e_pf *pf = vsi->back;
7978 	struct i40e_fwd_adapter *fwd;
7979 	int avail_macvlan, ret;
7980 
7981 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
7982 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7983 		return ERR_PTR(-EINVAL);
7984 	}
7985 	if (i40e_is_tc_mqprio_enabled(pf)) {
7986 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7987 		return ERR_PTR(-EINVAL);
7988 	}
7989 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7990 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7991 		return ERR_PTR(-EINVAL);
7992 	}
7993 
7994 	/* The macvlan device has to be a single Q device so that the
7995 	 * tc_to_txq field can be reused to pick the tx queue.
7996 	 */
7997 	if (netif_is_multiqueue(vdev))
7998 		return ERR_PTR(-ERANGE);
7999 
8000 	if (!vsi->macvlan_cnt) {
8001 		/* reserve bit 0 for the pf device */
8002 		set_bit(0, vsi->fwd_bitmask);
8003 
8004 		/* Try to reserve as many queues as possible for macvlans. First
8005 		 * reserve 3/4th of max vectors, then half, then quarter and
8006 		 * calculate Qs per macvlan as you go
8007 		 */
8008 		vectors = pf->num_lan_msix;
8009 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8010 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8011 			q_per_macvlan = 4;
8012 			macvlan_cnt = (vectors - 32) / 4;
8013 		} else if (vectors <= 64 && vectors > 32) {
8014 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8015 			q_per_macvlan = 2;
8016 			macvlan_cnt = (vectors - 16) / 2;
8017 		} else if (vectors <= 32 && vectors > 16) {
8018 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8019 			q_per_macvlan = 1;
8020 			macvlan_cnt = vectors - 16;
8021 		} else if (vectors <= 16 && vectors > 8) {
8022 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8023 			q_per_macvlan = 1;
8024 			macvlan_cnt = vectors - 8;
8025 		} else {
8026 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8027 			q_per_macvlan = 1;
8028 			macvlan_cnt = vectors - 1;
8029 		}
8030 
8031 		if (macvlan_cnt == 0)
8032 			return ERR_PTR(-EBUSY);
8033 
8034 		/* Quiesce VSI queues */
8035 		i40e_quiesce_vsi(vsi);
8036 
8037 		/* sets up the macvlans but does not "enable" them */
8038 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8039 					  vdev);
8040 		if (ret)
8041 			return ERR_PTR(ret);
8042 
8043 		/* Unquiesce VSI */
8044 		i40e_unquiesce_vsi(vsi);
8045 	}
8046 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8047 					    vsi->macvlan_cnt);
8048 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8049 		return ERR_PTR(-EBUSY);
8050 
8051 	/* create the fwd struct */
8052 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8053 	if (!fwd)
8054 		return ERR_PTR(-ENOMEM);
8055 
8056 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8057 	fwd->bit_no = avail_macvlan;
8058 	netdev_set_sb_channel(vdev, avail_macvlan);
8059 	fwd->netdev = vdev;
8060 
8061 	if (!netif_running(netdev))
8062 		return fwd;
8063 
8064 	/* Set fwd ring up */
8065 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8066 	if (ret) {
8067 		/* unbind the queues and drop the subordinate channel config */
8068 		netdev_unbind_sb_channel(netdev, vdev);
8069 		netdev_set_sb_channel(vdev, 0);
8070 
8071 		kfree(fwd);
8072 		return ERR_PTR(-EINVAL);
8073 	}
8074 
8075 	return fwd;
8076 }
8077 
8078 /**
8079  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8080  * @vsi: the VSI we want to access
8081  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8082 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8083 {
8084 	struct i40e_channel *ch, *ch_tmp;
8085 	struct i40e_pf *pf = vsi->back;
8086 	struct i40e_hw *hw = &pf->hw;
8087 	int aq_err, ret = 0;
8088 
8089 	if (list_empty(&vsi->macvlan_list))
8090 		return;
8091 
8092 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8093 		if (i40e_is_channel_macvlan(ch)) {
8094 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8095 						      i40e_channel_mac(ch),
8096 						      &aq_err);
8097 			if (!ret) {
8098 				/* Reset queue contexts */
8099 				i40e_reset_ch_rings(vsi, ch);
8100 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8101 				netdev_unbind_sb_channel(vsi->netdev,
8102 							 ch->fwd->netdev);
8103 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8104 				kfree(ch->fwd);
8105 				ch->fwd = NULL;
8106 			}
8107 		}
8108 	}
8109 }
8110 
8111 /**
8112  * i40e_fwd_del - delete macvlan interfaces
8113  * @netdev: net device to configure
8114  * @vdev: macvlan netdevice
8115  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8116 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8117 {
8118 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8119 	struct i40e_fwd_adapter *fwd = vdev;
8120 	struct i40e_channel *ch, *ch_tmp;
8121 	struct i40e_vsi *vsi = np->vsi;
8122 	struct i40e_pf *pf = vsi->back;
8123 	struct i40e_hw *hw = &pf->hw;
8124 	int aq_err, ret = 0;
8125 
8126 	/* Find the channel associated with the macvlan and del mac filter */
8127 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8128 		if (i40e_is_channel_macvlan(ch) &&
8129 		    ether_addr_equal(i40e_channel_mac(ch),
8130 				     fwd->netdev->dev_addr)) {
8131 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8132 						      i40e_channel_mac(ch),
8133 						      &aq_err);
8134 			if (!ret) {
8135 				/* Reset queue contexts */
8136 				i40e_reset_ch_rings(vsi, ch);
8137 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8138 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8139 				netdev_set_sb_channel(fwd->netdev, 0);
8140 				kfree(ch->fwd);
8141 				ch->fwd = NULL;
8142 			} else {
8143 				dev_info(&pf->pdev->dev,
8144 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8145 					  ERR_PTR(ret), libie_aq_str(aq_err));
8146 			}
8147 			break;
8148 		}
8149 	}
8150 }
8151 
8152 /**
8153  * i40e_setup_tc - configure multiple traffic classes
8154  * @netdev: net device to configure
8155  * @type_data: tc offload data
8156  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8157 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8158 {
8159 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8160 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8161 	struct i40e_vsi *vsi = np->vsi;
8162 	struct i40e_pf *pf = vsi->back;
8163 	u8 enabled_tc = 0, num_tc, hw;
8164 	bool need_reset = false;
8165 	int old_queue_pairs;
8166 	int ret = -EINVAL;
8167 	u16 mode;
8168 	int i;
8169 
8170 	old_queue_pairs = vsi->num_queue_pairs;
8171 	num_tc = mqprio_qopt->qopt.num_tc;
8172 	hw = mqprio_qopt->qopt.hw;
8173 	mode = mqprio_qopt->mode;
8174 	if (!hw) {
8175 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8176 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8177 		goto config_tc;
8178 	}
8179 
8180 	/* Check if MFP enabled */
8181 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8182 		netdev_info(netdev,
8183 			    "Configuring TC not supported in MFP mode\n");
8184 		return ret;
8185 	}
8186 	switch (mode) {
8187 	case TC_MQPRIO_MODE_DCB:
8188 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8189 
8190 		/* Check if DCB enabled to continue */
8191 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8192 			netdev_info(netdev,
8193 				    "DCB is not enabled for adapter\n");
8194 			return ret;
8195 		}
8196 
8197 		/* Check whether tc count is within enabled limit */
8198 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8199 			netdev_info(netdev,
8200 				    "TC count greater than enabled on link for adapter\n");
8201 			return ret;
8202 		}
8203 		break;
8204 	case TC_MQPRIO_MODE_CHANNEL:
8205 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8206 			netdev_info(netdev,
8207 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8208 			return ret;
8209 		}
8210 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8211 			return ret;
8212 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8213 		if (ret)
8214 			return ret;
8215 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8216 		       sizeof(*mqprio_qopt));
8217 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8218 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8219 		break;
8220 	default:
8221 		return -EINVAL;
8222 	}
8223 
8224 config_tc:
8225 	/* Generate TC map for number of tc requested */
8226 	for (i = 0; i < num_tc; i++)
8227 		enabled_tc |= BIT(i);
8228 
8229 	/* Requesting same TC configuration as already enabled */
8230 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8231 	    mode != TC_MQPRIO_MODE_CHANNEL)
8232 		return 0;
8233 
8234 	/* Quiesce VSI queues */
8235 	i40e_quiesce_vsi(vsi);
8236 
8237 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8238 		i40e_remove_queue_channels(vsi);
8239 
8240 	/* Configure VSI for enabled TCs */
8241 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8242 	if (ret) {
8243 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8244 			    vsi->seid);
8245 		need_reset = true;
8246 		goto exit;
8247 	} else if (enabled_tc &&
8248 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8249 		netdev_info(netdev,
8250 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8251 			    vsi->tc_config.tc_info[0].qcount);
8252 		ret = -EINVAL;
8253 		need_reset = true;
8254 		goto exit;
8255 	}
8256 
8257 	dev_info(&vsi->back->pdev->dev,
8258 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8259 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8260 
8261 	if (i40e_is_tc_mqprio_enabled(pf)) {
8262 		if (vsi->mqprio_qopt.max_rate[0]) {
8263 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8264 						  vsi->mqprio_qopt.max_rate[0]);
8265 
8266 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8267 			if (!ret) {
8268 				u64 credits = max_tx_rate;
8269 
8270 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8271 				dev_dbg(&vsi->back->pdev->dev,
8272 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8273 					max_tx_rate,
8274 					credits,
8275 					vsi->seid);
8276 			} else {
8277 				need_reset = true;
8278 				goto exit;
8279 			}
8280 		}
8281 		ret = i40e_configure_queue_channels(vsi);
8282 		if (ret) {
8283 			vsi->num_queue_pairs = old_queue_pairs;
8284 			netdev_info(netdev,
8285 				    "Failed configuring queue channels\n");
8286 			need_reset = true;
8287 			goto exit;
8288 		}
8289 	}
8290 
8291 exit:
8292 	/* Reset the configuration data to defaults, only TC0 is enabled */
8293 	if (need_reset) {
8294 		i40e_vsi_set_default_tc_config(vsi);
8295 		need_reset = false;
8296 	}
8297 
8298 	/* Unquiesce VSI */
8299 	i40e_unquiesce_vsi(vsi);
8300 	return ret;
8301 }
8302 
8303 /**
8304  * i40e_set_cld_element - sets cloud filter element data
8305  * @filter: cloud filter rule
8306  * @cld: ptr to cloud filter element data
8307  *
8308  * This is helper function to copy data into cloud filter element
8309  **/
8310 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8311 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8312 		     struct i40e_aqc_cloud_filters_element_data *cld)
8313 {
8314 	u32 ipa;
8315 	int i;
8316 
8317 	memset(cld, 0, sizeof(*cld));
8318 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8319 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8320 
8321 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8322 		return;
8323 
8324 	if (filter->n_proto == ETH_P_IPV6) {
8325 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8326 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8327 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8328 
8329 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8330 		}
8331 	} else {
8332 		ipa = be32_to_cpu(filter->dst_ipv4);
8333 
8334 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8335 	}
8336 
8337 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8338 
8339 	/* tenant_id is not supported by FW now, once the support is enabled
8340 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8341 	 */
8342 	if (filter->tenant_id)
8343 		return;
8344 }
8345 
8346 /**
8347  * i40e_add_del_cloud_filter - Add/del cloud filter
8348  * @vsi: pointer to VSI
8349  * @filter: cloud filter rule
8350  * @add: if true, add, if false, delete
8351  *
8352  * Add or delete a cloud filter for a specific flow spec.
8353  * Returns 0 if the filter were successfully added.
8354  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8355 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8356 			      struct i40e_cloud_filter *filter, bool add)
8357 {
8358 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8359 	struct i40e_pf *pf = vsi->back;
8360 	int ret;
8361 	static const u16 flag_table[128] = {
8362 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8363 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8364 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8365 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8366 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8367 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8368 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8369 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8370 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8371 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8372 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8373 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8374 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8375 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8376 	};
8377 
8378 	if (filter->flags >= ARRAY_SIZE(flag_table))
8379 		return -EIO;
8380 
8381 	memset(&cld_filter, 0, sizeof(cld_filter));
8382 
8383 	/* copy element needed to add cloud filter from filter */
8384 	i40e_set_cld_element(filter, &cld_filter);
8385 
8386 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8387 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8388 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8389 
8390 	if (filter->n_proto == ETH_P_IPV6)
8391 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8392 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8393 	else
8394 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8395 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8396 
8397 	if (add)
8398 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8399 						&cld_filter, 1);
8400 	else
8401 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8402 						&cld_filter, 1);
8403 	if (ret)
8404 		dev_dbg(&pf->pdev->dev,
8405 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8406 			add ? "add" : "delete", filter->dst_port, ret,
8407 			pf->hw.aq.asq_last_status);
8408 	else
8409 		dev_info(&pf->pdev->dev,
8410 			 "%s cloud filter for VSI: %d\n",
8411 			 add ? "Added" : "Deleted", filter->seid);
8412 	return ret;
8413 }
8414 
8415 /**
8416  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8417  * @vsi: pointer to VSI
8418  * @filter: cloud filter rule
8419  * @add: if true, add, if false, delete
8420  *
8421  * Add or delete a cloud filter for a specific flow spec using big buffer.
8422  * Returns 0 if the filter were successfully added.
8423  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8424 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8425 				      struct i40e_cloud_filter *filter,
8426 				      bool add)
8427 {
8428 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8429 	struct i40e_pf *pf = vsi->back;
8430 	int ret;
8431 
8432 	/* Both (src/dst) valid mac_addr are not supported */
8433 	if ((is_valid_ether_addr(filter->dst_mac) &&
8434 	     is_valid_ether_addr(filter->src_mac)) ||
8435 	    (is_multicast_ether_addr(filter->dst_mac) &&
8436 	     is_multicast_ether_addr(filter->src_mac)))
8437 		return -EOPNOTSUPP;
8438 
8439 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8440 	 * ports are not supported via big buffer now.
8441 	 */
8442 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8443 		return -EOPNOTSUPP;
8444 
8445 	/* adding filter using src_port/src_ip is not supported at this stage */
8446 	if (filter->src_port ||
8447 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8448 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8449 		return -EOPNOTSUPP;
8450 
8451 	memset(&cld_filter, 0, sizeof(cld_filter));
8452 
8453 	/* copy element needed to add cloud filter from filter */
8454 	i40e_set_cld_element(filter, &cld_filter.element);
8455 
8456 	if (is_valid_ether_addr(filter->dst_mac) ||
8457 	    is_valid_ether_addr(filter->src_mac) ||
8458 	    is_multicast_ether_addr(filter->dst_mac) ||
8459 	    is_multicast_ether_addr(filter->src_mac)) {
8460 		/* MAC + IP : unsupported mode */
8461 		if (filter->dst_ipv4)
8462 			return -EOPNOTSUPP;
8463 
8464 		/* since we validated that L4 port must be valid before
8465 		 * we get here, start with respective "flags" value
8466 		 * and update if vlan is present or not
8467 		 */
8468 		cld_filter.element.flags =
8469 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8470 
8471 		if (filter->vlan_id) {
8472 			cld_filter.element.flags =
8473 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8474 		}
8475 
8476 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8477 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8478 		cld_filter.element.flags =
8479 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8480 		if (filter->n_proto == ETH_P_IPV6)
8481 			cld_filter.element.flags |=
8482 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8483 		else
8484 			cld_filter.element.flags |=
8485 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8486 	} else {
8487 		dev_err(&pf->pdev->dev,
8488 			"either mac or ip has to be valid for cloud filter\n");
8489 		return -EINVAL;
8490 	}
8491 
8492 	/* Now copy L4 port in Byte 6..7 in general fields */
8493 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8494 						be16_to_cpu(filter->dst_port);
8495 
8496 	if (add) {
8497 		/* Validate current device switch mode, change if necessary */
8498 		ret = i40e_validate_and_set_switch_mode(vsi);
8499 		if (ret) {
8500 			dev_err(&pf->pdev->dev,
8501 				"failed to set switch mode, ret %d\n",
8502 				ret);
8503 			return ret;
8504 		}
8505 
8506 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8507 						   &cld_filter, 1);
8508 	} else {
8509 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8510 						   &cld_filter, 1);
8511 	}
8512 
8513 	if (ret)
8514 		dev_dbg(&pf->pdev->dev,
8515 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8516 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8517 	else
8518 		dev_info(&pf->pdev->dev,
8519 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8520 			 add ? "add" : "delete", filter->seid,
8521 			 ntohs(filter->dst_port));
8522 	return ret;
8523 }
8524 
8525 /**
8526  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8527  * @vsi: Pointer to VSI
8528  * @f: Pointer to struct flow_cls_offload
8529  * @filter: Pointer to cloud filter structure
8530  *
8531  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8532 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8533 				 struct flow_cls_offload *f,
8534 				 struct i40e_cloud_filter *filter)
8535 {
8536 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8537 	struct flow_dissector *dissector = rule->match.dissector;
8538 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8539 	struct i40e_pf *pf = vsi->back;
8540 	u8 field_flags = 0;
8541 
8542 	if (dissector->used_keys &
8543 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8544 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8545 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8546 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8547 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8548 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8549 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8550 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8551 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8552 			dissector->used_keys);
8553 		return -EOPNOTSUPP;
8554 	}
8555 
8556 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8557 		struct flow_match_enc_keyid match;
8558 
8559 		flow_rule_match_enc_keyid(rule, &match);
8560 		if (match.mask->keyid != 0)
8561 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8562 
8563 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8564 	}
8565 
8566 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8567 		struct flow_match_basic match;
8568 
8569 		flow_rule_match_basic(rule, &match);
8570 		n_proto_key = ntohs(match.key->n_proto);
8571 		n_proto_mask = ntohs(match.mask->n_proto);
8572 
8573 		if (n_proto_key == ETH_P_ALL) {
8574 			n_proto_key = 0;
8575 			n_proto_mask = 0;
8576 		}
8577 		filter->n_proto = n_proto_key & n_proto_mask;
8578 		filter->ip_proto = match.key->ip_proto;
8579 	}
8580 
8581 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8582 		struct flow_match_eth_addrs match;
8583 
8584 		flow_rule_match_eth_addrs(rule, &match);
8585 
8586 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8587 		if (!is_zero_ether_addr(match.mask->dst)) {
8588 			if (is_broadcast_ether_addr(match.mask->dst)) {
8589 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8590 			} else {
8591 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8592 					match.mask->dst);
8593 				return -EIO;
8594 			}
8595 		}
8596 
8597 		if (!is_zero_ether_addr(match.mask->src)) {
8598 			if (is_broadcast_ether_addr(match.mask->src)) {
8599 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8600 			} else {
8601 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8602 					match.mask->src);
8603 				return -EIO;
8604 			}
8605 		}
8606 		ether_addr_copy(filter->dst_mac, match.key->dst);
8607 		ether_addr_copy(filter->src_mac, match.key->src);
8608 	}
8609 
8610 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8611 		struct flow_match_vlan match;
8612 
8613 		flow_rule_match_vlan(rule, &match);
8614 		if (match.mask->vlan_id) {
8615 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8616 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8617 
8618 			} else {
8619 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8620 					match.mask->vlan_id);
8621 				return -EIO;
8622 			}
8623 		}
8624 
8625 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8626 	}
8627 
8628 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8629 		struct flow_match_control match;
8630 
8631 		flow_rule_match_control(rule, &match);
8632 		addr_type = match.key->addr_type;
8633 
8634 		if (flow_rule_has_control_flags(match.mask->flags,
8635 						f->common.extack))
8636 			return -EOPNOTSUPP;
8637 	}
8638 
8639 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8640 		struct flow_match_ipv4_addrs match;
8641 
8642 		flow_rule_match_ipv4_addrs(rule, &match);
8643 		if (match.mask->dst) {
8644 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8645 				field_flags |= I40E_CLOUD_FIELD_IIP;
8646 			} else {
8647 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8648 					&match.mask->dst);
8649 				return -EIO;
8650 			}
8651 		}
8652 
8653 		if (match.mask->src) {
8654 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8655 				field_flags |= I40E_CLOUD_FIELD_IIP;
8656 			} else {
8657 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8658 					&match.mask->src);
8659 				return -EIO;
8660 			}
8661 		}
8662 
8663 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8664 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8665 			return -EIO;
8666 		}
8667 		filter->dst_ipv4 = match.key->dst;
8668 		filter->src_ipv4 = match.key->src;
8669 	}
8670 
8671 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8672 		struct flow_match_ipv6_addrs match;
8673 
8674 		flow_rule_match_ipv6_addrs(rule, &match);
8675 
8676 		/* src and dest IPV6 address should not be LOOPBACK
8677 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8678 		 */
8679 		if (ipv6_addr_loopback(&match.key->dst) ||
8680 		    ipv6_addr_loopback(&match.key->src)) {
8681 			dev_err(&pf->pdev->dev,
8682 				"Bad ipv6, addr is LOOPBACK\n");
8683 			return -EIO;
8684 		}
8685 		if (!ipv6_addr_any(&match.mask->dst) ||
8686 		    !ipv6_addr_any(&match.mask->src))
8687 			field_flags |= I40E_CLOUD_FIELD_IIP;
8688 
8689 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8690 		       sizeof(filter->src_ipv6));
8691 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8692 		       sizeof(filter->dst_ipv6));
8693 	}
8694 
8695 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8696 		struct flow_match_ports match;
8697 
8698 		flow_rule_match_ports(rule, &match);
8699 		if (match.mask->src) {
8700 			if (match.mask->src == cpu_to_be16(0xffff)) {
8701 				field_flags |= I40E_CLOUD_FIELD_IIP;
8702 			} else {
8703 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8704 					be16_to_cpu(match.mask->src));
8705 				return -EIO;
8706 			}
8707 		}
8708 
8709 		if (match.mask->dst) {
8710 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8711 				field_flags |= I40E_CLOUD_FIELD_IIP;
8712 			} else {
8713 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8714 					be16_to_cpu(match.mask->dst));
8715 				return -EIO;
8716 			}
8717 		}
8718 
8719 		filter->dst_port = match.key->dst;
8720 		filter->src_port = match.key->src;
8721 
8722 		switch (filter->ip_proto) {
8723 		case IPPROTO_TCP:
8724 		case IPPROTO_UDP:
8725 			break;
8726 		default:
8727 			dev_err(&pf->pdev->dev,
8728 				"Only UDP and TCP transport are supported\n");
8729 			return -EINVAL;
8730 		}
8731 	}
8732 	filter->flags = field_flags;
8733 	return 0;
8734 }
8735 
8736 /**
8737  * i40e_handle_tclass: Forward to a traffic class on the device
8738  * @vsi: Pointer to VSI
8739  * @tc: traffic class index on the device
8740  * @filter: Pointer to cloud filter structure
8741  *
8742  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8743 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8744 			      struct i40e_cloud_filter *filter)
8745 {
8746 	struct i40e_channel *ch, *ch_tmp;
8747 
8748 	/* direct to a traffic class on the same device */
8749 	if (tc == 0) {
8750 		filter->seid = vsi->seid;
8751 		return 0;
8752 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8753 		if (!filter->dst_port) {
8754 			dev_err(&vsi->back->pdev->dev,
8755 				"Specify destination port to direct to traffic class that is not default\n");
8756 			return -EINVAL;
8757 		}
8758 		if (list_empty(&vsi->ch_list))
8759 			return -EINVAL;
8760 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8761 					 list) {
8762 			if (ch->seid == vsi->tc_seid_map[tc])
8763 				filter->seid = ch->seid;
8764 		}
8765 		return 0;
8766 	}
8767 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8768 	return -EINVAL;
8769 }
8770 
8771 /**
8772  * i40e_configure_clsflower - Configure tc flower filters
8773  * @vsi: Pointer to VSI
8774  * @cls_flower: Pointer to struct flow_cls_offload
8775  *
8776  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8777 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8778 				    struct flow_cls_offload *cls_flower)
8779 {
8780 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8781 	struct i40e_cloud_filter *filter = NULL;
8782 	struct i40e_pf *pf = vsi->back;
8783 	int err = 0;
8784 
8785 	if (tc < 0) {
8786 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8787 		return -EOPNOTSUPP;
8788 	}
8789 
8790 	if (!tc) {
8791 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8792 		return -EINVAL;
8793 	}
8794 
8795 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8796 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8797 		return -EBUSY;
8798 
8799 	if (pf->fdir_pf_active_filters ||
8800 	    (!hlist_empty(&pf->fdir_filter_list))) {
8801 		dev_err(&vsi->back->pdev->dev,
8802 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8803 		return -EINVAL;
8804 	}
8805 
8806 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8807 		dev_err(&vsi->back->pdev->dev,
8808 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8809 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8810 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8811 	}
8812 
8813 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8814 	if (!filter)
8815 		return -ENOMEM;
8816 
8817 	filter->cookie = cls_flower->cookie;
8818 
8819 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8820 	if (err < 0)
8821 		goto err;
8822 
8823 	err = i40e_handle_tclass(vsi, tc, filter);
8824 	if (err < 0)
8825 		goto err;
8826 
8827 	/* Add cloud filter */
8828 	if (filter->dst_port)
8829 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8830 	else
8831 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8832 
8833 	if (err) {
8834 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8835 			err);
8836 		goto err;
8837 	}
8838 
8839 	/* add filter to the ordered list */
8840 	INIT_HLIST_NODE(&filter->cloud_node);
8841 
8842 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8843 
8844 	pf->num_cloud_filters++;
8845 
8846 	return err;
8847 err:
8848 	kfree(filter);
8849 	return err;
8850 }
8851 
8852 /**
8853  * i40e_find_cloud_filter - Find the could filter in the list
8854  * @vsi: Pointer to VSI
8855  * @cookie: filter specific cookie
8856  *
8857  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8858 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8859 							unsigned long *cookie)
8860 {
8861 	struct i40e_cloud_filter *filter = NULL;
8862 	struct hlist_node *node2;
8863 
8864 	hlist_for_each_entry_safe(filter, node2,
8865 				  &vsi->back->cloud_filter_list, cloud_node)
8866 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8867 			return filter;
8868 	return NULL;
8869 }
8870 
8871 /**
8872  * i40e_delete_clsflower - Remove tc flower filters
8873  * @vsi: Pointer to VSI
8874  * @cls_flower: Pointer to struct flow_cls_offload
8875  *
8876  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8877 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8878 				 struct flow_cls_offload *cls_flower)
8879 {
8880 	struct i40e_cloud_filter *filter = NULL;
8881 	struct i40e_pf *pf = vsi->back;
8882 	int err = 0;
8883 
8884 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8885 
8886 	if (!filter)
8887 		return -EINVAL;
8888 
8889 	hash_del(&filter->cloud_node);
8890 
8891 	if (filter->dst_port)
8892 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8893 	else
8894 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8895 
8896 	kfree(filter);
8897 	if (err) {
8898 		dev_err(&pf->pdev->dev,
8899 			"Failed to delete cloud filter, err %pe\n",
8900 			ERR_PTR(err));
8901 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8902 	}
8903 
8904 	pf->num_cloud_filters--;
8905 	if (!pf->num_cloud_filters)
8906 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8907 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8908 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8909 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8910 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8911 		}
8912 	return 0;
8913 }
8914 
8915 /**
8916  * i40e_setup_tc_cls_flower - flower classifier offloads
8917  * @np: net device to configure
8918  * @cls_flower: offload data
8919  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8920 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8921 				    struct flow_cls_offload *cls_flower)
8922 {
8923 	struct i40e_vsi *vsi = np->vsi;
8924 
8925 	switch (cls_flower->command) {
8926 	case FLOW_CLS_REPLACE:
8927 		return i40e_configure_clsflower(vsi, cls_flower);
8928 	case FLOW_CLS_DESTROY:
8929 		return i40e_delete_clsflower(vsi, cls_flower);
8930 	case FLOW_CLS_STATS:
8931 		return -EOPNOTSUPP;
8932 	default:
8933 		return -EOPNOTSUPP;
8934 	}
8935 }
8936 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8937 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8938 				  void *cb_priv)
8939 {
8940 	struct i40e_netdev_priv *np = cb_priv;
8941 
8942 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8943 		return -EOPNOTSUPP;
8944 
8945 	switch (type) {
8946 	case TC_SETUP_CLSFLOWER:
8947 		return i40e_setup_tc_cls_flower(np, type_data);
8948 
8949 	default:
8950 		return -EOPNOTSUPP;
8951 	}
8952 }
8953 
8954 static LIST_HEAD(i40e_block_cb_list);
8955 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8956 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8957 			   void *type_data)
8958 {
8959 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8960 
8961 	switch (type) {
8962 	case TC_SETUP_QDISC_MQPRIO:
8963 		return i40e_setup_tc(netdev, type_data);
8964 	case TC_SETUP_BLOCK:
8965 		return flow_block_cb_setup_simple(type_data,
8966 						  &i40e_block_cb_list,
8967 						  i40e_setup_tc_block_cb,
8968 						  np, np, true);
8969 	default:
8970 		return -EOPNOTSUPP;
8971 	}
8972 }
8973 
8974 /**
8975  * i40e_open - Called when a network interface is made active
8976  * @netdev: network interface device structure
8977  *
8978  * The open entry point is called when a network interface is made
8979  * active by the system (IFF_UP).  At this point all resources needed
8980  * for transmit and receive operations are allocated, the interrupt
8981  * handler is registered with the OS, the netdev watchdog subtask is
8982  * enabled, and the stack is notified that the interface is ready.
8983  *
8984  * Returns 0 on success, negative value on failure
8985  **/
i40e_open(struct net_device * netdev)8986 int i40e_open(struct net_device *netdev)
8987 {
8988 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8989 	struct i40e_vsi *vsi = np->vsi;
8990 	struct i40e_pf *pf = vsi->back;
8991 	int err;
8992 
8993 	/* disallow open during test or if eeprom is broken */
8994 	if (test_bit(__I40E_TESTING, pf->state) ||
8995 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8996 		return -EBUSY;
8997 
8998 	netif_carrier_off(netdev);
8999 
9000 	if (i40e_force_link_state(pf, true))
9001 		return -EAGAIN;
9002 
9003 	err = i40e_vsi_open(vsi);
9004 	if (err)
9005 		return err;
9006 
9007 	/* configure global TSO hardware offload settings */
9008 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9009 						       TCP_FLAG_FIN) >> 16);
9010 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9011 						       TCP_FLAG_FIN |
9012 						       TCP_FLAG_CWR) >> 16);
9013 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9014 	udp_tunnel_get_rx_info(netdev);
9015 
9016 	return 0;
9017 }
9018 
9019 /**
9020  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9021  * @vsi: vsi structure
9022  *
9023  * This updates netdev's number of tx/rx queues
9024  *
9025  * Returns status of setting tx/rx queues
9026  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9027 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9028 {
9029 	int ret;
9030 
9031 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9032 					   vsi->num_queue_pairs);
9033 	if (ret)
9034 		return ret;
9035 
9036 	return netif_set_real_num_tx_queues(vsi->netdev,
9037 					    vsi->num_queue_pairs);
9038 }
9039 
9040 /**
9041  * i40e_vsi_open -
9042  * @vsi: the VSI to open
9043  *
9044  * Finish initialization of the VSI.
9045  *
9046  * Returns 0 on success, negative value on failure
9047  *
9048  * Note: expects to be called while under rtnl_lock()
9049  **/
i40e_vsi_open(struct i40e_vsi * vsi)9050 int i40e_vsi_open(struct i40e_vsi *vsi)
9051 {
9052 	struct i40e_pf *pf = vsi->back;
9053 	char int_name[I40E_INT_NAME_STR_LEN];
9054 	int err;
9055 
9056 	/* allocate descriptors */
9057 	err = i40e_vsi_setup_tx_resources(vsi);
9058 	if (err)
9059 		goto err_setup_tx;
9060 	err = i40e_vsi_setup_rx_resources(vsi);
9061 	if (err)
9062 		goto err_setup_rx;
9063 
9064 	err = i40e_vsi_configure(vsi);
9065 	if (err)
9066 		goto err_setup_rx;
9067 
9068 	if (vsi->netdev) {
9069 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9070 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9071 		err = i40e_vsi_request_irq(vsi, int_name);
9072 		if (err)
9073 			goto err_setup_rx;
9074 
9075 		/* Notify the stack of the actual queue counts. */
9076 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9077 		if (err)
9078 			goto err_set_queues;
9079 
9080 	} else if (vsi->type == I40E_VSI_FDIR) {
9081 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9082 			 dev_driver_string(&pf->pdev->dev),
9083 			 dev_name(&pf->pdev->dev));
9084 		err = i40e_vsi_request_irq(vsi, int_name);
9085 		if (err)
9086 			goto err_setup_rx;
9087 
9088 	} else {
9089 		err = -EINVAL;
9090 		goto err_setup_rx;
9091 	}
9092 
9093 	err = i40e_up_complete(vsi);
9094 	if (err)
9095 		goto err_up_complete;
9096 
9097 	return 0;
9098 
9099 err_up_complete:
9100 	i40e_down(vsi);
9101 err_set_queues:
9102 	i40e_vsi_free_irq(vsi);
9103 err_setup_rx:
9104 	i40e_vsi_free_rx_resources(vsi);
9105 err_setup_tx:
9106 	i40e_vsi_free_tx_resources(vsi);
9107 	if (vsi->type == I40E_VSI_MAIN)
9108 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9109 
9110 	return err;
9111 }
9112 
9113 /**
9114  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9115  * @pf: Pointer to PF
9116  *
9117  * This function destroys the hlist where all the Flow Director
9118  * filters were saved.
9119  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9120 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9121 {
9122 	struct i40e_fdir_filter *filter;
9123 	struct i40e_flex_pit *pit_entry, *tmp;
9124 	struct hlist_node *node2;
9125 
9126 	hlist_for_each_entry_safe(filter, node2,
9127 				  &pf->fdir_filter_list, fdir_node) {
9128 		hlist_del(&filter->fdir_node);
9129 		kfree(filter);
9130 	}
9131 
9132 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9133 		list_del(&pit_entry->list);
9134 		kfree(pit_entry);
9135 	}
9136 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9137 
9138 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9139 		list_del(&pit_entry->list);
9140 		kfree(pit_entry);
9141 	}
9142 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9143 
9144 	pf->fdir_pf_active_filters = 0;
9145 	i40e_reset_fdir_filter_cnt(pf);
9146 
9147 	/* Reprogram the default input set for TCP/IPv4 */
9148 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9149 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9150 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9151 
9152 	/* Reprogram the default input set for TCP/IPv6 */
9153 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP,
9154 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9155 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9156 
9157 	/* Reprogram the default input set for UDP/IPv4 */
9158 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_UDP,
9159 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9160 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9161 
9162 	/* Reprogram the default input set for UDP/IPv6 */
9163 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_UDP,
9164 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9165 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9166 
9167 	/* Reprogram the default input set for SCTP/IPv4 */
9168 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP,
9169 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9170 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9171 
9172 	/* Reprogram the default input set for SCTP/IPv6 */
9173 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP,
9174 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9175 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9176 
9177 	/* Reprogram the default input set for Other/IPv4 */
9178 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_OTHER,
9179 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9180 
9181 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV4,
9182 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9183 
9184 	/* Reprogram the default input set for Other/IPv6 */
9185 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_OTHER,
9186 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9187 
9188 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV6,
9189 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9190 }
9191 
9192 /**
9193  * i40e_cloud_filter_exit - Cleans up the cloud filters
9194  * @pf: Pointer to PF
9195  *
9196  * This function destroys the hlist where all the cloud filters
9197  * were saved.
9198  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9199 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9200 {
9201 	struct i40e_cloud_filter *cfilter;
9202 	struct hlist_node *node;
9203 
9204 	hlist_for_each_entry_safe(cfilter, node,
9205 				  &pf->cloud_filter_list, cloud_node) {
9206 		hlist_del(&cfilter->cloud_node);
9207 		kfree(cfilter);
9208 	}
9209 	pf->num_cloud_filters = 0;
9210 
9211 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9212 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9213 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9214 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9215 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9216 	}
9217 }
9218 
9219 /**
9220  * i40e_close - Disables a network interface
9221  * @netdev: network interface device structure
9222  *
9223  * The close entry point is called when an interface is de-activated
9224  * by the OS.  The hardware is still under the driver's control, but
9225  * this netdev interface is disabled.
9226  *
9227  * Returns 0, this is not allowed to fail
9228  **/
i40e_close(struct net_device * netdev)9229 int i40e_close(struct net_device *netdev)
9230 {
9231 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9232 	struct i40e_vsi *vsi = np->vsi;
9233 
9234 	i40e_vsi_close(vsi);
9235 
9236 	return 0;
9237 }
9238 
9239 /**
9240  * i40e_do_reset - Start a PF or Core Reset sequence
9241  * @pf: board private structure
9242  * @reset_flags: which reset is requested
9243  * @lock_acquired: indicates whether or not the lock has been acquired
9244  * before this function was called.
9245  *
9246  * The essential difference in resets is that the PF Reset
9247  * doesn't clear the packet buffers, doesn't reset the PE
9248  * firmware, and doesn't bother the other PFs on the chip.
9249  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9250 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9251 {
9252 	struct i40e_vsi *vsi;
9253 	u32 val;
9254 	int i;
9255 
9256 	/* do the biggest reset indicated */
9257 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9258 
9259 		/* Request a Global Reset
9260 		 *
9261 		 * This will start the chip's countdown to the actual full
9262 		 * chip reset event, and a warning interrupt to be sent
9263 		 * to all PFs, including the requestor.  Our handler
9264 		 * for the warning interrupt will deal with the shutdown
9265 		 * and recovery of the switch setup.
9266 		 */
9267 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9268 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9269 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9270 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9271 
9272 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9273 
9274 		/* Request a Core Reset
9275 		 *
9276 		 * Same as Global Reset, except does *not* include the MAC/PHY
9277 		 */
9278 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9279 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9280 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9281 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9282 		i40e_flush(&pf->hw);
9283 
9284 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9285 
9286 		/* Request a PF Reset
9287 		 *
9288 		 * Resets only the PF-specific registers
9289 		 *
9290 		 * This goes directly to the tear-down and rebuild of
9291 		 * the switch, since we need to do all the recovery as
9292 		 * for the Core Reset.
9293 		 */
9294 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9295 		i40e_handle_reset_warning(pf, lock_acquired);
9296 
9297 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9298 		/* Request a PF Reset
9299 		 *
9300 		 * Resets PF and reinitializes PFs VSI.
9301 		 */
9302 		i40e_prep_for_reset(pf);
9303 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9304 		dev_info(&pf->pdev->dev,
9305 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9306 			 "FW LLDP is disabled\n" :
9307 			 "FW LLDP is enabled\n");
9308 
9309 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9310 		/* Find the VSI(s) that requested a re-init */
9311 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9312 
9313 		i40e_pf_for_each_vsi(pf, i, vsi) {
9314 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9315 					       vsi->state))
9316 				i40e_vsi_reinit_locked(vsi);
9317 		}
9318 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9319 		/* Find the VSI(s) that needs to be brought down */
9320 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9321 
9322 		i40e_pf_for_each_vsi(pf, i, vsi) {
9323 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9324 					       vsi->state)) {
9325 				set_bit(__I40E_VSI_DOWN, vsi->state);
9326 				i40e_down(vsi);
9327 			}
9328 		}
9329 	} else {
9330 		dev_info(&pf->pdev->dev,
9331 			 "bad reset request 0x%08x\n", reset_flags);
9332 	}
9333 }
9334 
9335 #ifdef CONFIG_I40E_DCB
9336 /**
9337  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9338  * @pf: board private structure
9339  * @old_cfg: current DCB config
9340  * @new_cfg: new DCB config
9341  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9342 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9343 			    struct i40e_dcbx_config *old_cfg,
9344 			    struct i40e_dcbx_config *new_cfg)
9345 {
9346 	bool need_reconfig = false;
9347 
9348 	/* Check if ETS configuration has changed */
9349 	if (memcmp(&new_cfg->etscfg,
9350 		   &old_cfg->etscfg,
9351 		   sizeof(new_cfg->etscfg))) {
9352 		/* If Priority Table has changed reconfig is needed */
9353 		if (memcmp(&new_cfg->etscfg.prioritytable,
9354 			   &old_cfg->etscfg.prioritytable,
9355 			   sizeof(new_cfg->etscfg.prioritytable))) {
9356 			need_reconfig = true;
9357 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9358 		}
9359 
9360 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9361 			   &old_cfg->etscfg.tcbwtable,
9362 			   sizeof(new_cfg->etscfg.tcbwtable)))
9363 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9364 
9365 		if (memcmp(&new_cfg->etscfg.tsatable,
9366 			   &old_cfg->etscfg.tsatable,
9367 			   sizeof(new_cfg->etscfg.tsatable)))
9368 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9369 	}
9370 
9371 	/* Check if PFC configuration has changed */
9372 	if (memcmp(&new_cfg->pfc,
9373 		   &old_cfg->pfc,
9374 		   sizeof(new_cfg->pfc))) {
9375 		need_reconfig = true;
9376 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9377 	}
9378 
9379 	/* Check if APP Table has changed */
9380 	if (memcmp(&new_cfg->app,
9381 		   &old_cfg->app,
9382 		   sizeof(new_cfg->app))) {
9383 		need_reconfig = true;
9384 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9385 	}
9386 
9387 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9388 	return need_reconfig;
9389 }
9390 
9391 /**
9392  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9393  * @pf: board private structure
9394  * @e: event info posted on ARQ
9395  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9396 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9397 				  struct i40e_arq_event_info *e)
9398 {
9399 	struct i40e_aqc_lldp_get_mib *mib = libie_aq_raw(&e->desc);
9400 	struct i40e_hw *hw = &pf->hw;
9401 	struct i40e_dcbx_config tmp_dcbx_cfg;
9402 	bool need_reconfig = false;
9403 	int ret = 0;
9404 	u8 type;
9405 
9406 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9407 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9408 	    (hw->phy.link_info.link_speed &
9409 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9410 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9411 		/* let firmware decide if the DCB should be disabled */
9412 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9413 
9414 	/* Not DCB capable or capability disabled */
9415 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9416 		return ret;
9417 
9418 	/* Ignore if event is not for Nearest Bridge */
9419 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9420 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9421 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9422 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9423 		return ret;
9424 
9425 	/* Check MIB Type and return if event for Remote MIB update */
9426 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9427 	dev_dbg(&pf->pdev->dev,
9428 		"LLDP event mib type %s\n", type ? "remote" : "local");
9429 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9430 		/* Update the remote cached instance and return */
9431 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9432 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9433 				&hw->remote_dcbx_config);
9434 		goto exit;
9435 	}
9436 
9437 	/* Store the old configuration */
9438 	tmp_dcbx_cfg = hw->local_dcbx_config;
9439 
9440 	/* Reset the old DCBx configuration data */
9441 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9442 	/* Get updated DCBX data from firmware */
9443 	ret = i40e_get_dcb_config(&pf->hw);
9444 	if (ret) {
9445 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9446 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9447 		    (hw->phy.link_info.link_speed &
9448 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9449 			dev_warn(&pf->pdev->dev,
9450 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9451 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9452 		} else {
9453 			dev_info(&pf->pdev->dev,
9454 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9455 				 ERR_PTR(ret),
9456 				 libie_aq_str(pf->hw.aq.asq_last_status));
9457 		}
9458 		goto exit;
9459 	}
9460 
9461 	/* No change detected in DCBX configs */
9462 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9463 		    sizeof(tmp_dcbx_cfg))) {
9464 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9465 		goto exit;
9466 	}
9467 
9468 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9469 					       &hw->local_dcbx_config);
9470 
9471 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9472 
9473 	if (!need_reconfig)
9474 		goto exit;
9475 
9476 	/* Enable DCB tagging only when more than one TC */
9477 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9478 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9479 	else
9480 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9481 
9482 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9483 	/* Reconfiguration needed quiesce all VSIs */
9484 	i40e_pf_quiesce_all_vsi(pf);
9485 
9486 	/* Changes in configuration update VEB/VSI */
9487 	i40e_dcb_reconfigure(pf);
9488 
9489 	ret = i40e_resume_port_tx(pf);
9490 
9491 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9492 	/* In case of error no point in resuming VSIs */
9493 	if (ret)
9494 		goto exit;
9495 
9496 	/* Wait for the PF's queues to be disabled */
9497 	ret = i40e_pf_wait_queues_disabled(pf);
9498 	if (ret) {
9499 		/* Schedule PF reset to recover */
9500 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9501 		i40e_service_event_schedule(pf);
9502 	} else {
9503 		i40e_pf_unquiesce_all_vsi(pf);
9504 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9505 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9506 	}
9507 
9508 exit:
9509 	return ret;
9510 }
9511 #endif /* CONFIG_I40E_DCB */
9512 
9513 /**
9514  * i40e_do_reset_safe - Protected reset path for userland calls.
9515  * @pf: board private structure
9516  * @reset_flags: which reset is requested
9517  *
9518  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9519 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9520 {
9521 	rtnl_lock();
9522 	i40e_do_reset(pf, reset_flags, true);
9523 	rtnl_unlock();
9524 }
9525 
9526 /**
9527  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9528  * @pf: board private structure
9529  * @e: event info posted on ARQ
9530  *
9531  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9532  * and VF queues
9533  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9534 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9535 					   struct i40e_arq_event_info *e)
9536 {
9537 	struct i40e_aqc_lan_overflow *data = libie_aq_raw(&e->desc);
9538 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9539 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9540 	struct i40e_hw *hw = &pf->hw;
9541 	struct i40e_vf *vf;
9542 	u16 vf_id;
9543 
9544 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9545 		queue, qtx_ctl);
9546 
9547 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9548 	    I40E_QTX_CTL_VF_QUEUE)
9549 		return;
9550 
9551 	/* Queue belongs to VF, find the VF and issue VF reset */
9552 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9553 	vf_id -= hw->func_caps.vf_base_id;
9554 	vf = &pf->vf[vf_id];
9555 	i40e_vc_notify_vf_reset(vf);
9556 	/* Allow VF to process pending reset notification */
9557 	msleep(20);
9558 	i40e_reset_vf(vf, false);
9559 }
9560 
9561 /**
9562  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9563  * @pf: board private structure
9564  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9565 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9566 {
9567 	u32 val, fcnt_prog;
9568 
9569 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9570 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9571 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9572 	return fcnt_prog;
9573 }
9574 
9575 /**
9576  * i40e_get_global_fd_count - Get total FD filters programmed on device
9577  * @pf: board private structure
9578  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9579 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9580 {
9581 	u32 val, fcnt_prog;
9582 
9583 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9584 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9585 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9586 	return fcnt_prog;
9587 }
9588 
9589 /**
9590  * i40e_reenable_fdir_sb - Restore FDir SB capability
9591  * @pf: board private structure
9592  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9593 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9594 {
9595 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9596 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9597 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9598 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9599 }
9600 
9601 /**
9602  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9603  * @pf: board private structure
9604  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9605 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9606 {
9607 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9608 		/* ATR uses the same filtering logic as SB rules. It only
9609 		 * functions properly if the input set mask is at the default
9610 		 * settings. It is safe to restore the default input set
9611 		 * because there are no active TCPv4 filter rules.
9612 		 */
9613 		i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9614 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9615 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9616 
9617 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9618 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9619 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9620 	}
9621 }
9622 
9623 /**
9624  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9625  * @pf: board private structure
9626  * @filter: FDir filter to remove
9627  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9628 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9629 				       struct i40e_fdir_filter *filter)
9630 {
9631 	/* Update counters */
9632 	pf->fdir_pf_active_filters--;
9633 	pf->fd_inv = 0;
9634 
9635 	switch (filter->flow_type) {
9636 	case TCP_V4_FLOW:
9637 		pf->fd_tcp4_filter_cnt--;
9638 		break;
9639 	case UDP_V4_FLOW:
9640 		pf->fd_udp4_filter_cnt--;
9641 		break;
9642 	case SCTP_V4_FLOW:
9643 		pf->fd_sctp4_filter_cnt--;
9644 		break;
9645 	case TCP_V6_FLOW:
9646 		pf->fd_tcp6_filter_cnt--;
9647 		break;
9648 	case UDP_V6_FLOW:
9649 		pf->fd_udp6_filter_cnt--;
9650 		break;
9651 	case SCTP_V6_FLOW:
9652 		pf->fd_udp6_filter_cnt--;
9653 		break;
9654 	case IP_USER_FLOW:
9655 		switch (filter->ipl4_proto) {
9656 		case IPPROTO_TCP:
9657 			pf->fd_tcp4_filter_cnt--;
9658 			break;
9659 		case IPPROTO_UDP:
9660 			pf->fd_udp4_filter_cnt--;
9661 			break;
9662 		case IPPROTO_SCTP:
9663 			pf->fd_sctp4_filter_cnt--;
9664 			break;
9665 		case IPPROTO_IP:
9666 			pf->fd_ip4_filter_cnt--;
9667 			break;
9668 		}
9669 		break;
9670 	case IPV6_USER_FLOW:
9671 		switch (filter->ipl4_proto) {
9672 		case IPPROTO_TCP:
9673 			pf->fd_tcp6_filter_cnt--;
9674 			break;
9675 		case IPPROTO_UDP:
9676 			pf->fd_udp6_filter_cnt--;
9677 			break;
9678 		case IPPROTO_SCTP:
9679 			pf->fd_sctp6_filter_cnt--;
9680 			break;
9681 		case IPPROTO_IP:
9682 			pf->fd_ip6_filter_cnt--;
9683 			break;
9684 		}
9685 		break;
9686 	}
9687 
9688 	/* Remove the filter from the list and free memory */
9689 	hlist_del(&filter->fdir_node);
9690 	kfree(filter);
9691 }
9692 
9693 /**
9694  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9695  * @pf: board private structure
9696  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9697 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9698 {
9699 	struct i40e_fdir_filter *filter;
9700 	u32 fcnt_prog, fcnt_avail;
9701 	struct hlist_node *node;
9702 
9703 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9704 		return;
9705 
9706 	/* Check if we have enough room to re-enable FDir SB capability. */
9707 	fcnt_prog = i40e_get_global_fd_count(pf);
9708 	fcnt_avail = pf->fdir_pf_filter_count;
9709 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9710 	    (pf->fd_add_err == 0) ||
9711 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9712 		i40e_reenable_fdir_sb(pf);
9713 
9714 	/* We should wait for even more space before re-enabling ATR.
9715 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9716 	 * rules active.
9717 	 */
9718 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9719 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9720 		i40e_reenable_fdir_atr(pf);
9721 
9722 	/* if hw had a problem adding a filter, delete it */
9723 	if (pf->fd_inv > 0) {
9724 		hlist_for_each_entry_safe(filter, node,
9725 					  &pf->fdir_filter_list, fdir_node)
9726 			if (filter->fd_id == pf->fd_inv)
9727 				i40e_delete_invalid_filter(pf, filter);
9728 	}
9729 }
9730 
9731 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9732 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9733 /**
9734  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9735  * @pf: board private structure
9736  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9737 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9738 {
9739 	unsigned long min_flush_time;
9740 	int flush_wait_retry = 50;
9741 	bool disable_atr = false;
9742 	int fd_room;
9743 	int reg;
9744 
9745 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9746 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9747 		return;
9748 
9749 	/* If the flush is happening too quick and we have mostly SB rules we
9750 	 * should not re-enable ATR for some time.
9751 	 */
9752 	min_flush_time = pf->fd_flush_timestamp +
9753 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9754 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9755 
9756 	if (!(time_after(jiffies, min_flush_time)) &&
9757 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9758 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9759 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9760 		disable_atr = true;
9761 	}
9762 
9763 	pf->fd_flush_timestamp = jiffies;
9764 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9765 	/* flush all filters */
9766 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9767 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9768 	i40e_flush(&pf->hw);
9769 	pf->fd_flush_cnt++;
9770 	pf->fd_add_err = 0;
9771 	do {
9772 		/* Check FD flush status every 5-6msec */
9773 		usleep_range(5000, 6000);
9774 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9775 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9776 			break;
9777 	} while (flush_wait_retry--);
9778 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9779 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9780 	} else {
9781 		/* replay sideband filters */
9782 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9783 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9784 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9785 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9786 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9787 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9788 	}
9789 }
9790 
9791 /**
9792  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9793  * @pf: board private structure
9794  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9795 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9796 {
9797 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9798 }
9799 
9800 /**
9801  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9802  * @pf: board private structure
9803  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9804 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9805 {
9806 
9807 	/* if interface is down do nothing */
9808 	if (test_bit(__I40E_DOWN, pf->state))
9809 		return;
9810 
9811 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9812 		i40e_fdir_flush_and_replay(pf);
9813 
9814 	i40e_fdir_check_and_reenable(pf);
9815 
9816 }
9817 
9818 /**
9819  * i40e_vsi_link_event - notify VSI of a link event
9820  * @vsi: vsi to be notified
9821  * @link_up: link up or down
9822  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9823 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9824 {
9825 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9826 		return;
9827 
9828 	switch (vsi->type) {
9829 	case I40E_VSI_MAIN:
9830 		if (!vsi->netdev || !vsi->netdev_registered)
9831 			break;
9832 
9833 		if (link_up) {
9834 			netif_carrier_on(vsi->netdev);
9835 			netif_tx_wake_all_queues(vsi->netdev);
9836 		} else {
9837 			netif_carrier_off(vsi->netdev);
9838 			netif_tx_stop_all_queues(vsi->netdev);
9839 		}
9840 		break;
9841 
9842 	case I40E_VSI_SRIOV:
9843 	case I40E_VSI_VMDQ2:
9844 	case I40E_VSI_CTRL:
9845 	case I40E_VSI_IWARP:
9846 	case I40E_VSI_MIRROR:
9847 	default:
9848 		/* there is no notification for other VSIs */
9849 		break;
9850 	}
9851 }
9852 
9853 /**
9854  * i40e_veb_link_event - notify elements on the veb of a link event
9855  * @veb: veb to be notified
9856  * @link_up: link up or down
9857  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9858 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9859 {
9860 	struct i40e_vsi *vsi;
9861 	struct i40e_pf *pf;
9862 	int i;
9863 
9864 	if (!veb || !veb->pf)
9865 		return;
9866 	pf = veb->pf;
9867 
9868 	/* Send link event to contained VSIs */
9869 	i40e_pf_for_each_vsi(pf, i, vsi)
9870 		if (vsi->uplink_seid == veb->seid)
9871 			i40e_vsi_link_event(vsi, link_up);
9872 }
9873 
9874 /**
9875  * i40e_link_event - Update netif_carrier status
9876  * @pf: board private structure
9877  **/
i40e_link_event(struct i40e_pf * pf)9878 static void i40e_link_event(struct i40e_pf *pf)
9879 {
9880 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9881 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9882 	u8 new_link_speed, old_link_speed;
9883 	bool new_link, old_link;
9884 	int status;
9885 #ifdef CONFIG_I40E_DCB
9886 	int err;
9887 #endif /* CONFIG_I40E_DCB */
9888 
9889 	/* set this to force the get_link_status call to refresh state */
9890 	pf->hw.phy.get_link_info = true;
9891 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9892 	status = i40e_get_link_status(&pf->hw, &new_link);
9893 
9894 	/* On success, disable temp link polling */
9895 	if (status == 0) {
9896 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9897 	} else {
9898 		/* Enable link polling temporarily until i40e_get_link_status
9899 		 * returns 0
9900 		 */
9901 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9902 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9903 			status);
9904 		return;
9905 	}
9906 
9907 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9908 	new_link_speed = pf->hw.phy.link_info.link_speed;
9909 
9910 	if (new_link == old_link &&
9911 	    new_link_speed == old_link_speed &&
9912 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9913 	     new_link == netif_carrier_ok(vsi->netdev)))
9914 		return;
9915 
9916 	if (!new_link && old_link)
9917 		pf->link_down_events++;
9918 
9919 	i40e_print_link_message(vsi, new_link);
9920 
9921 	/* Notify the base of the switch tree connected to
9922 	 * the link.  Floating VEBs are not notified.
9923 	 */
9924 	if (veb)
9925 		i40e_veb_link_event(veb, new_link);
9926 	else
9927 		i40e_vsi_link_event(vsi, new_link);
9928 
9929 	if (pf->vf)
9930 		i40e_vc_notify_link_state(pf);
9931 
9932 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9933 		i40e_ptp_set_increment(pf);
9934 #ifdef CONFIG_I40E_DCB
9935 	if (new_link == old_link)
9936 		return;
9937 	/* Not SW DCB so firmware will take care of default settings */
9938 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9939 		return;
9940 
9941 	/* We cover here only link down, as after link up in case of SW DCB
9942 	 * SW LLDP agent will take care of setting it up
9943 	 */
9944 	if (!new_link) {
9945 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9946 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9947 		err = i40e_dcb_sw_default_config(pf);
9948 		if (err) {
9949 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9950 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9951 		} else {
9952 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9953 				       DCB_CAP_DCBX_VER_IEEE;
9954 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9955 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9956 		}
9957 	}
9958 #endif /* CONFIG_I40E_DCB */
9959 }
9960 
9961 /**
9962  * i40e_watchdog_subtask - periodic checks not using event driven response
9963  * @pf: board private structure
9964  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9965 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9966 {
9967 	struct i40e_vsi *vsi;
9968 	struct i40e_veb *veb;
9969 	int i;
9970 
9971 	/* if interface is down do nothing */
9972 	if (test_bit(__I40E_DOWN, pf->state) ||
9973 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9974 		return;
9975 
9976 	/* make sure we don't do these things too often */
9977 	if (time_before(jiffies, (pf->service_timer_previous +
9978 				  pf->service_timer_period)))
9979 		return;
9980 	pf->service_timer_previous = jiffies;
9981 
9982 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
9983 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9984 		i40e_link_event(pf);
9985 
9986 	/* Update the stats for active netdevs so the network stack
9987 	 * can look at updated numbers whenever it cares to
9988 	 */
9989 	i40e_pf_for_each_vsi(pf, i, vsi)
9990 		if (vsi->netdev)
9991 			i40e_update_stats(vsi);
9992 
9993 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
9994 		/* Update the stats for the active switching components */
9995 		i40e_pf_for_each_veb(pf, i, veb)
9996 			i40e_update_veb_stats(veb);
9997 	}
9998 
9999 	i40e_ptp_rx_hang(pf);
10000 	i40e_ptp_tx_hang(pf);
10001 }
10002 
10003 /**
10004  * i40e_reset_subtask - Set up for resetting the device and driver
10005  * @pf: board private structure
10006  **/
i40e_reset_subtask(struct i40e_pf * pf)10007 static void i40e_reset_subtask(struct i40e_pf *pf)
10008 {
10009 	u32 reset_flags = 0;
10010 
10011 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10012 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10013 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10014 	}
10015 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10016 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10017 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10018 	}
10019 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10020 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10021 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10022 	}
10023 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10024 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10025 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10026 	}
10027 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10028 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10029 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10030 	}
10031 
10032 	/* If there's a recovery already waiting, it takes
10033 	 * precedence before starting a new reset sequence.
10034 	 */
10035 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10036 		i40e_prep_for_reset(pf);
10037 		i40e_reset(pf);
10038 		i40e_rebuild(pf, false, false);
10039 	}
10040 
10041 	/* If we're already down or resetting, just bail */
10042 	if (reset_flags &&
10043 	    !test_bit(__I40E_DOWN, pf->state) &&
10044 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10045 		i40e_do_reset(pf, reset_flags, false);
10046 	}
10047 }
10048 
10049 /**
10050  * i40e_handle_link_event - Handle link event
10051  * @pf: board private structure
10052  * @e: event info posted on ARQ
10053  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10054 static void i40e_handle_link_event(struct i40e_pf *pf,
10055 				   struct i40e_arq_event_info *e)
10056 {
10057 	struct i40e_aqc_get_link_status *status = libie_aq_raw(&e->desc);
10058 
10059 	/* Do a new status request to re-enable LSE reporting
10060 	 * and load new status information into the hw struct
10061 	 * This completely ignores any state information
10062 	 * in the ARQ event info, instead choosing to always
10063 	 * issue the AQ update link status command.
10064 	 */
10065 	i40e_link_event(pf);
10066 
10067 	/* Check if module meets thermal requirements */
10068 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10069 		dev_err(&pf->pdev->dev,
10070 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10071 		dev_err(&pf->pdev->dev,
10072 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10073 	} else {
10074 		/* check for unqualified module, if link is down, suppress
10075 		 * the message if link was forced to be down.
10076 		 */
10077 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10078 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10079 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10080 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10081 			dev_err(&pf->pdev->dev,
10082 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10083 			dev_err(&pf->pdev->dev,
10084 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10085 		}
10086 	}
10087 }
10088 
10089 /**
10090  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10091  * @pf: board private structure
10092  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10093 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10094 {
10095 	struct i40e_arq_event_info event;
10096 	struct i40e_hw *hw = &pf->hw;
10097 	u16 pending, i = 0;
10098 	u16 opcode;
10099 	u32 oldval;
10100 	int ret;
10101 	u32 val;
10102 
10103 	/* Do not run clean AQ when PF reset fails */
10104 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10105 		return;
10106 
10107 	/* check for error indications */
10108 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10109 	oldval = val;
10110 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10111 		if (hw->debug_mask & I40E_DEBUG_AQ)
10112 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10113 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10114 	}
10115 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10116 		if (hw->debug_mask & I40E_DEBUG_AQ)
10117 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10118 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10119 		pf->arq_overflows++;
10120 	}
10121 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10122 		if (hw->debug_mask & I40E_DEBUG_AQ)
10123 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10124 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10125 	}
10126 	if (oldval != val)
10127 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10128 
10129 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10130 	oldval = val;
10131 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10132 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10133 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10134 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10135 	}
10136 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10137 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10138 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10139 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10140 	}
10141 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10142 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10143 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10144 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10145 	}
10146 	if (oldval != val)
10147 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10148 
10149 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10150 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10151 	if (!event.msg_buf)
10152 		return;
10153 
10154 	do {
10155 		ret = i40e_clean_arq_element(hw, &event, &pending);
10156 		if (ret == -EALREADY)
10157 			break;
10158 		else if (ret) {
10159 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10160 			break;
10161 		}
10162 
10163 		opcode = le16_to_cpu(event.desc.opcode);
10164 		switch (opcode) {
10165 
10166 		case i40e_aqc_opc_get_link_status:
10167 			rtnl_lock();
10168 			i40e_handle_link_event(pf, &event);
10169 			rtnl_unlock();
10170 			break;
10171 		case i40e_aqc_opc_send_msg_to_pf:
10172 			ret = i40e_vc_process_vf_msg(pf,
10173 					le16_to_cpu(event.desc.retval),
10174 					le32_to_cpu(event.desc.cookie_high),
10175 					le32_to_cpu(event.desc.cookie_low),
10176 					event.msg_buf,
10177 					event.msg_len);
10178 			break;
10179 		case i40e_aqc_opc_lldp_update_mib:
10180 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10181 #ifdef CONFIG_I40E_DCB
10182 			rtnl_lock();
10183 			i40e_handle_lldp_event(pf, &event);
10184 			rtnl_unlock();
10185 #endif /* CONFIG_I40E_DCB */
10186 			break;
10187 		case i40e_aqc_opc_event_lan_overflow:
10188 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10189 			i40e_handle_lan_overflow_event(pf, &event);
10190 			break;
10191 		case i40e_aqc_opc_send_msg_to_peer:
10192 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10193 			break;
10194 		case i40e_aqc_opc_nvm_erase:
10195 		case i40e_aqc_opc_nvm_update:
10196 		case i40e_aqc_opc_oem_post_update:
10197 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10198 				   "ARQ NVM operation 0x%04x completed\n",
10199 				   opcode);
10200 			break;
10201 		default:
10202 			dev_info(&pf->pdev->dev,
10203 				 "ARQ: Unknown event 0x%04x ignored\n",
10204 				 opcode);
10205 			break;
10206 		}
10207 	} while (i++ < I40E_AQ_WORK_LIMIT);
10208 
10209 	if (i < I40E_AQ_WORK_LIMIT)
10210 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10211 
10212 	/* re-enable Admin queue interrupt cause */
10213 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10214 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10215 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10216 	i40e_flush(hw);
10217 
10218 	kfree(event.msg_buf);
10219 }
10220 
10221 /**
10222  * i40e_verify_eeprom - make sure eeprom is good to use
10223  * @pf: board private structure
10224  **/
i40e_verify_eeprom(struct i40e_pf * pf)10225 static void i40e_verify_eeprom(struct i40e_pf *pf)
10226 {
10227 	int err;
10228 
10229 	err = i40e_diag_eeprom_test(&pf->hw);
10230 	if (err) {
10231 		/* retry in case of garbage read */
10232 		err = i40e_diag_eeprom_test(&pf->hw);
10233 		if (err) {
10234 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10235 				 err);
10236 			set_bit(__I40E_BAD_EEPROM, pf->state);
10237 		}
10238 	}
10239 
10240 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10241 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10242 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10243 	}
10244 }
10245 
10246 /**
10247  * i40e_enable_pf_switch_lb
10248  * @pf: pointer to the PF structure
10249  *
10250  * enable switch loop back or die - no point in a return value
10251  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10252 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10253 {
10254 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10255 	struct i40e_vsi_context ctxt;
10256 	int ret;
10257 
10258 	ctxt.seid = pf->main_vsi_seid;
10259 	ctxt.pf_num = pf->hw.pf_id;
10260 	ctxt.vf_num = 0;
10261 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10262 	if (ret) {
10263 		dev_info(&pf->pdev->dev,
10264 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10265 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10266 		return;
10267 	}
10268 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10269 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10270 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10271 
10272 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10273 	if (ret) {
10274 		dev_info(&pf->pdev->dev,
10275 			 "update vsi switch failed, err %pe aq_err %s\n",
10276 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10277 	}
10278 }
10279 
10280 /**
10281  * i40e_disable_pf_switch_lb
10282  * @pf: pointer to the PF structure
10283  *
10284  * disable switch loop back or die - no point in a return value
10285  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10286 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10287 {
10288 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10289 	struct i40e_vsi_context ctxt;
10290 	int ret;
10291 
10292 	ctxt.seid = pf->main_vsi_seid;
10293 	ctxt.pf_num = pf->hw.pf_id;
10294 	ctxt.vf_num = 0;
10295 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10296 	if (ret) {
10297 		dev_info(&pf->pdev->dev,
10298 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10299 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10300 		return;
10301 	}
10302 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10303 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10304 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10305 
10306 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10307 	if (ret) {
10308 		dev_info(&pf->pdev->dev,
10309 			 "update vsi switch failed, err %pe aq_err %s\n",
10310 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10311 	}
10312 }
10313 
10314 /**
10315  * i40e_config_bridge_mode - Configure the HW bridge mode
10316  * @veb: pointer to the bridge instance
10317  *
10318  * Configure the loop back mode for the LAN VSI that is downlink to the
10319  * specified HW bridge instance. It is expected this function is called
10320  * when a new HW bridge is instantiated.
10321  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10322 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10323 {
10324 	struct i40e_pf *pf = veb->pf;
10325 
10326 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10327 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10328 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10329 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10330 		i40e_disable_pf_switch_lb(pf);
10331 	else
10332 		i40e_enable_pf_switch_lb(pf);
10333 }
10334 
10335 /**
10336  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10337  * @veb: pointer to the VEB instance
10338  *
10339  * This is a function that builds the attached VSIs. We track the connections
10340  * through our own index numbers because the seid's from the HW could change
10341  * across the reset.
10342  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10343 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10344 {
10345 	struct i40e_vsi *ctl_vsi = NULL;
10346 	struct i40e_pf *pf = veb->pf;
10347 	struct i40e_vsi *vsi;
10348 	int v, ret;
10349 
10350 	/* As we do not maintain PV (port virtualizer) switch element then
10351 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10352 	 * and its control VSI is the main one.
10353 	 */
10354 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10355 		dev_err(&pf->pdev->dev,
10356 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10357 		return -ENOENT;
10358 	}
10359 
10360 	if (veb->uplink_seid == pf->mac_seid) {
10361 		/* Check that the LAN VSI has VEB owning flag set */
10362 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10363 
10364 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10365 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10366 			dev_err(&pf->pdev->dev,
10367 				"Invalid control VSI for VEB %d\n", veb->idx);
10368 			return -ENOENT;
10369 		}
10370 
10371 		/* Add the control VSI to switch */
10372 		ret = i40e_add_vsi(ctl_vsi);
10373 		if (ret) {
10374 			dev_err(&pf->pdev->dev,
10375 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10376 				veb->idx, ret);
10377 			return ret;
10378 		}
10379 
10380 		i40e_vsi_reset_stats(ctl_vsi);
10381 	}
10382 
10383 	/* create the VEB in the switch and move the VSI onto the VEB */
10384 	ret = i40e_add_veb(veb, ctl_vsi);
10385 	if (ret)
10386 		return ret;
10387 
10388 	if (veb->uplink_seid) {
10389 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10390 			veb->bridge_mode = BRIDGE_MODE_VEB;
10391 		else
10392 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10393 		i40e_config_bridge_mode(veb);
10394 	}
10395 
10396 	/* create the remaining VSIs attached to this VEB */
10397 	i40e_pf_for_each_vsi(pf, v, vsi) {
10398 		if (vsi == ctl_vsi)
10399 			continue;
10400 
10401 		if (vsi->veb_idx == veb->idx) {
10402 			vsi->uplink_seid = veb->seid;
10403 			ret = i40e_add_vsi(vsi);
10404 			if (ret) {
10405 				dev_info(&pf->pdev->dev,
10406 					 "rebuild of vsi_idx %d failed: %d\n",
10407 					 v, ret);
10408 				return ret;
10409 			}
10410 			i40e_vsi_reset_stats(vsi);
10411 		}
10412 	}
10413 
10414 	return ret;
10415 }
10416 
10417 /**
10418  * i40e_get_capabilities - get info about the HW
10419  * @pf: the PF struct
10420  * @list_type: AQ capability to be queried
10421  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10422 static int i40e_get_capabilities(struct i40e_pf *pf,
10423 				 enum i40e_admin_queue_opc list_type)
10424 {
10425 	struct libie_aqc_list_caps_elem *cap_buf;
10426 	u16 data_size;
10427 	int buf_len;
10428 	int err;
10429 
10430 	buf_len = 40 * sizeof(struct libie_aqc_list_caps_elem);
10431 	do {
10432 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10433 		if (!cap_buf)
10434 			return -ENOMEM;
10435 
10436 		/* this loads the data into the hw struct for us */
10437 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10438 						    &data_size, list_type,
10439 						    NULL);
10440 		/* data loaded, buffer no longer needed */
10441 		kfree(cap_buf);
10442 
10443 		if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_ENOMEM) {
10444 			/* retry with a larger buffer */
10445 			buf_len = data_size;
10446 		} else if (pf->hw.aq.asq_last_status != LIBIE_AQ_RC_OK || err) {
10447 			dev_info(&pf->pdev->dev,
10448 				 "capability discovery failed, err %pe aq_err %s\n",
10449 				 ERR_PTR(err),
10450 				 libie_aq_str(pf->hw.aq.asq_last_status));
10451 			return -ENODEV;
10452 		}
10453 	} while (err);
10454 
10455 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10456 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10457 			dev_info(&pf->pdev->dev,
10458 				 "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",
10459 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10460 				 pf->hw.func_caps.num_msix_vectors,
10461 				 pf->hw.func_caps.num_msix_vectors_vf,
10462 				 pf->hw.func_caps.fd_filters_guaranteed,
10463 				 pf->hw.func_caps.fd_filters_best_effort,
10464 				 pf->hw.func_caps.num_tx_qp,
10465 				 pf->hw.func_caps.num_vsis);
10466 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10467 			dev_info(&pf->pdev->dev,
10468 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10469 				 pf->hw.dev_caps.switch_mode,
10470 				 pf->hw.dev_caps.valid_functions);
10471 			dev_info(&pf->pdev->dev,
10472 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10473 				 pf->hw.dev_caps.sr_iov_1_1,
10474 				 pf->hw.dev_caps.num_vfs);
10475 			dev_info(&pf->pdev->dev,
10476 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10477 				 pf->hw.dev_caps.num_vsis,
10478 				 pf->hw.dev_caps.num_rx_qp,
10479 				 pf->hw.dev_caps.num_tx_qp);
10480 		}
10481 	}
10482 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10483 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10484 		       + pf->hw.func_caps.num_vfs)
10485 		if (pf->hw.revision_id == 0 &&
10486 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10487 			dev_info(&pf->pdev->dev,
10488 				 "got num_vsis %d, setting num_vsis to %d\n",
10489 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10490 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10491 		}
10492 	}
10493 	return 0;
10494 }
10495 
10496 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10497 
10498 /**
10499  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10500  * @pf: board private structure
10501  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10502 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10503 {
10504 	struct i40e_vsi *main_vsi, *vsi;
10505 
10506 	/* quick workaround for an NVM issue that leaves a critical register
10507 	 * uninitialized
10508 	 */
10509 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10510 		static const u32 hkey[] = {
10511 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10512 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10513 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10514 			0x95b3a76d};
10515 		int i;
10516 
10517 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10518 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10519 	}
10520 
10521 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10522 		return;
10523 
10524 	/* find existing VSI and see if it needs configuring */
10525 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10526 
10527 	/* create a new VSI if none exists */
10528 	if (!vsi) {
10529 		main_vsi = i40e_pf_get_main_vsi(pf);
10530 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10531 		if (!vsi) {
10532 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10533 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10534 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10535 			return;
10536 		}
10537 	}
10538 
10539 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10540 }
10541 
10542 /**
10543  * i40e_fdir_teardown - release the Flow Director resources
10544  * @pf: board private structure
10545  **/
i40e_fdir_teardown(struct i40e_pf * pf)10546 static void i40e_fdir_teardown(struct i40e_pf *pf)
10547 {
10548 	struct i40e_vsi *vsi;
10549 
10550 	i40e_fdir_filter_exit(pf);
10551 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10552 	if (vsi)
10553 		i40e_vsi_release(vsi);
10554 }
10555 
10556 /**
10557  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10558  * @vsi: PF main vsi
10559  * @seid: seid of main or channel VSIs
10560  *
10561  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10562  * existed before reset
10563  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10564 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10565 {
10566 	struct i40e_cloud_filter *cfilter;
10567 	struct i40e_pf *pf = vsi->back;
10568 	struct hlist_node *node;
10569 	int ret;
10570 
10571 	/* Add cloud filters back if they exist */
10572 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10573 				  cloud_node) {
10574 		if (cfilter->seid != seid)
10575 			continue;
10576 
10577 		if (cfilter->dst_port)
10578 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10579 								true);
10580 		else
10581 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10582 
10583 		if (ret) {
10584 			dev_dbg(&pf->pdev->dev,
10585 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10586 				ERR_PTR(ret),
10587 				libie_aq_str(pf->hw.aq.asq_last_status));
10588 			return ret;
10589 		}
10590 	}
10591 	return 0;
10592 }
10593 
10594 /**
10595  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10596  * @vsi: PF main vsi
10597  *
10598  * Rebuilds channel VSIs if they existed before reset
10599  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10600 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10601 {
10602 	struct i40e_channel *ch, *ch_tmp;
10603 	int ret;
10604 
10605 	if (list_empty(&vsi->ch_list))
10606 		return 0;
10607 
10608 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10609 		if (!ch->initialized)
10610 			break;
10611 		/* Proceed with creation of channel (VMDq2) VSI */
10612 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10613 		if (ret) {
10614 			dev_info(&vsi->back->pdev->dev,
10615 				 "failed to rebuild channels using uplink_seid %u\n",
10616 				 vsi->uplink_seid);
10617 			return ret;
10618 		}
10619 		/* Reconfigure TX queues using QTX_CTL register */
10620 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10621 		if (ret) {
10622 			dev_info(&vsi->back->pdev->dev,
10623 				 "failed to configure TX rings for channel %u\n",
10624 				 ch->seid);
10625 			return ret;
10626 		}
10627 		/* update 'next_base_queue' */
10628 		vsi->next_base_queue = vsi->next_base_queue +
10629 							ch->num_queue_pairs;
10630 		if (ch->max_tx_rate) {
10631 			u64 credits = ch->max_tx_rate;
10632 
10633 			if (i40e_set_bw_limit(vsi, ch->seid,
10634 					      ch->max_tx_rate))
10635 				return -EINVAL;
10636 
10637 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10638 			dev_dbg(&vsi->back->pdev->dev,
10639 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10640 				ch->max_tx_rate,
10641 				credits,
10642 				ch->seid);
10643 		}
10644 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10645 		if (ret) {
10646 			dev_dbg(&vsi->back->pdev->dev,
10647 				"Failed to rebuild cloud filters for channel VSI %u\n",
10648 				ch->seid);
10649 			return ret;
10650 		}
10651 	}
10652 	return 0;
10653 }
10654 
10655 /**
10656  * i40e_clean_xps_state - clean xps state for every tx_ring
10657  * @vsi: ptr to the VSI
10658  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10659 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10660 {
10661 	int i;
10662 
10663 	if (vsi->tx_rings)
10664 		for (i = 0; i < vsi->num_queue_pairs; i++)
10665 			if (vsi->tx_rings[i])
10666 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10667 					  vsi->tx_rings[i]->state);
10668 }
10669 
10670 /**
10671  * i40e_prep_for_reset - prep for the core to reset
10672  * @pf: board private structure
10673  *
10674  * Close up the VFs and other things in prep for PF Reset.
10675   **/
i40e_prep_for_reset(struct i40e_pf * pf)10676 static void i40e_prep_for_reset(struct i40e_pf *pf)
10677 {
10678 	struct i40e_hw *hw = &pf->hw;
10679 	struct i40e_vsi *vsi;
10680 	int ret = 0;
10681 	u32 v;
10682 
10683 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10684 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10685 		return;
10686 	if (i40e_check_asq_alive(&pf->hw))
10687 		i40e_vc_notify_reset(pf);
10688 
10689 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10690 
10691 	/* quiesce the VSIs and their queues that are not already DOWN */
10692 	i40e_pf_quiesce_all_vsi(pf);
10693 
10694 	i40e_pf_for_each_vsi(pf, v, vsi) {
10695 		i40e_clean_xps_state(vsi);
10696 		vsi->seid = 0;
10697 	}
10698 
10699 	i40e_shutdown_adminq(&pf->hw);
10700 
10701 	/* call shutdown HMC */
10702 	if (hw->hmc.hmc_obj) {
10703 		ret = i40e_shutdown_lan_hmc(hw);
10704 		if (ret)
10705 			dev_warn(&pf->pdev->dev,
10706 				 "shutdown_lan_hmc failed: %d\n", ret);
10707 	}
10708 
10709 	/* Save the current PTP time so that we can restore the time after the
10710 	 * reset completes.
10711 	 */
10712 	i40e_ptp_save_hw_time(pf);
10713 }
10714 
10715 /**
10716  * i40e_send_version - update firmware with driver version
10717  * @pf: PF struct
10718  */
i40e_send_version(struct i40e_pf * pf)10719 static void i40e_send_version(struct i40e_pf *pf)
10720 {
10721 	struct i40e_driver_version dv;
10722 
10723 	dv.major_version = 0xff;
10724 	dv.minor_version = 0xff;
10725 	dv.build_version = 0xff;
10726 	dv.subbuild_version = 0;
10727 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10728 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10729 }
10730 
10731 /**
10732  * i40e_get_oem_version - get OEM specific version information
10733  * @hw: pointer to the hardware structure
10734  **/
i40e_get_oem_version(struct i40e_hw * hw)10735 static void i40e_get_oem_version(struct i40e_hw *hw)
10736 {
10737 	u16 block_offset = 0xffff;
10738 	u16 block_length = 0;
10739 	u16 capabilities = 0;
10740 	u16 gen_snap = 0;
10741 	u16 release = 0;
10742 
10743 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10744 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10745 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10746 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10747 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10748 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10749 #define I40E_NVM_OEM_LENGTH			3
10750 
10751 	/* Check if pointer to OEM version block is valid. */
10752 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10753 	if (block_offset == 0xffff)
10754 		return;
10755 
10756 	/* Check if OEM version block has correct length. */
10757 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10758 			   &block_length);
10759 	if (block_length < I40E_NVM_OEM_LENGTH)
10760 		return;
10761 
10762 	/* Check if OEM version format is as expected. */
10763 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10764 			   &capabilities);
10765 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10766 		return;
10767 
10768 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10769 			   &gen_snap);
10770 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10771 			   &release);
10772 	hw->nvm.oem_ver =
10773 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10774 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10775 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10776 }
10777 
10778 /**
10779  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10780  * @pf: board private structure
10781  **/
i40e_reset(struct i40e_pf * pf)10782 static int i40e_reset(struct i40e_pf *pf)
10783 {
10784 	struct i40e_hw *hw = &pf->hw;
10785 	int ret;
10786 
10787 	ret = i40e_pf_reset(hw);
10788 	if (ret) {
10789 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10790 		set_bit(__I40E_RESET_FAILED, pf->state);
10791 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10792 	} else {
10793 		pf->pfr_count++;
10794 	}
10795 	return ret;
10796 }
10797 
10798 /**
10799  * i40e_rebuild - rebuild using a saved config
10800  * @pf: board private structure
10801  * @reinit: if the Main VSI needs to re-initialized.
10802  * @lock_acquired: indicates whether or not the lock has been acquired
10803  * before this function was called.
10804  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10805 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10806 {
10807 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10808 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10809 	struct i40e_hw *hw = &pf->hw;
10810 	struct i40e_veb *veb;
10811 	int ret;
10812 	u32 val;
10813 	int v;
10814 
10815 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10816 	    is_recovery_mode_reported)
10817 		i40e_set_ethtool_ops(vsi->netdev);
10818 
10819 	if (test_bit(__I40E_DOWN, pf->state) &&
10820 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10821 		goto clear_recovery;
10822 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10823 
10824 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10825 	ret = i40e_init_adminq(&pf->hw);
10826 	if (ret) {
10827 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10828 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10829 		goto clear_recovery;
10830 	}
10831 	i40e_get_oem_version(&pf->hw);
10832 
10833 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10834 		/* The following delay is necessary for firmware update. */
10835 		mdelay(1000);
10836 	}
10837 
10838 	/* re-verify the eeprom if we just had an EMP reset */
10839 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10840 		i40e_verify_eeprom(pf);
10841 
10842 	/* if we are going out of or into recovery mode we have to act
10843 	 * accordingly with regard to resources initialization
10844 	 * and deinitialization
10845 	 */
10846 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10847 		if (i40e_get_capabilities(pf,
10848 					  i40e_aqc_opc_list_func_capabilities))
10849 			goto end_unlock;
10850 
10851 		if (is_recovery_mode_reported) {
10852 			/* we're staying in recovery mode so we'll reinitialize
10853 			 * misc vector here
10854 			 */
10855 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10856 				goto end_unlock;
10857 		} else {
10858 			if (!lock_acquired)
10859 				rtnl_lock();
10860 			/* we're going out of recovery mode so we'll free
10861 			 * the IRQ allocated specifically for recovery mode
10862 			 * and restore the interrupt scheme
10863 			 */
10864 			free_irq(pf->pdev->irq, pf);
10865 			i40e_clear_interrupt_scheme(pf);
10866 			if (i40e_restore_interrupt_scheme(pf))
10867 				goto end_unlock;
10868 		}
10869 
10870 		/* tell the firmware that we're starting */
10871 		i40e_send_version(pf);
10872 
10873 		/* bail out in case recovery mode was detected, as there is
10874 		 * no need for further configuration.
10875 		 */
10876 		goto end_unlock;
10877 	}
10878 
10879 	i40e_clear_pxe_mode(hw);
10880 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10881 	if (ret)
10882 		goto end_core_reset;
10883 
10884 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10885 				hw->func_caps.num_rx_qp, 0, 0);
10886 	if (ret) {
10887 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10888 		goto end_core_reset;
10889 	}
10890 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10891 	if (ret) {
10892 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10893 		goto end_core_reset;
10894 	}
10895 
10896 #ifdef CONFIG_I40E_DCB
10897 	/* Enable FW to write a default DCB config on link-up
10898 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10899 	 * is not supported with new link speed
10900 	 */
10901 	if (i40e_is_tc_mqprio_enabled(pf)) {
10902 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10903 	} else {
10904 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10905 		    (hw->phy.link_info.link_speed &
10906 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10907 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10908 			dev_warn(&pf->pdev->dev,
10909 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10910 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10911 		} else {
10912 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10913 			ret = i40e_init_pf_dcb(pf);
10914 			if (ret) {
10915 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10916 					 ret);
10917 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10918 				/* Continue without DCB enabled */
10919 			}
10920 		}
10921 	}
10922 
10923 #endif /* CONFIG_I40E_DCB */
10924 	if (!lock_acquired)
10925 		rtnl_lock();
10926 	ret = i40e_setup_pf_switch(pf, reinit, true);
10927 	if (ret)
10928 		goto end_unlock;
10929 
10930 	/* The driver only wants link up/down and module qualification
10931 	 * reports from firmware.  Note the negative logic.
10932 	 */
10933 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10934 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10935 					 I40E_AQ_EVENT_MEDIA_NA |
10936 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10937 	if (ret)
10938 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10939 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10940 
10941 	/* Rebuild the VSIs and VEBs that existed before reset.
10942 	 * They are still in our local switch element arrays, so only
10943 	 * need to rebuild the switch model in the HW.
10944 	 *
10945 	 * If there were VEBs but the reconstitution failed, we'll try
10946 	 * to recover minimal use by getting the basic PF VSI working.
10947 	 */
10948 	if (vsi->uplink_seid != pf->mac_seid) {
10949 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10950 
10951 		/* Rebuild VEBs */
10952 		i40e_pf_for_each_veb(pf, v, veb) {
10953 			ret = i40e_reconstitute_veb(veb);
10954 			if (!ret)
10955 				continue;
10956 
10957 			/* If Main VEB failed, we're in deep doodoo,
10958 			 * so give up rebuilding the switch and set up
10959 			 * for minimal rebuild of PF VSI.
10960 			 * If orphan failed, we'll report the error
10961 			 * but try to keep going.
10962 			 */
10963 			if (veb->uplink_seid == pf->mac_seid) {
10964 				dev_info(&pf->pdev->dev,
10965 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10966 					 ret);
10967 				vsi->uplink_seid = pf->mac_seid;
10968 				break;
10969 			} else if (veb->uplink_seid == 0) {
10970 				dev_info(&pf->pdev->dev,
10971 					 "rebuild of orphan VEB failed: %d\n",
10972 					 ret);
10973 			}
10974 		}
10975 	}
10976 
10977 	if (vsi->uplink_seid == pf->mac_seid) {
10978 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10979 		/* no VEB, so rebuild only the Main VSI */
10980 		ret = i40e_add_vsi(vsi);
10981 		if (ret) {
10982 			dev_info(&pf->pdev->dev,
10983 				 "rebuild of Main VSI failed: %d\n", ret);
10984 			goto end_unlock;
10985 		}
10986 	}
10987 
10988 	if (vsi->mqprio_qopt.max_rate[0]) {
10989 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
10990 						  vsi->mqprio_qopt.max_rate[0]);
10991 		u64 credits = 0;
10992 
10993 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10994 		if (ret)
10995 			goto end_unlock;
10996 
10997 		credits = max_tx_rate;
10998 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10999 		dev_dbg(&vsi->back->pdev->dev,
11000 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11001 			max_tx_rate,
11002 			credits,
11003 			vsi->seid);
11004 	}
11005 
11006 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11007 	if (ret)
11008 		goto end_unlock;
11009 
11010 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11011 	 * for this main VSI if they exist
11012 	 */
11013 	ret = i40e_rebuild_channels(vsi);
11014 	if (ret)
11015 		goto end_unlock;
11016 
11017 	/* Reconfigure hardware for allowing smaller MSS in the case
11018 	 * of TSO, so that we avoid the MDD being fired and causing
11019 	 * a reset in the case of small MSS+TSO.
11020 	 */
11021 #define I40E_REG_MSS          0x000E64DC
11022 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11023 #define I40E_64BYTE_MSS       0x400000
11024 	val = rd32(hw, I40E_REG_MSS);
11025 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11026 		val &= ~I40E_REG_MSS_MIN_MASK;
11027 		val |= I40E_64BYTE_MSS;
11028 		wr32(hw, I40E_REG_MSS, val);
11029 	}
11030 
11031 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11032 		msleep(75);
11033 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11034 		if (ret)
11035 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11036 				 ERR_PTR(ret),
11037 				 libie_aq_str(pf->hw.aq.asq_last_status));
11038 	}
11039 	/* reinit the misc interrupt */
11040 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11041 		ret = i40e_setup_misc_vector(pf);
11042 		if (ret)
11043 			goto end_unlock;
11044 	}
11045 
11046 	/* Add a filter to drop all Flow control frames from any VSI from being
11047 	 * transmitted. By doing so we stop a malicious VF from sending out
11048 	 * PAUSE or PFC frames and potentially controlling traffic for other
11049 	 * PF/VF VSIs.
11050 	 * The FW can still send Flow control frames if enabled.
11051 	 */
11052 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11053 						       pf->main_vsi_seid);
11054 
11055 	/* restart the VSIs that were rebuilt and running before the reset */
11056 	i40e_pf_unquiesce_all_vsi(pf);
11057 
11058 	/* Release the RTNL lock before we start resetting VFs */
11059 	if (!lock_acquired)
11060 		rtnl_unlock();
11061 
11062 	/* Restore promiscuous settings */
11063 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11064 	if (ret)
11065 		dev_warn(&pf->pdev->dev,
11066 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11067 			 pf->cur_promisc ? "on" : "off",
11068 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
11069 
11070 	i40e_reset_all_vfs(pf, true);
11071 
11072 	/* tell the firmware that we're starting */
11073 	i40e_send_version(pf);
11074 
11075 	/* We've already released the lock, so don't do it again */
11076 	goto end_core_reset;
11077 
11078 end_unlock:
11079 	if (!lock_acquired)
11080 		rtnl_unlock();
11081 end_core_reset:
11082 	clear_bit(__I40E_RESET_FAILED, pf->state);
11083 clear_recovery:
11084 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11085 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11086 }
11087 
11088 /**
11089  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11090  * @pf: board private structure
11091  * @reinit: if the Main VSI needs to re-initialized.
11092  * @lock_acquired: indicates whether or not the lock has been acquired
11093  * before this function was called.
11094  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11095 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11096 				   bool lock_acquired)
11097 {
11098 	int ret;
11099 
11100 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11101 		return;
11102 	/* Now we wait for GRST to settle out.
11103 	 * We don't have to delete the VEBs or VSIs from the hw switch
11104 	 * because the reset will make them disappear.
11105 	 */
11106 	ret = i40e_reset(pf);
11107 	if (!ret)
11108 		i40e_rebuild(pf, reinit, lock_acquired);
11109 	else
11110 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11111 }
11112 
11113 /**
11114  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11115  * @pf: board private structure
11116  *
11117  * Close up the VFs and other things in prep for a Core Reset,
11118  * then get ready to rebuild the world.
11119  * @lock_acquired: indicates whether or not the lock has been acquired
11120  * before this function was called.
11121  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11122 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11123 {
11124 	i40e_prep_for_reset(pf);
11125 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11126 }
11127 
11128 /**
11129  * i40e_print_vf_mdd_event - print VF Tx/Rx malicious driver detect event
11130  * @pf: board private structure
11131  * @vf: pointer to the VF structure
11132  * @is_tx: true - for Tx event, false - for  Rx
11133  */
i40e_print_vf_mdd_event(struct i40e_pf * pf,struct i40e_vf * vf,bool is_tx)11134 static void i40e_print_vf_mdd_event(struct i40e_pf *pf, struct i40e_vf *vf,
11135 				    bool is_tx)
11136 {
11137 	dev_err(&pf->pdev->dev, is_tx ?
11138 		"%lld Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n" :
11139 		"%lld Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n",
11140 		is_tx ? vf->mdd_tx_events.count : vf->mdd_rx_events.count,
11141 		pf->hw.pf_id,
11142 		vf->vf_id,
11143 		vf->default_lan_addr.addr,
11144 		str_on_off(test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)));
11145 }
11146 
11147 /**
11148  * i40e_print_vfs_mdd_events - print VFs malicious driver detect event
11149  * @pf: pointer to the PF structure
11150  *
11151  * Called from i40e_handle_mdd_event to rate limit and print VFs MDD events.
11152  */
i40e_print_vfs_mdd_events(struct i40e_pf * pf)11153 static void i40e_print_vfs_mdd_events(struct i40e_pf *pf)
11154 {
11155 	unsigned int i;
11156 
11157 	/* check that there are pending MDD events to print */
11158 	if (!test_and_clear_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state))
11159 		return;
11160 
11161 	if (!__ratelimit(&pf->mdd_message_rate_limit))
11162 		return;
11163 
11164 	for (i = 0; i < pf->num_alloc_vfs; i++) {
11165 		struct i40e_vf *vf = &pf->vf[i];
11166 		bool is_printed = false;
11167 
11168 		/* only print Rx MDD event message if there are new events */
11169 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
11170 			vf->mdd_rx_events.last_printed = vf->mdd_rx_events.count;
11171 			i40e_print_vf_mdd_event(pf, vf, false);
11172 			is_printed = true;
11173 		}
11174 
11175 		/* only print Tx MDD event message if there are new events */
11176 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
11177 			vf->mdd_tx_events.last_printed = vf->mdd_tx_events.count;
11178 			i40e_print_vf_mdd_event(pf, vf, true);
11179 			is_printed = true;
11180 		}
11181 
11182 		if (is_printed && !test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags))
11183 			dev_info(&pf->pdev->dev,
11184 				 "Use PF Control I/F to re-enable the VF #%d\n",
11185 				 i);
11186 	}
11187 }
11188 
11189 /**
11190  * i40e_handle_mdd_event
11191  * @pf: pointer to the PF structure
11192  *
11193  * Called from the MDD irq handler to identify possibly malicious vfs
11194  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11195 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11196 {
11197 	struct i40e_hw *hw = &pf->hw;
11198 	bool mdd_detected = false;
11199 	struct i40e_vf *vf;
11200 	u32 reg;
11201 	int i;
11202 
11203 	if (!test_and_clear_bit(__I40E_MDD_EVENT_PENDING, pf->state)) {
11204 		/* Since the VF MDD event logging is rate limited, check if
11205 		 * there are pending MDD events.
11206 		 */
11207 		i40e_print_vfs_mdd_events(pf);
11208 		return;
11209 	}
11210 
11211 	/* find what triggered the MDD event */
11212 	reg = rd32(hw, I40E_GL_MDET_TX);
11213 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11214 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11215 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11216 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11217 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11218 				pf->hw.func_caps.base_queue;
11219 		if (netif_msg_tx_err(pf))
11220 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11221 				 event, queue, pf_num, vf_num);
11222 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11223 		mdd_detected = true;
11224 	}
11225 	reg = rd32(hw, I40E_GL_MDET_RX);
11226 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11227 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11228 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11229 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11230 				pf->hw.func_caps.base_queue;
11231 		if (netif_msg_rx_err(pf))
11232 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11233 				 event, queue, func);
11234 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11235 		mdd_detected = true;
11236 	}
11237 
11238 	if (mdd_detected) {
11239 		reg = rd32(hw, I40E_PF_MDET_TX);
11240 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11241 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11242 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11243 		}
11244 		reg = rd32(hw, I40E_PF_MDET_RX);
11245 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11246 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11247 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11248 		}
11249 	}
11250 
11251 	/* see if one of the VFs needs its hand slapped */
11252 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11253 		bool is_mdd_on_tx = false;
11254 		bool is_mdd_on_rx = false;
11255 
11256 		vf = &(pf->vf[i]);
11257 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11258 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11259 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11260 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11261 			vf->mdd_tx_events.count++;
11262 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11263 			is_mdd_on_tx = true;
11264 		}
11265 
11266 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11267 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11268 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11269 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11270 			vf->mdd_rx_events.count++;
11271 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11272 			is_mdd_on_rx = true;
11273 		}
11274 
11275 		if ((is_mdd_on_tx || is_mdd_on_rx) &&
11276 		    test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
11277 			/* VF MDD event counters will be cleared by
11278 			 * reset, so print the event prior to reset.
11279 			 */
11280 			if (is_mdd_on_rx)
11281 				i40e_print_vf_mdd_event(pf, vf, false);
11282 			if (is_mdd_on_tx)
11283 				i40e_print_vf_mdd_event(pf, vf, true);
11284 
11285 			i40e_vc_reset_vf(vf, true);
11286 		}
11287 	}
11288 
11289 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11290 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11291 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11292 	i40e_flush(hw);
11293 
11294 	i40e_print_vfs_mdd_events(pf);
11295 }
11296 
11297 /**
11298  * i40e_service_task - Run the driver's async subtasks
11299  * @work: pointer to work_struct containing our data
11300  **/
i40e_service_task(struct work_struct * work)11301 static void i40e_service_task(struct work_struct *work)
11302 {
11303 	struct i40e_pf *pf = container_of(work,
11304 					  struct i40e_pf,
11305 					  service_task);
11306 	unsigned long start_time = jiffies;
11307 
11308 	/* don't bother with service tasks if a reset is in progress */
11309 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11310 	    test_bit(__I40E_SUSPENDED, pf->state))
11311 		return;
11312 
11313 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11314 		return;
11315 
11316 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11317 		i40e_detect_recover_hung(pf);
11318 		i40e_sync_filters_subtask(pf);
11319 		i40e_reset_subtask(pf);
11320 		i40e_handle_mdd_event(pf);
11321 		i40e_vc_process_vflr_event(pf);
11322 		i40e_watchdog_subtask(pf);
11323 		i40e_fdir_reinit_subtask(pf);
11324 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11325 			/* Client subtask will reopen next time through. */
11326 			i40e_notify_client_of_netdev_close(pf, true);
11327 		} else {
11328 			i40e_client_subtask(pf);
11329 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11330 					       pf->state))
11331 				i40e_notify_client_of_l2_param_changes(pf);
11332 		}
11333 		i40e_sync_filters_subtask(pf);
11334 	} else {
11335 		i40e_reset_subtask(pf);
11336 	}
11337 
11338 	i40e_clean_adminq_subtask(pf);
11339 
11340 	/* flush memory to make sure state is correct before next watchdog */
11341 	smp_mb__before_atomic();
11342 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11343 
11344 	/* If the tasks have taken longer than one timer cycle or there
11345 	 * is more work to be done, reschedule the service task now
11346 	 * rather than wait for the timer to tick again.
11347 	 */
11348 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11349 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11350 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11351 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11352 		i40e_service_event_schedule(pf);
11353 }
11354 
11355 /**
11356  * i40e_service_timer - timer callback
11357  * @t: timer list pointer
11358  **/
i40e_service_timer(struct timer_list * t)11359 static void i40e_service_timer(struct timer_list *t)
11360 {
11361 	struct i40e_pf *pf = timer_container_of(pf, t, service_timer);
11362 
11363 	mod_timer(&pf->service_timer,
11364 		  round_jiffies(jiffies + pf->service_timer_period));
11365 	i40e_service_event_schedule(pf);
11366 }
11367 
11368 /**
11369  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11370  * @vsi: the VSI being configured
11371  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11372 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11373 {
11374 	struct i40e_pf *pf = vsi->back;
11375 
11376 	switch (vsi->type) {
11377 	case I40E_VSI_MAIN:
11378 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11379 		if (!vsi->num_tx_desc)
11380 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11381 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11382 		if (!vsi->num_rx_desc)
11383 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11384 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11385 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11386 			vsi->num_q_vectors = pf->num_lan_msix;
11387 		else
11388 			vsi->num_q_vectors = 1;
11389 
11390 		break;
11391 
11392 	case I40E_VSI_FDIR:
11393 		vsi->alloc_queue_pairs = 1;
11394 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11395 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11396 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11397 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11398 		vsi->num_q_vectors = pf->num_fdsb_msix;
11399 		break;
11400 
11401 	case I40E_VSI_VMDQ2:
11402 		vsi->alloc_queue_pairs = pf->num_vmdq_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 		vsi->num_q_vectors = pf->num_vmdq_msix;
11410 		break;
11411 
11412 	case I40E_VSI_SRIOV:
11413 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11414 		if (!vsi->num_tx_desc)
11415 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11416 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11417 		if (!vsi->num_rx_desc)
11418 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11419 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11420 		break;
11421 
11422 	default:
11423 		WARN_ON(1);
11424 		return -ENODATA;
11425 	}
11426 
11427 	if (is_kdump_kernel()) {
11428 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11429 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11430 	}
11431 
11432 	return 0;
11433 }
11434 
11435 /**
11436  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11437  * @vsi: VSI pointer
11438  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11439  *
11440  * On error: returns error code (negative)
11441  * On success: returns 0
11442  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11443 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11444 {
11445 	struct i40e_ring **next_rings;
11446 	int size;
11447 	int ret = 0;
11448 
11449 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11450 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11451 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11452 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11453 	if (!vsi->tx_rings)
11454 		return -ENOMEM;
11455 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11456 	if (i40e_enabled_xdp_vsi(vsi)) {
11457 		vsi->xdp_rings = next_rings;
11458 		next_rings += vsi->alloc_queue_pairs;
11459 	}
11460 	vsi->rx_rings = next_rings;
11461 
11462 	if (alloc_qvectors) {
11463 		/* allocate memory for q_vector pointers */
11464 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11465 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11466 		if (!vsi->q_vectors) {
11467 			ret = -ENOMEM;
11468 			goto err_vectors;
11469 		}
11470 	}
11471 	return ret;
11472 
11473 err_vectors:
11474 	kfree(vsi->tx_rings);
11475 	return ret;
11476 }
11477 
11478 /**
11479  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11480  * @pf: board private structure
11481  * @type: type of VSI
11482  *
11483  * On error: returns error code (negative)
11484  * On success: returns vsi index in PF (positive)
11485  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11486 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11487 {
11488 	int ret = -ENODEV;
11489 	struct i40e_vsi *vsi;
11490 	int vsi_idx;
11491 	int i;
11492 
11493 	/* Need to protect the allocation of the VSIs at the PF level */
11494 	mutex_lock(&pf->switch_mutex);
11495 
11496 	/* VSI list may be fragmented if VSI creation/destruction has
11497 	 * been happening.  We can afford to do a quick scan to look
11498 	 * for any free VSIs in the list.
11499 	 *
11500 	 * find next empty vsi slot, looping back around if necessary
11501 	 */
11502 	i = pf->next_vsi;
11503 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11504 		i++;
11505 	if (i >= pf->num_alloc_vsi) {
11506 		i = 0;
11507 		while (i < pf->next_vsi && pf->vsi[i])
11508 			i++;
11509 	}
11510 
11511 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11512 		vsi_idx = i;             /* Found one! */
11513 	} else {
11514 		ret = -ENODEV;
11515 		goto unlock_pf;  /* out of VSI slots! */
11516 	}
11517 	pf->next_vsi = ++i;
11518 
11519 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11520 	if (!vsi) {
11521 		ret = -ENOMEM;
11522 		goto unlock_pf;
11523 	}
11524 	vsi->type = type;
11525 	vsi->back = pf;
11526 	set_bit(__I40E_VSI_DOWN, vsi->state);
11527 	vsi->flags = 0;
11528 	vsi->idx = vsi_idx;
11529 	vsi->int_rate_limit = 0;
11530 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11531 				pf->rss_table_size : 64;
11532 	vsi->netdev_registered = false;
11533 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11534 	hash_init(vsi->mac_filter_hash);
11535 	vsi->irqs_ready = false;
11536 
11537 	if (type == I40E_VSI_MAIN) {
11538 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11539 		if (!vsi->af_xdp_zc_qps)
11540 			goto err_rings;
11541 	}
11542 
11543 	ret = i40e_set_num_rings_in_vsi(vsi);
11544 	if (ret)
11545 		goto err_rings;
11546 
11547 	ret = i40e_vsi_alloc_arrays(vsi, true);
11548 	if (ret)
11549 		goto err_rings;
11550 
11551 	/* Setup default MSIX irq handler for VSI */
11552 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11553 
11554 	/* Initialize VSI lock */
11555 	spin_lock_init(&vsi->mac_filter_hash_lock);
11556 	pf->vsi[vsi_idx] = vsi;
11557 	ret = vsi_idx;
11558 	goto unlock_pf;
11559 
11560 err_rings:
11561 	bitmap_free(vsi->af_xdp_zc_qps);
11562 	pf->next_vsi = i - 1;
11563 	kfree(vsi);
11564 unlock_pf:
11565 	mutex_unlock(&pf->switch_mutex);
11566 	return ret;
11567 }
11568 
11569 /**
11570  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11571  * @vsi: VSI pointer
11572  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11573  *
11574  * On error: returns error code (negative)
11575  * On success: returns 0
11576  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11577 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11578 {
11579 	/* free the ring and vector containers */
11580 	if (free_qvectors) {
11581 		kfree(vsi->q_vectors);
11582 		vsi->q_vectors = NULL;
11583 	}
11584 	kfree(vsi->tx_rings);
11585 	vsi->tx_rings = NULL;
11586 	vsi->rx_rings = NULL;
11587 	vsi->xdp_rings = NULL;
11588 }
11589 
11590 /**
11591  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11592  * and lookup table
11593  * @vsi: Pointer to VSI structure
11594  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11595 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11596 {
11597 	if (!vsi)
11598 		return;
11599 
11600 	kfree(vsi->rss_hkey_user);
11601 	vsi->rss_hkey_user = NULL;
11602 
11603 	kfree(vsi->rss_lut_user);
11604 	vsi->rss_lut_user = NULL;
11605 }
11606 
11607 /**
11608  * i40e_vsi_clear - Deallocate the VSI provided
11609  * @vsi: the VSI being un-configured
11610  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11611 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11612 {
11613 	struct i40e_pf *pf;
11614 
11615 	if (!vsi)
11616 		return 0;
11617 
11618 	if (!vsi->back)
11619 		goto free_vsi;
11620 	pf = vsi->back;
11621 
11622 	mutex_lock(&pf->switch_mutex);
11623 	if (!pf->vsi[vsi->idx]) {
11624 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11625 			vsi->idx, vsi->idx, vsi->type);
11626 		goto unlock_vsi;
11627 	}
11628 
11629 	if (pf->vsi[vsi->idx] != vsi) {
11630 		dev_err(&pf->pdev->dev,
11631 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11632 			pf->vsi[vsi->idx]->idx,
11633 			pf->vsi[vsi->idx]->type,
11634 			vsi->idx, vsi->type);
11635 		goto unlock_vsi;
11636 	}
11637 
11638 	/* updates the PF for this cleared vsi */
11639 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11640 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11641 
11642 	bitmap_free(vsi->af_xdp_zc_qps);
11643 	i40e_vsi_free_arrays(vsi, true);
11644 	i40e_clear_rss_config_user(vsi);
11645 
11646 	pf->vsi[vsi->idx] = NULL;
11647 	if (vsi->idx < pf->next_vsi)
11648 		pf->next_vsi = vsi->idx;
11649 
11650 unlock_vsi:
11651 	mutex_unlock(&pf->switch_mutex);
11652 free_vsi:
11653 	kfree(vsi);
11654 
11655 	return 0;
11656 }
11657 
11658 /**
11659  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11660  * @vsi: the VSI being cleaned
11661  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11662 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11663 {
11664 	int i;
11665 
11666 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11667 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11668 			kfree_rcu(vsi->tx_rings[i], rcu);
11669 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11670 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11671 			if (vsi->xdp_rings)
11672 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11673 		}
11674 	}
11675 }
11676 
11677 /**
11678  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11679  * @vsi: the VSI being configured
11680  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11681 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11682 {
11683 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11684 	struct i40e_pf *pf = vsi->back;
11685 	struct i40e_ring *ring;
11686 
11687 	/* Set basic values in the rings to be used later during open() */
11688 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11689 		/* allocate space for both Tx and Rx in one shot */
11690 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11691 		if (!ring)
11692 			goto err_out;
11693 
11694 		ring->queue_index = i;
11695 		ring->reg_idx = vsi->base_queue + i;
11696 		ring->ring_active = false;
11697 		ring->vsi = vsi;
11698 		ring->netdev = vsi->netdev;
11699 		ring->dev = &pf->pdev->dev;
11700 		ring->count = vsi->num_tx_desc;
11701 		ring->size = 0;
11702 		ring->dcb_tc = 0;
11703 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11704 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11705 		ring->itr_setting = pf->tx_itr_default;
11706 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11707 
11708 		if (!i40e_enabled_xdp_vsi(vsi))
11709 			goto setup_rx;
11710 
11711 		ring->queue_index = vsi->alloc_queue_pairs + i;
11712 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11713 		ring->ring_active = false;
11714 		ring->vsi = vsi;
11715 		ring->netdev = NULL;
11716 		ring->dev = &pf->pdev->dev;
11717 		ring->count = vsi->num_tx_desc;
11718 		ring->size = 0;
11719 		ring->dcb_tc = 0;
11720 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11721 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11722 		set_ring_xdp(ring);
11723 		ring->itr_setting = pf->tx_itr_default;
11724 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11725 
11726 setup_rx:
11727 		ring->queue_index = i;
11728 		ring->reg_idx = vsi->base_queue + i;
11729 		ring->ring_active = false;
11730 		ring->vsi = vsi;
11731 		ring->netdev = vsi->netdev;
11732 		ring->dev = &pf->pdev->dev;
11733 		ring->count = vsi->num_rx_desc;
11734 		ring->size = 0;
11735 		ring->dcb_tc = 0;
11736 		ring->itr_setting = pf->rx_itr_default;
11737 		WRITE_ONCE(vsi->rx_rings[i], ring);
11738 	}
11739 
11740 	return 0;
11741 
11742 err_out:
11743 	i40e_vsi_clear_rings(vsi);
11744 	return -ENOMEM;
11745 }
11746 
11747 /**
11748  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11749  * @pf: board private structure
11750  * @vectors: the number of MSI-X vectors to request
11751  *
11752  * Returns the number of vectors reserved, or error
11753  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11754 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11755 {
11756 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11757 					I40E_MIN_MSIX, vectors);
11758 	if (vectors < 0) {
11759 		dev_info(&pf->pdev->dev,
11760 			 "MSI-X vector reservation failed: %d\n", vectors);
11761 		vectors = 0;
11762 	}
11763 
11764 	return vectors;
11765 }
11766 
11767 /**
11768  * i40e_init_msix - Setup the MSIX capability
11769  * @pf: board private structure
11770  *
11771  * Work with the OS to set up the MSIX vectors needed.
11772  *
11773  * Returns the number of vectors reserved or negative on failure
11774  **/
i40e_init_msix(struct i40e_pf * pf)11775 static int i40e_init_msix(struct i40e_pf *pf)
11776 {
11777 	struct i40e_hw *hw = &pf->hw;
11778 	int cpus, extra_vectors;
11779 	int vectors_left;
11780 	int v_budget, i;
11781 	int v_actual;
11782 	int iwarp_requested = 0;
11783 
11784 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11785 		return -ENODEV;
11786 
11787 	/* The number of vectors we'll request will be comprised of:
11788 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11789 	 *   - The number of LAN queue pairs
11790 	 *	- Queues being used for RSS.
11791 	 *		We don't need as many as max_rss_size vectors.
11792 	 *		use rss_size instead in the calculation since that
11793 	 *		is governed by number of cpus in the system.
11794 	 *	- assumes symmetric Tx/Rx pairing
11795 	 *   - The number of VMDq pairs
11796 	 *   - The CPU count within the NUMA node if iWARP is enabled
11797 	 * Once we count this up, try the request.
11798 	 *
11799 	 * If we can't get what we want, we'll simplify to nearly nothing
11800 	 * and try again.  If that still fails, we punt.
11801 	 */
11802 	vectors_left = hw->func_caps.num_msix_vectors;
11803 	v_budget = 0;
11804 
11805 	/* reserve one vector for miscellaneous handler */
11806 	if (vectors_left) {
11807 		v_budget++;
11808 		vectors_left--;
11809 	}
11810 
11811 	/* reserve some vectors for the main PF traffic queues. Initially we
11812 	 * only reserve at most 50% of the available vectors, in the case that
11813 	 * the number of online CPUs is large. This ensures that we can enable
11814 	 * extra features as well. Once we've enabled the other features, we
11815 	 * will use any remaining vectors to reach as close as we can to the
11816 	 * number of online CPUs.
11817 	 */
11818 	cpus = num_online_cpus();
11819 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11820 	vectors_left -= pf->num_lan_msix;
11821 
11822 	/* reserve one vector for sideband flow director */
11823 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11824 		if (vectors_left) {
11825 			pf->num_fdsb_msix = 1;
11826 			v_budget++;
11827 			vectors_left--;
11828 		} else {
11829 			pf->num_fdsb_msix = 0;
11830 		}
11831 	}
11832 
11833 	/* can we reserve enough for iWARP? */
11834 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11835 		iwarp_requested = pf->num_iwarp_msix;
11836 
11837 		if (!vectors_left)
11838 			pf->num_iwarp_msix = 0;
11839 		else if (vectors_left < pf->num_iwarp_msix)
11840 			pf->num_iwarp_msix = 1;
11841 		v_budget += pf->num_iwarp_msix;
11842 		vectors_left -= pf->num_iwarp_msix;
11843 	}
11844 
11845 	/* any vectors left over go for VMDq support */
11846 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11847 		if (!vectors_left) {
11848 			pf->num_vmdq_msix = 0;
11849 			pf->num_vmdq_qps = 0;
11850 		} else {
11851 			int vmdq_vecs_wanted =
11852 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11853 			int vmdq_vecs =
11854 				min_t(int, vectors_left, vmdq_vecs_wanted);
11855 
11856 			/* if we're short on vectors for what's desired, we limit
11857 			 * the queues per vmdq.  If this is still more than are
11858 			 * available, the user will need to change the number of
11859 			 * queues/vectors used by the PF later with the ethtool
11860 			 * channels command
11861 			 */
11862 			if (vectors_left < vmdq_vecs_wanted) {
11863 				pf->num_vmdq_qps = 1;
11864 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11865 				vmdq_vecs = min_t(int,
11866 						  vectors_left,
11867 						  vmdq_vecs_wanted);
11868 			}
11869 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11870 
11871 			v_budget += vmdq_vecs;
11872 			vectors_left -= vmdq_vecs;
11873 		}
11874 	}
11875 
11876 	/* On systems with a large number of SMP cores, we previously limited
11877 	 * the number of vectors for num_lan_msix to be at most 50% of the
11878 	 * available vectors, to allow for other features. Now, we add back
11879 	 * the remaining vectors. However, we ensure that the total
11880 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11881 	 * calculate the number of vectors we can add without going over the
11882 	 * cap of CPUs. For systems with a small number of CPUs this will be
11883 	 * zero.
11884 	 */
11885 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11886 	pf->num_lan_msix += extra_vectors;
11887 	vectors_left -= extra_vectors;
11888 
11889 	WARN(vectors_left < 0,
11890 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11891 
11892 	v_budget += pf->num_lan_msix;
11893 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11894 				   GFP_KERNEL);
11895 	if (!pf->msix_entries)
11896 		return -ENOMEM;
11897 
11898 	for (i = 0; i < v_budget; i++)
11899 		pf->msix_entries[i].entry = i;
11900 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11901 
11902 	if (v_actual < I40E_MIN_MSIX) {
11903 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11904 		kfree(pf->msix_entries);
11905 		pf->msix_entries = NULL;
11906 		pci_disable_msix(pf->pdev);
11907 		return -ENODEV;
11908 
11909 	} else if (v_actual == I40E_MIN_MSIX) {
11910 		/* Adjust for minimal MSIX use */
11911 		pf->num_vmdq_vsis = 0;
11912 		pf->num_vmdq_qps = 0;
11913 		pf->num_lan_qps = 1;
11914 		pf->num_lan_msix = 1;
11915 
11916 	} else if (v_actual != v_budget) {
11917 		/* If we have limited resources, we will start with no vectors
11918 		 * for the special features and then allocate vectors to some
11919 		 * of these features based on the policy and at the end disable
11920 		 * the features that did not get any vectors.
11921 		 */
11922 		int vec;
11923 
11924 		dev_info(&pf->pdev->dev,
11925 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11926 			 v_actual, v_budget);
11927 		/* reserve the misc vector */
11928 		vec = v_actual - 1;
11929 
11930 		/* Scale vector usage down */
11931 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11932 		pf->num_vmdq_vsis = 1;
11933 		pf->num_vmdq_qps = 1;
11934 
11935 		/* partition out the remaining vectors */
11936 		switch (vec) {
11937 		case 2:
11938 			pf->num_lan_msix = 1;
11939 			break;
11940 		case 3:
11941 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11942 				pf->num_lan_msix = 1;
11943 				pf->num_iwarp_msix = 1;
11944 			} else {
11945 				pf->num_lan_msix = 2;
11946 			}
11947 			break;
11948 		default:
11949 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11950 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11951 						 iwarp_requested);
11952 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11953 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11954 			} else {
11955 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11956 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11957 			}
11958 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11959 				pf->num_fdsb_msix = 1;
11960 				vec--;
11961 			}
11962 			pf->num_lan_msix = min_t(int,
11963 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11964 							      pf->num_lan_msix);
11965 			pf->num_lan_qps = pf->num_lan_msix;
11966 			break;
11967 		}
11968 	}
11969 
11970 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11971 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11972 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11973 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11974 	}
11975 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11976 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11977 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
11978 	}
11979 
11980 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
11981 	    pf->num_iwarp_msix == 0) {
11982 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11983 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
11984 	}
11985 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11986 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11987 		   pf->num_lan_msix,
11988 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11989 		   pf->num_fdsb_msix,
11990 		   pf->num_iwarp_msix);
11991 
11992 	return v_actual;
11993 }
11994 
11995 /**
11996  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11997  * @vsi: the VSI being configured
11998  * @v_idx: index of the vector in the vsi struct
11999  *
12000  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12001  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)12002 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12003 {
12004 	struct i40e_q_vector *q_vector;
12005 
12006 	/* allocate q_vector */
12007 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12008 	if (!q_vector)
12009 		return -ENOMEM;
12010 
12011 	q_vector->vsi = vsi;
12012 	q_vector->v_idx = v_idx;
12013 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12014 
12015 	if (vsi->netdev)
12016 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12017 
12018 	/* tie q_vector and vsi together */
12019 	vsi->q_vectors[v_idx] = q_vector;
12020 
12021 	return 0;
12022 }
12023 
12024 /**
12025  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12026  * @vsi: the VSI being configured
12027  *
12028  * We allocate one q_vector per queue interrupt.  If allocation fails we
12029  * return -ENOMEM.
12030  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)12031 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12032 {
12033 	struct i40e_pf *pf = vsi->back;
12034 	int err, v_idx, num_q_vectors;
12035 
12036 	/* if not MSIX, give the one vector only to the LAN VSI */
12037 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12038 		num_q_vectors = vsi->num_q_vectors;
12039 	else if (vsi->type == I40E_VSI_MAIN)
12040 		num_q_vectors = 1;
12041 	else
12042 		return -EINVAL;
12043 
12044 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12045 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12046 		if (err)
12047 			goto err_out;
12048 	}
12049 
12050 	return 0;
12051 
12052 err_out:
12053 	while (v_idx--)
12054 		i40e_free_q_vector(vsi, v_idx);
12055 
12056 	return err;
12057 }
12058 
12059 /**
12060  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12061  * @pf: board private structure to initialize
12062  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12063 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12064 {
12065 	int vectors = 0;
12066 	ssize_t size;
12067 
12068 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12069 		vectors = i40e_init_msix(pf);
12070 		if (vectors < 0) {
12071 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12072 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12073 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12074 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12075 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12076 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12077 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12078 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12079 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12080 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12081 
12082 			/* rework the queue expectations without MSIX */
12083 			i40e_determine_queue_usage(pf);
12084 		}
12085 	}
12086 
12087 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12088 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12089 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12090 		vectors = pci_enable_msi(pf->pdev);
12091 		if (vectors < 0) {
12092 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12093 				 vectors);
12094 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12095 		}
12096 		vectors = 1;  /* one MSI or Legacy vector */
12097 	}
12098 
12099 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12100 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12101 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12102 
12103 	/* set up vector assignment tracking */
12104 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12105 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12106 	if (!pf->irq_pile)
12107 		return -ENOMEM;
12108 
12109 	pf->irq_pile->num_entries = vectors;
12110 
12111 	/* track first vector for misc interrupts, ignore return */
12112 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12113 
12114 	return 0;
12115 }
12116 
12117 /**
12118  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12119  * @pf: private board data structure
12120  *
12121  * Restore the interrupt scheme that was cleared when we suspended the
12122  * device. This should be called during resume to re-allocate the q_vectors
12123  * and reacquire IRQs.
12124  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12125 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12126 {
12127 	struct i40e_vsi *vsi;
12128 	int err, i;
12129 
12130 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12131 	 * scheme. We need to re-enabled them here in order to attempt to
12132 	 * re-acquire the MSI or MSI-X vectors
12133 	 */
12134 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12135 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12136 
12137 	err = i40e_init_interrupt_scheme(pf);
12138 	if (err)
12139 		return err;
12140 
12141 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12142 	 * rings together again.
12143 	 */
12144 	i40e_pf_for_each_vsi(pf, i, vsi) {
12145 		err = i40e_vsi_alloc_q_vectors(vsi);
12146 		if (err)
12147 			goto err_unwind;
12148 
12149 		i40e_vsi_map_rings_to_vectors(vsi);
12150 	}
12151 
12152 	err = i40e_setup_misc_vector(pf);
12153 	if (err)
12154 		goto err_unwind;
12155 
12156 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12157 		i40e_client_update_msix_info(pf);
12158 
12159 	return 0;
12160 
12161 err_unwind:
12162 	while (i--) {
12163 		if (pf->vsi[i])
12164 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12165 	}
12166 
12167 	return err;
12168 }
12169 
12170 /**
12171  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12172  * non queue events in recovery mode
12173  * @pf: board private structure
12174  *
12175  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12176  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12177  * This is handled differently than in recovery mode since no Tx/Rx resources
12178  * are being allocated.
12179  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12180 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12181 {
12182 	int err;
12183 
12184 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12185 		err = i40e_setup_misc_vector(pf);
12186 
12187 		if (err) {
12188 			dev_info(&pf->pdev->dev,
12189 				 "MSI-X misc vector request failed, error %d\n",
12190 				 err);
12191 			return err;
12192 		}
12193 	} else {
12194 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12195 
12196 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12197 				  pf->int_name, pf);
12198 
12199 		if (err) {
12200 			dev_info(&pf->pdev->dev,
12201 				 "MSI/legacy misc vector request failed, error %d\n",
12202 				 err);
12203 			return err;
12204 		}
12205 		i40e_enable_misc_int_causes(pf);
12206 		i40e_irq_dynamic_enable_icr0(pf);
12207 	}
12208 
12209 	return 0;
12210 }
12211 
12212 /**
12213  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12214  * @pf: board private structure
12215  *
12216  * This sets up the handler for MSIX 0, which is used to manage the
12217  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12218  * when in MSI or Legacy interrupt mode.
12219  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12220 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12221 {
12222 	struct i40e_hw *hw = &pf->hw;
12223 	int err = 0;
12224 
12225 	/* Only request the IRQ once, the first time through. */
12226 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12227 		err = request_irq(pf->msix_entries[0].vector,
12228 				  i40e_intr, 0, pf->int_name, pf);
12229 		if (err) {
12230 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12231 			dev_info(&pf->pdev->dev,
12232 				 "request_irq for %s failed: %d\n",
12233 				 pf->int_name, err);
12234 			return -EFAULT;
12235 		}
12236 	}
12237 
12238 	i40e_enable_misc_int_causes(pf);
12239 
12240 	/* associate no queues to the misc vector */
12241 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12242 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12243 
12244 	i40e_flush(hw);
12245 
12246 	i40e_irq_dynamic_enable_icr0(pf);
12247 
12248 	return err;
12249 }
12250 
12251 /**
12252  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12253  * @vsi: Pointer to vsi structure
12254  * @seed: Buffter to store the hash keys
12255  * @lut: Buffer to store the lookup table entries
12256  * @lut_size: Size of buffer to store the lookup table entries
12257  *
12258  * Return 0 on success, negative on failure
12259  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12260 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12261 			   u8 *lut, u16 lut_size)
12262 {
12263 	struct i40e_pf *pf = vsi->back;
12264 	struct i40e_hw *hw = &pf->hw;
12265 	int ret = 0;
12266 
12267 	if (seed) {
12268 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12269 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12270 		if (ret) {
12271 			dev_info(&pf->pdev->dev,
12272 				 "Cannot get RSS key, err %pe aq_err %s\n",
12273 				 ERR_PTR(ret),
12274 				 libie_aq_str(pf->hw.aq.asq_last_status));
12275 			return ret;
12276 		}
12277 	}
12278 
12279 	if (lut) {
12280 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12281 
12282 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12283 		if (ret) {
12284 			dev_info(&pf->pdev->dev,
12285 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12286 				 ERR_PTR(ret),
12287 				 libie_aq_str(pf->hw.aq.asq_last_status));
12288 			return ret;
12289 		}
12290 	}
12291 
12292 	return ret;
12293 }
12294 
12295 /**
12296  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12297  * @vsi: Pointer to vsi structure
12298  * @seed: RSS hash seed
12299  * @lut: Lookup table
12300  * @lut_size: Lookup table size
12301  *
12302  * Returns 0 on success, negative on failure
12303  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12304 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12305 			       const u8 *lut, u16 lut_size)
12306 {
12307 	struct i40e_pf *pf = vsi->back;
12308 	struct i40e_hw *hw = &pf->hw;
12309 	u16 vf_id = vsi->vf_id;
12310 	u8 i;
12311 
12312 	/* Fill out hash function seed */
12313 	if (seed) {
12314 		u32 *seed_dw = (u32 *)seed;
12315 
12316 		if (vsi->type == I40E_VSI_MAIN) {
12317 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12318 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12319 		} else if (vsi->type == I40E_VSI_SRIOV) {
12320 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12321 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12322 		} else {
12323 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12324 		}
12325 	}
12326 
12327 	if (lut) {
12328 		u32 *lut_dw = (u32 *)lut;
12329 
12330 		if (vsi->type == I40E_VSI_MAIN) {
12331 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12332 				return -EINVAL;
12333 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12334 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12335 		} else if (vsi->type == I40E_VSI_SRIOV) {
12336 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12337 				return -EINVAL;
12338 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12339 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12340 		} else {
12341 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12342 		}
12343 	}
12344 	i40e_flush(hw);
12345 
12346 	return 0;
12347 }
12348 
12349 /**
12350  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12351  * @vsi: Pointer to VSI structure
12352  * @seed: Buffer to store the keys
12353  * @lut: Buffer to store the lookup table entries
12354  * @lut_size: Size of buffer to store the lookup table entries
12355  *
12356  * Returns 0 on success, negative on failure
12357  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12358 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12359 			    u8 *lut, u16 lut_size)
12360 {
12361 	struct i40e_pf *pf = vsi->back;
12362 	struct i40e_hw *hw = &pf->hw;
12363 	u16 i;
12364 
12365 	if (seed) {
12366 		u32 *seed_dw = (u32 *)seed;
12367 
12368 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12369 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12370 	}
12371 	if (lut) {
12372 		u32 *lut_dw = (u32 *)lut;
12373 
12374 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12375 			return -EINVAL;
12376 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12377 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12378 	}
12379 
12380 	return 0;
12381 }
12382 
12383 /**
12384  * i40e_config_rss - Configure RSS keys and lut
12385  * @vsi: Pointer to VSI structure
12386  * @seed: RSS hash seed
12387  * @lut: Lookup table
12388  * @lut_size: Lookup table size
12389  *
12390  * Returns 0 on success, negative on failure
12391  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12392 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12393 {
12394 	struct i40e_pf *pf = vsi->back;
12395 
12396 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12397 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12398 	else
12399 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12400 }
12401 
12402 /**
12403  * i40e_get_rss - Get RSS keys and lut
12404  * @vsi: Pointer to VSI structure
12405  * @seed: Buffer to store the keys
12406  * @lut: Buffer to store the lookup table entries
12407  * @lut_size: Size of buffer to store the lookup table entries
12408  *
12409  * Returns 0 on success, negative on failure
12410  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12411 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12412 {
12413 	struct i40e_pf *pf = vsi->back;
12414 
12415 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12416 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12417 	else
12418 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12419 }
12420 
12421 /**
12422  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12423  * @pf: Pointer to board private structure
12424  * @lut: Lookup table
12425  * @rss_table_size: Lookup table size
12426  * @rss_size: Range of queue number for hashing
12427  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12428 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12429 		       u16 rss_table_size, u16 rss_size)
12430 {
12431 	u16 i;
12432 
12433 	for (i = 0; i < rss_table_size; i++)
12434 		lut[i] = i % rss_size;
12435 }
12436 
12437 /**
12438  * i40e_pf_config_rss - Prepare for RSS if used
12439  * @pf: board private structure
12440  **/
i40e_pf_config_rss(struct i40e_pf * pf)12441 static int i40e_pf_config_rss(struct i40e_pf *pf)
12442 {
12443 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12444 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12445 	u8 *lut;
12446 	struct i40e_hw *hw = &pf->hw;
12447 	u32 reg_val;
12448 	u64 hena;
12449 	int ret;
12450 
12451 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12452 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12453 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12454 	hena |= i40e_pf_get_default_rss_hashcfg(pf);
12455 
12456 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12457 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12458 
12459 	/* Determine the RSS table size based on the hardware capabilities */
12460 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12461 	reg_val = (pf->rss_table_size == 512) ?
12462 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12463 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12464 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12465 
12466 	/* Determine the RSS size of the VSI */
12467 	if (!vsi->rss_size) {
12468 		u16 qcount;
12469 		/* If the firmware does something weird during VSI init, we
12470 		 * could end up with zero TCs. Check for that to avoid
12471 		 * divide-by-zero. It probably won't pass traffic, but it also
12472 		 * won't panic.
12473 		 */
12474 		qcount = vsi->num_queue_pairs /
12475 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12476 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12477 	}
12478 	if (!vsi->rss_size)
12479 		return -EINVAL;
12480 
12481 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12482 	if (!lut)
12483 		return -ENOMEM;
12484 
12485 	/* Use user configured lut if there is one, otherwise use default */
12486 	if (vsi->rss_lut_user)
12487 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12488 	else
12489 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12490 
12491 	/* Use user configured hash key if there is one, otherwise
12492 	 * use default.
12493 	 */
12494 	if (vsi->rss_hkey_user)
12495 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12496 	else
12497 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12498 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12499 	kfree(lut);
12500 
12501 	return ret;
12502 }
12503 
12504 /**
12505  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12506  * @pf: board private structure
12507  * @queue_count: the requested queue count for rss.
12508  *
12509  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12510  * count which may be different from the requested queue count.
12511  * Note: expects to be called while under rtnl_lock()
12512  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12513 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12514 {
12515 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12516 	int new_rss_size;
12517 
12518 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12519 		return 0;
12520 
12521 	queue_count = min_t(int, queue_count, num_online_cpus());
12522 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12523 
12524 	if (queue_count != vsi->num_queue_pairs) {
12525 		u16 qcount;
12526 
12527 		vsi->req_queue_pairs = queue_count;
12528 		i40e_prep_for_reset(pf);
12529 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12530 			return pf->alloc_rss_size;
12531 
12532 		pf->alloc_rss_size = new_rss_size;
12533 
12534 		i40e_reset_and_rebuild(pf, true, true);
12535 
12536 		/* Discard the user configured hash keys and lut, if less
12537 		 * queues are enabled.
12538 		 */
12539 		if (queue_count < vsi->rss_size) {
12540 			i40e_clear_rss_config_user(vsi);
12541 			dev_dbg(&pf->pdev->dev,
12542 				"discard user configured hash keys and lut\n");
12543 		}
12544 
12545 		/* Reset vsi->rss_size, as number of enabled queues changed */
12546 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12547 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12548 
12549 		i40e_pf_config_rss(pf);
12550 	}
12551 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12552 		 vsi->req_queue_pairs, pf->rss_size_max);
12553 	return pf->alloc_rss_size;
12554 }
12555 
12556 /**
12557  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12558  * @pf: board private structure
12559  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12560 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12561 {
12562 	bool min_valid, max_valid;
12563 	u32 max_bw, min_bw;
12564 	int status;
12565 
12566 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12567 					   &min_valid, &max_valid);
12568 
12569 	if (!status) {
12570 		if (min_valid)
12571 			pf->min_bw = min_bw;
12572 		if (max_valid)
12573 			pf->max_bw = max_bw;
12574 	}
12575 
12576 	return status;
12577 }
12578 
12579 /**
12580  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12581  * @pf: board private structure
12582  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12583 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12584 {
12585 	struct i40e_aqc_configure_partition_bw_data bw_data;
12586 	int status;
12587 
12588 	memset(&bw_data, 0, sizeof(bw_data));
12589 
12590 	/* Set the valid bit for this PF */
12591 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12592 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12593 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12594 
12595 	/* Set the new bandwidths */
12596 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12597 
12598 	return status;
12599 }
12600 
12601 /**
12602  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12603  * if total port shutdown feature is enabled for this PF
12604  * @pf: board private structure
12605  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12606 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12607 {
12608 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12609 #define I40E_FEATURES_ENABLE_PTR		0x2A
12610 #define I40E_CURRENT_SETTING_PTR		0x2B
12611 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12612 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12613 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12614 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12615 	u16 sr_emp_sr_settings_ptr = 0;
12616 	u16 features_enable = 0;
12617 	u16 link_behavior = 0;
12618 	int read_status = 0;
12619 	bool ret = false;
12620 
12621 	read_status = i40e_read_nvm_word(&pf->hw,
12622 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12623 					 &sr_emp_sr_settings_ptr);
12624 	if (read_status)
12625 		goto err_nvm;
12626 	read_status = i40e_read_nvm_word(&pf->hw,
12627 					 sr_emp_sr_settings_ptr +
12628 					 I40E_FEATURES_ENABLE_PTR,
12629 					 &features_enable);
12630 	if (read_status)
12631 		goto err_nvm;
12632 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12633 		read_status = i40e_read_nvm_module_data(&pf->hw,
12634 							I40E_SR_EMP_SR_SETTINGS_PTR,
12635 							I40E_CURRENT_SETTING_PTR,
12636 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12637 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12638 							&link_behavior);
12639 		if (read_status)
12640 			goto err_nvm;
12641 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12642 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12643 	}
12644 	return ret;
12645 
12646 err_nvm:
12647 	dev_warn(&pf->pdev->dev,
12648 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12649 		 ERR_PTR(read_status));
12650 	return ret;
12651 }
12652 
12653 /**
12654  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12655  * @pf: board private structure to initialize
12656  *
12657  * i40e_sw_init initializes the Adapter private data structure.
12658  * Fields are initialized based on PCI device information and
12659  * OS network device settings (MTU size).
12660  **/
i40e_sw_init(struct i40e_pf * pf)12661 static int i40e_sw_init(struct i40e_pf *pf)
12662 {
12663 	int err = 0;
12664 	int size;
12665 	u16 pow;
12666 
12667 	/* Set default capability flags */
12668 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12669 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12670 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12671 
12672 	/* Set default ITR */
12673 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12674 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12675 
12676 	/* Depending on PF configurations, it is possible that the RSS
12677 	 * maximum might end up larger than the available queues
12678 	 */
12679 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12680 	pf->alloc_rss_size = 1;
12681 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12682 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12683 				 pf->hw.func_caps.num_tx_qp);
12684 
12685 	/* find the next higher power-of-2 of num cpus */
12686 	pow = roundup_pow_of_two(num_online_cpus());
12687 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12688 
12689 	if (pf->hw.func_caps.rss) {
12690 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12691 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12692 					   num_online_cpus());
12693 	}
12694 
12695 	/* MFP mode enabled */
12696 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12697 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12698 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12699 		if (i40e_get_partition_bw_setting(pf)) {
12700 			dev_warn(&pf->pdev->dev,
12701 				 "Could not get partition bw settings\n");
12702 		} else {
12703 			dev_info(&pf->pdev->dev,
12704 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12705 				 pf->min_bw, pf->max_bw);
12706 
12707 			/* nudge the Tx scheduler */
12708 			i40e_set_partition_bw_setting(pf);
12709 		}
12710 	}
12711 
12712 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12713 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12714 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12715 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12716 		    pf->hw.num_partitions > 1)
12717 			dev_info(&pf->pdev->dev,
12718 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12719 		else
12720 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12721 		pf->fdir_pf_filter_count =
12722 				 pf->hw.func_caps.fd_filters_guaranteed;
12723 		pf->hw.fdir_shared_filter_count =
12724 				 pf->hw.func_caps.fd_filters_best_effort;
12725 	}
12726 
12727 	/* Enable HW ATR eviction if possible */
12728 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12729 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12730 
12731 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12732 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12733 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12734 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12735 	}
12736 
12737 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12738 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12739 		/* IWARP needs one extra vector for CQP just like MISC.*/
12740 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12741 	}
12742 	/* Stopping FW LLDP engine is supported on XL710 and X722
12743 	 * starting from FW versions determined in i40e_init_adminq.
12744 	 * Stopping the FW LLDP engine is not supported on XL710
12745 	 * if NPAR is functioning so unset this hw flag in this case.
12746 	 */
12747 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12748 	    pf->hw.func_caps.npar_enable)
12749 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12750 
12751 #ifdef CONFIG_PCI_IOV
12752 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12753 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12754 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12755 		pf->num_req_vfs = min_t(int,
12756 					pf->hw.func_caps.num_vfs,
12757 					I40E_MAX_VF_COUNT);
12758 	}
12759 #endif /* CONFIG_PCI_IOV */
12760 	pf->lan_veb = I40E_NO_VEB;
12761 	pf->lan_vsi = I40E_NO_VSI;
12762 
12763 	/* By default FW has this off for performance reasons */
12764 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12765 
12766 	/* set up queue assignment tracking */
12767 	size = sizeof(struct i40e_lump_tracking)
12768 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12769 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12770 	if (!pf->qp_pile) {
12771 		err = -ENOMEM;
12772 		goto sw_init_done;
12773 	}
12774 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12775 
12776 	pf->tx_timeout_recovery_level = 1;
12777 
12778 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12779 	    i40e_is_total_port_shutdown_enabled(pf)) {
12780 		/* Link down on close must be on when total port shutdown
12781 		 * is enabled for a given port
12782 		 */
12783 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12784 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12785 		dev_info(&pf->pdev->dev,
12786 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12787 	}
12788 	mutex_init(&pf->switch_mutex);
12789 
12790 sw_init_done:
12791 	return err;
12792 }
12793 
12794 /**
12795  * i40e_set_ntuple - set the ntuple feature flag and take action
12796  * @pf: board private structure to initialize
12797  * @features: the feature set that the stack is suggesting
12798  *
12799  * returns a bool to indicate if reset needs to happen
12800  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12801 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12802 {
12803 	bool need_reset = false;
12804 
12805 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12806 	 * the state changed, we need to reset.
12807 	 */
12808 	if (features & NETIF_F_NTUPLE) {
12809 		/* Enable filters and mark for reset */
12810 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12811 			need_reset = true;
12812 		/* enable FD_SB only if there is MSI-X vector and no cloud
12813 		 * filters exist
12814 		 */
12815 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12816 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12817 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12818 		}
12819 	} else {
12820 		/* turn off filters, mark for reset and clear SW filter list */
12821 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12822 			need_reset = true;
12823 			i40e_fdir_filter_exit(pf);
12824 		}
12825 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12826 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12827 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12828 
12829 		/* reset fd counters */
12830 		pf->fd_add_err = 0;
12831 		pf->fd_atr_cnt = 0;
12832 		/* if ATR was auto disabled it can be re-enabled. */
12833 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12834 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12835 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12836 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12837 	}
12838 	return need_reset;
12839 }
12840 
12841 /**
12842  * i40e_clear_rss_lut - clear the rx hash lookup table
12843  * @vsi: the VSI being configured
12844  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12845 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12846 {
12847 	struct i40e_pf *pf = vsi->back;
12848 	struct i40e_hw *hw = &pf->hw;
12849 	u16 vf_id = vsi->vf_id;
12850 	u8 i;
12851 
12852 	if (vsi->type == I40E_VSI_MAIN) {
12853 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12854 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12855 	} else if (vsi->type == I40E_VSI_SRIOV) {
12856 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12857 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12858 	} else {
12859 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12860 	}
12861 }
12862 
12863 /**
12864  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12865  * @vsi: ptr to VSI
12866  * @ena: flag to indicate the on/off setting
12867  */
i40e_set_loopback(struct i40e_vsi * vsi,bool ena)12868 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12869 {
12870 	bool if_running = netif_running(vsi->netdev) &&
12871 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12872 	int ret;
12873 
12874 	if (if_running)
12875 		i40e_down(vsi);
12876 
12877 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12878 	if (ret)
12879 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12880 	if (if_running)
12881 		i40e_up(vsi);
12882 
12883 	return ret;
12884 }
12885 
12886 /**
12887  * i40e_set_features - set the netdev feature flags
12888  * @netdev: ptr to the netdev being adjusted
12889  * @features: the feature set that the stack is suggesting
12890  * Note: expects to be called while under rtnl_lock()
12891  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12892 static int i40e_set_features(struct net_device *netdev,
12893 			     netdev_features_t features)
12894 {
12895 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12896 	struct i40e_vsi *vsi = np->vsi;
12897 	struct i40e_pf *pf = vsi->back;
12898 	bool need_reset;
12899 
12900 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12901 		i40e_pf_config_rss(pf);
12902 	else if (!(features & NETIF_F_RXHASH) &&
12903 		 netdev->features & NETIF_F_RXHASH)
12904 		i40e_clear_rss_lut(vsi);
12905 
12906 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12907 		i40e_vlan_stripping_enable(vsi);
12908 	else
12909 		i40e_vlan_stripping_disable(vsi);
12910 
12911 	if (!(features & NETIF_F_HW_TC) &&
12912 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12913 		dev_err(&pf->pdev->dev,
12914 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12915 		return -EINVAL;
12916 	}
12917 
12918 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12919 		i40e_del_all_macvlans(vsi);
12920 
12921 	need_reset = i40e_set_ntuple(pf, features);
12922 
12923 	if (need_reset)
12924 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12925 
12926 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12927 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12928 
12929 	return 0;
12930 }
12931 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12932 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12933 				    unsigned int table, unsigned int idx,
12934 				    struct udp_tunnel_info *ti)
12935 {
12936 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12937 	struct i40e_hw *hw = &np->vsi->back->hw;
12938 	u8 type, filter_index;
12939 	int ret;
12940 
12941 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12942 						   I40E_AQC_TUNNEL_TYPE_NGE;
12943 
12944 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12945 				     NULL);
12946 	if (ret) {
12947 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12948 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12949 		return -EIO;
12950 	}
12951 
12952 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12953 	return 0;
12954 }
12955 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12956 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12957 				      unsigned int table, unsigned int idx,
12958 				      struct udp_tunnel_info *ti)
12959 {
12960 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12961 	struct i40e_hw *hw = &np->vsi->back->hw;
12962 	int ret;
12963 
12964 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12965 	if (ret) {
12966 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
12967 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12968 		return -EIO;
12969 	}
12970 
12971 	return 0;
12972 }
12973 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)12974 static int i40e_get_phys_port_id(struct net_device *netdev,
12975 				 struct netdev_phys_item_id *ppid)
12976 {
12977 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12978 	struct i40e_pf *pf = np->vsi->back;
12979 	struct i40e_hw *hw = &pf->hw;
12980 
12981 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
12982 		return -EOPNOTSUPP;
12983 
12984 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12985 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12986 
12987 	return 0;
12988 }
12989 
12990 /**
12991  * i40e_ndo_fdb_add - add an entry to the hardware database
12992  * @ndm: the input from the stack
12993  * @tb: pointer to array of nladdr (unused)
12994  * @dev: the net device pointer
12995  * @addr: the MAC address entry being added
12996  * @vid: VLAN ID
12997  * @flags: instructions from stack about fdb operation
12998  * @notified: whether notification was emitted
12999  * @extack: netlink extended ack, unused currently
13000  */
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)13001 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13002 			    struct net_device *dev,
13003 			    const unsigned char *addr, u16 vid,
13004 			    u16 flags, bool *notified,
13005 			    struct netlink_ext_ack *extack)
13006 {
13007 	struct i40e_netdev_priv *np = netdev_priv(dev);
13008 	struct i40e_pf *pf = np->vsi->back;
13009 	int err = 0;
13010 
13011 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13012 		return -EOPNOTSUPP;
13013 
13014 	if (vid) {
13015 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13016 		return -EINVAL;
13017 	}
13018 
13019 	/* Hardware does not support aging addresses so if a
13020 	 * ndm_state is given only allow permanent addresses
13021 	 */
13022 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13023 		netdev_info(dev, "FDB only supports static addresses\n");
13024 		return -EINVAL;
13025 	}
13026 
13027 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13028 		err = dev_uc_add_excl(dev, addr);
13029 	else if (is_multicast_ether_addr(addr))
13030 		err = dev_mc_add_excl(dev, addr);
13031 	else
13032 		err = -EINVAL;
13033 
13034 	/* Only return duplicate errors if NLM_F_EXCL is set */
13035 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13036 		err = 0;
13037 
13038 	return err;
13039 }
13040 
13041 /**
13042  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13043  * @dev: the netdev being configured
13044  * @nlh: RTNL message
13045  * @flags: bridge flags
13046  * @extack: netlink extended ack
13047  *
13048  * Inserts a new hardware bridge if not already created and
13049  * enables the bridging mode requested (VEB or VEPA). If the
13050  * hardware bridge has already been inserted and the request
13051  * is to change the mode then that requires a PF reset to
13052  * allow rebuild of the components with required hardware
13053  * bridge mode enabled.
13054  *
13055  * Note: expects to be called while under rtnl_lock()
13056  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13057 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13058 				   struct nlmsghdr *nlh,
13059 				   u16 flags,
13060 				   struct netlink_ext_ack *extack)
13061 {
13062 	struct i40e_netdev_priv *np = netdev_priv(dev);
13063 	struct i40e_vsi *vsi = np->vsi;
13064 	struct i40e_pf *pf = vsi->back;
13065 	struct nlattr *attr, *br_spec;
13066 	struct i40e_veb *veb;
13067 	int rem;
13068 
13069 	/* Only for PF VSI for now */
13070 	if (vsi->type != I40E_VSI_MAIN)
13071 		return -EOPNOTSUPP;
13072 
13073 	/* Find the HW bridge for PF VSI */
13074 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13075 
13076 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13077 	if (!br_spec)
13078 		return -EINVAL;
13079 
13080 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13081 		__u16 mode = nla_get_u16(attr);
13082 
13083 		if ((mode != BRIDGE_MODE_VEPA) &&
13084 		    (mode != BRIDGE_MODE_VEB))
13085 			return -EINVAL;
13086 
13087 		/* Insert a new HW bridge */
13088 		if (!veb) {
13089 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13090 					     vsi->tc_config.enabled_tc);
13091 			if (veb) {
13092 				veb->bridge_mode = mode;
13093 				i40e_config_bridge_mode(veb);
13094 			} else {
13095 				/* No Bridge HW offload available */
13096 				return -ENOENT;
13097 			}
13098 			break;
13099 		} else if (mode != veb->bridge_mode) {
13100 			/* Existing HW bridge but different mode needs reset */
13101 			veb->bridge_mode = mode;
13102 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13103 			if (mode == BRIDGE_MODE_VEB)
13104 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13105 			else
13106 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13107 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13108 			break;
13109 		}
13110 	}
13111 
13112 	return 0;
13113 }
13114 
13115 /**
13116  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13117  * @skb: skb buff
13118  * @pid: process id
13119  * @seq: RTNL message seq #
13120  * @dev: the netdev being configured
13121  * @filter_mask: unused
13122  * @nlflags: netlink flags passed in
13123  *
13124  * Return the mode in which the hardware bridge is operating in
13125  * i.e VEB or VEPA.
13126  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13127 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13128 				   struct net_device *dev,
13129 				   u32 __always_unused filter_mask,
13130 				   int nlflags)
13131 {
13132 	struct i40e_netdev_priv *np = netdev_priv(dev);
13133 	struct i40e_vsi *vsi = np->vsi;
13134 	struct i40e_pf *pf = vsi->back;
13135 	struct i40e_veb *veb;
13136 
13137 	/* Only for PF VSI for now */
13138 	if (vsi->type != I40E_VSI_MAIN)
13139 		return -EOPNOTSUPP;
13140 
13141 	/* Find the HW bridge for the PF VSI */
13142 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13143 	if (!veb)
13144 		return 0;
13145 
13146 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13147 				       0, 0, nlflags, filter_mask, NULL);
13148 }
13149 
13150 /**
13151  * i40e_features_check - Validate encapsulated packet conforms to limits
13152  * @skb: skb buff
13153  * @dev: This physical port's netdev
13154  * @features: Offload features that the stack believes apply
13155  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13156 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13157 					     struct net_device *dev,
13158 					     netdev_features_t features)
13159 {
13160 	size_t len;
13161 
13162 	/* No point in doing any of this if neither checksum nor GSO are
13163 	 * being requested for this frame.  We can rule out both by just
13164 	 * checking for CHECKSUM_PARTIAL
13165 	 */
13166 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13167 		return features;
13168 
13169 	/* We cannot support GSO if the MSS is going to be less than
13170 	 * 64 bytes.  If it is then we need to drop support for GSO.
13171 	 */
13172 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13173 		features &= ~NETIF_F_GSO_MASK;
13174 
13175 	/* MACLEN can support at most 63 words */
13176 	len = skb_network_offset(skb);
13177 	if (len & ~(63 * 2))
13178 		goto out_err;
13179 
13180 	/* IPLEN and EIPLEN can support at most 127 dwords */
13181 	len = skb_network_header_len(skb);
13182 	if (len & ~(127 * 4))
13183 		goto out_err;
13184 
13185 	if (skb->encapsulation) {
13186 		/* L4TUNLEN can support 127 words */
13187 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13188 		if (len & ~(127 * 2))
13189 			goto out_err;
13190 
13191 		/* IPLEN can support at most 127 dwords */
13192 		len = skb_inner_transport_header(skb) -
13193 		      skb_inner_network_header(skb);
13194 		if (len & ~(127 * 4))
13195 			goto out_err;
13196 	}
13197 
13198 	/* No need to validate L4LEN as TCP is the only protocol with a
13199 	 * flexible value and we support all possible values supported
13200 	 * by TCP, which is at most 15 dwords
13201 	 */
13202 
13203 	return features;
13204 out_err:
13205 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13206 }
13207 
13208 /**
13209  * i40e_xdp_setup - add/remove an XDP program
13210  * @vsi: VSI to changed
13211  * @prog: XDP program
13212  * @extack: netlink extended ack
13213  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13214 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13215 			  struct netlink_ext_ack *extack)
13216 {
13217 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13218 	struct i40e_pf *pf = vsi->back;
13219 	struct bpf_prog *old_prog;
13220 	bool need_reset;
13221 	int i;
13222 
13223 	/* VSI shall be deleted in a moment, block loading new programs */
13224 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13225 		return -EINVAL;
13226 
13227 	/* Don't allow frames that span over multiple buffers */
13228 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13229 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13230 		return -EINVAL;
13231 	}
13232 
13233 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13234 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13235 	if (need_reset)
13236 		i40e_prep_for_reset(pf);
13237 
13238 	old_prog = xchg(&vsi->xdp_prog, prog);
13239 
13240 	if (need_reset) {
13241 		if (!prog) {
13242 			xdp_features_clear_redirect_target(vsi->netdev);
13243 			/* Wait until ndo_xsk_wakeup completes. */
13244 			synchronize_rcu();
13245 		}
13246 		i40e_reset_and_rebuild(pf, true, true);
13247 	}
13248 
13249 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13250 		if (i40e_realloc_rx_bi_zc(vsi, true))
13251 			return -ENOMEM;
13252 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13253 		if (i40e_realloc_rx_bi_zc(vsi, false))
13254 			return -ENOMEM;
13255 	}
13256 
13257 	for (i = 0; i < vsi->num_queue_pairs; i++)
13258 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13259 
13260 	if (old_prog)
13261 		bpf_prog_put(old_prog);
13262 
13263 	/* Kick start the NAPI context if there is an AF_XDP socket open
13264 	 * on that queue id. This so that receiving will start.
13265 	 */
13266 	if (need_reset && prog) {
13267 		for (i = 0; i < vsi->num_queue_pairs; i++)
13268 			if (vsi->xdp_rings[i]->xsk_pool)
13269 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13270 						      XDP_WAKEUP_RX);
13271 		xdp_features_set_redirect_target(vsi->netdev, true);
13272 	}
13273 
13274 	return 0;
13275 }
13276 
13277 /**
13278  * i40e_enter_busy_conf - Enters busy config state
13279  * @vsi: vsi
13280  *
13281  * Returns 0 on success, <0 for failure.
13282  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13283 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13284 {
13285 	struct i40e_pf *pf = vsi->back;
13286 	int timeout = 50;
13287 
13288 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13289 		timeout--;
13290 		if (!timeout)
13291 			return -EBUSY;
13292 		usleep_range(1000, 2000);
13293 	}
13294 
13295 	return 0;
13296 }
13297 
13298 /**
13299  * i40e_exit_busy_conf - Exits busy config state
13300  * @vsi: vsi
13301  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13302 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13303 {
13304 	struct i40e_pf *pf = vsi->back;
13305 
13306 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13307 }
13308 
13309 /**
13310  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13311  * @vsi: vsi
13312  * @queue_pair: queue pair
13313  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13314 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13315 {
13316 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13317 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13318 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13319 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13320 	if (i40e_enabled_xdp_vsi(vsi)) {
13321 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13322 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13323 	}
13324 }
13325 
13326 /**
13327  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13328  * @vsi: vsi
13329  * @queue_pair: queue pair
13330  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13331 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13332 {
13333 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13334 	if (i40e_enabled_xdp_vsi(vsi)) {
13335 		/* Make sure that in-progress ndo_xdp_xmit calls are
13336 		 * completed.
13337 		 */
13338 		synchronize_rcu();
13339 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13340 	}
13341 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13342 }
13343 
13344 /**
13345  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13346  * @vsi: vsi
13347  * @queue_pair: queue pair
13348  * @enable: true for enable, false for disable
13349  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13350 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13351 					bool enable)
13352 {
13353 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13354 	struct i40e_q_vector *q_vector = rxr->q_vector;
13355 
13356 	if (!vsi->netdev)
13357 		return;
13358 
13359 	/* All rings in a qp belong to the same qvector. */
13360 	if (q_vector->rx.ring || q_vector->tx.ring) {
13361 		if (enable)
13362 			napi_enable(&q_vector->napi);
13363 		else
13364 			napi_disable(&q_vector->napi);
13365 	}
13366 }
13367 
13368 /**
13369  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13370  * @vsi: vsi
13371  * @queue_pair: queue pair
13372  * @enable: true for enable, false for disable
13373  *
13374  * Returns 0 on success, <0 on failure.
13375  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13376 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13377 					bool enable)
13378 {
13379 	struct i40e_pf *pf = vsi->back;
13380 	int pf_q, ret = 0;
13381 
13382 	pf_q = vsi->base_queue + queue_pair;
13383 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13384 				     false /*is xdp*/, enable);
13385 	if (ret) {
13386 		dev_info(&pf->pdev->dev,
13387 			 "VSI seid %d Tx ring %d %sable timeout\n",
13388 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13389 		return ret;
13390 	}
13391 
13392 	i40e_control_rx_q(pf, pf_q, enable);
13393 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13394 	if (ret) {
13395 		dev_info(&pf->pdev->dev,
13396 			 "VSI seid %d Rx ring %d %sable timeout\n",
13397 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13398 		return ret;
13399 	}
13400 
13401 	/* Due to HW errata, on Rx disable only, the register can
13402 	 * indicate done before it really is. Needs 50ms to be sure
13403 	 */
13404 	if (!enable)
13405 		mdelay(50);
13406 
13407 	if (!i40e_enabled_xdp_vsi(vsi))
13408 		return ret;
13409 
13410 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13411 				     pf_q + vsi->alloc_queue_pairs,
13412 				     true /*is xdp*/, enable);
13413 	if (ret) {
13414 		dev_info(&pf->pdev->dev,
13415 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13416 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13417 	}
13418 
13419 	return ret;
13420 }
13421 
13422 /**
13423  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13424  * @vsi: vsi
13425  * @queue_pair: queue_pair
13426  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13427 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13428 {
13429 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13430 	struct i40e_pf *pf = vsi->back;
13431 	struct i40e_hw *hw = &pf->hw;
13432 
13433 	/* All rings in a qp belong to the same qvector. */
13434 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13435 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13436 	else
13437 		i40e_irq_dynamic_enable_icr0(pf);
13438 
13439 	i40e_flush(hw);
13440 }
13441 
13442 /**
13443  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13444  * @vsi: vsi
13445  * @queue_pair: queue_pair
13446  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13447 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13448 {
13449 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13450 	struct i40e_pf *pf = vsi->back;
13451 	struct i40e_hw *hw = &pf->hw;
13452 
13453 	/* For simplicity, instead of removing the qp interrupt causes
13454 	 * from the interrupt linked list, we simply disable the interrupt, and
13455 	 * leave the list intact.
13456 	 *
13457 	 * All rings in a qp belong to the same qvector.
13458 	 */
13459 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13460 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13461 
13462 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13463 		i40e_flush(hw);
13464 		synchronize_irq(pf->msix_entries[intpf].vector);
13465 	} else {
13466 		/* Legacy and MSI mode - this stops all interrupt handling */
13467 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13468 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13469 		i40e_flush(hw);
13470 		synchronize_irq(pf->pdev->irq);
13471 	}
13472 }
13473 
13474 /**
13475  * i40e_queue_pair_disable - Disables a queue pair
13476  * @vsi: vsi
13477  * @queue_pair: queue pair
13478  *
13479  * Returns 0 on success, <0 on failure.
13480  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13481 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13482 {
13483 	int err;
13484 
13485 	err = i40e_enter_busy_conf(vsi);
13486 	if (err)
13487 		return err;
13488 
13489 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13490 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13491 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13492 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13493 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13494 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13495 
13496 	return err;
13497 }
13498 
13499 /**
13500  * i40e_queue_pair_enable - Enables a queue pair
13501  * @vsi: vsi
13502  * @queue_pair: queue pair
13503  *
13504  * Returns 0 on success, <0 on failure.
13505  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13506 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13507 {
13508 	int err;
13509 
13510 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13511 	if (err)
13512 		return err;
13513 
13514 	if (i40e_enabled_xdp_vsi(vsi)) {
13515 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13516 		if (err)
13517 			return err;
13518 	}
13519 
13520 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13521 	if (err)
13522 		return err;
13523 
13524 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13525 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13526 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13527 
13528 	i40e_exit_busy_conf(vsi);
13529 
13530 	return err;
13531 }
13532 
13533 /**
13534  * i40e_xdp - implements ndo_bpf for i40e
13535  * @dev: netdevice
13536  * @xdp: XDP command
13537  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13538 static int i40e_xdp(struct net_device *dev,
13539 		    struct netdev_bpf *xdp)
13540 {
13541 	struct i40e_netdev_priv *np = netdev_priv(dev);
13542 	struct i40e_vsi *vsi = np->vsi;
13543 
13544 	if (vsi->type != I40E_VSI_MAIN)
13545 		return -EINVAL;
13546 
13547 	switch (xdp->command) {
13548 	case XDP_SETUP_PROG:
13549 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13550 	case XDP_SETUP_XSK_POOL:
13551 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13552 					   xdp->xsk.queue_id);
13553 	default:
13554 		return -EINVAL;
13555 	}
13556 }
13557 
13558 static const struct net_device_ops i40e_netdev_ops = {
13559 	.ndo_open		= i40e_open,
13560 	.ndo_stop		= i40e_close,
13561 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13562 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13563 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13564 	.ndo_validate_addr	= eth_validate_addr,
13565 	.ndo_set_mac_address	= i40e_set_mac,
13566 	.ndo_change_mtu		= i40e_change_mtu,
13567 	.ndo_tx_timeout		= i40e_tx_timeout,
13568 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13569 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13570 #ifdef CONFIG_NET_POLL_CONTROLLER
13571 	.ndo_poll_controller	= i40e_netpoll,
13572 #endif
13573 	.ndo_setup_tc		= __i40e_setup_tc,
13574 	.ndo_select_queue	= i40e_lan_select_queue,
13575 	.ndo_set_features	= i40e_set_features,
13576 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13577 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13578 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13579 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13580 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13581 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13582 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13583 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13584 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13585 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13586 	.ndo_features_check	= i40e_features_check,
13587 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13588 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13589 	.ndo_bpf		= i40e_xdp,
13590 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13591 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13592 	.ndo_dfwd_add_station	= i40e_fwd_add,
13593 	.ndo_dfwd_del_station	= i40e_fwd_del,
13594 	.ndo_hwtstamp_get	= i40e_ptp_hwtstamp_get,
13595 	.ndo_hwtstamp_set	= i40e_ptp_hwtstamp_set,
13596 };
13597 
13598 /**
13599  * i40e_config_netdev - Setup the netdev flags
13600  * @vsi: the VSI being configured
13601  *
13602  * Returns 0 on success, negative value on failure
13603  **/
i40e_config_netdev(struct i40e_vsi * vsi)13604 static int i40e_config_netdev(struct i40e_vsi *vsi)
13605 {
13606 	struct i40e_pf *pf = vsi->back;
13607 	struct i40e_hw *hw = &pf->hw;
13608 	struct i40e_netdev_priv *np;
13609 	struct net_device *netdev;
13610 	u8 broadcast[ETH_ALEN];
13611 	u8 mac_addr[ETH_ALEN];
13612 	int etherdev_size;
13613 	netdev_features_t hw_enc_features;
13614 	netdev_features_t hw_features;
13615 
13616 	etherdev_size = sizeof(struct i40e_netdev_priv);
13617 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13618 	if (!netdev)
13619 		return -ENOMEM;
13620 
13621 	vsi->netdev = netdev;
13622 	np = netdev_priv(netdev);
13623 	np->vsi = vsi;
13624 
13625 	hw_enc_features = NETIF_F_SG			|
13626 			  NETIF_F_HW_CSUM		|
13627 			  NETIF_F_HIGHDMA		|
13628 			  NETIF_F_SOFT_FEATURES		|
13629 			  NETIF_F_TSO			|
13630 			  NETIF_F_TSO_ECN		|
13631 			  NETIF_F_TSO6			|
13632 			  NETIF_F_GSO_GRE		|
13633 			  NETIF_F_GSO_GRE_CSUM		|
13634 			  NETIF_F_GSO_PARTIAL		|
13635 			  NETIF_F_GSO_IPXIP4		|
13636 			  NETIF_F_GSO_IPXIP6		|
13637 			  NETIF_F_GSO_UDP_TUNNEL	|
13638 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13639 			  NETIF_F_GSO_UDP_L4		|
13640 			  NETIF_F_SCTP_CRC		|
13641 			  NETIF_F_RXHASH		|
13642 			  NETIF_F_RXCSUM		|
13643 			  0;
13644 
13645 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13646 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13647 
13648 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13649 
13650 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13651 
13652 	netdev->hw_enc_features |= hw_enc_features;
13653 
13654 	/* record features VLANs can make use of */
13655 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13656 
13657 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13658 				   NETIF_F_GSO_GRE_CSUM |	\
13659 				   NETIF_F_GSO_IPXIP4 |		\
13660 				   NETIF_F_GSO_IPXIP6 |		\
13661 				   NETIF_F_GSO_UDP_TUNNEL |	\
13662 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13663 
13664 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13665 	netdev->features |= NETIF_F_GSO_PARTIAL |
13666 			    I40E_GSO_PARTIAL_FEATURES;
13667 
13668 	netdev->mpls_features |= NETIF_F_SG;
13669 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13670 	netdev->mpls_features |= NETIF_F_TSO;
13671 	netdev->mpls_features |= NETIF_F_TSO6;
13672 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13673 
13674 	/* enable macvlan offloads */
13675 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13676 
13677 	hw_features = hw_enc_features		|
13678 		      NETIF_F_HW_VLAN_CTAG_TX	|
13679 		      NETIF_F_HW_VLAN_CTAG_RX;
13680 
13681 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13682 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13683 
13684 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13685 
13686 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13687 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13688 
13689 	netdev->features &= ~NETIF_F_HW_TC;
13690 
13691 	if (vsi->type == I40E_VSI_MAIN) {
13692 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13693 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13694 		/* The following steps are necessary for two reasons. First,
13695 		 * some older NVM configurations load a default MAC-VLAN
13696 		 * filter that will accept any tagged packet, and we want to
13697 		 * replace this with a normal filter. Additionally, it is
13698 		 * possible our MAC address was provided by the platform using
13699 		 * Open Firmware or similar.
13700 		 *
13701 		 * Thus, we need to remove the default filter and install one
13702 		 * specific to the MAC address.
13703 		 */
13704 		i40e_rm_default_mac_filter(vsi, mac_addr);
13705 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13706 		i40e_add_mac_filter(vsi, mac_addr);
13707 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13708 
13709 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13710 				       NETDEV_XDP_ACT_REDIRECT |
13711 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13712 				       NETDEV_XDP_ACT_RX_SG;
13713 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13714 	} else {
13715 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13716 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13717 		 * the end, which is 4 bytes long, so force truncation of the
13718 		 * original name by IFNAMSIZ - 4
13719 		 */
13720 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13721 
13722 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13723 			 main_vsi->netdev->name);
13724 		eth_random_addr(mac_addr);
13725 
13726 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13727 		i40e_add_mac_filter(vsi, mac_addr);
13728 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13729 	}
13730 
13731 	/* Add the broadcast filter so that we initially will receive
13732 	 * broadcast packets. Note that when a new VLAN is first added the
13733 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13734 	 * specific filters as part of transitioning into "vlan" operation.
13735 	 * When more VLANs are added, the driver will copy each existing MAC
13736 	 * filter and add it for the new VLAN.
13737 	 *
13738 	 * Broadcast filters are handled specially by
13739 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13740 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13741 	 * filter. The subtask will update the correct broadcast promiscuous
13742 	 * bits as VLANs become active or inactive.
13743 	 */
13744 	eth_broadcast_addr(broadcast);
13745 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13746 	i40e_add_mac_filter(vsi, broadcast);
13747 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13748 
13749 	eth_hw_addr_set(netdev, mac_addr);
13750 	ether_addr_copy(netdev->perm_addr, mac_addr);
13751 
13752 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13753 	netdev->neigh_priv_len = sizeof(u32) * 4;
13754 
13755 	netdev->priv_flags |= IFF_UNICAST_FLT;
13756 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13757 	/* Setup netdev TC information */
13758 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13759 
13760 	netdev->netdev_ops = &i40e_netdev_ops;
13761 	netdev->watchdog_timeo = 5 * HZ;
13762 	i40e_set_ethtool_ops(netdev);
13763 
13764 	/* MTU range: 68 - 9706 */
13765 	netdev->min_mtu = ETH_MIN_MTU;
13766 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13767 
13768 	return 0;
13769 }
13770 
13771 /**
13772  * i40e_vsi_delete - Delete a VSI from the switch
13773  * @vsi: the VSI being removed
13774  *
13775  * Returns 0 on success, negative value on failure
13776  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13777 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13778 {
13779 	/* remove default VSI is not allowed */
13780 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13781 		return;
13782 
13783 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13784 }
13785 
13786 /**
13787  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13788  * @vsi: the VSI being queried
13789  *
13790  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13791  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13792 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13793 {
13794 	struct i40e_veb *veb;
13795 	struct i40e_pf *pf = vsi->back;
13796 
13797 	/* Uplink is not a bridge so default to VEB */
13798 	if (vsi->veb_idx >= I40E_MAX_VEB)
13799 		return 1;
13800 
13801 	veb = pf->veb[vsi->veb_idx];
13802 	if (!veb) {
13803 		dev_info(&pf->pdev->dev,
13804 			 "There is no veb associated with the bridge\n");
13805 		return -ENOENT;
13806 	}
13807 
13808 	/* Uplink is a bridge in VEPA mode */
13809 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13810 		return 0;
13811 	} else {
13812 		/* Uplink is a bridge in VEB mode */
13813 		return 1;
13814 	}
13815 
13816 	/* VEPA is now default bridge, so return 0 */
13817 	return 0;
13818 }
13819 
13820 /**
13821  * i40e_add_vsi - Add a VSI to the switch
13822  * @vsi: the VSI being configured
13823  *
13824  * This initializes a VSI context depending on the VSI type to be added and
13825  * passes it down to the add_vsi aq command.
13826  **/
i40e_add_vsi(struct i40e_vsi * vsi)13827 static int i40e_add_vsi(struct i40e_vsi *vsi)
13828 {
13829 	int ret = -ENODEV;
13830 	struct i40e_pf *pf = vsi->back;
13831 	struct i40e_hw *hw = &pf->hw;
13832 	struct i40e_vsi_context ctxt;
13833 	struct i40e_mac_filter *f;
13834 	struct hlist_node *h;
13835 	int bkt;
13836 
13837 	u8 enabled_tc = 0x1; /* TC0 enabled */
13838 	int f_count = 0;
13839 
13840 	memset(&ctxt, 0, sizeof(ctxt));
13841 	switch (vsi->type) {
13842 	case I40E_VSI_MAIN:
13843 		/* The PF's main VSI is already setup as part of the
13844 		 * device initialization, so we'll not bother with
13845 		 * the add_vsi call, but we will retrieve the current
13846 		 * VSI context.
13847 		 */
13848 		ctxt.seid = pf->main_vsi_seid;
13849 		ctxt.pf_num = pf->hw.pf_id;
13850 		ctxt.vf_num = 0;
13851 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13852 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13853 		if (ret) {
13854 			dev_info(&pf->pdev->dev,
13855 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13856 				 ERR_PTR(ret),
13857 				 libie_aq_str(pf->hw.aq.asq_last_status));
13858 			return -ENOENT;
13859 		}
13860 		vsi->info = ctxt.info;
13861 		vsi->info.valid_sections = 0;
13862 
13863 		vsi->seid = ctxt.seid;
13864 		vsi->id = ctxt.vsi_number;
13865 
13866 		enabled_tc = i40e_pf_get_tc_map(pf);
13867 
13868 		/* Source pruning is enabled by default, so the flag is
13869 		 * negative logic - if it's set, we need to fiddle with
13870 		 * the VSI to disable source pruning.
13871 		 */
13872 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13873 			memset(&ctxt, 0, sizeof(ctxt));
13874 			ctxt.seid = pf->main_vsi_seid;
13875 			ctxt.pf_num = pf->hw.pf_id;
13876 			ctxt.vf_num = 0;
13877 			ctxt.info.valid_sections |=
13878 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13879 			ctxt.info.switch_id =
13880 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13881 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13882 			if (ret) {
13883 				dev_info(&pf->pdev->dev,
13884 					 "update vsi failed, err %d aq_err %s\n",
13885 					 ret,
13886 					 libie_aq_str(pf->hw.aq.asq_last_status));
13887 				ret = -ENOENT;
13888 				goto err;
13889 			}
13890 		}
13891 
13892 		/* MFP mode setup queue map and update VSI */
13893 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13894 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13895 			memset(&ctxt, 0, sizeof(ctxt));
13896 			ctxt.seid = pf->main_vsi_seid;
13897 			ctxt.pf_num = pf->hw.pf_id;
13898 			ctxt.vf_num = 0;
13899 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13900 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13901 			if (ret) {
13902 				dev_info(&pf->pdev->dev,
13903 					 "update vsi failed, err %pe aq_err %s\n",
13904 					 ERR_PTR(ret),
13905 					 libie_aq_str(pf->hw.aq.asq_last_status));
13906 				ret = -ENOENT;
13907 				goto err;
13908 			}
13909 			/* update the local VSI info queue map */
13910 			i40e_vsi_update_queue_map(vsi, &ctxt);
13911 			vsi->info.valid_sections = 0;
13912 		} else {
13913 			/* Default/Main VSI is only enabled for TC0
13914 			 * reconfigure it to enable all TCs that are
13915 			 * available on the port in SFP mode.
13916 			 * For MFP case the iSCSI PF would use this
13917 			 * flow to enable LAN+iSCSI TC.
13918 			 */
13919 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13920 			if (ret) {
13921 				/* Single TC condition is not fatal,
13922 				 * message and continue
13923 				 */
13924 				dev_info(&pf->pdev->dev,
13925 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13926 					 enabled_tc,
13927 					 ERR_PTR(ret),
13928 					 libie_aq_str(pf->hw.aq.asq_last_status));
13929 			}
13930 		}
13931 		break;
13932 
13933 	case I40E_VSI_FDIR:
13934 		ctxt.pf_num = hw->pf_id;
13935 		ctxt.vf_num = 0;
13936 		ctxt.uplink_seid = vsi->uplink_seid;
13937 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13938 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13939 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
13940 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13941 			ctxt.info.valid_sections |=
13942 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13943 			ctxt.info.switch_id =
13944 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13945 		}
13946 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13947 		break;
13948 
13949 	case I40E_VSI_VMDQ2:
13950 		ctxt.pf_num = hw->pf_id;
13951 		ctxt.vf_num = 0;
13952 		ctxt.uplink_seid = vsi->uplink_seid;
13953 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13954 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13955 
13956 		/* This VSI is connected to VEB so the switch_id
13957 		 * should be set to zero by default.
13958 		 */
13959 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13960 			ctxt.info.valid_sections |=
13961 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13962 			ctxt.info.switch_id =
13963 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13964 		}
13965 
13966 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13967 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13968 		break;
13969 
13970 	case I40E_VSI_SRIOV:
13971 		ctxt.pf_num = hw->pf_id;
13972 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13973 		ctxt.uplink_seid = vsi->uplink_seid;
13974 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13975 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13976 
13977 		/* This VSI is connected to VEB so the switch_id
13978 		 * should be set to zero by default.
13979 		 */
13980 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13981 			ctxt.info.valid_sections |=
13982 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13983 			ctxt.info.switch_id =
13984 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13985 		}
13986 
13987 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
13988 			ctxt.info.valid_sections |=
13989 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13990 			ctxt.info.queueing_opt_flags |=
13991 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13992 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13993 		}
13994 
13995 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13996 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13997 		if (pf->vf[vsi->vf_id].spoofchk) {
13998 			ctxt.info.valid_sections |=
13999 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14000 			ctxt.info.sec_flags |=
14001 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14002 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14003 		}
14004 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14005 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14006 		break;
14007 
14008 	case I40E_VSI_IWARP:
14009 		/* send down message to iWARP */
14010 		break;
14011 
14012 	default:
14013 		return -ENODEV;
14014 	}
14015 
14016 	if (vsi->type != I40E_VSI_MAIN) {
14017 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14018 		if (ret) {
14019 			dev_info(&vsi->back->pdev->dev,
14020 				 "add vsi failed, err %pe aq_err %s\n",
14021 				 ERR_PTR(ret),
14022 				 libie_aq_str(pf->hw.aq.asq_last_status));
14023 			ret = -ENOENT;
14024 			goto err;
14025 		}
14026 		vsi->info = ctxt.info;
14027 		vsi->info.valid_sections = 0;
14028 		vsi->seid = ctxt.seid;
14029 		vsi->id = ctxt.vsi_number;
14030 	}
14031 
14032 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14033 	vsi->active_filters = 0;
14034 	/* If macvlan filters already exist, force them to get loaded */
14035 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14036 		f->state = I40E_FILTER_NEW;
14037 		f_count++;
14038 	}
14039 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14040 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14041 
14042 	if (f_count) {
14043 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14044 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14045 	}
14046 
14047 	/* Update VSI BW information */
14048 	ret = i40e_vsi_get_bw_info(vsi);
14049 	if (ret) {
14050 		dev_info(&pf->pdev->dev,
14051 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14052 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14053 		/* VSI is already added so not tearing that up */
14054 		ret = 0;
14055 	}
14056 
14057 err:
14058 	return ret;
14059 }
14060 
14061 /**
14062  * i40e_vsi_release - Delete a VSI and free its resources
14063  * @vsi: the VSI being removed
14064  *
14065  * Returns 0 on success or < 0 on error
14066  **/
i40e_vsi_release(struct i40e_vsi * vsi)14067 int i40e_vsi_release(struct i40e_vsi *vsi)
14068 {
14069 	struct i40e_mac_filter *f;
14070 	struct hlist_node *h;
14071 	struct i40e_veb *veb;
14072 	struct i40e_pf *pf;
14073 	u16 uplink_seid;
14074 	int i, n, bkt;
14075 
14076 	pf = vsi->back;
14077 
14078 	/* release of a VEB-owner or last VSI is not allowed */
14079 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14080 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14081 			 vsi->seid, vsi->uplink_seid);
14082 		return -ENODEV;
14083 	}
14084 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14085 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14086 		return -ENODEV;
14087 	}
14088 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14089 	uplink_seid = vsi->uplink_seid;
14090 
14091 	if (vsi->type != I40E_VSI_SRIOV) {
14092 		if (vsi->netdev_registered) {
14093 			vsi->netdev_registered = false;
14094 			if (vsi->netdev) {
14095 				/* results in a call to i40e_close() */
14096 				unregister_netdev(vsi->netdev);
14097 			}
14098 		} else {
14099 			i40e_vsi_close(vsi);
14100 		}
14101 		i40e_vsi_disable_irq(vsi);
14102 	}
14103 
14104 	if (vsi->type == I40E_VSI_MAIN)
14105 		i40e_devlink_destroy_port(pf);
14106 
14107 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14108 
14109 	/* clear the sync flag on all filters */
14110 	if (vsi->netdev) {
14111 		__dev_uc_unsync(vsi->netdev, NULL);
14112 		__dev_mc_unsync(vsi->netdev, NULL);
14113 	}
14114 
14115 	/* make sure any remaining filters are marked for deletion */
14116 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14117 		__i40e_del_filter(vsi, f);
14118 
14119 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14120 
14121 	i40e_sync_vsi_filters(vsi);
14122 
14123 	i40e_vsi_delete(vsi);
14124 	i40e_vsi_free_q_vectors(vsi);
14125 	if (vsi->netdev) {
14126 		free_netdev(vsi->netdev);
14127 		vsi->netdev = NULL;
14128 	}
14129 	i40e_vsi_clear_rings(vsi);
14130 	i40e_vsi_clear(vsi);
14131 
14132 	/* If this was the last thing on the VEB, except for the
14133 	 * controlling VSI, remove the VEB, which puts the controlling
14134 	 * VSI onto the uplink port.
14135 	 *
14136 	 * Well, okay, there's one more exception here: don't remove
14137 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14138 	 * from up the network stack.
14139 	 */
14140 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14141 	if (veb && veb->uplink_seid) {
14142 		n = 0;
14143 
14144 		/* Count non-controlling VSIs present on  the VEB */
14145 		i40e_pf_for_each_vsi(pf, i, vsi)
14146 			if (vsi->uplink_seid == uplink_seid &&
14147 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14148 				n++;
14149 
14150 		/* If there is no VSI except the control one then release
14151 		 * the VEB and put the control VSI onto VEB uplink.
14152 		 */
14153 		if (!n)
14154 			i40e_veb_release(veb);
14155 	}
14156 
14157 	return 0;
14158 }
14159 
14160 /**
14161  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14162  * @vsi: ptr to the VSI
14163  *
14164  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14165  * corresponding SW VSI structure and initializes num_queue_pairs for the
14166  * newly allocated VSI.
14167  *
14168  * Returns 0 on success or negative on failure
14169  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14170 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14171 {
14172 	int ret = -ENOENT;
14173 	struct i40e_pf *pf = vsi->back;
14174 
14175 	if (vsi->q_vectors[0]) {
14176 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14177 			 vsi->seid);
14178 		return -EEXIST;
14179 	}
14180 
14181 	if (vsi->base_vector) {
14182 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14183 			 vsi->seid, vsi->base_vector);
14184 		return -EEXIST;
14185 	}
14186 
14187 	ret = i40e_vsi_alloc_q_vectors(vsi);
14188 	if (ret) {
14189 		dev_info(&pf->pdev->dev,
14190 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14191 			 vsi->num_q_vectors, vsi->seid, ret);
14192 		vsi->num_q_vectors = 0;
14193 		goto vector_setup_out;
14194 	}
14195 
14196 	/* In Legacy mode, we do not have to get any other vector since we
14197 	 * piggyback on the misc/ICR0 for queue interrupts.
14198 	*/
14199 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14200 		return ret;
14201 	if (vsi->num_q_vectors)
14202 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14203 						 vsi->num_q_vectors, vsi->idx);
14204 	if (vsi->base_vector < 0) {
14205 		dev_info(&pf->pdev->dev,
14206 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14207 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14208 		i40e_vsi_free_q_vectors(vsi);
14209 		ret = -ENOENT;
14210 		goto vector_setup_out;
14211 	}
14212 
14213 vector_setup_out:
14214 	return ret;
14215 }
14216 
14217 /**
14218  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14219  * @vsi: pointer to the vsi.
14220  *
14221  * This re-allocates a vsi's queue resources.
14222  *
14223  * Returns pointer to the successfully allocated and configured VSI sw struct
14224  * on success, otherwise returns NULL on failure.
14225  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14226 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14227 {
14228 	struct i40e_vsi *main_vsi;
14229 	u16 alloc_queue_pairs;
14230 	struct i40e_pf *pf;
14231 	int ret;
14232 
14233 	if (!vsi)
14234 		return NULL;
14235 
14236 	pf = vsi->back;
14237 
14238 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14239 	i40e_vsi_clear_rings(vsi);
14240 
14241 	i40e_vsi_free_arrays(vsi, false);
14242 	i40e_set_num_rings_in_vsi(vsi);
14243 	ret = i40e_vsi_alloc_arrays(vsi, false);
14244 	if (ret)
14245 		goto err_vsi;
14246 
14247 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14248 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14249 
14250 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14251 	if (ret < 0) {
14252 		dev_info(&pf->pdev->dev,
14253 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14254 			 alloc_queue_pairs, vsi->seid, ret);
14255 		goto err_vsi;
14256 	}
14257 	vsi->base_queue = ret;
14258 
14259 	/* Update the FW view of the VSI. Force a reset of TC and queue
14260 	 * layout configurations.
14261 	 */
14262 	main_vsi = i40e_pf_get_main_vsi(pf);
14263 	main_vsi->seid = pf->main_vsi_seid;
14264 	i40e_vsi_reconfig_tc(main_vsi);
14265 
14266 	if (vsi->type == I40E_VSI_MAIN)
14267 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14268 
14269 	/* assign it some queues */
14270 	ret = i40e_alloc_rings(vsi);
14271 	if (ret)
14272 		goto err_rings;
14273 
14274 	/* map all of the rings to the q_vectors */
14275 	i40e_vsi_map_rings_to_vectors(vsi);
14276 	return vsi;
14277 
14278 err_rings:
14279 	i40e_vsi_free_q_vectors(vsi);
14280 	if (vsi->netdev_registered) {
14281 		vsi->netdev_registered = false;
14282 		unregister_netdev(vsi->netdev);
14283 		free_netdev(vsi->netdev);
14284 		vsi->netdev = NULL;
14285 	}
14286 	if (vsi->type == I40E_VSI_MAIN)
14287 		i40e_devlink_destroy_port(pf);
14288 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14289 err_vsi:
14290 	i40e_vsi_clear(vsi);
14291 	return NULL;
14292 }
14293 
14294 /**
14295  * i40e_vsi_setup - Set up a VSI by a given type
14296  * @pf: board private structure
14297  * @type: VSI type
14298  * @uplink_seid: the switch element to link to
14299  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14300  *
14301  * This allocates the sw VSI structure and its queue resources, then add a VSI
14302  * to the identified VEB.
14303  *
14304  * Returns pointer to the successfully allocated and configure VSI sw struct on
14305  * success, otherwise returns NULL on failure.
14306  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14307 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14308 				u16 uplink_seid, u32 param1)
14309 {
14310 	struct i40e_vsi *vsi = NULL;
14311 	struct i40e_veb *veb = NULL;
14312 	u16 alloc_queue_pairs;
14313 	int v_idx;
14314 	int ret;
14315 
14316 	/* The requested uplink_seid must be either
14317 	 *     - the PF's port seid
14318 	 *              no VEB is needed because this is the PF
14319 	 *              or this is a Flow Director special case VSI
14320 	 *     - seid of an existing VEB
14321 	 *     - seid of a VSI that owns an existing VEB
14322 	 *     - seid of a VSI that doesn't own a VEB
14323 	 *              a new VEB is created and the VSI becomes the owner
14324 	 *     - seid of the PF VSI, which is what creates the first VEB
14325 	 *              this is a special case of the previous
14326 	 *
14327 	 * Find which uplink_seid we were given and create a new VEB if needed
14328 	 */
14329 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14330 	if (!veb && uplink_seid != pf->mac_seid) {
14331 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14332 		if (!vsi) {
14333 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14334 				 uplink_seid);
14335 			return NULL;
14336 		}
14337 
14338 		if (vsi->uplink_seid == pf->mac_seid)
14339 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14340 					     vsi->tc_config.enabled_tc);
14341 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14342 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14343 					     vsi->tc_config.enabled_tc);
14344 		if (veb) {
14345 			if (vsi->type != I40E_VSI_MAIN) {
14346 				dev_info(&vsi->back->pdev->dev,
14347 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14348 				return NULL;
14349 			}
14350 			/* We come up by default in VEPA mode if SRIOV is not
14351 			 * already enabled, in which case we can't force VEPA
14352 			 * mode.
14353 			 */
14354 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14355 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14356 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14357 			}
14358 			i40e_config_bridge_mode(veb);
14359 		}
14360 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14361 		if (!veb) {
14362 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14363 			return NULL;
14364 		}
14365 
14366 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14367 		uplink_seid = veb->seid;
14368 	}
14369 
14370 	/* get vsi sw struct */
14371 	v_idx = i40e_vsi_mem_alloc(pf, type);
14372 	if (v_idx < 0)
14373 		goto err_alloc;
14374 	vsi = pf->vsi[v_idx];
14375 	if (!vsi)
14376 		goto err_alloc;
14377 	vsi->type = type;
14378 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14379 
14380 	if (type == I40E_VSI_MAIN)
14381 		pf->lan_vsi = v_idx;
14382 	else if (type == I40E_VSI_SRIOV)
14383 		vsi->vf_id = param1;
14384 	/* assign it some queues */
14385 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14386 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14387 
14388 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14389 	if (ret < 0) {
14390 		dev_info(&pf->pdev->dev,
14391 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14392 			 alloc_queue_pairs, vsi->seid, ret);
14393 		goto err_vsi;
14394 	}
14395 	vsi->base_queue = ret;
14396 
14397 	/* get a VSI from the hardware */
14398 	vsi->uplink_seid = uplink_seid;
14399 	ret = i40e_add_vsi(vsi);
14400 	if (ret)
14401 		goto err_vsi;
14402 
14403 	switch (vsi->type) {
14404 	/* setup the netdev if needed */
14405 	case I40E_VSI_MAIN:
14406 	case I40E_VSI_VMDQ2:
14407 		ret = i40e_config_netdev(vsi);
14408 		if (ret)
14409 			goto err_netdev;
14410 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14411 		if (ret)
14412 			goto err_netdev;
14413 		if (vsi->type == I40E_VSI_MAIN) {
14414 			ret = i40e_devlink_create_port(pf);
14415 			if (ret)
14416 				goto err_netdev;
14417 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14418 		}
14419 		ret = register_netdev(vsi->netdev);
14420 		if (ret)
14421 			goto err_dl_port;
14422 		vsi->netdev_registered = true;
14423 		netif_carrier_off(vsi->netdev);
14424 #ifdef CONFIG_I40E_DCB
14425 		/* Setup DCB netlink interface */
14426 		i40e_dcbnl_setup(vsi);
14427 #endif /* CONFIG_I40E_DCB */
14428 		fallthrough;
14429 	case I40E_VSI_FDIR:
14430 		/* set up vectors and rings if needed */
14431 		ret = i40e_vsi_setup_vectors(vsi);
14432 		if (ret)
14433 			goto err_msix;
14434 
14435 		ret = i40e_alloc_rings(vsi);
14436 		if (ret)
14437 			goto err_rings;
14438 
14439 		/* map all of the rings to the q_vectors */
14440 		i40e_vsi_map_rings_to_vectors(vsi);
14441 
14442 		i40e_vsi_reset_stats(vsi);
14443 		break;
14444 	default:
14445 		/* no netdev or rings for the other VSI types */
14446 		break;
14447 	}
14448 
14449 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14450 	    vsi->type == I40E_VSI_VMDQ2) {
14451 		ret = i40e_vsi_config_rss(vsi);
14452 		if (ret)
14453 			goto err_config;
14454 	}
14455 	return vsi;
14456 
14457 err_config:
14458 	i40e_vsi_clear_rings(vsi);
14459 err_rings:
14460 	i40e_vsi_free_q_vectors(vsi);
14461 err_msix:
14462 	if (vsi->netdev_registered) {
14463 		vsi->netdev_registered = false;
14464 		unregister_netdev(vsi->netdev);
14465 		free_netdev(vsi->netdev);
14466 		vsi->netdev = NULL;
14467 	}
14468 err_dl_port:
14469 	if (vsi->type == I40E_VSI_MAIN)
14470 		i40e_devlink_destroy_port(pf);
14471 err_netdev:
14472 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14473 err_vsi:
14474 	i40e_vsi_clear(vsi);
14475 err_alloc:
14476 	return NULL;
14477 }
14478 
14479 /**
14480  * i40e_veb_get_bw_info - Query VEB BW information
14481  * @veb: the veb to query
14482  *
14483  * Query the Tx scheduler BW configuration data for given VEB
14484  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14485 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14486 {
14487 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14488 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14489 	struct i40e_pf *pf = veb->pf;
14490 	struct i40e_hw *hw = &pf->hw;
14491 	u32 tc_bw_max;
14492 	int ret = 0;
14493 	int i;
14494 
14495 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14496 						  &bw_data, NULL);
14497 	if (ret) {
14498 		dev_info(&pf->pdev->dev,
14499 			 "query veb bw config failed, err %pe aq_err %s\n",
14500 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14501 		goto out;
14502 	}
14503 
14504 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14505 						   &ets_data, NULL);
14506 	if (ret) {
14507 		dev_info(&pf->pdev->dev,
14508 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14509 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14510 		goto out;
14511 	}
14512 
14513 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14514 	veb->bw_max_quanta = ets_data.tc_bw_max;
14515 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14516 	veb->enabled_tc = ets_data.tc_valid_bits;
14517 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14518 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14519 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14520 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14521 		veb->bw_tc_limit_credits[i] =
14522 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14523 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14524 	}
14525 
14526 out:
14527 	return ret;
14528 }
14529 
14530 /**
14531  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14532  * @pf: board private structure
14533  *
14534  * On error: returns error code (negative)
14535  * On success: returns vsi index in PF (positive)
14536  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14537 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14538 {
14539 	int ret = -ENOENT;
14540 	struct i40e_veb *veb;
14541 	int i;
14542 
14543 	/* Need to protect the allocation of switch elements at the PF level */
14544 	mutex_lock(&pf->switch_mutex);
14545 
14546 	/* VEB list may be fragmented if VEB creation/destruction has
14547 	 * been happening.  We can afford to do a quick scan to look
14548 	 * for any free slots in the list.
14549 	 *
14550 	 * find next empty veb slot, looping back around if necessary
14551 	 */
14552 	i = 0;
14553 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14554 		i++;
14555 	if (i >= I40E_MAX_VEB) {
14556 		ret = -ENOMEM;
14557 		goto err_alloc_veb;  /* out of VEB slots! */
14558 	}
14559 
14560 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14561 	if (!veb) {
14562 		ret = -ENOMEM;
14563 		goto err_alloc_veb;
14564 	}
14565 	veb->pf = pf;
14566 	veb->idx = i;
14567 	veb->enabled_tc = 1;
14568 
14569 	pf->veb[i] = veb;
14570 	ret = i;
14571 err_alloc_veb:
14572 	mutex_unlock(&pf->switch_mutex);
14573 	return ret;
14574 }
14575 
14576 /**
14577  * i40e_switch_branch_release - Delete a branch of the switch tree
14578  * @branch: where to start deleting
14579  *
14580  * This uses recursion to find the tips of the branch to be
14581  * removed, deleting until we get back to and can delete this VEB.
14582  **/
i40e_switch_branch_release(struct i40e_veb * branch)14583 static void i40e_switch_branch_release(struct i40e_veb *branch)
14584 {
14585 	struct i40e_pf *pf = branch->pf;
14586 	u16 branch_seid = branch->seid;
14587 	u16 veb_idx = branch->idx;
14588 	struct i40e_vsi *vsi;
14589 	struct i40e_veb *veb;
14590 	int i;
14591 
14592 	/* release any VEBs on this VEB - RECURSION */
14593 	i40e_pf_for_each_veb(pf, i, veb)
14594 		if (veb->uplink_seid == branch->seid)
14595 			i40e_switch_branch_release(veb);
14596 
14597 	/* Release the VSIs on this VEB, but not the owner VSI.
14598 	 *
14599 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14600 	 *       the VEB itself, so don't use (*branch) after this loop.
14601 	 */
14602 	i40e_pf_for_each_vsi(pf, i, vsi)
14603 		if (vsi->uplink_seid == branch_seid &&
14604 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14605 			i40e_vsi_release(vsi);
14606 
14607 	/* There's one corner case where the VEB might not have been
14608 	 * removed, so double check it here and remove it if needed.
14609 	 * This case happens if the veb was created from the debugfs
14610 	 * commands and no VSIs were added to it.
14611 	 */
14612 	if (pf->veb[veb_idx])
14613 		i40e_veb_release(pf->veb[veb_idx]);
14614 }
14615 
14616 /**
14617  * i40e_veb_clear - remove veb struct
14618  * @veb: the veb to remove
14619  **/
i40e_veb_clear(struct i40e_veb * veb)14620 static void i40e_veb_clear(struct i40e_veb *veb)
14621 {
14622 	if (!veb)
14623 		return;
14624 
14625 	if (veb->pf) {
14626 		struct i40e_pf *pf = veb->pf;
14627 
14628 		mutex_lock(&pf->switch_mutex);
14629 		if (pf->veb[veb->idx] == veb)
14630 			pf->veb[veb->idx] = NULL;
14631 		mutex_unlock(&pf->switch_mutex);
14632 	}
14633 
14634 	kfree(veb);
14635 }
14636 
14637 /**
14638  * i40e_veb_release - Delete a VEB and free its resources
14639  * @veb: the VEB being removed
14640  **/
i40e_veb_release(struct i40e_veb * veb)14641 void i40e_veb_release(struct i40e_veb *veb)
14642 {
14643 	struct i40e_vsi *vsi, *vsi_it;
14644 	struct i40e_pf *pf;
14645 	int i, n = 0;
14646 
14647 	pf = veb->pf;
14648 
14649 	/* find the remaining VSI and check for extras */
14650 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14651 		if (vsi_it->uplink_seid == veb->seid) {
14652 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14653 				vsi = vsi_it;
14654 			n++;
14655 		}
14656 
14657 	/* Floating VEB has to be empty and regular one must have
14658 	 * single owner VSI.
14659 	 */
14660 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14661 		dev_info(&pf->pdev->dev,
14662 			 "can't remove VEB %d with %d VSIs left\n",
14663 			 veb->seid, n);
14664 		return;
14665 	}
14666 
14667 	/* For regular VEB move the owner VSI to uplink port */
14668 	if (veb->uplink_seid) {
14669 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14670 		vsi->uplink_seid = veb->uplink_seid;
14671 		vsi->veb_idx = I40E_NO_VEB;
14672 	}
14673 
14674 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14675 	i40e_veb_clear(veb);
14676 }
14677 
14678 /**
14679  * i40e_add_veb - create the VEB in the switch
14680  * @veb: the VEB to be instantiated
14681  * @vsi: the controlling VSI
14682  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14683 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14684 {
14685 	struct i40e_pf *pf = veb->pf;
14686 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14687 	int ret;
14688 
14689 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14690 			      veb->enabled_tc, vsi ? false : true,
14691 			      &veb->seid, enable_stats, NULL);
14692 
14693 	/* get a VEB from the hardware */
14694 	if (ret) {
14695 		dev_info(&pf->pdev->dev,
14696 			 "couldn't add VEB, err %pe aq_err %s\n",
14697 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14698 		return -EPERM;
14699 	}
14700 
14701 	/* get statistics counter */
14702 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14703 					 &veb->stats_idx, NULL, NULL, NULL);
14704 	if (ret) {
14705 		dev_info(&pf->pdev->dev,
14706 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14707 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14708 		return -EPERM;
14709 	}
14710 	ret = i40e_veb_get_bw_info(veb);
14711 	if (ret) {
14712 		dev_info(&pf->pdev->dev,
14713 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14714 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14715 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14716 		return -ENOENT;
14717 	}
14718 
14719 	if (vsi) {
14720 		vsi->uplink_seid = veb->seid;
14721 		vsi->veb_idx = veb->idx;
14722 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14723 	}
14724 
14725 	return 0;
14726 }
14727 
14728 /**
14729  * i40e_veb_setup - Set up a VEB
14730  * @pf: board private structure
14731  * @uplink_seid: the switch element to link to
14732  * @vsi_seid: the initial VSI seid
14733  * @enabled_tc: Enabled TC bit-map
14734  *
14735  * This allocates the sw VEB structure and links it into the switch
14736  * It is possible and legal for this to be a duplicate of an already
14737  * existing VEB.  It is also possible for both uplink and vsi seids
14738  * to be zero, in order to create a floating VEB.
14739  *
14740  * Returns pointer to the successfully allocated VEB sw struct on
14741  * success, otherwise returns NULL on failure.
14742  **/
i40e_veb_setup(struct i40e_pf * pf,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14743 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14744 				u16 vsi_seid, u8 enabled_tc)
14745 {
14746 	struct i40e_vsi *vsi = NULL;
14747 	struct i40e_veb *veb;
14748 	int veb_idx;
14749 	int ret;
14750 
14751 	/* if one seid is 0, the other must be 0 to create a floating relay */
14752 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14753 	    (uplink_seid + vsi_seid != 0)) {
14754 		dev_info(&pf->pdev->dev,
14755 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14756 			 uplink_seid, vsi_seid);
14757 		return NULL;
14758 	}
14759 
14760 	/* make sure there is such a vsi and uplink */
14761 	if (vsi_seid) {
14762 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14763 		if (!vsi) {
14764 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14765 				vsi_seid);
14766 			return NULL;
14767 		}
14768 	}
14769 
14770 	/* get veb sw struct */
14771 	veb_idx = i40e_veb_mem_alloc(pf);
14772 	if (veb_idx < 0)
14773 		goto err_alloc;
14774 	veb = pf->veb[veb_idx];
14775 	veb->uplink_seid = uplink_seid;
14776 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14777 
14778 	/* create the VEB in the switch */
14779 	ret = i40e_add_veb(veb, vsi);
14780 	if (ret)
14781 		goto err_veb;
14782 
14783 	if (vsi && vsi->idx == pf->lan_vsi)
14784 		pf->lan_veb = veb->idx;
14785 
14786 	return veb;
14787 
14788 err_veb:
14789 	i40e_veb_clear(veb);
14790 err_alloc:
14791 	return NULL;
14792 }
14793 
14794 /**
14795  * i40e_setup_pf_switch_element - set PF vars based on switch type
14796  * @pf: board private structure
14797  * @ele: element we are building info from
14798  * @num_reported: total number of elements
14799  * @printconfig: should we print the contents
14800  *
14801  * helper function to assist in extracting a few useful SEID values.
14802  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14803 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14804 				struct i40e_aqc_switch_config_element_resp *ele,
14805 				u16 num_reported, bool printconfig)
14806 {
14807 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14808 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14809 	u8 element_type = ele->element_type;
14810 	u16 seid = le16_to_cpu(ele->seid);
14811 	struct i40e_veb *veb;
14812 
14813 	if (printconfig)
14814 		dev_info(&pf->pdev->dev,
14815 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14816 			 element_type, seid, uplink_seid, downlink_seid);
14817 
14818 	switch (element_type) {
14819 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14820 		pf->mac_seid = seid;
14821 		break;
14822 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14823 		/* Main VEB? */
14824 		if (uplink_seid != pf->mac_seid)
14825 			break;
14826 		veb = i40e_pf_get_main_veb(pf);
14827 		if (!veb) {
14828 			int v;
14829 
14830 			/* find existing or else empty VEB */
14831 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14832 			if (veb) {
14833 				pf->lan_veb = veb->idx;
14834 			} else {
14835 				v = i40e_veb_mem_alloc(pf);
14836 				if (v < 0)
14837 					break;
14838 				pf->lan_veb = v;
14839 			}
14840 		}
14841 
14842 		/* Try to get again main VEB as pf->lan_veb may have changed */
14843 		veb = i40e_pf_get_main_veb(pf);
14844 		if (!veb)
14845 			break;
14846 
14847 		veb->seid = seid;
14848 		veb->uplink_seid = pf->mac_seid;
14849 		veb->pf = pf;
14850 		break;
14851 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14852 		if (num_reported != 1)
14853 			break;
14854 		/* This is immediately after a reset so we can assume this is
14855 		 * the PF's VSI
14856 		 */
14857 		pf->mac_seid = uplink_seid;
14858 		pf->main_vsi_seid = seid;
14859 		if (printconfig)
14860 			dev_info(&pf->pdev->dev,
14861 				 "pf_seid=%d main_vsi_seid=%d\n",
14862 				 downlink_seid, pf->main_vsi_seid);
14863 		break;
14864 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14865 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14866 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14867 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14868 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14869 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14870 		/* ignore these for now */
14871 		break;
14872 	default:
14873 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14874 			 element_type, seid);
14875 		break;
14876 	}
14877 }
14878 
14879 /**
14880  * i40e_fetch_switch_configuration - Get switch config from firmware
14881  * @pf: board private structure
14882  * @printconfig: should we print the contents
14883  *
14884  * Get the current switch configuration from the device and
14885  * extract a few useful SEID values.
14886  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14887 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14888 {
14889 	struct i40e_aqc_get_switch_config_resp *sw_config;
14890 	u16 next_seid = 0;
14891 	int ret = 0;
14892 	u8 *aq_buf;
14893 	int i;
14894 
14895 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14896 	if (!aq_buf)
14897 		return -ENOMEM;
14898 
14899 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14900 	do {
14901 		u16 num_reported, num_total;
14902 
14903 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14904 						I40E_AQ_LARGE_BUF,
14905 						&next_seid, NULL);
14906 		if (ret) {
14907 			dev_info(&pf->pdev->dev,
14908 				 "get switch config failed err %d aq_err %s\n",
14909 				 ret, libie_aq_str(pf->hw.aq.asq_last_status));
14910 			kfree(aq_buf);
14911 			return -ENOENT;
14912 		}
14913 
14914 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14915 		num_total = le16_to_cpu(sw_config->header.num_total);
14916 
14917 		if (printconfig)
14918 			dev_info(&pf->pdev->dev,
14919 				 "header: %d reported %d total\n",
14920 				 num_reported, num_total);
14921 
14922 		for (i = 0; i < num_reported; i++) {
14923 			struct i40e_aqc_switch_config_element_resp *ele =
14924 				&sw_config->element[i];
14925 
14926 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14927 						     printconfig);
14928 		}
14929 	} while (next_seid != 0);
14930 
14931 	kfree(aq_buf);
14932 	return ret;
14933 }
14934 
14935 /**
14936  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14937  * @pf: board private structure
14938  * @reinit: if the Main VSI needs to re-initialized.
14939  * @lock_acquired: indicates whether or not the lock has been acquired
14940  *
14941  * Returns 0 on success, negative value on failure
14942  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)14943 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14944 {
14945 	struct i40e_vsi *main_vsi;
14946 	u16 flags = 0;
14947 	int ret;
14948 
14949 	/* find out what's out there already */
14950 	ret = i40e_fetch_switch_configuration(pf, false);
14951 	if (ret) {
14952 		dev_info(&pf->pdev->dev,
14953 			 "couldn't fetch switch config, err %pe aq_err %s\n",
14954 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14955 		return ret;
14956 	}
14957 	i40e_pf_reset_stats(pf);
14958 
14959 	/* set the switch config bit for the whole device to
14960 	 * support limited promisc or true promisc
14961 	 * when user requests promisc. The default is limited
14962 	 * promisc.
14963 	*/
14964 
14965 	if ((pf->hw.pf_id == 0) &&
14966 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
14967 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14968 		pf->last_sw_conf_flags = flags;
14969 	}
14970 
14971 	if (pf->hw.pf_id == 0) {
14972 		u16 valid_flags;
14973 
14974 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14975 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14976 						NULL);
14977 		if (ret && pf->hw.aq.asq_last_status != LIBIE_AQ_RC_ESRCH) {
14978 			dev_info(&pf->pdev->dev,
14979 				 "couldn't set switch config bits, err %pe aq_err %s\n",
14980 				 ERR_PTR(ret),
14981 				 libie_aq_str(pf->hw.aq.asq_last_status));
14982 			/* not a fatal problem, just keep going */
14983 		}
14984 		pf->last_sw_conf_valid_flags = valid_flags;
14985 	}
14986 
14987 	/* first time setup */
14988 	main_vsi = i40e_pf_get_main_vsi(pf);
14989 	if (!main_vsi || reinit) {
14990 		struct i40e_veb *veb;
14991 		u16 uplink_seid;
14992 
14993 		/* Set up the PF VSI associated with the PF's main VSI
14994 		 * that is already in the HW switch
14995 		 */
14996 		veb = i40e_pf_get_main_veb(pf);
14997 		if (veb)
14998 			uplink_seid = veb->seid;
14999 		else
15000 			uplink_seid = pf->mac_seid;
15001 		if (!main_vsi)
15002 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15003 						  uplink_seid, 0);
15004 		else if (reinit)
15005 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15006 		if (!main_vsi) {
15007 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15008 			i40e_cloud_filter_exit(pf);
15009 			i40e_fdir_teardown(pf);
15010 			return -EAGAIN;
15011 		}
15012 	} else {
15013 		/* force a reset of TC and queue layout configurations */
15014 		main_vsi->seid = pf->main_vsi_seid;
15015 		i40e_vsi_reconfig_tc(main_vsi);
15016 	}
15017 	i40e_vlan_stripping_disable(main_vsi);
15018 
15019 	i40e_fdir_sb_setup(pf);
15020 
15021 	/* Setup static PF queue filter control settings */
15022 	ret = i40e_setup_pf_filter_control(pf);
15023 	if (ret) {
15024 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15025 			 ret);
15026 		/* Failure here should not stop continuing other steps */
15027 	}
15028 
15029 	/* enable RSS in the HW, even for only one queue, as the stack can use
15030 	 * the hash
15031 	 */
15032 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15033 		i40e_pf_config_rss(pf);
15034 
15035 	/* fill in link information and enable LSE reporting */
15036 	i40e_link_event(pf);
15037 
15038 	i40e_ptp_init(pf);
15039 
15040 	if (!lock_acquired)
15041 		rtnl_lock();
15042 
15043 	/* repopulate tunnel port filters */
15044 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15045 
15046 	if (!lock_acquired)
15047 		rtnl_unlock();
15048 
15049 	return ret;
15050 }
15051 
15052 /**
15053  * i40e_determine_queue_usage - Work out queue distribution
15054  * @pf: board private structure
15055  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15056 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15057 {
15058 	int queues_left;
15059 	int q_max;
15060 
15061 	pf->num_lan_qps = 0;
15062 
15063 	/* Find the max queues to be put into basic use.  We'll always be
15064 	 * using TC0, whether or not DCB is running, and TC0 will get the
15065 	 * big RSS set.
15066 	 */
15067 	queues_left = pf->hw.func_caps.num_tx_qp;
15068 
15069 	if ((queues_left == 1) ||
15070 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15071 		/* one qp for PF, no queues for anything else */
15072 		queues_left = 0;
15073 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15074 
15075 		/* make sure all the fancies are disabled */
15076 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15077 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15078 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15079 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15080 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15081 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15082 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15083 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15084 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15085 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15086 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15087 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15088 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15089 		/* one qp for PF */
15090 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15091 		queues_left -= pf->num_lan_qps;
15092 
15093 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15094 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15095 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15096 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15097 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15098 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15099 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15100 	} else {
15101 		/* Not enough queues for all TCs */
15102 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15103 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15104 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15105 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15106 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15107 		}
15108 
15109 		/* limit lan qps to the smaller of qps, cpus or msix */
15110 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15111 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15112 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15113 		pf->num_lan_qps = q_max;
15114 
15115 		queues_left -= pf->num_lan_qps;
15116 	}
15117 
15118 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15119 		if (queues_left > 1) {
15120 			queues_left -= 1; /* save 1 queue for FD */
15121 		} else {
15122 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15123 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15124 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15125 		}
15126 	}
15127 
15128 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15129 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15130 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15131 					(queues_left / pf->num_vf_qps));
15132 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15133 	}
15134 
15135 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15136 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15137 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15138 					  (queues_left / pf->num_vmdq_qps));
15139 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15140 	}
15141 
15142 	pf->queues_left = queues_left;
15143 	dev_dbg(&pf->pdev->dev,
15144 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15145 		pf->hw.func_caps.num_tx_qp,
15146 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15147 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15148 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15149 		queues_left);
15150 }
15151 
15152 /**
15153  * i40e_setup_pf_filter_control - Setup PF static filter control
15154  * @pf: PF to be setup
15155  *
15156  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15157  * settings. If PE/FCoE are enabled then it will also set the per PF
15158  * based filter sizes required for them. It also enables Flow director,
15159  * ethertype and macvlan type filter settings for the pf.
15160  *
15161  * Returns 0 on success, negative on failure
15162  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15163 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15164 {
15165 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15166 
15167 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15168 
15169 	/* Flow Director is enabled */
15170 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15171 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15172 		settings->enable_fdir = true;
15173 
15174 	/* Ethtype and MACVLAN filters enabled for PF */
15175 	settings->enable_ethtype = true;
15176 	settings->enable_macvlan = true;
15177 
15178 	if (i40e_set_filter_control(&pf->hw, settings))
15179 		return -ENOENT;
15180 
15181 	return 0;
15182 }
15183 
15184 #define INFO_STRING_LEN 255
15185 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15186 static void i40e_print_features(struct i40e_pf *pf)
15187 {
15188 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15189 	struct i40e_hw *hw = &pf->hw;
15190 	char *buf;
15191 	int i;
15192 
15193 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15194 	if (!buf)
15195 		return;
15196 
15197 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15198 #ifdef CONFIG_PCI_IOV
15199 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15200 #endif
15201 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15202 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15203 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15204 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15205 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15206 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15207 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15208 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15209 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15210 	}
15211 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15212 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15213 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15214 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15215 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15216 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15217 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15218 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15219 	else
15220 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15221 
15222 	dev_info(&pf->pdev->dev, "%s\n", buf);
15223 	kfree(buf);
15224 	WARN_ON(i > INFO_STRING_LEN);
15225 }
15226 
15227 /**
15228  * i40e_get_platform_mac_addr - get platform-specific MAC address
15229  * @pdev: PCI device information struct
15230  * @pf: board private structure
15231  *
15232  * Look up the MAC address for the device. First we'll try
15233  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15234  * specific fallback. Otherwise, we'll default to the stored value in
15235  * firmware.
15236  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15237 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15238 {
15239 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15240 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15241 }
15242 
15243 /**
15244  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15245  * @fec_cfg: FEC option to set in flags
15246  * @flags: ptr to flags in which we set FEC option
15247  **/
i40e_set_fec_in_flags(u8 fec_cfg,unsigned long * flags)15248 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15249 {
15250 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15251 		set_bit(I40E_FLAG_RS_FEC, flags);
15252 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15253 	}
15254 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15255 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15256 		set_bit(I40E_FLAG_RS_FEC, flags);
15257 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15258 	}
15259 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15260 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15261 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15262 		clear_bit(I40E_FLAG_RS_FEC, flags);
15263 	}
15264 	if (fec_cfg == 0) {
15265 		clear_bit(I40E_FLAG_RS_FEC, flags);
15266 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15267 	}
15268 }
15269 
15270 /**
15271  * i40e_check_recovery_mode - check if we are running transition firmware
15272  * @pf: board private structure
15273  *
15274  * Check registers indicating the firmware runs in recovery mode. Sets the
15275  * appropriate driver state.
15276  *
15277  * Returns true if the recovery mode was detected, false otherwise
15278  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15279 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15280 {
15281 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15282 
15283 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15284 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15285 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15286 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15287 
15288 		return true;
15289 	}
15290 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15291 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15292 
15293 	return false;
15294 }
15295 
15296 /**
15297  * i40e_pf_loop_reset - perform reset in a loop.
15298  * @pf: board private structure
15299  *
15300  * This function is useful when a NIC is about to enter recovery mode.
15301  * When a NIC's internal data structures are corrupted the NIC's
15302  * firmware is going to enter recovery mode.
15303  * Right after a POR it takes about 7 minutes for firmware to enter
15304  * recovery mode. Until that time a NIC is in some kind of intermediate
15305  * state. After that time period the NIC almost surely enters
15306  * recovery mode. The only way for a driver to detect intermediate
15307  * state is to issue a series of pf-resets and check a return value.
15308  * If a PF reset returns success then the firmware could be in recovery
15309  * mode so the caller of this code needs to check for recovery mode
15310  * if this function returns success. There is a little chance that
15311  * firmware will hang in intermediate state forever.
15312  * Since waiting 7 minutes is quite a lot of time this function waits
15313  * 10 seconds and then gives up by returning an error.
15314  *
15315  * Return 0 on success, negative on failure.
15316  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15317 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15318 {
15319 	/* wait max 10 seconds for PF reset to succeed */
15320 	const unsigned long time_end = jiffies + 10 * HZ;
15321 	struct i40e_hw *hw = &pf->hw;
15322 	int ret;
15323 
15324 	ret = i40e_pf_reset(hw);
15325 	while (ret != 0 && time_before(jiffies, time_end)) {
15326 		usleep_range(10000, 20000);
15327 		ret = i40e_pf_reset(hw);
15328 	}
15329 
15330 	if (ret == 0)
15331 		pf->pfr_count++;
15332 	else
15333 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15334 
15335 	return ret;
15336 }
15337 
15338 /**
15339  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15340  * @pf: board private structure
15341  *
15342  * Check FW registers to determine if FW issued unexpected EMP Reset.
15343  * Every time when unexpected EMP Reset occurs the FW increments
15344  * a counter of unexpected EMP Resets. When the counter reaches 10
15345  * the FW should enter the Recovery mode
15346  *
15347  * Returns true if FW issued unexpected EMP Reset
15348  **/
i40e_check_fw_empr(struct i40e_pf * pf)15349 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15350 {
15351 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15352 			   I40E_GL_FWSTS_FWS1B_MASK;
15353 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15354 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15355 }
15356 
15357 /**
15358  * i40e_handle_resets - handle EMP resets and PF resets
15359  * @pf: board private structure
15360  *
15361  * Handle both EMP resets and PF resets and conclude whether there are
15362  * any issues regarding these resets. If there are any issues then
15363  * generate log entry.
15364  *
15365  * Return 0 if NIC is healthy or negative value when there are issues
15366  * with resets
15367  **/
i40e_handle_resets(struct i40e_pf * pf)15368 static int i40e_handle_resets(struct i40e_pf *pf)
15369 {
15370 	const int pfr = i40e_pf_loop_reset(pf);
15371 	const bool is_empr = i40e_check_fw_empr(pf);
15372 
15373 	if (is_empr || pfr != 0)
15374 		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");
15375 
15376 	return is_empr ? -EIO : pfr;
15377 }
15378 
15379 /**
15380  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15381  * @pf: board private structure
15382  * @hw: ptr to the hardware info
15383  *
15384  * This function does a minimal setup of all subsystems needed for running
15385  * recovery mode.
15386  *
15387  * Returns 0 on success, negative on failure
15388  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15389 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15390 {
15391 	struct i40e_vsi *vsi;
15392 	int err;
15393 	int v_idx;
15394 
15395 	pci_set_drvdata(pf->pdev, pf);
15396 	pci_save_state(pf->pdev);
15397 
15398 	/* set up periodic task facility */
15399 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15400 	pf->service_timer_period = HZ;
15401 
15402 	INIT_WORK(&pf->service_task, i40e_service_task);
15403 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15404 
15405 	err = i40e_init_interrupt_scheme(pf);
15406 	if (err)
15407 		goto err_switch_setup;
15408 
15409 	/* The number of VSIs reported by the FW is the minimum guaranteed
15410 	 * to us; HW supports far more and we share the remaining pool with
15411 	 * the other PFs. We allocate space for more than the guarantee with
15412 	 * the understanding that we might not get them all later.
15413 	 */
15414 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15415 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15416 	else
15417 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15418 
15419 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15420 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15421 			  GFP_KERNEL);
15422 	if (!pf->vsi) {
15423 		err = -ENOMEM;
15424 		goto err_switch_setup;
15425 	}
15426 
15427 	/* We allocate one VSI which is needed as absolute minimum
15428 	 * in order to register the netdev
15429 	 */
15430 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15431 	if (v_idx < 0) {
15432 		err = v_idx;
15433 		goto err_switch_setup;
15434 	}
15435 	pf->lan_vsi = v_idx;
15436 	vsi = pf->vsi[v_idx];
15437 	if (!vsi) {
15438 		err = -EFAULT;
15439 		goto err_switch_setup;
15440 	}
15441 	vsi->alloc_queue_pairs = 1;
15442 	err = i40e_config_netdev(vsi);
15443 	if (err)
15444 		goto err_switch_setup;
15445 	err = register_netdev(vsi->netdev);
15446 	if (err)
15447 		goto err_switch_setup;
15448 	vsi->netdev_registered = true;
15449 	i40e_dbg_pf_init(pf);
15450 
15451 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15452 	if (err)
15453 		goto err_switch_setup;
15454 
15455 	/* tell the firmware that we're starting */
15456 	i40e_send_version(pf);
15457 
15458 	/* since everything's happy, start the service_task timer */
15459 	mod_timer(&pf->service_timer,
15460 		  round_jiffies(jiffies + pf->service_timer_period));
15461 
15462 	return 0;
15463 
15464 err_switch_setup:
15465 	i40e_reset_interrupt_capability(pf);
15466 	timer_shutdown_sync(&pf->service_timer);
15467 	i40e_shutdown_adminq(hw);
15468 	iounmap(hw->hw_addr);
15469 	pci_release_mem_regions(pf->pdev);
15470 	pci_disable_device(pf->pdev);
15471 	i40e_free_pf(pf);
15472 
15473 	return err;
15474 }
15475 
15476 /**
15477  * i40e_set_subsystem_device_id - set subsystem device id
15478  * @hw: pointer to the hardware info
15479  *
15480  * Set PCI subsystem device id either from a pci_dev structure or
15481  * a specific FW register.
15482  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15483 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15484 {
15485 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15486 
15487 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15488 		pf->pdev->subsystem_device :
15489 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15490 }
15491 
15492 /**
15493  * i40e_probe - Device initialization routine
15494  * @pdev: PCI device information struct
15495  * @ent: entry in i40e_pci_tbl
15496  *
15497  * i40e_probe initializes a PF identified by a pci_dev structure.
15498  * The OS initialization, configuring of the PF private structure,
15499  * and a hardware reset occur.
15500  *
15501  * Returns 0 on success, negative on failure
15502  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15503 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15504 {
15505 	struct i40e_aq_get_phy_abilities_resp abilities;
15506 #ifdef CONFIG_I40E_DCB
15507 	enum i40e_get_fw_lldp_status_resp lldp_status;
15508 #endif /* CONFIG_I40E_DCB */
15509 	struct i40e_vsi *vsi;
15510 	struct i40e_pf *pf;
15511 	struct i40e_hw *hw;
15512 	u16 wol_nvm_bits;
15513 	char nvm_ver[32];
15514 	u16 link_status;
15515 #ifdef CONFIG_I40E_DCB
15516 	int status;
15517 #endif /* CONFIG_I40E_DCB */
15518 	int err;
15519 	u32 val;
15520 
15521 	err = pci_enable_device_mem(pdev);
15522 	if (err)
15523 		return err;
15524 
15525 	/* set up for high or low dma */
15526 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15527 	if (err) {
15528 		dev_err(&pdev->dev,
15529 			"DMA configuration failed: 0x%x\n", err);
15530 		goto err_dma;
15531 	}
15532 
15533 	/* set up pci connections */
15534 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15535 	if (err) {
15536 		dev_info(&pdev->dev,
15537 			 "pci_request_selected_regions failed %d\n", err);
15538 		goto err_pci_reg;
15539 	}
15540 
15541 	pci_set_master(pdev);
15542 
15543 	/* Now that we have a PCI connection, we need to do the
15544 	 * low level device setup.  This is primarily setting up
15545 	 * the Admin Queue structures and then querying for the
15546 	 * device's current profile information.
15547 	 */
15548 	pf = i40e_alloc_pf(&pdev->dev);
15549 	if (!pf) {
15550 		err = -ENOMEM;
15551 		goto err_pf_alloc;
15552 	}
15553 	pf->next_vsi = 0;
15554 	pf->pdev = pdev;
15555 	set_bit(__I40E_DOWN, pf->state);
15556 
15557 	hw = &pf->hw;
15558 
15559 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15560 				I40E_MAX_CSR_SPACE);
15561 	/* We believe that the highest register to read is
15562 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15563 	 * is not less than that before mapping to prevent a
15564 	 * kernel panic.
15565 	 */
15566 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15567 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15568 			pf->ioremap_len);
15569 		err = -ENOMEM;
15570 		goto err_ioremap;
15571 	}
15572 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15573 	if (!hw->hw_addr) {
15574 		err = -EIO;
15575 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15576 			 (unsigned int)pci_resource_start(pdev, 0),
15577 			 pf->ioremap_len, err);
15578 		goto err_ioremap;
15579 	}
15580 	hw->vendor_id = pdev->vendor;
15581 	hw->device_id = pdev->device;
15582 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15583 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15584 	i40e_set_subsystem_device_id(hw);
15585 	hw->bus.device = PCI_SLOT(pdev->devfn);
15586 	hw->bus.func = PCI_FUNC(pdev->devfn);
15587 	hw->bus.bus_id = pdev->bus->number;
15588 
15589 	/* Select something other than the 802.1ad ethertype for the
15590 	 * switch to use internally and drop on ingress.
15591 	 */
15592 	hw->switch_tag = 0xffff;
15593 	hw->first_tag = ETH_P_8021AD;
15594 	hw->second_tag = ETH_P_8021Q;
15595 
15596 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15597 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15598 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15599 
15600 	/* set up the locks for the AQ, do this only once in probe
15601 	 * and destroy them only once in remove
15602 	 */
15603 	mutex_init(&hw->aq.asq_mutex);
15604 	mutex_init(&hw->aq.arq_mutex);
15605 
15606 	pf->msg_enable = netif_msg_init(debug,
15607 					NETIF_MSG_DRV |
15608 					NETIF_MSG_PROBE |
15609 					NETIF_MSG_LINK);
15610 	if (debug < -1)
15611 		pf->hw.debug_mask = debug;
15612 
15613 	/* do a special CORER for clearing PXE mode once at init */
15614 	if (hw->revision_id == 0 &&
15615 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15616 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15617 		i40e_flush(hw);
15618 		msleep(200);
15619 		pf->corer_count++;
15620 
15621 		i40e_clear_pxe_mode(hw);
15622 	}
15623 
15624 	/* Reset here to make sure all is clean and to define PF 'n' */
15625 	i40e_clear_hw(hw);
15626 
15627 	err = i40e_set_mac_type(hw);
15628 	if (err) {
15629 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15630 			 err);
15631 		goto err_pf_reset;
15632 	}
15633 
15634 	err = i40e_handle_resets(pf);
15635 	if (err)
15636 		goto err_pf_reset;
15637 
15638 	i40e_check_recovery_mode(pf);
15639 
15640 	if (is_kdump_kernel()) {
15641 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15642 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15643 	} else {
15644 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15645 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15646 	}
15647 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15648 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15649 
15650 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15651 		 "%s-%s:misc",
15652 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15653 
15654 	err = i40e_init_shared_code(hw);
15655 	if (err) {
15656 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15657 			 err);
15658 		goto err_pf_reset;
15659 	}
15660 
15661 	/* set up a default setting for link flow control */
15662 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15663 
15664 	err = i40e_init_adminq(hw);
15665 	if (err) {
15666 		if (err == -EIO)
15667 			dev_info(&pdev->dev,
15668 				 "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",
15669 				 hw->aq.api_maj_ver,
15670 				 hw->aq.api_min_ver,
15671 				 I40E_FW_API_VERSION_MAJOR,
15672 				 I40E_FW_MINOR_VERSION(hw));
15673 		else
15674 			dev_info(&pdev->dev,
15675 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15676 
15677 		goto err_pf_reset;
15678 	}
15679 	i40e_get_oem_version(hw);
15680 	i40e_get_pba_string(hw);
15681 
15682 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15683 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15684 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15685 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15686 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15687 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15688 		 hw->subsystem_device_id);
15689 
15690 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15691 				  I40E_FW_MINOR_VERSION(hw) + 1))
15692 		dev_dbg(&pdev->dev,
15693 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15694 			 hw->aq.api_maj_ver,
15695 			 hw->aq.api_min_ver,
15696 			 I40E_FW_API_VERSION_MAJOR,
15697 			 I40E_FW_MINOR_VERSION(hw));
15698 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15699 		dev_info(&pdev->dev,
15700 			 "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",
15701 			 hw->aq.api_maj_ver,
15702 			 hw->aq.api_min_ver,
15703 			 I40E_FW_API_VERSION_MAJOR,
15704 			 I40E_FW_MINOR_VERSION(hw));
15705 
15706 	i40e_verify_eeprom(pf);
15707 
15708 	/* Rev 0 hardware was never productized */
15709 	if (hw->revision_id < 1)
15710 		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");
15711 
15712 	i40e_clear_pxe_mode(hw);
15713 
15714 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15715 	if (err)
15716 		goto err_adminq_setup;
15717 
15718 	err = i40e_sw_init(pf);
15719 	if (err) {
15720 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15721 		goto err_sw_init;
15722 	}
15723 
15724 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15725 		return i40e_init_recovery_mode(pf, hw);
15726 
15727 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15728 				hw->func_caps.num_rx_qp, 0, 0);
15729 	if (err) {
15730 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15731 		goto err_init_lan_hmc;
15732 	}
15733 
15734 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15735 	if (err) {
15736 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15737 		err = -ENOENT;
15738 		goto err_configure_lan_hmc;
15739 	}
15740 
15741 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15742 	 * Ignore error return codes because if it was already disabled via
15743 	 * hardware settings this will fail
15744 	 */
15745 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15746 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15747 		i40e_aq_stop_lldp(hw, true, false, NULL);
15748 	}
15749 
15750 	/* allow a platform config to override the HW addr */
15751 	i40e_get_platform_mac_addr(pdev, pf);
15752 
15753 	if (!is_valid_ether_addr(hw->mac.addr)) {
15754 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15755 		err = -EIO;
15756 		goto err_mac_addr;
15757 	}
15758 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15759 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15760 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15761 	if (is_valid_ether_addr(hw->mac.port_addr))
15762 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15763 
15764 	i40e_ptp_alloc_pins(pf);
15765 	pci_set_drvdata(pdev, pf);
15766 	pci_save_state(pdev);
15767 
15768 #ifdef CONFIG_I40E_DCB
15769 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15770 	(!status &&
15771 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15772 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15773 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15774 	dev_info(&pdev->dev,
15775 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15776 			"FW LLDP is disabled\n" :
15777 			"FW LLDP is enabled\n");
15778 
15779 	/* Enable FW to write default DCB config on link-up */
15780 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15781 
15782 	err = i40e_init_pf_dcb(pf);
15783 	if (err) {
15784 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15785 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15786 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15787 		/* Continue without DCB enabled */
15788 	}
15789 #endif /* CONFIG_I40E_DCB */
15790 
15791 	/* set up periodic task facility */
15792 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15793 	pf->service_timer_period = HZ;
15794 
15795 	INIT_WORK(&pf->service_task, i40e_service_task);
15796 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15797 
15798 	/* NVM bit on means WoL disabled for the port */
15799 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15800 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15801 		pf->wol_en = false;
15802 	else
15803 		pf->wol_en = true;
15804 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15805 
15806 	/* set up the main switch operations */
15807 	i40e_determine_queue_usage(pf);
15808 	err = i40e_init_interrupt_scheme(pf);
15809 	if (err)
15810 		goto err_switch_setup;
15811 
15812 	/* Reduce Tx and Rx pairs for kdump
15813 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15814 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15815 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15816 	 */
15817 	if (is_kdump_kernel())
15818 		pf->num_lan_msix = 1;
15819 
15820 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15821 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15822 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15823 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15824 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15825 						    UDP_TUNNEL_TYPE_GENEVE;
15826 
15827 	/* The number of VSIs reported by the FW is the minimum guaranteed
15828 	 * to us; HW supports far more and we share the remaining pool with
15829 	 * the other PFs. We allocate space for more than the guarantee with
15830 	 * the understanding that we might not get them all later.
15831 	 */
15832 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15833 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15834 	else
15835 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15836 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15837 		dev_warn(&pf->pdev->dev,
15838 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15839 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15840 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15841 	}
15842 
15843 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15844 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15845 			  GFP_KERNEL);
15846 	if (!pf->vsi) {
15847 		err = -ENOMEM;
15848 		goto err_switch_setup;
15849 	}
15850 
15851 #ifdef CONFIG_PCI_IOV
15852 	/* prep for VF support */
15853 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15854 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15855 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15856 		if (pci_num_vf(pdev))
15857 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15858 	}
15859 #endif
15860 	err = i40e_setup_pf_switch(pf, false, false);
15861 	if (err) {
15862 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15863 		goto err_vsis;
15864 	}
15865 
15866 	vsi = i40e_pf_get_main_vsi(pf);
15867 	INIT_LIST_HEAD(&vsi->ch_list);
15868 
15869 	/* if FDIR VSI was set up, start it now */
15870 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15871 	if (vsi)
15872 		i40e_vsi_open(vsi);
15873 
15874 	/* The driver only wants link up/down and module qualification
15875 	 * reports from firmware.  Note the negative logic.
15876 	 */
15877 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15878 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15879 					 I40E_AQ_EVENT_MEDIA_NA |
15880 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15881 	if (err)
15882 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15883 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
15884 
15885 	/* VF MDD event logs are rate limited to one second intervals */
15886 	ratelimit_state_init(&pf->mdd_message_rate_limit, 1 * HZ, 1);
15887 
15888 	/* Reconfigure hardware for allowing smaller MSS in the case
15889 	 * of TSO, so that we avoid the MDD being fired and causing
15890 	 * a reset in the case of small MSS+TSO.
15891 	 */
15892 	val = rd32(hw, I40E_REG_MSS);
15893 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15894 		val &= ~I40E_REG_MSS_MIN_MASK;
15895 		val |= I40E_64BYTE_MSS;
15896 		wr32(hw, I40E_REG_MSS, val);
15897 	}
15898 
15899 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15900 		msleep(75);
15901 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15902 		if (err)
15903 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15904 				 ERR_PTR(err),
15905 				 libie_aq_str(pf->hw.aq.asq_last_status));
15906 	}
15907 	/* The main driver is (mostly) up and happy. We need to set this state
15908 	 * before setting up the misc vector or we get a race and the vector
15909 	 * ends up disabled forever.
15910 	 */
15911 	clear_bit(__I40E_DOWN, pf->state);
15912 
15913 	/* In case of MSIX we are going to setup the misc vector right here
15914 	 * to handle admin queue events etc. In case of legacy and MSI
15915 	 * the misc functionality and queue processing is combined in
15916 	 * the same vector and that gets setup at open.
15917 	 */
15918 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15919 		err = i40e_setup_misc_vector(pf);
15920 		if (err) {
15921 			dev_info(&pdev->dev,
15922 				 "setup of misc vector failed: %d\n", err);
15923 			i40e_cloud_filter_exit(pf);
15924 			i40e_fdir_teardown(pf);
15925 			goto err_vsis;
15926 		}
15927 	}
15928 
15929 #ifdef CONFIG_PCI_IOV
15930 	/* prep for VF support */
15931 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15932 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15933 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15934 		/* disable link interrupts for VFs */
15935 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15936 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15937 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15938 		i40e_flush(hw);
15939 
15940 		if (pci_num_vf(pdev)) {
15941 			dev_info(&pdev->dev,
15942 				 "Active VFs found, allocating resources.\n");
15943 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15944 			if (err)
15945 				dev_info(&pdev->dev,
15946 					 "Error %d allocating resources for existing VFs\n",
15947 					 err);
15948 		}
15949 	}
15950 #endif /* CONFIG_PCI_IOV */
15951 
15952 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15953 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15954 						      pf->num_iwarp_msix,
15955 						      I40E_IWARP_IRQ_PILE_ID);
15956 		if (pf->iwarp_base_vector < 0) {
15957 			dev_info(&pdev->dev,
15958 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15959 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15960 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15961 		}
15962 	}
15963 
15964 	i40e_dbg_pf_init(pf);
15965 
15966 	/* tell the firmware that we're starting */
15967 	i40e_send_version(pf);
15968 
15969 	/* since everything's happy, start the service_task timer */
15970 	mod_timer(&pf->service_timer,
15971 		  round_jiffies(jiffies + pf->service_timer_period));
15972 
15973 	/* add this PF to client device list and launch a client service task */
15974 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15975 		err = i40e_lan_add_device(pf);
15976 		if (err)
15977 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15978 				 err);
15979 	}
15980 
15981 #define PCI_SPEED_SIZE 8
15982 #define PCI_WIDTH_SIZE 8
15983 	/* Devices on the IOSF bus do not have this information
15984 	 * and will report PCI Gen 1 x 1 by default so don't bother
15985 	 * checking them.
15986 	 */
15987 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
15988 		char speed[PCI_SPEED_SIZE] = "Unknown";
15989 		char width[PCI_WIDTH_SIZE] = "Unknown";
15990 
15991 		/* Get the negotiated link width and speed from PCI config
15992 		 * space
15993 		 */
15994 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15995 					  &link_status);
15996 
15997 		i40e_set_pci_config_data(hw, link_status);
15998 
15999 		switch (hw->bus.speed) {
16000 		case i40e_bus_speed_8000:
16001 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16002 		case i40e_bus_speed_5000:
16003 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16004 		case i40e_bus_speed_2500:
16005 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16006 		default:
16007 			break;
16008 		}
16009 		switch (hw->bus.width) {
16010 		case i40e_bus_width_pcie_x8:
16011 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16012 		case i40e_bus_width_pcie_x4:
16013 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16014 		case i40e_bus_width_pcie_x2:
16015 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16016 		case i40e_bus_width_pcie_x1:
16017 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16018 		default:
16019 			break;
16020 		}
16021 
16022 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16023 			 speed, width);
16024 
16025 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16026 		    hw->bus.speed < i40e_bus_speed_8000) {
16027 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16028 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16029 		}
16030 	}
16031 
16032 	/* get the requested speeds from the fw */
16033 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16034 	if (err)
16035 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16036 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16037 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16038 
16039 	/* set the FEC config due to the board capabilities */
16040 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16041 
16042 	/* get the supported phy types from the fw */
16043 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16044 	if (err)
16045 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16046 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16047 
16048 	/* make sure the MFS hasn't been set lower than the default */
16049 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16050 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16051 			rd32(&pf->hw, I40E_PRTGL_SAH));
16052 	if (val < MAX_FRAME_SIZE_DEFAULT)
16053 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16054 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16055 
16056 	/* Add a filter to drop all Flow control frames from any VSI from being
16057 	 * transmitted. By doing so we stop a malicious VF from sending out
16058 	 * PAUSE or PFC frames and potentially controlling traffic for other
16059 	 * PF/VF VSIs.
16060 	 * The FW can still send Flow control frames if enabled.
16061 	 */
16062 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16063 						       pf->main_vsi_seid);
16064 
16065 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16066 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16067 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16068 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16069 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16070 	/* print a string summarizing features */
16071 	i40e_print_features(pf);
16072 
16073 	i40e_devlink_register(pf);
16074 
16075 	return 0;
16076 
16077 	/* Unwind what we've done if something failed in the setup */
16078 err_vsis:
16079 	set_bit(__I40E_DOWN, pf->state);
16080 	i40e_clear_interrupt_scheme(pf);
16081 	kfree(pf->vsi);
16082 err_switch_setup:
16083 	i40e_reset_interrupt_capability(pf);
16084 	timer_shutdown_sync(&pf->service_timer);
16085 err_mac_addr:
16086 err_configure_lan_hmc:
16087 	(void)i40e_shutdown_lan_hmc(hw);
16088 err_init_lan_hmc:
16089 	kfree(pf->qp_pile);
16090 err_sw_init:
16091 err_adminq_setup:
16092 err_pf_reset:
16093 	iounmap(hw->hw_addr);
16094 err_ioremap:
16095 	i40e_free_pf(pf);
16096 err_pf_alloc:
16097 	pci_release_mem_regions(pdev);
16098 err_pci_reg:
16099 err_dma:
16100 	pci_disable_device(pdev);
16101 	return err;
16102 }
16103 
16104 /**
16105  * i40e_remove - Device removal routine
16106  * @pdev: PCI device information struct
16107  *
16108  * i40e_remove is called by the PCI subsystem to alert the driver
16109  * that is should release a PCI device.  This could be caused by a
16110  * Hot-Plug event, or because the driver is going to be removed from
16111  * memory.
16112  **/
i40e_remove(struct pci_dev * pdev)16113 static void i40e_remove(struct pci_dev *pdev)
16114 {
16115 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16116 	struct i40e_hw *hw = &pf->hw;
16117 	struct i40e_vsi *vsi;
16118 	struct i40e_veb *veb;
16119 	int ret_code;
16120 	int i;
16121 
16122 	i40e_devlink_unregister(pf);
16123 
16124 	i40e_dbg_pf_exit(pf);
16125 
16126 	i40e_ptp_stop(pf);
16127 
16128 	/* Disable RSS in hw */
16129 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16130 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16131 
16132 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16133 	 * flags, once they are set, i40e_rebuild should not be called as
16134 	 * i40e_prep_for_reset always returns early.
16135 	 */
16136 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16137 		usleep_range(1000, 2000);
16138 	set_bit(__I40E_IN_REMOVE, pf->state);
16139 
16140 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16141 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16142 		i40e_free_vfs(pf);
16143 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16144 	}
16145 	/* no more scheduling of any task */
16146 	set_bit(__I40E_SUSPENDED, pf->state);
16147 	set_bit(__I40E_DOWN, pf->state);
16148 	if (pf->service_timer.function)
16149 		timer_shutdown_sync(&pf->service_timer);
16150 	if (pf->service_task.func)
16151 		cancel_work_sync(&pf->service_task);
16152 
16153 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16154 		struct i40e_vsi *vsi = pf->vsi[0];
16155 
16156 		/* We know that we have allocated only one vsi for this PF,
16157 		 * it was just for registering netdevice, so the interface
16158 		 * could be visible in the 'ifconfig' output
16159 		 */
16160 		unregister_netdev(vsi->netdev);
16161 		free_netdev(vsi->netdev);
16162 
16163 		goto unmap;
16164 	}
16165 
16166 	/* Client close must be called explicitly here because the timer
16167 	 * has been stopped.
16168 	 */
16169 	i40e_notify_client_of_netdev_close(pf, false);
16170 
16171 	i40e_fdir_teardown(pf);
16172 
16173 	/* If there is a switch structure or any orphans, remove them.
16174 	 * This will leave only the PF's VSI remaining.
16175 	 */
16176 	i40e_pf_for_each_veb(pf, i, veb)
16177 		if (veb->uplink_seid == pf->mac_seid ||
16178 		    veb->uplink_seid == 0)
16179 			i40e_switch_branch_release(veb);
16180 
16181 	/* Now we can shutdown the PF's VSIs, just before we kill
16182 	 * adminq and hmc.
16183 	 */
16184 	i40e_pf_for_each_vsi(pf, i, vsi) {
16185 		i40e_vsi_close(vsi);
16186 		i40e_vsi_release(vsi);
16187 		pf->vsi[i] = NULL;
16188 	}
16189 
16190 	i40e_cloud_filter_exit(pf);
16191 
16192 	/* remove attached clients */
16193 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16194 		ret_code = i40e_lan_del_device(pf);
16195 		if (ret_code)
16196 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16197 				 ret_code);
16198 	}
16199 
16200 	/* shutdown and destroy the HMC */
16201 	if (hw->hmc.hmc_obj) {
16202 		ret_code = i40e_shutdown_lan_hmc(hw);
16203 		if (ret_code)
16204 			dev_warn(&pdev->dev,
16205 				 "Failed to destroy the HMC resources: %d\n",
16206 				 ret_code);
16207 	}
16208 
16209 unmap:
16210 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16211 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16212 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16213 		free_irq(pf->pdev->irq, pf);
16214 
16215 	/* shutdown the adminq */
16216 	i40e_shutdown_adminq(hw);
16217 
16218 	/* destroy the locks only once, here */
16219 	mutex_destroy(&hw->aq.arq_mutex);
16220 	mutex_destroy(&hw->aq.asq_mutex);
16221 
16222 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16223 	rtnl_lock();
16224 	i40e_clear_interrupt_scheme(pf);
16225 	i40e_pf_for_each_vsi(pf, i, vsi) {
16226 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16227 			i40e_vsi_clear_rings(vsi);
16228 
16229 		i40e_vsi_clear(vsi);
16230 		pf->vsi[i] = NULL;
16231 	}
16232 	rtnl_unlock();
16233 
16234 	i40e_pf_for_each_veb(pf, i, veb) {
16235 		kfree(veb);
16236 		pf->veb[i] = NULL;
16237 	}
16238 
16239 	kfree(pf->qp_pile);
16240 	kfree(pf->vsi);
16241 
16242 	iounmap(hw->hw_addr);
16243 	i40e_free_pf(pf);
16244 	pci_release_mem_regions(pdev);
16245 
16246 	pci_disable_device(pdev);
16247 }
16248 
16249 /**
16250  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16251  * using the mac_address_write admin q function
16252  * @pf: pointer to i40e_pf struct
16253  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16254 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16255 {
16256 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16257 	struct i40e_hw *hw = &pf->hw;
16258 	u8 mac_addr[6];
16259 	u16 flags = 0;
16260 	int ret;
16261 
16262 	/* Get current MAC address in case it's an LAA */
16263 	if (main_vsi && main_vsi->netdev) {
16264 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16265 	} else {
16266 		dev_err(&pf->pdev->dev,
16267 			"Failed to retrieve MAC address; using default\n");
16268 		ether_addr_copy(mac_addr, hw->mac.addr);
16269 	}
16270 
16271 	/* The FW expects the mac address write cmd to first be called with
16272 	 * one of these flags before calling it again with the multicast
16273 	 * enable flags.
16274 	 */
16275 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16276 
16277 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16278 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16279 
16280 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16281 	if (ret) {
16282 		dev_err(&pf->pdev->dev,
16283 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16284 		return;
16285 	}
16286 
16287 	flags = I40E_AQC_MC_MAG_EN
16288 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16289 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16290 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16291 	if (ret)
16292 		dev_err(&pf->pdev->dev,
16293 			"Failed to enable Multicast Magic Packet wake up\n");
16294 }
16295 
16296 /**
16297  * i40e_io_suspend - suspend all IO operations
16298  * @pf: pointer to i40e_pf struct
16299  *
16300  **/
i40e_io_suspend(struct i40e_pf * pf)16301 static int i40e_io_suspend(struct i40e_pf *pf)
16302 {
16303 	struct i40e_hw *hw = &pf->hw;
16304 
16305 	set_bit(__I40E_DOWN, pf->state);
16306 
16307 	/* Ensure service task will not be running */
16308 	timer_delete_sync(&pf->service_timer);
16309 	cancel_work_sync(&pf->service_task);
16310 
16311 	/* Client close must be called explicitly here because the timer
16312 	 * has been stopped.
16313 	 */
16314 	i40e_notify_client_of_netdev_close(pf, false);
16315 
16316 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16317 	    pf->wol_en)
16318 		i40e_enable_mc_magic_wake(pf);
16319 
16320 	/* Since we're going to destroy queues during the
16321 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16322 	 * whole section
16323 	 */
16324 	rtnl_lock();
16325 
16326 	i40e_prep_for_reset(pf);
16327 
16328 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16329 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16330 
16331 	/* Clear the interrupt scheme and release our IRQs so that the system
16332 	 * can safely hibernate even when there are a large number of CPUs.
16333 	 * Otherwise hibernation might fail when mapping all the vectors back
16334 	 * to CPU0.
16335 	 */
16336 	i40e_clear_interrupt_scheme(pf);
16337 
16338 	rtnl_unlock();
16339 
16340 	return 0;
16341 }
16342 
16343 /**
16344  * i40e_io_resume - resume IO operations
16345  * @pf: pointer to i40e_pf struct
16346  *
16347  **/
i40e_io_resume(struct i40e_pf * pf)16348 static int i40e_io_resume(struct i40e_pf *pf)
16349 {
16350 	struct device *dev = &pf->pdev->dev;
16351 	int err;
16352 
16353 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16354 	 * since we're going to be restoring queues
16355 	 */
16356 	rtnl_lock();
16357 
16358 	/* We cleared the interrupt scheme when we suspended, so we need to
16359 	 * restore it now to resume device functionality.
16360 	 */
16361 	err = i40e_restore_interrupt_scheme(pf);
16362 	if (err) {
16363 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16364 			err);
16365 	}
16366 
16367 	clear_bit(__I40E_DOWN, pf->state);
16368 	i40e_reset_and_rebuild(pf, false, true);
16369 
16370 	rtnl_unlock();
16371 
16372 	/* Clear suspended state last after everything is recovered */
16373 	clear_bit(__I40E_SUSPENDED, pf->state);
16374 
16375 	/* Restart the service task */
16376 	mod_timer(&pf->service_timer,
16377 		  round_jiffies(jiffies + pf->service_timer_period));
16378 
16379 	return 0;
16380 }
16381 
16382 /**
16383  * i40e_pci_error_detected - warning that something funky happened in PCI land
16384  * @pdev: PCI device information struct
16385  * @error: the type of PCI error
16386  *
16387  * Called to warn that something happened and the error handling steps
16388  * are in progress.  Allows the driver to quiesce things, be ready for
16389  * remediation.
16390  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16391 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16392 						pci_channel_state_t error)
16393 {
16394 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16395 
16396 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16397 
16398 	if (!pf) {
16399 		dev_info(&pdev->dev,
16400 			 "Cannot recover - error happened during device probe\n");
16401 		return PCI_ERS_RESULT_DISCONNECT;
16402 	}
16403 
16404 	/* shutdown all operations */
16405 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16406 		i40e_io_suspend(pf);
16407 
16408 	/* Request a slot reset */
16409 	return PCI_ERS_RESULT_NEED_RESET;
16410 }
16411 
16412 /**
16413  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16414  * @pdev: PCI device information struct
16415  *
16416  * Called to find if the driver can work with the device now that
16417  * the pci slot has been reset.  If a basic connection seems good
16418  * (registers are readable and have sane content) then return a
16419  * happy little PCI_ERS_RESULT_xxx.
16420  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16421 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16422 {
16423 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16424 	pci_ers_result_t result;
16425 	u32 reg;
16426 
16427 	dev_dbg(&pdev->dev, "%s\n", __func__);
16428 	/* enable I/O and memory of the device  */
16429 	if (pci_enable_device(pdev)) {
16430 		dev_info(&pdev->dev,
16431 			 "Cannot re-enable PCI device after reset.\n");
16432 		result = PCI_ERS_RESULT_DISCONNECT;
16433 	} else {
16434 		pci_set_master(pdev);
16435 		pci_restore_state(pdev);
16436 		pci_save_state(pdev);
16437 		pci_wake_from_d3(pdev, false);
16438 
16439 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16440 		if (reg == 0)
16441 			result = PCI_ERS_RESULT_RECOVERED;
16442 		else
16443 			result = PCI_ERS_RESULT_DISCONNECT;
16444 	}
16445 
16446 	return result;
16447 }
16448 
16449 /**
16450  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16451  * @pdev: PCI device information struct
16452  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16453 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16454 {
16455 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16456 
16457 	i40e_prep_for_reset(pf);
16458 }
16459 
16460 /**
16461  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16462  * @pdev: PCI device information struct
16463  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16464 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16465 {
16466 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16467 
16468 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16469 		return;
16470 
16471 	i40e_reset_and_rebuild(pf, false, false);
16472 #ifdef CONFIG_PCI_IOV
16473 	i40e_restore_all_vfs_msi_state(pdev);
16474 #endif /* CONFIG_PCI_IOV */
16475 }
16476 
16477 /**
16478  * i40e_pci_error_resume - restart operations after PCI error recovery
16479  * @pdev: PCI device information struct
16480  *
16481  * Called to allow the driver to bring things back up after PCI error
16482  * and/or reset recovery has finished.
16483  **/
i40e_pci_error_resume(struct pci_dev * pdev)16484 static void i40e_pci_error_resume(struct pci_dev *pdev)
16485 {
16486 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16487 
16488 	dev_dbg(&pdev->dev, "%s\n", __func__);
16489 	if (test_bit(__I40E_SUSPENDED, pf->state))
16490 		return;
16491 
16492 	i40e_io_resume(pf);
16493 }
16494 
16495 /**
16496  * i40e_shutdown - PCI callback for shutting down
16497  * @pdev: PCI device information struct
16498  **/
i40e_shutdown(struct pci_dev * pdev)16499 static void i40e_shutdown(struct pci_dev *pdev)
16500 {
16501 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16502 	struct i40e_hw *hw = &pf->hw;
16503 
16504 	set_bit(__I40E_SUSPENDED, pf->state);
16505 	set_bit(__I40E_DOWN, pf->state);
16506 
16507 	timer_delete_sync(&pf->service_timer);
16508 	cancel_work_sync(&pf->service_task);
16509 	i40e_cloud_filter_exit(pf);
16510 	i40e_fdir_teardown(pf);
16511 
16512 	/* Client close must be called explicitly here because the timer
16513 	 * has been stopped.
16514 	 */
16515 	i40e_notify_client_of_netdev_close(pf, false);
16516 
16517 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16518 	    pf->wol_en)
16519 		i40e_enable_mc_magic_wake(pf);
16520 
16521 	i40e_prep_for_reset(pf);
16522 
16523 	wr32(hw, I40E_PFPM_APM,
16524 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16525 	wr32(hw, I40E_PFPM_WUFC,
16526 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16527 
16528 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16529 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16530 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16531 		free_irq(pf->pdev->irq, pf);
16532 
16533 	/* Since we're going to destroy queues during the
16534 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16535 	 * whole section
16536 	 */
16537 	rtnl_lock();
16538 	i40e_clear_interrupt_scheme(pf);
16539 	rtnl_unlock();
16540 
16541 	if (system_state == SYSTEM_POWER_OFF) {
16542 		pci_wake_from_d3(pdev, pf->wol_en);
16543 		pci_set_power_state(pdev, PCI_D3hot);
16544 	}
16545 }
16546 
16547 /**
16548  * i40e_suspend - PM callback for moving to D3
16549  * @dev: generic device information structure
16550  **/
i40e_suspend(struct device * dev)16551 static int i40e_suspend(struct device *dev)
16552 {
16553 	struct i40e_pf *pf = dev_get_drvdata(dev);
16554 
16555 	/* If we're already suspended, then there is nothing to do */
16556 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16557 		return 0;
16558 	return i40e_io_suspend(pf);
16559 }
16560 
16561 /**
16562  * i40e_resume - PM callback for waking up from D3
16563  * @dev: generic device information structure
16564  **/
i40e_resume(struct device * dev)16565 static int i40e_resume(struct device *dev)
16566 {
16567 	struct i40e_pf *pf = dev_get_drvdata(dev);
16568 
16569 	/* If we're not suspended, then there is nothing to do */
16570 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16571 		return 0;
16572 	return i40e_io_resume(pf);
16573 }
16574 
16575 static const struct pci_error_handlers i40e_err_handler = {
16576 	.error_detected = i40e_pci_error_detected,
16577 	.slot_reset = i40e_pci_error_slot_reset,
16578 	.reset_prepare = i40e_pci_error_reset_prepare,
16579 	.reset_done = i40e_pci_error_reset_done,
16580 	.resume = i40e_pci_error_resume,
16581 };
16582 
16583 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16584 
16585 static struct pci_driver i40e_driver = {
16586 	.name     = i40e_driver_name,
16587 	.id_table = i40e_pci_tbl,
16588 	.probe    = i40e_probe,
16589 	.remove   = i40e_remove,
16590 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16591 	.shutdown = i40e_shutdown,
16592 	.err_handler = &i40e_err_handler,
16593 	.sriov_configure = i40e_pci_sriov_configure,
16594 };
16595 
16596 /**
16597  * i40e_init_module - Driver registration routine
16598  *
16599  * i40e_init_module is the first routine called when the driver is
16600  * loaded. All it does is register with the PCI subsystem.
16601  **/
i40e_init_module(void)16602 static int __init i40e_init_module(void)
16603 {
16604 	int err;
16605 
16606 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16607 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16608 
16609 	/* There is no need to throttle the number of active tasks because
16610 	 * each device limits its own task using a state bit for scheduling
16611 	 * the service task, and the device tasks do not interfere with each
16612 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16613 	 * since we need to be able to guarantee forward progress even under
16614 	 * memory pressure.
16615 	 */
16616 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16617 	if (!i40e_wq) {
16618 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16619 		return -ENOMEM;
16620 	}
16621 
16622 	i40e_dbg_init();
16623 	err = pci_register_driver(&i40e_driver);
16624 	if (err) {
16625 		destroy_workqueue(i40e_wq);
16626 		i40e_dbg_exit();
16627 		return err;
16628 	}
16629 
16630 	return 0;
16631 }
16632 module_init(i40e_init_module);
16633 
16634 /**
16635  * i40e_exit_module - Driver exit cleanup routine
16636  *
16637  * i40e_exit_module is called just before the driver is removed
16638  * from memory.
16639  **/
i40e_exit_module(void)16640 static void __exit i40e_exit_module(void)
16641 {
16642 	pci_unregister_driver(&i40e_driver);
16643 	destroy_workqueue(i40e_wq);
16644 	ida_destroy(&i40e_client_ida);
16645 	i40e_dbg_exit();
16646 }
16647 module_exit(i40e_exit_module);
16648