xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 
10 /* Local includes */
11 #include "i40e.h"
12 #include "i40e_diag.h"
13 #include "i40e_xsk.h"
14 #include <net/udp_tunnel.h>
15 #include <net/xdp_sock_drv.h>
16 /* All i40e tracepoints are defined by the include below, which
17  * must be included exactly once across the whole kernel with
18  * CREATE_TRACE_POINTS defined
19  */
20 #define CREATE_TRACE_POINTS
21 #include "i40e_trace.h"
22 
23 const char i40e_driver_name[] = "i40e";
24 static const char i40e_driver_string[] =
25 			"Intel(R) Ethernet Connection XL710 Network Driver";
26 
27 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
28 
29 /* a bit of forward declarations */
30 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
31 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
32 static int i40e_add_vsi(struct i40e_vsi *vsi);
33 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
34 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
35 static int i40e_setup_misc_vector(struct i40e_pf *pf);
36 static void i40e_determine_queue_usage(struct i40e_pf *pf);
37 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
38 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
39 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
40 				   bool lock_acquired);
41 static int i40e_reset(struct i40e_pf *pf);
42 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
43 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
44 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
45 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
46 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
47 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
48 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
49 static int i40e_get_capabilities(struct i40e_pf *pf,
50 				 enum i40e_admin_queue_opc list_type);
51 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
52 
53 /* i40e_pci_tbl - PCI Device ID Table
54  *
55  * Last entry must be all 0s
56  *
57  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
58  *   Class, Class Mask, private data (not used) }
59  */
60 static const struct pci_device_id i40e_pci_tbl[] = {
61 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
85 	/* required last entry */
86 	{0, }
87 };
88 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
89 
90 #define I40E_MAX_VF_COUNT 128
91 static int debug = -1;
92 module_param(debug, uint, 0);
93 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
94 
95 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
96 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
97 MODULE_LICENSE("GPL v2");
98 
99 static struct workqueue_struct *i40e_wq;
100 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)101 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
102 				  struct net_device *netdev, int delta)
103 {
104 	struct netdev_hw_addr *ha;
105 
106 	if (!f || !netdev)
107 		return;
108 
109 	netdev_for_each_mc_addr(ha, netdev) {
110 		if (ether_addr_equal(ha->addr, f->macaddr)) {
111 			ha->refcount += delta;
112 			if (ha->refcount <= 0)
113 				ha->refcount = 1;
114 			break;
115 		}
116 	}
117 }
118 
119 /**
120  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
121  * @hw:   pointer to the HW structure
122  * @mem:  ptr to mem struct to fill out
123  * @size: size of memory requested
124  * @alignment: what to align the allocation to
125  **/
i40e_allocate_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)126 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
127 			    u64 size, u32 alignment)
128 {
129 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
130 
131 	mem->size = ALIGN(size, alignment);
132 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
133 				     GFP_KERNEL);
134 	if (!mem->va)
135 		return -ENOMEM;
136 
137 	return 0;
138 }
139 
140 /**
141  * i40e_free_dma_mem_d - OS specific memory free for shared code
142  * @hw:   pointer to the HW structure
143  * @mem:  ptr to mem struct to free
144  **/
i40e_free_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem)145 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
146 {
147 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
148 
149 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
150 	mem->va = NULL;
151 	mem->pa = 0;
152 	mem->size = 0;
153 
154 	return 0;
155 }
156 
157 /**
158  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
159  * @hw:   pointer to the HW structure
160  * @mem:  ptr to mem struct to fill out
161  * @size: size of memory requested
162  **/
i40e_allocate_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)163 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
164 			     u32 size)
165 {
166 	mem->size = size;
167 	mem->va = kzalloc(size, GFP_KERNEL);
168 
169 	if (!mem->va)
170 		return -ENOMEM;
171 
172 	return 0;
173 }
174 
175 /**
176  * i40e_free_virt_mem_d - OS specific memory free for shared code
177  * @hw:   pointer to the HW structure
178  * @mem:  ptr to mem struct to free
179  **/
i40e_free_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem)180 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
181 {
182 	/* it's ok to kfree a NULL pointer */
183 	kfree(mem->va);
184 	mem->va = NULL;
185 	mem->size = 0;
186 
187 	return 0;
188 }
189 
190 /**
191  * i40e_get_lump - find a lump of free generic resource
192  * @pf: board private structure
193  * @pile: the pile of resource to search
194  * @needed: the number of items needed
195  * @id: an owner id to stick on the items assigned
196  *
197  * Returns the base item index of the lump, or negative for error
198  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)199 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
200 			 u16 needed, u16 id)
201 {
202 	int ret = -ENOMEM;
203 	int i, j;
204 
205 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
206 		dev_info(&pf->pdev->dev,
207 			 "param err: pile=%s needed=%d id=0x%04x\n",
208 			 pile ? "<valid>" : "<null>", needed, id);
209 		return -EINVAL;
210 	}
211 
212 	/* Allocate last queue in the pile for FDIR VSI queue
213 	 * so it doesn't fragment the qp_pile
214 	 */
215 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
216 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
217 			dev_err(&pf->pdev->dev,
218 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
219 				pile->num_entries - 1);
220 			return -ENOMEM;
221 		}
222 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
223 		return pile->num_entries - 1;
224 	}
225 
226 	i = 0;
227 	while (i < pile->num_entries) {
228 		/* skip already allocated entries */
229 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
230 			i++;
231 			continue;
232 		}
233 
234 		/* do we have enough in this lump? */
235 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
236 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
237 				break;
238 		}
239 
240 		if (j == needed) {
241 			/* there was enough, so assign it to the requestor */
242 			for (j = 0; j < needed; j++)
243 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
244 			ret = i;
245 			break;
246 		}
247 
248 		/* not enough, so skip over it and continue looking */
249 		i += j;
250 	}
251 
252 	return ret;
253 }
254 
255 /**
256  * i40e_put_lump - return a lump of generic resource
257  * @pile: the pile of resource to search
258  * @index: the base item index
259  * @id: the owner id of the items assigned
260  *
261  * Returns the count of items in the lump
262  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)263 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
264 {
265 	int valid_id = (id | I40E_PILE_VALID_BIT);
266 	int count = 0;
267 	u16 i;
268 
269 	if (!pile || index >= pile->num_entries)
270 		return -EINVAL;
271 
272 	for (i = index;
273 	     i < pile->num_entries && pile->list[i] == valid_id;
274 	     i++) {
275 		pile->list[i] = 0;
276 		count++;
277 	}
278 
279 
280 	return count;
281 }
282 
283 /**
284  * i40e_find_vsi_from_id - searches for the vsi with the given id
285  * @pf: the pf structure to search for the vsi
286  * @id: id of the vsi it is searching for
287  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)288 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
289 {
290 	int i;
291 
292 	for (i = 0; i < pf->num_alloc_vsi; i++)
293 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
294 			return pf->vsi[i];
295 
296 	return NULL;
297 }
298 
299 /**
300  * i40e_service_event_schedule - Schedule the service task to wake up
301  * @pf: board private structure
302  *
303  * If not already scheduled, this puts the task into the work queue
304  **/
i40e_service_event_schedule(struct i40e_pf * pf)305 void i40e_service_event_schedule(struct i40e_pf *pf)
306 {
307 	if ((!test_bit(__I40E_DOWN, pf->state) &&
308 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
309 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
310 		queue_work(i40e_wq, &pf->service_task);
311 }
312 
313 /**
314  * i40e_tx_timeout - Respond to a Tx Hang
315  * @netdev: network interface device structure
316  * @txqueue: queue number timing out
317  *
318  * If any port has noticed a Tx timeout, it is likely that the whole
319  * device is munged, not just the one netdev port, so go for the full
320  * reset.
321  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)322 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
323 {
324 	struct i40e_netdev_priv *np = netdev_priv(netdev);
325 	struct i40e_vsi *vsi = np->vsi;
326 	struct i40e_pf *pf = vsi->back;
327 	struct i40e_ring *tx_ring = NULL;
328 	unsigned int i;
329 	u32 head, val;
330 
331 	pf->tx_timeout_count++;
332 
333 	/* with txqueue index, find the tx_ring struct */
334 	for (i = 0; i < vsi->num_queue_pairs; i++) {
335 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
336 			if (txqueue ==
337 			    vsi->tx_rings[i]->queue_index) {
338 				tx_ring = vsi->tx_rings[i];
339 				break;
340 			}
341 		}
342 	}
343 
344 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
345 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
346 	else if (time_before(jiffies,
347 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
348 		return;   /* don't do any new action before the next timeout */
349 
350 	/* don't kick off another recovery if one is already pending */
351 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
352 		return;
353 
354 	if (tx_ring) {
355 		head = i40e_get_head(tx_ring);
356 		/* Read interrupt register */
357 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 			val = rd32(&pf->hw,
359 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 						tx_ring->vsi->base_vector - 1));
361 		else
362 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363 
364 		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",
365 			    vsi->seid, txqueue, tx_ring->next_to_clean,
366 			    head, tx_ring->next_to_use,
367 			    readl(tx_ring->tail), val);
368 	}
369 
370 	pf->tx_timeout_last_recovery = jiffies;
371 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
372 		    pf->tx_timeout_recovery_level, txqueue);
373 
374 	switch (pf->tx_timeout_recovery_level) {
375 	case 1:
376 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
377 		break;
378 	case 2:
379 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
380 		break;
381 	case 3:
382 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
383 		break;
384 	default:
385 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
386 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
387 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
388 		break;
389 	}
390 
391 	i40e_service_event_schedule(pf);
392 	pf->tx_timeout_recovery_level++;
393 }
394 
395 /**
396  * i40e_get_vsi_stats_struct - Get System Network Statistics
397  * @vsi: the VSI we care about
398  *
399  * Returns the address of the device statistics structure.
400  * The statistics are actually updated from the service task.
401  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
403 {
404 	return &vsi->net_stats;
405 }
406 
407 /**
408  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409  * @ring: Tx ring to get statistics from
410  * @stats: statistics entry to be updated
411  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 					    struct rtnl_link_stats64 *stats)
414 {
415 	u64 bytes, packets;
416 	unsigned int start;
417 
418 	do {
419 		start = u64_stats_fetch_begin_irq(&ring->syncp);
420 		packets = ring->stats.packets;
421 		bytes   = ring->stats.bytes;
422 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
423 
424 	stats->tx_packets += packets;
425 	stats->tx_bytes   += bytes;
426 }
427 
428 /**
429  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430  * @netdev: network interface device structure
431  * @stats: data structure to store statistics
432  *
433  * Returns the address of the device statistics structure.
434  * The statistics are actually updated from the service task.
435  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 				  struct rtnl_link_stats64 *stats)
438 {
439 	struct i40e_netdev_priv *np = netdev_priv(netdev);
440 	struct i40e_vsi *vsi = np->vsi;
441 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 	struct i40e_ring *ring;
443 	int i;
444 
445 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
446 		return;
447 
448 	if (!vsi->tx_rings)
449 		return;
450 
451 	rcu_read_lock();
452 	for (i = 0; i < vsi->num_queue_pairs; i++) {
453 		u64 bytes, packets;
454 		unsigned int start;
455 
456 		ring = READ_ONCE(vsi->tx_rings[i]);
457 		if (!ring)
458 			continue;
459 		i40e_get_netdev_stats_struct_tx(ring, stats);
460 
461 		if (i40e_enabled_xdp_vsi(vsi)) {
462 			ring = READ_ONCE(vsi->xdp_rings[i]);
463 			if (!ring)
464 				continue;
465 			i40e_get_netdev_stats_struct_tx(ring, stats);
466 		}
467 
468 		ring = READ_ONCE(vsi->rx_rings[i]);
469 		if (!ring)
470 			continue;
471 		do {
472 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
473 			packets = ring->stats.packets;
474 			bytes   = ring->stats.bytes;
475 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
476 
477 		stats->rx_packets += packets;
478 		stats->rx_bytes   += bytes;
479 
480 	}
481 	rcu_read_unlock();
482 
483 	/* following stats updated by i40e_watchdog_subtask() */
484 	stats->multicast	= vsi_stats->multicast;
485 	stats->tx_errors	= vsi_stats->tx_errors;
486 	stats->tx_dropped	= vsi_stats->tx_dropped;
487 	stats->rx_errors	= vsi_stats->rx_errors;
488 	stats->rx_dropped	= vsi_stats->rx_dropped;
489 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
490 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
491 }
492 
493 /**
494  * i40e_vsi_reset_stats - Resets all stats of the given vsi
495  * @vsi: the VSI to have its stats reset
496  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
498 {
499 	struct rtnl_link_stats64 *ns;
500 	int i;
501 
502 	if (!vsi)
503 		return;
504 
505 	ns = i40e_get_vsi_stats_struct(vsi);
506 	memset(ns, 0, sizeof(*ns));
507 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 	if (vsi->rx_rings && vsi->rx_rings[0]) {
511 		for (i = 0; i < vsi->num_queue_pairs; i++) {
512 			memset(&vsi->rx_rings[i]->stats, 0,
513 			       sizeof(vsi->rx_rings[i]->stats));
514 			memset(&vsi->rx_rings[i]->rx_stats, 0,
515 			       sizeof(vsi->rx_rings[i]->rx_stats));
516 			memset(&vsi->tx_rings[i]->stats, 0,
517 			       sizeof(vsi->tx_rings[i]->stats));
518 			memset(&vsi->tx_rings[i]->tx_stats, 0,
519 			       sizeof(vsi->tx_rings[i]->tx_stats));
520 		}
521 	}
522 	vsi->stat_offsets_loaded = false;
523 }
524 
525 /**
526  * i40e_pf_reset_stats - Reset all of the stats for the given PF
527  * @pf: the PF to be reset
528  **/
i40e_pf_reset_stats(struct i40e_pf * pf)529 void i40e_pf_reset_stats(struct i40e_pf *pf)
530 {
531 	int i;
532 
533 	memset(&pf->stats, 0, sizeof(pf->stats));
534 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 	pf->stat_offsets_loaded = false;
536 
537 	for (i = 0; i < I40E_MAX_VEB; i++) {
538 		if (pf->veb[i]) {
539 			memset(&pf->veb[i]->stats, 0,
540 			       sizeof(pf->veb[i]->stats));
541 			memset(&pf->veb[i]->stats_offsets, 0,
542 			       sizeof(pf->veb[i]->stats_offsets));
543 			memset(&pf->veb[i]->tc_stats, 0,
544 			       sizeof(pf->veb[i]->tc_stats));
545 			memset(&pf->veb[i]->tc_stats_offsets, 0,
546 			       sizeof(pf->veb[i]->tc_stats_offsets));
547 			pf->veb[i]->stat_offsets_loaded = false;
548 		}
549 	}
550 	pf->hw_csum_rx_error = 0;
551 }
552 
553 /**
554  * i40e_compute_pci_to_hw_id - compute index form PCI function.
555  * @vsi: ptr to the VSI to read from.
556  * @hw: ptr to the hardware info.
557  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)558 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
559 {
560 	int pf_count = i40e_get_pf_count(hw);
561 
562 	if (vsi->type == I40E_VSI_SRIOV)
563 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
564 
565 	return hw->port + BIT(7);
566 }
567 
568 /**
569  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
570  * @hw: ptr to the hardware info.
571  * @hireg: the high 32 bit reg to read.
572  * @loreg: the low 32 bit reg to read.
573  * @offset_loaded: has the initial offset been loaded yet.
574  * @offset: ptr to current offset value.
575  * @stat: ptr to the stat.
576  *
577  * Since the device stats are not reset at PFReset, they will not
578  * be zeroed when the driver starts.  We'll save the first values read
579  * and use them as offsets to be subtracted from the raw values in order
580  * to report stats that count from zero.
581  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)582 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
583 			       bool offset_loaded, u64 *offset, u64 *stat)
584 {
585 	u64 new_data;
586 
587 	new_data = rd64(hw, loreg);
588 
589 	if (!offset_loaded || new_data < *offset)
590 		*offset = new_data;
591 	*stat = new_data - *offset;
592 }
593 
594 /**
595  * i40e_stat_update48 - read and update a 48 bit stat from the chip
596  * @hw: ptr to the hardware info
597  * @hireg: the high 32 bit reg to read
598  * @loreg: the low 32 bit reg to read
599  * @offset_loaded: has the initial offset been loaded yet
600  * @offset: ptr to current offset value
601  * @stat: ptr to the stat
602  *
603  * Since the device stats are not reset at PFReset, they likely will not
604  * be zeroed when the driver starts.  We'll save the first values read
605  * and use them as offsets to be subtracted from the raw values in order
606  * to report stats that count from zero.  In the process, we also manage
607  * the potential roll-over.
608  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)609 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
610 			       bool offset_loaded, u64 *offset, u64 *stat)
611 {
612 	u64 new_data;
613 
614 	if (hw->device_id == I40E_DEV_ID_QEMU) {
615 		new_data = rd32(hw, loreg);
616 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
617 	} else {
618 		new_data = rd64(hw, loreg);
619 	}
620 	if (!offset_loaded)
621 		*offset = new_data;
622 	if (likely(new_data >= *offset))
623 		*stat = new_data - *offset;
624 	else
625 		*stat = (new_data + BIT_ULL(48)) - *offset;
626 	*stat &= 0xFFFFFFFFFFFFULL;
627 }
628 
629 /**
630  * i40e_stat_update32 - read and update a 32 bit stat from the chip
631  * @hw: ptr to the hardware info
632  * @reg: the hw reg to read
633  * @offset_loaded: has the initial offset been loaded yet
634  * @offset: ptr to current offset value
635  * @stat: ptr to the stat
636  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)637 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
638 			       bool offset_loaded, u64 *offset, u64 *stat)
639 {
640 	u32 new_data;
641 
642 	new_data = rd32(hw, reg);
643 	if (!offset_loaded)
644 		*offset = new_data;
645 	if (likely(new_data >= *offset))
646 		*stat = (u32)(new_data - *offset);
647 	else
648 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
649 }
650 
651 /**
652  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
653  * @hw: ptr to the hardware info
654  * @reg: the hw reg to read and clear
655  * @stat: ptr to the stat
656  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)657 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
658 {
659 	u32 new_data = rd32(hw, reg);
660 
661 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
662 	*stat += new_data;
663 }
664 
665 /**
666  * i40e_stats_update_rx_discards - update rx_discards.
667  * @vsi: ptr to the VSI to be updated.
668  * @hw: ptr to the hardware info.
669  * @stat_idx: VSI's stat_counter_idx.
670  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
671  * @stat_offset: ptr to stat_offset to store first read of specific register.
672  * @stat: ptr to VSI's stat to be updated.
673  **/
674 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)675 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
676 			      int stat_idx, bool offset_loaded,
677 			      struct i40e_eth_stats *stat_offset,
678 			      struct i40e_eth_stats *stat)
679 {
680 	u64 rx_rdpc, rx_rxerr;
681 
682 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
683 			   &stat_offset->rx_discards, &rx_rdpc);
684 	i40e_stat_update64(hw,
685 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
686 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
687 			   offset_loaded, &stat_offset->rx_discards_other,
688 			   &rx_rxerr);
689 
690 	stat->rx_discards = rx_rdpc + rx_rxerr;
691 }
692 
693 /**
694  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
695  * @vsi: the VSI to be updated
696  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)697 void i40e_update_eth_stats(struct i40e_vsi *vsi)
698 {
699 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
700 	struct i40e_pf *pf = vsi->back;
701 	struct i40e_hw *hw = &pf->hw;
702 	struct i40e_eth_stats *oes;
703 	struct i40e_eth_stats *es;     /* device's eth stats */
704 
705 	es = &vsi->eth_stats;
706 	oes = &vsi->eth_stats_offsets;
707 
708 	/* Gather up the stats that the hw collects */
709 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
710 			   vsi->stat_offsets_loaded,
711 			   &oes->tx_errors, &es->tx_errors);
712 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
713 			   vsi->stat_offsets_loaded,
714 			   &oes->rx_discards, &es->rx_discards);
715 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
716 			   vsi->stat_offsets_loaded,
717 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
718 
719 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
720 			   I40E_GLV_GORCL(stat_idx),
721 			   vsi->stat_offsets_loaded,
722 			   &oes->rx_bytes, &es->rx_bytes);
723 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
724 			   I40E_GLV_UPRCL(stat_idx),
725 			   vsi->stat_offsets_loaded,
726 			   &oes->rx_unicast, &es->rx_unicast);
727 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
728 			   I40E_GLV_MPRCL(stat_idx),
729 			   vsi->stat_offsets_loaded,
730 			   &oes->rx_multicast, &es->rx_multicast);
731 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
732 			   I40E_GLV_BPRCL(stat_idx),
733 			   vsi->stat_offsets_loaded,
734 			   &oes->rx_broadcast, &es->rx_broadcast);
735 
736 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
737 			   I40E_GLV_GOTCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->tx_bytes, &es->tx_bytes);
740 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
741 			   I40E_GLV_UPTCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->tx_unicast, &es->tx_unicast);
744 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
745 			   I40E_GLV_MPTCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->tx_multicast, &es->tx_multicast);
748 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
749 			   I40E_GLV_BPTCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->tx_broadcast, &es->tx_broadcast);
752 
753 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
754 				      vsi->stat_offsets_loaded, oes, es);
755 
756 	vsi->stat_offsets_loaded = true;
757 }
758 
759 /**
760  * i40e_update_veb_stats - Update Switch component statistics
761  * @veb: the VEB being updated
762  **/
i40e_update_veb_stats(struct i40e_veb * veb)763 void i40e_update_veb_stats(struct i40e_veb *veb)
764 {
765 	struct i40e_pf *pf = veb->pf;
766 	struct i40e_hw *hw = &pf->hw;
767 	struct i40e_eth_stats *oes;
768 	struct i40e_eth_stats *es;     /* device's eth stats */
769 	struct i40e_veb_tc_stats *veb_oes;
770 	struct i40e_veb_tc_stats *veb_es;
771 	int i, idx = 0;
772 
773 	idx = veb->stats_idx;
774 	es = &veb->stats;
775 	oes = &veb->stats_offsets;
776 	veb_es = &veb->tc_stats;
777 	veb_oes = &veb->tc_stats_offsets;
778 
779 	/* Gather up the stats that the hw collects */
780 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
781 			   veb->stat_offsets_loaded,
782 			   &oes->tx_discards, &es->tx_discards);
783 	if (hw->revision_id > 0)
784 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
785 				   veb->stat_offsets_loaded,
786 				   &oes->rx_unknown_protocol,
787 				   &es->rx_unknown_protocol);
788 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
789 			   veb->stat_offsets_loaded,
790 			   &oes->rx_bytes, &es->rx_bytes);
791 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->rx_unicast, &es->rx_unicast);
794 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->rx_multicast, &es->rx_multicast);
797 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_broadcast, &es->rx_broadcast);
800 
801 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
802 			   veb->stat_offsets_loaded,
803 			   &oes->tx_bytes, &es->tx_bytes);
804 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->tx_unicast, &es->tx_unicast);
807 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->tx_multicast, &es->tx_multicast);
810 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_broadcast, &es->tx_broadcast);
813 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
814 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
815 				   I40E_GLVEBTC_RPCL(i, idx),
816 				   veb->stat_offsets_loaded,
817 				   &veb_oes->tc_rx_packets[i],
818 				   &veb_es->tc_rx_packets[i]);
819 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
820 				   I40E_GLVEBTC_RBCL(i, idx),
821 				   veb->stat_offsets_loaded,
822 				   &veb_oes->tc_rx_bytes[i],
823 				   &veb_es->tc_rx_bytes[i]);
824 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
825 				   I40E_GLVEBTC_TPCL(i, idx),
826 				   veb->stat_offsets_loaded,
827 				   &veb_oes->tc_tx_packets[i],
828 				   &veb_es->tc_tx_packets[i]);
829 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
830 				   I40E_GLVEBTC_TBCL(i, idx),
831 				   veb->stat_offsets_loaded,
832 				   &veb_oes->tc_tx_bytes[i],
833 				   &veb_es->tc_tx_bytes[i]);
834 	}
835 	veb->stat_offsets_loaded = true;
836 }
837 
838 /**
839  * i40e_update_vsi_stats - Update the vsi statistics counters.
840  * @vsi: the VSI to be updated
841  *
842  * There are a few instances where we store the same stat in a
843  * couple of different structs.  This is partly because we have
844  * the netdev stats that need to be filled out, which is slightly
845  * different from the "eth_stats" defined by the chip and used in
846  * VF communications.  We sort it out here.
847  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)848 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
849 {
850 	struct i40e_pf *pf = vsi->back;
851 	struct rtnl_link_stats64 *ons;
852 	struct rtnl_link_stats64 *ns;   /* netdev stats */
853 	struct i40e_eth_stats *oes;
854 	struct i40e_eth_stats *es;     /* device's eth stats */
855 	u64 tx_restart, tx_busy;
856 	struct i40e_ring *p;
857 	u64 rx_page, rx_buf;
858 	u64 bytes, packets;
859 	unsigned int start;
860 	u64 tx_linearize;
861 	u64 tx_force_wb;
862 	u64 rx_p, rx_b;
863 	u64 tx_p, tx_b;
864 	u16 q;
865 
866 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
867 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
868 		return;
869 
870 	ns = i40e_get_vsi_stats_struct(vsi);
871 	ons = &vsi->net_stats_offsets;
872 	es = &vsi->eth_stats;
873 	oes = &vsi->eth_stats_offsets;
874 
875 	/* Gather up the netdev and vsi stats that the driver collects
876 	 * on the fly during packet processing
877 	 */
878 	rx_b = rx_p = 0;
879 	tx_b = tx_p = 0;
880 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
881 	rx_page = 0;
882 	rx_buf = 0;
883 	rcu_read_lock();
884 	for (q = 0; q < vsi->num_queue_pairs; q++) {
885 		/* locate Tx ring */
886 		p = READ_ONCE(vsi->tx_rings[q]);
887 		if (!p)
888 			continue;
889 
890 		do {
891 			start = u64_stats_fetch_begin_irq(&p->syncp);
892 			packets = p->stats.packets;
893 			bytes = p->stats.bytes;
894 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
895 		tx_b += bytes;
896 		tx_p += packets;
897 		tx_restart += p->tx_stats.restart_queue;
898 		tx_busy += p->tx_stats.tx_busy;
899 		tx_linearize += p->tx_stats.tx_linearize;
900 		tx_force_wb += p->tx_stats.tx_force_wb;
901 
902 		/* locate Rx ring */
903 		p = READ_ONCE(vsi->rx_rings[q]);
904 		if (!p)
905 			continue;
906 
907 		do {
908 			start = u64_stats_fetch_begin_irq(&p->syncp);
909 			packets = p->stats.packets;
910 			bytes = p->stats.bytes;
911 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
912 		rx_b += bytes;
913 		rx_p += packets;
914 		rx_buf += p->rx_stats.alloc_buff_failed;
915 		rx_page += p->rx_stats.alloc_page_failed;
916 
917 		if (i40e_enabled_xdp_vsi(vsi)) {
918 			/* locate XDP ring */
919 			p = READ_ONCE(vsi->xdp_rings[q]);
920 			if (!p)
921 				continue;
922 
923 			do {
924 				start = u64_stats_fetch_begin_irq(&p->syncp);
925 				packets = p->stats.packets;
926 				bytes = p->stats.bytes;
927 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
928 			tx_b += bytes;
929 			tx_p += packets;
930 			tx_restart += p->tx_stats.restart_queue;
931 			tx_busy += p->tx_stats.tx_busy;
932 			tx_linearize += p->tx_stats.tx_linearize;
933 			tx_force_wb += p->tx_stats.tx_force_wb;
934 		}
935 	}
936 	rcu_read_unlock();
937 	vsi->tx_restart = tx_restart;
938 	vsi->tx_busy = tx_busy;
939 	vsi->tx_linearize = tx_linearize;
940 	vsi->tx_force_wb = tx_force_wb;
941 	vsi->rx_page_failed = rx_page;
942 	vsi->rx_buf_failed = rx_buf;
943 
944 	ns->rx_packets = rx_p;
945 	ns->rx_bytes = rx_b;
946 	ns->tx_packets = tx_p;
947 	ns->tx_bytes = tx_b;
948 
949 	/* update netdev stats from eth stats */
950 	i40e_update_eth_stats(vsi);
951 	ons->tx_errors = oes->tx_errors;
952 	ns->tx_errors = es->tx_errors;
953 	ons->multicast = oes->rx_multicast;
954 	ns->multicast = es->rx_multicast;
955 	ons->rx_dropped = oes->rx_discards;
956 	ns->rx_dropped = es->rx_discards;
957 	ons->tx_dropped = oes->tx_discards;
958 	ns->tx_dropped = es->tx_discards;
959 
960 	/* pull in a couple PF stats if this is the main vsi */
961 	if (vsi == pf->vsi[pf->lan_vsi]) {
962 		ns->rx_crc_errors = pf->stats.crc_errors;
963 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
964 		ns->rx_length_errors = pf->stats.rx_length_errors;
965 	}
966 }
967 
968 /**
969  * i40e_update_pf_stats - Update the PF statistics counters.
970  * @pf: the PF to be updated
971  **/
i40e_update_pf_stats(struct i40e_pf * pf)972 static void i40e_update_pf_stats(struct i40e_pf *pf)
973 {
974 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
975 	struct i40e_hw_port_stats *nsd = &pf->stats;
976 	struct i40e_hw *hw = &pf->hw;
977 	u32 val;
978 	int i;
979 
980 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
981 			   I40E_GLPRT_GORCL(hw->port),
982 			   pf->stat_offsets_loaded,
983 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
984 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
985 			   I40E_GLPRT_GOTCL(hw->port),
986 			   pf->stat_offsets_loaded,
987 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
988 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
989 			   pf->stat_offsets_loaded,
990 			   &osd->eth.rx_discards,
991 			   &nsd->eth.rx_discards);
992 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
993 			   I40E_GLPRT_UPRCL(hw->port),
994 			   pf->stat_offsets_loaded,
995 			   &osd->eth.rx_unicast,
996 			   &nsd->eth.rx_unicast);
997 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
998 			   I40E_GLPRT_MPRCL(hw->port),
999 			   pf->stat_offsets_loaded,
1000 			   &osd->eth.rx_multicast,
1001 			   &nsd->eth.rx_multicast);
1002 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1003 			   I40E_GLPRT_BPRCL(hw->port),
1004 			   pf->stat_offsets_loaded,
1005 			   &osd->eth.rx_broadcast,
1006 			   &nsd->eth.rx_broadcast);
1007 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1008 			   I40E_GLPRT_UPTCL(hw->port),
1009 			   pf->stat_offsets_loaded,
1010 			   &osd->eth.tx_unicast,
1011 			   &nsd->eth.tx_unicast);
1012 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1013 			   I40E_GLPRT_MPTCL(hw->port),
1014 			   pf->stat_offsets_loaded,
1015 			   &osd->eth.tx_multicast,
1016 			   &nsd->eth.tx_multicast);
1017 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1018 			   I40E_GLPRT_BPTCL(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->eth.tx_broadcast,
1021 			   &nsd->eth.tx_broadcast);
1022 
1023 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->tx_dropped_link_down,
1026 			   &nsd->tx_dropped_link_down);
1027 
1028 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->crc_errors, &nsd->crc_errors);
1031 
1032 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1035 
1036 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->mac_local_faults,
1039 			   &nsd->mac_local_faults);
1040 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1041 			   pf->stat_offsets_loaded,
1042 			   &osd->mac_remote_faults,
1043 			   &nsd->mac_remote_faults);
1044 
1045 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1046 			   pf->stat_offsets_loaded,
1047 			   &osd->rx_length_errors,
1048 			   &nsd->rx_length_errors);
1049 
1050 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1053 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1056 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1059 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1062 
1063 	for (i = 0; i < 8; i++) {
1064 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1065 				   pf->stat_offsets_loaded,
1066 				   &osd->priority_xoff_rx[i],
1067 				   &nsd->priority_xoff_rx[i]);
1068 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1069 				   pf->stat_offsets_loaded,
1070 				   &osd->priority_xon_rx[i],
1071 				   &nsd->priority_xon_rx[i]);
1072 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1073 				   pf->stat_offsets_loaded,
1074 				   &osd->priority_xon_tx[i],
1075 				   &nsd->priority_xon_tx[i]);
1076 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1077 				   pf->stat_offsets_loaded,
1078 				   &osd->priority_xoff_tx[i],
1079 				   &nsd->priority_xoff_tx[i]);
1080 		i40e_stat_update32(hw,
1081 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1082 				   pf->stat_offsets_loaded,
1083 				   &osd->priority_xon_2_xoff[i],
1084 				   &nsd->priority_xon_2_xoff[i]);
1085 	}
1086 
1087 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1088 			   I40E_GLPRT_PRC64L(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->rx_size_64, &nsd->rx_size_64);
1091 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1092 			   I40E_GLPRT_PRC127L(hw->port),
1093 			   pf->stat_offsets_loaded,
1094 			   &osd->rx_size_127, &nsd->rx_size_127);
1095 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1096 			   I40E_GLPRT_PRC255L(hw->port),
1097 			   pf->stat_offsets_loaded,
1098 			   &osd->rx_size_255, &nsd->rx_size_255);
1099 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1100 			   I40E_GLPRT_PRC511L(hw->port),
1101 			   pf->stat_offsets_loaded,
1102 			   &osd->rx_size_511, &nsd->rx_size_511);
1103 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1104 			   I40E_GLPRT_PRC1023L(hw->port),
1105 			   pf->stat_offsets_loaded,
1106 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1107 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1108 			   I40E_GLPRT_PRC1522L(hw->port),
1109 			   pf->stat_offsets_loaded,
1110 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1111 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1112 			   I40E_GLPRT_PRC9522L(hw->port),
1113 			   pf->stat_offsets_loaded,
1114 			   &osd->rx_size_big, &nsd->rx_size_big);
1115 
1116 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1117 			   I40E_GLPRT_PTC64L(hw->port),
1118 			   pf->stat_offsets_loaded,
1119 			   &osd->tx_size_64, &nsd->tx_size_64);
1120 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1121 			   I40E_GLPRT_PTC127L(hw->port),
1122 			   pf->stat_offsets_loaded,
1123 			   &osd->tx_size_127, &nsd->tx_size_127);
1124 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1125 			   I40E_GLPRT_PTC255L(hw->port),
1126 			   pf->stat_offsets_loaded,
1127 			   &osd->tx_size_255, &nsd->tx_size_255);
1128 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1129 			   I40E_GLPRT_PTC511L(hw->port),
1130 			   pf->stat_offsets_loaded,
1131 			   &osd->tx_size_511, &nsd->tx_size_511);
1132 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1133 			   I40E_GLPRT_PTC1023L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1137 			   I40E_GLPRT_PTC1522L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1140 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1141 			   I40E_GLPRT_PTC9522L(hw->port),
1142 			   pf->stat_offsets_loaded,
1143 			   &osd->tx_size_big, &nsd->tx_size_big);
1144 
1145 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1146 			   pf->stat_offsets_loaded,
1147 			   &osd->rx_undersize, &nsd->rx_undersize);
1148 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->rx_fragments, &nsd->rx_fragments);
1151 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1152 			   pf->stat_offsets_loaded,
1153 			   &osd->rx_oversize, &nsd->rx_oversize);
1154 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1155 			   pf->stat_offsets_loaded,
1156 			   &osd->rx_jabber, &nsd->rx_jabber);
1157 
1158 	/* FDIR stats */
1159 	i40e_stat_update_and_clear32(hw,
1160 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1161 			&nsd->fd_atr_match);
1162 	i40e_stat_update_and_clear32(hw,
1163 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1164 			&nsd->fd_sb_match);
1165 	i40e_stat_update_and_clear32(hw,
1166 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1167 			&nsd->fd_atr_tunnel_match);
1168 
1169 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1170 	nsd->tx_lpi_status =
1171 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1172 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1173 	nsd->rx_lpi_status =
1174 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1175 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1176 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1179 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1180 			   pf->stat_offsets_loaded,
1181 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1182 
1183 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1184 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1185 		nsd->fd_sb_status = true;
1186 	else
1187 		nsd->fd_sb_status = false;
1188 
1189 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1190 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1191 		nsd->fd_atr_status = true;
1192 	else
1193 		nsd->fd_atr_status = false;
1194 
1195 	pf->stat_offsets_loaded = true;
1196 }
1197 
1198 /**
1199  * i40e_update_stats - Update the various statistics counters.
1200  * @vsi: the VSI to be updated
1201  *
1202  * Update the various stats for this VSI and its related entities.
1203  **/
i40e_update_stats(struct i40e_vsi * vsi)1204 void i40e_update_stats(struct i40e_vsi *vsi)
1205 {
1206 	struct i40e_pf *pf = vsi->back;
1207 
1208 	if (vsi == pf->vsi[pf->lan_vsi])
1209 		i40e_update_pf_stats(pf);
1210 
1211 	i40e_update_vsi_stats(vsi);
1212 }
1213 
1214 /**
1215  * i40e_count_filters - counts VSI mac filters
1216  * @vsi: the VSI to be searched
1217  *
1218  * Returns count of mac filters
1219  **/
i40e_count_filters(struct i40e_vsi * vsi)1220 int i40e_count_filters(struct i40e_vsi *vsi)
1221 {
1222 	struct i40e_mac_filter *f;
1223 	struct hlist_node *h;
1224 	int bkt;
1225 	int cnt = 0;
1226 
1227 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1228 		++cnt;
1229 
1230 	return cnt;
1231 }
1232 
1233 /**
1234  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1235  * @vsi: the VSI to be searched
1236  * @macaddr: the MAC address
1237  * @vlan: the vlan
1238  *
1239  * Returns ptr to the filter object or NULL
1240  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1241 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1242 						const u8 *macaddr, s16 vlan)
1243 {
1244 	struct i40e_mac_filter *f;
1245 	u64 key;
1246 
1247 	if (!vsi || !macaddr)
1248 		return NULL;
1249 
1250 	key = i40e_addr_to_hkey(macaddr);
1251 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1252 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1253 		    (vlan == f->vlan))
1254 			return f;
1255 	}
1256 	return NULL;
1257 }
1258 
1259 /**
1260  * i40e_find_mac - Find a mac addr in the macvlan filters list
1261  * @vsi: the VSI to be searched
1262  * @macaddr: the MAC address we are searching for
1263  *
1264  * Returns the first filter with the provided MAC address or NULL if
1265  * MAC address was not found
1266  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1267 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1268 {
1269 	struct i40e_mac_filter *f;
1270 	u64 key;
1271 
1272 	if (!vsi || !macaddr)
1273 		return NULL;
1274 
1275 	key = i40e_addr_to_hkey(macaddr);
1276 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1277 		if ((ether_addr_equal(macaddr, f->macaddr)))
1278 			return f;
1279 	}
1280 	return NULL;
1281 }
1282 
1283 /**
1284  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1285  * @vsi: the VSI to be searched
1286  *
1287  * Returns true if VSI is in vlan mode or false otherwise
1288  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1289 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1290 {
1291 	/* If we have a PVID, always operate in VLAN mode */
1292 	if (vsi->info.pvid)
1293 		return true;
1294 
1295 	/* We need to operate in VLAN mode whenever we have any filters with
1296 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1297 	 * time, incurring search cost repeatedly. However, we can notice two
1298 	 * things:
1299 	 *
1300 	 * 1) the only place where we can gain a VLAN filter is in
1301 	 *    i40e_add_filter.
1302 	 *
1303 	 * 2) the only place where filters are actually removed is in
1304 	 *    i40e_sync_filters_subtask.
1305 	 *
1306 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1307 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1308 	 * we have to perform the full search after deleting filters in
1309 	 * i40e_sync_filters_subtask, but we already have to search
1310 	 * filters here and can perform the check at the same time. This
1311 	 * results in avoiding embedding a loop for VLAN mode inside another
1312 	 * loop over all the filters, and should maintain correctness as noted
1313 	 * above.
1314 	 */
1315 	return vsi->has_vlan_filter;
1316 }
1317 
1318 /**
1319  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1320  * @vsi: the VSI to configure
1321  * @tmp_add_list: list of filters ready to be added
1322  * @tmp_del_list: list of filters ready to be deleted
1323  * @vlan_filters: the number of active VLAN filters
1324  *
1325  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1326  * behave as expected. If we have any active VLAN filters remaining or about
1327  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1328  * so that they only match against untagged traffic. If we no longer have any
1329  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1330  * so that they match against both tagged and untagged traffic. In this way,
1331  * we ensure that we correctly receive the desired traffic. This ensures that
1332  * when we have an active VLAN we will receive only untagged traffic and
1333  * traffic matching active VLANs. If we have no active VLANs then we will
1334  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1335  *
1336  * Finally, in a similar fashion, this function also corrects filters when
1337  * there is an active PVID assigned to this VSI.
1338  *
1339  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1340  *
1341  * This function is only expected to be called from within
1342  * i40e_sync_vsi_filters.
1343  *
1344  * NOTE: This function expects to be called while under the
1345  * mac_filter_hash_lock
1346  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1347 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1348 					 struct hlist_head *tmp_add_list,
1349 					 struct hlist_head *tmp_del_list,
1350 					 int vlan_filters)
1351 {
1352 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1353 	struct i40e_mac_filter *f, *add_head;
1354 	struct i40e_new_mac_filter *new;
1355 	struct hlist_node *h;
1356 	int bkt, new_vlan;
1357 
1358 	/* To determine if a particular filter needs to be replaced we
1359 	 * have the three following conditions:
1360 	 *
1361 	 * a) if we have a PVID assigned, then all filters which are
1362 	 *    not marked as VLAN=PVID must be replaced with filters that
1363 	 *    are.
1364 	 * b) otherwise, if we have any active VLANS, all filters
1365 	 *    which are marked as VLAN=-1 must be replaced with
1366 	 *    filters marked as VLAN=0
1367 	 * c) finally, if we do not have any active VLANS, all filters
1368 	 *    which are marked as VLAN=0 must be replaced with filters
1369 	 *    marked as VLAN=-1
1370 	 */
1371 
1372 	/* Update the filters about to be added in place */
1373 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1374 		if (pvid && new->f->vlan != pvid)
1375 			new->f->vlan = pvid;
1376 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1377 			new->f->vlan = 0;
1378 		else if (!vlan_filters && new->f->vlan == 0)
1379 			new->f->vlan = I40E_VLAN_ANY;
1380 	}
1381 
1382 	/* Update the remaining active filters */
1383 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1384 		/* Combine the checks for whether a filter needs to be changed
1385 		 * and then determine the new VLAN inside the if block, in
1386 		 * order to avoid duplicating code for adding the new filter
1387 		 * then deleting the old filter.
1388 		 */
1389 		if ((pvid && f->vlan != pvid) ||
1390 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1391 		    (!vlan_filters && f->vlan == 0)) {
1392 			/* Determine the new vlan we will be adding */
1393 			if (pvid)
1394 				new_vlan = pvid;
1395 			else if (vlan_filters)
1396 				new_vlan = 0;
1397 			else
1398 				new_vlan = I40E_VLAN_ANY;
1399 
1400 			/* Create the new filter */
1401 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1402 			if (!add_head)
1403 				return -ENOMEM;
1404 
1405 			/* Create a temporary i40e_new_mac_filter */
1406 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1407 			if (!new)
1408 				return -ENOMEM;
1409 
1410 			new->f = add_head;
1411 			new->state = add_head->state;
1412 
1413 			/* Add the new filter to the tmp list */
1414 			hlist_add_head(&new->hlist, tmp_add_list);
1415 
1416 			/* Put the original filter into the delete list */
1417 			f->state = I40E_FILTER_REMOVE;
1418 			hash_del(&f->hlist);
1419 			hlist_add_head(&f->hlist, tmp_del_list);
1420 		}
1421 	}
1422 
1423 	vsi->has_vlan_filter = !!vlan_filters;
1424 
1425 	return 0;
1426 }
1427 
1428 /**
1429  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1430  * @vsi: the PF Main VSI - inappropriate for any other VSI
1431  * @macaddr: the MAC address
1432  *
1433  * Remove whatever filter the firmware set up so the driver can manage
1434  * its own filtering intelligently.
1435  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1436 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1437 {
1438 	struct i40e_aqc_remove_macvlan_element_data element;
1439 	struct i40e_pf *pf = vsi->back;
1440 
1441 	/* Only appropriate for the PF main VSI */
1442 	if (vsi->type != I40E_VSI_MAIN)
1443 		return;
1444 
1445 	memset(&element, 0, sizeof(element));
1446 	ether_addr_copy(element.mac_addr, macaddr);
1447 	element.vlan_tag = 0;
1448 	/* Ignore error returns, some firmware does it this way... */
1449 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1450 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1451 
1452 	memset(&element, 0, sizeof(element));
1453 	ether_addr_copy(element.mac_addr, macaddr);
1454 	element.vlan_tag = 0;
1455 	/* ...and some firmware does it this way. */
1456 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1457 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1458 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1459 }
1460 
1461 /**
1462  * i40e_add_filter - Add a mac/vlan filter to the VSI
1463  * @vsi: the VSI to be searched
1464  * @macaddr: the MAC address
1465  * @vlan: the vlan
1466  *
1467  * Returns ptr to the filter object or NULL when no memory available.
1468  *
1469  * NOTE: This function is expected to be called with mac_filter_hash_lock
1470  * being held.
1471  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1472 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1473 					const u8 *macaddr, s16 vlan)
1474 {
1475 	struct i40e_mac_filter *f;
1476 	u64 key;
1477 
1478 	if (!vsi || !macaddr)
1479 		return NULL;
1480 
1481 	f = i40e_find_filter(vsi, macaddr, vlan);
1482 	if (!f) {
1483 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1484 		if (!f)
1485 			return NULL;
1486 
1487 		/* Update the boolean indicating if we need to function in
1488 		 * VLAN mode.
1489 		 */
1490 		if (vlan >= 0)
1491 			vsi->has_vlan_filter = true;
1492 
1493 		ether_addr_copy(f->macaddr, macaddr);
1494 		f->vlan = vlan;
1495 		f->state = I40E_FILTER_NEW;
1496 		INIT_HLIST_NODE(&f->hlist);
1497 
1498 		key = i40e_addr_to_hkey(macaddr);
1499 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1500 
1501 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1502 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1503 	}
1504 
1505 	/* If we're asked to add a filter that has been marked for removal, it
1506 	 * is safe to simply restore it to active state. __i40e_del_filter
1507 	 * will have simply deleted any filters which were previously marked
1508 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1509 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1510 	 * task, just restore this filter to the ACTIVE state so that the
1511 	 * sync task leaves it in place
1512 	 */
1513 	if (f->state == I40E_FILTER_REMOVE)
1514 		f->state = I40E_FILTER_ACTIVE;
1515 
1516 	return f;
1517 }
1518 
1519 /**
1520  * __i40e_del_filter - Remove a specific filter from the VSI
1521  * @vsi: VSI to remove from
1522  * @f: the filter to remove from the list
1523  *
1524  * This function should be called instead of i40e_del_filter only if you know
1525  * the exact filter you will remove already, such as via i40e_find_filter or
1526  * i40e_find_mac.
1527  *
1528  * NOTE: This function is expected to be called with mac_filter_hash_lock
1529  * being held.
1530  * ANOTHER NOTE: This function MUST be called from within the context of
1531  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1532  * instead of list_for_each_entry().
1533  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1534 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1535 {
1536 	if (!f)
1537 		return;
1538 
1539 	/* If the filter was never added to firmware then we can just delete it
1540 	 * directly and we don't want to set the status to remove or else an
1541 	 * admin queue command will unnecessarily fire.
1542 	 */
1543 	if ((f->state == I40E_FILTER_FAILED) ||
1544 	    (f->state == I40E_FILTER_NEW)) {
1545 		hash_del(&f->hlist);
1546 		kfree(f);
1547 	} else {
1548 		f->state = I40E_FILTER_REMOVE;
1549 	}
1550 
1551 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1552 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1553 }
1554 
1555 /**
1556  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1557  * @vsi: the VSI to be searched
1558  * @macaddr: the MAC address
1559  * @vlan: the VLAN
1560  *
1561  * NOTE: This function is expected to be called with mac_filter_hash_lock
1562  * being held.
1563  * ANOTHER NOTE: This function MUST be called from within the context of
1564  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1565  * instead of list_for_each_entry().
1566  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1567 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1568 {
1569 	struct i40e_mac_filter *f;
1570 
1571 	if (!vsi || !macaddr)
1572 		return;
1573 
1574 	f = i40e_find_filter(vsi, macaddr, vlan);
1575 	__i40e_del_filter(vsi, f);
1576 }
1577 
1578 /**
1579  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1580  * @vsi: the VSI to be searched
1581  * @macaddr: the mac address to be filtered
1582  *
1583  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1584  * go through all the macvlan filters and add a macvlan filter for each
1585  * unique vlan that already exists. If a PVID has been assigned, instead only
1586  * add the macaddr to that VLAN.
1587  *
1588  * Returns last filter added on success, else NULL
1589  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1590 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1591 					    const u8 *macaddr)
1592 {
1593 	struct i40e_mac_filter *f, *add = NULL;
1594 	struct hlist_node *h;
1595 	int bkt;
1596 
1597 	if (vsi->info.pvid)
1598 		return i40e_add_filter(vsi, macaddr,
1599 				       le16_to_cpu(vsi->info.pvid));
1600 
1601 	if (!i40e_is_vsi_in_vlan(vsi))
1602 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1603 
1604 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1605 		if (f->state == I40E_FILTER_REMOVE)
1606 			continue;
1607 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1608 		if (!add)
1609 			return NULL;
1610 	}
1611 
1612 	return add;
1613 }
1614 
1615 /**
1616  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1617  * @vsi: the VSI to be searched
1618  * @macaddr: the mac address to be removed
1619  *
1620  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1621  * associated with.
1622  *
1623  * Returns 0 for success, or error
1624  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1625 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1626 {
1627 	struct i40e_mac_filter *f;
1628 	struct hlist_node *h;
1629 	bool found = false;
1630 	int bkt;
1631 
1632 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1633 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1634 		if (ether_addr_equal(macaddr, f->macaddr)) {
1635 			__i40e_del_filter(vsi, f);
1636 			found = true;
1637 		}
1638 	}
1639 
1640 	if (found)
1641 		return 0;
1642 	else
1643 		return -ENOENT;
1644 }
1645 
1646 /**
1647  * i40e_set_mac - NDO callback to set mac address
1648  * @netdev: network interface device structure
1649  * @p: pointer to an address structure
1650  *
1651  * Returns 0 on success, negative on failure
1652  **/
i40e_set_mac(struct net_device * netdev,void * p)1653 static int i40e_set_mac(struct net_device *netdev, void *p)
1654 {
1655 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1656 	struct i40e_vsi *vsi = np->vsi;
1657 	struct i40e_pf *pf = vsi->back;
1658 	struct i40e_hw *hw = &pf->hw;
1659 	struct sockaddr *addr = p;
1660 
1661 	if (!is_valid_ether_addr(addr->sa_data))
1662 		return -EADDRNOTAVAIL;
1663 
1664 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1665 		netdev_info(netdev, "already using mac address %pM\n",
1666 			    addr->sa_data);
1667 		return 0;
1668 	}
1669 
1670 	if (test_bit(__I40E_DOWN, pf->state) ||
1671 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1672 		return -EADDRNOTAVAIL;
1673 
1674 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1675 		netdev_info(netdev, "returning to hw mac address %pM\n",
1676 			    hw->mac.addr);
1677 	else
1678 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1679 
1680 	/* Copy the address first, so that we avoid a possible race with
1681 	 * .set_rx_mode().
1682 	 * - Remove old address from MAC filter
1683 	 * - Copy new address
1684 	 * - Add new address to MAC filter
1685 	 */
1686 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1687 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1688 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1689 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1690 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1691 
1692 	if (vsi->type == I40E_VSI_MAIN) {
1693 		i40e_status ret;
1694 
1695 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1696 						addr->sa_data, NULL);
1697 		if (ret)
1698 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1699 				    i40e_stat_str(hw, ret),
1700 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1701 	}
1702 
1703 	/* schedule our worker thread which will take care of
1704 	 * applying the new filter changes
1705 	 */
1706 	i40e_service_event_schedule(pf);
1707 	return 0;
1708 }
1709 
1710 /**
1711  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1712  * @vsi: vsi structure
1713  * @seed: RSS hash seed
1714  * @lut: pointer to lookup table of lut_size
1715  * @lut_size: size of the lookup table
1716  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1717 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1718 			      u8 *lut, u16 lut_size)
1719 {
1720 	struct i40e_pf *pf = vsi->back;
1721 	struct i40e_hw *hw = &pf->hw;
1722 	int ret = 0;
1723 
1724 	if (seed) {
1725 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1726 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1727 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1728 		if (ret) {
1729 			dev_info(&pf->pdev->dev,
1730 				 "Cannot set RSS key, err %s aq_err %s\n",
1731 				 i40e_stat_str(hw, ret),
1732 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1733 			return ret;
1734 		}
1735 	}
1736 	if (lut) {
1737 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1738 
1739 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1740 		if (ret) {
1741 			dev_info(&pf->pdev->dev,
1742 				 "Cannot set RSS lut, err %s aq_err %s\n",
1743 				 i40e_stat_str(hw, ret),
1744 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1745 			return ret;
1746 		}
1747 	}
1748 	return ret;
1749 }
1750 
1751 /**
1752  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1753  * @vsi: VSI structure
1754  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1755 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1756 {
1757 	struct i40e_pf *pf = vsi->back;
1758 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1759 	u8 *lut;
1760 	int ret;
1761 
1762 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1763 		return 0;
1764 	if (!vsi->rss_size)
1765 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1766 				      vsi->num_queue_pairs);
1767 	if (!vsi->rss_size)
1768 		return -EINVAL;
1769 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1770 	if (!lut)
1771 		return -ENOMEM;
1772 
1773 	/* Use the user configured hash keys and lookup table if there is one,
1774 	 * otherwise use default
1775 	 */
1776 	if (vsi->rss_lut_user)
1777 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1778 	else
1779 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1780 	if (vsi->rss_hkey_user)
1781 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1782 	else
1783 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1784 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1785 	kfree(lut);
1786 	return ret;
1787 }
1788 
1789 /**
1790  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1791  * @vsi: the VSI being configured,
1792  * @ctxt: VSI context structure
1793  * @enabled_tc: number of traffic classes to enable
1794  *
1795  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1796  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1797 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1798 					   struct i40e_vsi_context *ctxt,
1799 					   u8 enabled_tc)
1800 {
1801 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1802 	int i, override_q, pow, num_qps, ret;
1803 	u8 netdev_tc = 0, offset = 0;
1804 
1805 	if (vsi->type != I40E_VSI_MAIN)
1806 		return -EINVAL;
1807 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1808 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1809 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1810 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1811 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1812 
1813 	/* find the next higher power-of-2 of num queue pairs */
1814 	pow = ilog2(num_qps);
1815 	if (!is_power_of_2(num_qps))
1816 		pow++;
1817 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1818 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1819 
1820 	/* Setup queue offset/count for all TCs for given VSI */
1821 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1822 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1823 		/* See if the given TC is enabled for the given VSI */
1824 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1825 			offset = vsi->mqprio_qopt.qopt.offset[i];
1826 			qcount = vsi->mqprio_qopt.qopt.count[i];
1827 			if (qcount > max_qcount)
1828 				max_qcount = qcount;
1829 			vsi->tc_config.tc_info[i].qoffset = offset;
1830 			vsi->tc_config.tc_info[i].qcount = qcount;
1831 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1832 		} else {
1833 			/* TC is not enabled so set the offset to
1834 			 * default queue and allocate one queue
1835 			 * for the given TC.
1836 			 */
1837 			vsi->tc_config.tc_info[i].qoffset = 0;
1838 			vsi->tc_config.tc_info[i].qcount = 1;
1839 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1840 		}
1841 	}
1842 
1843 	/* Set actual Tx/Rx queue pairs */
1844 	vsi->num_queue_pairs = offset + qcount;
1845 
1846 	/* Setup queue TC[0].qmap for given VSI context */
1847 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1848 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1849 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1850 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1851 
1852 	/* Reconfigure RSS for main VSI with max queue count */
1853 	vsi->rss_size = max_qcount;
1854 	ret = i40e_vsi_config_rss(vsi);
1855 	if (ret) {
1856 		dev_info(&vsi->back->pdev->dev,
1857 			 "Failed to reconfig rss for num_queues (%u)\n",
1858 			 max_qcount);
1859 		return ret;
1860 	}
1861 	vsi->reconfig_rss = true;
1862 	dev_dbg(&vsi->back->pdev->dev,
1863 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1864 
1865 	/* Find queue count available for channel VSIs and starting offset
1866 	 * for channel VSIs
1867 	 */
1868 	override_q = vsi->mqprio_qopt.qopt.count[0];
1869 	if (override_q && override_q < vsi->num_queue_pairs) {
1870 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1871 		vsi->next_base_queue = override_q;
1872 	}
1873 	return 0;
1874 }
1875 
1876 /**
1877  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1878  * @vsi: the VSI being setup
1879  * @ctxt: VSI context structure
1880  * @enabled_tc: Enabled TCs bitmap
1881  * @is_add: True if called before Add VSI
1882  *
1883  * Setup VSI queue mapping for enabled traffic classes.
1884  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)1885 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1886 				     struct i40e_vsi_context *ctxt,
1887 				     u8 enabled_tc,
1888 				     bool is_add)
1889 {
1890 	struct i40e_pf *pf = vsi->back;
1891 	u16 num_tc_qps = 0;
1892 	u16 sections = 0;
1893 	u8 netdev_tc = 0;
1894 	u16 numtc = 1;
1895 	u16 qcount;
1896 	u8 offset;
1897 	u16 qmap;
1898 	int i;
1899 
1900 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1901 	offset = 0;
1902 	/* zero out queue mapping, it will get updated on the end of the function */
1903 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1904 
1905 	if (vsi->type == I40E_VSI_MAIN) {
1906 		/* This code helps add more queue to the VSI if we have
1907 		 * more cores than RSS can support, the higher cores will
1908 		 * be served by ATR or other filters. Furthermore, the
1909 		 * non-zero req_queue_pairs says that user requested a new
1910 		 * queue count via ethtool's set_channels, so use this
1911 		 * value for queues distribution across traffic classes
1912 		 * We need at least one queue pair for the interface
1913 		 * to be usable as we see in else statement.
1914 		 */
1915 		if (vsi->req_queue_pairs > 0)
1916 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1917 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1918 			vsi->num_queue_pairs = pf->num_lan_msix;
1919 		else
1920 			vsi->num_queue_pairs = 1;
1921 	}
1922 
1923 	/* Number of queues per enabled TC */
1924 	if (vsi->type == I40E_VSI_MAIN ||
1925 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1926 		num_tc_qps = vsi->num_queue_pairs;
1927 	else
1928 		num_tc_qps = vsi->alloc_queue_pairs;
1929 
1930 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1931 		/* Find numtc from enabled TC bitmap */
1932 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1933 			if (enabled_tc & BIT(i)) /* TC is enabled */
1934 				numtc++;
1935 		}
1936 		if (!numtc) {
1937 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1938 			numtc = 1;
1939 		}
1940 		num_tc_qps = num_tc_qps / numtc;
1941 		num_tc_qps = min_t(int, num_tc_qps,
1942 				   i40e_pf_get_max_q_per_tc(pf));
1943 	}
1944 
1945 	vsi->tc_config.numtc = numtc;
1946 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1947 
1948 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1949 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1950 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1951 
1952 	/* Setup queue offset/count for all TCs for given VSI */
1953 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1954 		/* See if the given TC is enabled for the given VSI */
1955 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1956 			/* TC is enabled */
1957 			int pow, num_qps;
1958 
1959 			switch (vsi->type) {
1960 			case I40E_VSI_MAIN:
1961 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1962 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1963 				    vsi->tc_config.enabled_tc != 1) {
1964 					qcount = min_t(int, pf->alloc_rss_size,
1965 						       num_tc_qps);
1966 					break;
1967 				}
1968 				fallthrough;
1969 			case I40E_VSI_FDIR:
1970 			case I40E_VSI_SRIOV:
1971 			case I40E_VSI_VMDQ2:
1972 			default:
1973 				qcount = num_tc_qps;
1974 				WARN_ON(i != 0);
1975 				break;
1976 			}
1977 			vsi->tc_config.tc_info[i].qoffset = offset;
1978 			vsi->tc_config.tc_info[i].qcount = qcount;
1979 
1980 			/* find the next higher power-of-2 of num queue pairs */
1981 			num_qps = qcount;
1982 			pow = 0;
1983 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1984 				pow++;
1985 				num_qps >>= 1;
1986 			}
1987 
1988 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1989 			qmap =
1990 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1991 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1992 
1993 			offset += qcount;
1994 		} else {
1995 			/* TC is not enabled so set the offset to
1996 			 * default queue and allocate one queue
1997 			 * for the given TC.
1998 			 */
1999 			vsi->tc_config.tc_info[i].qoffset = 0;
2000 			vsi->tc_config.tc_info[i].qcount = 1;
2001 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2002 
2003 			qmap = 0;
2004 		}
2005 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2006 	}
2007 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2008 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2009 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2010 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2011 		vsi->num_queue_pairs = offset;
2012 
2013 	/* Scheduler section valid can only be set for ADD VSI */
2014 	if (is_add) {
2015 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2016 
2017 		ctxt->info.up_enable_bits = enabled_tc;
2018 	}
2019 	if (vsi->type == I40E_VSI_SRIOV) {
2020 		ctxt->info.mapping_flags |=
2021 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2022 		for (i = 0; i < vsi->num_queue_pairs; i++)
2023 			ctxt->info.queue_mapping[i] =
2024 					       cpu_to_le16(vsi->base_queue + i);
2025 	} else {
2026 		ctxt->info.mapping_flags |=
2027 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2028 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2029 	}
2030 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2031 }
2032 
2033 /**
2034  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2035  * @netdev: the netdevice
2036  * @addr: address to add
2037  *
2038  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2039  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2040  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2041 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2042 {
2043 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2044 	struct i40e_vsi *vsi = np->vsi;
2045 
2046 	if (i40e_add_mac_filter(vsi, addr))
2047 		return 0;
2048 	else
2049 		return -ENOMEM;
2050 }
2051 
2052 /**
2053  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2054  * @netdev: the netdevice
2055  * @addr: address to add
2056  *
2057  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2058  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2059  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2060 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2061 {
2062 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2063 	struct i40e_vsi *vsi = np->vsi;
2064 
2065 	/* Under some circumstances, we might receive a request to delete
2066 	 * our own device address from our uc list. Because we store the
2067 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2068 	 * such requests and not delete our device address from this list.
2069 	 */
2070 	if (ether_addr_equal(addr, netdev->dev_addr))
2071 		return 0;
2072 
2073 	i40e_del_mac_filter(vsi, addr);
2074 
2075 	return 0;
2076 }
2077 
2078 /**
2079  * i40e_set_rx_mode - NDO callback to set the netdev filters
2080  * @netdev: network interface device structure
2081  **/
i40e_set_rx_mode(struct net_device * netdev)2082 static void i40e_set_rx_mode(struct net_device *netdev)
2083 {
2084 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2085 	struct i40e_vsi *vsi = np->vsi;
2086 
2087 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2088 
2089 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2090 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2091 
2092 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2093 
2094 	/* check for other flag changes */
2095 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2096 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2097 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2098 	}
2099 }
2100 
2101 /**
2102  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2103  * @vsi: Pointer to VSI struct
2104  * @from: Pointer to list which contains MAC filter entries - changes to
2105  *        those entries needs to be undone.
2106  *
2107  * MAC filter entries from this list were slated for deletion.
2108  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2109 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2110 					 struct hlist_head *from)
2111 {
2112 	struct i40e_mac_filter *f;
2113 	struct hlist_node *h;
2114 
2115 	hlist_for_each_entry_safe(f, h, from, hlist) {
2116 		u64 key = i40e_addr_to_hkey(f->macaddr);
2117 
2118 		/* Move the element back into MAC filter list*/
2119 		hlist_del(&f->hlist);
2120 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2121 	}
2122 }
2123 
2124 /**
2125  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2126  * @vsi: Pointer to vsi struct
2127  * @from: Pointer to list which contains MAC filter entries - changes to
2128  *        those entries needs to be undone.
2129  *
2130  * MAC filter entries from this list were slated for addition.
2131  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2132 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2133 					 struct hlist_head *from)
2134 {
2135 	struct i40e_new_mac_filter *new;
2136 	struct hlist_node *h;
2137 
2138 	hlist_for_each_entry_safe(new, h, from, hlist) {
2139 		/* We can simply free the wrapper structure */
2140 		hlist_del(&new->hlist);
2141 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2142 		kfree(new);
2143 	}
2144 }
2145 
2146 /**
2147  * i40e_next_entry - Get the next non-broadcast filter from a list
2148  * @next: pointer to filter in list
2149  *
2150  * Returns the next non-broadcast filter in the list. Required so that we
2151  * ignore broadcast filters within the list, since these are not handled via
2152  * the normal firmware update path.
2153  */
2154 static
i40e_next_filter(struct i40e_new_mac_filter * next)2155 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2156 {
2157 	hlist_for_each_entry_continue(next, hlist) {
2158 		if (!is_broadcast_ether_addr(next->f->macaddr))
2159 			return next;
2160 	}
2161 
2162 	return NULL;
2163 }
2164 
2165 /**
2166  * i40e_update_filter_state - Update filter state based on return data
2167  * from firmware
2168  * @count: Number of filters added
2169  * @add_list: return data from fw
2170  * @add_head: pointer to first filter in current batch
2171  *
2172  * MAC filter entries from list were slated to be added to device. Returns
2173  * number of successful filters. Note that 0 does NOT mean success!
2174  **/
2175 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2176 i40e_update_filter_state(int count,
2177 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2178 			 struct i40e_new_mac_filter *add_head)
2179 {
2180 	int retval = 0;
2181 	int i;
2182 
2183 	for (i = 0; i < count; i++) {
2184 		/* Always check status of each filter. We don't need to check
2185 		 * the firmware return status because we pre-set the filter
2186 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2187 		 * request to the adminq. Thus, if it no longer matches then
2188 		 * we know the filter is active.
2189 		 */
2190 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2191 			add_head->state = I40E_FILTER_FAILED;
2192 		} else {
2193 			add_head->state = I40E_FILTER_ACTIVE;
2194 			retval++;
2195 		}
2196 
2197 		add_head = i40e_next_filter(add_head);
2198 		if (!add_head)
2199 			break;
2200 	}
2201 
2202 	return retval;
2203 }
2204 
2205 /**
2206  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2207  * @vsi: ptr to the VSI
2208  * @vsi_name: name to display in messages
2209  * @list: the list of filters to send to firmware
2210  * @num_del: the number of filters to delete
2211  * @retval: Set to -EIO on failure to delete
2212  *
2213  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2214  * *retval instead of a return value so that success does not force ret_val to
2215  * be set to 0. This ensures that a sequence of calls to this function
2216  * preserve the previous value of *retval on successful delete.
2217  */
2218 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)2219 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2220 			  struct i40e_aqc_remove_macvlan_element_data *list,
2221 			  int num_del, int *retval)
2222 {
2223 	struct i40e_hw *hw = &vsi->back->hw;
2224 	i40e_status aq_ret;
2225 	int aq_err;
2226 
2227 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2228 	aq_err = hw->aq.asq_last_status;
2229 
2230 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2231 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2232 		*retval = -EIO;
2233 		dev_info(&vsi->back->pdev->dev,
2234 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2235 			 vsi_name, i40e_stat_str(hw, aq_ret),
2236 			 i40e_aq_str(hw, aq_err));
2237 	}
2238 }
2239 
2240 /**
2241  * i40e_aqc_add_filters - Request firmware to add a set of filters
2242  * @vsi: ptr to the VSI
2243  * @vsi_name: name to display in messages
2244  * @list: the list of filters to send to firmware
2245  * @add_head: Position in the add hlist
2246  * @num_add: the number of filters to add
2247  *
2248  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2249  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2250  * space for more filters.
2251  */
2252 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)2253 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2254 			  struct i40e_aqc_add_macvlan_element_data *list,
2255 			  struct i40e_new_mac_filter *add_head,
2256 			  int num_add)
2257 {
2258 	struct i40e_hw *hw = &vsi->back->hw;
2259 	int aq_err, fcnt;
2260 
2261 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2262 	aq_err = hw->aq.asq_last_status;
2263 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2264 
2265 	if (fcnt != num_add) {
2266 		if (vsi->type == I40E_VSI_MAIN) {
2267 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2268 			dev_warn(&vsi->back->pdev->dev,
2269 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2270 				 i40e_aq_str(hw, aq_err), vsi_name);
2271 		} else if (vsi->type == I40E_VSI_SRIOV ||
2272 			   vsi->type == I40E_VSI_VMDQ1 ||
2273 			   vsi->type == I40E_VSI_VMDQ2) {
2274 			dev_warn(&vsi->back->pdev->dev,
2275 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2276 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2277 		} else {
2278 			dev_warn(&vsi->back->pdev->dev,
2279 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2280 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2281 		}
2282 	}
2283 }
2284 
2285 /**
2286  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2287  * @vsi: pointer to the VSI
2288  * @vsi_name: the VSI name
2289  * @f: filter data
2290  *
2291  * This function sets or clears the promiscuous broadcast flags for VLAN
2292  * filters in order to properly receive broadcast frames. Assumes that only
2293  * broadcast filters are passed.
2294  *
2295  * Returns status indicating success or failure;
2296  **/
2297 static i40e_status
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2298 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2299 			  struct i40e_mac_filter *f)
2300 {
2301 	bool enable = f->state == I40E_FILTER_NEW;
2302 	struct i40e_hw *hw = &vsi->back->hw;
2303 	i40e_status aq_ret;
2304 
2305 	if (f->vlan == I40E_VLAN_ANY) {
2306 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2307 						   vsi->seid,
2308 						   enable,
2309 						   NULL);
2310 	} else {
2311 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2312 							    vsi->seid,
2313 							    enable,
2314 							    f->vlan,
2315 							    NULL);
2316 	}
2317 
2318 	if (aq_ret) {
2319 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2320 		dev_warn(&vsi->back->pdev->dev,
2321 			 "Error %s, forcing overflow promiscuous on %s\n",
2322 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2323 			 vsi_name);
2324 	}
2325 
2326 	return aq_ret;
2327 }
2328 
2329 /**
2330  * i40e_set_promiscuous - set promiscuous mode
2331  * @pf: board private structure
2332  * @promisc: promisc on or off
2333  *
2334  * There are different ways of setting promiscuous mode on a PF depending on
2335  * what state/environment we're in.  This identifies and sets it appropriately.
2336  * Returns 0 on success.
2337  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2338 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2339 {
2340 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2341 	struct i40e_hw *hw = &pf->hw;
2342 	i40e_status aq_ret;
2343 
2344 	if (vsi->type == I40E_VSI_MAIN &&
2345 	    pf->lan_veb != I40E_NO_VEB &&
2346 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2347 		/* set defport ON for Main VSI instead of true promisc
2348 		 * this way we will get all unicast/multicast and VLAN
2349 		 * promisc behavior but will not get VF or VMDq traffic
2350 		 * replicated on the Main VSI.
2351 		 */
2352 		if (promisc)
2353 			aq_ret = i40e_aq_set_default_vsi(hw,
2354 							 vsi->seid,
2355 							 NULL);
2356 		else
2357 			aq_ret = i40e_aq_clear_default_vsi(hw,
2358 							   vsi->seid,
2359 							   NULL);
2360 		if (aq_ret) {
2361 			dev_info(&pf->pdev->dev,
2362 				 "Set default VSI failed, err %s, aq_err %s\n",
2363 				 i40e_stat_str(hw, aq_ret),
2364 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2365 		}
2366 	} else {
2367 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2368 						  hw,
2369 						  vsi->seid,
2370 						  promisc, NULL,
2371 						  true);
2372 		if (aq_ret) {
2373 			dev_info(&pf->pdev->dev,
2374 				 "set unicast promisc failed, err %s, aq_err %s\n",
2375 				 i40e_stat_str(hw, aq_ret),
2376 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2377 		}
2378 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2379 						  hw,
2380 						  vsi->seid,
2381 						  promisc, NULL);
2382 		if (aq_ret) {
2383 			dev_info(&pf->pdev->dev,
2384 				 "set multicast promisc failed, err %s, aq_err %s\n",
2385 				 i40e_stat_str(hw, aq_ret),
2386 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2387 		}
2388 	}
2389 
2390 	if (!aq_ret)
2391 		pf->cur_promisc = promisc;
2392 
2393 	return aq_ret;
2394 }
2395 
2396 /**
2397  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2398  * @vsi: ptr to the VSI
2399  *
2400  * Push any outstanding VSI filter changes through the AdminQ.
2401  *
2402  * Returns 0 or error value
2403  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2404 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2405 {
2406 	struct hlist_head tmp_add_list, tmp_del_list;
2407 	struct i40e_mac_filter *f;
2408 	struct i40e_new_mac_filter *new, *add_head = NULL;
2409 	struct i40e_hw *hw = &vsi->back->hw;
2410 	bool old_overflow, new_overflow;
2411 	unsigned int failed_filters = 0;
2412 	unsigned int vlan_filters = 0;
2413 	char vsi_name[16] = "PF";
2414 	int filter_list_len = 0;
2415 	i40e_status aq_ret = 0;
2416 	u32 changed_flags = 0;
2417 	struct hlist_node *h;
2418 	struct i40e_pf *pf;
2419 	int num_add = 0;
2420 	int num_del = 0;
2421 	int retval = 0;
2422 	u16 cmd_flags;
2423 	int list_size;
2424 	int bkt;
2425 
2426 	/* empty array typed pointers, kcalloc later */
2427 	struct i40e_aqc_add_macvlan_element_data *add_list;
2428 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2429 
2430 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2431 		usleep_range(1000, 2000);
2432 	pf = vsi->back;
2433 
2434 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2435 
2436 	if (vsi->netdev) {
2437 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2438 		vsi->current_netdev_flags = vsi->netdev->flags;
2439 	}
2440 
2441 	INIT_HLIST_HEAD(&tmp_add_list);
2442 	INIT_HLIST_HEAD(&tmp_del_list);
2443 
2444 	if (vsi->type == I40E_VSI_SRIOV)
2445 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2446 	else if (vsi->type != I40E_VSI_MAIN)
2447 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2448 
2449 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2450 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2451 
2452 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2453 		/* Create a list of filters to delete. */
2454 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2455 			if (f->state == I40E_FILTER_REMOVE) {
2456 				/* Move the element into temporary del_list */
2457 				hash_del(&f->hlist);
2458 				hlist_add_head(&f->hlist, &tmp_del_list);
2459 
2460 				/* Avoid counting removed filters */
2461 				continue;
2462 			}
2463 			if (f->state == I40E_FILTER_NEW) {
2464 				/* Create a temporary i40e_new_mac_filter */
2465 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2466 				if (!new)
2467 					goto err_no_memory_locked;
2468 
2469 				/* Store pointer to the real filter */
2470 				new->f = f;
2471 				new->state = f->state;
2472 
2473 				/* Add it to the hash list */
2474 				hlist_add_head(&new->hlist, &tmp_add_list);
2475 			}
2476 
2477 			/* Count the number of active (current and new) VLAN
2478 			 * filters we have now. Does not count filters which
2479 			 * are marked for deletion.
2480 			 */
2481 			if (f->vlan > 0)
2482 				vlan_filters++;
2483 		}
2484 
2485 		retval = i40e_correct_mac_vlan_filters(vsi,
2486 						       &tmp_add_list,
2487 						       &tmp_del_list,
2488 						       vlan_filters);
2489 
2490 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2491 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2492 
2493 		if (retval)
2494 			goto err_no_memory_locked;
2495 
2496 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2497 	}
2498 
2499 	/* Now process 'del_list' outside the lock */
2500 	if (!hlist_empty(&tmp_del_list)) {
2501 		filter_list_len = hw->aq.asq_buf_size /
2502 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2503 		list_size = filter_list_len *
2504 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2505 		del_list = kzalloc(list_size, GFP_ATOMIC);
2506 		if (!del_list)
2507 			goto err_no_memory;
2508 
2509 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2510 			cmd_flags = 0;
2511 
2512 			/* handle broadcast filters by updating the broadcast
2513 			 * promiscuous flag and release filter list.
2514 			 */
2515 			if (is_broadcast_ether_addr(f->macaddr)) {
2516 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2517 
2518 				hlist_del(&f->hlist);
2519 				kfree(f);
2520 				continue;
2521 			}
2522 
2523 			/* add to delete list */
2524 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2525 			if (f->vlan == I40E_VLAN_ANY) {
2526 				del_list[num_del].vlan_tag = 0;
2527 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2528 			} else {
2529 				del_list[num_del].vlan_tag =
2530 					cpu_to_le16((u16)(f->vlan));
2531 			}
2532 
2533 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2534 			del_list[num_del].flags = cmd_flags;
2535 			num_del++;
2536 
2537 			/* flush a full buffer */
2538 			if (num_del == filter_list_len) {
2539 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2540 						     num_del, &retval);
2541 				memset(del_list, 0, list_size);
2542 				num_del = 0;
2543 			}
2544 			/* Release memory for MAC filter entries which were
2545 			 * synced up with HW.
2546 			 */
2547 			hlist_del(&f->hlist);
2548 			kfree(f);
2549 		}
2550 
2551 		if (num_del) {
2552 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2553 					     num_del, &retval);
2554 		}
2555 
2556 		kfree(del_list);
2557 		del_list = NULL;
2558 	}
2559 
2560 	if (!hlist_empty(&tmp_add_list)) {
2561 		/* Do all the adds now. */
2562 		filter_list_len = hw->aq.asq_buf_size /
2563 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2564 		list_size = filter_list_len *
2565 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2566 		add_list = kzalloc(list_size, GFP_ATOMIC);
2567 		if (!add_list)
2568 			goto err_no_memory;
2569 
2570 		num_add = 0;
2571 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2572 			/* handle broadcast filters by updating the broadcast
2573 			 * promiscuous flag instead of adding a MAC filter.
2574 			 */
2575 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2576 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2577 							      new->f))
2578 					new->state = I40E_FILTER_FAILED;
2579 				else
2580 					new->state = I40E_FILTER_ACTIVE;
2581 				continue;
2582 			}
2583 
2584 			/* add to add array */
2585 			if (num_add == 0)
2586 				add_head = new;
2587 			cmd_flags = 0;
2588 			ether_addr_copy(add_list[num_add].mac_addr,
2589 					new->f->macaddr);
2590 			if (new->f->vlan == I40E_VLAN_ANY) {
2591 				add_list[num_add].vlan_tag = 0;
2592 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2593 			} else {
2594 				add_list[num_add].vlan_tag =
2595 					cpu_to_le16((u16)(new->f->vlan));
2596 			}
2597 			add_list[num_add].queue_number = 0;
2598 			/* set invalid match method for later detection */
2599 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2600 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2601 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2602 			num_add++;
2603 
2604 			/* flush a full buffer */
2605 			if (num_add == filter_list_len) {
2606 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2607 						     add_head, num_add);
2608 				memset(add_list, 0, list_size);
2609 				num_add = 0;
2610 			}
2611 		}
2612 		if (num_add) {
2613 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2614 					     num_add);
2615 		}
2616 		/* Now move all of the filters from the temp add list back to
2617 		 * the VSI's list.
2618 		 */
2619 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2620 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2621 			/* Only update the state if we're still NEW */
2622 			if (new->f->state == I40E_FILTER_NEW)
2623 				new->f->state = new->state;
2624 			hlist_del(&new->hlist);
2625 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2626 			kfree(new);
2627 		}
2628 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2629 		kfree(add_list);
2630 		add_list = NULL;
2631 	}
2632 
2633 	/* Determine the number of active and failed filters. */
2634 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2635 	vsi->active_filters = 0;
2636 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2637 		if (f->state == I40E_FILTER_ACTIVE)
2638 			vsi->active_filters++;
2639 		else if (f->state == I40E_FILTER_FAILED)
2640 			failed_filters++;
2641 	}
2642 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2643 
2644 	/* Check if we are able to exit overflow promiscuous mode. We can
2645 	 * safely exit if we didn't just enter, we no longer have any failed
2646 	 * filters, and we have reduced filters below the threshold value.
2647 	 */
2648 	if (old_overflow && !failed_filters &&
2649 	    vsi->active_filters < vsi->promisc_threshold) {
2650 		dev_info(&pf->pdev->dev,
2651 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2652 			 vsi_name);
2653 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2654 		vsi->promisc_threshold = 0;
2655 	}
2656 
2657 	/* if the VF is not trusted do not do promisc */
2658 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2659 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2660 		goto out;
2661 	}
2662 
2663 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2664 
2665 	/* If we are entering overflow promiscuous, we need to calculate a new
2666 	 * threshold for when we are safe to exit
2667 	 */
2668 	if (!old_overflow && new_overflow)
2669 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2670 
2671 	/* check for changes in promiscuous modes */
2672 	if (changed_flags & IFF_ALLMULTI) {
2673 		bool cur_multipromisc;
2674 
2675 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2676 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2677 							       vsi->seid,
2678 							       cur_multipromisc,
2679 							       NULL);
2680 		if (aq_ret) {
2681 			retval = i40e_aq_rc_to_posix(aq_ret,
2682 						     hw->aq.asq_last_status);
2683 			dev_info(&pf->pdev->dev,
2684 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2685 				 vsi_name,
2686 				 i40e_stat_str(hw, aq_ret),
2687 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2688 		} else {
2689 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2690 				 cur_multipromisc ? "entering" : "leaving");
2691 		}
2692 	}
2693 
2694 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2695 		bool cur_promisc;
2696 
2697 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2698 			       new_overflow);
2699 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2700 		if (aq_ret) {
2701 			retval = i40e_aq_rc_to_posix(aq_ret,
2702 						     hw->aq.asq_last_status);
2703 			dev_info(&pf->pdev->dev,
2704 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2705 				 cur_promisc ? "on" : "off",
2706 				 vsi_name,
2707 				 i40e_stat_str(hw, aq_ret),
2708 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2709 		}
2710 	}
2711 out:
2712 	/* if something went wrong then set the changed flag so we try again */
2713 	if (retval)
2714 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2715 
2716 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2717 	return retval;
2718 
2719 err_no_memory:
2720 	/* Restore elements on the temporary add and delete lists */
2721 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2722 err_no_memory_locked:
2723 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2724 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2725 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2726 
2727 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2728 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2729 	return -ENOMEM;
2730 }
2731 
2732 /**
2733  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2734  * @pf: board private structure
2735  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2736 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2737 {
2738 	int v;
2739 
2740 	if (!pf)
2741 		return;
2742 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2743 		return;
2744 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2745 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2746 		return;
2747 	}
2748 
2749 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2750 		if (pf->vsi[v] &&
2751 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2752 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2753 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2754 
2755 			if (ret) {
2756 				/* come back and try again later */
2757 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2758 					pf->state);
2759 				break;
2760 			}
2761 		}
2762 	}
2763 }
2764 
2765 /**
2766  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2767  * @vsi: the vsi
2768  **/
i40e_max_xdp_frame_size(struct i40e_vsi * vsi)2769 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2770 {
2771 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2772 		return I40E_RXBUFFER_2048;
2773 	else
2774 		return I40E_RXBUFFER_3072;
2775 }
2776 
2777 /**
2778  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2779  * @netdev: network interface device structure
2780  * @new_mtu: new value for maximum frame size
2781  *
2782  * Returns 0 on success, negative on failure
2783  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2784 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2785 {
2786 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2787 	struct i40e_vsi *vsi = np->vsi;
2788 	struct i40e_pf *pf = vsi->back;
2789 
2790 	if (i40e_enabled_xdp_vsi(vsi)) {
2791 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2792 
2793 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2794 			return -EINVAL;
2795 	}
2796 
2797 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2798 		   netdev->mtu, new_mtu);
2799 	netdev->mtu = new_mtu;
2800 	if (netif_running(netdev))
2801 		i40e_vsi_reinit_locked(vsi);
2802 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2803 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2804 	return 0;
2805 }
2806 
2807 /**
2808  * i40e_ioctl - Access the hwtstamp interface
2809  * @netdev: network interface device structure
2810  * @ifr: interface request data
2811  * @cmd: ioctl command
2812  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2813 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2814 {
2815 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2816 	struct i40e_pf *pf = np->vsi->back;
2817 
2818 	switch (cmd) {
2819 	case SIOCGHWTSTAMP:
2820 		return i40e_ptp_get_ts_config(pf, ifr);
2821 	case SIOCSHWTSTAMP:
2822 		return i40e_ptp_set_ts_config(pf, ifr);
2823 	default:
2824 		return -EOPNOTSUPP;
2825 	}
2826 }
2827 
2828 /**
2829  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2830  * @vsi: the vsi being adjusted
2831  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2832 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2833 {
2834 	struct i40e_vsi_context ctxt;
2835 	i40e_status ret;
2836 
2837 	/* Don't modify stripping options if a port VLAN is active */
2838 	if (vsi->info.pvid)
2839 		return;
2840 
2841 	if ((vsi->info.valid_sections &
2842 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2843 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2844 		return;  /* already enabled */
2845 
2846 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2847 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2848 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2849 
2850 	ctxt.seid = vsi->seid;
2851 	ctxt.info = vsi->info;
2852 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2853 	if (ret) {
2854 		dev_info(&vsi->back->pdev->dev,
2855 			 "update vlan stripping failed, err %s aq_err %s\n",
2856 			 i40e_stat_str(&vsi->back->hw, ret),
2857 			 i40e_aq_str(&vsi->back->hw,
2858 				     vsi->back->hw.aq.asq_last_status));
2859 	}
2860 }
2861 
2862 /**
2863  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2864  * @vsi: the vsi being adjusted
2865  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)2866 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2867 {
2868 	struct i40e_vsi_context ctxt;
2869 	i40e_status ret;
2870 
2871 	/* Don't modify stripping options if a port VLAN is active */
2872 	if (vsi->info.pvid)
2873 		return;
2874 
2875 	if ((vsi->info.valid_sections &
2876 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2877 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2878 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2879 		return;  /* already disabled */
2880 
2881 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2882 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2883 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2884 
2885 	ctxt.seid = vsi->seid;
2886 	ctxt.info = vsi->info;
2887 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2888 	if (ret) {
2889 		dev_info(&vsi->back->pdev->dev,
2890 			 "update vlan stripping failed, err %s aq_err %s\n",
2891 			 i40e_stat_str(&vsi->back->hw, ret),
2892 			 i40e_aq_str(&vsi->back->hw,
2893 				     vsi->back->hw.aq.asq_last_status));
2894 	}
2895 }
2896 
2897 /**
2898  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2899  * @vsi: the vsi being configured
2900  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2901  *
2902  * This is a helper function for adding a new MAC/VLAN filter with the
2903  * specified VLAN for each existing MAC address already in the hash table.
2904  * This function does *not* perform any accounting to update filters based on
2905  * VLAN mode.
2906  *
2907  * NOTE: this function expects to be called while under the
2908  * mac_filter_hash_lock
2909  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)2910 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2911 {
2912 	struct i40e_mac_filter *f, *add_f;
2913 	struct hlist_node *h;
2914 	int bkt;
2915 
2916 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2917 		if (f->state == I40E_FILTER_REMOVE)
2918 			continue;
2919 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2920 		if (!add_f) {
2921 			dev_info(&vsi->back->pdev->dev,
2922 				 "Could not add vlan filter %d for %pM\n",
2923 				 vid, f->macaddr);
2924 			return -ENOMEM;
2925 		}
2926 	}
2927 
2928 	return 0;
2929 }
2930 
2931 /**
2932  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2933  * @vsi: the VSI being configured
2934  * @vid: VLAN id to be added
2935  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)2936 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2937 {
2938 	int err;
2939 
2940 	if (vsi->info.pvid)
2941 		return -EINVAL;
2942 
2943 	/* The network stack will attempt to add VID=0, with the intention to
2944 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2945 	 * these packets by default when configured to receive untagged
2946 	 * packets, so we don't need to add a filter for this case.
2947 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2948 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2949 	 * Thus, we do not want to actually add a filter for VID=0
2950 	 */
2951 	if (!vid)
2952 		return 0;
2953 
2954 	/* Locked once because all functions invoked below iterates list*/
2955 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2956 	err = i40e_add_vlan_all_mac(vsi, vid);
2957 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2958 	if (err)
2959 		return err;
2960 
2961 	/* schedule our worker thread which will take care of
2962 	 * applying the new filter changes
2963 	 */
2964 	i40e_service_event_schedule(vsi->back);
2965 	return 0;
2966 }
2967 
2968 /**
2969  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2970  * @vsi: the vsi being configured
2971  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2972  *
2973  * This function should be used to remove all VLAN filters which match the
2974  * given VID. It does not schedule the service event and does not take the
2975  * mac_filter_hash_lock so it may be combined with other operations under
2976  * a single invocation of the mac_filter_hash_lock.
2977  *
2978  * NOTE: this function expects to be called while under the
2979  * mac_filter_hash_lock
2980  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)2981 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2982 {
2983 	struct i40e_mac_filter *f;
2984 	struct hlist_node *h;
2985 	int bkt;
2986 
2987 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2988 		if (f->vlan == vid)
2989 			__i40e_del_filter(vsi, f);
2990 	}
2991 }
2992 
2993 /**
2994  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2995  * @vsi: the VSI being configured
2996  * @vid: VLAN id to be removed
2997  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)2998 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2999 {
3000 	if (!vid || vsi->info.pvid)
3001 		return;
3002 
3003 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3004 	i40e_rm_vlan_all_mac(vsi, vid);
3005 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3006 
3007 	/* schedule our worker thread which will take care of
3008 	 * applying the new filter changes
3009 	 */
3010 	i40e_service_event_schedule(vsi->back);
3011 }
3012 
3013 /**
3014  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3015  * @netdev: network interface to be adjusted
3016  * @proto: unused protocol value
3017  * @vid: vlan id to be added
3018  *
3019  * net_device_ops implementation for adding vlan ids
3020  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3021 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3022 				__always_unused __be16 proto, u16 vid)
3023 {
3024 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3025 	struct i40e_vsi *vsi = np->vsi;
3026 	int ret = 0;
3027 
3028 	if (vid >= VLAN_N_VID)
3029 		return -EINVAL;
3030 
3031 	ret = i40e_vsi_add_vlan(vsi, vid);
3032 	if (!ret)
3033 		set_bit(vid, vsi->active_vlans);
3034 
3035 	return ret;
3036 }
3037 
3038 /**
3039  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3040  * @netdev: network interface to be adjusted
3041  * @proto: unused protocol value
3042  * @vid: vlan id to be added
3043  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3044 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3045 				    __always_unused __be16 proto, u16 vid)
3046 {
3047 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3048 	struct i40e_vsi *vsi = np->vsi;
3049 
3050 	if (vid >= VLAN_N_VID)
3051 		return;
3052 	set_bit(vid, vsi->active_vlans);
3053 }
3054 
3055 /**
3056  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3057  * @netdev: network interface to be adjusted
3058  * @proto: unused protocol value
3059  * @vid: vlan id to be removed
3060  *
3061  * net_device_ops implementation for removing vlan ids
3062  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3063 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3064 				 __always_unused __be16 proto, u16 vid)
3065 {
3066 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3067 	struct i40e_vsi *vsi = np->vsi;
3068 
3069 	/* return code is ignored as there is nothing a user
3070 	 * can do about failure to remove and a log message was
3071 	 * already printed from the other function
3072 	 */
3073 	i40e_vsi_kill_vlan(vsi, vid);
3074 
3075 	clear_bit(vid, vsi->active_vlans);
3076 
3077 	return 0;
3078 }
3079 
3080 /**
3081  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3082  * @vsi: the vsi being brought back up
3083  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3084 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3085 {
3086 	u16 vid;
3087 
3088 	if (!vsi->netdev)
3089 		return;
3090 
3091 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3092 		i40e_vlan_stripping_enable(vsi);
3093 	else
3094 		i40e_vlan_stripping_disable(vsi);
3095 
3096 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3097 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3098 					vid);
3099 }
3100 
3101 /**
3102  * i40e_vsi_add_pvid - Add pvid for the VSI
3103  * @vsi: the vsi being adjusted
3104  * @vid: the vlan id to set as a PVID
3105  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3106 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3107 {
3108 	struct i40e_vsi_context ctxt;
3109 	i40e_status ret;
3110 
3111 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3112 	vsi->info.pvid = cpu_to_le16(vid);
3113 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3114 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3115 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3116 
3117 	ctxt.seid = vsi->seid;
3118 	ctxt.info = vsi->info;
3119 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3120 	if (ret) {
3121 		dev_info(&vsi->back->pdev->dev,
3122 			 "add pvid failed, err %s aq_err %s\n",
3123 			 i40e_stat_str(&vsi->back->hw, ret),
3124 			 i40e_aq_str(&vsi->back->hw,
3125 				     vsi->back->hw.aq.asq_last_status));
3126 		return -ENOENT;
3127 	}
3128 
3129 	return 0;
3130 }
3131 
3132 /**
3133  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3134  * @vsi: the vsi being adjusted
3135  *
3136  * Just use the vlan_rx_register() service to put it back to normal
3137  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3138 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3139 {
3140 	vsi->info.pvid = 0;
3141 
3142 	i40e_vlan_stripping_disable(vsi);
3143 }
3144 
3145 /**
3146  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3147  * @vsi: ptr to the VSI
3148  *
3149  * If this function returns with an error, then it's possible one or
3150  * more of the rings is populated (while the rest are not).  It is the
3151  * callers duty to clean those orphaned rings.
3152  *
3153  * Return 0 on success, negative on failure
3154  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3155 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3156 {
3157 	int i, err = 0;
3158 
3159 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3160 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3161 
3162 	if (!i40e_enabled_xdp_vsi(vsi))
3163 		return err;
3164 
3165 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3166 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3167 
3168 	return err;
3169 }
3170 
3171 /**
3172  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3173  * @vsi: ptr to the VSI
3174  *
3175  * Free VSI's transmit software resources
3176  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3177 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3178 {
3179 	int i;
3180 
3181 	if (vsi->tx_rings) {
3182 		for (i = 0; i < vsi->num_queue_pairs; i++)
3183 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3184 				i40e_free_tx_resources(vsi->tx_rings[i]);
3185 	}
3186 
3187 	if (vsi->xdp_rings) {
3188 		for (i = 0; i < vsi->num_queue_pairs; i++)
3189 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3190 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3191 	}
3192 }
3193 
3194 /**
3195  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3196  * @vsi: ptr to the VSI
3197  *
3198  * If this function returns with an error, then it's possible one or
3199  * more of the rings is populated (while the rest are not).  It is the
3200  * callers duty to clean those orphaned rings.
3201  *
3202  * Return 0 on success, negative on failure
3203  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3204 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3205 {
3206 	int i, err = 0;
3207 
3208 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3209 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3210 	return err;
3211 }
3212 
3213 /**
3214  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3215  * @vsi: ptr to the VSI
3216  *
3217  * Free all receive software resources
3218  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3219 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3220 {
3221 	int i;
3222 
3223 	if (!vsi->rx_rings)
3224 		return;
3225 
3226 	for (i = 0; i < vsi->num_queue_pairs; i++)
3227 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3228 			i40e_free_rx_resources(vsi->rx_rings[i]);
3229 }
3230 
3231 /**
3232  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3233  * @ring: The Tx ring to configure
3234  *
3235  * This enables/disables XPS for a given Tx descriptor ring
3236  * based on the TCs enabled for the VSI that ring belongs to.
3237  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3238 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3239 {
3240 	int cpu;
3241 
3242 	if (!ring->q_vector || !ring->netdev || ring->ch)
3243 		return;
3244 
3245 	/* We only initialize XPS once, so as not to overwrite user settings */
3246 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3247 		return;
3248 
3249 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3250 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3251 			    ring->queue_index);
3252 }
3253 
3254 /**
3255  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3256  * @ring: The Tx or Rx ring
3257  *
3258  * Returns the AF_XDP buffer pool or NULL.
3259  **/
i40e_xsk_pool(struct i40e_ring * ring)3260 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3261 {
3262 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3263 	int qid = ring->queue_index;
3264 
3265 	if (ring_is_xdp(ring))
3266 		qid -= ring->vsi->alloc_queue_pairs;
3267 
3268 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3269 		return NULL;
3270 
3271 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3272 }
3273 
3274 /**
3275  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3276  * @ring: The Tx ring to configure
3277  *
3278  * Configure the Tx descriptor ring in the HMC context.
3279  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3280 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3281 {
3282 	struct i40e_vsi *vsi = ring->vsi;
3283 	u16 pf_q = vsi->base_queue + ring->queue_index;
3284 	struct i40e_hw *hw = &vsi->back->hw;
3285 	struct i40e_hmc_obj_txq tx_ctx;
3286 	i40e_status err = 0;
3287 	u32 qtx_ctl = 0;
3288 
3289 	if (ring_is_xdp(ring))
3290 		ring->xsk_pool = i40e_xsk_pool(ring);
3291 
3292 	/* some ATR related tx ring init */
3293 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3294 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3295 		ring->atr_count = 0;
3296 	} else {
3297 		ring->atr_sample_rate = 0;
3298 	}
3299 
3300 	/* configure XPS */
3301 	i40e_config_xps_tx_ring(ring);
3302 
3303 	/* clear the context structure first */
3304 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3305 
3306 	tx_ctx.new_context = 1;
3307 	tx_ctx.base = (ring->dma / 128);
3308 	tx_ctx.qlen = ring->count;
3309 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3310 					       I40E_FLAG_FD_ATR_ENABLED));
3311 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3312 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3313 	if (vsi->type != I40E_VSI_FDIR)
3314 		tx_ctx.head_wb_ena = 1;
3315 	tx_ctx.head_wb_addr = ring->dma +
3316 			      (ring->count * sizeof(struct i40e_tx_desc));
3317 
3318 	/* As part of VSI creation/update, FW allocates certain
3319 	 * Tx arbitration queue sets for each TC enabled for
3320 	 * the VSI. The FW returns the handles to these queue
3321 	 * sets as part of the response buffer to Add VSI,
3322 	 * Update VSI, etc. AQ commands. It is expected that
3323 	 * these queue set handles be associated with the Tx
3324 	 * queues by the driver as part of the TX queue context
3325 	 * initialization. This has to be done regardless of
3326 	 * DCB as by default everything is mapped to TC0.
3327 	 */
3328 
3329 	if (ring->ch)
3330 		tx_ctx.rdylist =
3331 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3332 
3333 	else
3334 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3335 
3336 	tx_ctx.rdylist_act = 0;
3337 
3338 	/* clear the context in the HMC */
3339 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3340 	if (err) {
3341 		dev_info(&vsi->back->pdev->dev,
3342 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3343 			 ring->queue_index, pf_q, err);
3344 		return -ENOMEM;
3345 	}
3346 
3347 	/* set the context in the HMC */
3348 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3349 	if (err) {
3350 		dev_info(&vsi->back->pdev->dev,
3351 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3352 			 ring->queue_index, pf_q, err);
3353 		return -ENOMEM;
3354 	}
3355 
3356 	/* Now associate this queue with this PCI function */
3357 	if (ring->ch) {
3358 		if (ring->ch->type == I40E_VSI_VMDQ2)
3359 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3360 		else
3361 			return -EINVAL;
3362 
3363 		qtx_ctl |= (ring->ch->vsi_number <<
3364 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3365 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3366 	} else {
3367 		if (vsi->type == I40E_VSI_VMDQ2) {
3368 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3369 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3370 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3371 		} else {
3372 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3373 		}
3374 	}
3375 
3376 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3377 		    I40E_QTX_CTL_PF_INDX_MASK);
3378 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3379 	i40e_flush(hw);
3380 
3381 	/* cache tail off for easier writes later */
3382 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3383 
3384 	return 0;
3385 }
3386 
3387 /**
3388  * i40e_configure_rx_ring - Configure a receive ring context
3389  * @ring: The Rx ring to configure
3390  *
3391  * Configure the Rx descriptor ring in the HMC context.
3392  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3393 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3394 {
3395 	struct i40e_vsi *vsi = ring->vsi;
3396 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3397 	u16 pf_q = vsi->base_queue + ring->queue_index;
3398 	struct i40e_hw *hw = &vsi->back->hw;
3399 	struct i40e_hmc_obj_rxq rx_ctx;
3400 	i40e_status err = 0;
3401 	bool ok;
3402 	int ret;
3403 
3404 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3405 
3406 	/* clear the context structure first */
3407 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3408 
3409 	if (ring->vsi->type == I40E_VSI_MAIN)
3410 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3411 
3412 	ring->xsk_pool = i40e_xsk_pool(ring);
3413 	if (ring->xsk_pool) {
3414 		ring->rx_buf_len =
3415 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3416 		/* For AF_XDP ZC, we disallow packets to span on
3417 		 * multiple buffers, thus letting us skip that
3418 		 * handling in the fast-path.
3419 		 */
3420 		chain_len = 1;
3421 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3422 						 MEM_TYPE_XSK_BUFF_POOL,
3423 						 NULL);
3424 		if (ret)
3425 			return ret;
3426 		dev_info(&vsi->back->pdev->dev,
3427 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3428 			 ring->queue_index);
3429 
3430 	} else {
3431 		ring->rx_buf_len = vsi->rx_buf_len;
3432 		if (ring->vsi->type == I40E_VSI_MAIN) {
3433 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3434 							 MEM_TYPE_PAGE_SHARED,
3435 							 NULL);
3436 			if (ret)
3437 				return ret;
3438 		}
3439 	}
3440 
3441 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3442 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3443 
3444 	rx_ctx.base = (ring->dma / 128);
3445 	rx_ctx.qlen = ring->count;
3446 
3447 	/* use 16 byte descriptors */
3448 	rx_ctx.dsize = 0;
3449 
3450 	/* descriptor type is always zero
3451 	 * rx_ctx.dtype = 0;
3452 	 */
3453 	rx_ctx.hsplit_0 = 0;
3454 
3455 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3456 	if (hw->revision_id == 0)
3457 		rx_ctx.lrxqthresh = 0;
3458 	else
3459 		rx_ctx.lrxqthresh = 1;
3460 	rx_ctx.crcstrip = 1;
3461 	rx_ctx.l2tsel = 1;
3462 	/* this controls whether VLAN is stripped from inner headers */
3463 	rx_ctx.showiv = 0;
3464 	/* set the prefena field to 1 because the manual says to */
3465 	rx_ctx.prefena = 1;
3466 
3467 	/* clear the context in the HMC */
3468 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3469 	if (err) {
3470 		dev_info(&vsi->back->pdev->dev,
3471 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3472 			 ring->queue_index, pf_q, err);
3473 		return -ENOMEM;
3474 	}
3475 
3476 	/* set the context in the HMC */
3477 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3478 	if (err) {
3479 		dev_info(&vsi->back->pdev->dev,
3480 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3481 			 ring->queue_index, pf_q, err);
3482 		return -ENOMEM;
3483 	}
3484 
3485 	/* configure Rx buffer alignment */
3486 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3487 		clear_ring_build_skb_enabled(ring);
3488 	else
3489 		set_ring_build_skb_enabled(ring);
3490 
3491 	/* cache tail for quicker writes, and clear the reg before use */
3492 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3493 	writel(0, ring->tail);
3494 
3495 	if (ring->xsk_pool) {
3496 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3497 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3498 	} else {
3499 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3500 	}
3501 	if (!ok) {
3502 		/* Log this in case the user has forgotten to give the kernel
3503 		 * any buffers, even later in the application.
3504 		 */
3505 		dev_info(&vsi->back->pdev->dev,
3506 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3507 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3508 			 ring->queue_index, pf_q);
3509 	}
3510 
3511 	return 0;
3512 }
3513 
3514 /**
3515  * i40e_vsi_configure_tx - Configure the VSI for Tx
3516  * @vsi: VSI structure describing this set of rings and resources
3517  *
3518  * Configure the Tx VSI for operation.
3519  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3520 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3521 {
3522 	int err = 0;
3523 	u16 i;
3524 
3525 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3526 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3527 
3528 	if (err || !i40e_enabled_xdp_vsi(vsi))
3529 		return err;
3530 
3531 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3532 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3533 
3534 	return err;
3535 }
3536 
3537 /**
3538  * i40e_vsi_configure_rx - Configure the VSI for Rx
3539  * @vsi: the VSI being configured
3540  *
3541  * Configure the Rx VSI for operation.
3542  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3543 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3544 {
3545 	int err = 0;
3546 	u16 i;
3547 
3548 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3549 		vsi->max_frame = I40E_MAX_RXBUFFER;
3550 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3551 #if (PAGE_SIZE < 8192)
3552 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3553 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3554 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3555 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3556 #endif
3557 	} else {
3558 		vsi->max_frame = I40E_MAX_RXBUFFER;
3559 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3560 						       I40E_RXBUFFER_2048;
3561 	}
3562 
3563 	/* set up individual rings */
3564 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3565 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3566 
3567 	return err;
3568 }
3569 
3570 /**
3571  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3572  * @vsi: ptr to the VSI
3573  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3574 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3575 {
3576 	struct i40e_ring *tx_ring, *rx_ring;
3577 	u16 qoffset, qcount;
3578 	int i, n;
3579 
3580 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3581 		/* Reset the TC information */
3582 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3583 			rx_ring = vsi->rx_rings[i];
3584 			tx_ring = vsi->tx_rings[i];
3585 			rx_ring->dcb_tc = 0;
3586 			tx_ring->dcb_tc = 0;
3587 		}
3588 		return;
3589 	}
3590 
3591 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3592 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3593 			continue;
3594 
3595 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3596 		qcount = vsi->tc_config.tc_info[n].qcount;
3597 		for (i = qoffset; i < (qoffset + qcount); i++) {
3598 			rx_ring = vsi->rx_rings[i];
3599 			tx_ring = vsi->tx_rings[i];
3600 			rx_ring->dcb_tc = n;
3601 			tx_ring->dcb_tc = n;
3602 		}
3603 	}
3604 }
3605 
3606 /**
3607  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3608  * @vsi: ptr to the VSI
3609  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3610 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3611 {
3612 	if (vsi->netdev)
3613 		i40e_set_rx_mode(vsi->netdev);
3614 }
3615 
3616 /**
3617  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3618  * @vsi: Pointer to the targeted VSI
3619  *
3620  * This function replays the hlist on the hw where all the SB Flow Director
3621  * filters were saved.
3622  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3623 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3624 {
3625 	struct i40e_fdir_filter *filter;
3626 	struct i40e_pf *pf = vsi->back;
3627 	struct hlist_node *node;
3628 
3629 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3630 		return;
3631 
3632 	/* Reset FDir counters as we're replaying all existing filters */
3633 	pf->fd_tcp4_filter_cnt = 0;
3634 	pf->fd_udp4_filter_cnt = 0;
3635 	pf->fd_sctp4_filter_cnt = 0;
3636 	pf->fd_ip4_filter_cnt = 0;
3637 
3638 	hlist_for_each_entry_safe(filter, node,
3639 				  &pf->fdir_filter_list, fdir_node) {
3640 		i40e_add_del_fdir(vsi, filter, true);
3641 	}
3642 }
3643 
3644 /**
3645  * i40e_vsi_configure - Set up the VSI for action
3646  * @vsi: the VSI being configured
3647  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3648 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3649 {
3650 	int err;
3651 
3652 	i40e_set_vsi_rx_mode(vsi);
3653 	i40e_restore_vlan(vsi);
3654 	i40e_vsi_config_dcb_rings(vsi);
3655 	err = i40e_vsi_configure_tx(vsi);
3656 	if (!err)
3657 		err = i40e_vsi_configure_rx(vsi);
3658 
3659 	return err;
3660 }
3661 
3662 /**
3663  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3664  * @vsi: the VSI being configured
3665  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3666 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3667 {
3668 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3669 	struct i40e_pf *pf = vsi->back;
3670 	struct i40e_hw *hw = &pf->hw;
3671 	u16 vector;
3672 	int i, q;
3673 	u32 qp;
3674 
3675 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3676 	 * and PFINT_LNKLSTn registers, e.g.:
3677 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3678 	 */
3679 	qp = vsi->base_queue;
3680 	vector = vsi->base_vector;
3681 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3682 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3683 
3684 		q_vector->rx.next_update = jiffies + 1;
3685 		q_vector->rx.target_itr =
3686 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3687 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3688 		     q_vector->rx.target_itr >> 1);
3689 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3690 
3691 		q_vector->tx.next_update = jiffies + 1;
3692 		q_vector->tx.target_itr =
3693 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3694 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3695 		     q_vector->tx.target_itr >> 1);
3696 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3697 
3698 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3699 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3700 
3701 		/* Linked list for the queuepairs assigned to this vector */
3702 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3703 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3704 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3705 			u32 val;
3706 
3707 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3708 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3709 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3710 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3711 			      (I40E_QUEUE_TYPE_TX <<
3712 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3713 
3714 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3715 
3716 			if (has_xdp) {
3717 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3718 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3719 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3720 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3721 				      (I40E_QUEUE_TYPE_TX <<
3722 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3723 
3724 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3725 			}
3726 
3727 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3728 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3729 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3730 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3731 			      (I40E_QUEUE_TYPE_RX <<
3732 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3733 
3734 			/* Terminate the linked list */
3735 			if (q == (q_vector->num_ringpairs - 1))
3736 				val |= (I40E_QUEUE_END_OF_LIST <<
3737 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3738 
3739 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3740 			qp++;
3741 		}
3742 	}
3743 
3744 	i40e_flush(hw);
3745 }
3746 
3747 /**
3748  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3749  * @pf: pointer to private device data structure
3750  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3751 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3752 {
3753 	struct i40e_hw *hw = &pf->hw;
3754 	u32 val;
3755 
3756 	/* clear things first */
3757 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3758 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3759 
3760 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3761 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3762 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3763 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3764 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3765 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3766 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3767 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3768 
3769 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3770 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3771 
3772 	if (pf->flags & I40E_FLAG_PTP)
3773 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3774 
3775 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3776 
3777 	/* SW_ITR_IDX = 0, but don't change INTENA */
3778 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3779 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3780 
3781 	/* OTHER_ITR_IDX = 0 */
3782 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3783 }
3784 
3785 /**
3786  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3787  * @vsi: the VSI being configured
3788  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3789 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3790 {
3791 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3792 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3793 	struct i40e_pf *pf = vsi->back;
3794 	struct i40e_hw *hw = &pf->hw;
3795 	u32 val;
3796 
3797 	/* set the ITR configuration */
3798 	q_vector->rx.next_update = jiffies + 1;
3799 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3800 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3801 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3802 	q_vector->tx.next_update = jiffies + 1;
3803 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3804 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3805 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3806 
3807 	i40e_enable_misc_int_causes(pf);
3808 
3809 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3810 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3811 
3812 	/* Associate the queue pair to the vector and enable the queue int */
3813 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3814 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3815 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3816 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3817 
3818 	wr32(hw, I40E_QINT_RQCTL(0), val);
3819 
3820 	if (i40e_enabled_xdp_vsi(vsi)) {
3821 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3822 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3823 		      (I40E_QUEUE_TYPE_TX
3824 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3825 
3826 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3827 	}
3828 
3829 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3830 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3831 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3832 
3833 	wr32(hw, I40E_QINT_TQCTL(0), val);
3834 	i40e_flush(hw);
3835 }
3836 
3837 /**
3838  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3839  * @pf: board private structure
3840  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)3841 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3842 {
3843 	struct i40e_hw *hw = &pf->hw;
3844 
3845 	wr32(hw, I40E_PFINT_DYN_CTL0,
3846 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3847 	i40e_flush(hw);
3848 }
3849 
3850 /**
3851  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3852  * @pf: board private structure
3853  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)3854 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3855 {
3856 	struct i40e_hw *hw = &pf->hw;
3857 	u32 val;
3858 
3859 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3860 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3861 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3862 
3863 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3864 	i40e_flush(hw);
3865 }
3866 
3867 /**
3868  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3869  * @irq: interrupt number
3870  * @data: pointer to a q_vector
3871  **/
i40e_msix_clean_rings(int irq,void * data)3872 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3873 {
3874 	struct i40e_q_vector *q_vector = data;
3875 
3876 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3877 		return IRQ_HANDLED;
3878 
3879 	napi_schedule_irqoff(&q_vector->napi);
3880 
3881 	return IRQ_HANDLED;
3882 }
3883 
3884 /**
3885  * i40e_irq_affinity_notify - Callback for affinity changes
3886  * @notify: context as to what irq was changed
3887  * @mask: the new affinity mask
3888  *
3889  * This is a callback function used by the irq_set_affinity_notifier function
3890  * so that we may register to receive changes to the irq affinity masks.
3891  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)3892 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3893 				     const cpumask_t *mask)
3894 {
3895 	struct i40e_q_vector *q_vector =
3896 		container_of(notify, struct i40e_q_vector, affinity_notify);
3897 
3898 	cpumask_copy(&q_vector->affinity_mask, mask);
3899 }
3900 
3901 /**
3902  * i40e_irq_affinity_release - Callback for affinity notifier release
3903  * @ref: internal core kernel usage
3904  *
3905  * This is a callback function used by the irq_set_affinity_notifier function
3906  * to inform the current notification subscriber that they will no longer
3907  * receive notifications.
3908  **/
i40e_irq_affinity_release(struct kref * ref)3909 static void i40e_irq_affinity_release(struct kref *ref) {}
3910 
3911 /**
3912  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3913  * @vsi: the VSI being configured
3914  * @basename: name for the vector
3915  *
3916  * Allocates MSI-X vectors and requests interrupts from the kernel.
3917  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)3918 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3919 {
3920 	int q_vectors = vsi->num_q_vectors;
3921 	struct i40e_pf *pf = vsi->back;
3922 	int base = vsi->base_vector;
3923 	int rx_int_idx = 0;
3924 	int tx_int_idx = 0;
3925 	int vector, err;
3926 	int irq_num;
3927 	int cpu;
3928 
3929 	for (vector = 0; vector < q_vectors; vector++) {
3930 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3931 
3932 		irq_num = pf->msix_entries[base + vector].vector;
3933 
3934 		if (q_vector->tx.ring && q_vector->rx.ring) {
3935 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3936 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3937 			tx_int_idx++;
3938 		} else if (q_vector->rx.ring) {
3939 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3940 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3941 		} else if (q_vector->tx.ring) {
3942 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3943 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3944 		} else {
3945 			/* skip this unused q_vector */
3946 			continue;
3947 		}
3948 		err = request_irq(irq_num,
3949 				  vsi->irq_handler,
3950 				  0,
3951 				  q_vector->name,
3952 				  q_vector);
3953 		if (err) {
3954 			dev_info(&pf->pdev->dev,
3955 				 "MSIX request_irq failed, error: %d\n", err);
3956 			goto free_queue_irqs;
3957 		}
3958 
3959 		/* register for affinity change notifications */
3960 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3961 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3962 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3963 		/* Spread affinity hints out across online CPUs.
3964 		 *
3965 		 * get_cpu_mask returns a static constant mask with
3966 		 * a permanent lifetime so it's ok to pass to
3967 		 * irq_set_affinity_hint without making a copy.
3968 		 */
3969 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3970 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3971 	}
3972 
3973 	vsi->irqs_ready = true;
3974 	return 0;
3975 
3976 free_queue_irqs:
3977 	while (vector) {
3978 		vector--;
3979 		irq_num = pf->msix_entries[base + vector].vector;
3980 		irq_set_affinity_notifier(irq_num, NULL);
3981 		irq_set_affinity_hint(irq_num, NULL);
3982 		free_irq(irq_num, &vsi->q_vectors[vector]);
3983 	}
3984 	return err;
3985 }
3986 
3987 /**
3988  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3989  * @vsi: the VSI being un-configured
3990  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)3991 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3992 {
3993 	struct i40e_pf *pf = vsi->back;
3994 	struct i40e_hw *hw = &pf->hw;
3995 	int base = vsi->base_vector;
3996 	int i;
3997 
3998 	/* disable interrupt causation from each queue */
3999 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4000 		u32 val;
4001 
4002 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4003 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4004 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4005 
4006 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4007 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4008 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4009 
4010 		if (!i40e_enabled_xdp_vsi(vsi))
4011 			continue;
4012 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4013 	}
4014 
4015 	/* disable each interrupt */
4016 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4017 		for (i = vsi->base_vector;
4018 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4019 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4020 
4021 		i40e_flush(hw);
4022 		for (i = 0; i < vsi->num_q_vectors; i++)
4023 			synchronize_irq(pf->msix_entries[i + base].vector);
4024 	} else {
4025 		/* Legacy and MSI mode - this stops all interrupt handling */
4026 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4027 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4028 		i40e_flush(hw);
4029 		synchronize_irq(pf->pdev->irq);
4030 	}
4031 }
4032 
4033 /**
4034  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4035  * @vsi: the VSI being configured
4036  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4037 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4038 {
4039 	struct i40e_pf *pf = vsi->back;
4040 	int i;
4041 
4042 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4043 		for (i = 0; i < vsi->num_q_vectors; i++)
4044 			i40e_irq_dynamic_enable(vsi, i);
4045 	} else {
4046 		i40e_irq_dynamic_enable_icr0(pf);
4047 	}
4048 
4049 	i40e_flush(&pf->hw);
4050 	return 0;
4051 }
4052 
4053 /**
4054  * i40e_free_misc_vector - Free the vector that handles non-queue events
4055  * @pf: board private structure
4056  **/
i40e_free_misc_vector(struct i40e_pf * pf)4057 static void i40e_free_misc_vector(struct i40e_pf *pf)
4058 {
4059 	/* Disable ICR 0 */
4060 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4061 	i40e_flush(&pf->hw);
4062 
4063 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4064 		synchronize_irq(pf->msix_entries[0].vector);
4065 		free_irq(pf->msix_entries[0].vector, pf);
4066 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4067 	}
4068 }
4069 
4070 /**
4071  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4072  * @irq: interrupt number
4073  * @data: pointer to a q_vector
4074  *
4075  * This is the handler used for all MSI/Legacy interrupts, and deals
4076  * with both queue and non-queue interrupts.  This is also used in
4077  * MSIX mode to handle the non-queue interrupts.
4078  **/
i40e_intr(int irq,void * data)4079 static irqreturn_t i40e_intr(int irq, void *data)
4080 {
4081 	struct i40e_pf *pf = (struct i40e_pf *)data;
4082 	struct i40e_hw *hw = &pf->hw;
4083 	irqreturn_t ret = IRQ_NONE;
4084 	u32 icr0, icr0_remaining;
4085 	u32 val, ena_mask;
4086 
4087 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4088 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4089 
4090 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4091 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4092 		goto enable_intr;
4093 
4094 	/* if interrupt but no bits showing, must be SWINT */
4095 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4096 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4097 		pf->sw_int_count++;
4098 
4099 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4100 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4101 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4102 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4103 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4104 	}
4105 
4106 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4107 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4108 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4109 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4110 
4111 		/* We do not have a way to disarm Queue causes while leaving
4112 		 * interrupt enabled for all other causes, ideally
4113 		 * interrupt should be disabled while we are in NAPI but
4114 		 * this is not a performance path and napi_schedule()
4115 		 * can deal with rescheduling.
4116 		 */
4117 		if (!test_bit(__I40E_DOWN, pf->state))
4118 			napi_schedule_irqoff(&q_vector->napi);
4119 	}
4120 
4121 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4122 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4123 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4124 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4125 	}
4126 
4127 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4128 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4129 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4130 	}
4131 
4132 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4133 		/* disable any further VFLR event notifications */
4134 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4135 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4136 
4137 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4138 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4139 		} else {
4140 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4141 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4142 		}
4143 	}
4144 
4145 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4146 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4147 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4148 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4149 		val = rd32(hw, I40E_GLGEN_RSTAT);
4150 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4151 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4152 		if (val == I40E_RESET_CORER) {
4153 			pf->corer_count++;
4154 		} else if (val == I40E_RESET_GLOBR) {
4155 			pf->globr_count++;
4156 		} else if (val == I40E_RESET_EMPR) {
4157 			pf->empr_count++;
4158 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4159 		}
4160 	}
4161 
4162 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4163 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4164 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4165 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4166 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4167 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4168 	}
4169 
4170 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4171 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4172 
4173 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4174 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4175 			i40e_ptp_tx_hwtstamp(pf);
4176 		}
4177 	}
4178 
4179 	/* If a critical error is pending we have no choice but to reset the
4180 	 * device.
4181 	 * Report and mask out any remaining unexpected interrupts.
4182 	 */
4183 	icr0_remaining = icr0 & ena_mask;
4184 	if (icr0_remaining) {
4185 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4186 			 icr0_remaining);
4187 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4188 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4189 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4190 			dev_info(&pf->pdev->dev, "device will be reset\n");
4191 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4192 			i40e_service_event_schedule(pf);
4193 		}
4194 		ena_mask &= ~icr0_remaining;
4195 	}
4196 	ret = IRQ_HANDLED;
4197 
4198 enable_intr:
4199 	/* re-enable interrupt causes */
4200 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4201 	if (!test_bit(__I40E_DOWN, pf->state) ||
4202 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4203 		i40e_service_event_schedule(pf);
4204 		i40e_irq_dynamic_enable_icr0(pf);
4205 	}
4206 
4207 	return ret;
4208 }
4209 
4210 /**
4211  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4212  * @tx_ring:  tx ring to clean
4213  * @budget:   how many cleans we're allowed
4214  *
4215  * Returns true if there's any budget left (e.g. the clean is finished)
4216  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4217 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4218 {
4219 	struct i40e_vsi *vsi = tx_ring->vsi;
4220 	u16 i = tx_ring->next_to_clean;
4221 	struct i40e_tx_buffer *tx_buf;
4222 	struct i40e_tx_desc *tx_desc;
4223 
4224 	tx_buf = &tx_ring->tx_bi[i];
4225 	tx_desc = I40E_TX_DESC(tx_ring, i);
4226 	i -= tx_ring->count;
4227 
4228 	do {
4229 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4230 
4231 		/* if next_to_watch is not set then there is no work pending */
4232 		if (!eop_desc)
4233 			break;
4234 
4235 		/* prevent any other reads prior to eop_desc */
4236 		smp_rmb();
4237 
4238 		/* if the descriptor isn't done, no work yet to do */
4239 		if (!(eop_desc->cmd_type_offset_bsz &
4240 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4241 			break;
4242 
4243 		/* clear next_to_watch to prevent false hangs */
4244 		tx_buf->next_to_watch = NULL;
4245 
4246 		tx_desc->buffer_addr = 0;
4247 		tx_desc->cmd_type_offset_bsz = 0;
4248 		/* move past filter desc */
4249 		tx_buf++;
4250 		tx_desc++;
4251 		i++;
4252 		if (unlikely(!i)) {
4253 			i -= tx_ring->count;
4254 			tx_buf = tx_ring->tx_bi;
4255 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4256 		}
4257 		/* unmap skb header data */
4258 		dma_unmap_single(tx_ring->dev,
4259 				 dma_unmap_addr(tx_buf, dma),
4260 				 dma_unmap_len(tx_buf, len),
4261 				 DMA_TO_DEVICE);
4262 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4263 			kfree(tx_buf->raw_buf);
4264 
4265 		tx_buf->raw_buf = NULL;
4266 		tx_buf->tx_flags = 0;
4267 		tx_buf->next_to_watch = NULL;
4268 		dma_unmap_len_set(tx_buf, len, 0);
4269 		tx_desc->buffer_addr = 0;
4270 		tx_desc->cmd_type_offset_bsz = 0;
4271 
4272 		/* move us past the eop_desc for start of next FD desc */
4273 		tx_buf++;
4274 		tx_desc++;
4275 		i++;
4276 		if (unlikely(!i)) {
4277 			i -= tx_ring->count;
4278 			tx_buf = tx_ring->tx_bi;
4279 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4280 		}
4281 
4282 		/* update budget accounting */
4283 		budget--;
4284 	} while (likely(budget));
4285 
4286 	i += tx_ring->count;
4287 	tx_ring->next_to_clean = i;
4288 
4289 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4290 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4291 
4292 	return budget > 0;
4293 }
4294 
4295 /**
4296  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4297  * @irq: interrupt number
4298  * @data: pointer to a q_vector
4299  **/
i40e_fdir_clean_ring(int irq,void * data)4300 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4301 {
4302 	struct i40e_q_vector *q_vector = data;
4303 	struct i40e_vsi *vsi;
4304 
4305 	if (!q_vector->tx.ring)
4306 		return IRQ_HANDLED;
4307 
4308 	vsi = q_vector->tx.ring->vsi;
4309 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4310 
4311 	return IRQ_HANDLED;
4312 }
4313 
4314 /**
4315  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4316  * @vsi: the VSI being configured
4317  * @v_idx: vector index
4318  * @qp_idx: queue pair index
4319  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4320 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4321 {
4322 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4323 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4324 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4325 
4326 	tx_ring->q_vector = q_vector;
4327 	tx_ring->next = q_vector->tx.ring;
4328 	q_vector->tx.ring = tx_ring;
4329 	q_vector->tx.count++;
4330 
4331 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4332 	if (i40e_enabled_xdp_vsi(vsi)) {
4333 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4334 
4335 		xdp_ring->q_vector = q_vector;
4336 		xdp_ring->next = q_vector->tx.ring;
4337 		q_vector->tx.ring = xdp_ring;
4338 		q_vector->tx.count++;
4339 	}
4340 
4341 	rx_ring->q_vector = q_vector;
4342 	rx_ring->next = q_vector->rx.ring;
4343 	q_vector->rx.ring = rx_ring;
4344 	q_vector->rx.count++;
4345 }
4346 
4347 /**
4348  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4349  * @vsi: the VSI being configured
4350  *
4351  * This function maps descriptor rings to the queue-specific vectors
4352  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4353  * one vector per queue pair, but on a constrained vector budget, we
4354  * group the queue pairs as "efficiently" as possible.
4355  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4356 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4357 {
4358 	int qp_remaining = vsi->num_queue_pairs;
4359 	int q_vectors = vsi->num_q_vectors;
4360 	int num_ringpairs;
4361 	int v_start = 0;
4362 	int qp_idx = 0;
4363 
4364 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4365 	 * group them so there are multiple queues per vector.
4366 	 * It is also important to go through all the vectors available to be
4367 	 * sure that if we don't use all the vectors, that the remaining vectors
4368 	 * are cleared. This is especially important when decreasing the
4369 	 * number of queues in use.
4370 	 */
4371 	for (; v_start < q_vectors; v_start++) {
4372 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4373 
4374 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4375 
4376 		q_vector->num_ringpairs = num_ringpairs;
4377 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4378 
4379 		q_vector->rx.count = 0;
4380 		q_vector->tx.count = 0;
4381 		q_vector->rx.ring = NULL;
4382 		q_vector->tx.ring = NULL;
4383 
4384 		while (num_ringpairs--) {
4385 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4386 			qp_idx++;
4387 			qp_remaining--;
4388 		}
4389 	}
4390 }
4391 
4392 /**
4393  * i40e_vsi_request_irq - Request IRQ from the OS
4394  * @vsi: the VSI being configured
4395  * @basename: name for the vector
4396  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4397 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4398 {
4399 	struct i40e_pf *pf = vsi->back;
4400 	int err;
4401 
4402 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4403 		err = i40e_vsi_request_irq_msix(vsi, basename);
4404 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4405 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4406 				  pf->int_name, pf);
4407 	else
4408 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4409 				  pf->int_name, pf);
4410 
4411 	if (err)
4412 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4413 
4414 	return err;
4415 }
4416 
4417 #ifdef CONFIG_NET_POLL_CONTROLLER
4418 /**
4419  * i40e_netpoll - A Polling 'interrupt' handler
4420  * @netdev: network interface device structure
4421  *
4422  * This is used by netconsole to send skbs without having to re-enable
4423  * interrupts.  It's not called while the normal interrupt routine is executing.
4424  **/
i40e_netpoll(struct net_device * netdev)4425 static void i40e_netpoll(struct net_device *netdev)
4426 {
4427 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4428 	struct i40e_vsi *vsi = np->vsi;
4429 	struct i40e_pf *pf = vsi->back;
4430 	int i;
4431 
4432 	/* if interface is down do nothing */
4433 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4434 		return;
4435 
4436 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4437 		for (i = 0; i < vsi->num_q_vectors; i++)
4438 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4439 	} else {
4440 		i40e_intr(pf->pdev->irq, netdev);
4441 	}
4442 }
4443 #endif
4444 
4445 #define I40E_QTX_ENA_WAIT_COUNT 50
4446 
4447 /**
4448  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4449  * @pf: the PF being configured
4450  * @pf_q: the PF queue
4451  * @enable: enable or disable state of the queue
4452  *
4453  * This routine will wait for the given Tx queue of the PF to reach the
4454  * enabled or disabled state.
4455  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4456  * multiple retries; else will return 0 in case of success.
4457  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4458 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4459 {
4460 	int i;
4461 	u32 tx_reg;
4462 
4463 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4464 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4465 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4466 			break;
4467 
4468 		usleep_range(10, 20);
4469 	}
4470 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4471 		return -ETIMEDOUT;
4472 
4473 	return 0;
4474 }
4475 
4476 /**
4477  * i40e_control_tx_q - Start or stop a particular Tx queue
4478  * @pf: the PF structure
4479  * @pf_q: the PF queue to configure
4480  * @enable: start or stop the queue
4481  *
4482  * This function enables or disables a single queue. Note that any delay
4483  * required after the operation is expected to be handled by the caller of
4484  * this function.
4485  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4486 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4487 {
4488 	struct i40e_hw *hw = &pf->hw;
4489 	u32 tx_reg;
4490 	int i;
4491 
4492 	/* warn the TX unit of coming changes */
4493 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4494 	if (!enable)
4495 		usleep_range(10, 20);
4496 
4497 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4498 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4499 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4500 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4501 			break;
4502 		usleep_range(1000, 2000);
4503 	}
4504 
4505 	/* Skip if the queue is already in the requested state */
4506 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4507 		return;
4508 
4509 	/* turn on/off the queue */
4510 	if (enable) {
4511 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4512 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4513 	} else {
4514 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4515 	}
4516 
4517 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4518 }
4519 
4520 /**
4521  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4522  * @seid: VSI SEID
4523  * @pf: the PF structure
4524  * @pf_q: the PF queue to configure
4525  * @is_xdp: true if the queue is used for XDP
4526  * @enable: start or stop the queue
4527  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4528 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4529 			   bool is_xdp, bool enable)
4530 {
4531 	int ret;
4532 
4533 	i40e_control_tx_q(pf, pf_q, enable);
4534 
4535 	/* wait for the change to finish */
4536 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4537 	if (ret) {
4538 		dev_info(&pf->pdev->dev,
4539 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4540 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4541 			 (enable ? "en" : "dis"));
4542 	}
4543 
4544 	return ret;
4545 }
4546 
4547 /**
4548  * i40e_vsi_enable_tx - Start a VSI's rings
4549  * @vsi: the VSI being configured
4550  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4551 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4552 {
4553 	struct i40e_pf *pf = vsi->back;
4554 	int i, pf_q, ret = 0;
4555 
4556 	pf_q = vsi->base_queue;
4557 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4558 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4559 					     pf_q,
4560 					     false /*is xdp*/, true);
4561 		if (ret)
4562 			break;
4563 
4564 		if (!i40e_enabled_xdp_vsi(vsi))
4565 			continue;
4566 
4567 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4568 					     pf_q + vsi->alloc_queue_pairs,
4569 					     true /*is xdp*/, true);
4570 		if (ret)
4571 			break;
4572 	}
4573 	return ret;
4574 }
4575 
4576 /**
4577  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4578  * @pf: the PF being configured
4579  * @pf_q: the PF queue
4580  * @enable: enable or disable state of the queue
4581  *
4582  * This routine will wait for the given Rx queue of the PF to reach the
4583  * enabled or disabled state.
4584  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4585  * multiple retries; else will return 0 in case of success.
4586  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4587 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4588 {
4589 	int i;
4590 	u32 rx_reg;
4591 
4592 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4593 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4594 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4595 			break;
4596 
4597 		usleep_range(10, 20);
4598 	}
4599 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4600 		return -ETIMEDOUT;
4601 
4602 	return 0;
4603 }
4604 
4605 /**
4606  * i40e_control_rx_q - Start or stop a particular Rx queue
4607  * @pf: the PF structure
4608  * @pf_q: the PF queue to configure
4609  * @enable: start or stop the queue
4610  *
4611  * This function enables or disables a single queue. Note that
4612  * any delay required after the operation is expected to be
4613  * handled by the caller of this function.
4614  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4615 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4616 {
4617 	struct i40e_hw *hw = &pf->hw;
4618 	u32 rx_reg;
4619 	int i;
4620 
4621 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4622 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4623 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4624 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4625 			break;
4626 		usleep_range(1000, 2000);
4627 	}
4628 
4629 	/* Skip if the queue is already in the requested state */
4630 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4631 		return;
4632 
4633 	/* turn on/off the queue */
4634 	if (enable)
4635 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4636 	else
4637 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4638 
4639 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4640 }
4641 
4642 /**
4643  * i40e_control_wait_rx_q
4644  * @pf: the PF structure
4645  * @pf_q: queue being configured
4646  * @enable: start or stop the rings
4647  *
4648  * This function enables or disables a single queue along with waiting
4649  * for the change to finish. The caller of this function should handle
4650  * the delays needed in the case of disabling queues.
4651  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4652 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4653 {
4654 	int ret = 0;
4655 
4656 	i40e_control_rx_q(pf, pf_q, enable);
4657 
4658 	/* wait for the change to finish */
4659 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4660 	if (ret)
4661 		return ret;
4662 
4663 	return ret;
4664 }
4665 
4666 /**
4667  * i40e_vsi_enable_rx - Start a VSI's rings
4668  * @vsi: the VSI being configured
4669  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4670 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4671 {
4672 	struct i40e_pf *pf = vsi->back;
4673 	int i, pf_q, ret = 0;
4674 
4675 	pf_q = vsi->base_queue;
4676 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4677 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4678 		if (ret) {
4679 			dev_info(&pf->pdev->dev,
4680 				 "VSI seid %d Rx ring %d enable timeout\n",
4681 				 vsi->seid, pf_q);
4682 			break;
4683 		}
4684 	}
4685 
4686 	return ret;
4687 }
4688 
4689 /**
4690  * i40e_vsi_start_rings - Start a VSI's rings
4691  * @vsi: the VSI being configured
4692  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4693 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4694 {
4695 	int ret = 0;
4696 
4697 	/* do rx first for enable and last for disable */
4698 	ret = i40e_vsi_enable_rx(vsi);
4699 	if (ret)
4700 		return ret;
4701 	ret = i40e_vsi_enable_tx(vsi);
4702 
4703 	return ret;
4704 }
4705 
4706 #define I40E_DISABLE_TX_GAP_MSEC	50
4707 
4708 /**
4709  * i40e_vsi_stop_rings - Stop a VSI's rings
4710  * @vsi: the VSI being configured
4711  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4712 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4713 {
4714 	struct i40e_pf *pf = vsi->back;
4715 	int pf_q, err, q_end;
4716 
4717 	/* When port TX is suspended, don't wait */
4718 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4719 		return i40e_vsi_stop_rings_no_wait(vsi);
4720 
4721 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4722 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4723 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4724 
4725 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4726 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4727 		if (err)
4728 			dev_info(&pf->pdev->dev,
4729 				 "VSI seid %d Rx ring %d dissable timeout\n",
4730 				 vsi->seid, pf_q);
4731 	}
4732 
4733 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4734 	pf_q = vsi->base_queue;
4735 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4736 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4737 
4738 	i40e_vsi_wait_queues_disabled(vsi);
4739 }
4740 
4741 /**
4742  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4743  * @vsi: the VSI being shutdown
4744  *
4745  * This function stops all the rings for a VSI but does not delay to verify
4746  * that rings have been disabled. It is expected that the caller is shutting
4747  * down multiple VSIs at once and will delay together for all the VSIs after
4748  * initiating the shutdown. This is particularly useful for shutting down lots
4749  * of VFs together. Otherwise, a large delay can be incurred while configuring
4750  * each VSI in serial.
4751  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4752 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4753 {
4754 	struct i40e_pf *pf = vsi->back;
4755 	int i, pf_q;
4756 
4757 	pf_q = vsi->base_queue;
4758 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4759 		i40e_control_tx_q(pf, pf_q, false);
4760 		i40e_control_rx_q(pf, pf_q, false);
4761 	}
4762 }
4763 
4764 /**
4765  * i40e_vsi_free_irq - Free the irq association with the OS
4766  * @vsi: the VSI being configured
4767  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4768 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4769 {
4770 	struct i40e_pf *pf = vsi->back;
4771 	struct i40e_hw *hw = &pf->hw;
4772 	int base = vsi->base_vector;
4773 	u32 val, qp;
4774 	int i;
4775 
4776 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4777 		if (!vsi->q_vectors)
4778 			return;
4779 
4780 		if (!vsi->irqs_ready)
4781 			return;
4782 
4783 		vsi->irqs_ready = false;
4784 		for (i = 0; i < vsi->num_q_vectors; i++) {
4785 			int irq_num;
4786 			u16 vector;
4787 
4788 			vector = i + base;
4789 			irq_num = pf->msix_entries[vector].vector;
4790 
4791 			/* free only the irqs that were actually requested */
4792 			if (!vsi->q_vectors[i] ||
4793 			    !vsi->q_vectors[i]->num_ringpairs)
4794 				continue;
4795 
4796 			/* clear the affinity notifier in the IRQ descriptor */
4797 			irq_set_affinity_notifier(irq_num, NULL);
4798 			/* remove our suggested affinity mask for this IRQ */
4799 			irq_set_affinity_hint(irq_num, NULL);
4800 			synchronize_irq(irq_num);
4801 			free_irq(irq_num, vsi->q_vectors[i]);
4802 
4803 			/* Tear down the interrupt queue link list
4804 			 *
4805 			 * We know that they come in pairs and always
4806 			 * the Rx first, then the Tx.  To clear the
4807 			 * link list, stick the EOL value into the
4808 			 * next_q field of the registers.
4809 			 */
4810 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4811 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4812 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4813 			val |= I40E_QUEUE_END_OF_LIST
4814 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4815 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4816 
4817 			while (qp != I40E_QUEUE_END_OF_LIST) {
4818 				u32 next;
4819 
4820 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4821 
4822 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4823 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4824 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4825 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4826 
4827 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4828 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4829 
4830 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4831 
4832 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4833 
4834 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4835 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4836 
4837 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4838 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4839 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4840 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4841 
4842 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4843 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4844 
4845 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4846 				qp = next;
4847 			}
4848 		}
4849 	} else {
4850 		free_irq(pf->pdev->irq, pf);
4851 
4852 		val = rd32(hw, I40E_PFINT_LNKLST0);
4853 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4854 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4855 		val |= I40E_QUEUE_END_OF_LIST
4856 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4857 		wr32(hw, I40E_PFINT_LNKLST0, val);
4858 
4859 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4860 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4861 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4862 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4863 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4864 
4865 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4866 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4867 
4868 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4869 
4870 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4871 
4872 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4873 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4874 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4875 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4876 
4877 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4878 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4879 
4880 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4881 	}
4882 }
4883 
4884 /**
4885  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4886  * @vsi: the VSI being configured
4887  * @v_idx: Index of vector to be freed
4888  *
4889  * This function frees the memory allocated to the q_vector.  In addition if
4890  * NAPI is enabled it will delete any references to the NAPI struct prior
4891  * to freeing the q_vector.
4892  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)4893 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4894 {
4895 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4896 	struct i40e_ring *ring;
4897 
4898 	if (!q_vector)
4899 		return;
4900 
4901 	/* disassociate q_vector from rings */
4902 	i40e_for_each_ring(ring, q_vector->tx)
4903 		ring->q_vector = NULL;
4904 
4905 	i40e_for_each_ring(ring, q_vector->rx)
4906 		ring->q_vector = NULL;
4907 
4908 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4909 	if (vsi->netdev)
4910 		netif_napi_del(&q_vector->napi);
4911 
4912 	vsi->q_vectors[v_idx] = NULL;
4913 
4914 	kfree_rcu(q_vector, rcu);
4915 }
4916 
4917 /**
4918  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4919  * @vsi: the VSI being un-configured
4920  *
4921  * This frees the memory allocated to the q_vectors and
4922  * deletes references to the NAPI struct.
4923  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)4924 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4925 {
4926 	int v_idx;
4927 
4928 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4929 		i40e_free_q_vector(vsi, v_idx);
4930 }
4931 
4932 /**
4933  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4934  * @pf: board private structure
4935  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)4936 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4937 {
4938 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4939 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4940 		pci_disable_msix(pf->pdev);
4941 		kfree(pf->msix_entries);
4942 		pf->msix_entries = NULL;
4943 		kfree(pf->irq_pile);
4944 		pf->irq_pile = NULL;
4945 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4946 		pci_disable_msi(pf->pdev);
4947 	}
4948 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4949 }
4950 
4951 /**
4952  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4953  * @pf: board private structure
4954  *
4955  * We go through and clear interrupt specific resources and reset the structure
4956  * to pre-load conditions
4957  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)4958 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4959 {
4960 	int i;
4961 
4962 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4963 		i40e_free_misc_vector(pf);
4964 
4965 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4966 		      I40E_IWARP_IRQ_PILE_ID);
4967 
4968 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4969 	for (i = 0; i < pf->num_alloc_vsi; i++)
4970 		if (pf->vsi[i])
4971 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4972 	i40e_reset_interrupt_capability(pf);
4973 }
4974 
4975 /**
4976  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4977  * @vsi: the VSI being configured
4978  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)4979 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4980 {
4981 	int q_idx;
4982 
4983 	if (!vsi->netdev)
4984 		return;
4985 
4986 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4987 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4988 
4989 		if (q_vector->rx.ring || q_vector->tx.ring)
4990 			napi_enable(&q_vector->napi);
4991 	}
4992 }
4993 
4994 /**
4995  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4996  * @vsi: the VSI being configured
4997  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)4998 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4999 {
5000 	int q_idx;
5001 
5002 	if (!vsi->netdev)
5003 		return;
5004 
5005 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5006 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5007 
5008 		if (q_vector->rx.ring || q_vector->tx.ring)
5009 			napi_disable(&q_vector->napi);
5010 	}
5011 }
5012 
5013 /**
5014  * i40e_vsi_close - Shut down a VSI
5015  * @vsi: the vsi to be quelled
5016  **/
i40e_vsi_close(struct i40e_vsi * vsi)5017 static void i40e_vsi_close(struct i40e_vsi *vsi)
5018 {
5019 	struct i40e_pf *pf = vsi->back;
5020 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5021 		i40e_down(vsi);
5022 	i40e_vsi_free_irq(vsi);
5023 	i40e_vsi_free_tx_resources(vsi);
5024 	i40e_vsi_free_rx_resources(vsi);
5025 	vsi->current_netdev_flags = 0;
5026 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5027 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5028 		set_bit(__I40E_CLIENT_RESET, pf->state);
5029 }
5030 
5031 /**
5032  * i40e_quiesce_vsi - Pause a given VSI
5033  * @vsi: the VSI being paused
5034  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5035 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5036 {
5037 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5038 		return;
5039 
5040 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5041 	if (vsi->netdev && netif_running(vsi->netdev))
5042 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5043 	else
5044 		i40e_vsi_close(vsi);
5045 }
5046 
5047 /**
5048  * i40e_unquiesce_vsi - Resume a given VSI
5049  * @vsi: the VSI being resumed
5050  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5051 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5052 {
5053 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5054 		return;
5055 
5056 	if (vsi->netdev && netif_running(vsi->netdev))
5057 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5058 	else
5059 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5060 }
5061 
5062 /**
5063  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5064  * @pf: the PF
5065  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5066 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5067 {
5068 	int v;
5069 
5070 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5071 		if (pf->vsi[v])
5072 			i40e_quiesce_vsi(pf->vsi[v]);
5073 	}
5074 }
5075 
5076 /**
5077  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5078  * @pf: the PF
5079  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5080 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5081 {
5082 	int v;
5083 
5084 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5085 		if (pf->vsi[v])
5086 			i40e_unquiesce_vsi(pf->vsi[v]);
5087 	}
5088 }
5089 
5090 /**
5091  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5092  * @vsi: the VSI being configured
5093  *
5094  * Wait until all queues on a given VSI have been disabled.
5095  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5096 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5097 {
5098 	struct i40e_pf *pf = vsi->back;
5099 	int i, pf_q, ret;
5100 
5101 	pf_q = vsi->base_queue;
5102 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5103 		/* Check and wait for the Tx queue */
5104 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5105 		if (ret) {
5106 			dev_info(&pf->pdev->dev,
5107 				 "VSI seid %d Tx ring %d disable timeout\n",
5108 				 vsi->seid, pf_q);
5109 			return ret;
5110 		}
5111 
5112 		if (!i40e_enabled_xdp_vsi(vsi))
5113 			goto wait_rx;
5114 
5115 		/* Check and wait for the XDP Tx queue */
5116 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5117 				       false);
5118 		if (ret) {
5119 			dev_info(&pf->pdev->dev,
5120 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5121 				 vsi->seid, pf_q);
5122 			return ret;
5123 		}
5124 wait_rx:
5125 		/* Check and wait for the Rx queue */
5126 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5127 		if (ret) {
5128 			dev_info(&pf->pdev->dev,
5129 				 "VSI seid %d Rx ring %d disable timeout\n",
5130 				 vsi->seid, pf_q);
5131 			return ret;
5132 		}
5133 	}
5134 
5135 	return 0;
5136 }
5137 
5138 #ifdef CONFIG_I40E_DCB
5139 /**
5140  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5141  * @pf: the PF
5142  *
5143  * This function waits for the queues to be in disabled state for all the
5144  * VSIs that are managed by this PF.
5145  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5146 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5147 {
5148 	int v, ret = 0;
5149 
5150 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5151 		if (pf->vsi[v]) {
5152 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5153 			if (ret)
5154 				break;
5155 		}
5156 	}
5157 
5158 	return ret;
5159 }
5160 
5161 #endif
5162 
5163 /**
5164  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5165  * @pf: pointer to PF
5166  *
5167  * Get TC map for ISCSI PF type that will include iSCSI TC
5168  * and LAN TC.
5169  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5170 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5171 {
5172 	struct i40e_dcb_app_priority_table app;
5173 	struct i40e_hw *hw = &pf->hw;
5174 	u8 enabled_tc = 1; /* TC0 is always enabled */
5175 	u8 tc, i;
5176 	/* Get the iSCSI APP TLV */
5177 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5178 
5179 	for (i = 0; i < dcbcfg->numapps; i++) {
5180 		app = dcbcfg->app[i];
5181 		if (app.selector == I40E_APP_SEL_TCPIP &&
5182 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5183 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5184 			enabled_tc |= BIT(tc);
5185 			break;
5186 		}
5187 	}
5188 
5189 	return enabled_tc;
5190 }
5191 
5192 /**
5193  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5194  * @dcbcfg: the corresponding DCBx configuration structure
5195  *
5196  * Return the number of TCs from given DCBx configuration
5197  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5198 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5199 {
5200 	int i, tc_unused = 0;
5201 	u8 num_tc = 0;
5202 	u8 ret = 0;
5203 
5204 	/* Scan the ETS Config Priority Table to find
5205 	 * traffic class enabled for a given priority
5206 	 * and create a bitmask of enabled TCs
5207 	 */
5208 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5209 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5210 
5211 	/* Now scan the bitmask to check for
5212 	 * contiguous TCs starting with TC0
5213 	 */
5214 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5215 		if (num_tc & BIT(i)) {
5216 			if (!tc_unused) {
5217 				ret++;
5218 			} else {
5219 				pr_err("Non-contiguous TC - Disabling DCB\n");
5220 				return 1;
5221 			}
5222 		} else {
5223 			tc_unused = 1;
5224 		}
5225 	}
5226 
5227 	/* There is always at least TC0 */
5228 	if (!ret)
5229 		ret = 1;
5230 
5231 	return ret;
5232 }
5233 
5234 /**
5235  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5236  * @dcbcfg: the corresponding DCBx configuration structure
5237  *
5238  * Query the current DCB configuration and return the number of
5239  * traffic classes enabled from the given DCBX config
5240  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5241 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5242 {
5243 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5244 	u8 enabled_tc = 1;
5245 	u8 i;
5246 
5247 	for (i = 0; i < num_tc; i++)
5248 		enabled_tc |= BIT(i);
5249 
5250 	return enabled_tc;
5251 }
5252 
5253 /**
5254  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5255  * @pf: PF being queried
5256  *
5257  * Query the current MQPRIO configuration and return the number of
5258  * traffic classes enabled.
5259  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5260 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5261 {
5262 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5263 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5264 	u8 enabled_tc = 1, i;
5265 
5266 	for (i = 1; i < num_tc; i++)
5267 		enabled_tc |= BIT(i);
5268 	return enabled_tc;
5269 }
5270 
5271 /**
5272  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5273  * @pf: PF being queried
5274  *
5275  * Return number of traffic classes enabled for the given PF
5276  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5277 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5278 {
5279 	struct i40e_hw *hw = &pf->hw;
5280 	u8 i, enabled_tc = 1;
5281 	u8 num_tc = 0;
5282 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5283 
5284 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5285 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5286 
5287 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5288 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5289 		return 1;
5290 
5291 	/* SFP mode will be enabled for all TCs on port */
5292 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5293 		return i40e_dcb_get_num_tc(dcbcfg);
5294 
5295 	/* MFP mode return count of enabled TCs for this PF */
5296 	if (pf->hw.func_caps.iscsi)
5297 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5298 	else
5299 		return 1; /* Only TC0 */
5300 
5301 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5302 		if (enabled_tc & BIT(i))
5303 			num_tc++;
5304 	}
5305 	return num_tc;
5306 }
5307 
5308 /**
5309  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5310  * @pf: PF being queried
5311  *
5312  * Return a bitmap for enabled traffic classes for this PF.
5313  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5314 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5315 {
5316 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5317 		return i40e_mqprio_get_enabled_tc(pf);
5318 
5319 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5320 	 * default TC
5321 	 */
5322 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5323 		return I40E_DEFAULT_TRAFFIC_CLASS;
5324 
5325 	/* SFP mode we want PF to be enabled for all TCs */
5326 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5327 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5328 
5329 	/* MFP enabled and iSCSI PF type */
5330 	if (pf->hw.func_caps.iscsi)
5331 		return i40e_get_iscsi_tc_map(pf);
5332 	else
5333 		return I40E_DEFAULT_TRAFFIC_CLASS;
5334 }
5335 
5336 /**
5337  * i40e_vsi_get_bw_info - Query VSI BW Information
5338  * @vsi: the VSI being queried
5339  *
5340  * Returns 0 on success, negative value on failure
5341  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5342 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5343 {
5344 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5345 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5346 	struct i40e_pf *pf = vsi->back;
5347 	struct i40e_hw *hw = &pf->hw;
5348 	i40e_status ret;
5349 	u32 tc_bw_max;
5350 	int i;
5351 
5352 	/* Get the VSI level BW configuration */
5353 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5354 	if (ret) {
5355 		dev_info(&pf->pdev->dev,
5356 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5357 			 i40e_stat_str(&pf->hw, ret),
5358 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5359 		return -EINVAL;
5360 	}
5361 
5362 	/* Get the VSI level BW configuration per TC */
5363 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5364 					       NULL);
5365 	if (ret) {
5366 		dev_info(&pf->pdev->dev,
5367 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5368 			 i40e_stat_str(&pf->hw, ret),
5369 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5370 		return -EINVAL;
5371 	}
5372 
5373 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5374 		dev_info(&pf->pdev->dev,
5375 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5376 			 bw_config.tc_valid_bits,
5377 			 bw_ets_config.tc_valid_bits);
5378 		/* Still continuing */
5379 	}
5380 
5381 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5382 	vsi->bw_max_quanta = bw_config.max_bw;
5383 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5384 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5385 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5386 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5387 		vsi->bw_ets_limit_credits[i] =
5388 					le16_to_cpu(bw_ets_config.credits[i]);
5389 		/* 3 bits out of 4 for each TC */
5390 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5391 	}
5392 
5393 	return 0;
5394 }
5395 
5396 /**
5397  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5398  * @vsi: the VSI being configured
5399  * @enabled_tc: TC bitmap
5400  * @bw_share: BW shared credits per TC
5401  *
5402  * Returns 0 on success, negative value on failure
5403  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5404 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5405 				       u8 *bw_share)
5406 {
5407 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5408 	struct i40e_pf *pf = vsi->back;
5409 	i40e_status ret;
5410 	int i;
5411 
5412 	/* There is no need to reset BW when mqprio mode is on.  */
5413 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5414 		return 0;
5415 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5416 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5417 		if (ret)
5418 			dev_info(&pf->pdev->dev,
5419 				 "Failed to reset tx rate for vsi->seid %u\n",
5420 				 vsi->seid);
5421 		return ret;
5422 	}
5423 	bw_data.tc_valid_bits = enabled_tc;
5424 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5425 		bw_data.tc_bw_credits[i] = bw_share[i];
5426 
5427 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5428 	if (ret) {
5429 		dev_info(&pf->pdev->dev,
5430 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5431 			 pf->hw.aq.asq_last_status);
5432 		return -EINVAL;
5433 	}
5434 
5435 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5436 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5437 
5438 	return 0;
5439 }
5440 
5441 /**
5442  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5443  * @vsi: the VSI being configured
5444  * @enabled_tc: TC map to be enabled
5445  *
5446  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5447 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5448 {
5449 	struct net_device *netdev = vsi->netdev;
5450 	struct i40e_pf *pf = vsi->back;
5451 	struct i40e_hw *hw = &pf->hw;
5452 	u8 netdev_tc = 0;
5453 	int i;
5454 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5455 
5456 	if (!netdev)
5457 		return;
5458 
5459 	if (!enabled_tc) {
5460 		netdev_reset_tc(netdev);
5461 		return;
5462 	}
5463 
5464 	/* Set up actual enabled TCs on the VSI */
5465 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5466 		return;
5467 
5468 	/* set per TC queues for the VSI */
5469 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5470 		/* Only set TC queues for enabled tcs
5471 		 *
5472 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5473 		 * enabled_tc bitmap would be 0x00001001; the driver
5474 		 * will set the numtc for netdev as 2 that will be
5475 		 * referenced by the netdev layer as TC 0 and 1.
5476 		 */
5477 		if (vsi->tc_config.enabled_tc & BIT(i))
5478 			netdev_set_tc_queue(netdev,
5479 					vsi->tc_config.tc_info[i].netdev_tc,
5480 					vsi->tc_config.tc_info[i].qcount,
5481 					vsi->tc_config.tc_info[i].qoffset);
5482 	}
5483 
5484 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5485 		return;
5486 
5487 	/* Assign UP2TC map for the VSI */
5488 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5489 		/* Get the actual TC# for the UP */
5490 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5491 		/* Get the mapped netdev TC# for the UP */
5492 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5493 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5494 	}
5495 }
5496 
5497 /**
5498  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5499  * @vsi: the VSI being configured
5500  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5501  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5502 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5503 				      struct i40e_vsi_context *ctxt)
5504 {
5505 	/* copy just the sections touched not the entire info
5506 	 * since not all sections are valid as returned by
5507 	 * update vsi params
5508 	 */
5509 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5510 	memcpy(&vsi->info.queue_mapping,
5511 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5512 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5513 	       sizeof(vsi->info.tc_mapping));
5514 }
5515 
5516 /**
5517  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5518  * @vsi: the VSI being reconfigured
5519  * @vsi_offset: offset from main VF VSI
5520  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5521 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5522 {
5523 	struct i40e_vsi_context ctxt = {};
5524 	struct i40e_pf *pf;
5525 	struct i40e_hw *hw;
5526 	int ret;
5527 
5528 	if (!vsi)
5529 		return I40E_ERR_PARAM;
5530 	pf = vsi->back;
5531 	hw = &pf->hw;
5532 
5533 	ctxt.seid = vsi->seid;
5534 	ctxt.pf_num = hw->pf_id;
5535 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5536 	ctxt.uplink_seid = vsi->uplink_seid;
5537 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5538 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5539 	ctxt.info = vsi->info;
5540 
5541 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5542 				 false);
5543 	if (vsi->reconfig_rss) {
5544 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5545 				      vsi->num_queue_pairs);
5546 		ret = i40e_vsi_config_rss(vsi);
5547 		if (ret) {
5548 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5549 			return ret;
5550 		}
5551 		vsi->reconfig_rss = false;
5552 	}
5553 
5554 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5555 	if (ret) {
5556 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5557 			 i40e_stat_str(hw, ret),
5558 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5559 		return ret;
5560 	}
5561 	/* update the local VSI info with updated queue map */
5562 	i40e_vsi_update_queue_map(vsi, &ctxt);
5563 	vsi->info.valid_sections = 0;
5564 
5565 	return ret;
5566 }
5567 
5568 /**
5569  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5570  * @vsi: VSI to be configured
5571  * @enabled_tc: TC bitmap
5572  *
5573  * This configures a particular VSI for TCs that are mapped to the
5574  * given TC bitmap. It uses default bandwidth share for TCs across
5575  * VSIs to configure TC for a particular VSI.
5576  *
5577  * NOTE:
5578  * It is expected that the VSI queues have been quisced before calling
5579  * this function.
5580  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5581 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5582 {
5583 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5584 	struct i40e_pf *pf = vsi->back;
5585 	struct i40e_hw *hw = &pf->hw;
5586 	struct i40e_vsi_context ctxt;
5587 	int ret = 0;
5588 	int i;
5589 
5590 	/* Check if enabled_tc is same as existing or new TCs */
5591 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5592 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5593 		return ret;
5594 
5595 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5596 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5597 		if (enabled_tc & BIT(i))
5598 			bw_share[i] = 1;
5599 	}
5600 
5601 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5602 	if (ret) {
5603 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5604 
5605 		dev_info(&pf->pdev->dev,
5606 			 "Failed configuring TC map %d for VSI %d\n",
5607 			 enabled_tc, vsi->seid);
5608 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5609 						  &bw_config, NULL);
5610 		if (ret) {
5611 			dev_info(&pf->pdev->dev,
5612 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5613 				 i40e_stat_str(hw, ret),
5614 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5615 			goto out;
5616 		}
5617 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5618 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5619 
5620 			if (!valid_tc)
5621 				valid_tc = bw_config.tc_valid_bits;
5622 			/* Always enable TC0, no matter what */
5623 			valid_tc |= 1;
5624 			dev_info(&pf->pdev->dev,
5625 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5626 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5627 			enabled_tc = valid_tc;
5628 		}
5629 
5630 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5631 		if (ret) {
5632 			dev_err(&pf->pdev->dev,
5633 				"Unable to  configure TC map %d for VSI %d\n",
5634 				enabled_tc, vsi->seid);
5635 			goto out;
5636 		}
5637 	}
5638 
5639 	/* Update Queue Pairs Mapping for currently enabled UPs */
5640 	ctxt.seid = vsi->seid;
5641 	ctxt.pf_num = vsi->back->hw.pf_id;
5642 	ctxt.vf_num = 0;
5643 	ctxt.uplink_seid = vsi->uplink_seid;
5644 	ctxt.info = vsi->info;
5645 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5646 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5647 		if (ret)
5648 			goto out;
5649 	} else {
5650 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5651 	}
5652 
5653 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5654 	 * queues changed.
5655 	 */
5656 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5657 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5658 				      vsi->num_queue_pairs);
5659 		ret = i40e_vsi_config_rss(vsi);
5660 		if (ret) {
5661 			dev_info(&vsi->back->pdev->dev,
5662 				 "Failed to reconfig rss for num_queues\n");
5663 			return ret;
5664 		}
5665 		vsi->reconfig_rss = false;
5666 	}
5667 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5668 		ctxt.info.valid_sections |=
5669 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5670 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5671 	}
5672 
5673 	/* Update the VSI after updating the VSI queue-mapping
5674 	 * information
5675 	 */
5676 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5677 	if (ret) {
5678 		dev_info(&pf->pdev->dev,
5679 			 "Update vsi tc config failed, err %s aq_err %s\n",
5680 			 i40e_stat_str(hw, ret),
5681 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5682 		goto out;
5683 	}
5684 	/* update the local VSI info with updated queue map */
5685 	i40e_vsi_update_queue_map(vsi, &ctxt);
5686 	vsi->info.valid_sections = 0;
5687 
5688 	/* Update current VSI BW information */
5689 	ret = i40e_vsi_get_bw_info(vsi);
5690 	if (ret) {
5691 		dev_info(&pf->pdev->dev,
5692 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5693 			 i40e_stat_str(hw, ret),
5694 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5695 		goto out;
5696 	}
5697 
5698 	/* Update the netdev TC setup */
5699 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5700 out:
5701 	return ret;
5702 }
5703 
5704 /**
5705  * i40e_get_link_speed - Returns link speed for the interface
5706  * @vsi: VSI to be configured
5707  *
5708  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5709 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5710 {
5711 	struct i40e_pf *pf = vsi->back;
5712 
5713 	switch (pf->hw.phy.link_info.link_speed) {
5714 	case I40E_LINK_SPEED_40GB:
5715 		return 40000;
5716 	case I40E_LINK_SPEED_25GB:
5717 		return 25000;
5718 	case I40E_LINK_SPEED_20GB:
5719 		return 20000;
5720 	case I40E_LINK_SPEED_10GB:
5721 		return 10000;
5722 	case I40E_LINK_SPEED_1GB:
5723 		return 1000;
5724 	default:
5725 		return -EINVAL;
5726 	}
5727 }
5728 
5729 /**
5730  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5731  * @vsi: Pointer to vsi structure
5732  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5733  *
5734  * Helper function to convert units before send to set BW limit
5735  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5736 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5737 {
5738 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5739 		dev_warn(&vsi->back->pdev->dev,
5740 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5741 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5742 	} else {
5743 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5744 	}
5745 
5746 	return max_tx_rate;
5747 }
5748 
5749 /**
5750  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5751  * @vsi: VSI to be configured
5752  * @seid: seid of the channel/VSI
5753  * @max_tx_rate: max TX rate to be configured as BW limit
5754  *
5755  * Helper function to set BW limit for a given VSI
5756  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5757 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5758 {
5759 	struct i40e_pf *pf = vsi->back;
5760 	u64 credits = 0;
5761 	int speed = 0;
5762 	int ret = 0;
5763 
5764 	speed = i40e_get_link_speed(vsi);
5765 	if (max_tx_rate > speed) {
5766 		dev_err(&pf->pdev->dev,
5767 			"Invalid max tx rate %llu specified for VSI seid %d.",
5768 			max_tx_rate, seid);
5769 		return -EINVAL;
5770 	}
5771 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5772 		dev_warn(&pf->pdev->dev,
5773 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5774 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5775 	}
5776 
5777 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5778 	credits = max_tx_rate;
5779 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5780 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5781 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5782 	if (ret)
5783 		dev_err(&pf->pdev->dev,
5784 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5785 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5786 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5787 	return ret;
5788 }
5789 
5790 /**
5791  * i40e_remove_queue_channels - Remove queue channels for the TCs
5792  * @vsi: VSI to be configured
5793  *
5794  * Remove queue channels for the TCs
5795  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5796 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5797 {
5798 	enum i40e_admin_queue_err last_aq_status;
5799 	struct i40e_cloud_filter *cfilter;
5800 	struct i40e_channel *ch, *ch_tmp;
5801 	struct i40e_pf *pf = vsi->back;
5802 	struct hlist_node *node;
5803 	int ret, i;
5804 
5805 	/* Reset rss size that was stored when reconfiguring rss for
5806 	 * channel VSIs with non-power-of-2 queue count.
5807 	 */
5808 	vsi->current_rss_size = 0;
5809 
5810 	/* perform cleanup for channels if they exist */
5811 	if (list_empty(&vsi->ch_list))
5812 		return;
5813 
5814 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5815 		struct i40e_vsi *p_vsi;
5816 
5817 		list_del(&ch->list);
5818 		p_vsi = ch->parent_vsi;
5819 		if (!p_vsi || !ch->initialized) {
5820 			kfree(ch);
5821 			continue;
5822 		}
5823 		/* Reset queue contexts */
5824 		for (i = 0; i < ch->num_queue_pairs; i++) {
5825 			struct i40e_ring *tx_ring, *rx_ring;
5826 			u16 pf_q;
5827 
5828 			pf_q = ch->base_queue + i;
5829 			tx_ring = vsi->tx_rings[pf_q];
5830 			tx_ring->ch = NULL;
5831 
5832 			rx_ring = vsi->rx_rings[pf_q];
5833 			rx_ring->ch = NULL;
5834 		}
5835 
5836 		/* Reset BW configured for this VSI via mqprio */
5837 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5838 		if (ret)
5839 			dev_info(&vsi->back->pdev->dev,
5840 				 "Failed to reset tx rate for ch->seid %u\n",
5841 				 ch->seid);
5842 
5843 		/* delete cloud filters associated with this channel */
5844 		hlist_for_each_entry_safe(cfilter, node,
5845 					  &pf->cloud_filter_list, cloud_node) {
5846 			if (cfilter->seid != ch->seid)
5847 				continue;
5848 
5849 			hash_del(&cfilter->cloud_node);
5850 			if (cfilter->dst_port)
5851 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5852 									cfilter,
5853 									false);
5854 			else
5855 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5856 								false);
5857 			last_aq_status = pf->hw.aq.asq_last_status;
5858 			if (ret)
5859 				dev_info(&pf->pdev->dev,
5860 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5861 					 i40e_stat_str(&pf->hw, ret),
5862 					 i40e_aq_str(&pf->hw, last_aq_status));
5863 			kfree(cfilter);
5864 		}
5865 
5866 		/* delete VSI from FW */
5867 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5868 					     NULL);
5869 		if (ret)
5870 			dev_err(&vsi->back->pdev->dev,
5871 				"unable to remove channel (%d) for parent VSI(%d)\n",
5872 				ch->seid, p_vsi->seid);
5873 		kfree(ch);
5874 	}
5875 	INIT_LIST_HEAD(&vsi->ch_list);
5876 }
5877 
5878 /**
5879  * i40e_get_max_queues_for_channel
5880  * @vsi: ptr to VSI to which channels are associated with
5881  *
5882  * Helper function which returns max value among the queue counts set on the
5883  * channels/TCs created.
5884  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)5885 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5886 {
5887 	struct i40e_channel *ch, *ch_tmp;
5888 	int max = 0;
5889 
5890 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5891 		if (!ch->initialized)
5892 			continue;
5893 		if (ch->num_queue_pairs > max)
5894 			max = ch->num_queue_pairs;
5895 	}
5896 
5897 	return max;
5898 }
5899 
5900 /**
5901  * i40e_validate_num_queues - validate num_queues w.r.t channel
5902  * @pf: ptr to PF device
5903  * @num_queues: number of queues
5904  * @vsi: the parent VSI
5905  * @reconfig_rss: indicates should the RSS be reconfigured or not
5906  *
5907  * This function validates number of queues in the context of new channel
5908  * which is being established and determines if RSS should be reconfigured
5909  * or not for parent VSI.
5910  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)5911 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5912 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5913 {
5914 	int max_ch_queues;
5915 
5916 	if (!reconfig_rss)
5917 		return -EINVAL;
5918 
5919 	*reconfig_rss = false;
5920 	if (vsi->current_rss_size) {
5921 		if (num_queues > vsi->current_rss_size) {
5922 			dev_dbg(&pf->pdev->dev,
5923 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5924 				num_queues, vsi->current_rss_size);
5925 			return -EINVAL;
5926 		} else if ((num_queues < vsi->current_rss_size) &&
5927 			   (!is_power_of_2(num_queues))) {
5928 			dev_dbg(&pf->pdev->dev,
5929 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5930 				num_queues, vsi->current_rss_size);
5931 			return -EINVAL;
5932 		}
5933 	}
5934 
5935 	if (!is_power_of_2(num_queues)) {
5936 		/* Find the max num_queues configured for channel if channel
5937 		 * exist.
5938 		 * if channel exist, then enforce 'num_queues' to be more than
5939 		 * max ever queues configured for channel.
5940 		 */
5941 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5942 		if (num_queues < max_ch_queues) {
5943 			dev_dbg(&pf->pdev->dev,
5944 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5945 				num_queues, max_ch_queues);
5946 			return -EINVAL;
5947 		}
5948 		*reconfig_rss = true;
5949 	}
5950 
5951 	return 0;
5952 }
5953 
5954 /**
5955  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5956  * @vsi: the VSI being setup
5957  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5958  *
5959  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5960  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)5961 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5962 {
5963 	struct i40e_pf *pf = vsi->back;
5964 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5965 	struct i40e_hw *hw = &pf->hw;
5966 	int local_rss_size;
5967 	u8 *lut;
5968 	int ret;
5969 
5970 	if (!vsi->rss_size)
5971 		return -EINVAL;
5972 
5973 	if (rss_size > vsi->rss_size)
5974 		return -EINVAL;
5975 
5976 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5977 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5978 	if (!lut)
5979 		return -ENOMEM;
5980 
5981 	/* Ignoring user configured lut if there is one */
5982 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5983 
5984 	/* Use user configured hash key if there is one, otherwise
5985 	 * use default.
5986 	 */
5987 	if (vsi->rss_hkey_user)
5988 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5989 	else
5990 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5991 
5992 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5993 	if (ret) {
5994 		dev_info(&pf->pdev->dev,
5995 			 "Cannot set RSS lut, err %s aq_err %s\n",
5996 			 i40e_stat_str(hw, ret),
5997 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5998 		kfree(lut);
5999 		return ret;
6000 	}
6001 	kfree(lut);
6002 
6003 	/* Do the update w.r.t. storing rss_size */
6004 	if (!vsi->orig_rss_size)
6005 		vsi->orig_rss_size = vsi->rss_size;
6006 	vsi->current_rss_size = local_rss_size;
6007 
6008 	return ret;
6009 }
6010 
6011 /**
6012  * i40e_channel_setup_queue_map - Setup a channel queue map
6013  * @pf: ptr to PF device
6014  * @ctxt: VSI context structure
6015  * @ch: ptr to channel structure
6016  *
6017  * Setup queue map for a specific channel
6018  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6019 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6020 					 struct i40e_vsi_context *ctxt,
6021 					 struct i40e_channel *ch)
6022 {
6023 	u16 qcount, qmap, sections = 0;
6024 	u8 offset = 0;
6025 	int pow;
6026 
6027 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6028 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6029 
6030 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6031 	ch->num_queue_pairs = qcount;
6032 
6033 	/* find the next higher power-of-2 of num queue pairs */
6034 	pow = ilog2(qcount);
6035 	if (!is_power_of_2(qcount))
6036 		pow++;
6037 
6038 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6039 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6040 
6041 	/* Setup queue TC[0].qmap for given VSI context */
6042 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6043 
6044 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6045 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6046 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6047 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6048 }
6049 
6050 /**
6051  * i40e_add_channel - add a channel by adding VSI
6052  * @pf: ptr to PF device
6053  * @uplink_seid: underlying HW switching element (VEB) ID
6054  * @ch: ptr to channel structure
6055  *
6056  * Add a channel (VSI) using add_vsi and queue_map
6057  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6058 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6059 			    struct i40e_channel *ch)
6060 {
6061 	struct i40e_hw *hw = &pf->hw;
6062 	struct i40e_vsi_context ctxt;
6063 	u8 enabled_tc = 0x1; /* TC0 enabled */
6064 	int ret;
6065 
6066 	if (ch->type != I40E_VSI_VMDQ2) {
6067 		dev_info(&pf->pdev->dev,
6068 			 "add new vsi failed, ch->type %d\n", ch->type);
6069 		return -EINVAL;
6070 	}
6071 
6072 	memset(&ctxt, 0, sizeof(ctxt));
6073 	ctxt.pf_num = hw->pf_id;
6074 	ctxt.vf_num = 0;
6075 	ctxt.uplink_seid = uplink_seid;
6076 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6077 	if (ch->type == I40E_VSI_VMDQ2)
6078 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6079 
6080 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6081 		ctxt.info.valid_sections |=
6082 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6083 		ctxt.info.switch_id =
6084 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6085 	}
6086 
6087 	/* Set queue map for a given VSI context */
6088 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6089 
6090 	/* Now time to create VSI */
6091 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6092 	if (ret) {
6093 		dev_info(&pf->pdev->dev,
6094 			 "add new vsi failed, err %s aq_err %s\n",
6095 			 i40e_stat_str(&pf->hw, ret),
6096 			 i40e_aq_str(&pf->hw,
6097 				     pf->hw.aq.asq_last_status));
6098 		return -ENOENT;
6099 	}
6100 
6101 	/* Success, update channel, set enabled_tc only if the channel
6102 	 * is not a macvlan
6103 	 */
6104 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6105 	ch->seid = ctxt.seid;
6106 	ch->vsi_number = ctxt.vsi_number;
6107 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6108 
6109 	/* copy just the sections touched not the entire info
6110 	 * since not all sections are valid as returned by
6111 	 * update vsi params
6112 	 */
6113 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6114 	memcpy(&ch->info.queue_mapping,
6115 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6116 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6117 	       sizeof(ctxt.info.tc_mapping));
6118 
6119 	return 0;
6120 }
6121 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6122 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6123 				  u8 *bw_share)
6124 {
6125 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6126 	i40e_status ret;
6127 	int i;
6128 
6129 	bw_data.tc_valid_bits = ch->enabled_tc;
6130 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6131 		bw_data.tc_bw_credits[i] = bw_share[i];
6132 
6133 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6134 				       &bw_data, NULL);
6135 	if (ret) {
6136 		dev_info(&vsi->back->pdev->dev,
6137 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6138 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6139 		return -EINVAL;
6140 	}
6141 
6142 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6143 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6144 
6145 	return 0;
6146 }
6147 
6148 /**
6149  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6150  * @pf: ptr to PF device
6151  * @vsi: the VSI being setup
6152  * @ch: ptr to channel structure
6153  *
6154  * Configure TX rings associated with channel (VSI) since queues are being
6155  * from parent VSI.
6156  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6157 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6158 				       struct i40e_vsi *vsi,
6159 				       struct i40e_channel *ch)
6160 {
6161 	i40e_status ret;
6162 	int i;
6163 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6164 
6165 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6166 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6167 		if (ch->enabled_tc & BIT(i))
6168 			bw_share[i] = 1;
6169 	}
6170 
6171 	/* configure BW for new VSI */
6172 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6173 	if (ret) {
6174 		dev_info(&vsi->back->pdev->dev,
6175 			 "Failed configuring TC map %d for channel (seid %u)\n",
6176 			 ch->enabled_tc, ch->seid);
6177 		return ret;
6178 	}
6179 
6180 	for (i = 0; i < ch->num_queue_pairs; i++) {
6181 		struct i40e_ring *tx_ring, *rx_ring;
6182 		u16 pf_q;
6183 
6184 		pf_q = ch->base_queue + i;
6185 
6186 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6187 		 * context
6188 		 */
6189 		tx_ring = vsi->tx_rings[pf_q];
6190 		tx_ring->ch = ch;
6191 
6192 		/* Get the RX ring ptr */
6193 		rx_ring = vsi->rx_rings[pf_q];
6194 		rx_ring->ch = ch;
6195 	}
6196 
6197 	return 0;
6198 }
6199 
6200 /**
6201  * i40e_setup_hw_channel - setup new channel
6202  * @pf: ptr to PF device
6203  * @vsi: the VSI being setup
6204  * @ch: ptr to channel structure
6205  * @uplink_seid: underlying HW switching element (VEB) ID
6206  * @type: type of channel to be created (VMDq2/VF)
6207  *
6208  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6209  * and configures TX rings accordingly
6210  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6211 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6212 					struct i40e_vsi *vsi,
6213 					struct i40e_channel *ch,
6214 					u16 uplink_seid, u8 type)
6215 {
6216 	int ret;
6217 
6218 	ch->initialized = false;
6219 	ch->base_queue = vsi->next_base_queue;
6220 	ch->type = type;
6221 
6222 	/* Proceed with creation of channel (VMDq2) VSI */
6223 	ret = i40e_add_channel(pf, uplink_seid, ch);
6224 	if (ret) {
6225 		dev_info(&pf->pdev->dev,
6226 			 "failed to add_channel using uplink_seid %u\n",
6227 			 uplink_seid);
6228 		return ret;
6229 	}
6230 
6231 	/* Mark the successful creation of channel */
6232 	ch->initialized = true;
6233 
6234 	/* Reconfigure TX queues using QTX_CTL register */
6235 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6236 	if (ret) {
6237 		dev_info(&pf->pdev->dev,
6238 			 "failed to configure TX rings for channel %u\n",
6239 			 ch->seid);
6240 		return ret;
6241 	}
6242 
6243 	/* update 'next_base_queue' */
6244 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6245 	dev_dbg(&pf->pdev->dev,
6246 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6247 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6248 		ch->num_queue_pairs,
6249 		vsi->next_base_queue);
6250 	return ret;
6251 }
6252 
6253 /**
6254  * i40e_setup_channel - setup new channel using uplink element
6255  * @pf: ptr to PF device
6256  * @vsi: pointer to the VSI to set up the channel within
6257  * @ch: ptr to channel structure
6258  *
6259  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6260  * and uplink switching element (uplink_seid)
6261  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6262 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6263 			       struct i40e_channel *ch)
6264 {
6265 	u8 vsi_type;
6266 	u16 seid;
6267 	int ret;
6268 
6269 	if (vsi->type == I40E_VSI_MAIN) {
6270 		vsi_type = I40E_VSI_VMDQ2;
6271 	} else {
6272 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6273 			vsi->type);
6274 		return false;
6275 	}
6276 
6277 	/* underlying switching element */
6278 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6279 
6280 	/* create channel (VSI), configure TX rings */
6281 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6282 	if (ret) {
6283 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6284 		return false;
6285 	}
6286 
6287 	return ch->initialized ? true : false;
6288 }
6289 
6290 /**
6291  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6292  * @vsi: ptr to VSI which has PF backing
6293  *
6294  * Sets up switch mode correctly if it needs to be changed and perform
6295  * what are allowed modes.
6296  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6297 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6298 {
6299 	u8 mode;
6300 	struct i40e_pf *pf = vsi->back;
6301 	struct i40e_hw *hw = &pf->hw;
6302 	int ret;
6303 
6304 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6305 	if (ret)
6306 		return -EINVAL;
6307 
6308 	if (hw->dev_caps.switch_mode) {
6309 		/* if switch mode is set, support mode2 (non-tunneled for
6310 		 * cloud filter) for now
6311 		 */
6312 		u32 switch_mode = hw->dev_caps.switch_mode &
6313 				  I40E_SWITCH_MODE_MASK;
6314 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6315 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6316 				return 0;
6317 			dev_err(&pf->pdev->dev,
6318 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6319 				hw->dev_caps.switch_mode);
6320 			return -EINVAL;
6321 		}
6322 	}
6323 
6324 	/* Set Bit 7 to be valid */
6325 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6326 
6327 	/* Set L4type for TCP support */
6328 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6329 
6330 	/* Set cloud filter mode */
6331 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6332 
6333 	/* Prep mode field for set_switch_config */
6334 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6335 					pf->last_sw_conf_valid_flags,
6336 					mode, NULL);
6337 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6338 		dev_err(&pf->pdev->dev,
6339 			"couldn't set switch config bits, err %s aq_err %s\n",
6340 			i40e_stat_str(hw, ret),
6341 			i40e_aq_str(hw,
6342 				    hw->aq.asq_last_status));
6343 
6344 	return ret;
6345 }
6346 
6347 /**
6348  * i40e_create_queue_channel - function to create channel
6349  * @vsi: VSI to be configured
6350  * @ch: ptr to channel (it contains channel specific params)
6351  *
6352  * This function creates channel (VSI) using num_queues specified by user,
6353  * reconfigs RSS if needed.
6354  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6355 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6356 			      struct i40e_channel *ch)
6357 {
6358 	struct i40e_pf *pf = vsi->back;
6359 	bool reconfig_rss;
6360 	int err;
6361 
6362 	if (!ch)
6363 		return -EINVAL;
6364 
6365 	if (!ch->num_queue_pairs) {
6366 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6367 			ch->num_queue_pairs);
6368 		return -EINVAL;
6369 	}
6370 
6371 	/* validate user requested num_queues for channel */
6372 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6373 				       &reconfig_rss);
6374 	if (err) {
6375 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6376 			 ch->num_queue_pairs);
6377 		return -EINVAL;
6378 	}
6379 
6380 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6381 	 * VSI to be added switch to VEB mode.
6382 	 */
6383 
6384 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6385 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6386 
6387 		if (vsi->type == I40E_VSI_MAIN) {
6388 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6389 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6390 			else
6391 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6392 		}
6393 		/* now onwards for main VSI, number of queues will be value
6394 		 * of TC0's queue count
6395 		 */
6396 	}
6397 
6398 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6399 	 * it should be more than num_queues
6400 	 */
6401 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6402 		dev_dbg(&pf->pdev->dev,
6403 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6404 			vsi->cnt_q_avail, ch->num_queue_pairs);
6405 		return -EINVAL;
6406 	}
6407 
6408 	/* reconfig_rss only if vsi type is MAIN_VSI */
6409 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6410 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6411 		if (err) {
6412 			dev_info(&pf->pdev->dev,
6413 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6414 				 ch->num_queue_pairs);
6415 			return -EINVAL;
6416 		}
6417 	}
6418 
6419 	if (!i40e_setup_channel(pf, vsi, ch)) {
6420 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6421 		return -EINVAL;
6422 	}
6423 
6424 	dev_info(&pf->pdev->dev,
6425 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6426 		 ch->seid, ch->num_queue_pairs);
6427 
6428 	/* configure VSI for BW limit */
6429 	if (ch->max_tx_rate) {
6430 		u64 credits = ch->max_tx_rate;
6431 
6432 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6433 			return -EINVAL;
6434 
6435 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6436 		dev_dbg(&pf->pdev->dev,
6437 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6438 			ch->max_tx_rate,
6439 			credits,
6440 			ch->seid);
6441 	}
6442 
6443 	/* in case of VF, this will be main SRIOV VSI */
6444 	ch->parent_vsi = vsi;
6445 
6446 	/* and update main_vsi's count for queue_available to use */
6447 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6448 
6449 	return 0;
6450 }
6451 
6452 /**
6453  * i40e_configure_queue_channels - Add queue channel for the given TCs
6454  * @vsi: VSI to be configured
6455  *
6456  * Configures queue channel mapping to the given TCs
6457  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6458 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6459 {
6460 	struct i40e_channel *ch;
6461 	u64 max_rate = 0;
6462 	int ret = 0, i;
6463 
6464 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6465 	vsi->tc_seid_map[0] = vsi->seid;
6466 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6467 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6468 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6469 			if (!ch) {
6470 				ret = -ENOMEM;
6471 				goto err_free;
6472 			}
6473 
6474 			INIT_LIST_HEAD(&ch->list);
6475 			ch->num_queue_pairs =
6476 				vsi->tc_config.tc_info[i].qcount;
6477 			ch->base_queue =
6478 				vsi->tc_config.tc_info[i].qoffset;
6479 
6480 			/* Bandwidth limit through tc interface is in bytes/s,
6481 			 * change to Mbit/s
6482 			 */
6483 			max_rate = vsi->mqprio_qopt.max_rate[i];
6484 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6485 			ch->max_tx_rate = max_rate;
6486 
6487 			list_add_tail(&ch->list, &vsi->ch_list);
6488 
6489 			ret = i40e_create_queue_channel(vsi, ch);
6490 			if (ret) {
6491 				dev_err(&vsi->back->pdev->dev,
6492 					"Failed creating queue channel with TC%d: queues %d\n",
6493 					i, ch->num_queue_pairs);
6494 				goto err_free;
6495 			}
6496 			vsi->tc_seid_map[i] = ch->seid;
6497 		}
6498 	}
6499 	return ret;
6500 
6501 err_free:
6502 	i40e_remove_queue_channels(vsi);
6503 	return ret;
6504 }
6505 
6506 /**
6507  * i40e_veb_config_tc - Configure TCs for given VEB
6508  * @veb: given VEB
6509  * @enabled_tc: TC bitmap
6510  *
6511  * Configures given TC bitmap for VEB (switching) element
6512  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6513 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6514 {
6515 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6516 	struct i40e_pf *pf = veb->pf;
6517 	int ret = 0;
6518 	int i;
6519 
6520 	/* No TCs or already enabled TCs just return */
6521 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6522 		return ret;
6523 
6524 	bw_data.tc_valid_bits = enabled_tc;
6525 	/* bw_data.absolute_credits is not set (relative) */
6526 
6527 	/* Enable ETS TCs with equal BW Share for now */
6528 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6529 		if (enabled_tc & BIT(i))
6530 			bw_data.tc_bw_share_credits[i] = 1;
6531 	}
6532 
6533 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6534 						   &bw_data, NULL);
6535 	if (ret) {
6536 		dev_info(&pf->pdev->dev,
6537 			 "VEB bw config failed, err %s aq_err %s\n",
6538 			 i40e_stat_str(&pf->hw, ret),
6539 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6540 		goto out;
6541 	}
6542 
6543 	/* Update the BW information */
6544 	ret = i40e_veb_get_bw_info(veb);
6545 	if (ret) {
6546 		dev_info(&pf->pdev->dev,
6547 			 "Failed getting veb bw config, err %s aq_err %s\n",
6548 			 i40e_stat_str(&pf->hw, ret),
6549 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6550 	}
6551 
6552 out:
6553 	return ret;
6554 }
6555 
6556 #ifdef CONFIG_I40E_DCB
6557 /**
6558  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6559  * @pf: PF struct
6560  *
6561  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6562  * the caller would've quiesce all the VSIs before calling
6563  * this function
6564  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6565 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6566 {
6567 	u8 tc_map = 0;
6568 	int ret;
6569 	u8 v;
6570 
6571 	/* Enable the TCs available on PF to all VEBs */
6572 	tc_map = i40e_pf_get_tc_map(pf);
6573 	for (v = 0; v < I40E_MAX_VEB; v++) {
6574 		if (!pf->veb[v])
6575 			continue;
6576 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6577 		if (ret) {
6578 			dev_info(&pf->pdev->dev,
6579 				 "Failed configuring TC for VEB seid=%d\n",
6580 				 pf->veb[v]->seid);
6581 			/* Will try to configure as many components */
6582 		}
6583 	}
6584 
6585 	/* Update each VSI */
6586 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6587 		if (!pf->vsi[v])
6588 			continue;
6589 
6590 		/* - Enable all TCs for the LAN VSI
6591 		 * - For all others keep them at TC0 for now
6592 		 */
6593 		if (v == pf->lan_vsi)
6594 			tc_map = i40e_pf_get_tc_map(pf);
6595 		else
6596 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6597 
6598 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6599 		if (ret) {
6600 			dev_info(&pf->pdev->dev,
6601 				 "Failed configuring TC for VSI seid=%d\n",
6602 				 pf->vsi[v]->seid);
6603 			/* Will try to configure as many components */
6604 		} else {
6605 			/* Re-configure VSI vectors based on updated TC map */
6606 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6607 			if (pf->vsi[v]->netdev)
6608 				i40e_dcbnl_set_all(pf->vsi[v]);
6609 		}
6610 	}
6611 }
6612 
6613 /**
6614  * i40e_resume_port_tx - Resume port Tx
6615  * @pf: PF struct
6616  *
6617  * Resume a port's Tx and issue a PF reset in case of failure to
6618  * resume.
6619  **/
i40e_resume_port_tx(struct i40e_pf * pf)6620 static int i40e_resume_port_tx(struct i40e_pf *pf)
6621 {
6622 	struct i40e_hw *hw = &pf->hw;
6623 	int ret;
6624 
6625 	ret = i40e_aq_resume_port_tx(hw, NULL);
6626 	if (ret) {
6627 		dev_info(&pf->pdev->dev,
6628 			 "Resume Port Tx failed, err %s aq_err %s\n",
6629 			  i40e_stat_str(&pf->hw, ret),
6630 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6631 		/* Schedule PF reset to recover */
6632 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6633 		i40e_service_event_schedule(pf);
6634 	}
6635 
6636 	return ret;
6637 }
6638 
6639 /**
6640  * i40e_init_pf_dcb - Initialize DCB configuration
6641  * @pf: PF being configured
6642  *
6643  * Query the current DCB configuration and cache it
6644  * in the hardware structure
6645  **/
i40e_init_pf_dcb(struct i40e_pf * pf)6646 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6647 {
6648 	struct i40e_hw *hw = &pf->hw;
6649 	int err = 0;
6650 
6651 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6652 	 * Also do not enable DCBx if FW LLDP agent is disabled
6653 	 */
6654 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6655 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
6656 		dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
6657 		err = I40E_NOT_SUPPORTED;
6658 		goto out;
6659 	}
6660 
6661 	err = i40e_init_dcb(hw, true);
6662 	if (!err) {
6663 		/* Device/Function is not DCBX capable */
6664 		if ((!hw->func_caps.dcb) ||
6665 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6666 			dev_info(&pf->pdev->dev,
6667 				 "DCBX offload is not supported or is disabled for this PF.\n");
6668 		} else {
6669 			/* When status is not DISABLED then DCBX in FW */
6670 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6671 				       DCB_CAP_DCBX_VER_IEEE;
6672 
6673 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6674 			/* Enable DCB tagging only when more than one TC
6675 			 * or explicitly disable if only one TC
6676 			 */
6677 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6678 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6679 			else
6680 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6681 			dev_dbg(&pf->pdev->dev,
6682 				"DCBX offload is supported for this PF.\n");
6683 		}
6684 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6685 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6686 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6687 	} else {
6688 		dev_info(&pf->pdev->dev,
6689 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6690 			 i40e_stat_str(&pf->hw, err),
6691 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6692 	}
6693 
6694 out:
6695 	return err;
6696 }
6697 #endif /* CONFIG_I40E_DCB */
6698 
6699 /**
6700  * i40e_print_link_message - print link up or down
6701  * @vsi: the VSI for which link needs a message
6702  * @isup: true of link is up, false otherwise
6703  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)6704 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6705 {
6706 	enum i40e_aq_link_speed new_speed;
6707 	struct i40e_pf *pf = vsi->back;
6708 	char *speed = "Unknown";
6709 	char *fc = "Unknown";
6710 	char *fec = "";
6711 	char *req_fec = "";
6712 	char *an = "";
6713 
6714 	if (isup)
6715 		new_speed = pf->hw.phy.link_info.link_speed;
6716 	else
6717 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6718 
6719 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6720 		return;
6721 	vsi->current_isup = isup;
6722 	vsi->current_speed = new_speed;
6723 	if (!isup) {
6724 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6725 		return;
6726 	}
6727 
6728 	/* Warn user if link speed on NPAR enabled partition is not at
6729 	 * least 10GB
6730 	 */
6731 	if (pf->hw.func_caps.npar_enable &&
6732 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6733 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6734 		netdev_warn(vsi->netdev,
6735 			    "The partition detected link speed that is less than 10Gbps\n");
6736 
6737 	switch (pf->hw.phy.link_info.link_speed) {
6738 	case I40E_LINK_SPEED_40GB:
6739 		speed = "40 G";
6740 		break;
6741 	case I40E_LINK_SPEED_20GB:
6742 		speed = "20 G";
6743 		break;
6744 	case I40E_LINK_SPEED_25GB:
6745 		speed = "25 G";
6746 		break;
6747 	case I40E_LINK_SPEED_10GB:
6748 		speed = "10 G";
6749 		break;
6750 	case I40E_LINK_SPEED_5GB:
6751 		speed = "5 G";
6752 		break;
6753 	case I40E_LINK_SPEED_2_5GB:
6754 		speed = "2.5 G";
6755 		break;
6756 	case I40E_LINK_SPEED_1GB:
6757 		speed = "1000 M";
6758 		break;
6759 	case I40E_LINK_SPEED_100MB:
6760 		speed = "100 M";
6761 		break;
6762 	default:
6763 		break;
6764 	}
6765 
6766 	switch (pf->hw.fc.current_mode) {
6767 	case I40E_FC_FULL:
6768 		fc = "RX/TX";
6769 		break;
6770 	case I40E_FC_TX_PAUSE:
6771 		fc = "TX";
6772 		break;
6773 	case I40E_FC_RX_PAUSE:
6774 		fc = "RX";
6775 		break;
6776 	default:
6777 		fc = "None";
6778 		break;
6779 	}
6780 
6781 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6782 		req_fec = "None";
6783 		fec = "None";
6784 		an = "False";
6785 
6786 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6787 			an = "True";
6788 
6789 		if (pf->hw.phy.link_info.fec_info &
6790 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6791 			fec = "CL74 FC-FEC/BASE-R";
6792 		else if (pf->hw.phy.link_info.fec_info &
6793 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6794 			fec = "CL108 RS-FEC";
6795 
6796 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6797 		 * both RS and FC are requested
6798 		 */
6799 		if (vsi->back->hw.phy.link_info.req_fec_info &
6800 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6801 			if (vsi->back->hw.phy.link_info.req_fec_info &
6802 			    I40E_AQ_REQUEST_FEC_RS)
6803 				req_fec = "CL108 RS-FEC";
6804 			else
6805 				req_fec = "CL74 FC-FEC/BASE-R";
6806 		}
6807 		netdev_info(vsi->netdev,
6808 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6809 			    speed, req_fec, fec, an, fc);
6810 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
6811 		req_fec = "None";
6812 		fec = "None";
6813 		an = "False";
6814 
6815 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6816 			an = "True";
6817 
6818 		if (pf->hw.phy.link_info.fec_info &
6819 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6820 			fec = "CL74 FC-FEC/BASE-R";
6821 
6822 		if (pf->hw.phy.link_info.req_fec_info &
6823 		    I40E_AQ_REQUEST_FEC_KR)
6824 			req_fec = "CL74 FC-FEC/BASE-R";
6825 
6826 		netdev_info(vsi->netdev,
6827 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6828 			    speed, req_fec, fec, an, fc);
6829 	} else {
6830 		netdev_info(vsi->netdev,
6831 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
6832 			    speed, fc);
6833 	}
6834 
6835 }
6836 
6837 /**
6838  * i40e_up_complete - Finish the last steps of bringing up a connection
6839  * @vsi: the VSI being configured
6840  **/
i40e_up_complete(struct i40e_vsi * vsi)6841 static int i40e_up_complete(struct i40e_vsi *vsi)
6842 {
6843 	struct i40e_pf *pf = vsi->back;
6844 	int err;
6845 
6846 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6847 		i40e_vsi_configure_msix(vsi);
6848 	else
6849 		i40e_configure_msi_and_legacy(vsi);
6850 
6851 	/* start rings */
6852 	err = i40e_vsi_start_rings(vsi);
6853 	if (err)
6854 		return err;
6855 
6856 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6857 	i40e_napi_enable_all(vsi);
6858 	i40e_vsi_enable_irq(vsi);
6859 
6860 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6861 	    (vsi->netdev)) {
6862 		i40e_print_link_message(vsi, true);
6863 		netif_tx_start_all_queues(vsi->netdev);
6864 		netif_carrier_on(vsi->netdev);
6865 	}
6866 
6867 	/* replay FDIR SB filters */
6868 	if (vsi->type == I40E_VSI_FDIR) {
6869 		/* reset fd counters */
6870 		pf->fd_add_err = 0;
6871 		pf->fd_atr_cnt = 0;
6872 		i40e_fdir_filter_restore(vsi);
6873 	}
6874 
6875 	/* On the next run of the service_task, notify any clients of the new
6876 	 * opened netdev
6877 	 */
6878 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6879 	i40e_service_event_schedule(pf);
6880 
6881 	return 0;
6882 }
6883 
6884 /**
6885  * i40e_vsi_reinit_locked - Reset the VSI
6886  * @vsi: the VSI being configured
6887  *
6888  * Rebuild the ring structs after some configuration
6889  * has changed, e.g. MTU size.
6890  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)6891 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6892 {
6893 	struct i40e_pf *pf = vsi->back;
6894 
6895 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6896 		usleep_range(1000, 2000);
6897 	i40e_down(vsi);
6898 
6899 	i40e_up(vsi);
6900 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6901 }
6902 
6903 /**
6904  * i40e_force_link_state - Force the link status
6905  * @pf: board private structure
6906  * @is_up: whether the link state should be forced up or down
6907  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)6908 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6909 {
6910 	struct i40e_aq_get_phy_abilities_resp abilities;
6911 	struct i40e_aq_set_phy_config config = {0};
6912 	bool non_zero_phy_type = is_up;
6913 	struct i40e_hw *hw = &pf->hw;
6914 	i40e_status err;
6915 	u64 mask;
6916 	u8 speed;
6917 
6918 	/* Card might've been put in an unstable state by other drivers
6919 	 * and applications, which causes incorrect speed values being
6920 	 * set on startup. In order to clear speed registers, we call
6921 	 * get_phy_capabilities twice, once to get initial state of
6922 	 * available speeds, and once to get current PHY config.
6923 	 */
6924 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6925 					   NULL);
6926 	if (err) {
6927 		dev_err(&pf->pdev->dev,
6928 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6929 			i40e_stat_str(hw, err),
6930 			i40e_aq_str(hw, hw->aq.asq_last_status));
6931 		return err;
6932 	}
6933 	speed = abilities.link_speed;
6934 
6935 	/* Get the current phy config */
6936 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6937 					   NULL);
6938 	if (err) {
6939 		dev_err(&pf->pdev->dev,
6940 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6941 			i40e_stat_str(hw, err),
6942 			i40e_aq_str(hw, hw->aq.asq_last_status));
6943 		return err;
6944 	}
6945 
6946 	/* If link needs to go up, but was not forced to go down,
6947 	 * and its speed values are OK, no need for a flap
6948 	 * if non_zero_phy_type was set, still need to force up
6949 	 */
6950 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
6951 		non_zero_phy_type = true;
6952 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6953 		return I40E_SUCCESS;
6954 
6955 	/* To force link we need to set bits for all supported PHY types,
6956 	 * but there are now more than 32, so we need to split the bitmap
6957 	 * across two fields.
6958 	 */
6959 	mask = I40E_PHY_TYPES_BITMASK;
6960 	config.phy_type =
6961 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6962 	config.phy_type_ext =
6963 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
6964 	/* Copy the old settings, except of phy_type */
6965 	config.abilities = abilities.abilities;
6966 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
6967 		if (is_up)
6968 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
6969 		else
6970 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
6971 	}
6972 	if (abilities.link_speed != 0)
6973 		config.link_speed = abilities.link_speed;
6974 	else
6975 		config.link_speed = speed;
6976 	config.eee_capability = abilities.eee_capability;
6977 	config.eeer = abilities.eeer_val;
6978 	config.low_power_ctrl = abilities.d3_lpan;
6979 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6980 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6981 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6982 
6983 	if (err) {
6984 		dev_err(&pf->pdev->dev,
6985 			"set phy config ret =  %s last_status =  %s\n",
6986 			i40e_stat_str(&pf->hw, err),
6987 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6988 		return err;
6989 	}
6990 
6991 	/* Update the link info */
6992 	err = i40e_update_link_info(hw);
6993 	if (err) {
6994 		/* Wait a little bit (on 40G cards it sometimes takes a really
6995 		 * long time for link to come back from the atomic reset)
6996 		 * and try once more
6997 		 */
6998 		msleep(1000);
6999 		i40e_update_link_info(hw);
7000 	}
7001 
7002 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7003 
7004 	return I40E_SUCCESS;
7005 }
7006 
7007 /**
7008  * i40e_up - Bring the connection back up after being down
7009  * @vsi: the VSI being configured
7010  **/
i40e_up(struct i40e_vsi * vsi)7011 int i40e_up(struct i40e_vsi *vsi)
7012 {
7013 	int err;
7014 
7015 	if (vsi->type == I40E_VSI_MAIN &&
7016 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7017 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7018 		i40e_force_link_state(vsi->back, true);
7019 
7020 	err = i40e_vsi_configure(vsi);
7021 	if (!err)
7022 		err = i40e_up_complete(vsi);
7023 
7024 	return err;
7025 }
7026 
7027 /**
7028  * i40e_down - Shutdown the connection processing
7029  * @vsi: the VSI being stopped
7030  **/
i40e_down(struct i40e_vsi * vsi)7031 void i40e_down(struct i40e_vsi *vsi)
7032 {
7033 	int i;
7034 
7035 	/* It is assumed that the caller of this function
7036 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7037 	 */
7038 	if (vsi->netdev) {
7039 		netif_carrier_off(vsi->netdev);
7040 		netif_tx_disable(vsi->netdev);
7041 	}
7042 	i40e_vsi_disable_irq(vsi);
7043 	i40e_vsi_stop_rings(vsi);
7044 	if (vsi->type == I40E_VSI_MAIN &&
7045 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7046 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7047 		i40e_force_link_state(vsi->back, false);
7048 	i40e_napi_disable_all(vsi);
7049 
7050 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7051 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7052 		if (i40e_enabled_xdp_vsi(vsi)) {
7053 			/* Make sure that in-progress ndo_xdp_xmit and
7054 			 * ndo_xsk_wakeup calls are completed.
7055 			 */
7056 			synchronize_rcu();
7057 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7058 		}
7059 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7060 	}
7061 
7062 }
7063 
7064 /**
7065  * i40e_validate_mqprio_qopt- validate queue mapping info
7066  * @vsi: the VSI being configured
7067  * @mqprio_qopt: queue parametrs
7068  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7069 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7070 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7071 {
7072 	u64 sum_max_rate = 0;
7073 	u64 max_rate = 0;
7074 	int i;
7075 
7076 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7077 	    mqprio_qopt->qopt.num_tc < 1 ||
7078 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7079 		return -EINVAL;
7080 	for (i = 0; ; i++) {
7081 		if (!mqprio_qopt->qopt.count[i])
7082 			return -EINVAL;
7083 		if (mqprio_qopt->min_rate[i]) {
7084 			dev_err(&vsi->back->pdev->dev,
7085 				"Invalid min tx rate (greater than 0) specified\n");
7086 			return -EINVAL;
7087 		}
7088 		max_rate = mqprio_qopt->max_rate[i];
7089 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7090 		sum_max_rate += max_rate;
7091 
7092 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7093 			break;
7094 		if (mqprio_qopt->qopt.offset[i + 1] !=
7095 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7096 			return -EINVAL;
7097 	}
7098 	if (vsi->num_queue_pairs <
7099 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7100 		dev_err(&vsi->back->pdev->dev,
7101 			"Failed to create traffic channel, insufficient number of queues.\n");
7102 		return -EINVAL;
7103 	}
7104 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7105 		dev_err(&vsi->back->pdev->dev,
7106 			"Invalid max tx rate specified\n");
7107 		return -EINVAL;
7108 	}
7109 	return 0;
7110 }
7111 
7112 /**
7113  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7114  * @vsi: the VSI being configured
7115  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7116 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7117 {
7118 	u16 qcount;
7119 	int i;
7120 
7121 	/* Only TC0 is enabled */
7122 	vsi->tc_config.numtc = 1;
7123 	vsi->tc_config.enabled_tc = 1;
7124 	qcount = min_t(int, vsi->alloc_queue_pairs,
7125 		       i40e_pf_get_max_q_per_tc(vsi->back));
7126 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7127 		/* For the TC that is not enabled set the offset to to default
7128 		 * queue and allocate one queue for the given TC.
7129 		 */
7130 		vsi->tc_config.tc_info[i].qoffset = 0;
7131 		if (i == 0)
7132 			vsi->tc_config.tc_info[i].qcount = qcount;
7133 		else
7134 			vsi->tc_config.tc_info[i].qcount = 1;
7135 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7136 	}
7137 }
7138 
7139 /**
7140  * i40e_del_macvlan_filter
7141  * @hw: pointer to the HW structure
7142  * @seid: seid of the channel VSI
7143  * @macaddr: the mac address to apply as a filter
7144  * @aq_err: store the admin Q error
7145  *
7146  * This function deletes a mac filter on the channel VSI which serves as the
7147  * macvlan. Returns 0 on success.
7148  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7149 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7150 					   const u8 *macaddr, int *aq_err)
7151 {
7152 	struct i40e_aqc_remove_macvlan_element_data element;
7153 	i40e_status status;
7154 
7155 	memset(&element, 0, sizeof(element));
7156 	ether_addr_copy(element.mac_addr, macaddr);
7157 	element.vlan_tag = 0;
7158 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7159 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7160 	*aq_err = hw->aq.asq_last_status;
7161 
7162 	return status;
7163 }
7164 
7165 /**
7166  * i40e_add_macvlan_filter
7167  * @hw: pointer to the HW structure
7168  * @seid: seid of the channel VSI
7169  * @macaddr: the mac address to apply as a filter
7170  * @aq_err: store the admin Q error
7171  *
7172  * This function adds a mac filter on the channel VSI which serves as the
7173  * macvlan. Returns 0 on success.
7174  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7175 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7176 					   const u8 *macaddr, int *aq_err)
7177 {
7178 	struct i40e_aqc_add_macvlan_element_data element;
7179 	i40e_status status;
7180 	u16 cmd_flags = 0;
7181 
7182 	ether_addr_copy(element.mac_addr, macaddr);
7183 	element.vlan_tag = 0;
7184 	element.queue_number = 0;
7185 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7186 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7187 	element.flags = cpu_to_le16(cmd_flags);
7188 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7189 	*aq_err = hw->aq.asq_last_status;
7190 
7191 	return status;
7192 }
7193 
7194 /**
7195  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7196  * @vsi: the VSI we want to access
7197  * @ch: the channel we want to access
7198  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7199 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7200 {
7201 	struct i40e_ring *tx_ring, *rx_ring;
7202 	u16 pf_q;
7203 	int i;
7204 
7205 	for (i = 0; i < ch->num_queue_pairs; i++) {
7206 		pf_q = ch->base_queue + i;
7207 		tx_ring = vsi->tx_rings[pf_q];
7208 		tx_ring->ch = NULL;
7209 		rx_ring = vsi->rx_rings[pf_q];
7210 		rx_ring->ch = NULL;
7211 	}
7212 }
7213 
7214 /**
7215  * i40e_free_macvlan_channels
7216  * @vsi: the VSI we want to access
7217  *
7218  * This function frees the Qs of the channel VSI from
7219  * the stack and also deletes the channel VSIs which
7220  * serve as macvlans.
7221  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7222 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7223 {
7224 	struct i40e_channel *ch, *ch_tmp;
7225 	int ret;
7226 
7227 	if (list_empty(&vsi->macvlan_list))
7228 		return;
7229 
7230 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7231 		struct i40e_vsi *parent_vsi;
7232 
7233 		if (i40e_is_channel_macvlan(ch)) {
7234 			i40e_reset_ch_rings(vsi, ch);
7235 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7236 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7237 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7238 			kfree(ch->fwd);
7239 			ch->fwd = NULL;
7240 		}
7241 
7242 		list_del(&ch->list);
7243 		parent_vsi = ch->parent_vsi;
7244 		if (!parent_vsi || !ch->initialized) {
7245 			kfree(ch);
7246 			continue;
7247 		}
7248 
7249 		/* remove the VSI */
7250 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7251 					     NULL);
7252 		if (ret)
7253 			dev_err(&vsi->back->pdev->dev,
7254 				"unable to remove channel (%d) for parent VSI(%d)\n",
7255 				ch->seid, parent_vsi->seid);
7256 		kfree(ch);
7257 	}
7258 	vsi->macvlan_cnt = 0;
7259 }
7260 
7261 /**
7262  * i40e_fwd_ring_up - bring the macvlan device up
7263  * @vsi: the VSI we want to access
7264  * @vdev: macvlan netdevice
7265  * @fwd: the private fwd structure
7266  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7267 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7268 			    struct i40e_fwd_adapter *fwd)
7269 {
7270 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7271 	int ret = 0, num_tc = 1,  i, aq_err;
7272 	struct i40e_pf *pf = vsi->back;
7273 	struct i40e_hw *hw = &pf->hw;
7274 
7275 	/* Go through the list and find an available channel */
7276 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7277 		if (!i40e_is_channel_macvlan(iter)) {
7278 			iter->fwd = fwd;
7279 			/* record configuration for macvlan interface in vdev */
7280 			for (i = 0; i < num_tc; i++)
7281 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7282 							     i,
7283 							     iter->num_queue_pairs,
7284 							     iter->base_queue);
7285 			for (i = 0; i < iter->num_queue_pairs; i++) {
7286 				struct i40e_ring *tx_ring, *rx_ring;
7287 				u16 pf_q;
7288 
7289 				pf_q = iter->base_queue + i;
7290 
7291 				/* Get to TX ring ptr */
7292 				tx_ring = vsi->tx_rings[pf_q];
7293 				tx_ring->ch = iter;
7294 
7295 				/* Get the RX ring ptr */
7296 				rx_ring = vsi->rx_rings[pf_q];
7297 				rx_ring->ch = iter;
7298 			}
7299 			ch = iter;
7300 			break;
7301 		}
7302 	}
7303 
7304 	if (!ch)
7305 		return -EINVAL;
7306 
7307 	/* Guarantee all rings are updated before we update the
7308 	 * MAC address filter.
7309 	 */
7310 	wmb();
7311 
7312 	/* Add a mac filter */
7313 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7314 	if (ret) {
7315 		/* if we cannot add the MAC rule then disable the offload */
7316 		macvlan_release_l2fw_offload(vdev);
7317 		for (i = 0; i < ch->num_queue_pairs; i++) {
7318 			struct i40e_ring *rx_ring;
7319 			u16 pf_q;
7320 
7321 			pf_q = ch->base_queue + i;
7322 			rx_ring = vsi->rx_rings[pf_q];
7323 			rx_ring->netdev = NULL;
7324 		}
7325 		dev_info(&pf->pdev->dev,
7326 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7327 			  i40e_stat_str(hw, ret),
7328 			  i40e_aq_str(hw, aq_err));
7329 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7330 	}
7331 
7332 	return ret;
7333 }
7334 
7335 /**
7336  * i40e_setup_macvlans - create the channels which will be macvlans
7337  * @vsi: the VSI we want to access
7338  * @macvlan_cnt: no. of macvlans to be setup
7339  * @qcnt: no. of Qs per macvlan
7340  * @vdev: macvlan netdevice
7341  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7342 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7343 			       struct net_device *vdev)
7344 {
7345 	struct i40e_pf *pf = vsi->back;
7346 	struct i40e_hw *hw = &pf->hw;
7347 	struct i40e_vsi_context ctxt;
7348 	u16 sections, qmap, num_qps;
7349 	struct i40e_channel *ch;
7350 	int i, pow, ret = 0;
7351 	u8 offset = 0;
7352 
7353 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7354 		return -EINVAL;
7355 
7356 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7357 
7358 	/* find the next higher power-of-2 of num queue pairs */
7359 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7360 
7361 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7362 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7363 
7364 	/* Setup context bits for the main VSI */
7365 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7366 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7367 	memset(&ctxt, 0, sizeof(ctxt));
7368 	ctxt.seid = vsi->seid;
7369 	ctxt.pf_num = vsi->back->hw.pf_id;
7370 	ctxt.vf_num = 0;
7371 	ctxt.uplink_seid = vsi->uplink_seid;
7372 	ctxt.info = vsi->info;
7373 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7374 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7375 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7376 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7377 
7378 	/* Reconfigure RSS for main VSI with new max queue count */
7379 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7380 	ret = i40e_vsi_config_rss(vsi);
7381 	if (ret) {
7382 		dev_info(&pf->pdev->dev,
7383 			 "Failed to reconfig RSS for num_queues (%u)\n",
7384 			 vsi->rss_size);
7385 		return ret;
7386 	}
7387 	vsi->reconfig_rss = true;
7388 	dev_dbg(&vsi->back->pdev->dev,
7389 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7390 	vsi->next_base_queue = num_qps;
7391 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7392 
7393 	/* Update the VSI after updating the VSI queue-mapping
7394 	 * information
7395 	 */
7396 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7397 	if (ret) {
7398 		dev_info(&pf->pdev->dev,
7399 			 "Update vsi tc config failed, err %s aq_err %s\n",
7400 			 i40e_stat_str(hw, ret),
7401 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7402 		return ret;
7403 	}
7404 	/* update the local VSI info with updated queue map */
7405 	i40e_vsi_update_queue_map(vsi, &ctxt);
7406 	vsi->info.valid_sections = 0;
7407 
7408 	/* Create channels for macvlans */
7409 	INIT_LIST_HEAD(&vsi->macvlan_list);
7410 	for (i = 0; i < macvlan_cnt; i++) {
7411 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7412 		if (!ch) {
7413 			ret = -ENOMEM;
7414 			goto err_free;
7415 		}
7416 		INIT_LIST_HEAD(&ch->list);
7417 		ch->num_queue_pairs = qcnt;
7418 		if (!i40e_setup_channel(pf, vsi, ch)) {
7419 			ret = -EINVAL;
7420 			kfree(ch);
7421 			goto err_free;
7422 		}
7423 		ch->parent_vsi = vsi;
7424 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7425 		vsi->macvlan_cnt++;
7426 		list_add_tail(&ch->list, &vsi->macvlan_list);
7427 	}
7428 
7429 	return ret;
7430 
7431 err_free:
7432 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7433 	i40e_free_macvlan_channels(vsi);
7434 
7435 	return ret;
7436 }
7437 
7438 /**
7439  * i40e_fwd_add - configure macvlans
7440  * @netdev: net device to configure
7441  * @vdev: macvlan netdevice
7442  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7443 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7444 {
7445 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7446 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7447 	struct i40e_vsi *vsi = np->vsi;
7448 	struct i40e_pf *pf = vsi->back;
7449 	struct i40e_fwd_adapter *fwd;
7450 	int avail_macvlan, ret;
7451 
7452 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7453 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7454 		return ERR_PTR(-EINVAL);
7455 	}
7456 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7457 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7458 		return ERR_PTR(-EINVAL);
7459 	}
7460 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7461 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7462 		return ERR_PTR(-EINVAL);
7463 	}
7464 
7465 	/* The macvlan device has to be a single Q device so that the
7466 	 * tc_to_txq field can be reused to pick the tx queue.
7467 	 */
7468 	if (netif_is_multiqueue(vdev))
7469 		return ERR_PTR(-ERANGE);
7470 
7471 	if (!vsi->macvlan_cnt) {
7472 		/* reserve bit 0 for the pf device */
7473 		set_bit(0, vsi->fwd_bitmask);
7474 
7475 		/* Try to reserve as many queues as possible for macvlans. First
7476 		 * reserve 3/4th of max vectors, then half, then quarter and
7477 		 * calculate Qs per macvlan as you go
7478 		 */
7479 		vectors = pf->num_lan_msix;
7480 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7481 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7482 			q_per_macvlan = 4;
7483 			macvlan_cnt = (vectors - 32) / 4;
7484 		} else if (vectors <= 64 && vectors > 32) {
7485 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7486 			q_per_macvlan = 2;
7487 			macvlan_cnt = (vectors - 16) / 2;
7488 		} else if (vectors <= 32 && vectors > 16) {
7489 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7490 			q_per_macvlan = 1;
7491 			macvlan_cnt = vectors - 16;
7492 		} else if (vectors <= 16 && vectors > 8) {
7493 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7494 			q_per_macvlan = 1;
7495 			macvlan_cnt = vectors - 8;
7496 		} else {
7497 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7498 			q_per_macvlan = 1;
7499 			macvlan_cnt = vectors - 1;
7500 		}
7501 
7502 		if (macvlan_cnt == 0)
7503 			return ERR_PTR(-EBUSY);
7504 
7505 		/* Quiesce VSI queues */
7506 		i40e_quiesce_vsi(vsi);
7507 
7508 		/* sets up the macvlans but does not "enable" them */
7509 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7510 					  vdev);
7511 		if (ret)
7512 			return ERR_PTR(ret);
7513 
7514 		/* Unquiesce VSI */
7515 		i40e_unquiesce_vsi(vsi);
7516 	}
7517 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7518 					    vsi->macvlan_cnt);
7519 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7520 		return ERR_PTR(-EBUSY);
7521 
7522 	/* create the fwd struct */
7523 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7524 	if (!fwd)
7525 		return ERR_PTR(-ENOMEM);
7526 
7527 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7528 	fwd->bit_no = avail_macvlan;
7529 	netdev_set_sb_channel(vdev, avail_macvlan);
7530 	fwd->netdev = vdev;
7531 
7532 	if (!netif_running(netdev))
7533 		return fwd;
7534 
7535 	/* Set fwd ring up */
7536 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7537 	if (ret) {
7538 		/* unbind the queues and drop the subordinate channel config */
7539 		netdev_unbind_sb_channel(netdev, vdev);
7540 		netdev_set_sb_channel(vdev, 0);
7541 
7542 		kfree(fwd);
7543 		return ERR_PTR(-EINVAL);
7544 	}
7545 
7546 	return fwd;
7547 }
7548 
7549 /**
7550  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7551  * @vsi: the VSI we want to access
7552  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)7553 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7554 {
7555 	struct i40e_channel *ch, *ch_tmp;
7556 	struct i40e_pf *pf = vsi->back;
7557 	struct i40e_hw *hw = &pf->hw;
7558 	int aq_err, ret = 0;
7559 
7560 	if (list_empty(&vsi->macvlan_list))
7561 		return;
7562 
7563 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7564 		if (i40e_is_channel_macvlan(ch)) {
7565 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7566 						      i40e_channel_mac(ch),
7567 						      &aq_err);
7568 			if (!ret) {
7569 				/* Reset queue contexts */
7570 				i40e_reset_ch_rings(vsi, ch);
7571 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7572 				netdev_unbind_sb_channel(vsi->netdev,
7573 							 ch->fwd->netdev);
7574 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7575 				kfree(ch->fwd);
7576 				ch->fwd = NULL;
7577 			}
7578 		}
7579 	}
7580 }
7581 
7582 /**
7583  * i40e_fwd_del - delete macvlan interfaces
7584  * @netdev: net device to configure
7585  * @vdev: macvlan netdevice
7586  */
i40e_fwd_del(struct net_device * netdev,void * vdev)7587 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7588 {
7589 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7590 	struct i40e_fwd_adapter *fwd = vdev;
7591 	struct i40e_channel *ch, *ch_tmp;
7592 	struct i40e_vsi *vsi = np->vsi;
7593 	struct i40e_pf *pf = vsi->back;
7594 	struct i40e_hw *hw = &pf->hw;
7595 	int aq_err, ret = 0;
7596 
7597 	/* Find the channel associated with the macvlan and del mac filter */
7598 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7599 		if (i40e_is_channel_macvlan(ch) &&
7600 		    ether_addr_equal(i40e_channel_mac(ch),
7601 				     fwd->netdev->dev_addr)) {
7602 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7603 						      i40e_channel_mac(ch),
7604 						      &aq_err);
7605 			if (!ret) {
7606 				/* Reset queue contexts */
7607 				i40e_reset_ch_rings(vsi, ch);
7608 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7609 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7610 				netdev_set_sb_channel(fwd->netdev, 0);
7611 				kfree(ch->fwd);
7612 				ch->fwd = NULL;
7613 			} else {
7614 				dev_info(&pf->pdev->dev,
7615 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7616 					  i40e_stat_str(hw, ret),
7617 					  i40e_aq_str(hw, aq_err));
7618 			}
7619 			break;
7620 		}
7621 	}
7622 }
7623 
7624 /**
7625  * i40e_setup_tc - configure multiple traffic classes
7626  * @netdev: net device to configure
7627  * @type_data: tc offload data
7628  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)7629 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7630 {
7631 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7632 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7633 	struct i40e_vsi *vsi = np->vsi;
7634 	struct i40e_pf *pf = vsi->back;
7635 	u8 enabled_tc = 0, num_tc, hw;
7636 	bool need_reset = false;
7637 	int old_queue_pairs;
7638 	int ret = -EINVAL;
7639 	u16 mode;
7640 	int i;
7641 
7642 	old_queue_pairs = vsi->num_queue_pairs;
7643 	num_tc = mqprio_qopt->qopt.num_tc;
7644 	hw = mqprio_qopt->qopt.hw;
7645 	mode = mqprio_qopt->mode;
7646 	if (!hw) {
7647 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7648 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7649 		goto config_tc;
7650 	}
7651 
7652 	/* Check if MFP enabled */
7653 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7654 		netdev_info(netdev,
7655 			    "Configuring TC not supported in MFP mode\n");
7656 		return ret;
7657 	}
7658 	switch (mode) {
7659 	case TC_MQPRIO_MODE_DCB:
7660 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7661 
7662 		/* Check if DCB enabled to continue */
7663 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7664 			netdev_info(netdev,
7665 				    "DCB is not enabled for adapter\n");
7666 			return ret;
7667 		}
7668 
7669 		/* Check whether tc count is within enabled limit */
7670 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7671 			netdev_info(netdev,
7672 				    "TC count greater than enabled on link for adapter\n");
7673 			return ret;
7674 		}
7675 		break;
7676 	case TC_MQPRIO_MODE_CHANNEL:
7677 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7678 			netdev_info(netdev,
7679 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7680 			return ret;
7681 		}
7682 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7683 			return ret;
7684 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7685 		if (ret)
7686 			return ret;
7687 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7688 		       sizeof(*mqprio_qopt));
7689 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7690 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7691 		break;
7692 	default:
7693 		return -EINVAL;
7694 	}
7695 
7696 config_tc:
7697 	/* Generate TC map for number of tc requested */
7698 	for (i = 0; i < num_tc; i++)
7699 		enabled_tc |= BIT(i);
7700 
7701 	/* Requesting same TC configuration as already enabled */
7702 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7703 	    mode != TC_MQPRIO_MODE_CHANNEL)
7704 		return 0;
7705 
7706 	/* Quiesce VSI queues */
7707 	i40e_quiesce_vsi(vsi);
7708 
7709 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7710 		i40e_remove_queue_channels(vsi);
7711 
7712 	/* Configure VSI for enabled TCs */
7713 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7714 	if (ret) {
7715 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7716 			    vsi->seid);
7717 		need_reset = true;
7718 		goto exit;
7719 	} else if (enabled_tc &&
7720 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
7721 		netdev_info(netdev,
7722 			    "Failed to create channel. Override queues (%u) not power of 2\n",
7723 			    vsi->tc_config.tc_info[0].qcount);
7724 		ret = -EINVAL;
7725 		need_reset = true;
7726 		goto exit;
7727 	}
7728 
7729 	dev_info(&vsi->back->pdev->dev,
7730 		 "Setup channel (id:%u) utilizing num_queues %d\n",
7731 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7732 
7733 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7734 		if (vsi->mqprio_qopt.max_rate[0]) {
7735 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
7736 						  vsi->mqprio_qopt.max_rate[0]);
7737 
7738 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7739 			if (!ret) {
7740 				u64 credits = max_tx_rate;
7741 
7742 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7743 				dev_dbg(&vsi->back->pdev->dev,
7744 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7745 					max_tx_rate,
7746 					credits,
7747 					vsi->seid);
7748 			} else {
7749 				need_reset = true;
7750 				goto exit;
7751 			}
7752 		}
7753 		ret = i40e_configure_queue_channels(vsi);
7754 		if (ret) {
7755 			vsi->num_queue_pairs = old_queue_pairs;
7756 			netdev_info(netdev,
7757 				    "Failed configuring queue channels\n");
7758 			need_reset = true;
7759 			goto exit;
7760 		}
7761 	}
7762 
7763 exit:
7764 	/* Reset the configuration data to defaults, only TC0 is enabled */
7765 	if (need_reset) {
7766 		i40e_vsi_set_default_tc_config(vsi);
7767 		need_reset = false;
7768 	}
7769 
7770 	/* Unquiesce VSI */
7771 	i40e_unquiesce_vsi(vsi);
7772 	return ret;
7773 }
7774 
7775 /**
7776  * i40e_set_cld_element - sets cloud filter element data
7777  * @filter: cloud filter rule
7778  * @cld: ptr to cloud filter element data
7779  *
7780  * This is helper function to copy data into cloud filter element
7781  **/
7782 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)7783 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7784 		     struct i40e_aqc_cloud_filters_element_data *cld)
7785 {
7786 	u32 ipa;
7787 	int i;
7788 
7789 	memset(cld, 0, sizeof(*cld));
7790 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7791 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7792 
7793 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7794 		return;
7795 
7796 	if (filter->n_proto == ETH_P_IPV6) {
7797 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7798 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
7799 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7800 
7801 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
7802 		}
7803 	} else {
7804 		ipa = be32_to_cpu(filter->dst_ipv4);
7805 
7806 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7807 	}
7808 
7809 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7810 
7811 	/* tenant_id is not supported by FW now, once the support is enabled
7812 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7813 	 */
7814 	if (filter->tenant_id)
7815 		return;
7816 }
7817 
7818 /**
7819  * i40e_add_del_cloud_filter - Add/del cloud filter
7820  * @vsi: pointer to VSI
7821  * @filter: cloud filter rule
7822  * @add: if true, add, if false, delete
7823  *
7824  * Add or delete a cloud filter for a specific flow spec.
7825  * Returns 0 if the filter were successfully added.
7826  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)7827 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7828 			      struct i40e_cloud_filter *filter, bool add)
7829 {
7830 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7831 	struct i40e_pf *pf = vsi->back;
7832 	int ret;
7833 	static const u16 flag_table[128] = {
7834 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7835 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7836 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7837 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7838 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7839 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7840 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7841 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7842 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7843 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7844 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7845 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7846 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7847 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7848 	};
7849 
7850 	if (filter->flags >= ARRAY_SIZE(flag_table))
7851 		return I40E_ERR_CONFIG;
7852 
7853 	memset(&cld_filter, 0, sizeof(cld_filter));
7854 
7855 	/* copy element needed to add cloud filter from filter */
7856 	i40e_set_cld_element(filter, &cld_filter);
7857 
7858 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7859 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7860 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7861 
7862 	if (filter->n_proto == ETH_P_IPV6)
7863 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7864 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7865 	else
7866 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7867 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7868 
7869 	if (add)
7870 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7871 						&cld_filter, 1);
7872 	else
7873 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7874 						&cld_filter, 1);
7875 	if (ret)
7876 		dev_dbg(&pf->pdev->dev,
7877 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7878 			add ? "add" : "delete", filter->dst_port, ret,
7879 			pf->hw.aq.asq_last_status);
7880 	else
7881 		dev_info(&pf->pdev->dev,
7882 			 "%s cloud filter for VSI: %d\n",
7883 			 add ? "Added" : "Deleted", filter->seid);
7884 	return ret;
7885 }
7886 
7887 /**
7888  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7889  * @vsi: pointer to VSI
7890  * @filter: cloud filter rule
7891  * @add: if true, add, if false, delete
7892  *
7893  * Add or delete a cloud filter for a specific flow spec using big buffer.
7894  * Returns 0 if the filter were successfully added.
7895  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)7896 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7897 				      struct i40e_cloud_filter *filter,
7898 				      bool add)
7899 {
7900 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7901 	struct i40e_pf *pf = vsi->back;
7902 	int ret;
7903 
7904 	/* Both (src/dst) valid mac_addr are not supported */
7905 	if ((is_valid_ether_addr(filter->dst_mac) &&
7906 	     is_valid_ether_addr(filter->src_mac)) ||
7907 	    (is_multicast_ether_addr(filter->dst_mac) &&
7908 	     is_multicast_ether_addr(filter->src_mac)))
7909 		return -EOPNOTSUPP;
7910 
7911 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7912 	 * ports are not supported via big buffer now.
7913 	 */
7914 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7915 		return -EOPNOTSUPP;
7916 
7917 	/* adding filter using src_port/src_ip is not supported at this stage */
7918 	if (filter->src_port ||
7919 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
7920 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7921 		return -EOPNOTSUPP;
7922 
7923 	memset(&cld_filter, 0, sizeof(cld_filter));
7924 
7925 	/* copy element needed to add cloud filter from filter */
7926 	i40e_set_cld_element(filter, &cld_filter.element);
7927 
7928 	if (is_valid_ether_addr(filter->dst_mac) ||
7929 	    is_valid_ether_addr(filter->src_mac) ||
7930 	    is_multicast_ether_addr(filter->dst_mac) ||
7931 	    is_multicast_ether_addr(filter->src_mac)) {
7932 		/* MAC + IP : unsupported mode */
7933 		if (filter->dst_ipv4)
7934 			return -EOPNOTSUPP;
7935 
7936 		/* since we validated that L4 port must be valid before
7937 		 * we get here, start with respective "flags" value
7938 		 * and update if vlan is present or not
7939 		 */
7940 		cld_filter.element.flags =
7941 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7942 
7943 		if (filter->vlan_id) {
7944 			cld_filter.element.flags =
7945 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7946 		}
7947 
7948 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
7949 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7950 		cld_filter.element.flags =
7951 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7952 		if (filter->n_proto == ETH_P_IPV6)
7953 			cld_filter.element.flags |=
7954 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7955 		else
7956 			cld_filter.element.flags |=
7957 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7958 	} else {
7959 		dev_err(&pf->pdev->dev,
7960 			"either mac or ip has to be valid for cloud filter\n");
7961 		return -EINVAL;
7962 	}
7963 
7964 	/* Now copy L4 port in Byte 6..7 in general fields */
7965 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7966 						be16_to_cpu(filter->dst_port);
7967 
7968 	if (add) {
7969 		/* Validate current device switch mode, change if necessary */
7970 		ret = i40e_validate_and_set_switch_mode(vsi);
7971 		if (ret) {
7972 			dev_err(&pf->pdev->dev,
7973 				"failed to set switch mode, ret %d\n",
7974 				ret);
7975 			return ret;
7976 		}
7977 
7978 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7979 						   &cld_filter, 1);
7980 	} else {
7981 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7982 						   &cld_filter, 1);
7983 	}
7984 
7985 	if (ret)
7986 		dev_dbg(&pf->pdev->dev,
7987 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7988 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7989 	else
7990 		dev_info(&pf->pdev->dev,
7991 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7992 			 add ? "add" : "delete", filter->seid,
7993 			 ntohs(filter->dst_port));
7994 	return ret;
7995 }
7996 
7997 /**
7998  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7999  * @vsi: Pointer to VSI
8000  * @f: Pointer to struct flow_cls_offload
8001  * @filter: Pointer to cloud filter structure
8002  *
8003  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8004 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8005 				 struct flow_cls_offload *f,
8006 				 struct i40e_cloud_filter *filter)
8007 {
8008 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8009 	struct flow_dissector *dissector = rule->match.dissector;
8010 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8011 	struct i40e_pf *pf = vsi->back;
8012 	u8 field_flags = 0;
8013 
8014 	if (dissector->used_keys &
8015 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8016 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8017 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8018 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8019 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8020 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8021 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8022 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8023 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8024 			dissector->used_keys);
8025 		return -EOPNOTSUPP;
8026 	}
8027 
8028 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8029 		struct flow_match_enc_keyid match;
8030 
8031 		flow_rule_match_enc_keyid(rule, &match);
8032 		if (match.mask->keyid != 0)
8033 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8034 
8035 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8036 	}
8037 
8038 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8039 		struct flow_match_basic match;
8040 
8041 		flow_rule_match_basic(rule, &match);
8042 		n_proto_key = ntohs(match.key->n_proto);
8043 		n_proto_mask = ntohs(match.mask->n_proto);
8044 
8045 		if (n_proto_key == ETH_P_ALL) {
8046 			n_proto_key = 0;
8047 			n_proto_mask = 0;
8048 		}
8049 		filter->n_proto = n_proto_key & n_proto_mask;
8050 		filter->ip_proto = match.key->ip_proto;
8051 	}
8052 
8053 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8054 		struct flow_match_eth_addrs match;
8055 
8056 		flow_rule_match_eth_addrs(rule, &match);
8057 
8058 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8059 		if (!is_zero_ether_addr(match.mask->dst)) {
8060 			if (is_broadcast_ether_addr(match.mask->dst)) {
8061 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8062 			} else {
8063 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8064 					match.mask->dst);
8065 				return I40E_ERR_CONFIG;
8066 			}
8067 		}
8068 
8069 		if (!is_zero_ether_addr(match.mask->src)) {
8070 			if (is_broadcast_ether_addr(match.mask->src)) {
8071 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8072 			} else {
8073 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8074 					match.mask->src);
8075 				return I40E_ERR_CONFIG;
8076 			}
8077 		}
8078 		ether_addr_copy(filter->dst_mac, match.key->dst);
8079 		ether_addr_copy(filter->src_mac, match.key->src);
8080 	}
8081 
8082 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8083 		struct flow_match_vlan match;
8084 
8085 		flow_rule_match_vlan(rule, &match);
8086 		if (match.mask->vlan_id) {
8087 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8088 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8089 
8090 			} else {
8091 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8092 					match.mask->vlan_id);
8093 				return I40E_ERR_CONFIG;
8094 			}
8095 		}
8096 
8097 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8098 	}
8099 
8100 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8101 		struct flow_match_control match;
8102 
8103 		flow_rule_match_control(rule, &match);
8104 		addr_type = match.key->addr_type;
8105 	}
8106 
8107 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8108 		struct flow_match_ipv4_addrs match;
8109 
8110 		flow_rule_match_ipv4_addrs(rule, &match);
8111 		if (match.mask->dst) {
8112 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8113 				field_flags |= I40E_CLOUD_FIELD_IIP;
8114 			} else {
8115 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8116 					&match.mask->dst);
8117 				return I40E_ERR_CONFIG;
8118 			}
8119 		}
8120 
8121 		if (match.mask->src) {
8122 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8123 				field_flags |= I40E_CLOUD_FIELD_IIP;
8124 			} else {
8125 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8126 					&match.mask->src);
8127 				return I40E_ERR_CONFIG;
8128 			}
8129 		}
8130 
8131 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8132 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8133 			return I40E_ERR_CONFIG;
8134 		}
8135 		filter->dst_ipv4 = match.key->dst;
8136 		filter->src_ipv4 = match.key->src;
8137 	}
8138 
8139 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8140 		struct flow_match_ipv6_addrs match;
8141 
8142 		flow_rule_match_ipv6_addrs(rule, &match);
8143 
8144 		/* src and dest IPV6 address should not be LOOPBACK
8145 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8146 		 */
8147 		if (ipv6_addr_loopback(&match.key->dst) ||
8148 		    ipv6_addr_loopback(&match.key->src)) {
8149 			dev_err(&pf->pdev->dev,
8150 				"Bad ipv6, addr is LOOPBACK\n");
8151 			return I40E_ERR_CONFIG;
8152 		}
8153 		if (!ipv6_addr_any(&match.mask->dst) ||
8154 		    !ipv6_addr_any(&match.mask->src))
8155 			field_flags |= I40E_CLOUD_FIELD_IIP;
8156 
8157 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8158 		       sizeof(filter->src_ipv6));
8159 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8160 		       sizeof(filter->dst_ipv6));
8161 	}
8162 
8163 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8164 		struct flow_match_ports match;
8165 
8166 		flow_rule_match_ports(rule, &match);
8167 		if (match.mask->src) {
8168 			if (match.mask->src == cpu_to_be16(0xffff)) {
8169 				field_flags |= I40E_CLOUD_FIELD_IIP;
8170 			} else {
8171 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8172 					be16_to_cpu(match.mask->src));
8173 				return I40E_ERR_CONFIG;
8174 			}
8175 		}
8176 
8177 		if (match.mask->dst) {
8178 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8179 				field_flags |= I40E_CLOUD_FIELD_IIP;
8180 			} else {
8181 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8182 					be16_to_cpu(match.mask->dst));
8183 				return I40E_ERR_CONFIG;
8184 			}
8185 		}
8186 
8187 		filter->dst_port = match.key->dst;
8188 		filter->src_port = match.key->src;
8189 
8190 		switch (filter->ip_proto) {
8191 		case IPPROTO_TCP:
8192 		case IPPROTO_UDP:
8193 			break;
8194 		default:
8195 			dev_err(&pf->pdev->dev,
8196 				"Only UDP and TCP transport are supported\n");
8197 			return -EINVAL;
8198 		}
8199 	}
8200 	filter->flags = field_flags;
8201 	return 0;
8202 }
8203 
8204 /**
8205  * i40e_handle_tclass: Forward to a traffic class on the device
8206  * @vsi: Pointer to VSI
8207  * @tc: traffic class index on the device
8208  * @filter: Pointer to cloud filter structure
8209  *
8210  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8211 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8212 			      struct i40e_cloud_filter *filter)
8213 {
8214 	struct i40e_channel *ch, *ch_tmp;
8215 
8216 	/* direct to a traffic class on the same device */
8217 	if (tc == 0) {
8218 		filter->seid = vsi->seid;
8219 		return 0;
8220 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8221 		if (!filter->dst_port) {
8222 			dev_err(&vsi->back->pdev->dev,
8223 				"Specify destination port to direct to traffic class that is not default\n");
8224 			return -EINVAL;
8225 		}
8226 		if (list_empty(&vsi->ch_list))
8227 			return -EINVAL;
8228 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8229 					 list) {
8230 			if (ch->seid == vsi->tc_seid_map[tc])
8231 				filter->seid = ch->seid;
8232 		}
8233 		return 0;
8234 	}
8235 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8236 	return -EINVAL;
8237 }
8238 
8239 /**
8240  * i40e_configure_clsflower - Configure tc flower filters
8241  * @vsi: Pointer to VSI
8242  * @cls_flower: Pointer to struct flow_cls_offload
8243  *
8244  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8245 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8246 				    struct flow_cls_offload *cls_flower)
8247 {
8248 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8249 	struct i40e_cloud_filter *filter = NULL;
8250 	struct i40e_pf *pf = vsi->back;
8251 	int err = 0;
8252 
8253 	if (tc < 0) {
8254 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8255 		return -EOPNOTSUPP;
8256 	}
8257 
8258 	if (!tc) {
8259 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8260 		return -EINVAL;
8261 	}
8262 
8263 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8264 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8265 		return -EBUSY;
8266 
8267 	if (pf->fdir_pf_active_filters ||
8268 	    (!hlist_empty(&pf->fdir_filter_list))) {
8269 		dev_err(&vsi->back->pdev->dev,
8270 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8271 		return -EINVAL;
8272 	}
8273 
8274 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8275 		dev_err(&vsi->back->pdev->dev,
8276 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8277 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8278 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8279 	}
8280 
8281 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8282 	if (!filter)
8283 		return -ENOMEM;
8284 
8285 	filter->cookie = cls_flower->cookie;
8286 
8287 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8288 	if (err < 0)
8289 		goto err;
8290 
8291 	err = i40e_handle_tclass(vsi, tc, filter);
8292 	if (err < 0)
8293 		goto err;
8294 
8295 	/* Add cloud filter */
8296 	if (filter->dst_port)
8297 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8298 	else
8299 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8300 
8301 	if (err) {
8302 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8303 			err);
8304 		goto err;
8305 	}
8306 
8307 	/* add filter to the ordered list */
8308 	INIT_HLIST_NODE(&filter->cloud_node);
8309 
8310 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8311 
8312 	pf->num_cloud_filters++;
8313 
8314 	return err;
8315 err:
8316 	kfree(filter);
8317 	return err;
8318 }
8319 
8320 /**
8321  * i40e_find_cloud_filter - Find the could filter in the list
8322  * @vsi: Pointer to VSI
8323  * @cookie: filter specific cookie
8324  *
8325  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8326 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8327 							unsigned long *cookie)
8328 {
8329 	struct i40e_cloud_filter *filter = NULL;
8330 	struct hlist_node *node2;
8331 
8332 	hlist_for_each_entry_safe(filter, node2,
8333 				  &vsi->back->cloud_filter_list, cloud_node)
8334 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8335 			return filter;
8336 	return NULL;
8337 }
8338 
8339 /**
8340  * i40e_delete_clsflower - Remove tc flower filters
8341  * @vsi: Pointer to VSI
8342  * @cls_flower: Pointer to struct flow_cls_offload
8343  *
8344  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8345 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8346 				 struct flow_cls_offload *cls_flower)
8347 {
8348 	struct i40e_cloud_filter *filter = NULL;
8349 	struct i40e_pf *pf = vsi->back;
8350 	int err = 0;
8351 
8352 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8353 
8354 	if (!filter)
8355 		return -EINVAL;
8356 
8357 	hash_del(&filter->cloud_node);
8358 
8359 	if (filter->dst_port)
8360 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8361 	else
8362 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8363 
8364 	kfree(filter);
8365 	if (err) {
8366 		dev_err(&pf->pdev->dev,
8367 			"Failed to delete cloud filter, err %s\n",
8368 			i40e_stat_str(&pf->hw, err));
8369 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8370 	}
8371 
8372 	pf->num_cloud_filters--;
8373 	if (!pf->num_cloud_filters)
8374 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8375 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8376 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8377 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8378 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8379 		}
8380 	return 0;
8381 }
8382 
8383 /**
8384  * i40e_setup_tc_cls_flower - flower classifier offloads
8385  * @np: net device to configure
8386  * @cls_flower: offload data
8387  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8388 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8389 				    struct flow_cls_offload *cls_flower)
8390 {
8391 	struct i40e_vsi *vsi = np->vsi;
8392 
8393 	switch (cls_flower->command) {
8394 	case FLOW_CLS_REPLACE:
8395 		return i40e_configure_clsflower(vsi, cls_flower);
8396 	case FLOW_CLS_DESTROY:
8397 		return i40e_delete_clsflower(vsi, cls_flower);
8398 	case FLOW_CLS_STATS:
8399 		return -EOPNOTSUPP;
8400 	default:
8401 		return -EOPNOTSUPP;
8402 	}
8403 }
8404 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8405 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8406 				  void *cb_priv)
8407 {
8408 	struct i40e_netdev_priv *np = cb_priv;
8409 
8410 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8411 		return -EOPNOTSUPP;
8412 
8413 	switch (type) {
8414 	case TC_SETUP_CLSFLOWER:
8415 		return i40e_setup_tc_cls_flower(np, type_data);
8416 
8417 	default:
8418 		return -EOPNOTSUPP;
8419 	}
8420 }
8421 
8422 static LIST_HEAD(i40e_block_cb_list);
8423 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8424 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8425 			   void *type_data)
8426 {
8427 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8428 
8429 	switch (type) {
8430 	case TC_SETUP_QDISC_MQPRIO:
8431 		return i40e_setup_tc(netdev, type_data);
8432 	case TC_SETUP_BLOCK:
8433 		return flow_block_cb_setup_simple(type_data,
8434 						  &i40e_block_cb_list,
8435 						  i40e_setup_tc_block_cb,
8436 						  np, np, true);
8437 	default:
8438 		return -EOPNOTSUPP;
8439 	}
8440 }
8441 
8442 /**
8443  * i40e_open - Called when a network interface is made active
8444  * @netdev: network interface device structure
8445  *
8446  * The open entry point is called when a network interface is made
8447  * active by the system (IFF_UP).  At this point all resources needed
8448  * for transmit and receive operations are allocated, the interrupt
8449  * handler is registered with the OS, the netdev watchdog subtask is
8450  * enabled, and the stack is notified that the interface is ready.
8451  *
8452  * Returns 0 on success, negative value on failure
8453  **/
i40e_open(struct net_device * netdev)8454 int i40e_open(struct net_device *netdev)
8455 {
8456 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8457 	struct i40e_vsi *vsi = np->vsi;
8458 	struct i40e_pf *pf = vsi->back;
8459 	int err;
8460 
8461 	/* disallow open during test or if eeprom is broken */
8462 	if (test_bit(__I40E_TESTING, pf->state) ||
8463 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8464 		return -EBUSY;
8465 
8466 	netif_carrier_off(netdev);
8467 
8468 	if (i40e_force_link_state(pf, true))
8469 		return -EAGAIN;
8470 
8471 	err = i40e_vsi_open(vsi);
8472 	if (err)
8473 		return err;
8474 
8475 	/* configure global TSO hardware offload settings */
8476 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8477 						       TCP_FLAG_FIN) >> 16);
8478 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8479 						       TCP_FLAG_FIN |
8480 						       TCP_FLAG_CWR) >> 16);
8481 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8482 
8483 	udp_tunnel_get_rx_info(netdev);
8484 
8485 	return 0;
8486 }
8487 
8488 /**
8489  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8490  * @vsi: vsi structure
8491  *
8492  * This updates netdev's number of tx/rx queues
8493  *
8494  * Returns status of setting tx/rx queues
8495  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)8496 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8497 {
8498 	int ret;
8499 
8500 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8501 					   vsi->num_queue_pairs);
8502 	if (ret)
8503 		return ret;
8504 
8505 	return netif_set_real_num_tx_queues(vsi->netdev,
8506 					    vsi->num_queue_pairs);
8507 }
8508 
8509 /**
8510  * i40e_vsi_open -
8511  * @vsi: the VSI to open
8512  *
8513  * Finish initialization of the VSI.
8514  *
8515  * Returns 0 on success, negative value on failure
8516  *
8517  * Note: expects to be called while under rtnl_lock()
8518  **/
i40e_vsi_open(struct i40e_vsi * vsi)8519 int i40e_vsi_open(struct i40e_vsi *vsi)
8520 {
8521 	struct i40e_pf *pf = vsi->back;
8522 	char int_name[I40E_INT_NAME_STR_LEN];
8523 	int err;
8524 
8525 	/* allocate descriptors */
8526 	err = i40e_vsi_setup_tx_resources(vsi);
8527 	if (err)
8528 		goto err_setup_tx;
8529 	err = i40e_vsi_setup_rx_resources(vsi);
8530 	if (err)
8531 		goto err_setup_rx;
8532 
8533 	err = i40e_vsi_configure(vsi);
8534 	if (err)
8535 		goto err_setup_rx;
8536 
8537 	if (vsi->netdev) {
8538 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8539 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8540 		err = i40e_vsi_request_irq(vsi, int_name);
8541 		if (err)
8542 			goto err_setup_rx;
8543 
8544 		/* Notify the stack of the actual queue counts. */
8545 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8546 		if (err)
8547 			goto err_set_queues;
8548 
8549 	} else if (vsi->type == I40E_VSI_FDIR) {
8550 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8551 			 dev_driver_string(&pf->pdev->dev),
8552 			 dev_name(&pf->pdev->dev));
8553 		err = i40e_vsi_request_irq(vsi, int_name);
8554 		if (err)
8555 			goto err_setup_rx;
8556 
8557 	} else {
8558 		err = -EINVAL;
8559 		goto err_setup_rx;
8560 	}
8561 
8562 	err = i40e_up_complete(vsi);
8563 	if (err)
8564 		goto err_up_complete;
8565 
8566 	return 0;
8567 
8568 err_up_complete:
8569 	i40e_down(vsi);
8570 err_set_queues:
8571 	i40e_vsi_free_irq(vsi);
8572 err_setup_rx:
8573 	i40e_vsi_free_rx_resources(vsi);
8574 err_setup_tx:
8575 	i40e_vsi_free_tx_resources(vsi);
8576 	if (vsi == pf->vsi[pf->lan_vsi])
8577 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8578 
8579 	return err;
8580 }
8581 
8582 /**
8583  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8584  * @pf: Pointer to PF
8585  *
8586  * This function destroys the hlist where all the Flow Director
8587  * filters were saved.
8588  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)8589 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8590 {
8591 	struct i40e_fdir_filter *filter;
8592 	struct i40e_flex_pit *pit_entry, *tmp;
8593 	struct hlist_node *node2;
8594 
8595 	hlist_for_each_entry_safe(filter, node2,
8596 				  &pf->fdir_filter_list, fdir_node) {
8597 		hlist_del(&filter->fdir_node);
8598 		kfree(filter);
8599 	}
8600 
8601 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8602 		list_del(&pit_entry->list);
8603 		kfree(pit_entry);
8604 	}
8605 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8606 
8607 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8608 		list_del(&pit_entry->list);
8609 		kfree(pit_entry);
8610 	}
8611 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8612 
8613 	pf->fdir_pf_active_filters = 0;
8614 	pf->fd_tcp4_filter_cnt = 0;
8615 	pf->fd_udp4_filter_cnt = 0;
8616 	pf->fd_sctp4_filter_cnt = 0;
8617 	pf->fd_ip4_filter_cnt = 0;
8618 
8619 	/* Reprogram the default input set for TCP/IPv4 */
8620 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8621 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8622 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8623 
8624 	/* Reprogram the default input set for UDP/IPv4 */
8625 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8626 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8627 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8628 
8629 	/* Reprogram the default input set for SCTP/IPv4 */
8630 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8631 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8632 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8633 
8634 	/* Reprogram the default input set for Other/IPv4 */
8635 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8636 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8637 
8638 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8639 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8640 }
8641 
8642 /**
8643  * i40e_cloud_filter_exit - Cleans up the cloud filters
8644  * @pf: Pointer to PF
8645  *
8646  * This function destroys the hlist where all the cloud filters
8647  * were saved.
8648  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)8649 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8650 {
8651 	struct i40e_cloud_filter *cfilter;
8652 	struct hlist_node *node;
8653 
8654 	hlist_for_each_entry_safe(cfilter, node,
8655 				  &pf->cloud_filter_list, cloud_node) {
8656 		hlist_del(&cfilter->cloud_node);
8657 		kfree(cfilter);
8658 	}
8659 	pf->num_cloud_filters = 0;
8660 
8661 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8662 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8663 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8664 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8665 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8666 	}
8667 }
8668 
8669 /**
8670  * i40e_close - Disables a network interface
8671  * @netdev: network interface device structure
8672  *
8673  * The close entry point is called when an interface is de-activated
8674  * by the OS.  The hardware is still under the driver's control, but
8675  * this netdev interface is disabled.
8676  *
8677  * Returns 0, this is not allowed to fail
8678  **/
i40e_close(struct net_device * netdev)8679 int i40e_close(struct net_device *netdev)
8680 {
8681 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8682 	struct i40e_vsi *vsi = np->vsi;
8683 
8684 	i40e_vsi_close(vsi);
8685 
8686 	return 0;
8687 }
8688 
8689 /**
8690  * i40e_do_reset - Start a PF or Core Reset sequence
8691  * @pf: board private structure
8692  * @reset_flags: which reset is requested
8693  * @lock_acquired: indicates whether or not the lock has been acquired
8694  * before this function was called.
8695  *
8696  * The essential difference in resets is that the PF Reset
8697  * doesn't clear the packet buffers, doesn't reset the PE
8698  * firmware, and doesn't bother the other PFs on the chip.
8699  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)8700 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8701 {
8702 	u32 val;
8703 
8704 	/* do the biggest reset indicated */
8705 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8706 
8707 		/* Request a Global Reset
8708 		 *
8709 		 * This will start the chip's countdown to the actual full
8710 		 * chip reset event, and a warning interrupt to be sent
8711 		 * to all PFs, including the requestor.  Our handler
8712 		 * for the warning interrupt will deal with the shutdown
8713 		 * and recovery of the switch setup.
8714 		 */
8715 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8716 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8717 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8718 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8719 
8720 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8721 
8722 		/* Request a Core Reset
8723 		 *
8724 		 * Same as Global Reset, except does *not* include the MAC/PHY
8725 		 */
8726 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8727 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8728 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8729 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8730 		i40e_flush(&pf->hw);
8731 
8732 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8733 
8734 		/* Request a PF Reset
8735 		 *
8736 		 * Resets only the PF-specific registers
8737 		 *
8738 		 * This goes directly to the tear-down and rebuild of
8739 		 * the switch, since we need to do all the recovery as
8740 		 * for the Core Reset.
8741 		 */
8742 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8743 		i40e_handle_reset_warning(pf, lock_acquired);
8744 
8745 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
8746 		/* Request a PF Reset
8747 		 *
8748 		 * Resets PF and reinitializes PFs VSI.
8749 		 */
8750 		i40e_prep_for_reset(pf, lock_acquired);
8751 		i40e_reset_and_rebuild(pf, true, lock_acquired);
8752 		dev_info(&pf->pdev->dev,
8753 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8754 			 "FW LLDP is disabled\n" :
8755 			 "FW LLDP is enabled\n");
8756 
8757 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8758 		int v;
8759 
8760 		/* Find the VSI(s) that requested a re-init */
8761 		dev_info(&pf->pdev->dev,
8762 			 "VSI reinit requested\n");
8763 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8764 			struct i40e_vsi *vsi = pf->vsi[v];
8765 
8766 			if (vsi != NULL &&
8767 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8768 					       vsi->state))
8769 				i40e_vsi_reinit_locked(pf->vsi[v]);
8770 		}
8771 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8772 		int v;
8773 
8774 		/* Find the VSI(s) that needs to be brought down */
8775 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8776 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8777 			struct i40e_vsi *vsi = pf->vsi[v];
8778 
8779 			if (vsi != NULL &&
8780 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8781 					       vsi->state)) {
8782 				set_bit(__I40E_VSI_DOWN, vsi->state);
8783 				i40e_down(vsi);
8784 			}
8785 		}
8786 	} else {
8787 		dev_info(&pf->pdev->dev,
8788 			 "bad reset request 0x%08x\n", reset_flags);
8789 	}
8790 }
8791 
8792 #ifdef CONFIG_I40E_DCB
8793 /**
8794  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8795  * @pf: board private structure
8796  * @old_cfg: current DCB config
8797  * @new_cfg: new DCB config
8798  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)8799 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8800 			    struct i40e_dcbx_config *old_cfg,
8801 			    struct i40e_dcbx_config *new_cfg)
8802 {
8803 	bool need_reconfig = false;
8804 
8805 	/* Check if ETS configuration has changed */
8806 	if (memcmp(&new_cfg->etscfg,
8807 		   &old_cfg->etscfg,
8808 		   sizeof(new_cfg->etscfg))) {
8809 		/* If Priority Table has changed reconfig is needed */
8810 		if (memcmp(&new_cfg->etscfg.prioritytable,
8811 			   &old_cfg->etscfg.prioritytable,
8812 			   sizeof(new_cfg->etscfg.prioritytable))) {
8813 			need_reconfig = true;
8814 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8815 		}
8816 
8817 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8818 			   &old_cfg->etscfg.tcbwtable,
8819 			   sizeof(new_cfg->etscfg.tcbwtable)))
8820 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8821 
8822 		if (memcmp(&new_cfg->etscfg.tsatable,
8823 			   &old_cfg->etscfg.tsatable,
8824 			   sizeof(new_cfg->etscfg.tsatable)))
8825 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8826 	}
8827 
8828 	/* Check if PFC configuration has changed */
8829 	if (memcmp(&new_cfg->pfc,
8830 		   &old_cfg->pfc,
8831 		   sizeof(new_cfg->pfc))) {
8832 		need_reconfig = true;
8833 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8834 	}
8835 
8836 	/* Check if APP Table has changed */
8837 	if (memcmp(&new_cfg->app,
8838 		   &old_cfg->app,
8839 		   sizeof(new_cfg->app))) {
8840 		need_reconfig = true;
8841 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8842 	}
8843 
8844 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8845 	return need_reconfig;
8846 }
8847 
8848 /**
8849  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8850  * @pf: board private structure
8851  * @e: event info posted on ARQ
8852  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)8853 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8854 				  struct i40e_arq_event_info *e)
8855 {
8856 	struct i40e_aqc_lldp_get_mib *mib =
8857 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8858 	struct i40e_hw *hw = &pf->hw;
8859 	struct i40e_dcbx_config tmp_dcbx_cfg;
8860 	bool need_reconfig = false;
8861 	int ret = 0;
8862 	u8 type;
8863 
8864 	/* Not DCB capable or capability disabled */
8865 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8866 		return ret;
8867 
8868 	/* Ignore if event is not for Nearest Bridge */
8869 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8870 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8871 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8872 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8873 		return ret;
8874 
8875 	/* Check MIB Type and return if event for Remote MIB update */
8876 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8877 	dev_dbg(&pf->pdev->dev,
8878 		"LLDP event mib type %s\n", type ? "remote" : "local");
8879 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8880 		/* Update the remote cached instance and return */
8881 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8882 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8883 				&hw->remote_dcbx_config);
8884 		goto exit;
8885 	}
8886 
8887 	/* Store the old configuration */
8888 	tmp_dcbx_cfg = hw->local_dcbx_config;
8889 
8890 	/* Reset the old DCBx configuration data */
8891 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8892 	/* Get updated DCBX data from firmware */
8893 	ret = i40e_get_dcb_config(&pf->hw);
8894 	if (ret) {
8895 		dev_info(&pf->pdev->dev,
8896 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8897 			 i40e_stat_str(&pf->hw, ret),
8898 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8899 		goto exit;
8900 	}
8901 
8902 	/* No change detected in DCBX configs */
8903 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8904 		    sizeof(tmp_dcbx_cfg))) {
8905 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8906 		goto exit;
8907 	}
8908 
8909 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8910 					       &hw->local_dcbx_config);
8911 
8912 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8913 
8914 	if (!need_reconfig)
8915 		goto exit;
8916 
8917 	/* Enable DCB tagging only when more than one TC */
8918 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8919 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8920 	else
8921 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8922 
8923 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8924 	/* Reconfiguration needed quiesce all VSIs */
8925 	i40e_pf_quiesce_all_vsi(pf);
8926 
8927 	/* Changes in configuration update VEB/VSI */
8928 	i40e_dcb_reconfigure(pf);
8929 
8930 	ret = i40e_resume_port_tx(pf);
8931 
8932 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8933 	/* In case of error no point in resuming VSIs */
8934 	if (ret)
8935 		goto exit;
8936 
8937 	/* Wait for the PF's queues to be disabled */
8938 	ret = i40e_pf_wait_queues_disabled(pf);
8939 	if (ret) {
8940 		/* Schedule PF reset to recover */
8941 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8942 		i40e_service_event_schedule(pf);
8943 	} else {
8944 		i40e_pf_unquiesce_all_vsi(pf);
8945 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8946 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8947 	}
8948 
8949 exit:
8950 	return ret;
8951 }
8952 #endif /* CONFIG_I40E_DCB */
8953 
8954 /**
8955  * i40e_do_reset_safe - Protected reset path for userland calls.
8956  * @pf: board private structure
8957  * @reset_flags: which reset is requested
8958  *
8959  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)8960 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8961 {
8962 	rtnl_lock();
8963 	i40e_do_reset(pf, reset_flags, true);
8964 	rtnl_unlock();
8965 }
8966 
8967 /**
8968  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8969  * @pf: board private structure
8970  * @e: event info posted on ARQ
8971  *
8972  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8973  * and VF queues
8974  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)8975 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8976 					   struct i40e_arq_event_info *e)
8977 {
8978 	struct i40e_aqc_lan_overflow *data =
8979 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8980 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8981 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8982 	struct i40e_hw *hw = &pf->hw;
8983 	struct i40e_vf *vf;
8984 	u16 vf_id;
8985 
8986 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8987 		queue, qtx_ctl);
8988 
8989 	/* Queue belongs to VF, find the VF and issue VF reset */
8990 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8991 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8992 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8993 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8994 		vf_id -= hw->func_caps.vf_base_id;
8995 		vf = &pf->vf[vf_id];
8996 		i40e_vc_notify_vf_reset(vf);
8997 		/* Allow VF to process pending reset notification */
8998 		msleep(20);
8999 		i40e_reset_vf(vf, false);
9000 	}
9001 }
9002 
9003 /**
9004  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9005  * @pf: board private structure
9006  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9007 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9008 {
9009 	u32 val, fcnt_prog;
9010 
9011 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9012 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9013 	return fcnt_prog;
9014 }
9015 
9016 /**
9017  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9018  * @pf: board private structure
9019  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9020 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9021 {
9022 	u32 val, fcnt_prog;
9023 
9024 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9025 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9026 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9027 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9028 	return fcnt_prog;
9029 }
9030 
9031 /**
9032  * i40e_get_global_fd_count - Get total FD filters programmed on device
9033  * @pf: board private structure
9034  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9035 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9036 {
9037 	u32 val, fcnt_prog;
9038 
9039 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9040 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9041 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9042 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9043 	return fcnt_prog;
9044 }
9045 
9046 /**
9047  * i40e_reenable_fdir_sb - Restore FDir SB capability
9048  * @pf: board private structure
9049  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9050 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9051 {
9052 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9053 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9054 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9055 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9056 }
9057 
9058 /**
9059  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9060  * @pf: board private structure
9061  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9062 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9063 {
9064 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9065 		/* ATR uses the same filtering logic as SB rules. It only
9066 		 * functions properly if the input set mask is at the default
9067 		 * settings. It is safe to restore the default input set
9068 		 * because there are no active TCPv4 filter rules.
9069 		 */
9070 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9071 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9072 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9073 
9074 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9075 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9076 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9077 	}
9078 }
9079 
9080 /**
9081  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9082  * @pf: board private structure
9083  * @filter: FDir filter to remove
9084  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9085 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9086 				       struct i40e_fdir_filter *filter)
9087 {
9088 	/* Update counters */
9089 	pf->fdir_pf_active_filters--;
9090 	pf->fd_inv = 0;
9091 
9092 	switch (filter->flow_type) {
9093 	case TCP_V4_FLOW:
9094 		pf->fd_tcp4_filter_cnt--;
9095 		break;
9096 	case UDP_V4_FLOW:
9097 		pf->fd_udp4_filter_cnt--;
9098 		break;
9099 	case SCTP_V4_FLOW:
9100 		pf->fd_sctp4_filter_cnt--;
9101 		break;
9102 	case IP_USER_FLOW:
9103 		switch (filter->ip4_proto) {
9104 		case IPPROTO_TCP:
9105 			pf->fd_tcp4_filter_cnt--;
9106 			break;
9107 		case IPPROTO_UDP:
9108 			pf->fd_udp4_filter_cnt--;
9109 			break;
9110 		case IPPROTO_SCTP:
9111 			pf->fd_sctp4_filter_cnt--;
9112 			break;
9113 		case IPPROTO_IP:
9114 			pf->fd_ip4_filter_cnt--;
9115 			break;
9116 		}
9117 		break;
9118 	}
9119 
9120 	/* Remove the filter from the list and free memory */
9121 	hlist_del(&filter->fdir_node);
9122 	kfree(filter);
9123 }
9124 
9125 /**
9126  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9127  * @pf: board private structure
9128  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9129 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9130 {
9131 	struct i40e_fdir_filter *filter;
9132 	u32 fcnt_prog, fcnt_avail;
9133 	struct hlist_node *node;
9134 
9135 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9136 		return;
9137 
9138 	/* Check if we have enough room to re-enable FDir SB capability. */
9139 	fcnt_prog = i40e_get_global_fd_count(pf);
9140 	fcnt_avail = pf->fdir_pf_filter_count;
9141 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9142 	    (pf->fd_add_err == 0) ||
9143 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9144 		i40e_reenable_fdir_sb(pf);
9145 
9146 	/* We should wait for even more space before re-enabling ATR.
9147 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9148 	 * rules active.
9149 	 */
9150 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9151 	    (pf->fd_tcp4_filter_cnt == 0))
9152 		i40e_reenable_fdir_atr(pf);
9153 
9154 	/* if hw had a problem adding a filter, delete it */
9155 	if (pf->fd_inv > 0) {
9156 		hlist_for_each_entry_safe(filter, node,
9157 					  &pf->fdir_filter_list, fdir_node)
9158 			if (filter->fd_id == pf->fd_inv)
9159 				i40e_delete_invalid_filter(pf, filter);
9160 	}
9161 }
9162 
9163 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9164 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9165 /**
9166  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9167  * @pf: board private structure
9168  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9169 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9170 {
9171 	unsigned long min_flush_time;
9172 	int flush_wait_retry = 50;
9173 	bool disable_atr = false;
9174 	int fd_room;
9175 	int reg;
9176 
9177 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9178 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9179 		return;
9180 
9181 	/* If the flush is happening too quick and we have mostly SB rules we
9182 	 * should not re-enable ATR for some time.
9183 	 */
9184 	min_flush_time = pf->fd_flush_timestamp +
9185 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9186 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9187 
9188 	if (!(time_after(jiffies, min_flush_time)) &&
9189 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9190 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9191 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9192 		disable_atr = true;
9193 	}
9194 
9195 	pf->fd_flush_timestamp = jiffies;
9196 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9197 	/* flush all filters */
9198 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9199 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9200 	i40e_flush(&pf->hw);
9201 	pf->fd_flush_cnt++;
9202 	pf->fd_add_err = 0;
9203 	do {
9204 		/* Check FD flush status every 5-6msec */
9205 		usleep_range(5000, 6000);
9206 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9207 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9208 			break;
9209 	} while (flush_wait_retry--);
9210 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9211 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9212 	} else {
9213 		/* replay sideband filters */
9214 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9215 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9216 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9217 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9218 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9219 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9220 	}
9221 }
9222 
9223 /**
9224  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
9225  * @pf: board private structure
9226  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9227 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9228 {
9229 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9230 }
9231 
9232 /**
9233  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9234  * @pf: board private structure
9235  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9236 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9237 {
9238 
9239 	/* if interface is down do nothing */
9240 	if (test_bit(__I40E_DOWN, pf->state))
9241 		return;
9242 
9243 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9244 		i40e_fdir_flush_and_replay(pf);
9245 
9246 	i40e_fdir_check_and_reenable(pf);
9247 
9248 }
9249 
9250 /**
9251  * i40e_vsi_link_event - notify VSI of a link event
9252  * @vsi: vsi to be notified
9253  * @link_up: link up or down
9254  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9255 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9256 {
9257 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9258 		return;
9259 
9260 	switch (vsi->type) {
9261 	case I40E_VSI_MAIN:
9262 		if (!vsi->netdev || !vsi->netdev_registered)
9263 			break;
9264 
9265 		if (link_up) {
9266 			netif_carrier_on(vsi->netdev);
9267 			netif_tx_wake_all_queues(vsi->netdev);
9268 		} else {
9269 			netif_carrier_off(vsi->netdev);
9270 			netif_tx_stop_all_queues(vsi->netdev);
9271 		}
9272 		break;
9273 
9274 	case I40E_VSI_SRIOV:
9275 	case I40E_VSI_VMDQ2:
9276 	case I40E_VSI_CTRL:
9277 	case I40E_VSI_IWARP:
9278 	case I40E_VSI_MIRROR:
9279 	default:
9280 		/* there is no notification for other VSIs */
9281 		break;
9282 	}
9283 }
9284 
9285 /**
9286  * i40e_veb_link_event - notify elements on the veb of a link event
9287  * @veb: veb to be notified
9288  * @link_up: link up or down
9289  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9290 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9291 {
9292 	struct i40e_pf *pf;
9293 	int i;
9294 
9295 	if (!veb || !veb->pf)
9296 		return;
9297 	pf = veb->pf;
9298 
9299 	/* depth first... */
9300 	for (i = 0; i < I40E_MAX_VEB; i++)
9301 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9302 			i40e_veb_link_event(pf->veb[i], link_up);
9303 
9304 	/* ... now the local VSIs */
9305 	for (i = 0; i < pf->num_alloc_vsi; i++)
9306 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9307 			i40e_vsi_link_event(pf->vsi[i], link_up);
9308 }
9309 
9310 /**
9311  * i40e_link_event - Update netif_carrier status
9312  * @pf: board private structure
9313  **/
i40e_link_event(struct i40e_pf * pf)9314 static void i40e_link_event(struct i40e_pf *pf)
9315 {
9316 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9317 	u8 new_link_speed, old_link_speed;
9318 	i40e_status status;
9319 	bool new_link, old_link;
9320 
9321 	/* set this to force the get_link_status call to refresh state */
9322 	pf->hw.phy.get_link_info = true;
9323 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9324 	status = i40e_get_link_status(&pf->hw, &new_link);
9325 
9326 	/* On success, disable temp link polling */
9327 	if (status == I40E_SUCCESS) {
9328 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9329 	} else {
9330 		/* Enable link polling temporarily until i40e_get_link_status
9331 		 * returns I40E_SUCCESS
9332 		 */
9333 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9334 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9335 			status);
9336 		return;
9337 	}
9338 
9339 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9340 	new_link_speed = pf->hw.phy.link_info.link_speed;
9341 
9342 	if (new_link == old_link &&
9343 	    new_link_speed == old_link_speed &&
9344 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9345 	     new_link == netif_carrier_ok(vsi->netdev)))
9346 		return;
9347 
9348 	i40e_print_link_message(vsi, new_link);
9349 
9350 	/* Notify the base of the switch tree connected to
9351 	 * the link.  Floating VEBs are not notified.
9352 	 */
9353 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9354 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9355 	else
9356 		i40e_vsi_link_event(vsi, new_link);
9357 
9358 	if (pf->vf)
9359 		i40e_vc_notify_link_state(pf);
9360 
9361 	if (pf->flags & I40E_FLAG_PTP)
9362 		i40e_ptp_set_increment(pf);
9363 }
9364 
9365 /**
9366  * i40e_watchdog_subtask - periodic checks not using event driven response
9367  * @pf: board private structure
9368  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9369 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9370 {
9371 	int i;
9372 
9373 	/* if interface is down do nothing */
9374 	if (test_bit(__I40E_DOWN, pf->state) ||
9375 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9376 		return;
9377 
9378 	/* make sure we don't do these things too often */
9379 	if (time_before(jiffies, (pf->service_timer_previous +
9380 				  pf->service_timer_period)))
9381 		return;
9382 	pf->service_timer_previous = jiffies;
9383 
9384 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9385 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9386 		i40e_link_event(pf);
9387 
9388 	/* Update the stats for active netdevs so the network stack
9389 	 * can look at updated numbers whenever it cares to
9390 	 */
9391 	for (i = 0; i < pf->num_alloc_vsi; i++)
9392 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9393 			i40e_update_stats(pf->vsi[i]);
9394 
9395 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9396 		/* Update the stats for the active switching components */
9397 		for (i = 0; i < I40E_MAX_VEB; i++)
9398 			if (pf->veb[i])
9399 				i40e_update_veb_stats(pf->veb[i]);
9400 	}
9401 
9402 	i40e_ptp_rx_hang(pf);
9403 	i40e_ptp_tx_hang(pf);
9404 }
9405 
9406 /**
9407  * i40e_reset_subtask - Set up for resetting the device and driver
9408  * @pf: board private structure
9409  **/
i40e_reset_subtask(struct i40e_pf * pf)9410 static void i40e_reset_subtask(struct i40e_pf *pf)
9411 {
9412 	u32 reset_flags = 0;
9413 
9414 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9415 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9416 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9417 	}
9418 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9419 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9420 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9421 	}
9422 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9423 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9424 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9425 	}
9426 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9427 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9428 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9429 	}
9430 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9431 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9432 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9433 	}
9434 
9435 	/* If there's a recovery already waiting, it takes
9436 	 * precedence before starting a new reset sequence.
9437 	 */
9438 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9439 		i40e_prep_for_reset(pf, false);
9440 		i40e_reset(pf);
9441 		i40e_rebuild(pf, false, false);
9442 	}
9443 
9444 	/* If we're already down or resetting, just bail */
9445 	if (reset_flags &&
9446 	    !test_bit(__I40E_DOWN, pf->state) &&
9447 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9448 		i40e_do_reset(pf, reset_flags, false);
9449 	}
9450 }
9451 
9452 /**
9453  * i40e_handle_link_event - Handle link event
9454  * @pf: board private structure
9455  * @e: event info posted on ARQ
9456  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9457 static void i40e_handle_link_event(struct i40e_pf *pf,
9458 				   struct i40e_arq_event_info *e)
9459 {
9460 	struct i40e_aqc_get_link_status *status =
9461 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9462 
9463 	/* Do a new status request to re-enable LSE reporting
9464 	 * and load new status information into the hw struct
9465 	 * This completely ignores any state information
9466 	 * in the ARQ event info, instead choosing to always
9467 	 * issue the AQ update link status command.
9468 	 */
9469 	i40e_link_event(pf);
9470 
9471 	/* Check if module meets thermal requirements */
9472 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9473 		dev_err(&pf->pdev->dev,
9474 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9475 		dev_err(&pf->pdev->dev,
9476 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9477 	} else {
9478 		/* check for unqualified module, if link is down, suppress
9479 		 * the message if link was forced to be down.
9480 		 */
9481 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9482 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9483 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9484 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9485 			dev_err(&pf->pdev->dev,
9486 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9487 			dev_err(&pf->pdev->dev,
9488 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9489 		}
9490 	}
9491 }
9492 
9493 /**
9494  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9495  * @pf: board private structure
9496  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)9497 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9498 {
9499 	struct i40e_arq_event_info event;
9500 	struct i40e_hw *hw = &pf->hw;
9501 	u16 pending, i = 0;
9502 	i40e_status ret;
9503 	u16 opcode;
9504 	u32 oldval;
9505 	u32 val;
9506 
9507 	/* Do not run clean AQ when PF reset fails */
9508 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9509 		return;
9510 
9511 	/* check for error indications */
9512 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9513 	oldval = val;
9514 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9515 		if (hw->debug_mask & I40E_DEBUG_AQ)
9516 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9517 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9518 	}
9519 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9520 		if (hw->debug_mask & I40E_DEBUG_AQ)
9521 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9522 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9523 		pf->arq_overflows++;
9524 	}
9525 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9526 		if (hw->debug_mask & I40E_DEBUG_AQ)
9527 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9528 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9529 	}
9530 	if (oldval != val)
9531 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9532 
9533 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9534 	oldval = val;
9535 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9536 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9537 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9538 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9539 	}
9540 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9541 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9542 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9543 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9544 	}
9545 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9546 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9547 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9548 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9549 	}
9550 	if (oldval != val)
9551 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9552 
9553 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9554 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9555 	if (!event.msg_buf)
9556 		return;
9557 
9558 	do {
9559 		ret = i40e_clean_arq_element(hw, &event, &pending);
9560 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9561 			break;
9562 		else if (ret) {
9563 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9564 			break;
9565 		}
9566 
9567 		opcode = le16_to_cpu(event.desc.opcode);
9568 		switch (opcode) {
9569 
9570 		case i40e_aqc_opc_get_link_status:
9571 			i40e_handle_link_event(pf, &event);
9572 			break;
9573 		case i40e_aqc_opc_send_msg_to_pf:
9574 			ret = i40e_vc_process_vf_msg(pf,
9575 					le16_to_cpu(event.desc.retval),
9576 					le32_to_cpu(event.desc.cookie_high),
9577 					le32_to_cpu(event.desc.cookie_low),
9578 					event.msg_buf,
9579 					event.msg_len);
9580 			break;
9581 		case i40e_aqc_opc_lldp_update_mib:
9582 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9583 #ifdef CONFIG_I40E_DCB
9584 			rtnl_lock();
9585 			ret = i40e_handle_lldp_event(pf, &event);
9586 			rtnl_unlock();
9587 #endif /* CONFIG_I40E_DCB */
9588 			break;
9589 		case i40e_aqc_opc_event_lan_overflow:
9590 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9591 			i40e_handle_lan_overflow_event(pf, &event);
9592 			break;
9593 		case i40e_aqc_opc_send_msg_to_peer:
9594 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9595 			break;
9596 		case i40e_aqc_opc_nvm_erase:
9597 		case i40e_aqc_opc_nvm_update:
9598 		case i40e_aqc_opc_oem_post_update:
9599 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9600 				   "ARQ NVM operation 0x%04x completed\n",
9601 				   opcode);
9602 			break;
9603 		default:
9604 			dev_info(&pf->pdev->dev,
9605 				 "ARQ: Unknown event 0x%04x ignored\n",
9606 				 opcode);
9607 			break;
9608 		}
9609 	} while (i++ < pf->adminq_work_limit);
9610 
9611 	if (i < pf->adminq_work_limit)
9612 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9613 
9614 	/* re-enable Admin queue interrupt cause */
9615 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9616 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9617 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9618 	i40e_flush(hw);
9619 
9620 	kfree(event.msg_buf);
9621 }
9622 
9623 /**
9624  * i40e_verify_eeprom - make sure eeprom is good to use
9625  * @pf: board private structure
9626  **/
i40e_verify_eeprom(struct i40e_pf * pf)9627 static void i40e_verify_eeprom(struct i40e_pf *pf)
9628 {
9629 	int err;
9630 
9631 	err = i40e_diag_eeprom_test(&pf->hw);
9632 	if (err) {
9633 		/* retry in case of garbage read */
9634 		err = i40e_diag_eeprom_test(&pf->hw);
9635 		if (err) {
9636 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9637 				 err);
9638 			set_bit(__I40E_BAD_EEPROM, pf->state);
9639 		}
9640 	}
9641 
9642 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9643 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9644 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9645 	}
9646 }
9647 
9648 /**
9649  * i40e_enable_pf_switch_lb
9650  * @pf: pointer to the PF structure
9651  *
9652  * enable switch loop back or die - no point in a return value
9653  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)9654 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9655 {
9656 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9657 	struct i40e_vsi_context ctxt;
9658 	int ret;
9659 
9660 	ctxt.seid = pf->main_vsi_seid;
9661 	ctxt.pf_num = pf->hw.pf_id;
9662 	ctxt.vf_num = 0;
9663 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9664 	if (ret) {
9665 		dev_info(&pf->pdev->dev,
9666 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9667 			 i40e_stat_str(&pf->hw, ret),
9668 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9669 		return;
9670 	}
9671 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9672 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9673 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9674 
9675 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9676 	if (ret) {
9677 		dev_info(&pf->pdev->dev,
9678 			 "update vsi switch failed, err %s aq_err %s\n",
9679 			 i40e_stat_str(&pf->hw, ret),
9680 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9681 	}
9682 }
9683 
9684 /**
9685  * i40e_disable_pf_switch_lb
9686  * @pf: pointer to the PF structure
9687  *
9688  * disable switch loop back or die - no point in a return value
9689  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)9690 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9691 {
9692 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9693 	struct i40e_vsi_context ctxt;
9694 	int ret;
9695 
9696 	ctxt.seid = pf->main_vsi_seid;
9697 	ctxt.pf_num = pf->hw.pf_id;
9698 	ctxt.vf_num = 0;
9699 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9700 	if (ret) {
9701 		dev_info(&pf->pdev->dev,
9702 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9703 			 i40e_stat_str(&pf->hw, ret),
9704 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9705 		return;
9706 	}
9707 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9708 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9709 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9710 
9711 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9712 	if (ret) {
9713 		dev_info(&pf->pdev->dev,
9714 			 "update vsi switch failed, err %s aq_err %s\n",
9715 			 i40e_stat_str(&pf->hw, ret),
9716 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9717 	}
9718 }
9719 
9720 /**
9721  * i40e_config_bridge_mode - Configure the HW bridge mode
9722  * @veb: pointer to the bridge instance
9723  *
9724  * Configure the loop back mode for the LAN VSI that is downlink to the
9725  * specified HW bridge instance. It is expected this function is called
9726  * when a new HW bridge is instantiated.
9727  **/
i40e_config_bridge_mode(struct i40e_veb * veb)9728 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9729 {
9730 	struct i40e_pf *pf = veb->pf;
9731 
9732 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9733 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9734 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9735 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9736 		i40e_disable_pf_switch_lb(pf);
9737 	else
9738 		i40e_enable_pf_switch_lb(pf);
9739 }
9740 
9741 /**
9742  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9743  * @veb: pointer to the VEB instance
9744  *
9745  * This is a recursive function that first builds the attached VSIs then
9746  * recurses in to build the next layer of VEB.  We track the connections
9747  * through our own index numbers because the seid's from the HW could
9748  * change across the reset.
9749  **/
i40e_reconstitute_veb(struct i40e_veb * veb)9750 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9751 {
9752 	struct i40e_vsi *ctl_vsi = NULL;
9753 	struct i40e_pf *pf = veb->pf;
9754 	int v, veb_idx;
9755 	int ret;
9756 
9757 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9758 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9759 		if (pf->vsi[v] &&
9760 		    pf->vsi[v]->veb_idx == veb->idx &&
9761 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9762 			ctl_vsi = pf->vsi[v];
9763 			break;
9764 		}
9765 	}
9766 	if (!ctl_vsi) {
9767 		dev_info(&pf->pdev->dev,
9768 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9769 		ret = -ENOENT;
9770 		goto end_reconstitute;
9771 	}
9772 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9773 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9774 	ret = i40e_add_vsi(ctl_vsi);
9775 	if (ret) {
9776 		dev_info(&pf->pdev->dev,
9777 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9778 			 veb->idx, ret);
9779 		goto end_reconstitute;
9780 	}
9781 	i40e_vsi_reset_stats(ctl_vsi);
9782 
9783 	/* create the VEB in the switch and move the VSI onto the VEB */
9784 	ret = i40e_add_veb(veb, ctl_vsi);
9785 	if (ret)
9786 		goto end_reconstitute;
9787 
9788 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9789 		veb->bridge_mode = BRIDGE_MODE_VEB;
9790 	else
9791 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9792 	i40e_config_bridge_mode(veb);
9793 
9794 	/* create the remaining VSIs attached to this VEB */
9795 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9796 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9797 			continue;
9798 
9799 		if (pf->vsi[v]->veb_idx == veb->idx) {
9800 			struct i40e_vsi *vsi = pf->vsi[v];
9801 
9802 			vsi->uplink_seid = veb->seid;
9803 			ret = i40e_add_vsi(vsi);
9804 			if (ret) {
9805 				dev_info(&pf->pdev->dev,
9806 					 "rebuild of vsi_idx %d failed: %d\n",
9807 					 v, ret);
9808 				goto end_reconstitute;
9809 			}
9810 			i40e_vsi_reset_stats(vsi);
9811 		}
9812 	}
9813 
9814 	/* create any VEBs attached to this VEB - RECURSION */
9815 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9816 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9817 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9818 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9819 			if (ret)
9820 				break;
9821 		}
9822 	}
9823 
9824 end_reconstitute:
9825 	return ret;
9826 }
9827 
9828 /**
9829  * i40e_get_capabilities - get info about the HW
9830  * @pf: the PF struct
9831  * @list_type: AQ capability to be queried
9832  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)9833 static int i40e_get_capabilities(struct i40e_pf *pf,
9834 				 enum i40e_admin_queue_opc list_type)
9835 {
9836 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9837 	u16 data_size;
9838 	int buf_len;
9839 	int err;
9840 
9841 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9842 	do {
9843 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9844 		if (!cap_buf)
9845 			return -ENOMEM;
9846 
9847 		/* this loads the data into the hw struct for us */
9848 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9849 						    &data_size, list_type,
9850 						    NULL);
9851 		/* data loaded, buffer no longer needed */
9852 		kfree(cap_buf);
9853 
9854 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9855 			/* retry with a larger buffer */
9856 			buf_len = data_size;
9857 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
9858 			dev_info(&pf->pdev->dev,
9859 				 "capability discovery failed, err %s aq_err %s\n",
9860 				 i40e_stat_str(&pf->hw, err),
9861 				 i40e_aq_str(&pf->hw,
9862 					     pf->hw.aq.asq_last_status));
9863 			return -ENODEV;
9864 		}
9865 	} while (err);
9866 
9867 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9868 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9869 			dev_info(&pf->pdev->dev,
9870 				 "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",
9871 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9872 				 pf->hw.func_caps.num_msix_vectors,
9873 				 pf->hw.func_caps.num_msix_vectors_vf,
9874 				 pf->hw.func_caps.fd_filters_guaranteed,
9875 				 pf->hw.func_caps.fd_filters_best_effort,
9876 				 pf->hw.func_caps.num_tx_qp,
9877 				 pf->hw.func_caps.num_vsis);
9878 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9879 			dev_info(&pf->pdev->dev,
9880 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9881 				 pf->hw.dev_caps.switch_mode,
9882 				 pf->hw.dev_caps.valid_functions);
9883 			dev_info(&pf->pdev->dev,
9884 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9885 				 pf->hw.dev_caps.sr_iov_1_1,
9886 				 pf->hw.dev_caps.num_vfs);
9887 			dev_info(&pf->pdev->dev,
9888 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9889 				 pf->hw.dev_caps.num_vsis,
9890 				 pf->hw.dev_caps.num_rx_qp,
9891 				 pf->hw.dev_caps.num_tx_qp);
9892 		}
9893 	}
9894 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9895 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9896 		       + pf->hw.func_caps.num_vfs)
9897 		if (pf->hw.revision_id == 0 &&
9898 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9899 			dev_info(&pf->pdev->dev,
9900 				 "got num_vsis %d, setting num_vsis to %d\n",
9901 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9902 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9903 		}
9904 	}
9905 	return 0;
9906 }
9907 
9908 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9909 
9910 /**
9911  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9912  * @pf: board private structure
9913  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)9914 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9915 {
9916 	struct i40e_vsi *vsi;
9917 
9918 	/* quick workaround for an NVM issue that leaves a critical register
9919 	 * uninitialized
9920 	 */
9921 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9922 		static const u32 hkey[] = {
9923 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9924 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9925 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9926 			0x95b3a76d};
9927 		int i;
9928 
9929 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9930 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9931 	}
9932 
9933 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9934 		return;
9935 
9936 	/* find existing VSI and see if it needs configuring */
9937 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9938 
9939 	/* create a new VSI if none exists */
9940 	if (!vsi) {
9941 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9942 				     pf->vsi[pf->lan_vsi]->seid, 0);
9943 		if (!vsi) {
9944 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9945 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9946 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9947 			return;
9948 		}
9949 	}
9950 
9951 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9952 }
9953 
9954 /**
9955  * i40e_fdir_teardown - release the Flow Director resources
9956  * @pf: board private structure
9957  **/
i40e_fdir_teardown(struct i40e_pf * pf)9958 static void i40e_fdir_teardown(struct i40e_pf *pf)
9959 {
9960 	struct i40e_vsi *vsi;
9961 
9962 	i40e_fdir_filter_exit(pf);
9963 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9964 	if (vsi)
9965 		i40e_vsi_release(vsi);
9966 }
9967 
9968 /**
9969  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9970  * @vsi: PF main vsi
9971  * @seid: seid of main or channel VSIs
9972  *
9973  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9974  * existed before reset
9975  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)9976 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9977 {
9978 	struct i40e_cloud_filter *cfilter;
9979 	struct i40e_pf *pf = vsi->back;
9980 	struct hlist_node *node;
9981 	i40e_status ret;
9982 
9983 	/* Add cloud filters back if they exist */
9984 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9985 				  cloud_node) {
9986 		if (cfilter->seid != seid)
9987 			continue;
9988 
9989 		if (cfilter->dst_port)
9990 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9991 								true);
9992 		else
9993 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9994 
9995 		if (ret) {
9996 			dev_dbg(&pf->pdev->dev,
9997 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9998 				i40e_stat_str(&pf->hw, ret),
9999 				i40e_aq_str(&pf->hw,
10000 					    pf->hw.aq.asq_last_status));
10001 			return ret;
10002 		}
10003 	}
10004 	return 0;
10005 }
10006 
10007 /**
10008  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10009  * @vsi: PF main vsi
10010  *
10011  * Rebuilds channel VSIs if they existed before reset
10012  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10013 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10014 {
10015 	struct i40e_channel *ch, *ch_tmp;
10016 	i40e_status ret;
10017 
10018 	if (list_empty(&vsi->ch_list))
10019 		return 0;
10020 
10021 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10022 		if (!ch->initialized)
10023 			break;
10024 		/* Proceed with creation of channel (VMDq2) VSI */
10025 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10026 		if (ret) {
10027 			dev_info(&vsi->back->pdev->dev,
10028 				 "failed to rebuild channels using uplink_seid %u\n",
10029 				 vsi->uplink_seid);
10030 			return ret;
10031 		}
10032 		/* Reconfigure TX queues using QTX_CTL register */
10033 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10034 		if (ret) {
10035 			dev_info(&vsi->back->pdev->dev,
10036 				 "failed to configure TX rings for channel %u\n",
10037 				 ch->seid);
10038 			return ret;
10039 		}
10040 		/* update 'next_base_queue' */
10041 		vsi->next_base_queue = vsi->next_base_queue +
10042 							ch->num_queue_pairs;
10043 		if (ch->max_tx_rate) {
10044 			u64 credits = ch->max_tx_rate;
10045 
10046 			if (i40e_set_bw_limit(vsi, ch->seid,
10047 					      ch->max_tx_rate))
10048 				return -EINVAL;
10049 
10050 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10051 			dev_dbg(&vsi->back->pdev->dev,
10052 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10053 				ch->max_tx_rate,
10054 				credits,
10055 				ch->seid);
10056 		}
10057 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10058 		if (ret) {
10059 			dev_dbg(&vsi->back->pdev->dev,
10060 				"Failed to rebuild cloud filters for channel VSI %u\n",
10061 				ch->seid);
10062 			return ret;
10063 		}
10064 	}
10065 	return 0;
10066 }
10067 
10068 /**
10069  * i40e_clean_xps_state - clean xps state for every tx_ring
10070  * @vsi: ptr to the VSI
10071  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10072 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10073 {
10074 	int i;
10075 
10076 	if (vsi->tx_rings)
10077 		for (i = 0; i < vsi->num_queue_pairs; i++)
10078 			if (vsi->tx_rings[i])
10079 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10080 					  vsi->tx_rings[i]->state);
10081 }
10082 
10083 /**
10084  * i40e_prep_for_reset - prep for the core to reset
10085  * @pf: board private structure
10086  * @lock_acquired: indicates whether or not the lock has been acquired
10087  * before this function was called.
10088  *
10089  * Close up the VFs and other things in prep for PF Reset.
10090   **/
i40e_prep_for_reset(struct i40e_pf * pf,bool lock_acquired)10091 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
10092 {
10093 	struct i40e_hw *hw = &pf->hw;
10094 	i40e_status ret = 0;
10095 	u32 v;
10096 
10097 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10098 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10099 		return;
10100 	if (i40e_check_asq_alive(&pf->hw))
10101 		i40e_vc_notify_reset(pf);
10102 
10103 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10104 
10105 	/* quiesce the VSIs and their queues that are not already DOWN */
10106 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
10107 	if (!lock_acquired)
10108 		rtnl_lock();
10109 	i40e_pf_quiesce_all_vsi(pf);
10110 	if (!lock_acquired)
10111 		rtnl_unlock();
10112 
10113 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10114 		if (pf->vsi[v]) {
10115 			i40e_clean_xps_state(pf->vsi[v]);
10116 			pf->vsi[v]->seid = 0;
10117 		}
10118 	}
10119 
10120 	i40e_shutdown_adminq(&pf->hw);
10121 
10122 	/* call shutdown HMC */
10123 	if (hw->hmc.hmc_obj) {
10124 		ret = i40e_shutdown_lan_hmc(hw);
10125 		if (ret)
10126 			dev_warn(&pf->pdev->dev,
10127 				 "shutdown_lan_hmc failed: %d\n", ret);
10128 	}
10129 
10130 	/* Save the current PTP time so that we can restore the time after the
10131 	 * reset completes.
10132 	 */
10133 	i40e_ptp_save_hw_time(pf);
10134 }
10135 
10136 /**
10137  * i40e_send_version - update firmware with driver version
10138  * @pf: PF struct
10139  */
i40e_send_version(struct i40e_pf * pf)10140 static void i40e_send_version(struct i40e_pf *pf)
10141 {
10142 	struct i40e_driver_version dv;
10143 
10144 	dv.major_version = 0xff;
10145 	dv.minor_version = 0xff;
10146 	dv.build_version = 0xff;
10147 	dv.subbuild_version = 0;
10148 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10149 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10150 }
10151 
10152 /**
10153  * i40e_get_oem_version - get OEM specific version information
10154  * @hw: pointer to the hardware structure
10155  **/
i40e_get_oem_version(struct i40e_hw * hw)10156 static void i40e_get_oem_version(struct i40e_hw *hw)
10157 {
10158 	u16 block_offset = 0xffff;
10159 	u16 block_length = 0;
10160 	u16 capabilities = 0;
10161 	u16 gen_snap = 0;
10162 	u16 release = 0;
10163 
10164 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10165 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10166 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10167 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10168 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10169 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10170 #define I40E_NVM_OEM_LENGTH			3
10171 
10172 	/* Check if pointer to OEM version block is valid. */
10173 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10174 	if (block_offset == 0xffff)
10175 		return;
10176 
10177 	/* Check if OEM version block has correct length. */
10178 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10179 			   &block_length);
10180 	if (block_length < I40E_NVM_OEM_LENGTH)
10181 		return;
10182 
10183 	/* Check if OEM version format is as expected. */
10184 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10185 			   &capabilities);
10186 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10187 		return;
10188 
10189 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10190 			   &gen_snap);
10191 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10192 			   &release);
10193 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10194 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10195 }
10196 
10197 /**
10198  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10199  * @pf: board private structure
10200  **/
i40e_reset(struct i40e_pf * pf)10201 static int i40e_reset(struct i40e_pf *pf)
10202 {
10203 	struct i40e_hw *hw = &pf->hw;
10204 	i40e_status ret;
10205 
10206 	ret = i40e_pf_reset(hw);
10207 	if (ret) {
10208 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10209 		set_bit(__I40E_RESET_FAILED, pf->state);
10210 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10211 	} else {
10212 		pf->pfr_count++;
10213 	}
10214 	return ret;
10215 }
10216 
10217 /**
10218  * i40e_rebuild - rebuild using a saved config
10219  * @pf: board private structure
10220  * @reinit: if the Main VSI needs to re-initialized.
10221  * @lock_acquired: indicates whether or not the lock has been acquired
10222  * before this function was called.
10223  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10224 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10225 {
10226 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10227 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10228 	struct i40e_hw *hw = &pf->hw;
10229 	i40e_status ret;
10230 	u32 val;
10231 	int v;
10232 
10233 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10234 	    is_recovery_mode_reported)
10235 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10236 
10237 	if (test_bit(__I40E_DOWN, pf->state) &&
10238 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10239 		goto clear_recovery;
10240 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10241 
10242 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10243 	ret = i40e_init_adminq(&pf->hw);
10244 	if (ret) {
10245 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10246 			 i40e_stat_str(&pf->hw, ret),
10247 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10248 		goto clear_recovery;
10249 	}
10250 	i40e_get_oem_version(&pf->hw);
10251 
10252 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10253 		/* The following delay is necessary for firmware update. */
10254 		mdelay(1000);
10255 	}
10256 
10257 	/* re-verify the eeprom if we just had an EMP reset */
10258 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10259 		i40e_verify_eeprom(pf);
10260 
10261 	/* if we are going out of or into recovery mode we have to act
10262 	 * accordingly with regard to resources initialization
10263 	 * and deinitialization
10264 	 */
10265 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10266 		if (i40e_get_capabilities(pf,
10267 					  i40e_aqc_opc_list_func_capabilities))
10268 			goto end_unlock;
10269 
10270 		if (is_recovery_mode_reported) {
10271 			/* we're staying in recovery mode so we'll reinitialize
10272 			 * misc vector here
10273 			 */
10274 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10275 				goto end_unlock;
10276 		} else {
10277 			if (!lock_acquired)
10278 				rtnl_lock();
10279 			/* we're going out of recovery mode so we'll free
10280 			 * the IRQ allocated specifically for recovery mode
10281 			 * and restore the interrupt scheme
10282 			 */
10283 			free_irq(pf->pdev->irq, pf);
10284 			i40e_clear_interrupt_scheme(pf);
10285 			if (i40e_restore_interrupt_scheme(pf))
10286 				goto end_unlock;
10287 		}
10288 
10289 		/* tell the firmware that we're starting */
10290 		i40e_send_version(pf);
10291 
10292 		/* bail out in case recovery mode was detected, as there is
10293 		 * no need for further configuration.
10294 		 */
10295 		goto end_unlock;
10296 	}
10297 
10298 	i40e_clear_pxe_mode(hw);
10299 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10300 	if (ret)
10301 		goto end_core_reset;
10302 
10303 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10304 				hw->func_caps.num_rx_qp, 0, 0);
10305 	if (ret) {
10306 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10307 		goto end_core_reset;
10308 	}
10309 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10310 	if (ret) {
10311 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10312 		goto end_core_reset;
10313 	}
10314 
10315 	/* Enable FW to write a default DCB config on link-up */
10316 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10317 
10318 #ifdef CONFIG_I40E_DCB
10319 	ret = i40e_init_pf_dcb(pf);
10320 	if (ret) {
10321 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10322 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10323 		/* Continue without DCB enabled */
10324 	}
10325 #endif /* CONFIG_I40E_DCB */
10326 	/* do basic switch setup */
10327 	if (!lock_acquired)
10328 		rtnl_lock();
10329 	ret = i40e_setup_pf_switch(pf, reinit, true);
10330 	if (ret)
10331 		goto end_unlock;
10332 
10333 	/* The driver only wants link up/down and module qualification
10334 	 * reports from firmware.  Note the negative logic.
10335 	 */
10336 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10337 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10338 					 I40E_AQ_EVENT_MEDIA_NA |
10339 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10340 	if (ret)
10341 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10342 			 i40e_stat_str(&pf->hw, ret),
10343 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10344 
10345 	/* Rebuild the VSIs and VEBs that existed before reset.
10346 	 * They are still in our local switch element arrays, so only
10347 	 * need to rebuild the switch model in the HW.
10348 	 *
10349 	 * If there were VEBs but the reconstitution failed, we'll try
10350 	 * try to recover minimal use by getting the basic PF VSI working.
10351 	 */
10352 	if (vsi->uplink_seid != pf->mac_seid) {
10353 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10354 		/* find the one VEB connected to the MAC, and find orphans */
10355 		for (v = 0; v < I40E_MAX_VEB; v++) {
10356 			if (!pf->veb[v])
10357 				continue;
10358 
10359 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10360 			    pf->veb[v]->uplink_seid == 0) {
10361 				ret = i40e_reconstitute_veb(pf->veb[v]);
10362 
10363 				if (!ret)
10364 					continue;
10365 
10366 				/* If Main VEB failed, we're in deep doodoo,
10367 				 * so give up rebuilding the switch and set up
10368 				 * for minimal rebuild of PF VSI.
10369 				 * If orphan failed, we'll report the error
10370 				 * but try to keep going.
10371 				 */
10372 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10373 					dev_info(&pf->pdev->dev,
10374 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10375 						 ret);
10376 					vsi->uplink_seid = pf->mac_seid;
10377 					break;
10378 				} else if (pf->veb[v]->uplink_seid == 0) {
10379 					dev_info(&pf->pdev->dev,
10380 						 "rebuild of orphan VEB failed: %d\n",
10381 						 ret);
10382 				}
10383 			}
10384 		}
10385 	}
10386 
10387 	if (vsi->uplink_seid == pf->mac_seid) {
10388 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10389 		/* no VEB, so rebuild only the Main VSI */
10390 		ret = i40e_add_vsi(vsi);
10391 		if (ret) {
10392 			dev_info(&pf->pdev->dev,
10393 				 "rebuild of Main VSI failed: %d\n", ret);
10394 			goto end_unlock;
10395 		}
10396 	}
10397 
10398 	if (vsi->mqprio_qopt.max_rate[0]) {
10399 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
10400 						  vsi->mqprio_qopt.max_rate[0]);
10401 		u64 credits = 0;
10402 
10403 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10404 		if (ret)
10405 			goto end_unlock;
10406 
10407 		credits = max_tx_rate;
10408 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10409 		dev_dbg(&vsi->back->pdev->dev,
10410 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10411 			max_tx_rate,
10412 			credits,
10413 			vsi->seid);
10414 	}
10415 
10416 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10417 	if (ret)
10418 		goto end_unlock;
10419 
10420 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10421 	 * for this main VSI if they exist
10422 	 */
10423 	ret = i40e_rebuild_channels(vsi);
10424 	if (ret)
10425 		goto end_unlock;
10426 
10427 	/* Reconfigure hardware for allowing smaller MSS in the case
10428 	 * of TSO, so that we avoid the MDD being fired and causing
10429 	 * a reset in the case of small MSS+TSO.
10430 	 */
10431 #define I40E_REG_MSS          0x000E64DC
10432 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10433 #define I40E_64BYTE_MSS       0x400000
10434 	val = rd32(hw, I40E_REG_MSS);
10435 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10436 		val &= ~I40E_REG_MSS_MIN_MASK;
10437 		val |= I40E_64BYTE_MSS;
10438 		wr32(hw, I40E_REG_MSS, val);
10439 	}
10440 
10441 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10442 		msleep(75);
10443 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10444 		if (ret)
10445 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10446 				 i40e_stat_str(&pf->hw, ret),
10447 				 i40e_aq_str(&pf->hw,
10448 					     pf->hw.aq.asq_last_status));
10449 	}
10450 	/* reinit the misc interrupt */
10451 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10452 		ret = i40e_setup_misc_vector(pf);
10453 
10454 	/* Add a filter to drop all Flow control frames from any VSI from being
10455 	 * transmitted. By doing so we stop a malicious VF from sending out
10456 	 * PAUSE or PFC frames and potentially controlling traffic for other
10457 	 * PF/VF VSIs.
10458 	 * The FW can still send Flow control frames if enabled.
10459 	 */
10460 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10461 						       pf->main_vsi_seid);
10462 
10463 	/* restart the VSIs that were rebuilt and running before the reset */
10464 	i40e_pf_unquiesce_all_vsi(pf);
10465 
10466 	/* Release the RTNL lock before we start resetting VFs */
10467 	if (!lock_acquired)
10468 		rtnl_unlock();
10469 
10470 	/* Restore promiscuous settings */
10471 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10472 	if (ret)
10473 		dev_warn(&pf->pdev->dev,
10474 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10475 			 pf->cur_promisc ? "on" : "off",
10476 			 i40e_stat_str(&pf->hw, ret),
10477 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10478 
10479 	i40e_reset_all_vfs(pf, true);
10480 
10481 	/* tell the firmware that we're starting */
10482 	i40e_send_version(pf);
10483 
10484 	/* We've already released the lock, so don't do it again */
10485 	goto end_core_reset;
10486 
10487 end_unlock:
10488 	if (!lock_acquired)
10489 		rtnl_unlock();
10490 end_core_reset:
10491 	clear_bit(__I40E_RESET_FAILED, pf->state);
10492 clear_recovery:
10493 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10494 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10495 }
10496 
10497 /**
10498  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10499  * @pf: board private structure
10500  * @reinit: if the Main VSI needs to re-initialized.
10501  * @lock_acquired: indicates whether or not the lock has been acquired
10502  * before this function was called.
10503  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10504 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10505 				   bool lock_acquired)
10506 {
10507 	int ret;
10508 	/* Now we wait for GRST to settle out.
10509 	 * We don't have to delete the VEBs or VSIs from the hw switch
10510 	 * because the reset will make them disappear.
10511 	 */
10512 	ret = i40e_reset(pf);
10513 	if (!ret)
10514 		i40e_rebuild(pf, reinit, lock_acquired);
10515 }
10516 
10517 /**
10518  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10519  * @pf: board private structure
10520  *
10521  * Close up the VFs and other things in prep for a Core Reset,
10522  * then get ready to rebuild the world.
10523  * @lock_acquired: indicates whether or not the lock has been acquired
10524  * before this function was called.
10525  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)10526 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10527 {
10528 	i40e_prep_for_reset(pf, lock_acquired);
10529 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10530 }
10531 
10532 /**
10533  * i40e_handle_mdd_event
10534  * @pf: pointer to the PF structure
10535  *
10536  * Called from the MDD irq handler to identify possibly malicious vfs
10537  **/
i40e_handle_mdd_event(struct i40e_pf * pf)10538 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10539 {
10540 	struct i40e_hw *hw = &pf->hw;
10541 	bool mdd_detected = false;
10542 	struct i40e_vf *vf;
10543 	u32 reg;
10544 	int i;
10545 
10546 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10547 		return;
10548 
10549 	/* find what triggered the MDD event */
10550 	reg = rd32(hw, I40E_GL_MDET_TX);
10551 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10552 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10553 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10554 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10555 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10556 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10557 				I40E_GL_MDET_TX_EVENT_SHIFT;
10558 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10559 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10560 				pf->hw.func_caps.base_queue;
10561 		if (netif_msg_tx_err(pf))
10562 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10563 				 event, queue, pf_num, vf_num);
10564 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10565 		mdd_detected = true;
10566 	}
10567 	reg = rd32(hw, I40E_GL_MDET_RX);
10568 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10569 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10570 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10571 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10572 				I40E_GL_MDET_RX_EVENT_SHIFT;
10573 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10574 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10575 				pf->hw.func_caps.base_queue;
10576 		if (netif_msg_rx_err(pf))
10577 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10578 				 event, queue, func);
10579 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10580 		mdd_detected = true;
10581 	}
10582 
10583 	if (mdd_detected) {
10584 		reg = rd32(hw, I40E_PF_MDET_TX);
10585 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10586 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10587 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10588 		}
10589 		reg = rd32(hw, I40E_PF_MDET_RX);
10590 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10591 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10592 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10593 		}
10594 	}
10595 
10596 	/* see if one of the VFs needs its hand slapped */
10597 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10598 		vf = &(pf->vf[i]);
10599 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10600 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10601 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10602 			vf->num_mdd_events++;
10603 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10604 				 i);
10605 			dev_info(&pf->pdev->dev,
10606 				 "Use PF Control I/F to re-enable the VF\n");
10607 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10608 		}
10609 
10610 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10611 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10612 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10613 			vf->num_mdd_events++;
10614 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10615 				 i);
10616 			dev_info(&pf->pdev->dev,
10617 				 "Use PF Control I/F to re-enable the VF\n");
10618 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10619 		}
10620 	}
10621 
10622 	/* re-enable mdd interrupt cause */
10623 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10624 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10625 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10626 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10627 	i40e_flush(hw);
10628 }
10629 
10630 /**
10631  * i40e_service_task - Run the driver's async subtasks
10632  * @work: pointer to work_struct containing our data
10633  **/
i40e_service_task(struct work_struct * work)10634 static void i40e_service_task(struct work_struct *work)
10635 {
10636 	struct i40e_pf *pf = container_of(work,
10637 					  struct i40e_pf,
10638 					  service_task);
10639 	unsigned long start_time = jiffies;
10640 
10641 	/* don't bother with service tasks if a reset is in progress */
10642 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10643 	    test_bit(__I40E_SUSPENDED, pf->state))
10644 		return;
10645 
10646 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10647 		return;
10648 
10649 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10650 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10651 		i40e_sync_filters_subtask(pf);
10652 		i40e_reset_subtask(pf);
10653 		i40e_handle_mdd_event(pf);
10654 		i40e_vc_process_vflr_event(pf);
10655 		i40e_watchdog_subtask(pf);
10656 		i40e_fdir_reinit_subtask(pf);
10657 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10658 			/* Client subtask will reopen next time through. */
10659 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10660 							   true);
10661 		} else {
10662 			i40e_client_subtask(pf);
10663 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10664 					       pf->state))
10665 				i40e_notify_client_of_l2_param_changes(
10666 								pf->vsi[pf->lan_vsi]);
10667 		}
10668 		i40e_sync_filters_subtask(pf);
10669 	} else {
10670 		i40e_reset_subtask(pf);
10671 	}
10672 
10673 	i40e_clean_adminq_subtask(pf);
10674 
10675 	/* flush memory to make sure state is correct before next watchdog */
10676 	smp_mb__before_atomic();
10677 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10678 
10679 	/* If the tasks have taken longer than one timer cycle or there
10680 	 * is more work to be done, reschedule the service task now
10681 	 * rather than wait for the timer to tick again.
10682 	 */
10683 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10684 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10685 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10686 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10687 		i40e_service_event_schedule(pf);
10688 }
10689 
10690 /**
10691  * i40e_service_timer - timer callback
10692  * @t: timer list pointer
10693  **/
i40e_service_timer(struct timer_list * t)10694 static void i40e_service_timer(struct timer_list *t)
10695 {
10696 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10697 
10698 	mod_timer(&pf->service_timer,
10699 		  round_jiffies(jiffies + pf->service_timer_period));
10700 	i40e_service_event_schedule(pf);
10701 }
10702 
10703 /**
10704  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10705  * @vsi: the VSI being configured
10706  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)10707 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10708 {
10709 	struct i40e_pf *pf = vsi->back;
10710 
10711 	switch (vsi->type) {
10712 	case I40E_VSI_MAIN:
10713 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10714 		if (!vsi->num_tx_desc)
10715 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10716 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10717 		if (!vsi->num_rx_desc)
10718 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10719 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10720 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10721 			vsi->num_q_vectors = pf->num_lan_msix;
10722 		else
10723 			vsi->num_q_vectors = 1;
10724 
10725 		break;
10726 
10727 	case I40E_VSI_FDIR:
10728 		vsi->alloc_queue_pairs = 1;
10729 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10730 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10731 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10732 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10733 		vsi->num_q_vectors = pf->num_fdsb_msix;
10734 		break;
10735 
10736 	case I40E_VSI_VMDQ2:
10737 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10738 		if (!vsi->num_tx_desc)
10739 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10740 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10741 		if (!vsi->num_rx_desc)
10742 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10743 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10744 		vsi->num_q_vectors = pf->num_vmdq_msix;
10745 		break;
10746 
10747 	case I40E_VSI_SRIOV:
10748 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10749 		if (!vsi->num_tx_desc)
10750 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10751 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10752 		if (!vsi->num_rx_desc)
10753 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10754 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10755 		break;
10756 
10757 	default:
10758 		WARN_ON(1);
10759 		return -ENODATA;
10760 	}
10761 
10762 	return 0;
10763 }
10764 
10765 /**
10766  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10767  * @vsi: VSI pointer
10768  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10769  *
10770  * On error: returns error code (negative)
10771  * On success: returns 0
10772  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)10773 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10774 {
10775 	struct i40e_ring **next_rings;
10776 	int size;
10777 	int ret = 0;
10778 
10779 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10780 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10781 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10782 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10783 	if (!vsi->tx_rings)
10784 		return -ENOMEM;
10785 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10786 	if (i40e_enabled_xdp_vsi(vsi)) {
10787 		vsi->xdp_rings = next_rings;
10788 		next_rings += vsi->alloc_queue_pairs;
10789 	}
10790 	vsi->rx_rings = next_rings;
10791 
10792 	if (alloc_qvectors) {
10793 		/* allocate memory for q_vector pointers */
10794 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10795 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10796 		if (!vsi->q_vectors) {
10797 			ret = -ENOMEM;
10798 			goto err_vectors;
10799 		}
10800 	}
10801 	return ret;
10802 
10803 err_vectors:
10804 	kfree(vsi->tx_rings);
10805 	return ret;
10806 }
10807 
10808 /**
10809  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10810  * @pf: board private structure
10811  * @type: type of VSI
10812  *
10813  * On error: returns error code (negative)
10814  * On success: returns vsi index in PF (positive)
10815  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)10816 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10817 {
10818 	int ret = -ENODEV;
10819 	struct i40e_vsi *vsi;
10820 	int vsi_idx;
10821 	int i;
10822 
10823 	/* Need to protect the allocation of the VSIs at the PF level */
10824 	mutex_lock(&pf->switch_mutex);
10825 
10826 	/* VSI list may be fragmented if VSI creation/destruction has
10827 	 * been happening.  We can afford to do a quick scan to look
10828 	 * for any free VSIs in the list.
10829 	 *
10830 	 * find next empty vsi slot, looping back around if necessary
10831 	 */
10832 	i = pf->next_vsi;
10833 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10834 		i++;
10835 	if (i >= pf->num_alloc_vsi) {
10836 		i = 0;
10837 		while (i < pf->next_vsi && pf->vsi[i])
10838 			i++;
10839 	}
10840 
10841 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10842 		vsi_idx = i;             /* Found one! */
10843 	} else {
10844 		ret = -ENODEV;
10845 		goto unlock_pf;  /* out of VSI slots! */
10846 	}
10847 	pf->next_vsi = ++i;
10848 
10849 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10850 	if (!vsi) {
10851 		ret = -ENOMEM;
10852 		goto unlock_pf;
10853 	}
10854 	vsi->type = type;
10855 	vsi->back = pf;
10856 	set_bit(__I40E_VSI_DOWN, vsi->state);
10857 	vsi->flags = 0;
10858 	vsi->idx = vsi_idx;
10859 	vsi->int_rate_limit = 0;
10860 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10861 				pf->rss_table_size : 64;
10862 	vsi->netdev_registered = false;
10863 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10864 	hash_init(vsi->mac_filter_hash);
10865 	vsi->irqs_ready = false;
10866 
10867 	if (type == I40E_VSI_MAIN) {
10868 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10869 		if (!vsi->af_xdp_zc_qps)
10870 			goto err_rings;
10871 	}
10872 
10873 	ret = i40e_set_num_rings_in_vsi(vsi);
10874 	if (ret)
10875 		goto err_rings;
10876 
10877 	ret = i40e_vsi_alloc_arrays(vsi, true);
10878 	if (ret)
10879 		goto err_rings;
10880 
10881 	/* Setup default MSIX irq handler for VSI */
10882 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10883 
10884 	/* Initialize VSI lock */
10885 	spin_lock_init(&vsi->mac_filter_hash_lock);
10886 	pf->vsi[vsi_idx] = vsi;
10887 	ret = vsi_idx;
10888 	goto unlock_pf;
10889 
10890 err_rings:
10891 	bitmap_free(vsi->af_xdp_zc_qps);
10892 	pf->next_vsi = i - 1;
10893 	kfree(vsi);
10894 unlock_pf:
10895 	mutex_unlock(&pf->switch_mutex);
10896 	return ret;
10897 }
10898 
10899 /**
10900  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10901  * @vsi: VSI pointer
10902  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10903  *
10904  * On error: returns error code (negative)
10905  * On success: returns 0
10906  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)10907 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10908 {
10909 	/* free the ring and vector containers */
10910 	if (free_qvectors) {
10911 		kfree(vsi->q_vectors);
10912 		vsi->q_vectors = NULL;
10913 	}
10914 	kfree(vsi->tx_rings);
10915 	vsi->tx_rings = NULL;
10916 	vsi->rx_rings = NULL;
10917 	vsi->xdp_rings = NULL;
10918 }
10919 
10920 /**
10921  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10922  * and lookup table
10923  * @vsi: Pointer to VSI structure
10924  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)10925 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10926 {
10927 	if (!vsi)
10928 		return;
10929 
10930 	kfree(vsi->rss_hkey_user);
10931 	vsi->rss_hkey_user = NULL;
10932 
10933 	kfree(vsi->rss_lut_user);
10934 	vsi->rss_lut_user = NULL;
10935 }
10936 
10937 /**
10938  * i40e_vsi_clear - Deallocate the VSI provided
10939  * @vsi: the VSI being un-configured
10940  **/
i40e_vsi_clear(struct i40e_vsi * vsi)10941 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10942 {
10943 	struct i40e_pf *pf;
10944 
10945 	if (!vsi)
10946 		return 0;
10947 
10948 	if (!vsi->back)
10949 		goto free_vsi;
10950 	pf = vsi->back;
10951 
10952 	mutex_lock(&pf->switch_mutex);
10953 	if (!pf->vsi[vsi->idx]) {
10954 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10955 			vsi->idx, vsi->idx, vsi->type);
10956 		goto unlock_vsi;
10957 	}
10958 
10959 	if (pf->vsi[vsi->idx] != vsi) {
10960 		dev_err(&pf->pdev->dev,
10961 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10962 			pf->vsi[vsi->idx]->idx,
10963 			pf->vsi[vsi->idx]->type,
10964 			vsi->idx, vsi->type);
10965 		goto unlock_vsi;
10966 	}
10967 
10968 	/* updates the PF for this cleared vsi */
10969 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10970 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10971 
10972 	bitmap_free(vsi->af_xdp_zc_qps);
10973 	i40e_vsi_free_arrays(vsi, true);
10974 	i40e_clear_rss_config_user(vsi);
10975 
10976 	pf->vsi[vsi->idx] = NULL;
10977 	if (vsi->idx < pf->next_vsi)
10978 		pf->next_vsi = vsi->idx;
10979 
10980 unlock_vsi:
10981 	mutex_unlock(&pf->switch_mutex);
10982 free_vsi:
10983 	kfree(vsi);
10984 
10985 	return 0;
10986 }
10987 
10988 /**
10989  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10990  * @vsi: the VSI being cleaned
10991  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)10992 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10993 {
10994 	int i;
10995 
10996 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10997 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10998 			kfree_rcu(vsi->tx_rings[i], rcu);
10999 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11000 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11001 			if (vsi->xdp_rings)
11002 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11003 		}
11004 	}
11005 }
11006 
11007 /**
11008  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11009  * @vsi: the VSI being configured
11010  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11011 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11012 {
11013 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11014 	struct i40e_pf *pf = vsi->back;
11015 	struct i40e_ring *ring;
11016 
11017 	/* Set basic values in the rings to be used later during open() */
11018 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11019 		/* allocate space for both Tx and Rx in one shot */
11020 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11021 		if (!ring)
11022 			goto err_out;
11023 
11024 		ring->queue_index = i;
11025 		ring->reg_idx = vsi->base_queue + i;
11026 		ring->ring_active = false;
11027 		ring->vsi = vsi;
11028 		ring->netdev = vsi->netdev;
11029 		ring->dev = &pf->pdev->dev;
11030 		ring->count = vsi->num_tx_desc;
11031 		ring->size = 0;
11032 		ring->dcb_tc = 0;
11033 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11034 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11035 		ring->itr_setting = pf->tx_itr_default;
11036 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11037 
11038 		if (!i40e_enabled_xdp_vsi(vsi))
11039 			goto setup_rx;
11040 
11041 		ring->queue_index = vsi->alloc_queue_pairs + i;
11042 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11043 		ring->ring_active = false;
11044 		ring->vsi = vsi;
11045 		ring->netdev = NULL;
11046 		ring->dev = &pf->pdev->dev;
11047 		ring->count = vsi->num_tx_desc;
11048 		ring->size = 0;
11049 		ring->dcb_tc = 0;
11050 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11051 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11052 		set_ring_xdp(ring);
11053 		ring->itr_setting = pf->tx_itr_default;
11054 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11055 
11056 setup_rx:
11057 		ring->queue_index = i;
11058 		ring->reg_idx = vsi->base_queue + i;
11059 		ring->ring_active = false;
11060 		ring->vsi = vsi;
11061 		ring->netdev = vsi->netdev;
11062 		ring->dev = &pf->pdev->dev;
11063 		ring->count = vsi->num_rx_desc;
11064 		ring->size = 0;
11065 		ring->dcb_tc = 0;
11066 		ring->itr_setting = pf->rx_itr_default;
11067 		WRITE_ONCE(vsi->rx_rings[i], ring);
11068 	}
11069 
11070 	return 0;
11071 
11072 err_out:
11073 	i40e_vsi_clear_rings(vsi);
11074 	return -ENOMEM;
11075 }
11076 
11077 /**
11078  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11079  * @pf: board private structure
11080  * @vectors: the number of MSI-X vectors to request
11081  *
11082  * Returns the number of vectors reserved, or error
11083  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11084 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11085 {
11086 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11087 					I40E_MIN_MSIX, vectors);
11088 	if (vectors < 0) {
11089 		dev_info(&pf->pdev->dev,
11090 			 "MSI-X vector reservation failed: %d\n", vectors);
11091 		vectors = 0;
11092 	}
11093 
11094 	return vectors;
11095 }
11096 
11097 /**
11098  * i40e_init_msix - Setup the MSIX capability
11099  * @pf: board private structure
11100  *
11101  * Work with the OS to set up the MSIX vectors needed.
11102  *
11103  * Returns the number of vectors reserved or negative on failure
11104  **/
i40e_init_msix(struct i40e_pf * pf)11105 static int i40e_init_msix(struct i40e_pf *pf)
11106 {
11107 	struct i40e_hw *hw = &pf->hw;
11108 	int cpus, extra_vectors;
11109 	int vectors_left;
11110 	int v_budget, i;
11111 	int v_actual;
11112 	int iwarp_requested = 0;
11113 
11114 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11115 		return -ENODEV;
11116 
11117 	/* The number of vectors we'll request will be comprised of:
11118 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11119 	 *   - The number of LAN queue pairs
11120 	 *	- Queues being used for RSS.
11121 	 *		We don't need as many as max_rss_size vectors.
11122 	 *		use rss_size instead in the calculation since that
11123 	 *		is governed by number of cpus in the system.
11124 	 *	- assumes symmetric Tx/Rx pairing
11125 	 *   - The number of VMDq pairs
11126 	 *   - The CPU count within the NUMA node if iWARP is enabled
11127 	 * Once we count this up, try the request.
11128 	 *
11129 	 * If we can't get what we want, we'll simplify to nearly nothing
11130 	 * and try again.  If that still fails, we punt.
11131 	 */
11132 	vectors_left = hw->func_caps.num_msix_vectors;
11133 	v_budget = 0;
11134 
11135 	/* reserve one vector for miscellaneous handler */
11136 	if (vectors_left) {
11137 		v_budget++;
11138 		vectors_left--;
11139 	}
11140 
11141 	/* reserve some vectors for the main PF traffic queues. Initially we
11142 	 * only reserve at most 50% of the available vectors, in the case that
11143 	 * the number of online CPUs is large. This ensures that we can enable
11144 	 * extra features as well. Once we've enabled the other features, we
11145 	 * will use any remaining vectors to reach as close as we can to the
11146 	 * number of online CPUs.
11147 	 */
11148 	cpus = num_online_cpus();
11149 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11150 	vectors_left -= pf->num_lan_msix;
11151 
11152 	/* reserve one vector for sideband flow director */
11153 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11154 		if (vectors_left) {
11155 			pf->num_fdsb_msix = 1;
11156 			v_budget++;
11157 			vectors_left--;
11158 		} else {
11159 			pf->num_fdsb_msix = 0;
11160 		}
11161 	}
11162 
11163 	/* can we reserve enough for iWARP? */
11164 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11165 		iwarp_requested = pf->num_iwarp_msix;
11166 
11167 		if (!vectors_left)
11168 			pf->num_iwarp_msix = 0;
11169 		else if (vectors_left < pf->num_iwarp_msix)
11170 			pf->num_iwarp_msix = 1;
11171 		v_budget += pf->num_iwarp_msix;
11172 		vectors_left -= pf->num_iwarp_msix;
11173 	}
11174 
11175 	/* any vectors left over go for VMDq support */
11176 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11177 		if (!vectors_left) {
11178 			pf->num_vmdq_msix = 0;
11179 			pf->num_vmdq_qps = 0;
11180 		} else {
11181 			int vmdq_vecs_wanted =
11182 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11183 			int vmdq_vecs =
11184 				min_t(int, vectors_left, vmdq_vecs_wanted);
11185 
11186 			/* if we're short on vectors for what's desired, we limit
11187 			 * the queues per vmdq.  If this is still more than are
11188 			 * available, the user will need to change the number of
11189 			 * queues/vectors used by the PF later with the ethtool
11190 			 * channels command
11191 			 */
11192 			if (vectors_left < vmdq_vecs_wanted) {
11193 				pf->num_vmdq_qps = 1;
11194 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11195 				vmdq_vecs = min_t(int,
11196 						  vectors_left,
11197 						  vmdq_vecs_wanted);
11198 			}
11199 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11200 
11201 			v_budget += vmdq_vecs;
11202 			vectors_left -= vmdq_vecs;
11203 		}
11204 	}
11205 
11206 	/* On systems with a large number of SMP cores, we previously limited
11207 	 * the number of vectors for num_lan_msix to be at most 50% of the
11208 	 * available vectors, to allow for other features. Now, we add back
11209 	 * the remaining vectors. However, we ensure that the total
11210 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11211 	 * calculate the number of vectors we can add without going over the
11212 	 * cap of CPUs. For systems with a small number of CPUs this will be
11213 	 * zero.
11214 	 */
11215 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11216 	pf->num_lan_msix += extra_vectors;
11217 	vectors_left -= extra_vectors;
11218 
11219 	WARN(vectors_left < 0,
11220 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11221 
11222 	v_budget += pf->num_lan_msix;
11223 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11224 				   GFP_KERNEL);
11225 	if (!pf->msix_entries)
11226 		return -ENOMEM;
11227 
11228 	for (i = 0; i < v_budget; i++)
11229 		pf->msix_entries[i].entry = i;
11230 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11231 
11232 	if (v_actual < I40E_MIN_MSIX) {
11233 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11234 		kfree(pf->msix_entries);
11235 		pf->msix_entries = NULL;
11236 		pci_disable_msix(pf->pdev);
11237 		return -ENODEV;
11238 
11239 	} else if (v_actual == I40E_MIN_MSIX) {
11240 		/* Adjust for minimal MSIX use */
11241 		pf->num_vmdq_vsis = 0;
11242 		pf->num_vmdq_qps = 0;
11243 		pf->num_lan_qps = 1;
11244 		pf->num_lan_msix = 1;
11245 
11246 	} else if (v_actual != v_budget) {
11247 		/* If we have limited resources, we will start with no vectors
11248 		 * for the special features and then allocate vectors to some
11249 		 * of these features based on the policy and at the end disable
11250 		 * the features that did not get any vectors.
11251 		 */
11252 		int vec;
11253 
11254 		dev_info(&pf->pdev->dev,
11255 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11256 			 v_actual, v_budget);
11257 		/* reserve the misc vector */
11258 		vec = v_actual - 1;
11259 
11260 		/* Scale vector usage down */
11261 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11262 		pf->num_vmdq_vsis = 1;
11263 		pf->num_vmdq_qps = 1;
11264 
11265 		/* partition out the remaining vectors */
11266 		switch (vec) {
11267 		case 2:
11268 			pf->num_lan_msix = 1;
11269 			break;
11270 		case 3:
11271 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11272 				pf->num_lan_msix = 1;
11273 				pf->num_iwarp_msix = 1;
11274 			} else {
11275 				pf->num_lan_msix = 2;
11276 			}
11277 			break;
11278 		default:
11279 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11280 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11281 						 iwarp_requested);
11282 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11283 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11284 			} else {
11285 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11286 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11287 			}
11288 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11289 				pf->num_fdsb_msix = 1;
11290 				vec--;
11291 			}
11292 			pf->num_lan_msix = min_t(int,
11293 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11294 							      pf->num_lan_msix);
11295 			pf->num_lan_qps = pf->num_lan_msix;
11296 			break;
11297 		}
11298 	}
11299 
11300 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11301 	    (pf->num_fdsb_msix == 0)) {
11302 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11303 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11304 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11305 	}
11306 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11307 	    (pf->num_vmdq_msix == 0)) {
11308 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11309 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11310 	}
11311 
11312 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11313 	    (pf->num_iwarp_msix == 0)) {
11314 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11315 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11316 	}
11317 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11318 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11319 		   pf->num_lan_msix,
11320 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11321 		   pf->num_fdsb_msix,
11322 		   pf->num_iwarp_msix);
11323 
11324 	return v_actual;
11325 }
11326 
11327 /**
11328  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11329  * @vsi: the VSI being configured
11330  * @v_idx: index of the vector in the vsi struct
11331  *
11332  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11333  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)11334 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11335 {
11336 	struct i40e_q_vector *q_vector;
11337 
11338 	/* allocate q_vector */
11339 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11340 	if (!q_vector)
11341 		return -ENOMEM;
11342 
11343 	q_vector->vsi = vsi;
11344 	q_vector->v_idx = v_idx;
11345 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11346 
11347 	if (vsi->netdev)
11348 		netif_napi_add(vsi->netdev, &q_vector->napi,
11349 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11350 
11351 	/* tie q_vector and vsi together */
11352 	vsi->q_vectors[v_idx] = q_vector;
11353 
11354 	return 0;
11355 }
11356 
11357 /**
11358  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11359  * @vsi: the VSI being configured
11360  *
11361  * We allocate one q_vector per queue interrupt.  If allocation fails we
11362  * return -ENOMEM.
11363  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)11364 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11365 {
11366 	struct i40e_pf *pf = vsi->back;
11367 	int err, v_idx, num_q_vectors;
11368 
11369 	/* if not MSIX, give the one vector only to the LAN VSI */
11370 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11371 		num_q_vectors = vsi->num_q_vectors;
11372 	else if (vsi == pf->vsi[pf->lan_vsi])
11373 		num_q_vectors = 1;
11374 	else
11375 		return -EINVAL;
11376 
11377 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11378 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11379 		if (err)
11380 			goto err_out;
11381 	}
11382 
11383 	return 0;
11384 
11385 err_out:
11386 	while (v_idx--)
11387 		i40e_free_q_vector(vsi, v_idx);
11388 
11389 	return err;
11390 }
11391 
11392 /**
11393  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11394  * @pf: board private structure to initialize
11395  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)11396 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11397 {
11398 	int vectors = 0;
11399 	ssize_t size;
11400 
11401 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11402 		vectors = i40e_init_msix(pf);
11403 		if (vectors < 0) {
11404 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11405 				       I40E_FLAG_IWARP_ENABLED	|
11406 				       I40E_FLAG_RSS_ENABLED	|
11407 				       I40E_FLAG_DCB_CAPABLE	|
11408 				       I40E_FLAG_DCB_ENABLED	|
11409 				       I40E_FLAG_SRIOV_ENABLED	|
11410 				       I40E_FLAG_FD_SB_ENABLED	|
11411 				       I40E_FLAG_FD_ATR_ENABLED	|
11412 				       I40E_FLAG_VMDQ_ENABLED);
11413 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11414 
11415 			/* rework the queue expectations without MSIX */
11416 			i40e_determine_queue_usage(pf);
11417 		}
11418 	}
11419 
11420 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11421 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11422 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11423 		vectors = pci_enable_msi(pf->pdev);
11424 		if (vectors < 0) {
11425 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11426 				 vectors);
11427 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11428 		}
11429 		vectors = 1;  /* one MSI or Legacy vector */
11430 	}
11431 
11432 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11433 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11434 
11435 	/* set up vector assignment tracking */
11436 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11437 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11438 	if (!pf->irq_pile)
11439 		return -ENOMEM;
11440 
11441 	pf->irq_pile->num_entries = vectors;
11442 
11443 	/* track first vector for misc interrupts, ignore return */
11444 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11445 
11446 	return 0;
11447 }
11448 
11449 /**
11450  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11451  * @pf: private board data structure
11452  *
11453  * Restore the interrupt scheme that was cleared when we suspended the
11454  * device. This should be called during resume to re-allocate the q_vectors
11455  * and reacquire IRQs.
11456  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)11457 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11458 {
11459 	int err, i;
11460 
11461 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11462 	 * scheme. We need to re-enabled them here in order to attempt to
11463 	 * re-acquire the MSI or MSI-X vectors
11464 	 */
11465 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11466 
11467 	err = i40e_init_interrupt_scheme(pf);
11468 	if (err)
11469 		return err;
11470 
11471 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11472 	 * rings together again.
11473 	 */
11474 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11475 		if (pf->vsi[i]) {
11476 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11477 			if (err)
11478 				goto err_unwind;
11479 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11480 		}
11481 	}
11482 
11483 	err = i40e_setup_misc_vector(pf);
11484 	if (err)
11485 		goto err_unwind;
11486 
11487 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11488 		i40e_client_update_msix_info(pf);
11489 
11490 	return 0;
11491 
11492 err_unwind:
11493 	while (i--) {
11494 		if (pf->vsi[i])
11495 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11496 	}
11497 
11498 	return err;
11499 }
11500 
11501 /**
11502  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11503  * non queue events in recovery mode
11504  * @pf: board private structure
11505  *
11506  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11507  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11508  * This is handled differently than in recovery mode since no Tx/Rx resources
11509  * are being allocated.
11510  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)11511 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11512 {
11513 	int err;
11514 
11515 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11516 		err = i40e_setup_misc_vector(pf);
11517 
11518 		if (err) {
11519 			dev_info(&pf->pdev->dev,
11520 				 "MSI-X misc vector request failed, error %d\n",
11521 				 err);
11522 			return err;
11523 		}
11524 	} else {
11525 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11526 
11527 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11528 				  pf->int_name, pf);
11529 
11530 		if (err) {
11531 			dev_info(&pf->pdev->dev,
11532 				 "MSI/legacy misc vector request failed, error %d\n",
11533 				 err);
11534 			return err;
11535 		}
11536 		i40e_enable_misc_int_causes(pf);
11537 		i40e_irq_dynamic_enable_icr0(pf);
11538 	}
11539 
11540 	return 0;
11541 }
11542 
11543 /**
11544  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11545  * @pf: board private structure
11546  *
11547  * This sets up the handler for MSIX 0, which is used to manage the
11548  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11549  * when in MSI or Legacy interrupt mode.
11550  **/
i40e_setup_misc_vector(struct i40e_pf * pf)11551 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11552 {
11553 	struct i40e_hw *hw = &pf->hw;
11554 	int err = 0;
11555 
11556 	/* Only request the IRQ once, the first time through. */
11557 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11558 		err = request_irq(pf->msix_entries[0].vector,
11559 				  i40e_intr, 0, pf->int_name, pf);
11560 		if (err) {
11561 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11562 			dev_info(&pf->pdev->dev,
11563 				 "request_irq for %s failed: %d\n",
11564 				 pf->int_name, err);
11565 			return -EFAULT;
11566 		}
11567 	}
11568 
11569 	i40e_enable_misc_int_causes(pf);
11570 
11571 	/* associate no queues to the misc vector */
11572 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11573 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11574 
11575 	i40e_flush(hw);
11576 
11577 	i40e_irq_dynamic_enable_icr0(pf);
11578 
11579 	return err;
11580 }
11581 
11582 /**
11583  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11584  * @vsi: Pointer to vsi structure
11585  * @seed: Buffter to store the hash keys
11586  * @lut: Buffer to store the lookup table entries
11587  * @lut_size: Size of buffer to store the lookup table entries
11588  *
11589  * Return 0 on success, negative on failure
11590  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)11591 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11592 			   u8 *lut, u16 lut_size)
11593 {
11594 	struct i40e_pf *pf = vsi->back;
11595 	struct i40e_hw *hw = &pf->hw;
11596 	int ret = 0;
11597 
11598 	if (seed) {
11599 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11600 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11601 		if (ret) {
11602 			dev_info(&pf->pdev->dev,
11603 				 "Cannot get RSS key, err %s aq_err %s\n",
11604 				 i40e_stat_str(&pf->hw, ret),
11605 				 i40e_aq_str(&pf->hw,
11606 					     pf->hw.aq.asq_last_status));
11607 			return ret;
11608 		}
11609 	}
11610 
11611 	if (lut) {
11612 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11613 
11614 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11615 		if (ret) {
11616 			dev_info(&pf->pdev->dev,
11617 				 "Cannot get RSS lut, err %s aq_err %s\n",
11618 				 i40e_stat_str(&pf->hw, ret),
11619 				 i40e_aq_str(&pf->hw,
11620 					     pf->hw.aq.asq_last_status));
11621 			return ret;
11622 		}
11623 	}
11624 
11625 	return ret;
11626 }
11627 
11628 /**
11629  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11630  * @vsi: Pointer to vsi structure
11631  * @seed: RSS hash seed
11632  * @lut: Lookup table
11633  * @lut_size: Lookup table size
11634  *
11635  * Returns 0 on success, negative on failure
11636  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)11637 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11638 			       const u8 *lut, u16 lut_size)
11639 {
11640 	struct i40e_pf *pf = vsi->back;
11641 	struct i40e_hw *hw = &pf->hw;
11642 	u16 vf_id = vsi->vf_id;
11643 	u8 i;
11644 
11645 	/* Fill out hash function seed */
11646 	if (seed) {
11647 		u32 *seed_dw = (u32 *)seed;
11648 
11649 		if (vsi->type == I40E_VSI_MAIN) {
11650 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11651 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11652 		} else if (vsi->type == I40E_VSI_SRIOV) {
11653 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11654 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11655 		} else {
11656 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11657 		}
11658 	}
11659 
11660 	if (lut) {
11661 		u32 *lut_dw = (u32 *)lut;
11662 
11663 		if (vsi->type == I40E_VSI_MAIN) {
11664 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11665 				return -EINVAL;
11666 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11667 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11668 		} else if (vsi->type == I40E_VSI_SRIOV) {
11669 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11670 				return -EINVAL;
11671 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11672 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11673 		} else {
11674 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11675 		}
11676 	}
11677 	i40e_flush(hw);
11678 
11679 	return 0;
11680 }
11681 
11682 /**
11683  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11684  * @vsi: Pointer to VSI structure
11685  * @seed: Buffer to store the keys
11686  * @lut: Buffer to store the lookup table entries
11687  * @lut_size: Size of buffer to store the lookup table entries
11688  *
11689  * Returns 0 on success, negative on failure
11690  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11691 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11692 			    u8 *lut, u16 lut_size)
11693 {
11694 	struct i40e_pf *pf = vsi->back;
11695 	struct i40e_hw *hw = &pf->hw;
11696 	u16 i;
11697 
11698 	if (seed) {
11699 		u32 *seed_dw = (u32 *)seed;
11700 
11701 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11702 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11703 	}
11704 	if (lut) {
11705 		u32 *lut_dw = (u32 *)lut;
11706 
11707 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11708 			return -EINVAL;
11709 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11710 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11711 	}
11712 
11713 	return 0;
11714 }
11715 
11716 /**
11717  * i40e_config_rss - Configure RSS keys and lut
11718  * @vsi: Pointer to VSI structure
11719  * @seed: RSS hash seed
11720  * @lut: Lookup table
11721  * @lut_size: Lookup table size
11722  *
11723  * Returns 0 on success, negative on failure
11724  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11725 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11726 {
11727 	struct i40e_pf *pf = vsi->back;
11728 
11729 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11730 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11731 	else
11732 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11733 }
11734 
11735 /**
11736  * i40e_get_rss - Get RSS keys and lut
11737  * @vsi: Pointer to VSI structure
11738  * @seed: Buffer to store the keys
11739  * @lut: Buffer to store the lookup table entries
11740  * @lut_size: Size of buffer to store the lookup table entries
11741  *
11742  * Returns 0 on success, negative on failure
11743  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11744 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11745 {
11746 	struct i40e_pf *pf = vsi->back;
11747 
11748 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11749 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11750 	else
11751 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11752 }
11753 
11754 /**
11755  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11756  * @pf: Pointer to board private structure
11757  * @lut: Lookup table
11758  * @rss_table_size: Lookup table size
11759  * @rss_size: Range of queue number for hashing
11760  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)11761 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11762 		       u16 rss_table_size, u16 rss_size)
11763 {
11764 	u16 i;
11765 
11766 	for (i = 0; i < rss_table_size; i++)
11767 		lut[i] = i % rss_size;
11768 }
11769 
11770 /**
11771  * i40e_pf_config_rss - Prepare for RSS if used
11772  * @pf: board private structure
11773  **/
i40e_pf_config_rss(struct i40e_pf * pf)11774 static int i40e_pf_config_rss(struct i40e_pf *pf)
11775 {
11776 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11777 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11778 	u8 *lut;
11779 	struct i40e_hw *hw = &pf->hw;
11780 	u32 reg_val;
11781 	u64 hena;
11782 	int ret;
11783 
11784 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11785 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11786 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11787 	hena |= i40e_pf_get_default_rss_hena(pf);
11788 
11789 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11790 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11791 
11792 	/* Determine the RSS table size based on the hardware capabilities */
11793 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11794 	reg_val = (pf->rss_table_size == 512) ?
11795 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11796 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11797 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11798 
11799 	/* Determine the RSS size of the VSI */
11800 	if (!vsi->rss_size) {
11801 		u16 qcount;
11802 		/* If the firmware does something weird during VSI init, we
11803 		 * could end up with zero TCs. Check for that to avoid
11804 		 * divide-by-zero. It probably won't pass traffic, but it also
11805 		 * won't panic.
11806 		 */
11807 		qcount = vsi->num_queue_pairs /
11808 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11809 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11810 	}
11811 	if (!vsi->rss_size)
11812 		return -EINVAL;
11813 
11814 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11815 	if (!lut)
11816 		return -ENOMEM;
11817 
11818 	/* Use user configured lut if there is one, otherwise use default */
11819 	if (vsi->rss_lut_user)
11820 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11821 	else
11822 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11823 
11824 	/* Use user configured hash key if there is one, otherwise
11825 	 * use default.
11826 	 */
11827 	if (vsi->rss_hkey_user)
11828 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11829 	else
11830 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11831 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11832 	kfree(lut);
11833 
11834 	return ret;
11835 }
11836 
11837 /**
11838  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11839  * @pf: board private structure
11840  * @queue_count: the requested queue count for rss.
11841  *
11842  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11843  * count which may be different from the requested queue count.
11844  * Note: expects to be called while under rtnl_lock()
11845  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)11846 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11847 {
11848 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11849 	int new_rss_size;
11850 
11851 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11852 		return 0;
11853 
11854 	queue_count = min_t(int, queue_count, num_online_cpus());
11855 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11856 
11857 	if (queue_count != vsi->num_queue_pairs) {
11858 		u16 qcount;
11859 
11860 		vsi->req_queue_pairs = queue_count;
11861 		i40e_prep_for_reset(pf, true);
11862 
11863 		pf->alloc_rss_size = new_rss_size;
11864 
11865 		i40e_reset_and_rebuild(pf, true, true);
11866 
11867 		/* Discard the user configured hash keys and lut, if less
11868 		 * queues are enabled.
11869 		 */
11870 		if (queue_count < vsi->rss_size) {
11871 			i40e_clear_rss_config_user(vsi);
11872 			dev_dbg(&pf->pdev->dev,
11873 				"discard user configured hash keys and lut\n");
11874 		}
11875 
11876 		/* Reset vsi->rss_size, as number of enabled queues changed */
11877 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11878 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11879 
11880 		i40e_pf_config_rss(pf);
11881 	}
11882 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11883 		 vsi->req_queue_pairs, pf->rss_size_max);
11884 	return pf->alloc_rss_size;
11885 }
11886 
11887 /**
11888  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11889  * @pf: board private structure
11890  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)11891 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11892 {
11893 	i40e_status status;
11894 	bool min_valid, max_valid;
11895 	u32 max_bw, min_bw;
11896 
11897 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11898 					   &min_valid, &max_valid);
11899 
11900 	if (!status) {
11901 		if (min_valid)
11902 			pf->min_bw = min_bw;
11903 		if (max_valid)
11904 			pf->max_bw = max_bw;
11905 	}
11906 
11907 	return status;
11908 }
11909 
11910 /**
11911  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11912  * @pf: board private structure
11913  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)11914 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11915 {
11916 	struct i40e_aqc_configure_partition_bw_data bw_data;
11917 	i40e_status status;
11918 
11919 	memset(&bw_data, 0, sizeof(bw_data));
11920 
11921 	/* Set the valid bit for this PF */
11922 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11923 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11924 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11925 
11926 	/* Set the new bandwidths */
11927 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11928 
11929 	return status;
11930 }
11931 
11932 /**
11933  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11934  * @pf: board private structure
11935  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)11936 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11937 {
11938 	/* Commit temporary BW setting to permanent NVM image */
11939 	enum i40e_admin_queue_err last_aq_status;
11940 	i40e_status ret;
11941 	u16 nvm_word;
11942 
11943 	if (pf->hw.partition_id != 1) {
11944 		dev_info(&pf->pdev->dev,
11945 			 "Commit BW only works on partition 1! This is partition %d",
11946 			 pf->hw.partition_id);
11947 		ret = I40E_NOT_SUPPORTED;
11948 		goto bw_commit_out;
11949 	}
11950 
11951 	/* Acquire NVM for read access */
11952 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11953 	last_aq_status = pf->hw.aq.asq_last_status;
11954 	if (ret) {
11955 		dev_info(&pf->pdev->dev,
11956 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11957 			 i40e_stat_str(&pf->hw, ret),
11958 			 i40e_aq_str(&pf->hw, last_aq_status));
11959 		goto bw_commit_out;
11960 	}
11961 
11962 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11963 	ret = i40e_aq_read_nvm(&pf->hw,
11964 			       I40E_SR_NVM_CONTROL_WORD,
11965 			       0x10, sizeof(nvm_word), &nvm_word,
11966 			       false, NULL);
11967 	/* Save off last admin queue command status before releasing
11968 	 * the NVM
11969 	 */
11970 	last_aq_status = pf->hw.aq.asq_last_status;
11971 	i40e_release_nvm(&pf->hw);
11972 	if (ret) {
11973 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11974 			 i40e_stat_str(&pf->hw, ret),
11975 			 i40e_aq_str(&pf->hw, last_aq_status));
11976 		goto bw_commit_out;
11977 	}
11978 
11979 	/* Wait a bit for NVM release to complete */
11980 	msleep(50);
11981 
11982 	/* Acquire NVM for write access */
11983 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11984 	last_aq_status = pf->hw.aq.asq_last_status;
11985 	if (ret) {
11986 		dev_info(&pf->pdev->dev,
11987 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11988 			 i40e_stat_str(&pf->hw, ret),
11989 			 i40e_aq_str(&pf->hw, last_aq_status));
11990 		goto bw_commit_out;
11991 	}
11992 	/* Write it back out unchanged to initiate update NVM,
11993 	 * which will force a write of the shadow (alt) RAM to
11994 	 * the NVM - thus storing the bandwidth values permanently.
11995 	 */
11996 	ret = i40e_aq_update_nvm(&pf->hw,
11997 				 I40E_SR_NVM_CONTROL_WORD,
11998 				 0x10, sizeof(nvm_word),
11999 				 &nvm_word, true, 0, NULL);
12000 	/* Save off last admin queue command status before releasing
12001 	 * the NVM
12002 	 */
12003 	last_aq_status = pf->hw.aq.asq_last_status;
12004 	i40e_release_nvm(&pf->hw);
12005 	if (ret)
12006 		dev_info(&pf->pdev->dev,
12007 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12008 			 i40e_stat_str(&pf->hw, ret),
12009 			 i40e_aq_str(&pf->hw, last_aq_status));
12010 bw_commit_out:
12011 
12012 	return ret;
12013 }
12014 
12015 /**
12016  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12017  * if total port shutdown feature is enabled for this PF
12018  * @pf: board private structure
12019  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12020 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12021 {
12022 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12023 #define I40E_FEATURES_ENABLE_PTR		0x2A
12024 #define I40E_CURRENT_SETTING_PTR		0x2B
12025 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12026 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12027 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12028 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12029 	i40e_status read_status = I40E_SUCCESS;
12030 	u16 sr_emp_sr_settings_ptr = 0;
12031 	u16 features_enable = 0;
12032 	u16 link_behavior = 0;
12033 	bool ret = false;
12034 
12035 	read_status = i40e_read_nvm_word(&pf->hw,
12036 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12037 					 &sr_emp_sr_settings_ptr);
12038 	if (read_status)
12039 		goto err_nvm;
12040 	read_status = i40e_read_nvm_word(&pf->hw,
12041 					 sr_emp_sr_settings_ptr +
12042 					 I40E_FEATURES_ENABLE_PTR,
12043 					 &features_enable);
12044 	if (read_status)
12045 		goto err_nvm;
12046 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12047 		read_status = i40e_read_nvm_module_data(&pf->hw,
12048 							I40E_SR_EMP_SR_SETTINGS_PTR,
12049 							I40E_CURRENT_SETTING_PTR,
12050 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12051 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12052 							&link_behavior);
12053 		if (read_status)
12054 			goto err_nvm;
12055 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12056 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12057 	}
12058 	return ret;
12059 
12060 err_nvm:
12061 	dev_warn(&pf->pdev->dev,
12062 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12063 		 i40e_stat_str(&pf->hw, read_status));
12064 	return ret;
12065 }
12066 
12067 /**
12068  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12069  * @pf: board private structure to initialize
12070  *
12071  * i40e_sw_init initializes the Adapter private data structure.
12072  * Fields are initialized based on PCI device information and
12073  * OS network device settings (MTU size).
12074  **/
i40e_sw_init(struct i40e_pf * pf)12075 static int i40e_sw_init(struct i40e_pf *pf)
12076 {
12077 	int err = 0;
12078 	int size;
12079 	u16 pow;
12080 
12081 	/* Set default capability flags */
12082 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12083 		    I40E_FLAG_MSI_ENABLED     |
12084 		    I40E_FLAG_MSIX_ENABLED;
12085 
12086 	/* Set default ITR */
12087 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12088 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12089 
12090 	/* Depending on PF configurations, it is possible that the RSS
12091 	 * maximum might end up larger than the available queues
12092 	 */
12093 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12094 	pf->alloc_rss_size = 1;
12095 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12096 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12097 				 pf->hw.func_caps.num_tx_qp);
12098 
12099 	/* find the next higher power-of-2 of num cpus */
12100 	pow = roundup_pow_of_two(num_online_cpus());
12101 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12102 
12103 	if (pf->hw.func_caps.rss) {
12104 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12105 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12106 					   num_online_cpus());
12107 	}
12108 
12109 	/* MFP mode enabled */
12110 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12111 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12112 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12113 		if (i40e_get_partition_bw_setting(pf)) {
12114 			dev_warn(&pf->pdev->dev,
12115 				 "Could not get partition bw settings\n");
12116 		} else {
12117 			dev_info(&pf->pdev->dev,
12118 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12119 				 pf->min_bw, pf->max_bw);
12120 
12121 			/* nudge the Tx scheduler */
12122 			i40e_set_partition_bw_setting(pf);
12123 		}
12124 	}
12125 
12126 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12127 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12128 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12129 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12130 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12131 		    pf->hw.num_partitions > 1)
12132 			dev_info(&pf->pdev->dev,
12133 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12134 		else
12135 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12136 		pf->fdir_pf_filter_count =
12137 				 pf->hw.func_caps.fd_filters_guaranteed;
12138 		pf->hw.fdir_shared_filter_count =
12139 				 pf->hw.func_caps.fd_filters_best_effort;
12140 	}
12141 
12142 	if (pf->hw.mac.type == I40E_MAC_X722) {
12143 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12144 				    I40E_HW_128_QP_RSS_CAPABLE |
12145 				    I40E_HW_ATR_EVICT_CAPABLE |
12146 				    I40E_HW_WB_ON_ITR_CAPABLE |
12147 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12148 				    I40E_HW_NO_PCI_LINK_CHECK |
12149 				    I40E_HW_USE_SET_LLDP_MIB |
12150 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12151 				    I40E_HW_PTP_L4_CAPABLE |
12152 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12153 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12154 
12155 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12156 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12157 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12158 			dev_warn(&pf->pdev->dev,
12159 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12160 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12161 		}
12162 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12163 		   ((pf->hw.aq.api_maj_ver == 1) &&
12164 		    (pf->hw.aq.api_min_ver > 4))) {
12165 		/* Supported in FW API version higher than 1.4 */
12166 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12167 	}
12168 
12169 	/* Enable HW ATR eviction if possible */
12170 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12171 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12172 
12173 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12174 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12175 	    (pf->hw.aq.fw_maj_ver < 4))) {
12176 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12177 		/* No DCB support  for FW < v4.33 */
12178 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12179 	}
12180 
12181 	/* Disable FW LLDP if FW < v4.3 */
12182 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12183 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12184 	    (pf->hw.aq.fw_maj_ver < 4)))
12185 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12186 
12187 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12188 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12189 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12190 	    (pf->hw.aq.fw_maj_ver >= 5)))
12191 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12192 
12193 	/* Enable PTP L4 if FW > v6.0 */
12194 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12195 	    pf->hw.aq.fw_maj_ver >= 6)
12196 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12197 
12198 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12199 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12200 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12201 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12202 	}
12203 
12204 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12205 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12206 		/* IWARP needs one extra vector for CQP just like MISC.*/
12207 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12208 	}
12209 	/* Stopping FW LLDP engine is supported on XL710 and X722
12210 	 * starting from FW versions determined in i40e_init_adminq.
12211 	 * Stopping the FW LLDP engine is not supported on XL710
12212 	 * if NPAR is functioning so unset this hw flag in this case.
12213 	 */
12214 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12215 	    pf->hw.func_caps.npar_enable &&
12216 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12217 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12218 
12219 #ifdef CONFIG_PCI_IOV
12220 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12221 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12222 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12223 		pf->num_req_vfs = min_t(int,
12224 					pf->hw.func_caps.num_vfs,
12225 					I40E_MAX_VF_COUNT);
12226 	}
12227 #endif /* CONFIG_PCI_IOV */
12228 	pf->eeprom_version = 0xDEAD;
12229 	pf->lan_veb = I40E_NO_VEB;
12230 	pf->lan_vsi = I40E_NO_VSI;
12231 
12232 	/* By default FW has this off for performance reasons */
12233 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12234 
12235 	/* set up queue assignment tracking */
12236 	size = sizeof(struct i40e_lump_tracking)
12237 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12238 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12239 	if (!pf->qp_pile) {
12240 		err = -ENOMEM;
12241 		goto sw_init_done;
12242 	}
12243 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12244 
12245 	pf->tx_timeout_recovery_level = 1;
12246 
12247 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12248 	    i40e_is_total_port_shutdown_enabled(pf)) {
12249 		/* Link down on close must be on when total port shutdown
12250 		 * is enabled for a given port
12251 		 */
12252 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12253 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12254 		dev_info(&pf->pdev->dev,
12255 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12256 	}
12257 	mutex_init(&pf->switch_mutex);
12258 
12259 sw_init_done:
12260 	return err;
12261 }
12262 
12263 /**
12264  * i40e_set_ntuple - set the ntuple feature flag and take action
12265  * @pf: board private structure to initialize
12266  * @features: the feature set that the stack is suggesting
12267  *
12268  * returns a bool to indicate if reset needs to happen
12269  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12270 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12271 {
12272 	bool need_reset = false;
12273 
12274 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12275 	 * the state changed, we need to reset.
12276 	 */
12277 	if (features & NETIF_F_NTUPLE) {
12278 		/* Enable filters and mark for reset */
12279 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12280 			need_reset = true;
12281 		/* enable FD_SB only if there is MSI-X vector and no cloud
12282 		 * filters exist
12283 		 */
12284 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12285 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12286 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12287 		}
12288 	} else {
12289 		/* turn off filters, mark for reset and clear SW filter list */
12290 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12291 			need_reset = true;
12292 			i40e_fdir_filter_exit(pf);
12293 		}
12294 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12295 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12296 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12297 
12298 		/* reset fd counters */
12299 		pf->fd_add_err = 0;
12300 		pf->fd_atr_cnt = 0;
12301 		/* if ATR was auto disabled it can be re-enabled. */
12302 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12303 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12304 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12305 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12306 	}
12307 	return need_reset;
12308 }
12309 
12310 /**
12311  * i40e_clear_rss_lut - clear the rx hash lookup table
12312  * @vsi: the VSI being configured
12313  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12314 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12315 {
12316 	struct i40e_pf *pf = vsi->back;
12317 	struct i40e_hw *hw = &pf->hw;
12318 	u16 vf_id = vsi->vf_id;
12319 	u8 i;
12320 
12321 	if (vsi->type == I40E_VSI_MAIN) {
12322 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12323 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12324 	} else if (vsi->type == I40E_VSI_SRIOV) {
12325 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12326 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12327 	} else {
12328 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12329 	}
12330 }
12331 
12332 /**
12333  * i40e_set_features - set the netdev feature flags
12334  * @netdev: ptr to the netdev being adjusted
12335  * @features: the feature set that the stack is suggesting
12336  * Note: expects to be called while under rtnl_lock()
12337  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12338 static int i40e_set_features(struct net_device *netdev,
12339 			     netdev_features_t features)
12340 {
12341 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12342 	struct i40e_vsi *vsi = np->vsi;
12343 	struct i40e_pf *pf = vsi->back;
12344 	bool need_reset;
12345 
12346 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12347 		i40e_pf_config_rss(pf);
12348 	else if (!(features & NETIF_F_RXHASH) &&
12349 		 netdev->features & NETIF_F_RXHASH)
12350 		i40e_clear_rss_lut(vsi);
12351 
12352 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12353 		i40e_vlan_stripping_enable(vsi);
12354 	else
12355 		i40e_vlan_stripping_disable(vsi);
12356 
12357 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12358 		dev_err(&pf->pdev->dev,
12359 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12360 		return -EINVAL;
12361 	}
12362 
12363 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12364 		i40e_del_all_macvlans(vsi);
12365 
12366 	need_reset = i40e_set_ntuple(pf, features);
12367 
12368 	if (need_reset)
12369 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12370 
12371 	return 0;
12372 }
12373 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12374 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12375 				    unsigned int table, unsigned int idx,
12376 				    struct udp_tunnel_info *ti)
12377 {
12378 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12379 	struct i40e_hw *hw = &np->vsi->back->hw;
12380 	u8 type, filter_index;
12381 	i40e_status ret;
12382 
12383 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12384 						   I40E_AQC_TUNNEL_TYPE_NGE;
12385 
12386 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12387 				     NULL);
12388 	if (ret) {
12389 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12390 			    i40e_stat_str(hw, ret),
12391 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12392 		return -EIO;
12393 	}
12394 
12395 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12396 	return 0;
12397 }
12398 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12399 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12400 				      unsigned int table, unsigned int idx,
12401 				      struct udp_tunnel_info *ti)
12402 {
12403 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12404 	struct i40e_hw *hw = &np->vsi->back->hw;
12405 	i40e_status ret;
12406 
12407 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12408 	if (ret) {
12409 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12410 			    i40e_stat_str(hw, ret),
12411 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12412 		return -EIO;
12413 	}
12414 
12415 	return 0;
12416 }
12417 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)12418 static int i40e_get_phys_port_id(struct net_device *netdev,
12419 				 struct netdev_phys_item_id *ppid)
12420 {
12421 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12422 	struct i40e_pf *pf = np->vsi->back;
12423 	struct i40e_hw *hw = &pf->hw;
12424 
12425 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12426 		return -EOPNOTSUPP;
12427 
12428 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12429 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12430 
12431 	return 0;
12432 }
12433 
12434 /**
12435  * i40e_ndo_fdb_add - add an entry to the hardware database
12436  * @ndm: the input from the stack
12437  * @tb: pointer to array of nladdr (unused)
12438  * @dev: the net device pointer
12439  * @addr: the MAC address entry being added
12440  * @vid: VLAN ID
12441  * @flags: instructions from stack about fdb operation
12442  * @extack: netlink extended ack, unused currently
12443  */
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)12444 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12445 			    struct net_device *dev,
12446 			    const unsigned char *addr, u16 vid,
12447 			    u16 flags,
12448 			    struct netlink_ext_ack *extack)
12449 {
12450 	struct i40e_netdev_priv *np = netdev_priv(dev);
12451 	struct i40e_pf *pf = np->vsi->back;
12452 	int err = 0;
12453 
12454 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12455 		return -EOPNOTSUPP;
12456 
12457 	if (vid) {
12458 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12459 		return -EINVAL;
12460 	}
12461 
12462 	/* Hardware does not support aging addresses so if a
12463 	 * ndm_state is given only allow permanent addresses
12464 	 */
12465 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12466 		netdev_info(dev, "FDB only supports static addresses\n");
12467 		return -EINVAL;
12468 	}
12469 
12470 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12471 		err = dev_uc_add_excl(dev, addr);
12472 	else if (is_multicast_ether_addr(addr))
12473 		err = dev_mc_add_excl(dev, addr);
12474 	else
12475 		err = -EINVAL;
12476 
12477 	/* Only return duplicate errors if NLM_F_EXCL is set */
12478 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12479 		err = 0;
12480 
12481 	return err;
12482 }
12483 
12484 /**
12485  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12486  * @dev: the netdev being configured
12487  * @nlh: RTNL message
12488  * @flags: bridge flags
12489  * @extack: netlink extended ack
12490  *
12491  * Inserts a new hardware bridge if not already created and
12492  * enables the bridging mode requested (VEB or VEPA). If the
12493  * hardware bridge has already been inserted and the request
12494  * is to change the mode then that requires a PF reset to
12495  * allow rebuild of the components with required hardware
12496  * bridge mode enabled.
12497  *
12498  * Note: expects to be called while under rtnl_lock()
12499  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)12500 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12501 				   struct nlmsghdr *nlh,
12502 				   u16 flags,
12503 				   struct netlink_ext_ack *extack)
12504 {
12505 	struct i40e_netdev_priv *np = netdev_priv(dev);
12506 	struct i40e_vsi *vsi = np->vsi;
12507 	struct i40e_pf *pf = vsi->back;
12508 	struct i40e_veb *veb = NULL;
12509 	struct nlattr *attr, *br_spec;
12510 	int i, rem;
12511 
12512 	/* Only for PF VSI for now */
12513 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12514 		return -EOPNOTSUPP;
12515 
12516 	/* Find the HW bridge for PF VSI */
12517 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12518 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12519 			veb = pf->veb[i];
12520 	}
12521 
12522 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12523 
12524 	nla_for_each_nested(attr, br_spec, rem) {
12525 		__u16 mode;
12526 
12527 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12528 			continue;
12529 
12530 		mode = nla_get_u16(attr);
12531 		if ((mode != BRIDGE_MODE_VEPA) &&
12532 		    (mode != BRIDGE_MODE_VEB))
12533 			return -EINVAL;
12534 
12535 		/* Insert a new HW bridge */
12536 		if (!veb) {
12537 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12538 					     vsi->tc_config.enabled_tc);
12539 			if (veb) {
12540 				veb->bridge_mode = mode;
12541 				i40e_config_bridge_mode(veb);
12542 			} else {
12543 				/* No Bridge HW offload available */
12544 				return -ENOENT;
12545 			}
12546 			break;
12547 		} else if (mode != veb->bridge_mode) {
12548 			/* Existing HW bridge but different mode needs reset */
12549 			veb->bridge_mode = mode;
12550 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12551 			if (mode == BRIDGE_MODE_VEB)
12552 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12553 			else
12554 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12555 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12556 			break;
12557 		}
12558 	}
12559 
12560 	return 0;
12561 }
12562 
12563 /**
12564  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12565  * @skb: skb buff
12566  * @pid: process id
12567  * @seq: RTNL message seq #
12568  * @dev: the netdev being configured
12569  * @filter_mask: unused
12570  * @nlflags: netlink flags passed in
12571  *
12572  * Return the mode in which the hardware bridge is operating in
12573  * i.e VEB or VEPA.
12574  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)12575 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12576 				   struct net_device *dev,
12577 				   u32 __always_unused filter_mask,
12578 				   int nlflags)
12579 {
12580 	struct i40e_netdev_priv *np = netdev_priv(dev);
12581 	struct i40e_vsi *vsi = np->vsi;
12582 	struct i40e_pf *pf = vsi->back;
12583 	struct i40e_veb *veb = NULL;
12584 	int i;
12585 
12586 	/* Only for PF VSI for now */
12587 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12588 		return -EOPNOTSUPP;
12589 
12590 	/* Find the HW bridge for the PF VSI */
12591 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12592 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12593 			veb = pf->veb[i];
12594 	}
12595 
12596 	if (!veb)
12597 		return 0;
12598 
12599 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12600 				       0, 0, nlflags, filter_mask, NULL);
12601 }
12602 
12603 /**
12604  * i40e_features_check - Validate encapsulated packet conforms to limits
12605  * @skb: skb buff
12606  * @dev: This physical port's netdev
12607  * @features: Offload features that the stack believes apply
12608  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)12609 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12610 					     struct net_device *dev,
12611 					     netdev_features_t features)
12612 {
12613 	size_t len;
12614 
12615 	/* No point in doing any of this if neither checksum nor GSO are
12616 	 * being requested for this frame.  We can rule out both by just
12617 	 * checking for CHECKSUM_PARTIAL
12618 	 */
12619 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12620 		return features;
12621 
12622 	/* We cannot support GSO if the MSS is going to be less than
12623 	 * 64 bytes.  If it is then we need to drop support for GSO.
12624 	 */
12625 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12626 		features &= ~NETIF_F_GSO_MASK;
12627 
12628 	/* MACLEN can support at most 63 words */
12629 	len = skb_network_header(skb) - skb->data;
12630 	if (len & ~(63 * 2))
12631 		goto out_err;
12632 
12633 	/* IPLEN and EIPLEN can support at most 127 dwords */
12634 	len = skb_transport_header(skb) - skb_network_header(skb);
12635 	if (len & ~(127 * 4))
12636 		goto out_err;
12637 
12638 	if (skb->encapsulation) {
12639 		/* L4TUNLEN can support 127 words */
12640 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12641 		if (len & ~(127 * 2))
12642 			goto out_err;
12643 
12644 		/* IPLEN can support at most 127 dwords */
12645 		len = skb_inner_transport_header(skb) -
12646 		      skb_inner_network_header(skb);
12647 		if (len & ~(127 * 4))
12648 			goto out_err;
12649 	}
12650 
12651 	/* No need to validate L4LEN as TCP is the only protocol with a
12652 	 * a flexible value and we support all possible values supported
12653 	 * by TCP, which is at most 15 dwords
12654 	 */
12655 
12656 	return features;
12657 out_err:
12658 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12659 }
12660 
12661 /**
12662  * i40e_xdp_setup - add/remove an XDP program
12663  * @vsi: VSI to changed
12664  * @prog: XDP program
12665  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog)12666 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12667 			  struct bpf_prog *prog)
12668 {
12669 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12670 	struct i40e_pf *pf = vsi->back;
12671 	struct bpf_prog *old_prog;
12672 	bool need_reset;
12673 	int i;
12674 
12675 	/* Don't allow frames that span over multiple buffers */
12676 	if (frame_size > vsi->rx_buf_len)
12677 		return -EINVAL;
12678 
12679 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12680 		return 0;
12681 
12682 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12683 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12684 
12685 	if (need_reset)
12686 		i40e_prep_for_reset(pf, true);
12687 
12688 	old_prog = xchg(&vsi->xdp_prog, prog);
12689 
12690 	if (need_reset) {
12691 		if (!prog)
12692 			/* Wait until ndo_xsk_wakeup completes. */
12693 			synchronize_rcu();
12694 		i40e_reset_and_rebuild(pf, true, true);
12695 	}
12696 
12697 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
12698 		if (i40e_realloc_rx_bi_zc(vsi, true))
12699 			return -ENOMEM;
12700 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
12701 		if (i40e_realloc_rx_bi_zc(vsi, false))
12702 			return -ENOMEM;
12703 	}
12704 
12705 	for (i = 0; i < vsi->num_queue_pairs; i++)
12706 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12707 
12708 	if (old_prog)
12709 		bpf_prog_put(old_prog);
12710 
12711 	/* Kick start the NAPI context if there is an AF_XDP socket open
12712 	 * on that queue id. This so that receiving will start.
12713 	 */
12714 	if (need_reset && prog)
12715 		for (i = 0; i < vsi->num_queue_pairs; i++)
12716 			if (vsi->xdp_rings[i]->xsk_pool)
12717 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12718 						      XDP_WAKEUP_RX);
12719 
12720 	return 0;
12721 }
12722 
12723 /**
12724  * i40e_enter_busy_conf - Enters busy config state
12725  * @vsi: vsi
12726  *
12727  * Returns 0 on success, <0 for failure.
12728  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)12729 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12730 {
12731 	struct i40e_pf *pf = vsi->back;
12732 	int timeout = 50;
12733 
12734 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12735 		timeout--;
12736 		if (!timeout)
12737 			return -EBUSY;
12738 		usleep_range(1000, 2000);
12739 	}
12740 
12741 	return 0;
12742 }
12743 
12744 /**
12745  * i40e_exit_busy_conf - Exits busy config state
12746  * @vsi: vsi
12747  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)12748 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12749 {
12750 	struct i40e_pf *pf = vsi->back;
12751 
12752 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12753 }
12754 
12755 /**
12756  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12757  * @vsi: vsi
12758  * @queue_pair: queue pair
12759  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)12760 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12761 {
12762 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12763 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12764 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12765 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12766 	if (i40e_enabled_xdp_vsi(vsi)) {
12767 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12768 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12769 	}
12770 }
12771 
12772 /**
12773  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12774  * @vsi: vsi
12775  * @queue_pair: queue pair
12776  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)12777 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12778 {
12779 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12780 	if (i40e_enabled_xdp_vsi(vsi)) {
12781 		/* Make sure that in-progress ndo_xdp_xmit calls are
12782 		 * completed.
12783 		 */
12784 		synchronize_rcu();
12785 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12786 	}
12787 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12788 }
12789 
12790 /**
12791  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12792  * @vsi: vsi
12793  * @queue_pair: queue pair
12794  * @enable: true for enable, false for disable
12795  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)12796 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12797 					bool enable)
12798 {
12799 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12800 	struct i40e_q_vector *q_vector = rxr->q_vector;
12801 
12802 	if (!vsi->netdev)
12803 		return;
12804 
12805 	/* All rings in a qp belong to the same qvector. */
12806 	if (q_vector->rx.ring || q_vector->tx.ring) {
12807 		if (enable)
12808 			napi_enable(&q_vector->napi);
12809 		else
12810 			napi_disable(&q_vector->napi);
12811 	}
12812 }
12813 
12814 /**
12815  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12816  * @vsi: vsi
12817  * @queue_pair: queue pair
12818  * @enable: true for enable, false for disable
12819  *
12820  * Returns 0 on success, <0 on failure.
12821  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)12822 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12823 					bool enable)
12824 {
12825 	struct i40e_pf *pf = vsi->back;
12826 	int pf_q, ret = 0;
12827 
12828 	pf_q = vsi->base_queue + queue_pair;
12829 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12830 				     false /*is xdp*/, enable);
12831 	if (ret) {
12832 		dev_info(&pf->pdev->dev,
12833 			 "VSI seid %d Tx ring %d %sable timeout\n",
12834 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12835 		return ret;
12836 	}
12837 
12838 	i40e_control_rx_q(pf, pf_q, enable);
12839 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12840 	if (ret) {
12841 		dev_info(&pf->pdev->dev,
12842 			 "VSI seid %d Rx ring %d %sable timeout\n",
12843 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12844 		return ret;
12845 	}
12846 
12847 	/* Due to HW errata, on Rx disable only, the register can
12848 	 * indicate done before it really is. Needs 50ms to be sure
12849 	 */
12850 	if (!enable)
12851 		mdelay(50);
12852 
12853 	if (!i40e_enabled_xdp_vsi(vsi))
12854 		return ret;
12855 
12856 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12857 				     pf_q + vsi->alloc_queue_pairs,
12858 				     true /*is xdp*/, enable);
12859 	if (ret) {
12860 		dev_info(&pf->pdev->dev,
12861 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12862 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12863 	}
12864 
12865 	return ret;
12866 }
12867 
12868 /**
12869  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12870  * @vsi: vsi
12871  * @queue_pair: queue_pair
12872  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)12873 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12874 {
12875 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12876 	struct i40e_pf *pf = vsi->back;
12877 	struct i40e_hw *hw = &pf->hw;
12878 
12879 	/* All rings in a qp belong to the same qvector. */
12880 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12881 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12882 	else
12883 		i40e_irq_dynamic_enable_icr0(pf);
12884 
12885 	i40e_flush(hw);
12886 }
12887 
12888 /**
12889  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12890  * @vsi: vsi
12891  * @queue_pair: queue_pair
12892  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)12893 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12894 {
12895 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12896 	struct i40e_pf *pf = vsi->back;
12897 	struct i40e_hw *hw = &pf->hw;
12898 
12899 	/* For simplicity, instead of removing the qp interrupt causes
12900 	 * from the interrupt linked list, we simply disable the interrupt, and
12901 	 * leave the list intact.
12902 	 *
12903 	 * All rings in a qp belong to the same qvector.
12904 	 */
12905 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12906 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12907 
12908 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12909 		i40e_flush(hw);
12910 		synchronize_irq(pf->msix_entries[intpf].vector);
12911 	} else {
12912 		/* Legacy and MSI mode - this stops all interrupt handling */
12913 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12914 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12915 		i40e_flush(hw);
12916 		synchronize_irq(pf->pdev->irq);
12917 	}
12918 }
12919 
12920 /**
12921  * i40e_queue_pair_disable - Disables a queue pair
12922  * @vsi: vsi
12923  * @queue_pair: queue pair
12924  *
12925  * Returns 0 on success, <0 on failure.
12926  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)12927 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12928 {
12929 	int err;
12930 
12931 	err = i40e_enter_busy_conf(vsi);
12932 	if (err)
12933 		return err;
12934 
12935 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12936 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12937 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12938 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12939 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12940 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12941 
12942 	return err;
12943 }
12944 
12945 /**
12946  * i40e_queue_pair_enable - Enables a queue pair
12947  * @vsi: vsi
12948  * @queue_pair: queue pair
12949  *
12950  * Returns 0 on success, <0 on failure.
12951  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)12952 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12953 {
12954 	int err;
12955 
12956 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12957 	if (err)
12958 		return err;
12959 
12960 	if (i40e_enabled_xdp_vsi(vsi)) {
12961 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12962 		if (err)
12963 			return err;
12964 	}
12965 
12966 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12967 	if (err)
12968 		return err;
12969 
12970 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12971 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12972 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12973 
12974 	i40e_exit_busy_conf(vsi);
12975 
12976 	return err;
12977 }
12978 
12979 /**
12980  * i40e_xdp - implements ndo_bpf for i40e
12981  * @dev: netdevice
12982  * @xdp: XDP command
12983  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)12984 static int i40e_xdp(struct net_device *dev,
12985 		    struct netdev_bpf *xdp)
12986 {
12987 	struct i40e_netdev_priv *np = netdev_priv(dev);
12988 	struct i40e_vsi *vsi = np->vsi;
12989 
12990 	if (vsi->type != I40E_VSI_MAIN)
12991 		return -EINVAL;
12992 
12993 	switch (xdp->command) {
12994 	case XDP_SETUP_PROG:
12995 		return i40e_xdp_setup(vsi, xdp->prog);
12996 	case XDP_SETUP_XSK_POOL:
12997 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
12998 					   xdp->xsk.queue_id);
12999 	default:
13000 		return -EINVAL;
13001 	}
13002 }
13003 
13004 static const struct net_device_ops i40e_netdev_ops = {
13005 	.ndo_open		= i40e_open,
13006 	.ndo_stop		= i40e_close,
13007 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13008 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13009 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13010 	.ndo_validate_addr	= eth_validate_addr,
13011 	.ndo_set_mac_address	= i40e_set_mac,
13012 	.ndo_change_mtu		= i40e_change_mtu,
13013 	.ndo_do_ioctl		= i40e_ioctl,
13014 	.ndo_tx_timeout		= i40e_tx_timeout,
13015 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13016 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13017 #ifdef CONFIG_NET_POLL_CONTROLLER
13018 	.ndo_poll_controller	= i40e_netpoll,
13019 #endif
13020 	.ndo_setup_tc		= __i40e_setup_tc,
13021 	.ndo_select_queue	= i40e_lan_select_queue,
13022 	.ndo_set_features	= i40e_set_features,
13023 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13024 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13025 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13026 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13027 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13028 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13029 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13030 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13031 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
13032 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
13033 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13034 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13035 	.ndo_features_check	= i40e_features_check,
13036 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13037 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13038 	.ndo_bpf		= i40e_xdp,
13039 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13040 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13041 	.ndo_dfwd_add_station	= i40e_fwd_add,
13042 	.ndo_dfwd_del_station	= i40e_fwd_del,
13043 };
13044 
13045 /**
13046  * i40e_config_netdev - Setup the netdev flags
13047  * @vsi: the VSI being configured
13048  *
13049  * Returns 0 on success, negative value on failure
13050  **/
i40e_config_netdev(struct i40e_vsi * vsi)13051 static int i40e_config_netdev(struct i40e_vsi *vsi)
13052 {
13053 	struct i40e_pf *pf = vsi->back;
13054 	struct i40e_hw *hw = &pf->hw;
13055 	struct i40e_netdev_priv *np;
13056 	struct net_device *netdev;
13057 	u8 broadcast[ETH_ALEN];
13058 	u8 mac_addr[ETH_ALEN];
13059 	int etherdev_size;
13060 	netdev_features_t hw_enc_features;
13061 	netdev_features_t hw_features;
13062 
13063 	etherdev_size = sizeof(struct i40e_netdev_priv);
13064 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13065 	if (!netdev)
13066 		return -ENOMEM;
13067 
13068 	vsi->netdev = netdev;
13069 	np = netdev_priv(netdev);
13070 	np->vsi = vsi;
13071 
13072 	hw_enc_features = NETIF_F_SG			|
13073 			  NETIF_F_IP_CSUM		|
13074 			  NETIF_F_IPV6_CSUM		|
13075 			  NETIF_F_HIGHDMA		|
13076 			  NETIF_F_SOFT_FEATURES		|
13077 			  NETIF_F_TSO			|
13078 			  NETIF_F_TSO_ECN		|
13079 			  NETIF_F_TSO6			|
13080 			  NETIF_F_GSO_GRE		|
13081 			  NETIF_F_GSO_GRE_CSUM		|
13082 			  NETIF_F_GSO_PARTIAL		|
13083 			  NETIF_F_GSO_IPXIP4		|
13084 			  NETIF_F_GSO_IPXIP6		|
13085 			  NETIF_F_GSO_UDP_TUNNEL	|
13086 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13087 			  NETIF_F_GSO_UDP_L4		|
13088 			  NETIF_F_SCTP_CRC		|
13089 			  NETIF_F_RXHASH		|
13090 			  NETIF_F_RXCSUM		|
13091 			  0;
13092 
13093 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13094 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13095 
13096 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13097 
13098 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13099 
13100 	netdev->hw_enc_features |= hw_enc_features;
13101 
13102 	/* record features VLANs can make use of */
13103 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13104 
13105 	/* enable macvlan offloads */
13106 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13107 
13108 	hw_features = hw_enc_features		|
13109 		      NETIF_F_HW_VLAN_CTAG_TX	|
13110 		      NETIF_F_HW_VLAN_CTAG_RX;
13111 
13112 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13113 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13114 
13115 	netdev->hw_features |= hw_features;
13116 
13117 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13118 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13119 
13120 	if (vsi->type == I40E_VSI_MAIN) {
13121 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13122 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13123 		/* The following steps are necessary for two reasons. First,
13124 		 * some older NVM configurations load a default MAC-VLAN
13125 		 * filter that will accept any tagged packet, and we want to
13126 		 * replace this with a normal filter. Additionally, it is
13127 		 * possible our MAC address was provided by the platform using
13128 		 * Open Firmware or similar.
13129 		 *
13130 		 * Thus, we need to remove the default filter and install one
13131 		 * specific to the MAC address.
13132 		 */
13133 		i40e_rm_default_mac_filter(vsi, mac_addr);
13134 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13135 		i40e_add_mac_filter(vsi, mac_addr);
13136 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13137 	} else {
13138 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13139 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13140 		 * the end, which is 4 bytes long, so force truncation of the
13141 		 * original name by IFNAMSIZ - 4
13142 		 */
13143 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13144 			 IFNAMSIZ - 4,
13145 			 pf->vsi[pf->lan_vsi]->netdev->name);
13146 		eth_random_addr(mac_addr);
13147 
13148 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13149 		i40e_add_mac_filter(vsi, mac_addr);
13150 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13151 	}
13152 
13153 	/* Add the broadcast filter so that we initially will receive
13154 	 * broadcast packets. Note that when a new VLAN is first added the
13155 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13156 	 * specific filters as part of transitioning into "vlan" operation.
13157 	 * When more VLANs are added, the driver will copy each existing MAC
13158 	 * filter and add it for the new VLAN.
13159 	 *
13160 	 * Broadcast filters are handled specially by
13161 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13162 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13163 	 * filter. The subtask will update the correct broadcast promiscuous
13164 	 * bits as VLANs become active or inactive.
13165 	 */
13166 	eth_broadcast_addr(broadcast);
13167 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13168 	i40e_add_mac_filter(vsi, broadcast);
13169 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13170 
13171 	ether_addr_copy(netdev->dev_addr, mac_addr);
13172 	ether_addr_copy(netdev->perm_addr, mac_addr);
13173 
13174 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13175 	netdev->neigh_priv_len = sizeof(u32) * 4;
13176 
13177 	netdev->priv_flags |= IFF_UNICAST_FLT;
13178 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13179 	/* Setup netdev TC information */
13180 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13181 
13182 	netdev->netdev_ops = &i40e_netdev_ops;
13183 	netdev->watchdog_timeo = 5 * HZ;
13184 	i40e_set_ethtool_ops(netdev);
13185 
13186 	/* MTU range: 68 - 9706 */
13187 	netdev->min_mtu = ETH_MIN_MTU;
13188 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13189 
13190 	return 0;
13191 }
13192 
13193 /**
13194  * i40e_vsi_delete - Delete a VSI from the switch
13195  * @vsi: the VSI being removed
13196  *
13197  * Returns 0 on success, negative value on failure
13198  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13199 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13200 {
13201 	/* remove default VSI is not allowed */
13202 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13203 		return;
13204 
13205 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13206 }
13207 
13208 /**
13209  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13210  * @vsi: the VSI being queried
13211  *
13212  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13213  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13214 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13215 {
13216 	struct i40e_veb *veb;
13217 	struct i40e_pf *pf = vsi->back;
13218 
13219 	/* Uplink is not a bridge so default to VEB */
13220 	if (vsi->veb_idx >= I40E_MAX_VEB)
13221 		return 1;
13222 
13223 	veb = pf->veb[vsi->veb_idx];
13224 	if (!veb) {
13225 		dev_info(&pf->pdev->dev,
13226 			 "There is no veb associated with the bridge\n");
13227 		return -ENOENT;
13228 	}
13229 
13230 	/* Uplink is a bridge in VEPA mode */
13231 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13232 		return 0;
13233 	} else {
13234 		/* Uplink is a bridge in VEB mode */
13235 		return 1;
13236 	}
13237 
13238 	/* VEPA is now default bridge, so return 0 */
13239 	return 0;
13240 }
13241 
13242 /**
13243  * i40e_add_vsi - Add a VSI to the switch
13244  * @vsi: the VSI being configured
13245  *
13246  * This initializes a VSI context depending on the VSI type to be added and
13247  * passes it down to the add_vsi aq command.
13248  **/
i40e_add_vsi(struct i40e_vsi * vsi)13249 static int i40e_add_vsi(struct i40e_vsi *vsi)
13250 {
13251 	int ret = -ENODEV;
13252 	struct i40e_pf *pf = vsi->back;
13253 	struct i40e_hw *hw = &pf->hw;
13254 	struct i40e_vsi_context ctxt;
13255 	struct i40e_mac_filter *f;
13256 	struct hlist_node *h;
13257 	int bkt;
13258 
13259 	u8 enabled_tc = 0x1; /* TC0 enabled */
13260 	int f_count = 0;
13261 
13262 	memset(&ctxt, 0, sizeof(ctxt));
13263 	switch (vsi->type) {
13264 	case I40E_VSI_MAIN:
13265 		/* The PF's main VSI is already setup as part of the
13266 		 * device initialization, so we'll not bother with
13267 		 * the add_vsi call, but we will retrieve the current
13268 		 * VSI context.
13269 		 */
13270 		ctxt.seid = pf->main_vsi_seid;
13271 		ctxt.pf_num = pf->hw.pf_id;
13272 		ctxt.vf_num = 0;
13273 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13274 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13275 		if (ret) {
13276 			dev_info(&pf->pdev->dev,
13277 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13278 				 i40e_stat_str(&pf->hw, ret),
13279 				 i40e_aq_str(&pf->hw,
13280 					     pf->hw.aq.asq_last_status));
13281 			return -ENOENT;
13282 		}
13283 		vsi->info = ctxt.info;
13284 		vsi->info.valid_sections = 0;
13285 
13286 		vsi->seid = ctxt.seid;
13287 		vsi->id = ctxt.vsi_number;
13288 
13289 		enabled_tc = i40e_pf_get_tc_map(pf);
13290 
13291 		/* Source pruning is enabled by default, so the flag is
13292 		 * negative logic - if it's set, we need to fiddle with
13293 		 * the VSI to disable source pruning.
13294 		 */
13295 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13296 			memset(&ctxt, 0, sizeof(ctxt));
13297 			ctxt.seid = pf->main_vsi_seid;
13298 			ctxt.pf_num = pf->hw.pf_id;
13299 			ctxt.vf_num = 0;
13300 			ctxt.info.valid_sections |=
13301 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13302 			ctxt.info.switch_id =
13303 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13304 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13305 			if (ret) {
13306 				dev_info(&pf->pdev->dev,
13307 					 "update vsi failed, err %s aq_err %s\n",
13308 					 i40e_stat_str(&pf->hw, ret),
13309 					 i40e_aq_str(&pf->hw,
13310 						     pf->hw.aq.asq_last_status));
13311 				ret = -ENOENT;
13312 				goto err;
13313 			}
13314 		}
13315 
13316 		/* MFP mode setup queue map and update VSI */
13317 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13318 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13319 			memset(&ctxt, 0, sizeof(ctxt));
13320 			ctxt.seid = pf->main_vsi_seid;
13321 			ctxt.pf_num = pf->hw.pf_id;
13322 			ctxt.vf_num = 0;
13323 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13324 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13325 			if (ret) {
13326 				dev_info(&pf->pdev->dev,
13327 					 "update vsi failed, err %s aq_err %s\n",
13328 					 i40e_stat_str(&pf->hw, ret),
13329 					 i40e_aq_str(&pf->hw,
13330 						    pf->hw.aq.asq_last_status));
13331 				ret = -ENOENT;
13332 				goto err;
13333 			}
13334 			/* update the local VSI info queue map */
13335 			i40e_vsi_update_queue_map(vsi, &ctxt);
13336 			vsi->info.valid_sections = 0;
13337 		} else {
13338 			/* Default/Main VSI is only enabled for TC0
13339 			 * reconfigure it to enable all TCs that are
13340 			 * available on the port in SFP mode.
13341 			 * For MFP case the iSCSI PF would use this
13342 			 * flow to enable LAN+iSCSI TC.
13343 			 */
13344 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13345 			if (ret) {
13346 				/* Single TC condition is not fatal,
13347 				 * message and continue
13348 				 */
13349 				dev_info(&pf->pdev->dev,
13350 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13351 					 enabled_tc,
13352 					 i40e_stat_str(&pf->hw, ret),
13353 					 i40e_aq_str(&pf->hw,
13354 						    pf->hw.aq.asq_last_status));
13355 			}
13356 		}
13357 		break;
13358 
13359 	case I40E_VSI_FDIR:
13360 		ctxt.pf_num = hw->pf_id;
13361 		ctxt.vf_num = 0;
13362 		ctxt.uplink_seid = vsi->uplink_seid;
13363 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13364 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13365 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13366 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13367 			ctxt.info.valid_sections |=
13368 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13369 			ctxt.info.switch_id =
13370 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13371 		}
13372 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13373 		break;
13374 
13375 	case I40E_VSI_VMDQ2:
13376 		ctxt.pf_num = hw->pf_id;
13377 		ctxt.vf_num = 0;
13378 		ctxt.uplink_seid = vsi->uplink_seid;
13379 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13380 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13381 
13382 		/* This VSI is connected to VEB so the switch_id
13383 		 * should be set to zero by default.
13384 		 */
13385 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13386 			ctxt.info.valid_sections |=
13387 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13388 			ctxt.info.switch_id =
13389 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13390 		}
13391 
13392 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13393 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13394 		break;
13395 
13396 	case I40E_VSI_SRIOV:
13397 		ctxt.pf_num = hw->pf_id;
13398 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13399 		ctxt.uplink_seid = vsi->uplink_seid;
13400 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13401 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13402 
13403 		/* This VSI is connected to VEB so the switch_id
13404 		 * should be set to zero by default.
13405 		 */
13406 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13407 			ctxt.info.valid_sections |=
13408 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13409 			ctxt.info.switch_id =
13410 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13411 		}
13412 
13413 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13414 			ctxt.info.valid_sections |=
13415 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13416 			ctxt.info.queueing_opt_flags |=
13417 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13418 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13419 		}
13420 
13421 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13422 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13423 		if (pf->vf[vsi->vf_id].spoofchk) {
13424 			ctxt.info.valid_sections |=
13425 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13426 			ctxt.info.sec_flags |=
13427 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13428 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13429 		}
13430 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13431 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13432 		break;
13433 
13434 	case I40E_VSI_IWARP:
13435 		/* send down message to iWARP */
13436 		break;
13437 
13438 	default:
13439 		return -ENODEV;
13440 	}
13441 
13442 	if (vsi->type != I40E_VSI_MAIN) {
13443 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13444 		if (ret) {
13445 			dev_info(&vsi->back->pdev->dev,
13446 				 "add vsi failed, err %s aq_err %s\n",
13447 				 i40e_stat_str(&pf->hw, ret),
13448 				 i40e_aq_str(&pf->hw,
13449 					     pf->hw.aq.asq_last_status));
13450 			ret = -ENOENT;
13451 			goto err;
13452 		}
13453 		vsi->info = ctxt.info;
13454 		vsi->info.valid_sections = 0;
13455 		vsi->seid = ctxt.seid;
13456 		vsi->id = ctxt.vsi_number;
13457 	}
13458 
13459 	vsi->active_filters = 0;
13460 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13461 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13462 	/* If macvlan filters already exist, force them to get loaded */
13463 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13464 		f->state = I40E_FILTER_NEW;
13465 		f_count++;
13466 	}
13467 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13468 
13469 	if (f_count) {
13470 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13471 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13472 	}
13473 
13474 	/* Update VSI BW information */
13475 	ret = i40e_vsi_get_bw_info(vsi);
13476 	if (ret) {
13477 		dev_info(&pf->pdev->dev,
13478 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13479 			 i40e_stat_str(&pf->hw, ret),
13480 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13481 		/* VSI is already added so not tearing that up */
13482 		ret = 0;
13483 	}
13484 
13485 err:
13486 	return ret;
13487 }
13488 
13489 /**
13490  * i40e_vsi_release - Delete a VSI and free its resources
13491  * @vsi: the VSI being removed
13492  *
13493  * Returns 0 on success or < 0 on error
13494  **/
i40e_vsi_release(struct i40e_vsi * vsi)13495 int i40e_vsi_release(struct i40e_vsi *vsi)
13496 {
13497 	struct i40e_mac_filter *f;
13498 	struct hlist_node *h;
13499 	struct i40e_veb *veb = NULL;
13500 	struct i40e_pf *pf;
13501 	u16 uplink_seid;
13502 	int i, n, bkt;
13503 
13504 	pf = vsi->back;
13505 
13506 	/* release of a VEB-owner or last VSI is not allowed */
13507 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13508 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13509 			 vsi->seid, vsi->uplink_seid);
13510 		return -ENODEV;
13511 	}
13512 	if (vsi == pf->vsi[pf->lan_vsi] &&
13513 	    !test_bit(__I40E_DOWN, pf->state)) {
13514 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13515 		return -ENODEV;
13516 	}
13517 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13518 	uplink_seid = vsi->uplink_seid;
13519 	if (vsi->type != I40E_VSI_SRIOV) {
13520 		if (vsi->netdev_registered) {
13521 			vsi->netdev_registered = false;
13522 			if (vsi->netdev) {
13523 				/* results in a call to i40e_close() */
13524 				unregister_netdev(vsi->netdev);
13525 			}
13526 		} else {
13527 			i40e_vsi_close(vsi);
13528 		}
13529 		i40e_vsi_disable_irq(vsi);
13530 	}
13531 
13532 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13533 
13534 	/* clear the sync flag on all filters */
13535 	if (vsi->netdev) {
13536 		__dev_uc_unsync(vsi->netdev, NULL);
13537 		__dev_mc_unsync(vsi->netdev, NULL);
13538 	}
13539 
13540 	/* make sure any remaining filters are marked for deletion */
13541 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13542 		__i40e_del_filter(vsi, f);
13543 
13544 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13545 
13546 	i40e_sync_vsi_filters(vsi);
13547 
13548 	i40e_vsi_delete(vsi);
13549 	i40e_vsi_free_q_vectors(vsi);
13550 	if (vsi->netdev) {
13551 		free_netdev(vsi->netdev);
13552 		vsi->netdev = NULL;
13553 	}
13554 	i40e_vsi_clear_rings(vsi);
13555 	i40e_vsi_clear(vsi);
13556 
13557 	/* If this was the last thing on the VEB, except for the
13558 	 * controlling VSI, remove the VEB, which puts the controlling
13559 	 * VSI onto the next level down in the switch.
13560 	 *
13561 	 * Well, okay, there's one more exception here: don't remove
13562 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13563 	 * from up the network stack.
13564 	 */
13565 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13566 		if (pf->vsi[i] &&
13567 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13568 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13569 			n++;      /* count the VSIs */
13570 		}
13571 	}
13572 	for (i = 0; i < I40E_MAX_VEB; i++) {
13573 		if (!pf->veb[i])
13574 			continue;
13575 		if (pf->veb[i]->uplink_seid == uplink_seid)
13576 			n++;     /* count the VEBs */
13577 		if (pf->veb[i]->seid == uplink_seid)
13578 			veb = pf->veb[i];
13579 	}
13580 	if (n == 0 && veb && veb->uplink_seid != 0)
13581 		i40e_veb_release(veb);
13582 
13583 	return 0;
13584 }
13585 
13586 /**
13587  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13588  * @vsi: ptr to the VSI
13589  *
13590  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13591  * corresponding SW VSI structure and initializes num_queue_pairs for the
13592  * newly allocated VSI.
13593  *
13594  * Returns 0 on success or negative on failure
13595  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)13596 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13597 {
13598 	int ret = -ENOENT;
13599 	struct i40e_pf *pf = vsi->back;
13600 
13601 	if (vsi->q_vectors[0]) {
13602 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13603 			 vsi->seid);
13604 		return -EEXIST;
13605 	}
13606 
13607 	if (vsi->base_vector) {
13608 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13609 			 vsi->seid, vsi->base_vector);
13610 		return -EEXIST;
13611 	}
13612 
13613 	ret = i40e_vsi_alloc_q_vectors(vsi);
13614 	if (ret) {
13615 		dev_info(&pf->pdev->dev,
13616 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13617 			 vsi->num_q_vectors, vsi->seid, ret);
13618 		vsi->num_q_vectors = 0;
13619 		goto vector_setup_out;
13620 	}
13621 
13622 	/* In Legacy mode, we do not have to get any other vector since we
13623 	 * piggyback on the misc/ICR0 for queue interrupts.
13624 	*/
13625 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13626 		return ret;
13627 	if (vsi->num_q_vectors)
13628 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13629 						 vsi->num_q_vectors, vsi->idx);
13630 	if (vsi->base_vector < 0) {
13631 		dev_info(&pf->pdev->dev,
13632 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13633 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13634 		i40e_vsi_free_q_vectors(vsi);
13635 		ret = -ENOENT;
13636 		goto vector_setup_out;
13637 	}
13638 
13639 vector_setup_out:
13640 	return ret;
13641 }
13642 
13643 /**
13644  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13645  * @vsi: pointer to the vsi.
13646  *
13647  * This re-allocates a vsi's queue resources.
13648  *
13649  * Returns pointer to the successfully allocated and configured VSI sw struct
13650  * on success, otherwise returns NULL on failure.
13651  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)13652 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13653 {
13654 	u16 alloc_queue_pairs;
13655 	struct i40e_pf *pf;
13656 	u8 enabled_tc;
13657 	int ret;
13658 
13659 	if (!vsi)
13660 		return NULL;
13661 
13662 	pf = vsi->back;
13663 
13664 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13665 	i40e_vsi_clear_rings(vsi);
13666 
13667 	i40e_vsi_free_arrays(vsi, false);
13668 	i40e_set_num_rings_in_vsi(vsi);
13669 	ret = i40e_vsi_alloc_arrays(vsi, false);
13670 	if (ret)
13671 		goto err_vsi;
13672 
13673 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13674 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13675 
13676 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13677 	if (ret < 0) {
13678 		dev_info(&pf->pdev->dev,
13679 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13680 			 alloc_queue_pairs, vsi->seid, ret);
13681 		goto err_vsi;
13682 	}
13683 	vsi->base_queue = ret;
13684 
13685 	/* Update the FW view of the VSI. Force a reset of TC and queue
13686 	 * layout configurations.
13687 	 */
13688 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13689 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13690 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13691 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13692 	if (vsi->type == I40E_VSI_MAIN)
13693 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13694 
13695 	/* assign it some queues */
13696 	ret = i40e_alloc_rings(vsi);
13697 	if (ret)
13698 		goto err_rings;
13699 
13700 	/* map all of the rings to the q_vectors */
13701 	i40e_vsi_map_rings_to_vectors(vsi);
13702 	return vsi;
13703 
13704 err_rings:
13705 	i40e_vsi_free_q_vectors(vsi);
13706 	if (vsi->netdev_registered) {
13707 		vsi->netdev_registered = false;
13708 		unregister_netdev(vsi->netdev);
13709 		free_netdev(vsi->netdev);
13710 		vsi->netdev = NULL;
13711 	}
13712 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13713 err_vsi:
13714 	i40e_vsi_clear(vsi);
13715 	return NULL;
13716 }
13717 
13718 /**
13719  * i40e_vsi_setup - Set up a VSI by a given type
13720  * @pf: board private structure
13721  * @type: VSI type
13722  * @uplink_seid: the switch element to link to
13723  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13724  *
13725  * This allocates the sw VSI structure and its queue resources, then add a VSI
13726  * to the identified VEB.
13727  *
13728  * Returns pointer to the successfully allocated and configure VSI sw struct on
13729  * success, otherwise returns NULL on failure.
13730  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)13731 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13732 				u16 uplink_seid, u32 param1)
13733 {
13734 	struct i40e_vsi *vsi = NULL;
13735 	struct i40e_veb *veb = NULL;
13736 	u16 alloc_queue_pairs;
13737 	int ret, i;
13738 	int v_idx;
13739 
13740 	/* The requested uplink_seid must be either
13741 	 *     - the PF's port seid
13742 	 *              no VEB is needed because this is the PF
13743 	 *              or this is a Flow Director special case VSI
13744 	 *     - seid of an existing VEB
13745 	 *     - seid of a VSI that owns an existing VEB
13746 	 *     - seid of a VSI that doesn't own a VEB
13747 	 *              a new VEB is created and the VSI becomes the owner
13748 	 *     - seid of the PF VSI, which is what creates the first VEB
13749 	 *              this is a special case of the previous
13750 	 *
13751 	 * Find which uplink_seid we were given and create a new VEB if needed
13752 	 */
13753 	for (i = 0; i < I40E_MAX_VEB; i++) {
13754 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13755 			veb = pf->veb[i];
13756 			break;
13757 		}
13758 	}
13759 
13760 	if (!veb && uplink_seid != pf->mac_seid) {
13761 
13762 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13763 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13764 				vsi = pf->vsi[i];
13765 				break;
13766 			}
13767 		}
13768 		if (!vsi) {
13769 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13770 				 uplink_seid);
13771 			return NULL;
13772 		}
13773 
13774 		if (vsi->uplink_seid == pf->mac_seid)
13775 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13776 					     vsi->tc_config.enabled_tc);
13777 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13778 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13779 					     vsi->tc_config.enabled_tc);
13780 		if (veb) {
13781 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13782 				dev_info(&vsi->back->pdev->dev,
13783 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13784 				return NULL;
13785 			}
13786 			/* We come up by default in VEPA mode if SRIOV is not
13787 			 * already enabled, in which case we can't force VEPA
13788 			 * mode.
13789 			 */
13790 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13791 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13792 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13793 			}
13794 			i40e_config_bridge_mode(veb);
13795 		}
13796 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13797 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13798 				veb = pf->veb[i];
13799 		}
13800 		if (!veb) {
13801 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13802 			return NULL;
13803 		}
13804 
13805 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13806 		uplink_seid = veb->seid;
13807 	}
13808 
13809 	/* get vsi sw struct */
13810 	v_idx = i40e_vsi_mem_alloc(pf, type);
13811 	if (v_idx < 0)
13812 		goto err_alloc;
13813 	vsi = pf->vsi[v_idx];
13814 	if (!vsi)
13815 		goto err_alloc;
13816 	vsi->type = type;
13817 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13818 
13819 	if (type == I40E_VSI_MAIN)
13820 		pf->lan_vsi = v_idx;
13821 	else if (type == I40E_VSI_SRIOV)
13822 		vsi->vf_id = param1;
13823 	/* assign it some queues */
13824 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13825 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13826 
13827 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13828 	if (ret < 0) {
13829 		dev_info(&pf->pdev->dev,
13830 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13831 			 alloc_queue_pairs, vsi->seid, ret);
13832 		goto err_vsi;
13833 	}
13834 	vsi->base_queue = ret;
13835 
13836 	/* get a VSI from the hardware */
13837 	vsi->uplink_seid = uplink_seid;
13838 	ret = i40e_add_vsi(vsi);
13839 	if (ret)
13840 		goto err_vsi;
13841 
13842 	switch (vsi->type) {
13843 	/* setup the netdev if needed */
13844 	case I40E_VSI_MAIN:
13845 	case I40E_VSI_VMDQ2:
13846 		ret = i40e_config_netdev(vsi);
13847 		if (ret)
13848 			goto err_netdev;
13849 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
13850 		if (ret)
13851 			goto err_netdev;
13852 		ret = register_netdev(vsi->netdev);
13853 		if (ret)
13854 			goto err_netdev;
13855 		vsi->netdev_registered = true;
13856 		netif_carrier_off(vsi->netdev);
13857 #ifdef CONFIG_I40E_DCB
13858 		/* Setup DCB netlink interface */
13859 		i40e_dcbnl_setup(vsi);
13860 #endif /* CONFIG_I40E_DCB */
13861 		fallthrough;
13862 	case I40E_VSI_FDIR:
13863 		/* set up vectors and rings if needed */
13864 		ret = i40e_vsi_setup_vectors(vsi);
13865 		if (ret)
13866 			goto err_msix;
13867 
13868 		ret = i40e_alloc_rings(vsi);
13869 		if (ret)
13870 			goto err_rings;
13871 
13872 		/* map all of the rings to the q_vectors */
13873 		i40e_vsi_map_rings_to_vectors(vsi);
13874 
13875 		i40e_vsi_reset_stats(vsi);
13876 		break;
13877 	default:
13878 		/* no netdev or rings for the other VSI types */
13879 		break;
13880 	}
13881 
13882 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13883 	    (vsi->type == I40E_VSI_VMDQ2)) {
13884 		ret = i40e_vsi_config_rss(vsi);
13885 	}
13886 	return vsi;
13887 
13888 err_rings:
13889 	i40e_vsi_free_q_vectors(vsi);
13890 err_msix:
13891 	if (vsi->netdev_registered) {
13892 		vsi->netdev_registered = false;
13893 		unregister_netdev(vsi->netdev);
13894 		free_netdev(vsi->netdev);
13895 		vsi->netdev = NULL;
13896 	}
13897 err_netdev:
13898 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13899 err_vsi:
13900 	i40e_vsi_clear(vsi);
13901 err_alloc:
13902 	return NULL;
13903 }
13904 
13905 /**
13906  * i40e_veb_get_bw_info - Query VEB BW information
13907  * @veb: the veb to query
13908  *
13909  * Query the Tx scheduler BW configuration data for given VEB
13910  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)13911 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13912 {
13913 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13914 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13915 	struct i40e_pf *pf = veb->pf;
13916 	struct i40e_hw *hw = &pf->hw;
13917 	u32 tc_bw_max;
13918 	int ret = 0;
13919 	int i;
13920 
13921 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13922 						  &bw_data, NULL);
13923 	if (ret) {
13924 		dev_info(&pf->pdev->dev,
13925 			 "query veb bw config failed, err %s aq_err %s\n",
13926 			 i40e_stat_str(&pf->hw, ret),
13927 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13928 		goto out;
13929 	}
13930 
13931 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13932 						   &ets_data, NULL);
13933 	if (ret) {
13934 		dev_info(&pf->pdev->dev,
13935 			 "query veb bw ets config failed, err %s aq_err %s\n",
13936 			 i40e_stat_str(&pf->hw, ret),
13937 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13938 		goto out;
13939 	}
13940 
13941 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13942 	veb->bw_max_quanta = ets_data.tc_bw_max;
13943 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13944 	veb->enabled_tc = ets_data.tc_valid_bits;
13945 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13946 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13947 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13948 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13949 		veb->bw_tc_limit_credits[i] =
13950 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13951 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13952 	}
13953 
13954 out:
13955 	return ret;
13956 }
13957 
13958 /**
13959  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13960  * @pf: board private structure
13961  *
13962  * On error: returns error code (negative)
13963  * On success: returns vsi index in PF (positive)
13964  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)13965 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13966 {
13967 	int ret = -ENOENT;
13968 	struct i40e_veb *veb;
13969 	int i;
13970 
13971 	/* Need to protect the allocation of switch elements at the PF level */
13972 	mutex_lock(&pf->switch_mutex);
13973 
13974 	/* VEB list may be fragmented if VEB creation/destruction has
13975 	 * been happening.  We can afford to do a quick scan to look
13976 	 * for any free slots in the list.
13977 	 *
13978 	 * find next empty veb slot, looping back around if necessary
13979 	 */
13980 	i = 0;
13981 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13982 		i++;
13983 	if (i >= I40E_MAX_VEB) {
13984 		ret = -ENOMEM;
13985 		goto err_alloc_veb;  /* out of VEB slots! */
13986 	}
13987 
13988 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13989 	if (!veb) {
13990 		ret = -ENOMEM;
13991 		goto err_alloc_veb;
13992 	}
13993 	veb->pf = pf;
13994 	veb->idx = i;
13995 	veb->enabled_tc = 1;
13996 
13997 	pf->veb[i] = veb;
13998 	ret = i;
13999 err_alloc_veb:
14000 	mutex_unlock(&pf->switch_mutex);
14001 	return ret;
14002 }
14003 
14004 /**
14005  * i40e_switch_branch_release - Delete a branch of the switch tree
14006  * @branch: where to start deleting
14007  *
14008  * This uses recursion to find the tips of the branch to be
14009  * removed, deleting until we get back to and can delete this VEB.
14010  **/
i40e_switch_branch_release(struct i40e_veb * branch)14011 static void i40e_switch_branch_release(struct i40e_veb *branch)
14012 {
14013 	struct i40e_pf *pf = branch->pf;
14014 	u16 branch_seid = branch->seid;
14015 	u16 veb_idx = branch->idx;
14016 	int i;
14017 
14018 	/* release any VEBs on this VEB - RECURSION */
14019 	for (i = 0; i < I40E_MAX_VEB; i++) {
14020 		if (!pf->veb[i])
14021 			continue;
14022 		if (pf->veb[i]->uplink_seid == branch->seid)
14023 			i40e_switch_branch_release(pf->veb[i]);
14024 	}
14025 
14026 	/* Release the VSIs on this VEB, but not the owner VSI.
14027 	 *
14028 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14029 	 *       the VEB itself, so don't use (*branch) after this loop.
14030 	 */
14031 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14032 		if (!pf->vsi[i])
14033 			continue;
14034 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14035 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14036 			i40e_vsi_release(pf->vsi[i]);
14037 		}
14038 	}
14039 
14040 	/* There's one corner case where the VEB might not have been
14041 	 * removed, so double check it here and remove it if needed.
14042 	 * This case happens if the veb was created from the debugfs
14043 	 * commands and no VSIs were added to it.
14044 	 */
14045 	if (pf->veb[veb_idx])
14046 		i40e_veb_release(pf->veb[veb_idx]);
14047 }
14048 
14049 /**
14050  * i40e_veb_clear - remove veb struct
14051  * @veb: the veb to remove
14052  **/
i40e_veb_clear(struct i40e_veb * veb)14053 static void i40e_veb_clear(struct i40e_veb *veb)
14054 {
14055 	if (!veb)
14056 		return;
14057 
14058 	if (veb->pf) {
14059 		struct i40e_pf *pf = veb->pf;
14060 
14061 		mutex_lock(&pf->switch_mutex);
14062 		if (pf->veb[veb->idx] == veb)
14063 			pf->veb[veb->idx] = NULL;
14064 		mutex_unlock(&pf->switch_mutex);
14065 	}
14066 
14067 	kfree(veb);
14068 }
14069 
14070 /**
14071  * i40e_veb_release - Delete a VEB and free its resources
14072  * @veb: the VEB being removed
14073  **/
i40e_veb_release(struct i40e_veb * veb)14074 void i40e_veb_release(struct i40e_veb *veb)
14075 {
14076 	struct i40e_vsi *vsi = NULL;
14077 	struct i40e_pf *pf;
14078 	int i, n = 0;
14079 
14080 	pf = veb->pf;
14081 
14082 	/* find the remaining VSI and check for extras */
14083 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14084 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14085 			n++;
14086 			vsi = pf->vsi[i];
14087 		}
14088 	}
14089 	if (n != 1) {
14090 		dev_info(&pf->pdev->dev,
14091 			 "can't remove VEB %d with %d VSIs left\n",
14092 			 veb->seid, n);
14093 		return;
14094 	}
14095 
14096 	/* move the remaining VSI to uplink veb */
14097 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14098 	if (veb->uplink_seid) {
14099 		vsi->uplink_seid = veb->uplink_seid;
14100 		if (veb->uplink_seid == pf->mac_seid)
14101 			vsi->veb_idx = I40E_NO_VEB;
14102 		else
14103 			vsi->veb_idx = veb->veb_idx;
14104 	} else {
14105 		/* floating VEB */
14106 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14107 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14108 	}
14109 
14110 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14111 	i40e_veb_clear(veb);
14112 }
14113 
14114 /**
14115  * i40e_add_veb - create the VEB in the switch
14116  * @veb: the VEB to be instantiated
14117  * @vsi: the controlling VSI
14118  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14119 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14120 {
14121 	struct i40e_pf *pf = veb->pf;
14122 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14123 	int ret;
14124 
14125 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14126 			      veb->enabled_tc, false,
14127 			      &veb->seid, enable_stats, NULL);
14128 
14129 	/* get a VEB from the hardware */
14130 	if (ret) {
14131 		dev_info(&pf->pdev->dev,
14132 			 "couldn't add VEB, err %s aq_err %s\n",
14133 			 i40e_stat_str(&pf->hw, ret),
14134 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14135 		return -EPERM;
14136 	}
14137 
14138 	/* get statistics counter */
14139 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14140 					 &veb->stats_idx, NULL, NULL, NULL);
14141 	if (ret) {
14142 		dev_info(&pf->pdev->dev,
14143 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14144 			 i40e_stat_str(&pf->hw, ret),
14145 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14146 		return -EPERM;
14147 	}
14148 	ret = i40e_veb_get_bw_info(veb);
14149 	if (ret) {
14150 		dev_info(&pf->pdev->dev,
14151 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14152 			 i40e_stat_str(&pf->hw, ret),
14153 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14154 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14155 		return -ENOENT;
14156 	}
14157 
14158 	vsi->uplink_seid = veb->seid;
14159 	vsi->veb_idx = veb->idx;
14160 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14161 
14162 	return 0;
14163 }
14164 
14165 /**
14166  * i40e_veb_setup - Set up a VEB
14167  * @pf: board private structure
14168  * @flags: VEB setup flags
14169  * @uplink_seid: the switch element to link to
14170  * @vsi_seid: the initial VSI seid
14171  * @enabled_tc: Enabled TC bit-map
14172  *
14173  * This allocates the sw VEB structure and links it into the switch
14174  * It is possible and legal for this to be a duplicate of an already
14175  * existing VEB.  It is also possible for both uplink and vsi seids
14176  * to be zero, in order to create a floating VEB.
14177  *
14178  * Returns pointer to the successfully allocated VEB sw struct on
14179  * success, otherwise returns NULL on failure.
14180  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14181 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14182 				u16 uplink_seid, u16 vsi_seid,
14183 				u8 enabled_tc)
14184 {
14185 	struct i40e_veb *veb, *uplink_veb = NULL;
14186 	int vsi_idx, veb_idx;
14187 	int ret;
14188 
14189 	/* if one seid is 0, the other must be 0 to create a floating relay */
14190 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14191 	    (uplink_seid + vsi_seid != 0)) {
14192 		dev_info(&pf->pdev->dev,
14193 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14194 			 uplink_seid, vsi_seid);
14195 		return NULL;
14196 	}
14197 
14198 	/* make sure there is such a vsi and uplink */
14199 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14200 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14201 			break;
14202 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14203 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14204 			 vsi_seid);
14205 		return NULL;
14206 	}
14207 
14208 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14209 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14210 			if (pf->veb[veb_idx] &&
14211 			    pf->veb[veb_idx]->seid == uplink_seid) {
14212 				uplink_veb = pf->veb[veb_idx];
14213 				break;
14214 			}
14215 		}
14216 		if (!uplink_veb) {
14217 			dev_info(&pf->pdev->dev,
14218 				 "uplink seid %d not found\n", uplink_seid);
14219 			return NULL;
14220 		}
14221 	}
14222 
14223 	/* get veb sw struct */
14224 	veb_idx = i40e_veb_mem_alloc(pf);
14225 	if (veb_idx < 0)
14226 		goto err_alloc;
14227 	veb = pf->veb[veb_idx];
14228 	veb->flags = flags;
14229 	veb->uplink_seid = uplink_seid;
14230 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14231 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14232 
14233 	/* create the VEB in the switch */
14234 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14235 	if (ret)
14236 		goto err_veb;
14237 	if (vsi_idx == pf->lan_vsi)
14238 		pf->lan_veb = veb->idx;
14239 
14240 	return veb;
14241 
14242 err_veb:
14243 	i40e_veb_clear(veb);
14244 err_alloc:
14245 	return NULL;
14246 }
14247 
14248 /**
14249  * i40e_setup_pf_switch_element - set PF vars based on switch type
14250  * @pf: board private structure
14251  * @ele: element we are building info from
14252  * @num_reported: total number of elements
14253  * @printconfig: should we print the contents
14254  *
14255  * helper function to assist in extracting a few useful SEID values.
14256  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14257 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14258 				struct i40e_aqc_switch_config_element_resp *ele,
14259 				u16 num_reported, bool printconfig)
14260 {
14261 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14262 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14263 	u8 element_type = ele->element_type;
14264 	u16 seid = le16_to_cpu(ele->seid);
14265 
14266 	if (printconfig)
14267 		dev_info(&pf->pdev->dev,
14268 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14269 			 element_type, seid, uplink_seid, downlink_seid);
14270 
14271 	switch (element_type) {
14272 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14273 		pf->mac_seid = seid;
14274 		break;
14275 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14276 		/* Main VEB? */
14277 		if (uplink_seid != pf->mac_seid)
14278 			break;
14279 		if (pf->lan_veb >= I40E_MAX_VEB) {
14280 			int v;
14281 
14282 			/* find existing or else empty VEB */
14283 			for (v = 0; v < I40E_MAX_VEB; v++) {
14284 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14285 					pf->lan_veb = v;
14286 					break;
14287 				}
14288 			}
14289 			if (pf->lan_veb >= I40E_MAX_VEB) {
14290 				v = i40e_veb_mem_alloc(pf);
14291 				if (v < 0)
14292 					break;
14293 				pf->lan_veb = v;
14294 			}
14295 		}
14296 		if (pf->lan_veb >= I40E_MAX_VEB)
14297 			break;
14298 
14299 		pf->veb[pf->lan_veb]->seid = seid;
14300 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14301 		pf->veb[pf->lan_veb]->pf = pf;
14302 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14303 		break;
14304 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14305 		if (num_reported != 1)
14306 			break;
14307 		/* This is immediately after a reset so we can assume this is
14308 		 * the PF's VSI
14309 		 */
14310 		pf->mac_seid = uplink_seid;
14311 		pf->pf_seid = downlink_seid;
14312 		pf->main_vsi_seid = seid;
14313 		if (printconfig)
14314 			dev_info(&pf->pdev->dev,
14315 				 "pf_seid=%d main_vsi_seid=%d\n",
14316 				 pf->pf_seid, pf->main_vsi_seid);
14317 		break;
14318 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14319 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14320 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14321 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14322 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14323 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14324 		/* ignore these for now */
14325 		break;
14326 	default:
14327 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14328 			 element_type, seid);
14329 		break;
14330 	}
14331 }
14332 
14333 /**
14334  * i40e_fetch_switch_configuration - Get switch config from firmware
14335  * @pf: board private structure
14336  * @printconfig: should we print the contents
14337  *
14338  * Get the current switch configuration from the device and
14339  * extract a few useful SEID values.
14340  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14341 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14342 {
14343 	struct i40e_aqc_get_switch_config_resp *sw_config;
14344 	u16 next_seid = 0;
14345 	int ret = 0;
14346 	u8 *aq_buf;
14347 	int i;
14348 
14349 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14350 	if (!aq_buf)
14351 		return -ENOMEM;
14352 
14353 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14354 	do {
14355 		u16 num_reported, num_total;
14356 
14357 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14358 						I40E_AQ_LARGE_BUF,
14359 						&next_seid, NULL);
14360 		if (ret) {
14361 			dev_info(&pf->pdev->dev,
14362 				 "get switch config failed err %s aq_err %s\n",
14363 				 i40e_stat_str(&pf->hw, ret),
14364 				 i40e_aq_str(&pf->hw,
14365 					     pf->hw.aq.asq_last_status));
14366 			kfree(aq_buf);
14367 			return -ENOENT;
14368 		}
14369 
14370 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14371 		num_total = le16_to_cpu(sw_config->header.num_total);
14372 
14373 		if (printconfig)
14374 			dev_info(&pf->pdev->dev,
14375 				 "header: %d reported %d total\n",
14376 				 num_reported, num_total);
14377 
14378 		for (i = 0; i < num_reported; i++) {
14379 			struct i40e_aqc_switch_config_element_resp *ele =
14380 				&sw_config->element[i];
14381 
14382 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14383 						     printconfig);
14384 		}
14385 	} while (next_seid != 0);
14386 
14387 	kfree(aq_buf);
14388 	return ret;
14389 }
14390 
14391 /**
14392  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14393  * @pf: board private structure
14394  * @reinit: if the Main VSI needs to re-initialized.
14395  * @lock_acquired: indicates whether or not the lock has been acquired
14396  *
14397  * Returns 0 on success, negative value on failure
14398  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)14399 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14400 {
14401 	u16 flags = 0;
14402 	int ret;
14403 
14404 	/* find out what's out there already */
14405 	ret = i40e_fetch_switch_configuration(pf, false);
14406 	if (ret) {
14407 		dev_info(&pf->pdev->dev,
14408 			 "couldn't fetch switch config, err %s aq_err %s\n",
14409 			 i40e_stat_str(&pf->hw, ret),
14410 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14411 		return ret;
14412 	}
14413 	i40e_pf_reset_stats(pf);
14414 
14415 	/* set the switch config bit for the whole device to
14416 	 * support limited promisc or true promisc
14417 	 * when user requests promisc. The default is limited
14418 	 * promisc.
14419 	*/
14420 
14421 	if ((pf->hw.pf_id == 0) &&
14422 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14423 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14424 		pf->last_sw_conf_flags = flags;
14425 	}
14426 
14427 	if (pf->hw.pf_id == 0) {
14428 		u16 valid_flags;
14429 
14430 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14431 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14432 						NULL);
14433 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14434 			dev_info(&pf->pdev->dev,
14435 				 "couldn't set switch config bits, err %s aq_err %s\n",
14436 				 i40e_stat_str(&pf->hw, ret),
14437 				 i40e_aq_str(&pf->hw,
14438 					     pf->hw.aq.asq_last_status));
14439 			/* not a fatal problem, just keep going */
14440 		}
14441 		pf->last_sw_conf_valid_flags = valid_flags;
14442 	}
14443 
14444 	/* first time setup */
14445 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14446 		struct i40e_vsi *vsi = NULL;
14447 		u16 uplink_seid;
14448 
14449 		/* Set up the PF VSI associated with the PF's main VSI
14450 		 * that is already in the HW switch
14451 		 */
14452 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14453 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14454 		else
14455 			uplink_seid = pf->mac_seid;
14456 		if (pf->lan_vsi == I40E_NO_VSI)
14457 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14458 		else if (reinit)
14459 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14460 		if (!vsi) {
14461 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14462 			i40e_cloud_filter_exit(pf);
14463 			i40e_fdir_teardown(pf);
14464 			return -EAGAIN;
14465 		}
14466 	} else {
14467 		/* force a reset of TC and queue layout configurations */
14468 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14469 
14470 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14471 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14472 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14473 	}
14474 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14475 
14476 	i40e_fdir_sb_setup(pf);
14477 
14478 	/* Setup static PF queue filter control settings */
14479 	ret = i40e_setup_pf_filter_control(pf);
14480 	if (ret) {
14481 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14482 			 ret);
14483 		/* Failure here should not stop continuing other steps */
14484 	}
14485 
14486 	/* enable RSS in the HW, even for only one queue, as the stack can use
14487 	 * the hash
14488 	 */
14489 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14490 		i40e_pf_config_rss(pf);
14491 
14492 	/* fill in link information and enable LSE reporting */
14493 	i40e_link_event(pf);
14494 
14495 	/* Initialize user-specific link properties */
14496 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14497 				  I40E_AQ_AN_COMPLETED) ? true : false);
14498 
14499 	i40e_ptp_init(pf);
14500 
14501 	if (!lock_acquired)
14502 		rtnl_lock();
14503 
14504 	/* repopulate tunnel port filters */
14505 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14506 
14507 	if (!lock_acquired)
14508 		rtnl_unlock();
14509 
14510 	return ret;
14511 }
14512 
14513 /**
14514  * i40e_determine_queue_usage - Work out queue distribution
14515  * @pf: board private structure
14516  **/
i40e_determine_queue_usage(struct i40e_pf * pf)14517 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14518 {
14519 	int queues_left;
14520 	int q_max;
14521 
14522 	pf->num_lan_qps = 0;
14523 
14524 	/* Find the max queues to be put into basic use.  We'll always be
14525 	 * using TC0, whether or not DCB is running, and TC0 will get the
14526 	 * big RSS set.
14527 	 */
14528 	queues_left = pf->hw.func_caps.num_tx_qp;
14529 
14530 	if ((queues_left == 1) ||
14531 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14532 		/* one qp for PF, no queues for anything else */
14533 		queues_left = 0;
14534 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14535 
14536 		/* make sure all the fancies are disabled */
14537 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14538 			       I40E_FLAG_IWARP_ENABLED	|
14539 			       I40E_FLAG_FD_SB_ENABLED	|
14540 			       I40E_FLAG_FD_ATR_ENABLED	|
14541 			       I40E_FLAG_DCB_CAPABLE	|
14542 			       I40E_FLAG_DCB_ENABLED	|
14543 			       I40E_FLAG_SRIOV_ENABLED	|
14544 			       I40E_FLAG_VMDQ_ENABLED);
14545 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14546 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14547 				  I40E_FLAG_FD_SB_ENABLED |
14548 				  I40E_FLAG_FD_ATR_ENABLED |
14549 				  I40E_FLAG_DCB_CAPABLE))) {
14550 		/* one qp for PF */
14551 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14552 		queues_left -= pf->num_lan_qps;
14553 
14554 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14555 			       I40E_FLAG_IWARP_ENABLED	|
14556 			       I40E_FLAG_FD_SB_ENABLED	|
14557 			       I40E_FLAG_FD_ATR_ENABLED	|
14558 			       I40E_FLAG_DCB_ENABLED	|
14559 			       I40E_FLAG_VMDQ_ENABLED);
14560 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14561 	} else {
14562 		/* Not enough queues for all TCs */
14563 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14564 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14565 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14566 					I40E_FLAG_DCB_ENABLED);
14567 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14568 		}
14569 
14570 		/* limit lan qps to the smaller of qps, cpus or msix */
14571 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14572 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14573 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14574 		pf->num_lan_qps = q_max;
14575 
14576 		queues_left -= pf->num_lan_qps;
14577 	}
14578 
14579 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14580 		if (queues_left > 1) {
14581 			queues_left -= 1; /* save 1 queue for FD */
14582 		} else {
14583 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14584 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14585 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14586 		}
14587 	}
14588 
14589 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14590 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14591 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14592 					(queues_left / pf->num_vf_qps));
14593 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14594 	}
14595 
14596 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14597 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14598 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14599 					  (queues_left / pf->num_vmdq_qps));
14600 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14601 	}
14602 
14603 	pf->queues_left = queues_left;
14604 	dev_dbg(&pf->pdev->dev,
14605 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14606 		pf->hw.func_caps.num_tx_qp,
14607 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14608 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14609 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14610 		queues_left);
14611 }
14612 
14613 /**
14614  * i40e_setup_pf_filter_control - Setup PF static filter control
14615  * @pf: PF to be setup
14616  *
14617  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14618  * settings. If PE/FCoE are enabled then it will also set the per PF
14619  * based filter sizes required for them. It also enables Flow director,
14620  * ethertype and macvlan type filter settings for the pf.
14621  *
14622  * Returns 0 on success, negative on failure
14623  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)14624 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14625 {
14626 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14627 
14628 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14629 
14630 	/* Flow Director is enabled */
14631 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14632 		settings->enable_fdir = true;
14633 
14634 	/* Ethtype and MACVLAN filters enabled for PF */
14635 	settings->enable_ethtype = true;
14636 	settings->enable_macvlan = true;
14637 
14638 	if (i40e_set_filter_control(&pf->hw, settings))
14639 		return -ENOENT;
14640 
14641 	return 0;
14642 }
14643 
14644 #define INFO_STRING_LEN 255
14645 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)14646 static void i40e_print_features(struct i40e_pf *pf)
14647 {
14648 	struct i40e_hw *hw = &pf->hw;
14649 	char *buf;
14650 	int i;
14651 
14652 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14653 	if (!buf)
14654 		return;
14655 
14656 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14657 #ifdef CONFIG_PCI_IOV
14658 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14659 #endif
14660 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14661 		      pf->hw.func_caps.num_vsis,
14662 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14663 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14664 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14665 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14666 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14667 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14668 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14669 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14670 	}
14671 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14672 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14673 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14674 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14675 	if (pf->flags & I40E_FLAG_PTP)
14676 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14677 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14678 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14679 	else
14680 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14681 
14682 	dev_info(&pf->pdev->dev, "%s\n", buf);
14683 	kfree(buf);
14684 	WARN_ON(i > INFO_STRING_LEN);
14685 }
14686 
14687 /**
14688  * i40e_get_platform_mac_addr - get platform-specific MAC address
14689  * @pdev: PCI device information struct
14690  * @pf: board private structure
14691  *
14692  * Look up the MAC address for the device. First we'll try
14693  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14694  * specific fallback. Otherwise, we'll default to the stored value in
14695  * firmware.
14696  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)14697 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14698 {
14699 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14700 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14701 }
14702 
14703 /**
14704  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14705  * @fec_cfg: FEC option to set in flags
14706  * @flags: ptr to flags in which we set FEC option
14707  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)14708 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14709 {
14710 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14711 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14712 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14713 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14714 		*flags |= I40E_FLAG_RS_FEC;
14715 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14716 	}
14717 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14718 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14719 		*flags |= I40E_FLAG_BASE_R_FEC;
14720 		*flags &= ~I40E_FLAG_RS_FEC;
14721 	}
14722 	if (fec_cfg == 0)
14723 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14724 }
14725 
14726 /**
14727  * i40e_check_recovery_mode - check if we are running transition firmware
14728  * @pf: board private structure
14729  *
14730  * Check registers indicating the firmware runs in recovery mode. Sets the
14731  * appropriate driver state.
14732  *
14733  * Returns true if the recovery mode was detected, false otherwise
14734  **/
i40e_check_recovery_mode(struct i40e_pf * pf)14735 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14736 {
14737 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14738 
14739 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14740 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14741 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14742 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14743 
14744 		return true;
14745 	}
14746 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14747 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
14748 
14749 	return false;
14750 }
14751 
14752 /**
14753  * i40e_pf_loop_reset - perform reset in a loop.
14754  * @pf: board private structure
14755  *
14756  * This function is useful when a NIC is about to enter recovery mode.
14757  * When a NIC's internal data structures are corrupted the NIC's
14758  * firmware is going to enter recovery mode.
14759  * Right after a POR it takes about 7 minutes for firmware to enter
14760  * recovery mode. Until that time a NIC is in some kind of intermediate
14761  * state. After that time period the NIC almost surely enters
14762  * recovery mode. The only way for a driver to detect intermediate
14763  * state is to issue a series of pf-resets and check a return value.
14764  * If a PF reset returns success then the firmware could be in recovery
14765  * mode so the caller of this code needs to check for recovery mode
14766  * if this function returns success. There is a little chance that
14767  * firmware will hang in intermediate state forever.
14768  * Since waiting 7 minutes is quite a lot of time this function waits
14769  * 10 seconds and then gives up by returning an error.
14770  *
14771  * Return 0 on success, negative on failure.
14772  **/
i40e_pf_loop_reset(struct i40e_pf * pf)14773 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
14774 {
14775 	/* wait max 10 seconds for PF reset to succeed */
14776 	const unsigned long time_end = jiffies + 10 * HZ;
14777 
14778 	struct i40e_hw *hw = &pf->hw;
14779 	i40e_status ret;
14780 
14781 	ret = i40e_pf_reset(hw);
14782 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
14783 		usleep_range(10000, 20000);
14784 		ret = i40e_pf_reset(hw);
14785 	}
14786 
14787 	if (ret == I40E_SUCCESS)
14788 		pf->pfr_count++;
14789 	else
14790 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
14791 
14792 	return ret;
14793 }
14794 
14795 /**
14796  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
14797  * @pf: board private structure
14798  *
14799  * Check FW registers to determine if FW issued unexpected EMP Reset.
14800  * Every time when unexpected EMP Reset occurs the FW increments
14801  * a counter of unexpected EMP Resets. When the counter reaches 10
14802  * the FW should enter the Recovery mode
14803  *
14804  * Returns true if FW issued unexpected EMP Reset
14805  **/
i40e_check_fw_empr(struct i40e_pf * pf)14806 static bool i40e_check_fw_empr(struct i40e_pf *pf)
14807 {
14808 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
14809 			   I40E_GL_FWSTS_FWS1B_MASK;
14810 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
14811 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
14812 }
14813 
14814 /**
14815  * i40e_handle_resets - handle EMP resets and PF resets
14816  * @pf: board private structure
14817  *
14818  * Handle both EMP resets and PF resets and conclude whether there are
14819  * any issues regarding these resets. If there are any issues then
14820  * generate log entry.
14821  *
14822  * Return 0 if NIC is healthy or negative value when there are issues
14823  * with resets
14824  **/
i40e_handle_resets(struct i40e_pf * pf)14825 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
14826 {
14827 	const i40e_status pfr = i40e_pf_loop_reset(pf);
14828 	const bool is_empr = i40e_check_fw_empr(pf);
14829 
14830 	if (is_empr || pfr != I40E_SUCCESS)
14831 		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");
14832 
14833 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
14834 }
14835 
14836 /**
14837  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14838  * @pf: board private structure
14839  * @hw: ptr to the hardware info
14840  *
14841  * This function does a minimal setup of all subsystems needed for running
14842  * recovery mode.
14843  *
14844  * Returns 0 on success, negative on failure
14845  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)14846 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14847 {
14848 	struct i40e_vsi *vsi;
14849 	int err;
14850 	int v_idx;
14851 
14852 	pci_save_state(pf->pdev);
14853 
14854 	/* set up periodic task facility */
14855 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14856 	pf->service_timer_period = HZ;
14857 
14858 	INIT_WORK(&pf->service_task, i40e_service_task);
14859 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14860 
14861 	err = i40e_init_interrupt_scheme(pf);
14862 	if (err)
14863 		goto err_switch_setup;
14864 
14865 	/* The number of VSIs reported by the FW is the minimum guaranteed
14866 	 * to us; HW supports far more and we share the remaining pool with
14867 	 * the other PFs. We allocate space for more than the guarantee with
14868 	 * the understanding that we might not get them all later.
14869 	 */
14870 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14871 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14872 	else
14873 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14874 
14875 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14876 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14877 			  GFP_KERNEL);
14878 	if (!pf->vsi) {
14879 		err = -ENOMEM;
14880 		goto err_switch_setup;
14881 	}
14882 
14883 	/* We allocate one VSI which is needed as absolute minimum
14884 	 * in order to register the netdev
14885 	 */
14886 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14887 	if (v_idx < 0) {
14888 		err = v_idx;
14889 		goto err_switch_setup;
14890 	}
14891 	pf->lan_vsi = v_idx;
14892 	vsi = pf->vsi[v_idx];
14893 	if (!vsi) {
14894 		err = -EFAULT;
14895 		goto err_switch_setup;
14896 	}
14897 	vsi->alloc_queue_pairs = 1;
14898 	err = i40e_config_netdev(vsi);
14899 	if (err)
14900 		goto err_switch_setup;
14901 	err = register_netdev(vsi->netdev);
14902 	if (err)
14903 		goto err_switch_setup;
14904 	vsi->netdev_registered = true;
14905 	i40e_dbg_pf_init(pf);
14906 
14907 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14908 	if (err)
14909 		goto err_switch_setup;
14910 
14911 	/* tell the firmware that we're starting */
14912 	i40e_send_version(pf);
14913 
14914 	/* since everything's happy, start the service_task timer */
14915 	mod_timer(&pf->service_timer,
14916 		  round_jiffies(jiffies + pf->service_timer_period));
14917 
14918 	return 0;
14919 
14920 err_switch_setup:
14921 	i40e_reset_interrupt_capability(pf);
14922 	del_timer_sync(&pf->service_timer);
14923 	i40e_shutdown_adminq(hw);
14924 	iounmap(hw->hw_addr);
14925 	pci_disable_pcie_error_reporting(pf->pdev);
14926 	pci_release_mem_regions(pf->pdev);
14927 	pci_disable_device(pf->pdev);
14928 	kfree(pf);
14929 
14930 	return err;
14931 }
14932 
14933 /**
14934  * i40e_probe - Device initialization routine
14935  * @pdev: PCI device information struct
14936  * @ent: entry in i40e_pci_tbl
14937  *
14938  * i40e_probe initializes a PF identified by a pci_dev structure.
14939  * The OS initialization, configuring of the PF private structure,
14940  * and a hardware reset occur.
14941  *
14942  * Returns 0 on success, negative on failure
14943  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)14944 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14945 {
14946 	struct i40e_aq_get_phy_abilities_resp abilities;
14947 	struct i40e_pf *pf;
14948 	struct i40e_hw *hw;
14949 	static u16 pfs_found;
14950 	u16 wol_nvm_bits;
14951 	u16 link_status;
14952 	int err;
14953 	u32 val;
14954 	u32 i;
14955 
14956 	err = pci_enable_device_mem(pdev);
14957 	if (err)
14958 		return err;
14959 
14960 	/* set up for high or low dma */
14961 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14962 	if (err) {
14963 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14964 		if (err) {
14965 			dev_err(&pdev->dev,
14966 				"DMA configuration failed: 0x%x\n", err);
14967 			goto err_dma;
14968 		}
14969 	}
14970 
14971 	/* set up pci connections */
14972 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14973 	if (err) {
14974 		dev_info(&pdev->dev,
14975 			 "pci_request_selected_regions failed %d\n", err);
14976 		goto err_pci_reg;
14977 	}
14978 
14979 	pci_enable_pcie_error_reporting(pdev);
14980 	pci_set_master(pdev);
14981 
14982 	/* Now that we have a PCI connection, we need to do the
14983 	 * low level device setup.  This is primarily setting up
14984 	 * the Admin Queue structures and then querying for the
14985 	 * device's current profile information.
14986 	 */
14987 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14988 	if (!pf) {
14989 		err = -ENOMEM;
14990 		goto err_pf_alloc;
14991 	}
14992 	pf->next_vsi = 0;
14993 	pf->pdev = pdev;
14994 	set_bit(__I40E_DOWN, pf->state);
14995 
14996 	hw = &pf->hw;
14997 	hw->back = pf;
14998 
14999 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15000 				I40E_MAX_CSR_SPACE);
15001 	/* We believe that the highest register to read is
15002 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15003 	 * is not less than that before mapping to prevent a
15004 	 * kernel panic.
15005 	 */
15006 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15007 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15008 			pf->ioremap_len);
15009 		err = -ENOMEM;
15010 		goto err_ioremap;
15011 	}
15012 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15013 	if (!hw->hw_addr) {
15014 		err = -EIO;
15015 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15016 			 (unsigned int)pci_resource_start(pdev, 0),
15017 			 pf->ioremap_len, err);
15018 		goto err_ioremap;
15019 	}
15020 	hw->vendor_id = pdev->vendor;
15021 	hw->device_id = pdev->device;
15022 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15023 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15024 	hw->subsystem_device_id = pdev->subsystem_device;
15025 	hw->bus.device = PCI_SLOT(pdev->devfn);
15026 	hw->bus.func = PCI_FUNC(pdev->devfn);
15027 	hw->bus.bus_id = pdev->bus->number;
15028 	pf->instance = pfs_found;
15029 
15030 	/* Select something other than the 802.1ad ethertype for the
15031 	 * switch to use internally and drop on ingress.
15032 	 */
15033 	hw->switch_tag = 0xffff;
15034 	hw->first_tag = ETH_P_8021AD;
15035 	hw->second_tag = ETH_P_8021Q;
15036 
15037 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15038 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15039 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15040 
15041 	/* set up the locks for the AQ, do this only once in probe
15042 	 * and destroy them only once in remove
15043 	 */
15044 	mutex_init(&hw->aq.asq_mutex);
15045 	mutex_init(&hw->aq.arq_mutex);
15046 
15047 	pf->msg_enable = netif_msg_init(debug,
15048 					NETIF_MSG_DRV |
15049 					NETIF_MSG_PROBE |
15050 					NETIF_MSG_LINK);
15051 	if (debug < -1)
15052 		pf->hw.debug_mask = debug;
15053 
15054 	/* do a special CORER for clearing PXE mode once at init */
15055 	if (hw->revision_id == 0 &&
15056 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15057 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15058 		i40e_flush(hw);
15059 		msleep(200);
15060 		pf->corer_count++;
15061 
15062 		i40e_clear_pxe_mode(hw);
15063 	}
15064 
15065 	/* Reset here to make sure all is clean and to define PF 'n' */
15066 	i40e_clear_hw(hw);
15067 
15068 	err = i40e_set_mac_type(hw);
15069 	if (err) {
15070 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15071 			 err);
15072 		goto err_pf_reset;
15073 	}
15074 
15075 	err = i40e_handle_resets(pf);
15076 	if (err)
15077 		goto err_pf_reset;
15078 
15079 	i40e_check_recovery_mode(pf);
15080 
15081 	hw->aq.num_arq_entries = I40E_AQ_LEN;
15082 	hw->aq.num_asq_entries = I40E_AQ_LEN;
15083 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15084 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15085 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15086 
15087 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15088 		 "%s-%s:misc",
15089 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15090 
15091 	err = i40e_init_shared_code(hw);
15092 	if (err) {
15093 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15094 			 err);
15095 		goto err_pf_reset;
15096 	}
15097 
15098 	/* set up a default setting for link flow control */
15099 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15100 
15101 	err = i40e_init_adminq(hw);
15102 	if (err) {
15103 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15104 			dev_info(&pdev->dev,
15105 				 "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",
15106 				 hw->aq.api_maj_ver,
15107 				 hw->aq.api_min_ver,
15108 				 I40E_FW_API_VERSION_MAJOR,
15109 				 I40E_FW_MINOR_VERSION(hw));
15110 		else
15111 			dev_info(&pdev->dev,
15112 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15113 
15114 		goto err_pf_reset;
15115 	}
15116 	i40e_get_oem_version(hw);
15117 
15118 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15119 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15120 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15121 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15122 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15123 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15124 
15125 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15126 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15127 		dev_dbg(&pdev->dev,
15128 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15129 			 hw->aq.api_maj_ver,
15130 			 hw->aq.api_min_ver,
15131 			 I40E_FW_API_VERSION_MAJOR,
15132 			 I40E_FW_MINOR_VERSION(hw));
15133 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15134 		dev_info(&pdev->dev,
15135 			 "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",
15136 			 hw->aq.api_maj_ver,
15137 			 hw->aq.api_min_ver,
15138 			 I40E_FW_API_VERSION_MAJOR,
15139 			 I40E_FW_MINOR_VERSION(hw));
15140 
15141 	i40e_verify_eeprom(pf);
15142 
15143 	/* Rev 0 hardware was never productized */
15144 	if (hw->revision_id < 1)
15145 		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");
15146 
15147 	i40e_clear_pxe_mode(hw);
15148 
15149 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15150 	if (err)
15151 		goto err_adminq_setup;
15152 
15153 	err = i40e_sw_init(pf);
15154 	if (err) {
15155 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15156 		goto err_sw_init;
15157 	}
15158 
15159 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15160 		return i40e_init_recovery_mode(pf, hw);
15161 
15162 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15163 				hw->func_caps.num_rx_qp, 0, 0);
15164 	if (err) {
15165 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15166 		goto err_init_lan_hmc;
15167 	}
15168 
15169 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15170 	if (err) {
15171 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15172 		err = -ENOENT;
15173 		goto err_configure_lan_hmc;
15174 	}
15175 
15176 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15177 	 * Ignore error return codes because if it was already disabled via
15178 	 * hardware settings this will fail
15179 	 */
15180 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15181 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15182 		i40e_aq_stop_lldp(hw, true, false, NULL);
15183 	}
15184 
15185 	/* allow a platform config to override the HW addr */
15186 	i40e_get_platform_mac_addr(pdev, pf);
15187 
15188 	if (!is_valid_ether_addr(hw->mac.addr)) {
15189 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15190 		err = -EIO;
15191 		goto err_mac_addr;
15192 	}
15193 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15194 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15195 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15196 	if (is_valid_ether_addr(hw->mac.port_addr))
15197 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15198 
15199 	pci_set_drvdata(pdev, pf);
15200 	pci_save_state(pdev);
15201 
15202 	dev_info(&pdev->dev,
15203 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15204 			"FW LLDP is disabled\n" :
15205 			"FW LLDP is enabled\n");
15206 
15207 	/* Enable FW to write default DCB config on link-up */
15208 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15209 
15210 #ifdef CONFIG_I40E_DCB
15211 	err = i40e_init_pf_dcb(pf);
15212 	if (err) {
15213 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15214 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15215 		/* Continue without DCB enabled */
15216 	}
15217 #endif /* CONFIG_I40E_DCB */
15218 
15219 	/* set up periodic task facility */
15220 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15221 	pf->service_timer_period = HZ;
15222 
15223 	INIT_WORK(&pf->service_task, i40e_service_task);
15224 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15225 
15226 	/* NVM bit on means WoL disabled for the port */
15227 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15228 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15229 		pf->wol_en = false;
15230 	else
15231 		pf->wol_en = true;
15232 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15233 
15234 	/* set up the main switch operations */
15235 	i40e_determine_queue_usage(pf);
15236 	err = i40e_init_interrupt_scheme(pf);
15237 	if (err)
15238 		goto err_switch_setup;
15239 
15240 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15241 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15242 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15243 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15244 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15245 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15246 						    UDP_TUNNEL_TYPE_GENEVE;
15247 
15248 	/* The number of VSIs reported by the FW is the minimum guaranteed
15249 	 * to us; HW supports far more and we share the remaining pool with
15250 	 * the other PFs. We allocate space for more than the guarantee with
15251 	 * the understanding that we might not get them all later.
15252 	 */
15253 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15254 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15255 	else
15256 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15257 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15258 		dev_warn(&pf->pdev->dev,
15259 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15260 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15261 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15262 	}
15263 
15264 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15265 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15266 			  GFP_KERNEL);
15267 	if (!pf->vsi) {
15268 		err = -ENOMEM;
15269 		goto err_switch_setup;
15270 	}
15271 
15272 #ifdef CONFIG_PCI_IOV
15273 	/* prep for VF support */
15274 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15275 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15276 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15277 		if (pci_num_vf(pdev))
15278 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15279 	}
15280 #endif
15281 	err = i40e_setup_pf_switch(pf, false, false);
15282 	if (err) {
15283 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15284 		goto err_vsis;
15285 	}
15286 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15287 
15288 	/* if FDIR VSI was set up, start it now */
15289 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15290 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15291 			i40e_vsi_open(pf->vsi[i]);
15292 			break;
15293 		}
15294 	}
15295 
15296 	/* The driver only wants link up/down and module qualification
15297 	 * reports from firmware.  Note the negative logic.
15298 	 */
15299 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15300 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15301 					 I40E_AQ_EVENT_MEDIA_NA |
15302 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15303 	if (err)
15304 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15305 			 i40e_stat_str(&pf->hw, err),
15306 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15307 
15308 	/* Reconfigure hardware for allowing smaller MSS in the case
15309 	 * of TSO, so that we avoid the MDD being fired and causing
15310 	 * a reset in the case of small MSS+TSO.
15311 	 */
15312 	val = rd32(hw, I40E_REG_MSS);
15313 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15314 		val &= ~I40E_REG_MSS_MIN_MASK;
15315 		val |= I40E_64BYTE_MSS;
15316 		wr32(hw, I40E_REG_MSS, val);
15317 	}
15318 
15319 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15320 		msleep(75);
15321 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15322 		if (err)
15323 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15324 				 i40e_stat_str(&pf->hw, err),
15325 				 i40e_aq_str(&pf->hw,
15326 					     pf->hw.aq.asq_last_status));
15327 	}
15328 	/* The main driver is (mostly) up and happy. We need to set this state
15329 	 * before setting up the misc vector or we get a race and the vector
15330 	 * ends up disabled forever.
15331 	 */
15332 	clear_bit(__I40E_DOWN, pf->state);
15333 
15334 	/* In case of MSIX we are going to setup the misc vector right here
15335 	 * to handle admin queue events etc. In case of legacy and MSI
15336 	 * the misc functionality and queue processing is combined in
15337 	 * the same vector and that gets setup at open.
15338 	 */
15339 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15340 		err = i40e_setup_misc_vector(pf);
15341 		if (err) {
15342 			dev_info(&pdev->dev,
15343 				 "setup of misc vector failed: %d\n", err);
15344 			i40e_cloud_filter_exit(pf);
15345 			i40e_fdir_teardown(pf);
15346 			goto err_vsis;
15347 		}
15348 	}
15349 
15350 #ifdef CONFIG_PCI_IOV
15351 	/* prep for VF support */
15352 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15353 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15354 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15355 		/* disable link interrupts for VFs */
15356 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15357 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15358 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15359 		i40e_flush(hw);
15360 
15361 		if (pci_num_vf(pdev)) {
15362 			dev_info(&pdev->dev,
15363 				 "Active VFs found, allocating resources.\n");
15364 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15365 			if (err)
15366 				dev_info(&pdev->dev,
15367 					 "Error %d allocating resources for existing VFs\n",
15368 					 err);
15369 		}
15370 	}
15371 #endif /* CONFIG_PCI_IOV */
15372 
15373 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15374 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15375 						      pf->num_iwarp_msix,
15376 						      I40E_IWARP_IRQ_PILE_ID);
15377 		if (pf->iwarp_base_vector < 0) {
15378 			dev_info(&pdev->dev,
15379 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15380 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15381 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15382 		}
15383 	}
15384 
15385 	i40e_dbg_pf_init(pf);
15386 
15387 	/* tell the firmware that we're starting */
15388 	i40e_send_version(pf);
15389 
15390 	/* since everything's happy, start the service_task timer */
15391 	mod_timer(&pf->service_timer,
15392 		  round_jiffies(jiffies + pf->service_timer_period));
15393 
15394 	/* add this PF to client device list and launch a client service task */
15395 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15396 		err = i40e_lan_add_device(pf);
15397 		if (err)
15398 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15399 				 err);
15400 	}
15401 
15402 #define PCI_SPEED_SIZE 8
15403 #define PCI_WIDTH_SIZE 8
15404 	/* Devices on the IOSF bus do not have this information
15405 	 * and will report PCI Gen 1 x 1 by default so don't bother
15406 	 * checking them.
15407 	 */
15408 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15409 		char speed[PCI_SPEED_SIZE] = "Unknown";
15410 		char width[PCI_WIDTH_SIZE] = "Unknown";
15411 
15412 		/* Get the negotiated link width and speed from PCI config
15413 		 * space
15414 		 */
15415 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15416 					  &link_status);
15417 
15418 		i40e_set_pci_config_data(hw, link_status);
15419 
15420 		switch (hw->bus.speed) {
15421 		case i40e_bus_speed_8000:
15422 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15423 		case i40e_bus_speed_5000:
15424 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15425 		case i40e_bus_speed_2500:
15426 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15427 		default:
15428 			break;
15429 		}
15430 		switch (hw->bus.width) {
15431 		case i40e_bus_width_pcie_x8:
15432 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15433 		case i40e_bus_width_pcie_x4:
15434 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15435 		case i40e_bus_width_pcie_x2:
15436 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15437 		case i40e_bus_width_pcie_x1:
15438 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15439 		default:
15440 			break;
15441 		}
15442 
15443 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15444 			 speed, width);
15445 
15446 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15447 		    hw->bus.speed < i40e_bus_speed_8000) {
15448 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15449 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15450 		}
15451 	}
15452 
15453 	/* get the requested speeds from the fw */
15454 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15455 	if (err)
15456 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15457 			i40e_stat_str(&pf->hw, err),
15458 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15459 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15460 
15461 	/* set the FEC config due to the board capabilities */
15462 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15463 
15464 	/* get the supported phy types from the fw */
15465 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15466 	if (err)
15467 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15468 			i40e_stat_str(&pf->hw, err),
15469 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15470 
15471 	/* make sure the MFS hasn't been set lower than the default */
15472 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15473 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15474 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15475 	if (val < MAX_FRAME_SIZE_DEFAULT)
15476 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15477 			 i, val);
15478 
15479 	/* Add a filter to drop all Flow control frames from any VSI from being
15480 	 * transmitted. By doing so we stop a malicious VF from sending out
15481 	 * PAUSE or PFC frames and potentially controlling traffic for other
15482 	 * PF/VF VSIs.
15483 	 * The FW can still send Flow control frames if enabled.
15484 	 */
15485 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15486 						       pf->main_vsi_seid);
15487 
15488 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15489 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15490 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15491 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15492 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15493 	/* print a string summarizing features */
15494 	i40e_print_features(pf);
15495 
15496 	return 0;
15497 
15498 	/* Unwind what we've done if something failed in the setup */
15499 err_vsis:
15500 	set_bit(__I40E_DOWN, pf->state);
15501 	i40e_clear_interrupt_scheme(pf);
15502 	kfree(pf->vsi);
15503 err_switch_setup:
15504 	i40e_reset_interrupt_capability(pf);
15505 	del_timer_sync(&pf->service_timer);
15506 err_mac_addr:
15507 err_configure_lan_hmc:
15508 	(void)i40e_shutdown_lan_hmc(hw);
15509 err_init_lan_hmc:
15510 	kfree(pf->qp_pile);
15511 err_sw_init:
15512 err_adminq_setup:
15513 err_pf_reset:
15514 	iounmap(hw->hw_addr);
15515 err_ioremap:
15516 	kfree(pf);
15517 err_pf_alloc:
15518 	pci_disable_pcie_error_reporting(pdev);
15519 	pci_release_mem_regions(pdev);
15520 err_pci_reg:
15521 err_dma:
15522 	pci_disable_device(pdev);
15523 	return err;
15524 }
15525 
15526 /**
15527  * i40e_remove - Device removal routine
15528  * @pdev: PCI device information struct
15529  *
15530  * i40e_remove is called by the PCI subsystem to alert the driver
15531  * that is should release a PCI device.  This could be caused by a
15532  * Hot-Plug event, or because the driver is going to be removed from
15533  * memory.
15534  **/
i40e_remove(struct pci_dev * pdev)15535 static void i40e_remove(struct pci_dev *pdev)
15536 {
15537 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15538 	struct i40e_hw *hw = &pf->hw;
15539 	i40e_status ret_code;
15540 	int i;
15541 
15542 	i40e_dbg_pf_exit(pf);
15543 
15544 	i40e_ptp_stop(pf);
15545 
15546 	/* Disable RSS in hw */
15547 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15548 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15549 
15550 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15551 		usleep_range(1000, 2000);
15552 
15553 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15554 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15555 		i40e_free_vfs(pf);
15556 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15557 	}
15558 	/* no more scheduling of any task */
15559 	set_bit(__I40E_SUSPENDED, pf->state);
15560 	set_bit(__I40E_DOWN, pf->state);
15561 	if (pf->service_timer.function)
15562 		del_timer_sync(&pf->service_timer);
15563 	if (pf->service_task.func)
15564 		cancel_work_sync(&pf->service_task);
15565 
15566 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15567 		struct i40e_vsi *vsi = pf->vsi[0];
15568 
15569 		/* We know that we have allocated only one vsi for this PF,
15570 		 * it was just for registering netdevice, so the interface
15571 		 * could be visible in the 'ifconfig' output
15572 		 */
15573 		unregister_netdev(vsi->netdev);
15574 		free_netdev(vsi->netdev);
15575 
15576 		goto unmap;
15577 	}
15578 
15579 	/* Client close must be called explicitly here because the timer
15580 	 * has been stopped.
15581 	 */
15582 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15583 
15584 	i40e_fdir_teardown(pf);
15585 
15586 	/* If there is a switch structure or any orphans, remove them.
15587 	 * This will leave only the PF's VSI remaining.
15588 	 */
15589 	for (i = 0; i < I40E_MAX_VEB; i++) {
15590 		if (!pf->veb[i])
15591 			continue;
15592 
15593 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15594 		    pf->veb[i]->uplink_seid == 0)
15595 			i40e_switch_branch_release(pf->veb[i]);
15596 	}
15597 
15598 	/* Now we can shutdown the PF's VSI, just before we kill
15599 	 * adminq and hmc.
15600 	 */
15601 	if (pf->vsi[pf->lan_vsi])
15602 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15603 
15604 	i40e_cloud_filter_exit(pf);
15605 
15606 	/* remove attached clients */
15607 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15608 		ret_code = i40e_lan_del_device(pf);
15609 		if (ret_code)
15610 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15611 				 ret_code);
15612 	}
15613 
15614 	/* shutdown and destroy the HMC */
15615 	if (hw->hmc.hmc_obj) {
15616 		ret_code = i40e_shutdown_lan_hmc(hw);
15617 		if (ret_code)
15618 			dev_warn(&pdev->dev,
15619 				 "Failed to destroy the HMC resources: %d\n",
15620 				 ret_code);
15621 	}
15622 
15623 unmap:
15624 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15625 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15626 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15627 		free_irq(pf->pdev->irq, pf);
15628 
15629 	/* shutdown the adminq */
15630 	i40e_shutdown_adminq(hw);
15631 
15632 	/* destroy the locks only once, here */
15633 	mutex_destroy(&hw->aq.arq_mutex);
15634 	mutex_destroy(&hw->aq.asq_mutex);
15635 
15636 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15637 	rtnl_lock();
15638 	i40e_clear_interrupt_scheme(pf);
15639 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15640 		if (pf->vsi[i]) {
15641 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15642 				i40e_vsi_clear_rings(pf->vsi[i]);
15643 			i40e_vsi_clear(pf->vsi[i]);
15644 			pf->vsi[i] = NULL;
15645 		}
15646 	}
15647 	rtnl_unlock();
15648 
15649 	for (i = 0; i < I40E_MAX_VEB; i++) {
15650 		kfree(pf->veb[i]);
15651 		pf->veb[i] = NULL;
15652 	}
15653 
15654 	kfree(pf->qp_pile);
15655 	kfree(pf->vsi);
15656 
15657 	iounmap(hw->hw_addr);
15658 	kfree(pf);
15659 	pci_release_mem_regions(pdev);
15660 
15661 	pci_disable_pcie_error_reporting(pdev);
15662 	pci_disable_device(pdev);
15663 }
15664 
15665 /**
15666  * i40e_pci_error_detected - warning that something funky happened in PCI land
15667  * @pdev: PCI device information struct
15668  * @error: the type of PCI error
15669  *
15670  * Called to warn that something happened and the error handling steps
15671  * are in progress.  Allows the driver to quiesce things, be ready for
15672  * remediation.
15673  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)15674 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15675 						pci_channel_state_t error)
15676 {
15677 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15678 
15679 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15680 
15681 	if (!pf) {
15682 		dev_info(&pdev->dev,
15683 			 "Cannot recover - error happened during device probe\n");
15684 		return PCI_ERS_RESULT_DISCONNECT;
15685 	}
15686 
15687 	/* shutdown all operations */
15688 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15689 		i40e_prep_for_reset(pf, false);
15690 
15691 	/* Request a slot reset */
15692 	return PCI_ERS_RESULT_NEED_RESET;
15693 }
15694 
15695 /**
15696  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15697  * @pdev: PCI device information struct
15698  *
15699  * Called to find if the driver can work with the device now that
15700  * the pci slot has been reset.  If a basic connection seems good
15701  * (registers are readable and have sane content) then return a
15702  * happy little PCI_ERS_RESULT_xxx.
15703  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)15704 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15705 {
15706 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15707 	pci_ers_result_t result;
15708 	u32 reg;
15709 
15710 	dev_dbg(&pdev->dev, "%s\n", __func__);
15711 	if (pci_enable_device_mem(pdev)) {
15712 		dev_info(&pdev->dev,
15713 			 "Cannot re-enable PCI device after reset.\n");
15714 		result = PCI_ERS_RESULT_DISCONNECT;
15715 	} else {
15716 		pci_set_master(pdev);
15717 		pci_restore_state(pdev);
15718 		pci_save_state(pdev);
15719 		pci_wake_from_d3(pdev, false);
15720 
15721 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15722 		if (reg == 0)
15723 			result = PCI_ERS_RESULT_RECOVERED;
15724 		else
15725 			result = PCI_ERS_RESULT_DISCONNECT;
15726 	}
15727 
15728 	return result;
15729 }
15730 
15731 /**
15732  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15733  * @pdev: PCI device information struct
15734  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)15735 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15736 {
15737 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15738 
15739 	i40e_prep_for_reset(pf, false);
15740 }
15741 
15742 /**
15743  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15744  * @pdev: PCI device information struct
15745  */
i40e_pci_error_reset_done(struct pci_dev * pdev)15746 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15747 {
15748 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15749 
15750 	i40e_reset_and_rebuild(pf, false, false);
15751 }
15752 
15753 /**
15754  * i40e_pci_error_resume - restart operations after PCI error recovery
15755  * @pdev: PCI device information struct
15756  *
15757  * Called to allow the driver to bring things back up after PCI error
15758  * and/or reset recovery has finished.
15759  **/
i40e_pci_error_resume(struct pci_dev * pdev)15760 static void i40e_pci_error_resume(struct pci_dev *pdev)
15761 {
15762 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15763 
15764 	dev_dbg(&pdev->dev, "%s\n", __func__);
15765 	if (test_bit(__I40E_SUSPENDED, pf->state))
15766 		return;
15767 
15768 	i40e_handle_reset_warning(pf, false);
15769 }
15770 
15771 /**
15772  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15773  * using the mac_address_write admin q function
15774  * @pf: pointer to i40e_pf struct
15775  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)15776 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15777 {
15778 	struct i40e_hw *hw = &pf->hw;
15779 	i40e_status ret;
15780 	u8 mac_addr[6];
15781 	u16 flags = 0;
15782 
15783 	/* Get current MAC address in case it's an LAA */
15784 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15785 		ether_addr_copy(mac_addr,
15786 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15787 	} else {
15788 		dev_err(&pf->pdev->dev,
15789 			"Failed to retrieve MAC address; using default\n");
15790 		ether_addr_copy(mac_addr, hw->mac.addr);
15791 	}
15792 
15793 	/* The FW expects the mac address write cmd to first be called with
15794 	 * one of these flags before calling it again with the multicast
15795 	 * enable flags.
15796 	 */
15797 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15798 
15799 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15800 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15801 
15802 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15803 	if (ret) {
15804 		dev_err(&pf->pdev->dev,
15805 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15806 		return;
15807 	}
15808 
15809 	flags = I40E_AQC_MC_MAG_EN
15810 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15811 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15812 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15813 	if (ret)
15814 		dev_err(&pf->pdev->dev,
15815 			"Failed to enable Multicast Magic Packet wake up\n");
15816 }
15817 
15818 /**
15819  * i40e_shutdown - PCI callback for shutting down
15820  * @pdev: PCI device information struct
15821  **/
i40e_shutdown(struct pci_dev * pdev)15822 static void i40e_shutdown(struct pci_dev *pdev)
15823 {
15824 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15825 	struct i40e_hw *hw = &pf->hw;
15826 
15827 	set_bit(__I40E_SUSPENDED, pf->state);
15828 	set_bit(__I40E_DOWN, pf->state);
15829 
15830 	del_timer_sync(&pf->service_timer);
15831 	cancel_work_sync(&pf->service_task);
15832 	i40e_cloud_filter_exit(pf);
15833 	i40e_fdir_teardown(pf);
15834 
15835 	/* Client close must be called explicitly here because the timer
15836 	 * has been stopped.
15837 	 */
15838 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15839 
15840 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15841 		i40e_enable_mc_magic_wake(pf);
15842 
15843 	i40e_prep_for_reset(pf, false);
15844 
15845 	wr32(hw, I40E_PFPM_APM,
15846 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15847 	wr32(hw, I40E_PFPM_WUFC,
15848 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15849 
15850 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15851 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15852 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15853 		free_irq(pf->pdev->irq, pf);
15854 
15855 	/* Since we're going to destroy queues during the
15856 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15857 	 * whole section
15858 	 */
15859 	rtnl_lock();
15860 	i40e_clear_interrupt_scheme(pf);
15861 	rtnl_unlock();
15862 
15863 	if (system_state == SYSTEM_POWER_OFF) {
15864 		pci_wake_from_d3(pdev, pf->wol_en);
15865 		pci_set_power_state(pdev, PCI_D3hot);
15866 	}
15867 }
15868 
15869 /**
15870  * i40e_suspend - PM callback for moving to D3
15871  * @dev: generic device information structure
15872  **/
i40e_suspend(struct device * dev)15873 static int __maybe_unused i40e_suspend(struct device *dev)
15874 {
15875 	struct i40e_pf *pf = dev_get_drvdata(dev);
15876 	struct i40e_hw *hw = &pf->hw;
15877 
15878 	/* If we're already suspended, then there is nothing to do */
15879 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15880 		return 0;
15881 
15882 	set_bit(__I40E_DOWN, pf->state);
15883 
15884 	/* Ensure service task will not be running */
15885 	del_timer_sync(&pf->service_timer);
15886 	cancel_work_sync(&pf->service_task);
15887 
15888 	/* Client close must be called explicitly here because the timer
15889 	 * has been stopped.
15890 	 */
15891 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15892 
15893 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15894 		i40e_enable_mc_magic_wake(pf);
15895 
15896 	/* Since we're going to destroy queues during the
15897 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15898 	 * whole section
15899 	 */
15900 	rtnl_lock();
15901 
15902 	i40e_prep_for_reset(pf, true);
15903 
15904 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15905 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15906 
15907 	/* Clear the interrupt scheme and release our IRQs so that the system
15908 	 * can safely hibernate even when there are a large number of CPUs.
15909 	 * Otherwise hibernation might fail when mapping all the vectors back
15910 	 * to CPU0.
15911 	 */
15912 	i40e_clear_interrupt_scheme(pf);
15913 
15914 	rtnl_unlock();
15915 
15916 	return 0;
15917 }
15918 
15919 /**
15920  * i40e_resume - PM callback for waking up from D3
15921  * @dev: generic device information structure
15922  **/
i40e_resume(struct device * dev)15923 static int __maybe_unused i40e_resume(struct device *dev)
15924 {
15925 	struct i40e_pf *pf = dev_get_drvdata(dev);
15926 	int err;
15927 
15928 	/* If we're not suspended, then there is nothing to do */
15929 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15930 		return 0;
15931 
15932 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15933 	 * since we're going to be restoring queues
15934 	 */
15935 	rtnl_lock();
15936 
15937 	/* We cleared the interrupt scheme when we suspended, so we need to
15938 	 * restore it now to resume device functionality.
15939 	 */
15940 	err = i40e_restore_interrupt_scheme(pf);
15941 	if (err) {
15942 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
15943 			err);
15944 	}
15945 
15946 	clear_bit(__I40E_DOWN, pf->state);
15947 	i40e_reset_and_rebuild(pf, false, true);
15948 
15949 	rtnl_unlock();
15950 
15951 	/* Clear suspended state last after everything is recovered */
15952 	clear_bit(__I40E_SUSPENDED, pf->state);
15953 
15954 	/* Restart the service task */
15955 	mod_timer(&pf->service_timer,
15956 		  round_jiffies(jiffies + pf->service_timer_period));
15957 
15958 	return 0;
15959 }
15960 
15961 static const struct pci_error_handlers i40e_err_handler = {
15962 	.error_detected = i40e_pci_error_detected,
15963 	.slot_reset = i40e_pci_error_slot_reset,
15964 	.reset_prepare = i40e_pci_error_reset_prepare,
15965 	.reset_done = i40e_pci_error_reset_done,
15966 	.resume = i40e_pci_error_resume,
15967 };
15968 
15969 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15970 
15971 static struct pci_driver i40e_driver = {
15972 	.name     = i40e_driver_name,
15973 	.id_table = i40e_pci_tbl,
15974 	.probe    = i40e_probe,
15975 	.remove   = i40e_remove,
15976 	.driver   = {
15977 		.pm = &i40e_pm_ops,
15978 	},
15979 	.shutdown = i40e_shutdown,
15980 	.err_handler = &i40e_err_handler,
15981 	.sriov_configure = i40e_pci_sriov_configure,
15982 };
15983 
15984 /**
15985  * i40e_init_module - Driver registration routine
15986  *
15987  * i40e_init_module is the first routine called when the driver is
15988  * loaded. All it does is register with the PCI subsystem.
15989  **/
i40e_init_module(void)15990 static int __init i40e_init_module(void)
15991 {
15992 	int err;
15993 
15994 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
15995 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15996 
15997 	/* There is no need to throttle the number of active tasks because
15998 	 * each device limits its own task using a state bit for scheduling
15999 	 * the service task, and the device tasks do not interfere with each
16000 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16001 	 * since we need to be able to guarantee forward progress even under
16002 	 * memory pressure.
16003 	 */
16004 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16005 	if (!i40e_wq) {
16006 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16007 		return -ENOMEM;
16008 	}
16009 
16010 	i40e_dbg_init();
16011 	err = pci_register_driver(&i40e_driver);
16012 	if (err) {
16013 		destroy_workqueue(i40e_wq);
16014 		i40e_dbg_exit();
16015 		return err;
16016 	}
16017 
16018 	return 0;
16019 }
16020 module_init(i40e_init_module);
16021 
16022 /**
16023  * i40e_exit_module - Driver exit cleanup routine
16024  *
16025  * i40e_exit_module is called just before the driver is removed
16026  * from memory.
16027  **/
i40e_exit_module(void)16028 static void __exit i40e_exit_module(void)
16029 {
16030 	pci_unregister_driver(&i40e_driver);
16031 	destroy_workqueue(i40e_wq);
16032 	i40e_dbg_exit();
16033 }
16034 module_exit(i40e_exit_module);
16035