• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/if_bridge.h>
7 #include <linux/if_macvlan.h>
8 #include <linux/module.h>
9 #include <net/pkt_cls.h>
10 #include <net/xdp_sock_drv.h>
11 
12 /* Local includes */
13 #include "i40e.h"
14 #include "i40e_devids.h"
15 #include "i40e_diag.h"
16 #include "i40e_lan_hmc.h"
17 #include "i40e_virtchnl_pf.h"
18 #include "i40e_xsk.h"
19 
20 /* All i40e tracepoints are defined by the include below, which
21  * must be included exactly once across the whole kernel with
22  * CREATE_TRACE_POINTS defined
23  */
24 #define CREATE_TRACE_POINTS
25 #include "i40e_trace.h"
26 
27 const char i40e_driver_name[] = "i40e";
28 static const char i40e_driver_string[] =
29 			"Intel(R) Ethernet Connection XL710 Network Driver";
30 
31 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
32 
33 /* a bit of forward declarations */
34 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
35 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
36 static int i40e_add_vsi(struct i40e_vsi *vsi);
37 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
38 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
39 static int i40e_setup_misc_vector(struct i40e_pf *pf);
40 static void i40e_determine_queue_usage(struct i40e_pf *pf);
41 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
42 static void i40e_prep_for_reset(struct i40e_pf *pf);
43 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
44 				   bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
48 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
49 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
50 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
51 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
52 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
53 static int i40e_get_capabilities(struct i40e_pf *pf,
54 				 enum i40e_admin_queue_opc list_type);
55 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
56 
57 /* i40e_pci_tbl - PCI Device ID Table
58  *
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
64 static const struct pci_device_id i40e_pci_tbl[] = {
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
91 	/* required last entry */
92 	{0, }
93 };
94 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95 
96 #define I40E_MAX_VF_COUNT 128
97 static int debug = -1;
98 module_param(debug, uint, 0);
99 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
100 
101 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102 MODULE_IMPORT_NS(LIBIE);
103 MODULE_LICENSE("GPL v2");
104 
105 static struct workqueue_struct *i40e_wq;
106 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)107 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
108 				  struct net_device *netdev, int delta)
109 {
110 	struct netdev_hw_addr_list *ha_list;
111 	struct netdev_hw_addr *ha;
112 
113 	if (!f || !netdev)
114 		return;
115 
116 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
117 		ha_list = &netdev->uc;
118 	else
119 		ha_list = &netdev->mc;
120 
121 	netdev_hw_addr_list_for_each(ha, ha_list) {
122 		if (ether_addr_equal(ha->addr, f->macaddr)) {
123 			ha->refcount += delta;
124 			if (ha->refcount <= 0)
125 				ha->refcount = 1;
126 			break;
127 		}
128 	}
129 }
130 
131 /**
132  * i40e_hw_to_dev - get device pointer from the hardware structure
133  * @hw: pointer to the device HW structure
134  **/
i40e_hw_to_dev(struct i40e_hw * hw)135 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
136 {
137 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
138 
139 	return &pf->pdev->dev;
140 }
141 
142 /**
143  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to fill out
146  * @size: size of memory requested
147  * @alignment: what to align the allocation to
148  **/
i40e_allocate_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)149 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
150 			  u64 size, u32 alignment)
151 {
152 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
153 
154 	mem->size = ALIGN(size, alignment);
155 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
156 				     GFP_KERNEL);
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_dma_mem - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
i40e_free_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem)168 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
169 {
170 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
171 
172 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
173 	mem->va = NULL;
174 	mem->pa = 0;
175 	mem->size = 0;
176 
177 	return 0;
178 }
179 
180 /**
181  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
182  * @hw:   pointer to the HW structure
183  * @mem:  ptr to mem struct to fill out
184  * @size: size of memory requested
185  **/
i40e_allocate_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)186 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
187 			   u32 size)
188 {
189 	mem->size = size;
190 	mem->va = kzalloc(size, GFP_KERNEL);
191 
192 	if (!mem->va)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * i40e_free_virt_mem - OS specific memory free for shared code
200  * @hw:   pointer to the HW structure
201  * @mem:  ptr to mem struct to free
202  **/
i40e_free_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem)203 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
204 {
205 	/* it's ok to kfree a NULL pointer */
206 	kfree(mem->va);
207 	mem->va = NULL;
208 	mem->size = 0;
209 
210 	return 0;
211 }
212 
213 /**
214  * i40e_get_lump - find a lump of free generic resource
215  * @pf: board private structure
216  * @pile: the pile of resource to search
217  * @needed: the number of items needed
218  * @id: an owner id to stick on the items assigned
219  *
220  * Returns the base item index of the lump, or negative for error
221  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)222 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
223 			 u16 needed, u16 id)
224 {
225 	int ret = -ENOMEM;
226 	int i, j;
227 
228 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
229 		dev_info(&pf->pdev->dev,
230 			 "param err: pile=%s needed=%d id=0x%04x\n",
231 			 pile ? "<valid>" : "<null>", needed, id);
232 		return -EINVAL;
233 	}
234 
235 	/* Allocate last queue in the pile for FDIR VSI queue
236 	 * so it doesn't fragment the qp_pile
237 	 */
238 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
239 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
240 			dev_err(&pf->pdev->dev,
241 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
242 				pile->num_entries - 1);
243 			return -ENOMEM;
244 		}
245 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
246 		return pile->num_entries - 1;
247 	}
248 
249 	i = 0;
250 	while (i < pile->num_entries) {
251 		/* skip already allocated entries */
252 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
253 			i++;
254 			continue;
255 		}
256 
257 		/* do we have enough in this lump? */
258 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
259 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
260 				break;
261 		}
262 
263 		if (j == needed) {
264 			/* there was enough, so assign it to the requestor */
265 			for (j = 0; j < needed; j++)
266 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
267 			ret = i;
268 			break;
269 		}
270 
271 		/* not enough, so skip over it and continue looking */
272 		i += j;
273 	}
274 
275 	return ret;
276 }
277 
278 /**
279  * i40e_put_lump - return a lump of generic resource
280  * @pile: the pile of resource to search
281  * @index: the base item index
282  * @id: the owner id of the items assigned
283  *
284  * Returns the count of items in the lump
285  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)286 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
287 {
288 	int valid_id = (id | I40E_PILE_VALID_BIT);
289 	int count = 0;
290 	u16 i;
291 
292 	if (!pile || index >= pile->num_entries)
293 		return -EINVAL;
294 
295 	for (i = index;
296 	     i < pile->num_entries && pile->list[i] == valid_id;
297 	     i++) {
298 		pile->list[i] = 0;
299 		count++;
300 	}
301 
302 
303 	return count;
304 }
305 
306 /**
307  * i40e_find_vsi_from_id - searches for the vsi with the given id
308  * @pf: the pf structure to search for the vsi
309  * @id: id of the vsi it is searching for
310  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)311 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
312 {
313 	struct i40e_vsi *vsi;
314 	int i;
315 
316 	i40e_pf_for_each_vsi(pf, i, vsi)
317 		if (vsi->id == id)
318 			return vsi;
319 
320 	return NULL;
321 }
322 
323 /**
324  * i40e_service_event_schedule - Schedule the service task to wake up
325  * @pf: board private structure
326  *
327  * If not already scheduled, this puts the task into the work queue
328  **/
i40e_service_event_schedule(struct i40e_pf * pf)329 void i40e_service_event_schedule(struct i40e_pf *pf)
330 {
331 	if ((!test_bit(__I40E_DOWN, pf->state) &&
332 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
333 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
334 		queue_work(i40e_wq, &pf->service_task);
335 }
336 
337 /**
338  * i40e_tx_timeout - Respond to a Tx Hang
339  * @netdev: network interface device structure
340  * @txqueue: queue number timing out
341  *
342  * If any port has noticed a Tx timeout, it is likely that the whole
343  * device is munged, not just the one netdev port, so go for the full
344  * reset.
345  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)346 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
347 {
348 	struct i40e_netdev_priv *np = netdev_priv(netdev);
349 	struct i40e_vsi *vsi = np->vsi;
350 	struct i40e_pf *pf = vsi->back;
351 	struct i40e_ring *tx_ring = NULL;
352 	unsigned int i;
353 	u32 head, val;
354 
355 	pf->tx_timeout_count++;
356 
357 	/* with txqueue index, find the tx_ring struct */
358 	for (i = 0; i < vsi->num_queue_pairs; i++) {
359 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
360 			if (txqueue ==
361 			    vsi->tx_rings[i]->queue_index) {
362 				tx_ring = vsi->tx_rings[i];
363 				break;
364 			}
365 		}
366 	}
367 
368 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
369 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
370 	else if (time_before(jiffies,
371 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
372 		return;   /* don't do any new action before the next timeout */
373 
374 	/* don't kick off another recovery if one is already pending */
375 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
376 		return;
377 
378 	if (tx_ring) {
379 		head = i40e_get_head(tx_ring);
380 		/* Read interrupt register */
381 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
382 			val = rd32(&pf->hw,
383 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
384 						tx_ring->vsi->base_vector - 1));
385 		else
386 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
387 
388 		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",
389 			    vsi->seid, txqueue, tx_ring->next_to_clean,
390 			    head, tx_ring->next_to_use,
391 			    readl(tx_ring->tail), val);
392 	}
393 
394 	pf->tx_timeout_last_recovery = jiffies;
395 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
396 		    pf->tx_timeout_recovery_level, txqueue);
397 
398 	switch (pf->tx_timeout_recovery_level) {
399 	case 1:
400 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
401 		break;
402 	case 2:
403 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
404 		break;
405 	case 3:
406 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
407 		break;
408 	default:
409 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
410 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
411 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
412 		break;
413 	}
414 
415 	i40e_service_event_schedule(pf);
416 	pf->tx_timeout_recovery_level++;
417 }
418 
419 /**
420  * i40e_get_vsi_stats_struct - Get System Network Statistics
421  * @vsi: the VSI we care about
422  *
423  * Returns the address of the device statistics structure.
424  * The statistics are actually updated from the service task.
425  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)426 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
427 {
428 	return &vsi->net_stats;
429 }
430 
431 /**
432  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
433  * @ring: Tx ring to get statistics from
434  * @stats: statistics entry to be updated
435  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)436 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
437 					    struct rtnl_link_stats64 *stats)
438 {
439 	u64 bytes, packets;
440 	unsigned int start;
441 
442 	do {
443 		start = u64_stats_fetch_begin(&ring->syncp);
444 		packets = ring->stats.packets;
445 		bytes   = ring->stats.bytes;
446 	} while (u64_stats_fetch_retry(&ring->syncp, start));
447 
448 	stats->tx_packets += packets;
449 	stats->tx_bytes   += bytes;
450 }
451 
452 /**
453  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
454  * @netdev: network interface device structure
455  * @stats: data structure to store statistics
456  *
457  * Returns the address of the device statistics structure.
458  * The statistics are actually updated from the service task.
459  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)460 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
461 				  struct rtnl_link_stats64 *stats)
462 {
463 	struct i40e_netdev_priv *np = netdev_priv(netdev);
464 	struct i40e_vsi *vsi = np->vsi;
465 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
466 	struct i40e_ring *ring;
467 	int i;
468 
469 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
470 		return;
471 
472 	if (!vsi->tx_rings)
473 		return;
474 
475 	rcu_read_lock();
476 	for (i = 0; i < vsi->num_queue_pairs; i++) {
477 		u64 bytes, packets;
478 		unsigned int start;
479 
480 		ring = READ_ONCE(vsi->tx_rings[i]);
481 		if (!ring)
482 			continue;
483 		i40e_get_netdev_stats_struct_tx(ring, stats);
484 
485 		if (i40e_enabled_xdp_vsi(vsi)) {
486 			ring = READ_ONCE(vsi->xdp_rings[i]);
487 			if (!ring)
488 				continue;
489 			i40e_get_netdev_stats_struct_tx(ring, stats);
490 		}
491 
492 		ring = READ_ONCE(vsi->rx_rings[i]);
493 		if (!ring)
494 			continue;
495 		do {
496 			start   = u64_stats_fetch_begin(&ring->syncp);
497 			packets = ring->stats.packets;
498 			bytes   = ring->stats.bytes;
499 		} while (u64_stats_fetch_retry(&ring->syncp, start));
500 
501 		stats->rx_packets += packets;
502 		stats->rx_bytes   += bytes;
503 
504 	}
505 	rcu_read_unlock();
506 
507 	/* following stats updated by i40e_watchdog_subtask() */
508 	stats->multicast	= vsi_stats->multicast;
509 	stats->tx_errors	= vsi_stats->tx_errors;
510 	stats->tx_dropped	= vsi_stats->tx_dropped;
511 	stats->rx_errors	= vsi_stats->rx_errors;
512 	stats->rx_dropped	= vsi_stats->rx_dropped;
513 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
514 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
515 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
516 }
517 
518 /**
519  * i40e_vsi_reset_stats - Resets all stats of the given vsi
520  * @vsi: the VSI to have its stats reset
521  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)522 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
523 {
524 	struct rtnl_link_stats64 *ns;
525 	int i;
526 
527 	if (!vsi)
528 		return;
529 
530 	ns = i40e_get_vsi_stats_struct(vsi);
531 	memset(ns, 0, sizeof(*ns));
532 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
533 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
534 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
535 	if (vsi->rx_rings && vsi->rx_rings[0]) {
536 		for (i = 0; i < vsi->num_queue_pairs; i++) {
537 			memset(&vsi->rx_rings[i]->stats, 0,
538 			       sizeof(vsi->rx_rings[i]->stats));
539 			memset(&vsi->rx_rings[i]->rx_stats, 0,
540 			       sizeof(vsi->rx_rings[i]->rx_stats));
541 			memset(&vsi->tx_rings[i]->stats, 0,
542 			       sizeof(vsi->tx_rings[i]->stats));
543 			memset(&vsi->tx_rings[i]->tx_stats, 0,
544 			       sizeof(vsi->tx_rings[i]->tx_stats));
545 		}
546 	}
547 	vsi->stat_offsets_loaded = false;
548 }
549 
550 /**
551  * i40e_pf_reset_stats - Reset all of the stats for the given PF
552  * @pf: the PF to be reset
553  **/
i40e_pf_reset_stats(struct i40e_pf * pf)554 void i40e_pf_reset_stats(struct i40e_pf *pf)
555 {
556 	struct i40e_veb *veb;
557 	int i;
558 
559 	memset(&pf->stats, 0, sizeof(pf->stats));
560 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
561 	pf->stat_offsets_loaded = false;
562 
563 	i40e_pf_for_each_veb(pf, i, veb) {
564 		memset(&veb->stats, 0, sizeof(veb->stats));
565 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
566 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
567 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
568 		veb->stat_offsets_loaded = false;
569 	}
570 	pf->hw_csum_rx_error = 0;
571 }
572 
573 /**
574  * i40e_compute_pci_to_hw_id - compute index form PCI function.
575  * @vsi: ptr to the VSI to read from.
576  * @hw: ptr to the hardware info.
577  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)578 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
579 {
580 	int pf_count = i40e_get_pf_count(hw);
581 
582 	if (vsi->type == I40E_VSI_SRIOV)
583 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
584 
585 	return hw->port + BIT(7);
586 }
587 
588 /**
589  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
590  * @hw: ptr to the hardware info.
591  * @hireg: the high 32 bit reg to read.
592  * @loreg: the low 32 bit reg to read.
593  * @offset_loaded: has the initial offset been loaded yet.
594  * @offset: ptr to current offset value.
595  * @stat: ptr to the stat.
596  *
597  * Since the device stats are not reset at PFReset, they will not
598  * be zeroed when the driver starts.  We'll save the first values read
599  * and use them as offsets to be subtracted from the raw values in order
600  * to report stats that count from zero.
601  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)602 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
603 			       bool offset_loaded, u64 *offset, u64 *stat)
604 {
605 	u64 new_data;
606 
607 	new_data = rd64(hw, loreg);
608 
609 	if (!offset_loaded || new_data < *offset)
610 		*offset = new_data;
611 	*stat = new_data - *offset;
612 }
613 
614 /**
615  * i40e_stat_update48 - read and update a 48 bit stat from the chip
616  * @hw: ptr to the hardware info
617  * @hireg: the high 32 bit reg to read
618  * @loreg: the low 32 bit reg to read
619  * @offset_loaded: has the initial offset been loaded yet
620  * @offset: ptr to current offset value
621  * @stat: ptr to the stat
622  *
623  * Since the device stats are not reset at PFReset, they likely will not
624  * be zeroed when the driver starts.  We'll save the first values read
625  * and use them as offsets to be subtracted from the raw values in order
626  * to report stats that count from zero.  In the process, we also manage
627  * the potential roll-over.
628  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)629 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
630 			       bool offset_loaded, u64 *offset, u64 *stat)
631 {
632 	u64 new_data;
633 
634 	if (hw->device_id == I40E_DEV_ID_QEMU) {
635 		new_data = rd32(hw, loreg);
636 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
637 	} else {
638 		new_data = rd64(hw, loreg);
639 	}
640 	if (!offset_loaded)
641 		*offset = new_data;
642 	if (likely(new_data >= *offset))
643 		*stat = new_data - *offset;
644 	else
645 		*stat = (new_data + BIT_ULL(48)) - *offset;
646 	*stat &= 0xFFFFFFFFFFFFULL;
647 }
648 
649 /**
650  * i40e_stat_update32 - read and update a 32 bit stat from the chip
651  * @hw: ptr to the hardware info
652  * @reg: the hw reg to read
653  * @offset_loaded: has the initial offset been loaded yet
654  * @offset: ptr to current offset value
655  * @stat: ptr to the stat
656  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)657 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
658 			       bool offset_loaded, u64 *offset, u64 *stat)
659 {
660 	u32 new_data;
661 
662 	new_data = rd32(hw, reg);
663 	if (!offset_loaded)
664 		*offset = new_data;
665 	if (likely(new_data >= *offset))
666 		*stat = (u32)(new_data - *offset);
667 	else
668 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
669 }
670 
671 /**
672  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
673  * @hw: ptr to the hardware info
674  * @reg: the hw reg to read and clear
675  * @stat: ptr to the stat
676  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)677 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
678 {
679 	u32 new_data = rd32(hw, reg);
680 
681 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
682 	*stat += new_data;
683 }
684 
685 /**
686  * i40e_stats_update_rx_discards - update rx_discards.
687  * @vsi: ptr to the VSI to be updated.
688  * @hw: ptr to the hardware info.
689  * @stat_idx: VSI's stat_counter_idx.
690  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
691  * @stat_offset: ptr to stat_offset to store first read of specific register.
692  * @stat: ptr to VSI's stat to be updated.
693  **/
694 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)695 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
696 			      int stat_idx, bool offset_loaded,
697 			      struct i40e_eth_stats *stat_offset,
698 			      struct i40e_eth_stats *stat)
699 {
700 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
701 			   &stat_offset->rx_discards, &stat->rx_discards);
702 	i40e_stat_update64(hw,
703 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
704 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
705 			   offset_loaded, &stat_offset->rx_discards_other,
706 			   &stat->rx_discards_other);
707 }
708 
709 /**
710  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
711  * @vsi: the VSI to be updated
712  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)713 void i40e_update_eth_stats(struct i40e_vsi *vsi)
714 {
715 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
716 	struct i40e_pf *pf = vsi->back;
717 	struct i40e_hw *hw = &pf->hw;
718 	struct i40e_eth_stats *oes;
719 	struct i40e_eth_stats *es;     /* device's eth stats */
720 
721 	es = &vsi->eth_stats;
722 	oes = &vsi->eth_stats_offsets;
723 
724 	/* Gather up the stats that the hw collects */
725 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
726 			   vsi->stat_offsets_loaded,
727 			   &oes->tx_errors, &es->tx_errors);
728 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
729 			   vsi->stat_offsets_loaded,
730 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
731 
732 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
733 			   I40E_GLV_GORCL(stat_idx),
734 			   vsi->stat_offsets_loaded,
735 			   &oes->rx_bytes, &es->rx_bytes);
736 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
737 			   I40E_GLV_UPRCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_unicast, &es->rx_unicast);
740 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
741 			   I40E_GLV_MPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_multicast, &es->rx_multicast);
744 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
745 			   I40E_GLV_BPRCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_broadcast, &es->rx_broadcast);
748 
749 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
750 			   I40E_GLV_GOTCL(stat_idx),
751 			   vsi->stat_offsets_loaded,
752 			   &oes->tx_bytes, &es->tx_bytes);
753 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
754 			   I40E_GLV_UPTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_unicast, &es->tx_unicast);
757 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
758 			   I40E_GLV_MPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_multicast, &es->tx_multicast);
761 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
762 			   I40E_GLV_BPTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_broadcast, &es->tx_broadcast);
765 
766 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
767 				      vsi->stat_offsets_loaded, oes, es);
768 
769 	vsi->stat_offsets_loaded = true;
770 }
771 
772 /**
773  * i40e_update_veb_stats - Update Switch component statistics
774  * @veb: the VEB being updated
775  **/
i40e_update_veb_stats(struct i40e_veb * veb)776 void i40e_update_veb_stats(struct i40e_veb *veb)
777 {
778 	struct i40e_pf *pf = veb->pf;
779 	struct i40e_hw *hw = &pf->hw;
780 	struct i40e_eth_stats *oes;
781 	struct i40e_eth_stats *es;     /* device's eth stats */
782 	struct i40e_veb_tc_stats *veb_oes;
783 	struct i40e_veb_tc_stats *veb_es;
784 	int i, idx = 0;
785 
786 	idx = veb->stats_idx;
787 	es = &veb->stats;
788 	oes = &veb->stats_offsets;
789 	veb_es = &veb->tc_stats;
790 	veb_oes = &veb->tc_stats_offsets;
791 
792 	/* Gather up the stats that the hw collects */
793 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
794 			   veb->stat_offsets_loaded,
795 			   &oes->tx_discards, &es->tx_discards);
796 	if (hw->revision_id > 0)
797 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
798 				   veb->stat_offsets_loaded,
799 				   &oes->rx_unknown_protocol,
800 				   &es->rx_unknown_protocol);
801 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
802 			   veb->stat_offsets_loaded,
803 			   &oes->rx_bytes, &es->rx_bytes);
804 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->rx_unicast, &es->rx_unicast);
807 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->rx_multicast, &es->rx_multicast);
810 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->rx_broadcast, &es->rx_broadcast);
813 
814 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
815 			   veb->stat_offsets_loaded,
816 			   &oes->tx_bytes, &es->tx_bytes);
817 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
818 			   veb->stat_offsets_loaded,
819 			   &oes->tx_unicast, &es->tx_unicast);
820 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
821 			   veb->stat_offsets_loaded,
822 			   &oes->tx_multicast, &es->tx_multicast);
823 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
824 			   veb->stat_offsets_loaded,
825 			   &oes->tx_broadcast, &es->tx_broadcast);
826 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
827 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
828 				   I40E_GLVEBTC_RPCL(i, idx),
829 				   veb->stat_offsets_loaded,
830 				   &veb_oes->tc_rx_packets[i],
831 				   &veb_es->tc_rx_packets[i]);
832 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
833 				   I40E_GLVEBTC_RBCL(i, idx),
834 				   veb->stat_offsets_loaded,
835 				   &veb_oes->tc_rx_bytes[i],
836 				   &veb_es->tc_rx_bytes[i]);
837 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
838 				   I40E_GLVEBTC_TPCL(i, idx),
839 				   veb->stat_offsets_loaded,
840 				   &veb_oes->tc_tx_packets[i],
841 				   &veb_es->tc_tx_packets[i]);
842 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
843 				   I40E_GLVEBTC_TBCL(i, idx),
844 				   veb->stat_offsets_loaded,
845 				   &veb_oes->tc_tx_bytes[i],
846 				   &veb_es->tc_tx_bytes[i]);
847 	}
848 	veb->stat_offsets_loaded = true;
849 }
850 
851 /**
852  * i40e_update_vsi_stats - Update the vsi statistics counters.
853  * @vsi: the VSI to be updated
854  *
855  * There are a few instances where we store the same stat in a
856  * couple of different structs.  This is partly because we have
857  * the netdev stats that need to be filled out, which is slightly
858  * different from the "eth_stats" defined by the chip and used in
859  * VF communications.  We sort it out here.
860  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)861 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
862 {
863 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
864 	struct i40e_pf *pf = vsi->back;
865 	struct rtnl_link_stats64 *ons;
866 	struct rtnl_link_stats64 *ns;   /* netdev stats */
867 	struct i40e_eth_stats *oes;
868 	struct i40e_eth_stats *es;     /* device's eth stats */
869 	u64 tx_restart, tx_busy;
870 	struct i40e_ring *p;
871 	u64 bytes, packets;
872 	unsigned int start;
873 	u64 tx_linearize;
874 	u64 tx_force_wb;
875 	u64 tx_stopped;
876 	u64 rx_p, rx_b;
877 	u64 tx_p, tx_b;
878 	u16 q;
879 
880 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
881 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
882 		return;
883 
884 	ns = i40e_get_vsi_stats_struct(vsi);
885 	ons = &vsi->net_stats_offsets;
886 	es = &vsi->eth_stats;
887 	oes = &vsi->eth_stats_offsets;
888 
889 	/* Gather up the netdev and vsi stats that the driver collects
890 	 * on the fly during packet processing
891 	 */
892 	rx_b = rx_p = 0;
893 	tx_b = tx_p = 0;
894 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
895 	tx_stopped = 0;
896 	rx_page = 0;
897 	rx_buf = 0;
898 	rx_reuse = 0;
899 	rx_alloc = 0;
900 	rx_waive = 0;
901 	rx_busy = 0;
902 	rcu_read_lock();
903 	for (q = 0; q < vsi->num_queue_pairs; q++) {
904 		/* locate Tx ring */
905 		p = READ_ONCE(vsi->tx_rings[q]);
906 		if (!p)
907 			continue;
908 
909 		do {
910 			start = u64_stats_fetch_begin(&p->syncp);
911 			packets = p->stats.packets;
912 			bytes = p->stats.bytes;
913 		} while (u64_stats_fetch_retry(&p->syncp, start));
914 		tx_b += bytes;
915 		tx_p += packets;
916 		tx_restart += p->tx_stats.restart_queue;
917 		tx_busy += p->tx_stats.tx_busy;
918 		tx_linearize += p->tx_stats.tx_linearize;
919 		tx_force_wb += p->tx_stats.tx_force_wb;
920 		tx_stopped += p->tx_stats.tx_stopped;
921 
922 		/* locate Rx ring */
923 		p = READ_ONCE(vsi->rx_rings[q]);
924 		if (!p)
925 			continue;
926 
927 		do {
928 			start = u64_stats_fetch_begin(&p->syncp);
929 			packets = p->stats.packets;
930 			bytes = p->stats.bytes;
931 		} while (u64_stats_fetch_retry(&p->syncp, start));
932 		rx_b += bytes;
933 		rx_p += packets;
934 		rx_buf += p->rx_stats.alloc_buff_failed;
935 		rx_page += p->rx_stats.alloc_page_failed;
936 		rx_reuse += p->rx_stats.page_reuse_count;
937 		rx_alloc += p->rx_stats.page_alloc_count;
938 		rx_waive += p->rx_stats.page_waive_count;
939 		rx_busy += p->rx_stats.page_busy_count;
940 
941 		if (i40e_enabled_xdp_vsi(vsi)) {
942 			/* locate XDP ring */
943 			p = READ_ONCE(vsi->xdp_rings[q]);
944 			if (!p)
945 				continue;
946 
947 			do {
948 				start = u64_stats_fetch_begin(&p->syncp);
949 				packets = p->stats.packets;
950 				bytes = p->stats.bytes;
951 			} while (u64_stats_fetch_retry(&p->syncp, start));
952 			tx_b += bytes;
953 			tx_p += packets;
954 			tx_restart += p->tx_stats.restart_queue;
955 			tx_busy += p->tx_stats.tx_busy;
956 			tx_linearize += p->tx_stats.tx_linearize;
957 			tx_force_wb += p->tx_stats.tx_force_wb;
958 		}
959 	}
960 	rcu_read_unlock();
961 	vsi->tx_restart = tx_restart;
962 	vsi->tx_busy = tx_busy;
963 	vsi->tx_linearize = tx_linearize;
964 	vsi->tx_force_wb = tx_force_wb;
965 	vsi->tx_stopped = tx_stopped;
966 	vsi->rx_page_failed = rx_page;
967 	vsi->rx_buf_failed = rx_buf;
968 	vsi->rx_page_reuse = rx_reuse;
969 	vsi->rx_page_alloc = rx_alloc;
970 	vsi->rx_page_waive = rx_waive;
971 	vsi->rx_page_busy = rx_busy;
972 
973 	ns->rx_packets = rx_p;
974 	ns->rx_bytes = rx_b;
975 	ns->tx_packets = tx_p;
976 	ns->tx_bytes = tx_b;
977 
978 	/* update netdev stats from eth stats */
979 	i40e_update_eth_stats(vsi);
980 	ons->tx_errors = oes->tx_errors;
981 	ns->tx_errors = es->tx_errors;
982 	ons->multicast = oes->rx_multicast;
983 	ns->multicast = es->rx_multicast;
984 	ons->rx_dropped = oes->rx_discards_other;
985 	ns->rx_dropped = es->rx_discards_other;
986 	ons->rx_missed_errors = oes->rx_discards;
987 	ns->rx_missed_errors = es->rx_discards;
988 	ons->tx_dropped = oes->tx_discards;
989 	ns->tx_dropped = es->tx_discards;
990 
991 	/* pull in a couple PF stats if this is the main vsi */
992 	if (vsi->type == I40E_VSI_MAIN) {
993 		ns->rx_crc_errors = pf->stats.crc_errors;
994 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
995 		ns->rx_length_errors = pf->stats.rx_length_errors;
996 	}
997 }
998 
999 /**
1000  * i40e_update_pf_stats - Update the PF statistics counters.
1001  * @pf: the PF to be updated
1002  **/
i40e_update_pf_stats(struct i40e_pf * pf)1003 static void i40e_update_pf_stats(struct i40e_pf *pf)
1004 {
1005 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1006 	struct i40e_hw_port_stats *nsd = &pf->stats;
1007 	struct i40e_hw *hw = &pf->hw;
1008 	u32 val;
1009 	int i;
1010 
1011 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1012 			   I40E_GLPRT_GORCL(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1015 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1016 			   I40E_GLPRT_GOTCL(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1019 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1020 			   pf->stat_offsets_loaded,
1021 			   &osd->eth.rx_discards,
1022 			   &nsd->eth.rx_discards);
1023 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1024 			   I40E_GLPRT_UPRCL(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->eth.rx_unicast,
1027 			   &nsd->eth.rx_unicast);
1028 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1029 			   I40E_GLPRT_MPRCL(hw->port),
1030 			   pf->stat_offsets_loaded,
1031 			   &osd->eth.rx_multicast,
1032 			   &nsd->eth.rx_multicast);
1033 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1034 			   I40E_GLPRT_BPRCL(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->eth.rx_broadcast,
1037 			   &nsd->eth.rx_broadcast);
1038 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1039 			   I40E_GLPRT_UPTCL(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->eth.tx_unicast,
1042 			   &nsd->eth.tx_unicast);
1043 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1044 			   I40E_GLPRT_MPTCL(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->eth.tx_multicast,
1047 			   &nsd->eth.tx_multicast);
1048 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1049 			   I40E_GLPRT_BPTCL(hw->port),
1050 			   pf->stat_offsets_loaded,
1051 			   &osd->eth.tx_broadcast,
1052 			   &nsd->eth.tx_broadcast);
1053 
1054 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1055 			   pf->stat_offsets_loaded,
1056 			   &osd->tx_dropped_link_down,
1057 			   &nsd->tx_dropped_link_down);
1058 
1059 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->crc_errors, &nsd->crc_errors);
1062 
1063 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->mac_local_faults,
1070 			   &nsd->mac_local_faults);
1071 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->mac_remote_faults,
1074 			   &nsd->mac_remote_faults);
1075 
1076 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->rx_length_errors,
1079 			   &nsd->rx_length_errors);
1080 
1081 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1082 			   pf->stat_offsets_loaded,
1083 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1084 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1087 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1088 			   pf->stat_offsets_loaded,
1089 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1090 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1091 			   pf->stat_offsets_loaded,
1092 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1093 
1094 	for (i = 0; i < 8; i++) {
1095 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1096 				   pf->stat_offsets_loaded,
1097 				   &osd->priority_xoff_rx[i],
1098 				   &nsd->priority_xoff_rx[i]);
1099 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1100 				   pf->stat_offsets_loaded,
1101 				   &osd->priority_xon_rx[i],
1102 				   &nsd->priority_xon_rx[i]);
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xon_tx[i],
1106 				   &nsd->priority_xon_tx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xoff_tx[i],
1110 				   &nsd->priority_xoff_tx[i]);
1111 		i40e_stat_update32(hw,
1112 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1113 				   pf->stat_offsets_loaded,
1114 				   &osd->priority_xon_2_xoff[i],
1115 				   &nsd->priority_xon_2_xoff[i]);
1116 	}
1117 
1118 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1119 			   I40E_GLPRT_PRC64L(hw->port),
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->rx_size_64, &nsd->rx_size_64);
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1123 			   I40E_GLPRT_PRC127L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_127, &nsd->rx_size_127);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1127 			   I40E_GLPRT_PRC255L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_255, &nsd->rx_size_255);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1131 			   I40E_GLPRT_PRC511L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_511, &nsd->rx_size_511);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1135 			   I40E_GLPRT_PRC1023L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1139 			   I40E_GLPRT_PRC1522L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1143 			   I40E_GLPRT_PRC9522L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_big, &nsd->rx_size_big);
1146 
1147 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1148 			   I40E_GLPRT_PTC64L(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->tx_size_64, &nsd->tx_size_64);
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1152 			   I40E_GLPRT_PTC127L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_127, &nsd->tx_size_127);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1156 			   I40E_GLPRT_PTC255L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_255, &nsd->tx_size_255);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1160 			   I40E_GLPRT_PTC511L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_511, &nsd->tx_size_511);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1164 			   I40E_GLPRT_PTC1023L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1168 			   I40E_GLPRT_PTC1522L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1172 			   I40E_GLPRT_PTC9522L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_big, &nsd->tx_size_big);
1175 
1176 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->rx_undersize, &nsd->rx_undersize);
1179 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1180 			   pf->stat_offsets_loaded,
1181 			   &osd->rx_fragments, &nsd->rx_fragments);
1182 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1183 			   pf->stat_offsets_loaded,
1184 			   &osd->rx_oversize, &nsd->rx_oversize);
1185 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1186 			   pf->stat_offsets_loaded,
1187 			   &osd->rx_jabber, &nsd->rx_jabber);
1188 
1189 	/* FDIR stats */
1190 	i40e_stat_update_and_clear32(hw,
1191 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1192 			&nsd->fd_atr_match);
1193 	i40e_stat_update_and_clear32(hw,
1194 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1195 			&nsd->fd_sb_match);
1196 	i40e_stat_update_and_clear32(hw,
1197 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1198 			&nsd->fd_atr_tunnel_match);
1199 
1200 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1201 	nsd->tx_lpi_status =
1202 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1203 	nsd->rx_lpi_status =
1204 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1205 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1206 			   pf->stat_offsets_loaded,
1207 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1208 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1209 			   pf->stat_offsets_loaded,
1210 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1211 
1212 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1213 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1214 		nsd->fd_sb_status = true;
1215 	else
1216 		nsd->fd_sb_status = false;
1217 
1218 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1219 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1220 		nsd->fd_atr_status = true;
1221 	else
1222 		nsd->fd_atr_status = false;
1223 
1224 	pf->stat_offsets_loaded = true;
1225 }
1226 
1227 /**
1228  * i40e_update_stats - Update the various statistics counters.
1229  * @vsi: the VSI to be updated
1230  *
1231  * Update the various stats for this VSI and its related entities.
1232  **/
i40e_update_stats(struct i40e_vsi * vsi)1233 void i40e_update_stats(struct i40e_vsi *vsi)
1234 {
1235 	struct i40e_pf *pf = vsi->back;
1236 
1237 	if (vsi->type == I40E_VSI_MAIN)
1238 		i40e_update_pf_stats(pf);
1239 
1240 	i40e_update_vsi_stats(vsi);
1241 }
1242 
1243 /**
1244  * i40e_count_all_filters - counts VSI MAC filters
1245  * @vsi: the VSI to be searched
1246  *
1247  * Return: count of MAC filters in any state.
1248  */
i40e_count_all_filters(struct i40e_vsi * vsi)1249 int i40e_count_all_filters(struct i40e_vsi *vsi)
1250 {
1251 	struct i40e_mac_filter *f;
1252 	struct hlist_node *h;
1253 	int bkt, cnt = 0;
1254 
1255 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1256 		cnt++;
1257 
1258 	return cnt;
1259 }
1260 
1261 /**
1262  * i40e_count_active_filters - counts VSI MAC filters
1263  * @vsi: the VSI to be searched
1264  *
1265  * Return: count of active MAC filters.
1266  */
i40e_count_active_filters(struct i40e_vsi * vsi)1267 int i40e_count_active_filters(struct i40e_vsi *vsi)
1268 {
1269 	struct i40e_mac_filter *f;
1270 	struct hlist_node *h;
1271 	int bkt;
1272 	int cnt = 0;
1273 
1274 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1275 		if (f->state == I40E_FILTER_NEW ||
1276 		    f->state == I40E_FILTER_NEW_SYNC ||
1277 		    f->state == I40E_FILTER_ACTIVE)
1278 			++cnt;
1279 	}
1280 
1281 	return cnt;
1282 }
1283 
1284 /**
1285  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1286  * @vsi: the VSI to be searched
1287  * @macaddr: the MAC address
1288  * @vlan: the vlan
1289  *
1290  * Returns ptr to the filter object or NULL
1291  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1292 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1293 						const u8 *macaddr, s16 vlan)
1294 {
1295 	struct i40e_mac_filter *f;
1296 	u64 key;
1297 
1298 	if (!vsi || !macaddr)
1299 		return NULL;
1300 
1301 	key = i40e_addr_to_hkey(macaddr);
1302 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1303 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1304 		    (vlan == f->vlan))
1305 			return f;
1306 	}
1307 	return NULL;
1308 }
1309 
1310 /**
1311  * i40e_find_mac - Find a mac addr in the macvlan filters list
1312  * @vsi: the VSI to be searched
1313  * @macaddr: the MAC address we are searching for
1314  *
1315  * Returns the first filter with the provided MAC address or NULL if
1316  * MAC address was not found
1317  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1318 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1319 {
1320 	struct i40e_mac_filter *f;
1321 	u64 key;
1322 
1323 	if (!vsi || !macaddr)
1324 		return NULL;
1325 
1326 	key = i40e_addr_to_hkey(macaddr);
1327 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1328 		if ((ether_addr_equal(macaddr, f->macaddr)))
1329 			return f;
1330 	}
1331 	return NULL;
1332 }
1333 
1334 /**
1335  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1336  * @vsi: the VSI to be searched
1337  *
1338  * Returns true if VSI is in vlan mode or false otherwise
1339  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1340 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1341 {
1342 	/* If we have a PVID, always operate in VLAN mode */
1343 	if (vsi->info.pvid)
1344 		return true;
1345 
1346 	/* We need to operate in VLAN mode whenever we have any filters with
1347 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1348 	 * time, incurring search cost repeatedly. However, we can notice two
1349 	 * things:
1350 	 *
1351 	 * 1) the only place where we can gain a VLAN filter is in
1352 	 *    i40e_add_filter.
1353 	 *
1354 	 * 2) the only place where filters are actually removed is in
1355 	 *    i40e_sync_filters_subtask.
1356 	 *
1357 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1358 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1359 	 * we have to perform the full search after deleting filters in
1360 	 * i40e_sync_filters_subtask, but we already have to search
1361 	 * filters here and can perform the check at the same time. This
1362 	 * results in avoiding embedding a loop for VLAN mode inside another
1363 	 * loop over all the filters, and should maintain correctness as noted
1364 	 * above.
1365 	 */
1366 	return vsi->has_vlan_filter;
1367 }
1368 
1369 /**
1370  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1371  * @vsi: the VSI to configure
1372  * @tmp_add_list: list of filters ready to be added
1373  * @tmp_del_list: list of filters ready to be deleted
1374  * @vlan_filters: the number of active VLAN filters
1375  *
1376  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1377  * behave as expected. If we have any active VLAN filters remaining or about
1378  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1379  * so that they only match against untagged traffic. If we no longer have any
1380  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1381  * so that they match against both tagged and untagged traffic. In this way,
1382  * we ensure that we correctly receive the desired traffic. This ensures that
1383  * when we have an active VLAN we will receive only untagged traffic and
1384  * traffic matching active VLANs. If we have no active VLANs then we will
1385  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1386  *
1387  * Finally, in a similar fashion, this function also corrects filters when
1388  * there is an active PVID assigned to this VSI.
1389  *
1390  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1391  *
1392  * This function is only expected to be called from within
1393  * i40e_sync_vsi_filters.
1394  *
1395  * NOTE: This function expects to be called while under the
1396  * mac_filter_hash_lock
1397  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1398 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1399 					 struct hlist_head *tmp_add_list,
1400 					 struct hlist_head *tmp_del_list,
1401 					 int vlan_filters)
1402 {
1403 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1404 	struct i40e_mac_filter *f, *add_head;
1405 	struct i40e_new_mac_filter *new;
1406 	struct hlist_node *h;
1407 	int bkt, new_vlan;
1408 
1409 	/* To determine if a particular filter needs to be replaced we
1410 	 * have the three following conditions:
1411 	 *
1412 	 * a) if we have a PVID assigned, then all filters which are
1413 	 *    not marked as VLAN=PVID must be replaced with filters that
1414 	 *    are.
1415 	 * b) otherwise, if we have any active VLANS, all filters
1416 	 *    which are marked as VLAN=-1 must be replaced with
1417 	 *    filters marked as VLAN=0
1418 	 * c) finally, if we do not have any active VLANS, all filters
1419 	 *    which are marked as VLAN=0 must be replaced with filters
1420 	 *    marked as VLAN=-1
1421 	 */
1422 
1423 	/* Update the filters about to be added in place */
1424 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1425 		if (pvid && new->f->vlan != pvid)
1426 			new->f->vlan = pvid;
1427 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1428 			new->f->vlan = 0;
1429 		else if (!vlan_filters && new->f->vlan == 0)
1430 			new->f->vlan = I40E_VLAN_ANY;
1431 	}
1432 
1433 	/* Update the remaining active filters */
1434 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1435 		/* Combine the checks for whether a filter needs to be changed
1436 		 * and then determine the new VLAN inside the if block, in
1437 		 * order to avoid duplicating code for adding the new filter
1438 		 * then deleting the old filter.
1439 		 */
1440 		if ((pvid && f->vlan != pvid) ||
1441 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1442 		    (!vlan_filters && f->vlan == 0)) {
1443 			/* Determine the new vlan we will be adding */
1444 			if (pvid)
1445 				new_vlan = pvid;
1446 			else if (vlan_filters)
1447 				new_vlan = 0;
1448 			else
1449 				new_vlan = I40E_VLAN_ANY;
1450 
1451 			/* Create the new filter */
1452 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1453 			if (!add_head)
1454 				return -ENOMEM;
1455 
1456 			/* Create a temporary i40e_new_mac_filter */
1457 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1458 			if (!new)
1459 				return -ENOMEM;
1460 
1461 			new->f = add_head;
1462 			new->state = add_head->state;
1463 			if (add_head->state == I40E_FILTER_NEW)
1464 				add_head->state = I40E_FILTER_NEW_SYNC;
1465 
1466 			/* Add the new filter to the tmp list */
1467 			hlist_add_head(&new->hlist, tmp_add_list);
1468 
1469 			/* Put the original filter into the delete list */
1470 			f->state = I40E_FILTER_REMOVE;
1471 			hash_del(&f->hlist);
1472 			hlist_add_head(&f->hlist, tmp_del_list);
1473 		}
1474 	}
1475 
1476 	vsi->has_vlan_filter = !!vlan_filters;
1477 
1478 	return 0;
1479 }
1480 
1481 /**
1482  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1483  * @vsi: the vsi to configure
1484  * @new_mac: new mac filter to be added
1485  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1486  * @vlan_filters: the number of active VLAN filters
1487  * @trusted: flag if the VF is trusted
1488  *
1489  * Get new VLAN id based on current VLAN filters, trust, PVID
1490  * and vf-vlan-prune-disable flag.
1491  *
1492  * Returns the value of the new vlan filter or
1493  * the old value if no new filter is needed.
1494  */
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)1495 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1496 				struct i40e_new_mac_filter *new_mac,
1497 				struct i40e_mac_filter *f,
1498 				int vlan_filters,
1499 				bool trusted)
1500 {
1501 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1502 	struct i40e_pf *pf = vsi->back;
1503 	bool is_any;
1504 
1505 	if (new_mac)
1506 		f = new_mac->f;
1507 
1508 	if (pvid && f->vlan != pvid)
1509 		return pvid;
1510 
1511 	is_any = (trusted ||
1512 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1513 
1514 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1515 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1516 	    (is_any && !vlan_filters && f->vlan == 0)) {
1517 		if (is_any)
1518 			return I40E_VLAN_ANY;
1519 		else
1520 			return 0;
1521 	}
1522 
1523 	return f->vlan;
1524 }
1525 
1526 /**
1527  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1528  * @vsi: the vsi to configure
1529  * @tmp_add_list: list of filters ready to be added
1530  * @tmp_del_list: list of filters ready to be deleted
1531  * @vlan_filters: the number of active VLAN filters
1532  * @trusted: flag if the VF is trusted
1533  *
1534  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1535  * and vf-vlan-prune-disable flag.
1536  *
1537  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1538  *
1539  * This function is only expected to be called from within
1540  * i40e_sync_vsi_filters.
1541  *
1542  * NOTE: This function expects to be called while under the
1543  * mac_filter_hash_lock
1544  */
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)1545 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1546 					    struct hlist_head *tmp_add_list,
1547 					    struct hlist_head *tmp_del_list,
1548 					    int vlan_filters,
1549 					    bool trusted)
1550 {
1551 	struct i40e_mac_filter *f, *add_head;
1552 	struct i40e_new_mac_filter *new_mac;
1553 	struct hlist_node *h;
1554 	int bkt, new_vlan;
1555 
1556 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1557 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1558 							vlan_filters, trusted);
1559 	}
1560 
1561 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1562 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1563 						trusted);
1564 		if (new_vlan != f->vlan) {
1565 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1566 			if (!add_head)
1567 				return -ENOMEM;
1568 			/* Create a temporary i40e_new_mac_filter */
1569 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1570 			if (!new_mac)
1571 				return -ENOMEM;
1572 			new_mac->f = add_head;
1573 			new_mac->state = add_head->state;
1574 			if (add_head->state == I40E_FILTER_NEW)
1575 				add_head->state = I40E_FILTER_NEW_SYNC;
1576 
1577 			/* Add the new filter to the tmp list */
1578 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1579 
1580 			/* Put the original filter into the delete list */
1581 			f->state = I40E_FILTER_REMOVE;
1582 			hash_del(&f->hlist);
1583 			hlist_add_head(&f->hlist, tmp_del_list);
1584 		}
1585 	}
1586 
1587 	vsi->has_vlan_filter = !!vlan_filters;
1588 	return 0;
1589 }
1590 
1591 /**
1592  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1593  * @vsi: the PF Main VSI - inappropriate for any other VSI
1594  * @macaddr: the MAC address
1595  *
1596  * Remove whatever filter the firmware set up so the driver can manage
1597  * its own filtering intelligently.
1598  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1599 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1600 {
1601 	struct i40e_aqc_remove_macvlan_element_data element;
1602 	struct i40e_pf *pf = vsi->back;
1603 
1604 	/* Only appropriate for the PF main VSI */
1605 	if (vsi->type != I40E_VSI_MAIN)
1606 		return;
1607 
1608 	memset(&element, 0, sizeof(element));
1609 	ether_addr_copy(element.mac_addr, macaddr);
1610 	element.vlan_tag = 0;
1611 	/* Ignore error returns, some firmware does it this way... */
1612 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1613 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1614 
1615 	memset(&element, 0, sizeof(element));
1616 	ether_addr_copy(element.mac_addr, macaddr);
1617 	element.vlan_tag = 0;
1618 	/* ...and some firmware does it this way. */
1619 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1620 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1621 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1622 }
1623 
1624 /**
1625  * i40e_add_filter - Add a mac/vlan filter to the VSI
1626  * @vsi: the VSI to be searched
1627  * @macaddr: the MAC address
1628  * @vlan: the vlan
1629  *
1630  * Returns ptr to the filter object or NULL when no memory available.
1631  *
1632  * NOTE: This function is expected to be called with mac_filter_hash_lock
1633  * being held.
1634  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1635 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1636 					const u8 *macaddr, s16 vlan)
1637 {
1638 	struct i40e_mac_filter *f;
1639 	u64 key;
1640 
1641 	if (!vsi || !macaddr)
1642 		return NULL;
1643 
1644 	f = i40e_find_filter(vsi, macaddr, vlan);
1645 	if (!f) {
1646 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1647 		if (!f)
1648 			return NULL;
1649 
1650 		/* Update the boolean indicating if we need to function in
1651 		 * VLAN mode.
1652 		 */
1653 		if (vlan >= 0)
1654 			vsi->has_vlan_filter = true;
1655 
1656 		ether_addr_copy(f->macaddr, macaddr);
1657 		f->vlan = vlan;
1658 		f->state = I40E_FILTER_NEW;
1659 		INIT_HLIST_NODE(&f->hlist);
1660 
1661 		key = i40e_addr_to_hkey(macaddr);
1662 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1663 
1664 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1665 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1666 	}
1667 
1668 	/* If we're asked to add a filter that has been marked for removal, it
1669 	 * is safe to simply restore it to active state. __i40e_del_filter
1670 	 * will have simply deleted any filters which were previously marked
1671 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1672 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1673 	 * task, just restore this filter to the ACTIVE state so that the
1674 	 * sync task leaves it in place
1675 	 */
1676 	if (f->state == I40E_FILTER_REMOVE)
1677 		f->state = I40E_FILTER_ACTIVE;
1678 
1679 	return f;
1680 }
1681 
1682 /**
1683  * __i40e_del_filter - Remove a specific filter from the VSI
1684  * @vsi: VSI to remove from
1685  * @f: the filter to remove from the list
1686  *
1687  * This function should be called instead of i40e_del_filter only if you know
1688  * the exact filter you will remove already, such as via i40e_find_filter or
1689  * i40e_find_mac.
1690  *
1691  * NOTE: This function is expected to be called with mac_filter_hash_lock
1692  * being held.
1693  * ANOTHER NOTE: This function MUST be called from within the context of
1694  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1695  * instead of list_for_each_entry().
1696  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1697 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1698 {
1699 	if (!f)
1700 		return;
1701 
1702 	/* If the filter was never added to firmware then we can just delete it
1703 	 * directly and we don't want to set the status to remove or else an
1704 	 * admin queue command will unnecessarily fire.
1705 	 */
1706 	if ((f->state == I40E_FILTER_FAILED) ||
1707 	    (f->state == I40E_FILTER_NEW)) {
1708 		hash_del(&f->hlist);
1709 		kfree(f);
1710 	} else {
1711 		f->state = I40E_FILTER_REMOVE;
1712 	}
1713 
1714 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1715 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1716 }
1717 
1718 /**
1719  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1720  * @vsi: the VSI to be searched
1721  * @macaddr: the MAC address
1722  * @vlan: the VLAN
1723  *
1724  * NOTE: This function is expected to be called with mac_filter_hash_lock
1725  * being held.
1726  * ANOTHER NOTE: This function MUST be called from within the context of
1727  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1728  * instead of list_for_each_entry().
1729  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1730 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1731 {
1732 	struct i40e_mac_filter *f;
1733 
1734 	if (!vsi || !macaddr)
1735 		return;
1736 
1737 	f = i40e_find_filter(vsi, macaddr, vlan);
1738 	__i40e_del_filter(vsi, f);
1739 }
1740 
1741 /**
1742  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1743  * @vsi: the VSI to be searched
1744  * @macaddr: the mac address to be filtered
1745  *
1746  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1747  * go through all the macvlan filters and add a macvlan filter for each
1748  * unique vlan that already exists. If a PVID has been assigned, instead only
1749  * add the macaddr to that VLAN.
1750  *
1751  * Returns last filter added on success, else NULL
1752  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1753 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1754 					    const u8 *macaddr)
1755 {
1756 	struct i40e_mac_filter *f, *add = NULL;
1757 	struct hlist_node *h;
1758 	int bkt;
1759 
1760 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1761 	if (vsi->info.pvid)
1762 		return i40e_add_filter(vsi, macaddr,
1763 				       le16_to_cpu(vsi->info.pvid));
1764 
1765 	if (!i40e_is_vsi_in_vlan(vsi))
1766 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1767 
1768 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1769 		if (f->state == I40E_FILTER_REMOVE)
1770 			continue;
1771 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1772 		if (!add)
1773 			return NULL;
1774 	}
1775 
1776 	return add;
1777 }
1778 
1779 /**
1780  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1781  * @vsi: the VSI to be searched
1782  * @macaddr: the mac address to be removed
1783  *
1784  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1785  * associated with.
1786  *
1787  * Returns 0 for success, or error
1788  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1789 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1790 {
1791 	struct i40e_mac_filter *f;
1792 	struct hlist_node *h;
1793 	bool found = false;
1794 	int bkt;
1795 
1796 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1797 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1798 		if (ether_addr_equal(macaddr, f->macaddr)) {
1799 			__i40e_del_filter(vsi, f);
1800 			found = true;
1801 		}
1802 	}
1803 
1804 	if (found)
1805 		return 0;
1806 	else
1807 		return -ENOENT;
1808 }
1809 
1810 /**
1811  * i40e_set_mac - NDO callback to set mac address
1812  * @netdev: network interface device structure
1813  * @p: pointer to an address structure
1814  *
1815  * Returns 0 on success, negative on failure
1816  **/
i40e_set_mac(struct net_device * netdev,void * p)1817 static int i40e_set_mac(struct net_device *netdev, void *p)
1818 {
1819 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1820 	struct i40e_vsi *vsi = np->vsi;
1821 	struct i40e_pf *pf = vsi->back;
1822 	struct i40e_hw *hw = &pf->hw;
1823 	struct sockaddr *addr = p;
1824 
1825 	if (!is_valid_ether_addr(addr->sa_data))
1826 		return -EADDRNOTAVAIL;
1827 
1828 	if (test_bit(__I40E_DOWN, pf->state) ||
1829 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1830 		return -EADDRNOTAVAIL;
1831 
1832 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1833 		netdev_info(netdev, "returning to hw mac address %pM\n",
1834 			    hw->mac.addr);
1835 	else
1836 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1837 
1838 	/* Copy the address first, so that we avoid a possible race with
1839 	 * .set_rx_mode().
1840 	 * - Remove old address from MAC filter
1841 	 * - Copy new address
1842 	 * - Add new address to MAC filter
1843 	 */
1844 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1845 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1846 	eth_hw_addr_set(netdev, addr->sa_data);
1847 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1848 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1849 
1850 	if (vsi->type == I40E_VSI_MAIN) {
1851 		int ret;
1852 
1853 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1854 						addr->sa_data, NULL);
1855 		if (ret)
1856 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1857 				    ERR_PTR(ret),
1858 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1859 	}
1860 
1861 	/* schedule our worker thread which will take care of
1862 	 * applying the new filter changes
1863 	 */
1864 	i40e_service_event_schedule(pf);
1865 	return 0;
1866 }
1867 
1868 /**
1869  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1870  * @vsi: vsi structure
1871  * @seed: RSS hash seed
1872  * @lut: pointer to lookup table of lut_size
1873  * @lut_size: size of the lookup table
1874  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1875 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1876 			      u8 *lut, u16 lut_size)
1877 {
1878 	struct i40e_pf *pf = vsi->back;
1879 	struct i40e_hw *hw = &pf->hw;
1880 	int ret = 0;
1881 
1882 	if (seed) {
1883 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1884 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1885 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1886 		if (ret) {
1887 			dev_info(&pf->pdev->dev,
1888 				 "Cannot set RSS key, err %pe aq_err %s\n",
1889 				 ERR_PTR(ret),
1890 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1891 			return ret;
1892 		}
1893 	}
1894 	if (lut) {
1895 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1896 
1897 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1898 		if (ret) {
1899 			dev_info(&pf->pdev->dev,
1900 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1901 				 ERR_PTR(ret),
1902 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1903 			return ret;
1904 		}
1905 	}
1906 	return ret;
1907 }
1908 
1909 /**
1910  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1911  * @vsi: VSI structure
1912  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1913 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1914 {
1915 	struct i40e_pf *pf = vsi->back;
1916 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1917 	u8 *lut;
1918 	int ret;
1919 
1920 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1921 		return 0;
1922 	if (!vsi->rss_size)
1923 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1924 				      vsi->num_queue_pairs);
1925 	if (!vsi->rss_size)
1926 		return -EINVAL;
1927 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1928 	if (!lut)
1929 		return -ENOMEM;
1930 
1931 	/* Use the user configured hash keys and lookup table if there is one,
1932 	 * otherwise use default
1933 	 */
1934 	if (vsi->rss_lut_user)
1935 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1936 	else
1937 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1938 	if (vsi->rss_hkey_user)
1939 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1940 	else
1941 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1942 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1943 	kfree(lut);
1944 	return ret;
1945 }
1946 
1947 /**
1948  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1949  * @vsi: the VSI being configured,
1950  * @ctxt: VSI context structure
1951  * @enabled_tc: number of traffic classes to enable
1952  *
1953  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1954  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1955 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1956 					   struct i40e_vsi_context *ctxt,
1957 					   u8 enabled_tc)
1958 {
1959 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1960 	int i, override_q, pow, num_qps, ret;
1961 	u8 netdev_tc = 0, offset = 0;
1962 
1963 	if (vsi->type != I40E_VSI_MAIN)
1964 		return -EINVAL;
1965 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1966 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1967 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1968 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1969 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1970 
1971 	/* find the next higher power-of-2 of num queue pairs */
1972 	pow = ilog2(num_qps);
1973 	if (!is_power_of_2(num_qps))
1974 		pow++;
1975 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1976 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1977 
1978 	/* Setup queue offset/count for all TCs for given VSI */
1979 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1980 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1981 		/* See if the given TC is enabled for the given VSI */
1982 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1983 			offset = vsi->mqprio_qopt.qopt.offset[i];
1984 			qcount = vsi->mqprio_qopt.qopt.count[i];
1985 			if (qcount > max_qcount)
1986 				max_qcount = qcount;
1987 			vsi->tc_config.tc_info[i].qoffset = offset;
1988 			vsi->tc_config.tc_info[i].qcount = qcount;
1989 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1990 		} else {
1991 			/* TC is not enabled so set the offset to
1992 			 * default queue and allocate one queue
1993 			 * for the given TC.
1994 			 */
1995 			vsi->tc_config.tc_info[i].qoffset = 0;
1996 			vsi->tc_config.tc_info[i].qcount = 1;
1997 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1998 		}
1999 	}
2000 
2001 	/* Set actual Tx/Rx queue pairs */
2002 	vsi->num_queue_pairs = offset + qcount;
2003 
2004 	/* Setup queue TC[0].qmap for given VSI context */
2005 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
2006 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2007 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2008 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2009 
2010 	/* Reconfigure RSS for main VSI with max queue count */
2011 	vsi->rss_size = max_qcount;
2012 	ret = i40e_vsi_config_rss(vsi);
2013 	if (ret) {
2014 		dev_info(&vsi->back->pdev->dev,
2015 			 "Failed to reconfig rss for num_queues (%u)\n",
2016 			 max_qcount);
2017 		return ret;
2018 	}
2019 	vsi->reconfig_rss = true;
2020 	dev_dbg(&vsi->back->pdev->dev,
2021 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2022 
2023 	/* Find queue count available for channel VSIs and starting offset
2024 	 * for channel VSIs
2025 	 */
2026 	override_q = vsi->mqprio_qopt.qopt.count[0];
2027 	if (override_q && override_q < vsi->num_queue_pairs) {
2028 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2029 		vsi->next_base_queue = override_q;
2030 	}
2031 	return 0;
2032 }
2033 
2034 /**
2035  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2036  * @vsi: the VSI being setup
2037  * @ctxt: VSI context structure
2038  * @enabled_tc: Enabled TCs bitmap
2039  * @is_add: True if called before Add VSI
2040  *
2041  * Setup VSI queue mapping for enabled traffic classes.
2042  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2043 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2044 				     struct i40e_vsi_context *ctxt,
2045 				     u8 enabled_tc,
2046 				     bool is_add)
2047 {
2048 	struct i40e_pf *pf = vsi->back;
2049 	u16 num_tc_qps = 0;
2050 	u16 sections = 0;
2051 	u8 netdev_tc = 0;
2052 	u16 numtc = 1;
2053 	u16 qcount;
2054 	u8 offset;
2055 	u16 qmap;
2056 	int i;
2057 
2058 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2059 	offset = 0;
2060 	/* zero out queue mapping, it will get updated on the end of the function */
2061 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2062 
2063 	if (vsi->type == I40E_VSI_MAIN) {
2064 		/* This code helps add more queue to the VSI if we have
2065 		 * more cores than RSS can support, the higher cores will
2066 		 * be served by ATR or other filters. Furthermore, the
2067 		 * non-zero req_queue_pairs says that user requested a new
2068 		 * queue count via ethtool's set_channels, so use this
2069 		 * value for queues distribution across traffic classes
2070 		 * We need at least one queue pair for the interface
2071 		 * to be usable as we see in else statement.
2072 		 */
2073 		if (vsi->req_queue_pairs > 0)
2074 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2075 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2076 			vsi->num_queue_pairs = pf->num_lan_msix;
2077 		else
2078 			vsi->num_queue_pairs = 1;
2079 	}
2080 
2081 	/* Number of queues per enabled TC */
2082 	if (vsi->type == I40E_VSI_MAIN ||
2083 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2084 		num_tc_qps = vsi->num_queue_pairs;
2085 	else
2086 		num_tc_qps = vsi->alloc_queue_pairs;
2087 
2088 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2089 		/* Find numtc from enabled TC bitmap */
2090 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2091 			if (enabled_tc & BIT(i)) /* TC is enabled */
2092 				numtc++;
2093 		}
2094 		if (!numtc) {
2095 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2096 			numtc = 1;
2097 		}
2098 		num_tc_qps = num_tc_qps / numtc;
2099 		num_tc_qps = min_t(int, num_tc_qps,
2100 				   i40e_pf_get_max_q_per_tc(pf));
2101 	}
2102 
2103 	vsi->tc_config.numtc = numtc;
2104 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2105 
2106 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2107 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2108 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2109 
2110 	/* Setup queue offset/count for all TCs for given VSI */
2111 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2112 		/* See if the given TC is enabled for the given VSI */
2113 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2114 			/* TC is enabled */
2115 			int pow, num_qps;
2116 
2117 			switch (vsi->type) {
2118 			case I40E_VSI_MAIN:
2119 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2120 					       pf->flags) &&
2121 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2122 					       pf->flags)) ||
2123 				    vsi->tc_config.enabled_tc != 1) {
2124 					qcount = min_t(int, pf->alloc_rss_size,
2125 						       num_tc_qps);
2126 					break;
2127 				}
2128 				fallthrough;
2129 			case I40E_VSI_FDIR:
2130 			case I40E_VSI_SRIOV:
2131 			case I40E_VSI_VMDQ2:
2132 			default:
2133 				qcount = num_tc_qps;
2134 				WARN_ON(i != 0);
2135 				break;
2136 			}
2137 			vsi->tc_config.tc_info[i].qoffset = offset;
2138 			vsi->tc_config.tc_info[i].qcount = qcount;
2139 
2140 			/* find the next higher power-of-2 of num queue pairs */
2141 			num_qps = qcount;
2142 			pow = 0;
2143 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2144 				pow++;
2145 				num_qps >>= 1;
2146 			}
2147 
2148 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2149 			qmap =
2150 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2151 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2152 
2153 			offset += qcount;
2154 		} else {
2155 			/* TC is not enabled so set the offset to
2156 			 * default queue and allocate one queue
2157 			 * for the given TC.
2158 			 */
2159 			vsi->tc_config.tc_info[i].qoffset = 0;
2160 			vsi->tc_config.tc_info[i].qcount = 1;
2161 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2162 
2163 			qmap = 0;
2164 		}
2165 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2166 	}
2167 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2168 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2169 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2170 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2171 		vsi->num_queue_pairs = offset;
2172 
2173 	/* Scheduler section valid can only be set for ADD VSI */
2174 	if (is_add) {
2175 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2176 
2177 		ctxt->info.up_enable_bits = enabled_tc;
2178 	}
2179 	if (vsi->type == I40E_VSI_SRIOV) {
2180 		ctxt->info.mapping_flags |=
2181 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2182 		for (i = 0; i < vsi->num_queue_pairs; i++)
2183 			ctxt->info.queue_mapping[i] =
2184 					       cpu_to_le16(vsi->base_queue + i);
2185 	} else {
2186 		ctxt->info.mapping_flags |=
2187 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2188 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2189 	}
2190 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2191 }
2192 
2193 /**
2194  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2195  * @netdev: the netdevice
2196  * @addr: address to add
2197  *
2198  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2199  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2200  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2201 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2202 {
2203 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2204 	struct i40e_vsi *vsi = np->vsi;
2205 
2206 	if (i40e_add_mac_filter(vsi, addr))
2207 		return 0;
2208 	else
2209 		return -ENOMEM;
2210 }
2211 
2212 /**
2213  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2214  * @netdev: the netdevice
2215  * @addr: address to add
2216  *
2217  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2218  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2219  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2220 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2221 {
2222 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2223 	struct i40e_vsi *vsi = np->vsi;
2224 
2225 	/* Under some circumstances, we might receive a request to delete
2226 	 * our own device address from our uc list. Because we store the
2227 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2228 	 * such requests and not delete our device address from this list.
2229 	 */
2230 	if (ether_addr_equal(addr, netdev->dev_addr))
2231 		return 0;
2232 
2233 	i40e_del_mac_filter(vsi, addr);
2234 
2235 	return 0;
2236 }
2237 
2238 /**
2239  * i40e_set_rx_mode - NDO callback to set the netdev filters
2240  * @netdev: network interface device structure
2241  **/
i40e_set_rx_mode(struct net_device * netdev)2242 static void i40e_set_rx_mode(struct net_device *netdev)
2243 {
2244 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2245 	struct i40e_vsi *vsi = np->vsi;
2246 
2247 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2248 
2249 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2250 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2251 
2252 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2253 
2254 	/* check for other flag changes */
2255 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2256 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2257 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2258 	}
2259 }
2260 
2261 /**
2262  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2263  * @vsi: Pointer to VSI struct
2264  * @from: Pointer to list which contains MAC filter entries - changes to
2265  *        those entries needs to be undone.
2266  *
2267  * MAC filter entries from this list were slated for deletion.
2268  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2269 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2270 					 struct hlist_head *from)
2271 {
2272 	struct i40e_mac_filter *f;
2273 	struct hlist_node *h;
2274 
2275 	hlist_for_each_entry_safe(f, h, from, hlist) {
2276 		u64 key = i40e_addr_to_hkey(f->macaddr);
2277 
2278 		/* Move the element back into MAC filter list*/
2279 		hlist_del(&f->hlist);
2280 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2281 	}
2282 }
2283 
2284 /**
2285  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2286  * @vsi: Pointer to vsi struct
2287  * @from: Pointer to list which contains MAC filter entries - changes to
2288  *        those entries needs to be undone.
2289  *
2290  * MAC filter entries from this list were slated for addition.
2291  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2292 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2293 					 struct hlist_head *from)
2294 {
2295 	struct i40e_new_mac_filter *new;
2296 	struct hlist_node *h;
2297 
2298 	hlist_for_each_entry_safe(new, h, from, hlist) {
2299 		/* We can simply free the wrapper structure */
2300 		hlist_del(&new->hlist);
2301 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2302 		kfree(new);
2303 	}
2304 }
2305 
2306 /**
2307  * i40e_next_filter - Get the next non-broadcast filter from a list
2308  * @next: pointer to filter in list
2309  *
2310  * Returns the next non-broadcast filter in the list. Required so that we
2311  * ignore broadcast filters within the list, since these are not handled via
2312  * the normal firmware update path.
2313  */
2314 static
i40e_next_filter(struct i40e_new_mac_filter * next)2315 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2316 {
2317 	hlist_for_each_entry_continue(next, hlist) {
2318 		if (!is_broadcast_ether_addr(next->f->macaddr))
2319 			return next;
2320 	}
2321 
2322 	return NULL;
2323 }
2324 
2325 /**
2326  * i40e_update_filter_state - Update filter state based on return data
2327  * from firmware
2328  * @count: Number of filters added
2329  * @add_list: return data from fw
2330  * @add_head: pointer to first filter in current batch
2331  *
2332  * MAC filter entries from list were slated to be added to device. Returns
2333  * number of successful filters. Note that 0 does NOT mean success!
2334  **/
2335 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2336 i40e_update_filter_state(int count,
2337 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2338 			 struct i40e_new_mac_filter *add_head)
2339 {
2340 	int retval = 0;
2341 	int i;
2342 
2343 	for (i = 0; i < count; i++) {
2344 		/* Always check status of each filter. We don't need to check
2345 		 * the firmware return status because we pre-set the filter
2346 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2347 		 * request to the adminq. Thus, if it no longer matches then
2348 		 * we know the filter is active.
2349 		 */
2350 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2351 			add_head->state = I40E_FILTER_FAILED;
2352 		} else {
2353 			add_head->state = I40E_FILTER_ACTIVE;
2354 			retval++;
2355 		}
2356 
2357 		add_head = i40e_next_filter(add_head);
2358 		if (!add_head)
2359 			break;
2360 	}
2361 
2362 	return retval;
2363 }
2364 
2365 /**
2366  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2367  * @vsi: ptr to the VSI
2368  * @vsi_name: name to display in messages
2369  * @list: the list of filters to send to firmware
2370  * @num_del: the number of filters to delete
2371  * @retval: Set to -EIO on failure to delete
2372  *
2373  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2374  * *retval instead of a return value so that success does not force ret_val to
2375  * be set to 0. This ensures that a sequence of calls to this function
2376  * preserve the previous value of *retval on successful delete.
2377  */
2378 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)2379 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2380 			  struct i40e_aqc_remove_macvlan_element_data *list,
2381 			  int num_del, int *retval)
2382 {
2383 	struct i40e_hw *hw = &vsi->back->hw;
2384 	enum i40e_admin_queue_err aq_status;
2385 	int aq_ret;
2386 
2387 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2388 					   &aq_status);
2389 
2390 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2391 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2392 		*retval = -EIO;
2393 		dev_info(&vsi->back->pdev->dev,
2394 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2395 			 vsi_name, ERR_PTR(aq_ret),
2396 			 i40e_aq_str(hw, aq_status));
2397 	}
2398 }
2399 
2400 /**
2401  * i40e_aqc_add_filters - Request firmware to add a set of filters
2402  * @vsi: ptr to the VSI
2403  * @vsi_name: name to display in messages
2404  * @list: the list of filters to send to firmware
2405  * @add_head: Position in the add hlist
2406  * @num_add: the number of filters to add
2407  *
2408  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2409  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2410  * space for more filters.
2411  */
2412 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)2413 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2414 			  struct i40e_aqc_add_macvlan_element_data *list,
2415 			  struct i40e_new_mac_filter *add_head,
2416 			  int num_add)
2417 {
2418 	struct i40e_hw *hw = &vsi->back->hw;
2419 	enum i40e_admin_queue_err aq_status;
2420 	int fcnt;
2421 
2422 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2423 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2424 
2425 	if (fcnt != num_add) {
2426 		if (vsi->type == I40E_VSI_MAIN) {
2427 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2428 			dev_warn(&vsi->back->pdev->dev,
2429 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2430 				 i40e_aq_str(hw, aq_status), vsi_name);
2431 		} else if (vsi->type == I40E_VSI_SRIOV ||
2432 			   vsi->type == I40E_VSI_VMDQ1 ||
2433 			   vsi->type == I40E_VSI_VMDQ2) {
2434 			dev_warn(&vsi->back->pdev->dev,
2435 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2436 				 i40e_aq_str(hw, aq_status), vsi_name,
2437 					     vsi_name);
2438 		} else {
2439 			dev_warn(&vsi->back->pdev->dev,
2440 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2441 				 i40e_aq_str(hw, aq_status), vsi_name,
2442 					     vsi->type);
2443 		}
2444 	}
2445 }
2446 
2447 /**
2448  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2449  * @vsi: pointer to the VSI
2450  * @vsi_name: the VSI name
2451  * @f: filter data
2452  *
2453  * This function sets or clears the promiscuous broadcast flags for VLAN
2454  * filters in order to properly receive broadcast frames. Assumes that only
2455  * broadcast filters are passed.
2456  *
2457  * Returns status indicating success or failure;
2458  **/
2459 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2460 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2461 			  struct i40e_mac_filter *f)
2462 {
2463 	bool enable = f->state == I40E_FILTER_NEW ||
2464 		      f->state == I40E_FILTER_NEW_SYNC;
2465 	struct i40e_hw *hw = &vsi->back->hw;
2466 	int aq_ret;
2467 
2468 	if (f->vlan == I40E_VLAN_ANY) {
2469 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2470 						   vsi->seid,
2471 						   enable,
2472 						   NULL);
2473 	} else {
2474 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2475 							    vsi->seid,
2476 							    enable,
2477 							    f->vlan,
2478 							    NULL);
2479 	}
2480 
2481 	if (aq_ret) {
2482 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2483 		dev_warn(&vsi->back->pdev->dev,
2484 			 "Error %s, forcing overflow promiscuous on %s\n",
2485 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2486 			 vsi_name);
2487 	}
2488 
2489 	return aq_ret;
2490 }
2491 
2492 /**
2493  * i40e_set_promiscuous - set promiscuous mode
2494  * @pf: board private structure
2495  * @promisc: promisc on or off
2496  *
2497  * There are different ways of setting promiscuous mode on a PF depending on
2498  * what state/environment we're in.  This identifies and sets it appropriately.
2499  * Returns 0 on success.
2500  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2501 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2502 {
2503 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2504 	struct i40e_hw *hw = &pf->hw;
2505 	int aq_ret;
2506 
2507 	if (vsi->type == I40E_VSI_MAIN &&
2508 	    i40e_pf_get_main_veb(pf) &&
2509 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2510 		/* set defport ON for Main VSI instead of true promisc
2511 		 * this way we will get all unicast/multicast and VLAN
2512 		 * promisc behavior but will not get VF or VMDq traffic
2513 		 * replicated on the Main VSI.
2514 		 */
2515 		if (promisc)
2516 			aq_ret = i40e_aq_set_default_vsi(hw,
2517 							 vsi->seid,
2518 							 NULL);
2519 		else
2520 			aq_ret = i40e_aq_clear_default_vsi(hw,
2521 							   vsi->seid,
2522 							   NULL);
2523 		if (aq_ret) {
2524 			dev_info(&pf->pdev->dev,
2525 				 "Set default VSI failed, err %pe, aq_err %s\n",
2526 				 ERR_PTR(aq_ret),
2527 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2528 		}
2529 	} else {
2530 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2531 						  hw,
2532 						  vsi->seid,
2533 						  promisc, NULL,
2534 						  true);
2535 		if (aq_ret) {
2536 			dev_info(&pf->pdev->dev,
2537 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2538 				 ERR_PTR(aq_ret),
2539 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2540 		}
2541 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2542 						  hw,
2543 						  vsi->seid,
2544 						  promisc, NULL);
2545 		if (aq_ret) {
2546 			dev_info(&pf->pdev->dev,
2547 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2548 				 ERR_PTR(aq_ret),
2549 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2550 		}
2551 	}
2552 
2553 	if (!aq_ret)
2554 		pf->cur_promisc = promisc;
2555 
2556 	return aq_ret;
2557 }
2558 
2559 /**
2560  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2561  * @vsi: ptr to the VSI
2562  *
2563  * Push any outstanding VSI filter changes through the AdminQ.
2564  *
2565  * Returns 0 or error value
2566  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2567 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2568 {
2569 	struct hlist_head tmp_add_list, tmp_del_list;
2570 	struct i40e_mac_filter *f;
2571 	struct i40e_new_mac_filter *new, *add_head = NULL;
2572 	struct i40e_hw *hw = &vsi->back->hw;
2573 	bool old_overflow, new_overflow;
2574 	unsigned int failed_filters = 0;
2575 	unsigned int vlan_filters = 0;
2576 	char vsi_name[16] = "PF";
2577 	int filter_list_len = 0;
2578 	u32 changed_flags = 0;
2579 	struct hlist_node *h;
2580 	struct i40e_pf *pf;
2581 	int num_add = 0;
2582 	int num_del = 0;
2583 	int aq_ret = 0;
2584 	int retval = 0;
2585 	u16 cmd_flags;
2586 	int list_size;
2587 	int bkt;
2588 
2589 	/* empty array typed pointers, kcalloc later */
2590 	struct i40e_aqc_add_macvlan_element_data *add_list;
2591 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2592 
2593 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2594 		usleep_range(1000, 2000);
2595 	pf = vsi->back;
2596 
2597 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2598 
2599 	if (vsi->netdev) {
2600 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2601 		vsi->current_netdev_flags = vsi->netdev->flags;
2602 	}
2603 
2604 	INIT_HLIST_HEAD(&tmp_add_list);
2605 	INIT_HLIST_HEAD(&tmp_del_list);
2606 
2607 	if (vsi->type == I40E_VSI_SRIOV)
2608 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2609 	else if (vsi->type != I40E_VSI_MAIN)
2610 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2611 
2612 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2613 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2614 
2615 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2616 		/* Create a list of filters to delete. */
2617 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2618 			if (f->state == I40E_FILTER_REMOVE) {
2619 				/* Move the element into temporary del_list */
2620 				hash_del(&f->hlist);
2621 				hlist_add_head(&f->hlist, &tmp_del_list);
2622 
2623 				/* Avoid counting removed filters */
2624 				continue;
2625 			}
2626 			if (f->state == I40E_FILTER_NEW) {
2627 				/* Create a temporary i40e_new_mac_filter */
2628 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2629 				if (!new)
2630 					goto err_no_memory_locked;
2631 
2632 				/* Store pointer to the real filter */
2633 				new->f = f;
2634 				new->state = f->state;
2635 
2636 				/* Add it to the hash list */
2637 				hlist_add_head(&new->hlist, &tmp_add_list);
2638 				f->state = I40E_FILTER_NEW_SYNC;
2639 			}
2640 
2641 			/* Count the number of active (current and new) VLAN
2642 			 * filters we have now. Does not count filters which
2643 			 * are marked for deletion.
2644 			 */
2645 			if (f->vlan > 0)
2646 				vlan_filters++;
2647 		}
2648 
2649 		if (vsi->type != I40E_VSI_SRIOV)
2650 			retval = i40e_correct_mac_vlan_filters
2651 				(vsi, &tmp_add_list, &tmp_del_list,
2652 				 vlan_filters);
2653 		else if (pf->vf)
2654 			retval = i40e_correct_vf_mac_vlan_filters
2655 				(vsi, &tmp_add_list, &tmp_del_list,
2656 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2657 
2658 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2659 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2660 
2661 		if (retval)
2662 			goto err_no_memory_locked;
2663 
2664 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2665 	}
2666 
2667 	/* Now process 'del_list' outside the lock */
2668 	if (!hlist_empty(&tmp_del_list)) {
2669 		filter_list_len = hw->aq.asq_buf_size /
2670 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2671 		list_size = filter_list_len *
2672 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2673 		del_list = kzalloc(list_size, GFP_ATOMIC);
2674 		if (!del_list)
2675 			goto err_no_memory;
2676 
2677 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2678 			cmd_flags = 0;
2679 
2680 			/* handle broadcast filters by updating the broadcast
2681 			 * promiscuous flag and release filter list.
2682 			 */
2683 			if (is_broadcast_ether_addr(f->macaddr)) {
2684 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2685 
2686 				hlist_del(&f->hlist);
2687 				kfree(f);
2688 				continue;
2689 			}
2690 
2691 			/* add to delete list */
2692 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2693 			if (f->vlan == I40E_VLAN_ANY) {
2694 				del_list[num_del].vlan_tag = 0;
2695 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2696 			} else {
2697 				del_list[num_del].vlan_tag =
2698 					cpu_to_le16((u16)(f->vlan));
2699 			}
2700 
2701 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2702 			del_list[num_del].flags = cmd_flags;
2703 			num_del++;
2704 
2705 			/* flush a full buffer */
2706 			if (num_del == filter_list_len) {
2707 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2708 						     num_del, &retval);
2709 				memset(del_list, 0, list_size);
2710 				num_del = 0;
2711 			}
2712 			/* Release memory for MAC filter entries which were
2713 			 * synced up with HW.
2714 			 */
2715 			hlist_del(&f->hlist);
2716 			kfree(f);
2717 		}
2718 
2719 		if (num_del) {
2720 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2721 					     num_del, &retval);
2722 		}
2723 
2724 		kfree(del_list);
2725 		del_list = NULL;
2726 	}
2727 
2728 	if (!hlist_empty(&tmp_add_list)) {
2729 		/* Do all the adds now. */
2730 		filter_list_len = hw->aq.asq_buf_size /
2731 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2732 		list_size = filter_list_len *
2733 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2734 		add_list = kzalloc(list_size, GFP_ATOMIC);
2735 		if (!add_list)
2736 			goto err_no_memory;
2737 
2738 		num_add = 0;
2739 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2740 			/* handle broadcast filters by updating the broadcast
2741 			 * promiscuous flag instead of adding a MAC filter.
2742 			 */
2743 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2744 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2745 							      new->f))
2746 					new->state = I40E_FILTER_FAILED;
2747 				else
2748 					new->state = I40E_FILTER_ACTIVE;
2749 				continue;
2750 			}
2751 
2752 			/* add to add array */
2753 			if (num_add == 0)
2754 				add_head = new;
2755 			cmd_flags = 0;
2756 			ether_addr_copy(add_list[num_add].mac_addr,
2757 					new->f->macaddr);
2758 			if (new->f->vlan == I40E_VLAN_ANY) {
2759 				add_list[num_add].vlan_tag = 0;
2760 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2761 			} else {
2762 				add_list[num_add].vlan_tag =
2763 					cpu_to_le16((u16)(new->f->vlan));
2764 			}
2765 			add_list[num_add].queue_number = 0;
2766 			/* set invalid match method for later detection */
2767 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2768 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2769 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2770 			num_add++;
2771 
2772 			/* flush a full buffer */
2773 			if (num_add == filter_list_len) {
2774 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2775 						     add_head, num_add);
2776 				memset(add_list, 0, list_size);
2777 				num_add = 0;
2778 			}
2779 		}
2780 		if (num_add) {
2781 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2782 					     num_add);
2783 		}
2784 		/* Now move all of the filters from the temp add list back to
2785 		 * the VSI's list.
2786 		 */
2787 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2788 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2789 			/* Only update the state if we're still NEW */
2790 			if (new->f->state == I40E_FILTER_NEW ||
2791 			    new->f->state == I40E_FILTER_NEW_SYNC)
2792 				new->f->state = new->state;
2793 			hlist_del(&new->hlist);
2794 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2795 			kfree(new);
2796 		}
2797 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2798 		kfree(add_list);
2799 		add_list = NULL;
2800 	}
2801 
2802 	/* Determine the number of active and failed filters. */
2803 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2804 	vsi->active_filters = 0;
2805 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2806 		if (f->state == I40E_FILTER_ACTIVE)
2807 			vsi->active_filters++;
2808 		else if (f->state == I40E_FILTER_FAILED)
2809 			failed_filters++;
2810 	}
2811 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2812 
2813 	/* Check if we are able to exit overflow promiscuous mode. We can
2814 	 * safely exit if we didn't just enter, we no longer have any failed
2815 	 * filters, and we have reduced filters below the threshold value.
2816 	 */
2817 	if (old_overflow && !failed_filters &&
2818 	    vsi->active_filters < vsi->promisc_threshold) {
2819 		dev_info(&pf->pdev->dev,
2820 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2821 			 vsi_name);
2822 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2823 		vsi->promisc_threshold = 0;
2824 	}
2825 
2826 	/* if the VF is not trusted do not do promisc */
2827 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2828 	    !pf->vf[vsi->vf_id].trusted) {
2829 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2830 		goto out;
2831 	}
2832 
2833 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2834 
2835 	/* If we are entering overflow promiscuous, we need to calculate a new
2836 	 * threshold for when we are safe to exit
2837 	 */
2838 	if (!old_overflow && new_overflow)
2839 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2840 
2841 	/* check for changes in promiscuous modes */
2842 	if (changed_flags & IFF_ALLMULTI) {
2843 		bool cur_multipromisc;
2844 
2845 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2846 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2847 							       vsi->seid,
2848 							       cur_multipromisc,
2849 							       NULL);
2850 		if (aq_ret) {
2851 			retval = i40e_aq_rc_to_posix(aq_ret,
2852 						     hw->aq.asq_last_status);
2853 			dev_info(&pf->pdev->dev,
2854 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2855 				 vsi_name,
2856 				 ERR_PTR(aq_ret),
2857 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2858 		} else {
2859 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2860 				 cur_multipromisc ? "entering" : "leaving");
2861 		}
2862 	}
2863 
2864 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2865 		bool cur_promisc;
2866 
2867 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2868 			       new_overflow);
2869 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2870 		if (aq_ret) {
2871 			retval = i40e_aq_rc_to_posix(aq_ret,
2872 						     hw->aq.asq_last_status);
2873 			dev_info(&pf->pdev->dev,
2874 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2875 				 cur_promisc ? "on" : "off",
2876 				 vsi_name,
2877 				 ERR_PTR(aq_ret),
2878 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2879 		}
2880 	}
2881 out:
2882 	/* if something went wrong then set the changed flag so we try again */
2883 	if (retval)
2884 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2885 
2886 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2887 	return retval;
2888 
2889 err_no_memory:
2890 	/* Restore elements on the temporary add and delete lists */
2891 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2892 err_no_memory_locked:
2893 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2894 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2895 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2896 
2897 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2898 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2899 	return -ENOMEM;
2900 }
2901 
2902 /**
2903  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2904  * @pf: board private structure
2905  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2906 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2907 {
2908 	struct i40e_vsi *vsi;
2909 	int v;
2910 
2911 	if (!pf)
2912 		return;
2913 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2914 		return;
2915 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2916 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2917 		return;
2918 	}
2919 
2920 	i40e_pf_for_each_vsi(pf, v, vsi) {
2921 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2922 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2923 			int ret = i40e_sync_vsi_filters(vsi);
2924 
2925 			if (ret) {
2926 				/* come back and try again later */
2927 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2928 					pf->state);
2929 				break;
2930 			}
2931 		}
2932 	}
2933 }
2934 
2935 /**
2936  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2937  *
2938  * @vsi: VSI to calculate rx_buf_len from
2939  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)2940 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2941 {
2942 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2943 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2944 
2945 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2946 }
2947 
2948 /**
2949  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2950  * @vsi: the vsi
2951  * @xdp_prog: XDP program
2952  **/
i40e_max_vsi_frame_size(struct i40e_vsi * vsi,struct bpf_prog * xdp_prog)2953 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2954 				   struct bpf_prog *xdp_prog)
2955 {
2956 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2957 	u16 chain_len;
2958 
2959 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2960 		chain_len = 1;
2961 	else
2962 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2963 
2964 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2965 }
2966 
2967 /**
2968  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2969  * @netdev: network interface device structure
2970  * @new_mtu: new value for maximum frame size
2971  *
2972  * Returns 0 on success, negative on failure
2973  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2974 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2975 {
2976 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2977 	struct i40e_vsi *vsi = np->vsi;
2978 	struct i40e_pf *pf = vsi->back;
2979 	int frame_size;
2980 
2981 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2982 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2983 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2984 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2985 		return -EINVAL;
2986 	}
2987 
2988 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2989 		   netdev->mtu, new_mtu);
2990 	WRITE_ONCE(netdev->mtu, new_mtu);
2991 	if (netif_running(netdev))
2992 		i40e_vsi_reinit_locked(vsi);
2993 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2994 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2995 	return 0;
2996 }
2997 
2998 /**
2999  * i40e_ioctl - Access the hwtstamp interface
3000  * @netdev: network interface device structure
3001  * @ifr: interface request data
3002  * @cmd: ioctl command
3003  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)3004 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3005 {
3006 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3007 	struct i40e_pf *pf = np->vsi->back;
3008 
3009 	switch (cmd) {
3010 	case SIOCGHWTSTAMP:
3011 		return i40e_ptp_get_ts_config(pf, ifr);
3012 	case SIOCSHWTSTAMP:
3013 		return i40e_ptp_set_ts_config(pf, ifr);
3014 	default:
3015 		return -EOPNOTSUPP;
3016 	}
3017 }
3018 
3019 /**
3020  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
3021  * @vsi: the vsi being adjusted
3022  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)3023 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
3024 {
3025 	struct i40e_vsi_context ctxt;
3026 	int ret;
3027 
3028 	/* Don't modify stripping options if a port VLAN is active */
3029 	if (vsi->info.pvid)
3030 		return;
3031 
3032 	if ((vsi->info.valid_sections &
3033 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3034 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3035 		return;  /* already enabled */
3036 
3037 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3038 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3039 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3040 
3041 	ctxt.seid = vsi->seid;
3042 	ctxt.info = vsi->info;
3043 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3044 	if (ret) {
3045 		dev_info(&vsi->back->pdev->dev,
3046 			 "update vlan stripping failed, err %pe aq_err %s\n",
3047 			 ERR_PTR(ret),
3048 			 i40e_aq_str(&vsi->back->hw,
3049 				     vsi->back->hw.aq.asq_last_status));
3050 	}
3051 }
3052 
3053 /**
3054  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3055  * @vsi: the vsi being adjusted
3056  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)3057 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3058 {
3059 	struct i40e_vsi_context ctxt;
3060 	int ret;
3061 
3062 	/* Don't modify stripping options if a port VLAN is active */
3063 	if (vsi->info.pvid)
3064 		return;
3065 
3066 	if ((vsi->info.valid_sections &
3067 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3068 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3069 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3070 		return;  /* already disabled */
3071 
3072 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3073 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3074 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3075 
3076 	ctxt.seid = vsi->seid;
3077 	ctxt.info = vsi->info;
3078 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3079 	if (ret) {
3080 		dev_info(&vsi->back->pdev->dev,
3081 			 "update vlan stripping failed, err %pe aq_err %s\n",
3082 			 ERR_PTR(ret),
3083 			 i40e_aq_str(&vsi->back->hw,
3084 				     vsi->back->hw.aq.asq_last_status));
3085 	}
3086 }
3087 
3088 /**
3089  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3090  * @vsi: the vsi being configured
3091  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3092  *
3093  * This is a helper function for adding a new MAC/VLAN filter with the
3094  * specified VLAN for each existing MAC address already in the hash table.
3095  * This function does *not* perform any accounting to update filters based on
3096  * VLAN mode.
3097  *
3098  * NOTE: this function expects to be called while under the
3099  * mac_filter_hash_lock
3100  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3101 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3102 {
3103 	struct i40e_mac_filter *f, *add_f;
3104 	struct hlist_node *h;
3105 	int bkt;
3106 
3107 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3108 		/* If we're asked to add a filter that has been marked for
3109 		 * removal, it is safe to simply restore it to active state.
3110 		 * __i40e_del_filter will have simply deleted any filters which
3111 		 * were previously marked NEW or FAILED, so if it is currently
3112 		 * marked REMOVE it must have previously been ACTIVE. Since we
3113 		 * haven't yet run the sync filters task, just restore this
3114 		 * filter to the ACTIVE state so that the sync task leaves it
3115 		 * in place.
3116 		 */
3117 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3118 			f->state = I40E_FILTER_ACTIVE;
3119 			continue;
3120 		} else if (f->state == I40E_FILTER_REMOVE) {
3121 			continue;
3122 		}
3123 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3124 		if (!add_f) {
3125 			dev_info(&vsi->back->pdev->dev,
3126 				 "Could not add vlan filter %d for %pM\n",
3127 				 vid, f->macaddr);
3128 			return -ENOMEM;
3129 		}
3130 	}
3131 
3132 	return 0;
3133 }
3134 
3135 /**
3136  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3137  * @vsi: the VSI being configured
3138  * @vid: VLAN id to be added
3139  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3140 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3141 {
3142 	int err;
3143 
3144 	if (vsi->info.pvid)
3145 		return -EINVAL;
3146 
3147 	/* The network stack will attempt to add VID=0, with the intention to
3148 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3149 	 * these packets by default when configured to receive untagged
3150 	 * packets, so we don't need to add a filter for this case.
3151 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3152 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3153 	 * Thus, we do not want to actually add a filter for VID=0
3154 	 */
3155 	if (!vid)
3156 		return 0;
3157 
3158 	/* Locked once because all functions invoked below iterates list*/
3159 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3160 	err = i40e_add_vlan_all_mac(vsi, vid);
3161 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3162 	if (err)
3163 		return err;
3164 
3165 	/* schedule our worker thread which will take care of
3166 	 * applying the new filter changes
3167 	 */
3168 	i40e_service_event_schedule(vsi->back);
3169 	return 0;
3170 }
3171 
3172 /**
3173  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3174  * @vsi: the vsi being configured
3175  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3176  *
3177  * This function should be used to remove all VLAN filters which match the
3178  * given VID. It does not schedule the service event and does not take the
3179  * mac_filter_hash_lock so it may be combined with other operations under
3180  * a single invocation of the mac_filter_hash_lock.
3181  *
3182  * NOTE: this function expects to be called while under the
3183  * mac_filter_hash_lock
3184  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3185 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3186 {
3187 	struct i40e_mac_filter *f;
3188 	struct hlist_node *h;
3189 	int bkt;
3190 
3191 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3192 		if (f->vlan == vid)
3193 			__i40e_del_filter(vsi, f);
3194 	}
3195 }
3196 
3197 /**
3198  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3199  * @vsi: the VSI being configured
3200  * @vid: VLAN id to be removed
3201  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3202 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3203 {
3204 	if (!vid || vsi->info.pvid)
3205 		return;
3206 
3207 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3208 	i40e_rm_vlan_all_mac(vsi, vid);
3209 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3210 
3211 	/* schedule our worker thread which will take care of
3212 	 * applying the new filter changes
3213 	 */
3214 	i40e_service_event_schedule(vsi->back);
3215 }
3216 
3217 /**
3218  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3219  * @netdev: network interface to be adjusted
3220  * @proto: unused protocol value
3221  * @vid: vlan id to be added
3222  *
3223  * net_device_ops implementation for adding vlan ids
3224  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3225 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3226 				__always_unused __be16 proto, u16 vid)
3227 {
3228 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3229 	struct i40e_vsi *vsi = np->vsi;
3230 	int ret = 0;
3231 
3232 	if (vid >= VLAN_N_VID)
3233 		return -EINVAL;
3234 
3235 	ret = i40e_vsi_add_vlan(vsi, vid);
3236 	if (!ret)
3237 		set_bit(vid, vsi->active_vlans);
3238 
3239 	return ret;
3240 }
3241 
3242 /**
3243  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3244  * @netdev: network interface to be adjusted
3245  * @proto: unused protocol value
3246  * @vid: vlan id to be added
3247  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3248 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3249 				    __always_unused __be16 proto, u16 vid)
3250 {
3251 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3252 	struct i40e_vsi *vsi = np->vsi;
3253 
3254 	if (vid >= VLAN_N_VID)
3255 		return;
3256 	set_bit(vid, vsi->active_vlans);
3257 }
3258 
3259 /**
3260  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3261  * @netdev: network interface to be adjusted
3262  * @proto: unused protocol value
3263  * @vid: vlan id to be removed
3264  *
3265  * net_device_ops implementation for removing vlan ids
3266  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3267 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3268 				 __always_unused __be16 proto, u16 vid)
3269 {
3270 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3271 	struct i40e_vsi *vsi = np->vsi;
3272 
3273 	/* return code is ignored as there is nothing a user
3274 	 * can do about failure to remove and a log message was
3275 	 * already printed from the other function
3276 	 */
3277 	i40e_vsi_kill_vlan(vsi, vid);
3278 
3279 	clear_bit(vid, vsi->active_vlans);
3280 
3281 	return 0;
3282 }
3283 
3284 /**
3285  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3286  * @vsi: the vsi being brought back up
3287  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3288 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3289 {
3290 	u16 vid;
3291 
3292 	if (!vsi->netdev)
3293 		return;
3294 
3295 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3296 		i40e_vlan_stripping_enable(vsi);
3297 	else
3298 		i40e_vlan_stripping_disable(vsi);
3299 
3300 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3301 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3302 					vid);
3303 }
3304 
3305 /**
3306  * i40e_vsi_add_pvid - Add pvid for the VSI
3307  * @vsi: the vsi being adjusted
3308  * @vid: the vlan id to set as a PVID
3309  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3310 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3311 {
3312 	struct i40e_vsi_context ctxt;
3313 	int ret;
3314 
3315 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3316 	vsi->info.pvid = cpu_to_le16(vid);
3317 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3318 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3319 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3320 
3321 	ctxt.seid = vsi->seid;
3322 	ctxt.info = vsi->info;
3323 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3324 	if (ret) {
3325 		dev_info(&vsi->back->pdev->dev,
3326 			 "add pvid failed, err %pe aq_err %s\n",
3327 			 ERR_PTR(ret),
3328 			 i40e_aq_str(&vsi->back->hw,
3329 				     vsi->back->hw.aq.asq_last_status));
3330 		return -ENOENT;
3331 	}
3332 
3333 	return 0;
3334 }
3335 
3336 /**
3337  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3338  * @vsi: the vsi being adjusted
3339  *
3340  * Just use the vlan_rx_register() service to put it back to normal
3341  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3342 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3343 {
3344 	vsi->info.pvid = 0;
3345 
3346 	i40e_vlan_stripping_disable(vsi);
3347 }
3348 
3349 /**
3350  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3351  * @vsi: ptr to the VSI
3352  *
3353  * If this function returns with an error, then it's possible one or
3354  * more of the rings is populated (while the rest are not).  It is the
3355  * callers duty to clean those orphaned rings.
3356  *
3357  * Return 0 on success, negative on failure
3358  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3359 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3360 {
3361 	int i, err = 0;
3362 
3363 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3364 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3365 
3366 	if (!i40e_enabled_xdp_vsi(vsi))
3367 		return err;
3368 
3369 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3370 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3371 
3372 	return err;
3373 }
3374 
3375 /**
3376  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3377  * @vsi: ptr to the VSI
3378  *
3379  * Free VSI's transmit software resources
3380  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3381 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3382 {
3383 	int i;
3384 
3385 	if (vsi->tx_rings) {
3386 		for (i = 0; i < vsi->num_queue_pairs; i++)
3387 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3388 				i40e_free_tx_resources(vsi->tx_rings[i]);
3389 	}
3390 
3391 	if (vsi->xdp_rings) {
3392 		for (i = 0; i < vsi->num_queue_pairs; i++)
3393 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3394 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3395 	}
3396 }
3397 
3398 /**
3399  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3400  * @vsi: ptr to the VSI
3401  *
3402  * If this function returns with an error, then it's possible one or
3403  * more of the rings is populated (while the rest are not).  It is the
3404  * callers duty to clean those orphaned rings.
3405  *
3406  * Return 0 on success, negative on failure
3407  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3408 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3409 {
3410 	int i, err = 0;
3411 
3412 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3413 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3414 	return err;
3415 }
3416 
3417 /**
3418  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3419  * @vsi: ptr to the VSI
3420  *
3421  * Free all receive software resources
3422  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3423 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3424 {
3425 	int i;
3426 
3427 	if (!vsi->rx_rings)
3428 		return;
3429 
3430 	for (i = 0; i < vsi->num_queue_pairs; i++)
3431 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3432 			i40e_free_rx_resources(vsi->rx_rings[i]);
3433 }
3434 
3435 /**
3436  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3437  * @ring: The Tx ring to configure
3438  *
3439  * This enables/disables XPS for a given Tx descriptor ring
3440  * based on the TCs enabled for the VSI that ring belongs to.
3441  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3442 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3443 {
3444 	int cpu;
3445 
3446 	if (!ring->q_vector || !ring->netdev || ring->ch)
3447 		return;
3448 
3449 	/* We only initialize XPS once, so as not to overwrite user settings */
3450 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3451 		return;
3452 
3453 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3454 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3455 			    ring->queue_index);
3456 }
3457 
3458 /**
3459  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3460  * @ring: The Tx or Rx ring
3461  *
3462  * Returns the AF_XDP buffer pool or NULL.
3463  **/
i40e_xsk_pool(struct i40e_ring * ring)3464 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3465 {
3466 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3467 	int qid = ring->queue_index;
3468 
3469 	if (ring_is_xdp(ring))
3470 		qid -= ring->vsi->alloc_queue_pairs;
3471 
3472 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3473 		return NULL;
3474 
3475 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3476 }
3477 
3478 /**
3479  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3480  * @ring: The Tx ring to configure
3481  *
3482  * Configure the Tx descriptor ring in the HMC context.
3483  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3484 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3485 {
3486 	struct i40e_vsi *vsi = ring->vsi;
3487 	u16 pf_q = vsi->base_queue + ring->queue_index;
3488 	struct i40e_hw *hw = &vsi->back->hw;
3489 	struct i40e_hmc_obj_txq tx_ctx;
3490 	u32 qtx_ctl = 0;
3491 	int err = 0;
3492 
3493 	if (ring_is_xdp(ring))
3494 		ring->xsk_pool = i40e_xsk_pool(ring);
3495 
3496 	/* some ATR related tx ring init */
3497 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3498 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3499 		ring->atr_count = 0;
3500 	} else {
3501 		ring->atr_sample_rate = 0;
3502 	}
3503 
3504 	/* configure XPS */
3505 	i40e_config_xps_tx_ring(ring);
3506 
3507 	/* clear the context structure first */
3508 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3509 
3510 	tx_ctx.new_context = 1;
3511 	tx_ctx.base = (ring->dma / 128);
3512 	tx_ctx.qlen = ring->count;
3513 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3514 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3515 		tx_ctx.fd_ena = 1;
3516 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3517 		tx_ctx.timesync_ena = 1;
3518 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3519 	if (vsi->type != I40E_VSI_FDIR)
3520 		tx_ctx.head_wb_ena = 1;
3521 	tx_ctx.head_wb_addr = ring->dma +
3522 			      (ring->count * sizeof(struct i40e_tx_desc));
3523 
3524 	/* As part of VSI creation/update, FW allocates certain
3525 	 * Tx arbitration queue sets for each TC enabled for
3526 	 * the VSI. The FW returns the handles to these queue
3527 	 * sets as part of the response buffer to Add VSI,
3528 	 * Update VSI, etc. AQ commands. It is expected that
3529 	 * these queue set handles be associated with the Tx
3530 	 * queues by the driver as part of the TX queue context
3531 	 * initialization. This has to be done regardless of
3532 	 * DCB as by default everything is mapped to TC0.
3533 	 */
3534 
3535 	if (ring->ch)
3536 		tx_ctx.rdylist =
3537 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3538 
3539 	else
3540 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3541 
3542 	tx_ctx.rdylist_act = 0;
3543 
3544 	/* clear the context in the HMC */
3545 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3546 	if (err) {
3547 		dev_info(&vsi->back->pdev->dev,
3548 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3549 			 ring->queue_index, pf_q, err);
3550 		return -ENOMEM;
3551 	}
3552 
3553 	/* set the context in the HMC */
3554 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3555 	if (err) {
3556 		dev_info(&vsi->back->pdev->dev,
3557 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3558 			 ring->queue_index, pf_q, err);
3559 		return -ENOMEM;
3560 	}
3561 
3562 	/* Now associate this queue with this PCI function */
3563 	if (ring->ch) {
3564 		if (ring->ch->type == I40E_VSI_VMDQ2)
3565 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3566 		else
3567 			return -EINVAL;
3568 
3569 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3570 				      ring->ch->vsi_number);
3571 	} else {
3572 		if (vsi->type == I40E_VSI_VMDQ2) {
3573 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3574 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3575 					      vsi->id);
3576 		} else {
3577 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3578 		}
3579 	}
3580 
3581 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3582 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3583 	i40e_flush(hw);
3584 
3585 	/* cache tail off for easier writes later */
3586 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3587 
3588 	return 0;
3589 }
3590 
3591 /**
3592  * i40e_rx_offset - Return expected offset into page to access data
3593  * @rx_ring: Ring we are requesting offset of
3594  *
3595  * Returns the offset value for ring into the data buffer.
3596  */
i40e_rx_offset(struct i40e_ring * rx_ring)3597 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3598 {
3599 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3600 }
3601 
3602 /**
3603  * i40e_configure_rx_ring - Configure a receive ring context
3604  * @ring: The Rx ring to configure
3605  *
3606  * Configure the Rx descriptor ring in the HMC context.
3607  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3608 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3609 {
3610 	struct i40e_vsi *vsi = ring->vsi;
3611 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3612 	u16 pf_q = vsi->base_queue + ring->queue_index;
3613 	struct i40e_hw *hw = &vsi->back->hw;
3614 	struct i40e_hmc_obj_rxq rx_ctx;
3615 	int err = 0;
3616 	bool ok;
3617 
3618 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3619 
3620 	/* clear the context structure first */
3621 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3622 
3623 	ring->rx_buf_len = vsi->rx_buf_len;
3624 
3625 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3626 	if (ring->vsi->type != I40E_VSI_MAIN)
3627 		goto skip;
3628 
3629 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3630 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3631 					 ring->queue_index,
3632 					 ring->q_vector->napi.napi_id,
3633 					 ring->rx_buf_len);
3634 		if (err)
3635 			return err;
3636 	}
3637 
3638 	ring->xsk_pool = i40e_xsk_pool(ring);
3639 	if (ring->xsk_pool) {
3640 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3641 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3642 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3643 					 ring->queue_index,
3644 					 ring->q_vector->napi.napi_id,
3645 					 ring->rx_buf_len);
3646 		if (err)
3647 			return err;
3648 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3649 						 MEM_TYPE_XSK_BUFF_POOL,
3650 						 NULL);
3651 		if (err)
3652 			return err;
3653 		dev_info(&vsi->back->pdev->dev,
3654 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3655 			 ring->queue_index);
3656 
3657 	} else {
3658 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3659 						 MEM_TYPE_PAGE_SHARED,
3660 						 NULL);
3661 		if (err)
3662 			return err;
3663 	}
3664 
3665 skip:
3666 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3667 
3668 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3669 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3670 
3671 	rx_ctx.base = (ring->dma / 128);
3672 	rx_ctx.qlen = ring->count;
3673 
3674 	/* use 16 byte descriptors */
3675 	rx_ctx.dsize = 0;
3676 
3677 	/* descriptor type is always zero
3678 	 * rx_ctx.dtype = 0;
3679 	 */
3680 	rx_ctx.hsplit_0 = 0;
3681 
3682 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3683 	if (hw->revision_id == 0)
3684 		rx_ctx.lrxqthresh = 0;
3685 	else
3686 		rx_ctx.lrxqthresh = 1;
3687 	rx_ctx.crcstrip = 1;
3688 	rx_ctx.l2tsel = 1;
3689 	/* this controls whether VLAN is stripped from inner headers */
3690 	rx_ctx.showiv = 0;
3691 	/* set the prefena field to 1 because the manual says to */
3692 	rx_ctx.prefena = 1;
3693 
3694 	/* clear the context in the HMC */
3695 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3696 	if (err) {
3697 		dev_info(&vsi->back->pdev->dev,
3698 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3699 			 ring->queue_index, pf_q, err);
3700 		return -ENOMEM;
3701 	}
3702 
3703 	/* set the context in the HMC */
3704 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3705 	if (err) {
3706 		dev_info(&vsi->back->pdev->dev,
3707 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3708 			 ring->queue_index, pf_q, err);
3709 		return -ENOMEM;
3710 	}
3711 
3712 	/* configure Rx buffer alignment */
3713 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3714 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3715 			dev_info(&vsi->back->pdev->dev,
3716 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3717 			return -EOPNOTSUPP;
3718 		}
3719 		clear_ring_build_skb_enabled(ring);
3720 	} else {
3721 		set_ring_build_skb_enabled(ring);
3722 	}
3723 
3724 	ring->rx_offset = i40e_rx_offset(ring);
3725 
3726 	/* cache tail for quicker writes, and clear the reg before use */
3727 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3728 	writel(0, ring->tail);
3729 
3730 	if (ring->xsk_pool) {
3731 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3732 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3733 	} else {
3734 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3735 	}
3736 	if (!ok) {
3737 		/* Log this in case the user has forgotten to give the kernel
3738 		 * any buffers, even later in the application.
3739 		 */
3740 		dev_info(&vsi->back->pdev->dev,
3741 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3742 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3743 			 ring->queue_index, pf_q);
3744 	}
3745 
3746 	return 0;
3747 }
3748 
3749 /**
3750  * i40e_vsi_configure_tx - Configure the VSI for Tx
3751  * @vsi: VSI structure describing this set of rings and resources
3752  *
3753  * Configure the Tx VSI for operation.
3754  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3755 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3756 {
3757 	int err = 0;
3758 	u16 i;
3759 
3760 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3761 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3762 
3763 	if (err || !i40e_enabled_xdp_vsi(vsi))
3764 		return err;
3765 
3766 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3767 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3768 
3769 	return err;
3770 }
3771 
3772 /**
3773  * i40e_vsi_configure_rx - Configure the VSI for Rx
3774  * @vsi: the VSI being configured
3775  *
3776  * Configure the Rx VSI for operation.
3777  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3778 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3779 {
3780 	int err = 0;
3781 	u16 i;
3782 
3783 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3784 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3785 
3786 #if (PAGE_SIZE < 8192)
3787 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3788 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3789 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3790 		vsi->max_frame = vsi->rx_buf_len;
3791 	}
3792 #endif
3793 
3794 	/* set up individual rings */
3795 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3796 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3797 
3798 	return err;
3799 }
3800 
3801 /**
3802  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3803  * @vsi: ptr to the VSI
3804  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3805 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3806 {
3807 	struct i40e_ring *tx_ring, *rx_ring;
3808 	u16 qoffset, qcount;
3809 	int i, n;
3810 
3811 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3812 		/* Reset the TC information */
3813 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3814 			rx_ring = vsi->rx_rings[i];
3815 			tx_ring = vsi->tx_rings[i];
3816 			rx_ring->dcb_tc = 0;
3817 			tx_ring->dcb_tc = 0;
3818 		}
3819 		return;
3820 	}
3821 
3822 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3823 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3824 			continue;
3825 
3826 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3827 		qcount = vsi->tc_config.tc_info[n].qcount;
3828 		for (i = qoffset; i < (qoffset + qcount); i++) {
3829 			rx_ring = vsi->rx_rings[i];
3830 			tx_ring = vsi->tx_rings[i];
3831 			rx_ring->dcb_tc = n;
3832 			tx_ring->dcb_tc = n;
3833 		}
3834 	}
3835 }
3836 
3837 /**
3838  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3839  * @vsi: ptr to the VSI
3840  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3841 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3842 {
3843 	if (vsi->netdev)
3844 		i40e_set_rx_mode(vsi->netdev);
3845 }
3846 
3847 /**
3848  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3849  * @pf: Pointer to the targeted PF
3850  *
3851  * Set all flow director counters to 0.
3852  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3853 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3854 {
3855 	pf->fd_tcp4_filter_cnt = 0;
3856 	pf->fd_udp4_filter_cnt = 0;
3857 	pf->fd_sctp4_filter_cnt = 0;
3858 	pf->fd_ip4_filter_cnt = 0;
3859 	pf->fd_tcp6_filter_cnt = 0;
3860 	pf->fd_udp6_filter_cnt = 0;
3861 	pf->fd_sctp6_filter_cnt = 0;
3862 	pf->fd_ip6_filter_cnt = 0;
3863 }
3864 
3865 /**
3866  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3867  * @vsi: Pointer to the targeted VSI
3868  *
3869  * This function replays the hlist on the hw where all the SB Flow Director
3870  * filters were saved.
3871  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3872 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3873 {
3874 	struct i40e_fdir_filter *filter;
3875 	struct i40e_pf *pf = vsi->back;
3876 	struct hlist_node *node;
3877 
3878 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3879 		return;
3880 
3881 	/* Reset FDir counters as we're replaying all existing filters */
3882 	i40e_reset_fdir_filter_cnt(pf);
3883 
3884 	hlist_for_each_entry_safe(filter, node,
3885 				  &pf->fdir_filter_list, fdir_node) {
3886 		i40e_add_del_fdir(vsi, filter, true);
3887 	}
3888 }
3889 
3890 /**
3891  * i40e_vsi_configure - Set up the VSI for action
3892  * @vsi: the VSI being configured
3893  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3894 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3895 {
3896 	int err;
3897 
3898 	i40e_set_vsi_rx_mode(vsi);
3899 	i40e_restore_vlan(vsi);
3900 	i40e_vsi_config_dcb_rings(vsi);
3901 	err = i40e_vsi_configure_tx(vsi);
3902 	if (!err)
3903 		err = i40e_vsi_configure_rx(vsi);
3904 
3905 	return err;
3906 }
3907 
3908 /**
3909  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3910  * @vsi: the VSI being configured
3911  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3912 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3913 {
3914 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3915 	struct i40e_pf *pf = vsi->back;
3916 	struct i40e_hw *hw = &pf->hw;
3917 	u16 vector;
3918 	int i, q;
3919 	u32 qp;
3920 
3921 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3922 	 * and PFINT_LNKLSTn registers, e.g.:
3923 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3924 	 */
3925 	qp = vsi->base_queue;
3926 	vector = vsi->base_vector;
3927 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3928 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3929 
3930 		q_vector->rx.next_update = jiffies + 1;
3931 		q_vector->rx.target_itr =
3932 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3933 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3934 		     q_vector->rx.target_itr >> 1);
3935 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3936 
3937 		q_vector->tx.next_update = jiffies + 1;
3938 		q_vector->tx.target_itr =
3939 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3940 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3941 		     q_vector->tx.target_itr >> 1);
3942 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3943 
3944 		/* Set ITR for software interrupts triggered after exiting
3945 		 * busy-loop polling.
3946 		 */
3947 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3948 		     I40E_ITR_20K);
3949 
3950 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3951 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3952 
3953 		/* begin of linked list for RX queue assigned to this vector */
3954 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3955 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3956 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3957 			u32 val;
3958 
3959 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3960 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3961 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3962 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3963 			      (I40E_QUEUE_TYPE_TX <<
3964 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3965 
3966 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3967 
3968 			if (has_xdp) {
3969 				/* TX queue with next queue set to TX */
3970 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3971 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3972 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3973 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3974 				      (I40E_QUEUE_TYPE_TX <<
3975 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3976 
3977 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3978 			}
3979 			/* TX queue with next RX or end of linked list */
3980 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3981 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3982 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3983 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3984 			      (I40E_QUEUE_TYPE_RX <<
3985 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3986 
3987 			/* Terminate the linked list */
3988 			if (q == (q_vector->num_ringpairs - 1))
3989 				val |= (I40E_QUEUE_END_OF_LIST <<
3990 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3991 
3992 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3993 			qp++;
3994 		}
3995 	}
3996 
3997 	i40e_flush(hw);
3998 }
3999 
4000 /**
4001  * i40e_enable_misc_int_causes - enable the non-queue interrupts
4002  * @pf: pointer to private device data structure
4003  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)4004 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
4005 {
4006 	struct i40e_hw *hw = &pf->hw;
4007 	u32 val;
4008 
4009 	/* clear things first */
4010 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
4011 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
4012 
4013 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
4014 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
4015 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
4016 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
4017 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
4018 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
4019 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
4020 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4021 
4022 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
4023 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4024 
4025 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
4026 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4027 
4028 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
4029 
4030 	/* SW_ITR_IDX = 0, but don't change INTENA */
4031 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
4032 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
4033 
4034 	/* OTHER_ITR_IDX = 0 */
4035 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4036 }
4037 
4038 /**
4039  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4040  * @vsi: the VSI being configured
4041  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)4042 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4043 {
4044 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4045 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4046 	struct i40e_pf *pf = vsi->back;
4047 	struct i40e_hw *hw = &pf->hw;
4048 
4049 	/* set the ITR configuration */
4050 	q_vector->rx.next_update = jiffies + 1;
4051 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4052 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4053 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4054 	q_vector->tx.next_update = jiffies + 1;
4055 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4056 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4057 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4058 
4059 	i40e_enable_misc_int_causes(pf);
4060 
4061 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4062 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4063 
4064 	/* Associate the queue pair to the vector and enable the queue
4065 	 * interrupt RX queue in linked list with next queue set to TX
4066 	 */
4067 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4068 
4069 	if (i40e_enabled_xdp_vsi(vsi)) {
4070 		/* TX queue in linked list with next queue set to TX */
4071 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4072 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4073 	}
4074 
4075 	/* last TX queue so the next RX queue doesn't matter */
4076 	wr32(hw, I40E_QINT_TQCTL(0),
4077 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4078 	i40e_flush(hw);
4079 }
4080 
4081 /**
4082  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4083  * @pf: board private structure
4084  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4085 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4086 {
4087 	struct i40e_hw *hw = &pf->hw;
4088 
4089 	wr32(hw, I40E_PFINT_DYN_CTL0,
4090 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4091 	i40e_flush(hw);
4092 }
4093 
4094 /**
4095  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4096  * @pf: board private structure
4097  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4098 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4099 {
4100 	struct i40e_hw *hw = &pf->hw;
4101 	u32 val;
4102 
4103 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4104 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4105 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4106 
4107 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4108 	i40e_flush(hw);
4109 }
4110 
4111 /**
4112  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4113  * @irq: interrupt number
4114  * @data: pointer to a q_vector
4115  **/
i40e_msix_clean_rings(int irq,void * data)4116 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4117 {
4118 	struct i40e_q_vector *q_vector = data;
4119 
4120 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4121 		return IRQ_HANDLED;
4122 
4123 	napi_schedule_irqoff(&q_vector->napi);
4124 
4125 	return IRQ_HANDLED;
4126 }
4127 
4128 /**
4129  * i40e_irq_affinity_notify - Callback for affinity changes
4130  * @notify: context as to what irq was changed
4131  * @mask: the new affinity mask
4132  *
4133  * This is a callback function used by the irq_set_affinity_notifier function
4134  * so that we may register to receive changes to the irq affinity masks.
4135  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4136 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4137 				     const cpumask_t *mask)
4138 {
4139 	struct i40e_q_vector *q_vector =
4140 		container_of(notify, struct i40e_q_vector, affinity_notify);
4141 
4142 	cpumask_copy(&q_vector->affinity_mask, mask);
4143 }
4144 
4145 /**
4146  * i40e_irq_affinity_release - Callback for affinity notifier release
4147  * @ref: internal core kernel usage
4148  *
4149  * This is a callback function used by the irq_set_affinity_notifier function
4150  * to inform the current notification subscriber that they will no longer
4151  * receive notifications.
4152  **/
i40e_irq_affinity_release(struct kref * ref)4153 static void i40e_irq_affinity_release(struct kref *ref) {}
4154 
4155 /**
4156  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4157  * @vsi: the VSI being configured
4158  * @basename: name for the vector
4159  *
4160  * Allocates MSI-X vectors and requests interrupts from the kernel.
4161  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4162 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4163 {
4164 	int q_vectors = vsi->num_q_vectors;
4165 	struct i40e_pf *pf = vsi->back;
4166 	int base = vsi->base_vector;
4167 	int rx_int_idx = 0;
4168 	int tx_int_idx = 0;
4169 	int vector, err;
4170 	int irq_num;
4171 	int cpu;
4172 
4173 	for (vector = 0; vector < q_vectors; vector++) {
4174 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4175 
4176 		irq_num = pf->msix_entries[base + vector].vector;
4177 
4178 		if (q_vector->tx.ring && q_vector->rx.ring) {
4179 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4180 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4181 			tx_int_idx++;
4182 		} else if (q_vector->rx.ring) {
4183 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4184 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4185 		} else if (q_vector->tx.ring) {
4186 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4187 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4188 		} else {
4189 			/* skip this unused q_vector */
4190 			continue;
4191 		}
4192 		err = request_irq(irq_num,
4193 				  vsi->irq_handler,
4194 				  0,
4195 				  q_vector->name,
4196 				  q_vector);
4197 		if (err) {
4198 			dev_info(&pf->pdev->dev,
4199 				 "MSIX request_irq failed, error: %d\n", err);
4200 			goto free_queue_irqs;
4201 		}
4202 
4203 		/* register for affinity change notifications */
4204 		q_vector->irq_num = irq_num;
4205 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4206 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4207 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4208 		/* Spread affinity hints out across online CPUs.
4209 		 *
4210 		 * get_cpu_mask returns a static constant mask with
4211 		 * a permanent lifetime so it's ok to pass to
4212 		 * irq_update_affinity_hint without making a copy.
4213 		 */
4214 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4215 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4216 	}
4217 
4218 	vsi->irqs_ready = true;
4219 	return 0;
4220 
4221 free_queue_irqs:
4222 	while (vector) {
4223 		vector--;
4224 		irq_num = pf->msix_entries[base + vector].vector;
4225 		irq_set_affinity_notifier(irq_num, NULL);
4226 		irq_update_affinity_hint(irq_num, NULL);
4227 		free_irq(irq_num, vsi->q_vectors[vector]);
4228 	}
4229 	return err;
4230 }
4231 
4232 /**
4233  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4234  * @vsi: the VSI being un-configured
4235  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4236 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4237 {
4238 	struct i40e_pf *pf = vsi->back;
4239 	struct i40e_hw *hw = &pf->hw;
4240 	int base = vsi->base_vector;
4241 	int i;
4242 
4243 	/* disable interrupt causation from each queue */
4244 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4245 		u32 val;
4246 
4247 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4248 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4249 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4250 
4251 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4252 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4253 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4254 
4255 		if (!i40e_enabled_xdp_vsi(vsi))
4256 			continue;
4257 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4258 	}
4259 
4260 	/* disable each interrupt */
4261 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4262 		for (i = vsi->base_vector;
4263 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4264 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4265 
4266 		i40e_flush(hw);
4267 		for (i = 0; i < vsi->num_q_vectors; i++)
4268 			synchronize_irq(pf->msix_entries[i + base].vector);
4269 	} else {
4270 		/* Legacy and MSI mode - this stops all interrupt handling */
4271 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4272 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4273 		i40e_flush(hw);
4274 		synchronize_irq(pf->pdev->irq);
4275 	}
4276 }
4277 
4278 /**
4279  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4280  * @vsi: the VSI being configured
4281  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4282 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4283 {
4284 	struct i40e_pf *pf = vsi->back;
4285 	int i;
4286 
4287 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4288 		for (i = 0; i < vsi->num_q_vectors; i++)
4289 			i40e_irq_dynamic_enable(vsi, i);
4290 	} else {
4291 		i40e_irq_dynamic_enable_icr0(pf);
4292 	}
4293 
4294 	i40e_flush(&pf->hw);
4295 	return 0;
4296 }
4297 
4298 /**
4299  * i40e_free_misc_vector - Free the vector that handles non-queue events
4300  * @pf: board private structure
4301  **/
i40e_free_misc_vector(struct i40e_pf * pf)4302 static void i40e_free_misc_vector(struct i40e_pf *pf)
4303 {
4304 	/* Disable ICR 0 */
4305 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4306 	i40e_flush(&pf->hw);
4307 
4308 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4309 		free_irq(pf->msix_entries[0].vector, pf);
4310 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4311 	}
4312 }
4313 
4314 /**
4315  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4316  * @irq: interrupt number
4317  * @data: pointer to a q_vector
4318  *
4319  * This is the handler used for all MSI/Legacy interrupts, and deals
4320  * with both queue and non-queue interrupts.  This is also used in
4321  * MSIX mode to handle the non-queue interrupts.
4322  **/
i40e_intr(int irq,void * data)4323 static irqreturn_t i40e_intr(int irq, void *data)
4324 {
4325 	struct i40e_pf *pf = (struct i40e_pf *)data;
4326 	struct i40e_hw *hw = &pf->hw;
4327 	irqreturn_t ret = IRQ_NONE;
4328 	u32 icr0, icr0_remaining;
4329 	u32 val, ena_mask;
4330 
4331 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4332 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4333 
4334 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4335 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4336 		goto enable_intr;
4337 
4338 	/* if interrupt but no bits showing, must be SWINT */
4339 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4340 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4341 		pf->sw_int_count++;
4342 
4343 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4344 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4345 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4346 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4347 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4348 	}
4349 
4350 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4351 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4352 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4353 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4354 
4355 		/* We do not have a way to disarm Queue causes while leaving
4356 		 * interrupt enabled for all other causes, ideally
4357 		 * interrupt should be disabled while we are in NAPI but
4358 		 * this is not a performance path and napi_schedule()
4359 		 * can deal with rescheduling.
4360 		 */
4361 		if (!test_bit(__I40E_DOWN, pf->state))
4362 			napi_schedule_irqoff(&q_vector->napi);
4363 	}
4364 
4365 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4366 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4367 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4368 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4369 	}
4370 
4371 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4372 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4373 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4374 	}
4375 
4376 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4377 		/* disable any further VFLR event notifications */
4378 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4379 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4380 
4381 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4382 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4383 		} else {
4384 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4385 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4386 		}
4387 	}
4388 
4389 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4390 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4391 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4392 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4393 		val = rd32(hw, I40E_GLGEN_RSTAT);
4394 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4395 		if (val == I40E_RESET_CORER) {
4396 			pf->corer_count++;
4397 		} else if (val == I40E_RESET_GLOBR) {
4398 			pf->globr_count++;
4399 		} else if (val == I40E_RESET_EMPR) {
4400 			pf->empr_count++;
4401 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4402 		}
4403 	}
4404 
4405 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4406 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4407 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4408 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4409 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4410 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4411 	}
4412 
4413 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4414 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4415 
4416 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4417 			schedule_work(&pf->ptp_extts0_work);
4418 
4419 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4420 			i40e_ptp_tx_hwtstamp(pf);
4421 
4422 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4423 	}
4424 
4425 	/* If a critical error is pending we have no choice but to reset the
4426 	 * device.
4427 	 * Report and mask out any remaining unexpected interrupts.
4428 	 */
4429 	icr0_remaining = icr0 & ena_mask;
4430 	if (icr0_remaining) {
4431 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4432 			 icr0_remaining);
4433 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4434 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4435 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4436 			dev_info(&pf->pdev->dev, "device will be reset\n");
4437 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4438 			i40e_service_event_schedule(pf);
4439 		}
4440 		ena_mask &= ~icr0_remaining;
4441 	}
4442 	ret = IRQ_HANDLED;
4443 
4444 enable_intr:
4445 	/* re-enable interrupt causes */
4446 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4447 	if (!test_bit(__I40E_DOWN, pf->state) ||
4448 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4449 		i40e_service_event_schedule(pf);
4450 		i40e_irq_dynamic_enable_icr0(pf);
4451 	}
4452 
4453 	return ret;
4454 }
4455 
4456 /**
4457  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4458  * @tx_ring:  tx ring to clean
4459  * @budget:   how many cleans we're allowed
4460  *
4461  * Returns true if there's any budget left (e.g. the clean is finished)
4462  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4463 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4464 {
4465 	struct i40e_vsi *vsi = tx_ring->vsi;
4466 	u16 i = tx_ring->next_to_clean;
4467 	struct i40e_tx_buffer *tx_buf;
4468 	struct i40e_tx_desc *tx_desc;
4469 
4470 	tx_buf = &tx_ring->tx_bi[i];
4471 	tx_desc = I40E_TX_DESC(tx_ring, i);
4472 	i -= tx_ring->count;
4473 
4474 	do {
4475 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4476 
4477 		/* if next_to_watch is not set then there is no work pending */
4478 		if (!eop_desc)
4479 			break;
4480 
4481 		/* prevent any other reads prior to eop_desc */
4482 		smp_rmb();
4483 
4484 		/* if the descriptor isn't done, no work yet to do */
4485 		if (!(eop_desc->cmd_type_offset_bsz &
4486 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4487 			break;
4488 
4489 		/* clear next_to_watch to prevent false hangs */
4490 		tx_buf->next_to_watch = NULL;
4491 
4492 		tx_desc->buffer_addr = 0;
4493 		tx_desc->cmd_type_offset_bsz = 0;
4494 		/* move past filter desc */
4495 		tx_buf++;
4496 		tx_desc++;
4497 		i++;
4498 		if (unlikely(!i)) {
4499 			i -= tx_ring->count;
4500 			tx_buf = tx_ring->tx_bi;
4501 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4502 		}
4503 		/* unmap skb header data */
4504 		dma_unmap_single(tx_ring->dev,
4505 				 dma_unmap_addr(tx_buf, dma),
4506 				 dma_unmap_len(tx_buf, len),
4507 				 DMA_TO_DEVICE);
4508 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4509 			kfree(tx_buf->raw_buf);
4510 
4511 		tx_buf->raw_buf = NULL;
4512 		tx_buf->tx_flags = 0;
4513 		tx_buf->next_to_watch = NULL;
4514 		dma_unmap_len_set(tx_buf, len, 0);
4515 		tx_desc->buffer_addr = 0;
4516 		tx_desc->cmd_type_offset_bsz = 0;
4517 
4518 		/* move us past the eop_desc for start of next FD desc */
4519 		tx_buf++;
4520 		tx_desc++;
4521 		i++;
4522 		if (unlikely(!i)) {
4523 			i -= tx_ring->count;
4524 			tx_buf = tx_ring->tx_bi;
4525 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4526 		}
4527 
4528 		/* update budget accounting */
4529 		budget--;
4530 	} while (likely(budget));
4531 
4532 	i += tx_ring->count;
4533 	tx_ring->next_to_clean = i;
4534 
4535 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4536 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4537 
4538 	return budget > 0;
4539 }
4540 
4541 /**
4542  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4543  * @irq: interrupt number
4544  * @data: pointer to a q_vector
4545  **/
i40e_fdir_clean_ring(int irq,void * data)4546 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4547 {
4548 	struct i40e_q_vector *q_vector = data;
4549 	struct i40e_vsi *vsi;
4550 
4551 	if (!q_vector->tx.ring)
4552 		return IRQ_HANDLED;
4553 
4554 	vsi = q_vector->tx.ring->vsi;
4555 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4556 
4557 	return IRQ_HANDLED;
4558 }
4559 
4560 /**
4561  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4562  * @vsi: the VSI being configured
4563  * @v_idx: vector index
4564  * @qp_idx: queue pair index
4565  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4566 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4567 {
4568 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4569 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4570 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4571 
4572 	tx_ring->q_vector = q_vector;
4573 	tx_ring->next = q_vector->tx.ring;
4574 	q_vector->tx.ring = tx_ring;
4575 	q_vector->tx.count++;
4576 
4577 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4578 	if (i40e_enabled_xdp_vsi(vsi)) {
4579 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4580 
4581 		xdp_ring->q_vector = q_vector;
4582 		xdp_ring->next = q_vector->tx.ring;
4583 		q_vector->tx.ring = xdp_ring;
4584 		q_vector->tx.count++;
4585 	}
4586 
4587 	rx_ring->q_vector = q_vector;
4588 	rx_ring->next = q_vector->rx.ring;
4589 	q_vector->rx.ring = rx_ring;
4590 	q_vector->rx.count++;
4591 }
4592 
4593 /**
4594  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4595  * @vsi: the VSI being configured
4596  *
4597  * This function maps descriptor rings to the queue-specific vectors
4598  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4599  * one vector per queue pair, but on a constrained vector budget, we
4600  * group the queue pairs as "efficiently" as possible.
4601  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4602 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4603 {
4604 	int qp_remaining = vsi->num_queue_pairs;
4605 	int q_vectors = vsi->num_q_vectors;
4606 	int num_ringpairs;
4607 	int v_start = 0;
4608 	int qp_idx = 0;
4609 
4610 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4611 	 * group them so there are multiple queues per vector.
4612 	 * It is also important to go through all the vectors available to be
4613 	 * sure that if we don't use all the vectors, that the remaining vectors
4614 	 * are cleared. This is especially important when decreasing the
4615 	 * number of queues in use.
4616 	 */
4617 	for (; v_start < q_vectors; v_start++) {
4618 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4619 
4620 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4621 
4622 		q_vector->num_ringpairs = num_ringpairs;
4623 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4624 
4625 		q_vector->rx.count = 0;
4626 		q_vector->tx.count = 0;
4627 		q_vector->rx.ring = NULL;
4628 		q_vector->tx.ring = NULL;
4629 
4630 		while (num_ringpairs--) {
4631 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4632 			qp_idx++;
4633 			qp_remaining--;
4634 		}
4635 	}
4636 }
4637 
4638 /**
4639  * i40e_vsi_request_irq - Request IRQ from the OS
4640  * @vsi: the VSI being configured
4641  * @basename: name for the vector
4642  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4643 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4644 {
4645 	struct i40e_pf *pf = vsi->back;
4646 	int err;
4647 
4648 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4649 		err = i40e_vsi_request_irq_msix(vsi, basename);
4650 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4651 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4652 				  pf->int_name, pf);
4653 	else
4654 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4655 				  pf->int_name, pf);
4656 
4657 	if (err)
4658 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4659 
4660 	return err;
4661 }
4662 
4663 #ifdef CONFIG_NET_POLL_CONTROLLER
4664 /**
4665  * i40e_netpoll - A Polling 'interrupt' handler
4666  * @netdev: network interface device structure
4667  *
4668  * This is used by netconsole to send skbs without having to re-enable
4669  * interrupts.  It's not called while the normal interrupt routine is executing.
4670  **/
i40e_netpoll(struct net_device * netdev)4671 static void i40e_netpoll(struct net_device *netdev)
4672 {
4673 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4674 	struct i40e_vsi *vsi = np->vsi;
4675 	struct i40e_pf *pf = vsi->back;
4676 	int i;
4677 
4678 	/* if interface is down do nothing */
4679 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4680 		return;
4681 
4682 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4683 		for (i = 0; i < vsi->num_q_vectors; i++)
4684 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4685 	} else {
4686 		i40e_intr(pf->pdev->irq, netdev);
4687 	}
4688 }
4689 #endif
4690 
4691 #define I40E_QTX_ENA_WAIT_COUNT 50
4692 
4693 /**
4694  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4695  * @pf: the PF being configured
4696  * @pf_q: the PF queue
4697  * @enable: enable or disable state of the queue
4698  *
4699  * This routine will wait for the given Tx queue of the PF to reach the
4700  * enabled or disabled state.
4701  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4702  * multiple retries; else will return 0 in case of success.
4703  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4704 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4705 {
4706 	int i;
4707 	u32 tx_reg;
4708 
4709 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4710 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4711 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4712 			break;
4713 
4714 		usleep_range(10, 20);
4715 	}
4716 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4717 		return -ETIMEDOUT;
4718 
4719 	return 0;
4720 }
4721 
4722 /**
4723  * i40e_control_tx_q - Start or stop a particular Tx queue
4724  * @pf: the PF structure
4725  * @pf_q: the PF queue to configure
4726  * @enable: start or stop the queue
4727  *
4728  * This function enables or disables a single queue. Note that any delay
4729  * required after the operation is expected to be handled by the caller of
4730  * this function.
4731  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4732 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4733 {
4734 	struct i40e_hw *hw = &pf->hw;
4735 	u32 tx_reg;
4736 	int i;
4737 
4738 	/* warn the TX unit of coming changes */
4739 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4740 	if (!enable)
4741 		usleep_range(10, 20);
4742 
4743 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4744 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4745 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4746 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4747 			break;
4748 		usleep_range(1000, 2000);
4749 	}
4750 
4751 	/* Skip if the queue is already in the requested state */
4752 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4753 		return;
4754 
4755 	/* turn on/off the queue */
4756 	if (enable) {
4757 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4758 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4759 	} else {
4760 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4761 	}
4762 
4763 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4764 }
4765 
4766 /**
4767  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4768  * @seid: VSI SEID
4769  * @pf: the PF structure
4770  * @pf_q: the PF queue to configure
4771  * @is_xdp: true if the queue is used for XDP
4772  * @enable: start or stop the queue
4773  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4774 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4775 			   bool is_xdp, bool enable)
4776 {
4777 	int ret;
4778 
4779 	i40e_control_tx_q(pf, pf_q, enable);
4780 
4781 	/* wait for the change to finish */
4782 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4783 	if (ret) {
4784 		dev_info(&pf->pdev->dev,
4785 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4786 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4787 			 (enable ? "en" : "dis"));
4788 	}
4789 
4790 	return ret;
4791 }
4792 
4793 /**
4794  * i40e_vsi_enable_tx - Start a VSI's rings
4795  * @vsi: the VSI being configured
4796  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4797 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4798 {
4799 	struct i40e_pf *pf = vsi->back;
4800 	int i, pf_q, ret = 0;
4801 
4802 	pf_q = vsi->base_queue;
4803 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4804 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4805 					     pf_q,
4806 					     false /*is xdp*/, true);
4807 		if (ret)
4808 			break;
4809 
4810 		if (!i40e_enabled_xdp_vsi(vsi))
4811 			continue;
4812 
4813 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4814 					     pf_q + vsi->alloc_queue_pairs,
4815 					     true /*is xdp*/, true);
4816 		if (ret)
4817 			break;
4818 	}
4819 	return ret;
4820 }
4821 
4822 /**
4823  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4824  * @pf: the PF being configured
4825  * @pf_q: the PF queue
4826  * @enable: enable or disable state of the queue
4827  *
4828  * This routine will wait for the given Rx queue of the PF to reach the
4829  * enabled or disabled state.
4830  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4831  * multiple retries; else will return 0 in case of success.
4832  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4833 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4834 {
4835 	int i;
4836 	u32 rx_reg;
4837 
4838 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4839 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4840 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4841 			break;
4842 
4843 		usleep_range(10, 20);
4844 	}
4845 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4846 		return -ETIMEDOUT;
4847 
4848 	return 0;
4849 }
4850 
4851 /**
4852  * i40e_control_rx_q - Start or stop a particular Rx queue
4853  * @pf: the PF structure
4854  * @pf_q: the PF queue to configure
4855  * @enable: start or stop the queue
4856  *
4857  * This function enables or disables a single queue. Note that
4858  * any delay required after the operation is expected to be
4859  * handled by the caller of this function.
4860  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4861 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4862 {
4863 	struct i40e_hw *hw = &pf->hw;
4864 	u32 rx_reg;
4865 	int i;
4866 
4867 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4868 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4869 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4870 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4871 			break;
4872 		usleep_range(1000, 2000);
4873 	}
4874 
4875 	/* Skip if the queue is already in the requested state */
4876 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4877 		return;
4878 
4879 	/* turn on/off the queue */
4880 	if (enable)
4881 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4882 	else
4883 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4884 
4885 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4886 }
4887 
4888 /**
4889  * i40e_control_wait_rx_q
4890  * @pf: the PF structure
4891  * @pf_q: queue being configured
4892  * @enable: start or stop the rings
4893  *
4894  * This function enables or disables a single queue along with waiting
4895  * for the change to finish. The caller of this function should handle
4896  * the delays needed in the case of disabling queues.
4897  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4898 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4899 {
4900 	int ret = 0;
4901 
4902 	i40e_control_rx_q(pf, pf_q, enable);
4903 
4904 	/* wait for the change to finish */
4905 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4906 	if (ret)
4907 		return ret;
4908 
4909 	return ret;
4910 }
4911 
4912 /**
4913  * i40e_vsi_enable_rx - Start a VSI's rings
4914  * @vsi: the VSI being configured
4915  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4916 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4917 {
4918 	struct i40e_pf *pf = vsi->back;
4919 	int i, pf_q, ret = 0;
4920 
4921 	pf_q = vsi->base_queue;
4922 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4923 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4924 		if (ret) {
4925 			dev_info(&pf->pdev->dev,
4926 				 "VSI seid %d Rx ring %d enable timeout\n",
4927 				 vsi->seid, pf_q);
4928 			break;
4929 		}
4930 	}
4931 
4932 	return ret;
4933 }
4934 
4935 /**
4936  * i40e_vsi_start_rings - Start a VSI's rings
4937  * @vsi: the VSI being configured
4938  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4939 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4940 {
4941 	int ret = 0;
4942 
4943 	/* do rx first for enable and last for disable */
4944 	ret = i40e_vsi_enable_rx(vsi);
4945 	if (ret)
4946 		return ret;
4947 	ret = i40e_vsi_enable_tx(vsi);
4948 
4949 	return ret;
4950 }
4951 
4952 #define I40E_DISABLE_TX_GAP_MSEC	50
4953 
4954 /**
4955  * i40e_vsi_stop_rings - Stop a VSI's rings
4956  * @vsi: the VSI being configured
4957  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4958 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4959 {
4960 	struct i40e_pf *pf = vsi->back;
4961 	u32 pf_q, tx_q_end, rx_q_end;
4962 
4963 	/* When port TX is suspended, don't wait */
4964 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4965 		return i40e_vsi_stop_rings_no_wait(vsi);
4966 
4967 	tx_q_end = vsi->base_queue +
4968 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4969 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4970 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4971 
4972 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4973 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4974 		i40e_control_rx_q(pf, pf_q, false);
4975 
4976 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4977 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4978 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4979 
4980 	i40e_vsi_wait_queues_disabled(vsi);
4981 }
4982 
4983 /**
4984  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4985  * @vsi: the VSI being shutdown
4986  *
4987  * This function stops all the rings for a VSI but does not delay to verify
4988  * that rings have been disabled. It is expected that the caller is shutting
4989  * down multiple VSIs at once and will delay together for all the VSIs after
4990  * initiating the shutdown. This is particularly useful for shutting down lots
4991  * of VFs together. Otherwise, a large delay can be incurred while configuring
4992  * each VSI in serial.
4993  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4994 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4995 {
4996 	struct i40e_pf *pf = vsi->back;
4997 	int i, pf_q;
4998 
4999 	pf_q = vsi->base_queue;
5000 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5001 		i40e_control_tx_q(pf, pf_q, false);
5002 		i40e_control_rx_q(pf, pf_q, false);
5003 	}
5004 }
5005 
5006 /**
5007  * i40e_vsi_free_irq - Free the irq association with the OS
5008  * @vsi: the VSI being configured
5009  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)5010 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
5011 {
5012 	struct i40e_pf *pf = vsi->back;
5013 	struct i40e_hw *hw = &pf->hw;
5014 	int base = vsi->base_vector;
5015 	u32 val, qp;
5016 	int i;
5017 
5018 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5019 		if (!vsi->q_vectors)
5020 			return;
5021 
5022 		if (!vsi->irqs_ready)
5023 			return;
5024 
5025 		vsi->irqs_ready = false;
5026 		for (i = 0; i < vsi->num_q_vectors; i++) {
5027 			int irq_num;
5028 			u16 vector;
5029 
5030 			vector = i + base;
5031 			irq_num = pf->msix_entries[vector].vector;
5032 
5033 			/* free only the irqs that were actually requested */
5034 			if (!vsi->q_vectors[i] ||
5035 			    !vsi->q_vectors[i]->num_ringpairs)
5036 				continue;
5037 
5038 			/* clear the affinity notifier in the IRQ descriptor */
5039 			irq_set_affinity_notifier(irq_num, NULL);
5040 			/* remove our suggested affinity mask for this IRQ */
5041 			irq_update_affinity_hint(irq_num, NULL);
5042 			free_irq(irq_num, vsi->q_vectors[i]);
5043 
5044 			/* Tear down the interrupt queue link list
5045 			 *
5046 			 * We know that they come in pairs and always
5047 			 * the Rx first, then the Tx.  To clear the
5048 			 * link list, stick the EOL value into the
5049 			 * next_q field of the registers.
5050 			 */
5051 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5052 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5053 				       val);
5054 			val |= I40E_QUEUE_END_OF_LIST
5055 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5056 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5057 
5058 			while (qp != I40E_QUEUE_END_OF_LIST) {
5059 				u32 next;
5060 
5061 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5062 
5063 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5064 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5065 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5066 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5067 
5068 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5069 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5070 
5071 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5072 
5073 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5074 
5075 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5076 						 val);
5077 
5078 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5079 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5080 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5081 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5082 
5083 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5084 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5085 
5086 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5087 				qp = next;
5088 			}
5089 		}
5090 	} else {
5091 		free_irq(pf->pdev->irq, pf);
5092 
5093 		val = rd32(hw, I40E_PFINT_LNKLST0);
5094 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5095 		val |= I40E_QUEUE_END_OF_LIST
5096 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5097 		wr32(hw, I40E_PFINT_LNKLST0, val);
5098 
5099 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5100 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5101 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5102 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5103 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5104 
5105 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5106 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5107 
5108 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5109 
5110 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5111 
5112 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5113 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5114 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5115 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5116 
5117 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5118 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5119 
5120 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5121 	}
5122 }
5123 
5124 /**
5125  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5126  * @vsi: the VSI being configured
5127  * @v_idx: Index of vector to be freed
5128  *
5129  * This function frees the memory allocated to the q_vector.  In addition if
5130  * NAPI is enabled it will delete any references to the NAPI struct prior
5131  * to freeing the q_vector.
5132  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5133 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5134 {
5135 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5136 	struct i40e_ring *ring;
5137 
5138 	if (!q_vector)
5139 		return;
5140 
5141 	/* disassociate q_vector from rings */
5142 	i40e_for_each_ring(ring, q_vector->tx)
5143 		ring->q_vector = NULL;
5144 
5145 	i40e_for_each_ring(ring, q_vector->rx)
5146 		ring->q_vector = NULL;
5147 
5148 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5149 	if (vsi->netdev)
5150 		netif_napi_del(&q_vector->napi);
5151 
5152 	vsi->q_vectors[v_idx] = NULL;
5153 
5154 	kfree_rcu(q_vector, rcu);
5155 }
5156 
5157 /**
5158  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5159  * @vsi: the VSI being un-configured
5160  *
5161  * This frees the memory allocated to the q_vectors and
5162  * deletes references to the NAPI struct.
5163  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5164 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5165 {
5166 	int v_idx;
5167 
5168 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5169 		i40e_free_q_vector(vsi, v_idx);
5170 }
5171 
5172 /**
5173  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5174  * @pf: board private structure
5175  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5176 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5177 {
5178 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5179 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5180 		pci_disable_msix(pf->pdev);
5181 		kfree(pf->msix_entries);
5182 		pf->msix_entries = NULL;
5183 		kfree(pf->irq_pile);
5184 		pf->irq_pile = NULL;
5185 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5186 		pci_disable_msi(pf->pdev);
5187 	}
5188 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5189 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5190 }
5191 
5192 /**
5193  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5194  * @pf: board private structure
5195  *
5196  * We go through and clear interrupt specific resources and reset the structure
5197  * to pre-load conditions
5198  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5199 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5200 {
5201 	struct i40e_vsi *vsi;
5202 	int i;
5203 
5204 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5205 		i40e_free_misc_vector(pf);
5206 
5207 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5208 		      I40E_IWARP_IRQ_PILE_ID);
5209 
5210 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5211 
5212 	i40e_pf_for_each_vsi(pf, i, vsi)
5213 		i40e_vsi_free_q_vectors(vsi);
5214 
5215 	i40e_reset_interrupt_capability(pf);
5216 }
5217 
5218 /**
5219  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5220  * @vsi: the VSI being configured
5221  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5222 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5223 {
5224 	int q_idx;
5225 
5226 	if (!vsi->netdev)
5227 		return;
5228 
5229 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5230 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5231 
5232 		if (q_vector->rx.ring || q_vector->tx.ring)
5233 			napi_enable(&q_vector->napi);
5234 	}
5235 }
5236 
5237 /**
5238  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5239  * @vsi: the VSI being configured
5240  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5241 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5242 {
5243 	int q_idx;
5244 
5245 	if (!vsi->netdev)
5246 		return;
5247 
5248 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5249 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5250 
5251 		if (q_vector->rx.ring || q_vector->tx.ring)
5252 			napi_disable(&q_vector->napi);
5253 	}
5254 }
5255 
5256 /**
5257  * i40e_vsi_close - Shut down a VSI
5258  * @vsi: the vsi to be quelled
5259  **/
i40e_vsi_close(struct i40e_vsi * vsi)5260 static void i40e_vsi_close(struct i40e_vsi *vsi)
5261 {
5262 	struct i40e_pf *pf = vsi->back;
5263 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5264 		i40e_down(vsi);
5265 	i40e_vsi_free_irq(vsi);
5266 	i40e_vsi_free_tx_resources(vsi);
5267 	i40e_vsi_free_rx_resources(vsi);
5268 	vsi->current_netdev_flags = 0;
5269 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5270 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5271 		set_bit(__I40E_CLIENT_RESET, pf->state);
5272 }
5273 
5274 /**
5275  * i40e_quiesce_vsi - Pause a given VSI
5276  * @vsi: the VSI being paused
5277  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5278 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5279 {
5280 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5281 		return;
5282 
5283 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5284 	if (vsi->netdev && netif_running(vsi->netdev))
5285 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5286 	else
5287 		i40e_vsi_close(vsi);
5288 }
5289 
5290 /**
5291  * i40e_unquiesce_vsi - Resume a given VSI
5292  * @vsi: the VSI being resumed
5293  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5294 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5295 {
5296 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5297 		return;
5298 
5299 	if (vsi->netdev && netif_running(vsi->netdev))
5300 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5301 	else
5302 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5303 }
5304 
5305 /**
5306  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5307  * @pf: the PF
5308  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5309 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5310 {
5311 	struct i40e_vsi *vsi;
5312 	int v;
5313 
5314 	i40e_pf_for_each_vsi(pf, v, vsi)
5315 		i40e_quiesce_vsi(vsi);
5316 }
5317 
5318 /**
5319  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5320  * @pf: the PF
5321  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5322 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5323 {
5324 	struct i40e_vsi *vsi;
5325 	int v;
5326 
5327 	i40e_pf_for_each_vsi(pf, v, vsi)
5328 		i40e_unquiesce_vsi(vsi);
5329 }
5330 
5331 /**
5332  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5333  * @vsi: the VSI being configured
5334  *
5335  * Wait until all queues on a given VSI have been disabled.
5336  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5337 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5338 {
5339 	struct i40e_pf *pf = vsi->back;
5340 	int i, pf_q, ret;
5341 
5342 	pf_q = vsi->base_queue;
5343 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5344 		/* Check and wait for the Tx queue */
5345 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5346 		if (ret) {
5347 			dev_info(&pf->pdev->dev,
5348 				 "VSI seid %d Tx ring %d disable timeout\n",
5349 				 vsi->seid, pf_q);
5350 			return ret;
5351 		}
5352 
5353 		if (!i40e_enabled_xdp_vsi(vsi))
5354 			goto wait_rx;
5355 
5356 		/* Check and wait for the XDP Tx queue */
5357 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5358 				       false);
5359 		if (ret) {
5360 			dev_info(&pf->pdev->dev,
5361 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5362 				 vsi->seid, pf_q);
5363 			return ret;
5364 		}
5365 wait_rx:
5366 		/* Check and wait for the Rx queue */
5367 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5368 		if (ret) {
5369 			dev_info(&pf->pdev->dev,
5370 				 "VSI seid %d Rx ring %d disable timeout\n",
5371 				 vsi->seid, pf_q);
5372 			return ret;
5373 		}
5374 	}
5375 
5376 	return 0;
5377 }
5378 
5379 #ifdef CONFIG_I40E_DCB
5380 /**
5381  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5382  * @pf: the PF
5383  *
5384  * This function waits for the queues to be in disabled state for all the
5385  * VSIs that are managed by this PF.
5386  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5387 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5388 {
5389 	struct i40e_vsi *vsi;
5390 	int v, ret = 0;
5391 
5392 	i40e_pf_for_each_vsi(pf, v, vsi) {
5393 		ret = i40e_vsi_wait_queues_disabled(vsi);
5394 		if (ret)
5395 			break;
5396 	}
5397 
5398 	return ret;
5399 }
5400 
5401 #endif
5402 
5403 /**
5404  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5405  * @pf: pointer to PF
5406  *
5407  * Get TC map for ISCSI PF type that will include iSCSI TC
5408  * and LAN TC.
5409  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5410 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5411 {
5412 	struct i40e_dcb_app_priority_table app;
5413 	struct i40e_hw *hw = &pf->hw;
5414 	u8 enabled_tc = 1; /* TC0 is always enabled */
5415 	u8 tc, i;
5416 	/* Get the iSCSI APP TLV */
5417 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5418 
5419 	for (i = 0; i < dcbcfg->numapps; i++) {
5420 		app = dcbcfg->app[i];
5421 		if (app.selector == I40E_APP_SEL_TCPIP &&
5422 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5423 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5424 			enabled_tc |= BIT(tc);
5425 			break;
5426 		}
5427 	}
5428 
5429 	return enabled_tc;
5430 }
5431 
5432 /**
5433  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5434  * @dcbcfg: the corresponding DCBx configuration structure
5435  *
5436  * Return the number of TCs from given DCBx configuration
5437  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5438 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5439 {
5440 	int i, tc_unused = 0;
5441 	u8 num_tc = 0;
5442 	u8 ret = 0;
5443 
5444 	/* Scan the ETS Config Priority Table to find
5445 	 * traffic class enabled for a given priority
5446 	 * and create a bitmask of enabled TCs
5447 	 */
5448 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5449 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5450 
5451 	/* Now scan the bitmask to check for
5452 	 * contiguous TCs starting with TC0
5453 	 */
5454 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5455 		if (num_tc & BIT(i)) {
5456 			if (!tc_unused) {
5457 				ret++;
5458 			} else {
5459 				pr_err("Non-contiguous TC - Disabling DCB\n");
5460 				return 1;
5461 			}
5462 		} else {
5463 			tc_unused = 1;
5464 		}
5465 	}
5466 
5467 	/* There is always at least TC0 */
5468 	if (!ret)
5469 		ret = 1;
5470 
5471 	return ret;
5472 }
5473 
5474 /**
5475  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5476  * @dcbcfg: the corresponding DCBx configuration structure
5477  *
5478  * Query the current DCB configuration and return the number of
5479  * traffic classes enabled from the given DCBX config
5480  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5481 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5482 {
5483 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5484 	u8 enabled_tc = 1;
5485 	u8 i;
5486 
5487 	for (i = 0; i < num_tc; i++)
5488 		enabled_tc |= BIT(i);
5489 
5490 	return enabled_tc;
5491 }
5492 
5493 /**
5494  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5495  * @pf: PF being queried
5496  *
5497  * Query the current MQPRIO configuration and return the number of
5498  * traffic classes enabled.
5499  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5500 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5501 {
5502 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5503 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5504 	u8 enabled_tc = 1, i;
5505 
5506 	for (i = 1; i < num_tc; i++)
5507 		enabled_tc |= BIT(i);
5508 	return enabled_tc;
5509 }
5510 
5511 /**
5512  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5513  * @pf: PF being queried
5514  *
5515  * Return number of traffic classes enabled for the given PF
5516  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5517 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5518 {
5519 	u8 i, enabled_tc = 1;
5520 	u8 num_tc = 0;
5521 
5522 	if (i40e_is_tc_mqprio_enabled(pf)) {
5523 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5524 
5525 		return vsi->mqprio_qopt.qopt.num_tc;
5526 	}
5527 
5528 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5529 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5530 		return 1;
5531 
5532 	/* SFP mode will be enabled for all TCs on port */
5533 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5534 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5535 
5536 	/* MFP mode return count of enabled TCs for this PF */
5537 	if (pf->hw.func_caps.iscsi)
5538 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5539 	else
5540 		return 1; /* Only TC0 */
5541 
5542 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5543 		if (enabled_tc & BIT(i))
5544 			num_tc++;
5545 	}
5546 	return num_tc;
5547 }
5548 
5549 /**
5550  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5551  * @pf: PF being queried
5552  *
5553  * Return a bitmap for enabled traffic classes for this PF.
5554  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5555 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5556 {
5557 	if (i40e_is_tc_mqprio_enabled(pf))
5558 		return i40e_mqprio_get_enabled_tc(pf);
5559 
5560 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5561 	 * default TC
5562 	 */
5563 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5564 		return I40E_DEFAULT_TRAFFIC_CLASS;
5565 
5566 	/* SFP mode we want PF to be enabled for all TCs */
5567 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5568 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5569 
5570 	/* MFP enabled and iSCSI PF type */
5571 	if (pf->hw.func_caps.iscsi)
5572 		return i40e_get_iscsi_tc_map(pf);
5573 	else
5574 		return I40E_DEFAULT_TRAFFIC_CLASS;
5575 }
5576 
5577 /**
5578  * i40e_vsi_get_bw_info - Query VSI BW Information
5579  * @vsi: the VSI being queried
5580  *
5581  * Returns 0 on success, negative value on failure
5582  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5583 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5584 {
5585 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5586 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5587 	struct i40e_pf *pf = vsi->back;
5588 	struct i40e_hw *hw = &pf->hw;
5589 	u32 tc_bw_max;
5590 	int ret;
5591 	int i;
5592 
5593 	/* Get the VSI level BW configuration */
5594 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5595 	if (ret) {
5596 		dev_info(&pf->pdev->dev,
5597 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5598 			 ERR_PTR(ret),
5599 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5600 		return -EINVAL;
5601 	}
5602 
5603 	/* Get the VSI level BW configuration per TC */
5604 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5605 					       NULL);
5606 	if (ret) {
5607 		dev_info(&pf->pdev->dev,
5608 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5609 			 ERR_PTR(ret),
5610 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5611 		return -EINVAL;
5612 	}
5613 
5614 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5615 		dev_info(&pf->pdev->dev,
5616 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5617 			 bw_config.tc_valid_bits,
5618 			 bw_ets_config.tc_valid_bits);
5619 		/* Still continuing */
5620 	}
5621 
5622 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5623 	vsi->bw_max_quanta = bw_config.max_bw;
5624 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5625 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5626 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5627 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5628 		vsi->bw_ets_limit_credits[i] =
5629 					le16_to_cpu(bw_ets_config.credits[i]);
5630 		/* 3 bits out of 4 for each TC */
5631 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5632 	}
5633 
5634 	return 0;
5635 }
5636 
5637 /**
5638  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5639  * @vsi: the VSI being configured
5640  * @enabled_tc: TC bitmap
5641  * @bw_share: BW shared credits per TC
5642  *
5643  * Returns 0 on success, negative value on failure
5644  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5645 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5646 				       u8 *bw_share)
5647 {
5648 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5649 	struct i40e_pf *pf = vsi->back;
5650 	int ret;
5651 	int i;
5652 
5653 	/* There is no need to reset BW when mqprio mode is on.  */
5654 	if (i40e_is_tc_mqprio_enabled(pf))
5655 		return 0;
5656 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5657 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5658 		if (ret)
5659 			dev_info(&pf->pdev->dev,
5660 				 "Failed to reset tx rate for vsi->seid %u\n",
5661 				 vsi->seid);
5662 		return ret;
5663 	}
5664 	memset(&bw_data, 0, sizeof(bw_data));
5665 	bw_data.tc_valid_bits = enabled_tc;
5666 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5667 		bw_data.tc_bw_credits[i] = bw_share[i];
5668 
5669 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5670 	if (ret) {
5671 		dev_info(&pf->pdev->dev,
5672 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5673 			 pf->hw.aq.asq_last_status);
5674 		return -EINVAL;
5675 	}
5676 
5677 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5678 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5679 
5680 	return 0;
5681 }
5682 
5683 /**
5684  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5685  * @vsi: the VSI being configured
5686  * @enabled_tc: TC map to be enabled
5687  *
5688  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5689 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5690 {
5691 	struct net_device *netdev = vsi->netdev;
5692 	struct i40e_pf *pf = vsi->back;
5693 	struct i40e_hw *hw = &pf->hw;
5694 	u8 netdev_tc = 0;
5695 	int i;
5696 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5697 
5698 	if (!netdev)
5699 		return;
5700 
5701 	if (!enabled_tc) {
5702 		netdev_reset_tc(netdev);
5703 		return;
5704 	}
5705 
5706 	/* Set up actual enabled TCs on the VSI */
5707 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5708 		return;
5709 
5710 	/* set per TC queues for the VSI */
5711 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5712 		/* Only set TC queues for enabled tcs
5713 		 *
5714 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5715 		 * enabled_tc bitmap would be 0x00001001; the driver
5716 		 * will set the numtc for netdev as 2 that will be
5717 		 * referenced by the netdev layer as TC 0 and 1.
5718 		 */
5719 		if (vsi->tc_config.enabled_tc & BIT(i))
5720 			netdev_set_tc_queue(netdev,
5721 					vsi->tc_config.tc_info[i].netdev_tc,
5722 					vsi->tc_config.tc_info[i].qcount,
5723 					vsi->tc_config.tc_info[i].qoffset);
5724 	}
5725 
5726 	if (i40e_is_tc_mqprio_enabled(pf))
5727 		return;
5728 
5729 	/* Assign UP2TC map for the VSI */
5730 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5731 		/* Get the actual TC# for the UP */
5732 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5733 		/* Get the mapped netdev TC# for the UP */
5734 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5735 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5736 	}
5737 }
5738 
5739 /**
5740  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5741  * @vsi: the VSI being configured
5742  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5743  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5744 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5745 				      struct i40e_vsi_context *ctxt)
5746 {
5747 	/* copy just the sections touched not the entire info
5748 	 * since not all sections are valid as returned by
5749 	 * update vsi params
5750 	 */
5751 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5752 	memcpy(&vsi->info.queue_mapping,
5753 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5754 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5755 	       sizeof(vsi->info.tc_mapping));
5756 }
5757 
5758 /**
5759  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5760  * @vsi: the VSI being reconfigured
5761  * @vsi_offset: offset from main VF VSI
5762  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5763 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5764 {
5765 	struct i40e_vsi_context ctxt = {};
5766 	struct i40e_pf *pf;
5767 	struct i40e_hw *hw;
5768 	int ret;
5769 
5770 	if (!vsi)
5771 		return -EINVAL;
5772 	pf = vsi->back;
5773 	hw = &pf->hw;
5774 
5775 	ctxt.seid = vsi->seid;
5776 	ctxt.pf_num = hw->pf_id;
5777 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5778 	ctxt.uplink_seid = vsi->uplink_seid;
5779 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5780 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5781 	ctxt.info = vsi->info;
5782 
5783 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5784 				 false);
5785 	if (vsi->reconfig_rss) {
5786 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5787 				      vsi->num_queue_pairs);
5788 		ret = i40e_vsi_config_rss(vsi);
5789 		if (ret) {
5790 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5791 			return ret;
5792 		}
5793 		vsi->reconfig_rss = false;
5794 	}
5795 
5796 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5797 	if (ret) {
5798 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5799 			 ERR_PTR(ret),
5800 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5801 		return ret;
5802 	}
5803 	/* update the local VSI info with updated queue map */
5804 	i40e_vsi_update_queue_map(vsi, &ctxt);
5805 	vsi->info.valid_sections = 0;
5806 
5807 	return ret;
5808 }
5809 
5810 /**
5811  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5812  * @vsi: VSI to be configured
5813  * @enabled_tc: TC bitmap
5814  *
5815  * This configures a particular VSI for TCs that are mapped to the
5816  * given TC bitmap. It uses default bandwidth share for TCs across
5817  * VSIs to configure TC for a particular VSI.
5818  *
5819  * NOTE:
5820  * It is expected that the VSI queues have been quisced before calling
5821  * this function.
5822  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5823 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5824 {
5825 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5826 	struct i40e_pf *pf = vsi->back;
5827 	struct i40e_hw *hw = &pf->hw;
5828 	struct i40e_vsi_context ctxt;
5829 	int ret = 0;
5830 	int i;
5831 
5832 	/* Check if enabled_tc is same as existing or new TCs */
5833 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5834 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5835 		return ret;
5836 
5837 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5838 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5839 		if (enabled_tc & BIT(i))
5840 			bw_share[i] = 1;
5841 	}
5842 
5843 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5844 	if (ret) {
5845 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5846 
5847 		dev_info(&pf->pdev->dev,
5848 			 "Failed configuring TC map %d for VSI %d\n",
5849 			 enabled_tc, vsi->seid);
5850 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5851 						  &bw_config, NULL);
5852 		if (ret) {
5853 			dev_info(&pf->pdev->dev,
5854 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5855 				 ERR_PTR(ret),
5856 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5857 			goto out;
5858 		}
5859 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5860 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5861 
5862 			if (!valid_tc)
5863 				valid_tc = bw_config.tc_valid_bits;
5864 			/* Always enable TC0, no matter what */
5865 			valid_tc |= 1;
5866 			dev_info(&pf->pdev->dev,
5867 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5868 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5869 			enabled_tc = valid_tc;
5870 		}
5871 
5872 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5873 		if (ret) {
5874 			dev_err(&pf->pdev->dev,
5875 				"Unable to  configure TC map %d for VSI %d\n",
5876 				enabled_tc, vsi->seid);
5877 			goto out;
5878 		}
5879 	}
5880 
5881 	/* Update Queue Pairs Mapping for currently enabled UPs */
5882 	ctxt.seid = vsi->seid;
5883 	ctxt.pf_num = vsi->back->hw.pf_id;
5884 	ctxt.vf_num = 0;
5885 	ctxt.uplink_seid = vsi->uplink_seid;
5886 	ctxt.info = vsi->info;
5887 	if (i40e_is_tc_mqprio_enabled(pf)) {
5888 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5889 		if (ret)
5890 			goto out;
5891 	} else {
5892 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5893 	}
5894 
5895 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5896 	 * queues changed.
5897 	 */
5898 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5899 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5900 				      vsi->num_queue_pairs);
5901 		ret = i40e_vsi_config_rss(vsi);
5902 		if (ret) {
5903 			dev_info(&vsi->back->pdev->dev,
5904 				 "Failed to reconfig rss for num_queues\n");
5905 			return ret;
5906 		}
5907 		vsi->reconfig_rss = false;
5908 	}
5909 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5910 		ctxt.info.valid_sections |=
5911 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5912 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5913 	}
5914 
5915 	/* Update the VSI after updating the VSI queue-mapping
5916 	 * information
5917 	 */
5918 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5919 	if (ret) {
5920 		dev_info(&pf->pdev->dev,
5921 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5922 			 ERR_PTR(ret),
5923 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5924 		goto out;
5925 	}
5926 	/* update the local VSI info with updated queue map */
5927 	i40e_vsi_update_queue_map(vsi, &ctxt);
5928 	vsi->info.valid_sections = 0;
5929 
5930 	/* Update current VSI BW information */
5931 	ret = i40e_vsi_get_bw_info(vsi);
5932 	if (ret) {
5933 		dev_info(&pf->pdev->dev,
5934 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5935 			 ERR_PTR(ret),
5936 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5937 		goto out;
5938 	}
5939 
5940 	/* Update the netdev TC setup */
5941 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5942 out:
5943 	return ret;
5944 }
5945 
5946 /**
5947  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5948  * @vsi: VSI to be reconfigured
5949  *
5950  * This reconfigures a particular VSI for TCs that are mapped to the
5951  * TC bitmap stored previously for the VSI.
5952  *
5953  * Context: It is expected that the VSI queues have been quisced before
5954  *          calling this function.
5955  *
5956  * Return: 0 on success, negative value on failure
5957  **/
i40e_vsi_reconfig_tc(struct i40e_vsi * vsi)5958 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5959 {
5960 	u8 enabled_tc;
5961 
5962 	enabled_tc = vsi->tc_config.enabled_tc;
5963 	vsi->tc_config.enabled_tc = 0;
5964 
5965 	return i40e_vsi_config_tc(vsi, enabled_tc);
5966 }
5967 
5968 /**
5969  * i40e_get_link_speed - Returns link speed for the interface
5970  * @vsi: VSI to be configured
5971  *
5972  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5973 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5974 {
5975 	struct i40e_pf *pf = vsi->back;
5976 
5977 	switch (pf->hw.phy.link_info.link_speed) {
5978 	case I40E_LINK_SPEED_40GB:
5979 		return 40000;
5980 	case I40E_LINK_SPEED_25GB:
5981 		return 25000;
5982 	case I40E_LINK_SPEED_20GB:
5983 		return 20000;
5984 	case I40E_LINK_SPEED_10GB:
5985 		return 10000;
5986 	case I40E_LINK_SPEED_1GB:
5987 		return 1000;
5988 	default:
5989 		return -EINVAL;
5990 	}
5991 }
5992 
5993 /**
5994  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5995  * @vsi: Pointer to vsi structure
5996  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5997  *
5998  * Helper function to convert units before send to set BW limit
5999  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)6000 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
6001 {
6002 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
6003 		dev_warn(&vsi->back->pdev->dev,
6004 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
6005 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
6006 	} else {
6007 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6008 	}
6009 
6010 	return max_tx_rate;
6011 }
6012 
6013 /**
6014  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
6015  * @vsi: VSI to be configured
6016  * @seid: seid of the channel/VSI
6017  * @max_tx_rate: max TX rate to be configured as BW limit
6018  *
6019  * Helper function to set BW limit for a given VSI
6020  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)6021 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
6022 {
6023 	struct i40e_pf *pf = vsi->back;
6024 	u64 credits = 0;
6025 	int speed = 0;
6026 	int ret = 0;
6027 
6028 	speed = i40e_get_link_speed(vsi);
6029 	if (max_tx_rate > speed) {
6030 		dev_err(&pf->pdev->dev,
6031 			"Invalid max tx rate %llu specified for VSI seid %d.",
6032 			max_tx_rate, seid);
6033 		return -EINVAL;
6034 	}
6035 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
6036 		dev_warn(&pf->pdev->dev,
6037 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
6038 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
6039 	}
6040 
6041 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
6042 	credits = max_tx_rate;
6043 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
6044 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
6045 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6046 	if (ret)
6047 		dev_err(&pf->pdev->dev,
6048 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6049 			max_tx_rate, seid, ERR_PTR(ret),
6050 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6051 	return ret;
6052 }
6053 
6054 /**
6055  * i40e_remove_queue_channels - Remove queue channels for the TCs
6056  * @vsi: VSI to be configured
6057  *
6058  * Remove queue channels for the TCs
6059  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)6060 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6061 {
6062 	enum i40e_admin_queue_err last_aq_status;
6063 	struct i40e_cloud_filter *cfilter;
6064 	struct i40e_channel *ch, *ch_tmp;
6065 	struct i40e_pf *pf = vsi->back;
6066 	struct hlist_node *node;
6067 	int ret, i;
6068 
6069 	/* Reset rss size that was stored when reconfiguring rss for
6070 	 * channel VSIs with non-power-of-2 queue count.
6071 	 */
6072 	vsi->current_rss_size = 0;
6073 
6074 	/* perform cleanup for channels if they exist */
6075 	if (list_empty(&vsi->ch_list))
6076 		return;
6077 
6078 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6079 		struct i40e_vsi *p_vsi;
6080 
6081 		list_del(&ch->list);
6082 		p_vsi = ch->parent_vsi;
6083 		if (!p_vsi || !ch->initialized) {
6084 			kfree(ch);
6085 			continue;
6086 		}
6087 		/* Reset queue contexts */
6088 		for (i = 0; i < ch->num_queue_pairs; i++) {
6089 			struct i40e_ring *tx_ring, *rx_ring;
6090 			u16 pf_q;
6091 
6092 			pf_q = ch->base_queue + i;
6093 			tx_ring = vsi->tx_rings[pf_q];
6094 			tx_ring->ch = NULL;
6095 
6096 			rx_ring = vsi->rx_rings[pf_q];
6097 			rx_ring->ch = NULL;
6098 		}
6099 
6100 		/* Reset BW configured for this VSI via mqprio */
6101 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6102 		if (ret)
6103 			dev_info(&vsi->back->pdev->dev,
6104 				 "Failed to reset tx rate for ch->seid %u\n",
6105 				 ch->seid);
6106 
6107 		/* delete cloud filters associated with this channel */
6108 		hlist_for_each_entry_safe(cfilter, node,
6109 					  &pf->cloud_filter_list, cloud_node) {
6110 			if (cfilter->seid != ch->seid)
6111 				continue;
6112 
6113 			hash_del(&cfilter->cloud_node);
6114 			if (cfilter->dst_port)
6115 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6116 									cfilter,
6117 									false);
6118 			else
6119 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6120 								false);
6121 			last_aq_status = pf->hw.aq.asq_last_status;
6122 			if (ret)
6123 				dev_info(&pf->pdev->dev,
6124 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6125 					 ERR_PTR(ret),
6126 					 i40e_aq_str(&pf->hw, last_aq_status));
6127 			kfree(cfilter);
6128 		}
6129 
6130 		/* delete VSI from FW */
6131 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6132 					     NULL);
6133 		if (ret)
6134 			dev_err(&vsi->back->pdev->dev,
6135 				"unable to remove channel (%d) for parent VSI(%d)\n",
6136 				ch->seid, p_vsi->seid);
6137 		kfree(ch);
6138 	}
6139 	INIT_LIST_HEAD(&vsi->ch_list);
6140 }
6141 
6142 /**
6143  * i40e_get_max_queues_for_channel
6144  * @vsi: ptr to VSI to which channels are associated with
6145  *
6146  * Helper function which returns max value among the queue counts set on the
6147  * channels/TCs created.
6148  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6149 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6150 {
6151 	struct i40e_channel *ch, *ch_tmp;
6152 	int max = 0;
6153 
6154 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6155 		if (!ch->initialized)
6156 			continue;
6157 		if (ch->num_queue_pairs > max)
6158 			max = ch->num_queue_pairs;
6159 	}
6160 
6161 	return max;
6162 }
6163 
6164 /**
6165  * i40e_validate_num_queues - validate num_queues w.r.t channel
6166  * @pf: ptr to PF device
6167  * @num_queues: number of queues
6168  * @vsi: the parent VSI
6169  * @reconfig_rss: indicates should the RSS be reconfigured or not
6170  *
6171  * This function validates number of queues in the context of new channel
6172  * which is being established and determines if RSS should be reconfigured
6173  * or not for parent VSI.
6174  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6175 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6176 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6177 {
6178 	int max_ch_queues;
6179 
6180 	if (!reconfig_rss)
6181 		return -EINVAL;
6182 
6183 	*reconfig_rss = false;
6184 	if (vsi->current_rss_size) {
6185 		if (num_queues > vsi->current_rss_size) {
6186 			dev_dbg(&pf->pdev->dev,
6187 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6188 				num_queues, vsi->current_rss_size);
6189 			return -EINVAL;
6190 		} else if ((num_queues < vsi->current_rss_size) &&
6191 			   (!is_power_of_2(num_queues))) {
6192 			dev_dbg(&pf->pdev->dev,
6193 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6194 				num_queues, vsi->current_rss_size);
6195 			return -EINVAL;
6196 		}
6197 	}
6198 
6199 	if (!is_power_of_2(num_queues)) {
6200 		/* Find the max num_queues configured for channel if channel
6201 		 * exist.
6202 		 * if channel exist, then enforce 'num_queues' to be more than
6203 		 * max ever queues configured for channel.
6204 		 */
6205 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6206 		if (num_queues < max_ch_queues) {
6207 			dev_dbg(&pf->pdev->dev,
6208 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6209 				num_queues, max_ch_queues);
6210 			return -EINVAL;
6211 		}
6212 		*reconfig_rss = true;
6213 	}
6214 
6215 	return 0;
6216 }
6217 
6218 /**
6219  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6220  * @vsi: the VSI being setup
6221  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6222  *
6223  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6224  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6225 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6226 {
6227 	struct i40e_pf *pf = vsi->back;
6228 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6229 	struct i40e_hw *hw = &pf->hw;
6230 	int local_rss_size;
6231 	u8 *lut;
6232 	int ret;
6233 
6234 	if (!vsi->rss_size)
6235 		return -EINVAL;
6236 
6237 	if (rss_size > vsi->rss_size)
6238 		return -EINVAL;
6239 
6240 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6241 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6242 	if (!lut)
6243 		return -ENOMEM;
6244 
6245 	/* Ignoring user configured lut if there is one */
6246 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6247 
6248 	/* Use user configured hash key if there is one, otherwise
6249 	 * use default.
6250 	 */
6251 	if (vsi->rss_hkey_user)
6252 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6253 	else
6254 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6255 
6256 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6257 	if (ret) {
6258 		dev_info(&pf->pdev->dev,
6259 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6260 			 ERR_PTR(ret),
6261 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6262 		kfree(lut);
6263 		return ret;
6264 	}
6265 	kfree(lut);
6266 
6267 	/* Do the update w.r.t. storing rss_size */
6268 	if (!vsi->orig_rss_size)
6269 		vsi->orig_rss_size = vsi->rss_size;
6270 	vsi->current_rss_size = local_rss_size;
6271 
6272 	return ret;
6273 }
6274 
6275 /**
6276  * i40e_channel_setup_queue_map - Setup a channel queue map
6277  * @pf: ptr to PF device
6278  * @ctxt: VSI context structure
6279  * @ch: ptr to channel structure
6280  *
6281  * Setup queue map for a specific channel
6282  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6283 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6284 					 struct i40e_vsi_context *ctxt,
6285 					 struct i40e_channel *ch)
6286 {
6287 	u16 qcount, qmap, sections = 0;
6288 	u8 offset = 0;
6289 	int pow;
6290 
6291 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6292 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6293 
6294 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6295 	ch->num_queue_pairs = qcount;
6296 
6297 	/* find the next higher power-of-2 of num queue pairs */
6298 	pow = ilog2(qcount);
6299 	if (!is_power_of_2(qcount))
6300 		pow++;
6301 
6302 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6303 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6304 
6305 	/* Setup queue TC[0].qmap for given VSI context */
6306 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6307 
6308 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6309 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6310 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6311 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6312 }
6313 
6314 /**
6315  * i40e_add_channel - add a channel by adding VSI
6316  * @pf: ptr to PF device
6317  * @uplink_seid: underlying HW switching element (VEB) ID
6318  * @ch: ptr to channel structure
6319  *
6320  * Add a channel (VSI) using add_vsi and queue_map
6321  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6322 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6323 			    struct i40e_channel *ch)
6324 {
6325 	struct i40e_hw *hw = &pf->hw;
6326 	struct i40e_vsi_context ctxt;
6327 	u8 enabled_tc = 0x1; /* TC0 enabled */
6328 	int ret;
6329 
6330 	if (ch->type != I40E_VSI_VMDQ2) {
6331 		dev_info(&pf->pdev->dev,
6332 			 "add new vsi failed, ch->type %d\n", ch->type);
6333 		return -EINVAL;
6334 	}
6335 
6336 	memset(&ctxt, 0, sizeof(ctxt));
6337 	ctxt.pf_num = hw->pf_id;
6338 	ctxt.vf_num = 0;
6339 	ctxt.uplink_seid = uplink_seid;
6340 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6341 	if (ch->type == I40E_VSI_VMDQ2)
6342 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6343 
6344 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6345 		ctxt.info.valid_sections |=
6346 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6347 		ctxt.info.switch_id =
6348 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6349 	}
6350 
6351 	/* Set queue map for a given VSI context */
6352 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6353 
6354 	/* Now time to create VSI */
6355 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6356 	if (ret) {
6357 		dev_info(&pf->pdev->dev,
6358 			 "add new vsi failed, err %pe aq_err %s\n",
6359 			 ERR_PTR(ret),
6360 			 i40e_aq_str(&pf->hw,
6361 				     pf->hw.aq.asq_last_status));
6362 		return -ENOENT;
6363 	}
6364 
6365 	/* Success, update channel, set enabled_tc only if the channel
6366 	 * is not a macvlan
6367 	 */
6368 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6369 	ch->seid = ctxt.seid;
6370 	ch->vsi_number = ctxt.vsi_number;
6371 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6372 
6373 	/* copy just the sections touched not the entire info
6374 	 * since not all sections are valid as returned by
6375 	 * update vsi params
6376 	 */
6377 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6378 	memcpy(&ch->info.queue_mapping,
6379 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6380 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6381 	       sizeof(ctxt.info.tc_mapping));
6382 
6383 	return 0;
6384 }
6385 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6386 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6387 				  u8 *bw_share)
6388 {
6389 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6390 	int ret;
6391 	int i;
6392 
6393 	memset(&bw_data, 0, sizeof(bw_data));
6394 	bw_data.tc_valid_bits = ch->enabled_tc;
6395 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6396 		bw_data.tc_bw_credits[i] = bw_share[i];
6397 
6398 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6399 				       &bw_data, NULL);
6400 	if (ret) {
6401 		dev_info(&vsi->back->pdev->dev,
6402 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6403 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6404 		return -EINVAL;
6405 	}
6406 
6407 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6408 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6409 
6410 	return 0;
6411 }
6412 
6413 /**
6414  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6415  * @pf: ptr to PF device
6416  * @vsi: the VSI being setup
6417  * @ch: ptr to channel structure
6418  *
6419  * Configure TX rings associated with channel (VSI) since queues are being
6420  * from parent VSI.
6421  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6422 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6423 				       struct i40e_vsi *vsi,
6424 				       struct i40e_channel *ch)
6425 {
6426 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6427 	int ret;
6428 	int i;
6429 
6430 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6431 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6432 		if (ch->enabled_tc & BIT(i))
6433 			bw_share[i] = 1;
6434 	}
6435 
6436 	/* configure BW for new VSI */
6437 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6438 	if (ret) {
6439 		dev_info(&vsi->back->pdev->dev,
6440 			 "Failed configuring TC map %d for channel (seid %u)\n",
6441 			 ch->enabled_tc, ch->seid);
6442 		return ret;
6443 	}
6444 
6445 	for (i = 0; i < ch->num_queue_pairs; i++) {
6446 		struct i40e_ring *tx_ring, *rx_ring;
6447 		u16 pf_q;
6448 
6449 		pf_q = ch->base_queue + i;
6450 
6451 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6452 		 * context
6453 		 */
6454 		tx_ring = vsi->tx_rings[pf_q];
6455 		tx_ring->ch = ch;
6456 
6457 		/* Get the RX ring ptr */
6458 		rx_ring = vsi->rx_rings[pf_q];
6459 		rx_ring->ch = ch;
6460 	}
6461 
6462 	return 0;
6463 }
6464 
6465 /**
6466  * i40e_setup_hw_channel - setup new channel
6467  * @pf: ptr to PF device
6468  * @vsi: the VSI being setup
6469  * @ch: ptr to channel structure
6470  * @uplink_seid: underlying HW switching element (VEB) ID
6471  * @type: type of channel to be created (VMDq2/VF)
6472  *
6473  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6474  * and configures TX rings accordingly
6475  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6476 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6477 					struct i40e_vsi *vsi,
6478 					struct i40e_channel *ch,
6479 					u16 uplink_seid, u8 type)
6480 {
6481 	int ret;
6482 
6483 	ch->initialized = false;
6484 	ch->base_queue = vsi->next_base_queue;
6485 	ch->type = type;
6486 
6487 	/* Proceed with creation of channel (VMDq2) VSI */
6488 	ret = i40e_add_channel(pf, uplink_seid, ch);
6489 	if (ret) {
6490 		dev_info(&pf->pdev->dev,
6491 			 "failed to add_channel using uplink_seid %u\n",
6492 			 uplink_seid);
6493 		return ret;
6494 	}
6495 
6496 	/* Mark the successful creation of channel */
6497 	ch->initialized = true;
6498 
6499 	/* Reconfigure TX queues using QTX_CTL register */
6500 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6501 	if (ret) {
6502 		dev_info(&pf->pdev->dev,
6503 			 "failed to configure TX rings for channel %u\n",
6504 			 ch->seid);
6505 		return ret;
6506 	}
6507 
6508 	/* update 'next_base_queue' */
6509 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6510 	dev_dbg(&pf->pdev->dev,
6511 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6512 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6513 		ch->num_queue_pairs,
6514 		vsi->next_base_queue);
6515 	return ret;
6516 }
6517 
6518 /**
6519  * i40e_setup_channel - setup new channel using uplink element
6520  * @pf: ptr to PF device
6521  * @vsi: pointer to the VSI to set up the channel within
6522  * @ch: ptr to channel structure
6523  *
6524  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6525  * and uplink switching element (uplink_seid)
6526  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6527 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6528 			       struct i40e_channel *ch)
6529 {
6530 	struct i40e_vsi *main_vsi;
6531 	u8 vsi_type;
6532 	u16 seid;
6533 	int ret;
6534 
6535 	if (vsi->type == I40E_VSI_MAIN) {
6536 		vsi_type = I40E_VSI_VMDQ2;
6537 	} else {
6538 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6539 			vsi->type);
6540 		return false;
6541 	}
6542 
6543 	/* underlying switching element */
6544 	main_vsi = i40e_pf_get_main_vsi(pf);
6545 	seid = main_vsi->uplink_seid;
6546 
6547 	/* create channel (VSI), configure TX rings */
6548 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6549 	if (ret) {
6550 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6551 		return false;
6552 	}
6553 
6554 	return ch->initialized ? true : false;
6555 }
6556 
6557 /**
6558  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6559  * @vsi: ptr to VSI which has PF backing
6560  *
6561  * Sets up switch mode correctly if it needs to be changed and perform
6562  * what are allowed modes.
6563  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6564 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6565 {
6566 	u8 mode;
6567 	struct i40e_pf *pf = vsi->back;
6568 	struct i40e_hw *hw = &pf->hw;
6569 	int ret;
6570 
6571 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6572 	if (ret)
6573 		return -EINVAL;
6574 
6575 	if (hw->dev_caps.switch_mode) {
6576 		/* if switch mode is set, support mode2 (non-tunneled for
6577 		 * cloud filter) for now
6578 		 */
6579 		u32 switch_mode = hw->dev_caps.switch_mode &
6580 				  I40E_SWITCH_MODE_MASK;
6581 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6582 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6583 				return 0;
6584 			dev_err(&pf->pdev->dev,
6585 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6586 				hw->dev_caps.switch_mode);
6587 			return -EINVAL;
6588 		}
6589 	}
6590 
6591 	/* Set Bit 7 to be valid */
6592 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6593 
6594 	/* Set L4type for TCP support */
6595 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6596 
6597 	/* Set cloud filter mode */
6598 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6599 
6600 	/* Prep mode field for set_switch_config */
6601 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6602 					pf->last_sw_conf_valid_flags,
6603 					mode, NULL);
6604 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6605 		dev_err(&pf->pdev->dev,
6606 			"couldn't set switch config bits, err %pe aq_err %s\n",
6607 			ERR_PTR(ret),
6608 			i40e_aq_str(hw,
6609 				    hw->aq.asq_last_status));
6610 
6611 	return ret;
6612 }
6613 
6614 /**
6615  * i40e_create_queue_channel - function to create channel
6616  * @vsi: VSI to be configured
6617  * @ch: ptr to channel (it contains channel specific params)
6618  *
6619  * This function creates channel (VSI) using num_queues specified by user,
6620  * reconfigs RSS if needed.
6621  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6622 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6623 			      struct i40e_channel *ch)
6624 {
6625 	struct i40e_pf *pf = vsi->back;
6626 	bool reconfig_rss;
6627 	int err;
6628 
6629 	if (!ch)
6630 		return -EINVAL;
6631 
6632 	if (!ch->num_queue_pairs) {
6633 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6634 			ch->num_queue_pairs);
6635 		return -EINVAL;
6636 	}
6637 
6638 	/* validate user requested num_queues for channel */
6639 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6640 				       &reconfig_rss);
6641 	if (err) {
6642 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6643 			 ch->num_queue_pairs);
6644 		return -EINVAL;
6645 	}
6646 
6647 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6648 	 * VSI to be added switch to VEB mode.
6649 	 */
6650 
6651 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6652 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6653 
6654 		if (vsi->type == I40E_VSI_MAIN) {
6655 			if (i40e_is_tc_mqprio_enabled(pf))
6656 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6657 			else
6658 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6659 		}
6660 		/* now onwards for main VSI, number of queues will be value
6661 		 * of TC0's queue count
6662 		 */
6663 	}
6664 
6665 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6666 	 * it should be more than num_queues
6667 	 */
6668 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6669 		dev_dbg(&pf->pdev->dev,
6670 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6671 			vsi->cnt_q_avail, ch->num_queue_pairs);
6672 		return -EINVAL;
6673 	}
6674 
6675 	/* reconfig_rss only if vsi type is MAIN_VSI */
6676 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6677 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6678 		if (err) {
6679 			dev_info(&pf->pdev->dev,
6680 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6681 				 ch->num_queue_pairs);
6682 			return -EINVAL;
6683 		}
6684 	}
6685 
6686 	if (!i40e_setup_channel(pf, vsi, ch)) {
6687 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6688 		return -EINVAL;
6689 	}
6690 
6691 	dev_info(&pf->pdev->dev,
6692 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6693 		 ch->seid, ch->num_queue_pairs);
6694 
6695 	/* configure VSI for BW limit */
6696 	if (ch->max_tx_rate) {
6697 		u64 credits = ch->max_tx_rate;
6698 
6699 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6700 			return -EINVAL;
6701 
6702 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6703 		dev_dbg(&pf->pdev->dev,
6704 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6705 			ch->max_tx_rate,
6706 			credits,
6707 			ch->seid);
6708 	}
6709 
6710 	/* in case of VF, this will be main SRIOV VSI */
6711 	ch->parent_vsi = vsi;
6712 
6713 	/* and update main_vsi's count for queue_available to use */
6714 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6715 
6716 	return 0;
6717 }
6718 
6719 /**
6720  * i40e_configure_queue_channels - Add queue channel for the given TCs
6721  * @vsi: VSI to be configured
6722  *
6723  * Configures queue channel mapping to the given TCs
6724  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6725 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6726 {
6727 	struct i40e_channel *ch;
6728 	u64 max_rate = 0;
6729 	int ret = 0, i;
6730 
6731 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6732 	vsi->tc_seid_map[0] = vsi->seid;
6733 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6734 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6735 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6736 			if (!ch) {
6737 				ret = -ENOMEM;
6738 				goto err_free;
6739 			}
6740 
6741 			INIT_LIST_HEAD(&ch->list);
6742 			ch->num_queue_pairs =
6743 				vsi->tc_config.tc_info[i].qcount;
6744 			ch->base_queue =
6745 				vsi->tc_config.tc_info[i].qoffset;
6746 
6747 			/* Bandwidth limit through tc interface is in bytes/s,
6748 			 * change to Mbit/s
6749 			 */
6750 			max_rate = vsi->mqprio_qopt.max_rate[i];
6751 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6752 			ch->max_tx_rate = max_rate;
6753 
6754 			list_add_tail(&ch->list, &vsi->ch_list);
6755 
6756 			ret = i40e_create_queue_channel(vsi, ch);
6757 			if (ret) {
6758 				dev_err(&vsi->back->pdev->dev,
6759 					"Failed creating queue channel with TC%d: queues %d\n",
6760 					i, ch->num_queue_pairs);
6761 				goto err_free;
6762 			}
6763 			vsi->tc_seid_map[i] = ch->seid;
6764 		}
6765 	}
6766 
6767 	/* reset to reconfigure TX queue contexts */
6768 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6769 	return ret;
6770 
6771 err_free:
6772 	i40e_remove_queue_channels(vsi);
6773 	return ret;
6774 }
6775 
6776 /**
6777  * i40e_veb_config_tc - Configure TCs for given VEB
6778  * @veb: given VEB
6779  * @enabled_tc: TC bitmap
6780  *
6781  * Configures given TC bitmap for VEB (switching) element
6782  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6783 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6784 {
6785 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6786 	struct i40e_pf *pf = veb->pf;
6787 	int ret = 0;
6788 	int i;
6789 
6790 	/* No TCs or already enabled TCs just return */
6791 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6792 		return ret;
6793 
6794 	bw_data.tc_valid_bits = enabled_tc;
6795 	/* bw_data.absolute_credits is not set (relative) */
6796 
6797 	/* Enable ETS TCs with equal BW Share for now */
6798 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6799 		if (enabled_tc & BIT(i))
6800 			bw_data.tc_bw_share_credits[i] = 1;
6801 	}
6802 
6803 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6804 						   &bw_data, NULL);
6805 	if (ret) {
6806 		dev_info(&pf->pdev->dev,
6807 			 "VEB bw config failed, err %pe aq_err %s\n",
6808 			 ERR_PTR(ret),
6809 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6810 		goto out;
6811 	}
6812 
6813 	/* Update the BW information */
6814 	ret = i40e_veb_get_bw_info(veb);
6815 	if (ret) {
6816 		dev_info(&pf->pdev->dev,
6817 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6818 			 ERR_PTR(ret),
6819 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6820 	}
6821 
6822 out:
6823 	return ret;
6824 }
6825 
6826 #ifdef CONFIG_I40E_DCB
6827 /**
6828  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6829  * @pf: PF struct
6830  *
6831  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6832  * the caller would've quiesce all the VSIs before calling
6833  * this function
6834  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6835 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6836 {
6837 	struct i40e_vsi *vsi;
6838 	struct i40e_veb *veb;
6839 	u8 tc_map = 0;
6840 	int ret;
6841 	int v;
6842 
6843 	/* Enable the TCs available on PF to all VEBs */
6844 	tc_map = i40e_pf_get_tc_map(pf);
6845 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6846 		return;
6847 
6848 	i40e_pf_for_each_veb(pf, v, veb) {
6849 		ret = i40e_veb_config_tc(veb, tc_map);
6850 		if (ret) {
6851 			dev_info(&pf->pdev->dev,
6852 				 "Failed configuring TC for VEB seid=%d\n",
6853 				 veb->seid);
6854 			/* Will try to configure as many components */
6855 		}
6856 	}
6857 
6858 	/* Update each VSI */
6859 	i40e_pf_for_each_vsi(pf, v, vsi) {
6860 		/* - Enable all TCs for the LAN VSI
6861 		 * - For all others keep them at TC0 for now
6862 		 */
6863 		if (vsi->type == I40E_VSI_MAIN)
6864 			tc_map = i40e_pf_get_tc_map(pf);
6865 		else
6866 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6867 
6868 		ret = i40e_vsi_config_tc(vsi, tc_map);
6869 		if (ret) {
6870 			dev_info(&pf->pdev->dev,
6871 				 "Failed configuring TC for VSI seid=%d\n",
6872 				 vsi->seid);
6873 			/* Will try to configure as many components */
6874 		} else {
6875 			/* Re-configure VSI vectors based on updated TC map */
6876 			i40e_vsi_map_rings_to_vectors(vsi);
6877 			if (vsi->netdev)
6878 				i40e_dcbnl_set_all(vsi);
6879 		}
6880 	}
6881 }
6882 
6883 /**
6884  * i40e_resume_port_tx - Resume port Tx
6885  * @pf: PF struct
6886  *
6887  * Resume a port's Tx and issue a PF reset in case of failure to
6888  * resume.
6889  **/
i40e_resume_port_tx(struct i40e_pf * pf)6890 static int i40e_resume_port_tx(struct i40e_pf *pf)
6891 {
6892 	struct i40e_hw *hw = &pf->hw;
6893 	int ret;
6894 
6895 	ret = i40e_aq_resume_port_tx(hw, NULL);
6896 	if (ret) {
6897 		dev_info(&pf->pdev->dev,
6898 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6899 			  ERR_PTR(ret),
6900 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6901 		/* Schedule PF reset to recover */
6902 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6903 		i40e_service_event_schedule(pf);
6904 	}
6905 
6906 	return ret;
6907 }
6908 
6909 /**
6910  * i40e_suspend_port_tx - Suspend port Tx
6911  * @pf: PF struct
6912  *
6913  * Suspend a port's Tx and issue a PF reset in case of failure.
6914  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6915 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6916 {
6917 	struct i40e_hw *hw = &pf->hw;
6918 	int ret;
6919 
6920 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6921 	if (ret) {
6922 		dev_info(&pf->pdev->dev,
6923 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6924 			 ERR_PTR(ret),
6925 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6926 		/* Schedule PF reset to recover */
6927 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6928 		i40e_service_event_schedule(pf);
6929 	}
6930 
6931 	return ret;
6932 }
6933 
6934 /**
6935  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6936  * @pf: PF being configured
6937  * @new_cfg: New DCBX configuration
6938  *
6939  * Program DCB settings into HW and reconfigure VEB/VSIs on
6940  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6941  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6942 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6943 				  struct i40e_dcbx_config *new_cfg)
6944 {
6945 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6946 	int ret;
6947 
6948 	/* Check if need reconfiguration */
6949 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6950 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6951 		return 0;
6952 	}
6953 
6954 	/* Config change disable all VSIs */
6955 	i40e_pf_quiesce_all_vsi(pf);
6956 
6957 	/* Copy the new config to the current config */
6958 	*old_cfg = *new_cfg;
6959 	old_cfg->etsrec = old_cfg->etscfg;
6960 	ret = i40e_set_dcb_config(&pf->hw);
6961 	if (ret) {
6962 		dev_info(&pf->pdev->dev,
6963 			 "Set DCB Config failed, err %pe aq_err %s\n",
6964 			 ERR_PTR(ret),
6965 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6966 		goto out;
6967 	}
6968 
6969 	/* Changes in configuration update VEB/VSI */
6970 	i40e_dcb_reconfigure(pf);
6971 out:
6972 	/* In case of reset do not try to resume anything */
6973 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6974 		/* Re-start the VSIs if disabled */
6975 		ret = i40e_resume_port_tx(pf);
6976 		/* In case of error no point in resuming VSIs */
6977 		if (ret)
6978 			goto err;
6979 		i40e_pf_unquiesce_all_vsi(pf);
6980 	}
6981 err:
6982 	return ret;
6983 }
6984 
6985 /**
6986  * i40e_hw_dcb_config - Program new DCBX settings into HW
6987  * @pf: PF being configured
6988  * @new_cfg: New DCBX configuration
6989  *
6990  * Program DCB settings into HW and reconfigure VEB/VSIs on
6991  * given PF
6992  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6993 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6994 {
6995 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6996 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6997 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6998 	struct i40e_dcbx_config *old_cfg;
6999 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
7000 	struct i40e_rx_pb_config pb_cfg;
7001 	struct i40e_hw *hw = &pf->hw;
7002 	u8 num_ports = hw->num_ports;
7003 	bool need_reconfig;
7004 	int ret = -EINVAL;
7005 	u8 lltc_map = 0;
7006 	u8 tc_map = 0;
7007 	u8 new_numtc;
7008 	u8 i;
7009 
7010 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
7011 	/* Un-pack information to Program ETS HW via shared API
7012 	 * numtc, tcmap
7013 	 * LLTC map
7014 	 * ETS/NON-ETS arbiter mode
7015 	 * max exponent (credit refills)
7016 	 * Total number of ports
7017 	 * PFC priority bit-map
7018 	 * Priority Table
7019 	 * BW % per TC
7020 	 * Arbiter mode between UPs sharing same TC
7021 	 * TSA table (ETS or non-ETS)
7022 	 * EEE enabled or not
7023 	 * MFS TC table
7024 	 */
7025 
7026 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
7027 
7028 	memset(&ets_data, 0, sizeof(ets_data));
7029 	for (i = 0; i < new_numtc; i++) {
7030 		tc_map |= BIT(i);
7031 		switch (new_cfg->etscfg.tsatable[i]) {
7032 		case I40E_IEEE_TSA_ETS:
7033 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
7034 			ets_data.tc_bw_share_credits[i] =
7035 					new_cfg->etscfg.tcbwtable[i];
7036 			break;
7037 		case I40E_IEEE_TSA_STRICT:
7038 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
7039 			lltc_map |= BIT(i);
7040 			ets_data.tc_bw_share_credits[i] =
7041 					I40E_DCB_STRICT_PRIO_CREDITS;
7042 			break;
7043 		default:
7044 			/* Invalid TSA type */
7045 			need_reconfig = false;
7046 			goto out;
7047 		}
7048 	}
7049 
7050 	old_cfg = &hw->local_dcbx_config;
7051 	/* Check if need reconfiguration */
7052 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7053 
7054 	/* If needed, enable/disable frame tagging, disable all VSIs
7055 	 * and suspend port tx
7056 	 */
7057 	if (need_reconfig) {
7058 		/* Enable DCB tagging only when more than one TC */
7059 		if (new_numtc > 1)
7060 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7061 		else
7062 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7063 
7064 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7065 		/* Reconfiguration needed quiesce all VSIs */
7066 		i40e_pf_quiesce_all_vsi(pf);
7067 		ret = i40e_suspend_port_tx(pf);
7068 		if (ret)
7069 			goto err;
7070 	}
7071 
7072 	/* Configure Port ETS Tx Scheduler */
7073 	ets_data.tc_valid_bits = tc_map;
7074 	ets_data.tc_strict_priority_flags = lltc_map;
7075 	ret = i40e_aq_config_switch_comp_ets
7076 		(hw, pf->mac_seid, &ets_data,
7077 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7078 	if (ret) {
7079 		dev_info(&pf->pdev->dev,
7080 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7081 			 ERR_PTR(ret),
7082 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7083 		goto out;
7084 	}
7085 
7086 	/* Configure Rx ETS HW */
7087 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7088 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7089 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7090 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7091 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7092 				   lltc_map);
7093 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7094 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7095 				     prio_type);
7096 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7097 			       new_cfg->etscfg.prioritytable);
7098 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7099 
7100 	/* Configure Rx Packet Buffers in HW */
7101 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7102 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7103 
7104 		mfs_tc[i] = main_vsi->netdev->mtu;
7105 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7106 	}
7107 
7108 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7109 					 false, new_cfg->pfc.pfcenable,
7110 					 mfs_tc, &pb_cfg);
7111 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7112 
7113 	/* Update the local Rx Packet buffer config */
7114 	pf->pb_cfg = pb_cfg;
7115 
7116 	/* Inform the FW about changes to DCB configuration */
7117 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7118 	if (ret) {
7119 		dev_info(&pf->pdev->dev,
7120 			 "DCB Updated failed, err %pe aq_err %s\n",
7121 			 ERR_PTR(ret),
7122 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7123 		goto out;
7124 	}
7125 
7126 	/* Update the port DCBx configuration */
7127 	*old_cfg = *new_cfg;
7128 
7129 	/* Changes in configuration update VEB/VSI */
7130 	i40e_dcb_reconfigure(pf);
7131 out:
7132 	/* Re-start the VSIs if disabled */
7133 	if (need_reconfig) {
7134 		ret = i40e_resume_port_tx(pf);
7135 
7136 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7137 		/* In case of error no point in resuming VSIs */
7138 		if (ret)
7139 			goto err;
7140 
7141 		/* Wait for the PF's queues to be disabled */
7142 		ret = i40e_pf_wait_queues_disabled(pf);
7143 		if (ret) {
7144 			/* Schedule PF reset to recover */
7145 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7146 			i40e_service_event_schedule(pf);
7147 			goto err;
7148 		} else {
7149 			i40e_pf_unquiesce_all_vsi(pf);
7150 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7151 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7152 		}
7153 		/* registers are set, lets apply */
7154 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7155 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7156 	}
7157 
7158 err:
7159 	return ret;
7160 }
7161 
7162 /**
7163  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7164  * @pf: PF being queried
7165  *
7166  * Set default DCB configuration in case DCB is to be done in SW.
7167  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7168 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7169 {
7170 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7171 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7172 	struct i40e_hw *hw = &pf->hw;
7173 	int err;
7174 
7175 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7176 		/* Update the local cached instance with TC0 ETS */
7177 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7178 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7179 		pf->tmp_cfg.etscfg.maxtcs = 0;
7180 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7181 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7182 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7183 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7184 		/* FW needs one App to configure HW */
7185 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7186 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7187 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7188 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7189 
7190 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7191 	}
7192 
7193 	memset(&ets_data, 0, sizeof(ets_data));
7194 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7195 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7196 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7197 
7198 	/* Enable ETS on the Physical port */
7199 	err = i40e_aq_config_switch_comp_ets
7200 		(hw, pf->mac_seid, &ets_data,
7201 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7202 	if (err) {
7203 		dev_info(&pf->pdev->dev,
7204 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7205 			 ERR_PTR(err),
7206 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7207 		err = -ENOENT;
7208 		goto out;
7209 	}
7210 
7211 	/* Update the local cached instance with TC0 ETS */
7212 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7213 	dcb_cfg->etscfg.cbs = 0;
7214 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7215 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7216 
7217 out:
7218 	return err;
7219 }
7220 
7221 /**
7222  * i40e_init_pf_dcb - Initialize DCB configuration
7223  * @pf: PF being configured
7224  *
7225  * Query the current DCB configuration and cache it
7226  * in the hardware structure
7227  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7228 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7229 {
7230 	struct i40e_hw *hw = &pf->hw;
7231 	int err;
7232 
7233 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7234 	 * Also do not enable DCBx if FW LLDP agent is disabled
7235 	 */
7236 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7237 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7238 		err = -EOPNOTSUPP;
7239 		goto out;
7240 	}
7241 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7242 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7243 		err = i40e_dcb_sw_default_config(pf);
7244 		if (err) {
7245 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7246 			goto out;
7247 		}
7248 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7249 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7250 			       DCB_CAP_DCBX_VER_IEEE;
7251 		/* at init capable but disabled */
7252 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7253 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7254 		goto out;
7255 	}
7256 	err = i40e_init_dcb(hw, true);
7257 	if (!err) {
7258 		/* Device/Function is not DCBX capable */
7259 		if ((!hw->func_caps.dcb) ||
7260 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7261 			dev_info(&pf->pdev->dev,
7262 				 "DCBX offload is not supported or is disabled for this PF.\n");
7263 		} else {
7264 			/* When status is not DISABLED then DCBX in FW */
7265 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7266 				       DCB_CAP_DCBX_VER_IEEE;
7267 
7268 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7269 			/* Enable DCB tagging only when more than one TC
7270 			 * or explicitly disable if only one TC
7271 			 */
7272 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7273 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7274 			else
7275 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7276 			dev_dbg(&pf->pdev->dev,
7277 				"DCBX offload is supported for this PF.\n");
7278 		}
7279 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7280 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7281 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7282 	} else {
7283 		dev_info(&pf->pdev->dev,
7284 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7285 			 ERR_PTR(err),
7286 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7287 	}
7288 
7289 out:
7290 	return err;
7291 }
7292 #endif /* CONFIG_I40E_DCB */
7293 
i40e_print_link_message_eee(struct i40e_vsi * vsi,const char * speed,const char * fc)7294 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7295 					const char *speed, const char *fc)
7296 {
7297 	struct ethtool_keee kedata;
7298 
7299 	memzero_explicit(&kedata, sizeof(kedata));
7300 	if (vsi->netdev->ethtool_ops->get_eee)
7301 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7302 
7303 	if (!linkmode_empty(kedata.supported))
7304 		netdev_info(vsi->netdev,
7305 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7306 			    speed, fc,
7307 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7308 	else
7309 		netdev_info(vsi->netdev,
7310 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7311 			    speed, fc);
7312 }
7313 
7314 /**
7315  * i40e_print_link_message - print link up or down
7316  * @vsi: the VSI for which link needs a message
7317  * @isup: true of link is up, false otherwise
7318  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7319 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7320 {
7321 	enum i40e_aq_link_speed new_speed;
7322 	struct i40e_pf *pf = vsi->back;
7323 	char *speed = "Unknown";
7324 	char *fc = "Unknown";
7325 	char *fec = "";
7326 	char *req_fec = "";
7327 	char *an = "";
7328 
7329 	if (isup)
7330 		new_speed = pf->hw.phy.link_info.link_speed;
7331 	else
7332 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7333 
7334 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7335 		return;
7336 	vsi->current_isup = isup;
7337 	vsi->current_speed = new_speed;
7338 	if (!isup) {
7339 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7340 		return;
7341 	}
7342 
7343 	/* Warn user if link speed on NPAR enabled partition is not at
7344 	 * least 10GB
7345 	 */
7346 	if (pf->hw.func_caps.npar_enable &&
7347 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7348 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7349 		netdev_warn(vsi->netdev,
7350 			    "The partition detected link speed that is less than 10Gbps\n");
7351 
7352 	switch (pf->hw.phy.link_info.link_speed) {
7353 	case I40E_LINK_SPEED_40GB:
7354 		speed = "40 G";
7355 		break;
7356 	case I40E_LINK_SPEED_20GB:
7357 		speed = "20 G";
7358 		break;
7359 	case I40E_LINK_SPEED_25GB:
7360 		speed = "25 G";
7361 		break;
7362 	case I40E_LINK_SPEED_10GB:
7363 		speed = "10 G";
7364 		break;
7365 	case I40E_LINK_SPEED_5GB:
7366 		speed = "5 G";
7367 		break;
7368 	case I40E_LINK_SPEED_2_5GB:
7369 		speed = "2.5 G";
7370 		break;
7371 	case I40E_LINK_SPEED_1GB:
7372 		speed = "1000 M";
7373 		break;
7374 	case I40E_LINK_SPEED_100MB:
7375 		speed = "100 M";
7376 		break;
7377 	default:
7378 		break;
7379 	}
7380 
7381 	switch (pf->hw.fc.current_mode) {
7382 	case I40E_FC_FULL:
7383 		fc = "RX/TX";
7384 		break;
7385 	case I40E_FC_TX_PAUSE:
7386 		fc = "TX";
7387 		break;
7388 	case I40E_FC_RX_PAUSE:
7389 		fc = "RX";
7390 		break;
7391 	default:
7392 		fc = "None";
7393 		break;
7394 	}
7395 
7396 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7397 		req_fec = "None";
7398 		fec = "None";
7399 		an = "False";
7400 
7401 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7402 			an = "True";
7403 
7404 		if (pf->hw.phy.link_info.fec_info &
7405 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7406 			fec = "CL74 FC-FEC/BASE-R";
7407 		else if (pf->hw.phy.link_info.fec_info &
7408 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7409 			fec = "CL108 RS-FEC";
7410 
7411 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7412 		 * both RS and FC are requested
7413 		 */
7414 		if (vsi->back->hw.phy.link_info.req_fec_info &
7415 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7416 			if (vsi->back->hw.phy.link_info.req_fec_info &
7417 			    I40E_AQ_REQUEST_FEC_RS)
7418 				req_fec = "CL108 RS-FEC";
7419 			else
7420 				req_fec = "CL74 FC-FEC/BASE-R";
7421 		}
7422 		netdev_info(vsi->netdev,
7423 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7424 			    speed, req_fec, fec, an, fc);
7425 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7426 		req_fec = "None";
7427 		fec = "None";
7428 		an = "False";
7429 
7430 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7431 			an = "True";
7432 
7433 		if (pf->hw.phy.link_info.fec_info &
7434 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7435 			fec = "CL74 FC-FEC/BASE-R";
7436 
7437 		if (pf->hw.phy.link_info.req_fec_info &
7438 		    I40E_AQ_REQUEST_FEC_KR)
7439 			req_fec = "CL74 FC-FEC/BASE-R";
7440 
7441 		netdev_info(vsi->netdev,
7442 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7443 			    speed, req_fec, fec, an, fc);
7444 	} else {
7445 		i40e_print_link_message_eee(vsi, speed, fc);
7446 	}
7447 
7448 }
7449 
7450 /**
7451  * i40e_up_complete - Finish the last steps of bringing up a connection
7452  * @vsi: the VSI being configured
7453  **/
i40e_up_complete(struct i40e_vsi * vsi)7454 static int i40e_up_complete(struct i40e_vsi *vsi)
7455 {
7456 	struct i40e_pf *pf = vsi->back;
7457 	int err;
7458 
7459 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7460 		i40e_vsi_configure_msix(vsi);
7461 	else
7462 		i40e_configure_msi_and_legacy(vsi);
7463 
7464 	/* start rings */
7465 	err = i40e_vsi_start_rings(vsi);
7466 	if (err)
7467 		return err;
7468 
7469 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7470 	i40e_napi_enable_all(vsi);
7471 	i40e_vsi_enable_irq(vsi);
7472 
7473 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7474 	    (vsi->netdev)) {
7475 		i40e_print_link_message(vsi, true);
7476 		netif_tx_start_all_queues(vsi->netdev);
7477 		netif_carrier_on(vsi->netdev);
7478 	}
7479 
7480 	/* replay FDIR SB filters */
7481 	if (vsi->type == I40E_VSI_FDIR) {
7482 		/* reset fd counters */
7483 		pf->fd_add_err = 0;
7484 		pf->fd_atr_cnt = 0;
7485 		i40e_fdir_filter_restore(vsi);
7486 	}
7487 
7488 	/* On the next run of the service_task, notify any clients of the new
7489 	 * opened netdev
7490 	 */
7491 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7492 	i40e_service_event_schedule(pf);
7493 
7494 	return 0;
7495 }
7496 
7497 /**
7498  * i40e_vsi_reinit_locked - Reset the VSI
7499  * @vsi: the VSI being configured
7500  *
7501  * Rebuild the ring structs after some configuration
7502  * has changed, e.g. MTU size.
7503  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7504 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7505 {
7506 	struct i40e_pf *pf = vsi->back;
7507 
7508 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7509 		usleep_range(1000, 2000);
7510 	i40e_down(vsi);
7511 
7512 	i40e_up(vsi);
7513 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7514 }
7515 
7516 /**
7517  * i40e_force_link_state - Force the link status
7518  * @pf: board private structure
7519  * @is_up: whether the link state should be forced up or down
7520  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7521 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7522 {
7523 	struct i40e_aq_get_phy_abilities_resp abilities;
7524 	struct i40e_aq_set_phy_config config = {0};
7525 	bool non_zero_phy_type = is_up;
7526 	struct i40e_hw *hw = &pf->hw;
7527 	u64 mask;
7528 	u8 speed;
7529 	int err;
7530 
7531 	/* Card might've been put in an unstable state by other drivers
7532 	 * and applications, which causes incorrect speed values being
7533 	 * set on startup. In order to clear speed registers, we call
7534 	 * get_phy_capabilities twice, once to get initial state of
7535 	 * available speeds, and once to get current PHY config.
7536 	 */
7537 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7538 					   NULL);
7539 	if (err) {
7540 		dev_err(&pf->pdev->dev,
7541 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7542 			ERR_PTR(err),
7543 			i40e_aq_str(hw, hw->aq.asq_last_status));
7544 		return err;
7545 	}
7546 	speed = abilities.link_speed;
7547 
7548 	/* Get the current phy config */
7549 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7550 					   NULL);
7551 	if (err) {
7552 		dev_err(&pf->pdev->dev,
7553 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7554 			ERR_PTR(err),
7555 			i40e_aq_str(hw, hw->aq.asq_last_status));
7556 		return err;
7557 	}
7558 
7559 	/* If link needs to go up, but was not forced to go down,
7560 	 * and its speed values are OK, no need for a flap
7561 	 * if non_zero_phy_type was set, still need to force up
7562 	 */
7563 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7564 		non_zero_phy_type = true;
7565 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7566 		return 0;
7567 
7568 	/* To force link we need to set bits for all supported PHY types,
7569 	 * but there are now more than 32, so we need to split the bitmap
7570 	 * across two fields.
7571 	 */
7572 	mask = I40E_PHY_TYPES_BITMASK;
7573 	config.phy_type =
7574 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7575 	config.phy_type_ext =
7576 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7577 	/* Copy the old settings, except of phy_type */
7578 	config.abilities = abilities.abilities;
7579 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7580 		if (is_up)
7581 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7582 		else
7583 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7584 	}
7585 	if (abilities.link_speed != 0)
7586 		config.link_speed = abilities.link_speed;
7587 	else
7588 		config.link_speed = speed;
7589 	config.eee_capability = abilities.eee_capability;
7590 	config.eeer = abilities.eeer_val;
7591 	config.low_power_ctrl = abilities.d3_lpan;
7592 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7593 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7594 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7595 
7596 	if (err) {
7597 		dev_err(&pf->pdev->dev,
7598 			"set phy config ret =  %pe last_status =  %s\n",
7599 			ERR_PTR(err),
7600 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7601 		return err;
7602 	}
7603 
7604 	/* Update the link info */
7605 	err = i40e_update_link_info(hw);
7606 	if (err) {
7607 		/* Wait a little bit (on 40G cards it sometimes takes a really
7608 		 * long time for link to come back from the atomic reset)
7609 		 * and try once more
7610 		 */
7611 		msleep(1000);
7612 		i40e_update_link_info(hw);
7613 	}
7614 
7615 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7616 
7617 	return 0;
7618 }
7619 
7620 /**
7621  * i40e_up - Bring the connection back up after being down
7622  * @vsi: the VSI being configured
7623  **/
i40e_up(struct i40e_vsi * vsi)7624 int i40e_up(struct i40e_vsi *vsi)
7625 {
7626 	int err;
7627 
7628 	if (vsi->type == I40E_VSI_MAIN &&
7629 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7630 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7631 		i40e_force_link_state(vsi->back, true);
7632 
7633 	err = i40e_vsi_configure(vsi);
7634 	if (!err)
7635 		err = i40e_up_complete(vsi);
7636 
7637 	return err;
7638 }
7639 
7640 /**
7641  * i40e_down - Shutdown the connection processing
7642  * @vsi: the VSI being stopped
7643  **/
i40e_down(struct i40e_vsi * vsi)7644 void i40e_down(struct i40e_vsi *vsi)
7645 {
7646 	int i;
7647 
7648 	/* It is assumed that the caller of this function
7649 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7650 	 */
7651 	if (vsi->netdev) {
7652 		netif_carrier_off(vsi->netdev);
7653 		netif_tx_disable(vsi->netdev);
7654 	}
7655 	i40e_vsi_disable_irq(vsi);
7656 	i40e_vsi_stop_rings(vsi);
7657 	if (vsi->type == I40E_VSI_MAIN &&
7658 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7659 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7660 		i40e_force_link_state(vsi->back, false);
7661 	i40e_napi_disable_all(vsi);
7662 
7663 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7664 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7665 		if (i40e_enabled_xdp_vsi(vsi)) {
7666 			/* Make sure that in-progress ndo_xdp_xmit and
7667 			 * ndo_xsk_wakeup calls are completed.
7668 			 */
7669 			synchronize_rcu();
7670 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7671 		}
7672 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7673 	}
7674 
7675 }
7676 
7677 /**
7678  * i40e_validate_mqprio_qopt- validate queue mapping info
7679  * @vsi: the VSI being configured
7680  * @mqprio_qopt: queue parametrs
7681  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7682 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7683 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7684 {
7685 	u64 sum_max_rate = 0;
7686 	u64 max_rate = 0;
7687 	int i;
7688 
7689 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7690 	    mqprio_qopt->qopt.num_tc < 1 ||
7691 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7692 		return -EINVAL;
7693 	for (i = 0; ; i++) {
7694 		if (!mqprio_qopt->qopt.count[i])
7695 			return -EINVAL;
7696 		if (mqprio_qopt->min_rate[i]) {
7697 			dev_err(&vsi->back->pdev->dev,
7698 				"Invalid min tx rate (greater than 0) specified\n");
7699 			return -EINVAL;
7700 		}
7701 		max_rate = mqprio_qopt->max_rate[i];
7702 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7703 		sum_max_rate += max_rate;
7704 
7705 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7706 			break;
7707 		if (mqprio_qopt->qopt.offset[i + 1] !=
7708 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7709 			return -EINVAL;
7710 	}
7711 	if (vsi->num_queue_pairs <
7712 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7713 		dev_err(&vsi->back->pdev->dev,
7714 			"Failed to create traffic channel, insufficient number of queues.\n");
7715 		return -EINVAL;
7716 	}
7717 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7718 		dev_err(&vsi->back->pdev->dev,
7719 			"Invalid max tx rate specified\n");
7720 		return -EINVAL;
7721 	}
7722 	return 0;
7723 }
7724 
7725 /**
7726  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7727  * @vsi: the VSI being configured
7728  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7729 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7730 {
7731 	u16 qcount;
7732 	int i;
7733 
7734 	/* Only TC0 is enabled */
7735 	vsi->tc_config.numtc = 1;
7736 	vsi->tc_config.enabled_tc = 1;
7737 	qcount = min_t(int, vsi->alloc_queue_pairs,
7738 		       i40e_pf_get_max_q_per_tc(vsi->back));
7739 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7740 		/* For the TC that is not enabled set the offset to default
7741 		 * queue and allocate one queue for the given TC.
7742 		 */
7743 		vsi->tc_config.tc_info[i].qoffset = 0;
7744 		if (i == 0)
7745 			vsi->tc_config.tc_info[i].qcount = qcount;
7746 		else
7747 			vsi->tc_config.tc_info[i].qcount = 1;
7748 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7749 	}
7750 }
7751 
7752 /**
7753  * i40e_del_macvlan_filter
7754  * @hw: pointer to the HW structure
7755  * @seid: seid of the channel VSI
7756  * @macaddr: the mac address to apply as a filter
7757  * @aq_err: store the admin Q error
7758  *
7759  * This function deletes a mac filter on the channel VSI which serves as the
7760  * macvlan. Returns 0 on success.
7761  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7762 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7763 				   const u8 *macaddr, int *aq_err)
7764 {
7765 	struct i40e_aqc_remove_macvlan_element_data element;
7766 	int status;
7767 
7768 	memset(&element, 0, sizeof(element));
7769 	ether_addr_copy(element.mac_addr, macaddr);
7770 	element.vlan_tag = 0;
7771 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7772 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7773 	*aq_err = hw->aq.asq_last_status;
7774 
7775 	return status;
7776 }
7777 
7778 /**
7779  * i40e_add_macvlan_filter
7780  * @hw: pointer to the HW structure
7781  * @seid: seid of the channel VSI
7782  * @macaddr: the mac address to apply as a filter
7783  * @aq_err: store the admin Q error
7784  *
7785  * This function adds a mac filter on the channel VSI which serves as the
7786  * macvlan. Returns 0 on success.
7787  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7788 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7789 				   const u8 *macaddr, int *aq_err)
7790 {
7791 	struct i40e_aqc_add_macvlan_element_data element;
7792 	u16 cmd_flags = 0;
7793 	int status;
7794 
7795 	ether_addr_copy(element.mac_addr, macaddr);
7796 	element.vlan_tag = 0;
7797 	element.queue_number = 0;
7798 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7799 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7800 	element.flags = cpu_to_le16(cmd_flags);
7801 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7802 	*aq_err = hw->aq.asq_last_status;
7803 
7804 	return status;
7805 }
7806 
7807 /**
7808  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7809  * @vsi: the VSI we want to access
7810  * @ch: the channel we want to access
7811  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7812 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7813 {
7814 	struct i40e_ring *tx_ring, *rx_ring;
7815 	u16 pf_q;
7816 	int i;
7817 
7818 	for (i = 0; i < ch->num_queue_pairs; i++) {
7819 		pf_q = ch->base_queue + i;
7820 		tx_ring = vsi->tx_rings[pf_q];
7821 		tx_ring->ch = NULL;
7822 		rx_ring = vsi->rx_rings[pf_q];
7823 		rx_ring->ch = NULL;
7824 	}
7825 }
7826 
7827 /**
7828  * i40e_free_macvlan_channels
7829  * @vsi: the VSI we want to access
7830  *
7831  * This function frees the Qs of the channel VSI from
7832  * the stack and also deletes the channel VSIs which
7833  * serve as macvlans.
7834  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7835 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7836 {
7837 	struct i40e_channel *ch, *ch_tmp;
7838 	int ret;
7839 
7840 	if (list_empty(&vsi->macvlan_list))
7841 		return;
7842 
7843 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7844 		struct i40e_vsi *parent_vsi;
7845 
7846 		if (i40e_is_channel_macvlan(ch)) {
7847 			i40e_reset_ch_rings(vsi, ch);
7848 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7849 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7850 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7851 			kfree(ch->fwd);
7852 			ch->fwd = NULL;
7853 		}
7854 
7855 		list_del(&ch->list);
7856 		parent_vsi = ch->parent_vsi;
7857 		if (!parent_vsi || !ch->initialized) {
7858 			kfree(ch);
7859 			continue;
7860 		}
7861 
7862 		/* remove the VSI */
7863 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7864 					     NULL);
7865 		if (ret)
7866 			dev_err(&vsi->back->pdev->dev,
7867 				"unable to remove channel (%d) for parent VSI(%d)\n",
7868 				ch->seid, parent_vsi->seid);
7869 		kfree(ch);
7870 	}
7871 	vsi->macvlan_cnt = 0;
7872 }
7873 
7874 /**
7875  * i40e_fwd_ring_up - bring the macvlan device up
7876  * @vsi: the VSI we want to access
7877  * @vdev: macvlan netdevice
7878  * @fwd: the private fwd structure
7879  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7880 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7881 			    struct i40e_fwd_adapter *fwd)
7882 {
7883 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7884 	int ret = 0, num_tc = 1,  i, aq_err;
7885 	struct i40e_pf *pf = vsi->back;
7886 	struct i40e_hw *hw = &pf->hw;
7887 
7888 	/* Go through the list and find an available channel */
7889 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7890 		if (!i40e_is_channel_macvlan(iter)) {
7891 			iter->fwd = fwd;
7892 			/* record configuration for macvlan interface in vdev */
7893 			for (i = 0; i < num_tc; i++)
7894 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7895 							     i,
7896 							     iter->num_queue_pairs,
7897 							     iter->base_queue);
7898 			for (i = 0; i < iter->num_queue_pairs; i++) {
7899 				struct i40e_ring *tx_ring, *rx_ring;
7900 				u16 pf_q;
7901 
7902 				pf_q = iter->base_queue + i;
7903 
7904 				/* Get to TX ring ptr */
7905 				tx_ring = vsi->tx_rings[pf_q];
7906 				tx_ring->ch = iter;
7907 
7908 				/* Get the RX ring ptr */
7909 				rx_ring = vsi->rx_rings[pf_q];
7910 				rx_ring->ch = iter;
7911 			}
7912 			ch = iter;
7913 			break;
7914 		}
7915 	}
7916 
7917 	if (!ch)
7918 		return -EINVAL;
7919 
7920 	/* Guarantee all rings are updated before we update the
7921 	 * MAC address filter.
7922 	 */
7923 	wmb();
7924 
7925 	/* Add a mac filter */
7926 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7927 	if (ret) {
7928 		/* if we cannot add the MAC rule then disable the offload */
7929 		macvlan_release_l2fw_offload(vdev);
7930 		for (i = 0; i < ch->num_queue_pairs; i++) {
7931 			struct i40e_ring *rx_ring;
7932 			u16 pf_q;
7933 
7934 			pf_q = ch->base_queue + i;
7935 			rx_ring = vsi->rx_rings[pf_q];
7936 			rx_ring->netdev = NULL;
7937 		}
7938 		dev_info(&pf->pdev->dev,
7939 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7940 			  ERR_PTR(ret),
7941 			  i40e_aq_str(hw, aq_err));
7942 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7943 	}
7944 
7945 	return ret;
7946 }
7947 
7948 /**
7949  * i40e_setup_macvlans - create the channels which will be macvlans
7950  * @vsi: the VSI we want to access
7951  * @macvlan_cnt: no. of macvlans to be setup
7952  * @qcnt: no. of Qs per macvlan
7953  * @vdev: macvlan netdevice
7954  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7955 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7956 			       struct net_device *vdev)
7957 {
7958 	struct i40e_pf *pf = vsi->back;
7959 	struct i40e_hw *hw = &pf->hw;
7960 	struct i40e_vsi_context ctxt;
7961 	u16 sections, qmap, num_qps;
7962 	struct i40e_channel *ch;
7963 	int i, pow, ret = 0;
7964 	u8 offset = 0;
7965 
7966 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7967 		return -EINVAL;
7968 
7969 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7970 
7971 	/* find the next higher power-of-2 of num queue pairs */
7972 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7973 
7974 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7975 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7976 
7977 	/* Setup context bits for the main VSI */
7978 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7979 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7980 	memset(&ctxt, 0, sizeof(ctxt));
7981 	ctxt.seid = vsi->seid;
7982 	ctxt.pf_num = vsi->back->hw.pf_id;
7983 	ctxt.vf_num = 0;
7984 	ctxt.uplink_seid = vsi->uplink_seid;
7985 	ctxt.info = vsi->info;
7986 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7987 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7988 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7989 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7990 
7991 	/* Reconfigure RSS for main VSI with new max queue count */
7992 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7993 	ret = i40e_vsi_config_rss(vsi);
7994 	if (ret) {
7995 		dev_info(&pf->pdev->dev,
7996 			 "Failed to reconfig RSS for num_queues (%u)\n",
7997 			 vsi->rss_size);
7998 		return ret;
7999 	}
8000 	vsi->reconfig_rss = true;
8001 	dev_dbg(&vsi->back->pdev->dev,
8002 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
8003 	vsi->next_base_queue = num_qps;
8004 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
8005 
8006 	/* Update the VSI after updating the VSI queue-mapping
8007 	 * information
8008 	 */
8009 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8010 	if (ret) {
8011 		dev_info(&pf->pdev->dev,
8012 			 "Update vsi tc config failed, err %pe aq_err %s\n",
8013 			 ERR_PTR(ret),
8014 			 i40e_aq_str(hw, hw->aq.asq_last_status));
8015 		return ret;
8016 	}
8017 	/* update the local VSI info with updated queue map */
8018 	i40e_vsi_update_queue_map(vsi, &ctxt);
8019 	vsi->info.valid_sections = 0;
8020 
8021 	/* Create channels for macvlans */
8022 	INIT_LIST_HEAD(&vsi->macvlan_list);
8023 	for (i = 0; i < macvlan_cnt; i++) {
8024 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8025 		if (!ch) {
8026 			ret = -ENOMEM;
8027 			goto err_free;
8028 		}
8029 		INIT_LIST_HEAD(&ch->list);
8030 		ch->num_queue_pairs = qcnt;
8031 		if (!i40e_setup_channel(pf, vsi, ch)) {
8032 			ret = -EINVAL;
8033 			kfree(ch);
8034 			goto err_free;
8035 		}
8036 		ch->parent_vsi = vsi;
8037 		vsi->cnt_q_avail -= ch->num_queue_pairs;
8038 		vsi->macvlan_cnt++;
8039 		list_add_tail(&ch->list, &vsi->macvlan_list);
8040 	}
8041 
8042 	return ret;
8043 
8044 err_free:
8045 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
8046 	i40e_free_macvlan_channels(vsi);
8047 
8048 	return ret;
8049 }
8050 
8051 /**
8052  * i40e_fwd_add - configure macvlans
8053  * @netdev: net device to configure
8054  * @vdev: macvlan netdevice
8055  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)8056 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
8057 {
8058 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8059 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
8060 	struct i40e_vsi *vsi = np->vsi;
8061 	struct i40e_pf *pf = vsi->back;
8062 	struct i40e_fwd_adapter *fwd;
8063 	int avail_macvlan, ret;
8064 
8065 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8066 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8067 		return ERR_PTR(-EINVAL);
8068 	}
8069 	if (i40e_is_tc_mqprio_enabled(pf)) {
8070 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8071 		return ERR_PTR(-EINVAL);
8072 	}
8073 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8074 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8075 		return ERR_PTR(-EINVAL);
8076 	}
8077 
8078 	/* The macvlan device has to be a single Q device so that the
8079 	 * tc_to_txq field can be reused to pick the tx queue.
8080 	 */
8081 	if (netif_is_multiqueue(vdev))
8082 		return ERR_PTR(-ERANGE);
8083 
8084 	if (!vsi->macvlan_cnt) {
8085 		/* reserve bit 0 for the pf device */
8086 		set_bit(0, vsi->fwd_bitmask);
8087 
8088 		/* Try to reserve as many queues as possible for macvlans. First
8089 		 * reserve 3/4th of max vectors, then half, then quarter and
8090 		 * calculate Qs per macvlan as you go
8091 		 */
8092 		vectors = pf->num_lan_msix;
8093 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8094 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8095 			q_per_macvlan = 4;
8096 			macvlan_cnt = (vectors - 32) / 4;
8097 		} else if (vectors <= 64 && vectors > 32) {
8098 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8099 			q_per_macvlan = 2;
8100 			macvlan_cnt = (vectors - 16) / 2;
8101 		} else if (vectors <= 32 && vectors > 16) {
8102 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8103 			q_per_macvlan = 1;
8104 			macvlan_cnt = vectors - 16;
8105 		} else if (vectors <= 16 && vectors > 8) {
8106 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8107 			q_per_macvlan = 1;
8108 			macvlan_cnt = vectors - 8;
8109 		} else {
8110 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8111 			q_per_macvlan = 1;
8112 			macvlan_cnt = vectors - 1;
8113 		}
8114 
8115 		if (macvlan_cnt == 0)
8116 			return ERR_PTR(-EBUSY);
8117 
8118 		/* Quiesce VSI queues */
8119 		i40e_quiesce_vsi(vsi);
8120 
8121 		/* sets up the macvlans but does not "enable" them */
8122 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8123 					  vdev);
8124 		if (ret)
8125 			return ERR_PTR(ret);
8126 
8127 		/* Unquiesce VSI */
8128 		i40e_unquiesce_vsi(vsi);
8129 	}
8130 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8131 					    vsi->macvlan_cnt);
8132 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8133 		return ERR_PTR(-EBUSY);
8134 
8135 	/* create the fwd struct */
8136 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8137 	if (!fwd)
8138 		return ERR_PTR(-ENOMEM);
8139 
8140 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8141 	fwd->bit_no = avail_macvlan;
8142 	netdev_set_sb_channel(vdev, avail_macvlan);
8143 	fwd->netdev = vdev;
8144 
8145 	if (!netif_running(netdev))
8146 		return fwd;
8147 
8148 	/* Set fwd ring up */
8149 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8150 	if (ret) {
8151 		/* unbind the queues and drop the subordinate channel config */
8152 		netdev_unbind_sb_channel(netdev, vdev);
8153 		netdev_set_sb_channel(vdev, 0);
8154 
8155 		kfree(fwd);
8156 		return ERR_PTR(-EINVAL);
8157 	}
8158 
8159 	return fwd;
8160 }
8161 
8162 /**
8163  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8164  * @vsi: the VSI we want to access
8165  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8166 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8167 {
8168 	struct i40e_channel *ch, *ch_tmp;
8169 	struct i40e_pf *pf = vsi->back;
8170 	struct i40e_hw *hw = &pf->hw;
8171 	int aq_err, ret = 0;
8172 
8173 	if (list_empty(&vsi->macvlan_list))
8174 		return;
8175 
8176 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8177 		if (i40e_is_channel_macvlan(ch)) {
8178 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8179 						      i40e_channel_mac(ch),
8180 						      &aq_err);
8181 			if (!ret) {
8182 				/* Reset queue contexts */
8183 				i40e_reset_ch_rings(vsi, ch);
8184 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8185 				netdev_unbind_sb_channel(vsi->netdev,
8186 							 ch->fwd->netdev);
8187 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8188 				kfree(ch->fwd);
8189 				ch->fwd = NULL;
8190 			}
8191 		}
8192 	}
8193 }
8194 
8195 /**
8196  * i40e_fwd_del - delete macvlan interfaces
8197  * @netdev: net device to configure
8198  * @vdev: macvlan netdevice
8199  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8200 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8201 {
8202 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8203 	struct i40e_fwd_adapter *fwd = vdev;
8204 	struct i40e_channel *ch, *ch_tmp;
8205 	struct i40e_vsi *vsi = np->vsi;
8206 	struct i40e_pf *pf = vsi->back;
8207 	struct i40e_hw *hw = &pf->hw;
8208 	int aq_err, ret = 0;
8209 
8210 	/* Find the channel associated with the macvlan and del mac filter */
8211 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8212 		if (i40e_is_channel_macvlan(ch) &&
8213 		    ether_addr_equal(i40e_channel_mac(ch),
8214 				     fwd->netdev->dev_addr)) {
8215 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8216 						      i40e_channel_mac(ch),
8217 						      &aq_err);
8218 			if (!ret) {
8219 				/* Reset queue contexts */
8220 				i40e_reset_ch_rings(vsi, ch);
8221 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8222 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8223 				netdev_set_sb_channel(fwd->netdev, 0);
8224 				kfree(ch->fwd);
8225 				ch->fwd = NULL;
8226 			} else {
8227 				dev_info(&pf->pdev->dev,
8228 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8229 					  ERR_PTR(ret),
8230 					  i40e_aq_str(hw, aq_err));
8231 			}
8232 			break;
8233 		}
8234 	}
8235 }
8236 
8237 /**
8238  * i40e_setup_tc - configure multiple traffic classes
8239  * @netdev: net device to configure
8240  * @type_data: tc offload data
8241  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8242 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8243 {
8244 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8245 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8246 	struct i40e_vsi *vsi = np->vsi;
8247 	struct i40e_pf *pf = vsi->back;
8248 	u8 enabled_tc = 0, num_tc, hw;
8249 	bool need_reset = false;
8250 	int old_queue_pairs;
8251 	int ret = -EINVAL;
8252 	u16 mode;
8253 	int i;
8254 
8255 	old_queue_pairs = vsi->num_queue_pairs;
8256 	num_tc = mqprio_qopt->qopt.num_tc;
8257 	hw = mqprio_qopt->qopt.hw;
8258 	mode = mqprio_qopt->mode;
8259 	if (!hw) {
8260 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8261 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8262 		goto config_tc;
8263 	}
8264 
8265 	/* Check if MFP enabled */
8266 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8267 		netdev_info(netdev,
8268 			    "Configuring TC not supported in MFP mode\n");
8269 		return ret;
8270 	}
8271 	switch (mode) {
8272 	case TC_MQPRIO_MODE_DCB:
8273 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8274 
8275 		/* Check if DCB enabled to continue */
8276 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8277 			netdev_info(netdev,
8278 				    "DCB is not enabled for adapter\n");
8279 			return ret;
8280 		}
8281 
8282 		/* Check whether tc count is within enabled limit */
8283 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8284 			netdev_info(netdev,
8285 				    "TC count greater than enabled on link for adapter\n");
8286 			return ret;
8287 		}
8288 		break;
8289 	case TC_MQPRIO_MODE_CHANNEL:
8290 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8291 			netdev_info(netdev,
8292 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8293 			return ret;
8294 		}
8295 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8296 			return ret;
8297 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8298 		if (ret)
8299 			return ret;
8300 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8301 		       sizeof(*mqprio_qopt));
8302 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8303 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8304 		break;
8305 	default:
8306 		return -EINVAL;
8307 	}
8308 
8309 config_tc:
8310 	/* Generate TC map for number of tc requested */
8311 	for (i = 0; i < num_tc; i++)
8312 		enabled_tc |= BIT(i);
8313 
8314 	/* Requesting same TC configuration as already enabled */
8315 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8316 	    mode != TC_MQPRIO_MODE_CHANNEL)
8317 		return 0;
8318 
8319 	/* Quiesce VSI queues */
8320 	i40e_quiesce_vsi(vsi);
8321 
8322 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8323 		i40e_remove_queue_channels(vsi);
8324 
8325 	/* Configure VSI for enabled TCs */
8326 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8327 	if (ret) {
8328 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8329 			    vsi->seid);
8330 		need_reset = true;
8331 		goto exit;
8332 	} else if (enabled_tc &&
8333 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8334 		netdev_info(netdev,
8335 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8336 			    vsi->tc_config.tc_info[0].qcount);
8337 		ret = -EINVAL;
8338 		need_reset = true;
8339 		goto exit;
8340 	}
8341 
8342 	dev_info(&vsi->back->pdev->dev,
8343 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8344 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8345 
8346 	if (i40e_is_tc_mqprio_enabled(pf)) {
8347 		if (vsi->mqprio_qopt.max_rate[0]) {
8348 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8349 						  vsi->mqprio_qopt.max_rate[0]);
8350 
8351 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8352 			if (!ret) {
8353 				u64 credits = max_tx_rate;
8354 
8355 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8356 				dev_dbg(&vsi->back->pdev->dev,
8357 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8358 					max_tx_rate,
8359 					credits,
8360 					vsi->seid);
8361 			} else {
8362 				need_reset = true;
8363 				goto exit;
8364 			}
8365 		}
8366 		ret = i40e_configure_queue_channels(vsi);
8367 		if (ret) {
8368 			vsi->num_queue_pairs = old_queue_pairs;
8369 			netdev_info(netdev,
8370 				    "Failed configuring queue channels\n");
8371 			need_reset = true;
8372 			goto exit;
8373 		}
8374 	}
8375 
8376 exit:
8377 	/* Reset the configuration data to defaults, only TC0 is enabled */
8378 	if (need_reset) {
8379 		i40e_vsi_set_default_tc_config(vsi);
8380 		need_reset = false;
8381 	}
8382 
8383 	/* Unquiesce VSI */
8384 	i40e_unquiesce_vsi(vsi);
8385 	return ret;
8386 }
8387 
8388 /**
8389  * i40e_set_cld_element - sets cloud filter element data
8390  * @filter: cloud filter rule
8391  * @cld: ptr to cloud filter element data
8392  *
8393  * This is helper function to copy data into cloud filter element
8394  **/
8395 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8396 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8397 		     struct i40e_aqc_cloud_filters_element_data *cld)
8398 {
8399 	u32 ipa;
8400 	int i;
8401 
8402 	memset(cld, 0, sizeof(*cld));
8403 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8404 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8405 
8406 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8407 		return;
8408 
8409 	if (filter->n_proto == ETH_P_IPV6) {
8410 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8411 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8412 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8413 
8414 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8415 		}
8416 	} else {
8417 		ipa = be32_to_cpu(filter->dst_ipv4);
8418 
8419 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8420 	}
8421 
8422 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8423 
8424 	/* tenant_id is not supported by FW now, once the support is enabled
8425 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8426 	 */
8427 	if (filter->tenant_id)
8428 		return;
8429 }
8430 
8431 /**
8432  * i40e_add_del_cloud_filter - Add/del cloud filter
8433  * @vsi: pointer to VSI
8434  * @filter: cloud filter rule
8435  * @add: if true, add, if false, delete
8436  *
8437  * Add or delete a cloud filter for a specific flow spec.
8438  * Returns 0 if the filter were successfully added.
8439  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8440 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8441 			      struct i40e_cloud_filter *filter, bool add)
8442 {
8443 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8444 	struct i40e_pf *pf = vsi->back;
8445 	int ret;
8446 	static const u16 flag_table[128] = {
8447 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8448 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8449 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8450 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8451 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8452 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8453 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8454 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8455 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8456 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8457 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8458 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8459 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8460 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8461 	};
8462 
8463 	if (filter->flags >= ARRAY_SIZE(flag_table))
8464 		return -EIO;
8465 
8466 	memset(&cld_filter, 0, sizeof(cld_filter));
8467 
8468 	/* copy element needed to add cloud filter from filter */
8469 	i40e_set_cld_element(filter, &cld_filter);
8470 
8471 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8472 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8473 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8474 
8475 	if (filter->n_proto == ETH_P_IPV6)
8476 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8477 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8478 	else
8479 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8480 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8481 
8482 	if (add)
8483 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8484 						&cld_filter, 1);
8485 	else
8486 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8487 						&cld_filter, 1);
8488 	if (ret)
8489 		dev_dbg(&pf->pdev->dev,
8490 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8491 			add ? "add" : "delete", filter->dst_port, ret,
8492 			pf->hw.aq.asq_last_status);
8493 	else
8494 		dev_info(&pf->pdev->dev,
8495 			 "%s cloud filter for VSI: %d\n",
8496 			 add ? "Added" : "Deleted", filter->seid);
8497 	return ret;
8498 }
8499 
8500 /**
8501  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8502  * @vsi: pointer to VSI
8503  * @filter: cloud filter rule
8504  * @add: if true, add, if false, delete
8505  *
8506  * Add or delete a cloud filter for a specific flow spec using big buffer.
8507  * Returns 0 if the filter were successfully added.
8508  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8509 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8510 				      struct i40e_cloud_filter *filter,
8511 				      bool add)
8512 {
8513 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8514 	struct i40e_pf *pf = vsi->back;
8515 	int ret;
8516 
8517 	/* Both (src/dst) valid mac_addr are not supported */
8518 	if ((is_valid_ether_addr(filter->dst_mac) &&
8519 	     is_valid_ether_addr(filter->src_mac)) ||
8520 	    (is_multicast_ether_addr(filter->dst_mac) &&
8521 	     is_multicast_ether_addr(filter->src_mac)))
8522 		return -EOPNOTSUPP;
8523 
8524 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8525 	 * ports are not supported via big buffer now.
8526 	 */
8527 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8528 		return -EOPNOTSUPP;
8529 
8530 	/* adding filter using src_port/src_ip is not supported at this stage */
8531 	if (filter->src_port ||
8532 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8533 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8534 		return -EOPNOTSUPP;
8535 
8536 	memset(&cld_filter, 0, sizeof(cld_filter));
8537 
8538 	/* copy element needed to add cloud filter from filter */
8539 	i40e_set_cld_element(filter, &cld_filter.element);
8540 
8541 	if (is_valid_ether_addr(filter->dst_mac) ||
8542 	    is_valid_ether_addr(filter->src_mac) ||
8543 	    is_multicast_ether_addr(filter->dst_mac) ||
8544 	    is_multicast_ether_addr(filter->src_mac)) {
8545 		/* MAC + IP : unsupported mode */
8546 		if (filter->dst_ipv4)
8547 			return -EOPNOTSUPP;
8548 
8549 		/* since we validated that L4 port must be valid before
8550 		 * we get here, start with respective "flags" value
8551 		 * and update if vlan is present or not
8552 		 */
8553 		cld_filter.element.flags =
8554 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8555 
8556 		if (filter->vlan_id) {
8557 			cld_filter.element.flags =
8558 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8559 		}
8560 
8561 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8562 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8563 		cld_filter.element.flags =
8564 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8565 		if (filter->n_proto == ETH_P_IPV6)
8566 			cld_filter.element.flags |=
8567 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8568 		else
8569 			cld_filter.element.flags |=
8570 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8571 	} else {
8572 		dev_err(&pf->pdev->dev,
8573 			"either mac or ip has to be valid for cloud filter\n");
8574 		return -EINVAL;
8575 	}
8576 
8577 	/* Now copy L4 port in Byte 6..7 in general fields */
8578 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8579 						be16_to_cpu(filter->dst_port);
8580 
8581 	if (add) {
8582 		/* Validate current device switch mode, change if necessary */
8583 		ret = i40e_validate_and_set_switch_mode(vsi);
8584 		if (ret) {
8585 			dev_err(&pf->pdev->dev,
8586 				"failed to set switch mode, ret %d\n",
8587 				ret);
8588 			return ret;
8589 		}
8590 
8591 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8592 						   &cld_filter, 1);
8593 	} else {
8594 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8595 						   &cld_filter, 1);
8596 	}
8597 
8598 	if (ret)
8599 		dev_dbg(&pf->pdev->dev,
8600 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8601 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8602 	else
8603 		dev_info(&pf->pdev->dev,
8604 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8605 			 add ? "add" : "delete", filter->seid,
8606 			 ntohs(filter->dst_port));
8607 	return ret;
8608 }
8609 
8610 /**
8611  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8612  * @vsi: Pointer to VSI
8613  * @f: Pointer to struct flow_cls_offload
8614  * @filter: Pointer to cloud filter structure
8615  *
8616  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8617 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8618 				 struct flow_cls_offload *f,
8619 				 struct i40e_cloud_filter *filter)
8620 {
8621 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8622 	struct flow_dissector *dissector = rule->match.dissector;
8623 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8624 	struct i40e_pf *pf = vsi->back;
8625 	u8 field_flags = 0;
8626 
8627 	if (dissector->used_keys &
8628 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8629 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8630 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8631 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8632 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8633 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8634 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8635 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8636 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8637 			dissector->used_keys);
8638 		return -EOPNOTSUPP;
8639 	}
8640 
8641 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8642 		struct flow_match_enc_keyid match;
8643 
8644 		flow_rule_match_enc_keyid(rule, &match);
8645 		if (match.mask->keyid != 0)
8646 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8647 
8648 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8649 	}
8650 
8651 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8652 		struct flow_match_basic match;
8653 
8654 		flow_rule_match_basic(rule, &match);
8655 		n_proto_key = ntohs(match.key->n_proto);
8656 		n_proto_mask = ntohs(match.mask->n_proto);
8657 
8658 		if (n_proto_key == ETH_P_ALL) {
8659 			n_proto_key = 0;
8660 			n_proto_mask = 0;
8661 		}
8662 		filter->n_proto = n_proto_key & n_proto_mask;
8663 		filter->ip_proto = match.key->ip_proto;
8664 	}
8665 
8666 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8667 		struct flow_match_eth_addrs match;
8668 
8669 		flow_rule_match_eth_addrs(rule, &match);
8670 
8671 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8672 		if (!is_zero_ether_addr(match.mask->dst)) {
8673 			if (is_broadcast_ether_addr(match.mask->dst)) {
8674 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8675 			} else {
8676 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8677 					match.mask->dst);
8678 				return -EIO;
8679 			}
8680 		}
8681 
8682 		if (!is_zero_ether_addr(match.mask->src)) {
8683 			if (is_broadcast_ether_addr(match.mask->src)) {
8684 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8685 			} else {
8686 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8687 					match.mask->src);
8688 				return -EIO;
8689 			}
8690 		}
8691 		ether_addr_copy(filter->dst_mac, match.key->dst);
8692 		ether_addr_copy(filter->src_mac, match.key->src);
8693 	}
8694 
8695 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8696 		struct flow_match_vlan match;
8697 
8698 		flow_rule_match_vlan(rule, &match);
8699 		if (match.mask->vlan_id) {
8700 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8701 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8702 
8703 			} else {
8704 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8705 					match.mask->vlan_id);
8706 				return -EIO;
8707 			}
8708 		}
8709 
8710 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8711 	}
8712 
8713 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8714 		struct flow_match_control match;
8715 
8716 		flow_rule_match_control(rule, &match);
8717 		addr_type = match.key->addr_type;
8718 
8719 		if (flow_rule_has_control_flags(match.mask->flags,
8720 						f->common.extack))
8721 			return -EOPNOTSUPP;
8722 	}
8723 
8724 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8725 		struct flow_match_ipv4_addrs match;
8726 
8727 		flow_rule_match_ipv4_addrs(rule, &match);
8728 		if (match.mask->dst) {
8729 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8730 				field_flags |= I40E_CLOUD_FIELD_IIP;
8731 			} else {
8732 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8733 					&match.mask->dst);
8734 				return -EIO;
8735 			}
8736 		}
8737 
8738 		if (match.mask->src) {
8739 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8740 				field_flags |= I40E_CLOUD_FIELD_IIP;
8741 			} else {
8742 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8743 					&match.mask->src);
8744 				return -EIO;
8745 			}
8746 		}
8747 
8748 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8749 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8750 			return -EIO;
8751 		}
8752 		filter->dst_ipv4 = match.key->dst;
8753 		filter->src_ipv4 = match.key->src;
8754 	}
8755 
8756 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8757 		struct flow_match_ipv6_addrs match;
8758 
8759 		flow_rule_match_ipv6_addrs(rule, &match);
8760 
8761 		/* src and dest IPV6 address should not be LOOPBACK
8762 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8763 		 */
8764 		if (ipv6_addr_loopback(&match.key->dst) ||
8765 		    ipv6_addr_loopback(&match.key->src)) {
8766 			dev_err(&pf->pdev->dev,
8767 				"Bad ipv6, addr is LOOPBACK\n");
8768 			return -EIO;
8769 		}
8770 		if (!ipv6_addr_any(&match.mask->dst) ||
8771 		    !ipv6_addr_any(&match.mask->src))
8772 			field_flags |= I40E_CLOUD_FIELD_IIP;
8773 
8774 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8775 		       sizeof(filter->src_ipv6));
8776 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8777 		       sizeof(filter->dst_ipv6));
8778 	}
8779 
8780 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8781 		struct flow_match_ports match;
8782 
8783 		flow_rule_match_ports(rule, &match);
8784 		if (match.mask->src) {
8785 			if (match.mask->src == cpu_to_be16(0xffff)) {
8786 				field_flags |= I40E_CLOUD_FIELD_IIP;
8787 			} else {
8788 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8789 					be16_to_cpu(match.mask->src));
8790 				return -EIO;
8791 			}
8792 		}
8793 
8794 		if (match.mask->dst) {
8795 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8796 				field_flags |= I40E_CLOUD_FIELD_IIP;
8797 			} else {
8798 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8799 					be16_to_cpu(match.mask->dst));
8800 				return -EIO;
8801 			}
8802 		}
8803 
8804 		filter->dst_port = match.key->dst;
8805 		filter->src_port = match.key->src;
8806 
8807 		switch (filter->ip_proto) {
8808 		case IPPROTO_TCP:
8809 		case IPPROTO_UDP:
8810 			break;
8811 		default:
8812 			dev_err(&pf->pdev->dev,
8813 				"Only UDP and TCP transport are supported\n");
8814 			return -EINVAL;
8815 		}
8816 	}
8817 	filter->flags = field_flags;
8818 	return 0;
8819 }
8820 
8821 /**
8822  * i40e_handle_tclass: Forward to a traffic class on the device
8823  * @vsi: Pointer to VSI
8824  * @tc: traffic class index on the device
8825  * @filter: Pointer to cloud filter structure
8826  *
8827  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8828 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8829 			      struct i40e_cloud_filter *filter)
8830 {
8831 	struct i40e_channel *ch, *ch_tmp;
8832 
8833 	/* direct to a traffic class on the same device */
8834 	if (tc == 0) {
8835 		filter->seid = vsi->seid;
8836 		return 0;
8837 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8838 		if (!filter->dst_port) {
8839 			dev_err(&vsi->back->pdev->dev,
8840 				"Specify destination port to direct to traffic class that is not default\n");
8841 			return -EINVAL;
8842 		}
8843 		if (list_empty(&vsi->ch_list))
8844 			return -EINVAL;
8845 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8846 					 list) {
8847 			if (ch->seid == vsi->tc_seid_map[tc])
8848 				filter->seid = ch->seid;
8849 		}
8850 		return 0;
8851 	}
8852 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8853 	return -EINVAL;
8854 }
8855 
8856 /**
8857  * i40e_configure_clsflower - Configure tc flower filters
8858  * @vsi: Pointer to VSI
8859  * @cls_flower: Pointer to struct flow_cls_offload
8860  *
8861  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8862 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8863 				    struct flow_cls_offload *cls_flower)
8864 {
8865 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8866 	struct i40e_cloud_filter *filter = NULL;
8867 	struct i40e_pf *pf = vsi->back;
8868 	int err = 0;
8869 
8870 	if (tc < 0) {
8871 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8872 		return -EOPNOTSUPP;
8873 	}
8874 
8875 	if (!tc) {
8876 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8877 		return -EINVAL;
8878 	}
8879 
8880 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8881 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8882 		return -EBUSY;
8883 
8884 	if (pf->fdir_pf_active_filters ||
8885 	    (!hlist_empty(&pf->fdir_filter_list))) {
8886 		dev_err(&vsi->back->pdev->dev,
8887 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8888 		return -EINVAL;
8889 	}
8890 
8891 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8892 		dev_err(&vsi->back->pdev->dev,
8893 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8894 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8895 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8896 	}
8897 
8898 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8899 	if (!filter)
8900 		return -ENOMEM;
8901 
8902 	filter->cookie = cls_flower->cookie;
8903 
8904 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8905 	if (err < 0)
8906 		goto err;
8907 
8908 	err = i40e_handle_tclass(vsi, tc, filter);
8909 	if (err < 0)
8910 		goto err;
8911 
8912 	/* Add cloud filter */
8913 	if (filter->dst_port)
8914 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8915 	else
8916 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8917 
8918 	if (err) {
8919 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8920 			err);
8921 		goto err;
8922 	}
8923 
8924 	/* add filter to the ordered list */
8925 	INIT_HLIST_NODE(&filter->cloud_node);
8926 
8927 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8928 
8929 	pf->num_cloud_filters++;
8930 
8931 	return err;
8932 err:
8933 	kfree(filter);
8934 	return err;
8935 }
8936 
8937 /**
8938  * i40e_find_cloud_filter - Find the could filter in the list
8939  * @vsi: Pointer to VSI
8940  * @cookie: filter specific cookie
8941  *
8942  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8943 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8944 							unsigned long *cookie)
8945 {
8946 	struct i40e_cloud_filter *filter = NULL;
8947 	struct hlist_node *node2;
8948 
8949 	hlist_for_each_entry_safe(filter, node2,
8950 				  &vsi->back->cloud_filter_list, cloud_node)
8951 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8952 			return filter;
8953 	return NULL;
8954 }
8955 
8956 /**
8957  * i40e_delete_clsflower - Remove tc flower filters
8958  * @vsi: Pointer to VSI
8959  * @cls_flower: Pointer to struct flow_cls_offload
8960  *
8961  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8962 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8963 				 struct flow_cls_offload *cls_flower)
8964 {
8965 	struct i40e_cloud_filter *filter = NULL;
8966 	struct i40e_pf *pf = vsi->back;
8967 	int err = 0;
8968 
8969 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8970 
8971 	if (!filter)
8972 		return -EINVAL;
8973 
8974 	hash_del(&filter->cloud_node);
8975 
8976 	if (filter->dst_port)
8977 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8978 	else
8979 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8980 
8981 	kfree(filter);
8982 	if (err) {
8983 		dev_err(&pf->pdev->dev,
8984 			"Failed to delete cloud filter, err %pe\n",
8985 			ERR_PTR(err));
8986 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8987 	}
8988 
8989 	pf->num_cloud_filters--;
8990 	if (!pf->num_cloud_filters)
8991 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8992 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8993 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8994 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8995 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8996 		}
8997 	return 0;
8998 }
8999 
9000 /**
9001  * i40e_setup_tc_cls_flower - flower classifier offloads
9002  * @np: net device to configure
9003  * @cls_flower: offload data
9004  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)9005 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
9006 				    struct flow_cls_offload *cls_flower)
9007 {
9008 	struct i40e_vsi *vsi = np->vsi;
9009 
9010 	switch (cls_flower->command) {
9011 	case FLOW_CLS_REPLACE:
9012 		return i40e_configure_clsflower(vsi, cls_flower);
9013 	case FLOW_CLS_DESTROY:
9014 		return i40e_delete_clsflower(vsi, cls_flower);
9015 	case FLOW_CLS_STATS:
9016 		return -EOPNOTSUPP;
9017 	default:
9018 		return -EOPNOTSUPP;
9019 	}
9020 }
9021 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)9022 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
9023 				  void *cb_priv)
9024 {
9025 	struct i40e_netdev_priv *np = cb_priv;
9026 
9027 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
9028 		return -EOPNOTSUPP;
9029 
9030 	switch (type) {
9031 	case TC_SETUP_CLSFLOWER:
9032 		return i40e_setup_tc_cls_flower(np, type_data);
9033 
9034 	default:
9035 		return -EOPNOTSUPP;
9036 	}
9037 }
9038 
9039 static LIST_HEAD(i40e_block_cb_list);
9040 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)9041 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9042 			   void *type_data)
9043 {
9044 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9045 
9046 	switch (type) {
9047 	case TC_SETUP_QDISC_MQPRIO:
9048 		return i40e_setup_tc(netdev, type_data);
9049 	case TC_SETUP_BLOCK:
9050 		return flow_block_cb_setup_simple(type_data,
9051 						  &i40e_block_cb_list,
9052 						  i40e_setup_tc_block_cb,
9053 						  np, np, true);
9054 	default:
9055 		return -EOPNOTSUPP;
9056 	}
9057 }
9058 
9059 /**
9060  * i40e_open - Called when a network interface is made active
9061  * @netdev: network interface device structure
9062  *
9063  * The open entry point is called when a network interface is made
9064  * active by the system (IFF_UP).  At this point all resources needed
9065  * for transmit and receive operations are allocated, the interrupt
9066  * handler is registered with the OS, the netdev watchdog subtask is
9067  * enabled, and the stack is notified that the interface is ready.
9068  *
9069  * Returns 0 on success, negative value on failure
9070  **/
i40e_open(struct net_device * netdev)9071 int i40e_open(struct net_device *netdev)
9072 {
9073 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9074 	struct i40e_vsi *vsi = np->vsi;
9075 	struct i40e_pf *pf = vsi->back;
9076 	int err;
9077 
9078 	/* disallow open during test or if eeprom is broken */
9079 	if (test_bit(__I40E_TESTING, pf->state) ||
9080 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9081 		return -EBUSY;
9082 
9083 	netif_carrier_off(netdev);
9084 
9085 	if (i40e_force_link_state(pf, true))
9086 		return -EAGAIN;
9087 
9088 	err = i40e_vsi_open(vsi);
9089 	if (err)
9090 		return err;
9091 
9092 	/* configure global TSO hardware offload settings */
9093 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9094 						       TCP_FLAG_FIN) >> 16);
9095 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9096 						       TCP_FLAG_FIN |
9097 						       TCP_FLAG_CWR) >> 16);
9098 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9099 	udp_tunnel_get_rx_info(netdev);
9100 
9101 	return 0;
9102 }
9103 
9104 /**
9105  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9106  * @vsi: vsi structure
9107  *
9108  * This updates netdev's number of tx/rx queues
9109  *
9110  * Returns status of setting tx/rx queues
9111  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9112 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9113 {
9114 	int ret;
9115 
9116 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9117 					   vsi->num_queue_pairs);
9118 	if (ret)
9119 		return ret;
9120 
9121 	return netif_set_real_num_tx_queues(vsi->netdev,
9122 					    vsi->num_queue_pairs);
9123 }
9124 
9125 /**
9126  * i40e_vsi_open -
9127  * @vsi: the VSI to open
9128  *
9129  * Finish initialization of the VSI.
9130  *
9131  * Returns 0 on success, negative value on failure
9132  *
9133  * Note: expects to be called while under rtnl_lock()
9134  **/
i40e_vsi_open(struct i40e_vsi * vsi)9135 int i40e_vsi_open(struct i40e_vsi *vsi)
9136 {
9137 	struct i40e_pf *pf = vsi->back;
9138 	char int_name[I40E_INT_NAME_STR_LEN];
9139 	int err;
9140 
9141 	/* allocate descriptors */
9142 	err = i40e_vsi_setup_tx_resources(vsi);
9143 	if (err)
9144 		goto err_setup_tx;
9145 	err = i40e_vsi_setup_rx_resources(vsi);
9146 	if (err)
9147 		goto err_setup_rx;
9148 
9149 	err = i40e_vsi_configure(vsi);
9150 	if (err)
9151 		goto err_setup_rx;
9152 
9153 	if (vsi->netdev) {
9154 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9155 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9156 		err = i40e_vsi_request_irq(vsi, int_name);
9157 		if (err)
9158 			goto err_setup_rx;
9159 
9160 		/* Notify the stack of the actual queue counts. */
9161 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9162 		if (err)
9163 			goto err_set_queues;
9164 
9165 	} else if (vsi->type == I40E_VSI_FDIR) {
9166 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9167 			 dev_driver_string(&pf->pdev->dev),
9168 			 dev_name(&pf->pdev->dev));
9169 		err = i40e_vsi_request_irq(vsi, int_name);
9170 		if (err)
9171 			goto err_setup_rx;
9172 
9173 	} else {
9174 		err = -EINVAL;
9175 		goto err_setup_rx;
9176 	}
9177 
9178 	err = i40e_up_complete(vsi);
9179 	if (err)
9180 		goto err_up_complete;
9181 
9182 	return 0;
9183 
9184 err_up_complete:
9185 	i40e_down(vsi);
9186 err_set_queues:
9187 	i40e_vsi_free_irq(vsi);
9188 err_setup_rx:
9189 	i40e_vsi_free_rx_resources(vsi);
9190 err_setup_tx:
9191 	i40e_vsi_free_tx_resources(vsi);
9192 	if (vsi->type == I40E_VSI_MAIN)
9193 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9194 
9195 	return err;
9196 }
9197 
9198 /**
9199  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9200  * @pf: Pointer to PF
9201  *
9202  * This function destroys the hlist where all the Flow Director
9203  * filters were saved.
9204  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9205 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9206 {
9207 	struct i40e_fdir_filter *filter;
9208 	struct i40e_flex_pit *pit_entry, *tmp;
9209 	struct hlist_node *node2;
9210 
9211 	hlist_for_each_entry_safe(filter, node2,
9212 				  &pf->fdir_filter_list, fdir_node) {
9213 		hlist_del(&filter->fdir_node);
9214 		kfree(filter);
9215 	}
9216 
9217 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9218 		list_del(&pit_entry->list);
9219 		kfree(pit_entry);
9220 	}
9221 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9222 
9223 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9224 		list_del(&pit_entry->list);
9225 		kfree(pit_entry);
9226 	}
9227 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9228 
9229 	pf->fdir_pf_active_filters = 0;
9230 	i40e_reset_fdir_filter_cnt(pf);
9231 
9232 	/* Reprogram the default input set for TCP/IPv4 */
9233 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9234 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9235 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9236 
9237 	/* Reprogram the default input set for TCP/IPv6 */
9238 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9239 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9240 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9241 
9242 	/* Reprogram the default input set for UDP/IPv4 */
9243 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9244 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9245 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9246 
9247 	/* Reprogram the default input set for UDP/IPv6 */
9248 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9249 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9250 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9251 
9252 	/* Reprogram the default input set for SCTP/IPv4 */
9253 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9254 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9255 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9256 
9257 	/* Reprogram the default input set for SCTP/IPv6 */
9258 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9259 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9260 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9261 
9262 	/* Reprogram the default input set for Other/IPv4 */
9263 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9264 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9265 
9266 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9267 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9268 
9269 	/* Reprogram the default input set for Other/IPv6 */
9270 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9271 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9272 
9273 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9274 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9275 }
9276 
9277 /**
9278  * i40e_cloud_filter_exit - Cleans up the cloud filters
9279  * @pf: Pointer to PF
9280  *
9281  * This function destroys the hlist where all the cloud filters
9282  * were saved.
9283  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9284 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9285 {
9286 	struct i40e_cloud_filter *cfilter;
9287 	struct hlist_node *node;
9288 
9289 	hlist_for_each_entry_safe(cfilter, node,
9290 				  &pf->cloud_filter_list, cloud_node) {
9291 		hlist_del(&cfilter->cloud_node);
9292 		kfree(cfilter);
9293 	}
9294 	pf->num_cloud_filters = 0;
9295 
9296 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9297 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9298 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9299 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9300 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9301 	}
9302 }
9303 
9304 /**
9305  * i40e_close - Disables a network interface
9306  * @netdev: network interface device structure
9307  *
9308  * The close entry point is called when an interface is de-activated
9309  * by the OS.  The hardware is still under the driver's control, but
9310  * this netdev interface is disabled.
9311  *
9312  * Returns 0, this is not allowed to fail
9313  **/
i40e_close(struct net_device * netdev)9314 int i40e_close(struct net_device *netdev)
9315 {
9316 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9317 	struct i40e_vsi *vsi = np->vsi;
9318 
9319 	i40e_vsi_close(vsi);
9320 
9321 	return 0;
9322 }
9323 
9324 /**
9325  * i40e_do_reset - Start a PF or Core Reset sequence
9326  * @pf: board private structure
9327  * @reset_flags: which reset is requested
9328  * @lock_acquired: indicates whether or not the lock has been acquired
9329  * before this function was called.
9330  *
9331  * The essential difference in resets is that the PF Reset
9332  * doesn't clear the packet buffers, doesn't reset the PE
9333  * firmware, and doesn't bother the other PFs on the chip.
9334  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9335 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9336 {
9337 	struct i40e_vsi *vsi;
9338 	u32 val;
9339 	int i;
9340 
9341 	/* do the biggest reset indicated */
9342 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9343 
9344 		/* Request a Global Reset
9345 		 *
9346 		 * This will start the chip's countdown to the actual full
9347 		 * chip reset event, and a warning interrupt to be sent
9348 		 * to all PFs, including the requestor.  Our handler
9349 		 * for the warning interrupt will deal with the shutdown
9350 		 * and recovery of the switch setup.
9351 		 */
9352 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9353 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9354 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9355 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9356 
9357 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9358 
9359 		/* Request a Core Reset
9360 		 *
9361 		 * Same as Global Reset, except does *not* include the MAC/PHY
9362 		 */
9363 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9364 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9365 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9366 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9367 		i40e_flush(&pf->hw);
9368 
9369 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9370 
9371 		/* Request a PF Reset
9372 		 *
9373 		 * Resets only the PF-specific registers
9374 		 *
9375 		 * This goes directly to the tear-down and rebuild of
9376 		 * the switch, since we need to do all the recovery as
9377 		 * for the Core Reset.
9378 		 */
9379 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9380 		i40e_handle_reset_warning(pf, lock_acquired);
9381 
9382 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9383 		/* Request a PF Reset
9384 		 *
9385 		 * Resets PF and reinitializes PFs VSI.
9386 		 */
9387 		i40e_prep_for_reset(pf);
9388 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9389 		dev_info(&pf->pdev->dev,
9390 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9391 			 "FW LLDP is disabled\n" :
9392 			 "FW LLDP is enabled\n");
9393 
9394 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9395 		/* Find the VSI(s) that requested a re-init */
9396 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9397 
9398 		i40e_pf_for_each_vsi(pf, i, vsi) {
9399 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9400 					       vsi->state))
9401 				i40e_vsi_reinit_locked(vsi);
9402 		}
9403 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9404 		/* Find the VSI(s) that needs to be brought down */
9405 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9406 
9407 		i40e_pf_for_each_vsi(pf, i, vsi) {
9408 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9409 					       vsi->state)) {
9410 				set_bit(__I40E_VSI_DOWN, vsi->state);
9411 				i40e_down(vsi);
9412 			}
9413 		}
9414 	} else {
9415 		dev_info(&pf->pdev->dev,
9416 			 "bad reset request 0x%08x\n", reset_flags);
9417 	}
9418 }
9419 
9420 #ifdef CONFIG_I40E_DCB
9421 /**
9422  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9423  * @pf: board private structure
9424  * @old_cfg: current DCB config
9425  * @new_cfg: new DCB config
9426  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9427 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9428 			    struct i40e_dcbx_config *old_cfg,
9429 			    struct i40e_dcbx_config *new_cfg)
9430 {
9431 	bool need_reconfig = false;
9432 
9433 	/* Check if ETS configuration has changed */
9434 	if (memcmp(&new_cfg->etscfg,
9435 		   &old_cfg->etscfg,
9436 		   sizeof(new_cfg->etscfg))) {
9437 		/* If Priority Table has changed reconfig is needed */
9438 		if (memcmp(&new_cfg->etscfg.prioritytable,
9439 			   &old_cfg->etscfg.prioritytable,
9440 			   sizeof(new_cfg->etscfg.prioritytable))) {
9441 			need_reconfig = true;
9442 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9443 		}
9444 
9445 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9446 			   &old_cfg->etscfg.tcbwtable,
9447 			   sizeof(new_cfg->etscfg.tcbwtable)))
9448 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9449 
9450 		if (memcmp(&new_cfg->etscfg.tsatable,
9451 			   &old_cfg->etscfg.tsatable,
9452 			   sizeof(new_cfg->etscfg.tsatable)))
9453 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9454 	}
9455 
9456 	/* Check if PFC configuration has changed */
9457 	if (memcmp(&new_cfg->pfc,
9458 		   &old_cfg->pfc,
9459 		   sizeof(new_cfg->pfc))) {
9460 		need_reconfig = true;
9461 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9462 	}
9463 
9464 	/* Check if APP Table has changed */
9465 	if (memcmp(&new_cfg->app,
9466 		   &old_cfg->app,
9467 		   sizeof(new_cfg->app))) {
9468 		need_reconfig = true;
9469 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9470 	}
9471 
9472 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9473 	return need_reconfig;
9474 }
9475 
9476 /**
9477  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9478  * @pf: board private structure
9479  * @e: event info posted on ARQ
9480  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9481 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9482 				  struct i40e_arq_event_info *e)
9483 {
9484 	struct i40e_aqc_lldp_get_mib *mib =
9485 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9486 	struct i40e_hw *hw = &pf->hw;
9487 	struct i40e_dcbx_config tmp_dcbx_cfg;
9488 	bool need_reconfig = false;
9489 	int ret = 0;
9490 	u8 type;
9491 
9492 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9493 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9494 	    (hw->phy.link_info.link_speed &
9495 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9496 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9497 		/* let firmware decide if the DCB should be disabled */
9498 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9499 
9500 	/* Not DCB capable or capability disabled */
9501 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9502 		return ret;
9503 
9504 	/* Ignore if event is not for Nearest Bridge */
9505 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9506 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9507 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9508 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9509 		return ret;
9510 
9511 	/* Check MIB Type and return if event for Remote MIB update */
9512 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9513 	dev_dbg(&pf->pdev->dev,
9514 		"LLDP event mib type %s\n", type ? "remote" : "local");
9515 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9516 		/* Update the remote cached instance and return */
9517 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9518 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9519 				&hw->remote_dcbx_config);
9520 		goto exit;
9521 	}
9522 
9523 	/* Store the old configuration */
9524 	tmp_dcbx_cfg = hw->local_dcbx_config;
9525 
9526 	/* Reset the old DCBx configuration data */
9527 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9528 	/* Get updated DCBX data from firmware */
9529 	ret = i40e_get_dcb_config(&pf->hw);
9530 	if (ret) {
9531 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9532 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9533 		    (hw->phy.link_info.link_speed &
9534 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9535 			dev_warn(&pf->pdev->dev,
9536 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9537 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9538 		} else {
9539 			dev_info(&pf->pdev->dev,
9540 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9541 				 ERR_PTR(ret),
9542 				 i40e_aq_str(&pf->hw,
9543 					     pf->hw.aq.asq_last_status));
9544 		}
9545 		goto exit;
9546 	}
9547 
9548 	/* No change detected in DCBX configs */
9549 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9550 		    sizeof(tmp_dcbx_cfg))) {
9551 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9552 		goto exit;
9553 	}
9554 
9555 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9556 					       &hw->local_dcbx_config);
9557 
9558 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9559 
9560 	if (!need_reconfig)
9561 		goto exit;
9562 
9563 	/* Enable DCB tagging only when more than one TC */
9564 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9565 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9566 	else
9567 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9568 
9569 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9570 	/* Reconfiguration needed quiesce all VSIs */
9571 	i40e_pf_quiesce_all_vsi(pf);
9572 
9573 	/* Changes in configuration update VEB/VSI */
9574 	i40e_dcb_reconfigure(pf);
9575 
9576 	ret = i40e_resume_port_tx(pf);
9577 
9578 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9579 	/* In case of error no point in resuming VSIs */
9580 	if (ret)
9581 		goto exit;
9582 
9583 	/* Wait for the PF's queues to be disabled */
9584 	ret = i40e_pf_wait_queues_disabled(pf);
9585 	if (ret) {
9586 		/* Schedule PF reset to recover */
9587 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9588 		i40e_service_event_schedule(pf);
9589 	} else {
9590 		i40e_pf_unquiesce_all_vsi(pf);
9591 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9592 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9593 	}
9594 
9595 exit:
9596 	return ret;
9597 }
9598 #endif /* CONFIG_I40E_DCB */
9599 
9600 /**
9601  * i40e_do_reset_safe - Protected reset path for userland calls.
9602  * @pf: board private structure
9603  * @reset_flags: which reset is requested
9604  *
9605  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9606 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9607 {
9608 	rtnl_lock();
9609 	i40e_do_reset(pf, reset_flags, true);
9610 	rtnl_unlock();
9611 }
9612 
9613 /**
9614  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9615  * @pf: board private structure
9616  * @e: event info posted on ARQ
9617  *
9618  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9619  * and VF queues
9620  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9621 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9622 					   struct i40e_arq_event_info *e)
9623 {
9624 	struct i40e_aqc_lan_overflow *data =
9625 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9626 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9627 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9628 	struct i40e_hw *hw = &pf->hw;
9629 	struct i40e_vf *vf;
9630 	u16 vf_id;
9631 
9632 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9633 		queue, qtx_ctl);
9634 
9635 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9636 	    I40E_QTX_CTL_VF_QUEUE)
9637 		return;
9638 
9639 	/* Queue belongs to VF, find the VF and issue VF reset */
9640 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9641 	vf_id -= hw->func_caps.vf_base_id;
9642 	vf = &pf->vf[vf_id];
9643 	i40e_vc_notify_vf_reset(vf);
9644 	/* Allow VF to process pending reset notification */
9645 	msleep(20);
9646 	i40e_reset_vf(vf, false);
9647 }
9648 
9649 /**
9650  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9651  * @pf: board private structure
9652  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9653 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9654 {
9655 	u32 val, fcnt_prog;
9656 
9657 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9658 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9659 	return fcnt_prog;
9660 }
9661 
9662 /**
9663  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9664  * @pf: board private structure
9665  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9666 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9667 {
9668 	u32 val, fcnt_prog;
9669 
9670 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9671 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9672 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9673 	return fcnt_prog;
9674 }
9675 
9676 /**
9677  * i40e_get_global_fd_count - Get total FD filters programmed on device
9678  * @pf: board private structure
9679  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9680 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9681 {
9682 	u32 val, fcnt_prog;
9683 
9684 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9685 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9686 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9687 	return fcnt_prog;
9688 }
9689 
9690 /**
9691  * i40e_reenable_fdir_sb - Restore FDir SB capability
9692  * @pf: board private structure
9693  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9694 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9695 {
9696 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9697 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9698 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9699 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9700 }
9701 
9702 /**
9703  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9704  * @pf: board private structure
9705  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9706 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9707 {
9708 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9709 		/* ATR uses the same filtering logic as SB rules. It only
9710 		 * functions properly if the input set mask is at the default
9711 		 * settings. It is safe to restore the default input set
9712 		 * because there are no active TCPv4 filter rules.
9713 		 */
9714 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9715 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9716 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9717 
9718 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9719 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9720 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9721 	}
9722 }
9723 
9724 /**
9725  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9726  * @pf: board private structure
9727  * @filter: FDir filter to remove
9728  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9729 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9730 				       struct i40e_fdir_filter *filter)
9731 {
9732 	/* Update counters */
9733 	pf->fdir_pf_active_filters--;
9734 	pf->fd_inv = 0;
9735 
9736 	switch (filter->flow_type) {
9737 	case TCP_V4_FLOW:
9738 		pf->fd_tcp4_filter_cnt--;
9739 		break;
9740 	case UDP_V4_FLOW:
9741 		pf->fd_udp4_filter_cnt--;
9742 		break;
9743 	case SCTP_V4_FLOW:
9744 		pf->fd_sctp4_filter_cnt--;
9745 		break;
9746 	case TCP_V6_FLOW:
9747 		pf->fd_tcp6_filter_cnt--;
9748 		break;
9749 	case UDP_V6_FLOW:
9750 		pf->fd_udp6_filter_cnt--;
9751 		break;
9752 	case SCTP_V6_FLOW:
9753 		pf->fd_udp6_filter_cnt--;
9754 		break;
9755 	case IP_USER_FLOW:
9756 		switch (filter->ipl4_proto) {
9757 		case IPPROTO_TCP:
9758 			pf->fd_tcp4_filter_cnt--;
9759 			break;
9760 		case IPPROTO_UDP:
9761 			pf->fd_udp4_filter_cnt--;
9762 			break;
9763 		case IPPROTO_SCTP:
9764 			pf->fd_sctp4_filter_cnt--;
9765 			break;
9766 		case IPPROTO_IP:
9767 			pf->fd_ip4_filter_cnt--;
9768 			break;
9769 		}
9770 		break;
9771 	case IPV6_USER_FLOW:
9772 		switch (filter->ipl4_proto) {
9773 		case IPPROTO_TCP:
9774 			pf->fd_tcp6_filter_cnt--;
9775 			break;
9776 		case IPPROTO_UDP:
9777 			pf->fd_udp6_filter_cnt--;
9778 			break;
9779 		case IPPROTO_SCTP:
9780 			pf->fd_sctp6_filter_cnt--;
9781 			break;
9782 		case IPPROTO_IP:
9783 			pf->fd_ip6_filter_cnt--;
9784 			break;
9785 		}
9786 		break;
9787 	}
9788 
9789 	/* Remove the filter from the list and free memory */
9790 	hlist_del(&filter->fdir_node);
9791 	kfree(filter);
9792 }
9793 
9794 /**
9795  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9796  * @pf: board private structure
9797  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9798 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9799 {
9800 	struct i40e_fdir_filter *filter;
9801 	u32 fcnt_prog, fcnt_avail;
9802 	struct hlist_node *node;
9803 
9804 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9805 		return;
9806 
9807 	/* Check if we have enough room to re-enable FDir SB capability. */
9808 	fcnt_prog = i40e_get_global_fd_count(pf);
9809 	fcnt_avail = pf->fdir_pf_filter_count;
9810 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9811 	    (pf->fd_add_err == 0) ||
9812 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9813 		i40e_reenable_fdir_sb(pf);
9814 
9815 	/* We should wait for even more space before re-enabling ATR.
9816 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9817 	 * rules active.
9818 	 */
9819 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9820 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9821 		i40e_reenable_fdir_atr(pf);
9822 
9823 	/* if hw had a problem adding a filter, delete it */
9824 	if (pf->fd_inv > 0) {
9825 		hlist_for_each_entry_safe(filter, node,
9826 					  &pf->fdir_filter_list, fdir_node)
9827 			if (filter->fd_id == pf->fd_inv)
9828 				i40e_delete_invalid_filter(pf, filter);
9829 	}
9830 }
9831 
9832 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9833 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9834 /**
9835  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9836  * @pf: board private structure
9837  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9838 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9839 {
9840 	unsigned long min_flush_time;
9841 	int flush_wait_retry = 50;
9842 	bool disable_atr = false;
9843 	int fd_room;
9844 	int reg;
9845 
9846 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9847 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9848 		return;
9849 
9850 	/* If the flush is happening too quick and we have mostly SB rules we
9851 	 * should not re-enable ATR for some time.
9852 	 */
9853 	min_flush_time = pf->fd_flush_timestamp +
9854 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9855 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9856 
9857 	if (!(time_after(jiffies, min_flush_time)) &&
9858 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9859 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9860 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9861 		disable_atr = true;
9862 	}
9863 
9864 	pf->fd_flush_timestamp = jiffies;
9865 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9866 	/* flush all filters */
9867 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9868 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9869 	i40e_flush(&pf->hw);
9870 	pf->fd_flush_cnt++;
9871 	pf->fd_add_err = 0;
9872 	do {
9873 		/* Check FD flush status every 5-6msec */
9874 		usleep_range(5000, 6000);
9875 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9876 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9877 			break;
9878 	} while (flush_wait_retry--);
9879 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9880 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9881 	} else {
9882 		/* replay sideband filters */
9883 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9884 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9885 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9886 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9887 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9888 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9889 	}
9890 }
9891 
9892 /**
9893  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9894  * @pf: board private structure
9895  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9896 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9897 {
9898 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9899 }
9900 
9901 /**
9902  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9903  * @pf: board private structure
9904  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9905 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9906 {
9907 
9908 	/* if interface is down do nothing */
9909 	if (test_bit(__I40E_DOWN, pf->state))
9910 		return;
9911 
9912 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9913 		i40e_fdir_flush_and_replay(pf);
9914 
9915 	i40e_fdir_check_and_reenable(pf);
9916 
9917 }
9918 
9919 /**
9920  * i40e_vsi_link_event - notify VSI of a link event
9921  * @vsi: vsi to be notified
9922  * @link_up: link up or down
9923  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9924 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9925 {
9926 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9927 		return;
9928 
9929 	switch (vsi->type) {
9930 	case I40E_VSI_MAIN:
9931 		if (!vsi->netdev || !vsi->netdev_registered)
9932 			break;
9933 
9934 		if (link_up) {
9935 			netif_carrier_on(vsi->netdev);
9936 			netif_tx_wake_all_queues(vsi->netdev);
9937 		} else {
9938 			netif_carrier_off(vsi->netdev);
9939 			netif_tx_stop_all_queues(vsi->netdev);
9940 		}
9941 		break;
9942 
9943 	case I40E_VSI_SRIOV:
9944 	case I40E_VSI_VMDQ2:
9945 	case I40E_VSI_CTRL:
9946 	case I40E_VSI_IWARP:
9947 	case I40E_VSI_MIRROR:
9948 	default:
9949 		/* there is no notification for other VSIs */
9950 		break;
9951 	}
9952 }
9953 
9954 /**
9955  * i40e_veb_link_event - notify elements on the veb of a link event
9956  * @veb: veb to be notified
9957  * @link_up: link up or down
9958  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9959 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9960 {
9961 	struct i40e_vsi *vsi;
9962 	struct i40e_pf *pf;
9963 	int i;
9964 
9965 	if (!veb || !veb->pf)
9966 		return;
9967 	pf = veb->pf;
9968 
9969 	/* Send link event to contained VSIs */
9970 	i40e_pf_for_each_vsi(pf, i, vsi)
9971 		if (vsi->uplink_seid == veb->seid)
9972 			i40e_vsi_link_event(vsi, link_up);
9973 }
9974 
9975 /**
9976  * i40e_link_event - Update netif_carrier status
9977  * @pf: board private structure
9978  **/
i40e_link_event(struct i40e_pf * pf)9979 static void i40e_link_event(struct i40e_pf *pf)
9980 {
9981 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9982 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9983 	u8 new_link_speed, old_link_speed;
9984 	bool new_link, old_link;
9985 	int status;
9986 #ifdef CONFIG_I40E_DCB
9987 	int err;
9988 #endif /* CONFIG_I40E_DCB */
9989 
9990 	/* set this to force the get_link_status call to refresh state */
9991 	pf->hw.phy.get_link_info = true;
9992 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9993 	status = i40e_get_link_status(&pf->hw, &new_link);
9994 
9995 	/* On success, disable temp link polling */
9996 	if (status == 0) {
9997 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9998 	} else {
9999 		/* Enable link polling temporarily until i40e_get_link_status
10000 		 * returns 0
10001 		 */
10002 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
10003 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
10004 			status);
10005 		return;
10006 	}
10007 
10008 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
10009 	new_link_speed = pf->hw.phy.link_info.link_speed;
10010 
10011 	if (new_link == old_link &&
10012 	    new_link_speed == old_link_speed &&
10013 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
10014 	     new_link == netif_carrier_ok(vsi->netdev)))
10015 		return;
10016 
10017 	i40e_print_link_message(vsi, new_link);
10018 
10019 	/* Notify the base of the switch tree connected to
10020 	 * the link.  Floating VEBs are not notified.
10021 	 */
10022 	if (veb)
10023 		i40e_veb_link_event(veb, new_link);
10024 	else
10025 		i40e_vsi_link_event(vsi, new_link);
10026 
10027 	if (pf->vf)
10028 		i40e_vc_notify_link_state(pf);
10029 
10030 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
10031 		i40e_ptp_set_increment(pf);
10032 #ifdef CONFIG_I40E_DCB
10033 	if (new_link == old_link)
10034 		return;
10035 	/* Not SW DCB so firmware will take care of default settings */
10036 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
10037 		return;
10038 
10039 	/* We cover here only link down, as after link up in case of SW DCB
10040 	 * SW LLDP agent will take care of setting it up
10041 	 */
10042 	if (!new_link) {
10043 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
10044 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
10045 		err = i40e_dcb_sw_default_config(pf);
10046 		if (err) {
10047 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10048 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
10049 		} else {
10050 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
10051 				       DCB_CAP_DCBX_VER_IEEE;
10052 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10053 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
10054 		}
10055 	}
10056 #endif /* CONFIG_I40E_DCB */
10057 }
10058 
10059 /**
10060  * i40e_watchdog_subtask - periodic checks not using event driven response
10061  * @pf: board private structure
10062  **/
i40e_watchdog_subtask(struct i40e_pf * pf)10063 static void i40e_watchdog_subtask(struct i40e_pf *pf)
10064 {
10065 	struct i40e_vsi *vsi;
10066 	struct i40e_veb *veb;
10067 	int i;
10068 
10069 	/* if interface is down do nothing */
10070 	if (test_bit(__I40E_DOWN, pf->state) ||
10071 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10072 		return;
10073 
10074 	/* make sure we don't do these things too often */
10075 	if (time_before(jiffies, (pf->service_timer_previous +
10076 				  pf->service_timer_period)))
10077 		return;
10078 	pf->service_timer_previous = jiffies;
10079 
10080 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10081 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10082 		i40e_link_event(pf);
10083 
10084 	/* Update the stats for active netdevs so the network stack
10085 	 * can look at updated numbers whenever it cares to
10086 	 */
10087 	i40e_pf_for_each_vsi(pf, i, vsi)
10088 		if (vsi->netdev)
10089 			i40e_update_stats(vsi);
10090 
10091 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10092 		/* Update the stats for the active switching components */
10093 		i40e_pf_for_each_veb(pf, i, veb)
10094 			i40e_update_veb_stats(veb);
10095 	}
10096 
10097 	i40e_ptp_rx_hang(pf);
10098 	i40e_ptp_tx_hang(pf);
10099 }
10100 
10101 /**
10102  * i40e_reset_subtask - Set up for resetting the device and driver
10103  * @pf: board private structure
10104  **/
i40e_reset_subtask(struct i40e_pf * pf)10105 static void i40e_reset_subtask(struct i40e_pf *pf)
10106 {
10107 	u32 reset_flags = 0;
10108 
10109 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10110 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10111 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10112 	}
10113 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10114 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10115 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10116 	}
10117 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10118 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10119 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10120 	}
10121 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10122 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10123 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10124 	}
10125 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10126 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10127 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10128 	}
10129 
10130 	/* If there's a recovery already waiting, it takes
10131 	 * precedence before starting a new reset sequence.
10132 	 */
10133 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10134 		i40e_prep_for_reset(pf);
10135 		i40e_reset(pf);
10136 		i40e_rebuild(pf, false, false);
10137 	}
10138 
10139 	/* If we're already down or resetting, just bail */
10140 	if (reset_flags &&
10141 	    !test_bit(__I40E_DOWN, pf->state) &&
10142 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10143 		i40e_do_reset(pf, reset_flags, false);
10144 	}
10145 }
10146 
10147 /**
10148  * i40e_handle_link_event - Handle link event
10149  * @pf: board private structure
10150  * @e: event info posted on ARQ
10151  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10152 static void i40e_handle_link_event(struct i40e_pf *pf,
10153 				   struct i40e_arq_event_info *e)
10154 {
10155 	struct i40e_aqc_get_link_status *status =
10156 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10157 
10158 	/* Do a new status request to re-enable LSE reporting
10159 	 * and load new status information into the hw struct
10160 	 * This completely ignores any state information
10161 	 * in the ARQ event info, instead choosing to always
10162 	 * issue the AQ update link status command.
10163 	 */
10164 	i40e_link_event(pf);
10165 
10166 	/* Check if module meets thermal requirements */
10167 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10168 		dev_err(&pf->pdev->dev,
10169 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10170 		dev_err(&pf->pdev->dev,
10171 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10172 	} else {
10173 		/* check for unqualified module, if link is down, suppress
10174 		 * the message if link was forced to be down.
10175 		 */
10176 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10177 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10178 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10179 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10180 			dev_err(&pf->pdev->dev,
10181 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10182 			dev_err(&pf->pdev->dev,
10183 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10184 		}
10185 	}
10186 }
10187 
10188 /**
10189  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10190  * @pf: board private structure
10191  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10192 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10193 {
10194 	struct i40e_arq_event_info event;
10195 	struct i40e_hw *hw = &pf->hw;
10196 	u16 pending, i = 0;
10197 	u16 opcode;
10198 	u32 oldval;
10199 	int ret;
10200 	u32 val;
10201 
10202 	/* Do not run clean AQ when PF reset fails */
10203 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10204 		return;
10205 
10206 	/* check for error indications */
10207 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10208 	oldval = val;
10209 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10210 		if (hw->debug_mask & I40E_DEBUG_AQ)
10211 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10212 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10213 	}
10214 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10215 		if (hw->debug_mask & I40E_DEBUG_AQ)
10216 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10217 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10218 		pf->arq_overflows++;
10219 	}
10220 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10221 		if (hw->debug_mask & I40E_DEBUG_AQ)
10222 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10223 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10224 	}
10225 	if (oldval != val)
10226 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10227 
10228 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10229 	oldval = val;
10230 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10231 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10232 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10233 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10234 	}
10235 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10236 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10237 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10238 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10239 	}
10240 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10241 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10242 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10243 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10244 	}
10245 	if (oldval != val)
10246 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10247 
10248 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10249 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10250 	if (!event.msg_buf)
10251 		return;
10252 
10253 	do {
10254 		ret = i40e_clean_arq_element(hw, &event, &pending);
10255 		if (ret == -EALREADY)
10256 			break;
10257 		else if (ret) {
10258 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10259 			break;
10260 		}
10261 
10262 		opcode = le16_to_cpu(event.desc.opcode);
10263 		switch (opcode) {
10264 
10265 		case i40e_aqc_opc_get_link_status:
10266 			rtnl_lock();
10267 			i40e_handle_link_event(pf, &event);
10268 			rtnl_unlock();
10269 			break;
10270 		case i40e_aqc_opc_send_msg_to_pf:
10271 			ret = i40e_vc_process_vf_msg(pf,
10272 					le16_to_cpu(event.desc.retval),
10273 					le32_to_cpu(event.desc.cookie_high),
10274 					le32_to_cpu(event.desc.cookie_low),
10275 					event.msg_buf,
10276 					event.msg_len);
10277 			break;
10278 		case i40e_aqc_opc_lldp_update_mib:
10279 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10280 #ifdef CONFIG_I40E_DCB
10281 			rtnl_lock();
10282 			i40e_handle_lldp_event(pf, &event);
10283 			rtnl_unlock();
10284 #endif /* CONFIG_I40E_DCB */
10285 			break;
10286 		case i40e_aqc_opc_event_lan_overflow:
10287 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10288 			i40e_handle_lan_overflow_event(pf, &event);
10289 			break;
10290 		case i40e_aqc_opc_send_msg_to_peer:
10291 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10292 			break;
10293 		case i40e_aqc_opc_nvm_erase:
10294 		case i40e_aqc_opc_nvm_update:
10295 		case i40e_aqc_opc_oem_post_update:
10296 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10297 				   "ARQ NVM operation 0x%04x completed\n",
10298 				   opcode);
10299 			break;
10300 		default:
10301 			dev_info(&pf->pdev->dev,
10302 				 "ARQ: Unknown event 0x%04x ignored\n",
10303 				 opcode);
10304 			break;
10305 		}
10306 	} while (i++ < I40E_AQ_WORK_LIMIT);
10307 
10308 	if (i < I40E_AQ_WORK_LIMIT)
10309 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10310 
10311 	/* re-enable Admin queue interrupt cause */
10312 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10313 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10314 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10315 	i40e_flush(hw);
10316 
10317 	kfree(event.msg_buf);
10318 }
10319 
10320 /**
10321  * i40e_verify_eeprom - make sure eeprom is good to use
10322  * @pf: board private structure
10323  **/
i40e_verify_eeprom(struct i40e_pf * pf)10324 static void i40e_verify_eeprom(struct i40e_pf *pf)
10325 {
10326 	int err;
10327 
10328 	err = i40e_diag_eeprom_test(&pf->hw);
10329 	if (err) {
10330 		/* retry in case of garbage read */
10331 		err = i40e_diag_eeprom_test(&pf->hw);
10332 		if (err) {
10333 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10334 				 err);
10335 			set_bit(__I40E_BAD_EEPROM, pf->state);
10336 		}
10337 	}
10338 
10339 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10340 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10341 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10342 	}
10343 }
10344 
10345 /**
10346  * i40e_enable_pf_switch_lb
10347  * @pf: pointer to the PF structure
10348  *
10349  * enable switch loop back or die - no point in a return value
10350  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10351 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10352 {
10353 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10354 	struct i40e_vsi_context ctxt;
10355 	int ret;
10356 
10357 	ctxt.seid = pf->main_vsi_seid;
10358 	ctxt.pf_num = pf->hw.pf_id;
10359 	ctxt.vf_num = 0;
10360 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10361 	if (ret) {
10362 		dev_info(&pf->pdev->dev,
10363 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10364 			 ERR_PTR(ret),
10365 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10366 		return;
10367 	}
10368 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10369 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10370 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10371 
10372 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10373 	if (ret) {
10374 		dev_info(&pf->pdev->dev,
10375 			 "update vsi switch failed, err %pe aq_err %s\n",
10376 			 ERR_PTR(ret),
10377 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10378 	}
10379 }
10380 
10381 /**
10382  * i40e_disable_pf_switch_lb
10383  * @pf: pointer to the PF structure
10384  *
10385  * disable switch loop back or die - no point in a return value
10386  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10387 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10388 {
10389 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10390 	struct i40e_vsi_context ctxt;
10391 	int ret;
10392 
10393 	ctxt.seid = pf->main_vsi_seid;
10394 	ctxt.pf_num = pf->hw.pf_id;
10395 	ctxt.vf_num = 0;
10396 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10397 	if (ret) {
10398 		dev_info(&pf->pdev->dev,
10399 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10400 			 ERR_PTR(ret),
10401 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10402 		return;
10403 	}
10404 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10405 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10406 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10407 
10408 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10409 	if (ret) {
10410 		dev_info(&pf->pdev->dev,
10411 			 "update vsi switch failed, err %pe aq_err %s\n",
10412 			 ERR_PTR(ret),
10413 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10414 	}
10415 }
10416 
10417 /**
10418  * i40e_config_bridge_mode - Configure the HW bridge mode
10419  * @veb: pointer to the bridge instance
10420  *
10421  * Configure the loop back mode for the LAN VSI that is downlink to the
10422  * specified HW bridge instance. It is expected this function is called
10423  * when a new HW bridge is instantiated.
10424  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10425 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10426 {
10427 	struct i40e_pf *pf = veb->pf;
10428 
10429 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10430 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10431 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10432 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10433 		i40e_disable_pf_switch_lb(pf);
10434 	else
10435 		i40e_enable_pf_switch_lb(pf);
10436 }
10437 
10438 /**
10439  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10440  * @veb: pointer to the VEB instance
10441  *
10442  * This is a function that builds the attached VSIs. We track the connections
10443  * through our own index numbers because the seid's from the HW could change
10444  * across the reset.
10445  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10446 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10447 {
10448 	struct i40e_vsi *ctl_vsi = NULL;
10449 	struct i40e_pf *pf = veb->pf;
10450 	struct i40e_vsi *vsi;
10451 	int v, ret;
10452 
10453 	/* As we do not maintain PV (port virtualizer) switch element then
10454 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10455 	 * and its control VSI is the main one.
10456 	 */
10457 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10458 		dev_err(&pf->pdev->dev,
10459 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10460 		return -ENOENT;
10461 	}
10462 
10463 	if (veb->uplink_seid == pf->mac_seid) {
10464 		/* Check that the LAN VSI has VEB owning flag set */
10465 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10466 
10467 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10468 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10469 			dev_err(&pf->pdev->dev,
10470 				"Invalid control VSI for VEB %d\n", veb->idx);
10471 			return -ENOENT;
10472 		}
10473 
10474 		/* Add the control VSI to switch */
10475 		ret = i40e_add_vsi(ctl_vsi);
10476 		if (ret) {
10477 			dev_err(&pf->pdev->dev,
10478 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10479 				veb->idx, ret);
10480 			return ret;
10481 		}
10482 
10483 		i40e_vsi_reset_stats(ctl_vsi);
10484 	}
10485 
10486 	/* create the VEB in the switch and move the VSI onto the VEB */
10487 	ret = i40e_add_veb(veb, ctl_vsi);
10488 	if (ret)
10489 		return ret;
10490 
10491 	if (veb->uplink_seid) {
10492 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10493 			veb->bridge_mode = BRIDGE_MODE_VEB;
10494 		else
10495 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10496 		i40e_config_bridge_mode(veb);
10497 	}
10498 
10499 	/* create the remaining VSIs attached to this VEB */
10500 	i40e_pf_for_each_vsi(pf, v, vsi) {
10501 		if (vsi == ctl_vsi)
10502 			continue;
10503 
10504 		if (vsi->veb_idx == veb->idx) {
10505 			vsi->uplink_seid = veb->seid;
10506 			ret = i40e_add_vsi(vsi);
10507 			if (ret) {
10508 				dev_info(&pf->pdev->dev,
10509 					 "rebuild of vsi_idx %d failed: %d\n",
10510 					 v, ret);
10511 				return ret;
10512 			}
10513 			i40e_vsi_reset_stats(vsi);
10514 		}
10515 	}
10516 
10517 	return ret;
10518 }
10519 
10520 /**
10521  * i40e_get_capabilities - get info about the HW
10522  * @pf: the PF struct
10523  * @list_type: AQ capability to be queried
10524  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10525 static int i40e_get_capabilities(struct i40e_pf *pf,
10526 				 enum i40e_admin_queue_opc list_type)
10527 {
10528 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10529 	u16 data_size;
10530 	int buf_len;
10531 	int err;
10532 
10533 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10534 	do {
10535 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10536 		if (!cap_buf)
10537 			return -ENOMEM;
10538 
10539 		/* this loads the data into the hw struct for us */
10540 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10541 						    &data_size, list_type,
10542 						    NULL);
10543 		/* data loaded, buffer no longer needed */
10544 		kfree(cap_buf);
10545 
10546 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10547 			/* retry with a larger buffer */
10548 			buf_len = data_size;
10549 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10550 			dev_info(&pf->pdev->dev,
10551 				 "capability discovery failed, err %pe aq_err %s\n",
10552 				 ERR_PTR(err),
10553 				 i40e_aq_str(&pf->hw,
10554 					     pf->hw.aq.asq_last_status));
10555 			return -ENODEV;
10556 		}
10557 	} while (err);
10558 
10559 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10560 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10561 			dev_info(&pf->pdev->dev,
10562 				 "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",
10563 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10564 				 pf->hw.func_caps.num_msix_vectors,
10565 				 pf->hw.func_caps.num_msix_vectors_vf,
10566 				 pf->hw.func_caps.fd_filters_guaranteed,
10567 				 pf->hw.func_caps.fd_filters_best_effort,
10568 				 pf->hw.func_caps.num_tx_qp,
10569 				 pf->hw.func_caps.num_vsis);
10570 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10571 			dev_info(&pf->pdev->dev,
10572 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10573 				 pf->hw.dev_caps.switch_mode,
10574 				 pf->hw.dev_caps.valid_functions);
10575 			dev_info(&pf->pdev->dev,
10576 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10577 				 pf->hw.dev_caps.sr_iov_1_1,
10578 				 pf->hw.dev_caps.num_vfs);
10579 			dev_info(&pf->pdev->dev,
10580 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10581 				 pf->hw.dev_caps.num_vsis,
10582 				 pf->hw.dev_caps.num_rx_qp,
10583 				 pf->hw.dev_caps.num_tx_qp);
10584 		}
10585 	}
10586 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10587 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10588 		       + pf->hw.func_caps.num_vfs)
10589 		if (pf->hw.revision_id == 0 &&
10590 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10591 			dev_info(&pf->pdev->dev,
10592 				 "got num_vsis %d, setting num_vsis to %d\n",
10593 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10594 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10595 		}
10596 	}
10597 	return 0;
10598 }
10599 
10600 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10601 
10602 /**
10603  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10604  * @pf: board private structure
10605  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10606 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10607 {
10608 	struct i40e_vsi *main_vsi, *vsi;
10609 
10610 	/* quick workaround for an NVM issue that leaves a critical register
10611 	 * uninitialized
10612 	 */
10613 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10614 		static const u32 hkey[] = {
10615 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10616 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10617 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10618 			0x95b3a76d};
10619 		int i;
10620 
10621 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10622 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10623 	}
10624 
10625 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10626 		return;
10627 
10628 	/* find existing VSI and see if it needs configuring */
10629 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10630 
10631 	/* create a new VSI if none exists */
10632 	if (!vsi) {
10633 		main_vsi = i40e_pf_get_main_vsi(pf);
10634 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10635 		if (!vsi) {
10636 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10637 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10638 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10639 			return;
10640 		}
10641 	}
10642 
10643 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10644 }
10645 
10646 /**
10647  * i40e_fdir_teardown - release the Flow Director resources
10648  * @pf: board private structure
10649  **/
i40e_fdir_teardown(struct i40e_pf * pf)10650 static void i40e_fdir_teardown(struct i40e_pf *pf)
10651 {
10652 	struct i40e_vsi *vsi;
10653 
10654 	i40e_fdir_filter_exit(pf);
10655 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10656 	if (vsi)
10657 		i40e_vsi_release(vsi);
10658 }
10659 
10660 /**
10661  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10662  * @vsi: PF main vsi
10663  * @seid: seid of main or channel VSIs
10664  *
10665  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10666  * existed before reset
10667  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10668 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10669 {
10670 	struct i40e_cloud_filter *cfilter;
10671 	struct i40e_pf *pf = vsi->back;
10672 	struct hlist_node *node;
10673 	int ret;
10674 
10675 	/* Add cloud filters back if they exist */
10676 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10677 				  cloud_node) {
10678 		if (cfilter->seid != seid)
10679 			continue;
10680 
10681 		if (cfilter->dst_port)
10682 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10683 								true);
10684 		else
10685 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10686 
10687 		if (ret) {
10688 			dev_dbg(&pf->pdev->dev,
10689 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10690 				ERR_PTR(ret),
10691 				i40e_aq_str(&pf->hw,
10692 					    pf->hw.aq.asq_last_status));
10693 			return ret;
10694 		}
10695 	}
10696 	return 0;
10697 }
10698 
10699 /**
10700  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10701  * @vsi: PF main vsi
10702  *
10703  * Rebuilds channel VSIs if they existed before reset
10704  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10705 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10706 {
10707 	struct i40e_channel *ch, *ch_tmp;
10708 	int ret;
10709 
10710 	if (list_empty(&vsi->ch_list))
10711 		return 0;
10712 
10713 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10714 		if (!ch->initialized)
10715 			break;
10716 		/* Proceed with creation of channel (VMDq2) VSI */
10717 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10718 		if (ret) {
10719 			dev_info(&vsi->back->pdev->dev,
10720 				 "failed to rebuild channels using uplink_seid %u\n",
10721 				 vsi->uplink_seid);
10722 			return ret;
10723 		}
10724 		/* Reconfigure TX queues using QTX_CTL register */
10725 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10726 		if (ret) {
10727 			dev_info(&vsi->back->pdev->dev,
10728 				 "failed to configure TX rings for channel %u\n",
10729 				 ch->seid);
10730 			return ret;
10731 		}
10732 		/* update 'next_base_queue' */
10733 		vsi->next_base_queue = vsi->next_base_queue +
10734 							ch->num_queue_pairs;
10735 		if (ch->max_tx_rate) {
10736 			u64 credits = ch->max_tx_rate;
10737 
10738 			if (i40e_set_bw_limit(vsi, ch->seid,
10739 					      ch->max_tx_rate))
10740 				return -EINVAL;
10741 
10742 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10743 			dev_dbg(&vsi->back->pdev->dev,
10744 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10745 				ch->max_tx_rate,
10746 				credits,
10747 				ch->seid);
10748 		}
10749 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10750 		if (ret) {
10751 			dev_dbg(&vsi->back->pdev->dev,
10752 				"Failed to rebuild cloud filters for channel VSI %u\n",
10753 				ch->seid);
10754 			return ret;
10755 		}
10756 	}
10757 	return 0;
10758 }
10759 
10760 /**
10761  * i40e_clean_xps_state - clean xps state for every tx_ring
10762  * @vsi: ptr to the VSI
10763  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10764 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10765 {
10766 	int i;
10767 
10768 	if (vsi->tx_rings)
10769 		for (i = 0; i < vsi->num_queue_pairs; i++)
10770 			if (vsi->tx_rings[i])
10771 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10772 					  vsi->tx_rings[i]->state);
10773 }
10774 
10775 /**
10776  * i40e_prep_for_reset - prep for the core to reset
10777  * @pf: board private structure
10778  *
10779  * Close up the VFs and other things in prep for PF Reset.
10780   **/
i40e_prep_for_reset(struct i40e_pf * pf)10781 static void i40e_prep_for_reset(struct i40e_pf *pf)
10782 {
10783 	struct i40e_hw *hw = &pf->hw;
10784 	struct i40e_vsi *vsi;
10785 	int ret = 0;
10786 	u32 v;
10787 
10788 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10789 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10790 		return;
10791 	if (i40e_check_asq_alive(&pf->hw))
10792 		i40e_vc_notify_reset(pf);
10793 
10794 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10795 
10796 	/* quiesce the VSIs and their queues that are not already DOWN */
10797 	i40e_pf_quiesce_all_vsi(pf);
10798 
10799 	i40e_pf_for_each_vsi(pf, v, vsi) {
10800 		i40e_clean_xps_state(vsi);
10801 		vsi->seid = 0;
10802 	}
10803 
10804 	i40e_shutdown_adminq(&pf->hw);
10805 
10806 	/* call shutdown HMC */
10807 	if (hw->hmc.hmc_obj) {
10808 		ret = i40e_shutdown_lan_hmc(hw);
10809 		if (ret)
10810 			dev_warn(&pf->pdev->dev,
10811 				 "shutdown_lan_hmc failed: %d\n", ret);
10812 	}
10813 
10814 	/* Save the current PTP time so that we can restore the time after the
10815 	 * reset completes.
10816 	 */
10817 	i40e_ptp_save_hw_time(pf);
10818 }
10819 
10820 /**
10821  * i40e_send_version - update firmware with driver version
10822  * @pf: PF struct
10823  */
i40e_send_version(struct i40e_pf * pf)10824 static void i40e_send_version(struct i40e_pf *pf)
10825 {
10826 	struct i40e_driver_version dv;
10827 
10828 	dv.major_version = 0xff;
10829 	dv.minor_version = 0xff;
10830 	dv.build_version = 0xff;
10831 	dv.subbuild_version = 0;
10832 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10833 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10834 }
10835 
10836 /**
10837  * i40e_get_oem_version - get OEM specific version information
10838  * @hw: pointer to the hardware structure
10839  **/
i40e_get_oem_version(struct i40e_hw * hw)10840 static void i40e_get_oem_version(struct i40e_hw *hw)
10841 {
10842 	u16 block_offset = 0xffff;
10843 	u16 block_length = 0;
10844 	u16 capabilities = 0;
10845 	u16 gen_snap = 0;
10846 	u16 release = 0;
10847 
10848 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10849 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10850 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10851 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10852 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10853 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10854 #define I40E_NVM_OEM_LENGTH			3
10855 
10856 	/* Check if pointer to OEM version block is valid. */
10857 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10858 	if (block_offset == 0xffff)
10859 		return;
10860 
10861 	/* Check if OEM version block has correct length. */
10862 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10863 			   &block_length);
10864 	if (block_length < I40E_NVM_OEM_LENGTH)
10865 		return;
10866 
10867 	/* Check if OEM version format is as expected. */
10868 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10869 			   &capabilities);
10870 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10871 		return;
10872 
10873 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10874 			   &gen_snap);
10875 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10876 			   &release);
10877 	hw->nvm.oem_ver =
10878 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10879 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10880 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10881 }
10882 
10883 /**
10884  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10885  * @pf: board private structure
10886  **/
i40e_reset(struct i40e_pf * pf)10887 static int i40e_reset(struct i40e_pf *pf)
10888 {
10889 	struct i40e_hw *hw = &pf->hw;
10890 	int ret;
10891 
10892 	ret = i40e_pf_reset(hw);
10893 	if (ret) {
10894 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10895 		set_bit(__I40E_RESET_FAILED, pf->state);
10896 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10897 	} else {
10898 		pf->pfr_count++;
10899 	}
10900 	return ret;
10901 }
10902 
10903 /**
10904  * i40e_rebuild - rebuild using a saved config
10905  * @pf: board private structure
10906  * @reinit: if the Main VSI needs to re-initialized.
10907  * @lock_acquired: indicates whether or not the lock has been acquired
10908  * before this function was called.
10909  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10910 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10911 {
10912 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10913 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10914 	struct i40e_hw *hw = &pf->hw;
10915 	struct i40e_veb *veb;
10916 	int ret;
10917 	u32 val;
10918 	int v;
10919 
10920 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10921 	    is_recovery_mode_reported)
10922 		i40e_set_ethtool_ops(vsi->netdev);
10923 
10924 	if (test_bit(__I40E_DOWN, pf->state) &&
10925 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10926 		goto clear_recovery;
10927 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10928 
10929 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10930 	ret = i40e_init_adminq(&pf->hw);
10931 	if (ret) {
10932 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10933 			 ERR_PTR(ret),
10934 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10935 		goto clear_recovery;
10936 	}
10937 	i40e_get_oem_version(&pf->hw);
10938 
10939 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10940 		/* The following delay is necessary for firmware update. */
10941 		mdelay(1000);
10942 	}
10943 
10944 	/* re-verify the eeprom if we just had an EMP reset */
10945 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10946 		i40e_verify_eeprom(pf);
10947 
10948 	/* if we are going out of or into recovery mode we have to act
10949 	 * accordingly with regard to resources initialization
10950 	 * and deinitialization
10951 	 */
10952 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10953 		if (i40e_get_capabilities(pf,
10954 					  i40e_aqc_opc_list_func_capabilities))
10955 			goto end_unlock;
10956 
10957 		if (is_recovery_mode_reported) {
10958 			/* we're staying in recovery mode so we'll reinitialize
10959 			 * misc vector here
10960 			 */
10961 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10962 				goto end_unlock;
10963 		} else {
10964 			if (!lock_acquired)
10965 				rtnl_lock();
10966 			/* we're going out of recovery mode so we'll free
10967 			 * the IRQ allocated specifically for recovery mode
10968 			 * and restore the interrupt scheme
10969 			 */
10970 			free_irq(pf->pdev->irq, pf);
10971 			i40e_clear_interrupt_scheme(pf);
10972 			if (i40e_restore_interrupt_scheme(pf))
10973 				goto end_unlock;
10974 		}
10975 
10976 		/* tell the firmware that we're starting */
10977 		i40e_send_version(pf);
10978 
10979 		/* bail out in case recovery mode was detected, as there is
10980 		 * no need for further configuration.
10981 		 */
10982 		goto end_unlock;
10983 	}
10984 
10985 	i40e_clear_pxe_mode(hw);
10986 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10987 	if (ret)
10988 		goto end_core_reset;
10989 
10990 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10991 				hw->func_caps.num_rx_qp, 0, 0);
10992 	if (ret) {
10993 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10994 		goto end_core_reset;
10995 	}
10996 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10997 	if (ret) {
10998 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10999 		goto end_core_reset;
11000 	}
11001 
11002 #ifdef CONFIG_I40E_DCB
11003 	/* Enable FW to write a default DCB config on link-up
11004 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
11005 	 * is not supported with new link speed
11006 	 */
11007 	if (i40e_is_tc_mqprio_enabled(pf)) {
11008 		i40e_aq_set_dcb_parameters(hw, false, NULL);
11009 	} else {
11010 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
11011 		    (hw->phy.link_info.link_speed &
11012 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
11013 			i40e_aq_set_dcb_parameters(hw, false, NULL);
11014 			dev_warn(&pf->pdev->dev,
11015 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
11016 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
11017 		} else {
11018 			i40e_aq_set_dcb_parameters(hw, true, NULL);
11019 			ret = i40e_init_pf_dcb(pf);
11020 			if (ret) {
11021 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
11022 					 ret);
11023 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
11024 				/* Continue without DCB enabled */
11025 			}
11026 		}
11027 	}
11028 
11029 #endif /* CONFIG_I40E_DCB */
11030 	if (!lock_acquired)
11031 		rtnl_lock();
11032 	ret = i40e_setup_pf_switch(pf, reinit, true);
11033 	if (ret)
11034 		goto end_unlock;
11035 
11036 	/* The driver only wants link up/down and module qualification
11037 	 * reports from firmware.  Note the negative logic.
11038 	 */
11039 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
11040 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
11041 					 I40E_AQ_EVENT_MEDIA_NA |
11042 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
11043 	if (ret)
11044 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
11045 			 ERR_PTR(ret),
11046 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11047 
11048 	/* Rebuild the VSIs and VEBs that existed before reset.
11049 	 * They are still in our local switch element arrays, so only
11050 	 * need to rebuild the switch model in the HW.
11051 	 *
11052 	 * If there were VEBs but the reconstitution failed, we'll try
11053 	 * to recover minimal use by getting the basic PF VSI working.
11054 	 */
11055 	if (vsi->uplink_seid != pf->mac_seid) {
11056 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
11057 
11058 		/* Rebuild VEBs */
11059 		i40e_pf_for_each_veb(pf, v, veb) {
11060 			ret = i40e_reconstitute_veb(veb);
11061 			if (!ret)
11062 				continue;
11063 
11064 			/* If Main VEB failed, we're in deep doodoo,
11065 			 * so give up rebuilding the switch and set up
11066 			 * for minimal rebuild of PF VSI.
11067 			 * If orphan failed, we'll report the error
11068 			 * but try to keep going.
11069 			 */
11070 			if (veb->uplink_seid == pf->mac_seid) {
11071 				dev_info(&pf->pdev->dev,
11072 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11073 					 ret);
11074 				vsi->uplink_seid = pf->mac_seid;
11075 				break;
11076 			} else if (veb->uplink_seid == 0) {
11077 				dev_info(&pf->pdev->dev,
11078 					 "rebuild of orphan VEB failed: %d\n",
11079 					 ret);
11080 			}
11081 		}
11082 	}
11083 
11084 	if (vsi->uplink_seid == pf->mac_seid) {
11085 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11086 		/* no VEB, so rebuild only the Main VSI */
11087 		ret = i40e_add_vsi(vsi);
11088 		if (ret) {
11089 			dev_info(&pf->pdev->dev,
11090 				 "rebuild of Main VSI failed: %d\n", ret);
11091 			goto end_unlock;
11092 		}
11093 	}
11094 
11095 	if (vsi->mqprio_qopt.max_rate[0]) {
11096 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11097 						  vsi->mqprio_qopt.max_rate[0]);
11098 		u64 credits = 0;
11099 
11100 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11101 		if (ret)
11102 			goto end_unlock;
11103 
11104 		credits = max_tx_rate;
11105 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11106 		dev_dbg(&vsi->back->pdev->dev,
11107 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11108 			max_tx_rate,
11109 			credits,
11110 			vsi->seid);
11111 	}
11112 
11113 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11114 	if (ret)
11115 		goto end_unlock;
11116 
11117 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11118 	 * for this main VSI if they exist
11119 	 */
11120 	ret = i40e_rebuild_channels(vsi);
11121 	if (ret)
11122 		goto end_unlock;
11123 
11124 	/* Reconfigure hardware for allowing smaller MSS in the case
11125 	 * of TSO, so that we avoid the MDD being fired and causing
11126 	 * a reset in the case of small MSS+TSO.
11127 	 */
11128 #define I40E_REG_MSS          0x000E64DC
11129 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11130 #define I40E_64BYTE_MSS       0x400000
11131 	val = rd32(hw, I40E_REG_MSS);
11132 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11133 		val &= ~I40E_REG_MSS_MIN_MASK;
11134 		val |= I40E_64BYTE_MSS;
11135 		wr32(hw, I40E_REG_MSS, val);
11136 	}
11137 
11138 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11139 		msleep(75);
11140 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11141 		if (ret)
11142 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11143 				 ERR_PTR(ret),
11144 				 i40e_aq_str(&pf->hw,
11145 					     pf->hw.aq.asq_last_status));
11146 	}
11147 	/* reinit the misc interrupt */
11148 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11149 		ret = i40e_setup_misc_vector(pf);
11150 		if (ret)
11151 			goto end_unlock;
11152 	}
11153 
11154 	/* Add a filter to drop all Flow control frames from any VSI from being
11155 	 * transmitted. By doing so we stop a malicious VF from sending out
11156 	 * PAUSE or PFC frames and potentially controlling traffic for other
11157 	 * PF/VF VSIs.
11158 	 * The FW can still send Flow control frames if enabled.
11159 	 */
11160 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11161 						       pf->main_vsi_seid);
11162 
11163 	/* restart the VSIs that were rebuilt and running before the reset */
11164 	i40e_pf_unquiesce_all_vsi(pf);
11165 
11166 	/* Release the RTNL lock before we start resetting VFs */
11167 	if (!lock_acquired)
11168 		rtnl_unlock();
11169 
11170 	/* Restore promiscuous settings */
11171 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11172 	if (ret)
11173 		dev_warn(&pf->pdev->dev,
11174 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11175 			 pf->cur_promisc ? "on" : "off",
11176 			 ERR_PTR(ret),
11177 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11178 
11179 	i40e_reset_all_vfs(pf, true);
11180 
11181 	/* tell the firmware that we're starting */
11182 	i40e_send_version(pf);
11183 
11184 	/* We've already released the lock, so don't do it again */
11185 	goto end_core_reset;
11186 
11187 end_unlock:
11188 	if (!lock_acquired)
11189 		rtnl_unlock();
11190 end_core_reset:
11191 	clear_bit(__I40E_RESET_FAILED, pf->state);
11192 clear_recovery:
11193 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11194 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11195 }
11196 
11197 /**
11198  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11199  * @pf: board private structure
11200  * @reinit: if the Main VSI needs to re-initialized.
11201  * @lock_acquired: indicates whether or not the lock has been acquired
11202  * before this function was called.
11203  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11204 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11205 				   bool lock_acquired)
11206 {
11207 	int ret;
11208 
11209 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11210 		return;
11211 	/* Now we wait for GRST to settle out.
11212 	 * We don't have to delete the VEBs or VSIs from the hw switch
11213 	 * because the reset will make them disappear.
11214 	 */
11215 	ret = i40e_reset(pf);
11216 	if (!ret)
11217 		i40e_rebuild(pf, reinit, lock_acquired);
11218 	else
11219 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11220 }
11221 
11222 /**
11223  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11224  * @pf: board private structure
11225  *
11226  * Close up the VFs and other things in prep for a Core Reset,
11227  * then get ready to rebuild the world.
11228  * @lock_acquired: indicates whether or not the lock has been acquired
11229  * before this function was called.
11230  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11231 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11232 {
11233 	i40e_prep_for_reset(pf);
11234 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11235 }
11236 
11237 /**
11238  * i40e_handle_mdd_event
11239  * @pf: pointer to the PF structure
11240  *
11241  * Called from the MDD irq handler to identify possibly malicious vfs
11242  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11243 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11244 {
11245 	struct i40e_hw *hw = &pf->hw;
11246 	bool mdd_detected = false;
11247 	struct i40e_vf *vf;
11248 	u32 reg;
11249 	int i;
11250 
11251 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11252 		return;
11253 
11254 	/* find what triggered the MDD event */
11255 	reg = rd32(hw, I40E_GL_MDET_TX);
11256 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11257 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11258 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11259 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11260 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11261 				pf->hw.func_caps.base_queue;
11262 		if (netif_msg_tx_err(pf))
11263 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11264 				 event, queue, pf_num, vf_num);
11265 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11266 		mdd_detected = true;
11267 	}
11268 	reg = rd32(hw, I40E_GL_MDET_RX);
11269 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11270 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11271 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11272 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11273 				pf->hw.func_caps.base_queue;
11274 		if (netif_msg_rx_err(pf))
11275 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11276 				 event, queue, func);
11277 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11278 		mdd_detected = true;
11279 	}
11280 
11281 	if (mdd_detected) {
11282 		reg = rd32(hw, I40E_PF_MDET_TX);
11283 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11284 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11285 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11286 		}
11287 		reg = rd32(hw, I40E_PF_MDET_RX);
11288 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11289 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11290 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11291 		}
11292 	}
11293 
11294 	/* see if one of the VFs needs its hand slapped */
11295 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11296 		vf = &(pf->vf[i]);
11297 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11298 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11299 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11300 			vf->num_mdd_events++;
11301 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11302 				 i);
11303 			dev_info(&pf->pdev->dev,
11304 				 "Use PF Control I/F to re-enable the VF\n");
11305 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11306 		}
11307 
11308 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11309 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11310 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11311 			vf->num_mdd_events++;
11312 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11313 				 i);
11314 			dev_info(&pf->pdev->dev,
11315 				 "Use PF Control I/F to re-enable the VF\n");
11316 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11317 		}
11318 	}
11319 
11320 	/* re-enable mdd interrupt cause */
11321 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11322 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11323 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11324 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11325 	i40e_flush(hw);
11326 }
11327 
11328 /**
11329  * i40e_service_task - Run the driver's async subtasks
11330  * @work: pointer to work_struct containing our data
11331  **/
i40e_service_task(struct work_struct * work)11332 static void i40e_service_task(struct work_struct *work)
11333 {
11334 	struct i40e_pf *pf = container_of(work,
11335 					  struct i40e_pf,
11336 					  service_task);
11337 	unsigned long start_time = jiffies;
11338 
11339 	/* don't bother with service tasks if a reset is in progress */
11340 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11341 	    test_bit(__I40E_SUSPENDED, pf->state))
11342 		return;
11343 
11344 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11345 		return;
11346 
11347 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11348 		i40e_detect_recover_hung(pf);
11349 		i40e_sync_filters_subtask(pf);
11350 		i40e_reset_subtask(pf);
11351 		i40e_handle_mdd_event(pf);
11352 		i40e_vc_process_vflr_event(pf);
11353 		i40e_watchdog_subtask(pf);
11354 		i40e_fdir_reinit_subtask(pf);
11355 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11356 			/* Client subtask will reopen next time through. */
11357 			i40e_notify_client_of_netdev_close(pf, true);
11358 		} else {
11359 			i40e_client_subtask(pf);
11360 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11361 					       pf->state))
11362 				i40e_notify_client_of_l2_param_changes(pf);
11363 		}
11364 		i40e_sync_filters_subtask(pf);
11365 	} else {
11366 		i40e_reset_subtask(pf);
11367 	}
11368 
11369 	i40e_clean_adminq_subtask(pf);
11370 
11371 	/* flush memory to make sure state is correct before next watchdog */
11372 	smp_mb__before_atomic();
11373 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11374 
11375 	/* If the tasks have taken longer than one timer cycle or there
11376 	 * is more work to be done, reschedule the service task now
11377 	 * rather than wait for the timer to tick again.
11378 	 */
11379 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11380 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11381 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11382 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11383 		i40e_service_event_schedule(pf);
11384 }
11385 
11386 /**
11387  * i40e_service_timer - timer callback
11388  * @t: timer list pointer
11389  **/
i40e_service_timer(struct timer_list * t)11390 static void i40e_service_timer(struct timer_list *t)
11391 {
11392 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11393 
11394 	mod_timer(&pf->service_timer,
11395 		  round_jiffies(jiffies + pf->service_timer_period));
11396 	i40e_service_event_schedule(pf);
11397 }
11398 
11399 /**
11400  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11401  * @vsi: the VSI being configured
11402  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11403 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11404 {
11405 	struct i40e_pf *pf = vsi->back;
11406 
11407 	switch (vsi->type) {
11408 	case I40E_VSI_MAIN:
11409 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11410 		if (!vsi->num_tx_desc)
11411 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11412 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11413 		if (!vsi->num_rx_desc)
11414 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11415 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11416 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11417 			vsi->num_q_vectors = pf->num_lan_msix;
11418 		else
11419 			vsi->num_q_vectors = 1;
11420 
11421 		break;
11422 
11423 	case I40E_VSI_FDIR:
11424 		vsi->alloc_queue_pairs = 1;
11425 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11426 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11427 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11428 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11429 		vsi->num_q_vectors = pf->num_fdsb_msix;
11430 		break;
11431 
11432 	case I40E_VSI_VMDQ2:
11433 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11434 		if (!vsi->num_tx_desc)
11435 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11436 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11437 		if (!vsi->num_rx_desc)
11438 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11439 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11440 		vsi->num_q_vectors = pf->num_vmdq_msix;
11441 		break;
11442 
11443 	case I40E_VSI_SRIOV:
11444 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11445 		if (!vsi->num_tx_desc)
11446 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11447 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11448 		if (!vsi->num_rx_desc)
11449 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11450 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11451 		break;
11452 
11453 	default:
11454 		WARN_ON(1);
11455 		return -ENODATA;
11456 	}
11457 
11458 	if (is_kdump_kernel()) {
11459 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11460 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11461 	}
11462 
11463 	return 0;
11464 }
11465 
11466 /**
11467  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11468  * @vsi: VSI pointer
11469  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11470  *
11471  * On error: returns error code (negative)
11472  * On success: returns 0
11473  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11474 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11475 {
11476 	struct i40e_ring **next_rings;
11477 	int size;
11478 	int ret = 0;
11479 
11480 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11481 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11482 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11483 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11484 	if (!vsi->tx_rings)
11485 		return -ENOMEM;
11486 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11487 	if (i40e_enabled_xdp_vsi(vsi)) {
11488 		vsi->xdp_rings = next_rings;
11489 		next_rings += vsi->alloc_queue_pairs;
11490 	}
11491 	vsi->rx_rings = next_rings;
11492 
11493 	if (alloc_qvectors) {
11494 		/* allocate memory for q_vector pointers */
11495 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11496 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11497 		if (!vsi->q_vectors) {
11498 			ret = -ENOMEM;
11499 			goto err_vectors;
11500 		}
11501 	}
11502 	return ret;
11503 
11504 err_vectors:
11505 	kfree(vsi->tx_rings);
11506 	return ret;
11507 }
11508 
11509 /**
11510  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11511  * @pf: board private structure
11512  * @type: type of VSI
11513  *
11514  * On error: returns error code (negative)
11515  * On success: returns vsi index in PF (positive)
11516  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11517 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11518 {
11519 	int ret = -ENODEV;
11520 	struct i40e_vsi *vsi;
11521 	int vsi_idx;
11522 	int i;
11523 
11524 	/* Need to protect the allocation of the VSIs at the PF level */
11525 	mutex_lock(&pf->switch_mutex);
11526 
11527 	/* VSI list may be fragmented if VSI creation/destruction has
11528 	 * been happening.  We can afford to do a quick scan to look
11529 	 * for any free VSIs in the list.
11530 	 *
11531 	 * find next empty vsi slot, looping back around if necessary
11532 	 */
11533 	i = pf->next_vsi;
11534 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11535 		i++;
11536 	if (i >= pf->num_alloc_vsi) {
11537 		i = 0;
11538 		while (i < pf->next_vsi && pf->vsi[i])
11539 			i++;
11540 	}
11541 
11542 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11543 		vsi_idx = i;             /* Found one! */
11544 	} else {
11545 		ret = -ENODEV;
11546 		goto unlock_pf;  /* out of VSI slots! */
11547 	}
11548 	pf->next_vsi = ++i;
11549 
11550 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11551 	if (!vsi) {
11552 		ret = -ENOMEM;
11553 		goto unlock_pf;
11554 	}
11555 	vsi->type = type;
11556 	vsi->back = pf;
11557 	set_bit(__I40E_VSI_DOWN, vsi->state);
11558 	vsi->flags = 0;
11559 	vsi->idx = vsi_idx;
11560 	vsi->int_rate_limit = 0;
11561 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11562 				pf->rss_table_size : 64;
11563 	vsi->netdev_registered = false;
11564 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11565 	hash_init(vsi->mac_filter_hash);
11566 	vsi->irqs_ready = false;
11567 
11568 	if (type == I40E_VSI_MAIN) {
11569 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11570 		if (!vsi->af_xdp_zc_qps)
11571 			goto err_rings;
11572 	}
11573 
11574 	ret = i40e_set_num_rings_in_vsi(vsi);
11575 	if (ret)
11576 		goto err_rings;
11577 
11578 	ret = i40e_vsi_alloc_arrays(vsi, true);
11579 	if (ret)
11580 		goto err_rings;
11581 
11582 	/* Setup default MSIX irq handler for VSI */
11583 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11584 
11585 	/* Initialize VSI lock */
11586 	spin_lock_init(&vsi->mac_filter_hash_lock);
11587 	pf->vsi[vsi_idx] = vsi;
11588 	ret = vsi_idx;
11589 	goto unlock_pf;
11590 
11591 err_rings:
11592 	bitmap_free(vsi->af_xdp_zc_qps);
11593 	pf->next_vsi = i - 1;
11594 	kfree(vsi);
11595 unlock_pf:
11596 	mutex_unlock(&pf->switch_mutex);
11597 	return ret;
11598 }
11599 
11600 /**
11601  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11602  * @vsi: VSI pointer
11603  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11604  *
11605  * On error: returns error code (negative)
11606  * On success: returns 0
11607  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11608 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11609 {
11610 	/* free the ring and vector containers */
11611 	if (free_qvectors) {
11612 		kfree(vsi->q_vectors);
11613 		vsi->q_vectors = NULL;
11614 	}
11615 	kfree(vsi->tx_rings);
11616 	vsi->tx_rings = NULL;
11617 	vsi->rx_rings = NULL;
11618 	vsi->xdp_rings = NULL;
11619 }
11620 
11621 /**
11622  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11623  * and lookup table
11624  * @vsi: Pointer to VSI structure
11625  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11626 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11627 {
11628 	if (!vsi)
11629 		return;
11630 
11631 	kfree(vsi->rss_hkey_user);
11632 	vsi->rss_hkey_user = NULL;
11633 
11634 	kfree(vsi->rss_lut_user);
11635 	vsi->rss_lut_user = NULL;
11636 }
11637 
11638 /**
11639  * i40e_vsi_clear - Deallocate the VSI provided
11640  * @vsi: the VSI being un-configured
11641  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11642 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11643 {
11644 	struct i40e_pf *pf;
11645 
11646 	if (!vsi)
11647 		return 0;
11648 
11649 	if (!vsi->back)
11650 		goto free_vsi;
11651 	pf = vsi->back;
11652 
11653 	mutex_lock(&pf->switch_mutex);
11654 	if (!pf->vsi[vsi->idx]) {
11655 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11656 			vsi->idx, vsi->idx, vsi->type);
11657 		goto unlock_vsi;
11658 	}
11659 
11660 	if (pf->vsi[vsi->idx] != vsi) {
11661 		dev_err(&pf->pdev->dev,
11662 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11663 			pf->vsi[vsi->idx]->idx,
11664 			pf->vsi[vsi->idx]->type,
11665 			vsi->idx, vsi->type);
11666 		goto unlock_vsi;
11667 	}
11668 
11669 	/* updates the PF for this cleared vsi */
11670 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11671 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11672 
11673 	bitmap_free(vsi->af_xdp_zc_qps);
11674 	i40e_vsi_free_arrays(vsi, true);
11675 	i40e_clear_rss_config_user(vsi);
11676 
11677 	pf->vsi[vsi->idx] = NULL;
11678 	if (vsi->idx < pf->next_vsi)
11679 		pf->next_vsi = vsi->idx;
11680 
11681 unlock_vsi:
11682 	mutex_unlock(&pf->switch_mutex);
11683 free_vsi:
11684 	kfree(vsi);
11685 
11686 	return 0;
11687 }
11688 
11689 /**
11690  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11691  * @vsi: the VSI being cleaned
11692  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11693 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11694 {
11695 	int i;
11696 
11697 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11698 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11699 			kfree_rcu(vsi->tx_rings[i], rcu);
11700 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11701 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11702 			if (vsi->xdp_rings)
11703 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11704 		}
11705 	}
11706 }
11707 
11708 /**
11709  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11710  * @vsi: the VSI being configured
11711  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11712 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11713 {
11714 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11715 	struct i40e_pf *pf = vsi->back;
11716 	struct i40e_ring *ring;
11717 
11718 	/* Set basic values in the rings to be used later during open() */
11719 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11720 		/* allocate space for both Tx and Rx in one shot */
11721 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11722 		if (!ring)
11723 			goto err_out;
11724 
11725 		ring->queue_index = i;
11726 		ring->reg_idx = vsi->base_queue + i;
11727 		ring->ring_active = false;
11728 		ring->vsi = vsi;
11729 		ring->netdev = vsi->netdev;
11730 		ring->dev = &pf->pdev->dev;
11731 		ring->count = vsi->num_tx_desc;
11732 		ring->size = 0;
11733 		ring->dcb_tc = 0;
11734 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11735 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11736 		ring->itr_setting = pf->tx_itr_default;
11737 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11738 
11739 		if (!i40e_enabled_xdp_vsi(vsi))
11740 			goto setup_rx;
11741 
11742 		ring->queue_index = vsi->alloc_queue_pairs + i;
11743 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11744 		ring->ring_active = false;
11745 		ring->vsi = vsi;
11746 		ring->netdev = NULL;
11747 		ring->dev = &pf->pdev->dev;
11748 		ring->count = vsi->num_tx_desc;
11749 		ring->size = 0;
11750 		ring->dcb_tc = 0;
11751 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11752 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11753 		set_ring_xdp(ring);
11754 		ring->itr_setting = pf->tx_itr_default;
11755 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11756 
11757 setup_rx:
11758 		ring->queue_index = i;
11759 		ring->reg_idx = vsi->base_queue + i;
11760 		ring->ring_active = false;
11761 		ring->vsi = vsi;
11762 		ring->netdev = vsi->netdev;
11763 		ring->dev = &pf->pdev->dev;
11764 		ring->count = vsi->num_rx_desc;
11765 		ring->size = 0;
11766 		ring->dcb_tc = 0;
11767 		ring->itr_setting = pf->rx_itr_default;
11768 		WRITE_ONCE(vsi->rx_rings[i], ring);
11769 	}
11770 
11771 	return 0;
11772 
11773 err_out:
11774 	i40e_vsi_clear_rings(vsi);
11775 	return -ENOMEM;
11776 }
11777 
11778 /**
11779  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11780  * @pf: board private structure
11781  * @vectors: the number of MSI-X vectors to request
11782  *
11783  * Returns the number of vectors reserved, or error
11784  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11785 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11786 {
11787 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11788 					I40E_MIN_MSIX, vectors);
11789 	if (vectors < 0) {
11790 		dev_info(&pf->pdev->dev,
11791 			 "MSI-X vector reservation failed: %d\n", vectors);
11792 		vectors = 0;
11793 	}
11794 
11795 	return vectors;
11796 }
11797 
11798 /**
11799  * i40e_init_msix - Setup the MSIX capability
11800  * @pf: board private structure
11801  *
11802  * Work with the OS to set up the MSIX vectors needed.
11803  *
11804  * Returns the number of vectors reserved or negative on failure
11805  **/
i40e_init_msix(struct i40e_pf * pf)11806 static int i40e_init_msix(struct i40e_pf *pf)
11807 {
11808 	struct i40e_hw *hw = &pf->hw;
11809 	int cpus, extra_vectors;
11810 	int vectors_left;
11811 	int v_budget, i;
11812 	int v_actual;
11813 	int iwarp_requested = 0;
11814 
11815 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11816 		return -ENODEV;
11817 
11818 	/* The number of vectors we'll request will be comprised of:
11819 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11820 	 *   - The number of LAN queue pairs
11821 	 *	- Queues being used for RSS.
11822 	 *		We don't need as many as max_rss_size vectors.
11823 	 *		use rss_size instead in the calculation since that
11824 	 *		is governed by number of cpus in the system.
11825 	 *	- assumes symmetric Tx/Rx pairing
11826 	 *   - The number of VMDq pairs
11827 	 *   - The CPU count within the NUMA node if iWARP is enabled
11828 	 * Once we count this up, try the request.
11829 	 *
11830 	 * If we can't get what we want, we'll simplify to nearly nothing
11831 	 * and try again.  If that still fails, we punt.
11832 	 */
11833 	vectors_left = hw->func_caps.num_msix_vectors;
11834 	v_budget = 0;
11835 
11836 	/* reserve one vector for miscellaneous handler */
11837 	if (vectors_left) {
11838 		v_budget++;
11839 		vectors_left--;
11840 	}
11841 
11842 	/* reserve some vectors for the main PF traffic queues. Initially we
11843 	 * only reserve at most 50% of the available vectors, in the case that
11844 	 * the number of online CPUs is large. This ensures that we can enable
11845 	 * extra features as well. Once we've enabled the other features, we
11846 	 * will use any remaining vectors to reach as close as we can to the
11847 	 * number of online CPUs.
11848 	 */
11849 	cpus = num_online_cpus();
11850 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11851 	vectors_left -= pf->num_lan_msix;
11852 
11853 	/* reserve one vector for sideband flow director */
11854 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11855 		if (vectors_left) {
11856 			pf->num_fdsb_msix = 1;
11857 			v_budget++;
11858 			vectors_left--;
11859 		} else {
11860 			pf->num_fdsb_msix = 0;
11861 		}
11862 	}
11863 
11864 	/* can we reserve enough for iWARP? */
11865 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11866 		iwarp_requested = pf->num_iwarp_msix;
11867 
11868 		if (!vectors_left)
11869 			pf->num_iwarp_msix = 0;
11870 		else if (vectors_left < pf->num_iwarp_msix)
11871 			pf->num_iwarp_msix = 1;
11872 		v_budget += pf->num_iwarp_msix;
11873 		vectors_left -= pf->num_iwarp_msix;
11874 	}
11875 
11876 	/* any vectors left over go for VMDq support */
11877 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11878 		if (!vectors_left) {
11879 			pf->num_vmdq_msix = 0;
11880 			pf->num_vmdq_qps = 0;
11881 		} else {
11882 			int vmdq_vecs_wanted =
11883 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11884 			int vmdq_vecs =
11885 				min_t(int, vectors_left, vmdq_vecs_wanted);
11886 
11887 			/* if we're short on vectors for what's desired, we limit
11888 			 * the queues per vmdq.  If this is still more than are
11889 			 * available, the user will need to change the number of
11890 			 * queues/vectors used by the PF later with the ethtool
11891 			 * channels command
11892 			 */
11893 			if (vectors_left < vmdq_vecs_wanted) {
11894 				pf->num_vmdq_qps = 1;
11895 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11896 				vmdq_vecs = min_t(int,
11897 						  vectors_left,
11898 						  vmdq_vecs_wanted);
11899 			}
11900 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11901 
11902 			v_budget += vmdq_vecs;
11903 			vectors_left -= vmdq_vecs;
11904 		}
11905 	}
11906 
11907 	/* On systems with a large number of SMP cores, we previously limited
11908 	 * the number of vectors for num_lan_msix to be at most 50% of the
11909 	 * available vectors, to allow for other features. Now, we add back
11910 	 * the remaining vectors. However, we ensure that the total
11911 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11912 	 * calculate the number of vectors we can add without going over the
11913 	 * cap of CPUs. For systems with a small number of CPUs this will be
11914 	 * zero.
11915 	 */
11916 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11917 	pf->num_lan_msix += extra_vectors;
11918 	vectors_left -= extra_vectors;
11919 
11920 	WARN(vectors_left < 0,
11921 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11922 
11923 	v_budget += pf->num_lan_msix;
11924 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11925 				   GFP_KERNEL);
11926 	if (!pf->msix_entries)
11927 		return -ENOMEM;
11928 
11929 	for (i = 0; i < v_budget; i++)
11930 		pf->msix_entries[i].entry = i;
11931 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11932 
11933 	if (v_actual < I40E_MIN_MSIX) {
11934 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11935 		kfree(pf->msix_entries);
11936 		pf->msix_entries = NULL;
11937 		pci_disable_msix(pf->pdev);
11938 		return -ENODEV;
11939 
11940 	} else if (v_actual == I40E_MIN_MSIX) {
11941 		/* Adjust for minimal MSIX use */
11942 		pf->num_vmdq_vsis = 0;
11943 		pf->num_vmdq_qps = 0;
11944 		pf->num_lan_qps = 1;
11945 		pf->num_lan_msix = 1;
11946 
11947 	} else if (v_actual != v_budget) {
11948 		/* If we have limited resources, we will start with no vectors
11949 		 * for the special features and then allocate vectors to some
11950 		 * of these features based on the policy and at the end disable
11951 		 * the features that did not get any vectors.
11952 		 */
11953 		int vec;
11954 
11955 		dev_info(&pf->pdev->dev,
11956 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11957 			 v_actual, v_budget);
11958 		/* reserve the misc vector */
11959 		vec = v_actual - 1;
11960 
11961 		/* Scale vector usage down */
11962 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11963 		pf->num_vmdq_vsis = 1;
11964 		pf->num_vmdq_qps = 1;
11965 
11966 		/* partition out the remaining vectors */
11967 		switch (vec) {
11968 		case 2:
11969 			pf->num_lan_msix = 1;
11970 			break;
11971 		case 3:
11972 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11973 				pf->num_lan_msix = 1;
11974 				pf->num_iwarp_msix = 1;
11975 			} else {
11976 				pf->num_lan_msix = 2;
11977 			}
11978 			break;
11979 		default:
11980 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11981 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11982 						 iwarp_requested);
11983 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11984 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11985 			} else {
11986 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11987 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11988 			}
11989 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11990 				pf->num_fdsb_msix = 1;
11991 				vec--;
11992 			}
11993 			pf->num_lan_msix = min_t(int,
11994 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11995 							      pf->num_lan_msix);
11996 			pf->num_lan_qps = pf->num_lan_msix;
11997 			break;
11998 		}
11999 	}
12000 
12001 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
12002 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
12003 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12004 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12005 	}
12006 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
12007 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
12008 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12009 	}
12010 
12011 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
12012 	    pf->num_iwarp_msix == 0) {
12013 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
12014 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12015 	}
12016 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
12017 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
12018 		   pf->num_lan_msix,
12019 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
12020 		   pf->num_fdsb_msix,
12021 		   pf->num_iwarp_msix);
12022 
12023 	return v_actual;
12024 }
12025 
12026 /**
12027  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12028  * @vsi: the VSI being configured
12029  * @v_idx: index of the vector in the vsi struct
12030  *
12031  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12032  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)12033 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12034 {
12035 	struct i40e_q_vector *q_vector;
12036 
12037 	/* allocate q_vector */
12038 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12039 	if (!q_vector)
12040 		return -ENOMEM;
12041 
12042 	q_vector->vsi = vsi;
12043 	q_vector->v_idx = v_idx;
12044 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12045 
12046 	if (vsi->netdev)
12047 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12048 
12049 	/* tie q_vector and vsi together */
12050 	vsi->q_vectors[v_idx] = q_vector;
12051 
12052 	return 0;
12053 }
12054 
12055 /**
12056  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12057  * @vsi: the VSI being configured
12058  *
12059  * We allocate one q_vector per queue interrupt.  If allocation fails we
12060  * return -ENOMEM.
12061  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)12062 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12063 {
12064 	struct i40e_pf *pf = vsi->back;
12065 	int err, v_idx, num_q_vectors;
12066 
12067 	/* if not MSIX, give the one vector only to the LAN VSI */
12068 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12069 		num_q_vectors = vsi->num_q_vectors;
12070 	else if (vsi->type == I40E_VSI_MAIN)
12071 		num_q_vectors = 1;
12072 	else
12073 		return -EINVAL;
12074 
12075 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12076 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12077 		if (err)
12078 			goto err_out;
12079 	}
12080 
12081 	return 0;
12082 
12083 err_out:
12084 	while (v_idx--)
12085 		i40e_free_q_vector(vsi, v_idx);
12086 
12087 	return err;
12088 }
12089 
12090 /**
12091  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12092  * @pf: board private structure to initialize
12093  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12094 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12095 {
12096 	int vectors = 0;
12097 	ssize_t size;
12098 
12099 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12100 		vectors = i40e_init_msix(pf);
12101 		if (vectors < 0) {
12102 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12103 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12104 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12105 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12106 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12107 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12108 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12109 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12110 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12111 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12112 
12113 			/* rework the queue expectations without MSIX */
12114 			i40e_determine_queue_usage(pf);
12115 		}
12116 	}
12117 
12118 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12119 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12120 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12121 		vectors = pci_enable_msi(pf->pdev);
12122 		if (vectors < 0) {
12123 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12124 				 vectors);
12125 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12126 		}
12127 		vectors = 1;  /* one MSI or Legacy vector */
12128 	}
12129 
12130 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12131 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12132 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12133 
12134 	/* set up vector assignment tracking */
12135 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12136 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12137 	if (!pf->irq_pile)
12138 		return -ENOMEM;
12139 
12140 	pf->irq_pile->num_entries = vectors;
12141 
12142 	/* track first vector for misc interrupts, ignore return */
12143 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12144 
12145 	return 0;
12146 }
12147 
12148 /**
12149  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12150  * @pf: private board data structure
12151  *
12152  * Restore the interrupt scheme that was cleared when we suspended the
12153  * device. This should be called during resume to re-allocate the q_vectors
12154  * and reacquire IRQs.
12155  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12156 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12157 {
12158 	struct i40e_vsi *vsi;
12159 	int err, i;
12160 
12161 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12162 	 * scheme. We need to re-enabled them here in order to attempt to
12163 	 * re-acquire the MSI or MSI-X vectors
12164 	 */
12165 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12166 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12167 
12168 	err = i40e_init_interrupt_scheme(pf);
12169 	if (err)
12170 		return err;
12171 
12172 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12173 	 * rings together again.
12174 	 */
12175 	i40e_pf_for_each_vsi(pf, i, vsi) {
12176 		err = i40e_vsi_alloc_q_vectors(vsi);
12177 		if (err)
12178 			goto err_unwind;
12179 
12180 		i40e_vsi_map_rings_to_vectors(vsi);
12181 	}
12182 
12183 	err = i40e_setup_misc_vector(pf);
12184 	if (err)
12185 		goto err_unwind;
12186 
12187 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12188 		i40e_client_update_msix_info(pf);
12189 
12190 	return 0;
12191 
12192 err_unwind:
12193 	while (i--) {
12194 		if (pf->vsi[i])
12195 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12196 	}
12197 
12198 	return err;
12199 }
12200 
12201 /**
12202  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12203  * non queue events in recovery mode
12204  * @pf: board private structure
12205  *
12206  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12207  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12208  * This is handled differently than in recovery mode since no Tx/Rx resources
12209  * are being allocated.
12210  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12211 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12212 {
12213 	int err;
12214 
12215 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12216 		err = i40e_setup_misc_vector(pf);
12217 
12218 		if (err) {
12219 			dev_info(&pf->pdev->dev,
12220 				 "MSI-X misc vector request failed, error %d\n",
12221 				 err);
12222 			return err;
12223 		}
12224 	} else {
12225 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12226 
12227 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12228 				  pf->int_name, pf);
12229 
12230 		if (err) {
12231 			dev_info(&pf->pdev->dev,
12232 				 "MSI/legacy misc vector request failed, error %d\n",
12233 				 err);
12234 			return err;
12235 		}
12236 		i40e_enable_misc_int_causes(pf);
12237 		i40e_irq_dynamic_enable_icr0(pf);
12238 	}
12239 
12240 	return 0;
12241 }
12242 
12243 /**
12244  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12245  * @pf: board private structure
12246  *
12247  * This sets up the handler for MSIX 0, which is used to manage the
12248  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12249  * when in MSI or Legacy interrupt mode.
12250  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12251 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12252 {
12253 	struct i40e_hw *hw = &pf->hw;
12254 	int err = 0;
12255 
12256 	/* Only request the IRQ once, the first time through. */
12257 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12258 		err = request_irq(pf->msix_entries[0].vector,
12259 				  i40e_intr, 0, pf->int_name, pf);
12260 		if (err) {
12261 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12262 			dev_info(&pf->pdev->dev,
12263 				 "request_irq for %s failed: %d\n",
12264 				 pf->int_name, err);
12265 			return -EFAULT;
12266 		}
12267 	}
12268 
12269 	i40e_enable_misc_int_causes(pf);
12270 
12271 	/* associate no queues to the misc vector */
12272 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12273 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12274 
12275 	i40e_flush(hw);
12276 
12277 	i40e_irq_dynamic_enable_icr0(pf);
12278 
12279 	return err;
12280 }
12281 
12282 /**
12283  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12284  * @vsi: Pointer to vsi structure
12285  * @seed: Buffter to store the hash keys
12286  * @lut: Buffer to store the lookup table entries
12287  * @lut_size: Size of buffer to store the lookup table entries
12288  *
12289  * Return 0 on success, negative on failure
12290  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12291 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12292 			   u8 *lut, u16 lut_size)
12293 {
12294 	struct i40e_pf *pf = vsi->back;
12295 	struct i40e_hw *hw = &pf->hw;
12296 	int ret = 0;
12297 
12298 	if (seed) {
12299 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12300 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12301 		if (ret) {
12302 			dev_info(&pf->pdev->dev,
12303 				 "Cannot get RSS key, err %pe aq_err %s\n",
12304 				 ERR_PTR(ret),
12305 				 i40e_aq_str(&pf->hw,
12306 					     pf->hw.aq.asq_last_status));
12307 			return ret;
12308 		}
12309 	}
12310 
12311 	if (lut) {
12312 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12313 
12314 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12315 		if (ret) {
12316 			dev_info(&pf->pdev->dev,
12317 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12318 				 ERR_PTR(ret),
12319 				 i40e_aq_str(&pf->hw,
12320 					     pf->hw.aq.asq_last_status));
12321 			return ret;
12322 		}
12323 	}
12324 
12325 	return ret;
12326 }
12327 
12328 /**
12329  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12330  * @vsi: Pointer to vsi structure
12331  * @seed: RSS hash seed
12332  * @lut: Lookup table
12333  * @lut_size: Lookup table size
12334  *
12335  * Returns 0 on success, negative on failure
12336  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12337 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12338 			       const u8 *lut, u16 lut_size)
12339 {
12340 	struct i40e_pf *pf = vsi->back;
12341 	struct i40e_hw *hw = &pf->hw;
12342 	u16 vf_id = vsi->vf_id;
12343 	u8 i;
12344 
12345 	/* Fill out hash function seed */
12346 	if (seed) {
12347 		u32 *seed_dw = (u32 *)seed;
12348 
12349 		if (vsi->type == I40E_VSI_MAIN) {
12350 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12351 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12352 		} else if (vsi->type == I40E_VSI_SRIOV) {
12353 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12354 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12355 		} else {
12356 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12357 		}
12358 	}
12359 
12360 	if (lut) {
12361 		u32 *lut_dw = (u32 *)lut;
12362 
12363 		if (vsi->type == I40E_VSI_MAIN) {
12364 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12365 				return -EINVAL;
12366 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12367 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12368 		} else if (vsi->type == I40E_VSI_SRIOV) {
12369 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12370 				return -EINVAL;
12371 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12372 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12373 		} else {
12374 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12375 		}
12376 	}
12377 	i40e_flush(hw);
12378 
12379 	return 0;
12380 }
12381 
12382 /**
12383  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12384  * @vsi: Pointer to VSI structure
12385  * @seed: Buffer to store the keys
12386  * @lut: Buffer to store the lookup table entries
12387  * @lut_size: Size of buffer to store the lookup table entries
12388  *
12389  * Returns 0 on success, negative on failure
12390  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12391 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12392 			    u8 *lut, u16 lut_size)
12393 {
12394 	struct i40e_pf *pf = vsi->back;
12395 	struct i40e_hw *hw = &pf->hw;
12396 	u16 i;
12397 
12398 	if (seed) {
12399 		u32 *seed_dw = (u32 *)seed;
12400 
12401 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12402 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12403 	}
12404 	if (lut) {
12405 		u32 *lut_dw = (u32 *)lut;
12406 
12407 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12408 			return -EINVAL;
12409 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12410 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12411 	}
12412 
12413 	return 0;
12414 }
12415 
12416 /**
12417  * i40e_config_rss - Configure RSS keys and lut
12418  * @vsi: Pointer to VSI structure
12419  * @seed: RSS hash seed
12420  * @lut: Lookup table
12421  * @lut_size: Lookup table size
12422  *
12423  * Returns 0 on success, negative on failure
12424  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12425 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12426 {
12427 	struct i40e_pf *pf = vsi->back;
12428 
12429 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12430 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12431 	else
12432 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12433 }
12434 
12435 /**
12436  * i40e_get_rss - Get RSS keys and lut
12437  * @vsi: Pointer to VSI structure
12438  * @seed: Buffer to store the keys
12439  * @lut: Buffer to store the lookup table entries
12440  * @lut_size: Size of buffer to store the lookup table entries
12441  *
12442  * Returns 0 on success, negative on failure
12443  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12444 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12445 {
12446 	struct i40e_pf *pf = vsi->back;
12447 
12448 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12449 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12450 	else
12451 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12452 }
12453 
12454 /**
12455  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12456  * @pf: Pointer to board private structure
12457  * @lut: Lookup table
12458  * @rss_table_size: Lookup table size
12459  * @rss_size: Range of queue number for hashing
12460  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12461 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12462 		       u16 rss_table_size, u16 rss_size)
12463 {
12464 	u16 i;
12465 
12466 	for (i = 0; i < rss_table_size; i++)
12467 		lut[i] = i % rss_size;
12468 }
12469 
12470 /**
12471  * i40e_pf_config_rss - Prepare for RSS if used
12472  * @pf: board private structure
12473  **/
i40e_pf_config_rss(struct i40e_pf * pf)12474 static int i40e_pf_config_rss(struct i40e_pf *pf)
12475 {
12476 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12477 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12478 	u8 *lut;
12479 	struct i40e_hw *hw = &pf->hw;
12480 	u32 reg_val;
12481 	u64 hena;
12482 	int ret;
12483 
12484 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12485 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12486 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12487 	hena |= i40e_pf_get_default_rss_hena(pf);
12488 
12489 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12490 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12491 
12492 	/* Determine the RSS table size based on the hardware capabilities */
12493 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12494 	reg_val = (pf->rss_table_size == 512) ?
12495 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12496 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12497 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12498 
12499 	/* Determine the RSS size of the VSI */
12500 	if (!vsi->rss_size) {
12501 		u16 qcount;
12502 		/* If the firmware does something weird during VSI init, we
12503 		 * could end up with zero TCs. Check for that to avoid
12504 		 * divide-by-zero. It probably won't pass traffic, but it also
12505 		 * won't panic.
12506 		 */
12507 		qcount = vsi->num_queue_pairs /
12508 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12509 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12510 	}
12511 	if (!vsi->rss_size)
12512 		return -EINVAL;
12513 
12514 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12515 	if (!lut)
12516 		return -ENOMEM;
12517 
12518 	/* Use user configured lut if there is one, otherwise use default */
12519 	if (vsi->rss_lut_user)
12520 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12521 	else
12522 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12523 
12524 	/* Use user configured hash key if there is one, otherwise
12525 	 * use default.
12526 	 */
12527 	if (vsi->rss_hkey_user)
12528 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12529 	else
12530 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12531 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12532 	kfree(lut);
12533 
12534 	return ret;
12535 }
12536 
12537 /**
12538  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12539  * @pf: board private structure
12540  * @queue_count: the requested queue count for rss.
12541  *
12542  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12543  * count which may be different from the requested queue count.
12544  * Note: expects to be called while under rtnl_lock()
12545  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12546 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12547 {
12548 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12549 	int new_rss_size;
12550 
12551 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12552 		return 0;
12553 
12554 	queue_count = min_t(int, queue_count, num_online_cpus());
12555 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12556 
12557 	if (queue_count != vsi->num_queue_pairs) {
12558 		u16 qcount;
12559 
12560 		vsi->req_queue_pairs = queue_count;
12561 		i40e_prep_for_reset(pf);
12562 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12563 			return pf->alloc_rss_size;
12564 
12565 		pf->alloc_rss_size = new_rss_size;
12566 
12567 		i40e_reset_and_rebuild(pf, true, true);
12568 
12569 		/* Discard the user configured hash keys and lut, if less
12570 		 * queues are enabled.
12571 		 */
12572 		if (queue_count < vsi->rss_size) {
12573 			i40e_clear_rss_config_user(vsi);
12574 			dev_dbg(&pf->pdev->dev,
12575 				"discard user configured hash keys and lut\n");
12576 		}
12577 
12578 		/* Reset vsi->rss_size, as number of enabled queues changed */
12579 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12580 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12581 
12582 		i40e_pf_config_rss(pf);
12583 	}
12584 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12585 		 vsi->req_queue_pairs, pf->rss_size_max);
12586 	return pf->alloc_rss_size;
12587 }
12588 
12589 /**
12590  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12591  * @pf: board private structure
12592  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12593 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12594 {
12595 	bool min_valid, max_valid;
12596 	u32 max_bw, min_bw;
12597 	int status;
12598 
12599 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12600 					   &min_valid, &max_valid);
12601 
12602 	if (!status) {
12603 		if (min_valid)
12604 			pf->min_bw = min_bw;
12605 		if (max_valid)
12606 			pf->max_bw = max_bw;
12607 	}
12608 
12609 	return status;
12610 }
12611 
12612 /**
12613  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12614  * @pf: board private structure
12615  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12616 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12617 {
12618 	struct i40e_aqc_configure_partition_bw_data bw_data;
12619 	int status;
12620 
12621 	memset(&bw_data, 0, sizeof(bw_data));
12622 
12623 	/* Set the valid bit for this PF */
12624 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12625 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12626 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12627 
12628 	/* Set the new bandwidths */
12629 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12630 
12631 	return status;
12632 }
12633 
12634 /**
12635  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12636  * @pf: board private structure
12637  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)12638 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12639 {
12640 	/* Commit temporary BW setting to permanent NVM image */
12641 	enum i40e_admin_queue_err last_aq_status;
12642 	u16 nvm_word;
12643 	int ret;
12644 
12645 	if (pf->hw.partition_id != 1) {
12646 		dev_info(&pf->pdev->dev,
12647 			 "Commit BW only works on partition 1! This is partition %d",
12648 			 pf->hw.partition_id);
12649 		ret = -EOPNOTSUPP;
12650 		goto bw_commit_out;
12651 	}
12652 
12653 	/* Acquire NVM for read access */
12654 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12655 	last_aq_status = pf->hw.aq.asq_last_status;
12656 	if (ret) {
12657 		dev_info(&pf->pdev->dev,
12658 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12659 			 ERR_PTR(ret),
12660 			 i40e_aq_str(&pf->hw, last_aq_status));
12661 		goto bw_commit_out;
12662 	}
12663 
12664 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12665 	ret = i40e_aq_read_nvm(&pf->hw,
12666 			       I40E_SR_NVM_CONTROL_WORD,
12667 			       0x10, sizeof(nvm_word), &nvm_word,
12668 			       false, NULL);
12669 	/* Save off last admin queue command status before releasing
12670 	 * the NVM
12671 	 */
12672 	last_aq_status = pf->hw.aq.asq_last_status;
12673 	i40e_release_nvm(&pf->hw);
12674 	if (ret) {
12675 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12676 			 ERR_PTR(ret),
12677 			 i40e_aq_str(&pf->hw, last_aq_status));
12678 		goto bw_commit_out;
12679 	}
12680 
12681 	/* Wait a bit for NVM release to complete */
12682 	msleep(50);
12683 
12684 	/* Acquire NVM for write access */
12685 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12686 	last_aq_status = pf->hw.aq.asq_last_status;
12687 	if (ret) {
12688 		dev_info(&pf->pdev->dev,
12689 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12690 			 ERR_PTR(ret),
12691 			 i40e_aq_str(&pf->hw, last_aq_status));
12692 		goto bw_commit_out;
12693 	}
12694 	/* Write it back out unchanged to initiate update NVM,
12695 	 * which will force a write of the shadow (alt) RAM to
12696 	 * the NVM - thus storing the bandwidth values permanently.
12697 	 */
12698 	ret = i40e_aq_update_nvm(&pf->hw,
12699 				 I40E_SR_NVM_CONTROL_WORD,
12700 				 0x10, sizeof(nvm_word),
12701 				 &nvm_word, true, 0, NULL);
12702 	/* Save off last admin queue command status before releasing
12703 	 * the NVM
12704 	 */
12705 	last_aq_status = pf->hw.aq.asq_last_status;
12706 	i40e_release_nvm(&pf->hw);
12707 	if (ret)
12708 		dev_info(&pf->pdev->dev,
12709 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12710 			 ERR_PTR(ret),
12711 			 i40e_aq_str(&pf->hw, last_aq_status));
12712 bw_commit_out:
12713 
12714 	return ret;
12715 }
12716 
12717 /**
12718  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12719  * if total port shutdown feature is enabled for this PF
12720  * @pf: board private structure
12721  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12722 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12723 {
12724 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12725 #define I40E_FEATURES_ENABLE_PTR		0x2A
12726 #define I40E_CURRENT_SETTING_PTR		0x2B
12727 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12728 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12729 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12730 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12731 	u16 sr_emp_sr_settings_ptr = 0;
12732 	u16 features_enable = 0;
12733 	u16 link_behavior = 0;
12734 	int read_status = 0;
12735 	bool ret = false;
12736 
12737 	read_status = i40e_read_nvm_word(&pf->hw,
12738 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12739 					 &sr_emp_sr_settings_ptr);
12740 	if (read_status)
12741 		goto err_nvm;
12742 	read_status = i40e_read_nvm_word(&pf->hw,
12743 					 sr_emp_sr_settings_ptr +
12744 					 I40E_FEATURES_ENABLE_PTR,
12745 					 &features_enable);
12746 	if (read_status)
12747 		goto err_nvm;
12748 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12749 		read_status = i40e_read_nvm_module_data(&pf->hw,
12750 							I40E_SR_EMP_SR_SETTINGS_PTR,
12751 							I40E_CURRENT_SETTING_PTR,
12752 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12753 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12754 							&link_behavior);
12755 		if (read_status)
12756 			goto err_nvm;
12757 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12758 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12759 	}
12760 	return ret;
12761 
12762 err_nvm:
12763 	dev_warn(&pf->pdev->dev,
12764 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12765 		 ERR_PTR(read_status));
12766 	return ret;
12767 }
12768 
12769 /**
12770  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12771  * @pf: board private structure to initialize
12772  *
12773  * i40e_sw_init initializes the Adapter private data structure.
12774  * Fields are initialized based on PCI device information and
12775  * OS network device settings (MTU size).
12776  **/
i40e_sw_init(struct i40e_pf * pf)12777 static int i40e_sw_init(struct i40e_pf *pf)
12778 {
12779 	int err = 0;
12780 	int size;
12781 	u16 pow;
12782 
12783 	/* Set default capability flags */
12784 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12785 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12786 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12787 
12788 	/* Set default ITR */
12789 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12790 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12791 
12792 	/* Depending on PF configurations, it is possible that the RSS
12793 	 * maximum might end up larger than the available queues
12794 	 */
12795 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12796 	pf->alloc_rss_size = 1;
12797 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12798 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12799 				 pf->hw.func_caps.num_tx_qp);
12800 
12801 	/* find the next higher power-of-2 of num cpus */
12802 	pow = roundup_pow_of_two(num_online_cpus());
12803 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12804 
12805 	if (pf->hw.func_caps.rss) {
12806 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12807 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12808 					   num_online_cpus());
12809 	}
12810 
12811 	/* MFP mode enabled */
12812 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12813 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12814 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12815 		if (i40e_get_partition_bw_setting(pf)) {
12816 			dev_warn(&pf->pdev->dev,
12817 				 "Could not get partition bw settings\n");
12818 		} else {
12819 			dev_info(&pf->pdev->dev,
12820 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12821 				 pf->min_bw, pf->max_bw);
12822 
12823 			/* nudge the Tx scheduler */
12824 			i40e_set_partition_bw_setting(pf);
12825 		}
12826 	}
12827 
12828 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12829 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12830 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12831 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12832 		    pf->hw.num_partitions > 1)
12833 			dev_info(&pf->pdev->dev,
12834 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12835 		else
12836 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12837 		pf->fdir_pf_filter_count =
12838 				 pf->hw.func_caps.fd_filters_guaranteed;
12839 		pf->hw.fdir_shared_filter_count =
12840 				 pf->hw.func_caps.fd_filters_best_effort;
12841 	}
12842 
12843 	/* Enable HW ATR eviction if possible */
12844 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12845 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12846 
12847 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12848 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12849 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12850 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12851 	}
12852 
12853 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12854 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12855 		/* IWARP needs one extra vector for CQP just like MISC.*/
12856 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12857 	}
12858 	/* Stopping FW LLDP engine is supported on XL710 and X722
12859 	 * starting from FW versions determined in i40e_init_adminq.
12860 	 * Stopping the FW LLDP engine is not supported on XL710
12861 	 * if NPAR is functioning so unset this hw flag in this case.
12862 	 */
12863 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12864 	    pf->hw.func_caps.npar_enable)
12865 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12866 
12867 #ifdef CONFIG_PCI_IOV
12868 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12869 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12870 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12871 		pf->num_req_vfs = min_t(int,
12872 					pf->hw.func_caps.num_vfs,
12873 					I40E_MAX_VF_COUNT);
12874 	}
12875 #endif /* CONFIG_PCI_IOV */
12876 	pf->lan_veb = I40E_NO_VEB;
12877 	pf->lan_vsi = I40E_NO_VSI;
12878 
12879 	/* By default FW has this off for performance reasons */
12880 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12881 
12882 	/* set up queue assignment tracking */
12883 	size = sizeof(struct i40e_lump_tracking)
12884 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12885 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12886 	if (!pf->qp_pile) {
12887 		err = -ENOMEM;
12888 		goto sw_init_done;
12889 	}
12890 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12891 
12892 	pf->tx_timeout_recovery_level = 1;
12893 
12894 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12895 	    i40e_is_total_port_shutdown_enabled(pf)) {
12896 		/* Link down on close must be on when total port shutdown
12897 		 * is enabled for a given port
12898 		 */
12899 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12900 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12901 		dev_info(&pf->pdev->dev,
12902 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12903 	}
12904 	mutex_init(&pf->switch_mutex);
12905 
12906 sw_init_done:
12907 	return err;
12908 }
12909 
12910 /**
12911  * i40e_set_ntuple - set the ntuple feature flag and take action
12912  * @pf: board private structure to initialize
12913  * @features: the feature set that the stack is suggesting
12914  *
12915  * returns a bool to indicate if reset needs to happen
12916  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12917 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12918 {
12919 	bool need_reset = false;
12920 
12921 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12922 	 * the state changed, we need to reset.
12923 	 */
12924 	if (features & NETIF_F_NTUPLE) {
12925 		/* Enable filters and mark for reset */
12926 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12927 			need_reset = true;
12928 		/* enable FD_SB only if there is MSI-X vector and no cloud
12929 		 * filters exist
12930 		 */
12931 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12932 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12933 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12934 		}
12935 	} else {
12936 		/* turn off filters, mark for reset and clear SW filter list */
12937 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12938 			need_reset = true;
12939 			i40e_fdir_filter_exit(pf);
12940 		}
12941 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12942 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12943 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12944 
12945 		/* reset fd counters */
12946 		pf->fd_add_err = 0;
12947 		pf->fd_atr_cnt = 0;
12948 		/* if ATR was auto disabled it can be re-enabled. */
12949 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12950 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12951 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12952 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12953 	}
12954 	return need_reset;
12955 }
12956 
12957 /**
12958  * i40e_clear_rss_lut - clear the rx hash lookup table
12959  * @vsi: the VSI being configured
12960  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12961 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12962 {
12963 	struct i40e_pf *pf = vsi->back;
12964 	struct i40e_hw *hw = &pf->hw;
12965 	u16 vf_id = vsi->vf_id;
12966 	u8 i;
12967 
12968 	if (vsi->type == I40E_VSI_MAIN) {
12969 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12970 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12971 	} else if (vsi->type == I40E_VSI_SRIOV) {
12972 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12973 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12974 	} else {
12975 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12976 	}
12977 }
12978 
12979 /**
12980  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12981  * @vsi: ptr to VSI
12982  * @ena: flag to indicate the on/off setting
12983  */
i40e_set_loopback(struct i40e_vsi * vsi,bool ena)12984 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12985 {
12986 	bool if_running = netif_running(vsi->netdev) &&
12987 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12988 	int ret;
12989 
12990 	if (if_running)
12991 		i40e_down(vsi);
12992 
12993 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12994 	if (ret)
12995 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12996 	if (if_running)
12997 		i40e_up(vsi);
12998 
12999 	return ret;
13000 }
13001 
13002 /**
13003  * i40e_set_features - set the netdev feature flags
13004  * @netdev: ptr to the netdev being adjusted
13005  * @features: the feature set that the stack is suggesting
13006  * Note: expects to be called while under rtnl_lock()
13007  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)13008 static int i40e_set_features(struct net_device *netdev,
13009 			     netdev_features_t features)
13010 {
13011 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13012 	struct i40e_vsi *vsi = np->vsi;
13013 	struct i40e_pf *pf = vsi->back;
13014 	bool need_reset;
13015 
13016 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13017 		i40e_pf_config_rss(pf);
13018 	else if (!(features & NETIF_F_RXHASH) &&
13019 		 netdev->features & NETIF_F_RXHASH)
13020 		i40e_clear_rss_lut(vsi);
13021 
13022 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13023 		i40e_vlan_stripping_enable(vsi);
13024 	else
13025 		i40e_vlan_stripping_disable(vsi);
13026 
13027 	if (!(features & NETIF_F_HW_TC) &&
13028 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13029 		dev_err(&pf->pdev->dev,
13030 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13031 		return -EINVAL;
13032 	}
13033 
13034 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13035 		i40e_del_all_macvlans(vsi);
13036 
13037 	need_reset = i40e_set_ntuple(pf, features);
13038 
13039 	if (need_reset)
13040 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13041 
13042 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13043 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13044 
13045 	return 0;
13046 }
13047 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13048 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13049 				    unsigned int table, unsigned int idx,
13050 				    struct udp_tunnel_info *ti)
13051 {
13052 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13053 	struct i40e_hw *hw = &np->vsi->back->hw;
13054 	u8 type, filter_index;
13055 	int ret;
13056 
13057 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13058 						   I40E_AQC_TUNNEL_TYPE_NGE;
13059 
13060 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13061 				     NULL);
13062 	if (ret) {
13063 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13064 			    ERR_PTR(ret),
13065 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13066 		return -EIO;
13067 	}
13068 
13069 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13070 	return 0;
13071 }
13072 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13073 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13074 				      unsigned int table, unsigned int idx,
13075 				      struct udp_tunnel_info *ti)
13076 {
13077 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13078 	struct i40e_hw *hw = &np->vsi->back->hw;
13079 	int ret;
13080 
13081 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13082 	if (ret) {
13083 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13084 			    ERR_PTR(ret),
13085 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13086 		return -EIO;
13087 	}
13088 
13089 	return 0;
13090 }
13091 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)13092 static int i40e_get_phys_port_id(struct net_device *netdev,
13093 				 struct netdev_phys_item_id *ppid)
13094 {
13095 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13096 	struct i40e_pf *pf = np->vsi->back;
13097 	struct i40e_hw *hw = &pf->hw;
13098 
13099 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13100 		return -EOPNOTSUPP;
13101 
13102 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13103 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13104 
13105 	return 0;
13106 }
13107 
13108 /**
13109  * i40e_ndo_fdb_add - add an entry to the hardware database
13110  * @ndm: the input from the stack
13111  * @tb: pointer to array of nladdr (unused)
13112  * @dev: the net device pointer
13113  * @addr: the MAC address entry being added
13114  * @vid: VLAN ID
13115  * @flags: instructions from stack about fdb operation
13116  * @extack: netlink extended ack, unused currently
13117  */
i40e_ndo_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)13118 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13119 			    struct net_device *dev,
13120 			    const unsigned char *addr, u16 vid,
13121 			    u16 flags,
13122 			    struct netlink_ext_ack *extack)
13123 {
13124 	struct i40e_netdev_priv *np = netdev_priv(dev);
13125 	struct i40e_pf *pf = np->vsi->back;
13126 	int err = 0;
13127 
13128 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13129 		return -EOPNOTSUPP;
13130 
13131 	if (vid) {
13132 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13133 		return -EINVAL;
13134 	}
13135 
13136 	/* Hardware does not support aging addresses so if a
13137 	 * ndm_state is given only allow permanent addresses
13138 	 */
13139 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13140 		netdev_info(dev, "FDB only supports static addresses\n");
13141 		return -EINVAL;
13142 	}
13143 
13144 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13145 		err = dev_uc_add_excl(dev, addr);
13146 	else if (is_multicast_ether_addr(addr))
13147 		err = dev_mc_add_excl(dev, addr);
13148 	else
13149 		err = -EINVAL;
13150 
13151 	/* Only return duplicate errors if NLM_F_EXCL is set */
13152 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13153 		err = 0;
13154 
13155 	return err;
13156 }
13157 
13158 /**
13159  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13160  * @dev: the netdev being configured
13161  * @nlh: RTNL message
13162  * @flags: bridge flags
13163  * @extack: netlink extended ack
13164  *
13165  * Inserts a new hardware bridge if not already created and
13166  * enables the bridging mode requested (VEB or VEPA). If the
13167  * hardware bridge has already been inserted and the request
13168  * is to change the mode then that requires a PF reset to
13169  * allow rebuild of the components with required hardware
13170  * bridge mode enabled.
13171  *
13172  * Note: expects to be called while under rtnl_lock()
13173  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13174 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13175 				   struct nlmsghdr *nlh,
13176 				   u16 flags,
13177 				   struct netlink_ext_ack *extack)
13178 {
13179 	struct i40e_netdev_priv *np = netdev_priv(dev);
13180 	struct i40e_vsi *vsi = np->vsi;
13181 	struct i40e_pf *pf = vsi->back;
13182 	struct nlattr *attr, *br_spec;
13183 	struct i40e_veb *veb;
13184 	int rem;
13185 
13186 	/* Only for PF VSI for now */
13187 	if (vsi->type != I40E_VSI_MAIN)
13188 		return -EOPNOTSUPP;
13189 
13190 	/* Find the HW bridge for PF VSI */
13191 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13192 
13193 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13194 	if (!br_spec)
13195 		return -EINVAL;
13196 
13197 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13198 		__u16 mode = nla_get_u16(attr);
13199 
13200 		if ((mode != BRIDGE_MODE_VEPA) &&
13201 		    (mode != BRIDGE_MODE_VEB))
13202 			return -EINVAL;
13203 
13204 		/* Insert a new HW bridge */
13205 		if (!veb) {
13206 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13207 					     vsi->tc_config.enabled_tc);
13208 			if (veb) {
13209 				veb->bridge_mode = mode;
13210 				i40e_config_bridge_mode(veb);
13211 			} else {
13212 				/* No Bridge HW offload available */
13213 				return -ENOENT;
13214 			}
13215 			break;
13216 		} else if (mode != veb->bridge_mode) {
13217 			/* Existing HW bridge but different mode needs reset */
13218 			veb->bridge_mode = mode;
13219 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13220 			if (mode == BRIDGE_MODE_VEB)
13221 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13222 			else
13223 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13224 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13225 			break;
13226 		}
13227 	}
13228 
13229 	return 0;
13230 }
13231 
13232 /**
13233  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13234  * @skb: skb buff
13235  * @pid: process id
13236  * @seq: RTNL message seq #
13237  * @dev: the netdev being configured
13238  * @filter_mask: unused
13239  * @nlflags: netlink flags passed in
13240  *
13241  * Return the mode in which the hardware bridge is operating in
13242  * i.e VEB or VEPA.
13243  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13244 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13245 				   struct net_device *dev,
13246 				   u32 __always_unused filter_mask,
13247 				   int nlflags)
13248 {
13249 	struct i40e_netdev_priv *np = netdev_priv(dev);
13250 	struct i40e_vsi *vsi = np->vsi;
13251 	struct i40e_pf *pf = vsi->back;
13252 	struct i40e_veb *veb;
13253 
13254 	/* Only for PF VSI for now */
13255 	if (vsi->type != I40E_VSI_MAIN)
13256 		return -EOPNOTSUPP;
13257 
13258 	/* Find the HW bridge for the PF VSI */
13259 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13260 	if (!veb)
13261 		return 0;
13262 
13263 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13264 				       0, 0, nlflags, filter_mask, NULL);
13265 }
13266 
13267 /**
13268  * i40e_features_check - Validate encapsulated packet conforms to limits
13269  * @skb: skb buff
13270  * @dev: This physical port's netdev
13271  * @features: Offload features that the stack believes apply
13272  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13273 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13274 					     struct net_device *dev,
13275 					     netdev_features_t features)
13276 {
13277 	size_t len;
13278 
13279 	/* No point in doing any of this if neither checksum nor GSO are
13280 	 * being requested for this frame.  We can rule out both by just
13281 	 * checking for CHECKSUM_PARTIAL
13282 	 */
13283 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13284 		return features;
13285 
13286 	/* We cannot support GSO if the MSS is going to be less than
13287 	 * 64 bytes.  If it is then we need to drop support for GSO.
13288 	 */
13289 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13290 		features &= ~NETIF_F_GSO_MASK;
13291 
13292 	/* MACLEN can support at most 63 words */
13293 	len = skb_network_offset(skb);
13294 	if (len & ~(63 * 2))
13295 		goto out_err;
13296 
13297 	/* IPLEN and EIPLEN can support at most 127 dwords */
13298 	len = skb_network_header_len(skb);
13299 	if (len & ~(127 * 4))
13300 		goto out_err;
13301 
13302 	if (skb->encapsulation) {
13303 		/* L4TUNLEN can support 127 words */
13304 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13305 		if (len & ~(127 * 2))
13306 			goto out_err;
13307 
13308 		/* IPLEN can support at most 127 dwords */
13309 		len = skb_inner_transport_header(skb) -
13310 		      skb_inner_network_header(skb);
13311 		if (len & ~(127 * 4))
13312 			goto out_err;
13313 	}
13314 
13315 	/* No need to validate L4LEN as TCP is the only protocol with a
13316 	 * flexible value and we support all possible values supported
13317 	 * by TCP, which is at most 15 dwords
13318 	 */
13319 
13320 	return features;
13321 out_err:
13322 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13323 }
13324 
13325 /**
13326  * i40e_xdp_setup - add/remove an XDP program
13327  * @vsi: VSI to changed
13328  * @prog: XDP program
13329  * @extack: netlink extended ack
13330  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13331 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13332 			  struct netlink_ext_ack *extack)
13333 {
13334 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13335 	struct i40e_pf *pf = vsi->back;
13336 	struct bpf_prog *old_prog;
13337 	bool need_reset;
13338 	int i;
13339 
13340 	/* VSI shall be deleted in a moment, block loading new programs */
13341 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13342 		return -EINVAL;
13343 
13344 	/* Don't allow frames that span over multiple buffers */
13345 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13346 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13347 		return -EINVAL;
13348 	}
13349 
13350 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13351 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13352 	if (need_reset)
13353 		i40e_prep_for_reset(pf);
13354 
13355 	old_prog = xchg(&vsi->xdp_prog, prog);
13356 
13357 	if (need_reset) {
13358 		if (!prog) {
13359 			xdp_features_clear_redirect_target(vsi->netdev);
13360 			/* Wait until ndo_xsk_wakeup completes. */
13361 			synchronize_rcu();
13362 		}
13363 		i40e_reset_and_rebuild(pf, true, true);
13364 	}
13365 
13366 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13367 		if (i40e_realloc_rx_bi_zc(vsi, true))
13368 			return -ENOMEM;
13369 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13370 		if (i40e_realloc_rx_bi_zc(vsi, false))
13371 			return -ENOMEM;
13372 	}
13373 
13374 	for (i = 0; i < vsi->num_queue_pairs; i++)
13375 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13376 
13377 	if (old_prog)
13378 		bpf_prog_put(old_prog);
13379 
13380 	/* Kick start the NAPI context if there is an AF_XDP socket open
13381 	 * on that queue id. This so that receiving will start.
13382 	 */
13383 	if (need_reset && prog) {
13384 		for (i = 0; i < vsi->num_queue_pairs; i++)
13385 			if (vsi->xdp_rings[i]->xsk_pool)
13386 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13387 						      XDP_WAKEUP_RX);
13388 		xdp_features_set_redirect_target(vsi->netdev, true);
13389 	}
13390 
13391 	return 0;
13392 }
13393 
13394 /**
13395  * i40e_enter_busy_conf - Enters busy config state
13396  * @vsi: vsi
13397  *
13398  * Returns 0 on success, <0 for failure.
13399  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13400 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13401 {
13402 	struct i40e_pf *pf = vsi->back;
13403 	int timeout = 50;
13404 
13405 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13406 		timeout--;
13407 		if (!timeout)
13408 			return -EBUSY;
13409 		usleep_range(1000, 2000);
13410 	}
13411 
13412 	return 0;
13413 }
13414 
13415 /**
13416  * i40e_exit_busy_conf - Exits busy config state
13417  * @vsi: vsi
13418  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13419 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13420 {
13421 	struct i40e_pf *pf = vsi->back;
13422 
13423 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13424 }
13425 
13426 /**
13427  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13428  * @vsi: vsi
13429  * @queue_pair: queue pair
13430  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13431 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13432 {
13433 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13434 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13435 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13436 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13437 	if (i40e_enabled_xdp_vsi(vsi)) {
13438 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13439 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13440 	}
13441 }
13442 
13443 /**
13444  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13445  * @vsi: vsi
13446  * @queue_pair: queue pair
13447  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13448 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13449 {
13450 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13451 	if (i40e_enabled_xdp_vsi(vsi)) {
13452 		/* Make sure that in-progress ndo_xdp_xmit calls are
13453 		 * completed.
13454 		 */
13455 		synchronize_rcu();
13456 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13457 	}
13458 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13459 }
13460 
13461 /**
13462  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13463  * @vsi: vsi
13464  * @queue_pair: queue pair
13465  * @enable: true for enable, false for disable
13466  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13467 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13468 					bool enable)
13469 {
13470 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13471 	struct i40e_q_vector *q_vector = rxr->q_vector;
13472 
13473 	if (!vsi->netdev)
13474 		return;
13475 
13476 	/* All rings in a qp belong to the same qvector. */
13477 	if (q_vector->rx.ring || q_vector->tx.ring) {
13478 		if (enable)
13479 			napi_enable(&q_vector->napi);
13480 		else
13481 			napi_disable(&q_vector->napi);
13482 	}
13483 }
13484 
13485 /**
13486  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13487  * @vsi: vsi
13488  * @queue_pair: queue pair
13489  * @enable: true for enable, false for disable
13490  *
13491  * Returns 0 on success, <0 on failure.
13492  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13493 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13494 					bool enable)
13495 {
13496 	struct i40e_pf *pf = vsi->back;
13497 	int pf_q, ret = 0;
13498 
13499 	pf_q = vsi->base_queue + queue_pair;
13500 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13501 				     false /*is xdp*/, enable);
13502 	if (ret) {
13503 		dev_info(&pf->pdev->dev,
13504 			 "VSI seid %d Tx ring %d %sable timeout\n",
13505 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13506 		return ret;
13507 	}
13508 
13509 	i40e_control_rx_q(pf, pf_q, enable);
13510 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13511 	if (ret) {
13512 		dev_info(&pf->pdev->dev,
13513 			 "VSI seid %d Rx ring %d %sable timeout\n",
13514 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13515 		return ret;
13516 	}
13517 
13518 	/* Due to HW errata, on Rx disable only, the register can
13519 	 * indicate done before it really is. Needs 50ms to be sure
13520 	 */
13521 	if (!enable)
13522 		mdelay(50);
13523 
13524 	if (!i40e_enabled_xdp_vsi(vsi))
13525 		return ret;
13526 
13527 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13528 				     pf_q + vsi->alloc_queue_pairs,
13529 				     true /*is xdp*/, enable);
13530 	if (ret) {
13531 		dev_info(&pf->pdev->dev,
13532 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13533 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13534 	}
13535 
13536 	return ret;
13537 }
13538 
13539 /**
13540  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13541  * @vsi: vsi
13542  * @queue_pair: queue_pair
13543  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13544 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13545 {
13546 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13547 	struct i40e_pf *pf = vsi->back;
13548 	struct i40e_hw *hw = &pf->hw;
13549 
13550 	/* All rings in a qp belong to the same qvector. */
13551 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13552 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13553 	else
13554 		i40e_irq_dynamic_enable_icr0(pf);
13555 
13556 	i40e_flush(hw);
13557 }
13558 
13559 /**
13560  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13561  * @vsi: vsi
13562  * @queue_pair: queue_pair
13563  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13564 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13565 {
13566 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13567 	struct i40e_pf *pf = vsi->back;
13568 	struct i40e_hw *hw = &pf->hw;
13569 
13570 	/* For simplicity, instead of removing the qp interrupt causes
13571 	 * from the interrupt linked list, we simply disable the interrupt, and
13572 	 * leave the list intact.
13573 	 *
13574 	 * All rings in a qp belong to the same qvector.
13575 	 */
13576 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13577 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13578 
13579 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13580 		i40e_flush(hw);
13581 		synchronize_irq(pf->msix_entries[intpf].vector);
13582 	} else {
13583 		/* Legacy and MSI mode - this stops all interrupt handling */
13584 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13585 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13586 		i40e_flush(hw);
13587 		synchronize_irq(pf->pdev->irq);
13588 	}
13589 }
13590 
13591 /**
13592  * i40e_queue_pair_disable - Disables a queue pair
13593  * @vsi: vsi
13594  * @queue_pair: queue pair
13595  *
13596  * Returns 0 on success, <0 on failure.
13597  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13598 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13599 {
13600 	int err;
13601 
13602 	err = i40e_enter_busy_conf(vsi);
13603 	if (err)
13604 		return err;
13605 
13606 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13607 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13608 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13609 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13610 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13611 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13612 
13613 	return err;
13614 }
13615 
13616 /**
13617  * i40e_queue_pair_enable - Enables a queue pair
13618  * @vsi: vsi
13619  * @queue_pair: queue pair
13620  *
13621  * Returns 0 on success, <0 on failure.
13622  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13623 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13624 {
13625 	int err;
13626 
13627 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13628 	if (err)
13629 		return err;
13630 
13631 	if (i40e_enabled_xdp_vsi(vsi)) {
13632 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13633 		if (err)
13634 			return err;
13635 	}
13636 
13637 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13638 	if (err)
13639 		return err;
13640 
13641 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13642 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13643 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13644 
13645 	i40e_exit_busy_conf(vsi);
13646 
13647 	return err;
13648 }
13649 
13650 /**
13651  * i40e_xdp - implements ndo_bpf for i40e
13652  * @dev: netdevice
13653  * @xdp: XDP command
13654  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13655 static int i40e_xdp(struct net_device *dev,
13656 		    struct netdev_bpf *xdp)
13657 {
13658 	struct i40e_netdev_priv *np = netdev_priv(dev);
13659 	struct i40e_vsi *vsi = np->vsi;
13660 
13661 	if (vsi->type != I40E_VSI_MAIN)
13662 		return -EINVAL;
13663 
13664 	switch (xdp->command) {
13665 	case XDP_SETUP_PROG:
13666 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13667 	case XDP_SETUP_XSK_POOL:
13668 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13669 					   xdp->xsk.queue_id);
13670 	default:
13671 		return -EINVAL;
13672 	}
13673 }
13674 
13675 static const struct net_device_ops i40e_netdev_ops = {
13676 	.ndo_open		= i40e_open,
13677 	.ndo_stop		= i40e_close,
13678 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13679 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13680 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13681 	.ndo_validate_addr	= eth_validate_addr,
13682 	.ndo_set_mac_address	= i40e_set_mac,
13683 	.ndo_change_mtu		= i40e_change_mtu,
13684 	.ndo_eth_ioctl		= i40e_ioctl,
13685 	.ndo_tx_timeout		= i40e_tx_timeout,
13686 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13687 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13688 #ifdef CONFIG_NET_POLL_CONTROLLER
13689 	.ndo_poll_controller	= i40e_netpoll,
13690 #endif
13691 	.ndo_setup_tc		= __i40e_setup_tc,
13692 	.ndo_select_queue	= i40e_lan_select_queue,
13693 	.ndo_set_features	= i40e_set_features,
13694 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13695 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13696 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13697 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13698 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13699 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13700 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13701 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13702 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13703 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13704 	.ndo_features_check	= i40e_features_check,
13705 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13706 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13707 	.ndo_bpf		= i40e_xdp,
13708 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13709 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13710 	.ndo_dfwd_add_station	= i40e_fwd_add,
13711 	.ndo_dfwd_del_station	= i40e_fwd_del,
13712 };
13713 
13714 /**
13715  * i40e_config_netdev - Setup the netdev flags
13716  * @vsi: the VSI being configured
13717  *
13718  * Returns 0 on success, negative value on failure
13719  **/
i40e_config_netdev(struct i40e_vsi * vsi)13720 static int i40e_config_netdev(struct i40e_vsi *vsi)
13721 {
13722 	struct i40e_pf *pf = vsi->back;
13723 	struct i40e_hw *hw = &pf->hw;
13724 	struct i40e_netdev_priv *np;
13725 	struct net_device *netdev;
13726 	u8 broadcast[ETH_ALEN];
13727 	u8 mac_addr[ETH_ALEN];
13728 	int etherdev_size;
13729 	netdev_features_t hw_enc_features;
13730 	netdev_features_t hw_features;
13731 
13732 	etherdev_size = sizeof(struct i40e_netdev_priv);
13733 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13734 	if (!netdev)
13735 		return -ENOMEM;
13736 
13737 	vsi->netdev = netdev;
13738 	np = netdev_priv(netdev);
13739 	np->vsi = vsi;
13740 
13741 	hw_enc_features = NETIF_F_SG			|
13742 			  NETIF_F_HW_CSUM		|
13743 			  NETIF_F_HIGHDMA		|
13744 			  NETIF_F_SOFT_FEATURES		|
13745 			  NETIF_F_TSO			|
13746 			  NETIF_F_TSO_ECN		|
13747 			  NETIF_F_TSO6			|
13748 			  NETIF_F_GSO_GRE		|
13749 			  NETIF_F_GSO_GRE_CSUM		|
13750 			  NETIF_F_GSO_PARTIAL		|
13751 			  NETIF_F_GSO_IPXIP4		|
13752 			  NETIF_F_GSO_IPXIP6		|
13753 			  NETIF_F_GSO_UDP_TUNNEL	|
13754 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13755 			  NETIF_F_GSO_UDP_L4		|
13756 			  NETIF_F_SCTP_CRC		|
13757 			  NETIF_F_RXHASH		|
13758 			  NETIF_F_RXCSUM		|
13759 			  0;
13760 
13761 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13762 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13763 
13764 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13765 
13766 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13767 
13768 	netdev->hw_enc_features |= hw_enc_features;
13769 
13770 	/* record features VLANs can make use of */
13771 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13772 
13773 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13774 				   NETIF_F_GSO_GRE_CSUM |	\
13775 				   NETIF_F_GSO_IPXIP4 |		\
13776 				   NETIF_F_GSO_IPXIP6 |		\
13777 				   NETIF_F_GSO_UDP_TUNNEL |	\
13778 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13779 
13780 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13781 	netdev->features |= NETIF_F_GSO_PARTIAL |
13782 			    I40E_GSO_PARTIAL_FEATURES;
13783 
13784 	netdev->mpls_features |= NETIF_F_SG;
13785 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13786 	netdev->mpls_features |= NETIF_F_TSO;
13787 	netdev->mpls_features |= NETIF_F_TSO6;
13788 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13789 
13790 	/* enable macvlan offloads */
13791 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13792 
13793 	hw_features = hw_enc_features		|
13794 		      NETIF_F_HW_VLAN_CTAG_TX	|
13795 		      NETIF_F_HW_VLAN_CTAG_RX;
13796 
13797 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13798 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13799 
13800 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13801 
13802 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13803 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13804 
13805 	netdev->features &= ~NETIF_F_HW_TC;
13806 
13807 	if (vsi->type == I40E_VSI_MAIN) {
13808 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13809 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13810 		/* The following steps are necessary for two reasons. First,
13811 		 * some older NVM configurations load a default MAC-VLAN
13812 		 * filter that will accept any tagged packet, and we want to
13813 		 * replace this with a normal filter. Additionally, it is
13814 		 * possible our MAC address was provided by the platform using
13815 		 * Open Firmware or similar.
13816 		 *
13817 		 * Thus, we need to remove the default filter and install one
13818 		 * specific to the MAC address.
13819 		 */
13820 		i40e_rm_default_mac_filter(vsi, mac_addr);
13821 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13822 		i40e_add_mac_filter(vsi, mac_addr);
13823 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13824 
13825 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13826 				       NETDEV_XDP_ACT_REDIRECT |
13827 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13828 				       NETDEV_XDP_ACT_RX_SG;
13829 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13830 	} else {
13831 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13832 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13833 		 * the end, which is 4 bytes long, so force truncation of the
13834 		 * original name by IFNAMSIZ - 4
13835 		 */
13836 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13837 
13838 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13839 			 main_vsi->netdev->name);
13840 		eth_random_addr(mac_addr);
13841 
13842 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13843 		i40e_add_mac_filter(vsi, mac_addr);
13844 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13845 	}
13846 
13847 	/* Add the broadcast filter so that we initially will receive
13848 	 * broadcast packets. Note that when a new VLAN is first added the
13849 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13850 	 * specific filters as part of transitioning into "vlan" operation.
13851 	 * When more VLANs are added, the driver will copy each existing MAC
13852 	 * filter and add it for the new VLAN.
13853 	 *
13854 	 * Broadcast filters are handled specially by
13855 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13856 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13857 	 * filter. The subtask will update the correct broadcast promiscuous
13858 	 * bits as VLANs become active or inactive.
13859 	 */
13860 	eth_broadcast_addr(broadcast);
13861 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13862 	i40e_add_mac_filter(vsi, broadcast);
13863 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13864 
13865 	eth_hw_addr_set(netdev, mac_addr);
13866 	ether_addr_copy(netdev->perm_addr, mac_addr);
13867 
13868 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13869 	netdev->neigh_priv_len = sizeof(u32) * 4;
13870 
13871 	netdev->priv_flags |= IFF_UNICAST_FLT;
13872 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13873 	/* Setup netdev TC information */
13874 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13875 
13876 	netdev->netdev_ops = &i40e_netdev_ops;
13877 	netdev->watchdog_timeo = 5 * HZ;
13878 	i40e_set_ethtool_ops(netdev);
13879 
13880 	/* MTU range: 68 - 9706 */
13881 	netdev->min_mtu = ETH_MIN_MTU;
13882 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13883 
13884 	return 0;
13885 }
13886 
13887 /**
13888  * i40e_vsi_delete - Delete a VSI from the switch
13889  * @vsi: the VSI being removed
13890  *
13891  * Returns 0 on success, negative value on failure
13892  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13893 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13894 {
13895 	/* remove default VSI is not allowed */
13896 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13897 		return;
13898 
13899 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13900 }
13901 
13902 /**
13903  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13904  * @vsi: the VSI being queried
13905  *
13906  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13907  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13908 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13909 {
13910 	struct i40e_veb *veb;
13911 	struct i40e_pf *pf = vsi->back;
13912 
13913 	/* Uplink is not a bridge so default to VEB */
13914 	if (vsi->veb_idx >= I40E_MAX_VEB)
13915 		return 1;
13916 
13917 	veb = pf->veb[vsi->veb_idx];
13918 	if (!veb) {
13919 		dev_info(&pf->pdev->dev,
13920 			 "There is no veb associated with the bridge\n");
13921 		return -ENOENT;
13922 	}
13923 
13924 	/* Uplink is a bridge in VEPA mode */
13925 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13926 		return 0;
13927 	} else {
13928 		/* Uplink is a bridge in VEB mode */
13929 		return 1;
13930 	}
13931 
13932 	/* VEPA is now default bridge, so return 0 */
13933 	return 0;
13934 }
13935 
13936 /**
13937  * i40e_add_vsi - Add a VSI to the switch
13938  * @vsi: the VSI being configured
13939  *
13940  * This initializes a VSI context depending on the VSI type to be added and
13941  * passes it down to the add_vsi aq command.
13942  **/
i40e_add_vsi(struct i40e_vsi * vsi)13943 static int i40e_add_vsi(struct i40e_vsi *vsi)
13944 {
13945 	int ret = -ENODEV;
13946 	struct i40e_pf *pf = vsi->back;
13947 	struct i40e_hw *hw = &pf->hw;
13948 	struct i40e_vsi_context ctxt;
13949 	struct i40e_mac_filter *f;
13950 	struct hlist_node *h;
13951 	int bkt;
13952 
13953 	u8 enabled_tc = 0x1; /* TC0 enabled */
13954 	int f_count = 0;
13955 
13956 	memset(&ctxt, 0, sizeof(ctxt));
13957 	switch (vsi->type) {
13958 	case I40E_VSI_MAIN:
13959 		/* The PF's main VSI is already setup as part of the
13960 		 * device initialization, so we'll not bother with
13961 		 * the add_vsi call, but we will retrieve the current
13962 		 * VSI context.
13963 		 */
13964 		ctxt.seid = pf->main_vsi_seid;
13965 		ctxt.pf_num = pf->hw.pf_id;
13966 		ctxt.vf_num = 0;
13967 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13968 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13969 		if (ret) {
13970 			dev_info(&pf->pdev->dev,
13971 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13972 				 ERR_PTR(ret),
13973 				 i40e_aq_str(&pf->hw,
13974 					     pf->hw.aq.asq_last_status));
13975 			return -ENOENT;
13976 		}
13977 		vsi->info = ctxt.info;
13978 		vsi->info.valid_sections = 0;
13979 
13980 		vsi->seid = ctxt.seid;
13981 		vsi->id = ctxt.vsi_number;
13982 
13983 		enabled_tc = i40e_pf_get_tc_map(pf);
13984 
13985 		/* Source pruning is enabled by default, so the flag is
13986 		 * negative logic - if it's set, we need to fiddle with
13987 		 * the VSI to disable source pruning.
13988 		 */
13989 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13990 			memset(&ctxt, 0, sizeof(ctxt));
13991 			ctxt.seid = pf->main_vsi_seid;
13992 			ctxt.pf_num = pf->hw.pf_id;
13993 			ctxt.vf_num = 0;
13994 			ctxt.info.valid_sections |=
13995 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13996 			ctxt.info.switch_id =
13997 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13998 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13999 			if (ret) {
14000 				dev_info(&pf->pdev->dev,
14001 					 "update vsi failed, err %d aq_err %s\n",
14002 					 ret,
14003 					 i40e_aq_str(&pf->hw,
14004 						     pf->hw.aq.asq_last_status));
14005 				ret = -ENOENT;
14006 				goto err;
14007 			}
14008 		}
14009 
14010 		/* MFP mode setup queue map and update VSI */
14011 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
14012 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14013 			memset(&ctxt, 0, sizeof(ctxt));
14014 			ctxt.seid = pf->main_vsi_seid;
14015 			ctxt.pf_num = pf->hw.pf_id;
14016 			ctxt.vf_num = 0;
14017 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14018 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14019 			if (ret) {
14020 				dev_info(&pf->pdev->dev,
14021 					 "update vsi failed, err %pe aq_err %s\n",
14022 					 ERR_PTR(ret),
14023 					 i40e_aq_str(&pf->hw,
14024 						    pf->hw.aq.asq_last_status));
14025 				ret = -ENOENT;
14026 				goto err;
14027 			}
14028 			/* update the local VSI info queue map */
14029 			i40e_vsi_update_queue_map(vsi, &ctxt);
14030 			vsi->info.valid_sections = 0;
14031 		} else {
14032 			/* Default/Main VSI is only enabled for TC0
14033 			 * reconfigure it to enable all TCs that are
14034 			 * available on the port in SFP mode.
14035 			 * For MFP case the iSCSI PF would use this
14036 			 * flow to enable LAN+iSCSI TC.
14037 			 */
14038 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14039 			if (ret) {
14040 				/* Single TC condition is not fatal,
14041 				 * message and continue
14042 				 */
14043 				dev_info(&pf->pdev->dev,
14044 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14045 					 enabled_tc,
14046 					 ERR_PTR(ret),
14047 					 i40e_aq_str(&pf->hw,
14048 						    pf->hw.aq.asq_last_status));
14049 			}
14050 		}
14051 		break;
14052 
14053 	case I40E_VSI_FDIR:
14054 		ctxt.pf_num = hw->pf_id;
14055 		ctxt.vf_num = 0;
14056 		ctxt.uplink_seid = vsi->uplink_seid;
14057 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14058 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14059 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
14060 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14061 			ctxt.info.valid_sections |=
14062 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14063 			ctxt.info.switch_id =
14064 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14065 		}
14066 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14067 		break;
14068 
14069 	case I40E_VSI_VMDQ2:
14070 		ctxt.pf_num = hw->pf_id;
14071 		ctxt.vf_num = 0;
14072 		ctxt.uplink_seid = vsi->uplink_seid;
14073 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14074 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14075 
14076 		/* This VSI is connected to VEB so the switch_id
14077 		 * should be set to zero by default.
14078 		 */
14079 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14080 			ctxt.info.valid_sections |=
14081 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14082 			ctxt.info.switch_id =
14083 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14084 		}
14085 
14086 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14087 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14088 		break;
14089 
14090 	case I40E_VSI_SRIOV:
14091 		ctxt.pf_num = hw->pf_id;
14092 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14093 		ctxt.uplink_seid = vsi->uplink_seid;
14094 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14095 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14096 
14097 		/* This VSI is connected to VEB so the switch_id
14098 		 * should be set to zero by default.
14099 		 */
14100 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14101 			ctxt.info.valid_sections |=
14102 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14103 			ctxt.info.switch_id =
14104 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14105 		}
14106 
14107 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14108 			ctxt.info.valid_sections |=
14109 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14110 			ctxt.info.queueing_opt_flags |=
14111 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14112 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14113 		}
14114 
14115 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14116 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14117 		if (pf->vf[vsi->vf_id].spoofchk) {
14118 			ctxt.info.valid_sections |=
14119 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14120 			ctxt.info.sec_flags |=
14121 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14122 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14123 		}
14124 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14125 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14126 		break;
14127 
14128 	case I40E_VSI_IWARP:
14129 		/* send down message to iWARP */
14130 		break;
14131 
14132 	default:
14133 		return -ENODEV;
14134 	}
14135 
14136 	if (vsi->type != I40E_VSI_MAIN) {
14137 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14138 		if (ret) {
14139 			dev_info(&vsi->back->pdev->dev,
14140 				 "add vsi failed, err %pe aq_err %s\n",
14141 				 ERR_PTR(ret),
14142 				 i40e_aq_str(&pf->hw,
14143 					     pf->hw.aq.asq_last_status));
14144 			ret = -ENOENT;
14145 			goto err;
14146 		}
14147 		vsi->info = ctxt.info;
14148 		vsi->info.valid_sections = 0;
14149 		vsi->seid = ctxt.seid;
14150 		vsi->id = ctxt.vsi_number;
14151 	}
14152 
14153 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14154 	vsi->active_filters = 0;
14155 	/* If macvlan filters already exist, force them to get loaded */
14156 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14157 		f->state = I40E_FILTER_NEW;
14158 		f_count++;
14159 	}
14160 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14161 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14162 
14163 	if (f_count) {
14164 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14165 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14166 	}
14167 
14168 	/* Update VSI BW information */
14169 	ret = i40e_vsi_get_bw_info(vsi);
14170 	if (ret) {
14171 		dev_info(&pf->pdev->dev,
14172 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14173 			 ERR_PTR(ret),
14174 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14175 		/* VSI is already added so not tearing that up */
14176 		ret = 0;
14177 	}
14178 
14179 err:
14180 	return ret;
14181 }
14182 
14183 /**
14184  * i40e_vsi_release - Delete a VSI and free its resources
14185  * @vsi: the VSI being removed
14186  *
14187  * Returns 0 on success or < 0 on error
14188  **/
i40e_vsi_release(struct i40e_vsi * vsi)14189 int i40e_vsi_release(struct i40e_vsi *vsi)
14190 {
14191 	struct i40e_mac_filter *f;
14192 	struct hlist_node *h;
14193 	struct i40e_veb *veb;
14194 	struct i40e_pf *pf;
14195 	u16 uplink_seid;
14196 	int i, n, bkt;
14197 
14198 	pf = vsi->back;
14199 
14200 	/* release of a VEB-owner or last VSI is not allowed */
14201 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14202 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14203 			 vsi->seid, vsi->uplink_seid);
14204 		return -ENODEV;
14205 	}
14206 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14207 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14208 		return -ENODEV;
14209 	}
14210 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14211 	uplink_seid = vsi->uplink_seid;
14212 
14213 	if (vsi->type != I40E_VSI_SRIOV) {
14214 		if (vsi->netdev_registered) {
14215 			vsi->netdev_registered = false;
14216 			if (vsi->netdev) {
14217 				/* results in a call to i40e_close() */
14218 				unregister_netdev(vsi->netdev);
14219 			}
14220 		} else {
14221 			i40e_vsi_close(vsi);
14222 		}
14223 		i40e_vsi_disable_irq(vsi);
14224 	}
14225 
14226 	if (vsi->type == I40E_VSI_MAIN)
14227 		i40e_devlink_destroy_port(pf);
14228 
14229 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14230 
14231 	/* clear the sync flag on all filters */
14232 	if (vsi->netdev) {
14233 		__dev_uc_unsync(vsi->netdev, NULL);
14234 		__dev_mc_unsync(vsi->netdev, NULL);
14235 	}
14236 
14237 	/* make sure any remaining filters are marked for deletion */
14238 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14239 		__i40e_del_filter(vsi, f);
14240 
14241 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14242 
14243 	i40e_sync_vsi_filters(vsi);
14244 
14245 	i40e_vsi_delete(vsi);
14246 	i40e_vsi_free_q_vectors(vsi);
14247 	if (vsi->netdev) {
14248 		free_netdev(vsi->netdev);
14249 		vsi->netdev = NULL;
14250 	}
14251 	i40e_vsi_clear_rings(vsi);
14252 	i40e_vsi_clear(vsi);
14253 
14254 	/* If this was the last thing on the VEB, except for the
14255 	 * controlling VSI, remove the VEB, which puts the controlling
14256 	 * VSI onto the uplink port.
14257 	 *
14258 	 * Well, okay, there's one more exception here: don't remove
14259 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14260 	 * from up the network stack.
14261 	 */
14262 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14263 	if (veb && veb->uplink_seid) {
14264 		n = 0;
14265 
14266 		/* Count non-controlling VSIs present on  the VEB */
14267 		i40e_pf_for_each_vsi(pf, i, vsi)
14268 			if (vsi->uplink_seid == uplink_seid &&
14269 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14270 				n++;
14271 
14272 		/* If there is no VSI except the control one then release
14273 		 * the VEB and put the control VSI onto VEB uplink.
14274 		 */
14275 		if (!n)
14276 			i40e_veb_release(veb);
14277 	}
14278 
14279 	return 0;
14280 }
14281 
14282 /**
14283  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14284  * @vsi: ptr to the VSI
14285  *
14286  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14287  * corresponding SW VSI structure and initializes num_queue_pairs for the
14288  * newly allocated VSI.
14289  *
14290  * Returns 0 on success or negative on failure
14291  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14292 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14293 {
14294 	int ret = -ENOENT;
14295 	struct i40e_pf *pf = vsi->back;
14296 
14297 	if (vsi->q_vectors[0]) {
14298 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14299 			 vsi->seid);
14300 		return -EEXIST;
14301 	}
14302 
14303 	if (vsi->base_vector) {
14304 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14305 			 vsi->seid, vsi->base_vector);
14306 		return -EEXIST;
14307 	}
14308 
14309 	ret = i40e_vsi_alloc_q_vectors(vsi);
14310 	if (ret) {
14311 		dev_info(&pf->pdev->dev,
14312 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14313 			 vsi->num_q_vectors, vsi->seid, ret);
14314 		vsi->num_q_vectors = 0;
14315 		goto vector_setup_out;
14316 	}
14317 
14318 	/* In Legacy mode, we do not have to get any other vector since we
14319 	 * piggyback on the misc/ICR0 for queue interrupts.
14320 	*/
14321 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14322 		return ret;
14323 	if (vsi->num_q_vectors)
14324 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14325 						 vsi->num_q_vectors, vsi->idx);
14326 	if (vsi->base_vector < 0) {
14327 		dev_info(&pf->pdev->dev,
14328 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14329 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14330 		i40e_vsi_free_q_vectors(vsi);
14331 		ret = -ENOENT;
14332 		goto vector_setup_out;
14333 	}
14334 
14335 vector_setup_out:
14336 	return ret;
14337 }
14338 
14339 /**
14340  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14341  * @vsi: pointer to the vsi.
14342  *
14343  * This re-allocates a vsi's queue resources.
14344  *
14345  * Returns pointer to the successfully allocated and configured VSI sw struct
14346  * on success, otherwise returns NULL on failure.
14347  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14348 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14349 {
14350 	struct i40e_vsi *main_vsi;
14351 	u16 alloc_queue_pairs;
14352 	struct i40e_pf *pf;
14353 	int ret;
14354 
14355 	if (!vsi)
14356 		return NULL;
14357 
14358 	pf = vsi->back;
14359 
14360 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14361 	i40e_vsi_clear_rings(vsi);
14362 
14363 	i40e_vsi_free_arrays(vsi, false);
14364 	i40e_set_num_rings_in_vsi(vsi);
14365 	ret = i40e_vsi_alloc_arrays(vsi, false);
14366 	if (ret)
14367 		goto err_vsi;
14368 
14369 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14370 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14371 
14372 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14373 	if (ret < 0) {
14374 		dev_info(&pf->pdev->dev,
14375 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14376 			 alloc_queue_pairs, vsi->seid, ret);
14377 		goto err_vsi;
14378 	}
14379 	vsi->base_queue = ret;
14380 
14381 	/* Update the FW view of the VSI. Force a reset of TC and queue
14382 	 * layout configurations.
14383 	 */
14384 	main_vsi = i40e_pf_get_main_vsi(pf);
14385 	main_vsi->seid = pf->main_vsi_seid;
14386 	i40e_vsi_reconfig_tc(main_vsi);
14387 
14388 	if (vsi->type == I40E_VSI_MAIN)
14389 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14390 
14391 	/* assign it some queues */
14392 	ret = i40e_alloc_rings(vsi);
14393 	if (ret)
14394 		goto err_rings;
14395 
14396 	/* map all of the rings to the q_vectors */
14397 	i40e_vsi_map_rings_to_vectors(vsi);
14398 	return vsi;
14399 
14400 err_rings:
14401 	i40e_vsi_free_q_vectors(vsi);
14402 	if (vsi->netdev_registered) {
14403 		vsi->netdev_registered = false;
14404 		unregister_netdev(vsi->netdev);
14405 		free_netdev(vsi->netdev);
14406 		vsi->netdev = NULL;
14407 	}
14408 	if (vsi->type == I40E_VSI_MAIN)
14409 		i40e_devlink_destroy_port(pf);
14410 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14411 err_vsi:
14412 	i40e_vsi_clear(vsi);
14413 	return NULL;
14414 }
14415 
14416 /**
14417  * i40e_vsi_setup - Set up a VSI by a given type
14418  * @pf: board private structure
14419  * @type: VSI type
14420  * @uplink_seid: the switch element to link to
14421  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14422  *
14423  * This allocates the sw VSI structure and its queue resources, then add a VSI
14424  * to the identified VEB.
14425  *
14426  * Returns pointer to the successfully allocated and configure VSI sw struct on
14427  * success, otherwise returns NULL on failure.
14428  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14429 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14430 				u16 uplink_seid, u32 param1)
14431 {
14432 	struct i40e_vsi *vsi = NULL;
14433 	struct i40e_veb *veb = NULL;
14434 	u16 alloc_queue_pairs;
14435 	int v_idx;
14436 	int ret;
14437 
14438 	/* The requested uplink_seid must be either
14439 	 *     - the PF's port seid
14440 	 *              no VEB is needed because this is the PF
14441 	 *              or this is a Flow Director special case VSI
14442 	 *     - seid of an existing VEB
14443 	 *     - seid of a VSI that owns an existing VEB
14444 	 *     - seid of a VSI that doesn't own a VEB
14445 	 *              a new VEB is created and the VSI becomes the owner
14446 	 *     - seid of the PF VSI, which is what creates the first VEB
14447 	 *              this is a special case of the previous
14448 	 *
14449 	 * Find which uplink_seid we were given and create a new VEB if needed
14450 	 */
14451 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14452 	if (!veb && uplink_seid != pf->mac_seid) {
14453 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14454 		if (!vsi) {
14455 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14456 				 uplink_seid);
14457 			return NULL;
14458 		}
14459 
14460 		if (vsi->uplink_seid == pf->mac_seid)
14461 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14462 					     vsi->tc_config.enabled_tc);
14463 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14464 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14465 					     vsi->tc_config.enabled_tc);
14466 		if (veb) {
14467 			if (vsi->type != I40E_VSI_MAIN) {
14468 				dev_info(&vsi->back->pdev->dev,
14469 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14470 				return NULL;
14471 			}
14472 			/* We come up by default in VEPA mode if SRIOV is not
14473 			 * already enabled, in which case we can't force VEPA
14474 			 * mode.
14475 			 */
14476 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14477 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14478 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14479 			}
14480 			i40e_config_bridge_mode(veb);
14481 		}
14482 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14483 		if (!veb) {
14484 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14485 			return NULL;
14486 		}
14487 
14488 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14489 		uplink_seid = veb->seid;
14490 	}
14491 
14492 	/* get vsi sw struct */
14493 	v_idx = i40e_vsi_mem_alloc(pf, type);
14494 	if (v_idx < 0)
14495 		goto err_alloc;
14496 	vsi = pf->vsi[v_idx];
14497 	if (!vsi)
14498 		goto err_alloc;
14499 	vsi->type = type;
14500 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14501 
14502 	if (type == I40E_VSI_MAIN)
14503 		pf->lan_vsi = v_idx;
14504 	else if (type == I40E_VSI_SRIOV)
14505 		vsi->vf_id = param1;
14506 	/* assign it some queues */
14507 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14508 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14509 
14510 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14511 	if (ret < 0) {
14512 		dev_info(&pf->pdev->dev,
14513 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14514 			 alloc_queue_pairs, vsi->seid, ret);
14515 		goto err_vsi;
14516 	}
14517 	vsi->base_queue = ret;
14518 
14519 	/* get a VSI from the hardware */
14520 	vsi->uplink_seid = uplink_seid;
14521 	ret = i40e_add_vsi(vsi);
14522 	if (ret)
14523 		goto err_vsi;
14524 
14525 	switch (vsi->type) {
14526 	/* setup the netdev if needed */
14527 	case I40E_VSI_MAIN:
14528 	case I40E_VSI_VMDQ2:
14529 		ret = i40e_config_netdev(vsi);
14530 		if (ret)
14531 			goto err_netdev;
14532 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14533 		if (ret)
14534 			goto err_netdev;
14535 		if (vsi->type == I40E_VSI_MAIN) {
14536 			ret = i40e_devlink_create_port(pf);
14537 			if (ret)
14538 				goto err_netdev;
14539 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14540 		}
14541 		ret = register_netdev(vsi->netdev);
14542 		if (ret)
14543 			goto err_dl_port;
14544 		vsi->netdev_registered = true;
14545 		netif_carrier_off(vsi->netdev);
14546 #ifdef CONFIG_I40E_DCB
14547 		/* Setup DCB netlink interface */
14548 		i40e_dcbnl_setup(vsi);
14549 #endif /* CONFIG_I40E_DCB */
14550 		fallthrough;
14551 	case I40E_VSI_FDIR:
14552 		/* set up vectors and rings if needed */
14553 		ret = i40e_vsi_setup_vectors(vsi);
14554 		if (ret)
14555 			goto err_msix;
14556 
14557 		ret = i40e_alloc_rings(vsi);
14558 		if (ret)
14559 			goto err_rings;
14560 
14561 		/* map all of the rings to the q_vectors */
14562 		i40e_vsi_map_rings_to_vectors(vsi);
14563 
14564 		i40e_vsi_reset_stats(vsi);
14565 		break;
14566 	default:
14567 		/* no netdev or rings for the other VSI types */
14568 		break;
14569 	}
14570 
14571 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14572 	    vsi->type == I40E_VSI_VMDQ2) {
14573 		ret = i40e_vsi_config_rss(vsi);
14574 		if (ret)
14575 			goto err_config;
14576 	}
14577 	return vsi;
14578 
14579 err_config:
14580 	i40e_vsi_clear_rings(vsi);
14581 err_rings:
14582 	i40e_vsi_free_q_vectors(vsi);
14583 err_msix:
14584 	if (vsi->netdev_registered) {
14585 		vsi->netdev_registered = false;
14586 		unregister_netdev(vsi->netdev);
14587 		free_netdev(vsi->netdev);
14588 		vsi->netdev = NULL;
14589 	}
14590 err_dl_port:
14591 	if (vsi->type == I40E_VSI_MAIN)
14592 		i40e_devlink_destroy_port(pf);
14593 err_netdev:
14594 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14595 err_vsi:
14596 	i40e_vsi_clear(vsi);
14597 err_alloc:
14598 	return NULL;
14599 }
14600 
14601 /**
14602  * i40e_veb_get_bw_info - Query VEB BW information
14603  * @veb: the veb to query
14604  *
14605  * Query the Tx scheduler BW configuration data for given VEB
14606  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14607 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14608 {
14609 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14610 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14611 	struct i40e_pf *pf = veb->pf;
14612 	struct i40e_hw *hw = &pf->hw;
14613 	u32 tc_bw_max;
14614 	int ret = 0;
14615 	int i;
14616 
14617 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14618 						  &bw_data, NULL);
14619 	if (ret) {
14620 		dev_info(&pf->pdev->dev,
14621 			 "query veb bw config failed, err %pe aq_err %s\n",
14622 			 ERR_PTR(ret),
14623 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14624 		goto out;
14625 	}
14626 
14627 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14628 						   &ets_data, NULL);
14629 	if (ret) {
14630 		dev_info(&pf->pdev->dev,
14631 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14632 			 ERR_PTR(ret),
14633 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14634 		goto out;
14635 	}
14636 
14637 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14638 	veb->bw_max_quanta = ets_data.tc_bw_max;
14639 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14640 	veb->enabled_tc = ets_data.tc_valid_bits;
14641 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14642 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14643 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14644 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14645 		veb->bw_tc_limit_credits[i] =
14646 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14647 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14648 	}
14649 
14650 out:
14651 	return ret;
14652 }
14653 
14654 /**
14655  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14656  * @pf: board private structure
14657  *
14658  * On error: returns error code (negative)
14659  * On success: returns vsi index in PF (positive)
14660  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14661 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14662 {
14663 	int ret = -ENOENT;
14664 	struct i40e_veb *veb;
14665 	int i;
14666 
14667 	/* Need to protect the allocation of switch elements at the PF level */
14668 	mutex_lock(&pf->switch_mutex);
14669 
14670 	/* VEB list may be fragmented if VEB creation/destruction has
14671 	 * been happening.  We can afford to do a quick scan to look
14672 	 * for any free slots in the list.
14673 	 *
14674 	 * find next empty veb slot, looping back around if necessary
14675 	 */
14676 	i = 0;
14677 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14678 		i++;
14679 	if (i >= I40E_MAX_VEB) {
14680 		ret = -ENOMEM;
14681 		goto err_alloc_veb;  /* out of VEB slots! */
14682 	}
14683 
14684 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14685 	if (!veb) {
14686 		ret = -ENOMEM;
14687 		goto err_alloc_veb;
14688 	}
14689 	veb->pf = pf;
14690 	veb->idx = i;
14691 	veb->enabled_tc = 1;
14692 
14693 	pf->veb[i] = veb;
14694 	ret = i;
14695 err_alloc_veb:
14696 	mutex_unlock(&pf->switch_mutex);
14697 	return ret;
14698 }
14699 
14700 /**
14701  * i40e_switch_branch_release - Delete a branch of the switch tree
14702  * @branch: where to start deleting
14703  *
14704  * This uses recursion to find the tips of the branch to be
14705  * removed, deleting until we get back to and can delete this VEB.
14706  **/
i40e_switch_branch_release(struct i40e_veb * branch)14707 static void i40e_switch_branch_release(struct i40e_veb *branch)
14708 {
14709 	struct i40e_pf *pf = branch->pf;
14710 	u16 branch_seid = branch->seid;
14711 	u16 veb_idx = branch->idx;
14712 	struct i40e_vsi *vsi;
14713 	struct i40e_veb *veb;
14714 	int i;
14715 
14716 	/* release any VEBs on this VEB - RECURSION */
14717 	i40e_pf_for_each_veb(pf, i, veb)
14718 		if (veb->uplink_seid == branch->seid)
14719 			i40e_switch_branch_release(veb);
14720 
14721 	/* Release the VSIs on this VEB, but not the owner VSI.
14722 	 *
14723 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14724 	 *       the VEB itself, so don't use (*branch) after this loop.
14725 	 */
14726 	i40e_pf_for_each_vsi(pf, i, vsi)
14727 		if (vsi->uplink_seid == branch_seid &&
14728 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14729 			i40e_vsi_release(vsi);
14730 
14731 	/* There's one corner case where the VEB might not have been
14732 	 * removed, so double check it here and remove it if needed.
14733 	 * This case happens if the veb was created from the debugfs
14734 	 * commands and no VSIs were added to it.
14735 	 */
14736 	if (pf->veb[veb_idx])
14737 		i40e_veb_release(pf->veb[veb_idx]);
14738 }
14739 
14740 /**
14741  * i40e_veb_clear - remove veb struct
14742  * @veb: the veb to remove
14743  **/
i40e_veb_clear(struct i40e_veb * veb)14744 static void i40e_veb_clear(struct i40e_veb *veb)
14745 {
14746 	if (!veb)
14747 		return;
14748 
14749 	if (veb->pf) {
14750 		struct i40e_pf *pf = veb->pf;
14751 
14752 		mutex_lock(&pf->switch_mutex);
14753 		if (pf->veb[veb->idx] == veb)
14754 			pf->veb[veb->idx] = NULL;
14755 		mutex_unlock(&pf->switch_mutex);
14756 	}
14757 
14758 	kfree(veb);
14759 }
14760 
14761 /**
14762  * i40e_veb_release - Delete a VEB and free its resources
14763  * @veb: the VEB being removed
14764  **/
i40e_veb_release(struct i40e_veb * veb)14765 void i40e_veb_release(struct i40e_veb *veb)
14766 {
14767 	struct i40e_vsi *vsi, *vsi_it;
14768 	struct i40e_pf *pf;
14769 	int i, n = 0;
14770 
14771 	pf = veb->pf;
14772 
14773 	/* find the remaining VSI and check for extras */
14774 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14775 		if (vsi_it->uplink_seid == veb->seid) {
14776 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14777 				vsi = vsi_it;
14778 			n++;
14779 		}
14780 
14781 	/* Floating VEB has to be empty and regular one must have
14782 	 * single owner VSI.
14783 	 */
14784 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14785 		dev_info(&pf->pdev->dev,
14786 			 "can't remove VEB %d with %d VSIs left\n",
14787 			 veb->seid, n);
14788 		return;
14789 	}
14790 
14791 	/* For regular VEB move the owner VSI to uplink port */
14792 	if (veb->uplink_seid) {
14793 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14794 		vsi->uplink_seid = veb->uplink_seid;
14795 		vsi->veb_idx = I40E_NO_VEB;
14796 	}
14797 
14798 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14799 	i40e_veb_clear(veb);
14800 }
14801 
14802 /**
14803  * i40e_add_veb - create the VEB in the switch
14804  * @veb: the VEB to be instantiated
14805  * @vsi: the controlling VSI
14806  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14807 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14808 {
14809 	struct i40e_pf *pf = veb->pf;
14810 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14811 	int ret;
14812 
14813 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14814 			      veb->enabled_tc, vsi ? false : true,
14815 			      &veb->seid, enable_stats, NULL);
14816 
14817 	/* get a VEB from the hardware */
14818 	if (ret) {
14819 		dev_info(&pf->pdev->dev,
14820 			 "couldn't add VEB, err %pe aq_err %s\n",
14821 			 ERR_PTR(ret),
14822 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14823 		return -EPERM;
14824 	}
14825 
14826 	/* get statistics counter */
14827 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14828 					 &veb->stats_idx, NULL, NULL, NULL);
14829 	if (ret) {
14830 		dev_info(&pf->pdev->dev,
14831 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14832 			 ERR_PTR(ret),
14833 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14834 		return -EPERM;
14835 	}
14836 	ret = i40e_veb_get_bw_info(veb);
14837 	if (ret) {
14838 		dev_info(&pf->pdev->dev,
14839 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14840 			 ERR_PTR(ret),
14841 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14842 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14843 		return -ENOENT;
14844 	}
14845 
14846 	if (vsi) {
14847 		vsi->uplink_seid = veb->seid;
14848 		vsi->veb_idx = veb->idx;
14849 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14850 	}
14851 
14852 	return 0;
14853 }
14854 
14855 /**
14856  * i40e_veb_setup - Set up a VEB
14857  * @pf: board private structure
14858  * @uplink_seid: the switch element to link to
14859  * @vsi_seid: the initial VSI seid
14860  * @enabled_tc: Enabled TC bit-map
14861  *
14862  * This allocates the sw VEB structure and links it into the switch
14863  * It is possible and legal for this to be a duplicate of an already
14864  * existing VEB.  It is also possible for both uplink and vsi seids
14865  * to be zero, in order to create a floating VEB.
14866  *
14867  * Returns pointer to the successfully allocated VEB sw struct on
14868  * success, otherwise returns NULL on failure.
14869  **/
i40e_veb_setup(struct i40e_pf * pf,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14870 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14871 				u16 vsi_seid, u8 enabled_tc)
14872 {
14873 	struct i40e_vsi *vsi = NULL;
14874 	struct i40e_veb *veb;
14875 	int veb_idx;
14876 	int ret;
14877 
14878 	/* if one seid is 0, the other must be 0 to create a floating relay */
14879 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14880 	    (uplink_seid + vsi_seid != 0)) {
14881 		dev_info(&pf->pdev->dev,
14882 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14883 			 uplink_seid, vsi_seid);
14884 		return NULL;
14885 	}
14886 
14887 	/* make sure there is such a vsi and uplink */
14888 	if (vsi_seid) {
14889 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14890 		if (!vsi) {
14891 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14892 				vsi_seid);
14893 			return NULL;
14894 		}
14895 	}
14896 
14897 	/* get veb sw struct */
14898 	veb_idx = i40e_veb_mem_alloc(pf);
14899 	if (veb_idx < 0)
14900 		goto err_alloc;
14901 	veb = pf->veb[veb_idx];
14902 	veb->uplink_seid = uplink_seid;
14903 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14904 
14905 	/* create the VEB in the switch */
14906 	ret = i40e_add_veb(veb, vsi);
14907 	if (ret)
14908 		goto err_veb;
14909 
14910 	if (vsi && vsi->idx == pf->lan_vsi)
14911 		pf->lan_veb = veb->idx;
14912 
14913 	return veb;
14914 
14915 err_veb:
14916 	i40e_veb_clear(veb);
14917 err_alloc:
14918 	return NULL;
14919 }
14920 
14921 /**
14922  * i40e_setup_pf_switch_element - set PF vars based on switch type
14923  * @pf: board private structure
14924  * @ele: element we are building info from
14925  * @num_reported: total number of elements
14926  * @printconfig: should we print the contents
14927  *
14928  * helper function to assist in extracting a few useful SEID values.
14929  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14930 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14931 				struct i40e_aqc_switch_config_element_resp *ele,
14932 				u16 num_reported, bool printconfig)
14933 {
14934 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14935 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14936 	u8 element_type = ele->element_type;
14937 	u16 seid = le16_to_cpu(ele->seid);
14938 	struct i40e_veb *veb;
14939 
14940 	if (printconfig)
14941 		dev_info(&pf->pdev->dev,
14942 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14943 			 element_type, seid, uplink_seid, downlink_seid);
14944 
14945 	switch (element_type) {
14946 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14947 		pf->mac_seid = seid;
14948 		break;
14949 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14950 		/* Main VEB? */
14951 		if (uplink_seid != pf->mac_seid)
14952 			break;
14953 		veb = i40e_pf_get_main_veb(pf);
14954 		if (!veb) {
14955 			int v;
14956 
14957 			/* find existing or else empty VEB */
14958 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14959 			if (veb) {
14960 				pf->lan_veb = veb->idx;
14961 			} else {
14962 				v = i40e_veb_mem_alloc(pf);
14963 				if (v < 0)
14964 					break;
14965 				pf->lan_veb = v;
14966 			}
14967 		}
14968 
14969 		/* Try to get again main VEB as pf->lan_veb may have changed */
14970 		veb = i40e_pf_get_main_veb(pf);
14971 		if (!veb)
14972 			break;
14973 
14974 		veb->seid = seid;
14975 		veb->uplink_seid = pf->mac_seid;
14976 		veb->pf = pf;
14977 		break;
14978 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14979 		if (num_reported != 1)
14980 			break;
14981 		/* This is immediately after a reset so we can assume this is
14982 		 * the PF's VSI
14983 		 */
14984 		pf->mac_seid = uplink_seid;
14985 		pf->main_vsi_seid = seid;
14986 		if (printconfig)
14987 			dev_info(&pf->pdev->dev,
14988 				 "pf_seid=%d main_vsi_seid=%d\n",
14989 				 downlink_seid, pf->main_vsi_seid);
14990 		break;
14991 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14992 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14993 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14994 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14995 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14996 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14997 		/* ignore these for now */
14998 		break;
14999 	default:
15000 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15001 			 element_type, seid);
15002 		break;
15003 	}
15004 }
15005 
15006 /**
15007  * i40e_fetch_switch_configuration - Get switch config from firmware
15008  * @pf: board private structure
15009  * @printconfig: should we print the contents
15010  *
15011  * Get the current switch configuration from the device and
15012  * extract a few useful SEID values.
15013  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)15014 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15015 {
15016 	struct i40e_aqc_get_switch_config_resp *sw_config;
15017 	u16 next_seid = 0;
15018 	int ret = 0;
15019 	u8 *aq_buf;
15020 	int i;
15021 
15022 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15023 	if (!aq_buf)
15024 		return -ENOMEM;
15025 
15026 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15027 	do {
15028 		u16 num_reported, num_total;
15029 
15030 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15031 						I40E_AQ_LARGE_BUF,
15032 						&next_seid, NULL);
15033 		if (ret) {
15034 			dev_info(&pf->pdev->dev,
15035 				 "get switch config failed err %d aq_err %s\n",
15036 				 ret,
15037 				 i40e_aq_str(&pf->hw,
15038 					     pf->hw.aq.asq_last_status));
15039 			kfree(aq_buf);
15040 			return -ENOENT;
15041 		}
15042 
15043 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15044 		num_total = le16_to_cpu(sw_config->header.num_total);
15045 
15046 		if (printconfig)
15047 			dev_info(&pf->pdev->dev,
15048 				 "header: %d reported %d total\n",
15049 				 num_reported, num_total);
15050 
15051 		for (i = 0; i < num_reported; i++) {
15052 			struct i40e_aqc_switch_config_element_resp *ele =
15053 				&sw_config->element[i];
15054 
15055 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15056 						     printconfig);
15057 		}
15058 	} while (next_seid != 0);
15059 
15060 	kfree(aq_buf);
15061 	return ret;
15062 }
15063 
15064 /**
15065  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15066  * @pf: board private structure
15067  * @reinit: if the Main VSI needs to re-initialized.
15068  * @lock_acquired: indicates whether or not the lock has been acquired
15069  *
15070  * Returns 0 on success, negative value on failure
15071  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)15072 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15073 {
15074 	struct i40e_vsi *main_vsi;
15075 	u16 flags = 0;
15076 	int ret;
15077 
15078 	/* find out what's out there already */
15079 	ret = i40e_fetch_switch_configuration(pf, false);
15080 	if (ret) {
15081 		dev_info(&pf->pdev->dev,
15082 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15083 			 ERR_PTR(ret),
15084 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15085 		return ret;
15086 	}
15087 	i40e_pf_reset_stats(pf);
15088 
15089 	/* set the switch config bit for the whole device to
15090 	 * support limited promisc or true promisc
15091 	 * when user requests promisc. The default is limited
15092 	 * promisc.
15093 	*/
15094 
15095 	if ((pf->hw.pf_id == 0) &&
15096 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15097 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15098 		pf->last_sw_conf_flags = flags;
15099 	}
15100 
15101 	if (pf->hw.pf_id == 0) {
15102 		u16 valid_flags;
15103 
15104 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15105 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15106 						NULL);
15107 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15108 			dev_info(&pf->pdev->dev,
15109 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15110 				 ERR_PTR(ret),
15111 				 i40e_aq_str(&pf->hw,
15112 					     pf->hw.aq.asq_last_status));
15113 			/* not a fatal problem, just keep going */
15114 		}
15115 		pf->last_sw_conf_valid_flags = valid_flags;
15116 	}
15117 
15118 	/* first time setup */
15119 	main_vsi = i40e_pf_get_main_vsi(pf);
15120 	if (!main_vsi || reinit) {
15121 		struct i40e_veb *veb;
15122 		u16 uplink_seid;
15123 
15124 		/* Set up the PF VSI associated with the PF's main VSI
15125 		 * that is already in the HW switch
15126 		 */
15127 		veb = i40e_pf_get_main_veb(pf);
15128 		if (veb)
15129 			uplink_seid = veb->seid;
15130 		else
15131 			uplink_seid = pf->mac_seid;
15132 		if (!main_vsi)
15133 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15134 						  uplink_seid, 0);
15135 		else if (reinit)
15136 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15137 		if (!main_vsi) {
15138 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15139 			i40e_cloud_filter_exit(pf);
15140 			i40e_fdir_teardown(pf);
15141 			return -EAGAIN;
15142 		}
15143 	} else {
15144 		/* force a reset of TC and queue layout configurations */
15145 		main_vsi->seid = pf->main_vsi_seid;
15146 		i40e_vsi_reconfig_tc(main_vsi);
15147 	}
15148 	i40e_vlan_stripping_disable(main_vsi);
15149 
15150 	i40e_fdir_sb_setup(pf);
15151 
15152 	/* Setup static PF queue filter control settings */
15153 	ret = i40e_setup_pf_filter_control(pf);
15154 	if (ret) {
15155 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15156 			 ret);
15157 		/* Failure here should not stop continuing other steps */
15158 	}
15159 
15160 	/* enable RSS in the HW, even for only one queue, as the stack can use
15161 	 * the hash
15162 	 */
15163 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15164 		i40e_pf_config_rss(pf);
15165 
15166 	/* fill in link information and enable LSE reporting */
15167 	i40e_link_event(pf);
15168 
15169 	i40e_ptp_init(pf);
15170 
15171 	if (!lock_acquired)
15172 		rtnl_lock();
15173 
15174 	/* repopulate tunnel port filters */
15175 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15176 
15177 	if (!lock_acquired)
15178 		rtnl_unlock();
15179 
15180 	return ret;
15181 }
15182 
15183 /**
15184  * i40e_determine_queue_usage - Work out queue distribution
15185  * @pf: board private structure
15186  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15187 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15188 {
15189 	int queues_left;
15190 	int q_max;
15191 
15192 	pf->num_lan_qps = 0;
15193 
15194 	/* Find the max queues to be put into basic use.  We'll always be
15195 	 * using TC0, whether or not DCB is running, and TC0 will get the
15196 	 * big RSS set.
15197 	 */
15198 	queues_left = pf->hw.func_caps.num_tx_qp;
15199 
15200 	if ((queues_left == 1) ||
15201 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15202 		/* one qp for PF, no queues for anything else */
15203 		queues_left = 0;
15204 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15205 
15206 		/* make sure all the fancies are disabled */
15207 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15208 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15209 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15210 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15211 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15212 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15213 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15214 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15215 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15216 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15217 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15218 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15219 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15220 		/* one qp for PF */
15221 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15222 		queues_left -= pf->num_lan_qps;
15223 
15224 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15225 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15226 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15227 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15228 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15229 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15230 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15231 	} else {
15232 		/* Not enough queues for all TCs */
15233 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15234 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15235 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15236 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15237 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15238 		}
15239 
15240 		/* limit lan qps to the smaller of qps, cpus or msix */
15241 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15242 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15243 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15244 		pf->num_lan_qps = q_max;
15245 
15246 		queues_left -= pf->num_lan_qps;
15247 	}
15248 
15249 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15250 		if (queues_left > 1) {
15251 			queues_left -= 1; /* save 1 queue for FD */
15252 		} else {
15253 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15254 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15255 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15256 		}
15257 	}
15258 
15259 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15260 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15261 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15262 					(queues_left / pf->num_vf_qps));
15263 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15264 	}
15265 
15266 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15267 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15268 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15269 					  (queues_left / pf->num_vmdq_qps));
15270 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15271 	}
15272 
15273 	pf->queues_left = queues_left;
15274 	dev_dbg(&pf->pdev->dev,
15275 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15276 		pf->hw.func_caps.num_tx_qp,
15277 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15278 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15279 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15280 		queues_left);
15281 }
15282 
15283 /**
15284  * i40e_setup_pf_filter_control - Setup PF static filter control
15285  * @pf: PF to be setup
15286  *
15287  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15288  * settings. If PE/FCoE are enabled then it will also set the per PF
15289  * based filter sizes required for them. It also enables Flow director,
15290  * ethertype and macvlan type filter settings for the pf.
15291  *
15292  * Returns 0 on success, negative on failure
15293  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15294 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15295 {
15296 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15297 
15298 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15299 
15300 	/* Flow Director is enabled */
15301 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15302 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15303 		settings->enable_fdir = true;
15304 
15305 	/* Ethtype and MACVLAN filters enabled for PF */
15306 	settings->enable_ethtype = true;
15307 	settings->enable_macvlan = true;
15308 
15309 	if (i40e_set_filter_control(&pf->hw, settings))
15310 		return -ENOENT;
15311 
15312 	return 0;
15313 }
15314 
15315 #define INFO_STRING_LEN 255
15316 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15317 static void i40e_print_features(struct i40e_pf *pf)
15318 {
15319 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15320 	struct i40e_hw *hw = &pf->hw;
15321 	char *buf;
15322 	int i;
15323 
15324 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15325 	if (!buf)
15326 		return;
15327 
15328 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15329 #ifdef CONFIG_PCI_IOV
15330 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15331 #endif
15332 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15333 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15334 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15335 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15336 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15337 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15338 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15339 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15340 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15341 	}
15342 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15343 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15344 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15345 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15346 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15347 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15348 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15349 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15350 	else
15351 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15352 
15353 	dev_info(&pf->pdev->dev, "%s\n", buf);
15354 	kfree(buf);
15355 	WARN_ON(i > INFO_STRING_LEN);
15356 }
15357 
15358 /**
15359  * i40e_get_platform_mac_addr - get platform-specific MAC address
15360  * @pdev: PCI device information struct
15361  * @pf: board private structure
15362  *
15363  * Look up the MAC address for the device. First we'll try
15364  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15365  * specific fallback. Otherwise, we'll default to the stored value in
15366  * firmware.
15367  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15368 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15369 {
15370 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15371 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15372 }
15373 
15374 /**
15375  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15376  * @fec_cfg: FEC option to set in flags
15377  * @flags: ptr to flags in which we set FEC option
15378  **/
i40e_set_fec_in_flags(u8 fec_cfg,unsigned long * flags)15379 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15380 {
15381 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15382 		set_bit(I40E_FLAG_RS_FEC, flags);
15383 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15384 	}
15385 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15386 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15387 		set_bit(I40E_FLAG_RS_FEC, flags);
15388 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15389 	}
15390 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15391 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15392 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15393 		clear_bit(I40E_FLAG_RS_FEC, flags);
15394 	}
15395 	if (fec_cfg == 0) {
15396 		clear_bit(I40E_FLAG_RS_FEC, flags);
15397 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15398 	}
15399 }
15400 
15401 /**
15402  * i40e_check_recovery_mode - check if we are running transition firmware
15403  * @pf: board private structure
15404  *
15405  * Check registers indicating the firmware runs in recovery mode. Sets the
15406  * appropriate driver state.
15407  *
15408  * Returns true if the recovery mode was detected, false otherwise
15409  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15410 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15411 {
15412 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15413 
15414 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15415 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15416 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15417 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15418 
15419 		return true;
15420 	}
15421 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15422 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15423 
15424 	return false;
15425 }
15426 
15427 /**
15428  * i40e_pf_loop_reset - perform reset in a loop.
15429  * @pf: board private structure
15430  *
15431  * This function is useful when a NIC is about to enter recovery mode.
15432  * When a NIC's internal data structures are corrupted the NIC's
15433  * firmware is going to enter recovery mode.
15434  * Right after a POR it takes about 7 minutes for firmware to enter
15435  * recovery mode. Until that time a NIC is in some kind of intermediate
15436  * state. After that time period the NIC almost surely enters
15437  * recovery mode. The only way for a driver to detect intermediate
15438  * state is to issue a series of pf-resets and check a return value.
15439  * If a PF reset returns success then the firmware could be in recovery
15440  * mode so the caller of this code needs to check for recovery mode
15441  * if this function returns success. There is a little chance that
15442  * firmware will hang in intermediate state forever.
15443  * Since waiting 7 minutes is quite a lot of time this function waits
15444  * 10 seconds and then gives up by returning an error.
15445  *
15446  * Return 0 on success, negative on failure.
15447  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15448 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15449 {
15450 	/* wait max 10 seconds for PF reset to succeed */
15451 	const unsigned long time_end = jiffies + 10 * HZ;
15452 	struct i40e_hw *hw = &pf->hw;
15453 	int ret;
15454 
15455 	ret = i40e_pf_reset(hw);
15456 	while (ret != 0 && time_before(jiffies, time_end)) {
15457 		usleep_range(10000, 20000);
15458 		ret = i40e_pf_reset(hw);
15459 	}
15460 
15461 	if (ret == 0)
15462 		pf->pfr_count++;
15463 	else
15464 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15465 
15466 	return ret;
15467 }
15468 
15469 /**
15470  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15471  * @pf: board private structure
15472  *
15473  * Check FW registers to determine if FW issued unexpected EMP Reset.
15474  * Every time when unexpected EMP Reset occurs the FW increments
15475  * a counter of unexpected EMP Resets. When the counter reaches 10
15476  * the FW should enter the Recovery mode
15477  *
15478  * Returns true if FW issued unexpected EMP Reset
15479  **/
i40e_check_fw_empr(struct i40e_pf * pf)15480 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15481 {
15482 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15483 			   I40E_GL_FWSTS_FWS1B_MASK;
15484 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15485 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15486 }
15487 
15488 /**
15489  * i40e_handle_resets - handle EMP resets and PF resets
15490  * @pf: board private structure
15491  *
15492  * Handle both EMP resets and PF resets and conclude whether there are
15493  * any issues regarding these resets. If there are any issues then
15494  * generate log entry.
15495  *
15496  * Return 0 if NIC is healthy or negative value when there are issues
15497  * with resets
15498  **/
i40e_handle_resets(struct i40e_pf * pf)15499 static int i40e_handle_resets(struct i40e_pf *pf)
15500 {
15501 	const int pfr = i40e_pf_loop_reset(pf);
15502 	const bool is_empr = i40e_check_fw_empr(pf);
15503 
15504 	if (is_empr || pfr != 0)
15505 		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");
15506 
15507 	return is_empr ? -EIO : pfr;
15508 }
15509 
15510 /**
15511  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15512  * @pf: board private structure
15513  * @hw: ptr to the hardware info
15514  *
15515  * This function does a minimal setup of all subsystems needed for running
15516  * recovery mode.
15517  *
15518  * Returns 0 on success, negative on failure
15519  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15520 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15521 {
15522 	struct i40e_vsi *vsi;
15523 	int err;
15524 	int v_idx;
15525 
15526 	pci_set_drvdata(pf->pdev, pf);
15527 	pci_save_state(pf->pdev);
15528 
15529 	/* set up periodic task facility */
15530 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15531 	pf->service_timer_period = HZ;
15532 
15533 	INIT_WORK(&pf->service_task, i40e_service_task);
15534 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15535 
15536 	err = i40e_init_interrupt_scheme(pf);
15537 	if (err)
15538 		goto err_switch_setup;
15539 
15540 	/* The number of VSIs reported by the FW is the minimum guaranteed
15541 	 * to us; HW supports far more and we share the remaining pool with
15542 	 * the other PFs. We allocate space for more than the guarantee with
15543 	 * the understanding that we might not get them all later.
15544 	 */
15545 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15546 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15547 	else
15548 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15549 
15550 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15551 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15552 			  GFP_KERNEL);
15553 	if (!pf->vsi) {
15554 		err = -ENOMEM;
15555 		goto err_switch_setup;
15556 	}
15557 
15558 	/* We allocate one VSI which is needed as absolute minimum
15559 	 * in order to register the netdev
15560 	 */
15561 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15562 	if (v_idx < 0) {
15563 		err = v_idx;
15564 		goto err_switch_setup;
15565 	}
15566 	pf->lan_vsi = v_idx;
15567 	vsi = pf->vsi[v_idx];
15568 	if (!vsi) {
15569 		err = -EFAULT;
15570 		goto err_switch_setup;
15571 	}
15572 	vsi->alloc_queue_pairs = 1;
15573 	err = i40e_config_netdev(vsi);
15574 	if (err)
15575 		goto err_switch_setup;
15576 	err = register_netdev(vsi->netdev);
15577 	if (err)
15578 		goto err_switch_setup;
15579 	vsi->netdev_registered = true;
15580 	i40e_dbg_pf_init(pf);
15581 
15582 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15583 	if (err)
15584 		goto err_switch_setup;
15585 
15586 	/* tell the firmware that we're starting */
15587 	i40e_send_version(pf);
15588 
15589 	/* since everything's happy, start the service_task timer */
15590 	mod_timer(&pf->service_timer,
15591 		  round_jiffies(jiffies + pf->service_timer_period));
15592 
15593 	return 0;
15594 
15595 err_switch_setup:
15596 	i40e_reset_interrupt_capability(pf);
15597 	timer_shutdown_sync(&pf->service_timer);
15598 	i40e_shutdown_adminq(hw);
15599 	iounmap(hw->hw_addr);
15600 	pci_release_mem_regions(pf->pdev);
15601 	pci_disable_device(pf->pdev);
15602 	i40e_free_pf(pf);
15603 
15604 	return err;
15605 }
15606 
15607 /**
15608  * i40e_set_subsystem_device_id - set subsystem device id
15609  * @hw: pointer to the hardware info
15610  *
15611  * Set PCI subsystem device id either from a pci_dev structure or
15612  * a specific FW register.
15613  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15614 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15615 {
15616 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15617 
15618 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15619 		pf->pdev->subsystem_device :
15620 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15621 }
15622 
15623 /**
15624  * i40e_probe - Device initialization routine
15625  * @pdev: PCI device information struct
15626  * @ent: entry in i40e_pci_tbl
15627  *
15628  * i40e_probe initializes a PF identified by a pci_dev structure.
15629  * The OS initialization, configuring of the PF private structure,
15630  * and a hardware reset occur.
15631  *
15632  * Returns 0 on success, negative on failure
15633  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15634 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15635 {
15636 	struct i40e_aq_get_phy_abilities_resp abilities;
15637 #ifdef CONFIG_I40E_DCB
15638 	enum i40e_get_fw_lldp_status_resp lldp_status;
15639 #endif /* CONFIG_I40E_DCB */
15640 	struct i40e_vsi *vsi;
15641 	struct i40e_pf *pf;
15642 	struct i40e_hw *hw;
15643 	u16 wol_nvm_bits;
15644 	char nvm_ver[32];
15645 	u16 link_status;
15646 #ifdef CONFIG_I40E_DCB
15647 	int status;
15648 #endif /* CONFIG_I40E_DCB */
15649 	int err;
15650 	u32 val;
15651 
15652 	err = pci_enable_device_mem(pdev);
15653 	if (err)
15654 		return err;
15655 
15656 	/* set up for high or low dma */
15657 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15658 	if (err) {
15659 		dev_err(&pdev->dev,
15660 			"DMA configuration failed: 0x%x\n", err);
15661 		goto err_dma;
15662 	}
15663 
15664 	/* set up pci connections */
15665 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15666 	if (err) {
15667 		dev_info(&pdev->dev,
15668 			 "pci_request_selected_regions failed %d\n", err);
15669 		goto err_pci_reg;
15670 	}
15671 
15672 	pci_set_master(pdev);
15673 
15674 	/* Now that we have a PCI connection, we need to do the
15675 	 * low level device setup.  This is primarily setting up
15676 	 * the Admin Queue structures and then querying for the
15677 	 * device's current profile information.
15678 	 */
15679 	pf = i40e_alloc_pf(&pdev->dev);
15680 	if (!pf) {
15681 		err = -ENOMEM;
15682 		goto err_pf_alloc;
15683 	}
15684 	pf->next_vsi = 0;
15685 	pf->pdev = pdev;
15686 	set_bit(__I40E_DOWN, pf->state);
15687 
15688 	hw = &pf->hw;
15689 
15690 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15691 				I40E_MAX_CSR_SPACE);
15692 	/* We believe that the highest register to read is
15693 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15694 	 * is not less than that before mapping to prevent a
15695 	 * kernel panic.
15696 	 */
15697 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15698 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15699 			pf->ioremap_len);
15700 		err = -ENOMEM;
15701 		goto err_ioremap;
15702 	}
15703 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15704 	if (!hw->hw_addr) {
15705 		err = -EIO;
15706 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15707 			 (unsigned int)pci_resource_start(pdev, 0),
15708 			 pf->ioremap_len, err);
15709 		goto err_ioremap;
15710 	}
15711 	hw->vendor_id = pdev->vendor;
15712 	hw->device_id = pdev->device;
15713 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15714 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15715 	i40e_set_subsystem_device_id(hw);
15716 	hw->bus.device = PCI_SLOT(pdev->devfn);
15717 	hw->bus.func = PCI_FUNC(pdev->devfn);
15718 	hw->bus.bus_id = pdev->bus->number;
15719 
15720 	/* Select something other than the 802.1ad ethertype for the
15721 	 * switch to use internally and drop on ingress.
15722 	 */
15723 	hw->switch_tag = 0xffff;
15724 	hw->first_tag = ETH_P_8021AD;
15725 	hw->second_tag = ETH_P_8021Q;
15726 
15727 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15728 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15729 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15730 
15731 	/* set up the locks for the AQ, do this only once in probe
15732 	 * and destroy them only once in remove
15733 	 */
15734 	mutex_init(&hw->aq.asq_mutex);
15735 	mutex_init(&hw->aq.arq_mutex);
15736 
15737 	pf->msg_enable = netif_msg_init(debug,
15738 					NETIF_MSG_DRV |
15739 					NETIF_MSG_PROBE |
15740 					NETIF_MSG_LINK);
15741 	if (debug < -1)
15742 		pf->hw.debug_mask = debug;
15743 
15744 	/* do a special CORER for clearing PXE mode once at init */
15745 	if (hw->revision_id == 0 &&
15746 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15747 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15748 		i40e_flush(hw);
15749 		msleep(200);
15750 		pf->corer_count++;
15751 
15752 		i40e_clear_pxe_mode(hw);
15753 	}
15754 
15755 	/* Reset here to make sure all is clean and to define PF 'n' */
15756 	i40e_clear_hw(hw);
15757 
15758 	err = i40e_set_mac_type(hw);
15759 	if (err) {
15760 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15761 			 err);
15762 		goto err_pf_reset;
15763 	}
15764 
15765 	err = i40e_handle_resets(pf);
15766 	if (err)
15767 		goto err_pf_reset;
15768 
15769 	i40e_check_recovery_mode(pf);
15770 
15771 	if (is_kdump_kernel()) {
15772 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15773 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15774 	} else {
15775 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15776 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15777 	}
15778 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15779 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15780 
15781 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15782 		 "%s-%s:misc",
15783 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15784 
15785 	err = i40e_init_shared_code(hw);
15786 	if (err) {
15787 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15788 			 err);
15789 		goto err_pf_reset;
15790 	}
15791 
15792 	/* set up a default setting for link flow control */
15793 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15794 
15795 	err = i40e_init_adminq(hw);
15796 	if (err) {
15797 		if (err == -EIO)
15798 			dev_info(&pdev->dev,
15799 				 "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",
15800 				 hw->aq.api_maj_ver,
15801 				 hw->aq.api_min_ver,
15802 				 I40E_FW_API_VERSION_MAJOR,
15803 				 I40E_FW_MINOR_VERSION(hw));
15804 		else
15805 			dev_info(&pdev->dev,
15806 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15807 
15808 		goto err_pf_reset;
15809 	}
15810 	i40e_get_oem_version(hw);
15811 	i40e_get_pba_string(hw);
15812 
15813 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15814 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15815 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15816 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15817 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15818 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15819 		 hw->subsystem_device_id);
15820 
15821 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15822 				  I40E_FW_MINOR_VERSION(hw) + 1))
15823 		dev_dbg(&pdev->dev,
15824 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15825 			 hw->aq.api_maj_ver,
15826 			 hw->aq.api_min_ver,
15827 			 I40E_FW_API_VERSION_MAJOR,
15828 			 I40E_FW_MINOR_VERSION(hw));
15829 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15830 		dev_info(&pdev->dev,
15831 			 "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",
15832 			 hw->aq.api_maj_ver,
15833 			 hw->aq.api_min_ver,
15834 			 I40E_FW_API_VERSION_MAJOR,
15835 			 I40E_FW_MINOR_VERSION(hw));
15836 
15837 	i40e_verify_eeprom(pf);
15838 
15839 	/* Rev 0 hardware was never productized */
15840 	if (hw->revision_id < 1)
15841 		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");
15842 
15843 	i40e_clear_pxe_mode(hw);
15844 
15845 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15846 	if (err)
15847 		goto err_adminq_setup;
15848 
15849 	err = i40e_sw_init(pf);
15850 	if (err) {
15851 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15852 		goto err_sw_init;
15853 	}
15854 
15855 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15856 		return i40e_init_recovery_mode(pf, hw);
15857 
15858 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15859 				hw->func_caps.num_rx_qp, 0, 0);
15860 	if (err) {
15861 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15862 		goto err_init_lan_hmc;
15863 	}
15864 
15865 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15866 	if (err) {
15867 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15868 		err = -ENOENT;
15869 		goto err_configure_lan_hmc;
15870 	}
15871 
15872 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15873 	 * Ignore error return codes because if it was already disabled via
15874 	 * hardware settings this will fail
15875 	 */
15876 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15877 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15878 		i40e_aq_stop_lldp(hw, true, false, NULL);
15879 	}
15880 
15881 	/* allow a platform config to override the HW addr */
15882 	i40e_get_platform_mac_addr(pdev, pf);
15883 
15884 	if (!is_valid_ether_addr(hw->mac.addr)) {
15885 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15886 		err = -EIO;
15887 		goto err_mac_addr;
15888 	}
15889 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15890 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15891 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15892 	if (is_valid_ether_addr(hw->mac.port_addr))
15893 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15894 
15895 	i40e_ptp_alloc_pins(pf);
15896 	pci_set_drvdata(pdev, pf);
15897 	pci_save_state(pdev);
15898 
15899 #ifdef CONFIG_I40E_DCB
15900 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15901 	(!status &&
15902 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15903 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15904 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15905 	dev_info(&pdev->dev,
15906 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15907 			"FW LLDP is disabled\n" :
15908 			"FW LLDP is enabled\n");
15909 
15910 	/* Enable FW to write default DCB config on link-up */
15911 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15912 
15913 	err = i40e_init_pf_dcb(pf);
15914 	if (err) {
15915 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15916 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15917 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15918 		/* Continue without DCB enabled */
15919 	}
15920 #endif /* CONFIG_I40E_DCB */
15921 
15922 	/* set up periodic task facility */
15923 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15924 	pf->service_timer_period = HZ;
15925 
15926 	INIT_WORK(&pf->service_task, i40e_service_task);
15927 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15928 
15929 	/* NVM bit on means WoL disabled for the port */
15930 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15931 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15932 		pf->wol_en = false;
15933 	else
15934 		pf->wol_en = true;
15935 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15936 
15937 	/* set up the main switch operations */
15938 	i40e_determine_queue_usage(pf);
15939 	err = i40e_init_interrupt_scheme(pf);
15940 	if (err)
15941 		goto err_switch_setup;
15942 
15943 	/* Reduce Tx and Rx pairs for kdump
15944 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15945 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15946 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15947 	 */
15948 	if (is_kdump_kernel())
15949 		pf->num_lan_msix = 1;
15950 
15951 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15952 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15953 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15954 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15955 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15956 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15957 						    UDP_TUNNEL_TYPE_GENEVE;
15958 
15959 	/* The number of VSIs reported by the FW is the minimum guaranteed
15960 	 * to us; HW supports far more and we share the remaining pool with
15961 	 * the other PFs. We allocate space for more than the guarantee with
15962 	 * the understanding that we might not get them all later.
15963 	 */
15964 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15965 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15966 	else
15967 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15968 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15969 		dev_warn(&pf->pdev->dev,
15970 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15971 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15972 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15973 	}
15974 
15975 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15976 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15977 			  GFP_KERNEL);
15978 	if (!pf->vsi) {
15979 		err = -ENOMEM;
15980 		goto err_switch_setup;
15981 	}
15982 
15983 #ifdef CONFIG_PCI_IOV
15984 	/* prep for VF support */
15985 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15986 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15987 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15988 		if (pci_num_vf(pdev))
15989 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15990 	}
15991 #endif
15992 	err = i40e_setup_pf_switch(pf, false, false);
15993 	if (err) {
15994 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15995 		goto err_vsis;
15996 	}
15997 
15998 	vsi = i40e_pf_get_main_vsi(pf);
15999 	INIT_LIST_HEAD(&vsi->ch_list);
16000 
16001 	/* if FDIR VSI was set up, start it now */
16002 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
16003 	if (vsi)
16004 		i40e_vsi_open(vsi);
16005 
16006 	/* The driver only wants link up/down and module qualification
16007 	 * reports from firmware.  Note the negative logic.
16008 	 */
16009 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16010 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16011 					 I40E_AQ_EVENT_MEDIA_NA |
16012 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16013 	if (err)
16014 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16015 			 ERR_PTR(err),
16016 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16017 
16018 	/* Reconfigure hardware for allowing smaller MSS in the case
16019 	 * of TSO, so that we avoid the MDD being fired and causing
16020 	 * a reset in the case of small MSS+TSO.
16021 	 */
16022 	val = rd32(hw, I40E_REG_MSS);
16023 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16024 		val &= ~I40E_REG_MSS_MIN_MASK;
16025 		val |= I40E_64BYTE_MSS;
16026 		wr32(hw, I40E_REG_MSS, val);
16027 	}
16028 
16029 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
16030 		msleep(75);
16031 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16032 		if (err)
16033 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16034 				 ERR_PTR(err),
16035 				 i40e_aq_str(&pf->hw,
16036 					     pf->hw.aq.asq_last_status));
16037 	}
16038 	/* The main driver is (mostly) up and happy. We need to set this state
16039 	 * before setting up the misc vector or we get a race and the vector
16040 	 * ends up disabled forever.
16041 	 */
16042 	clear_bit(__I40E_DOWN, pf->state);
16043 
16044 	/* In case of MSIX we are going to setup the misc vector right here
16045 	 * to handle admin queue events etc. In case of legacy and MSI
16046 	 * the misc functionality and queue processing is combined in
16047 	 * the same vector and that gets setup at open.
16048 	 */
16049 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
16050 		err = i40e_setup_misc_vector(pf);
16051 		if (err) {
16052 			dev_info(&pdev->dev,
16053 				 "setup of misc vector failed: %d\n", err);
16054 			i40e_cloud_filter_exit(pf);
16055 			i40e_fdir_teardown(pf);
16056 			goto err_vsis;
16057 		}
16058 	}
16059 
16060 #ifdef CONFIG_PCI_IOV
16061 	/* prep for VF support */
16062 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
16063 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
16064 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16065 		/* disable link interrupts for VFs */
16066 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16067 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16068 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16069 		i40e_flush(hw);
16070 
16071 		if (pci_num_vf(pdev)) {
16072 			dev_info(&pdev->dev,
16073 				 "Active VFs found, allocating resources.\n");
16074 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16075 			if (err)
16076 				dev_info(&pdev->dev,
16077 					 "Error %d allocating resources for existing VFs\n",
16078 					 err);
16079 		}
16080 	}
16081 #endif /* CONFIG_PCI_IOV */
16082 
16083 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16084 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16085 						      pf->num_iwarp_msix,
16086 						      I40E_IWARP_IRQ_PILE_ID);
16087 		if (pf->iwarp_base_vector < 0) {
16088 			dev_info(&pdev->dev,
16089 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16090 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16091 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16092 		}
16093 	}
16094 
16095 	i40e_dbg_pf_init(pf);
16096 
16097 	/* tell the firmware that we're starting */
16098 	i40e_send_version(pf);
16099 
16100 	/* since everything's happy, start the service_task timer */
16101 	mod_timer(&pf->service_timer,
16102 		  round_jiffies(jiffies + pf->service_timer_period));
16103 
16104 	/* add this PF to client device list and launch a client service task */
16105 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16106 		err = i40e_lan_add_device(pf);
16107 		if (err)
16108 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16109 				 err);
16110 	}
16111 
16112 #define PCI_SPEED_SIZE 8
16113 #define PCI_WIDTH_SIZE 8
16114 	/* Devices on the IOSF bus do not have this information
16115 	 * and will report PCI Gen 1 x 1 by default so don't bother
16116 	 * checking them.
16117 	 */
16118 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16119 		char speed[PCI_SPEED_SIZE] = "Unknown";
16120 		char width[PCI_WIDTH_SIZE] = "Unknown";
16121 
16122 		/* Get the negotiated link width and speed from PCI config
16123 		 * space
16124 		 */
16125 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16126 					  &link_status);
16127 
16128 		i40e_set_pci_config_data(hw, link_status);
16129 
16130 		switch (hw->bus.speed) {
16131 		case i40e_bus_speed_8000:
16132 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16133 		case i40e_bus_speed_5000:
16134 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16135 		case i40e_bus_speed_2500:
16136 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16137 		default:
16138 			break;
16139 		}
16140 		switch (hw->bus.width) {
16141 		case i40e_bus_width_pcie_x8:
16142 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16143 		case i40e_bus_width_pcie_x4:
16144 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16145 		case i40e_bus_width_pcie_x2:
16146 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16147 		case i40e_bus_width_pcie_x1:
16148 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16149 		default:
16150 			break;
16151 		}
16152 
16153 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16154 			 speed, width);
16155 
16156 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16157 		    hw->bus.speed < i40e_bus_speed_8000) {
16158 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16159 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16160 		}
16161 	}
16162 
16163 	/* get the requested speeds from the fw */
16164 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16165 	if (err)
16166 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16167 			ERR_PTR(err),
16168 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16169 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16170 
16171 	/* set the FEC config due to the board capabilities */
16172 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16173 
16174 	/* get the supported phy types from the fw */
16175 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16176 	if (err)
16177 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16178 			ERR_PTR(err),
16179 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16180 
16181 	/* make sure the MFS hasn't been set lower than the default */
16182 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16183 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16184 			rd32(&pf->hw, I40E_PRTGL_SAH));
16185 	if (val < MAX_FRAME_SIZE_DEFAULT)
16186 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16187 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16188 
16189 	/* Add a filter to drop all Flow control frames from any VSI from being
16190 	 * transmitted. By doing so we stop a malicious VF from sending out
16191 	 * PAUSE or PFC frames and potentially controlling traffic for other
16192 	 * PF/VF VSIs.
16193 	 * The FW can still send Flow control frames if enabled.
16194 	 */
16195 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16196 						       pf->main_vsi_seid);
16197 
16198 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16199 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16200 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16201 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16202 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16203 	/* print a string summarizing features */
16204 	i40e_print_features(pf);
16205 
16206 	i40e_devlink_register(pf);
16207 
16208 	return 0;
16209 
16210 	/* Unwind what we've done if something failed in the setup */
16211 err_vsis:
16212 	set_bit(__I40E_DOWN, pf->state);
16213 	i40e_clear_interrupt_scheme(pf);
16214 	kfree(pf->vsi);
16215 err_switch_setup:
16216 	i40e_reset_interrupt_capability(pf);
16217 	timer_shutdown_sync(&pf->service_timer);
16218 err_mac_addr:
16219 err_configure_lan_hmc:
16220 	(void)i40e_shutdown_lan_hmc(hw);
16221 err_init_lan_hmc:
16222 	kfree(pf->qp_pile);
16223 err_sw_init:
16224 err_adminq_setup:
16225 err_pf_reset:
16226 	iounmap(hw->hw_addr);
16227 err_ioremap:
16228 	i40e_free_pf(pf);
16229 err_pf_alloc:
16230 	pci_release_mem_regions(pdev);
16231 err_pci_reg:
16232 err_dma:
16233 	pci_disable_device(pdev);
16234 	return err;
16235 }
16236 
16237 /**
16238  * i40e_remove - Device removal routine
16239  * @pdev: PCI device information struct
16240  *
16241  * i40e_remove is called by the PCI subsystem to alert the driver
16242  * that is should release a PCI device.  This could be caused by a
16243  * Hot-Plug event, or because the driver is going to be removed from
16244  * memory.
16245  **/
i40e_remove(struct pci_dev * pdev)16246 static void i40e_remove(struct pci_dev *pdev)
16247 {
16248 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16249 	struct i40e_hw *hw = &pf->hw;
16250 	struct i40e_vsi *vsi;
16251 	struct i40e_veb *veb;
16252 	int ret_code;
16253 	int i;
16254 
16255 	i40e_devlink_unregister(pf);
16256 
16257 	i40e_dbg_pf_exit(pf);
16258 
16259 	i40e_ptp_stop(pf);
16260 
16261 	/* Disable RSS in hw */
16262 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16263 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16264 
16265 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16266 	 * flags, once they are set, i40e_rebuild should not be called as
16267 	 * i40e_prep_for_reset always returns early.
16268 	 */
16269 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16270 		usleep_range(1000, 2000);
16271 	set_bit(__I40E_IN_REMOVE, pf->state);
16272 
16273 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16274 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16275 		i40e_free_vfs(pf);
16276 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16277 	}
16278 	/* no more scheduling of any task */
16279 	set_bit(__I40E_SUSPENDED, pf->state);
16280 	set_bit(__I40E_DOWN, pf->state);
16281 	if (pf->service_timer.function)
16282 		timer_shutdown_sync(&pf->service_timer);
16283 	if (pf->service_task.func)
16284 		cancel_work_sync(&pf->service_task);
16285 
16286 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16287 		struct i40e_vsi *vsi = pf->vsi[0];
16288 
16289 		/* We know that we have allocated only one vsi for this PF,
16290 		 * it was just for registering netdevice, so the interface
16291 		 * could be visible in the 'ifconfig' output
16292 		 */
16293 		unregister_netdev(vsi->netdev);
16294 		free_netdev(vsi->netdev);
16295 
16296 		goto unmap;
16297 	}
16298 
16299 	/* Client close must be called explicitly here because the timer
16300 	 * has been stopped.
16301 	 */
16302 	i40e_notify_client_of_netdev_close(pf, false);
16303 
16304 	i40e_fdir_teardown(pf);
16305 
16306 	/* If there is a switch structure or any orphans, remove them.
16307 	 * This will leave only the PF's VSI remaining.
16308 	 */
16309 	i40e_pf_for_each_veb(pf, i, veb)
16310 		if (veb->uplink_seid == pf->mac_seid ||
16311 		    veb->uplink_seid == 0)
16312 			i40e_switch_branch_release(veb);
16313 
16314 	/* Now we can shutdown the PF's VSIs, just before we kill
16315 	 * adminq and hmc.
16316 	 */
16317 	i40e_pf_for_each_vsi(pf, i, vsi) {
16318 		i40e_vsi_close(vsi);
16319 		i40e_vsi_release(vsi);
16320 		pf->vsi[i] = NULL;
16321 	}
16322 
16323 	i40e_cloud_filter_exit(pf);
16324 
16325 	/* remove attached clients */
16326 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16327 		ret_code = i40e_lan_del_device(pf);
16328 		if (ret_code)
16329 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16330 				 ret_code);
16331 	}
16332 
16333 	/* shutdown and destroy the HMC */
16334 	if (hw->hmc.hmc_obj) {
16335 		ret_code = i40e_shutdown_lan_hmc(hw);
16336 		if (ret_code)
16337 			dev_warn(&pdev->dev,
16338 				 "Failed to destroy the HMC resources: %d\n",
16339 				 ret_code);
16340 	}
16341 
16342 unmap:
16343 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16344 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16345 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16346 		free_irq(pf->pdev->irq, pf);
16347 
16348 	/* shutdown the adminq */
16349 	i40e_shutdown_adminq(hw);
16350 
16351 	/* destroy the locks only once, here */
16352 	mutex_destroy(&hw->aq.arq_mutex);
16353 	mutex_destroy(&hw->aq.asq_mutex);
16354 
16355 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16356 	rtnl_lock();
16357 	i40e_clear_interrupt_scheme(pf);
16358 	i40e_pf_for_each_vsi(pf, i, vsi) {
16359 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16360 			i40e_vsi_clear_rings(vsi);
16361 
16362 		i40e_vsi_clear(vsi);
16363 		pf->vsi[i] = NULL;
16364 	}
16365 	rtnl_unlock();
16366 
16367 	i40e_pf_for_each_veb(pf, i, veb) {
16368 		kfree(veb);
16369 		pf->veb[i] = NULL;
16370 	}
16371 
16372 	kfree(pf->qp_pile);
16373 	kfree(pf->vsi);
16374 
16375 	iounmap(hw->hw_addr);
16376 	i40e_free_pf(pf);
16377 	pci_release_mem_regions(pdev);
16378 
16379 	pci_disable_device(pdev);
16380 }
16381 
16382 /**
16383  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16384  * using the mac_address_write admin q function
16385  * @pf: pointer to i40e_pf struct
16386  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16387 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16388 {
16389 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16390 	struct i40e_hw *hw = &pf->hw;
16391 	u8 mac_addr[6];
16392 	u16 flags = 0;
16393 	int ret;
16394 
16395 	/* Get current MAC address in case it's an LAA */
16396 	if (main_vsi && main_vsi->netdev) {
16397 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16398 	} else {
16399 		dev_err(&pf->pdev->dev,
16400 			"Failed to retrieve MAC address; using default\n");
16401 		ether_addr_copy(mac_addr, hw->mac.addr);
16402 	}
16403 
16404 	/* The FW expects the mac address write cmd to first be called with
16405 	 * one of these flags before calling it again with the multicast
16406 	 * enable flags.
16407 	 */
16408 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16409 
16410 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16411 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16412 
16413 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16414 	if (ret) {
16415 		dev_err(&pf->pdev->dev,
16416 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16417 		return;
16418 	}
16419 
16420 	flags = I40E_AQC_MC_MAG_EN
16421 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16422 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16423 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16424 	if (ret)
16425 		dev_err(&pf->pdev->dev,
16426 			"Failed to enable Multicast Magic Packet wake up\n");
16427 }
16428 
16429 /**
16430  * i40e_io_suspend - suspend all IO operations
16431  * @pf: pointer to i40e_pf struct
16432  *
16433  **/
i40e_io_suspend(struct i40e_pf * pf)16434 static int i40e_io_suspend(struct i40e_pf *pf)
16435 {
16436 	struct i40e_hw *hw = &pf->hw;
16437 
16438 	set_bit(__I40E_DOWN, pf->state);
16439 
16440 	/* Ensure service task will not be running */
16441 	del_timer_sync(&pf->service_timer);
16442 	cancel_work_sync(&pf->service_task);
16443 
16444 	/* Client close must be called explicitly here because the timer
16445 	 * has been stopped.
16446 	 */
16447 	i40e_notify_client_of_netdev_close(pf, false);
16448 
16449 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16450 	    pf->wol_en)
16451 		i40e_enable_mc_magic_wake(pf);
16452 
16453 	/* Since we're going to destroy queues during the
16454 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16455 	 * whole section
16456 	 */
16457 	rtnl_lock();
16458 
16459 	i40e_prep_for_reset(pf);
16460 
16461 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16462 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16463 
16464 	/* Clear the interrupt scheme and release our IRQs so that the system
16465 	 * can safely hibernate even when there are a large number of CPUs.
16466 	 * Otherwise hibernation might fail when mapping all the vectors back
16467 	 * to CPU0.
16468 	 */
16469 	i40e_clear_interrupt_scheme(pf);
16470 
16471 	rtnl_unlock();
16472 
16473 	return 0;
16474 }
16475 
16476 /**
16477  * i40e_io_resume - resume IO operations
16478  * @pf: pointer to i40e_pf struct
16479  *
16480  **/
i40e_io_resume(struct i40e_pf * pf)16481 static int i40e_io_resume(struct i40e_pf *pf)
16482 {
16483 	struct device *dev = &pf->pdev->dev;
16484 	int err;
16485 
16486 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16487 	 * since we're going to be restoring queues
16488 	 */
16489 	rtnl_lock();
16490 
16491 	/* We cleared the interrupt scheme when we suspended, so we need to
16492 	 * restore it now to resume device functionality.
16493 	 */
16494 	err = i40e_restore_interrupt_scheme(pf);
16495 	if (err) {
16496 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16497 			err);
16498 	}
16499 
16500 	clear_bit(__I40E_DOWN, pf->state);
16501 	i40e_reset_and_rebuild(pf, false, true);
16502 
16503 	rtnl_unlock();
16504 
16505 	/* Clear suspended state last after everything is recovered */
16506 	clear_bit(__I40E_SUSPENDED, pf->state);
16507 
16508 	/* Restart the service task */
16509 	mod_timer(&pf->service_timer,
16510 		  round_jiffies(jiffies + pf->service_timer_period));
16511 
16512 	return 0;
16513 }
16514 
16515 /**
16516  * i40e_pci_error_detected - warning that something funky happened in PCI land
16517  * @pdev: PCI device information struct
16518  * @error: the type of PCI error
16519  *
16520  * Called to warn that something happened and the error handling steps
16521  * are in progress.  Allows the driver to quiesce things, be ready for
16522  * remediation.
16523  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16524 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16525 						pci_channel_state_t error)
16526 {
16527 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16528 
16529 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16530 
16531 	if (!pf) {
16532 		dev_info(&pdev->dev,
16533 			 "Cannot recover - error happened during device probe\n");
16534 		return PCI_ERS_RESULT_DISCONNECT;
16535 	}
16536 
16537 	/* shutdown all operations */
16538 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16539 		i40e_io_suspend(pf);
16540 
16541 	/* Request a slot reset */
16542 	return PCI_ERS_RESULT_NEED_RESET;
16543 }
16544 
16545 /**
16546  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16547  * @pdev: PCI device information struct
16548  *
16549  * Called to find if the driver can work with the device now that
16550  * the pci slot has been reset.  If a basic connection seems good
16551  * (registers are readable and have sane content) then return a
16552  * happy little PCI_ERS_RESULT_xxx.
16553  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16554 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16555 {
16556 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16557 	pci_ers_result_t result;
16558 	u32 reg;
16559 
16560 	dev_dbg(&pdev->dev, "%s\n", __func__);
16561 	/* enable I/O and memory of the device  */
16562 	if (pci_enable_device(pdev)) {
16563 		dev_info(&pdev->dev,
16564 			 "Cannot re-enable PCI device after reset.\n");
16565 		result = PCI_ERS_RESULT_DISCONNECT;
16566 	} else {
16567 		pci_set_master(pdev);
16568 		pci_restore_state(pdev);
16569 		pci_save_state(pdev);
16570 		pci_wake_from_d3(pdev, false);
16571 
16572 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16573 		if (reg == 0)
16574 			result = PCI_ERS_RESULT_RECOVERED;
16575 		else
16576 			result = PCI_ERS_RESULT_DISCONNECT;
16577 	}
16578 
16579 	return result;
16580 }
16581 
16582 /**
16583  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16584  * @pdev: PCI device information struct
16585  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16586 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16587 {
16588 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16589 
16590 	i40e_prep_for_reset(pf);
16591 }
16592 
16593 /**
16594  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16595  * @pdev: PCI device information struct
16596  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16597 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16598 {
16599 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16600 
16601 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16602 		return;
16603 
16604 	i40e_reset_and_rebuild(pf, false, false);
16605 #ifdef CONFIG_PCI_IOV
16606 	i40e_restore_all_vfs_msi_state(pdev);
16607 #endif /* CONFIG_PCI_IOV */
16608 }
16609 
16610 /**
16611  * i40e_pci_error_resume - restart operations after PCI error recovery
16612  * @pdev: PCI device information struct
16613  *
16614  * Called to allow the driver to bring things back up after PCI error
16615  * and/or reset recovery has finished.
16616  **/
i40e_pci_error_resume(struct pci_dev * pdev)16617 static void i40e_pci_error_resume(struct pci_dev *pdev)
16618 {
16619 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16620 
16621 	dev_dbg(&pdev->dev, "%s\n", __func__);
16622 	if (test_bit(__I40E_SUSPENDED, pf->state))
16623 		return;
16624 
16625 	i40e_io_resume(pf);
16626 }
16627 
16628 /**
16629  * i40e_shutdown - PCI callback for shutting down
16630  * @pdev: PCI device information struct
16631  **/
i40e_shutdown(struct pci_dev * pdev)16632 static void i40e_shutdown(struct pci_dev *pdev)
16633 {
16634 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16635 	struct i40e_hw *hw = &pf->hw;
16636 
16637 	set_bit(__I40E_SUSPENDED, pf->state);
16638 	set_bit(__I40E_DOWN, pf->state);
16639 
16640 	del_timer_sync(&pf->service_timer);
16641 	cancel_work_sync(&pf->service_task);
16642 	i40e_cloud_filter_exit(pf);
16643 	i40e_fdir_teardown(pf);
16644 
16645 	/* Client close must be called explicitly here because the timer
16646 	 * has been stopped.
16647 	 */
16648 	i40e_notify_client_of_netdev_close(pf, false);
16649 
16650 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16651 	    pf->wol_en)
16652 		i40e_enable_mc_magic_wake(pf);
16653 
16654 	i40e_prep_for_reset(pf);
16655 
16656 	wr32(hw, I40E_PFPM_APM,
16657 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16658 	wr32(hw, I40E_PFPM_WUFC,
16659 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16660 
16661 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16662 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16663 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16664 		free_irq(pf->pdev->irq, pf);
16665 
16666 	/* Since we're going to destroy queues during the
16667 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16668 	 * whole section
16669 	 */
16670 	rtnl_lock();
16671 	i40e_clear_interrupt_scheme(pf);
16672 	rtnl_unlock();
16673 
16674 	if (system_state == SYSTEM_POWER_OFF) {
16675 		pci_wake_from_d3(pdev, pf->wol_en);
16676 		pci_set_power_state(pdev, PCI_D3hot);
16677 	}
16678 }
16679 
16680 /**
16681  * i40e_suspend - PM callback for moving to D3
16682  * @dev: generic device information structure
16683  **/
i40e_suspend(struct device * dev)16684 static int i40e_suspend(struct device *dev)
16685 {
16686 	struct i40e_pf *pf = dev_get_drvdata(dev);
16687 
16688 	/* If we're already suspended, then there is nothing to do */
16689 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16690 		return 0;
16691 	return i40e_io_suspend(pf);
16692 }
16693 
16694 /**
16695  * i40e_resume - PM callback for waking up from D3
16696  * @dev: generic device information structure
16697  **/
i40e_resume(struct device * dev)16698 static int i40e_resume(struct device *dev)
16699 {
16700 	struct i40e_pf *pf = dev_get_drvdata(dev);
16701 
16702 	/* If we're not suspended, then there is nothing to do */
16703 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16704 		return 0;
16705 	return i40e_io_resume(pf);
16706 }
16707 
16708 static const struct pci_error_handlers i40e_err_handler = {
16709 	.error_detected = i40e_pci_error_detected,
16710 	.slot_reset = i40e_pci_error_slot_reset,
16711 	.reset_prepare = i40e_pci_error_reset_prepare,
16712 	.reset_done = i40e_pci_error_reset_done,
16713 	.resume = i40e_pci_error_resume,
16714 };
16715 
16716 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16717 
16718 static struct pci_driver i40e_driver = {
16719 	.name     = i40e_driver_name,
16720 	.id_table = i40e_pci_tbl,
16721 	.probe    = i40e_probe,
16722 	.remove   = i40e_remove,
16723 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16724 	.shutdown = i40e_shutdown,
16725 	.err_handler = &i40e_err_handler,
16726 	.sriov_configure = i40e_pci_sriov_configure,
16727 };
16728 
16729 /**
16730  * i40e_init_module - Driver registration routine
16731  *
16732  * i40e_init_module is the first routine called when the driver is
16733  * loaded. All it does is register with the PCI subsystem.
16734  **/
i40e_init_module(void)16735 static int __init i40e_init_module(void)
16736 {
16737 	int err;
16738 
16739 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16740 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16741 
16742 	/* There is no need to throttle the number of active tasks because
16743 	 * each device limits its own task using a state bit for scheduling
16744 	 * the service task, and the device tasks do not interfere with each
16745 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16746 	 * since we need to be able to guarantee forward progress even under
16747 	 * memory pressure.
16748 	 */
16749 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16750 	if (!i40e_wq) {
16751 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16752 		return -ENOMEM;
16753 	}
16754 
16755 	i40e_dbg_init();
16756 	err = pci_register_driver(&i40e_driver);
16757 	if (err) {
16758 		destroy_workqueue(i40e_wq);
16759 		i40e_dbg_exit();
16760 		return err;
16761 	}
16762 
16763 	return 0;
16764 }
16765 module_init(i40e_init_module);
16766 
16767 /**
16768  * i40e_exit_module - Driver exit cleanup routine
16769  *
16770  * i40e_exit_module is called just before the driver is removed
16771  * from memory.
16772  **/
i40e_exit_module(void)16773 static void __exit i40e_exit_module(void)
16774 {
16775 	pci_unregister_driver(&i40e_driver);
16776 	destroy_workqueue(i40e_wq);
16777 	ida_destroy(&i40e_client_ida);
16778 	i40e_dbg_exit();
16779 }
16780 module_exit(i40e_exit_module);
16781