• 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_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
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 	int i;
314 
315 	for (i = 0; i < pf->num_alloc_vsi; i++)
316 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
317 			return pf->vsi[i];
318 
319 	return NULL;
320 }
321 
322 /**
323  * i40e_service_event_schedule - Schedule the service task to wake up
324  * @pf: board private structure
325  *
326  * If not already scheduled, this puts the task into the work queue
327  **/
i40e_service_event_schedule(struct i40e_pf * pf)328 void i40e_service_event_schedule(struct i40e_pf *pf)
329 {
330 	if ((!test_bit(__I40E_DOWN, pf->state) &&
331 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
332 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
333 		queue_work(i40e_wq, &pf->service_task);
334 }
335 
336 /**
337  * i40e_tx_timeout - Respond to a Tx Hang
338  * @netdev: network interface device structure
339  * @txqueue: queue number timing out
340  *
341  * If any port has noticed a Tx timeout, it is likely that the whole
342  * device is munged, not just the one netdev port, so go for the full
343  * reset.
344  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)345 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
346 {
347 	struct i40e_netdev_priv *np = netdev_priv(netdev);
348 	struct i40e_vsi *vsi = np->vsi;
349 	struct i40e_pf *pf = vsi->back;
350 	struct i40e_ring *tx_ring = NULL;
351 	unsigned int i;
352 	u32 head, val;
353 
354 	pf->tx_timeout_count++;
355 
356 	/* with txqueue index, find the tx_ring struct */
357 	for (i = 0; i < vsi->num_queue_pairs; i++) {
358 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
359 			if (txqueue ==
360 			    vsi->tx_rings[i]->queue_index) {
361 				tx_ring = vsi->tx_rings[i];
362 				break;
363 			}
364 		}
365 	}
366 
367 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
368 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
369 	else if (time_before(jiffies,
370 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
371 		return;   /* don't do any new action before the next timeout */
372 
373 	/* don't kick off another recovery if one is already pending */
374 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
375 		return;
376 
377 	if (tx_ring) {
378 		head = i40e_get_head(tx_ring);
379 		/* Read interrupt register */
380 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
381 			val = rd32(&pf->hw,
382 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
383 						tx_ring->vsi->base_vector - 1));
384 		else
385 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
386 
387 		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",
388 			    vsi->seid, txqueue, tx_ring->next_to_clean,
389 			    head, tx_ring->next_to_use,
390 			    readl(tx_ring->tail), val);
391 	}
392 
393 	pf->tx_timeout_last_recovery = jiffies;
394 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
395 		    pf->tx_timeout_recovery_level, txqueue);
396 
397 	switch (pf->tx_timeout_recovery_level) {
398 	case 1:
399 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
400 		break;
401 	case 2:
402 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
403 		break;
404 	case 3:
405 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
406 		break;
407 	default:
408 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
409 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
410 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
411 		break;
412 	}
413 
414 	i40e_service_event_schedule(pf);
415 	pf->tx_timeout_recovery_level++;
416 }
417 
418 /**
419  * i40e_get_vsi_stats_struct - Get System Network Statistics
420  * @vsi: the VSI we care about
421  *
422  * Returns the address of the device statistics structure.
423  * The statistics are actually updated from the service task.
424  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)425 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
426 {
427 	return &vsi->net_stats;
428 }
429 
430 /**
431  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
432  * @ring: Tx ring to get statistics from
433  * @stats: statistics entry to be updated
434  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)435 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
436 					    struct rtnl_link_stats64 *stats)
437 {
438 	u64 bytes, packets;
439 	unsigned int start;
440 
441 	do {
442 		start = u64_stats_fetch_begin(&ring->syncp);
443 		packets = ring->stats.packets;
444 		bytes   = ring->stats.bytes;
445 	} while (u64_stats_fetch_retry(&ring->syncp, start));
446 
447 	stats->tx_packets += packets;
448 	stats->tx_bytes   += bytes;
449 }
450 
451 /**
452  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
453  * @netdev: network interface device structure
454  * @stats: data structure to store statistics
455  *
456  * Returns the address of the device statistics structure.
457  * The statistics are actually updated from the service task.
458  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)459 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
460 				  struct rtnl_link_stats64 *stats)
461 {
462 	struct i40e_netdev_priv *np = netdev_priv(netdev);
463 	struct i40e_vsi *vsi = np->vsi;
464 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
465 	struct i40e_ring *ring;
466 	int i;
467 
468 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
469 		return;
470 
471 	if (!vsi->tx_rings)
472 		return;
473 
474 	rcu_read_lock();
475 	for (i = 0; i < vsi->num_queue_pairs; i++) {
476 		u64 bytes, packets;
477 		unsigned int start;
478 
479 		ring = READ_ONCE(vsi->tx_rings[i]);
480 		if (!ring)
481 			continue;
482 		i40e_get_netdev_stats_struct_tx(ring, stats);
483 
484 		if (i40e_enabled_xdp_vsi(vsi)) {
485 			ring = READ_ONCE(vsi->xdp_rings[i]);
486 			if (!ring)
487 				continue;
488 			i40e_get_netdev_stats_struct_tx(ring, stats);
489 		}
490 
491 		ring = READ_ONCE(vsi->rx_rings[i]);
492 		if (!ring)
493 			continue;
494 		do {
495 			start   = u64_stats_fetch_begin(&ring->syncp);
496 			packets = ring->stats.packets;
497 			bytes   = ring->stats.bytes;
498 		} while (u64_stats_fetch_retry(&ring->syncp, start));
499 
500 		stats->rx_packets += packets;
501 		stats->rx_bytes   += bytes;
502 
503 	}
504 	rcu_read_unlock();
505 
506 	/* following stats updated by i40e_watchdog_subtask() */
507 	stats->multicast	= vsi_stats->multicast;
508 	stats->tx_errors	= vsi_stats->tx_errors;
509 	stats->tx_dropped	= vsi_stats->tx_dropped;
510 	stats->rx_errors	= vsi_stats->rx_errors;
511 	stats->rx_dropped	= vsi_stats->rx_dropped;
512 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
513 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
514 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
515 }
516 
517 /**
518  * i40e_vsi_reset_stats - Resets all stats of the given vsi
519  * @vsi: the VSI to have its stats reset
520  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)521 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
522 {
523 	struct rtnl_link_stats64 *ns;
524 	int i;
525 
526 	if (!vsi)
527 		return;
528 
529 	ns = i40e_get_vsi_stats_struct(vsi);
530 	memset(ns, 0, sizeof(*ns));
531 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
532 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
533 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
534 	if (vsi->rx_rings && vsi->rx_rings[0]) {
535 		for (i = 0; i < vsi->num_queue_pairs; i++) {
536 			memset(&vsi->rx_rings[i]->stats, 0,
537 			       sizeof(vsi->rx_rings[i]->stats));
538 			memset(&vsi->rx_rings[i]->rx_stats, 0,
539 			       sizeof(vsi->rx_rings[i]->rx_stats));
540 			memset(&vsi->tx_rings[i]->stats, 0,
541 			       sizeof(vsi->tx_rings[i]->stats));
542 			memset(&vsi->tx_rings[i]->tx_stats, 0,
543 			       sizeof(vsi->tx_rings[i]->tx_stats));
544 		}
545 	}
546 	vsi->stat_offsets_loaded = false;
547 }
548 
549 /**
550  * i40e_pf_reset_stats - Reset all of the stats for the given PF
551  * @pf: the PF to be reset
552  **/
i40e_pf_reset_stats(struct i40e_pf * pf)553 void i40e_pf_reset_stats(struct i40e_pf *pf)
554 {
555 	int i;
556 
557 	memset(&pf->stats, 0, sizeof(pf->stats));
558 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
559 	pf->stat_offsets_loaded = false;
560 
561 	for (i = 0; i < I40E_MAX_VEB; i++) {
562 		if (pf->veb[i]) {
563 			memset(&pf->veb[i]->stats, 0,
564 			       sizeof(pf->veb[i]->stats));
565 			memset(&pf->veb[i]->stats_offsets, 0,
566 			       sizeof(pf->veb[i]->stats_offsets));
567 			memset(&pf->veb[i]->tc_stats, 0,
568 			       sizeof(pf->veb[i]->tc_stats));
569 			memset(&pf->veb[i]->tc_stats_offsets, 0,
570 			       sizeof(pf->veb[i]->tc_stats_offsets));
571 			pf->veb[i]->stat_offsets_loaded = false;
572 		}
573 	}
574 	pf->hw_csum_rx_error = 0;
575 }
576 
577 /**
578  * i40e_compute_pci_to_hw_id - compute index form PCI function.
579  * @vsi: ptr to the VSI to read from.
580  * @hw: ptr to the hardware info.
581  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)582 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
583 {
584 	int pf_count = i40e_get_pf_count(hw);
585 
586 	if (vsi->type == I40E_VSI_SRIOV)
587 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
588 
589 	return hw->port + BIT(7);
590 }
591 
592 /**
593  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
594  * @hw: ptr to the hardware info.
595  * @hireg: the high 32 bit reg to read.
596  * @loreg: the low 32 bit reg to read.
597  * @offset_loaded: has the initial offset been loaded yet.
598  * @offset: ptr to current offset value.
599  * @stat: ptr to the stat.
600  *
601  * Since the device stats are not reset at PFReset, they will not
602  * be zeroed when the driver starts.  We'll save the first values read
603  * and use them as offsets to be subtracted from the raw values in order
604  * to report stats that count from zero.
605  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)606 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
607 			       bool offset_loaded, u64 *offset, u64 *stat)
608 {
609 	u64 new_data;
610 
611 	new_data = rd64(hw, loreg);
612 
613 	if (!offset_loaded || new_data < *offset)
614 		*offset = new_data;
615 	*stat = new_data - *offset;
616 }
617 
618 /**
619  * i40e_stat_update48 - read and update a 48 bit stat from the chip
620  * @hw: ptr to the hardware info
621  * @hireg: the high 32 bit reg to read
622  * @loreg: the low 32 bit reg to read
623  * @offset_loaded: has the initial offset been loaded yet
624  * @offset: ptr to current offset value
625  * @stat: ptr to the stat
626  *
627  * Since the device stats are not reset at PFReset, they likely will not
628  * be zeroed when the driver starts.  We'll save the first values read
629  * and use them as offsets to be subtracted from the raw values in order
630  * to report stats that count from zero.  In the process, we also manage
631  * the potential roll-over.
632  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)633 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
634 			       bool offset_loaded, u64 *offset, u64 *stat)
635 {
636 	u64 new_data;
637 
638 	if (hw->device_id == I40E_DEV_ID_QEMU) {
639 		new_data = rd32(hw, loreg);
640 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
641 	} else {
642 		new_data = rd64(hw, loreg);
643 	}
644 	if (!offset_loaded)
645 		*offset = new_data;
646 	if (likely(new_data >= *offset))
647 		*stat = new_data - *offset;
648 	else
649 		*stat = (new_data + BIT_ULL(48)) - *offset;
650 	*stat &= 0xFFFFFFFFFFFFULL;
651 }
652 
653 /**
654  * i40e_stat_update32 - read and update a 32 bit stat from the chip
655  * @hw: ptr to the hardware info
656  * @reg: the hw reg to read
657  * @offset_loaded: has the initial offset been loaded yet
658  * @offset: ptr to current offset value
659  * @stat: ptr to the stat
660  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)661 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
662 			       bool offset_loaded, u64 *offset, u64 *stat)
663 {
664 	u32 new_data;
665 
666 	new_data = rd32(hw, reg);
667 	if (!offset_loaded)
668 		*offset = new_data;
669 	if (likely(new_data >= *offset))
670 		*stat = (u32)(new_data - *offset);
671 	else
672 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
673 }
674 
675 /**
676  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
677  * @hw: ptr to the hardware info
678  * @reg: the hw reg to read and clear
679  * @stat: ptr to the stat
680  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)681 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
682 {
683 	u32 new_data = rd32(hw, reg);
684 
685 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
686 	*stat += new_data;
687 }
688 
689 /**
690  * i40e_stats_update_rx_discards - update rx_discards.
691  * @vsi: ptr to the VSI to be updated.
692  * @hw: ptr to the hardware info.
693  * @stat_idx: VSI's stat_counter_idx.
694  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
695  * @stat_offset: ptr to stat_offset to store first read of specific register.
696  * @stat: ptr to VSI's stat to be updated.
697  **/
698 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)699 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
700 			      int stat_idx, bool offset_loaded,
701 			      struct i40e_eth_stats *stat_offset,
702 			      struct i40e_eth_stats *stat)
703 {
704 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
705 			   &stat_offset->rx_discards, &stat->rx_discards);
706 	i40e_stat_update64(hw,
707 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
708 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
709 			   offset_loaded, &stat_offset->rx_discards_other,
710 			   &stat->rx_discards_other);
711 }
712 
713 /**
714  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
715  * @vsi: the VSI to be updated
716  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)717 void i40e_update_eth_stats(struct i40e_vsi *vsi)
718 {
719 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
720 	struct i40e_pf *pf = vsi->back;
721 	struct i40e_hw *hw = &pf->hw;
722 	struct i40e_eth_stats *oes;
723 	struct i40e_eth_stats *es;     /* device's eth stats */
724 
725 	es = &vsi->eth_stats;
726 	oes = &vsi->eth_stats_offsets;
727 
728 	/* Gather up the stats that the hw collects */
729 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
730 			   vsi->stat_offsets_loaded,
731 			   &oes->tx_errors, &es->tx_errors);
732 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
733 			   vsi->stat_offsets_loaded,
734 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
735 
736 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
737 			   I40E_GLV_GORCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_bytes, &es->rx_bytes);
740 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
741 			   I40E_GLV_UPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_unicast, &es->rx_unicast);
744 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
745 			   I40E_GLV_MPRCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_multicast, &es->rx_multicast);
748 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
749 			   I40E_GLV_BPRCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->rx_broadcast, &es->rx_broadcast);
752 
753 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
754 			   I40E_GLV_GOTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_bytes, &es->tx_bytes);
757 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
758 			   I40E_GLV_UPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_unicast, &es->tx_unicast);
761 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
762 			   I40E_GLV_MPTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_multicast, &es->tx_multicast);
765 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
766 			   I40E_GLV_BPTCL(stat_idx),
767 			   vsi->stat_offsets_loaded,
768 			   &oes->tx_broadcast, &es->tx_broadcast);
769 
770 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
771 				      vsi->stat_offsets_loaded, oes, es);
772 
773 	vsi->stat_offsets_loaded = true;
774 }
775 
776 /**
777  * i40e_update_veb_stats - Update Switch component statistics
778  * @veb: the VEB being updated
779  **/
i40e_update_veb_stats(struct i40e_veb * veb)780 void i40e_update_veb_stats(struct i40e_veb *veb)
781 {
782 	struct i40e_pf *pf = veb->pf;
783 	struct i40e_hw *hw = &pf->hw;
784 	struct i40e_eth_stats *oes;
785 	struct i40e_eth_stats *es;     /* device's eth stats */
786 	struct i40e_veb_tc_stats *veb_oes;
787 	struct i40e_veb_tc_stats *veb_es;
788 	int i, idx = 0;
789 
790 	idx = veb->stats_idx;
791 	es = &veb->stats;
792 	oes = &veb->stats_offsets;
793 	veb_es = &veb->tc_stats;
794 	veb_oes = &veb->tc_stats_offsets;
795 
796 	/* Gather up the stats that the hw collects */
797 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->tx_discards, &es->tx_discards);
800 	if (hw->revision_id > 0)
801 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
802 				   veb->stat_offsets_loaded,
803 				   &oes->rx_unknown_protocol,
804 				   &es->rx_unknown_protocol);
805 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
806 			   veb->stat_offsets_loaded,
807 			   &oes->rx_bytes, &es->rx_bytes);
808 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
809 			   veb->stat_offsets_loaded,
810 			   &oes->rx_unicast, &es->rx_unicast);
811 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
812 			   veb->stat_offsets_loaded,
813 			   &oes->rx_multicast, &es->rx_multicast);
814 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
815 			   veb->stat_offsets_loaded,
816 			   &oes->rx_broadcast, &es->rx_broadcast);
817 
818 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
819 			   veb->stat_offsets_loaded,
820 			   &oes->tx_bytes, &es->tx_bytes);
821 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
822 			   veb->stat_offsets_loaded,
823 			   &oes->tx_unicast, &es->tx_unicast);
824 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
825 			   veb->stat_offsets_loaded,
826 			   &oes->tx_multicast, &es->tx_multicast);
827 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
828 			   veb->stat_offsets_loaded,
829 			   &oes->tx_broadcast, &es->tx_broadcast);
830 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
831 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
832 				   I40E_GLVEBTC_RPCL(i, idx),
833 				   veb->stat_offsets_loaded,
834 				   &veb_oes->tc_rx_packets[i],
835 				   &veb_es->tc_rx_packets[i]);
836 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
837 				   I40E_GLVEBTC_RBCL(i, idx),
838 				   veb->stat_offsets_loaded,
839 				   &veb_oes->tc_rx_bytes[i],
840 				   &veb_es->tc_rx_bytes[i]);
841 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
842 				   I40E_GLVEBTC_TPCL(i, idx),
843 				   veb->stat_offsets_loaded,
844 				   &veb_oes->tc_tx_packets[i],
845 				   &veb_es->tc_tx_packets[i]);
846 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
847 				   I40E_GLVEBTC_TBCL(i, idx),
848 				   veb->stat_offsets_loaded,
849 				   &veb_oes->tc_tx_bytes[i],
850 				   &veb_es->tc_tx_bytes[i]);
851 	}
852 	veb->stat_offsets_loaded = true;
853 }
854 
855 /**
856  * i40e_update_vsi_stats - Update the vsi statistics counters.
857  * @vsi: the VSI to be updated
858  *
859  * There are a few instances where we store the same stat in a
860  * couple of different structs.  This is partly because we have
861  * the netdev stats that need to be filled out, which is slightly
862  * different from the "eth_stats" defined by the chip and used in
863  * VF communications.  We sort it out here.
864  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)865 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
866 {
867 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
868 	struct i40e_pf *pf = vsi->back;
869 	struct rtnl_link_stats64 *ons;
870 	struct rtnl_link_stats64 *ns;   /* netdev stats */
871 	struct i40e_eth_stats *oes;
872 	struct i40e_eth_stats *es;     /* device's eth stats */
873 	u64 tx_restart, tx_busy;
874 	struct i40e_ring *p;
875 	u64 bytes, packets;
876 	unsigned int start;
877 	u64 tx_linearize;
878 	u64 tx_force_wb;
879 	u64 tx_stopped;
880 	u64 rx_p, rx_b;
881 	u64 tx_p, tx_b;
882 	u16 q;
883 
884 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
885 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
886 		return;
887 
888 	ns = i40e_get_vsi_stats_struct(vsi);
889 	ons = &vsi->net_stats_offsets;
890 	es = &vsi->eth_stats;
891 	oes = &vsi->eth_stats_offsets;
892 
893 	/* Gather up the netdev and vsi stats that the driver collects
894 	 * on the fly during packet processing
895 	 */
896 	rx_b = rx_p = 0;
897 	tx_b = tx_p = 0;
898 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
899 	tx_stopped = 0;
900 	rx_page = 0;
901 	rx_buf = 0;
902 	rx_reuse = 0;
903 	rx_alloc = 0;
904 	rx_waive = 0;
905 	rx_busy = 0;
906 	rcu_read_lock();
907 	for (q = 0; q < vsi->num_queue_pairs; q++) {
908 		/* locate Tx ring */
909 		p = READ_ONCE(vsi->tx_rings[q]);
910 		if (!p)
911 			continue;
912 
913 		do {
914 			start = u64_stats_fetch_begin(&p->syncp);
915 			packets = p->stats.packets;
916 			bytes = p->stats.bytes;
917 		} while (u64_stats_fetch_retry(&p->syncp, start));
918 		tx_b += bytes;
919 		tx_p += packets;
920 		tx_restart += p->tx_stats.restart_queue;
921 		tx_busy += p->tx_stats.tx_busy;
922 		tx_linearize += p->tx_stats.tx_linearize;
923 		tx_force_wb += p->tx_stats.tx_force_wb;
924 		tx_stopped += p->tx_stats.tx_stopped;
925 
926 		/* locate Rx ring */
927 		p = READ_ONCE(vsi->rx_rings[q]);
928 		if (!p)
929 			continue;
930 
931 		do {
932 			start = u64_stats_fetch_begin(&p->syncp);
933 			packets = p->stats.packets;
934 			bytes = p->stats.bytes;
935 		} while (u64_stats_fetch_retry(&p->syncp, start));
936 		rx_b += bytes;
937 		rx_p += packets;
938 		rx_buf += p->rx_stats.alloc_buff_failed;
939 		rx_page += p->rx_stats.alloc_page_failed;
940 		rx_reuse += p->rx_stats.page_reuse_count;
941 		rx_alloc += p->rx_stats.page_alloc_count;
942 		rx_waive += p->rx_stats.page_waive_count;
943 		rx_busy += p->rx_stats.page_busy_count;
944 
945 		if (i40e_enabled_xdp_vsi(vsi)) {
946 			/* locate XDP ring */
947 			p = READ_ONCE(vsi->xdp_rings[q]);
948 			if (!p)
949 				continue;
950 
951 			do {
952 				start = u64_stats_fetch_begin(&p->syncp);
953 				packets = p->stats.packets;
954 				bytes = p->stats.bytes;
955 			} while (u64_stats_fetch_retry(&p->syncp, start));
956 			tx_b += bytes;
957 			tx_p += packets;
958 			tx_restart += p->tx_stats.restart_queue;
959 			tx_busy += p->tx_stats.tx_busy;
960 			tx_linearize += p->tx_stats.tx_linearize;
961 			tx_force_wb += p->tx_stats.tx_force_wb;
962 		}
963 	}
964 	rcu_read_unlock();
965 	vsi->tx_restart = tx_restart;
966 	vsi->tx_busy = tx_busy;
967 	vsi->tx_linearize = tx_linearize;
968 	vsi->tx_force_wb = tx_force_wb;
969 	vsi->tx_stopped = tx_stopped;
970 	vsi->rx_page_failed = rx_page;
971 	vsi->rx_buf_failed = rx_buf;
972 	vsi->rx_page_reuse = rx_reuse;
973 	vsi->rx_page_alloc = rx_alloc;
974 	vsi->rx_page_waive = rx_waive;
975 	vsi->rx_page_busy = rx_busy;
976 
977 	ns->rx_packets = rx_p;
978 	ns->rx_bytes = rx_b;
979 	ns->tx_packets = tx_p;
980 	ns->tx_bytes = tx_b;
981 
982 	/* update netdev stats from eth stats */
983 	i40e_update_eth_stats(vsi);
984 	ons->tx_errors = oes->tx_errors;
985 	ns->tx_errors = es->tx_errors;
986 	ons->multicast = oes->rx_multicast;
987 	ns->multicast = es->rx_multicast;
988 	ons->rx_dropped = oes->rx_discards_other;
989 	ns->rx_dropped = es->rx_discards_other;
990 	ons->rx_missed_errors = oes->rx_discards;
991 	ns->rx_missed_errors = es->rx_discards;
992 	ons->tx_dropped = oes->tx_discards;
993 	ns->tx_dropped = es->tx_discards;
994 
995 	/* pull in a couple PF stats if this is the main vsi */
996 	if (vsi == pf->vsi[pf->lan_vsi]) {
997 		ns->rx_crc_errors = pf->stats.crc_errors;
998 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
999 		ns->rx_length_errors = pf->stats.rx_length_errors;
1000 	}
1001 }
1002 
1003 /**
1004  * i40e_update_pf_stats - Update the PF statistics counters.
1005  * @pf: the PF to be updated
1006  **/
i40e_update_pf_stats(struct i40e_pf * pf)1007 static void i40e_update_pf_stats(struct i40e_pf *pf)
1008 {
1009 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1010 	struct i40e_hw_port_stats *nsd = &pf->stats;
1011 	struct i40e_hw *hw = &pf->hw;
1012 	u32 val;
1013 	int i;
1014 
1015 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1016 			   I40E_GLPRT_GORCL(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1019 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1020 			   I40E_GLPRT_GOTCL(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1023 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->eth.rx_discards,
1026 			   &nsd->eth.rx_discards);
1027 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1028 			   I40E_GLPRT_UPRCL(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->eth.rx_unicast,
1031 			   &nsd->eth.rx_unicast);
1032 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1033 			   I40E_GLPRT_MPRCL(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->eth.rx_multicast,
1036 			   &nsd->eth.rx_multicast);
1037 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1038 			   I40E_GLPRT_BPRCL(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->eth.rx_broadcast,
1041 			   &nsd->eth.rx_broadcast);
1042 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1043 			   I40E_GLPRT_UPTCL(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->eth.tx_unicast,
1046 			   &nsd->eth.tx_unicast);
1047 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1048 			   I40E_GLPRT_MPTCL(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->eth.tx_multicast,
1051 			   &nsd->eth.tx_multicast);
1052 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1053 			   I40E_GLPRT_BPTCL(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->eth.tx_broadcast,
1056 			   &nsd->eth.tx_broadcast);
1057 
1058 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->tx_dropped_link_down,
1061 			   &nsd->tx_dropped_link_down);
1062 
1063 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->crc_errors, &nsd->crc_errors);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->mac_local_faults,
1074 			   &nsd->mac_local_faults);
1075 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->mac_remote_faults,
1078 			   &nsd->mac_remote_faults);
1079 
1080 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_length_errors,
1083 			   &nsd->rx_length_errors);
1084 
1085 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1086 			   pf->stat_offsets_loaded,
1087 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1088 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1091 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1094 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1097 
1098 	for (i = 0; i < 8; i++) {
1099 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1100 				   pf->stat_offsets_loaded,
1101 				   &osd->priority_xoff_rx[i],
1102 				   &nsd->priority_xoff_rx[i]);
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xon_rx[i],
1106 				   &nsd->priority_xon_rx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xon_tx[i],
1110 				   &nsd->priority_xon_tx[i]);
1111 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1112 				   pf->stat_offsets_loaded,
1113 				   &osd->priority_xoff_tx[i],
1114 				   &nsd->priority_xoff_tx[i]);
1115 		i40e_stat_update32(hw,
1116 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1117 				   pf->stat_offsets_loaded,
1118 				   &osd->priority_xon_2_xoff[i],
1119 				   &nsd->priority_xon_2_xoff[i]);
1120 	}
1121 
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1123 			   I40E_GLPRT_PRC64L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_64, &nsd->rx_size_64);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1127 			   I40E_GLPRT_PRC127L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_127, &nsd->rx_size_127);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1131 			   I40E_GLPRT_PRC255L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_255, &nsd->rx_size_255);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1135 			   I40E_GLPRT_PRC511L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_511, &nsd->rx_size_511);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1139 			   I40E_GLPRT_PRC1023L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1143 			   I40E_GLPRT_PRC1522L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1147 			   I40E_GLPRT_PRC9522L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->rx_size_big, &nsd->rx_size_big);
1150 
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1152 			   I40E_GLPRT_PTC64L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_64, &nsd->tx_size_64);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1156 			   I40E_GLPRT_PTC127L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_127, &nsd->tx_size_127);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1160 			   I40E_GLPRT_PTC255L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_255, &nsd->tx_size_255);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1164 			   I40E_GLPRT_PTC511L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_511, &nsd->tx_size_511);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1168 			   I40E_GLPRT_PTC1023L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1172 			   I40E_GLPRT_PTC1522L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1175 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1176 			   I40E_GLPRT_PTC9522L(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_size_big, &nsd->tx_size_big);
1179 
1180 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->rx_undersize, &nsd->rx_undersize);
1183 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1184 			   pf->stat_offsets_loaded,
1185 			   &osd->rx_fragments, &nsd->rx_fragments);
1186 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1187 			   pf->stat_offsets_loaded,
1188 			   &osd->rx_oversize, &nsd->rx_oversize);
1189 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1190 			   pf->stat_offsets_loaded,
1191 			   &osd->rx_jabber, &nsd->rx_jabber);
1192 
1193 	/* FDIR stats */
1194 	i40e_stat_update_and_clear32(hw,
1195 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1196 			&nsd->fd_atr_match);
1197 	i40e_stat_update_and_clear32(hw,
1198 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1199 			&nsd->fd_sb_match);
1200 	i40e_stat_update_and_clear32(hw,
1201 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1202 			&nsd->fd_atr_tunnel_match);
1203 
1204 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1205 	nsd->tx_lpi_status =
1206 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1207 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1208 	nsd->rx_lpi_status =
1209 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1210 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1211 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1212 			   pf->stat_offsets_loaded,
1213 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1214 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1215 			   pf->stat_offsets_loaded,
1216 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1217 
1218 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1219 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1220 		nsd->fd_sb_status = true;
1221 	else
1222 		nsd->fd_sb_status = false;
1223 
1224 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1225 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1226 		nsd->fd_atr_status = true;
1227 	else
1228 		nsd->fd_atr_status = false;
1229 
1230 	pf->stat_offsets_loaded = true;
1231 }
1232 
1233 /**
1234  * i40e_update_stats - Update the various statistics counters.
1235  * @vsi: the VSI to be updated
1236  *
1237  * Update the various stats for this VSI and its related entities.
1238  **/
i40e_update_stats(struct i40e_vsi * vsi)1239 void i40e_update_stats(struct i40e_vsi *vsi)
1240 {
1241 	struct i40e_pf *pf = vsi->back;
1242 
1243 	if (vsi == pf->vsi[pf->lan_vsi])
1244 		i40e_update_pf_stats(pf);
1245 
1246 	i40e_update_vsi_stats(vsi);
1247 }
1248 
1249 /**
1250  * i40e_count_filters - counts VSI mac filters
1251  * @vsi: the VSI to be searched
1252  *
1253  * Returns count of mac filters
1254  **/
i40e_count_filters(struct i40e_vsi * vsi)1255 int i40e_count_filters(struct i40e_vsi *vsi)
1256 {
1257 	struct i40e_mac_filter *f;
1258 	struct hlist_node *h;
1259 	int bkt;
1260 	int cnt = 0;
1261 
1262 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1263 		if (f->state == I40E_FILTER_NEW ||
1264 		    f->state == I40E_FILTER_NEW_SYNC ||
1265 		    f->state == I40E_FILTER_ACTIVE)
1266 			++cnt;
1267 	}
1268 
1269 	return cnt;
1270 }
1271 
1272 /**
1273  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1274  * @vsi: the VSI to be searched
1275  * @macaddr: the MAC address
1276  * @vlan: the vlan
1277  *
1278  * Returns ptr to the filter object or NULL
1279  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1280 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1281 						const u8 *macaddr, s16 vlan)
1282 {
1283 	struct i40e_mac_filter *f;
1284 	u64 key;
1285 
1286 	if (!vsi || !macaddr)
1287 		return NULL;
1288 
1289 	key = i40e_addr_to_hkey(macaddr);
1290 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1291 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1292 		    (vlan == f->vlan))
1293 			return f;
1294 	}
1295 	return NULL;
1296 }
1297 
1298 /**
1299  * i40e_find_mac - Find a mac addr in the macvlan filters list
1300  * @vsi: the VSI to be searched
1301  * @macaddr: the MAC address we are searching for
1302  *
1303  * Returns the first filter with the provided MAC address or NULL if
1304  * MAC address was not found
1305  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1306 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1307 {
1308 	struct i40e_mac_filter *f;
1309 	u64 key;
1310 
1311 	if (!vsi || !macaddr)
1312 		return NULL;
1313 
1314 	key = i40e_addr_to_hkey(macaddr);
1315 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1316 		if ((ether_addr_equal(macaddr, f->macaddr)))
1317 			return f;
1318 	}
1319 	return NULL;
1320 }
1321 
1322 /**
1323  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1324  * @vsi: the VSI to be searched
1325  *
1326  * Returns true if VSI is in vlan mode or false otherwise
1327  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1328 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1329 {
1330 	/* If we have a PVID, always operate in VLAN mode */
1331 	if (vsi->info.pvid)
1332 		return true;
1333 
1334 	/* We need to operate in VLAN mode whenever we have any filters with
1335 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1336 	 * time, incurring search cost repeatedly. However, we can notice two
1337 	 * things:
1338 	 *
1339 	 * 1) the only place where we can gain a VLAN filter is in
1340 	 *    i40e_add_filter.
1341 	 *
1342 	 * 2) the only place where filters are actually removed is in
1343 	 *    i40e_sync_filters_subtask.
1344 	 *
1345 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1346 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1347 	 * we have to perform the full search after deleting filters in
1348 	 * i40e_sync_filters_subtask, but we already have to search
1349 	 * filters here and can perform the check at the same time. This
1350 	 * results in avoiding embedding a loop for VLAN mode inside another
1351 	 * loop over all the filters, and should maintain correctness as noted
1352 	 * above.
1353 	 */
1354 	return vsi->has_vlan_filter;
1355 }
1356 
1357 /**
1358  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1359  * @vsi: the VSI to configure
1360  * @tmp_add_list: list of filters ready to be added
1361  * @tmp_del_list: list of filters ready to be deleted
1362  * @vlan_filters: the number of active VLAN filters
1363  *
1364  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1365  * behave as expected. If we have any active VLAN filters remaining or about
1366  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1367  * so that they only match against untagged traffic. If we no longer have any
1368  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1369  * so that they match against both tagged and untagged traffic. In this way,
1370  * we ensure that we correctly receive the desired traffic. This ensures that
1371  * when we have an active VLAN we will receive only untagged traffic and
1372  * traffic matching active VLANs. If we have no active VLANs then we will
1373  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1374  *
1375  * Finally, in a similar fashion, this function also corrects filters when
1376  * there is an active PVID assigned to this VSI.
1377  *
1378  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1379  *
1380  * This function is only expected to be called from within
1381  * i40e_sync_vsi_filters.
1382  *
1383  * NOTE: This function expects to be called while under the
1384  * mac_filter_hash_lock
1385  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1386 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1387 					 struct hlist_head *tmp_add_list,
1388 					 struct hlist_head *tmp_del_list,
1389 					 int vlan_filters)
1390 {
1391 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1392 	struct i40e_mac_filter *f, *add_head;
1393 	struct i40e_new_mac_filter *new;
1394 	struct hlist_node *h;
1395 	int bkt, new_vlan;
1396 
1397 	/* To determine if a particular filter needs to be replaced we
1398 	 * have the three following conditions:
1399 	 *
1400 	 * a) if we have a PVID assigned, then all filters which are
1401 	 *    not marked as VLAN=PVID must be replaced with filters that
1402 	 *    are.
1403 	 * b) otherwise, if we have any active VLANS, all filters
1404 	 *    which are marked as VLAN=-1 must be replaced with
1405 	 *    filters marked as VLAN=0
1406 	 * c) finally, if we do not have any active VLANS, all filters
1407 	 *    which are marked as VLAN=0 must be replaced with filters
1408 	 *    marked as VLAN=-1
1409 	 */
1410 
1411 	/* Update the filters about to be added in place */
1412 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1413 		if (pvid && new->f->vlan != pvid)
1414 			new->f->vlan = pvid;
1415 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1416 			new->f->vlan = 0;
1417 		else if (!vlan_filters && new->f->vlan == 0)
1418 			new->f->vlan = I40E_VLAN_ANY;
1419 	}
1420 
1421 	/* Update the remaining active filters */
1422 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1423 		/* Combine the checks for whether a filter needs to be changed
1424 		 * and then determine the new VLAN inside the if block, in
1425 		 * order to avoid duplicating code for adding the new filter
1426 		 * then deleting the old filter.
1427 		 */
1428 		if ((pvid && f->vlan != pvid) ||
1429 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1430 		    (!vlan_filters && f->vlan == 0)) {
1431 			/* Determine the new vlan we will be adding */
1432 			if (pvid)
1433 				new_vlan = pvid;
1434 			else if (vlan_filters)
1435 				new_vlan = 0;
1436 			else
1437 				new_vlan = I40E_VLAN_ANY;
1438 
1439 			/* Create the new filter */
1440 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1441 			if (!add_head)
1442 				return -ENOMEM;
1443 
1444 			/* Create a temporary i40e_new_mac_filter */
1445 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1446 			if (!new)
1447 				return -ENOMEM;
1448 
1449 			new->f = add_head;
1450 			new->state = add_head->state;
1451 			if (add_head->state == I40E_FILTER_NEW)
1452 				add_head->state = I40E_FILTER_NEW_SYNC;
1453 
1454 			/* Add the new filter to the tmp list */
1455 			hlist_add_head(&new->hlist, tmp_add_list);
1456 
1457 			/* Put the original filter into the delete list */
1458 			f->state = I40E_FILTER_REMOVE;
1459 			hash_del(&f->hlist);
1460 			hlist_add_head(&f->hlist, tmp_del_list);
1461 		}
1462 	}
1463 
1464 	vsi->has_vlan_filter = !!vlan_filters;
1465 
1466 	return 0;
1467 }
1468 
1469 /**
1470  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1471  * @vsi: the vsi to configure
1472  * @new_mac: new mac filter to be added
1473  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1474  * @vlan_filters: the number of active VLAN filters
1475  * @trusted: flag if the VF is trusted
1476  *
1477  * Get new VLAN id based on current VLAN filters, trust, PVID
1478  * and vf-vlan-prune-disable flag.
1479  *
1480  * Returns the value of the new vlan filter or
1481  * the old value if no new filter is needed.
1482  */
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)1483 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1484 				struct i40e_new_mac_filter *new_mac,
1485 				struct i40e_mac_filter *f,
1486 				int vlan_filters,
1487 				bool trusted)
1488 {
1489 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1490 	struct i40e_pf *pf = vsi->back;
1491 	bool is_any;
1492 
1493 	if (new_mac)
1494 		f = new_mac->f;
1495 
1496 	if (pvid && f->vlan != pvid)
1497 		return pvid;
1498 
1499 	is_any = (trusted ||
1500 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1501 
1502 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1503 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1504 	    (is_any && !vlan_filters && f->vlan == 0)) {
1505 		if (is_any)
1506 			return I40E_VLAN_ANY;
1507 		else
1508 			return 0;
1509 	}
1510 
1511 	return f->vlan;
1512 }
1513 
1514 /**
1515  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1516  * @vsi: the vsi to configure
1517  * @tmp_add_list: list of filters ready to be added
1518  * @tmp_del_list: list of filters ready to be deleted
1519  * @vlan_filters: the number of active VLAN filters
1520  * @trusted: flag if the VF is trusted
1521  *
1522  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1523  * and vf-vlan-prune-disable flag.
1524  *
1525  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1526  *
1527  * This function is only expected to be called from within
1528  * i40e_sync_vsi_filters.
1529  *
1530  * NOTE: This function expects to be called while under the
1531  * mac_filter_hash_lock
1532  */
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)1533 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1534 					    struct hlist_head *tmp_add_list,
1535 					    struct hlist_head *tmp_del_list,
1536 					    int vlan_filters,
1537 					    bool trusted)
1538 {
1539 	struct i40e_mac_filter *f, *add_head;
1540 	struct i40e_new_mac_filter *new_mac;
1541 	struct hlist_node *h;
1542 	int bkt, new_vlan;
1543 
1544 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1545 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1546 							vlan_filters, trusted);
1547 	}
1548 
1549 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1550 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1551 						trusted);
1552 		if (new_vlan != f->vlan) {
1553 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1554 			if (!add_head)
1555 				return -ENOMEM;
1556 			/* Create a temporary i40e_new_mac_filter */
1557 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1558 			if (!new_mac)
1559 				return -ENOMEM;
1560 			new_mac->f = add_head;
1561 			new_mac->state = add_head->state;
1562 			if (add_head->state == I40E_FILTER_NEW)
1563 				add_head->state = I40E_FILTER_NEW_SYNC;
1564 
1565 			/* Add the new filter to the tmp list */
1566 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1567 
1568 			/* Put the original filter into the delete list */
1569 			f->state = I40E_FILTER_REMOVE;
1570 			hash_del(&f->hlist);
1571 			hlist_add_head(&f->hlist, tmp_del_list);
1572 		}
1573 	}
1574 
1575 	vsi->has_vlan_filter = !!vlan_filters;
1576 	return 0;
1577 }
1578 
1579 /**
1580  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1581  * @vsi: the PF Main VSI - inappropriate for any other VSI
1582  * @macaddr: the MAC address
1583  *
1584  * Remove whatever filter the firmware set up so the driver can manage
1585  * its own filtering intelligently.
1586  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1587 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1588 {
1589 	struct i40e_aqc_remove_macvlan_element_data element;
1590 	struct i40e_pf *pf = vsi->back;
1591 
1592 	/* Only appropriate for the PF main VSI */
1593 	if (vsi->type != I40E_VSI_MAIN)
1594 		return;
1595 
1596 	memset(&element, 0, sizeof(element));
1597 	ether_addr_copy(element.mac_addr, macaddr);
1598 	element.vlan_tag = 0;
1599 	/* Ignore error returns, some firmware does it this way... */
1600 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1601 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1602 
1603 	memset(&element, 0, sizeof(element));
1604 	ether_addr_copy(element.mac_addr, macaddr);
1605 	element.vlan_tag = 0;
1606 	/* ...and some firmware does it this way. */
1607 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1608 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1609 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1610 }
1611 
1612 /**
1613  * i40e_add_filter - Add a mac/vlan filter to the VSI
1614  * @vsi: the VSI to be searched
1615  * @macaddr: the MAC address
1616  * @vlan: the vlan
1617  *
1618  * Returns ptr to the filter object or NULL when no memory available.
1619  *
1620  * NOTE: This function is expected to be called with mac_filter_hash_lock
1621  * being held.
1622  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1623 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1624 					const u8 *macaddr, s16 vlan)
1625 {
1626 	struct i40e_mac_filter *f;
1627 	u64 key;
1628 
1629 	if (!vsi || !macaddr)
1630 		return NULL;
1631 
1632 	f = i40e_find_filter(vsi, macaddr, vlan);
1633 	if (!f) {
1634 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1635 		if (!f)
1636 			return NULL;
1637 
1638 		/* Update the boolean indicating if we need to function in
1639 		 * VLAN mode.
1640 		 */
1641 		if (vlan >= 0)
1642 			vsi->has_vlan_filter = true;
1643 
1644 		ether_addr_copy(f->macaddr, macaddr);
1645 		f->vlan = vlan;
1646 		f->state = I40E_FILTER_NEW;
1647 		INIT_HLIST_NODE(&f->hlist);
1648 
1649 		key = i40e_addr_to_hkey(macaddr);
1650 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1651 
1652 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1653 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1654 	}
1655 
1656 	/* If we're asked to add a filter that has been marked for removal, it
1657 	 * is safe to simply restore it to active state. __i40e_del_filter
1658 	 * will have simply deleted any filters which were previously marked
1659 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1660 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1661 	 * task, just restore this filter to the ACTIVE state so that the
1662 	 * sync task leaves it in place
1663 	 */
1664 	if (f->state == I40E_FILTER_REMOVE)
1665 		f->state = I40E_FILTER_ACTIVE;
1666 
1667 	return f;
1668 }
1669 
1670 /**
1671  * __i40e_del_filter - Remove a specific filter from the VSI
1672  * @vsi: VSI to remove from
1673  * @f: the filter to remove from the list
1674  *
1675  * This function should be called instead of i40e_del_filter only if you know
1676  * the exact filter you will remove already, such as via i40e_find_filter or
1677  * i40e_find_mac.
1678  *
1679  * NOTE: This function is expected to be called with mac_filter_hash_lock
1680  * being held.
1681  * ANOTHER NOTE: This function MUST be called from within the context of
1682  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1683  * instead of list_for_each_entry().
1684  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1685 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1686 {
1687 	if (!f)
1688 		return;
1689 
1690 	/* If the filter was never added to firmware then we can just delete it
1691 	 * directly and we don't want to set the status to remove or else an
1692 	 * admin queue command will unnecessarily fire.
1693 	 */
1694 	if ((f->state == I40E_FILTER_FAILED) ||
1695 	    (f->state == I40E_FILTER_NEW)) {
1696 		hash_del(&f->hlist);
1697 		kfree(f);
1698 	} else {
1699 		f->state = I40E_FILTER_REMOVE;
1700 	}
1701 
1702 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1703 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1704 }
1705 
1706 /**
1707  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1708  * @vsi: the VSI to be searched
1709  * @macaddr: the MAC address
1710  * @vlan: the VLAN
1711  *
1712  * NOTE: This function is expected to be called with mac_filter_hash_lock
1713  * being held.
1714  * ANOTHER NOTE: This function MUST be called from within the context of
1715  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1716  * instead of list_for_each_entry().
1717  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1718 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1719 {
1720 	struct i40e_mac_filter *f;
1721 
1722 	if (!vsi || !macaddr)
1723 		return;
1724 
1725 	f = i40e_find_filter(vsi, macaddr, vlan);
1726 	__i40e_del_filter(vsi, f);
1727 }
1728 
1729 /**
1730  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1731  * @vsi: the VSI to be searched
1732  * @macaddr: the mac address to be filtered
1733  *
1734  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1735  * go through all the macvlan filters and add a macvlan filter for each
1736  * unique vlan that already exists. If a PVID has been assigned, instead only
1737  * add the macaddr to that VLAN.
1738  *
1739  * Returns last filter added on success, else NULL
1740  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1741 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1742 					    const u8 *macaddr)
1743 {
1744 	struct i40e_mac_filter *f, *add = NULL;
1745 	struct hlist_node *h;
1746 	int bkt;
1747 
1748 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1749 	if (vsi->info.pvid)
1750 		return i40e_add_filter(vsi, macaddr,
1751 				       le16_to_cpu(vsi->info.pvid));
1752 
1753 	if (!i40e_is_vsi_in_vlan(vsi))
1754 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1755 
1756 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1757 		if (f->state == I40E_FILTER_REMOVE)
1758 			continue;
1759 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1760 		if (!add)
1761 			return NULL;
1762 	}
1763 
1764 	return add;
1765 }
1766 
1767 /**
1768  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1769  * @vsi: the VSI to be searched
1770  * @macaddr: the mac address to be removed
1771  *
1772  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1773  * associated with.
1774  *
1775  * Returns 0 for success, or error
1776  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1777 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1778 {
1779 	struct i40e_mac_filter *f;
1780 	struct hlist_node *h;
1781 	bool found = false;
1782 	int bkt;
1783 
1784 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1785 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1786 		if (ether_addr_equal(macaddr, f->macaddr)) {
1787 			__i40e_del_filter(vsi, f);
1788 			found = true;
1789 		}
1790 	}
1791 
1792 	if (found)
1793 		return 0;
1794 	else
1795 		return -ENOENT;
1796 }
1797 
1798 /**
1799  * i40e_set_mac - NDO callback to set mac address
1800  * @netdev: network interface device structure
1801  * @p: pointer to an address structure
1802  *
1803  * Returns 0 on success, negative on failure
1804  **/
i40e_set_mac(struct net_device * netdev,void * p)1805 static int i40e_set_mac(struct net_device *netdev, void *p)
1806 {
1807 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1808 	struct i40e_vsi *vsi = np->vsi;
1809 	struct i40e_pf *pf = vsi->back;
1810 	struct i40e_hw *hw = &pf->hw;
1811 	struct sockaddr *addr = p;
1812 
1813 	if (!is_valid_ether_addr(addr->sa_data))
1814 		return -EADDRNOTAVAIL;
1815 
1816 	if (test_bit(__I40E_DOWN, pf->state) ||
1817 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1818 		return -EADDRNOTAVAIL;
1819 
1820 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1821 		netdev_info(netdev, "returning to hw mac address %pM\n",
1822 			    hw->mac.addr);
1823 	else
1824 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1825 
1826 	/* Copy the address first, so that we avoid a possible race with
1827 	 * .set_rx_mode().
1828 	 * - Remove old address from MAC filter
1829 	 * - Copy new address
1830 	 * - Add new address to MAC filter
1831 	 */
1832 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1833 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1834 	eth_hw_addr_set(netdev, addr->sa_data);
1835 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1836 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1837 
1838 	if (vsi->type == I40E_VSI_MAIN) {
1839 		int ret;
1840 
1841 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1842 						addr->sa_data, NULL);
1843 		if (ret)
1844 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1845 				    ERR_PTR(ret),
1846 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1847 	}
1848 
1849 	/* schedule our worker thread which will take care of
1850 	 * applying the new filter changes
1851 	 */
1852 	i40e_service_event_schedule(pf);
1853 	return 0;
1854 }
1855 
1856 /**
1857  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1858  * @vsi: vsi structure
1859  * @seed: RSS hash seed
1860  * @lut: pointer to lookup table of lut_size
1861  * @lut_size: size of the lookup table
1862  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1863 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1864 			      u8 *lut, u16 lut_size)
1865 {
1866 	struct i40e_pf *pf = vsi->back;
1867 	struct i40e_hw *hw = &pf->hw;
1868 	int ret = 0;
1869 
1870 	if (seed) {
1871 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1872 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1873 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1874 		if (ret) {
1875 			dev_info(&pf->pdev->dev,
1876 				 "Cannot set RSS key, err %pe aq_err %s\n",
1877 				 ERR_PTR(ret),
1878 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1879 			return ret;
1880 		}
1881 	}
1882 	if (lut) {
1883 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1884 
1885 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1886 		if (ret) {
1887 			dev_info(&pf->pdev->dev,
1888 				 "Cannot set RSS lut, 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 	return ret;
1895 }
1896 
1897 /**
1898  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1899  * @vsi: VSI structure
1900  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1901 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1902 {
1903 	struct i40e_pf *pf = vsi->back;
1904 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1905 	u8 *lut;
1906 	int ret;
1907 
1908 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1909 		return 0;
1910 	if (!vsi->rss_size)
1911 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1912 				      vsi->num_queue_pairs);
1913 	if (!vsi->rss_size)
1914 		return -EINVAL;
1915 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1916 	if (!lut)
1917 		return -ENOMEM;
1918 
1919 	/* Use the user configured hash keys and lookup table if there is one,
1920 	 * otherwise use default
1921 	 */
1922 	if (vsi->rss_lut_user)
1923 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1924 	else
1925 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1926 	if (vsi->rss_hkey_user)
1927 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1928 	else
1929 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1930 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1931 	kfree(lut);
1932 	return ret;
1933 }
1934 
1935 /**
1936  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1937  * @vsi: the VSI being configured,
1938  * @ctxt: VSI context structure
1939  * @enabled_tc: number of traffic classes to enable
1940  *
1941  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1942  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1943 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1944 					   struct i40e_vsi_context *ctxt,
1945 					   u8 enabled_tc)
1946 {
1947 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1948 	int i, override_q, pow, num_qps, ret;
1949 	u8 netdev_tc = 0, offset = 0;
1950 
1951 	if (vsi->type != I40E_VSI_MAIN)
1952 		return -EINVAL;
1953 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1954 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1955 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1956 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1957 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1958 
1959 	/* find the next higher power-of-2 of num queue pairs */
1960 	pow = ilog2(num_qps);
1961 	if (!is_power_of_2(num_qps))
1962 		pow++;
1963 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1964 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1965 
1966 	/* Setup queue offset/count for all TCs for given VSI */
1967 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1968 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1969 		/* See if the given TC is enabled for the given VSI */
1970 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1971 			offset = vsi->mqprio_qopt.qopt.offset[i];
1972 			qcount = vsi->mqprio_qopt.qopt.count[i];
1973 			if (qcount > max_qcount)
1974 				max_qcount = qcount;
1975 			vsi->tc_config.tc_info[i].qoffset = offset;
1976 			vsi->tc_config.tc_info[i].qcount = qcount;
1977 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1978 		} else {
1979 			/* TC is not enabled so set the offset to
1980 			 * default queue and allocate one queue
1981 			 * for the given TC.
1982 			 */
1983 			vsi->tc_config.tc_info[i].qoffset = 0;
1984 			vsi->tc_config.tc_info[i].qcount = 1;
1985 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1986 		}
1987 	}
1988 
1989 	/* Set actual Tx/Rx queue pairs */
1990 	vsi->num_queue_pairs = offset + qcount;
1991 
1992 	/* Setup queue TC[0].qmap for given VSI context */
1993 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1994 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1995 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1996 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1997 
1998 	/* Reconfigure RSS for main VSI with max queue count */
1999 	vsi->rss_size = max_qcount;
2000 	ret = i40e_vsi_config_rss(vsi);
2001 	if (ret) {
2002 		dev_info(&vsi->back->pdev->dev,
2003 			 "Failed to reconfig rss for num_queues (%u)\n",
2004 			 max_qcount);
2005 		return ret;
2006 	}
2007 	vsi->reconfig_rss = true;
2008 	dev_dbg(&vsi->back->pdev->dev,
2009 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2010 
2011 	/* Find queue count available for channel VSIs and starting offset
2012 	 * for channel VSIs
2013 	 */
2014 	override_q = vsi->mqprio_qopt.qopt.count[0];
2015 	if (override_q && override_q < vsi->num_queue_pairs) {
2016 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2017 		vsi->next_base_queue = override_q;
2018 	}
2019 	return 0;
2020 }
2021 
2022 /**
2023  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2024  * @vsi: the VSI being setup
2025  * @ctxt: VSI context structure
2026  * @enabled_tc: Enabled TCs bitmap
2027  * @is_add: True if called before Add VSI
2028  *
2029  * Setup VSI queue mapping for enabled traffic classes.
2030  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2031 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2032 				     struct i40e_vsi_context *ctxt,
2033 				     u8 enabled_tc,
2034 				     bool is_add)
2035 {
2036 	struct i40e_pf *pf = vsi->back;
2037 	u16 num_tc_qps = 0;
2038 	u16 sections = 0;
2039 	u8 netdev_tc = 0;
2040 	u16 numtc = 1;
2041 	u16 qcount;
2042 	u8 offset;
2043 	u16 qmap;
2044 	int i;
2045 
2046 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2047 	offset = 0;
2048 	/* zero out queue mapping, it will get updated on the end of the function */
2049 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2050 
2051 	if (vsi->type == I40E_VSI_MAIN) {
2052 		/* This code helps add more queue to the VSI if we have
2053 		 * more cores than RSS can support, the higher cores will
2054 		 * be served by ATR or other filters. Furthermore, the
2055 		 * non-zero req_queue_pairs says that user requested a new
2056 		 * queue count via ethtool's set_channels, so use this
2057 		 * value for queues distribution across traffic classes
2058 		 * We need at least one queue pair for the interface
2059 		 * to be usable as we see in else statement.
2060 		 */
2061 		if (vsi->req_queue_pairs > 0)
2062 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2063 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2064 			vsi->num_queue_pairs = pf->num_lan_msix;
2065 		else
2066 			vsi->num_queue_pairs = 1;
2067 	}
2068 
2069 	/* Number of queues per enabled TC */
2070 	if (vsi->type == I40E_VSI_MAIN ||
2071 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2072 		num_tc_qps = vsi->num_queue_pairs;
2073 	else
2074 		num_tc_qps = vsi->alloc_queue_pairs;
2075 
2076 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2077 		/* Find numtc from enabled TC bitmap */
2078 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2079 			if (enabled_tc & BIT(i)) /* TC is enabled */
2080 				numtc++;
2081 		}
2082 		if (!numtc) {
2083 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2084 			numtc = 1;
2085 		}
2086 		num_tc_qps = num_tc_qps / numtc;
2087 		num_tc_qps = min_t(int, num_tc_qps,
2088 				   i40e_pf_get_max_q_per_tc(pf));
2089 	}
2090 
2091 	vsi->tc_config.numtc = numtc;
2092 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2093 
2094 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2095 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2096 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2097 
2098 	/* Setup queue offset/count for all TCs for given VSI */
2099 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2100 		/* See if the given TC is enabled for the given VSI */
2101 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2102 			/* TC is enabled */
2103 			int pow, num_qps;
2104 
2105 			switch (vsi->type) {
2106 			case I40E_VSI_MAIN:
2107 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2108 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2109 				    vsi->tc_config.enabled_tc != 1) {
2110 					qcount = min_t(int, pf->alloc_rss_size,
2111 						       num_tc_qps);
2112 					break;
2113 				}
2114 				fallthrough;
2115 			case I40E_VSI_FDIR:
2116 			case I40E_VSI_SRIOV:
2117 			case I40E_VSI_VMDQ2:
2118 			default:
2119 				qcount = num_tc_qps;
2120 				WARN_ON(i != 0);
2121 				break;
2122 			}
2123 			vsi->tc_config.tc_info[i].qoffset = offset;
2124 			vsi->tc_config.tc_info[i].qcount = qcount;
2125 
2126 			/* find the next higher power-of-2 of num queue pairs */
2127 			num_qps = qcount;
2128 			pow = 0;
2129 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2130 				pow++;
2131 				num_qps >>= 1;
2132 			}
2133 
2134 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2135 			qmap =
2136 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2137 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2138 
2139 			offset += qcount;
2140 		} else {
2141 			/* TC is not enabled so set the offset to
2142 			 * default queue and allocate one queue
2143 			 * for the given TC.
2144 			 */
2145 			vsi->tc_config.tc_info[i].qoffset = 0;
2146 			vsi->tc_config.tc_info[i].qcount = 1;
2147 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2148 
2149 			qmap = 0;
2150 		}
2151 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2152 	}
2153 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2154 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2155 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2156 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2157 		vsi->num_queue_pairs = offset;
2158 
2159 	/* Scheduler section valid can only be set for ADD VSI */
2160 	if (is_add) {
2161 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2162 
2163 		ctxt->info.up_enable_bits = enabled_tc;
2164 	}
2165 	if (vsi->type == I40E_VSI_SRIOV) {
2166 		ctxt->info.mapping_flags |=
2167 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2168 		for (i = 0; i < vsi->num_queue_pairs; i++)
2169 			ctxt->info.queue_mapping[i] =
2170 					       cpu_to_le16(vsi->base_queue + i);
2171 	} else {
2172 		ctxt->info.mapping_flags |=
2173 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2174 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2175 	}
2176 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2177 }
2178 
2179 /**
2180  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2181  * @netdev: the netdevice
2182  * @addr: address to add
2183  *
2184  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2185  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2186  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2187 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2188 {
2189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2190 	struct i40e_vsi *vsi = np->vsi;
2191 
2192 	if (i40e_add_mac_filter(vsi, addr))
2193 		return 0;
2194 	else
2195 		return -ENOMEM;
2196 }
2197 
2198 /**
2199  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2200  * @netdev: the netdevice
2201  * @addr: address to add
2202  *
2203  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2204  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2205  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2206 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2207 {
2208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2209 	struct i40e_vsi *vsi = np->vsi;
2210 
2211 	/* Under some circumstances, we might receive a request to delete
2212 	 * our own device address from our uc list. Because we store the
2213 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2214 	 * such requests and not delete our device address from this list.
2215 	 */
2216 	if (ether_addr_equal(addr, netdev->dev_addr))
2217 		return 0;
2218 
2219 	i40e_del_mac_filter(vsi, addr);
2220 
2221 	return 0;
2222 }
2223 
2224 /**
2225  * i40e_set_rx_mode - NDO callback to set the netdev filters
2226  * @netdev: network interface device structure
2227  **/
i40e_set_rx_mode(struct net_device * netdev)2228 static void i40e_set_rx_mode(struct net_device *netdev)
2229 {
2230 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2231 	struct i40e_vsi *vsi = np->vsi;
2232 
2233 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2234 
2235 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2236 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2237 
2238 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2239 
2240 	/* check for other flag changes */
2241 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2242 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2243 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2244 	}
2245 }
2246 
2247 /**
2248  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2249  * @vsi: Pointer to VSI struct
2250  * @from: Pointer to list which contains MAC filter entries - changes to
2251  *        those entries needs to be undone.
2252  *
2253  * MAC filter entries from this list were slated for deletion.
2254  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2255 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2256 					 struct hlist_head *from)
2257 {
2258 	struct i40e_mac_filter *f;
2259 	struct hlist_node *h;
2260 
2261 	hlist_for_each_entry_safe(f, h, from, hlist) {
2262 		u64 key = i40e_addr_to_hkey(f->macaddr);
2263 
2264 		/* Move the element back into MAC filter list*/
2265 		hlist_del(&f->hlist);
2266 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2267 	}
2268 }
2269 
2270 /**
2271  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2272  * @vsi: Pointer to vsi struct
2273  * @from: Pointer to list which contains MAC filter entries - changes to
2274  *        those entries needs to be undone.
2275  *
2276  * MAC filter entries from this list were slated for addition.
2277  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2278 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2279 					 struct hlist_head *from)
2280 {
2281 	struct i40e_new_mac_filter *new;
2282 	struct hlist_node *h;
2283 
2284 	hlist_for_each_entry_safe(new, h, from, hlist) {
2285 		/* We can simply free the wrapper structure */
2286 		hlist_del(&new->hlist);
2287 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2288 		kfree(new);
2289 	}
2290 }
2291 
2292 /**
2293  * i40e_next_filter - Get the next non-broadcast filter from a list
2294  * @next: pointer to filter in list
2295  *
2296  * Returns the next non-broadcast filter in the list. Required so that we
2297  * ignore broadcast filters within the list, since these are not handled via
2298  * the normal firmware update path.
2299  */
2300 static
i40e_next_filter(struct i40e_new_mac_filter * next)2301 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2302 {
2303 	hlist_for_each_entry_continue(next, hlist) {
2304 		if (!is_broadcast_ether_addr(next->f->macaddr))
2305 			return next;
2306 	}
2307 
2308 	return NULL;
2309 }
2310 
2311 /**
2312  * i40e_update_filter_state - Update filter state based on return data
2313  * from firmware
2314  * @count: Number of filters added
2315  * @add_list: return data from fw
2316  * @add_head: pointer to first filter in current batch
2317  *
2318  * MAC filter entries from list were slated to be added to device. Returns
2319  * number of successful filters. Note that 0 does NOT mean success!
2320  **/
2321 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2322 i40e_update_filter_state(int count,
2323 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2324 			 struct i40e_new_mac_filter *add_head)
2325 {
2326 	int retval = 0;
2327 	int i;
2328 
2329 	for (i = 0; i < count; i++) {
2330 		/* Always check status of each filter. We don't need to check
2331 		 * the firmware return status because we pre-set the filter
2332 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2333 		 * request to the adminq. Thus, if it no longer matches then
2334 		 * we know the filter is active.
2335 		 */
2336 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2337 			add_head->state = I40E_FILTER_FAILED;
2338 		} else {
2339 			add_head->state = I40E_FILTER_ACTIVE;
2340 			retval++;
2341 		}
2342 
2343 		add_head = i40e_next_filter(add_head);
2344 		if (!add_head)
2345 			break;
2346 	}
2347 
2348 	return retval;
2349 }
2350 
2351 /**
2352  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2353  * @vsi: ptr to the VSI
2354  * @vsi_name: name to display in messages
2355  * @list: the list of filters to send to firmware
2356  * @num_del: the number of filters to delete
2357  * @retval: Set to -EIO on failure to delete
2358  *
2359  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2360  * *retval instead of a return value so that success does not force ret_val to
2361  * be set to 0. This ensures that a sequence of calls to this function
2362  * preserve the previous value of *retval on successful delete.
2363  */
2364 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)2365 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2366 			  struct i40e_aqc_remove_macvlan_element_data *list,
2367 			  int num_del, int *retval)
2368 {
2369 	struct i40e_hw *hw = &vsi->back->hw;
2370 	enum i40e_admin_queue_err aq_status;
2371 	int aq_ret;
2372 
2373 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2374 					   &aq_status);
2375 
2376 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2377 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2378 		*retval = -EIO;
2379 		dev_info(&vsi->back->pdev->dev,
2380 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2381 			 vsi_name, ERR_PTR(aq_ret),
2382 			 i40e_aq_str(hw, aq_status));
2383 	}
2384 }
2385 
2386 /**
2387  * i40e_aqc_add_filters - Request firmware to add a set of filters
2388  * @vsi: ptr to the VSI
2389  * @vsi_name: name to display in messages
2390  * @list: the list of filters to send to firmware
2391  * @add_head: Position in the add hlist
2392  * @num_add: the number of filters to add
2393  *
2394  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2395  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2396  * space for more filters.
2397  */
2398 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)2399 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2400 			  struct i40e_aqc_add_macvlan_element_data *list,
2401 			  struct i40e_new_mac_filter *add_head,
2402 			  int num_add)
2403 {
2404 	struct i40e_hw *hw = &vsi->back->hw;
2405 	enum i40e_admin_queue_err aq_status;
2406 	int fcnt;
2407 
2408 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2409 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2410 
2411 	if (fcnt != num_add) {
2412 		if (vsi->type == I40E_VSI_MAIN) {
2413 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2414 			dev_warn(&vsi->back->pdev->dev,
2415 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2416 				 i40e_aq_str(hw, aq_status), vsi_name);
2417 		} else if (vsi->type == I40E_VSI_SRIOV ||
2418 			   vsi->type == I40E_VSI_VMDQ1 ||
2419 			   vsi->type == I40E_VSI_VMDQ2) {
2420 			dev_warn(&vsi->back->pdev->dev,
2421 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2422 				 i40e_aq_str(hw, aq_status), vsi_name,
2423 					     vsi_name);
2424 		} else {
2425 			dev_warn(&vsi->back->pdev->dev,
2426 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2427 				 i40e_aq_str(hw, aq_status), vsi_name,
2428 					     vsi->type);
2429 		}
2430 	}
2431 }
2432 
2433 /**
2434  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2435  * @vsi: pointer to the VSI
2436  * @vsi_name: the VSI name
2437  * @f: filter data
2438  *
2439  * This function sets or clears the promiscuous broadcast flags for VLAN
2440  * filters in order to properly receive broadcast frames. Assumes that only
2441  * broadcast filters are passed.
2442  *
2443  * Returns status indicating success or failure;
2444  **/
2445 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2446 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2447 			  struct i40e_mac_filter *f)
2448 {
2449 	bool enable = f->state == I40E_FILTER_NEW ||
2450 		      f->state == I40E_FILTER_NEW_SYNC;
2451 	struct i40e_hw *hw = &vsi->back->hw;
2452 	int aq_ret;
2453 
2454 	if (f->vlan == I40E_VLAN_ANY) {
2455 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2456 						   vsi->seid,
2457 						   enable,
2458 						   NULL);
2459 	} else {
2460 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2461 							    vsi->seid,
2462 							    enable,
2463 							    f->vlan,
2464 							    NULL);
2465 	}
2466 
2467 	if (aq_ret) {
2468 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2469 		dev_warn(&vsi->back->pdev->dev,
2470 			 "Error %s, forcing overflow promiscuous on %s\n",
2471 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2472 			 vsi_name);
2473 	}
2474 
2475 	return aq_ret;
2476 }
2477 
2478 /**
2479  * i40e_set_promiscuous - set promiscuous mode
2480  * @pf: board private structure
2481  * @promisc: promisc on or off
2482  *
2483  * There are different ways of setting promiscuous mode on a PF depending on
2484  * what state/environment we're in.  This identifies and sets it appropriately.
2485  * Returns 0 on success.
2486  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2487 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2488 {
2489 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2490 	struct i40e_hw *hw = &pf->hw;
2491 	int aq_ret;
2492 
2493 	if (vsi->type == I40E_VSI_MAIN &&
2494 	    pf->lan_veb != I40E_NO_VEB &&
2495 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2496 		/* set defport ON for Main VSI instead of true promisc
2497 		 * this way we will get all unicast/multicast and VLAN
2498 		 * promisc behavior but will not get VF or VMDq traffic
2499 		 * replicated on the Main VSI.
2500 		 */
2501 		if (promisc)
2502 			aq_ret = i40e_aq_set_default_vsi(hw,
2503 							 vsi->seid,
2504 							 NULL);
2505 		else
2506 			aq_ret = i40e_aq_clear_default_vsi(hw,
2507 							   vsi->seid,
2508 							   NULL);
2509 		if (aq_ret) {
2510 			dev_info(&pf->pdev->dev,
2511 				 "Set default VSI failed, err %pe, aq_err %s\n",
2512 				 ERR_PTR(aq_ret),
2513 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2514 		}
2515 	} else {
2516 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2517 						  hw,
2518 						  vsi->seid,
2519 						  promisc, NULL,
2520 						  true);
2521 		if (aq_ret) {
2522 			dev_info(&pf->pdev->dev,
2523 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2524 				 ERR_PTR(aq_ret),
2525 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2526 		}
2527 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2528 						  hw,
2529 						  vsi->seid,
2530 						  promisc, NULL);
2531 		if (aq_ret) {
2532 			dev_info(&pf->pdev->dev,
2533 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2534 				 ERR_PTR(aq_ret),
2535 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2536 		}
2537 	}
2538 
2539 	if (!aq_ret)
2540 		pf->cur_promisc = promisc;
2541 
2542 	return aq_ret;
2543 }
2544 
2545 /**
2546  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2547  * @vsi: ptr to the VSI
2548  *
2549  * Push any outstanding VSI filter changes through the AdminQ.
2550  *
2551  * Returns 0 or error value
2552  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2553 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2554 {
2555 	struct hlist_head tmp_add_list, tmp_del_list;
2556 	struct i40e_mac_filter *f;
2557 	struct i40e_new_mac_filter *new, *add_head = NULL;
2558 	struct i40e_hw *hw = &vsi->back->hw;
2559 	bool old_overflow, new_overflow;
2560 	unsigned int failed_filters = 0;
2561 	unsigned int vlan_filters = 0;
2562 	char vsi_name[16] = "PF";
2563 	int filter_list_len = 0;
2564 	u32 changed_flags = 0;
2565 	struct hlist_node *h;
2566 	struct i40e_pf *pf;
2567 	int num_add = 0;
2568 	int num_del = 0;
2569 	int aq_ret = 0;
2570 	int retval = 0;
2571 	u16 cmd_flags;
2572 	int list_size;
2573 	int bkt;
2574 
2575 	/* empty array typed pointers, kcalloc later */
2576 	struct i40e_aqc_add_macvlan_element_data *add_list;
2577 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2578 
2579 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2580 		usleep_range(1000, 2000);
2581 	pf = vsi->back;
2582 
2583 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2584 
2585 	if (vsi->netdev) {
2586 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2587 		vsi->current_netdev_flags = vsi->netdev->flags;
2588 	}
2589 
2590 	INIT_HLIST_HEAD(&tmp_add_list);
2591 	INIT_HLIST_HEAD(&tmp_del_list);
2592 
2593 	if (vsi->type == I40E_VSI_SRIOV)
2594 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2595 	else if (vsi->type != I40E_VSI_MAIN)
2596 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2597 
2598 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2599 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2600 
2601 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2602 		/* Create a list of filters to delete. */
2603 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2604 			if (f->state == I40E_FILTER_REMOVE) {
2605 				/* Move the element into temporary del_list */
2606 				hash_del(&f->hlist);
2607 				hlist_add_head(&f->hlist, &tmp_del_list);
2608 
2609 				/* Avoid counting removed filters */
2610 				continue;
2611 			}
2612 			if (f->state == I40E_FILTER_NEW) {
2613 				/* Create a temporary i40e_new_mac_filter */
2614 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2615 				if (!new)
2616 					goto err_no_memory_locked;
2617 
2618 				/* Store pointer to the real filter */
2619 				new->f = f;
2620 				new->state = f->state;
2621 
2622 				/* Add it to the hash list */
2623 				hlist_add_head(&new->hlist, &tmp_add_list);
2624 				f->state = I40E_FILTER_NEW_SYNC;
2625 			}
2626 
2627 			/* Count the number of active (current and new) VLAN
2628 			 * filters we have now. Does not count filters which
2629 			 * are marked for deletion.
2630 			 */
2631 			if (f->vlan > 0)
2632 				vlan_filters++;
2633 		}
2634 
2635 		if (vsi->type != I40E_VSI_SRIOV)
2636 			retval = i40e_correct_mac_vlan_filters
2637 				(vsi, &tmp_add_list, &tmp_del_list,
2638 				 vlan_filters);
2639 		else if (pf->vf)
2640 			retval = i40e_correct_vf_mac_vlan_filters
2641 				(vsi, &tmp_add_list, &tmp_del_list,
2642 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2643 
2644 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2645 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2646 
2647 		if (retval)
2648 			goto err_no_memory_locked;
2649 
2650 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2651 	}
2652 
2653 	/* Now process 'del_list' outside the lock */
2654 	if (!hlist_empty(&tmp_del_list)) {
2655 		filter_list_len = hw->aq.asq_buf_size /
2656 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2657 		list_size = filter_list_len *
2658 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2659 		del_list = kzalloc(list_size, GFP_ATOMIC);
2660 		if (!del_list)
2661 			goto err_no_memory;
2662 
2663 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2664 			cmd_flags = 0;
2665 
2666 			/* handle broadcast filters by updating the broadcast
2667 			 * promiscuous flag and release filter list.
2668 			 */
2669 			if (is_broadcast_ether_addr(f->macaddr)) {
2670 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2671 
2672 				hlist_del(&f->hlist);
2673 				kfree(f);
2674 				continue;
2675 			}
2676 
2677 			/* add to delete list */
2678 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2679 			if (f->vlan == I40E_VLAN_ANY) {
2680 				del_list[num_del].vlan_tag = 0;
2681 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2682 			} else {
2683 				del_list[num_del].vlan_tag =
2684 					cpu_to_le16((u16)(f->vlan));
2685 			}
2686 
2687 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2688 			del_list[num_del].flags = cmd_flags;
2689 			num_del++;
2690 
2691 			/* flush a full buffer */
2692 			if (num_del == filter_list_len) {
2693 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2694 						     num_del, &retval);
2695 				memset(del_list, 0, list_size);
2696 				num_del = 0;
2697 			}
2698 			/* Release memory for MAC filter entries which were
2699 			 * synced up with HW.
2700 			 */
2701 			hlist_del(&f->hlist);
2702 			kfree(f);
2703 		}
2704 
2705 		if (num_del) {
2706 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2707 					     num_del, &retval);
2708 		}
2709 
2710 		kfree(del_list);
2711 		del_list = NULL;
2712 	}
2713 
2714 	if (!hlist_empty(&tmp_add_list)) {
2715 		/* Do all the adds now. */
2716 		filter_list_len = hw->aq.asq_buf_size /
2717 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2718 		list_size = filter_list_len *
2719 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2720 		add_list = kzalloc(list_size, GFP_ATOMIC);
2721 		if (!add_list)
2722 			goto err_no_memory;
2723 
2724 		num_add = 0;
2725 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2726 			/* handle broadcast filters by updating the broadcast
2727 			 * promiscuous flag instead of adding a MAC filter.
2728 			 */
2729 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2730 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2731 							      new->f))
2732 					new->state = I40E_FILTER_FAILED;
2733 				else
2734 					new->state = I40E_FILTER_ACTIVE;
2735 				continue;
2736 			}
2737 
2738 			/* add to add array */
2739 			if (num_add == 0)
2740 				add_head = new;
2741 			cmd_flags = 0;
2742 			ether_addr_copy(add_list[num_add].mac_addr,
2743 					new->f->macaddr);
2744 			if (new->f->vlan == I40E_VLAN_ANY) {
2745 				add_list[num_add].vlan_tag = 0;
2746 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2747 			} else {
2748 				add_list[num_add].vlan_tag =
2749 					cpu_to_le16((u16)(new->f->vlan));
2750 			}
2751 			add_list[num_add].queue_number = 0;
2752 			/* set invalid match method for later detection */
2753 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2754 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2755 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2756 			num_add++;
2757 
2758 			/* flush a full buffer */
2759 			if (num_add == filter_list_len) {
2760 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2761 						     add_head, num_add);
2762 				memset(add_list, 0, list_size);
2763 				num_add = 0;
2764 			}
2765 		}
2766 		if (num_add) {
2767 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2768 					     num_add);
2769 		}
2770 		/* Now move all of the filters from the temp add list back to
2771 		 * the VSI's list.
2772 		 */
2773 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2774 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2775 			/* Only update the state if we're still NEW */
2776 			if (new->f->state == I40E_FILTER_NEW ||
2777 			    new->f->state == I40E_FILTER_NEW_SYNC)
2778 				new->f->state = new->state;
2779 			hlist_del(&new->hlist);
2780 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2781 			kfree(new);
2782 		}
2783 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2784 		kfree(add_list);
2785 		add_list = NULL;
2786 	}
2787 
2788 	/* Determine the number of active and failed filters. */
2789 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2790 	vsi->active_filters = 0;
2791 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2792 		if (f->state == I40E_FILTER_ACTIVE)
2793 			vsi->active_filters++;
2794 		else if (f->state == I40E_FILTER_FAILED)
2795 			failed_filters++;
2796 	}
2797 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2798 
2799 	/* Check if we are able to exit overflow promiscuous mode. We can
2800 	 * safely exit if we didn't just enter, we no longer have any failed
2801 	 * filters, and we have reduced filters below the threshold value.
2802 	 */
2803 	if (old_overflow && !failed_filters &&
2804 	    vsi->active_filters < vsi->promisc_threshold) {
2805 		dev_info(&pf->pdev->dev,
2806 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2807 			 vsi_name);
2808 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2809 		vsi->promisc_threshold = 0;
2810 	}
2811 
2812 	/* if the VF is not trusted do not do promisc */
2813 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2814 	    !pf->vf[vsi->vf_id].trusted) {
2815 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2816 		goto out;
2817 	}
2818 
2819 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2820 
2821 	/* If we are entering overflow promiscuous, we need to calculate a new
2822 	 * threshold for when we are safe to exit
2823 	 */
2824 	if (!old_overflow && new_overflow)
2825 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2826 
2827 	/* check for changes in promiscuous modes */
2828 	if (changed_flags & IFF_ALLMULTI) {
2829 		bool cur_multipromisc;
2830 
2831 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2832 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2833 							       vsi->seid,
2834 							       cur_multipromisc,
2835 							       NULL);
2836 		if (aq_ret) {
2837 			retval = i40e_aq_rc_to_posix(aq_ret,
2838 						     hw->aq.asq_last_status);
2839 			dev_info(&pf->pdev->dev,
2840 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2841 				 vsi_name,
2842 				 ERR_PTR(aq_ret),
2843 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2844 		} else {
2845 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2846 				 cur_multipromisc ? "entering" : "leaving");
2847 		}
2848 	}
2849 
2850 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2851 		bool cur_promisc;
2852 
2853 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2854 			       new_overflow);
2855 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2856 		if (aq_ret) {
2857 			retval = i40e_aq_rc_to_posix(aq_ret,
2858 						     hw->aq.asq_last_status);
2859 			dev_info(&pf->pdev->dev,
2860 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2861 				 cur_promisc ? "on" : "off",
2862 				 vsi_name,
2863 				 ERR_PTR(aq_ret),
2864 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2865 		}
2866 	}
2867 out:
2868 	/* if something went wrong then set the changed flag so we try again */
2869 	if (retval)
2870 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2871 
2872 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2873 	return retval;
2874 
2875 err_no_memory:
2876 	/* Restore elements on the temporary add and delete lists */
2877 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2878 err_no_memory_locked:
2879 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2880 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2881 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2882 
2883 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2884 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2885 	return -ENOMEM;
2886 }
2887 
2888 /**
2889  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2890  * @pf: board private structure
2891  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2892 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2893 {
2894 	int v;
2895 
2896 	if (!pf)
2897 		return;
2898 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2899 		return;
2900 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2901 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2902 		return;
2903 	}
2904 
2905 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2906 		if (pf->vsi[v] &&
2907 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2908 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2909 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2910 
2911 			if (ret) {
2912 				/* come back and try again later */
2913 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2914 					pf->state);
2915 				break;
2916 			}
2917 		}
2918 	}
2919 }
2920 
2921 /**
2922  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2923  *
2924  * @vsi: VSI to calculate rx_buf_len from
2925  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)2926 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2927 {
2928 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2929 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2930 
2931 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2932 }
2933 
2934 /**
2935  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2936  * @vsi: the vsi
2937  * @xdp_prog: XDP program
2938  **/
i40e_max_vsi_frame_size(struct i40e_vsi * vsi,struct bpf_prog * xdp_prog)2939 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2940 				   struct bpf_prog *xdp_prog)
2941 {
2942 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2943 	u16 chain_len;
2944 
2945 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2946 		chain_len = 1;
2947 	else
2948 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2949 
2950 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2951 }
2952 
2953 /**
2954  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2955  * @netdev: network interface device structure
2956  * @new_mtu: new value for maximum frame size
2957  *
2958  * Returns 0 on success, negative on failure
2959  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2960 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2961 {
2962 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2963 	struct i40e_vsi *vsi = np->vsi;
2964 	struct i40e_pf *pf = vsi->back;
2965 	int frame_size;
2966 
2967 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2968 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2969 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2970 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2971 		return -EINVAL;
2972 	}
2973 
2974 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2975 		   netdev->mtu, new_mtu);
2976 	netdev->mtu = new_mtu;
2977 	if (netif_running(netdev))
2978 		i40e_vsi_reinit_locked(vsi);
2979 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2980 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2981 	return 0;
2982 }
2983 
2984 /**
2985  * i40e_ioctl - Access the hwtstamp interface
2986  * @netdev: network interface device structure
2987  * @ifr: interface request data
2988  * @cmd: ioctl command
2989  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2990 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2991 {
2992 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2993 	struct i40e_pf *pf = np->vsi->back;
2994 
2995 	switch (cmd) {
2996 	case SIOCGHWTSTAMP:
2997 		return i40e_ptp_get_ts_config(pf, ifr);
2998 	case SIOCSHWTSTAMP:
2999 		return i40e_ptp_set_ts_config(pf, ifr);
3000 	default:
3001 		return -EOPNOTSUPP;
3002 	}
3003 }
3004 
3005 /**
3006  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
3007  * @vsi: the vsi being adjusted
3008  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)3009 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
3010 {
3011 	struct i40e_vsi_context ctxt;
3012 	int ret;
3013 
3014 	/* Don't modify stripping options if a port VLAN is active */
3015 	if (vsi->info.pvid)
3016 		return;
3017 
3018 	if ((vsi->info.valid_sections &
3019 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3020 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3021 		return;  /* already enabled */
3022 
3023 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3024 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3025 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3026 
3027 	ctxt.seid = vsi->seid;
3028 	ctxt.info = vsi->info;
3029 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3030 	if (ret) {
3031 		dev_info(&vsi->back->pdev->dev,
3032 			 "update vlan stripping failed, err %pe aq_err %s\n",
3033 			 ERR_PTR(ret),
3034 			 i40e_aq_str(&vsi->back->hw,
3035 				     vsi->back->hw.aq.asq_last_status));
3036 	}
3037 }
3038 
3039 /**
3040  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3041  * @vsi: the vsi being adjusted
3042  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)3043 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3044 {
3045 	struct i40e_vsi_context ctxt;
3046 	int ret;
3047 
3048 	/* Don't modify stripping options if a port VLAN is active */
3049 	if (vsi->info.pvid)
3050 		return;
3051 
3052 	if ((vsi->info.valid_sections &
3053 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3054 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3055 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3056 		return;  /* already disabled */
3057 
3058 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3059 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3060 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3061 
3062 	ctxt.seid = vsi->seid;
3063 	ctxt.info = vsi->info;
3064 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3065 	if (ret) {
3066 		dev_info(&vsi->back->pdev->dev,
3067 			 "update vlan stripping failed, err %pe aq_err %s\n",
3068 			 ERR_PTR(ret),
3069 			 i40e_aq_str(&vsi->back->hw,
3070 				     vsi->back->hw.aq.asq_last_status));
3071 	}
3072 }
3073 
3074 /**
3075  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3076  * @vsi: the vsi being configured
3077  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3078  *
3079  * This is a helper function for adding a new MAC/VLAN filter with the
3080  * specified VLAN for each existing MAC address already in the hash table.
3081  * This function does *not* perform any accounting to update filters based on
3082  * VLAN mode.
3083  *
3084  * NOTE: this function expects to be called while under the
3085  * mac_filter_hash_lock
3086  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3087 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3088 {
3089 	struct i40e_mac_filter *f, *add_f;
3090 	struct hlist_node *h;
3091 	int bkt;
3092 
3093 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3094 		/* If we're asked to add a filter that has been marked for
3095 		 * removal, it is safe to simply restore it to active state.
3096 		 * __i40e_del_filter will have simply deleted any filters which
3097 		 * were previously marked NEW or FAILED, so if it is currently
3098 		 * marked REMOVE it must have previously been ACTIVE. Since we
3099 		 * haven't yet run the sync filters task, just restore this
3100 		 * filter to the ACTIVE state so that the sync task leaves it
3101 		 * in place.
3102 		 */
3103 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3104 			f->state = I40E_FILTER_ACTIVE;
3105 			continue;
3106 		} else if (f->state == I40E_FILTER_REMOVE) {
3107 			continue;
3108 		}
3109 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3110 		if (!add_f) {
3111 			dev_info(&vsi->back->pdev->dev,
3112 				 "Could not add vlan filter %d for %pM\n",
3113 				 vid, f->macaddr);
3114 			return -ENOMEM;
3115 		}
3116 	}
3117 
3118 	return 0;
3119 }
3120 
3121 /**
3122  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3123  * @vsi: the VSI being configured
3124  * @vid: VLAN id to be added
3125  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3126 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3127 {
3128 	int err;
3129 
3130 	if (vsi->info.pvid)
3131 		return -EINVAL;
3132 
3133 	/* The network stack will attempt to add VID=0, with the intention to
3134 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3135 	 * these packets by default when configured to receive untagged
3136 	 * packets, so we don't need to add a filter for this case.
3137 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3138 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3139 	 * Thus, we do not want to actually add a filter for VID=0
3140 	 */
3141 	if (!vid)
3142 		return 0;
3143 
3144 	/* Locked once because all functions invoked below iterates list*/
3145 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3146 	err = i40e_add_vlan_all_mac(vsi, vid);
3147 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3148 	if (err)
3149 		return err;
3150 
3151 	/* schedule our worker thread which will take care of
3152 	 * applying the new filter changes
3153 	 */
3154 	i40e_service_event_schedule(vsi->back);
3155 	return 0;
3156 }
3157 
3158 /**
3159  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3160  * @vsi: the vsi being configured
3161  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3162  *
3163  * This function should be used to remove all VLAN filters which match the
3164  * given VID. It does not schedule the service event and does not take the
3165  * mac_filter_hash_lock so it may be combined with other operations under
3166  * a single invocation of the mac_filter_hash_lock.
3167  *
3168  * NOTE: this function expects to be called while under the
3169  * mac_filter_hash_lock
3170  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3171 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3172 {
3173 	struct i40e_mac_filter *f;
3174 	struct hlist_node *h;
3175 	int bkt;
3176 
3177 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3178 		if (f->vlan == vid)
3179 			__i40e_del_filter(vsi, f);
3180 	}
3181 }
3182 
3183 /**
3184  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3185  * @vsi: the VSI being configured
3186  * @vid: VLAN id to be removed
3187  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3188 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3189 {
3190 	if (!vid || vsi->info.pvid)
3191 		return;
3192 
3193 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3194 	i40e_rm_vlan_all_mac(vsi, vid);
3195 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3196 
3197 	/* schedule our worker thread which will take care of
3198 	 * applying the new filter changes
3199 	 */
3200 	i40e_service_event_schedule(vsi->back);
3201 }
3202 
3203 /**
3204  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3205  * @netdev: network interface to be adjusted
3206  * @proto: unused protocol value
3207  * @vid: vlan id to be added
3208  *
3209  * net_device_ops implementation for adding vlan ids
3210  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3211 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3212 				__always_unused __be16 proto, u16 vid)
3213 {
3214 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3215 	struct i40e_vsi *vsi = np->vsi;
3216 	int ret = 0;
3217 
3218 	if (vid >= VLAN_N_VID)
3219 		return -EINVAL;
3220 
3221 	ret = i40e_vsi_add_vlan(vsi, vid);
3222 	if (!ret)
3223 		set_bit(vid, vsi->active_vlans);
3224 
3225 	return ret;
3226 }
3227 
3228 /**
3229  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3230  * @netdev: network interface to be adjusted
3231  * @proto: unused protocol value
3232  * @vid: vlan id to be added
3233  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3234 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3235 				    __always_unused __be16 proto, u16 vid)
3236 {
3237 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3238 	struct i40e_vsi *vsi = np->vsi;
3239 
3240 	if (vid >= VLAN_N_VID)
3241 		return;
3242 	set_bit(vid, vsi->active_vlans);
3243 }
3244 
3245 /**
3246  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3247  * @netdev: network interface to be adjusted
3248  * @proto: unused protocol value
3249  * @vid: vlan id to be removed
3250  *
3251  * net_device_ops implementation for removing vlan ids
3252  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3253 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3254 				 __always_unused __be16 proto, u16 vid)
3255 {
3256 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3257 	struct i40e_vsi *vsi = np->vsi;
3258 
3259 	/* return code is ignored as there is nothing a user
3260 	 * can do about failure to remove and a log message was
3261 	 * already printed from the other function
3262 	 */
3263 	i40e_vsi_kill_vlan(vsi, vid);
3264 
3265 	clear_bit(vid, vsi->active_vlans);
3266 
3267 	return 0;
3268 }
3269 
3270 /**
3271  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3272  * @vsi: the vsi being brought back up
3273  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3274 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3275 {
3276 	u16 vid;
3277 
3278 	if (!vsi->netdev)
3279 		return;
3280 
3281 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3282 		i40e_vlan_stripping_enable(vsi);
3283 	else
3284 		i40e_vlan_stripping_disable(vsi);
3285 
3286 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3287 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3288 					vid);
3289 }
3290 
3291 /**
3292  * i40e_vsi_add_pvid - Add pvid for the VSI
3293  * @vsi: the vsi being adjusted
3294  * @vid: the vlan id to set as a PVID
3295  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3296 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3297 {
3298 	struct i40e_vsi_context ctxt;
3299 	int ret;
3300 
3301 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3302 	vsi->info.pvid = cpu_to_le16(vid);
3303 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3304 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3305 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3306 
3307 	ctxt.seid = vsi->seid;
3308 	ctxt.info = vsi->info;
3309 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3310 	if (ret) {
3311 		dev_info(&vsi->back->pdev->dev,
3312 			 "add pvid failed, err %pe aq_err %s\n",
3313 			 ERR_PTR(ret),
3314 			 i40e_aq_str(&vsi->back->hw,
3315 				     vsi->back->hw.aq.asq_last_status));
3316 		return -ENOENT;
3317 	}
3318 
3319 	return 0;
3320 }
3321 
3322 /**
3323  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3324  * @vsi: the vsi being adjusted
3325  *
3326  * Just use the vlan_rx_register() service to put it back to normal
3327  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3328 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3329 {
3330 	vsi->info.pvid = 0;
3331 
3332 	i40e_vlan_stripping_disable(vsi);
3333 }
3334 
3335 /**
3336  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3337  * @vsi: ptr to the VSI
3338  *
3339  * If this function returns with an error, then it's possible one or
3340  * more of the rings is populated (while the rest are not).  It is the
3341  * callers duty to clean those orphaned rings.
3342  *
3343  * Return 0 on success, negative on failure
3344  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3345 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3346 {
3347 	int i, err = 0;
3348 
3349 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3350 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3351 
3352 	if (!i40e_enabled_xdp_vsi(vsi))
3353 		return err;
3354 
3355 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3356 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3357 
3358 	return err;
3359 }
3360 
3361 /**
3362  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3363  * @vsi: ptr to the VSI
3364  *
3365  * Free VSI's transmit software resources
3366  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3367 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3368 {
3369 	int i;
3370 
3371 	if (vsi->tx_rings) {
3372 		for (i = 0; i < vsi->num_queue_pairs; i++)
3373 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3374 				i40e_free_tx_resources(vsi->tx_rings[i]);
3375 	}
3376 
3377 	if (vsi->xdp_rings) {
3378 		for (i = 0; i < vsi->num_queue_pairs; i++)
3379 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3380 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3381 	}
3382 }
3383 
3384 /**
3385  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3386  * @vsi: ptr to the VSI
3387  *
3388  * If this function returns with an error, then it's possible one or
3389  * more of the rings is populated (while the rest are not).  It is the
3390  * callers duty to clean those orphaned rings.
3391  *
3392  * Return 0 on success, negative on failure
3393  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3394 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3395 {
3396 	int i, err = 0;
3397 
3398 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3399 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3400 	return err;
3401 }
3402 
3403 /**
3404  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3405  * @vsi: ptr to the VSI
3406  *
3407  * Free all receive software resources
3408  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3409 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3410 {
3411 	int i;
3412 
3413 	if (!vsi->rx_rings)
3414 		return;
3415 
3416 	for (i = 0; i < vsi->num_queue_pairs; i++)
3417 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3418 			i40e_free_rx_resources(vsi->rx_rings[i]);
3419 }
3420 
3421 /**
3422  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3423  * @ring: The Tx ring to configure
3424  *
3425  * This enables/disables XPS for a given Tx descriptor ring
3426  * based on the TCs enabled for the VSI that ring belongs to.
3427  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3428 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3429 {
3430 	int cpu;
3431 
3432 	if (!ring->q_vector || !ring->netdev || ring->ch)
3433 		return;
3434 
3435 	/* We only initialize XPS once, so as not to overwrite user settings */
3436 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3437 		return;
3438 
3439 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3440 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3441 			    ring->queue_index);
3442 }
3443 
3444 /**
3445  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3446  * @ring: The Tx or Rx ring
3447  *
3448  * Returns the AF_XDP buffer pool or NULL.
3449  **/
i40e_xsk_pool(struct i40e_ring * ring)3450 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3451 {
3452 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3453 	int qid = ring->queue_index;
3454 
3455 	if (ring_is_xdp(ring))
3456 		qid -= ring->vsi->alloc_queue_pairs;
3457 
3458 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3459 		return NULL;
3460 
3461 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3462 }
3463 
3464 /**
3465  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3466  * @ring: The Tx ring to configure
3467  *
3468  * Configure the Tx descriptor ring in the HMC context.
3469  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3470 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3471 {
3472 	struct i40e_vsi *vsi = ring->vsi;
3473 	u16 pf_q = vsi->base_queue + ring->queue_index;
3474 	struct i40e_hw *hw = &vsi->back->hw;
3475 	struct i40e_hmc_obj_txq tx_ctx;
3476 	u32 qtx_ctl = 0;
3477 	int err = 0;
3478 
3479 	if (ring_is_xdp(ring))
3480 		ring->xsk_pool = i40e_xsk_pool(ring);
3481 
3482 	/* some ATR related tx ring init */
3483 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3484 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3485 		ring->atr_count = 0;
3486 	} else {
3487 		ring->atr_sample_rate = 0;
3488 	}
3489 
3490 	/* configure XPS */
3491 	i40e_config_xps_tx_ring(ring);
3492 
3493 	/* clear the context structure first */
3494 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3495 
3496 	tx_ctx.new_context = 1;
3497 	tx_ctx.base = (ring->dma / 128);
3498 	tx_ctx.qlen = ring->count;
3499 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3500 					       I40E_FLAG_FD_ATR_ENABLED));
3501 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3502 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3503 	if (vsi->type != I40E_VSI_FDIR)
3504 		tx_ctx.head_wb_ena = 1;
3505 	tx_ctx.head_wb_addr = ring->dma +
3506 			      (ring->count * sizeof(struct i40e_tx_desc));
3507 
3508 	/* As part of VSI creation/update, FW allocates certain
3509 	 * Tx arbitration queue sets for each TC enabled for
3510 	 * the VSI. The FW returns the handles to these queue
3511 	 * sets as part of the response buffer to Add VSI,
3512 	 * Update VSI, etc. AQ commands. It is expected that
3513 	 * these queue set handles be associated with the Tx
3514 	 * queues by the driver as part of the TX queue context
3515 	 * initialization. This has to be done regardless of
3516 	 * DCB as by default everything is mapped to TC0.
3517 	 */
3518 
3519 	if (ring->ch)
3520 		tx_ctx.rdylist =
3521 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3522 
3523 	else
3524 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3525 
3526 	tx_ctx.rdylist_act = 0;
3527 
3528 	/* clear the context in the HMC */
3529 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3530 	if (err) {
3531 		dev_info(&vsi->back->pdev->dev,
3532 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3533 			 ring->queue_index, pf_q, err);
3534 		return -ENOMEM;
3535 	}
3536 
3537 	/* set the context in the HMC */
3538 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3539 	if (err) {
3540 		dev_info(&vsi->back->pdev->dev,
3541 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3542 			 ring->queue_index, pf_q, err);
3543 		return -ENOMEM;
3544 	}
3545 
3546 	/* Now associate this queue with this PCI function */
3547 	if (ring->ch) {
3548 		if (ring->ch->type == I40E_VSI_VMDQ2)
3549 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3550 		else
3551 			return -EINVAL;
3552 
3553 		qtx_ctl |= (ring->ch->vsi_number <<
3554 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3555 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3556 	} else {
3557 		if (vsi->type == I40E_VSI_VMDQ2) {
3558 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3559 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3560 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3561 		} else {
3562 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3563 		}
3564 	}
3565 
3566 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3567 		    I40E_QTX_CTL_PF_INDX_MASK);
3568 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3569 	i40e_flush(hw);
3570 
3571 	/* cache tail off for easier writes later */
3572 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3573 
3574 	return 0;
3575 }
3576 
3577 /**
3578  * i40e_rx_offset - Return expected offset into page to access data
3579  * @rx_ring: Ring we are requesting offset of
3580  *
3581  * Returns the offset value for ring into the data buffer.
3582  */
i40e_rx_offset(struct i40e_ring * rx_ring)3583 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3584 {
3585 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3586 }
3587 
3588 /**
3589  * i40e_configure_rx_ring - Configure a receive ring context
3590  * @ring: The Rx ring to configure
3591  *
3592  * Configure the Rx descriptor ring in the HMC context.
3593  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3594 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3595 {
3596 	struct i40e_vsi *vsi = ring->vsi;
3597 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3598 	u16 pf_q = vsi->base_queue + ring->queue_index;
3599 	struct i40e_hw *hw = &vsi->back->hw;
3600 	struct i40e_hmc_obj_rxq rx_ctx;
3601 	int err = 0;
3602 	bool ok;
3603 
3604 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3605 
3606 	/* clear the context structure first */
3607 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3608 
3609 	ring->rx_buf_len = vsi->rx_buf_len;
3610 
3611 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3612 	if (ring->vsi->type != I40E_VSI_MAIN)
3613 		goto skip;
3614 
3615 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3616 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3617 					 ring->queue_index,
3618 					 ring->q_vector->napi.napi_id,
3619 					 ring->rx_buf_len);
3620 		if (err)
3621 			return err;
3622 	}
3623 
3624 	ring->xsk_pool = i40e_xsk_pool(ring);
3625 	if (ring->xsk_pool) {
3626 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3627 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3628 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3629 					 ring->queue_index,
3630 					 ring->q_vector->napi.napi_id,
3631 					 ring->rx_buf_len);
3632 		if (err)
3633 			return err;
3634 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3635 						 MEM_TYPE_XSK_BUFF_POOL,
3636 						 NULL);
3637 		if (err)
3638 			return err;
3639 		dev_info(&vsi->back->pdev->dev,
3640 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3641 			 ring->queue_index);
3642 
3643 	} else {
3644 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3645 						 MEM_TYPE_PAGE_SHARED,
3646 						 NULL);
3647 		if (err)
3648 			return err;
3649 	}
3650 
3651 skip:
3652 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3653 
3654 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3655 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3656 
3657 	rx_ctx.base = (ring->dma / 128);
3658 	rx_ctx.qlen = ring->count;
3659 
3660 	/* use 16 byte descriptors */
3661 	rx_ctx.dsize = 0;
3662 
3663 	/* descriptor type is always zero
3664 	 * rx_ctx.dtype = 0;
3665 	 */
3666 	rx_ctx.hsplit_0 = 0;
3667 
3668 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3669 	if (hw->revision_id == 0)
3670 		rx_ctx.lrxqthresh = 0;
3671 	else
3672 		rx_ctx.lrxqthresh = 1;
3673 	rx_ctx.crcstrip = 1;
3674 	rx_ctx.l2tsel = 1;
3675 	/* this controls whether VLAN is stripped from inner headers */
3676 	rx_ctx.showiv = 0;
3677 	/* set the prefena field to 1 because the manual says to */
3678 	rx_ctx.prefena = 1;
3679 
3680 	/* clear the context in the HMC */
3681 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3682 	if (err) {
3683 		dev_info(&vsi->back->pdev->dev,
3684 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3685 			 ring->queue_index, pf_q, err);
3686 		return -ENOMEM;
3687 	}
3688 
3689 	/* set the context in the HMC */
3690 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3691 	if (err) {
3692 		dev_info(&vsi->back->pdev->dev,
3693 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3694 			 ring->queue_index, pf_q, err);
3695 		return -ENOMEM;
3696 	}
3697 
3698 	/* configure Rx buffer alignment */
3699 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3700 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3701 			dev_info(&vsi->back->pdev->dev,
3702 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3703 			return -EOPNOTSUPP;
3704 		}
3705 		clear_ring_build_skb_enabled(ring);
3706 	} else {
3707 		set_ring_build_skb_enabled(ring);
3708 	}
3709 
3710 	ring->rx_offset = i40e_rx_offset(ring);
3711 
3712 	/* cache tail for quicker writes, and clear the reg before use */
3713 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3714 	writel(0, ring->tail);
3715 
3716 	if (ring->xsk_pool) {
3717 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3718 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3719 	} else {
3720 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3721 	}
3722 	if (!ok) {
3723 		/* Log this in case the user has forgotten to give the kernel
3724 		 * any buffers, even later in the application.
3725 		 */
3726 		dev_info(&vsi->back->pdev->dev,
3727 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3728 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3729 			 ring->queue_index, pf_q);
3730 	}
3731 
3732 	return 0;
3733 }
3734 
3735 /**
3736  * i40e_vsi_configure_tx - Configure the VSI for Tx
3737  * @vsi: VSI structure describing this set of rings and resources
3738  *
3739  * Configure the Tx VSI for operation.
3740  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3741 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3742 {
3743 	int err = 0;
3744 	u16 i;
3745 
3746 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3747 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3748 
3749 	if (err || !i40e_enabled_xdp_vsi(vsi))
3750 		return err;
3751 
3752 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3753 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3754 
3755 	return err;
3756 }
3757 
3758 /**
3759  * i40e_vsi_configure_rx - Configure the VSI for Rx
3760  * @vsi: the VSI being configured
3761  *
3762  * Configure the Rx VSI for operation.
3763  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3764 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3765 {
3766 	int err = 0;
3767 	u16 i;
3768 
3769 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3770 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3771 
3772 #if (PAGE_SIZE < 8192)
3773 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3774 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3775 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3776 		vsi->max_frame = vsi->rx_buf_len;
3777 	}
3778 #endif
3779 
3780 	/* set up individual rings */
3781 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3782 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3783 
3784 	return err;
3785 }
3786 
3787 /**
3788  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3789  * @vsi: ptr to the VSI
3790  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3791 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3792 {
3793 	struct i40e_ring *tx_ring, *rx_ring;
3794 	u16 qoffset, qcount;
3795 	int i, n;
3796 
3797 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3798 		/* Reset the TC information */
3799 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3800 			rx_ring = vsi->rx_rings[i];
3801 			tx_ring = vsi->tx_rings[i];
3802 			rx_ring->dcb_tc = 0;
3803 			tx_ring->dcb_tc = 0;
3804 		}
3805 		return;
3806 	}
3807 
3808 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3809 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3810 			continue;
3811 
3812 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3813 		qcount = vsi->tc_config.tc_info[n].qcount;
3814 		for (i = qoffset; i < (qoffset + qcount); i++) {
3815 			rx_ring = vsi->rx_rings[i];
3816 			tx_ring = vsi->tx_rings[i];
3817 			rx_ring->dcb_tc = n;
3818 			tx_ring->dcb_tc = n;
3819 		}
3820 	}
3821 }
3822 
3823 /**
3824  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3825  * @vsi: ptr to the VSI
3826  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3827 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3828 {
3829 	if (vsi->netdev)
3830 		i40e_set_rx_mode(vsi->netdev);
3831 }
3832 
3833 /**
3834  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3835  * @pf: Pointer to the targeted PF
3836  *
3837  * Set all flow director counters to 0.
3838  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3839 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3840 {
3841 	pf->fd_tcp4_filter_cnt = 0;
3842 	pf->fd_udp4_filter_cnt = 0;
3843 	pf->fd_sctp4_filter_cnt = 0;
3844 	pf->fd_ip4_filter_cnt = 0;
3845 	pf->fd_tcp6_filter_cnt = 0;
3846 	pf->fd_udp6_filter_cnt = 0;
3847 	pf->fd_sctp6_filter_cnt = 0;
3848 	pf->fd_ip6_filter_cnt = 0;
3849 }
3850 
3851 /**
3852  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3853  * @vsi: Pointer to the targeted VSI
3854  *
3855  * This function replays the hlist on the hw where all the SB Flow Director
3856  * filters were saved.
3857  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3858 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3859 {
3860 	struct i40e_fdir_filter *filter;
3861 	struct i40e_pf *pf = vsi->back;
3862 	struct hlist_node *node;
3863 
3864 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3865 		return;
3866 
3867 	/* Reset FDir counters as we're replaying all existing filters */
3868 	i40e_reset_fdir_filter_cnt(pf);
3869 
3870 	hlist_for_each_entry_safe(filter, node,
3871 				  &pf->fdir_filter_list, fdir_node) {
3872 		i40e_add_del_fdir(vsi, filter, true);
3873 	}
3874 }
3875 
3876 /**
3877  * i40e_vsi_configure - Set up the VSI for action
3878  * @vsi: the VSI being configured
3879  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3880 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3881 {
3882 	int err;
3883 
3884 	i40e_set_vsi_rx_mode(vsi);
3885 	i40e_restore_vlan(vsi);
3886 	i40e_vsi_config_dcb_rings(vsi);
3887 	err = i40e_vsi_configure_tx(vsi);
3888 	if (!err)
3889 		err = i40e_vsi_configure_rx(vsi);
3890 
3891 	return err;
3892 }
3893 
3894 /**
3895  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3896  * @vsi: the VSI being configured
3897  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3898 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3899 {
3900 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3901 	struct i40e_pf *pf = vsi->back;
3902 	struct i40e_hw *hw = &pf->hw;
3903 	u16 vector;
3904 	int i, q;
3905 	u32 qp;
3906 
3907 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3908 	 * and PFINT_LNKLSTn registers, e.g.:
3909 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3910 	 */
3911 	qp = vsi->base_queue;
3912 	vector = vsi->base_vector;
3913 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3914 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3915 
3916 		q_vector->rx.next_update = jiffies + 1;
3917 		q_vector->rx.target_itr =
3918 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3919 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3920 		     q_vector->rx.target_itr >> 1);
3921 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3922 
3923 		q_vector->tx.next_update = jiffies + 1;
3924 		q_vector->tx.target_itr =
3925 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3926 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3927 		     q_vector->tx.target_itr >> 1);
3928 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3929 
3930 		/* Set ITR for software interrupts triggered after exiting
3931 		 * busy-loop polling.
3932 		 */
3933 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3934 		     I40E_ITR_20K);
3935 
3936 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3937 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3938 
3939 		/* begin of linked list for RX queue assigned to this vector */
3940 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3941 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3942 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3943 			u32 val;
3944 
3945 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3946 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3947 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3948 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3949 			      (I40E_QUEUE_TYPE_TX <<
3950 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3951 
3952 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3953 
3954 			if (has_xdp) {
3955 				/* TX queue with next queue set to TX */
3956 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3957 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3958 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3959 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3960 				      (I40E_QUEUE_TYPE_TX <<
3961 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3962 
3963 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3964 			}
3965 			/* TX queue with next RX or end of linked list */
3966 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3967 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3968 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3969 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3970 			      (I40E_QUEUE_TYPE_RX <<
3971 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3972 
3973 			/* Terminate the linked list */
3974 			if (q == (q_vector->num_ringpairs - 1))
3975 				val |= (I40E_QUEUE_END_OF_LIST <<
3976 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3977 
3978 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3979 			qp++;
3980 		}
3981 	}
3982 
3983 	i40e_flush(hw);
3984 }
3985 
3986 /**
3987  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3988  * @pf: pointer to private device data structure
3989  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3990 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3991 {
3992 	struct i40e_hw *hw = &pf->hw;
3993 	u32 val;
3994 
3995 	/* clear things first */
3996 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3997 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3998 
3999 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
4000 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
4001 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
4002 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
4003 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
4004 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
4005 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
4006 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4007 
4008 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
4009 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4010 
4011 	if (pf->flags & I40E_FLAG_PTP)
4012 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4013 
4014 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
4015 
4016 	/* SW_ITR_IDX = 0, but don't change INTENA */
4017 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
4018 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
4019 
4020 	/* OTHER_ITR_IDX = 0 */
4021 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4022 }
4023 
4024 /**
4025  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4026  * @vsi: the VSI being configured
4027  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)4028 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4029 {
4030 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4031 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4032 	struct i40e_pf *pf = vsi->back;
4033 	struct i40e_hw *hw = &pf->hw;
4034 
4035 	/* set the ITR configuration */
4036 	q_vector->rx.next_update = jiffies + 1;
4037 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4038 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4039 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4040 	q_vector->tx.next_update = jiffies + 1;
4041 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4042 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4043 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4044 
4045 	i40e_enable_misc_int_causes(pf);
4046 
4047 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4048 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4049 
4050 	/* Associate the queue pair to the vector and enable the queue
4051 	 * interrupt RX queue in linked list with next queue set to TX
4052 	 */
4053 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4054 
4055 	if (i40e_enabled_xdp_vsi(vsi)) {
4056 		/* TX queue in linked list with next queue set to TX */
4057 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4058 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4059 	}
4060 
4061 	/* last TX queue so the next RX queue doesn't matter */
4062 	wr32(hw, I40E_QINT_TQCTL(0),
4063 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4064 	i40e_flush(hw);
4065 }
4066 
4067 /**
4068  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4069  * @pf: board private structure
4070  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4071 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4072 {
4073 	struct i40e_hw *hw = &pf->hw;
4074 
4075 	wr32(hw, I40E_PFINT_DYN_CTL0,
4076 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4077 	i40e_flush(hw);
4078 }
4079 
4080 /**
4081  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4082  * @pf: board private structure
4083  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4084 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4085 {
4086 	struct i40e_hw *hw = &pf->hw;
4087 	u32 val;
4088 
4089 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4090 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4091 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4092 
4093 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4094 	i40e_flush(hw);
4095 }
4096 
4097 /**
4098  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4099  * @irq: interrupt number
4100  * @data: pointer to a q_vector
4101  **/
i40e_msix_clean_rings(int irq,void * data)4102 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4103 {
4104 	struct i40e_q_vector *q_vector = data;
4105 
4106 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4107 		return IRQ_HANDLED;
4108 
4109 	napi_schedule_irqoff(&q_vector->napi);
4110 
4111 	return IRQ_HANDLED;
4112 }
4113 
4114 /**
4115  * i40e_irq_affinity_notify - Callback for affinity changes
4116  * @notify: context as to what irq was changed
4117  * @mask: the new affinity mask
4118  *
4119  * This is a callback function used by the irq_set_affinity_notifier function
4120  * so that we may register to receive changes to the irq affinity masks.
4121  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4122 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4123 				     const cpumask_t *mask)
4124 {
4125 	struct i40e_q_vector *q_vector =
4126 		container_of(notify, struct i40e_q_vector, affinity_notify);
4127 
4128 	cpumask_copy(&q_vector->affinity_mask, mask);
4129 }
4130 
4131 /**
4132  * i40e_irq_affinity_release - Callback for affinity notifier release
4133  * @ref: internal core kernel usage
4134  *
4135  * This is a callback function used by the irq_set_affinity_notifier function
4136  * to inform the current notification subscriber that they will no longer
4137  * receive notifications.
4138  **/
i40e_irq_affinity_release(struct kref * ref)4139 static void i40e_irq_affinity_release(struct kref *ref) {}
4140 
4141 /**
4142  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4143  * @vsi: the VSI being configured
4144  * @basename: name for the vector
4145  *
4146  * Allocates MSI-X vectors and requests interrupts from the kernel.
4147  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4148 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4149 {
4150 	int q_vectors = vsi->num_q_vectors;
4151 	struct i40e_pf *pf = vsi->back;
4152 	int base = vsi->base_vector;
4153 	int rx_int_idx = 0;
4154 	int tx_int_idx = 0;
4155 	int vector, err;
4156 	int irq_num;
4157 	int cpu;
4158 
4159 	for (vector = 0; vector < q_vectors; vector++) {
4160 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4161 
4162 		irq_num = pf->msix_entries[base + vector].vector;
4163 
4164 		if (q_vector->tx.ring && q_vector->rx.ring) {
4165 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4166 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4167 			tx_int_idx++;
4168 		} else if (q_vector->rx.ring) {
4169 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4170 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4171 		} else if (q_vector->tx.ring) {
4172 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4173 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4174 		} else {
4175 			/* skip this unused q_vector */
4176 			continue;
4177 		}
4178 		err = request_irq(irq_num,
4179 				  vsi->irq_handler,
4180 				  0,
4181 				  q_vector->name,
4182 				  q_vector);
4183 		if (err) {
4184 			dev_info(&pf->pdev->dev,
4185 				 "MSIX request_irq failed, error: %d\n", err);
4186 			goto free_queue_irqs;
4187 		}
4188 
4189 		/* register for affinity change notifications */
4190 		q_vector->irq_num = irq_num;
4191 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4192 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4193 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4194 		/* Spread affinity hints out across online CPUs.
4195 		 *
4196 		 * get_cpu_mask returns a static constant mask with
4197 		 * a permanent lifetime so it's ok to pass to
4198 		 * irq_update_affinity_hint without making a copy.
4199 		 */
4200 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4201 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4202 	}
4203 
4204 	vsi->irqs_ready = true;
4205 	return 0;
4206 
4207 free_queue_irqs:
4208 	while (vector) {
4209 		vector--;
4210 		irq_num = pf->msix_entries[base + vector].vector;
4211 		irq_set_affinity_notifier(irq_num, NULL);
4212 		irq_update_affinity_hint(irq_num, NULL);
4213 		free_irq(irq_num, &vsi->q_vectors[vector]);
4214 	}
4215 	return err;
4216 }
4217 
4218 /**
4219  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4220  * @vsi: the VSI being un-configured
4221  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4222 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4223 {
4224 	struct i40e_pf *pf = vsi->back;
4225 	struct i40e_hw *hw = &pf->hw;
4226 	int base = vsi->base_vector;
4227 	int i;
4228 
4229 	/* disable interrupt causation from each queue */
4230 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4231 		u32 val;
4232 
4233 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4234 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4235 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4236 
4237 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4238 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4239 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4240 
4241 		if (!i40e_enabled_xdp_vsi(vsi))
4242 			continue;
4243 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4244 	}
4245 
4246 	/* disable each interrupt */
4247 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4248 		for (i = vsi->base_vector;
4249 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4250 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4251 
4252 		i40e_flush(hw);
4253 		for (i = 0; i < vsi->num_q_vectors; i++)
4254 			synchronize_irq(pf->msix_entries[i + base].vector);
4255 	} else {
4256 		/* Legacy and MSI mode - this stops all interrupt handling */
4257 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4258 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4259 		i40e_flush(hw);
4260 		synchronize_irq(pf->pdev->irq);
4261 	}
4262 }
4263 
4264 /**
4265  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4266  * @vsi: the VSI being configured
4267  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4268 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4269 {
4270 	struct i40e_pf *pf = vsi->back;
4271 	int i;
4272 
4273 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4274 		for (i = 0; i < vsi->num_q_vectors; i++)
4275 			i40e_irq_dynamic_enable(vsi, i);
4276 	} else {
4277 		i40e_irq_dynamic_enable_icr0(pf);
4278 	}
4279 
4280 	i40e_flush(&pf->hw);
4281 	return 0;
4282 }
4283 
4284 /**
4285  * i40e_free_misc_vector - Free the vector that handles non-queue events
4286  * @pf: board private structure
4287  **/
i40e_free_misc_vector(struct i40e_pf * pf)4288 static void i40e_free_misc_vector(struct i40e_pf *pf)
4289 {
4290 	/* Disable ICR 0 */
4291 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4292 	i40e_flush(&pf->hw);
4293 
4294 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4295 		free_irq(pf->msix_entries[0].vector, pf);
4296 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4297 	}
4298 }
4299 
4300 /**
4301  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4302  * @irq: interrupt number
4303  * @data: pointer to a q_vector
4304  *
4305  * This is the handler used for all MSI/Legacy interrupts, and deals
4306  * with both queue and non-queue interrupts.  This is also used in
4307  * MSIX mode to handle the non-queue interrupts.
4308  **/
i40e_intr(int irq,void * data)4309 static irqreturn_t i40e_intr(int irq, void *data)
4310 {
4311 	struct i40e_pf *pf = (struct i40e_pf *)data;
4312 	struct i40e_hw *hw = &pf->hw;
4313 	irqreturn_t ret = IRQ_NONE;
4314 	u32 icr0, icr0_remaining;
4315 	u32 val, ena_mask;
4316 
4317 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4318 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4319 
4320 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4321 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4322 		goto enable_intr;
4323 
4324 	/* if interrupt but no bits showing, must be SWINT */
4325 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4326 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4327 		pf->sw_int_count++;
4328 
4329 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4330 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4331 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4332 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4333 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4334 	}
4335 
4336 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4337 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4338 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4339 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4340 
4341 		/* We do not have a way to disarm Queue causes while leaving
4342 		 * interrupt enabled for all other causes, ideally
4343 		 * interrupt should be disabled while we are in NAPI but
4344 		 * this is not a performance path and napi_schedule()
4345 		 * can deal with rescheduling.
4346 		 */
4347 		if (!test_bit(__I40E_DOWN, pf->state))
4348 			napi_schedule_irqoff(&q_vector->napi);
4349 	}
4350 
4351 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4352 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4353 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4354 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4355 	}
4356 
4357 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4358 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4359 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4360 	}
4361 
4362 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4363 		/* disable any further VFLR event notifications */
4364 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4365 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4366 
4367 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4368 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4369 		} else {
4370 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4371 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4372 		}
4373 	}
4374 
4375 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4376 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4377 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4378 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4379 		val = rd32(hw, I40E_GLGEN_RSTAT);
4380 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4381 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4382 		if (val == I40E_RESET_CORER) {
4383 			pf->corer_count++;
4384 		} else if (val == I40E_RESET_GLOBR) {
4385 			pf->globr_count++;
4386 		} else if (val == I40E_RESET_EMPR) {
4387 			pf->empr_count++;
4388 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4389 		}
4390 	}
4391 
4392 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4393 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4394 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4395 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4396 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4397 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4398 	}
4399 
4400 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4401 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4402 
4403 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4404 			schedule_work(&pf->ptp_extts0_work);
4405 
4406 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4407 			i40e_ptp_tx_hwtstamp(pf);
4408 
4409 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4410 	}
4411 
4412 	/* If a critical error is pending we have no choice but to reset the
4413 	 * device.
4414 	 * Report and mask out any remaining unexpected interrupts.
4415 	 */
4416 	icr0_remaining = icr0 & ena_mask;
4417 	if (icr0_remaining) {
4418 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4419 			 icr0_remaining);
4420 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4421 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4422 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4423 			dev_info(&pf->pdev->dev, "device will be reset\n");
4424 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4425 			i40e_service_event_schedule(pf);
4426 		}
4427 		ena_mask &= ~icr0_remaining;
4428 	}
4429 	ret = IRQ_HANDLED;
4430 
4431 enable_intr:
4432 	/* re-enable interrupt causes */
4433 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4434 	if (!test_bit(__I40E_DOWN, pf->state) ||
4435 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4436 		i40e_service_event_schedule(pf);
4437 		i40e_irq_dynamic_enable_icr0(pf);
4438 	}
4439 
4440 	return ret;
4441 }
4442 
4443 /**
4444  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4445  * @tx_ring:  tx ring to clean
4446  * @budget:   how many cleans we're allowed
4447  *
4448  * Returns true if there's any budget left (e.g. the clean is finished)
4449  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4450 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4451 {
4452 	struct i40e_vsi *vsi = tx_ring->vsi;
4453 	u16 i = tx_ring->next_to_clean;
4454 	struct i40e_tx_buffer *tx_buf;
4455 	struct i40e_tx_desc *tx_desc;
4456 
4457 	tx_buf = &tx_ring->tx_bi[i];
4458 	tx_desc = I40E_TX_DESC(tx_ring, i);
4459 	i -= tx_ring->count;
4460 
4461 	do {
4462 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4463 
4464 		/* if next_to_watch is not set then there is no work pending */
4465 		if (!eop_desc)
4466 			break;
4467 
4468 		/* prevent any other reads prior to eop_desc */
4469 		smp_rmb();
4470 
4471 		/* if the descriptor isn't done, no work yet to do */
4472 		if (!(eop_desc->cmd_type_offset_bsz &
4473 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4474 			break;
4475 
4476 		/* clear next_to_watch to prevent false hangs */
4477 		tx_buf->next_to_watch = NULL;
4478 
4479 		tx_desc->buffer_addr = 0;
4480 		tx_desc->cmd_type_offset_bsz = 0;
4481 		/* move past filter desc */
4482 		tx_buf++;
4483 		tx_desc++;
4484 		i++;
4485 		if (unlikely(!i)) {
4486 			i -= tx_ring->count;
4487 			tx_buf = tx_ring->tx_bi;
4488 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4489 		}
4490 		/* unmap skb header data */
4491 		dma_unmap_single(tx_ring->dev,
4492 				 dma_unmap_addr(tx_buf, dma),
4493 				 dma_unmap_len(tx_buf, len),
4494 				 DMA_TO_DEVICE);
4495 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4496 			kfree(tx_buf->raw_buf);
4497 
4498 		tx_buf->raw_buf = NULL;
4499 		tx_buf->tx_flags = 0;
4500 		tx_buf->next_to_watch = NULL;
4501 		dma_unmap_len_set(tx_buf, len, 0);
4502 		tx_desc->buffer_addr = 0;
4503 		tx_desc->cmd_type_offset_bsz = 0;
4504 
4505 		/* move us past the eop_desc for start of next FD desc */
4506 		tx_buf++;
4507 		tx_desc++;
4508 		i++;
4509 		if (unlikely(!i)) {
4510 			i -= tx_ring->count;
4511 			tx_buf = tx_ring->tx_bi;
4512 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4513 		}
4514 
4515 		/* update budget accounting */
4516 		budget--;
4517 	} while (likely(budget));
4518 
4519 	i += tx_ring->count;
4520 	tx_ring->next_to_clean = i;
4521 
4522 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4523 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4524 
4525 	return budget > 0;
4526 }
4527 
4528 /**
4529  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4530  * @irq: interrupt number
4531  * @data: pointer to a q_vector
4532  **/
i40e_fdir_clean_ring(int irq,void * data)4533 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4534 {
4535 	struct i40e_q_vector *q_vector = data;
4536 	struct i40e_vsi *vsi;
4537 
4538 	if (!q_vector->tx.ring)
4539 		return IRQ_HANDLED;
4540 
4541 	vsi = q_vector->tx.ring->vsi;
4542 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4543 
4544 	return IRQ_HANDLED;
4545 }
4546 
4547 /**
4548  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4549  * @vsi: the VSI being configured
4550  * @v_idx: vector index
4551  * @qp_idx: queue pair index
4552  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4553 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4554 {
4555 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4556 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4557 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4558 
4559 	tx_ring->q_vector = q_vector;
4560 	tx_ring->next = q_vector->tx.ring;
4561 	q_vector->tx.ring = tx_ring;
4562 	q_vector->tx.count++;
4563 
4564 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4565 	if (i40e_enabled_xdp_vsi(vsi)) {
4566 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4567 
4568 		xdp_ring->q_vector = q_vector;
4569 		xdp_ring->next = q_vector->tx.ring;
4570 		q_vector->tx.ring = xdp_ring;
4571 		q_vector->tx.count++;
4572 	}
4573 
4574 	rx_ring->q_vector = q_vector;
4575 	rx_ring->next = q_vector->rx.ring;
4576 	q_vector->rx.ring = rx_ring;
4577 	q_vector->rx.count++;
4578 }
4579 
4580 /**
4581  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4582  * @vsi: the VSI being configured
4583  *
4584  * This function maps descriptor rings to the queue-specific vectors
4585  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4586  * one vector per queue pair, but on a constrained vector budget, we
4587  * group the queue pairs as "efficiently" as possible.
4588  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4589 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4590 {
4591 	int qp_remaining = vsi->num_queue_pairs;
4592 	int q_vectors = vsi->num_q_vectors;
4593 	int num_ringpairs;
4594 	int v_start = 0;
4595 	int qp_idx = 0;
4596 
4597 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4598 	 * group them so there are multiple queues per vector.
4599 	 * It is also important to go through all the vectors available to be
4600 	 * sure that if we don't use all the vectors, that the remaining vectors
4601 	 * are cleared. This is especially important when decreasing the
4602 	 * number of queues in use.
4603 	 */
4604 	for (; v_start < q_vectors; v_start++) {
4605 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4606 
4607 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4608 
4609 		q_vector->num_ringpairs = num_ringpairs;
4610 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4611 
4612 		q_vector->rx.count = 0;
4613 		q_vector->tx.count = 0;
4614 		q_vector->rx.ring = NULL;
4615 		q_vector->tx.ring = NULL;
4616 
4617 		while (num_ringpairs--) {
4618 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4619 			qp_idx++;
4620 			qp_remaining--;
4621 		}
4622 	}
4623 }
4624 
4625 /**
4626  * i40e_vsi_request_irq - Request IRQ from the OS
4627  * @vsi: the VSI being configured
4628  * @basename: name for the vector
4629  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4630 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4631 {
4632 	struct i40e_pf *pf = vsi->back;
4633 	int err;
4634 
4635 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4636 		err = i40e_vsi_request_irq_msix(vsi, basename);
4637 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4638 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4639 				  pf->int_name, pf);
4640 	else
4641 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4642 				  pf->int_name, pf);
4643 
4644 	if (err)
4645 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4646 
4647 	return err;
4648 }
4649 
4650 #ifdef CONFIG_NET_POLL_CONTROLLER
4651 /**
4652  * i40e_netpoll - A Polling 'interrupt' handler
4653  * @netdev: network interface device structure
4654  *
4655  * This is used by netconsole to send skbs without having to re-enable
4656  * interrupts.  It's not called while the normal interrupt routine is executing.
4657  **/
i40e_netpoll(struct net_device * netdev)4658 static void i40e_netpoll(struct net_device *netdev)
4659 {
4660 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4661 	struct i40e_vsi *vsi = np->vsi;
4662 	struct i40e_pf *pf = vsi->back;
4663 	int i;
4664 
4665 	/* if interface is down do nothing */
4666 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4667 		return;
4668 
4669 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4670 		for (i = 0; i < vsi->num_q_vectors; i++)
4671 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4672 	} else {
4673 		i40e_intr(pf->pdev->irq, netdev);
4674 	}
4675 }
4676 #endif
4677 
4678 #define I40E_QTX_ENA_WAIT_COUNT 50
4679 
4680 /**
4681  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4682  * @pf: the PF being configured
4683  * @pf_q: the PF queue
4684  * @enable: enable or disable state of the queue
4685  *
4686  * This routine will wait for the given Tx queue of the PF to reach the
4687  * enabled or disabled state.
4688  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4689  * multiple retries; else will return 0 in case of success.
4690  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4691 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4692 {
4693 	int i;
4694 	u32 tx_reg;
4695 
4696 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4697 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4698 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4699 			break;
4700 
4701 		usleep_range(10, 20);
4702 	}
4703 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4704 		return -ETIMEDOUT;
4705 
4706 	return 0;
4707 }
4708 
4709 /**
4710  * i40e_control_tx_q - Start or stop a particular Tx queue
4711  * @pf: the PF structure
4712  * @pf_q: the PF queue to configure
4713  * @enable: start or stop the queue
4714  *
4715  * This function enables or disables a single queue. Note that any delay
4716  * required after the operation is expected to be handled by the caller of
4717  * this function.
4718  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4719 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4720 {
4721 	struct i40e_hw *hw = &pf->hw;
4722 	u32 tx_reg;
4723 	int i;
4724 
4725 	/* warn the TX unit of coming changes */
4726 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4727 	if (!enable)
4728 		usleep_range(10, 20);
4729 
4730 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4731 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4732 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4733 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4734 			break;
4735 		usleep_range(1000, 2000);
4736 	}
4737 
4738 	/* Skip if the queue is already in the requested state */
4739 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4740 		return;
4741 
4742 	/* turn on/off the queue */
4743 	if (enable) {
4744 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4745 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4746 	} else {
4747 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4748 	}
4749 
4750 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4751 }
4752 
4753 /**
4754  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4755  * @seid: VSI SEID
4756  * @pf: the PF structure
4757  * @pf_q: the PF queue to configure
4758  * @is_xdp: true if the queue is used for XDP
4759  * @enable: start or stop the queue
4760  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4761 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4762 			   bool is_xdp, bool enable)
4763 {
4764 	int ret;
4765 
4766 	i40e_control_tx_q(pf, pf_q, enable);
4767 
4768 	/* wait for the change to finish */
4769 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4770 	if (ret) {
4771 		dev_info(&pf->pdev->dev,
4772 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4773 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4774 			 (enable ? "en" : "dis"));
4775 	}
4776 
4777 	return ret;
4778 }
4779 
4780 /**
4781  * i40e_vsi_enable_tx - Start a VSI's rings
4782  * @vsi: the VSI being configured
4783  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4784 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4785 {
4786 	struct i40e_pf *pf = vsi->back;
4787 	int i, pf_q, ret = 0;
4788 
4789 	pf_q = vsi->base_queue;
4790 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4791 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4792 					     pf_q,
4793 					     false /*is xdp*/, true);
4794 		if (ret)
4795 			break;
4796 
4797 		if (!i40e_enabled_xdp_vsi(vsi))
4798 			continue;
4799 
4800 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4801 					     pf_q + vsi->alloc_queue_pairs,
4802 					     true /*is xdp*/, true);
4803 		if (ret)
4804 			break;
4805 	}
4806 	return ret;
4807 }
4808 
4809 /**
4810  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4811  * @pf: the PF being configured
4812  * @pf_q: the PF queue
4813  * @enable: enable or disable state of the queue
4814  *
4815  * This routine will wait for the given Rx queue of the PF to reach the
4816  * enabled or disabled state.
4817  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4818  * multiple retries; else will return 0 in case of success.
4819  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4820 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4821 {
4822 	int i;
4823 	u32 rx_reg;
4824 
4825 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4826 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4827 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4828 			break;
4829 
4830 		usleep_range(10, 20);
4831 	}
4832 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4833 		return -ETIMEDOUT;
4834 
4835 	return 0;
4836 }
4837 
4838 /**
4839  * i40e_control_rx_q - Start or stop a particular Rx queue
4840  * @pf: the PF structure
4841  * @pf_q: the PF queue to configure
4842  * @enable: start or stop the queue
4843  *
4844  * This function enables or disables a single queue. Note that
4845  * any delay required after the operation is expected to be
4846  * handled by the caller of this function.
4847  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4848 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4849 {
4850 	struct i40e_hw *hw = &pf->hw;
4851 	u32 rx_reg;
4852 	int i;
4853 
4854 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4855 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4856 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4857 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4858 			break;
4859 		usleep_range(1000, 2000);
4860 	}
4861 
4862 	/* Skip if the queue is already in the requested state */
4863 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4864 		return;
4865 
4866 	/* turn on/off the queue */
4867 	if (enable)
4868 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4869 	else
4870 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4871 
4872 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4873 }
4874 
4875 /**
4876  * i40e_control_wait_rx_q
4877  * @pf: the PF structure
4878  * @pf_q: queue being configured
4879  * @enable: start or stop the rings
4880  *
4881  * This function enables or disables a single queue along with waiting
4882  * for the change to finish. The caller of this function should handle
4883  * the delays needed in the case of disabling queues.
4884  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4885 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4886 {
4887 	int ret = 0;
4888 
4889 	i40e_control_rx_q(pf, pf_q, enable);
4890 
4891 	/* wait for the change to finish */
4892 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4893 	if (ret)
4894 		return ret;
4895 
4896 	return ret;
4897 }
4898 
4899 /**
4900  * i40e_vsi_enable_rx - Start a VSI's rings
4901  * @vsi: the VSI being configured
4902  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4903 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4904 {
4905 	struct i40e_pf *pf = vsi->back;
4906 	int i, pf_q, ret = 0;
4907 
4908 	pf_q = vsi->base_queue;
4909 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4910 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4911 		if (ret) {
4912 			dev_info(&pf->pdev->dev,
4913 				 "VSI seid %d Rx ring %d enable timeout\n",
4914 				 vsi->seid, pf_q);
4915 			break;
4916 		}
4917 	}
4918 
4919 	return ret;
4920 }
4921 
4922 /**
4923  * i40e_vsi_start_rings - Start a VSI's rings
4924  * @vsi: the VSI being configured
4925  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4926 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4927 {
4928 	int ret = 0;
4929 
4930 	/* do rx first for enable and last for disable */
4931 	ret = i40e_vsi_enable_rx(vsi);
4932 	if (ret)
4933 		return ret;
4934 	ret = i40e_vsi_enable_tx(vsi);
4935 
4936 	return ret;
4937 }
4938 
4939 #define I40E_DISABLE_TX_GAP_MSEC	50
4940 
4941 /**
4942  * i40e_vsi_stop_rings - Stop a VSI's rings
4943  * @vsi: the VSI being configured
4944  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4945 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4946 {
4947 	struct i40e_pf *pf = vsi->back;
4948 	int pf_q, err, q_end;
4949 
4950 	/* When port TX is suspended, don't wait */
4951 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4952 		return i40e_vsi_stop_rings_no_wait(vsi);
4953 
4954 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4955 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4956 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4957 
4958 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4959 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4960 		if (err)
4961 			dev_info(&pf->pdev->dev,
4962 				 "VSI seid %d Rx ring %d disable timeout\n",
4963 				 vsi->seid, pf_q);
4964 	}
4965 
4966 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4967 	pf_q = vsi->base_queue;
4968 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4969 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4970 
4971 	i40e_vsi_wait_queues_disabled(vsi);
4972 }
4973 
4974 /**
4975  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4976  * @vsi: the VSI being shutdown
4977  *
4978  * This function stops all the rings for a VSI but does not delay to verify
4979  * that rings have been disabled. It is expected that the caller is shutting
4980  * down multiple VSIs at once and will delay together for all the VSIs after
4981  * initiating the shutdown. This is particularly useful for shutting down lots
4982  * of VFs together. Otherwise, a large delay can be incurred while configuring
4983  * each VSI in serial.
4984  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4985 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4986 {
4987 	struct i40e_pf *pf = vsi->back;
4988 	int i, pf_q;
4989 
4990 	pf_q = vsi->base_queue;
4991 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4992 		i40e_control_tx_q(pf, pf_q, false);
4993 		i40e_control_rx_q(pf, pf_q, false);
4994 	}
4995 }
4996 
4997 /**
4998  * i40e_vsi_free_irq - Free the irq association with the OS
4999  * @vsi: the VSI being configured
5000  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)5001 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
5002 {
5003 	struct i40e_pf *pf = vsi->back;
5004 	struct i40e_hw *hw = &pf->hw;
5005 	int base = vsi->base_vector;
5006 	u32 val, qp;
5007 	int i;
5008 
5009 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5010 		if (!vsi->q_vectors)
5011 			return;
5012 
5013 		if (!vsi->irqs_ready)
5014 			return;
5015 
5016 		vsi->irqs_ready = false;
5017 		for (i = 0; i < vsi->num_q_vectors; i++) {
5018 			int irq_num;
5019 			u16 vector;
5020 
5021 			vector = i + base;
5022 			irq_num = pf->msix_entries[vector].vector;
5023 
5024 			/* free only the irqs that were actually requested */
5025 			if (!vsi->q_vectors[i] ||
5026 			    !vsi->q_vectors[i]->num_ringpairs)
5027 				continue;
5028 
5029 			/* clear the affinity notifier in the IRQ descriptor */
5030 			irq_set_affinity_notifier(irq_num, NULL);
5031 			/* remove our suggested affinity mask for this IRQ */
5032 			irq_update_affinity_hint(irq_num, NULL);
5033 			free_irq(irq_num, vsi->q_vectors[i]);
5034 
5035 			/* Tear down the interrupt queue link list
5036 			 *
5037 			 * We know that they come in pairs and always
5038 			 * the Rx first, then the Tx.  To clear the
5039 			 * link list, stick the EOL value into the
5040 			 * next_q field of the registers.
5041 			 */
5042 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5043 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5044 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5045 			val |= I40E_QUEUE_END_OF_LIST
5046 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5047 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5048 
5049 			while (qp != I40E_QUEUE_END_OF_LIST) {
5050 				u32 next;
5051 
5052 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5053 
5054 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5055 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5056 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5057 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5058 
5059 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5060 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5061 
5062 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5063 
5064 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5065 
5066 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5067 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5068 
5069 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5070 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5071 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5072 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5073 
5074 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5075 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5076 
5077 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5078 				qp = next;
5079 			}
5080 		}
5081 	} else {
5082 		free_irq(pf->pdev->irq, pf);
5083 
5084 		val = rd32(hw, I40E_PFINT_LNKLST0);
5085 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5086 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5087 		val |= I40E_QUEUE_END_OF_LIST
5088 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5089 		wr32(hw, I40E_PFINT_LNKLST0, val);
5090 
5091 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5092 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5093 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5094 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5095 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5096 
5097 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5098 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5099 
5100 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5101 
5102 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5103 
5104 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5105 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5106 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5107 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5108 
5109 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5110 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5111 
5112 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5113 	}
5114 }
5115 
5116 /**
5117  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5118  * @vsi: the VSI being configured
5119  * @v_idx: Index of vector to be freed
5120  *
5121  * This function frees the memory allocated to the q_vector.  In addition if
5122  * NAPI is enabled it will delete any references to the NAPI struct prior
5123  * to freeing the q_vector.
5124  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5125 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5126 {
5127 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5128 	struct i40e_ring *ring;
5129 
5130 	if (!q_vector)
5131 		return;
5132 
5133 	/* disassociate q_vector from rings */
5134 	i40e_for_each_ring(ring, q_vector->tx)
5135 		ring->q_vector = NULL;
5136 
5137 	i40e_for_each_ring(ring, q_vector->rx)
5138 		ring->q_vector = NULL;
5139 
5140 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5141 	if (vsi->netdev)
5142 		netif_napi_del(&q_vector->napi);
5143 
5144 	vsi->q_vectors[v_idx] = NULL;
5145 
5146 	kfree_rcu(q_vector, rcu);
5147 }
5148 
5149 /**
5150  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5151  * @vsi: the VSI being un-configured
5152  *
5153  * This frees the memory allocated to the q_vectors and
5154  * deletes references to the NAPI struct.
5155  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5156 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5157 {
5158 	int v_idx;
5159 
5160 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5161 		i40e_free_q_vector(vsi, v_idx);
5162 }
5163 
5164 /**
5165  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5166  * @pf: board private structure
5167  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5168 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5169 {
5170 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5171 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5172 		pci_disable_msix(pf->pdev);
5173 		kfree(pf->msix_entries);
5174 		pf->msix_entries = NULL;
5175 		kfree(pf->irq_pile);
5176 		pf->irq_pile = NULL;
5177 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5178 		pci_disable_msi(pf->pdev);
5179 	}
5180 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5181 }
5182 
5183 /**
5184  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5185  * @pf: board private structure
5186  *
5187  * We go through and clear interrupt specific resources and reset the structure
5188  * to pre-load conditions
5189  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5190 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5191 {
5192 	int i;
5193 
5194 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5195 		i40e_free_misc_vector(pf);
5196 
5197 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5198 		      I40E_IWARP_IRQ_PILE_ID);
5199 
5200 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5201 	for (i = 0; i < pf->num_alloc_vsi; i++)
5202 		if (pf->vsi[i])
5203 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5204 	i40e_reset_interrupt_capability(pf);
5205 }
5206 
5207 /**
5208  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5209  * @vsi: the VSI being configured
5210  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5211 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5212 {
5213 	int q_idx;
5214 
5215 	if (!vsi->netdev)
5216 		return;
5217 
5218 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5219 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5220 
5221 		if (q_vector->rx.ring || q_vector->tx.ring)
5222 			napi_enable(&q_vector->napi);
5223 	}
5224 }
5225 
5226 /**
5227  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5228  * @vsi: the VSI being configured
5229  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5230 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5231 {
5232 	int q_idx;
5233 
5234 	if (!vsi->netdev)
5235 		return;
5236 
5237 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5238 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5239 
5240 		if (q_vector->rx.ring || q_vector->tx.ring)
5241 			napi_disable(&q_vector->napi);
5242 	}
5243 }
5244 
5245 /**
5246  * i40e_vsi_close - Shut down a VSI
5247  * @vsi: the vsi to be quelled
5248  **/
i40e_vsi_close(struct i40e_vsi * vsi)5249 static void i40e_vsi_close(struct i40e_vsi *vsi)
5250 {
5251 	struct i40e_pf *pf = vsi->back;
5252 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5253 		i40e_down(vsi);
5254 	i40e_vsi_free_irq(vsi);
5255 	i40e_vsi_free_tx_resources(vsi);
5256 	i40e_vsi_free_rx_resources(vsi);
5257 	vsi->current_netdev_flags = 0;
5258 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5259 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5260 		set_bit(__I40E_CLIENT_RESET, pf->state);
5261 }
5262 
5263 /**
5264  * i40e_quiesce_vsi - Pause a given VSI
5265  * @vsi: the VSI being paused
5266  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5267 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5268 {
5269 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5270 		return;
5271 
5272 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5273 	if (vsi->netdev && netif_running(vsi->netdev))
5274 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5275 	else
5276 		i40e_vsi_close(vsi);
5277 }
5278 
5279 /**
5280  * i40e_unquiesce_vsi - Resume a given VSI
5281  * @vsi: the VSI being resumed
5282  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5283 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5284 {
5285 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5286 		return;
5287 
5288 	if (vsi->netdev && netif_running(vsi->netdev))
5289 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5290 	else
5291 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5292 }
5293 
5294 /**
5295  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5296  * @pf: the PF
5297  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5298 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5299 {
5300 	int v;
5301 
5302 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5303 		if (pf->vsi[v])
5304 			i40e_quiesce_vsi(pf->vsi[v]);
5305 	}
5306 }
5307 
5308 /**
5309  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5310  * @pf: the PF
5311  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5312 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5313 {
5314 	int v;
5315 
5316 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5317 		if (pf->vsi[v])
5318 			i40e_unquiesce_vsi(pf->vsi[v]);
5319 	}
5320 }
5321 
5322 /**
5323  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5324  * @vsi: the VSI being configured
5325  *
5326  * Wait until all queues on a given VSI have been disabled.
5327  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5328 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5329 {
5330 	struct i40e_pf *pf = vsi->back;
5331 	int i, pf_q, ret;
5332 
5333 	pf_q = vsi->base_queue;
5334 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5335 		/* Check and wait for the Tx queue */
5336 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5337 		if (ret) {
5338 			dev_info(&pf->pdev->dev,
5339 				 "VSI seid %d Tx ring %d disable timeout\n",
5340 				 vsi->seid, pf_q);
5341 			return ret;
5342 		}
5343 
5344 		if (!i40e_enabled_xdp_vsi(vsi))
5345 			goto wait_rx;
5346 
5347 		/* Check and wait for the XDP Tx queue */
5348 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5349 				       false);
5350 		if (ret) {
5351 			dev_info(&pf->pdev->dev,
5352 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5353 				 vsi->seid, pf_q);
5354 			return ret;
5355 		}
5356 wait_rx:
5357 		/* Check and wait for the Rx queue */
5358 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5359 		if (ret) {
5360 			dev_info(&pf->pdev->dev,
5361 				 "VSI seid %d Rx ring %d disable timeout\n",
5362 				 vsi->seid, pf_q);
5363 			return ret;
5364 		}
5365 	}
5366 
5367 	return 0;
5368 }
5369 
5370 #ifdef CONFIG_I40E_DCB
5371 /**
5372  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5373  * @pf: the PF
5374  *
5375  * This function waits for the queues to be in disabled state for all the
5376  * VSIs that are managed by this PF.
5377  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5378 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5379 {
5380 	int v, ret = 0;
5381 
5382 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5383 		if (pf->vsi[v]) {
5384 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5385 			if (ret)
5386 				break;
5387 		}
5388 	}
5389 
5390 	return ret;
5391 }
5392 
5393 #endif
5394 
5395 /**
5396  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5397  * @pf: pointer to PF
5398  *
5399  * Get TC map for ISCSI PF type that will include iSCSI TC
5400  * and LAN TC.
5401  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5402 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5403 {
5404 	struct i40e_dcb_app_priority_table app;
5405 	struct i40e_hw *hw = &pf->hw;
5406 	u8 enabled_tc = 1; /* TC0 is always enabled */
5407 	u8 tc, i;
5408 	/* Get the iSCSI APP TLV */
5409 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5410 
5411 	for (i = 0; i < dcbcfg->numapps; i++) {
5412 		app = dcbcfg->app[i];
5413 		if (app.selector == I40E_APP_SEL_TCPIP &&
5414 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5415 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5416 			enabled_tc |= BIT(tc);
5417 			break;
5418 		}
5419 	}
5420 
5421 	return enabled_tc;
5422 }
5423 
5424 /**
5425  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5426  * @dcbcfg: the corresponding DCBx configuration structure
5427  *
5428  * Return the number of TCs from given DCBx configuration
5429  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5430 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5431 {
5432 	int i, tc_unused = 0;
5433 	u8 num_tc = 0;
5434 	u8 ret = 0;
5435 
5436 	/* Scan the ETS Config Priority Table to find
5437 	 * traffic class enabled for a given priority
5438 	 * and create a bitmask of enabled TCs
5439 	 */
5440 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5441 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5442 
5443 	/* Now scan the bitmask to check for
5444 	 * contiguous TCs starting with TC0
5445 	 */
5446 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5447 		if (num_tc & BIT(i)) {
5448 			if (!tc_unused) {
5449 				ret++;
5450 			} else {
5451 				pr_err("Non-contiguous TC - Disabling DCB\n");
5452 				return 1;
5453 			}
5454 		} else {
5455 			tc_unused = 1;
5456 		}
5457 	}
5458 
5459 	/* There is always at least TC0 */
5460 	if (!ret)
5461 		ret = 1;
5462 
5463 	return ret;
5464 }
5465 
5466 /**
5467  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5468  * @dcbcfg: the corresponding DCBx configuration structure
5469  *
5470  * Query the current DCB configuration and return the number of
5471  * traffic classes enabled from the given DCBX config
5472  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5473 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5474 {
5475 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5476 	u8 enabled_tc = 1;
5477 	u8 i;
5478 
5479 	for (i = 0; i < num_tc; i++)
5480 		enabled_tc |= BIT(i);
5481 
5482 	return enabled_tc;
5483 }
5484 
5485 /**
5486  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5487  * @pf: PF being queried
5488  *
5489  * Query the current MQPRIO configuration and return the number of
5490  * traffic classes enabled.
5491  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5492 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5493 {
5494 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5495 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5496 	u8 enabled_tc = 1, i;
5497 
5498 	for (i = 1; i < num_tc; i++)
5499 		enabled_tc |= BIT(i);
5500 	return enabled_tc;
5501 }
5502 
5503 /**
5504  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5505  * @pf: PF being queried
5506  *
5507  * Return number of traffic classes enabled for the given PF
5508  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5509 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5510 {
5511 	struct i40e_hw *hw = &pf->hw;
5512 	u8 i, enabled_tc = 1;
5513 	u8 num_tc = 0;
5514 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5515 
5516 	if (i40e_is_tc_mqprio_enabled(pf))
5517 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5518 
5519 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5520 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5521 		return 1;
5522 
5523 	/* SFP mode will be enabled for all TCs on port */
5524 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5525 		return i40e_dcb_get_num_tc(dcbcfg);
5526 
5527 	/* MFP mode return count of enabled TCs for this PF */
5528 	if (pf->hw.func_caps.iscsi)
5529 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5530 	else
5531 		return 1; /* Only TC0 */
5532 
5533 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5534 		if (enabled_tc & BIT(i))
5535 			num_tc++;
5536 	}
5537 	return num_tc;
5538 }
5539 
5540 /**
5541  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5542  * @pf: PF being queried
5543  *
5544  * Return a bitmap for enabled traffic classes for this PF.
5545  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5546 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5547 {
5548 	if (i40e_is_tc_mqprio_enabled(pf))
5549 		return i40e_mqprio_get_enabled_tc(pf);
5550 
5551 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5552 	 * default TC
5553 	 */
5554 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5555 		return I40E_DEFAULT_TRAFFIC_CLASS;
5556 
5557 	/* SFP mode we want PF to be enabled for all TCs */
5558 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5559 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5560 
5561 	/* MFP enabled and iSCSI PF type */
5562 	if (pf->hw.func_caps.iscsi)
5563 		return i40e_get_iscsi_tc_map(pf);
5564 	else
5565 		return I40E_DEFAULT_TRAFFIC_CLASS;
5566 }
5567 
5568 /**
5569  * i40e_vsi_get_bw_info - Query VSI BW Information
5570  * @vsi: the VSI being queried
5571  *
5572  * Returns 0 on success, negative value on failure
5573  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5574 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5575 {
5576 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5577 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5578 	struct i40e_pf *pf = vsi->back;
5579 	struct i40e_hw *hw = &pf->hw;
5580 	u32 tc_bw_max;
5581 	int ret;
5582 	int i;
5583 
5584 	/* Get the VSI level BW configuration */
5585 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5586 	if (ret) {
5587 		dev_info(&pf->pdev->dev,
5588 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5589 			 ERR_PTR(ret),
5590 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5591 		return -EINVAL;
5592 	}
5593 
5594 	/* Get the VSI level BW configuration per TC */
5595 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5596 					       NULL);
5597 	if (ret) {
5598 		dev_info(&pf->pdev->dev,
5599 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5600 			 ERR_PTR(ret),
5601 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5602 		return -EINVAL;
5603 	}
5604 
5605 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5606 		dev_info(&pf->pdev->dev,
5607 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5608 			 bw_config.tc_valid_bits,
5609 			 bw_ets_config.tc_valid_bits);
5610 		/* Still continuing */
5611 	}
5612 
5613 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5614 	vsi->bw_max_quanta = bw_config.max_bw;
5615 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5616 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5617 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5618 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5619 		vsi->bw_ets_limit_credits[i] =
5620 					le16_to_cpu(bw_ets_config.credits[i]);
5621 		/* 3 bits out of 4 for each TC */
5622 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5623 	}
5624 
5625 	return 0;
5626 }
5627 
5628 /**
5629  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5630  * @vsi: the VSI being configured
5631  * @enabled_tc: TC bitmap
5632  * @bw_share: BW shared credits per TC
5633  *
5634  * Returns 0 on success, negative value on failure
5635  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5636 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5637 				       u8 *bw_share)
5638 {
5639 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5640 	struct i40e_pf *pf = vsi->back;
5641 	int ret;
5642 	int i;
5643 
5644 	/* There is no need to reset BW when mqprio mode is on.  */
5645 	if (i40e_is_tc_mqprio_enabled(pf))
5646 		return 0;
5647 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5648 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5649 		if (ret)
5650 			dev_info(&pf->pdev->dev,
5651 				 "Failed to reset tx rate for vsi->seid %u\n",
5652 				 vsi->seid);
5653 		return ret;
5654 	}
5655 	memset(&bw_data, 0, sizeof(bw_data));
5656 	bw_data.tc_valid_bits = enabled_tc;
5657 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5658 		bw_data.tc_bw_credits[i] = bw_share[i];
5659 
5660 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5661 	if (ret) {
5662 		dev_info(&pf->pdev->dev,
5663 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5664 			 pf->hw.aq.asq_last_status);
5665 		return -EINVAL;
5666 	}
5667 
5668 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5669 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5670 
5671 	return 0;
5672 }
5673 
5674 /**
5675  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5676  * @vsi: the VSI being configured
5677  * @enabled_tc: TC map to be enabled
5678  *
5679  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5680 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5681 {
5682 	struct net_device *netdev = vsi->netdev;
5683 	struct i40e_pf *pf = vsi->back;
5684 	struct i40e_hw *hw = &pf->hw;
5685 	u8 netdev_tc = 0;
5686 	int i;
5687 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5688 
5689 	if (!netdev)
5690 		return;
5691 
5692 	if (!enabled_tc) {
5693 		netdev_reset_tc(netdev);
5694 		return;
5695 	}
5696 
5697 	/* Set up actual enabled TCs on the VSI */
5698 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5699 		return;
5700 
5701 	/* set per TC queues for the VSI */
5702 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5703 		/* Only set TC queues for enabled tcs
5704 		 *
5705 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5706 		 * enabled_tc bitmap would be 0x00001001; the driver
5707 		 * will set the numtc for netdev as 2 that will be
5708 		 * referenced by the netdev layer as TC 0 and 1.
5709 		 */
5710 		if (vsi->tc_config.enabled_tc & BIT(i))
5711 			netdev_set_tc_queue(netdev,
5712 					vsi->tc_config.tc_info[i].netdev_tc,
5713 					vsi->tc_config.tc_info[i].qcount,
5714 					vsi->tc_config.tc_info[i].qoffset);
5715 	}
5716 
5717 	if (i40e_is_tc_mqprio_enabled(pf))
5718 		return;
5719 
5720 	/* Assign UP2TC map for the VSI */
5721 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5722 		/* Get the actual TC# for the UP */
5723 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5724 		/* Get the mapped netdev TC# for the UP */
5725 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5726 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5727 	}
5728 }
5729 
5730 /**
5731  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5732  * @vsi: the VSI being configured
5733  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5734  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5735 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5736 				      struct i40e_vsi_context *ctxt)
5737 {
5738 	/* copy just the sections touched not the entire info
5739 	 * since not all sections are valid as returned by
5740 	 * update vsi params
5741 	 */
5742 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5743 	memcpy(&vsi->info.queue_mapping,
5744 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5745 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5746 	       sizeof(vsi->info.tc_mapping));
5747 }
5748 
5749 /**
5750  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5751  * @vsi: the VSI being reconfigured
5752  * @vsi_offset: offset from main VF VSI
5753  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5754 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5755 {
5756 	struct i40e_vsi_context ctxt = {};
5757 	struct i40e_pf *pf;
5758 	struct i40e_hw *hw;
5759 	int ret;
5760 
5761 	if (!vsi)
5762 		return -EINVAL;
5763 	pf = vsi->back;
5764 	hw = &pf->hw;
5765 
5766 	ctxt.seid = vsi->seid;
5767 	ctxt.pf_num = hw->pf_id;
5768 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5769 	ctxt.uplink_seid = vsi->uplink_seid;
5770 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5771 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5772 	ctxt.info = vsi->info;
5773 
5774 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5775 				 false);
5776 	if (vsi->reconfig_rss) {
5777 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5778 				      vsi->num_queue_pairs);
5779 		ret = i40e_vsi_config_rss(vsi);
5780 		if (ret) {
5781 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5782 			return ret;
5783 		}
5784 		vsi->reconfig_rss = false;
5785 	}
5786 
5787 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5788 	if (ret) {
5789 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5790 			 ERR_PTR(ret),
5791 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5792 		return ret;
5793 	}
5794 	/* update the local VSI info with updated queue map */
5795 	i40e_vsi_update_queue_map(vsi, &ctxt);
5796 	vsi->info.valid_sections = 0;
5797 
5798 	return ret;
5799 }
5800 
5801 /**
5802  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5803  * @vsi: VSI to be configured
5804  * @enabled_tc: TC bitmap
5805  *
5806  * This configures a particular VSI for TCs that are mapped to the
5807  * given TC bitmap. It uses default bandwidth share for TCs across
5808  * VSIs to configure TC for a particular VSI.
5809  *
5810  * NOTE:
5811  * It is expected that the VSI queues have been quisced before calling
5812  * this function.
5813  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5814 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5815 {
5816 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5817 	struct i40e_pf *pf = vsi->back;
5818 	struct i40e_hw *hw = &pf->hw;
5819 	struct i40e_vsi_context ctxt;
5820 	int ret = 0;
5821 	int i;
5822 
5823 	/* Check if enabled_tc is same as existing or new TCs */
5824 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5825 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5826 		return ret;
5827 
5828 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5829 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5830 		if (enabled_tc & BIT(i))
5831 			bw_share[i] = 1;
5832 	}
5833 
5834 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5835 	if (ret) {
5836 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5837 
5838 		dev_info(&pf->pdev->dev,
5839 			 "Failed configuring TC map %d for VSI %d\n",
5840 			 enabled_tc, vsi->seid);
5841 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5842 						  &bw_config, NULL);
5843 		if (ret) {
5844 			dev_info(&pf->pdev->dev,
5845 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5846 				 ERR_PTR(ret),
5847 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5848 			goto out;
5849 		}
5850 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5851 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5852 
5853 			if (!valid_tc)
5854 				valid_tc = bw_config.tc_valid_bits;
5855 			/* Always enable TC0, no matter what */
5856 			valid_tc |= 1;
5857 			dev_info(&pf->pdev->dev,
5858 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5859 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5860 			enabled_tc = valid_tc;
5861 		}
5862 
5863 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5864 		if (ret) {
5865 			dev_err(&pf->pdev->dev,
5866 				"Unable to  configure TC map %d for VSI %d\n",
5867 				enabled_tc, vsi->seid);
5868 			goto out;
5869 		}
5870 	}
5871 
5872 	/* Update Queue Pairs Mapping for currently enabled UPs */
5873 	ctxt.seid = vsi->seid;
5874 	ctxt.pf_num = vsi->back->hw.pf_id;
5875 	ctxt.vf_num = 0;
5876 	ctxt.uplink_seid = vsi->uplink_seid;
5877 	ctxt.info = vsi->info;
5878 	if (i40e_is_tc_mqprio_enabled(pf)) {
5879 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5880 		if (ret)
5881 			goto out;
5882 	} else {
5883 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5884 	}
5885 
5886 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5887 	 * queues changed.
5888 	 */
5889 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5890 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5891 				      vsi->num_queue_pairs);
5892 		ret = i40e_vsi_config_rss(vsi);
5893 		if (ret) {
5894 			dev_info(&vsi->back->pdev->dev,
5895 				 "Failed to reconfig rss for num_queues\n");
5896 			return ret;
5897 		}
5898 		vsi->reconfig_rss = false;
5899 	}
5900 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5901 		ctxt.info.valid_sections |=
5902 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5903 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5904 	}
5905 
5906 	/* Update the VSI after updating the VSI queue-mapping
5907 	 * information
5908 	 */
5909 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5910 	if (ret) {
5911 		dev_info(&pf->pdev->dev,
5912 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5913 			 ERR_PTR(ret),
5914 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5915 		goto out;
5916 	}
5917 	/* update the local VSI info with updated queue map */
5918 	i40e_vsi_update_queue_map(vsi, &ctxt);
5919 	vsi->info.valid_sections = 0;
5920 
5921 	/* Update current VSI BW information */
5922 	ret = i40e_vsi_get_bw_info(vsi);
5923 	if (ret) {
5924 		dev_info(&pf->pdev->dev,
5925 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5926 			 ERR_PTR(ret),
5927 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5928 		goto out;
5929 	}
5930 
5931 	/* Update the netdev TC setup */
5932 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5933 out:
5934 	return ret;
5935 }
5936 
5937 /**
5938  * i40e_get_link_speed - Returns link speed for the interface
5939  * @vsi: VSI to be configured
5940  *
5941  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5942 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5943 {
5944 	struct i40e_pf *pf = vsi->back;
5945 
5946 	switch (pf->hw.phy.link_info.link_speed) {
5947 	case I40E_LINK_SPEED_40GB:
5948 		return 40000;
5949 	case I40E_LINK_SPEED_25GB:
5950 		return 25000;
5951 	case I40E_LINK_SPEED_20GB:
5952 		return 20000;
5953 	case I40E_LINK_SPEED_10GB:
5954 		return 10000;
5955 	case I40E_LINK_SPEED_1GB:
5956 		return 1000;
5957 	default:
5958 		return -EINVAL;
5959 	}
5960 }
5961 
5962 /**
5963  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5964  * @vsi: Pointer to vsi structure
5965  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5966  *
5967  * Helper function to convert units before send to set BW limit
5968  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5969 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5970 {
5971 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5972 		dev_warn(&vsi->back->pdev->dev,
5973 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5974 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5975 	} else {
5976 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5977 	}
5978 
5979 	return max_tx_rate;
5980 }
5981 
5982 /**
5983  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5984  * @vsi: VSI to be configured
5985  * @seid: seid of the channel/VSI
5986  * @max_tx_rate: max TX rate to be configured as BW limit
5987  *
5988  * Helper function to set BW limit for a given VSI
5989  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5990 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5991 {
5992 	struct i40e_pf *pf = vsi->back;
5993 	u64 credits = 0;
5994 	int speed = 0;
5995 	int ret = 0;
5996 
5997 	speed = i40e_get_link_speed(vsi);
5998 	if (max_tx_rate > speed) {
5999 		dev_err(&pf->pdev->dev,
6000 			"Invalid max tx rate %llu specified for VSI seid %d.",
6001 			max_tx_rate, seid);
6002 		return -EINVAL;
6003 	}
6004 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
6005 		dev_warn(&pf->pdev->dev,
6006 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
6007 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
6008 	}
6009 
6010 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
6011 	credits = max_tx_rate;
6012 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
6013 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
6014 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6015 	if (ret)
6016 		dev_err(&pf->pdev->dev,
6017 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6018 			max_tx_rate, seid, ERR_PTR(ret),
6019 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6020 	return ret;
6021 }
6022 
6023 /**
6024  * i40e_remove_queue_channels - Remove queue channels for the TCs
6025  * @vsi: VSI to be configured
6026  *
6027  * Remove queue channels for the TCs
6028  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)6029 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6030 {
6031 	enum i40e_admin_queue_err last_aq_status;
6032 	struct i40e_cloud_filter *cfilter;
6033 	struct i40e_channel *ch, *ch_tmp;
6034 	struct i40e_pf *pf = vsi->back;
6035 	struct hlist_node *node;
6036 	int ret, i;
6037 
6038 	/* Reset rss size that was stored when reconfiguring rss for
6039 	 * channel VSIs with non-power-of-2 queue count.
6040 	 */
6041 	vsi->current_rss_size = 0;
6042 
6043 	/* perform cleanup for channels if they exist */
6044 	if (list_empty(&vsi->ch_list))
6045 		return;
6046 
6047 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6048 		struct i40e_vsi *p_vsi;
6049 
6050 		list_del(&ch->list);
6051 		p_vsi = ch->parent_vsi;
6052 		if (!p_vsi || !ch->initialized) {
6053 			kfree(ch);
6054 			continue;
6055 		}
6056 		/* Reset queue contexts */
6057 		for (i = 0; i < ch->num_queue_pairs; i++) {
6058 			struct i40e_ring *tx_ring, *rx_ring;
6059 			u16 pf_q;
6060 
6061 			pf_q = ch->base_queue + i;
6062 			tx_ring = vsi->tx_rings[pf_q];
6063 			tx_ring->ch = NULL;
6064 
6065 			rx_ring = vsi->rx_rings[pf_q];
6066 			rx_ring->ch = NULL;
6067 		}
6068 
6069 		/* Reset BW configured for this VSI via mqprio */
6070 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6071 		if (ret)
6072 			dev_info(&vsi->back->pdev->dev,
6073 				 "Failed to reset tx rate for ch->seid %u\n",
6074 				 ch->seid);
6075 
6076 		/* delete cloud filters associated with this channel */
6077 		hlist_for_each_entry_safe(cfilter, node,
6078 					  &pf->cloud_filter_list, cloud_node) {
6079 			if (cfilter->seid != ch->seid)
6080 				continue;
6081 
6082 			hash_del(&cfilter->cloud_node);
6083 			if (cfilter->dst_port)
6084 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6085 									cfilter,
6086 									false);
6087 			else
6088 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6089 								false);
6090 			last_aq_status = pf->hw.aq.asq_last_status;
6091 			if (ret)
6092 				dev_info(&pf->pdev->dev,
6093 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6094 					 ERR_PTR(ret),
6095 					 i40e_aq_str(&pf->hw, last_aq_status));
6096 			kfree(cfilter);
6097 		}
6098 
6099 		/* delete VSI from FW */
6100 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6101 					     NULL);
6102 		if (ret)
6103 			dev_err(&vsi->back->pdev->dev,
6104 				"unable to remove channel (%d) for parent VSI(%d)\n",
6105 				ch->seid, p_vsi->seid);
6106 		kfree(ch);
6107 	}
6108 	INIT_LIST_HEAD(&vsi->ch_list);
6109 }
6110 
6111 /**
6112  * i40e_get_max_queues_for_channel
6113  * @vsi: ptr to VSI to which channels are associated with
6114  *
6115  * Helper function which returns max value among the queue counts set on the
6116  * channels/TCs created.
6117  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6118 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6119 {
6120 	struct i40e_channel *ch, *ch_tmp;
6121 	int max = 0;
6122 
6123 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6124 		if (!ch->initialized)
6125 			continue;
6126 		if (ch->num_queue_pairs > max)
6127 			max = ch->num_queue_pairs;
6128 	}
6129 
6130 	return max;
6131 }
6132 
6133 /**
6134  * i40e_validate_num_queues - validate num_queues w.r.t channel
6135  * @pf: ptr to PF device
6136  * @num_queues: number of queues
6137  * @vsi: the parent VSI
6138  * @reconfig_rss: indicates should the RSS be reconfigured or not
6139  *
6140  * This function validates number of queues in the context of new channel
6141  * which is being established and determines if RSS should be reconfigured
6142  * or not for parent VSI.
6143  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6144 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6145 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6146 {
6147 	int max_ch_queues;
6148 
6149 	if (!reconfig_rss)
6150 		return -EINVAL;
6151 
6152 	*reconfig_rss = false;
6153 	if (vsi->current_rss_size) {
6154 		if (num_queues > vsi->current_rss_size) {
6155 			dev_dbg(&pf->pdev->dev,
6156 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6157 				num_queues, vsi->current_rss_size);
6158 			return -EINVAL;
6159 		} else if ((num_queues < vsi->current_rss_size) &&
6160 			   (!is_power_of_2(num_queues))) {
6161 			dev_dbg(&pf->pdev->dev,
6162 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6163 				num_queues, vsi->current_rss_size);
6164 			return -EINVAL;
6165 		}
6166 	}
6167 
6168 	if (!is_power_of_2(num_queues)) {
6169 		/* Find the max num_queues configured for channel if channel
6170 		 * exist.
6171 		 * if channel exist, then enforce 'num_queues' to be more than
6172 		 * max ever queues configured for channel.
6173 		 */
6174 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6175 		if (num_queues < max_ch_queues) {
6176 			dev_dbg(&pf->pdev->dev,
6177 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6178 				num_queues, max_ch_queues);
6179 			return -EINVAL;
6180 		}
6181 		*reconfig_rss = true;
6182 	}
6183 
6184 	return 0;
6185 }
6186 
6187 /**
6188  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6189  * @vsi: the VSI being setup
6190  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6191  *
6192  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6193  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6194 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6195 {
6196 	struct i40e_pf *pf = vsi->back;
6197 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6198 	struct i40e_hw *hw = &pf->hw;
6199 	int local_rss_size;
6200 	u8 *lut;
6201 	int ret;
6202 
6203 	if (!vsi->rss_size)
6204 		return -EINVAL;
6205 
6206 	if (rss_size > vsi->rss_size)
6207 		return -EINVAL;
6208 
6209 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6210 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6211 	if (!lut)
6212 		return -ENOMEM;
6213 
6214 	/* Ignoring user configured lut if there is one */
6215 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6216 
6217 	/* Use user configured hash key if there is one, otherwise
6218 	 * use default.
6219 	 */
6220 	if (vsi->rss_hkey_user)
6221 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6222 	else
6223 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6224 
6225 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6226 	if (ret) {
6227 		dev_info(&pf->pdev->dev,
6228 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6229 			 ERR_PTR(ret),
6230 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6231 		kfree(lut);
6232 		return ret;
6233 	}
6234 	kfree(lut);
6235 
6236 	/* Do the update w.r.t. storing rss_size */
6237 	if (!vsi->orig_rss_size)
6238 		vsi->orig_rss_size = vsi->rss_size;
6239 	vsi->current_rss_size = local_rss_size;
6240 
6241 	return ret;
6242 }
6243 
6244 /**
6245  * i40e_channel_setup_queue_map - Setup a channel queue map
6246  * @pf: ptr to PF device
6247  * @ctxt: VSI context structure
6248  * @ch: ptr to channel structure
6249  *
6250  * Setup queue map for a specific channel
6251  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6252 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6253 					 struct i40e_vsi_context *ctxt,
6254 					 struct i40e_channel *ch)
6255 {
6256 	u16 qcount, qmap, sections = 0;
6257 	u8 offset = 0;
6258 	int pow;
6259 
6260 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6261 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6262 
6263 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6264 	ch->num_queue_pairs = qcount;
6265 
6266 	/* find the next higher power-of-2 of num queue pairs */
6267 	pow = ilog2(qcount);
6268 	if (!is_power_of_2(qcount))
6269 		pow++;
6270 
6271 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6272 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6273 
6274 	/* Setup queue TC[0].qmap for given VSI context */
6275 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6276 
6277 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6278 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6279 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6280 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6281 }
6282 
6283 /**
6284  * i40e_add_channel - add a channel by adding VSI
6285  * @pf: ptr to PF device
6286  * @uplink_seid: underlying HW switching element (VEB) ID
6287  * @ch: ptr to channel structure
6288  *
6289  * Add a channel (VSI) using add_vsi and queue_map
6290  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6291 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6292 			    struct i40e_channel *ch)
6293 {
6294 	struct i40e_hw *hw = &pf->hw;
6295 	struct i40e_vsi_context ctxt;
6296 	u8 enabled_tc = 0x1; /* TC0 enabled */
6297 	int ret;
6298 
6299 	if (ch->type != I40E_VSI_VMDQ2) {
6300 		dev_info(&pf->pdev->dev,
6301 			 "add new vsi failed, ch->type %d\n", ch->type);
6302 		return -EINVAL;
6303 	}
6304 
6305 	memset(&ctxt, 0, sizeof(ctxt));
6306 	ctxt.pf_num = hw->pf_id;
6307 	ctxt.vf_num = 0;
6308 	ctxt.uplink_seid = uplink_seid;
6309 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6310 	if (ch->type == I40E_VSI_VMDQ2)
6311 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6312 
6313 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6314 		ctxt.info.valid_sections |=
6315 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6316 		ctxt.info.switch_id =
6317 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6318 	}
6319 
6320 	/* Set queue map for a given VSI context */
6321 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6322 
6323 	/* Now time to create VSI */
6324 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6325 	if (ret) {
6326 		dev_info(&pf->pdev->dev,
6327 			 "add new vsi failed, err %pe aq_err %s\n",
6328 			 ERR_PTR(ret),
6329 			 i40e_aq_str(&pf->hw,
6330 				     pf->hw.aq.asq_last_status));
6331 		return -ENOENT;
6332 	}
6333 
6334 	/* Success, update channel, set enabled_tc only if the channel
6335 	 * is not a macvlan
6336 	 */
6337 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6338 	ch->seid = ctxt.seid;
6339 	ch->vsi_number = ctxt.vsi_number;
6340 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6341 
6342 	/* copy just the sections touched not the entire info
6343 	 * since not all sections are valid as returned by
6344 	 * update vsi params
6345 	 */
6346 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6347 	memcpy(&ch->info.queue_mapping,
6348 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6349 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6350 	       sizeof(ctxt.info.tc_mapping));
6351 
6352 	return 0;
6353 }
6354 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6355 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6356 				  u8 *bw_share)
6357 {
6358 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6359 	int ret;
6360 	int i;
6361 
6362 	memset(&bw_data, 0, sizeof(bw_data));
6363 	bw_data.tc_valid_bits = ch->enabled_tc;
6364 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6365 		bw_data.tc_bw_credits[i] = bw_share[i];
6366 
6367 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6368 				       &bw_data, NULL);
6369 	if (ret) {
6370 		dev_info(&vsi->back->pdev->dev,
6371 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6372 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6373 		return -EINVAL;
6374 	}
6375 
6376 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6377 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6378 
6379 	return 0;
6380 }
6381 
6382 /**
6383  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6384  * @pf: ptr to PF device
6385  * @vsi: the VSI being setup
6386  * @ch: ptr to channel structure
6387  *
6388  * Configure TX rings associated with channel (VSI) since queues are being
6389  * from parent VSI.
6390  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6391 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6392 				       struct i40e_vsi *vsi,
6393 				       struct i40e_channel *ch)
6394 {
6395 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6396 	int ret;
6397 	int i;
6398 
6399 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6400 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6401 		if (ch->enabled_tc & BIT(i))
6402 			bw_share[i] = 1;
6403 	}
6404 
6405 	/* configure BW for new VSI */
6406 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6407 	if (ret) {
6408 		dev_info(&vsi->back->pdev->dev,
6409 			 "Failed configuring TC map %d for channel (seid %u)\n",
6410 			 ch->enabled_tc, ch->seid);
6411 		return ret;
6412 	}
6413 
6414 	for (i = 0; i < ch->num_queue_pairs; i++) {
6415 		struct i40e_ring *tx_ring, *rx_ring;
6416 		u16 pf_q;
6417 
6418 		pf_q = ch->base_queue + i;
6419 
6420 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6421 		 * context
6422 		 */
6423 		tx_ring = vsi->tx_rings[pf_q];
6424 		tx_ring->ch = ch;
6425 
6426 		/* Get the RX ring ptr */
6427 		rx_ring = vsi->rx_rings[pf_q];
6428 		rx_ring->ch = ch;
6429 	}
6430 
6431 	return 0;
6432 }
6433 
6434 /**
6435  * i40e_setup_hw_channel - setup new channel
6436  * @pf: ptr to PF device
6437  * @vsi: the VSI being setup
6438  * @ch: ptr to channel structure
6439  * @uplink_seid: underlying HW switching element (VEB) ID
6440  * @type: type of channel to be created (VMDq2/VF)
6441  *
6442  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6443  * and configures TX rings accordingly
6444  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6445 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6446 					struct i40e_vsi *vsi,
6447 					struct i40e_channel *ch,
6448 					u16 uplink_seid, u8 type)
6449 {
6450 	int ret;
6451 
6452 	ch->initialized = false;
6453 	ch->base_queue = vsi->next_base_queue;
6454 	ch->type = type;
6455 
6456 	/* Proceed with creation of channel (VMDq2) VSI */
6457 	ret = i40e_add_channel(pf, uplink_seid, ch);
6458 	if (ret) {
6459 		dev_info(&pf->pdev->dev,
6460 			 "failed to add_channel using uplink_seid %u\n",
6461 			 uplink_seid);
6462 		return ret;
6463 	}
6464 
6465 	/* Mark the successful creation of channel */
6466 	ch->initialized = true;
6467 
6468 	/* Reconfigure TX queues using QTX_CTL register */
6469 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6470 	if (ret) {
6471 		dev_info(&pf->pdev->dev,
6472 			 "failed to configure TX rings for channel %u\n",
6473 			 ch->seid);
6474 		return ret;
6475 	}
6476 
6477 	/* update 'next_base_queue' */
6478 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6479 	dev_dbg(&pf->pdev->dev,
6480 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6481 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6482 		ch->num_queue_pairs,
6483 		vsi->next_base_queue);
6484 	return ret;
6485 }
6486 
6487 /**
6488  * i40e_setup_channel - setup new channel using uplink element
6489  * @pf: ptr to PF device
6490  * @vsi: pointer to the VSI to set up the channel within
6491  * @ch: ptr to channel structure
6492  *
6493  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6494  * and uplink switching element (uplink_seid)
6495  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6496 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6497 			       struct i40e_channel *ch)
6498 {
6499 	u8 vsi_type;
6500 	u16 seid;
6501 	int ret;
6502 
6503 	if (vsi->type == I40E_VSI_MAIN) {
6504 		vsi_type = I40E_VSI_VMDQ2;
6505 	} else {
6506 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6507 			vsi->type);
6508 		return false;
6509 	}
6510 
6511 	/* underlying switching element */
6512 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6513 
6514 	/* create channel (VSI), configure TX rings */
6515 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6516 	if (ret) {
6517 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6518 		return false;
6519 	}
6520 
6521 	return ch->initialized ? true : false;
6522 }
6523 
6524 /**
6525  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6526  * @vsi: ptr to VSI which has PF backing
6527  *
6528  * Sets up switch mode correctly if it needs to be changed and perform
6529  * what are allowed modes.
6530  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6531 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6532 {
6533 	u8 mode;
6534 	struct i40e_pf *pf = vsi->back;
6535 	struct i40e_hw *hw = &pf->hw;
6536 	int ret;
6537 
6538 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6539 	if (ret)
6540 		return -EINVAL;
6541 
6542 	if (hw->dev_caps.switch_mode) {
6543 		/* if switch mode is set, support mode2 (non-tunneled for
6544 		 * cloud filter) for now
6545 		 */
6546 		u32 switch_mode = hw->dev_caps.switch_mode &
6547 				  I40E_SWITCH_MODE_MASK;
6548 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6549 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6550 				return 0;
6551 			dev_err(&pf->pdev->dev,
6552 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6553 				hw->dev_caps.switch_mode);
6554 			return -EINVAL;
6555 		}
6556 	}
6557 
6558 	/* Set Bit 7 to be valid */
6559 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6560 
6561 	/* Set L4type for TCP support */
6562 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6563 
6564 	/* Set cloud filter mode */
6565 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6566 
6567 	/* Prep mode field for set_switch_config */
6568 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6569 					pf->last_sw_conf_valid_flags,
6570 					mode, NULL);
6571 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6572 		dev_err(&pf->pdev->dev,
6573 			"couldn't set switch config bits, err %pe aq_err %s\n",
6574 			ERR_PTR(ret),
6575 			i40e_aq_str(hw,
6576 				    hw->aq.asq_last_status));
6577 
6578 	return ret;
6579 }
6580 
6581 /**
6582  * i40e_create_queue_channel - function to create channel
6583  * @vsi: VSI to be configured
6584  * @ch: ptr to channel (it contains channel specific params)
6585  *
6586  * This function creates channel (VSI) using num_queues specified by user,
6587  * reconfigs RSS if needed.
6588  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6589 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6590 			      struct i40e_channel *ch)
6591 {
6592 	struct i40e_pf *pf = vsi->back;
6593 	bool reconfig_rss;
6594 	int err;
6595 
6596 	if (!ch)
6597 		return -EINVAL;
6598 
6599 	if (!ch->num_queue_pairs) {
6600 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6601 			ch->num_queue_pairs);
6602 		return -EINVAL;
6603 	}
6604 
6605 	/* validate user requested num_queues for channel */
6606 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6607 				       &reconfig_rss);
6608 	if (err) {
6609 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6610 			 ch->num_queue_pairs);
6611 		return -EINVAL;
6612 	}
6613 
6614 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6615 	 * VSI to be added switch to VEB mode.
6616 	 */
6617 
6618 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6619 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6620 
6621 		if (vsi->type == I40E_VSI_MAIN) {
6622 			if (i40e_is_tc_mqprio_enabled(pf))
6623 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6624 			else
6625 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6626 		}
6627 		/* now onwards for main VSI, number of queues will be value
6628 		 * of TC0's queue count
6629 		 */
6630 	}
6631 
6632 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6633 	 * it should be more than num_queues
6634 	 */
6635 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6636 		dev_dbg(&pf->pdev->dev,
6637 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6638 			vsi->cnt_q_avail, ch->num_queue_pairs);
6639 		return -EINVAL;
6640 	}
6641 
6642 	/* reconfig_rss only if vsi type is MAIN_VSI */
6643 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6644 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6645 		if (err) {
6646 			dev_info(&pf->pdev->dev,
6647 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6648 				 ch->num_queue_pairs);
6649 			return -EINVAL;
6650 		}
6651 	}
6652 
6653 	if (!i40e_setup_channel(pf, vsi, ch)) {
6654 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6655 		return -EINVAL;
6656 	}
6657 
6658 	dev_info(&pf->pdev->dev,
6659 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6660 		 ch->seid, ch->num_queue_pairs);
6661 
6662 	/* configure VSI for BW limit */
6663 	if (ch->max_tx_rate) {
6664 		u64 credits = ch->max_tx_rate;
6665 
6666 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6667 			return -EINVAL;
6668 
6669 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6670 		dev_dbg(&pf->pdev->dev,
6671 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6672 			ch->max_tx_rate,
6673 			credits,
6674 			ch->seid);
6675 	}
6676 
6677 	/* in case of VF, this will be main SRIOV VSI */
6678 	ch->parent_vsi = vsi;
6679 
6680 	/* and update main_vsi's count for queue_available to use */
6681 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6682 
6683 	return 0;
6684 }
6685 
6686 /**
6687  * i40e_configure_queue_channels - Add queue channel for the given TCs
6688  * @vsi: VSI to be configured
6689  *
6690  * Configures queue channel mapping to the given TCs
6691  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6692 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6693 {
6694 	struct i40e_channel *ch;
6695 	u64 max_rate = 0;
6696 	int ret = 0, i;
6697 
6698 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6699 	vsi->tc_seid_map[0] = vsi->seid;
6700 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6701 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6702 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6703 			if (!ch) {
6704 				ret = -ENOMEM;
6705 				goto err_free;
6706 			}
6707 
6708 			INIT_LIST_HEAD(&ch->list);
6709 			ch->num_queue_pairs =
6710 				vsi->tc_config.tc_info[i].qcount;
6711 			ch->base_queue =
6712 				vsi->tc_config.tc_info[i].qoffset;
6713 
6714 			/* Bandwidth limit through tc interface is in bytes/s,
6715 			 * change to Mbit/s
6716 			 */
6717 			max_rate = vsi->mqprio_qopt.max_rate[i];
6718 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6719 			ch->max_tx_rate = max_rate;
6720 
6721 			list_add_tail(&ch->list, &vsi->ch_list);
6722 
6723 			ret = i40e_create_queue_channel(vsi, ch);
6724 			if (ret) {
6725 				dev_err(&vsi->back->pdev->dev,
6726 					"Failed creating queue channel with TC%d: queues %d\n",
6727 					i, ch->num_queue_pairs);
6728 				goto err_free;
6729 			}
6730 			vsi->tc_seid_map[i] = ch->seid;
6731 		}
6732 	}
6733 
6734 	/* reset to reconfigure TX queue contexts */
6735 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6736 	return ret;
6737 
6738 err_free:
6739 	i40e_remove_queue_channels(vsi);
6740 	return ret;
6741 }
6742 
6743 /**
6744  * i40e_veb_config_tc - Configure TCs for given VEB
6745  * @veb: given VEB
6746  * @enabled_tc: TC bitmap
6747  *
6748  * Configures given TC bitmap for VEB (switching) element
6749  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6750 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6751 {
6752 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6753 	struct i40e_pf *pf = veb->pf;
6754 	int ret = 0;
6755 	int i;
6756 
6757 	/* No TCs or already enabled TCs just return */
6758 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6759 		return ret;
6760 
6761 	bw_data.tc_valid_bits = enabled_tc;
6762 	/* bw_data.absolute_credits is not set (relative) */
6763 
6764 	/* Enable ETS TCs with equal BW Share for now */
6765 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6766 		if (enabled_tc & BIT(i))
6767 			bw_data.tc_bw_share_credits[i] = 1;
6768 	}
6769 
6770 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6771 						   &bw_data, NULL);
6772 	if (ret) {
6773 		dev_info(&pf->pdev->dev,
6774 			 "VEB bw config failed, err %pe aq_err %s\n",
6775 			 ERR_PTR(ret),
6776 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6777 		goto out;
6778 	}
6779 
6780 	/* Update the BW information */
6781 	ret = i40e_veb_get_bw_info(veb);
6782 	if (ret) {
6783 		dev_info(&pf->pdev->dev,
6784 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6785 			 ERR_PTR(ret),
6786 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6787 	}
6788 
6789 out:
6790 	return ret;
6791 }
6792 
6793 #ifdef CONFIG_I40E_DCB
6794 /**
6795  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6796  * @pf: PF struct
6797  *
6798  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6799  * the caller would've quiesce all the VSIs before calling
6800  * this function
6801  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6802 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6803 {
6804 	u8 tc_map = 0;
6805 	int ret;
6806 	u8 v;
6807 
6808 	/* Enable the TCs available on PF to all VEBs */
6809 	tc_map = i40e_pf_get_tc_map(pf);
6810 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6811 		return;
6812 
6813 	for (v = 0; v < I40E_MAX_VEB; v++) {
6814 		if (!pf->veb[v])
6815 			continue;
6816 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6817 		if (ret) {
6818 			dev_info(&pf->pdev->dev,
6819 				 "Failed configuring TC for VEB seid=%d\n",
6820 				 pf->veb[v]->seid);
6821 			/* Will try to configure as many components */
6822 		}
6823 	}
6824 
6825 	/* Update each VSI */
6826 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6827 		if (!pf->vsi[v])
6828 			continue;
6829 
6830 		/* - Enable all TCs for the LAN VSI
6831 		 * - For all others keep them at TC0 for now
6832 		 */
6833 		if (v == pf->lan_vsi)
6834 			tc_map = i40e_pf_get_tc_map(pf);
6835 		else
6836 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6837 
6838 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6839 		if (ret) {
6840 			dev_info(&pf->pdev->dev,
6841 				 "Failed configuring TC for VSI seid=%d\n",
6842 				 pf->vsi[v]->seid);
6843 			/* Will try to configure as many components */
6844 		} else {
6845 			/* Re-configure VSI vectors based on updated TC map */
6846 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6847 			if (pf->vsi[v]->netdev)
6848 				i40e_dcbnl_set_all(pf->vsi[v]);
6849 		}
6850 	}
6851 }
6852 
6853 /**
6854  * i40e_resume_port_tx - Resume port Tx
6855  * @pf: PF struct
6856  *
6857  * Resume a port's Tx and issue a PF reset in case of failure to
6858  * resume.
6859  **/
i40e_resume_port_tx(struct i40e_pf * pf)6860 static int i40e_resume_port_tx(struct i40e_pf *pf)
6861 {
6862 	struct i40e_hw *hw = &pf->hw;
6863 	int ret;
6864 
6865 	ret = i40e_aq_resume_port_tx(hw, NULL);
6866 	if (ret) {
6867 		dev_info(&pf->pdev->dev,
6868 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6869 			  ERR_PTR(ret),
6870 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6871 		/* Schedule PF reset to recover */
6872 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6873 		i40e_service_event_schedule(pf);
6874 	}
6875 
6876 	return ret;
6877 }
6878 
6879 /**
6880  * i40e_suspend_port_tx - Suspend port Tx
6881  * @pf: PF struct
6882  *
6883  * Suspend a port's Tx and issue a PF reset in case of failure.
6884  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6885 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6886 {
6887 	struct i40e_hw *hw = &pf->hw;
6888 	int ret;
6889 
6890 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6891 	if (ret) {
6892 		dev_info(&pf->pdev->dev,
6893 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6894 			 ERR_PTR(ret),
6895 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6896 		/* Schedule PF reset to recover */
6897 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6898 		i40e_service_event_schedule(pf);
6899 	}
6900 
6901 	return ret;
6902 }
6903 
6904 /**
6905  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6906  * @pf: PF being configured
6907  * @new_cfg: New DCBX configuration
6908  *
6909  * Program DCB settings into HW and reconfigure VEB/VSIs on
6910  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6911  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6912 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6913 				  struct i40e_dcbx_config *new_cfg)
6914 {
6915 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6916 	int ret;
6917 
6918 	/* Check if need reconfiguration */
6919 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6920 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6921 		return 0;
6922 	}
6923 
6924 	/* Config change disable all VSIs */
6925 	i40e_pf_quiesce_all_vsi(pf);
6926 
6927 	/* Copy the new config to the current config */
6928 	*old_cfg = *new_cfg;
6929 	old_cfg->etsrec = old_cfg->etscfg;
6930 	ret = i40e_set_dcb_config(&pf->hw);
6931 	if (ret) {
6932 		dev_info(&pf->pdev->dev,
6933 			 "Set DCB Config failed, err %pe aq_err %s\n",
6934 			 ERR_PTR(ret),
6935 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6936 		goto out;
6937 	}
6938 
6939 	/* Changes in configuration update VEB/VSI */
6940 	i40e_dcb_reconfigure(pf);
6941 out:
6942 	/* In case of reset do not try to resume anything */
6943 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6944 		/* Re-start the VSIs if disabled */
6945 		ret = i40e_resume_port_tx(pf);
6946 		/* In case of error no point in resuming VSIs */
6947 		if (ret)
6948 			goto err;
6949 		i40e_pf_unquiesce_all_vsi(pf);
6950 	}
6951 err:
6952 	return ret;
6953 }
6954 
6955 /**
6956  * i40e_hw_dcb_config - Program new DCBX settings into HW
6957  * @pf: PF being configured
6958  * @new_cfg: New DCBX configuration
6959  *
6960  * Program DCB settings into HW and reconfigure VEB/VSIs on
6961  * given PF
6962  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6963 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6964 {
6965 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6966 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6967 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6968 	struct i40e_dcbx_config *old_cfg;
6969 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6970 	struct i40e_rx_pb_config pb_cfg;
6971 	struct i40e_hw *hw = &pf->hw;
6972 	u8 num_ports = hw->num_ports;
6973 	bool need_reconfig;
6974 	int ret = -EINVAL;
6975 	u8 lltc_map = 0;
6976 	u8 tc_map = 0;
6977 	u8 new_numtc;
6978 	u8 i;
6979 
6980 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6981 	/* Un-pack information to Program ETS HW via shared API
6982 	 * numtc, tcmap
6983 	 * LLTC map
6984 	 * ETS/NON-ETS arbiter mode
6985 	 * max exponent (credit refills)
6986 	 * Total number of ports
6987 	 * PFC priority bit-map
6988 	 * Priority Table
6989 	 * BW % per TC
6990 	 * Arbiter mode between UPs sharing same TC
6991 	 * TSA table (ETS or non-ETS)
6992 	 * EEE enabled or not
6993 	 * MFS TC table
6994 	 */
6995 
6996 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6997 
6998 	memset(&ets_data, 0, sizeof(ets_data));
6999 	for (i = 0; i < new_numtc; i++) {
7000 		tc_map |= BIT(i);
7001 		switch (new_cfg->etscfg.tsatable[i]) {
7002 		case I40E_IEEE_TSA_ETS:
7003 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
7004 			ets_data.tc_bw_share_credits[i] =
7005 					new_cfg->etscfg.tcbwtable[i];
7006 			break;
7007 		case I40E_IEEE_TSA_STRICT:
7008 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
7009 			lltc_map |= BIT(i);
7010 			ets_data.tc_bw_share_credits[i] =
7011 					I40E_DCB_STRICT_PRIO_CREDITS;
7012 			break;
7013 		default:
7014 			/* Invalid TSA type */
7015 			need_reconfig = false;
7016 			goto out;
7017 		}
7018 	}
7019 
7020 	old_cfg = &hw->local_dcbx_config;
7021 	/* Check if need reconfiguration */
7022 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7023 
7024 	/* If needed, enable/disable frame tagging, disable all VSIs
7025 	 * and suspend port tx
7026 	 */
7027 	if (need_reconfig) {
7028 		/* Enable DCB tagging only when more than one TC */
7029 		if (new_numtc > 1)
7030 			pf->flags |= I40E_FLAG_DCB_ENABLED;
7031 		else
7032 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7033 
7034 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7035 		/* Reconfiguration needed quiesce all VSIs */
7036 		i40e_pf_quiesce_all_vsi(pf);
7037 		ret = i40e_suspend_port_tx(pf);
7038 		if (ret)
7039 			goto err;
7040 	}
7041 
7042 	/* Configure Port ETS Tx Scheduler */
7043 	ets_data.tc_valid_bits = tc_map;
7044 	ets_data.tc_strict_priority_flags = lltc_map;
7045 	ret = i40e_aq_config_switch_comp_ets
7046 		(hw, pf->mac_seid, &ets_data,
7047 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7048 	if (ret) {
7049 		dev_info(&pf->pdev->dev,
7050 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7051 			 ERR_PTR(ret),
7052 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7053 		goto out;
7054 	}
7055 
7056 	/* Configure Rx ETS HW */
7057 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7058 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7059 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7060 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7061 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7062 				   lltc_map);
7063 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7064 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7065 				     prio_type);
7066 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7067 			       new_cfg->etscfg.prioritytable);
7068 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7069 
7070 	/* Configure Rx Packet Buffers in HW */
7071 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7072 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7073 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7074 	}
7075 
7076 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7077 					 false, new_cfg->pfc.pfcenable,
7078 					 mfs_tc, &pb_cfg);
7079 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7080 
7081 	/* Update the local Rx Packet buffer config */
7082 	pf->pb_cfg = pb_cfg;
7083 
7084 	/* Inform the FW about changes to DCB configuration */
7085 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7086 	if (ret) {
7087 		dev_info(&pf->pdev->dev,
7088 			 "DCB Updated failed, err %pe aq_err %s\n",
7089 			 ERR_PTR(ret),
7090 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7091 		goto out;
7092 	}
7093 
7094 	/* Update the port DCBx configuration */
7095 	*old_cfg = *new_cfg;
7096 
7097 	/* Changes in configuration update VEB/VSI */
7098 	i40e_dcb_reconfigure(pf);
7099 out:
7100 	/* Re-start the VSIs if disabled */
7101 	if (need_reconfig) {
7102 		ret = i40e_resume_port_tx(pf);
7103 
7104 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7105 		/* In case of error no point in resuming VSIs */
7106 		if (ret)
7107 			goto err;
7108 
7109 		/* Wait for the PF's queues to be disabled */
7110 		ret = i40e_pf_wait_queues_disabled(pf);
7111 		if (ret) {
7112 			/* Schedule PF reset to recover */
7113 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7114 			i40e_service_event_schedule(pf);
7115 			goto err;
7116 		} else {
7117 			i40e_pf_unquiesce_all_vsi(pf);
7118 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7119 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7120 		}
7121 		/* registers are set, lets apply */
7122 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7123 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7124 	}
7125 
7126 err:
7127 	return ret;
7128 }
7129 
7130 /**
7131  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7132  * @pf: PF being queried
7133  *
7134  * Set default DCB configuration in case DCB is to be done in SW.
7135  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7136 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7137 {
7138 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7139 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7140 	struct i40e_hw *hw = &pf->hw;
7141 	int err;
7142 
7143 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7144 		/* Update the local cached instance with TC0 ETS */
7145 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7146 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7147 		pf->tmp_cfg.etscfg.maxtcs = 0;
7148 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7149 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7150 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7151 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7152 		/* FW needs one App to configure HW */
7153 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7154 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7155 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7156 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7157 
7158 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7159 	}
7160 
7161 	memset(&ets_data, 0, sizeof(ets_data));
7162 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7163 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7164 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7165 
7166 	/* Enable ETS on the Physical port */
7167 	err = i40e_aq_config_switch_comp_ets
7168 		(hw, pf->mac_seid, &ets_data,
7169 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7170 	if (err) {
7171 		dev_info(&pf->pdev->dev,
7172 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7173 			 ERR_PTR(err),
7174 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7175 		err = -ENOENT;
7176 		goto out;
7177 	}
7178 
7179 	/* Update the local cached instance with TC0 ETS */
7180 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7181 	dcb_cfg->etscfg.cbs = 0;
7182 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7183 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7184 
7185 out:
7186 	return err;
7187 }
7188 
7189 /**
7190  * i40e_init_pf_dcb - Initialize DCB configuration
7191  * @pf: PF being configured
7192  *
7193  * Query the current DCB configuration and cache it
7194  * in the hardware structure
7195  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7196 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7197 {
7198 	struct i40e_hw *hw = &pf->hw;
7199 	int err;
7200 
7201 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7202 	 * Also do not enable DCBx if FW LLDP agent is disabled
7203 	 */
7204 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7205 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7206 		err = -EOPNOTSUPP;
7207 		goto out;
7208 	}
7209 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7210 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7211 		err = i40e_dcb_sw_default_config(pf);
7212 		if (err) {
7213 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7214 			goto out;
7215 		}
7216 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7217 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7218 			       DCB_CAP_DCBX_VER_IEEE;
7219 		/* at init capable but disabled */
7220 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7221 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7222 		goto out;
7223 	}
7224 	err = i40e_init_dcb(hw, true);
7225 	if (!err) {
7226 		/* Device/Function is not DCBX capable */
7227 		if ((!hw->func_caps.dcb) ||
7228 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7229 			dev_info(&pf->pdev->dev,
7230 				 "DCBX offload is not supported or is disabled for this PF.\n");
7231 		} else {
7232 			/* When status is not DISABLED then DCBX in FW */
7233 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7234 				       DCB_CAP_DCBX_VER_IEEE;
7235 
7236 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7237 			/* Enable DCB tagging only when more than one TC
7238 			 * or explicitly disable if only one TC
7239 			 */
7240 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7241 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7242 			else
7243 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7244 			dev_dbg(&pf->pdev->dev,
7245 				"DCBX offload is supported for this PF.\n");
7246 		}
7247 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7248 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7249 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7250 	} else {
7251 		dev_info(&pf->pdev->dev,
7252 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7253 			 ERR_PTR(err),
7254 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7255 	}
7256 
7257 out:
7258 	return err;
7259 }
7260 #endif /* CONFIG_I40E_DCB */
7261 
7262 /**
7263  * i40e_print_link_message - print link up or down
7264  * @vsi: the VSI for which link needs a message
7265  * @isup: true of link is up, false otherwise
7266  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7267 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7268 {
7269 	enum i40e_aq_link_speed new_speed;
7270 	struct i40e_pf *pf = vsi->back;
7271 	char *speed = "Unknown";
7272 	char *fc = "Unknown";
7273 	char *fec = "";
7274 	char *req_fec = "";
7275 	char *an = "";
7276 
7277 	if (isup)
7278 		new_speed = pf->hw.phy.link_info.link_speed;
7279 	else
7280 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7281 
7282 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7283 		return;
7284 	vsi->current_isup = isup;
7285 	vsi->current_speed = new_speed;
7286 	if (!isup) {
7287 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7288 		return;
7289 	}
7290 
7291 	/* Warn user if link speed on NPAR enabled partition is not at
7292 	 * least 10GB
7293 	 */
7294 	if (pf->hw.func_caps.npar_enable &&
7295 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7296 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7297 		netdev_warn(vsi->netdev,
7298 			    "The partition detected link speed that is less than 10Gbps\n");
7299 
7300 	switch (pf->hw.phy.link_info.link_speed) {
7301 	case I40E_LINK_SPEED_40GB:
7302 		speed = "40 G";
7303 		break;
7304 	case I40E_LINK_SPEED_20GB:
7305 		speed = "20 G";
7306 		break;
7307 	case I40E_LINK_SPEED_25GB:
7308 		speed = "25 G";
7309 		break;
7310 	case I40E_LINK_SPEED_10GB:
7311 		speed = "10 G";
7312 		break;
7313 	case I40E_LINK_SPEED_5GB:
7314 		speed = "5 G";
7315 		break;
7316 	case I40E_LINK_SPEED_2_5GB:
7317 		speed = "2.5 G";
7318 		break;
7319 	case I40E_LINK_SPEED_1GB:
7320 		speed = "1000 M";
7321 		break;
7322 	case I40E_LINK_SPEED_100MB:
7323 		speed = "100 M";
7324 		break;
7325 	default:
7326 		break;
7327 	}
7328 
7329 	switch (pf->hw.fc.current_mode) {
7330 	case I40E_FC_FULL:
7331 		fc = "RX/TX";
7332 		break;
7333 	case I40E_FC_TX_PAUSE:
7334 		fc = "TX";
7335 		break;
7336 	case I40E_FC_RX_PAUSE:
7337 		fc = "RX";
7338 		break;
7339 	default:
7340 		fc = "None";
7341 		break;
7342 	}
7343 
7344 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7345 		req_fec = "None";
7346 		fec = "None";
7347 		an = "False";
7348 
7349 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7350 			an = "True";
7351 
7352 		if (pf->hw.phy.link_info.fec_info &
7353 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7354 			fec = "CL74 FC-FEC/BASE-R";
7355 		else if (pf->hw.phy.link_info.fec_info &
7356 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7357 			fec = "CL108 RS-FEC";
7358 
7359 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7360 		 * both RS and FC are requested
7361 		 */
7362 		if (vsi->back->hw.phy.link_info.req_fec_info &
7363 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7364 			if (vsi->back->hw.phy.link_info.req_fec_info &
7365 			    I40E_AQ_REQUEST_FEC_RS)
7366 				req_fec = "CL108 RS-FEC";
7367 			else
7368 				req_fec = "CL74 FC-FEC/BASE-R";
7369 		}
7370 		netdev_info(vsi->netdev,
7371 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7372 			    speed, req_fec, fec, an, fc);
7373 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7374 		req_fec = "None";
7375 		fec = "None";
7376 		an = "False";
7377 
7378 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7379 			an = "True";
7380 
7381 		if (pf->hw.phy.link_info.fec_info &
7382 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7383 			fec = "CL74 FC-FEC/BASE-R";
7384 
7385 		if (pf->hw.phy.link_info.req_fec_info &
7386 		    I40E_AQ_REQUEST_FEC_KR)
7387 			req_fec = "CL74 FC-FEC/BASE-R";
7388 
7389 		netdev_info(vsi->netdev,
7390 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7391 			    speed, req_fec, fec, an, fc);
7392 	} else {
7393 		netdev_info(vsi->netdev,
7394 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7395 			    speed, fc);
7396 	}
7397 
7398 }
7399 
7400 /**
7401  * i40e_up_complete - Finish the last steps of bringing up a connection
7402  * @vsi: the VSI being configured
7403  **/
i40e_up_complete(struct i40e_vsi * vsi)7404 static int i40e_up_complete(struct i40e_vsi *vsi)
7405 {
7406 	struct i40e_pf *pf = vsi->back;
7407 	int err;
7408 
7409 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7410 		i40e_vsi_configure_msix(vsi);
7411 	else
7412 		i40e_configure_msi_and_legacy(vsi);
7413 
7414 	/* start rings */
7415 	err = i40e_vsi_start_rings(vsi);
7416 	if (err)
7417 		return err;
7418 
7419 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7420 	i40e_napi_enable_all(vsi);
7421 	i40e_vsi_enable_irq(vsi);
7422 
7423 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7424 	    (vsi->netdev)) {
7425 		i40e_print_link_message(vsi, true);
7426 		netif_tx_start_all_queues(vsi->netdev);
7427 		netif_carrier_on(vsi->netdev);
7428 	}
7429 
7430 	/* replay FDIR SB filters */
7431 	if (vsi->type == I40E_VSI_FDIR) {
7432 		/* reset fd counters */
7433 		pf->fd_add_err = 0;
7434 		pf->fd_atr_cnt = 0;
7435 		i40e_fdir_filter_restore(vsi);
7436 	}
7437 
7438 	/* On the next run of the service_task, notify any clients of the new
7439 	 * opened netdev
7440 	 */
7441 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7442 	i40e_service_event_schedule(pf);
7443 
7444 	return 0;
7445 }
7446 
7447 /**
7448  * i40e_vsi_reinit_locked - Reset the VSI
7449  * @vsi: the VSI being configured
7450  *
7451  * Rebuild the ring structs after some configuration
7452  * has changed, e.g. MTU size.
7453  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7454 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7455 {
7456 	struct i40e_pf *pf = vsi->back;
7457 
7458 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7459 		usleep_range(1000, 2000);
7460 	i40e_down(vsi);
7461 
7462 	i40e_up(vsi);
7463 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7464 }
7465 
7466 /**
7467  * i40e_force_link_state - Force the link status
7468  * @pf: board private structure
7469  * @is_up: whether the link state should be forced up or down
7470  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7471 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7472 {
7473 	struct i40e_aq_get_phy_abilities_resp abilities;
7474 	struct i40e_aq_set_phy_config config = {0};
7475 	bool non_zero_phy_type = is_up;
7476 	struct i40e_hw *hw = &pf->hw;
7477 	u64 mask;
7478 	u8 speed;
7479 	int err;
7480 
7481 	/* Card might've been put in an unstable state by other drivers
7482 	 * and applications, which causes incorrect speed values being
7483 	 * set on startup. In order to clear speed registers, we call
7484 	 * get_phy_capabilities twice, once to get initial state of
7485 	 * available speeds, and once to get current PHY config.
7486 	 */
7487 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7488 					   NULL);
7489 	if (err) {
7490 		dev_err(&pf->pdev->dev,
7491 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7492 			ERR_PTR(err),
7493 			i40e_aq_str(hw, hw->aq.asq_last_status));
7494 		return err;
7495 	}
7496 	speed = abilities.link_speed;
7497 
7498 	/* Get the current phy config */
7499 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7500 					   NULL);
7501 	if (err) {
7502 		dev_err(&pf->pdev->dev,
7503 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7504 			ERR_PTR(err),
7505 			i40e_aq_str(hw, hw->aq.asq_last_status));
7506 		return err;
7507 	}
7508 
7509 	/* If link needs to go up, but was not forced to go down,
7510 	 * and its speed values are OK, no need for a flap
7511 	 * if non_zero_phy_type was set, still need to force up
7512 	 */
7513 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7514 		non_zero_phy_type = true;
7515 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7516 		return 0;
7517 
7518 	/* To force link we need to set bits for all supported PHY types,
7519 	 * but there are now more than 32, so we need to split the bitmap
7520 	 * across two fields.
7521 	 */
7522 	mask = I40E_PHY_TYPES_BITMASK;
7523 	config.phy_type =
7524 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7525 	config.phy_type_ext =
7526 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7527 	/* Copy the old settings, except of phy_type */
7528 	config.abilities = abilities.abilities;
7529 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7530 		if (is_up)
7531 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7532 		else
7533 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7534 	}
7535 	if (abilities.link_speed != 0)
7536 		config.link_speed = abilities.link_speed;
7537 	else
7538 		config.link_speed = speed;
7539 	config.eee_capability = abilities.eee_capability;
7540 	config.eeer = abilities.eeer_val;
7541 	config.low_power_ctrl = abilities.d3_lpan;
7542 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7543 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7544 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7545 
7546 	if (err) {
7547 		dev_err(&pf->pdev->dev,
7548 			"set phy config ret =  %pe last_status =  %s\n",
7549 			ERR_PTR(err),
7550 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7551 		return err;
7552 	}
7553 
7554 	/* Update the link info */
7555 	err = i40e_update_link_info(hw);
7556 	if (err) {
7557 		/* Wait a little bit (on 40G cards it sometimes takes a really
7558 		 * long time for link to come back from the atomic reset)
7559 		 * and try once more
7560 		 */
7561 		msleep(1000);
7562 		i40e_update_link_info(hw);
7563 	}
7564 
7565 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7566 
7567 	return 0;
7568 }
7569 
7570 /**
7571  * i40e_up - Bring the connection back up after being down
7572  * @vsi: the VSI being configured
7573  **/
i40e_up(struct i40e_vsi * vsi)7574 int i40e_up(struct i40e_vsi *vsi)
7575 {
7576 	int err;
7577 
7578 	if (vsi->type == I40E_VSI_MAIN &&
7579 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7580 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7581 		i40e_force_link_state(vsi->back, true);
7582 
7583 	err = i40e_vsi_configure(vsi);
7584 	if (!err)
7585 		err = i40e_up_complete(vsi);
7586 
7587 	return err;
7588 }
7589 
7590 /**
7591  * i40e_down - Shutdown the connection processing
7592  * @vsi: the VSI being stopped
7593  **/
i40e_down(struct i40e_vsi * vsi)7594 void i40e_down(struct i40e_vsi *vsi)
7595 {
7596 	int i;
7597 
7598 	/* It is assumed that the caller of this function
7599 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7600 	 */
7601 	if (vsi->netdev) {
7602 		netif_carrier_off(vsi->netdev);
7603 		netif_tx_disable(vsi->netdev);
7604 	}
7605 	i40e_vsi_disable_irq(vsi);
7606 	i40e_vsi_stop_rings(vsi);
7607 	if (vsi->type == I40E_VSI_MAIN &&
7608 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7609 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7610 		i40e_force_link_state(vsi->back, false);
7611 	i40e_napi_disable_all(vsi);
7612 
7613 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7614 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7615 		if (i40e_enabled_xdp_vsi(vsi)) {
7616 			/* Make sure that in-progress ndo_xdp_xmit and
7617 			 * ndo_xsk_wakeup calls are completed.
7618 			 */
7619 			synchronize_rcu();
7620 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7621 		}
7622 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7623 	}
7624 
7625 }
7626 
7627 /**
7628  * i40e_validate_mqprio_qopt- validate queue mapping info
7629  * @vsi: the VSI being configured
7630  * @mqprio_qopt: queue parametrs
7631  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7632 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7633 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7634 {
7635 	u64 sum_max_rate = 0;
7636 	u64 max_rate = 0;
7637 	int i;
7638 
7639 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7640 	    mqprio_qopt->qopt.num_tc < 1 ||
7641 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7642 		return -EINVAL;
7643 	for (i = 0; ; i++) {
7644 		if (!mqprio_qopt->qopt.count[i])
7645 			return -EINVAL;
7646 		if (mqprio_qopt->min_rate[i]) {
7647 			dev_err(&vsi->back->pdev->dev,
7648 				"Invalid min tx rate (greater than 0) specified\n");
7649 			return -EINVAL;
7650 		}
7651 		max_rate = mqprio_qopt->max_rate[i];
7652 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7653 		sum_max_rate += max_rate;
7654 
7655 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7656 			break;
7657 		if (mqprio_qopt->qopt.offset[i + 1] !=
7658 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7659 			return -EINVAL;
7660 	}
7661 	if (vsi->num_queue_pairs <
7662 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7663 		dev_err(&vsi->back->pdev->dev,
7664 			"Failed to create traffic channel, insufficient number of queues.\n");
7665 		return -EINVAL;
7666 	}
7667 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7668 		dev_err(&vsi->back->pdev->dev,
7669 			"Invalid max tx rate specified\n");
7670 		return -EINVAL;
7671 	}
7672 	return 0;
7673 }
7674 
7675 /**
7676  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7677  * @vsi: the VSI being configured
7678  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7679 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7680 {
7681 	u16 qcount;
7682 	int i;
7683 
7684 	/* Only TC0 is enabled */
7685 	vsi->tc_config.numtc = 1;
7686 	vsi->tc_config.enabled_tc = 1;
7687 	qcount = min_t(int, vsi->alloc_queue_pairs,
7688 		       i40e_pf_get_max_q_per_tc(vsi->back));
7689 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7690 		/* For the TC that is not enabled set the offset to default
7691 		 * queue and allocate one queue for the given TC.
7692 		 */
7693 		vsi->tc_config.tc_info[i].qoffset = 0;
7694 		if (i == 0)
7695 			vsi->tc_config.tc_info[i].qcount = qcount;
7696 		else
7697 			vsi->tc_config.tc_info[i].qcount = 1;
7698 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7699 	}
7700 }
7701 
7702 /**
7703  * i40e_del_macvlan_filter
7704  * @hw: pointer to the HW structure
7705  * @seid: seid of the channel VSI
7706  * @macaddr: the mac address to apply as a filter
7707  * @aq_err: store the admin Q error
7708  *
7709  * This function deletes a mac filter on the channel VSI which serves as the
7710  * macvlan. Returns 0 on success.
7711  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7712 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7713 				   const u8 *macaddr, int *aq_err)
7714 {
7715 	struct i40e_aqc_remove_macvlan_element_data element;
7716 	int status;
7717 
7718 	memset(&element, 0, sizeof(element));
7719 	ether_addr_copy(element.mac_addr, macaddr);
7720 	element.vlan_tag = 0;
7721 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7722 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7723 	*aq_err = hw->aq.asq_last_status;
7724 
7725 	return status;
7726 }
7727 
7728 /**
7729  * i40e_add_macvlan_filter
7730  * @hw: pointer to the HW structure
7731  * @seid: seid of the channel VSI
7732  * @macaddr: the mac address to apply as a filter
7733  * @aq_err: store the admin Q error
7734  *
7735  * This function adds a mac filter on the channel VSI which serves as the
7736  * macvlan. Returns 0 on success.
7737  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7738 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7739 				   const u8 *macaddr, int *aq_err)
7740 {
7741 	struct i40e_aqc_add_macvlan_element_data element;
7742 	u16 cmd_flags = 0;
7743 	int status;
7744 
7745 	ether_addr_copy(element.mac_addr, macaddr);
7746 	element.vlan_tag = 0;
7747 	element.queue_number = 0;
7748 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7749 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7750 	element.flags = cpu_to_le16(cmd_flags);
7751 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7752 	*aq_err = hw->aq.asq_last_status;
7753 
7754 	return status;
7755 }
7756 
7757 /**
7758  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7759  * @vsi: the VSI we want to access
7760  * @ch: the channel we want to access
7761  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7762 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7763 {
7764 	struct i40e_ring *tx_ring, *rx_ring;
7765 	u16 pf_q;
7766 	int i;
7767 
7768 	for (i = 0; i < ch->num_queue_pairs; i++) {
7769 		pf_q = ch->base_queue + i;
7770 		tx_ring = vsi->tx_rings[pf_q];
7771 		tx_ring->ch = NULL;
7772 		rx_ring = vsi->rx_rings[pf_q];
7773 		rx_ring->ch = NULL;
7774 	}
7775 }
7776 
7777 /**
7778  * i40e_free_macvlan_channels
7779  * @vsi: the VSI we want to access
7780  *
7781  * This function frees the Qs of the channel VSI from
7782  * the stack and also deletes the channel VSIs which
7783  * serve as macvlans.
7784  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7785 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7786 {
7787 	struct i40e_channel *ch, *ch_tmp;
7788 	int ret;
7789 
7790 	if (list_empty(&vsi->macvlan_list))
7791 		return;
7792 
7793 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7794 		struct i40e_vsi *parent_vsi;
7795 
7796 		if (i40e_is_channel_macvlan(ch)) {
7797 			i40e_reset_ch_rings(vsi, ch);
7798 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7799 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7800 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7801 			kfree(ch->fwd);
7802 			ch->fwd = NULL;
7803 		}
7804 
7805 		list_del(&ch->list);
7806 		parent_vsi = ch->parent_vsi;
7807 		if (!parent_vsi || !ch->initialized) {
7808 			kfree(ch);
7809 			continue;
7810 		}
7811 
7812 		/* remove the VSI */
7813 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7814 					     NULL);
7815 		if (ret)
7816 			dev_err(&vsi->back->pdev->dev,
7817 				"unable to remove channel (%d) for parent VSI(%d)\n",
7818 				ch->seid, parent_vsi->seid);
7819 		kfree(ch);
7820 	}
7821 	vsi->macvlan_cnt = 0;
7822 }
7823 
7824 /**
7825  * i40e_fwd_ring_up - bring the macvlan device up
7826  * @vsi: the VSI we want to access
7827  * @vdev: macvlan netdevice
7828  * @fwd: the private fwd structure
7829  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7830 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7831 			    struct i40e_fwd_adapter *fwd)
7832 {
7833 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7834 	int ret = 0, num_tc = 1,  i, aq_err;
7835 	struct i40e_pf *pf = vsi->back;
7836 	struct i40e_hw *hw = &pf->hw;
7837 
7838 	/* Go through the list and find an available channel */
7839 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7840 		if (!i40e_is_channel_macvlan(iter)) {
7841 			iter->fwd = fwd;
7842 			/* record configuration for macvlan interface in vdev */
7843 			for (i = 0; i < num_tc; i++)
7844 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7845 							     i,
7846 							     iter->num_queue_pairs,
7847 							     iter->base_queue);
7848 			for (i = 0; i < iter->num_queue_pairs; i++) {
7849 				struct i40e_ring *tx_ring, *rx_ring;
7850 				u16 pf_q;
7851 
7852 				pf_q = iter->base_queue + i;
7853 
7854 				/* Get to TX ring ptr */
7855 				tx_ring = vsi->tx_rings[pf_q];
7856 				tx_ring->ch = iter;
7857 
7858 				/* Get the RX ring ptr */
7859 				rx_ring = vsi->rx_rings[pf_q];
7860 				rx_ring->ch = iter;
7861 			}
7862 			ch = iter;
7863 			break;
7864 		}
7865 	}
7866 
7867 	if (!ch)
7868 		return -EINVAL;
7869 
7870 	/* Guarantee all rings are updated before we update the
7871 	 * MAC address filter.
7872 	 */
7873 	wmb();
7874 
7875 	/* Add a mac filter */
7876 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7877 	if (ret) {
7878 		/* if we cannot add the MAC rule then disable the offload */
7879 		macvlan_release_l2fw_offload(vdev);
7880 		for (i = 0; i < ch->num_queue_pairs; i++) {
7881 			struct i40e_ring *rx_ring;
7882 			u16 pf_q;
7883 
7884 			pf_q = ch->base_queue + i;
7885 			rx_ring = vsi->rx_rings[pf_q];
7886 			rx_ring->netdev = NULL;
7887 		}
7888 		dev_info(&pf->pdev->dev,
7889 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7890 			  ERR_PTR(ret),
7891 			  i40e_aq_str(hw, aq_err));
7892 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7893 	}
7894 
7895 	return ret;
7896 }
7897 
7898 /**
7899  * i40e_setup_macvlans - create the channels which will be macvlans
7900  * @vsi: the VSI we want to access
7901  * @macvlan_cnt: no. of macvlans to be setup
7902  * @qcnt: no. of Qs per macvlan
7903  * @vdev: macvlan netdevice
7904  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7905 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7906 			       struct net_device *vdev)
7907 {
7908 	struct i40e_pf *pf = vsi->back;
7909 	struct i40e_hw *hw = &pf->hw;
7910 	struct i40e_vsi_context ctxt;
7911 	u16 sections, qmap, num_qps;
7912 	struct i40e_channel *ch;
7913 	int i, pow, ret = 0;
7914 	u8 offset = 0;
7915 
7916 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7917 		return -EINVAL;
7918 
7919 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7920 
7921 	/* find the next higher power-of-2 of num queue pairs */
7922 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7923 
7924 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7925 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7926 
7927 	/* Setup context bits for the main VSI */
7928 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7929 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7930 	memset(&ctxt, 0, sizeof(ctxt));
7931 	ctxt.seid = vsi->seid;
7932 	ctxt.pf_num = vsi->back->hw.pf_id;
7933 	ctxt.vf_num = 0;
7934 	ctxt.uplink_seid = vsi->uplink_seid;
7935 	ctxt.info = vsi->info;
7936 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7937 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7938 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7939 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7940 
7941 	/* Reconfigure RSS for main VSI with new max queue count */
7942 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7943 	ret = i40e_vsi_config_rss(vsi);
7944 	if (ret) {
7945 		dev_info(&pf->pdev->dev,
7946 			 "Failed to reconfig RSS for num_queues (%u)\n",
7947 			 vsi->rss_size);
7948 		return ret;
7949 	}
7950 	vsi->reconfig_rss = true;
7951 	dev_dbg(&vsi->back->pdev->dev,
7952 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7953 	vsi->next_base_queue = num_qps;
7954 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7955 
7956 	/* Update the VSI after updating the VSI queue-mapping
7957 	 * information
7958 	 */
7959 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7960 	if (ret) {
7961 		dev_info(&pf->pdev->dev,
7962 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7963 			 ERR_PTR(ret),
7964 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7965 		return ret;
7966 	}
7967 	/* update the local VSI info with updated queue map */
7968 	i40e_vsi_update_queue_map(vsi, &ctxt);
7969 	vsi->info.valid_sections = 0;
7970 
7971 	/* Create channels for macvlans */
7972 	INIT_LIST_HEAD(&vsi->macvlan_list);
7973 	for (i = 0; i < macvlan_cnt; i++) {
7974 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7975 		if (!ch) {
7976 			ret = -ENOMEM;
7977 			goto err_free;
7978 		}
7979 		INIT_LIST_HEAD(&ch->list);
7980 		ch->num_queue_pairs = qcnt;
7981 		if (!i40e_setup_channel(pf, vsi, ch)) {
7982 			ret = -EINVAL;
7983 			kfree(ch);
7984 			goto err_free;
7985 		}
7986 		ch->parent_vsi = vsi;
7987 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7988 		vsi->macvlan_cnt++;
7989 		list_add_tail(&ch->list, &vsi->macvlan_list);
7990 	}
7991 
7992 	return ret;
7993 
7994 err_free:
7995 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7996 	i40e_free_macvlan_channels(vsi);
7997 
7998 	return ret;
7999 }
8000 
8001 /**
8002  * i40e_fwd_add - configure macvlans
8003  * @netdev: net device to configure
8004  * @vdev: macvlan netdevice
8005  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)8006 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
8007 {
8008 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8009 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
8010 	struct i40e_vsi *vsi = np->vsi;
8011 	struct i40e_pf *pf = vsi->back;
8012 	struct i40e_fwd_adapter *fwd;
8013 	int avail_macvlan, ret;
8014 
8015 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
8016 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8017 		return ERR_PTR(-EINVAL);
8018 	}
8019 	if (i40e_is_tc_mqprio_enabled(pf)) {
8020 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8021 		return ERR_PTR(-EINVAL);
8022 	}
8023 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8024 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8025 		return ERR_PTR(-EINVAL);
8026 	}
8027 
8028 	/* The macvlan device has to be a single Q device so that the
8029 	 * tc_to_txq field can be reused to pick the tx queue.
8030 	 */
8031 	if (netif_is_multiqueue(vdev))
8032 		return ERR_PTR(-ERANGE);
8033 
8034 	if (!vsi->macvlan_cnt) {
8035 		/* reserve bit 0 for the pf device */
8036 		set_bit(0, vsi->fwd_bitmask);
8037 
8038 		/* Try to reserve as many queues as possible for macvlans. First
8039 		 * reserve 3/4th of max vectors, then half, then quarter and
8040 		 * calculate Qs per macvlan as you go
8041 		 */
8042 		vectors = pf->num_lan_msix;
8043 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8044 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8045 			q_per_macvlan = 4;
8046 			macvlan_cnt = (vectors - 32) / 4;
8047 		} else if (vectors <= 64 && vectors > 32) {
8048 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8049 			q_per_macvlan = 2;
8050 			macvlan_cnt = (vectors - 16) / 2;
8051 		} else if (vectors <= 32 && vectors > 16) {
8052 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8053 			q_per_macvlan = 1;
8054 			macvlan_cnt = vectors - 16;
8055 		} else if (vectors <= 16 && vectors > 8) {
8056 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8057 			q_per_macvlan = 1;
8058 			macvlan_cnt = vectors - 8;
8059 		} else {
8060 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8061 			q_per_macvlan = 1;
8062 			macvlan_cnt = vectors - 1;
8063 		}
8064 
8065 		if (macvlan_cnt == 0)
8066 			return ERR_PTR(-EBUSY);
8067 
8068 		/* Quiesce VSI queues */
8069 		i40e_quiesce_vsi(vsi);
8070 
8071 		/* sets up the macvlans but does not "enable" them */
8072 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8073 					  vdev);
8074 		if (ret)
8075 			return ERR_PTR(ret);
8076 
8077 		/* Unquiesce VSI */
8078 		i40e_unquiesce_vsi(vsi);
8079 	}
8080 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8081 					    vsi->macvlan_cnt);
8082 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8083 		return ERR_PTR(-EBUSY);
8084 
8085 	/* create the fwd struct */
8086 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8087 	if (!fwd)
8088 		return ERR_PTR(-ENOMEM);
8089 
8090 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8091 	fwd->bit_no = avail_macvlan;
8092 	netdev_set_sb_channel(vdev, avail_macvlan);
8093 	fwd->netdev = vdev;
8094 
8095 	if (!netif_running(netdev))
8096 		return fwd;
8097 
8098 	/* Set fwd ring up */
8099 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8100 	if (ret) {
8101 		/* unbind the queues and drop the subordinate channel config */
8102 		netdev_unbind_sb_channel(netdev, vdev);
8103 		netdev_set_sb_channel(vdev, 0);
8104 
8105 		kfree(fwd);
8106 		return ERR_PTR(-EINVAL);
8107 	}
8108 
8109 	return fwd;
8110 }
8111 
8112 /**
8113  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8114  * @vsi: the VSI we want to access
8115  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8116 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8117 {
8118 	struct i40e_channel *ch, *ch_tmp;
8119 	struct i40e_pf *pf = vsi->back;
8120 	struct i40e_hw *hw = &pf->hw;
8121 	int aq_err, ret = 0;
8122 
8123 	if (list_empty(&vsi->macvlan_list))
8124 		return;
8125 
8126 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8127 		if (i40e_is_channel_macvlan(ch)) {
8128 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8129 						      i40e_channel_mac(ch),
8130 						      &aq_err);
8131 			if (!ret) {
8132 				/* Reset queue contexts */
8133 				i40e_reset_ch_rings(vsi, ch);
8134 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8135 				netdev_unbind_sb_channel(vsi->netdev,
8136 							 ch->fwd->netdev);
8137 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8138 				kfree(ch->fwd);
8139 				ch->fwd = NULL;
8140 			}
8141 		}
8142 	}
8143 }
8144 
8145 /**
8146  * i40e_fwd_del - delete macvlan interfaces
8147  * @netdev: net device to configure
8148  * @vdev: macvlan netdevice
8149  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8150 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8151 {
8152 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8153 	struct i40e_fwd_adapter *fwd = vdev;
8154 	struct i40e_channel *ch, *ch_tmp;
8155 	struct i40e_vsi *vsi = np->vsi;
8156 	struct i40e_pf *pf = vsi->back;
8157 	struct i40e_hw *hw = &pf->hw;
8158 	int aq_err, ret = 0;
8159 
8160 	/* Find the channel associated with the macvlan and del mac filter */
8161 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8162 		if (i40e_is_channel_macvlan(ch) &&
8163 		    ether_addr_equal(i40e_channel_mac(ch),
8164 				     fwd->netdev->dev_addr)) {
8165 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8166 						      i40e_channel_mac(ch),
8167 						      &aq_err);
8168 			if (!ret) {
8169 				/* Reset queue contexts */
8170 				i40e_reset_ch_rings(vsi, ch);
8171 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8172 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8173 				netdev_set_sb_channel(fwd->netdev, 0);
8174 				kfree(ch->fwd);
8175 				ch->fwd = NULL;
8176 			} else {
8177 				dev_info(&pf->pdev->dev,
8178 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8179 					  ERR_PTR(ret),
8180 					  i40e_aq_str(hw, aq_err));
8181 			}
8182 			break;
8183 		}
8184 	}
8185 }
8186 
8187 /**
8188  * i40e_setup_tc - configure multiple traffic classes
8189  * @netdev: net device to configure
8190  * @type_data: tc offload data
8191  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8192 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8193 {
8194 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8195 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8196 	struct i40e_vsi *vsi = np->vsi;
8197 	struct i40e_pf *pf = vsi->back;
8198 	u8 enabled_tc = 0, num_tc, hw;
8199 	bool need_reset = false;
8200 	int old_queue_pairs;
8201 	int ret = -EINVAL;
8202 	u16 mode;
8203 	int i;
8204 
8205 	old_queue_pairs = vsi->num_queue_pairs;
8206 	num_tc = mqprio_qopt->qopt.num_tc;
8207 	hw = mqprio_qopt->qopt.hw;
8208 	mode = mqprio_qopt->mode;
8209 	if (!hw) {
8210 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8211 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8212 		goto config_tc;
8213 	}
8214 
8215 	/* Check if MFP enabled */
8216 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8217 		netdev_info(netdev,
8218 			    "Configuring TC not supported in MFP mode\n");
8219 		return ret;
8220 	}
8221 	switch (mode) {
8222 	case TC_MQPRIO_MODE_DCB:
8223 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8224 
8225 		/* Check if DCB enabled to continue */
8226 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8227 			netdev_info(netdev,
8228 				    "DCB is not enabled for adapter\n");
8229 			return ret;
8230 		}
8231 
8232 		/* Check whether tc count is within enabled limit */
8233 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8234 			netdev_info(netdev,
8235 				    "TC count greater than enabled on link for adapter\n");
8236 			return ret;
8237 		}
8238 		break;
8239 	case TC_MQPRIO_MODE_CHANNEL:
8240 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8241 			netdev_info(netdev,
8242 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8243 			return ret;
8244 		}
8245 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8246 			return ret;
8247 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8248 		if (ret)
8249 			return ret;
8250 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8251 		       sizeof(*mqprio_qopt));
8252 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8253 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8254 		break;
8255 	default:
8256 		return -EINVAL;
8257 	}
8258 
8259 config_tc:
8260 	/* Generate TC map for number of tc requested */
8261 	for (i = 0; i < num_tc; i++)
8262 		enabled_tc |= BIT(i);
8263 
8264 	/* Requesting same TC configuration as already enabled */
8265 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8266 	    mode != TC_MQPRIO_MODE_CHANNEL)
8267 		return 0;
8268 
8269 	/* Quiesce VSI queues */
8270 	i40e_quiesce_vsi(vsi);
8271 
8272 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8273 		i40e_remove_queue_channels(vsi);
8274 
8275 	/* Configure VSI for enabled TCs */
8276 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8277 	if (ret) {
8278 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8279 			    vsi->seid);
8280 		need_reset = true;
8281 		goto exit;
8282 	} else if (enabled_tc &&
8283 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8284 		netdev_info(netdev,
8285 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8286 			    vsi->tc_config.tc_info[0].qcount);
8287 		ret = -EINVAL;
8288 		need_reset = true;
8289 		goto exit;
8290 	}
8291 
8292 	dev_info(&vsi->back->pdev->dev,
8293 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8294 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8295 
8296 	if (i40e_is_tc_mqprio_enabled(pf)) {
8297 		if (vsi->mqprio_qopt.max_rate[0]) {
8298 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8299 						  vsi->mqprio_qopt.max_rate[0]);
8300 
8301 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8302 			if (!ret) {
8303 				u64 credits = max_tx_rate;
8304 
8305 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8306 				dev_dbg(&vsi->back->pdev->dev,
8307 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8308 					max_tx_rate,
8309 					credits,
8310 					vsi->seid);
8311 			} else {
8312 				need_reset = true;
8313 				goto exit;
8314 			}
8315 		}
8316 		ret = i40e_configure_queue_channels(vsi);
8317 		if (ret) {
8318 			vsi->num_queue_pairs = old_queue_pairs;
8319 			netdev_info(netdev,
8320 				    "Failed configuring queue channels\n");
8321 			need_reset = true;
8322 			goto exit;
8323 		}
8324 	}
8325 
8326 exit:
8327 	/* Reset the configuration data to defaults, only TC0 is enabled */
8328 	if (need_reset) {
8329 		i40e_vsi_set_default_tc_config(vsi);
8330 		need_reset = false;
8331 	}
8332 
8333 	/* Unquiesce VSI */
8334 	i40e_unquiesce_vsi(vsi);
8335 	return ret;
8336 }
8337 
8338 /**
8339  * i40e_set_cld_element - sets cloud filter element data
8340  * @filter: cloud filter rule
8341  * @cld: ptr to cloud filter element data
8342  *
8343  * This is helper function to copy data into cloud filter element
8344  **/
8345 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8346 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8347 		     struct i40e_aqc_cloud_filters_element_data *cld)
8348 {
8349 	u32 ipa;
8350 	int i;
8351 
8352 	memset(cld, 0, sizeof(*cld));
8353 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8354 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8355 
8356 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8357 		return;
8358 
8359 	if (filter->n_proto == ETH_P_IPV6) {
8360 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8361 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8362 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8363 
8364 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8365 		}
8366 	} else {
8367 		ipa = be32_to_cpu(filter->dst_ipv4);
8368 
8369 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8370 	}
8371 
8372 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8373 
8374 	/* tenant_id is not supported by FW now, once the support is enabled
8375 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8376 	 */
8377 	if (filter->tenant_id)
8378 		return;
8379 }
8380 
8381 /**
8382  * i40e_add_del_cloud_filter - Add/del cloud filter
8383  * @vsi: pointer to VSI
8384  * @filter: cloud filter rule
8385  * @add: if true, add, if false, delete
8386  *
8387  * Add or delete a cloud filter for a specific flow spec.
8388  * Returns 0 if the filter were successfully added.
8389  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8390 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8391 			      struct i40e_cloud_filter *filter, bool add)
8392 {
8393 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8394 	struct i40e_pf *pf = vsi->back;
8395 	int ret;
8396 	static const u16 flag_table[128] = {
8397 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8398 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8399 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8400 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8401 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8402 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8403 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8404 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8405 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8406 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8407 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8408 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8409 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8410 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8411 	};
8412 
8413 	if (filter->flags >= ARRAY_SIZE(flag_table))
8414 		return -EIO;
8415 
8416 	memset(&cld_filter, 0, sizeof(cld_filter));
8417 
8418 	/* copy element needed to add cloud filter from filter */
8419 	i40e_set_cld_element(filter, &cld_filter);
8420 
8421 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8422 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8423 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8424 
8425 	if (filter->n_proto == ETH_P_IPV6)
8426 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8427 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8428 	else
8429 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8430 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8431 
8432 	if (add)
8433 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8434 						&cld_filter, 1);
8435 	else
8436 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8437 						&cld_filter, 1);
8438 	if (ret)
8439 		dev_dbg(&pf->pdev->dev,
8440 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8441 			add ? "add" : "delete", filter->dst_port, ret,
8442 			pf->hw.aq.asq_last_status);
8443 	else
8444 		dev_info(&pf->pdev->dev,
8445 			 "%s cloud filter for VSI: %d\n",
8446 			 add ? "Added" : "Deleted", filter->seid);
8447 	return ret;
8448 }
8449 
8450 /**
8451  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8452  * @vsi: pointer to VSI
8453  * @filter: cloud filter rule
8454  * @add: if true, add, if false, delete
8455  *
8456  * Add or delete a cloud filter for a specific flow spec using big buffer.
8457  * Returns 0 if the filter were successfully added.
8458  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8459 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8460 				      struct i40e_cloud_filter *filter,
8461 				      bool add)
8462 {
8463 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8464 	struct i40e_pf *pf = vsi->back;
8465 	int ret;
8466 
8467 	/* Both (src/dst) valid mac_addr are not supported */
8468 	if ((is_valid_ether_addr(filter->dst_mac) &&
8469 	     is_valid_ether_addr(filter->src_mac)) ||
8470 	    (is_multicast_ether_addr(filter->dst_mac) &&
8471 	     is_multicast_ether_addr(filter->src_mac)))
8472 		return -EOPNOTSUPP;
8473 
8474 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8475 	 * ports are not supported via big buffer now.
8476 	 */
8477 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8478 		return -EOPNOTSUPP;
8479 
8480 	/* adding filter using src_port/src_ip is not supported at this stage */
8481 	if (filter->src_port ||
8482 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8483 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8484 		return -EOPNOTSUPP;
8485 
8486 	memset(&cld_filter, 0, sizeof(cld_filter));
8487 
8488 	/* copy element needed to add cloud filter from filter */
8489 	i40e_set_cld_element(filter, &cld_filter.element);
8490 
8491 	if (is_valid_ether_addr(filter->dst_mac) ||
8492 	    is_valid_ether_addr(filter->src_mac) ||
8493 	    is_multicast_ether_addr(filter->dst_mac) ||
8494 	    is_multicast_ether_addr(filter->src_mac)) {
8495 		/* MAC + IP : unsupported mode */
8496 		if (filter->dst_ipv4)
8497 			return -EOPNOTSUPP;
8498 
8499 		/* since we validated that L4 port must be valid before
8500 		 * we get here, start with respective "flags" value
8501 		 * and update if vlan is present or not
8502 		 */
8503 		cld_filter.element.flags =
8504 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8505 
8506 		if (filter->vlan_id) {
8507 			cld_filter.element.flags =
8508 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8509 		}
8510 
8511 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8512 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8513 		cld_filter.element.flags =
8514 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8515 		if (filter->n_proto == ETH_P_IPV6)
8516 			cld_filter.element.flags |=
8517 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8518 		else
8519 			cld_filter.element.flags |=
8520 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8521 	} else {
8522 		dev_err(&pf->pdev->dev,
8523 			"either mac or ip has to be valid for cloud filter\n");
8524 		return -EINVAL;
8525 	}
8526 
8527 	/* Now copy L4 port in Byte 6..7 in general fields */
8528 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8529 						be16_to_cpu(filter->dst_port);
8530 
8531 	if (add) {
8532 		/* Validate current device switch mode, change if necessary */
8533 		ret = i40e_validate_and_set_switch_mode(vsi);
8534 		if (ret) {
8535 			dev_err(&pf->pdev->dev,
8536 				"failed to set switch mode, ret %d\n",
8537 				ret);
8538 			return ret;
8539 		}
8540 
8541 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8542 						   &cld_filter, 1);
8543 	} else {
8544 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8545 						   &cld_filter, 1);
8546 	}
8547 
8548 	if (ret)
8549 		dev_dbg(&pf->pdev->dev,
8550 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8551 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8552 	else
8553 		dev_info(&pf->pdev->dev,
8554 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8555 			 add ? "add" : "delete", filter->seid,
8556 			 ntohs(filter->dst_port));
8557 	return ret;
8558 }
8559 
8560 /**
8561  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8562  * @vsi: Pointer to VSI
8563  * @f: Pointer to struct flow_cls_offload
8564  * @filter: Pointer to cloud filter structure
8565  *
8566  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8567 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8568 				 struct flow_cls_offload *f,
8569 				 struct i40e_cloud_filter *filter)
8570 {
8571 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8572 	struct flow_dissector *dissector = rule->match.dissector;
8573 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8574 	struct i40e_pf *pf = vsi->back;
8575 	u8 field_flags = 0;
8576 
8577 	if (dissector->used_keys &
8578 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8579 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8580 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8581 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8582 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8583 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8584 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8585 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8586 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8587 			dissector->used_keys);
8588 		return -EOPNOTSUPP;
8589 	}
8590 
8591 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8592 		struct flow_match_enc_keyid match;
8593 
8594 		flow_rule_match_enc_keyid(rule, &match);
8595 		if (match.mask->keyid != 0)
8596 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8597 
8598 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8599 	}
8600 
8601 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8602 		struct flow_match_basic match;
8603 
8604 		flow_rule_match_basic(rule, &match);
8605 		n_proto_key = ntohs(match.key->n_proto);
8606 		n_proto_mask = ntohs(match.mask->n_proto);
8607 
8608 		if (n_proto_key == ETH_P_ALL) {
8609 			n_proto_key = 0;
8610 			n_proto_mask = 0;
8611 		}
8612 		filter->n_proto = n_proto_key & n_proto_mask;
8613 		filter->ip_proto = match.key->ip_proto;
8614 	}
8615 
8616 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8617 		struct flow_match_eth_addrs match;
8618 
8619 		flow_rule_match_eth_addrs(rule, &match);
8620 
8621 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8622 		if (!is_zero_ether_addr(match.mask->dst)) {
8623 			if (is_broadcast_ether_addr(match.mask->dst)) {
8624 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8625 			} else {
8626 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8627 					match.mask->dst);
8628 				return -EIO;
8629 			}
8630 		}
8631 
8632 		if (!is_zero_ether_addr(match.mask->src)) {
8633 			if (is_broadcast_ether_addr(match.mask->src)) {
8634 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8635 			} else {
8636 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8637 					match.mask->src);
8638 				return -EIO;
8639 			}
8640 		}
8641 		ether_addr_copy(filter->dst_mac, match.key->dst);
8642 		ether_addr_copy(filter->src_mac, match.key->src);
8643 	}
8644 
8645 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8646 		struct flow_match_vlan match;
8647 
8648 		flow_rule_match_vlan(rule, &match);
8649 		if (match.mask->vlan_id) {
8650 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8651 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8652 
8653 			} else {
8654 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8655 					match.mask->vlan_id);
8656 				return -EIO;
8657 			}
8658 		}
8659 
8660 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8661 	}
8662 
8663 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8664 		struct flow_match_control match;
8665 
8666 		flow_rule_match_control(rule, &match);
8667 		addr_type = match.key->addr_type;
8668 	}
8669 
8670 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8671 		struct flow_match_ipv4_addrs match;
8672 
8673 		flow_rule_match_ipv4_addrs(rule, &match);
8674 		if (match.mask->dst) {
8675 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8676 				field_flags |= I40E_CLOUD_FIELD_IIP;
8677 			} else {
8678 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8679 					&match.mask->dst);
8680 				return -EIO;
8681 			}
8682 		}
8683 
8684 		if (match.mask->src) {
8685 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8686 				field_flags |= I40E_CLOUD_FIELD_IIP;
8687 			} else {
8688 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8689 					&match.mask->src);
8690 				return -EIO;
8691 			}
8692 		}
8693 
8694 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8695 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8696 			return -EIO;
8697 		}
8698 		filter->dst_ipv4 = match.key->dst;
8699 		filter->src_ipv4 = match.key->src;
8700 	}
8701 
8702 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8703 		struct flow_match_ipv6_addrs match;
8704 
8705 		flow_rule_match_ipv6_addrs(rule, &match);
8706 
8707 		/* src and dest IPV6 address should not be LOOPBACK
8708 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8709 		 */
8710 		if (ipv6_addr_loopback(&match.key->dst) ||
8711 		    ipv6_addr_loopback(&match.key->src)) {
8712 			dev_err(&pf->pdev->dev,
8713 				"Bad ipv6, addr is LOOPBACK\n");
8714 			return -EIO;
8715 		}
8716 		if (!ipv6_addr_any(&match.mask->dst) ||
8717 		    !ipv6_addr_any(&match.mask->src))
8718 			field_flags |= I40E_CLOUD_FIELD_IIP;
8719 
8720 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8721 		       sizeof(filter->src_ipv6));
8722 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8723 		       sizeof(filter->dst_ipv6));
8724 	}
8725 
8726 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8727 		struct flow_match_ports match;
8728 
8729 		flow_rule_match_ports(rule, &match);
8730 		if (match.mask->src) {
8731 			if (match.mask->src == cpu_to_be16(0xffff)) {
8732 				field_flags |= I40E_CLOUD_FIELD_IIP;
8733 			} else {
8734 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8735 					be16_to_cpu(match.mask->src));
8736 				return -EIO;
8737 			}
8738 		}
8739 
8740 		if (match.mask->dst) {
8741 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8742 				field_flags |= I40E_CLOUD_FIELD_IIP;
8743 			} else {
8744 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8745 					be16_to_cpu(match.mask->dst));
8746 				return -EIO;
8747 			}
8748 		}
8749 
8750 		filter->dst_port = match.key->dst;
8751 		filter->src_port = match.key->src;
8752 
8753 		switch (filter->ip_proto) {
8754 		case IPPROTO_TCP:
8755 		case IPPROTO_UDP:
8756 			break;
8757 		default:
8758 			dev_err(&pf->pdev->dev,
8759 				"Only UDP and TCP transport are supported\n");
8760 			return -EINVAL;
8761 		}
8762 	}
8763 	filter->flags = field_flags;
8764 	return 0;
8765 }
8766 
8767 /**
8768  * i40e_handle_tclass: Forward to a traffic class on the device
8769  * @vsi: Pointer to VSI
8770  * @tc: traffic class index on the device
8771  * @filter: Pointer to cloud filter structure
8772  *
8773  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8774 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8775 			      struct i40e_cloud_filter *filter)
8776 {
8777 	struct i40e_channel *ch, *ch_tmp;
8778 
8779 	/* direct to a traffic class on the same device */
8780 	if (tc == 0) {
8781 		filter->seid = vsi->seid;
8782 		return 0;
8783 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8784 		if (!filter->dst_port) {
8785 			dev_err(&vsi->back->pdev->dev,
8786 				"Specify destination port to direct to traffic class that is not default\n");
8787 			return -EINVAL;
8788 		}
8789 		if (list_empty(&vsi->ch_list))
8790 			return -EINVAL;
8791 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8792 					 list) {
8793 			if (ch->seid == vsi->tc_seid_map[tc])
8794 				filter->seid = ch->seid;
8795 		}
8796 		return 0;
8797 	}
8798 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8799 	return -EINVAL;
8800 }
8801 
8802 /**
8803  * i40e_configure_clsflower - Configure tc flower filters
8804  * @vsi: Pointer to VSI
8805  * @cls_flower: Pointer to struct flow_cls_offload
8806  *
8807  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8808 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8809 				    struct flow_cls_offload *cls_flower)
8810 {
8811 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8812 	struct i40e_cloud_filter *filter = NULL;
8813 	struct i40e_pf *pf = vsi->back;
8814 	int err = 0;
8815 
8816 	if (tc < 0) {
8817 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8818 		return -EOPNOTSUPP;
8819 	}
8820 
8821 	if (!tc) {
8822 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8823 		return -EINVAL;
8824 	}
8825 
8826 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8827 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8828 		return -EBUSY;
8829 
8830 	if (pf->fdir_pf_active_filters ||
8831 	    (!hlist_empty(&pf->fdir_filter_list))) {
8832 		dev_err(&vsi->back->pdev->dev,
8833 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8834 		return -EINVAL;
8835 	}
8836 
8837 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8838 		dev_err(&vsi->back->pdev->dev,
8839 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8840 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8841 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8842 	}
8843 
8844 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8845 	if (!filter)
8846 		return -ENOMEM;
8847 
8848 	filter->cookie = cls_flower->cookie;
8849 
8850 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8851 	if (err < 0)
8852 		goto err;
8853 
8854 	err = i40e_handle_tclass(vsi, tc, filter);
8855 	if (err < 0)
8856 		goto err;
8857 
8858 	/* Add cloud filter */
8859 	if (filter->dst_port)
8860 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8861 	else
8862 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8863 
8864 	if (err) {
8865 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8866 			err);
8867 		goto err;
8868 	}
8869 
8870 	/* add filter to the ordered list */
8871 	INIT_HLIST_NODE(&filter->cloud_node);
8872 
8873 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8874 
8875 	pf->num_cloud_filters++;
8876 
8877 	return err;
8878 err:
8879 	kfree(filter);
8880 	return err;
8881 }
8882 
8883 /**
8884  * i40e_find_cloud_filter - Find the could filter in the list
8885  * @vsi: Pointer to VSI
8886  * @cookie: filter specific cookie
8887  *
8888  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8889 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8890 							unsigned long *cookie)
8891 {
8892 	struct i40e_cloud_filter *filter = NULL;
8893 	struct hlist_node *node2;
8894 
8895 	hlist_for_each_entry_safe(filter, node2,
8896 				  &vsi->back->cloud_filter_list, cloud_node)
8897 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8898 			return filter;
8899 	return NULL;
8900 }
8901 
8902 /**
8903  * i40e_delete_clsflower - Remove tc flower filters
8904  * @vsi: Pointer to VSI
8905  * @cls_flower: Pointer to struct flow_cls_offload
8906  *
8907  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8908 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8909 				 struct flow_cls_offload *cls_flower)
8910 {
8911 	struct i40e_cloud_filter *filter = NULL;
8912 	struct i40e_pf *pf = vsi->back;
8913 	int err = 0;
8914 
8915 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8916 
8917 	if (!filter)
8918 		return -EINVAL;
8919 
8920 	hash_del(&filter->cloud_node);
8921 
8922 	if (filter->dst_port)
8923 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8924 	else
8925 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8926 
8927 	kfree(filter);
8928 	if (err) {
8929 		dev_err(&pf->pdev->dev,
8930 			"Failed to delete cloud filter, err %pe\n",
8931 			ERR_PTR(err));
8932 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8933 	}
8934 
8935 	pf->num_cloud_filters--;
8936 	if (!pf->num_cloud_filters)
8937 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8938 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8939 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8940 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8941 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8942 		}
8943 	return 0;
8944 }
8945 
8946 /**
8947  * i40e_setup_tc_cls_flower - flower classifier offloads
8948  * @np: net device to configure
8949  * @cls_flower: offload data
8950  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8951 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8952 				    struct flow_cls_offload *cls_flower)
8953 {
8954 	struct i40e_vsi *vsi = np->vsi;
8955 
8956 	switch (cls_flower->command) {
8957 	case FLOW_CLS_REPLACE:
8958 		return i40e_configure_clsflower(vsi, cls_flower);
8959 	case FLOW_CLS_DESTROY:
8960 		return i40e_delete_clsflower(vsi, cls_flower);
8961 	case FLOW_CLS_STATS:
8962 		return -EOPNOTSUPP;
8963 	default:
8964 		return -EOPNOTSUPP;
8965 	}
8966 }
8967 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8968 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8969 				  void *cb_priv)
8970 {
8971 	struct i40e_netdev_priv *np = cb_priv;
8972 
8973 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8974 		return -EOPNOTSUPP;
8975 
8976 	switch (type) {
8977 	case TC_SETUP_CLSFLOWER:
8978 		return i40e_setup_tc_cls_flower(np, type_data);
8979 
8980 	default:
8981 		return -EOPNOTSUPP;
8982 	}
8983 }
8984 
8985 static LIST_HEAD(i40e_block_cb_list);
8986 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8987 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8988 			   void *type_data)
8989 {
8990 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8991 
8992 	switch (type) {
8993 	case TC_SETUP_QDISC_MQPRIO:
8994 		return i40e_setup_tc(netdev, type_data);
8995 	case TC_SETUP_BLOCK:
8996 		return flow_block_cb_setup_simple(type_data,
8997 						  &i40e_block_cb_list,
8998 						  i40e_setup_tc_block_cb,
8999 						  np, np, true);
9000 	default:
9001 		return -EOPNOTSUPP;
9002 	}
9003 }
9004 
9005 /**
9006  * i40e_open - Called when a network interface is made active
9007  * @netdev: network interface device structure
9008  *
9009  * The open entry point is called when a network interface is made
9010  * active by the system (IFF_UP).  At this point all resources needed
9011  * for transmit and receive operations are allocated, the interrupt
9012  * handler is registered with the OS, the netdev watchdog subtask is
9013  * enabled, and the stack is notified that the interface is ready.
9014  *
9015  * Returns 0 on success, negative value on failure
9016  **/
i40e_open(struct net_device * netdev)9017 int i40e_open(struct net_device *netdev)
9018 {
9019 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9020 	struct i40e_vsi *vsi = np->vsi;
9021 	struct i40e_pf *pf = vsi->back;
9022 	int err;
9023 
9024 	/* disallow open during test or if eeprom is broken */
9025 	if (test_bit(__I40E_TESTING, pf->state) ||
9026 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9027 		return -EBUSY;
9028 
9029 	netif_carrier_off(netdev);
9030 
9031 	if (i40e_force_link_state(pf, true))
9032 		return -EAGAIN;
9033 
9034 	err = i40e_vsi_open(vsi);
9035 	if (err)
9036 		return err;
9037 
9038 	/* configure global TSO hardware offload settings */
9039 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9040 						       TCP_FLAG_FIN) >> 16);
9041 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9042 						       TCP_FLAG_FIN |
9043 						       TCP_FLAG_CWR) >> 16);
9044 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9045 	udp_tunnel_get_rx_info(netdev);
9046 
9047 	return 0;
9048 }
9049 
9050 /**
9051  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9052  * @vsi: vsi structure
9053  *
9054  * This updates netdev's number of tx/rx queues
9055  *
9056  * Returns status of setting tx/rx queues
9057  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9058 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9059 {
9060 	int ret;
9061 
9062 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9063 					   vsi->num_queue_pairs);
9064 	if (ret)
9065 		return ret;
9066 
9067 	return netif_set_real_num_tx_queues(vsi->netdev,
9068 					    vsi->num_queue_pairs);
9069 }
9070 
9071 /**
9072  * i40e_vsi_open -
9073  * @vsi: the VSI to open
9074  *
9075  * Finish initialization of the VSI.
9076  *
9077  * Returns 0 on success, negative value on failure
9078  *
9079  * Note: expects to be called while under rtnl_lock()
9080  **/
i40e_vsi_open(struct i40e_vsi * vsi)9081 int i40e_vsi_open(struct i40e_vsi *vsi)
9082 {
9083 	struct i40e_pf *pf = vsi->back;
9084 	char int_name[I40E_INT_NAME_STR_LEN];
9085 	int err;
9086 
9087 	/* allocate descriptors */
9088 	err = i40e_vsi_setup_tx_resources(vsi);
9089 	if (err)
9090 		goto err_setup_tx;
9091 	err = i40e_vsi_setup_rx_resources(vsi);
9092 	if (err)
9093 		goto err_setup_rx;
9094 
9095 	err = i40e_vsi_configure(vsi);
9096 	if (err)
9097 		goto err_setup_rx;
9098 
9099 	if (vsi->netdev) {
9100 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9101 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9102 		err = i40e_vsi_request_irq(vsi, int_name);
9103 		if (err)
9104 			goto err_setup_rx;
9105 
9106 		/* Notify the stack of the actual queue counts. */
9107 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9108 		if (err)
9109 			goto err_set_queues;
9110 
9111 	} else if (vsi->type == I40E_VSI_FDIR) {
9112 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9113 			 dev_driver_string(&pf->pdev->dev),
9114 			 dev_name(&pf->pdev->dev));
9115 		err = i40e_vsi_request_irq(vsi, int_name);
9116 		if (err)
9117 			goto err_setup_rx;
9118 
9119 	} else {
9120 		err = -EINVAL;
9121 		goto err_setup_rx;
9122 	}
9123 
9124 	err = i40e_up_complete(vsi);
9125 	if (err)
9126 		goto err_up_complete;
9127 
9128 	return 0;
9129 
9130 err_up_complete:
9131 	i40e_down(vsi);
9132 err_set_queues:
9133 	i40e_vsi_free_irq(vsi);
9134 err_setup_rx:
9135 	i40e_vsi_free_rx_resources(vsi);
9136 err_setup_tx:
9137 	i40e_vsi_free_tx_resources(vsi);
9138 	if (vsi == pf->vsi[pf->lan_vsi])
9139 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9140 
9141 	return err;
9142 }
9143 
9144 /**
9145  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9146  * @pf: Pointer to PF
9147  *
9148  * This function destroys the hlist where all the Flow Director
9149  * filters were saved.
9150  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9151 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9152 {
9153 	struct i40e_fdir_filter *filter;
9154 	struct i40e_flex_pit *pit_entry, *tmp;
9155 	struct hlist_node *node2;
9156 
9157 	hlist_for_each_entry_safe(filter, node2,
9158 				  &pf->fdir_filter_list, fdir_node) {
9159 		hlist_del(&filter->fdir_node);
9160 		kfree(filter);
9161 	}
9162 
9163 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9164 		list_del(&pit_entry->list);
9165 		kfree(pit_entry);
9166 	}
9167 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9168 
9169 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9170 		list_del(&pit_entry->list);
9171 		kfree(pit_entry);
9172 	}
9173 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9174 
9175 	pf->fdir_pf_active_filters = 0;
9176 	i40e_reset_fdir_filter_cnt(pf);
9177 
9178 	/* Reprogram the default input set for TCP/IPv4 */
9179 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9180 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9181 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9182 
9183 	/* Reprogram the default input set for TCP/IPv6 */
9184 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9185 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9186 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9187 
9188 	/* Reprogram the default input set for UDP/IPv4 */
9189 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9190 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9191 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9192 
9193 	/* Reprogram the default input set for UDP/IPv6 */
9194 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9195 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9196 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9197 
9198 	/* Reprogram the default input set for SCTP/IPv4 */
9199 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9200 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9201 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9202 
9203 	/* Reprogram the default input set for SCTP/IPv6 */
9204 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9205 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9206 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9207 
9208 	/* Reprogram the default input set for Other/IPv4 */
9209 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9210 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9211 
9212 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9213 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9214 
9215 	/* Reprogram the default input set for Other/IPv6 */
9216 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9217 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9218 
9219 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9220 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9221 }
9222 
9223 /**
9224  * i40e_cloud_filter_exit - Cleans up the cloud filters
9225  * @pf: Pointer to PF
9226  *
9227  * This function destroys the hlist where all the cloud filters
9228  * were saved.
9229  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9230 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9231 {
9232 	struct i40e_cloud_filter *cfilter;
9233 	struct hlist_node *node;
9234 
9235 	hlist_for_each_entry_safe(cfilter, node,
9236 				  &pf->cloud_filter_list, cloud_node) {
9237 		hlist_del(&cfilter->cloud_node);
9238 		kfree(cfilter);
9239 	}
9240 	pf->num_cloud_filters = 0;
9241 
9242 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9243 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9244 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9245 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9246 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9247 	}
9248 }
9249 
9250 /**
9251  * i40e_close - Disables a network interface
9252  * @netdev: network interface device structure
9253  *
9254  * The close entry point is called when an interface is de-activated
9255  * by the OS.  The hardware is still under the driver's control, but
9256  * this netdev interface is disabled.
9257  *
9258  * Returns 0, this is not allowed to fail
9259  **/
i40e_close(struct net_device * netdev)9260 int i40e_close(struct net_device *netdev)
9261 {
9262 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9263 	struct i40e_vsi *vsi = np->vsi;
9264 
9265 	i40e_vsi_close(vsi);
9266 
9267 	return 0;
9268 }
9269 
9270 /**
9271  * i40e_do_reset - Start a PF or Core Reset sequence
9272  * @pf: board private structure
9273  * @reset_flags: which reset is requested
9274  * @lock_acquired: indicates whether or not the lock has been acquired
9275  * before this function was called.
9276  *
9277  * The essential difference in resets is that the PF Reset
9278  * doesn't clear the packet buffers, doesn't reset the PE
9279  * firmware, and doesn't bother the other PFs on the chip.
9280  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9281 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9282 {
9283 	u32 val;
9284 
9285 	/* do the biggest reset indicated */
9286 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9287 
9288 		/* Request a Global Reset
9289 		 *
9290 		 * This will start the chip's countdown to the actual full
9291 		 * chip reset event, and a warning interrupt to be sent
9292 		 * to all PFs, including the requestor.  Our handler
9293 		 * for the warning interrupt will deal with the shutdown
9294 		 * and recovery of the switch setup.
9295 		 */
9296 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9297 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9298 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9299 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9300 
9301 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9302 
9303 		/* Request a Core Reset
9304 		 *
9305 		 * Same as Global Reset, except does *not* include the MAC/PHY
9306 		 */
9307 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9308 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9309 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9310 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9311 		i40e_flush(&pf->hw);
9312 
9313 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9314 
9315 		/* Request a PF Reset
9316 		 *
9317 		 * Resets only the PF-specific registers
9318 		 *
9319 		 * This goes directly to the tear-down and rebuild of
9320 		 * the switch, since we need to do all the recovery as
9321 		 * for the Core Reset.
9322 		 */
9323 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9324 		i40e_handle_reset_warning(pf, lock_acquired);
9325 
9326 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9327 		/* Request a PF Reset
9328 		 *
9329 		 * Resets PF and reinitializes PFs VSI.
9330 		 */
9331 		i40e_prep_for_reset(pf);
9332 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9333 		dev_info(&pf->pdev->dev,
9334 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9335 			 "FW LLDP is disabled\n" :
9336 			 "FW LLDP is enabled\n");
9337 
9338 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9339 		int v;
9340 
9341 		/* Find the VSI(s) that requested a re-init */
9342 		dev_info(&pf->pdev->dev,
9343 			 "VSI reinit requested\n");
9344 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9345 			struct i40e_vsi *vsi = pf->vsi[v];
9346 
9347 			if (vsi != NULL &&
9348 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9349 					       vsi->state))
9350 				i40e_vsi_reinit_locked(pf->vsi[v]);
9351 		}
9352 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9353 		int v;
9354 
9355 		/* Find the VSI(s) that needs to be brought down */
9356 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9357 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9358 			struct i40e_vsi *vsi = pf->vsi[v];
9359 
9360 			if (vsi != NULL &&
9361 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9362 					       vsi->state)) {
9363 				set_bit(__I40E_VSI_DOWN, vsi->state);
9364 				i40e_down(vsi);
9365 			}
9366 		}
9367 	} else {
9368 		dev_info(&pf->pdev->dev,
9369 			 "bad reset request 0x%08x\n", reset_flags);
9370 	}
9371 }
9372 
9373 #ifdef CONFIG_I40E_DCB
9374 /**
9375  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9376  * @pf: board private structure
9377  * @old_cfg: current DCB config
9378  * @new_cfg: new DCB config
9379  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9380 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9381 			    struct i40e_dcbx_config *old_cfg,
9382 			    struct i40e_dcbx_config *new_cfg)
9383 {
9384 	bool need_reconfig = false;
9385 
9386 	/* Check if ETS configuration has changed */
9387 	if (memcmp(&new_cfg->etscfg,
9388 		   &old_cfg->etscfg,
9389 		   sizeof(new_cfg->etscfg))) {
9390 		/* If Priority Table has changed reconfig is needed */
9391 		if (memcmp(&new_cfg->etscfg.prioritytable,
9392 			   &old_cfg->etscfg.prioritytable,
9393 			   sizeof(new_cfg->etscfg.prioritytable))) {
9394 			need_reconfig = true;
9395 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9396 		}
9397 
9398 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9399 			   &old_cfg->etscfg.tcbwtable,
9400 			   sizeof(new_cfg->etscfg.tcbwtable)))
9401 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9402 
9403 		if (memcmp(&new_cfg->etscfg.tsatable,
9404 			   &old_cfg->etscfg.tsatable,
9405 			   sizeof(new_cfg->etscfg.tsatable)))
9406 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9407 	}
9408 
9409 	/* Check if PFC configuration has changed */
9410 	if (memcmp(&new_cfg->pfc,
9411 		   &old_cfg->pfc,
9412 		   sizeof(new_cfg->pfc))) {
9413 		need_reconfig = true;
9414 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9415 	}
9416 
9417 	/* Check if APP Table has changed */
9418 	if (memcmp(&new_cfg->app,
9419 		   &old_cfg->app,
9420 		   sizeof(new_cfg->app))) {
9421 		need_reconfig = true;
9422 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9423 	}
9424 
9425 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9426 	return need_reconfig;
9427 }
9428 
9429 /**
9430  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9431  * @pf: board private structure
9432  * @e: event info posted on ARQ
9433  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9434 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9435 				  struct i40e_arq_event_info *e)
9436 {
9437 	struct i40e_aqc_lldp_get_mib *mib =
9438 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9439 	struct i40e_hw *hw = &pf->hw;
9440 	struct i40e_dcbx_config tmp_dcbx_cfg;
9441 	bool need_reconfig = false;
9442 	int ret = 0;
9443 	u8 type;
9444 
9445 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9446 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9447 	    (hw->phy.link_info.link_speed &
9448 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9449 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9450 		/* let firmware decide if the DCB should be disabled */
9451 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9452 
9453 	/* Not DCB capable or capability disabled */
9454 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9455 		return ret;
9456 
9457 	/* Ignore if event is not for Nearest Bridge */
9458 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9459 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9460 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9461 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9462 		return ret;
9463 
9464 	/* Check MIB Type and return if event for Remote MIB update */
9465 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9466 	dev_dbg(&pf->pdev->dev,
9467 		"LLDP event mib type %s\n", type ? "remote" : "local");
9468 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9469 		/* Update the remote cached instance and return */
9470 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9471 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9472 				&hw->remote_dcbx_config);
9473 		goto exit;
9474 	}
9475 
9476 	/* Store the old configuration */
9477 	tmp_dcbx_cfg = hw->local_dcbx_config;
9478 
9479 	/* Reset the old DCBx configuration data */
9480 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9481 	/* Get updated DCBX data from firmware */
9482 	ret = i40e_get_dcb_config(&pf->hw);
9483 	if (ret) {
9484 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9485 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9486 		    (hw->phy.link_info.link_speed &
9487 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9488 			dev_warn(&pf->pdev->dev,
9489 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9490 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9491 		} else {
9492 			dev_info(&pf->pdev->dev,
9493 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9494 				 ERR_PTR(ret),
9495 				 i40e_aq_str(&pf->hw,
9496 					     pf->hw.aq.asq_last_status));
9497 		}
9498 		goto exit;
9499 	}
9500 
9501 	/* No change detected in DCBX configs */
9502 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9503 		    sizeof(tmp_dcbx_cfg))) {
9504 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9505 		goto exit;
9506 	}
9507 
9508 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9509 					       &hw->local_dcbx_config);
9510 
9511 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9512 
9513 	if (!need_reconfig)
9514 		goto exit;
9515 
9516 	/* Enable DCB tagging only when more than one TC */
9517 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9518 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9519 	else
9520 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9521 
9522 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9523 	/* Reconfiguration needed quiesce all VSIs */
9524 	i40e_pf_quiesce_all_vsi(pf);
9525 
9526 	/* Changes in configuration update VEB/VSI */
9527 	i40e_dcb_reconfigure(pf);
9528 
9529 	ret = i40e_resume_port_tx(pf);
9530 
9531 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9532 	/* In case of error no point in resuming VSIs */
9533 	if (ret)
9534 		goto exit;
9535 
9536 	/* Wait for the PF's queues to be disabled */
9537 	ret = i40e_pf_wait_queues_disabled(pf);
9538 	if (ret) {
9539 		/* Schedule PF reset to recover */
9540 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9541 		i40e_service_event_schedule(pf);
9542 	} else {
9543 		i40e_pf_unquiesce_all_vsi(pf);
9544 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9545 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9546 	}
9547 
9548 exit:
9549 	return ret;
9550 }
9551 #endif /* CONFIG_I40E_DCB */
9552 
9553 /**
9554  * i40e_do_reset_safe - Protected reset path for userland calls.
9555  * @pf: board private structure
9556  * @reset_flags: which reset is requested
9557  *
9558  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9559 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9560 {
9561 	rtnl_lock();
9562 	i40e_do_reset(pf, reset_flags, true);
9563 	rtnl_unlock();
9564 }
9565 
9566 /**
9567  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9568  * @pf: board private structure
9569  * @e: event info posted on ARQ
9570  *
9571  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9572  * and VF queues
9573  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9574 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9575 					   struct i40e_arq_event_info *e)
9576 {
9577 	struct i40e_aqc_lan_overflow *data =
9578 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9579 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9580 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9581 	struct i40e_hw *hw = &pf->hw;
9582 	struct i40e_vf *vf;
9583 	u16 vf_id;
9584 
9585 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9586 		queue, qtx_ctl);
9587 
9588 	/* Queue belongs to VF, find the VF and issue VF reset */
9589 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9590 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9591 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9592 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9593 		vf_id -= hw->func_caps.vf_base_id;
9594 		vf = &pf->vf[vf_id];
9595 		i40e_vc_notify_vf_reset(vf);
9596 		/* Allow VF to process pending reset notification */
9597 		msleep(20);
9598 		i40e_reset_vf(vf, false);
9599 	}
9600 }
9601 
9602 /**
9603  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9604  * @pf: board private structure
9605  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9606 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9607 {
9608 	u32 val, fcnt_prog;
9609 
9610 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9611 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9612 	return fcnt_prog;
9613 }
9614 
9615 /**
9616  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9617  * @pf: board private structure
9618  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9619 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9620 {
9621 	u32 val, fcnt_prog;
9622 
9623 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9624 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9625 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9626 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9627 	return fcnt_prog;
9628 }
9629 
9630 /**
9631  * i40e_get_global_fd_count - Get total FD filters programmed on device
9632  * @pf: board private structure
9633  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9634 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9635 {
9636 	u32 val, fcnt_prog;
9637 
9638 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9639 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9640 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9641 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9642 	return fcnt_prog;
9643 }
9644 
9645 /**
9646  * i40e_reenable_fdir_sb - Restore FDir SB capability
9647  * @pf: board private structure
9648  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9649 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9650 {
9651 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9652 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9653 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9654 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9655 }
9656 
9657 /**
9658  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9659  * @pf: board private structure
9660  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9661 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9662 {
9663 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9664 		/* ATR uses the same filtering logic as SB rules. It only
9665 		 * functions properly if the input set mask is at the default
9666 		 * settings. It is safe to restore the default input set
9667 		 * because there are no active TCPv4 filter rules.
9668 		 */
9669 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9670 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9671 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9672 
9673 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9674 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9675 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9676 	}
9677 }
9678 
9679 /**
9680  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9681  * @pf: board private structure
9682  * @filter: FDir filter to remove
9683  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9684 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9685 				       struct i40e_fdir_filter *filter)
9686 {
9687 	/* Update counters */
9688 	pf->fdir_pf_active_filters--;
9689 	pf->fd_inv = 0;
9690 
9691 	switch (filter->flow_type) {
9692 	case TCP_V4_FLOW:
9693 		pf->fd_tcp4_filter_cnt--;
9694 		break;
9695 	case UDP_V4_FLOW:
9696 		pf->fd_udp4_filter_cnt--;
9697 		break;
9698 	case SCTP_V4_FLOW:
9699 		pf->fd_sctp4_filter_cnt--;
9700 		break;
9701 	case TCP_V6_FLOW:
9702 		pf->fd_tcp6_filter_cnt--;
9703 		break;
9704 	case UDP_V6_FLOW:
9705 		pf->fd_udp6_filter_cnt--;
9706 		break;
9707 	case SCTP_V6_FLOW:
9708 		pf->fd_udp6_filter_cnt--;
9709 		break;
9710 	case IP_USER_FLOW:
9711 		switch (filter->ipl4_proto) {
9712 		case IPPROTO_TCP:
9713 			pf->fd_tcp4_filter_cnt--;
9714 			break;
9715 		case IPPROTO_UDP:
9716 			pf->fd_udp4_filter_cnt--;
9717 			break;
9718 		case IPPROTO_SCTP:
9719 			pf->fd_sctp4_filter_cnt--;
9720 			break;
9721 		case IPPROTO_IP:
9722 			pf->fd_ip4_filter_cnt--;
9723 			break;
9724 		}
9725 		break;
9726 	case IPV6_USER_FLOW:
9727 		switch (filter->ipl4_proto) {
9728 		case IPPROTO_TCP:
9729 			pf->fd_tcp6_filter_cnt--;
9730 			break;
9731 		case IPPROTO_UDP:
9732 			pf->fd_udp6_filter_cnt--;
9733 			break;
9734 		case IPPROTO_SCTP:
9735 			pf->fd_sctp6_filter_cnt--;
9736 			break;
9737 		case IPPROTO_IP:
9738 			pf->fd_ip6_filter_cnt--;
9739 			break;
9740 		}
9741 		break;
9742 	}
9743 
9744 	/* Remove the filter from the list and free memory */
9745 	hlist_del(&filter->fdir_node);
9746 	kfree(filter);
9747 }
9748 
9749 /**
9750  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9751  * @pf: board private structure
9752  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9753 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9754 {
9755 	struct i40e_fdir_filter *filter;
9756 	u32 fcnt_prog, fcnt_avail;
9757 	struct hlist_node *node;
9758 
9759 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9760 		return;
9761 
9762 	/* Check if we have enough room to re-enable FDir SB capability. */
9763 	fcnt_prog = i40e_get_global_fd_count(pf);
9764 	fcnt_avail = pf->fdir_pf_filter_count;
9765 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9766 	    (pf->fd_add_err == 0) ||
9767 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9768 		i40e_reenable_fdir_sb(pf);
9769 
9770 	/* We should wait for even more space before re-enabling ATR.
9771 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9772 	 * rules active.
9773 	 */
9774 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9775 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9776 		i40e_reenable_fdir_atr(pf);
9777 
9778 	/* if hw had a problem adding a filter, delete it */
9779 	if (pf->fd_inv > 0) {
9780 		hlist_for_each_entry_safe(filter, node,
9781 					  &pf->fdir_filter_list, fdir_node)
9782 			if (filter->fd_id == pf->fd_inv)
9783 				i40e_delete_invalid_filter(pf, filter);
9784 	}
9785 }
9786 
9787 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9788 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9789 /**
9790  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9791  * @pf: board private structure
9792  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9793 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9794 {
9795 	unsigned long min_flush_time;
9796 	int flush_wait_retry = 50;
9797 	bool disable_atr = false;
9798 	int fd_room;
9799 	int reg;
9800 
9801 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9802 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9803 		return;
9804 
9805 	/* If the flush is happening too quick and we have mostly SB rules we
9806 	 * should not re-enable ATR for some time.
9807 	 */
9808 	min_flush_time = pf->fd_flush_timestamp +
9809 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9810 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9811 
9812 	if (!(time_after(jiffies, min_flush_time)) &&
9813 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9814 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9815 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9816 		disable_atr = true;
9817 	}
9818 
9819 	pf->fd_flush_timestamp = jiffies;
9820 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9821 	/* flush all filters */
9822 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9823 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9824 	i40e_flush(&pf->hw);
9825 	pf->fd_flush_cnt++;
9826 	pf->fd_add_err = 0;
9827 	do {
9828 		/* Check FD flush status every 5-6msec */
9829 		usleep_range(5000, 6000);
9830 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9831 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9832 			break;
9833 	} while (flush_wait_retry--);
9834 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9835 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9836 	} else {
9837 		/* replay sideband filters */
9838 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9839 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9840 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9841 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9842 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9843 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9844 	}
9845 }
9846 
9847 /**
9848  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9849  * @pf: board private structure
9850  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9851 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9852 {
9853 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9854 }
9855 
9856 /**
9857  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9858  * @pf: board private structure
9859  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9860 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9861 {
9862 
9863 	/* if interface is down do nothing */
9864 	if (test_bit(__I40E_DOWN, pf->state))
9865 		return;
9866 
9867 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9868 		i40e_fdir_flush_and_replay(pf);
9869 
9870 	i40e_fdir_check_and_reenable(pf);
9871 
9872 }
9873 
9874 /**
9875  * i40e_vsi_link_event - notify VSI of a link event
9876  * @vsi: vsi to be notified
9877  * @link_up: link up or down
9878  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9879 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9880 {
9881 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9882 		return;
9883 
9884 	switch (vsi->type) {
9885 	case I40E_VSI_MAIN:
9886 		if (!vsi->netdev || !vsi->netdev_registered)
9887 			break;
9888 
9889 		if (link_up) {
9890 			netif_carrier_on(vsi->netdev);
9891 			netif_tx_wake_all_queues(vsi->netdev);
9892 		} else {
9893 			netif_carrier_off(vsi->netdev);
9894 			netif_tx_stop_all_queues(vsi->netdev);
9895 		}
9896 		break;
9897 
9898 	case I40E_VSI_SRIOV:
9899 	case I40E_VSI_VMDQ2:
9900 	case I40E_VSI_CTRL:
9901 	case I40E_VSI_IWARP:
9902 	case I40E_VSI_MIRROR:
9903 	default:
9904 		/* there is no notification for other VSIs */
9905 		break;
9906 	}
9907 }
9908 
9909 /**
9910  * i40e_veb_link_event - notify elements on the veb of a link event
9911  * @veb: veb to be notified
9912  * @link_up: link up or down
9913  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9914 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9915 {
9916 	struct i40e_pf *pf;
9917 	int i;
9918 
9919 	if (!veb || !veb->pf)
9920 		return;
9921 	pf = veb->pf;
9922 
9923 	/* depth first... */
9924 	for (i = 0; i < I40E_MAX_VEB; i++)
9925 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9926 			i40e_veb_link_event(pf->veb[i], link_up);
9927 
9928 	/* ... now the local VSIs */
9929 	for (i = 0; i < pf->num_alloc_vsi; i++)
9930 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9931 			i40e_vsi_link_event(pf->vsi[i], link_up);
9932 }
9933 
9934 /**
9935  * i40e_link_event - Update netif_carrier status
9936  * @pf: board private structure
9937  **/
i40e_link_event(struct i40e_pf * pf)9938 static void i40e_link_event(struct i40e_pf *pf)
9939 {
9940 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9941 	u8 new_link_speed, old_link_speed;
9942 	bool new_link, old_link;
9943 	int status;
9944 #ifdef CONFIG_I40E_DCB
9945 	int err;
9946 #endif /* CONFIG_I40E_DCB */
9947 
9948 	/* set this to force the get_link_status call to refresh state */
9949 	pf->hw.phy.get_link_info = true;
9950 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9951 	status = i40e_get_link_status(&pf->hw, &new_link);
9952 
9953 	/* On success, disable temp link polling */
9954 	if (status == 0) {
9955 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9956 	} else {
9957 		/* Enable link polling temporarily until i40e_get_link_status
9958 		 * returns 0
9959 		 */
9960 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9961 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9962 			status);
9963 		return;
9964 	}
9965 
9966 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9967 	new_link_speed = pf->hw.phy.link_info.link_speed;
9968 
9969 	if (new_link == old_link &&
9970 	    new_link_speed == old_link_speed &&
9971 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9972 	     new_link == netif_carrier_ok(vsi->netdev)))
9973 		return;
9974 
9975 	i40e_print_link_message(vsi, new_link);
9976 
9977 	/* Notify the base of the switch tree connected to
9978 	 * the link.  Floating VEBs are not notified.
9979 	 */
9980 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9981 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9982 	else
9983 		i40e_vsi_link_event(vsi, new_link);
9984 
9985 	if (pf->vf)
9986 		i40e_vc_notify_link_state(pf);
9987 
9988 	if (pf->flags & I40E_FLAG_PTP)
9989 		i40e_ptp_set_increment(pf);
9990 #ifdef CONFIG_I40E_DCB
9991 	if (new_link == old_link)
9992 		return;
9993 	/* Not SW DCB so firmware will take care of default settings */
9994 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9995 		return;
9996 
9997 	/* We cover here only link down, as after link up in case of SW DCB
9998 	 * SW LLDP agent will take care of setting it up
9999 	 */
10000 	if (!new_link) {
10001 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
10002 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
10003 		err = i40e_dcb_sw_default_config(pf);
10004 		if (err) {
10005 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10006 				       I40E_FLAG_DCB_ENABLED);
10007 		} else {
10008 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
10009 				       DCB_CAP_DCBX_VER_IEEE;
10010 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
10011 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
10012 		}
10013 	}
10014 #endif /* CONFIG_I40E_DCB */
10015 }
10016 
10017 /**
10018  * i40e_watchdog_subtask - periodic checks not using event driven response
10019  * @pf: board private structure
10020  **/
i40e_watchdog_subtask(struct i40e_pf * pf)10021 static void i40e_watchdog_subtask(struct i40e_pf *pf)
10022 {
10023 	int i;
10024 
10025 	/* if interface is down do nothing */
10026 	if (test_bit(__I40E_DOWN, pf->state) ||
10027 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10028 		return;
10029 
10030 	/* make sure we don't do these things too often */
10031 	if (time_before(jiffies, (pf->service_timer_previous +
10032 				  pf->service_timer_period)))
10033 		return;
10034 	pf->service_timer_previous = jiffies;
10035 
10036 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
10037 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10038 		i40e_link_event(pf);
10039 
10040 	/* Update the stats for active netdevs so the network stack
10041 	 * can look at updated numbers whenever it cares to
10042 	 */
10043 	for (i = 0; i < pf->num_alloc_vsi; i++)
10044 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10045 			i40e_update_stats(pf->vsi[i]);
10046 
10047 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10048 		/* Update the stats for the active switching components */
10049 		for (i = 0; i < I40E_MAX_VEB; i++)
10050 			if (pf->veb[i])
10051 				i40e_update_veb_stats(pf->veb[i]);
10052 	}
10053 
10054 	i40e_ptp_rx_hang(pf);
10055 	i40e_ptp_tx_hang(pf);
10056 }
10057 
10058 /**
10059  * i40e_reset_subtask - Set up for resetting the device and driver
10060  * @pf: board private structure
10061  **/
i40e_reset_subtask(struct i40e_pf * pf)10062 static void i40e_reset_subtask(struct i40e_pf *pf)
10063 {
10064 	u32 reset_flags = 0;
10065 
10066 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10067 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10068 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10069 	}
10070 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10071 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10072 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10073 	}
10074 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10075 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10076 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10077 	}
10078 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10079 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10080 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10081 	}
10082 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10083 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10084 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10085 	}
10086 
10087 	/* If there's a recovery already waiting, it takes
10088 	 * precedence before starting a new reset sequence.
10089 	 */
10090 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10091 		i40e_prep_for_reset(pf);
10092 		i40e_reset(pf);
10093 		i40e_rebuild(pf, false, false);
10094 	}
10095 
10096 	/* If we're already down or resetting, just bail */
10097 	if (reset_flags &&
10098 	    !test_bit(__I40E_DOWN, pf->state) &&
10099 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10100 		i40e_do_reset(pf, reset_flags, false);
10101 	}
10102 }
10103 
10104 /**
10105  * i40e_handle_link_event - Handle link event
10106  * @pf: board private structure
10107  * @e: event info posted on ARQ
10108  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10109 static void i40e_handle_link_event(struct i40e_pf *pf,
10110 				   struct i40e_arq_event_info *e)
10111 {
10112 	struct i40e_aqc_get_link_status *status =
10113 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10114 
10115 	/* Do a new status request to re-enable LSE reporting
10116 	 * and load new status information into the hw struct
10117 	 * This completely ignores any state information
10118 	 * in the ARQ event info, instead choosing to always
10119 	 * issue the AQ update link status command.
10120 	 */
10121 	i40e_link_event(pf);
10122 
10123 	/* Check if module meets thermal requirements */
10124 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10125 		dev_err(&pf->pdev->dev,
10126 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10127 		dev_err(&pf->pdev->dev,
10128 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10129 	} else {
10130 		/* check for unqualified module, if link is down, suppress
10131 		 * the message if link was forced to be down.
10132 		 */
10133 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10134 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10135 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10136 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10137 			dev_err(&pf->pdev->dev,
10138 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10139 			dev_err(&pf->pdev->dev,
10140 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10141 		}
10142 	}
10143 }
10144 
10145 /**
10146  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10147  * @pf: board private structure
10148  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10149 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10150 {
10151 	struct i40e_arq_event_info event;
10152 	struct i40e_hw *hw = &pf->hw;
10153 	u16 pending, i = 0;
10154 	u16 opcode;
10155 	u32 oldval;
10156 	int ret;
10157 	u32 val;
10158 
10159 	/* Do not run clean AQ when PF reset fails */
10160 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10161 		return;
10162 
10163 	/* check for error indications */
10164 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10165 	oldval = val;
10166 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10167 		if (hw->debug_mask & I40E_DEBUG_AQ)
10168 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10169 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10170 	}
10171 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10172 		if (hw->debug_mask & I40E_DEBUG_AQ)
10173 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10174 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10175 		pf->arq_overflows++;
10176 	}
10177 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10178 		if (hw->debug_mask & I40E_DEBUG_AQ)
10179 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10180 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10181 	}
10182 	if (oldval != val)
10183 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10184 
10185 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10186 	oldval = val;
10187 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10188 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10189 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10190 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10191 	}
10192 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10193 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10194 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10195 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10196 	}
10197 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10198 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10199 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10200 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10201 	}
10202 	if (oldval != val)
10203 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10204 
10205 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10206 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10207 	if (!event.msg_buf)
10208 		return;
10209 
10210 	do {
10211 		ret = i40e_clean_arq_element(hw, &event, &pending);
10212 		if (ret == -EALREADY)
10213 			break;
10214 		else if (ret) {
10215 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10216 			break;
10217 		}
10218 
10219 		opcode = le16_to_cpu(event.desc.opcode);
10220 		switch (opcode) {
10221 
10222 		case i40e_aqc_opc_get_link_status:
10223 			rtnl_lock();
10224 			i40e_handle_link_event(pf, &event);
10225 			rtnl_unlock();
10226 			break;
10227 		case i40e_aqc_opc_send_msg_to_pf:
10228 			ret = i40e_vc_process_vf_msg(pf,
10229 					le16_to_cpu(event.desc.retval),
10230 					le32_to_cpu(event.desc.cookie_high),
10231 					le32_to_cpu(event.desc.cookie_low),
10232 					event.msg_buf,
10233 					event.msg_len);
10234 			break;
10235 		case i40e_aqc_opc_lldp_update_mib:
10236 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10237 #ifdef CONFIG_I40E_DCB
10238 			rtnl_lock();
10239 			i40e_handle_lldp_event(pf, &event);
10240 			rtnl_unlock();
10241 #endif /* CONFIG_I40E_DCB */
10242 			break;
10243 		case i40e_aqc_opc_event_lan_overflow:
10244 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10245 			i40e_handle_lan_overflow_event(pf, &event);
10246 			break;
10247 		case i40e_aqc_opc_send_msg_to_peer:
10248 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10249 			break;
10250 		case i40e_aqc_opc_nvm_erase:
10251 		case i40e_aqc_opc_nvm_update:
10252 		case i40e_aqc_opc_oem_post_update:
10253 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10254 				   "ARQ NVM operation 0x%04x completed\n",
10255 				   opcode);
10256 			break;
10257 		default:
10258 			dev_info(&pf->pdev->dev,
10259 				 "ARQ: Unknown event 0x%04x ignored\n",
10260 				 opcode);
10261 			break;
10262 		}
10263 	} while (i++ < pf->adminq_work_limit);
10264 
10265 	if (i < pf->adminq_work_limit)
10266 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10267 
10268 	/* re-enable Admin queue interrupt cause */
10269 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10270 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10271 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10272 	i40e_flush(hw);
10273 
10274 	kfree(event.msg_buf);
10275 }
10276 
10277 /**
10278  * i40e_verify_eeprom - make sure eeprom is good to use
10279  * @pf: board private structure
10280  **/
i40e_verify_eeprom(struct i40e_pf * pf)10281 static void i40e_verify_eeprom(struct i40e_pf *pf)
10282 {
10283 	int err;
10284 
10285 	err = i40e_diag_eeprom_test(&pf->hw);
10286 	if (err) {
10287 		/* retry in case of garbage read */
10288 		err = i40e_diag_eeprom_test(&pf->hw);
10289 		if (err) {
10290 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10291 				 err);
10292 			set_bit(__I40E_BAD_EEPROM, pf->state);
10293 		}
10294 	}
10295 
10296 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10297 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10298 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10299 	}
10300 }
10301 
10302 /**
10303  * i40e_enable_pf_switch_lb
10304  * @pf: pointer to the PF structure
10305  *
10306  * enable switch loop back or die - no point in a return value
10307  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10308 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10309 {
10310 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10311 	struct i40e_vsi_context ctxt;
10312 	int ret;
10313 
10314 	ctxt.seid = pf->main_vsi_seid;
10315 	ctxt.pf_num = pf->hw.pf_id;
10316 	ctxt.vf_num = 0;
10317 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10318 	if (ret) {
10319 		dev_info(&pf->pdev->dev,
10320 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10321 			 ERR_PTR(ret),
10322 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10323 		return;
10324 	}
10325 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10326 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10327 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10328 
10329 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10330 	if (ret) {
10331 		dev_info(&pf->pdev->dev,
10332 			 "update vsi switch failed, err %pe aq_err %s\n",
10333 			 ERR_PTR(ret),
10334 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10335 	}
10336 }
10337 
10338 /**
10339  * i40e_disable_pf_switch_lb
10340  * @pf: pointer to the PF structure
10341  *
10342  * disable switch loop back or die - no point in a return value
10343  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10344 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10345 {
10346 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10347 	struct i40e_vsi_context ctxt;
10348 	int ret;
10349 
10350 	ctxt.seid = pf->main_vsi_seid;
10351 	ctxt.pf_num = pf->hw.pf_id;
10352 	ctxt.vf_num = 0;
10353 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10354 	if (ret) {
10355 		dev_info(&pf->pdev->dev,
10356 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10357 			 ERR_PTR(ret),
10358 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10359 		return;
10360 	}
10361 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10362 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10363 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10364 
10365 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10366 	if (ret) {
10367 		dev_info(&pf->pdev->dev,
10368 			 "update vsi switch failed, err %pe aq_err %s\n",
10369 			 ERR_PTR(ret),
10370 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10371 	}
10372 }
10373 
10374 /**
10375  * i40e_config_bridge_mode - Configure the HW bridge mode
10376  * @veb: pointer to the bridge instance
10377  *
10378  * Configure the loop back mode for the LAN VSI that is downlink to the
10379  * specified HW bridge instance. It is expected this function is called
10380  * when a new HW bridge is instantiated.
10381  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10382 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10383 {
10384 	struct i40e_pf *pf = veb->pf;
10385 
10386 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10387 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10388 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10389 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10390 		i40e_disable_pf_switch_lb(pf);
10391 	else
10392 		i40e_enable_pf_switch_lb(pf);
10393 }
10394 
10395 /**
10396  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10397  * @veb: pointer to the VEB instance
10398  *
10399  * This is a recursive function that first builds the attached VSIs then
10400  * recurses in to build the next layer of VEB.  We track the connections
10401  * through our own index numbers because the seid's from the HW could
10402  * change across the reset.
10403  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10404 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10405 {
10406 	struct i40e_vsi *ctl_vsi = NULL;
10407 	struct i40e_pf *pf = veb->pf;
10408 	int v, veb_idx;
10409 	int ret;
10410 
10411 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10412 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10413 		if (pf->vsi[v] &&
10414 		    pf->vsi[v]->veb_idx == veb->idx &&
10415 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10416 			ctl_vsi = pf->vsi[v];
10417 			break;
10418 		}
10419 	}
10420 	if (!ctl_vsi) {
10421 		dev_info(&pf->pdev->dev,
10422 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10423 		ret = -ENOENT;
10424 		goto end_reconstitute;
10425 	}
10426 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10427 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10428 	ret = i40e_add_vsi(ctl_vsi);
10429 	if (ret) {
10430 		dev_info(&pf->pdev->dev,
10431 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10432 			 veb->idx, ret);
10433 		goto end_reconstitute;
10434 	}
10435 	i40e_vsi_reset_stats(ctl_vsi);
10436 
10437 	/* create the VEB in the switch and move the VSI onto the VEB */
10438 	ret = i40e_add_veb(veb, ctl_vsi);
10439 	if (ret)
10440 		goto end_reconstitute;
10441 
10442 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10443 		veb->bridge_mode = BRIDGE_MODE_VEB;
10444 	else
10445 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10446 	i40e_config_bridge_mode(veb);
10447 
10448 	/* create the remaining VSIs attached to this VEB */
10449 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10450 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10451 			continue;
10452 
10453 		if (pf->vsi[v]->veb_idx == veb->idx) {
10454 			struct i40e_vsi *vsi = pf->vsi[v];
10455 
10456 			vsi->uplink_seid = veb->seid;
10457 			ret = i40e_add_vsi(vsi);
10458 			if (ret) {
10459 				dev_info(&pf->pdev->dev,
10460 					 "rebuild of vsi_idx %d failed: %d\n",
10461 					 v, ret);
10462 				goto end_reconstitute;
10463 			}
10464 			i40e_vsi_reset_stats(vsi);
10465 		}
10466 	}
10467 
10468 	/* create any VEBs attached to this VEB - RECURSION */
10469 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10470 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10471 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10472 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10473 			if (ret)
10474 				break;
10475 		}
10476 	}
10477 
10478 end_reconstitute:
10479 	return ret;
10480 }
10481 
10482 /**
10483  * i40e_get_capabilities - get info about the HW
10484  * @pf: the PF struct
10485  * @list_type: AQ capability to be queried
10486  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10487 static int i40e_get_capabilities(struct i40e_pf *pf,
10488 				 enum i40e_admin_queue_opc list_type)
10489 {
10490 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10491 	u16 data_size;
10492 	int buf_len;
10493 	int err;
10494 
10495 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10496 	do {
10497 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10498 		if (!cap_buf)
10499 			return -ENOMEM;
10500 
10501 		/* this loads the data into the hw struct for us */
10502 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10503 						    &data_size, list_type,
10504 						    NULL);
10505 		/* data loaded, buffer no longer needed */
10506 		kfree(cap_buf);
10507 
10508 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10509 			/* retry with a larger buffer */
10510 			buf_len = data_size;
10511 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10512 			dev_info(&pf->pdev->dev,
10513 				 "capability discovery failed, err %pe aq_err %s\n",
10514 				 ERR_PTR(err),
10515 				 i40e_aq_str(&pf->hw,
10516 					     pf->hw.aq.asq_last_status));
10517 			return -ENODEV;
10518 		}
10519 	} while (err);
10520 
10521 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10522 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10523 			dev_info(&pf->pdev->dev,
10524 				 "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",
10525 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10526 				 pf->hw.func_caps.num_msix_vectors,
10527 				 pf->hw.func_caps.num_msix_vectors_vf,
10528 				 pf->hw.func_caps.fd_filters_guaranteed,
10529 				 pf->hw.func_caps.fd_filters_best_effort,
10530 				 pf->hw.func_caps.num_tx_qp,
10531 				 pf->hw.func_caps.num_vsis);
10532 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10533 			dev_info(&pf->pdev->dev,
10534 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10535 				 pf->hw.dev_caps.switch_mode,
10536 				 pf->hw.dev_caps.valid_functions);
10537 			dev_info(&pf->pdev->dev,
10538 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10539 				 pf->hw.dev_caps.sr_iov_1_1,
10540 				 pf->hw.dev_caps.num_vfs);
10541 			dev_info(&pf->pdev->dev,
10542 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10543 				 pf->hw.dev_caps.num_vsis,
10544 				 pf->hw.dev_caps.num_rx_qp,
10545 				 pf->hw.dev_caps.num_tx_qp);
10546 		}
10547 	}
10548 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10549 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10550 		       + pf->hw.func_caps.num_vfs)
10551 		if (pf->hw.revision_id == 0 &&
10552 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10553 			dev_info(&pf->pdev->dev,
10554 				 "got num_vsis %d, setting num_vsis to %d\n",
10555 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10556 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10557 		}
10558 	}
10559 	return 0;
10560 }
10561 
10562 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10563 
10564 /**
10565  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10566  * @pf: board private structure
10567  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10568 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10569 {
10570 	struct i40e_vsi *vsi;
10571 
10572 	/* quick workaround for an NVM issue that leaves a critical register
10573 	 * uninitialized
10574 	 */
10575 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10576 		static const u32 hkey[] = {
10577 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10578 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10579 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10580 			0x95b3a76d};
10581 		int i;
10582 
10583 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10584 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10585 	}
10586 
10587 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10588 		return;
10589 
10590 	/* find existing VSI and see if it needs configuring */
10591 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10592 
10593 	/* create a new VSI if none exists */
10594 	if (!vsi) {
10595 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10596 				     pf->vsi[pf->lan_vsi]->seid, 0);
10597 		if (!vsi) {
10598 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10599 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10600 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10601 			return;
10602 		}
10603 	}
10604 
10605 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10606 }
10607 
10608 /**
10609  * i40e_fdir_teardown - release the Flow Director resources
10610  * @pf: board private structure
10611  **/
i40e_fdir_teardown(struct i40e_pf * pf)10612 static void i40e_fdir_teardown(struct i40e_pf *pf)
10613 {
10614 	struct i40e_vsi *vsi;
10615 
10616 	i40e_fdir_filter_exit(pf);
10617 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10618 	if (vsi)
10619 		i40e_vsi_release(vsi);
10620 }
10621 
10622 /**
10623  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10624  * @vsi: PF main vsi
10625  * @seid: seid of main or channel VSIs
10626  *
10627  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10628  * existed before reset
10629  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10630 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10631 {
10632 	struct i40e_cloud_filter *cfilter;
10633 	struct i40e_pf *pf = vsi->back;
10634 	struct hlist_node *node;
10635 	int ret;
10636 
10637 	/* Add cloud filters back if they exist */
10638 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10639 				  cloud_node) {
10640 		if (cfilter->seid != seid)
10641 			continue;
10642 
10643 		if (cfilter->dst_port)
10644 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10645 								true);
10646 		else
10647 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10648 
10649 		if (ret) {
10650 			dev_dbg(&pf->pdev->dev,
10651 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10652 				ERR_PTR(ret),
10653 				i40e_aq_str(&pf->hw,
10654 					    pf->hw.aq.asq_last_status));
10655 			return ret;
10656 		}
10657 	}
10658 	return 0;
10659 }
10660 
10661 /**
10662  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10663  * @vsi: PF main vsi
10664  *
10665  * Rebuilds channel VSIs if they existed before reset
10666  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10667 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10668 {
10669 	struct i40e_channel *ch, *ch_tmp;
10670 	int ret;
10671 
10672 	if (list_empty(&vsi->ch_list))
10673 		return 0;
10674 
10675 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10676 		if (!ch->initialized)
10677 			break;
10678 		/* Proceed with creation of channel (VMDq2) VSI */
10679 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10680 		if (ret) {
10681 			dev_info(&vsi->back->pdev->dev,
10682 				 "failed to rebuild channels using uplink_seid %u\n",
10683 				 vsi->uplink_seid);
10684 			return ret;
10685 		}
10686 		/* Reconfigure TX queues using QTX_CTL register */
10687 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10688 		if (ret) {
10689 			dev_info(&vsi->back->pdev->dev,
10690 				 "failed to configure TX rings for channel %u\n",
10691 				 ch->seid);
10692 			return ret;
10693 		}
10694 		/* update 'next_base_queue' */
10695 		vsi->next_base_queue = vsi->next_base_queue +
10696 							ch->num_queue_pairs;
10697 		if (ch->max_tx_rate) {
10698 			u64 credits = ch->max_tx_rate;
10699 
10700 			if (i40e_set_bw_limit(vsi, ch->seid,
10701 					      ch->max_tx_rate))
10702 				return -EINVAL;
10703 
10704 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10705 			dev_dbg(&vsi->back->pdev->dev,
10706 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10707 				ch->max_tx_rate,
10708 				credits,
10709 				ch->seid);
10710 		}
10711 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10712 		if (ret) {
10713 			dev_dbg(&vsi->back->pdev->dev,
10714 				"Failed to rebuild cloud filters for channel VSI %u\n",
10715 				ch->seid);
10716 			return ret;
10717 		}
10718 	}
10719 	return 0;
10720 }
10721 
10722 /**
10723  * i40e_clean_xps_state - clean xps state for every tx_ring
10724  * @vsi: ptr to the VSI
10725  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10726 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10727 {
10728 	int i;
10729 
10730 	if (vsi->tx_rings)
10731 		for (i = 0; i < vsi->num_queue_pairs; i++)
10732 			if (vsi->tx_rings[i])
10733 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10734 					  vsi->tx_rings[i]->state);
10735 }
10736 
10737 /**
10738  * i40e_prep_for_reset - prep for the core to reset
10739  * @pf: board private structure
10740  *
10741  * Close up the VFs and other things in prep for PF Reset.
10742   **/
i40e_prep_for_reset(struct i40e_pf * pf)10743 static void i40e_prep_for_reset(struct i40e_pf *pf)
10744 {
10745 	struct i40e_hw *hw = &pf->hw;
10746 	int ret = 0;
10747 	u32 v;
10748 
10749 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10750 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10751 		return;
10752 	if (i40e_check_asq_alive(&pf->hw))
10753 		i40e_vc_notify_reset(pf);
10754 
10755 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10756 
10757 	/* quiesce the VSIs and their queues that are not already DOWN */
10758 	i40e_pf_quiesce_all_vsi(pf);
10759 
10760 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10761 		if (pf->vsi[v]) {
10762 			i40e_clean_xps_state(pf->vsi[v]);
10763 			pf->vsi[v]->seid = 0;
10764 		}
10765 	}
10766 
10767 	i40e_shutdown_adminq(&pf->hw);
10768 
10769 	/* call shutdown HMC */
10770 	if (hw->hmc.hmc_obj) {
10771 		ret = i40e_shutdown_lan_hmc(hw);
10772 		if (ret)
10773 			dev_warn(&pf->pdev->dev,
10774 				 "shutdown_lan_hmc failed: %d\n", ret);
10775 	}
10776 
10777 	/* Save the current PTP time so that we can restore the time after the
10778 	 * reset completes.
10779 	 */
10780 	i40e_ptp_save_hw_time(pf);
10781 }
10782 
10783 /**
10784  * i40e_send_version - update firmware with driver version
10785  * @pf: PF struct
10786  */
i40e_send_version(struct i40e_pf * pf)10787 static void i40e_send_version(struct i40e_pf *pf)
10788 {
10789 	struct i40e_driver_version dv;
10790 
10791 	dv.major_version = 0xff;
10792 	dv.minor_version = 0xff;
10793 	dv.build_version = 0xff;
10794 	dv.subbuild_version = 0;
10795 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10796 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10797 }
10798 
10799 /**
10800  * i40e_get_oem_version - get OEM specific version information
10801  * @hw: pointer to the hardware structure
10802  **/
i40e_get_oem_version(struct i40e_hw * hw)10803 static void i40e_get_oem_version(struct i40e_hw *hw)
10804 {
10805 	u16 block_offset = 0xffff;
10806 	u16 block_length = 0;
10807 	u16 capabilities = 0;
10808 	u16 gen_snap = 0;
10809 	u16 release = 0;
10810 
10811 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10812 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10813 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10814 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10815 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10816 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10817 #define I40E_NVM_OEM_LENGTH			3
10818 
10819 	/* Check if pointer to OEM version block is valid. */
10820 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10821 	if (block_offset == 0xffff)
10822 		return;
10823 
10824 	/* Check if OEM version block has correct length. */
10825 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10826 			   &block_length);
10827 	if (block_length < I40E_NVM_OEM_LENGTH)
10828 		return;
10829 
10830 	/* Check if OEM version format is as expected. */
10831 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10832 			   &capabilities);
10833 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10834 		return;
10835 
10836 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10837 			   &gen_snap);
10838 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10839 			   &release);
10840 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10841 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10842 }
10843 
10844 /**
10845  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10846  * @pf: board private structure
10847  **/
i40e_reset(struct i40e_pf * pf)10848 static int i40e_reset(struct i40e_pf *pf)
10849 {
10850 	struct i40e_hw *hw = &pf->hw;
10851 	int ret;
10852 
10853 	ret = i40e_pf_reset(hw);
10854 	if (ret) {
10855 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10856 		set_bit(__I40E_RESET_FAILED, pf->state);
10857 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10858 	} else {
10859 		pf->pfr_count++;
10860 	}
10861 	return ret;
10862 }
10863 
10864 /**
10865  * i40e_rebuild - rebuild using a saved config
10866  * @pf: board private structure
10867  * @reinit: if the Main VSI needs to re-initialized.
10868  * @lock_acquired: indicates whether or not the lock has been acquired
10869  * before this function was called.
10870  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10871 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10872 {
10873 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10874 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10875 	struct i40e_hw *hw = &pf->hw;
10876 	int ret;
10877 	u32 val;
10878 	int v;
10879 
10880 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10881 	    is_recovery_mode_reported)
10882 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10883 
10884 	if (test_bit(__I40E_DOWN, pf->state) &&
10885 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10886 		goto clear_recovery;
10887 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10888 
10889 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10890 	ret = i40e_init_adminq(&pf->hw);
10891 	if (ret) {
10892 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10893 			 ERR_PTR(ret),
10894 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10895 		goto clear_recovery;
10896 	}
10897 	i40e_get_oem_version(&pf->hw);
10898 
10899 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10900 		/* The following delay is necessary for firmware update. */
10901 		mdelay(1000);
10902 	}
10903 
10904 	/* re-verify the eeprom if we just had an EMP reset */
10905 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10906 		i40e_verify_eeprom(pf);
10907 
10908 	/* if we are going out of or into recovery mode we have to act
10909 	 * accordingly with regard to resources initialization
10910 	 * and deinitialization
10911 	 */
10912 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10913 		if (i40e_get_capabilities(pf,
10914 					  i40e_aqc_opc_list_func_capabilities))
10915 			goto end_unlock;
10916 
10917 		if (is_recovery_mode_reported) {
10918 			/* we're staying in recovery mode so we'll reinitialize
10919 			 * misc vector here
10920 			 */
10921 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10922 				goto end_unlock;
10923 		} else {
10924 			if (!lock_acquired)
10925 				rtnl_lock();
10926 			/* we're going out of recovery mode so we'll free
10927 			 * the IRQ allocated specifically for recovery mode
10928 			 * and restore the interrupt scheme
10929 			 */
10930 			free_irq(pf->pdev->irq, pf);
10931 			i40e_clear_interrupt_scheme(pf);
10932 			if (i40e_restore_interrupt_scheme(pf))
10933 				goto end_unlock;
10934 		}
10935 
10936 		/* tell the firmware that we're starting */
10937 		i40e_send_version(pf);
10938 
10939 		/* bail out in case recovery mode was detected, as there is
10940 		 * no need for further configuration.
10941 		 */
10942 		goto end_unlock;
10943 	}
10944 
10945 	i40e_clear_pxe_mode(hw);
10946 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10947 	if (ret)
10948 		goto end_core_reset;
10949 
10950 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10951 				hw->func_caps.num_rx_qp, 0, 0);
10952 	if (ret) {
10953 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10954 		goto end_core_reset;
10955 	}
10956 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10957 	if (ret) {
10958 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10959 		goto end_core_reset;
10960 	}
10961 
10962 #ifdef CONFIG_I40E_DCB
10963 	/* Enable FW to write a default DCB config on link-up
10964 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10965 	 * is not supported with new link speed
10966 	 */
10967 	if (i40e_is_tc_mqprio_enabled(pf)) {
10968 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10969 	} else {
10970 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10971 		    (hw->phy.link_info.link_speed &
10972 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10973 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10974 			dev_warn(&pf->pdev->dev,
10975 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10976 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10977 		} else {
10978 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10979 			ret = i40e_init_pf_dcb(pf);
10980 			if (ret) {
10981 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10982 					 ret);
10983 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10984 				/* Continue without DCB enabled */
10985 			}
10986 		}
10987 	}
10988 
10989 #endif /* CONFIG_I40E_DCB */
10990 	if (!lock_acquired)
10991 		rtnl_lock();
10992 	ret = i40e_setup_pf_switch(pf, reinit, true);
10993 	if (ret)
10994 		goto end_unlock;
10995 
10996 	/* The driver only wants link up/down and module qualification
10997 	 * reports from firmware.  Note the negative logic.
10998 	 */
10999 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
11000 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
11001 					 I40E_AQ_EVENT_MEDIA_NA |
11002 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
11003 	if (ret)
11004 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
11005 			 ERR_PTR(ret),
11006 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11007 
11008 	/* Rebuild the VSIs and VEBs that existed before reset.
11009 	 * They are still in our local switch element arrays, so only
11010 	 * need to rebuild the switch model in the HW.
11011 	 *
11012 	 * If there were VEBs but the reconstitution failed, we'll try
11013 	 * to recover minimal use by getting the basic PF VSI working.
11014 	 */
11015 	if (vsi->uplink_seid != pf->mac_seid) {
11016 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
11017 		/* find the one VEB connected to the MAC, and find orphans */
11018 		for (v = 0; v < I40E_MAX_VEB; v++) {
11019 			if (!pf->veb[v])
11020 				continue;
11021 
11022 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
11023 			    pf->veb[v]->uplink_seid == 0) {
11024 				ret = i40e_reconstitute_veb(pf->veb[v]);
11025 
11026 				if (!ret)
11027 					continue;
11028 
11029 				/* If Main VEB failed, we're in deep doodoo,
11030 				 * so give up rebuilding the switch and set up
11031 				 * for minimal rebuild of PF VSI.
11032 				 * If orphan failed, we'll report the error
11033 				 * but try to keep going.
11034 				 */
11035 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
11036 					dev_info(&pf->pdev->dev,
11037 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11038 						 ret);
11039 					vsi->uplink_seid = pf->mac_seid;
11040 					break;
11041 				} else if (pf->veb[v]->uplink_seid == 0) {
11042 					dev_info(&pf->pdev->dev,
11043 						 "rebuild of orphan VEB failed: %d\n",
11044 						 ret);
11045 				}
11046 			}
11047 		}
11048 	}
11049 
11050 	if (vsi->uplink_seid == pf->mac_seid) {
11051 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11052 		/* no VEB, so rebuild only the Main VSI */
11053 		ret = i40e_add_vsi(vsi);
11054 		if (ret) {
11055 			dev_info(&pf->pdev->dev,
11056 				 "rebuild of Main VSI failed: %d\n", ret);
11057 			goto end_unlock;
11058 		}
11059 	}
11060 
11061 	if (vsi->mqprio_qopt.max_rate[0]) {
11062 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11063 						  vsi->mqprio_qopt.max_rate[0]);
11064 		u64 credits = 0;
11065 
11066 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11067 		if (ret)
11068 			goto end_unlock;
11069 
11070 		credits = max_tx_rate;
11071 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11072 		dev_dbg(&vsi->back->pdev->dev,
11073 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11074 			max_tx_rate,
11075 			credits,
11076 			vsi->seid);
11077 	}
11078 
11079 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11080 	if (ret)
11081 		goto end_unlock;
11082 
11083 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11084 	 * for this main VSI if they exist
11085 	 */
11086 	ret = i40e_rebuild_channels(vsi);
11087 	if (ret)
11088 		goto end_unlock;
11089 
11090 	/* Reconfigure hardware for allowing smaller MSS in the case
11091 	 * of TSO, so that we avoid the MDD being fired and causing
11092 	 * a reset in the case of small MSS+TSO.
11093 	 */
11094 #define I40E_REG_MSS          0x000E64DC
11095 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11096 #define I40E_64BYTE_MSS       0x400000
11097 	val = rd32(hw, I40E_REG_MSS);
11098 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11099 		val &= ~I40E_REG_MSS_MIN_MASK;
11100 		val |= I40E_64BYTE_MSS;
11101 		wr32(hw, I40E_REG_MSS, val);
11102 	}
11103 
11104 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11105 		msleep(75);
11106 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11107 		if (ret)
11108 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11109 				 ERR_PTR(ret),
11110 				 i40e_aq_str(&pf->hw,
11111 					     pf->hw.aq.asq_last_status));
11112 	}
11113 	/* reinit the misc interrupt */
11114 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11115 		ret = i40e_setup_misc_vector(pf);
11116 		if (ret)
11117 			goto end_unlock;
11118 	}
11119 
11120 	/* Add a filter to drop all Flow control frames from any VSI from being
11121 	 * transmitted. By doing so we stop a malicious VF from sending out
11122 	 * PAUSE or PFC frames and potentially controlling traffic for other
11123 	 * PF/VF VSIs.
11124 	 * The FW can still send Flow control frames if enabled.
11125 	 */
11126 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11127 						       pf->main_vsi_seid);
11128 
11129 	/* restart the VSIs that were rebuilt and running before the reset */
11130 	i40e_pf_unquiesce_all_vsi(pf);
11131 
11132 	/* Release the RTNL lock before we start resetting VFs */
11133 	if (!lock_acquired)
11134 		rtnl_unlock();
11135 
11136 	/* Restore promiscuous settings */
11137 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11138 	if (ret)
11139 		dev_warn(&pf->pdev->dev,
11140 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11141 			 pf->cur_promisc ? "on" : "off",
11142 			 ERR_PTR(ret),
11143 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11144 
11145 	i40e_reset_all_vfs(pf, true);
11146 
11147 	/* tell the firmware that we're starting */
11148 	i40e_send_version(pf);
11149 
11150 	/* We've already released the lock, so don't do it again */
11151 	goto end_core_reset;
11152 
11153 end_unlock:
11154 	if (!lock_acquired)
11155 		rtnl_unlock();
11156 end_core_reset:
11157 	clear_bit(__I40E_RESET_FAILED, pf->state);
11158 clear_recovery:
11159 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11160 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11161 }
11162 
11163 /**
11164  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11165  * @pf: board private structure
11166  * @reinit: if the Main VSI needs to re-initialized.
11167  * @lock_acquired: indicates whether or not the lock has been acquired
11168  * before this function was called.
11169  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11170 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11171 				   bool lock_acquired)
11172 {
11173 	int ret;
11174 
11175 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11176 		return;
11177 	/* Now we wait for GRST to settle out.
11178 	 * We don't have to delete the VEBs or VSIs from the hw switch
11179 	 * because the reset will make them disappear.
11180 	 */
11181 	ret = i40e_reset(pf);
11182 	if (!ret)
11183 		i40e_rebuild(pf, reinit, lock_acquired);
11184 }
11185 
11186 /**
11187  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11188  * @pf: board private structure
11189  *
11190  * Close up the VFs and other things in prep for a Core Reset,
11191  * then get ready to rebuild the world.
11192  * @lock_acquired: indicates whether or not the lock has been acquired
11193  * before this function was called.
11194  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11195 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11196 {
11197 	i40e_prep_for_reset(pf);
11198 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11199 }
11200 
11201 /**
11202  * i40e_handle_mdd_event
11203  * @pf: pointer to the PF structure
11204  *
11205  * Called from the MDD irq handler to identify possibly malicious vfs
11206  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11207 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11208 {
11209 	struct i40e_hw *hw = &pf->hw;
11210 	bool mdd_detected = false;
11211 	struct i40e_vf *vf;
11212 	u32 reg;
11213 	int i;
11214 
11215 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11216 		return;
11217 
11218 	/* find what triggered the MDD event */
11219 	reg = rd32(hw, I40E_GL_MDET_TX);
11220 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11221 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11222 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11223 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11224 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11225 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11226 				I40E_GL_MDET_TX_EVENT_SHIFT;
11227 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11228 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11229 				pf->hw.func_caps.base_queue;
11230 		if (netif_msg_tx_err(pf))
11231 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11232 				 event, queue, pf_num, vf_num);
11233 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11234 		mdd_detected = true;
11235 	}
11236 	reg = rd32(hw, I40E_GL_MDET_RX);
11237 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11238 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11239 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11240 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11241 				I40E_GL_MDET_RX_EVENT_SHIFT;
11242 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11243 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11244 				pf->hw.func_caps.base_queue;
11245 		if (netif_msg_rx_err(pf))
11246 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11247 				 event, queue, func);
11248 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11249 		mdd_detected = true;
11250 	}
11251 
11252 	if (mdd_detected) {
11253 		reg = rd32(hw, I40E_PF_MDET_TX);
11254 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11255 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11256 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11257 		}
11258 		reg = rd32(hw, I40E_PF_MDET_RX);
11259 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11260 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11261 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11262 		}
11263 	}
11264 
11265 	/* see if one of the VFs needs its hand slapped */
11266 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11267 		vf = &(pf->vf[i]);
11268 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11269 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11270 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11271 			vf->num_mdd_events++;
11272 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11273 				 i);
11274 			dev_info(&pf->pdev->dev,
11275 				 "Use PF Control I/F to re-enable the VF\n");
11276 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11277 		}
11278 
11279 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11280 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11281 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11282 			vf->num_mdd_events++;
11283 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11284 				 i);
11285 			dev_info(&pf->pdev->dev,
11286 				 "Use PF Control I/F to re-enable the VF\n");
11287 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11288 		}
11289 	}
11290 
11291 	/* re-enable mdd interrupt cause */
11292 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11293 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11294 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11295 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11296 	i40e_flush(hw);
11297 }
11298 
11299 /**
11300  * i40e_service_task - Run the driver's async subtasks
11301  * @work: pointer to work_struct containing our data
11302  **/
i40e_service_task(struct work_struct * work)11303 static void i40e_service_task(struct work_struct *work)
11304 {
11305 	struct i40e_pf *pf = container_of(work,
11306 					  struct i40e_pf,
11307 					  service_task);
11308 	unsigned long start_time = jiffies;
11309 
11310 	/* don't bother with service tasks if a reset is in progress */
11311 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11312 	    test_bit(__I40E_SUSPENDED, pf->state))
11313 		return;
11314 
11315 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11316 		return;
11317 
11318 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11319 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11320 		i40e_sync_filters_subtask(pf);
11321 		i40e_reset_subtask(pf);
11322 		i40e_handle_mdd_event(pf);
11323 		i40e_vc_process_vflr_event(pf);
11324 		i40e_watchdog_subtask(pf);
11325 		i40e_fdir_reinit_subtask(pf);
11326 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11327 			/* Client subtask will reopen next time through. */
11328 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11329 							   true);
11330 		} else {
11331 			i40e_client_subtask(pf);
11332 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11333 					       pf->state))
11334 				i40e_notify_client_of_l2_param_changes(
11335 								pf->vsi[pf->lan_vsi]);
11336 		}
11337 		i40e_sync_filters_subtask(pf);
11338 	} else {
11339 		i40e_reset_subtask(pf);
11340 	}
11341 
11342 	i40e_clean_adminq_subtask(pf);
11343 
11344 	/* flush memory to make sure state is correct before next watchdog */
11345 	smp_mb__before_atomic();
11346 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11347 
11348 	/* If the tasks have taken longer than one timer cycle or there
11349 	 * is more work to be done, reschedule the service task now
11350 	 * rather than wait for the timer to tick again.
11351 	 */
11352 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11353 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11354 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11355 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11356 		i40e_service_event_schedule(pf);
11357 }
11358 
11359 /**
11360  * i40e_service_timer - timer callback
11361  * @t: timer list pointer
11362  **/
i40e_service_timer(struct timer_list * t)11363 static void i40e_service_timer(struct timer_list *t)
11364 {
11365 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11366 
11367 	mod_timer(&pf->service_timer,
11368 		  round_jiffies(jiffies + pf->service_timer_period));
11369 	i40e_service_event_schedule(pf);
11370 }
11371 
11372 /**
11373  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11374  * @vsi: the VSI being configured
11375  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11376 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11377 {
11378 	struct i40e_pf *pf = vsi->back;
11379 
11380 	switch (vsi->type) {
11381 	case I40E_VSI_MAIN:
11382 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11383 		if (!vsi->num_tx_desc)
11384 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11385 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11386 		if (!vsi->num_rx_desc)
11387 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11388 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11389 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11390 			vsi->num_q_vectors = pf->num_lan_msix;
11391 		else
11392 			vsi->num_q_vectors = 1;
11393 
11394 		break;
11395 
11396 	case I40E_VSI_FDIR:
11397 		vsi->alloc_queue_pairs = 1;
11398 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11399 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11400 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11401 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11402 		vsi->num_q_vectors = pf->num_fdsb_msix;
11403 		break;
11404 
11405 	case I40E_VSI_VMDQ2:
11406 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11407 		if (!vsi->num_tx_desc)
11408 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11409 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11410 		if (!vsi->num_rx_desc)
11411 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11412 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11413 		vsi->num_q_vectors = pf->num_vmdq_msix;
11414 		break;
11415 
11416 	case I40E_VSI_SRIOV:
11417 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11418 		if (!vsi->num_tx_desc)
11419 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11420 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11421 		if (!vsi->num_rx_desc)
11422 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11423 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11424 		break;
11425 
11426 	default:
11427 		WARN_ON(1);
11428 		return -ENODATA;
11429 	}
11430 
11431 	if (is_kdump_kernel()) {
11432 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11433 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11434 	}
11435 
11436 	return 0;
11437 }
11438 
11439 /**
11440  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11441  * @vsi: VSI pointer
11442  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11443  *
11444  * On error: returns error code (negative)
11445  * On success: returns 0
11446  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11447 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11448 {
11449 	struct i40e_ring **next_rings;
11450 	int size;
11451 	int ret = 0;
11452 
11453 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11454 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11455 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11456 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11457 	if (!vsi->tx_rings)
11458 		return -ENOMEM;
11459 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11460 	if (i40e_enabled_xdp_vsi(vsi)) {
11461 		vsi->xdp_rings = next_rings;
11462 		next_rings += vsi->alloc_queue_pairs;
11463 	}
11464 	vsi->rx_rings = next_rings;
11465 
11466 	if (alloc_qvectors) {
11467 		/* allocate memory for q_vector pointers */
11468 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11469 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11470 		if (!vsi->q_vectors) {
11471 			ret = -ENOMEM;
11472 			goto err_vectors;
11473 		}
11474 	}
11475 	return ret;
11476 
11477 err_vectors:
11478 	kfree(vsi->tx_rings);
11479 	return ret;
11480 }
11481 
11482 /**
11483  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11484  * @pf: board private structure
11485  * @type: type of VSI
11486  *
11487  * On error: returns error code (negative)
11488  * On success: returns vsi index in PF (positive)
11489  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11490 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11491 {
11492 	int ret = -ENODEV;
11493 	struct i40e_vsi *vsi;
11494 	int vsi_idx;
11495 	int i;
11496 
11497 	/* Need to protect the allocation of the VSIs at the PF level */
11498 	mutex_lock(&pf->switch_mutex);
11499 
11500 	/* VSI list may be fragmented if VSI creation/destruction has
11501 	 * been happening.  We can afford to do a quick scan to look
11502 	 * for any free VSIs in the list.
11503 	 *
11504 	 * find next empty vsi slot, looping back around if necessary
11505 	 */
11506 	i = pf->next_vsi;
11507 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11508 		i++;
11509 	if (i >= pf->num_alloc_vsi) {
11510 		i = 0;
11511 		while (i < pf->next_vsi && pf->vsi[i])
11512 			i++;
11513 	}
11514 
11515 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11516 		vsi_idx = i;             /* Found one! */
11517 	} else {
11518 		ret = -ENODEV;
11519 		goto unlock_pf;  /* out of VSI slots! */
11520 	}
11521 	pf->next_vsi = ++i;
11522 
11523 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11524 	if (!vsi) {
11525 		ret = -ENOMEM;
11526 		goto unlock_pf;
11527 	}
11528 	vsi->type = type;
11529 	vsi->back = pf;
11530 	set_bit(__I40E_VSI_DOWN, vsi->state);
11531 	vsi->flags = 0;
11532 	vsi->idx = vsi_idx;
11533 	vsi->int_rate_limit = 0;
11534 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11535 				pf->rss_table_size : 64;
11536 	vsi->netdev_registered = false;
11537 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11538 	hash_init(vsi->mac_filter_hash);
11539 	vsi->irqs_ready = false;
11540 
11541 	if (type == I40E_VSI_MAIN) {
11542 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11543 		if (!vsi->af_xdp_zc_qps)
11544 			goto err_rings;
11545 	}
11546 
11547 	ret = i40e_set_num_rings_in_vsi(vsi);
11548 	if (ret)
11549 		goto err_rings;
11550 
11551 	ret = i40e_vsi_alloc_arrays(vsi, true);
11552 	if (ret)
11553 		goto err_rings;
11554 
11555 	/* Setup default MSIX irq handler for VSI */
11556 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11557 
11558 	/* Initialize VSI lock */
11559 	spin_lock_init(&vsi->mac_filter_hash_lock);
11560 	pf->vsi[vsi_idx] = vsi;
11561 	ret = vsi_idx;
11562 	goto unlock_pf;
11563 
11564 err_rings:
11565 	bitmap_free(vsi->af_xdp_zc_qps);
11566 	pf->next_vsi = i - 1;
11567 	kfree(vsi);
11568 unlock_pf:
11569 	mutex_unlock(&pf->switch_mutex);
11570 	return ret;
11571 }
11572 
11573 /**
11574  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11575  * @vsi: VSI pointer
11576  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11577  *
11578  * On error: returns error code (negative)
11579  * On success: returns 0
11580  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11581 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11582 {
11583 	/* free the ring and vector containers */
11584 	if (free_qvectors) {
11585 		kfree(vsi->q_vectors);
11586 		vsi->q_vectors = NULL;
11587 	}
11588 	kfree(vsi->tx_rings);
11589 	vsi->tx_rings = NULL;
11590 	vsi->rx_rings = NULL;
11591 	vsi->xdp_rings = NULL;
11592 }
11593 
11594 /**
11595  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11596  * and lookup table
11597  * @vsi: Pointer to VSI structure
11598  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11599 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11600 {
11601 	if (!vsi)
11602 		return;
11603 
11604 	kfree(vsi->rss_hkey_user);
11605 	vsi->rss_hkey_user = NULL;
11606 
11607 	kfree(vsi->rss_lut_user);
11608 	vsi->rss_lut_user = NULL;
11609 }
11610 
11611 /**
11612  * i40e_vsi_clear - Deallocate the VSI provided
11613  * @vsi: the VSI being un-configured
11614  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11615 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11616 {
11617 	struct i40e_pf *pf;
11618 
11619 	if (!vsi)
11620 		return 0;
11621 
11622 	if (!vsi->back)
11623 		goto free_vsi;
11624 	pf = vsi->back;
11625 
11626 	mutex_lock(&pf->switch_mutex);
11627 	if (!pf->vsi[vsi->idx]) {
11628 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11629 			vsi->idx, vsi->idx, vsi->type);
11630 		goto unlock_vsi;
11631 	}
11632 
11633 	if (pf->vsi[vsi->idx] != vsi) {
11634 		dev_err(&pf->pdev->dev,
11635 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11636 			pf->vsi[vsi->idx]->idx,
11637 			pf->vsi[vsi->idx]->type,
11638 			vsi->idx, vsi->type);
11639 		goto unlock_vsi;
11640 	}
11641 
11642 	/* updates the PF for this cleared vsi */
11643 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11644 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11645 
11646 	bitmap_free(vsi->af_xdp_zc_qps);
11647 	i40e_vsi_free_arrays(vsi, true);
11648 	i40e_clear_rss_config_user(vsi);
11649 
11650 	pf->vsi[vsi->idx] = NULL;
11651 	if (vsi->idx < pf->next_vsi)
11652 		pf->next_vsi = vsi->idx;
11653 
11654 unlock_vsi:
11655 	mutex_unlock(&pf->switch_mutex);
11656 free_vsi:
11657 	kfree(vsi);
11658 
11659 	return 0;
11660 }
11661 
11662 /**
11663  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11664  * @vsi: the VSI being cleaned
11665  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11666 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11667 {
11668 	int i;
11669 
11670 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11671 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11672 			kfree_rcu(vsi->tx_rings[i], rcu);
11673 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11674 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11675 			if (vsi->xdp_rings)
11676 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11677 		}
11678 	}
11679 }
11680 
11681 /**
11682  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11683  * @vsi: the VSI being configured
11684  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11685 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11686 {
11687 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11688 	struct i40e_pf *pf = vsi->back;
11689 	struct i40e_ring *ring;
11690 
11691 	/* Set basic values in the rings to be used later during open() */
11692 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11693 		/* allocate space for both Tx and Rx in one shot */
11694 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11695 		if (!ring)
11696 			goto err_out;
11697 
11698 		ring->queue_index = i;
11699 		ring->reg_idx = vsi->base_queue + i;
11700 		ring->ring_active = false;
11701 		ring->vsi = vsi;
11702 		ring->netdev = vsi->netdev;
11703 		ring->dev = &pf->pdev->dev;
11704 		ring->count = vsi->num_tx_desc;
11705 		ring->size = 0;
11706 		ring->dcb_tc = 0;
11707 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11708 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11709 		ring->itr_setting = pf->tx_itr_default;
11710 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11711 
11712 		if (!i40e_enabled_xdp_vsi(vsi))
11713 			goto setup_rx;
11714 
11715 		ring->queue_index = vsi->alloc_queue_pairs + i;
11716 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11717 		ring->ring_active = false;
11718 		ring->vsi = vsi;
11719 		ring->netdev = NULL;
11720 		ring->dev = &pf->pdev->dev;
11721 		ring->count = vsi->num_tx_desc;
11722 		ring->size = 0;
11723 		ring->dcb_tc = 0;
11724 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11725 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11726 		set_ring_xdp(ring);
11727 		ring->itr_setting = pf->tx_itr_default;
11728 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11729 
11730 setup_rx:
11731 		ring->queue_index = i;
11732 		ring->reg_idx = vsi->base_queue + i;
11733 		ring->ring_active = false;
11734 		ring->vsi = vsi;
11735 		ring->netdev = vsi->netdev;
11736 		ring->dev = &pf->pdev->dev;
11737 		ring->count = vsi->num_rx_desc;
11738 		ring->size = 0;
11739 		ring->dcb_tc = 0;
11740 		ring->itr_setting = pf->rx_itr_default;
11741 		WRITE_ONCE(vsi->rx_rings[i], ring);
11742 	}
11743 
11744 	return 0;
11745 
11746 err_out:
11747 	i40e_vsi_clear_rings(vsi);
11748 	return -ENOMEM;
11749 }
11750 
11751 /**
11752  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11753  * @pf: board private structure
11754  * @vectors: the number of MSI-X vectors to request
11755  *
11756  * Returns the number of vectors reserved, or error
11757  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11758 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11759 {
11760 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11761 					I40E_MIN_MSIX, vectors);
11762 	if (vectors < 0) {
11763 		dev_info(&pf->pdev->dev,
11764 			 "MSI-X vector reservation failed: %d\n", vectors);
11765 		vectors = 0;
11766 	}
11767 
11768 	return vectors;
11769 }
11770 
11771 /**
11772  * i40e_init_msix - Setup the MSIX capability
11773  * @pf: board private structure
11774  *
11775  * Work with the OS to set up the MSIX vectors needed.
11776  *
11777  * Returns the number of vectors reserved or negative on failure
11778  **/
i40e_init_msix(struct i40e_pf * pf)11779 static int i40e_init_msix(struct i40e_pf *pf)
11780 {
11781 	struct i40e_hw *hw = &pf->hw;
11782 	int cpus, extra_vectors;
11783 	int vectors_left;
11784 	int v_budget, i;
11785 	int v_actual;
11786 	int iwarp_requested = 0;
11787 
11788 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11789 		return -ENODEV;
11790 
11791 	/* The number of vectors we'll request will be comprised of:
11792 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11793 	 *   - The number of LAN queue pairs
11794 	 *	- Queues being used for RSS.
11795 	 *		We don't need as many as max_rss_size vectors.
11796 	 *		use rss_size instead in the calculation since that
11797 	 *		is governed by number of cpus in the system.
11798 	 *	- assumes symmetric Tx/Rx pairing
11799 	 *   - The number of VMDq pairs
11800 	 *   - The CPU count within the NUMA node if iWARP is enabled
11801 	 * Once we count this up, try the request.
11802 	 *
11803 	 * If we can't get what we want, we'll simplify to nearly nothing
11804 	 * and try again.  If that still fails, we punt.
11805 	 */
11806 	vectors_left = hw->func_caps.num_msix_vectors;
11807 	v_budget = 0;
11808 
11809 	/* reserve one vector for miscellaneous handler */
11810 	if (vectors_left) {
11811 		v_budget++;
11812 		vectors_left--;
11813 	}
11814 
11815 	/* reserve some vectors for the main PF traffic queues. Initially we
11816 	 * only reserve at most 50% of the available vectors, in the case that
11817 	 * the number of online CPUs is large. This ensures that we can enable
11818 	 * extra features as well. Once we've enabled the other features, we
11819 	 * will use any remaining vectors to reach as close as we can to the
11820 	 * number of online CPUs.
11821 	 */
11822 	cpus = num_online_cpus();
11823 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11824 	vectors_left -= pf->num_lan_msix;
11825 
11826 	/* reserve one vector for sideband flow director */
11827 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11828 		if (vectors_left) {
11829 			pf->num_fdsb_msix = 1;
11830 			v_budget++;
11831 			vectors_left--;
11832 		} else {
11833 			pf->num_fdsb_msix = 0;
11834 		}
11835 	}
11836 
11837 	/* can we reserve enough for iWARP? */
11838 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11839 		iwarp_requested = pf->num_iwarp_msix;
11840 
11841 		if (!vectors_left)
11842 			pf->num_iwarp_msix = 0;
11843 		else if (vectors_left < pf->num_iwarp_msix)
11844 			pf->num_iwarp_msix = 1;
11845 		v_budget += pf->num_iwarp_msix;
11846 		vectors_left -= pf->num_iwarp_msix;
11847 	}
11848 
11849 	/* any vectors left over go for VMDq support */
11850 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11851 		if (!vectors_left) {
11852 			pf->num_vmdq_msix = 0;
11853 			pf->num_vmdq_qps = 0;
11854 		} else {
11855 			int vmdq_vecs_wanted =
11856 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11857 			int vmdq_vecs =
11858 				min_t(int, vectors_left, vmdq_vecs_wanted);
11859 
11860 			/* if we're short on vectors for what's desired, we limit
11861 			 * the queues per vmdq.  If this is still more than are
11862 			 * available, the user will need to change the number of
11863 			 * queues/vectors used by the PF later with the ethtool
11864 			 * channels command
11865 			 */
11866 			if (vectors_left < vmdq_vecs_wanted) {
11867 				pf->num_vmdq_qps = 1;
11868 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11869 				vmdq_vecs = min_t(int,
11870 						  vectors_left,
11871 						  vmdq_vecs_wanted);
11872 			}
11873 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11874 
11875 			v_budget += vmdq_vecs;
11876 			vectors_left -= vmdq_vecs;
11877 		}
11878 	}
11879 
11880 	/* On systems with a large number of SMP cores, we previously limited
11881 	 * the number of vectors for num_lan_msix to be at most 50% of the
11882 	 * available vectors, to allow for other features. Now, we add back
11883 	 * the remaining vectors. However, we ensure that the total
11884 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11885 	 * calculate the number of vectors we can add without going over the
11886 	 * cap of CPUs. For systems with a small number of CPUs this will be
11887 	 * zero.
11888 	 */
11889 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11890 	pf->num_lan_msix += extra_vectors;
11891 	vectors_left -= extra_vectors;
11892 
11893 	WARN(vectors_left < 0,
11894 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11895 
11896 	v_budget += pf->num_lan_msix;
11897 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11898 				   GFP_KERNEL);
11899 	if (!pf->msix_entries)
11900 		return -ENOMEM;
11901 
11902 	for (i = 0; i < v_budget; i++)
11903 		pf->msix_entries[i].entry = i;
11904 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11905 
11906 	if (v_actual < I40E_MIN_MSIX) {
11907 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11908 		kfree(pf->msix_entries);
11909 		pf->msix_entries = NULL;
11910 		pci_disable_msix(pf->pdev);
11911 		return -ENODEV;
11912 
11913 	} else if (v_actual == I40E_MIN_MSIX) {
11914 		/* Adjust for minimal MSIX use */
11915 		pf->num_vmdq_vsis = 0;
11916 		pf->num_vmdq_qps = 0;
11917 		pf->num_lan_qps = 1;
11918 		pf->num_lan_msix = 1;
11919 
11920 	} else if (v_actual != v_budget) {
11921 		/* If we have limited resources, we will start with no vectors
11922 		 * for the special features and then allocate vectors to some
11923 		 * of these features based on the policy and at the end disable
11924 		 * the features that did not get any vectors.
11925 		 */
11926 		int vec;
11927 
11928 		dev_info(&pf->pdev->dev,
11929 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11930 			 v_actual, v_budget);
11931 		/* reserve the misc vector */
11932 		vec = v_actual - 1;
11933 
11934 		/* Scale vector usage down */
11935 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11936 		pf->num_vmdq_vsis = 1;
11937 		pf->num_vmdq_qps = 1;
11938 
11939 		/* partition out the remaining vectors */
11940 		switch (vec) {
11941 		case 2:
11942 			pf->num_lan_msix = 1;
11943 			break;
11944 		case 3:
11945 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11946 				pf->num_lan_msix = 1;
11947 				pf->num_iwarp_msix = 1;
11948 			} else {
11949 				pf->num_lan_msix = 2;
11950 			}
11951 			break;
11952 		default:
11953 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11954 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11955 						 iwarp_requested);
11956 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11957 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11958 			} else {
11959 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11960 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11961 			}
11962 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11963 				pf->num_fdsb_msix = 1;
11964 				vec--;
11965 			}
11966 			pf->num_lan_msix = min_t(int,
11967 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11968 							      pf->num_lan_msix);
11969 			pf->num_lan_qps = pf->num_lan_msix;
11970 			break;
11971 		}
11972 	}
11973 
11974 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11975 	    (pf->num_fdsb_msix == 0)) {
11976 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11977 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11978 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11979 	}
11980 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11981 	    (pf->num_vmdq_msix == 0)) {
11982 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11983 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11984 	}
11985 
11986 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11987 	    (pf->num_iwarp_msix == 0)) {
11988 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11989 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11990 	}
11991 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11992 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11993 		   pf->num_lan_msix,
11994 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11995 		   pf->num_fdsb_msix,
11996 		   pf->num_iwarp_msix);
11997 
11998 	return v_actual;
11999 }
12000 
12001 /**
12002  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12003  * @vsi: the VSI being configured
12004  * @v_idx: index of the vector in the vsi struct
12005  *
12006  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12007  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)12008 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12009 {
12010 	struct i40e_q_vector *q_vector;
12011 
12012 	/* allocate q_vector */
12013 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12014 	if (!q_vector)
12015 		return -ENOMEM;
12016 
12017 	q_vector->vsi = vsi;
12018 	q_vector->v_idx = v_idx;
12019 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12020 
12021 	if (vsi->netdev)
12022 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12023 
12024 	/* tie q_vector and vsi together */
12025 	vsi->q_vectors[v_idx] = q_vector;
12026 
12027 	return 0;
12028 }
12029 
12030 /**
12031  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12032  * @vsi: the VSI being configured
12033  *
12034  * We allocate one q_vector per queue interrupt.  If allocation fails we
12035  * return -ENOMEM.
12036  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)12037 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12038 {
12039 	struct i40e_pf *pf = vsi->back;
12040 	int err, v_idx, num_q_vectors;
12041 
12042 	/* if not MSIX, give the one vector only to the LAN VSI */
12043 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12044 		num_q_vectors = vsi->num_q_vectors;
12045 	else if (vsi == pf->vsi[pf->lan_vsi])
12046 		num_q_vectors = 1;
12047 	else
12048 		return -EINVAL;
12049 
12050 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12051 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12052 		if (err)
12053 			goto err_out;
12054 	}
12055 
12056 	return 0;
12057 
12058 err_out:
12059 	while (v_idx--)
12060 		i40e_free_q_vector(vsi, v_idx);
12061 
12062 	return err;
12063 }
12064 
12065 /**
12066  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12067  * @pf: board private structure to initialize
12068  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12069 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12070 {
12071 	int vectors = 0;
12072 	ssize_t size;
12073 
12074 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12075 		vectors = i40e_init_msix(pf);
12076 		if (vectors < 0) {
12077 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12078 				       I40E_FLAG_IWARP_ENABLED	|
12079 				       I40E_FLAG_RSS_ENABLED	|
12080 				       I40E_FLAG_DCB_CAPABLE	|
12081 				       I40E_FLAG_DCB_ENABLED	|
12082 				       I40E_FLAG_SRIOV_ENABLED	|
12083 				       I40E_FLAG_FD_SB_ENABLED	|
12084 				       I40E_FLAG_FD_ATR_ENABLED	|
12085 				       I40E_FLAG_VMDQ_ENABLED);
12086 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12087 
12088 			/* rework the queue expectations without MSIX */
12089 			i40e_determine_queue_usage(pf);
12090 		}
12091 	}
12092 
12093 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12094 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12095 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12096 		vectors = pci_enable_msi(pf->pdev);
12097 		if (vectors < 0) {
12098 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12099 				 vectors);
12100 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12101 		}
12102 		vectors = 1;  /* one MSI or Legacy vector */
12103 	}
12104 
12105 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12106 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12107 
12108 	/* set up vector assignment tracking */
12109 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12110 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12111 	if (!pf->irq_pile)
12112 		return -ENOMEM;
12113 
12114 	pf->irq_pile->num_entries = vectors;
12115 
12116 	/* track first vector for misc interrupts, ignore return */
12117 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12118 
12119 	return 0;
12120 }
12121 
12122 /**
12123  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12124  * @pf: private board data structure
12125  *
12126  * Restore the interrupt scheme that was cleared when we suspended the
12127  * device. This should be called during resume to re-allocate the q_vectors
12128  * and reacquire IRQs.
12129  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12130 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12131 {
12132 	int err, i;
12133 
12134 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12135 	 * scheme. We need to re-enabled them here in order to attempt to
12136 	 * re-acquire the MSI or MSI-X vectors
12137 	 */
12138 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12139 
12140 	err = i40e_init_interrupt_scheme(pf);
12141 	if (err)
12142 		return err;
12143 
12144 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12145 	 * rings together again.
12146 	 */
12147 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12148 		if (pf->vsi[i]) {
12149 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12150 			if (err)
12151 				goto err_unwind;
12152 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12153 		}
12154 	}
12155 
12156 	err = i40e_setup_misc_vector(pf);
12157 	if (err)
12158 		goto err_unwind;
12159 
12160 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12161 		i40e_client_update_msix_info(pf);
12162 
12163 	return 0;
12164 
12165 err_unwind:
12166 	while (i--) {
12167 		if (pf->vsi[i])
12168 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12169 	}
12170 
12171 	return err;
12172 }
12173 
12174 /**
12175  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12176  * non queue events in recovery mode
12177  * @pf: board private structure
12178  *
12179  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12180  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12181  * This is handled differently than in recovery mode since no Tx/Rx resources
12182  * are being allocated.
12183  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12184 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12185 {
12186 	int err;
12187 
12188 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12189 		err = i40e_setup_misc_vector(pf);
12190 
12191 		if (err) {
12192 			dev_info(&pf->pdev->dev,
12193 				 "MSI-X misc vector request failed, error %d\n",
12194 				 err);
12195 			return err;
12196 		}
12197 	} else {
12198 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12199 
12200 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12201 				  pf->int_name, pf);
12202 
12203 		if (err) {
12204 			dev_info(&pf->pdev->dev,
12205 				 "MSI/legacy misc vector request failed, error %d\n",
12206 				 err);
12207 			return err;
12208 		}
12209 		i40e_enable_misc_int_causes(pf);
12210 		i40e_irq_dynamic_enable_icr0(pf);
12211 	}
12212 
12213 	return 0;
12214 }
12215 
12216 /**
12217  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12218  * @pf: board private structure
12219  *
12220  * This sets up the handler for MSIX 0, which is used to manage the
12221  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12222  * when in MSI or Legacy interrupt mode.
12223  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12224 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12225 {
12226 	struct i40e_hw *hw = &pf->hw;
12227 	int err = 0;
12228 
12229 	/* Only request the IRQ once, the first time through. */
12230 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12231 		err = request_irq(pf->msix_entries[0].vector,
12232 				  i40e_intr, 0, pf->int_name, pf);
12233 		if (err) {
12234 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12235 			dev_info(&pf->pdev->dev,
12236 				 "request_irq for %s failed: %d\n",
12237 				 pf->int_name, err);
12238 			return -EFAULT;
12239 		}
12240 	}
12241 
12242 	i40e_enable_misc_int_causes(pf);
12243 
12244 	/* associate no queues to the misc vector */
12245 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12246 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12247 
12248 	i40e_flush(hw);
12249 
12250 	i40e_irq_dynamic_enable_icr0(pf);
12251 
12252 	return err;
12253 }
12254 
12255 /**
12256  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12257  * @vsi: Pointer to vsi structure
12258  * @seed: Buffter to store the hash keys
12259  * @lut: Buffer to store the lookup table entries
12260  * @lut_size: Size of buffer to store the lookup table entries
12261  *
12262  * Return 0 on success, negative on failure
12263  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12264 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12265 			   u8 *lut, u16 lut_size)
12266 {
12267 	struct i40e_pf *pf = vsi->back;
12268 	struct i40e_hw *hw = &pf->hw;
12269 	int ret = 0;
12270 
12271 	if (seed) {
12272 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12273 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12274 		if (ret) {
12275 			dev_info(&pf->pdev->dev,
12276 				 "Cannot get RSS key, err %pe aq_err %s\n",
12277 				 ERR_PTR(ret),
12278 				 i40e_aq_str(&pf->hw,
12279 					     pf->hw.aq.asq_last_status));
12280 			return ret;
12281 		}
12282 	}
12283 
12284 	if (lut) {
12285 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12286 
12287 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12288 		if (ret) {
12289 			dev_info(&pf->pdev->dev,
12290 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12291 				 ERR_PTR(ret),
12292 				 i40e_aq_str(&pf->hw,
12293 					     pf->hw.aq.asq_last_status));
12294 			return ret;
12295 		}
12296 	}
12297 
12298 	return ret;
12299 }
12300 
12301 /**
12302  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12303  * @vsi: Pointer to vsi structure
12304  * @seed: RSS hash seed
12305  * @lut: Lookup table
12306  * @lut_size: Lookup table size
12307  *
12308  * Returns 0 on success, negative on failure
12309  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12310 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12311 			       const u8 *lut, u16 lut_size)
12312 {
12313 	struct i40e_pf *pf = vsi->back;
12314 	struct i40e_hw *hw = &pf->hw;
12315 	u16 vf_id = vsi->vf_id;
12316 	u8 i;
12317 
12318 	/* Fill out hash function seed */
12319 	if (seed) {
12320 		u32 *seed_dw = (u32 *)seed;
12321 
12322 		if (vsi->type == I40E_VSI_MAIN) {
12323 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12324 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12325 		} else if (vsi->type == I40E_VSI_SRIOV) {
12326 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12327 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12328 		} else {
12329 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12330 		}
12331 	}
12332 
12333 	if (lut) {
12334 		u32 *lut_dw = (u32 *)lut;
12335 
12336 		if (vsi->type == I40E_VSI_MAIN) {
12337 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12338 				return -EINVAL;
12339 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12340 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12341 		} else if (vsi->type == I40E_VSI_SRIOV) {
12342 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12343 				return -EINVAL;
12344 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12345 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12346 		} else {
12347 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12348 		}
12349 	}
12350 	i40e_flush(hw);
12351 
12352 	return 0;
12353 }
12354 
12355 /**
12356  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12357  * @vsi: Pointer to VSI structure
12358  * @seed: Buffer to store the keys
12359  * @lut: Buffer to store the lookup table entries
12360  * @lut_size: Size of buffer to store the lookup table entries
12361  *
12362  * Returns 0 on success, negative on failure
12363  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12364 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12365 			    u8 *lut, u16 lut_size)
12366 {
12367 	struct i40e_pf *pf = vsi->back;
12368 	struct i40e_hw *hw = &pf->hw;
12369 	u16 i;
12370 
12371 	if (seed) {
12372 		u32 *seed_dw = (u32 *)seed;
12373 
12374 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12375 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12376 	}
12377 	if (lut) {
12378 		u32 *lut_dw = (u32 *)lut;
12379 
12380 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12381 			return -EINVAL;
12382 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12383 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12384 	}
12385 
12386 	return 0;
12387 }
12388 
12389 /**
12390  * i40e_config_rss - Configure RSS keys and lut
12391  * @vsi: Pointer to VSI structure
12392  * @seed: RSS hash seed
12393  * @lut: Lookup table
12394  * @lut_size: Lookup table size
12395  *
12396  * Returns 0 on success, negative on failure
12397  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12398 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12399 {
12400 	struct i40e_pf *pf = vsi->back;
12401 
12402 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12403 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12404 	else
12405 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12406 }
12407 
12408 /**
12409  * i40e_get_rss - Get RSS keys and lut
12410  * @vsi: Pointer to VSI structure
12411  * @seed: Buffer to store the keys
12412  * @lut: Buffer to store the lookup table entries
12413  * @lut_size: Size of buffer to store the lookup table entries
12414  *
12415  * Returns 0 on success, negative on failure
12416  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12417 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12418 {
12419 	struct i40e_pf *pf = vsi->back;
12420 
12421 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12422 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12423 	else
12424 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12425 }
12426 
12427 /**
12428  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12429  * @pf: Pointer to board private structure
12430  * @lut: Lookup table
12431  * @rss_table_size: Lookup table size
12432  * @rss_size: Range of queue number for hashing
12433  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12434 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12435 		       u16 rss_table_size, u16 rss_size)
12436 {
12437 	u16 i;
12438 
12439 	for (i = 0; i < rss_table_size; i++)
12440 		lut[i] = i % rss_size;
12441 }
12442 
12443 /**
12444  * i40e_pf_config_rss - Prepare for RSS if used
12445  * @pf: board private structure
12446  **/
i40e_pf_config_rss(struct i40e_pf * pf)12447 static int i40e_pf_config_rss(struct i40e_pf *pf)
12448 {
12449 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12450 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12451 	u8 *lut;
12452 	struct i40e_hw *hw = &pf->hw;
12453 	u32 reg_val;
12454 	u64 hena;
12455 	int ret;
12456 
12457 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12458 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12459 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12460 	hena |= i40e_pf_get_default_rss_hena(pf);
12461 
12462 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12463 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12464 
12465 	/* Determine the RSS table size based on the hardware capabilities */
12466 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12467 	reg_val = (pf->rss_table_size == 512) ?
12468 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12469 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12470 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12471 
12472 	/* Determine the RSS size of the VSI */
12473 	if (!vsi->rss_size) {
12474 		u16 qcount;
12475 		/* If the firmware does something weird during VSI init, we
12476 		 * could end up with zero TCs. Check for that to avoid
12477 		 * divide-by-zero. It probably won't pass traffic, but it also
12478 		 * won't panic.
12479 		 */
12480 		qcount = vsi->num_queue_pairs /
12481 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12482 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12483 	}
12484 	if (!vsi->rss_size)
12485 		return -EINVAL;
12486 
12487 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12488 	if (!lut)
12489 		return -ENOMEM;
12490 
12491 	/* Use user configured lut if there is one, otherwise use default */
12492 	if (vsi->rss_lut_user)
12493 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12494 	else
12495 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12496 
12497 	/* Use user configured hash key if there is one, otherwise
12498 	 * use default.
12499 	 */
12500 	if (vsi->rss_hkey_user)
12501 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12502 	else
12503 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12504 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12505 	kfree(lut);
12506 
12507 	return ret;
12508 }
12509 
12510 /**
12511  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12512  * @pf: board private structure
12513  * @queue_count: the requested queue count for rss.
12514  *
12515  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12516  * count which may be different from the requested queue count.
12517  * Note: expects to be called while under rtnl_lock()
12518  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12519 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12520 {
12521 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12522 	int new_rss_size;
12523 
12524 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12525 		return 0;
12526 
12527 	queue_count = min_t(int, queue_count, num_online_cpus());
12528 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12529 
12530 	if (queue_count != vsi->num_queue_pairs) {
12531 		u16 qcount;
12532 
12533 		vsi->req_queue_pairs = queue_count;
12534 		i40e_prep_for_reset(pf);
12535 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12536 			return pf->alloc_rss_size;
12537 
12538 		pf->alloc_rss_size = new_rss_size;
12539 
12540 		i40e_reset_and_rebuild(pf, true, true);
12541 
12542 		/* Discard the user configured hash keys and lut, if less
12543 		 * queues are enabled.
12544 		 */
12545 		if (queue_count < vsi->rss_size) {
12546 			i40e_clear_rss_config_user(vsi);
12547 			dev_dbg(&pf->pdev->dev,
12548 				"discard user configured hash keys and lut\n");
12549 		}
12550 
12551 		/* Reset vsi->rss_size, as number of enabled queues changed */
12552 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12553 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12554 
12555 		i40e_pf_config_rss(pf);
12556 	}
12557 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12558 		 vsi->req_queue_pairs, pf->rss_size_max);
12559 	return pf->alloc_rss_size;
12560 }
12561 
12562 /**
12563  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12564  * @pf: board private structure
12565  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12566 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12567 {
12568 	bool min_valid, max_valid;
12569 	u32 max_bw, min_bw;
12570 	int status;
12571 
12572 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12573 					   &min_valid, &max_valid);
12574 
12575 	if (!status) {
12576 		if (min_valid)
12577 			pf->min_bw = min_bw;
12578 		if (max_valid)
12579 			pf->max_bw = max_bw;
12580 	}
12581 
12582 	return status;
12583 }
12584 
12585 /**
12586  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12587  * @pf: board private structure
12588  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12589 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12590 {
12591 	struct i40e_aqc_configure_partition_bw_data bw_data;
12592 	int status;
12593 
12594 	memset(&bw_data, 0, sizeof(bw_data));
12595 
12596 	/* Set the valid bit for this PF */
12597 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12598 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12599 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12600 
12601 	/* Set the new bandwidths */
12602 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12603 
12604 	return status;
12605 }
12606 
12607 /**
12608  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12609  * @pf: board private structure
12610  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)12611 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12612 {
12613 	/* Commit temporary BW setting to permanent NVM image */
12614 	enum i40e_admin_queue_err last_aq_status;
12615 	u16 nvm_word;
12616 	int ret;
12617 
12618 	if (pf->hw.partition_id != 1) {
12619 		dev_info(&pf->pdev->dev,
12620 			 "Commit BW only works on partition 1! This is partition %d",
12621 			 pf->hw.partition_id);
12622 		ret = -EOPNOTSUPP;
12623 		goto bw_commit_out;
12624 	}
12625 
12626 	/* Acquire NVM for read access */
12627 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12628 	last_aq_status = pf->hw.aq.asq_last_status;
12629 	if (ret) {
12630 		dev_info(&pf->pdev->dev,
12631 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12632 			 ERR_PTR(ret),
12633 			 i40e_aq_str(&pf->hw, last_aq_status));
12634 		goto bw_commit_out;
12635 	}
12636 
12637 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12638 	ret = i40e_aq_read_nvm(&pf->hw,
12639 			       I40E_SR_NVM_CONTROL_WORD,
12640 			       0x10, sizeof(nvm_word), &nvm_word,
12641 			       false, NULL);
12642 	/* Save off last admin queue command status before releasing
12643 	 * the NVM
12644 	 */
12645 	last_aq_status = pf->hw.aq.asq_last_status;
12646 	i40e_release_nvm(&pf->hw);
12647 	if (ret) {
12648 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12649 			 ERR_PTR(ret),
12650 			 i40e_aq_str(&pf->hw, last_aq_status));
12651 		goto bw_commit_out;
12652 	}
12653 
12654 	/* Wait a bit for NVM release to complete */
12655 	msleep(50);
12656 
12657 	/* Acquire NVM for write access */
12658 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12659 	last_aq_status = pf->hw.aq.asq_last_status;
12660 	if (ret) {
12661 		dev_info(&pf->pdev->dev,
12662 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12663 			 ERR_PTR(ret),
12664 			 i40e_aq_str(&pf->hw, last_aq_status));
12665 		goto bw_commit_out;
12666 	}
12667 	/* Write it back out unchanged to initiate update NVM,
12668 	 * which will force a write of the shadow (alt) RAM to
12669 	 * the NVM - thus storing the bandwidth values permanently.
12670 	 */
12671 	ret = i40e_aq_update_nvm(&pf->hw,
12672 				 I40E_SR_NVM_CONTROL_WORD,
12673 				 0x10, sizeof(nvm_word),
12674 				 &nvm_word, true, 0, NULL);
12675 	/* Save off last admin queue command status before releasing
12676 	 * the NVM
12677 	 */
12678 	last_aq_status = pf->hw.aq.asq_last_status;
12679 	i40e_release_nvm(&pf->hw);
12680 	if (ret)
12681 		dev_info(&pf->pdev->dev,
12682 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12683 			 ERR_PTR(ret),
12684 			 i40e_aq_str(&pf->hw, last_aq_status));
12685 bw_commit_out:
12686 
12687 	return ret;
12688 }
12689 
12690 /**
12691  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12692  * if total port shutdown feature is enabled for this PF
12693  * @pf: board private structure
12694  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12695 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12696 {
12697 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12698 #define I40E_FEATURES_ENABLE_PTR		0x2A
12699 #define I40E_CURRENT_SETTING_PTR		0x2B
12700 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12701 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12702 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12703 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12704 	u16 sr_emp_sr_settings_ptr = 0;
12705 	u16 features_enable = 0;
12706 	u16 link_behavior = 0;
12707 	int read_status = 0;
12708 	bool ret = false;
12709 
12710 	read_status = i40e_read_nvm_word(&pf->hw,
12711 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12712 					 &sr_emp_sr_settings_ptr);
12713 	if (read_status)
12714 		goto err_nvm;
12715 	read_status = i40e_read_nvm_word(&pf->hw,
12716 					 sr_emp_sr_settings_ptr +
12717 					 I40E_FEATURES_ENABLE_PTR,
12718 					 &features_enable);
12719 	if (read_status)
12720 		goto err_nvm;
12721 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12722 		read_status = i40e_read_nvm_module_data(&pf->hw,
12723 							I40E_SR_EMP_SR_SETTINGS_PTR,
12724 							I40E_CURRENT_SETTING_PTR,
12725 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12726 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12727 							&link_behavior);
12728 		if (read_status)
12729 			goto err_nvm;
12730 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12731 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12732 	}
12733 	return ret;
12734 
12735 err_nvm:
12736 	dev_warn(&pf->pdev->dev,
12737 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12738 		 ERR_PTR(read_status));
12739 	return ret;
12740 }
12741 
12742 /**
12743  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12744  * @pf: board private structure to initialize
12745  *
12746  * i40e_sw_init initializes the Adapter private data structure.
12747  * Fields are initialized based on PCI device information and
12748  * OS network device settings (MTU size).
12749  **/
i40e_sw_init(struct i40e_pf * pf)12750 static int i40e_sw_init(struct i40e_pf *pf)
12751 {
12752 	int err = 0;
12753 	int size;
12754 	u16 pow;
12755 
12756 	/* Set default capability flags */
12757 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12758 		    I40E_FLAG_MSI_ENABLED     |
12759 		    I40E_FLAG_MSIX_ENABLED;
12760 
12761 	/* Set default ITR */
12762 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12763 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12764 
12765 	/* Depending on PF configurations, it is possible that the RSS
12766 	 * maximum might end up larger than the available queues
12767 	 */
12768 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12769 	pf->alloc_rss_size = 1;
12770 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12771 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12772 				 pf->hw.func_caps.num_tx_qp);
12773 
12774 	/* find the next higher power-of-2 of num cpus */
12775 	pow = roundup_pow_of_two(num_online_cpus());
12776 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12777 
12778 	if (pf->hw.func_caps.rss) {
12779 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12780 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12781 					   num_online_cpus());
12782 	}
12783 
12784 	/* MFP mode enabled */
12785 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12786 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12787 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12788 		if (i40e_get_partition_bw_setting(pf)) {
12789 			dev_warn(&pf->pdev->dev,
12790 				 "Could not get partition bw settings\n");
12791 		} else {
12792 			dev_info(&pf->pdev->dev,
12793 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12794 				 pf->min_bw, pf->max_bw);
12795 
12796 			/* nudge the Tx scheduler */
12797 			i40e_set_partition_bw_setting(pf);
12798 		}
12799 	}
12800 
12801 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12802 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12803 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12804 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12805 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12806 		    pf->hw.num_partitions > 1)
12807 			dev_info(&pf->pdev->dev,
12808 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12809 		else
12810 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12811 		pf->fdir_pf_filter_count =
12812 				 pf->hw.func_caps.fd_filters_guaranteed;
12813 		pf->hw.fdir_shared_filter_count =
12814 				 pf->hw.func_caps.fd_filters_best_effort;
12815 	}
12816 
12817 	if (pf->hw.mac.type == I40E_MAC_X722) {
12818 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12819 				    I40E_HW_128_QP_RSS_CAPABLE |
12820 				    I40E_HW_ATR_EVICT_CAPABLE |
12821 				    I40E_HW_WB_ON_ITR_CAPABLE |
12822 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12823 				    I40E_HW_NO_PCI_LINK_CHECK |
12824 				    I40E_HW_USE_SET_LLDP_MIB |
12825 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12826 				    I40E_HW_PTP_L4_CAPABLE |
12827 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12828 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12829 
12830 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12831 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12832 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12833 			dev_warn(&pf->pdev->dev,
12834 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12835 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12836 		}
12837 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12838 		   ((pf->hw.aq.api_maj_ver == 1) &&
12839 		    (pf->hw.aq.api_min_ver > 4))) {
12840 		/* Supported in FW API version higher than 1.4 */
12841 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12842 	}
12843 
12844 	/* Enable HW ATR eviction if possible */
12845 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12846 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12847 
12848 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12849 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12850 	    (pf->hw.aq.fw_maj_ver < 4))) {
12851 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12852 		/* No DCB support  for FW < v4.33 */
12853 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12854 	}
12855 
12856 	/* Disable FW LLDP if FW < v4.3 */
12857 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12858 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12859 	    (pf->hw.aq.fw_maj_ver < 4)))
12860 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12861 
12862 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12863 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12864 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12865 	    (pf->hw.aq.fw_maj_ver >= 5)))
12866 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12867 
12868 	/* Enable PTP L4 if FW > v6.0 */
12869 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12870 	    pf->hw.aq.fw_maj_ver >= 6)
12871 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12872 
12873 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12874 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12875 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12876 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12877 	}
12878 
12879 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12880 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12881 		/* IWARP needs one extra vector for CQP just like MISC.*/
12882 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12883 	}
12884 	/* Stopping FW LLDP engine is supported on XL710 and X722
12885 	 * starting from FW versions determined in i40e_init_adminq.
12886 	 * Stopping the FW LLDP engine is not supported on XL710
12887 	 * if NPAR is functioning so unset this hw flag in this case.
12888 	 */
12889 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12890 	    pf->hw.func_caps.npar_enable &&
12891 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12892 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12893 
12894 #ifdef CONFIG_PCI_IOV
12895 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12896 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12897 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12898 		pf->num_req_vfs = min_t(int,
12899 					pf->hw.func_caps.num_vfs,
12900 					I40E_MAX_VF_COUNT);
12901 	}
12902 #endif /* CONFIG_PCI_IOV */
12903 	pf->eeprom_version = 0xDEAD;
12904 	pf->lan_veb = I40E_NO_VEB;
12905 	pf->lan_vsi = I40E_NO_VSI;
12906 
12907 	/* By default FW has this off for performance reasons */
12908 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12909 
12910 	/* set up queue assignment tracking */
12911 	size = sizeof(struct i40e_lump_tracking)
12912 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12913 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12914 	if (!pf->qp_pile) {
12915 		err = -ENOMEM;
12916 		goto sw_init_done;
12917 	}
12918 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12919 
12920 	pf->tx_timeout_recovery_level = 1;
12921 
12922 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12923 	    i40e_is_total_port_shutdown_enabled(pf)) {
12924 		/* Link down on close must be on when total port shutdown
12925 		 * is enabled for a given port
12926 		 */
12927 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12928 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12929 		dev_info(&pf->pdev->dev,
12930 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12931 	}
12932 	mutex_init(&pf->switch_mutex);
12933 
12934 sw_init_done:
12935 	return err;
12936 }
12937 
12938 /**
12939  * i40e_set_ntuple - set the ntuple feature flag and take action
12940  * @pf: board private structure to initialize
12941  * @features: the feature set that the stack is suggesting
12942  *
12943  * returns a bool to indicate if reset needs to happen
12944  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12945 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12946 {
12947 	bool need_reset = false;
12948 
12949 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12950 	 * the state changed, we need to reset.
12951 	 */
12952 	if (features & NETIF_F_NTUPLE) {
12953 		/* Enable filters and mark for reset */
12954 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12955 			need_reset = true;
12956 		/* enable FD_SB only if there is MSI-X vector and no cloud
12957 		 * filters exist
12958 		 */
12959 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12960 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12961 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12962 		}
12963 	} else {
12964 		/* turn off filters, mark for reset and clear SW filter list */
12965 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12966 			need_reset = true;
12967 			i40e_fdir_filter_exit(pf);
12968 		}
12969 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12970 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12971 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12972 
12973 		/* reset fd counters */
12974 		pf->fd_add_err = 0;
12975 		pf->fd_atr_cnt = 0;
12976 		/* if ATR was auto disabled it can be re-enabled. */
12977 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12978 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12979 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12980 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12981 	}
12982 	return need_reset;
12983 }
12984 
12985 /**
12986  * i40e_clear_rss_lut - clear the rx hash lookup table
12987  * @vsi: the VSI being configured
12988  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12989 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12990 {
12991 	struct i40e_pf *pf = vsi->back;
12992 	struct i40e_hw *hw = &pf->hw;
12993 	u16 vf_id = vsi->vf_id;
12994 	u8 i;
12995 
12996 	if (vsi->type == I40E_VSI_MAIN) {
12997 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12998 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12999 	} else if (vsi->type == I40E_VSI_SRIOV) {
13000 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
13001 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
13002 	} else {
13003 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
13004 	}
13005 }
13006 
13007 /**
13008  * i40e_set_loopback - turn on/off loopback mode on underlying PF
13009  * @vsi: ptr to VSI
13010  * @ena: flag to indicate the on/off setting
13011  */
i40e_set_loopback(struct i40e_vsi * vsi,bool ena)13012 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
13013 {
13014 	bool if_running = netif_running(vsi->netdev) &&
13015 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
13016 	int ret;
13017 
13018 	if (if_running)
13019 		i40e_down(vsi);
13020 
13021 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
13022 	if (ret)
13023 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
13024 	if (if_running)
13025 		i40e_up(vsi);
13026 
13027 	return ret;
13028 }
13029 
13030 /**
13031  * i40e_set_features - set the netdev feature flags
13032  * @netdev: ptr to the netdev being adjusted
13033  * @features: the feature set that the stack is suggesting
13034  * Note: expects to be called while under rtnl_lock()
13035  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)13036 static int i40e_set_features(struct net_device *netdev,
13037 			     netdev_features_t features)
13038 {
13039 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13040 	struct i40e_vsi *vsi = np->vsi;
13041 	struct i40e_pf *pf = vsi->back;
13042 	bool need_reset;
13043 
13044 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13045 		i40e_pf_config_rss(pf);
13046 	else if (!(features & NETIF_F_RXHASH) &&
13047 		 netdev->features & NETIF_F_RXHASH)
13048 		i40e_clear_rss_lut(vsi);
13049 
13050 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13051 		i40e_vlan_stripping_enable(vsi);
13052 	else
13053 		i40e_vlan_stripping_disable(vsi);
13054 
13055 	if (!(features & NETIF_F_HW_TC) &&
13056 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13057 		dev_err(&pf->pdev->dev,
13058 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13059 		return -EINVAL;
13060 	}
13061 
13062 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13063 		i40e_del_all_macvlans(vsi);
13064 
13065 	need_reset = i40e_set_ntuple(pf, features);
13066 
13067 	if (need_reset)
13068 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13069 
13070 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13071 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13072 
13073 	return 0;
13074 }
13075 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13076 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13077 				    unsigned int table, unsigned int idx,
13078 				    struct udp_tunnel_info *ti)
13079 {
13080 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13081 	struct i40e_hw *hw = &np->vsi->back->hw;
13082 	u8 type, filter_index;
13083 	int ret;
13084 
13085 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13086 						   I40E_AQC_TUNNEL_TYPE_NGE;
13087 
13088 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13089 				     NULL);
13090 	if (ret) {
13091 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13092 			    ERR_PTR(ret),
13093 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13094 		return -EIO;
13095 	}
13096 
13097 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13098 	return 0;
13099 }
13100 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13101 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13102 				      unsigned int table, unsigned int idx,
13103 				      struct udp_tunnel_info *ti)
13104 {
13105 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13106 	struct i40e_hw *hw = &np->vsi->back->hw;
13107 	int ret;
13108 
13109 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13110 	if (ret) {
13111 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13112 			    ERR_PTR(ret),
13113 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13114 		return -EIO;
13115 	}
13116 
13117 	return 0;
13118 }
13119 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)13120 static int i40e_get_phys_port_id(struct net_device *netdev,
13121 				 struct netdev_phys_item_id *ppid)
13122 {
13123 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13124 	struct i40e_pf *pf = np->vsi->back;
13125 	struct i40e_hw *hw = &pf->hw;
13126 
13127 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13128 		return -EOPNOTSUPP;
13129 
13130 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13131 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13132 
13133 	return 0;
13134 }
13135 
13136 /**
13137  * i40e_ndo_fdb_add - add an entry to the hardware database
13138  * @ndm: the input from the stack
13139  * @tb: pointer to array of nladdr (unused)
13140  * @dev: the net device pointer
13141  * @addr: the MAC address entry being added
13142  * @vid: VLAN ID
13143  * @flags: instructions from stack about fdb operation
13144  * @extack: netlink extended ack, unused currently
13145  */
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)13146 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13147 			    struct net_device *dev,
13148 			    const unsigned char *addr, u16 vid,
13149 			    u16 flags,
13150 			    struct netlink_ext_ack *extack)
13151 {
13152 	struct i40e_netdev_priv *np = netdev_priv(dev);
13153 	struct i40e_pf *pf = np->vsi->back;
13154 	int err = 0;
13155 
13156 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13157 		return -EOPNOTSUPP;
13158 
13159 	if (vid) {
13160 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13161 		return -EINVAL;
13162 	}
13163 
13164 	/* Hardware does not support aging addresses so if a
13165 	 * ndm_state is given only allow permanent addresses
13166 	 */
13167 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13168 		netdev_info(dev, "FDB only supports static addresses\n");
13169 		return -EINVAL;
13170 	}
13171 
13172 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13173 		err = dev_uc_add_excl(dev, addr);
13174 	else if (is_multicast_ether_addr(addr))
13175 		err = dev_mc_add_excl(dev, addr);
13176 	else
13177 		err = -EINVAL;
13178 
13179 	/* Only return duplicate errors if NLM_F_EXCL is set */
13180 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13181 		err = 0;
13182 
13183 	return err;
13184 }
13185 
13186 /**
13187  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13188  * @dev: the netdev being configured
13189  * @nlh: RTNL message
13190  * @flags: bridge flags
13191  * @extack: netlink extended ack
13192  *
13193  * Inserts a new hardware bridge if not already created and
13194  * enables the bridging mode requested (VEB or VEPA). If the
13195  * hardware bridge has already been inserted and the request
13196  * is to change the mode then that requires a PF reset to
13197  * allow rebuild of the components with required hardware
13198  * bridge mode enabled.
13199  *
13200  * Note: expects to be called while under rtnl_lock()
13201  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13202 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13203 				   struct nlmsghdr *nlh,
13204 				   u16 flags,
13205 				   struct netlink_ext_ack *extack)
13206 {
13207 	struct i40e_netdev_priv *np = netdev_priv(dev);
13208 	struct i40e_vsi *vsi = np->vsi;
13209 	struct i40e_pf *pf = vsi->back;
13210 	struct i40e_veb *veb = NULL;
13211 	struct nlattr *attr, *br_spec;
13212 	int i, rem;
13213 
13214 	/* Only for PF VSI for now */
13215 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13216 		return -EOPNOTSUPP;
13217 
13218 	/* Find the HW bridge for PF VSI */
13219 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13220 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13221 			veb = pf->veb[i];
13222 	}
13223 
13224 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13225 	if (!br_spec)
13226 		return -EINVAL;
13227 
13228 	nla_for_each_nested(attr, br_spec, rem) {
13229 		__u16 mode;
13230 
13231 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13232 			continue;
13233 
13234 		mode = nla_get_u16(attr);
13235 		if ((mode != BRIDGE_MODE_VEPA) &&
13236 		    (mode != BRIDGE_MODE_VEB))
13237 			return -EINVAL;
13238 
13239 		/* Insert a new HW bridge */
13240 		if (!veb) {
13241 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13242 					     vsi->tc_config.enabled_tc);
13243 			if (veb) {
13244 				veb->bridge_mode = mode;
13245 				i40e_config_bridge_mode(veb);
13246 			} else {
13247 				/* No Bridge HW offload available */
13248 				return -ENOENT;
13249 			}
13250 			break;
13251 		} else if (mode != veb->bridge_mode) {
13252 			/* Existing HW bridge but different mode needs reset */
13253 			veb->bridge_mode = mode;
13254 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13255 			if (mode == BRIDGE_MODE_VEB)
13256 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13257 			else
13258 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13259 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13260 			break;
13261 		}
13262 	}
13263 
13264 	return 0;
13265 }
13266 
13267 /**
13268  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13269  * @skb: skb buff
13270  * @pid: process id
13271  * @seq: RTNL message seq #
13272  * @dev: the netdev being configured
13273  * @filter_mask: unused
13274  * @nlflags: netlink flags passed in
13275  *
13276  * Return the mode in which the hardware bridge is operating in
13277  * i.e VEB or VEPA.
13278  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13279 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13280 				   struct net_device *dev,
13281 				   u32 __always_unused filter_mask,
13282 				   int nlflags)
13283 {
13284 	struct i40e_netdev_priv *np = netdev_priv(dev);
13285 	struct i40e_vsi *vsi = np->vsi;
13286 	struct i40e_pf *pf = vsi->back;
13287 	struct i40e_veb *veb = NULL;
13288 	int i;
13289 
13290 	/* Only for PF VSI for now */
13291 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13292 		return -EOPNOTSUPP;
13293 
13294 	/* Find the HW bridge for the PF VSI */
13295 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13296 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13297 			veb = pf->veb[i];
13298 	}
13299 
13300 	if (!veb)
13301 		return 0;
13302 
13303 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13304 				       0, 0, nlflags, filter_mask, NULL);
13305 }
13306 
13307 /**
13308  * i40e_features_check - Validate encapsulated packet conforms to limits
13309  * @skb: skb buff
13310  * @dev: This physical port's netdev
13311  * @features: Offload features that the stack believes apply
13312  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13313 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13314 					     struct net_device *dev,
13315 					     netdev_features_t features)
13316 {
13317 	size_t len;
13318 
13319 	/* No point in doing any of this if neither checksum nor GSO are
13320 	 * being requested for this frame.  We can rule out both by just
13321 	 * checking for CHECKSUM_PARTIAL
13322 	 */
13323 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13324 		return features;
13325 
13326 	/* We cannot support GSO if the MSS is going to be less than
13327 	 * 64 bytes.  If it is then we need to drop support for GSO.
13328 	 */
13329 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13330 		features &= ~NETIF_F_GSO_MASK;
13331 
13332 	/* MACLEN can support at most 63 words */
13333 	len = skb_network_header(skb) - skb->data;
13334 	if (len & ~(63 * 2))
13335 		goto out_err;
13336 
13337 	/* IPLEN and EIPLEN can support at most 127 dwords */
13338 	len = skb_transport_header(skb) - skb_network_header(skb);
13339 	if (len & ~(127 * 4))
13340 		goto out_err;
13341 
13342 	if (skb->encapsulation) {
13343 		/* L4TUNLEN can support 127 words */
13344 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13345 		if (len & ~(127 * 2))
13346 			goto out_err;
13347 
13348 		/* IPLEN can support at most 127 dwords */
13349 		len = skb_inner_transport_header(skb) -
13350 		      skb_inner_network_header(skb);
13351 		if (len & ~(127 * 4))
13352 			goto out_err;
13353 	}
13354 
13355 	/* No need to validate L4LEN as TCP is the only protocol with a
13356 	 * flexible value and we support all possible values supported
13357 	 * by TCP, which is at most 15 dwords
13358 	 */
13359 
13360 	return features;
13361 out_err:
13362 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13363 }
13364 
13365 /**
13366  * i40e_xdp_setup - add/remove an XDP program
13367  * @vsi: VSI to changed
13368  * @prog: XDP program
13369  * @extack: netlink extended ack
13370  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13371 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13372 			  struct netlink_ext_ack *extack)
13373 {
13374 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13375 	struct i40e_pf *pf = vsi->back;
13376 	struct bpf_prog *old_prog;
13377 	bool need_reset;
13378 	int i;
13379 
13380 	/* VSI shall be deleted in a moment, block loading new programs */
13381 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13382 		return -EINVAL;
13383 
13384 	/* Don't allow frames that span over multiple buffers */
13385 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13386 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13387 		return -EINVAL;
13388 	}
13389 
13390 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13391 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13392 	if (need_reset)
13393 		i40e_prep_for_reset(pf);
13394 
13395 	old_prog = xchg(&vsi->xdp_prog, prog);
13396 
13397 	if (need_reset) {
13398 		if (!prog) {
13399 			xdp_features_clear_redirect_target(vsi->netdev);
13400 			/* Wait until ndo_xsk_wakeup completes. */
13401 			synchronize_rcu();
13402 		}
13403 		i40e_reset_and_rebuild(pf, true, true);
13404 	}
13405 
13406 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13407 		if (i40e_realloc_rx_bi_zc(vsi, true))
13408 			return -ENOMEM;
13409 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13410 		if (i40e_realloc_rx_bi_zc(vsi, false))
13411 			return -ENOMEM;
13412 	}
13413 
13414 	for (i = 0; i < vsi->num_queue_pairs; i++)
13415 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13416 
13417 	if (old_prog)
13418 		bpf_prog_put(old_prog);
13419 
13420 	/* Kick start the NAPI context if there is an AF_XDP socket open
13421 	 * on that queue id. This so that receiving will start.
13422 	 */
13423 	if (need_reset && prog) {
13424 		for (i = 0; i < vsi->num_queue_pairs; i++)
13425 			if (vsi->xdp_rings[i]->xsk_pool)
13426 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13427 						      XDP_WAKEUP_RX);
13428 		xdp_features_set_redirect_target(vsi->netdev, true);
13429 	}
13430 
13431 	return 0;
13432 }
13433 
13434 /**
13435  * i40e_enter_busy_conf - Enters busy config state
13436  * @vsi: vsi
13437  *
13438  * Returns 0 on success, <0 for failure.
13439  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13440 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13441 {
13442 	struct i40e_pf *pf = vsi->back;
13443 	int timeout = 50;
13444 
13445 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13446 		timeout--;
13447 		if (!timeout)
13448 			return -EBUSY;
13449 		usleep_range(1000, 2000);
13450 	}
13451 
13452 	return 0;
13453 }
13454 
13455 /**
13456  * i40e_exit_busy_conf - Exits busy config state
13457  * @vsi: vsi
13458  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13459 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13460 {
13461 	struct i40e_pf *pf = vsi->back;
13462 
13463 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13464 }
13465 
13466 /**
13467  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13468  * @vsi: vsi
13469  * @queue_pair: queue pair
13470  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13471 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13472 {
13473 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13474 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13475 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13476 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13477 	if (i40e_enabled_xdp_vsi(vsi)) {
13478 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13479 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13480 	}
13481 }
13482 
13483 /**
13484  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13485  * @vsi: vsi
13486  * @queue_pair: queue pair
13487  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13488 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13489 {
13490 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13491 	if (i40e_enabled_xdp_vsi(vsi)) {
13492 		/* Make sure that in-progress ndo_xdp_xmit calls are
13493 		 * completed.
13494 		 */
13495 		synchronize_rcu();
13496 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13497 	}
13498 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13499 }
13500 
13501 /**
13502  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13503  * @vsi: vsi
13504  * @queue_pair: queue pair
13505  * @enable: true for enable, false for disable
13506  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13507 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13508 					bool enable)
13509 {
13510 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13511 	struct i40e_q_vector *q_vector = rxr->q_vector;
13512 
13513 	if (!vsi->netdev)
13514 		return;
13515 
13516 	/* All rings in a qp belong to the same qvector. */
13517 	if (q_vector->rx.ring || q_vector->tx.ring) {
13518 		if (enable)
13519 			napi_enable(&q_vector->napi);
13520 		else
13521 			napi_disable(&q_vector->napi);
13522 	}
13523 }
13524 
13525 /**
13526  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13527  * @vsi: vsi
13528  * @queue_pair: queue pair
13529  * @enable: true for enable, false for disable
13530  *
13531  * Returns 0 on success, <0 on failure.
13532  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13533 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13534 					bool enable)
13535 {
13536 	struct i40e_pf *pf = vsi->back;
13537 	int pf_q, ret = 0;
13538 
13539 	pf_q = vsi->base_queue + queue_pair;
13540 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13541 				     false /*is xdp*/, enable);
13542 	if (ret) {
13543 		dev_info(&pf->pdev->dev,
13544 			 "VSI seid %d Tx ring %d %sable timeout\n",
13545 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13546 		return ret;
13547 	}
13548 
13549 	i40e_control_rx_q(pf, pf_q, enable);
13550 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13551 	if (ret) {
13552 		dev_info(&pf->pdev->dev,
13553 			 "VSI seid %d Rx ring %d %sable timeout\n",
13554 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13555 		return ret;
13556 	}
13557 
13558 	/* Due to HW errata, on Rx disable only, the register can
13559 	 * indicate done before it really is. Needs 50ms to be sure
13560 	 */
13561 	if (!enable)
13562 		mdelay(50);
13563 
13564 	if (!i40e_enabled_xdp_vsi(vsi))
13565 		return ret;
13566 
13567 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13568 				     pf_q + vsi->alloc_queue_pairs,
13569 				     true /*is xdp*/, enable);
13570 	if (ret) {
13571 		dev_info(&pf->pdev->dev,
13572 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13573 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13574 	}
13575 
13576 	return ret;
13577 }
13578 
13579 /**
13580  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13581  * @vsi: vsi
13582  * @queue_pair: queue_pair
13583  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13584 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13585 {
13586 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13587 	struct i40e_pf *pf = vsi->back;
13588 	struct i40e_hw *hw = &pf->hw;
13589 
13590 	/* All rings in a qp belong to the same qvector. */
13591 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13592 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13593 	else
13594 		i40e_irq_dynamic_enable_icr0(pf);
13595 
13596 	i40e_flush(hw);
13597 }
13598 
13599 /**
13600  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13601  * @vsi: vsi
13602  * @queue_pair: queue_pair
13603  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13604 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13605 {
13606 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13607 	struct i40e_pf *pf = vsi->back;
13608 	struct i40e_hw *hw = &pf->hw;
13609 
13610 	/* For simplicity, instead of removing the qp interrupt causes
13611 	 * from the interrupt linked list, we simply disable the interrupt, and
13612 	 * leave the list intact.
13613 	 *
13614 	 * All rings in a qp belong to the same qvector.
13615 	 */
13616 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13617 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13618 
13619 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13620 		i40e_flush(hw);
13621 		synchronize_irq(pf->msix_entries[intpf].vector);
13622 	} else {
13623 		/* Legacy and MSI mode - this stops all interrupt handling */
13624 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13625 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13626 		i40e_flush(hw);
13627 		synchronize_irq(pf->pdev->irq);
13628 	}
13629 }
13630 
13631 /**
13632  * i40e_queue_pair_disable - Disables a queue pair
13633  * @vsi: vsi
13634  * @queue_pair: queue pair
13635  *
13636  * Returns 0 on success, <0 on failure.
13637  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13638 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13639 {
13640 	int err;
13641 
13642 	err = i40e_enter_busy_conf(vsi);
13643 	if (err)
13644 		return err;
13645 
13646 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13647 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13648 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13649 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13650 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13651 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13652 
13653 	return err;
13654 }
13655 
13656 /**
13657  * i40e_queue_pair_enable - Enables a queue pair
13658  * @vsi: vsi
13659  * @queue_pair: queue pair
13660  *
13661  * Returns 0 on success, <0 on failure.
13662  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13663 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13664 {
13665 	int err;
13666 
13667 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13668 	if (err)
13669 		return err;
13670 
13671 	if (i40e_enabled_xdp_vsi(vsi)) {
13672 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13673 		if (err)
13674 			return err;
13675 	}
13676 
13677 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13678 	if (err)
13679 		return err;
13680 
13681 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13682 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13683 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13684 
13685 	i40e_exit_busy_conf(vsi);
13686 
13687 	return err;
13688 }
13689 
13690 /**
13691  * i40e_xdp - implements ndo_bpf for i40e
13692  * @dev: netdevice
13693  * @xdp: XDP command
13694  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13695 static int i40e_xdp(struct net_device *dev,
13696 		    struct netdev_bpf *xdp)
13697 {
13698 	struct i40e_netdev_priv *np = netdev_priv(dev);
13699 	struct i40e_vsi *vsi = np->vsi;
13700 
13701 	if (vsi->type != I40E_VSI_MAIN)
13702 		return -EINVAL;
13703 
13704 	switch (xdp->command) {
13705 	case XDP_SETUP_PROG:
13706 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13707 	case XDP_SETUP_XSK_POOL:
13708 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13709 					   xdp->xsk.queue_id);
13710 	default:
13711 		return -EINVAL;
13712 	}
13713 }
13714 
13715 static const struct net_device_ops i40e_netdev_ops = {
13716 	.ndo_open		= i40e_open,
13717 	.ndo_stop		= i40e_close,
13718 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13719 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13720 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13721 	.ndo_validate_addr	= eth_validate_addr,
13722 	.ndo_set_mac_address	= i40e_set_mac,
13723 	.ndo_change_mtu		= i40e_change_mtu,
13724 	.ndo_eth_ioctl		= i40e_ioctl,
13725 	.ndo_tx_timeout		= i40e_tx_timeout,
13726 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13727 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13728 #ifdef CONFIG_NET_POLL_CONTROLLER
13729 	.ndo_poll_controller	= i40e_netpoll,
13730 #endif
13731 	.ndo_setup_tc		= __i40e_setup_tc,
13732 	.ndo_select_queue	= i40e_lan_select_queue,
13733 	.ndo_set_features	= i40e_set_features,
13734 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13735 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13736 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13737 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13738 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13739 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13740 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13741 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13742 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13743 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13744 	.ndo_features_check	= i40e_features_check,
13745 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13746 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13747 	.ndo_bpf		= i40e_xdp,
13748 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13749 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13750 	.ndo_dfwd_add_station	= i40e_fwd_add,
13751 	.ndo_dfwd_del_station	= i40e_fwd_del,
13752 };
13753 
13754 /**
13755  * i40e_config_netdev - Setup the netdev flags
13756  * @vsi: the VSI being configured
13757  *
13758  * Returns 0 on success, negative value on failure
13759  **/
i40e_config_netdev(struct i40e_vsi * vsi)13760 static int i40e_config_netdev(struct i40e_vsi *vsi)
13761 {
13762 	struct i40e_pf *pf = vsi->back;
13763 	struct i40e_hw *hw = &pf->hw;
13764 	struct i40e_netdev_priv *np;
13765 	struct net_device *netdev;
13766 	u8 broadcast[ETH_ALEN];
13767 	u8 mac_addr[ETH_ALEN];
13768 	int etherdev_size;
13769 	netdev_features_t hw_enc_features;
13770 	netdev_features_t hw_features;
13771 
13772 	etherdev_size = sizeof(struct i40e_netdev_priv);
13773 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13774 	if (!netdev)
13775 		return -ENOMEM;
13776 
13777 	vsi->netdev = netdev;
13778 	np = netdev_priv(netdev);
13779 	np->vsi = vsi;
13780 
13781 	hw_enc_features = NETIF_F_SG			|
13782 			  NETIF_F_HW_CSUM		|
13783 			  NETIF_F_HIGHDMA		|
13784 			  NETIF_F_SOFT_FEATURES		|
13785 			  NETIF_F_TSO			|
13786 			  NETIF_F_TSO_ECN		|
13787 			  NETIF_F_TSO6			|
13788 			  NETIF_F_GSO_GRE		|
13789 			  NETIF_F_GSO_GRE_CSUM		|
13790 			  NETIF_F_GSO_PARTIAL		|
13791 			  NETIF_F_GSO_IPXIP4		|
13792 			  NETIF_F_GSO_IPXIP6		|
13793 			  NETIF_F_GSO_UDP_TUNNEL	|
13794 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13795 			  NETIF_F_GSO_UDP_L4		|
13796 			  NETIF_F_SCTP_CRC		|
13797 			  NETIF_F_RXHASH		|
13798 			  NETIF_F_RXCSUM		|
13799 			  0;
13800 
13801 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13802 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13803 
13804 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13805 
13806 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13807 
13808 	netdev->hw_enc_features |= hw_enc_features;
13809 
13810 	/* record features VLANs can make use of */
13811 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13812 
13813 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13814 				   NETIF_F_GSO_GRE_CSUM |	\
13815 				   NETIF_F_GSO_IPXIP4 |		\
13816 				   NETIF_F_GSO_IPXIP6 |		\
13817 				   NETIF_F_GSO_UDP_TUNNEL |	\
13818 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13819 
13820 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13821 	netdev->features |= NETIF_F_GSO_PARTIAL |
13822 			    I40E_GSO_PARTIAL_FEATURES;
13823 
13824 	netdev->mpls_features |= NETIF_F_SG;
13825 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13826 	netdev->mpls_features |= NETIF_F_TSO;
13827 	netdev->mpls_features |= NETIF_F_TSO6;
13828 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13829 
13830 	/* enable macvlan offloads */
13831 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13832 
13833 	hw_features = hw_enc_features		|
13834 		      NETIF_F_HW_VLAN_CTAG_TX	|
13835 		      NETIF_F_HW_VLAN_CTAG_RX;
13836 
13837 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13838 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13839 
13840 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13841 
13842 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13843 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13844 
13845 	netdev->features &= ~NETIF_F_HW_TC;
13846 
13847 	if (vsi->type == I40E_VSI_MAIN) {
13848 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13849 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13850 		/* The following steps are necessary for two reasons. First,
13851 		 * some older NVM configurations load a default MAC-VLAN
13852 		 * filter that will accept any tagged packet, and we want to
13853 		 * replace this with a normal filter. Additionally, it is
13854 		 * possible our MAC address was provided by the platform using
13855 		 * Open Firmware or similar.
13856 		 *
13857 		 * Thus, we need to remove the default filter and install one
13858 		 * specific to the MAC address.
13859 		 */
13860 		i40e_rm_default_mac_filter(vsi, mac_addr);
13861 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13862 		i40e_add_mac_filter(vsi, mac_addr);
13863 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13864 
13865 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13866 				       NETDEV_XDP_ACT_REDIRECT |
13867 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13868 				       NETDEV_XDP_ACT_RX_SG;
13869 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13870 	} else {
13871 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13872 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13873 		 * the end, which is 4 bytes long, so force truncation of the
13874 		 * original name by IFNAMSIZ - 4
13875 		 */
13876 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13877 			 IFNAMSIZ - 4,
13878 			 pf->vsi[pf->lan_vsi]->netdev->name);
13879 		eth_random_addr(mac_addr);
13880 
13881 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13882 		i40e_add_mac_filter(vsi, mac_addr);
13883 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13884 	}
13885 
13886 	/* Add the broadcast filter so that we initially will receive
13887 	 * broadcast packets. Note that when a new VLAN is first added the
13888 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13889 	 * specific filters as part of transitioning into "vlan" operation.
13890 	 * When more VLANs are added, the driver will copy each existing MAC
13891 	 * filter and add it for the new VLAN.
13892 	 *
13893 	 * Broadcast filters are handled specially by
13894 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13895 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13896 	 * filter. The subtask will update the correct broadcast promiscuous
13897 	 * bits as VLANs become active or inactive.
13898 	 */
13899 	eth_broadcast_addr(broadcast);
13900 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13901 	i40e_add_mac_filter(vsi, broadcast);
13902 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13903 
13904 	eth_hw_addr_set(netdev, mac_addr);
13905 	ether_addr_copy(netdev->perm_addr, mac_addr);
13906 
13907 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13908 	netdev->neigh_priv_len = sizeof(u32) * 4;
13909 
13910 	netdev->priv_flags |= IFF_UNICAST_FLT;
13911 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13912 	/* Setup netdev TC information */
13913 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13914 
13915 	netdev->netdev_ops = &i40e_netdev_ops;
13916 	netdev->watchdog_timeo = 5 * HZ;
13917 	i40e_set_ethtool_ops(netdev);
13918 
13919 	/* MTU range: 68 - 9706 */
13920 	netdev->min_mtu = ETH_MIN_MTU;
13921 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13922 
13923 	return 0;
13924 }
13925 
13926 /**
13927  * i40e_vsi_delete - Delete a VSI from the switch
13928  * @vsi: the VSI being removed
13929  *
13930  * Returns 0 on success, negative value on failure
13931  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13932 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13933 {
13934 	/* remove default VSI is not allowed */
13935 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13936 		return;
13937 
13938 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13939 }
13940 
13941 /**
13942  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13943  * @vsi: the VSI being queried
13944  *
13945  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13946  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13947 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13948 {
13949 	struct i40e_veb *veb;
13950 	struct i40e_pf *pf = vsi->back;
13951 
13952 	/* Uplink is not a bridge so default to VEB */
13953 	if (vsi->veb_idx >= I40E_MAX_VEB)
13954 		return 1;
13955 
13956 	veb = pf->veb[vsi->veb_idx];
13957 	if (!veb) {
13958 		dev_info(&pf->pdev->dev,
13959 			 "There is no veb associated with the bridge\n");
13960 		return -ENOENT;
13961 	}
13962 
13963 	/* Uplink is a bridge in VEPA mode */
13964 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13965 		return 0;
13966 	} else {
13967 		/* Uplink is a bridge in VEB mode */
13968 		return 1;
13969 	}
13970 
13971 	/* VEPA is now default bridge, so return 0 */
13972 	return 0;
13973 }
13974 
13975 /**
13976  * i40e_add_vsi - Add a VSI to the switch
13977  * @vsi: the VSI being configured
13978  *
13979  * This initializes a VSI context depending on the VSI type to be added and
13980  * passes it down to the add_vsi aq command.
13981  **/
i40e_add_vsi(struct i40e_vsi * vsi)13982 static int i40e_add_vsi(struct i40e_vsi *vsi)
13983 {
13984 	int ret = -ENODEV;
13985 	struct i40e_pf *pf = vsi->back;
13986 	struct i40e_hw *hw = &pf->hw;
13987 	struct i40e_vsi_context ctxt;
13988 	struct i40e_mac_filter *f;
13989 	struct hlist_node *h;
13990 	int bkt;
13991 
13992 	u8 enabled_tc = 0x1; /* TC0 enabled */
13993 	int f_count = 0;
13994 
13995 	memset(&ctxt, 0, sizeof(ctxt));
13996 	switch (vsi->type) {
13997 	case I40E_VSI_MAIN:
13998 		/* The PF's main VSI is already setup as part of the
13999 		 * device initialization, so we'll not bother with
14000 		 * the add_vsi call, but we will retrieve the current
14001 		 * VSI context.
14002 		 */
14003 		ctxt.seid = pf->main_vsi_seid;
14004 		ctxt.pf_num = pf->hw.pf_id;
14005 		ctxt.vf_num = 0;
14006 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
14007 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14008 		if (ret) {
14009 			dev_info(&pf->pdev->dev,
14010 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
14011 				 ERR_PTR(ret),
14012 				 i40e_aq_str(&pf->hw,
14013 					     pf->hw.aq.asq_last_status));
14014 			return -ENOENT;
14015 		}
14016 		vsi->info = ctxt.info;
14017 		vsi->info.valid_sections = 0;
14018 
14019 		vsi->seid = ctxt.seid;
14020 		vsi->id = ctxt.vsi_number;
14021 
14022 		enabled_tc = i40e_pf_get_tc_map(pf);
14023 
14024 		/* Source pruning is enabled by default, so the flag is
14025 		 * negative logic - if it's set, we need to fiddle with
14026 		 * the VSI to disable source pruning.
14027 		 */
14028 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
14029 			memset(&ctxt, 0, sizeof(ctxt));
14030 			ctxt.seid = pf->main_vsi_seid;
14031 			ctxt.pf_num = pf->hw.pf_id;
14032 			ctxt.vf_num = 0;
14033 			ctxt.info.valid_sections |=
14034 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14035 			ctxt.info.switch_id =
14036 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
14037 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14038 			if (ret) {
14039 				dev_info(&pf->pdev->dev,
14040 					 "update vsi failed, err %d aq_err %s\n",
14041 					 ret,
14042 					 i40e_aq_str(&pf->hw,
14043 						     pf->hw.aq.asq_last_status));
14044 				ret = -ENOENT;
14045 				goto err;
14046 			}
14047 		}
14048 
14049 		/* MFP mode setup queue map and update VSI */
14050 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14051 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14052 			memset(&ctxt, 0, sizeof(ctxt));
14053 			ctxt.seid = pf->main_vsi_seid;
14054 			ctxt.pf_num = pf->hw.pf_id;
14055 			ctxt.vf_num = 0;
14056 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14057 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14058 			if (ret) {
14059 				dev_info(&pf->pdev->dev,
14060 					 "update vsi failed, err %pe aq_err %s\n",
14061 					 ERR_PTR(ret),
14062 					 i40e_aq_str(&pf->hw,
14063 						    pf->hw.aq.asq_last_status));
14064 				ret = -ENOENT;
14065 				goto err;
14066 			}
14067 			/* update the local VSI info queue map */
14068 			i40e_vsi_update_queue_map(vsi, &ctxt);
14069 			vsi->info.valid_sections = 0;
14070 		} else {
14071 			/* Default/Main VSI is only enabled for TC0
14072 			 * reconfigure it to enable all TCs that are
14073 			 * available on the port in SFP mode.
14074 			 * For MFP case the iSCSI PF would use this
14075 			 * flow to enable LAN+iSCSI TC.
14076 			 */
14077 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14078 			if (ret) {
14079 				/* Single TC condition is not fatal,
14080 				 * message and continue
14081 				 */
14082 				dev_info(&pf->pdev->dev,
14083 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14084 					 enabled_tc,
14085 					 ERR_PTR(ret),
14086 					 i40e_aq_str(&pf->hw,
14087 						    pf->hw.aq.asq_last_status));
14088 			}
14089 		}
14090 		break;
14091 
14092 	case I40E_VSI_FDIR:
14093 		ctxt.pf_num = hw->pf_id;
14094 		ctxt.vf_num = 0;
14095 		ctxt.uplink_seid = vsi->uplink_seid;
14096 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14097 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14098 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14099 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14100 			ctxt.info.valid_sections |=
14101 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14102 			ctxt.info.switch_id =
14103 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14104 		}
14105 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14106 		break;
14107 
14108 	case I40E_VSI_VMDQ2:
14109 		ctxt.pf_num = hw->pf_id;
14110 		ctxt.vf_num = 0;
14111 		ctxt.uplink_seid = vsi->uplink_seid;
14112 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14113 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14114 
14115 		/* This VSI is connected to VEB so the switch_id
14116 		 * should be set to zero by default.
14117 		 */
14118 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14119 			ctxt.info.valid_sections |=
14120 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14121 			ctxt.info.switch_id =
14122 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14123 		}
14124 
14125 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14126 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14127 		break;
14128 
14129 	case I40E_VSI_SRIOV:
14130 		ctxt.pf_num = hw->pf_id;
14131 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14132 		ctxt.uplink_seid = vsi->uplink_seid;
14133 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14134 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14135 
14136 		/* This VSI is connected to VEB so the switch_id
14137 		 * should be set to zero by default.
14138 		 */
14139 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14140 			ctxt.info.valid_sections |=
14141 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14142 			ctxt.info.switch_id =
14143 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14144 		}
14145 
14146 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14147 			ctxt.info.valid_sections |=
14148 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14149 			ctxt.info.queueing_opt_flags |=
14150 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14151 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14152 		}
14153 
14154 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14155 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14156 		if (pf->vf[vsi->vf_id].spoofchk) {
14157 			ctxt.info.valid_sections |=
14158 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14159 			ctxt.info.sec_flags |=
14160 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14161 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14162 		}
14163 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14164 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14165 		break;
14166 
14167 	case I40E_VSI_IWARP:
14168 		/* send down message to iWARP */
14169 		break;
14170 
14171 	default:
14172 		return -ENODEV;
14173 	}
14174 
14175 	if (vsi->type != I40E_VSI_MAIN) {
14176 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14177 		if (ret) {
14178 			dev_info(&vsi->back->pdev->dev,
14179 				 "add vsi failed, err %pe aq_err %s\n",
14180 				 ERR_PTR(ret),
14181 				 i40e_aq_str(&pf->hw,
14182 					     pf->hw.aq.asq_last_status));
14183 			ret = -ENOENT;
14184 			goto err;
14185 		}
14186 		vsi->info = ctxt.info;
14187 		vsi->info.valid_sections = 0;
14188 		vsi->seid = ctxt.seid;
14189 		vsi->id = ctxt.vsi_number;
14190 	}
14191 
14192 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14193 	vsi->active_filters = 0;
14194 	/* If macvlan filters already exist, force them to get loaded */
14195 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14196 		f->state = I40E_FILTER_NEW;
14197 		f_count++;
14198 	}
14199 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14200 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14201 
14202 	if (f_count) {
14203 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14204 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14205 	}
14206 
14207 	/* Update VSI BW information */
14208 	ret = i40e_vsi_get_bw_info(vsi);
14209 	if (ret) {
14210 		dev_info(&pf->pdev->dev,
14211 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14212 			 ERR_PTR(ret),
14213 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14214 		/* VSI is already added so not tearing that up */
14215 		ret = 0;
14216 	}
14217 
14218 err:
14219 	return ret;
14220 }
14221 
14222 /**
14223  * i40e_vsi_release - Delete a VSI and free its resources
14224  * @vsi: the VSI being removed
14225  *
14226  * Returns 0 on success or < 0 on error
14227  **/
i40e_vsi_release(struct i40e_vsi * vsi)14228 int i40e_vsi_release(struct i40e_vsi *vsi)
14229 {
14230 	struct i40e_mac_filter *f;
14231 	struct hlist_node *h;
14232 	struct i40e_veb *veb = NULL;
14233 	struct i40e_pf *pf;
14234 	u16 uplink_seid;
14235 	int i, n, bkt;
14236 
14237 	pf = vsi->back;
14238 
14239 	/* release of a VEB-owner or last VSI is not allowed */
14240 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14241 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14242 			 vsi->seid, vsi->uplink_seid);
14243 		return -ENODEV;
14244 	}
14245 	if (vsi == pf->vsi[pf->lan_vsi] &&
14246 	    !test_bit(__I40E_DOWN, pf->state)) {
14247 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14248 		return -ENODEV;
14249 	}
14250 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14251 	uplink_seid = vsi->uplink_seid;
14252 	if (vsi->type != I40E_VSI_SRIOV) {
14253 		if (vsi->netdev_registered) {
14254 			vsi->netdev_registered = false;
14255 			if (vsi->netdev) {
14256 				/* results in a call to i40e_close() */
14257 				unregister_netdev(vsi->netdev);
14258 			}
14259 		} else {
14260 			i40e_vsi_close(vsi);
14261 		}
14262 		i40e_vsi_disable_irq(vsi);
14263 	}
14264 
14265 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14266 
14267 	/* clear the sync flag on all filters */
14268 	if (vsi->netdev) {
14269 		__dev_uc_unsync(vsi->netdev, NULL);
14270 		__dev_mc_unsync(vsi->netdev, NULL);
14271 	}
14272 
14273 	/* make sure any remaining filters are marked for deletion */
14274 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14275 		__i40e_del_filter(vsi, f);
14276 
14277 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14278 
14279 	i40e_sync_vsi_filters(vsi);
14280 
14281 	i40e_vsi_delete(vsi);
14282 	i40e_vsi_free_q_vectors(vsi);
14283 	if (vsi->netdev) {
14284 		free_netdev(vsi->netdev);
14285 		vsi->netdev = NULL;
14286 	}
14287 	i40e_vsi_clear_rings(vsi);
14288 	i40e_vsi_clear(vsi);
14289 
14290 	/* If this was the last thing on the VEB, except for the
14291 	 * controlling VSI, remove the VEB, which puts the controlling
14292 	 * VSI onto the next level down in the switch.
14293 	 *
14294 	 * Well, okay, there's one more exception here: don't remove
14295 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14296 	 * from up the network stack.
14297 	 */
14298 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14299 		if (pf->vsi[i] &&
14300 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14301 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14302 			n++;      /* count the VSIs */
14303 		}
14304 	}
14305 	for (i = 0; i < I40E_MAX_VEB; i++) {
14306 		if (!pf->veb[i])
14307 			continue;
14308 		if (pf->veb[i]->uplink_seid == uplink_seid)
14309 			n++;     /* count the VEBs */
14310 		if (pf->veb[i]->seid == uplink_seid)
14311 			veb = pf->veb[i];
14312 	}
14313 	if (n == 0 && veb && veb->uplink_seid != 0)
14314 		i40e_veb_release(veb);
14315 
14316 	return 0;
14317 }
14318 
14319 /**
14320  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14321  * @vsi: ptr to the VSI
14322  *
14323  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14324  * corresponding SW VSI structure and initializes num_queue_pairs for the
14325  * newly allocated VSI.
14326  *
14327  * Returns 0 on success or negative on failure
14328  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14329 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14330 {
14331 	int ret = -ENOENT;
14332 	struct i40e_pf *pf = vsi->back;
14333 
14334 	if (vsi->q_vectors[0]) {
14335 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14336 			 vsi->seid);
14337 		return -EEXIST;
14338 	}
14339 
14340 	if (vsi->base_vector) {
14341 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14342 			 vsi->seid, vsi->base_vector);
14343 		return -EEXIST;
14344 	}
14345 
14346 	ret = i40e_vsi_alloc_q_vectors(vsi);
14347 	if (ret) {
14348 		dev_info(&pf->pdev->dev,
14349 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14350 			 vsi->num_q_vectors, vsi->seid, ret);
14351 		vsi->num_q_vectors = 0;
14352 		goto vector_setup_out;
14353 	}
14354 
14355 	/* In Legacy mode, we do not have to get any other vector since we
14356 	 * piggyback on the misc/ICR0 for queue interrupts.
14357 	*/
14358 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14359 		return ret;
14360 	if (vsi->num_q_vectors)
14361 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14362 						 vsi->num_q_vectors, vsi->idx);
14363 	if (vsi->base_vector < 0) {
14364 		dev_info(&pf->pdev->dev,
14365 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14366 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14367 		i40e_vsi_free_q_vectors(vsi);
14368 		ret = -ENOENT;
14369 		goto vector_setup_out;
14370 	}
14371 
14372 vector_setup_out:
14373 	return ret;
14374 }
14375 
14376 /**
14377  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14378  * @vsi: pointer to the vsi.
14379  *
14380  * This re-allocates a vsi's queue resources.
14381  *
14382  * Returns pointer to the successfully allocated and configured VSI sw struct
14383  * on success, otherwise returns NULL on failure.
14384  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14385 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14386 {
14387 	u16 alloc_queue_pairs;
14388 	struct i40e_pf *pf;
14389 	u8 enabled_tc;
14390 	int ret;
14391 
14392 	if (!vsi)
14393 		return NULL;
14394 
14395 	pf = vsi->back;
14396 
14397 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14398 	i40e_vsi_clear_rings(vsi);
14399 
14400 	i40e_vsi_free_arrays(vsi, false);
14401 	i40e_set_num_rings_in_vsi(vsi);
14402 	ret = i40e_vsi_alloc_arrays(vsi, false);
14403 	if (ret)
14404 		goto err_vsi;
14405 
14406 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14407 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14408 
14409 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14410 	if (ret < 0) {
14411 		dev_info(&pf->pdev->dev,
14412 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14413 			 alloc_queue_pairs, vsi->seid, ret);
14414 		goto err_vsi;
14415 	}
14416 	vsi->base_queue = ret;
14417 
14418 	/* Update the FW view of the VSI. Force a reset of TC and queue
14419 	 * layout configurations.
14420 	 */
14421 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14422 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14423 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14424 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14425 	if (vsi->type == I40E_VSI_MAIN)
14426 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14427 
14428 	/* assign it some queues */
14429 	ret = i40e_alloc_rings(vsi);
14430 	if (ret)
14431 		goto err_rings;
14432 
14433 	/* map all of the rings to the q_vectors */
14434 	i40e_vsi_map_rings_to_vectors(vsi);
14435 	return vsi;
14436 
14437 err_rings:
14438 	i40e_vsi_free_q_vectors(vsi);
14439 	if (vsi->netdev_registered) {
14440 		vsi->netdev_registered = false;
14441 		unregister_netdev(vsi->netdev);
14442 		free_netdev(vsi->netdev);
14443 		vsi->netdev = NULL;
14444 	}
14445 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14446 err_vsi:
14447 	i40e_vsi_clear(vsi);
14448 	return NULL;
14449 }
14450 
14451 /**
14452  * i40e_vsi_setup - Set up a VSI by a given type
14453  * @pf: board private structure
14454  * @type: VSI type
14455  * @uplink_seid: the switch element to link to
14456  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14457  *
14458  * This allocates the sw VSI structure and its queue resources, then add a VSI
14459  * to the identified VEB.
14460  *
14461  * Returns pointer to the successfully allocated and configure VSI sw struct on
14462  * success, otherwise returns NULL on failure.
14463  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14464 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14465 				u16 uplink_seid, u32 param1)
14466 {
14467 	struct i40e_vsi *vsi = NULL;
14468 	struct i40e_veb *veb = NULL;
14469 	u16 alloc_queue_pairs;
14470 	int ret, i;
14471 	int v_idx;
14472 
14473 	/* The requested uplink_seid must be either
14474 	 *     - the PF's port seid
14475 	 *              no VEB is needed because this is the PF
14476 	 *              or this is a Flow Director special case VSI
14477 	 *     - seid of an existing VEB
14478 	 *     - seid of a VSI that owns an existing VEB
14479 	 *     - seid of a VSI that doesn't own a VEB
14480 	 *              a new VEB is created and the VSI becomes the owner
14481 	 *     - seid of the PF VSI, which is what creates the first VEB
14482 	 *              this is a special case of the previous
14483 	 *
14484 	 * Find which uplink_seid we were given and create a new VEB if needed
14485 	 */
14486 	for (i = 0; i < I40E_MAX_VEB; i++) {
14487 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14488 			veb = pf->veb[i];
14489 			break;
14490 		}
14491 	}
14492 
14493 	if (!veb && uplink_seid != pf->mac_seid) {
14494 
14495 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14496 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14497 				vsi = pf->vsi[i];
14498 				break;
14499 			}
14500 		}
14501 		if (!vsi) {
14502 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14503 				 uplink_seid);
14504 			return NULL;
14505 		}
14506 
14507 		if (vsi->uplink_seid == pf->mac_seid)
14508 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14509 					     vsi->tc_config.enabled_tc);
14510 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14511 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14512 					     vsi->tc_config.enabled_tc);
14513 		if (veb) {
14514 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14515 				dev_info(&vsi->back->pdev->dev,
14516 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14517 				return NULL;
14518 			}
14519 			/* We come up by default in VEPA mode if SRIOV is not
14520 			 * already enabled, in which case we can't force VEPA
14521 			 * mode.
14522 			 */
14523 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14524 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14525 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14526 			}
14527 			i40e_config_bridge_mode(veb);
14528 		}
14529 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14530 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14531 				veb = pf->veb[i];
14532 		}
14533 		if (!veb) {
14534 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14535 			return NULL;
14536 		}
14537 
14538 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14539 		uplink_seid = veb->seid;
14540 	}
14541 
14542 	/* get vsi sw struct */
14543 	v_idx = i40e_vsi_mem_alloc(pf, type);
14544 	if (v_idx < 0)
14545 		goto err_alloc;
14546 	vsi = pf->vsi[v_idx];
14547 	if (!vsi)
14548 		goto err_alloc;
14549 	vsi->type = type;
14550 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14551 
14552 	if (type == I40E_VSI_MAIN)
14553 		pf->lan_vsi = v_idx;
14554 	else if (type == I40E_VSI_SRIOV)
14555 		vsi->vf_id = param1;
14556 	/* assign it some queues */
14557 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14558 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14559 
14560 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14561 	if (ret < 0) {
14562 		dev_info(&pf->pdev->dev,
14563 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14564 			 alloc_queue_pairs, vsi->seid, ret);
14565 		goto err_vsi;
14566 	}
14567 	vsi->base_queue = ret;
14568 
14569 	/* get a VSI from the hardware */
14570 	vsi->uplink_seid = uplink_seid;
14571 	ret = i40e_add_vsi(vsi);
14572 	if (ret)
14573 		goto err_vsi;
14574 
14575 	switch (vsi->type) {
14576 	/* setup the netdev if needed */
14577 	case I40E_VSI_MAIN:
14578 	case I40E_VSI_VMDQ2:
14579 		ret = i40e_config_netdev(vsi);
14580 		if (ret)
14581 			goto err_netdev;
14582 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14583 		if (ret)
14584 			goto err_netdev;
14585 		ret = register_netdev(vsi->netdev);
14586 		if (ret)
14587 			goto err_netdev;
14588 		vsi->netdev_registered = true;
14589 		netif_carrier_off(vsi->netdev);
14590 #ifdef CONFIG_I40E_DCB
14591 		/* Setup DCB netlink interface */
14592 		i40e_dcbnl_setup(vsi);
14593 #endif /* CONFIG_I40E_DCB */
14594 		fallthrough;
14595 	case I40E_VSI_FDIR:
14596 		/* set up vectors and rings if needed */
14597 		ret = i40e_vsi_setup_vectors(vsi);
14598 		if (ret)
14599 			goto err_msix;
14600 
14601 		ret = i40e_alloc_rings(vsi);
14602 		if (ret)
14603 			goto err_rings;
14604 
14605 		/* map all of the rings to the q_vectors */
14606 		i40e_vsi_map_rings_to_vectors(vsi);
14607 
14608 		i40e_vsi_reset_stats(vsi);
14609 		break;
14610 	default:
14611 		/* no netdev or rings for the other VSI types */
14612 		break;
14613 	}
14614 
14615 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14616 	    (vsi->type == I40E_VSI_VMDQ2)) {
14617 		ret = i40e_vsi_config_rss(vsi);
14618 	}
14619 	return vsi;
14620 
14621 err_rings:
14622 	i40e_vsi_free_q_vectors(vsi);
14623 err_msix:
14624 	if (vsi->netdev_registered) {
14625 		vsi->netdev_registered = false;
14626 		unregister_netdev(vsi->netdev);
14627 		free_netdev(vsi->netdev);
14628 		vsi->netdev = NULL;
14629 	}
14630 err_netdev:
14631 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14632 err_vsi:
14633 	i40e_vsi_clear(vsi);
14634 err_alloc:
14635 	return NULL;
14636 }
14637 
14638 /**
14639  * i40e_veb_get_bw_info - Query VEB BW information
14640  * @veb: the veb to query
14641  *
14642  * Query the Tx scheduler BW configuration data for given VEB
14643  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14644 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14645 {
14646 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14647 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14648 	struct i40e_pf *pf = veb->pf;
14649 	struct i40e_hw *hw = &pf->hw;
14650 	u32 tc_bw_max;
14651 	int ret = 0;
14652 	int i;
14653 
14654 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14655 						  &bw_data, NULL);
14656 	if (ret) {
14657 		dev_info(&pf->pdev->dev,
14658 			 "query veb bw config failed, err %pe aq_err %s\n",
14659 			 ERR_PTR(ret),
14660 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14661 		goto out;
14662 	}
14663 
14664 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14665 						   &ets_data, NULL);
14666 	if (ret) {
14667 		dev_info(&pf->pdev->dev,
14668 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14669 			 ERR_PTR(ret),
14670 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14671 		goto out;
14672 	}
14673 
14674 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14675 	veb->bw_max_quanta = ets_data.tc_bw_max;
14676 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14677 	veb->enabled_tc = ets_data.tc_valid_bits;
14678 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14679 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14680 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14681 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14682 		veb->bw_tc_limit_credits[i] =
14683 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14684 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14685 	}
14686 
14687 out:
14688 	return ret;
14689 }
14690 
14691 /**
14692  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14693  * @pf: board private structure
14694  *
14695  * On error: returns error code (negative)
14696  * On success: returns vsi index in PF (positive)
14697  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14698 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14699 {
14700 	int ret = -ENOENT;
14701 	struct i40e_veb *veb;
14702 	int i;
14703 
14704 	/* Need to protect the allocation of switch elements at the PF level */
14705 	mutex_lock(&pf->switch_mutex);
14706 
14707 	/* VEB list may be fragmented if VEB creation/destruction has
14708 	 * been happening.  We can afford to do a quick scan to look
14709 	 * for any free slots in the list.
14710 	 *
14711 	 * find next empty veb slot, looping back around if necessary
14712 	 */
14713 	i = 0;
14714 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14715 		i++;
14716 	if (i >= I40E_MAX_VEB) {
14717 		ret = -ENOMEM;
14718 		goto err_alloc_veb;  /* out of VEB slots! */
14719 	}
14720 
14721 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14722 	if (!veb) {
14723 		ret = -ENOMEM;
14724 		goto err_alloc_veb;
14725 	}
14726 	veb->pf = pf;
14727 	veb->idx = i;
14728 	veb->enabled_tc = 1;
14729 
14730 	pf->veb[i] = veb;
14731 	ret = i;
14732 err_alloc_veb:
14733 	mutex_unlock(&pf->switch_mutex);
14734 	return ret;
14735 }
14736 
14737 /**
14738  * i40e_switch_branch_release - Delete a branch of the switch tree
14739  * @branch: where to start deleting
14740  *
14741  * This uses recursion to find the tips of the branch to be
14742  * removed, deleting until we get back to and can delete this VEB.
14743  **/
i40e_switch_branch_release(struct i40e_veb * branch)14744 static void i40e_switch_branch_release(struct i40e_veb *branch)
14745 {
14746 	struct i40e_pf *pf = branch->pf;
14747 	u16 branch_seid = branch->seid;
14748 	u16 veb_idx = branch->idx;
14749 	int i;
14750 
14751 	/* release any VEBs on this VEB - RECURSION */
14752 	for (i = 0; i < I40E_MAX_VEB; i++) {
14753 		if (!pf->veb[i])
14754 			continue;
14755 		if (pf->veb[i]->uplink_seid == branch->seid)
14756 			i40e_switch_branch_release(pf->veb[i]);
14757 	}
14758 
14759 	/* Release the VSIs on this VEB, but not the owner VSI.
14760 	 *
14761 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14762 	 *       the VEB itself, so don't use (*branch) after this loop.
14763 	 */
14764 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14765 		if (!pf->vsi[i])
14766 			continue;
14767 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14768 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14769 			i40e_vsi_release(pf->vsi[i]);
14770 		}
14771 	}
14772 
14773 	/* There's one corner case where the VEB might not have been
14774 	 * removed, so double check it here and remove it if needed.
14775 	 * This case happens if the veb was created from the debugfs
14776 	 * commands and no VSIs were added to it.
14777 	 */
14778 	if (pf->veb[veb_idx])
14779 		i40e_veb_release(pf->veb[veb_idx]);
14780 }
14781 
14782 /**
14783  * i40e_veb_clear - remove veb struct
14784  * @veb: the veb to remove
14785  **/
i40e_veb_clear(struct i40e_veb * veb)14786 static void i40e_veb_clear(struct i40e_veb *veb)
14787 {
14788 	if (!veb)
14789 		return;
14790 
14791 	if (veb->pf) {
14792 		struct i40e_pf *pf = veb->pf;
14793 
14794 		mutex_lock(&pf->switch_mutex);
14795 		if (pf->veb[veb->idx] == veb)
14796 			pf->veb[veb->idx] = NULL;
14797 		mutex_unlock(&pf->switch_mutex);
14798 	}
14799 
14800 	kfree(veb);
14801 }
14802 
14803 /**
14804  * i40e_veb_release - Delete a VEB and free its resources
14805  * @veb: the VEB being removed
14806  **/
i40e_veb_release(struct i40e_veb * veb)14807 void i40e_veb_release(struct i40e_veb *veb)
14808 {
14809 	struct i40e_vsi *vsi = NULL;
14810 	struct i40e_pf *pf;
14811 	int i, n = 0;
14812 
14813 	pf = veb->pf;
14814 
14815 	/* find the remaining VSI and check for extras */
14816 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14817 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14818 			n++;
14819 			vsi = pf->vsi[i];
14820 		}
14821 	}
14822 	if (n != 1) {
14823 		dev_info(&pf->pdev->dev,
14824 			 "can't remove VEB %d with %d VSIs left\n",
14825 			 veb->seid, n);
14826 		return;
14827 	}
14828 
14829 	/* move the remaining VSI to uplink veb */
14830 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14831 	if (veb->uplink_seid) {
14832 		vsi->uplink_seid = veb->uplink_seid;
14833 		if (veb->uplink_seid == pf->mac_seid)
14834 			vsi->veb_idx = I40E_NO_VEB;
14835 		else
14836 			vsi->veb_idx = veb->veb_idx;
14837 	} else {
14838 		/* floating VEB */
14839 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14840 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14841 	}
14842 
14843 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14844 	i40e_veb_clear(veb);
14845 }
14846 
14847 /**
14848  * i40e_add_veb - create the VEB in the switch
14849  * @veb: the VEB to be instantiated
14850  * @vsi: the controlling VSI
14851  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14852 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14853 {
14854 	struct i40e_pf *pf = veb->pf;
14855 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14856 	int ret;
14857 
14858 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14859 			      veb->enabled_tc, false,
14860 			      &veb->seid, enable_stats, NULL);
14861 
14862 	/* get a VEB from the hardware */
14863 	if (ret) {
14864 		dev_info(&pf->pdev->dev,
14865 			 "couldn't add VEB, err %pe aq_err %s\n",
14866 			 ERR_PTR(ret),
14867 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14868 		return -EPERM;
14869 	}
14870 
14871 	/* get statistics counter */
14872 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14873 					 &veb->stats_idx, NULL, NULL, NULL);
14874 	if (ret) {
14875 		dev_info(&pf->pdev->dev,
14876 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14877 			 ERR_PTR(ret),
14878 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14879 		return -EPERM;
14880 	}
14881 	ret = i40e_veb_get_bw_info(veb);
14882 	if (ret) {
14883 		dev_info(&pf->pdev->dev,
14884 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14885 			 ERR_PTR(ret),
14886 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14887 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14888 		return -ENOENT;
14889 	}
14890 
14891 	vsi->uplink_seid = veb->seid;
14892 	vsi->veb_idx = veb->idx;
14893 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14894 
14895 	return 0;
14896 }
14897 
14898 /**
14899  * i40e_veb_setup - Set up a VEB
14900  * @pf: board private structure
14901  * @flags: VEB setup flags
14902  * @uplink_seid: the switch element to link to
14903  * @vsi_seid: the initial VSI seid
14904  * @enabled_tc: Enabled TC bit-map
14905  *
14906  * This allocates the sw VEB structure and links it into the switch
14907  * It is possible and legal for this to be a duplicate of an already
14908  * existing VEB.  It is also possible for both uplink and vsi seids
14909  * to be zero, in order to create a floating VEB.
14910  *
14911  * Returns pointer to the successfully allocated VEB sw struct on
14912  * success, otherwise returns NULL on failure.
14913  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14914 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14915 				u16 uplink_seid, u16 vsi_seid,
14916 				u8 enabled_tc)
14917 {
14918 	struct i40e_veb *veb, *uplink_veb = NULL;
14919 	int vsi_idx, veb_idx;
14920 	int ret;
14921 
14922 	/* if one seid is 0, the other must be 0 to create a floating relay */
14923 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14924 	    (uplink_seid + vsi_seid != 0)) {
14925 		dev_info(&pf->pdev->dev,
14926 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14927 			 uplink_seid, vsi_seid);
14928 		return NULL;
14929 	}
14930 
14931 	/* make sure there is such a vsi and uplink */
14932 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14933 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14934 			break;
14935 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14936 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14937 			 vsi_seid);
14938 		return NULL;
14939 	}
14940 
14941 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14942 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14943 			if (pf->veb[veb_idx] &&
14944 			    pf->veb[veb_idx]->seid == uplink_seid) {
14945 				uplink_veb = pf->veb[veb_idx];
14946 				break;
14947 			}
14948 		}
14949 		if (!uplink_veb) {
14950 			dev_info(&pf->pdev->dev,
14951 				 "uplink seid %d not found\n", uplink_seid);
14952 			return NULL;
14953 		}
14954 	}
14955 
14956 	/* get veb sw struct */
14957 	veb_idx = i40e_veb_mem_alloc(pf);
14958 	if (veb_idx < 0)
14959 		goto err_alloc;
14960 	veb = pf->veb[veb_idx];
14961 	veb->flags = flags;
14962 	veb->uplink_seid = uplink_seid;
14963 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14964 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14965 
14966 	/* create the VEB in the switch */
14967 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14968 	if (ret)
14969 		goto err_veb;
14970 	if (vsi_idx == pf->lan_vsi)
14971 		pf->lan_veb = veb->idx;
14972 
14973 	return veb;
14974 
14975 err_veb:
14976 	i40e_veb_clear(veb);
14977 err_alloc:
14978 	return NULL;
14979 }
14980 
14981 /**
14982  * i40e_setup_pf_switch_element - set PF vars based on switch type
14983  * @pf: board private structure
14984  * @ele: element we are building info from
14985  * @num_reported: total number of elements
14986  * @printconfig: should we print the contents
14987  *
14988  * helper function to assist in extracting a few useful SEID values.
14989  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14990 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14991 				struct i40e_aqc_switch_config_element_resp *ele,
14992 				u16 num_reported, bool printconfig)
14993 {
14994 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14995 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14996 	u8 element_type = ele->element_type;
14997 	u16 seid = le16_to_cpu(ele->seid);
14998 
14999 	if (printconfig)
15000 		dev_info(&pf->pdev->dev,
15001 			 "type=%d seid=%d uplink=%d downlink=%d\n",
15002 			 element_type, seid, uplink_seid, downlink_seid);
15003 
15004 	switch (element_type) {
15005 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
15006 		pf->mac_seid = seid;
15007 		break;
15008 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
15009 		/* Main VEB? */
15010 		if (uplink_seid != pf->mac_seid)
15011 			break;
15012 		if (pf->lan_veb >= I40E_MAX_VEB) {
15013 			int v;
15014 
15015 			/* find existing or else empty VEB */
15016 			for (v = 0; v < I40E_MAX_VEB; v++) {
15017 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
15018 					pf->lan_veb = v;
15019 					break;
15020 				}
15021 			}
15022 			if (pf->lan_veb >= I40E_MAX_VEB) {
15023 				v = i40e_veb_mem_alloc(pf);
15024 				if (v < 0)
15025 					break;
15026 				pf->lan_veb = v;
15027 			}
15028 		}
15029 		if (pf->lan_veb >= I40E_MAX_VEB)
15030 			break;
15031 
15032 		pf->veb[pf->lan_veb]->seid = seid;
15033 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
15034 		pf->veb[pf->lan_veb]->pf = pf;
15035 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
15036 		break;
15037 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
15038 		if (num_reported != 1)
15039 			break;
15040 		/* This is immediately after a reset so we can assume this is
15041 		 * the PF's VSI
15042 		 */
15043 		pf->mac_seid = uplink_seid;
15044 		pf->pf_seid = downlink_seid;
15045 		pf->main_vsi_seid = seid;
15046 		if (printconfig)
15047 			dev_info(&pf->pdev->dev,
15048 				 "pf_seid=%d main_vsi_seid=%d\n",
15049 				 pf->pf_seid, pf->main_vsi_seid);
15050 		break;
15051 	case I40E_SWITCH_ELEMENT_TYPE_PF:
15052 	case I40E_SWITCH_ELEMENT_TYPE_VF:
15053 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
15054 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
15055 	case I40E_SWITCH_ELEMENT_TYPE_PE:
15056 	case I40E_SWITCH_ELEMENT_TYPE_PA:
15057 		/* ignore these for now */
15058 		break;
15059 	default:
15060 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15061 			 element_type, seid);
15062 		break;
15063 	}
15064 }
15065 
15066 /**
15067  * i40e_fetch_switch_configuration - Get switch config from firmware
15068  * @pf: board private structure
15069  * @printconfig: should we print the contents
15070  *
15071  * Get the current switch configuration from the device and
15072  * extract a few useful SEID values.
15073  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)15074 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15075 {
15076 	struct i40e_aqc_get_switch_config_resp *sw_config;
15077 	u16 next_seid = 0;
15078 	int ret = 0;
15079 	u8 *aq_buf;
15080 	int i;
15081 
15082 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15083 	if (!aq_buf)
15084 		return -ENOMEM;
15085 
15086 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15087 	do {
15088 		u16 num_reported, num_total;
15089 
15090 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15091 						I40E_AQ_LARGE_BUF,
15092 						&next_seid, NULL);
15093 		if (ret) {
15094 			dev_info(&pf->pdev->dev,
15095 				 "get switch config failed err %d aq_err %s\n",
15096 				 ret,
15097 				 i40e_aq_str(&pf->hw,
15098 					     pf->hw.aq.asq_last_status));
15099 			kfree(aq_buf);
15100 			return -ENOENT;
15101 		}
15102 
15103 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15104 		num_total = le16_to_cpu(sw_config->header.num_total);
15105 
15106 		if (printconfig)
15107 			dev_info(&pf->pdev->dev,
15108 				 "header: %d reported %d total\n",
15109 				 num_reported, num_total);
15110 
15111 		for (i = 0; i < num_reported; i++) {
15112 			struct i40e_aqc_switch_config_element_resp *ele =
15113 				&sw_config->element[i];
15114 
15115 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15116 						     printconfig);
15117 		}
15118 	} while (next_seid != 0);
15119 
15120 	kfree(aq_buf);
15121 	return ret;
15122 }
15123 
15124 /**
15125  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15126  * @pf: board private structure
15127  * @reinit: if the Main VSI needs to re-initialized.
15128  * @lock_acquired: indicates whether or not the lock has been acquired
15129  *
15130  * Returns 0 on success, negative value on failure
15131  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)15132 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15133 {
15134 	u16 flags = 0;
15135 	int ret;
15136 
15137 	/* find out what's out there already */
15138 	ret = i40e_fetch_switch_configuration(pf, false);
15139 	if (ret) {
15140 		dev_info(&pf->pdev->dev,
15141 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15142 			 ERR_PTR(ret),
15143 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15144 		return ret;
15145 	}
15146 	i40e_pf_reset_stats(pf);
15147 
15148 	/* set the switch config bit for the whole device to
15149 	 * support limited promisc or true promisc
15150 	 * when user requests promisc. The default is limited
15151 	 * promisc.
15152 	*/
15153 
15154 	if ((pf->hw.pf_id == 0) &&
15155 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15156 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15157 		pf->last_sw_conf_flags = flags;
15158 	}
15159 
15160 	if (pf->hw.pf_id == 0) {
15161 		u16 valid_flags;
15162 
15163 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15164 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15165 						NULL);
15166 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15167 			dev_info(&pf->pdev->dev,
15168 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15169 				 ERR_PTR(ret),
15170 				 i40e_aq_str(&pf->hw,
15171 					     pf->hw.aq.asq_last_status));
15172 			/* not a fatal problem, just keep going */
15173 		}
15174 		pf->last_sw_conf_valid_flags = valid_flags;
15175 	}
15176 
15177 	/* first time setup */
15178 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15179 		struct i40e_vsi *vsi = NULL;
15180 		u16 uplink_seid;
15181 
15182 		/* Set up the PF VSI associated with the PF's main VSI
15183 		 * that is already in the HW switch
15184 		 */
15185 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15186 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15187 		else
15188 			uplink_seid = pf->mac_seid;
15189 		if (pf->lan_vsi == I40E_NO_VSI)
15190 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15191 		else if (reinit)
15192 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15193 		if (!vsi) {
15194 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15195 			i40e_cloud_filter_exit(pf);
15196 			i40e_fdir_teardown(pf);
15197 			return -EAGAIN;
15198 		}
15199 	} else {
15200 		/* force a reset of TC and queue layout configurations */
15201 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15202 
15203 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15204 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15205 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15206 	}
15207 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15208 
15209 	i40e_fdir_sb_setup(pf);
15210 
15211 	/* Setup static PF queue filter control settings */
15212 	ret = i40e_setup_pf_filter_control(pf);
15213 	if (ret) {
15214 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15215 			 ret);
15216 		/* Failure here should not stop continuing other steps */
15217 	}
15218 
15219 	/* enable RSS in the HW, even for only one queue, as the stack can use
15220 	 * the hash
15221 	 */
15222 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15223 		i40e_pf_config_rss(pf);
15224 
15225 	/* fill in link information and enable LSE reporting */
15226 	i40e_link_event(pf);
15227 
15228 	/* Initialize user-specific link properties */
15229 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15230 				  I40E_AQ_AN_COMPLETED) ? true : false);
15231 
15232 	i40e_ptp_init(pf);
15233 
15234 	if (!lock_acquired)
15235 		rtnl_lock();
15236 
15237 	/* repopulate tunnel port filters */
15238 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15239 
15240 	if (!lock_acquired)
15241 		rtnl_unlock();
15242 
15243 	return ret;
15244 }
15245 
15246 /**
15247  * i40e_determine_queue_usage - Work out queue distribution
15248  * @pf: board private structure
15249  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15250 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15251 {
15252 	int queues_left;
15253 	int q_max;
15254 
15255 	pf->num_lan_qps = 0;
15256 
15257 	/* Find the max queues to be put into basic use.  We'll always be
15258 	 * using TC0, whether or not DCB is running, and TC0 will get the
15259 	 * big RSS set.
15260 	 */
15261 	queues_left = pf->hw.func_caps.num_tx_qp;
15262 
15263 	if ((queues_left == 1) ||
15264 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15265 		/* one qp for PF, no queues for anything else */
15266 		queues_left = 0;
15267 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15268 
15269 		/* make sure all the fancies are disabled */
15270 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15271 			       I40E_FLAG_IWARP_ENABLED	|
15272 			       I40E_FLAG_FD_SB_ENABLED	|
15273 			       I40E_FLAG_FD_ATR_ENABLED	|
15274 			       I40E_FLAG_DCB_CAPABLE	|
15275 			       I40E_FLAG_DCB_ENABLED	|
15276 			       I40E_FLAG_SRIOV_ENABLED	|
15277 			       I40E_FLAG_VMDQ_ENABLED);
15278 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15279 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15280 				  I40E_FLAG_FD_SB_ENABLED |
15281 				  I40E_FLAG_FD_ATR_ENABLED |
15282 				  I40E_FLAG_DCB_CAPABLE))) {
15283 		/* one qp for PF */
15284 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15285 		queues_left -= pf->num_lan_qps;
15286 
15287 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15288 			       I40E_FLAG_IWARP_ENABLED	|
15289 			       I40E_FLAG_FD_SB_ENABLED	|
15290 			       I40E_FLAG_FD_ATR_ENABLED	|
15291 			       I40E_FLAG_DCB_ENABLED	|
15292 			       I40E_FLAG_VMDQ_ENABLED);
15293 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15294 	} else {
15295 		/* Not enough queues for all TCs */
15296 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15297 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15298 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15299 					I40E_FLAG_DCB_ENABLED);
15300 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15301 		}
15302 
15303 		/* limit lan qps to the smaller of qps, cpus or msix */
15304 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15305 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15306 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15307 		pf->num_lan_qps = q_max;
15308 
15309 		queues_left -= pf->num_lan_qps;
15310 	}
15311 
15312 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15313 		if (queues_left > 1) {
15314 			queues_left -= 1; /* save 1 queue for FD */
15315 		} else {
15316 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15317 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15318 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15319 		}
15320 	}
15321 
15322 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15323 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15324 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15325 					(queues_left / pf->num_vf_qps));
15326 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15327 	}
15328 
15329 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15330 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15331 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15332 					  (queues_left / pf->num_vmdq_qps));
15333 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15334 	}
15335 
15336 	pf->queues_left = queues_left;
15337 	dev_dbg(&pf->pdev->dev,
15338 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15339 		pf->hw.func_caps.num_tx_qp,
15340 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15341 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15342 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15343 		queues_left);
15344 }
15345 
15346 /**
15347  * i40e_setup_pf_filter_control - Setup PF static filter control
15348  * @pf: PF to be setup
15349  *
15350  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15351  * settings. If PE/FCoE are enabled then it will also set the per PF
15352  * based filter sizes required for them. It also enables Flow director,
15353  * ethertype and macvlan type filter settings for the pf.
15354  *
15355  * Returns 0 on success, negative on failure
15356  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15357 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15358 {
15359 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15360 
15361 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15362 
15363 	/* Flow Director is enabled */
15364 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15365 		settings->enable_fdir = true;
15366 
15367 	/* Ethtype and MACVLAN filters enabled for PF */
15368 	settings->enable_ethtype = true;
15369 	settings->enable_macvlan = true;
15370 
15371 	if (i40e_set_filter_control(&pf->hw, settings))
15372 		return -ENOENT;
15373 
15374 	return 0;
15375 }
15376 
15377 #define INFO_STRING_LEN 255
15378 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15379 static void i40e_print_features(struct i40e_pf *pf)
15380 {
15381 	struct i40e_hw *hw = &pf->hw;
15382 	char *buf;
15383 	int i;
15384 
15385 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15386 	if (!buf)
15387 		return;
15388 
15389 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15390 #ifdef CONFIG_PCI_IOV
15391 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15392 #endif
15393 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15394 		      pf->hw.func_caps.num_vsis,
15395 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15396 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15397 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15398 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15399 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15400 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15401 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15402 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15403 	}
15404 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15405 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15406 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15407 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15408 	if (pf->flags & I40E_FLAG_PTP)
15409 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15410 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15411 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15412 	else
15413 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15414 
15415 	dev_info(&pf->pdev->dev, "%s\n", buf);
15416 	kfree(buf);
15417 	WARN_ON(i > INFO_STRING_LEN);
15418 }
15419 
15420 /**
15421  * i40e_get_platform_mac_addr - get platform-specific MAC address
15422  * @pdev: PCI device information struct
15423  * @pf: board private structure
15424  *
15425  * Look up the MAC address for the device. First we'll try
15426  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15427  * specific fallback. Otherwise, we'll default to the stored value in
15428  * firmware.
15429  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15430 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15431 {
15432 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15433 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15434 }
15435 
15436 /**
15437  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15438  * @fec_cfg: FEC option to set in flags
15439  * @flags: ptr to flags in which we set FEC option
15440  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)15441 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15442 {
15443 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15444 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15445 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15446 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15447 		*flags |= I40E_FLAG_RS_FEC;
15448 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15449 	}
15450 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15451 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15452 		*flags |= I40E_FLAG_BASE_R_FEC;
15453 		*flags &= ~I40E_FLAG_RS_FEC;
15454 	}
15455 	if (fec_cfg == 0)
15456 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15457 }
15458 
15459 /**
15460  * i40e_check_recovery_mode - check if we are running transition firmware
15461  * @pf: board private structure
15462  *
15463  * Check registers indicating the firmware runs in recovery mode. Sets the
15464  * appropriate driver state.
15465  *
15466  * Returns true if the recovery mode was detected, false otherwise
15467  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15468 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15469 {
15470 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15471 
15472 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15473 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15474 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15475 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15476 
15477 		return true;
15478 	}
15479 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15480 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15481 
15482 	return false;
15483 }
15484 
15485 /**
15486  * i40e_pf_loop_reset - perform reset in a loop.
15487  * @pf: board private structure
15488  *
15489  * This function is useful when a NIC is about to enter recovery mode.
15490  * When a NIC's internal data structures are corrupted the NIC's
15491  * firmware is going to enter recovery mode.
15492  * Right after a POR it takes about 7 minutes for firmware to enter
15493  * recovery mode. Until that time a NIC is in some kind of intermediate
15494  * state. After that time period the NIC almost surely enters
15495  * recovery mode. The only way for a driver to detect intermediate
15496  * state is to issue a series of pf-resets and check a return value.
15497  * If a PF reset returns success then the firmware could be in recovery
15498  * mode so the caller of this code needs to check for recovery mode
15499  * if this function returns success. There is a little chance that
15500  * firmware will hang in intermediate state forever.
15501  * Since waiting 7 minutes is quite a lot of time this function waits
15502  * 10 seconds and then gives up by returning an error.
15503  *
15504  * Return 0 on success, negative on failure.
15505  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15506 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15507 {
15508 	/* wait max 10 seconds for PF reset to succeed */
15509 	const unsigned long time_end = jiffies + 10 * HZ;
15510 	struct i40e_hw *hw = &pf->hw;
15511 	int ret;
15512 
15513 	ret = i40e_pf_reset(hw);
15514 	while (ret != 0 && time_before(jiffies, time_end)) {
15515 		usleep_range(10000, 20000);
15516 		ret = i40e_pf_reset(hw);
15517 	}
15518 
15519 	if (ret == 0)
15520 		pf->pfr_count++;
15521 	else
15522 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15523 
15524 	return ret;
15525 }
15526 
15527 /**
15528  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15529  * @pf: board private structure
15530  *
15531  * Check FW registers to determine if FW issued unexpected EMP Reset.
15532  * Every time when unexpected EMP Reset occurs the FW increments
15533  * a counter of unexpected EMP Resets. When the counter reaches 10
15534  * the FW should enter the Recovery mode
15535  *
15536  * Returns true if FW issued unexpected EMP Reset
15537  **/
i40e_check_fw_empr(struct i40e_pf * pf)15538 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15539 {
15540 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15541 			   I40E_GL_FWSTS_FWS1B_MASK;
15542 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15543 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15544 }
15545 
15546 /**
15547  * i40e_handle_resets - handle EMP resets and PF resets
15548  * @pf: board private structure
15549  *
15550  * Handle both EMP resets and PF resets and conclude whether there are
15551  * any issues regarding these resets. If there are any issues then
15552  * generate log entry.
15553  *
15554  * Return 0 if NIC is healthy or negative value when there are issues
15555  * with resets
15556  **/
i40e_handle_resets(struct i40e_pf * pf)15557 static int i40e_handle_resets(struct i40e_pf *pf)
15558 {
15559 	const int pfr = i40e_pf_loop_reset(pf);
15560 	const bool is_empr = i40e_check_fw_empr(pf);
15561 
15562 	if (is_empr || pfr != 0)
15563 		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");
15564 
15565 	return is_empr ? -EIO : pfr;
15566 }
15567 
15568 /**
15569  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15570  * @pf: board private structure
15571  * @hw: ptr to the hardware info
15572  *
15573  * This function does a minimal setup of all subsystems needed for running
15574  * recovery mode.
15575  *
15576  * Returns 0 on success, negative on failure
15577  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15578 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15579 {
15580 	struct i40e_vsi *vsi;
15581 	int err;
15582 	int v_idx;
15583 
15584 	pci_set_drvdata(pf->pdev, pf);
15585 	pci_save_state(pf->pdev);
15586 
15587 	/* set up periodic task facility */
15588 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15589 	pf->service_timer_period = HZ;
15590 
15591 	INIT_WORK(&pf->service_task, i40e_service_task);
15592 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15593 
15594 	err = i40e_init_interrupt_scheme(pf);
15595 	if (err)
15596 		goto err_switch_setup;
15597 
15598 	/* The number of VSIs reported by the FW is the minimum guaranteed
15599 	 * to us; HW supports far more and we share the remaining pool with
15600 	 * the other PFs. We allocate space for more than the guarantee with
15601 	 * the understanding that we might not get them all later.
15602 	 */
15603 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15604 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15605 	else
15606 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15607 
15608 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15609 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15610 			  GFP_KERNEL);
15611 	if (!pf->vsi) {
15612 		err = -ENOMEM;
15613 		goto err_switch_setup;
15614 	}
15615 
15616 	/* We allocate one VSI which is needed as absolute minimum
15617 	 * in order to register the netdev
15618 	 */
15619 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15620 	if (v_idx < 0) {
15621 		err = v_idx;
15622 		goto err_switch_setup;
15623 	}
15624 	pf->lan_vsi = v_idx;
15625 	vsi = pf->vsi[v_idx];
15626 	if (!vsi) {
15627 		err = -EFAULT;
15628 		goto err_switch_setup;
15629 	}
15630 	vsi->alloc_queue_pairs = 1;
15631 	err = i40e_config_netdev(vsi);
15632 	if (err)
15633 		goto err_switch_setup;
15634 	err = register_netdev(vsi->netdev);
15635 	if (err)
15636 		goto err_switch_setup;
15637 	vsi->netdev_registered = true;
15638 	i40e_dbg_pf_init(pf);
15639 
15640 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15641 	if (err)
15642 		goto err_switch_setup;
15643 
15644 	/* tell the firmware that we're starting */
15645 	i40e_send_version(pf);
15646 
15647 	/* since everything's happy, start the service_task timer */
15648 	mod_timer(&pf->service_timer,
15649 		  round_jiffies(jiffies + pf->service_timer_period));
15650 
15651 	return 0;
15652 
15653 err_switch_setup:
15654 	i40e_reset_interrupt_capability(pf);
15655 	timer_shutdown_sync(&pf->service_timer);
15656 	i40e_shutdown_adminq(hw);
15657 	iounmap(hw->hw_addr);
15658 	pci_release_mem_regions(pf->pdev);
15659 	pci_disable_device(pf->pdev);
15660 	kfree(pf);
15661 
15662 	return err;
15663 }
15664 
15665 /**
15666  * i40e_set_subsystem_device_id - set subsystem device id
15667  * @hw: pointer to the hardware info
15668  *
15669  * Set PCI subsystem device id either from a pci_dev structure or
15670  * a specific FW register.
15671  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15672 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15673 {
15674 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15675 
15676 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15677 		pf->pdev->subsystem_device :
15678 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15679 }
15680 
15681 /**
15682  * i40e_probe - Device initialization routine
15683  * @pdev: PCI device information struct
15684  * @ent: entry in i40e_pci_tbl
15685  *
15686  * i40e_probe initializes a PF identified by a pci_dev structure.
15687  * The OS initialization, configuring of the PF private structure,
15688  * and a hardware reset occur.
15689  *
15690  * Returns 0 on success, negative on failure
15691  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15692 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15693 {
15694 	struct i40e_aq_get_phy_abilities_resp abilities;
15695 #ifdef CONFIG_I40E_DCB
15696 	enum i40e_get_fw_lldp_status_resp lldp_status;
15697 #endif /* CONFIG_I40E_DCB */
15698 	struct i40e_pf *pf;
15699 	struct i40e_hw *hw;
15700 	static u16 pfs_found;
15701 	u16 wol_nvm_bits;
15702 	u16 link_status;
15703 #ifdef CONFIG_I40E_DCB
15704 	int status;
15705 #endif /* CONFIG_I40E_DCB */
15706 	int err;
15707 	u32 val;
15708 	u32 i;
15709 
15710 	err = pci_enable_device_mem(pdev);
15711 	if (err)
15712 		return err;
15713 
15714 	/* set up for high or low dma */
15715 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15716 	if (err) {
15717 		dev_err(&pdev->dev,
15718 			"DMA configuration failed: 0x%x\n", err);
15719 		goto err_dma;
15720 	}
15721 
15722 	/* set up pci connections */
15723 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15724 	if (err) {
15725 		dev_info(&pdev->dev,
15726 			 "pci_request_selected_regions failed %d\n", err);
15727 		goto err_pci_reg;
15728 	}
15729 
15730 	pci_set_master(pdev);
15731 
15732 	/* Now that we have a PCI connection, we need to do the
15733 	 * low level device setup.  This is primarily setting up
15734 	 * the Admin Queue structures and then querying for the
15735 	 * device's current profile information.
15736 	 */
15737 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15738 	if (!pf) {
15739 		err = -ENOMEM;
15740 		goto err_pf_alloc;
15741 	}
15742 	pf->next_vsi = 0;
15743 	pf->pdev = pdev;
15744 	set_bit(__I40E_DOWN, pf->state);
15745 
15746 	hw = &pf->hw;
15747 
15748 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15749 				I40E_MAX_CSR_SPACE);
15750 	/* We believe that the highest register to read is
15751 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15752 	 * is not less than that before mapping to prevent a
15753 	 * kernel panic.
15754 	 */
15755 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15756 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15757 			pf->ioremap_len);
15758 		err = -ENOMEM;
15759 		goto err_ioremap;
15760 	}
15761 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15762 	if (!hw->hw_addr) {
15763 		err = -EIO;
15764 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15765 			 (unsigned int)pci_resource_start(pdev, 0),
15766 			 pf->ioremap_len, err);
15767 		goto err_ioremap;
15768 	}
15769 	hw->vendor_id = pdev->vendor;
15770 	hw->device_id = pdev->device;
15771 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15772 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15773 	i40e_set_subsystem_device_id(hw);
15774 	hw->bus.device = PCI_SLOT(pdev->devfn);
15775 	hw->bus.func = PCI_FUNC(pdev->devfn);
15776 	hw->bus.bus_id = pdev->bus->number;
15777 	pf->instance = pfs_found;
15778 
15779 	/* Select something other than the 802.1ad ethertype for the
15780 	 * switch to use internally and drop on ingress.
15781 	 */
15782 	hw->switch_tag = 0xffff;
15783 	hw->first_tag = ETH_P_8021AD;
15784 	hw->second_tag = ETH_P_8021Q;
15785 
15786 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15787 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15788 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15789 
15790 	/* set up the locks for the AQ, do this only once in probe
15791 	 * and destroy them only once in remove
15792 	 */
15793 	mutex_init(&hw->aq.asq_mutex);
15794 	mutex_init(&hw->aq.arq_mutex);
15795 
15796 	pf->msg_enable = netif_msg_init(debug,
15797 					NETIF_MSG_DRV |
15798 					NETIF_MSG_PROBE |
15799 					NETIF_MSG_LINK);
15800 	if (debug < -1)
15801 		pf->hw.debug_mask = debug;
15802 
15803 	/* do a special CORER for clearing PXE mode once at init */
15804 	if (hw->revision_id == 0 &&
15805 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15806 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15807 		i40e_flush(hw);
15808 		msleep(200);
15809 		pf->corer_count++;
15810 
15811 		i40e_clear_pxe_mode(hw);
15812 	}
15813 
15814 	/* Reset here to make sure all is clean and to define PF 'n' */
15815 	i40e_clear_hw(hw);
15816 
15817 	err = i40e_set_mac_type(hw);
15818 	if (err) {
15819 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15820 			 err);
15821 		goto err_pf_reset;
15822 	}
15823 
15824 	err = i40e_handle_resets(pf);
15825 	if (err)
15826 		goto err_pf_reset;
15827 
15828 	i40e_check_recovery_mode(pf);
15829 
15830 	if (is_kdump_kernel()) {
15831 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15832 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15833 	} else {
15834 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15835 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15836 	}
15837 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15838 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15839 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15840 
15841 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15842 		 "%s-%s:misc",
15843 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15844 
15845 	err = i40e_init_shared_code(hw);
15846 	if (err) {
15847 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15848 			 err);
15849 		goto err_pf_reset;
15850 	}
15851 
15852 	/* set up a default setting for link flow control */
15853 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15854 
15855 	err = i40e_init_adminq(hw);
15856 	if (err) {
15857 		if (err == -EIO)
15858 			dev_info(&pdev->dev,
15859 				 "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",
15860 				 hw->aq.api_maj_ver,
15861 				 hw->aq.api_min_ver,
15862 				 I40E_FW_API_VERSION_MAJOR,
15863 				 I40E_FW_MINOR_VERSION(hw));
15864 		else
15865 			dev_info(&pdev->dev,
15866 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15867 
15868 		goto err_pf_reset;
15869 	}
15870 	i40e_get_oem_version(hw);
15871 
15872 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15873 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15874 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15875 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15876 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15877 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15878 
15879 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15880 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15881 		dev_dbg(&pdev->dev,
15882 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15883 			 hw->aq.api_maj_ver,
15884 			 hw->aq.api_min_ver,
15885 			 I40E_FW_API_VERSION_MAJOR,
15886 			 I40E_FW_MINOR_VERSION(hw));
15887 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15888 		dev_info(&pdev->dev,
15889 			 "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",
15890 			 hw->aq.api_maj_ver,
15891 			 hw->aq.api_min_ver,
15892 			 I40E_FW_API_VERSION_MAJOR,
15893 			 I40E_FW_MINOR_VERSION(hw));
15894 
15895 	i40e_verify_eeprom(pf);
15896 
15897 	/* Rev 0 hardware was never productized */
15898 	if (hw->revision_id < 1)
15899 		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");
15900 
15901 	i40e_clear_pxe_mode(hw);
15902 
15903 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15904 	if (err)
15905 		goto err_adminq_setup;
15906 
15907 	err = i40e_sw_init(pf);
15908 	if (err) {
15909 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15910 		goto err_sw_init;
15911 	}
15912 
15913 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15914 		return i40e_init_recovery_mode(pf, hw);
15915 
15916 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15917 				hw->func_caps.num_rx_qp, 0, 0);
15918 	if (err) {
15919 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15920 		goto err_init_lan_hmc;
15921 	}
15922 
15923 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15924 	if (err) {
15925 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15926 		err = -ENOENT;
15927 		goto err_configure_lan_hmc;
15928 	}
15929 
15930 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15931 	 * Ignore error return codes because if it was already disabled via
15932 	 * hardware settings this will fail
15933 	 */
15934 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15935 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15936 		i40e_aq_stop_lldp(hw, true, false, NULL);
15937 	}
15938 
15939 	/* allow a platform config to override the HW addr */
15940 	i40e_get_platform_mac_addr(pdev, pf);
15941 
15942 	if (!is_valid_ether_addr(hw->mac.addr)) {
15943 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15944 		err = -EIO;
15945 		goto err_mac_addr;
15946 	}
15947 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15948 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15949 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15950 	if (is_valid_ether_addr(hw->mac.port_addr))
15951 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15952 
15953 	i40e_ptp_alloc_pins(pf);
15954 	pci_set_drvdata(pdev, pf);
15955 	pci_save_state(pdev);
15956 
15957 #ifdef CONFIG_I40E_DCB
15958 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15959 	(!status &&
15960 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15961 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15962 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15963 	dev_info(&pdev->dev,
15964 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15965 			"FW LLDP is disabled\n" :
15966 			"FW LLDP is enabled\n");
15967 
15968 	/* Enable FW to write default DCB config on link-up */
15969 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15970 
15971 	err = i40e_init_pf_dcb(pf);
15972 	if (err) {
15973 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15974 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15975 		/* Continue without DCB enabled */
15976 	}
15977 #endif /* CONFIG_I40E_DCB */
15978 
15979 	/* set up periodic task facility */
15980 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15981 	pf->service_timer_period = HZ;
15982 
15983 	INIT_WORK(&pf->service_task, i40e_service_task);
15984 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15985 
15986 	/* NVM bit on means WoL disabled for the port */
15987 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15988 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15989 		pf->wol_en = false;
15990 	else
15991 		pf->wol_en = true;
15992 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15993 
15994 	/* set up the main switch operations */
15995 	i40e_determine_queue_usage(pf);
15996 	err = i40e_init_interrupt_scheme(pf);
15997 	if (err)
15998 		goto err_switch_setup;
15999 
16000 	/* Reduce Tx and Rx pairs for kdump
16001 	 * When MSI-X is enabled, it's not allowed to use more TC queue
16002 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
16003 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
16004 	 */
16005 	if (is_kdump_kernel())
16006 		pf->num_lan_msix = 1;
16007 
16008 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
16009 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
16010 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
16011 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
16012 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
16013 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
16014 						    UDP_TUNNEL_TYPE_GENEVE;
16015 
16016 	/* The number of VSIs reported by the FW is the minimum guaranteed
16017 	 * to us; HW supports far more and we share the remaining pool with
16018 	 * the other PFs. We allocate space for more than the guarantee with
16019 	 * the understanding that we might not get them all later.
16020 	 */
16021 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
16022 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
16023 	else
16024 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
16025 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
16026 		dev_warn(&pf->pdev->dev,
16027 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
16028 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
16029 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
16030 	}
16031 
16032 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
16033 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
16034 			  GFP_KERNEL);
16035 	if (!pf->vsi) {
16036 		err = -ENOMEM;
16037 		goto err_switch_setup;
16038 	}
16039 
16040 #ifdef CONFIG_PCI_IOV
16041 	/* prep for VF support */
16042 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16043 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16044 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16045 		if (pci_num_vf(pdev))
16046 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16047 	}
16048 #endif
16049 	err = i40e_setup_pf_switch(pf, false, false);
16050 	if (err) {
16051 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16052 		goto err_vsis;
16053 	}
16054 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16055 
16056 	/* if FDIR VSI was set up, start it now */
16057 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16058 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16059 			i40e_vsi_open(pf->vsi[i]);
16060 			break;
16061 		}
16062 	}
16063 
16064 	/* The driver only wants link up/down and module qualification
16065 	 * reports from firmware.  Note the negative logic.
16066 	 */
16067 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16068 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16069 					 I40E_AQ_EVENT_MEDIA_NA |
16070 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16071 	if (err)
16072 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16073 			 ERR_PTR(err),
16074 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16075 
16076 	/* Reconfigure hardware for allowing smaller MSS in the case
16077 	 * of TSO, so that we avoid the MDD being fired and causing
16078 	 * a reset in the case of small MSS+TSO.
16079 	 */
16080 	val = rd32(hw, I40E_REG_MSS);
16081 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16082 		val &= ~I40E_REG_MSS_MIN_MASK;
16083 		val |= I40E_64BYTE_MSS;
16084 		wr32(hw, I40E_REG_MSS, val);
16085 	}
16086 
16087 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16088 		msleep(75);
16089 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16090 		if (err)
16091 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16092 				 ERR_PTR(err),
16093 				 i40e_aq_str(&pf->hw,
16094 					     pf->hw.aq.asq_last_status));
16095 	}
16096 	/* The main driver is (mostly) up and happy. We need to set this state
16097 	 * before setting up the misc vector or we get a race and the vector
16098 	 * ends up disabled forever.
16099 	 */
16100 	clear_bit(__I40E_DOWN, pf->state);
16101 
16102 	/* In case of MSIX we are going to setup the misc vector right here
16103 	 * to handle admin queue events etc. In case of legacy and MSI
16104 	 * the misc functionality and queue processing is combined in
16105 	 * the same vector and that gets setup at open.
16106 	 */
16107 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16108 		err = i40e_setup_misc_vector(pf);
16109 		if (err) {
16110 			dev_info(&pdev->dev,
16111 				 "setup of misc vector failed: %d\n", err);
16112 			i40e_cloud_filter_exit(pf);
16113 			i40e_fdir_teardown(pf);
16114 			goto err_vsis;
16115 		}
16116 	}
16117 
16118 #ifdef CONFIG_PCI_IOV
16119 	/* prep for VF support */
16120 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16121 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16122 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16123 		/* disable link interrupts for VFs */
16124 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16125 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16126 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16127 		i40e_flush(hw);
16128 
16129 		if (pci_num_vf(pdev)) {
16130 			dev_info(&pdev->dev,
16131 				 "Active VFs found, allocating resources.\n");
16132 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16133 			if (err)
16134 				dev_info(&pdev->dev,
16135 					 "Error %d allocating resources for existing VFs\n",
16136 					 err);
16137 		}
16138 	}
16139 #endif /* CONFIG_PCI_IOV */
16140 
16141 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16142 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16143 						      pf->num_iwarp_msix,
16144 						      I40E_IWARP_IRQ_PILE_ID);
16145 		if (pf->iwarp_base_vector < 0) {
16146 			dev_info(&pdev->dev,
16147 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16148 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16149 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16150 		}
16151 	}
16152 
16153 	i40e_dbg_pf_init(pf);
16154 
16155 	/* tell the firmware that we're starting */
16156 	i40e_send_version(pf);
16157 
16158 	/* since everything's happy, start the service_task timer */
16159 	mod_timer(&pf->service_timer,
16160 		  round_jiffies(jiffies + pf->service_timer_period));
16161 
16162 	/* add this PF to client device list and launch a client service task */
16163 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16164 		err = i40e_lan_add_device(pf);
16165 		if (err)
16166 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16167 				 err);
16168 	}
16169 
16170 #define PCI_SPEED_SIZE 8
16171 #define PCI_WIDTH_SIZE 8
16172 	/* Devices on the IOSF bus do not have this information
16173 	 * and will report PCI Gen 1 x 1 by default so don't bother
16174 	 * checking them.
16175 	 */
16176 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16177 		char speed[PCI_SPEED_SIZE] = "Unknown";
16178 		char width[PCI_WIDTH_SIZE] = "Unknown";
16179 
16180 		/* Get the negotiated link width and speed from PCI config
16181 		 * space
16182 		 */
16183 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16184 					  &link_status);
16185 
16186 		i40e_set_pci_config_data(hw, link_status);
16187 
16188 		switch (hw->bus.speed) {
16189 		case i40e_bus_speed_8000:
16190 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16191 		case i40e_bus_speed_5000:
16192 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16193 		case i40e_bus_speed_2500:
16194 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16195 		default:
16196 			break;
16197 		}
16198 		switch (hw->bus.width) {
16199 		case i40e_bus_width_pcie_x8:
16200 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16201 		case i40e_bus_width_pcie_x4:
16202 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16203 		case i40e_bus_width_pcie_x2:
16204 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16205 		case i40e_bus_width_pcie_x1:
16206 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16207 		default:
16208 			break;
16209 		}
16210 
16211 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16212 			 speed, width);
16213 
16214 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16215 		    hw->bus.speed < i40e_bus_speed_8000) {
16216 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16217 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16218 		}
16219 	}
16220 
16221 	/* get the requested speeds from the fw */
16222 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16223 	if (err)
16224 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16225 			ERR_PTR(err),
16226 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16227 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16228 
16229 	/* set the FEC config due to the board capabilities */
16230 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16231 
16232 	/* get the supported phy types from the fw */
16233 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16234 	if (err)
16235 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16236 			ERR_PTR(err),
16237 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16238 
16239 	/* make sure the MFS hasn't been set lower than the default */
16240 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16241 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16242 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16243 	if (val < MAX_FRAME_SIZE_DEFAULT)
16244 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16245 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16246 
16247 	/* Add a filter to drop all Flow control frames from any VSI from being
16248 	 * transmitted. By doing so we stop a malicious VF from sending out
16249 	 * PAUSE or PFC frames and potentially controlling traffic for other
16250 	 * PF/VF VSIs.
16251 	 * The FW can still send Flow control frames if enabled.
16252 	 */
16253 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16254 						       pf->main_vsi_seid);
16255 
16256 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16257 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16258 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16259 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16260 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16261 	/* print a string summarizing features */
16262 	i40e_print_features(pf);
16263 
16264 	return 0;
16265 
16266 	/* Unwind what we've done if something failed in the setup */
16267 err_vsis:
16268 	set_bit(__I40E_DOWN, pf->state);
16269 	i40e_clear_interrupt_scheme(pf);
16270 	kfree(pf->vsi);
16271 err_switch_setup:
16272 	i40e_reset_interrupt_capability(pf);
16273 	timer_shutdown_sync(&pf->service_timer);
16274 err_mac_addr:
16275 err_configure_lan_hmc:
16276 	(void)i40e_shutdown_lan_hmc(hw);
16277 err_init_lan_hmc:
16278 	kfree(pf->qp_pile);
16279 err_sw_init:
16280 err_adminq_setup:
16281 err_pf_reset:
16282 	iounmap(hw->hw_addr);
16283 err_ioremap:
16284 	kfree(pf);
16285 err_pf_alloc:
16286 	pci_release_mem_regions(pdev);
16287 err_pci_reg:
16288 err_dma:
16289 	pci_disable_device(pdev);
16290 	return err;
16291 }
16292 
16293 /**
16294  * i40e_remove - Device removal routine
16295  * @pdev: PCI device information struct
16296  *
16297  * i40e_remove is called by the PCI subsystem to alert the driver
16298  * that is should release a PCI device.  This could be caused by a
16299  * Hot-Plug event, or because the driver is going to be removed from
16300  * memory.
16301  **/
i40e_remove(struct pci_dev * pdev)16302 static void i40e_remove(struct pci_dev *pdev)
16303 {
16304 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16305 	struct i40e_hw *hw = &pf->hw;
16306 	int ret_code;
16307 	int i;
16308 
16309 	i40e_dbg_pf_exit(pf);
16310 
16311 	i40e_ptp_stop(pf);
16312 
16313 	/* Disable RSS in hw */
16314 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16315 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16316 
16317 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16318 	 * flags, once they are set, i40e_rebuild should not be called as
16319 	 * i40e_prep_for_reset always returns early.
16320 	 */
16321 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16322 		usleep_range(1000, 2000);
16323 	set_bit(__I40E_IN_REMOVE, pf->state);
16324 
16325 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16326 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16327 		i40e_free_vfs(pf);
16328 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16329 	}
16330 	/* no more scheduling of any task */
16331 	set_bit(__I40E_SUSPENDED, pf->state);
16332 	set_bit(__I40E_DOWN, pf->state);
16333 	if (pf->service_timer.function)
16334 		timer_shutdown_sync(&pf->service_timer);
16335 	if (pf->service_task.func)
16336 		cancel_work_sync(&pf->service_task);
16337 
16338 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16339 		struct i40e_vsi *vsi = pf->vsi[0];
16340 
16341 		/* We know that we have allocated only one vsi for this PF,
16342 		 * it was just for registering netdevice, so the interface
16343 		 * could be visible in the 'ifconfig' output
16344 		 */
16345 		unregister_netdev(vsi->netdev);
16346 		free_netdev(vsi->netdev);
16347 
16348 		goto unmap;
16349 	}
16350 
16351 	/* Client close must be called explicitly here because the timer
16352 	 * has been stopped.
16353 	 */
16354 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16355 
16356 	i40e_fdir_teardown(pf);
16357 
16358 	/* If there is a switch structure or any orphans, remove them.
16359 	 * This will leave only the PF's VSI remaining.
16360 	 */
16361 	for (i = 0; i < I40E_MAX_VEB; i++) {
16362 		if (!pf->veb[i])
16363 			continue;
16364 
16365 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16366 		    pf->veb[i]->uplink_seid == 0)
16367 			i40e_switch_branch_release(pf->veb[i]);
16368 	}
16369 
16370 	/* Now we can shutdown the PF's VSIs, just before we kill
16371 	 * adminq and hmc.
16372 	 */
16373 	for (i = pf->num_alloc_vsi; i--;)
16374 		if (pf->vsi[i]) {
16375 			i40e_vsi_close(pf->vsi[i]);
16376 			i40e_vsi_release(pf->vsi[i]);
16377 			pf->vsi[i] = NULL;
16378 		}
16379 
16380 	i40e_cloud_filter_exit(pf);
16381 
16382 	/* remove attached clients */
16383 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16384 		ret_code = i40e_lan_del_device(pf);
16385 		if (ret_code)
16386 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16387 				 ret_code);
16388 	}
16389 
16390 	/* shutdown and destroy the HMC */
16391 	if (hw->hmc.hmc_obj) {
16392 		ret_code = i40e_shutdown_lan_hmc(hw);
16393 		if (ret_code)
16394 			dev_warn(&pdev->dev,
16395 				 "Failed to destroy the HMC resources: %d\n",
16396 				 ret_code);
16397 	}
16398 
16399 unmap:
16400 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16401 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16402 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16403 		free_irq(pf->pdev->irq, pf);
16404 
16405 	/* shutdown the adminq */
16406 	i40e_shutdown_adminq(hw);
16407 
16408 	/* destroy the locks only once, here */
16409 	mutex_destroy(&hw->aq.arq_mutex);
16410 	mutex_destroy(&hw->aq.asq_mutex);
16411 
16412 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16413 	rtnl_lock();
16414 	i40e_clear_interrupt_scheme(pf);
16415 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16416 		if (pf->vsi[i]) {
16417 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16418 				i40e_vsi_clear_rings(pf->vsi[i]);
16419 			i40e_vsi_clear(pf->vsi[i]);
16420 			pf->vsi[i] = NULL;
16421 		}
16422 	}
16423 	rtnl_unlock();
16424 
16425 	for (i = 0; i < I40E_MAX_VEB; i++) {
16426 		kfree(pf->veb[i]);
16427 		pf->veb[i] = NULL;
16428 	}
16429 
16430 	kfree(pf->qp_pile);
16431 	kfree(pf->vsi);
16432 
16433 	iounmap(hw->hw_addr);
16434 	kfree(pf);
16435 	pci_release_mem_regions(pdev);
16436 
16437 	pci_disable_device(pdev);
16438 }
16439 
16440 /**
16441  * i40e_pci_error_detected - warning that something funky happened in PCI land
16442  * @pdev: PCI device information struct
16443  * @error: the type of PCI error
16444  *
16445  * Called to warn that something happened and the error handling steps
16446  * are in progress.  Allows the driver to quiesce things, be ready for
16447  * remediation.
16448  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16449 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16450 						pci_channel_state_t error)
16451 {
16452 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16453 
16454 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16455 
16456 	if (!pf) {
16457 		dev_info(&pdev->dev,
16458 			 "Cannot recover - error happened during device probe\n");
16459 		return PCI_ERS_RESULT_DISCONNECT;
16460 	}
16461 
16462 	/* shutdown all operations */
16463 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16464 		i40e_prep_for_reset(pf);
16465 
16466 	/* Request a slot reset */
16467 	return PCI_ERS_RESULT_NEED_RESET;
16468 }
16469 
16470 /**
16471  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16472  * @pdev: PCI device information struct
16473  *
16474  * Called to find if the driver can work with the device now that
16475  * the pci slot has been reset.  If a basic connection seems good
16476  * (registers are readable and have sane content) then return a
16477  * happy little PCI_ERS_RESULT_xxx.
16478  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16479 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16480 {
16481 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16482 	pci_ers_result_t result;
16483 	u32 reg;
16484 
16485 	dev_dbg(&pdev->dev, "%s\n", __func__);
16486 	if (pci_enable_device_mem(pdev)) {
16487 		dev_info(&pdev->dev,
16488 			 "Cannot re-enable PCI device after reset.\n");
16489 		result = PCI_ERS_RESULT_DISCONNECT;
16490 	} else {
16491 		pci_set_master(pdev);
16492 		pci_restore_state(pdev);
16493 		pci_save_state(pdev);
16494 		pci_wake_from_d3(pdev, false);
16495 
16496 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16497 		if (reg == 0)
16498 			result = PCI_ERS_RESULT_RECOVERED;
16499 		else
16500 			result = PCI_ERS_RESULT_DISCONNECT;
16501 	}
16502 
16503 	return result;
16504 }
16505 
16506 /**
16507  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16508  * @pdev: PCI device information struct
16509  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16510 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16511 {
16512 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16513 
16514 	i40e_prep_for_reset(pf);
16515 }
16516 
16517 /**
16518  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16519  * @pdev: PCI device information struct
16520  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16521 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16522 {
16523 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16524 
16525 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16526 		return;
16527 
16528 	i40e_reset_and_rebuild(pf, false, false);
16529 #ifdef CONFIG_PCI_IOV
16530 	i40e_restore_all_vfs_msi_state(pdev);
16531 #endif /* CONFIG_PCI_IOV */
16532 }
16533 
16534 /**
16535  * i40e_pci_error_resume - restart operations after PCI error recovery
16536  * @pdev: PCI device information struct
16537  *
16538  * Called to allow the driver to bring things back up after PCI error
16539  * and/or reset recovery has finished.
16540  **/
i40e_pci_error_resume(struct pci_dev * pdev)16541 static void i40e_pci_error_resume(struct pci_dev *pdev)
16542 {
16543 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16544 
16545 	dev_dbg(&pdev->dev, "%s\n", __func__);
16546 	if (test_bit(__I40E_SUSPENDED, pf->state))
16547 		return;
16548 
16549 	i40e_handle_reset_warning(pf, false);
16550 }
16551 
16552 /**
16553  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16554  * using the mac_address_write admin q function
16555  * @pf: pointer to i40e_pf struct
16556  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16557 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16558 {
16559 	struct i40e_hw *hw = &pf->hw;
16560 	u8 mac_addr[6];
16561 	u16 flags = 0;
16562 	int ret;
16563 
16564 	/* Get current MAC address in case it's an LAA */
16565 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16566 		ether_addr_copy(mac_addr,
16567 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16568 	} else {
16569 		dev_err(&pf->pdev->dev,
16570 			"Failed to retrieve MAC address; using default\n");
16571 		ether_addr_copy(mac_addr, hw->mac.addr);
16572 	}
16573 
16574 	/* The FW expects the mac address write cmd to first be called with
16575 	 * one of these flags before calling it again with the multicast
16576 	 * enable flags.
16577 	 */
16578 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16579 
16580 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16581 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16582 
16583 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16584 	if (ret) {
16585 		dev_err(&pf->pdev->dev,
16586 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16587 		return;
16588 	}
16589 
16590 	flags = I40E_AQC_MC_MAG_EN
16591 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16592 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16593 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16594 	if (ret)
16595 		dev_err(&pf->pdev->dev,
16596 			"Failed to enable Multicast Magic Packet wake up\n");
16597 }
16598 
16599 /**
16600  * i40e_shutdown - PCI callback for shutting down
16601  * @pdev: PCI device information struct
16602  **/
i40e_shutdown(struct pci_dev * pdev)16603 static void i40e_shutdown(struct pci_dev *pdev)
16604 {
16605 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16606 	struct i40e_hw *hw = &pf->hw;
16607 
16608 	set_bit(__I40E_SUSPENDED, pf->state);
16609 	set_bit(__I40E_DOWN, pf->state);
16610 
16611 	del_timer_sync(&pf->service_timer);
16612 	cancel_work_sync(&pf->service_task);
16613 	i40e_cloud_filter_exit(pf);
16614 	i40e_fdir_teardown(pf);
16615 
16616 	/* Client close must be called explicitly here because the timer
16617 	 * has been stopped.
16618 	 */
16619 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16620 
16621 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16622 		i40e_enable_mc_magic_wake(pf);
16623 
16624 	i40e_prep_for_reset(pf);
16625 
16626 	wr32(hw, I40E_PFPM_APM,
16627 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16628 	wr32(hw, I40E_PFPM_WUFC,
16629 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16630 
16631 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16632 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16633 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16634 		free_irq(pf->pdev->irq, pf);
16635 
16636 	/* Since we're going to destroy queues during the
16637 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16638 	 * whole section
16639 	 */
16640 	rtnl_lock();
16641 	i40e_clear_interrupt_scheme(pf);
16642 	rtnl_unlock();
16643 
16644 	if (system_state == SYSTEM_POWER_OFF) {
16645 		pci_wake_from_d3(pdev, pf->wol_en);
16646 		pci_set_power_state(pdev, PCI_D3hot);
16647 	}
16648 }
16649 
16650 /**
16651  * i40e_suspend - PM callback for moving to D3
16652  * @dev: generic device information structure
16653  **/
i40e_suspend(struct device * dev)16654 static int __maybe_unused i40e_suspend(struct device *dev)
16655 {
16656 	struct i40e_pf *pf = dev_get_drvdata(dev);
16657 	struct i40e_hw *hw = &pf->hw;
16658 
16659 	/* If we're already suspended, then there is nothing to do */
16660 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16661 		return 0;
16662 
16663 	set_bit(__I40E_DOWN, pf->state);
16664 
16665 	/* Ensure service task will not be running */
16666 	del_timer_sync(&pf->service_timer);
16667 	cancel_work_sync(&pf->service_task);
16668 
16669 	/* Client close must be called explicitly here because the timer
16670 	 * has been stopped.
16671 	 */
16672 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16673 
16674 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16675 		i40e_enable_mc_magic_wake(pf);
16676 
16677 	/* Since we're going to destroy queues during the
16678 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16679 	 * whole section
16680 	 */
16681 	rtnl_lock();
16682 
16683 	i40e_prep_for_reset(pf);
16684 
16685 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16686 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16687 
16688 	/* Clear the interrupt scheme and release our IRQs so that the system
16689 	 * can safely hibernate even when there are a large number of CPUs.
16690 	 * Otherwise hibernation might fail when mapping all the vectors back
16691 	 * to CPU0.
16692 	 */
16693 	i40e_clear_interrupt_scheme(pf);
16694 
16695 	rtnl_unlock();
16696 
16697 	return 0;
16698 }
16699 
16700 /**
16701  * i40e_resume - PM callback for waking up from D3
16702  * @dev: generic device information structure
16703  **/
i40e_resume(struct device * dev)16704 static int __maybe_unused i40e_resume(struct device *dev)
16705 {
16706 	struct i40e_pf *pf = dev_get_drvdata(dev);
16707 	int err;
16708 
16709 	/* If we're not suspended, then there is nothing to do */
16710 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16711 		return 0;
16712 
16713 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16714 	 * since we're going to be restoring queues
16715 	 */
16716 	rtnl_lock();
16717 
16718 	/* We cleared the interrupt scheme when we suspended, so we need to
16719 	 * restore it now to resume device functionality.
16720 	 */
16721 	err = i40e_restore_interrupt_scheme(pf);
16722 	if (err) {
16723 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16724 			err);
16725 	}
16726 
16727 	clear_bit(__I40E_DOWN, pf->state);
16728 	i40e_reset_and_rebuild(pf, false, true);
16729 
16730 	rtnl_unlock();
16731 
16732 	/* Clear suspended state last after everything is recovered */
16733 	clear_bit(__I40E_SUSPENDED, pf->state);
16734 
16735 	/* Restart the service task */
16736 	mod_timer(&pf->service_timer,
16737 		  round_jiffies(jiffies + pf->service_timer_period));
16738 
16739 	return 0;
16740 }
16741 
16742 static const struct pci_error_handlers i40e_err_handler = {
16743 	.error_detected = i40e_pci_error_detected,
16744 	.slot_reset = i40e_pci_error_slot_reset,
16745 	.reset_prepare = i40e_pci_error_reset_prepare,
16746 	.reset_done = i40e_pci_error_reset_done,
16747 	.resume = i40e_pci_error_resume,
16748 };
16749 
16750 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16751 
16752 static struct pci_driver i40e_driver = {
16753 	.name     = i40e_driver_name,
16754 	.id_table = i40e_pci_tbl,
16755 	.probe    = i40e_probe,
16756 	.remove   = i40e_remove,
16757 	.driver   = {
16758 		.pm = &i40e_pm_ops,
16759 	},
16760 	.shutdown = i40e_shutdown,
16761 	.err_handler = &i40e_err_handler,
16762 	.sriov_configure = i40e_pci_sriov_configure,
16763 };
16764 
16765 /**
16766  * i40e_init_module - Driver registration routine
16767  *
16768  * i40e_init_module is the first routine called when the driver is
16769  * loaded. All it does is register with the PCI subsystem.
16770  **/
i40e_init_module(void)16771 static int __init i40e_init_module(void)
16772 {
16773 	int err;
16774 
16775 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16776 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16777 
16778 	/* There is no need to throttle the number of active tasks because
16779 	 * each device limits its own task using a state bit for scheduling
16780 	 * the service task, and the device tasks do not interfere with each
16781 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16782 	 * since we need to be able to guarantee forward progress even under
16783 	 * memory pressure.
16784 	 */
16785 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16786 	if (!i40e_wq) {
16787 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16788 		return -ENOMEM;
16789 	}
16790 
16791 	i40e_dbg_init();
16792 	err = pci_register_driver(&i40e_driver);
16793 	if (err) {
16794 		destroy_workqueue(i40e_wq);
16795 		i40e_dbg_exit();
16796 		return err;
16797 	}
16798 
16799 	return 0;
16800 }
16801 module_init(i40e_init_module);
16802 
16803 /**
16804  * i40e_exit_module - Driver exit cleanup routine
16805  *
16806  * i40e_exit_module is called just before the driver is removed
16807  * from memory.
16808  **/
i40e_exit_module(void)16809 static void __exit i40e_exit_module(void)
16810 {
16811 	pci_unregister_driver(&i40e_driver);
16812 	destroy_workqueue(i40e_wq);
16813 	ida_destroy(&i40e_client_ida);
16814 	i40e_dbg_exit();
16815 }
16816 module_exit(i40e_exit_module);
16817