• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 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 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
88 	/* required last entry */
89 	{0, }
90 };
91 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
92 
93 #define I40E_MAX_VF_COUNT 128
94 static int debug = -1;
95 module_param(debug, uint, 0);
96 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
97 
98 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
99 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
100 MODULE_LICENSE("GPL v2");
101 
102 static struct workqueue_struct *i40e_wq;
103 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)104 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
105 				  struct net_device *netdev, int delta)
106 {
107 	struct netdev_hw_addr_list *ha_list;
108 	struct netdev_hw_addr *ha;
109 
110 	if (!f || !netdev)
111 		return;
112 
113 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
114 		ha_list = &netdev->uc;
115 	else
116 		ha_list = &netdev->mc;
117 
118 	netdev_hw_addr_list_for_each(ha, ha_list) {
119 		if (ether_addr_equal(ha->addr, f->macaddr)) {
120 			ha->refcount += delta;
121 			if (ha->refcount <= 0)
122 				ha->refcount = 1;
123 			break;
124 		}
125 	}
126 }
127 
128 /**
129  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
130  * @hw:   pointer to the HW structure
131  * @mem:  ptr to mem struct to fill out
132  * @size: size of memory requested
133  * @alignment: what to align the allocation to
134  **/
i40e_allocate_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)135 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
136 			    u64 size, u32 alignment)
137 {
138 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
139 
140 	mem->size = ALIGN(size, alignment);
141 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
142 				     GFP_KERNEL);
143 	if (!mem->va)
144 		return -ENOMEM;
145 
146 	return 0;
147 }
148 
149 /**
150  * i40e_free_dma_mem_d - OS specific memory free for shared code
151  * @hw:   pointer to the HW structure
152  * @mem:  ptr to mem struct to free
153  **/
i40e_free_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem)154 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
155 {
156 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
157 
158 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
159 	mem->va = NULL;
160 	mem->pa = 0;
161 	mem->size = 0;
162 
163 	return 0;
164 }
165 
166 /**
167  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
168  * @hw:   pointer to the HW structure
169  * @mem:  ptr to mem struct to fill out
170  * @size: size of memory requested
171  **/
i40e_allocate_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)172 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
173 			     u32 size)
174 {
175 	mem->size = size;
176 	mem->va = kzalloc(size, GFP_KERNEL);
177 
178 	if (!mem->va)
179 		return -ENOMEM;
180 
181 	return 0;
182 }
183 
184 /**
185  * i40e_free_virt_mem_d - OS specific memory free for shared code
186  * @hw:   pointer to the HW structure
187  * @mem:  ptr to mem struct to free
188  **/
i40e_free_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem)189 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
190 {
191 	/* it's ok to kfree a NULL pointer */
192 	kfree(mem->va);
193 	mem->va = NULL;
194 	mem->size = 0;
195 
196 	return 0;
197 }
198 
199 /**
200  * i40e_get_lump - find a lump of free generic resource
201  * @pf: board private structure
202  * @pile: the pile of resource to search
203  * @needed: the number of items needed
204  * @id: an owner id to stick on the items assigned
205  *
206  * Returns the base item index of the lump, or negative for error
207  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)208 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
209 			 u16 needed, u16 id)
210 {
211 	int ret = -ENOMEM;
212 	int i, j;
213 
214 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
215 		dev_info(&pf->pdev->dev,
216 			 "param err: pile=%s needed=%d id=0x%04x\n",
217 			 pile ? "<valid>" : "<null>", needed, id);
218 		return -EINVAL;
219 	}
220 
221 	/* Allocate last queue in the pile for FDIR VSI queue
222 	 * so it doesn't fragment the qp_pile
223 	 */
224 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
225 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
226 			dev_err(&pf->pdev->dev,
227 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
228 				pile->num_entries - 1);
229 			return -ENOMEM;
230 		}
231 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
232 		return pile->num_entries - 1;
233 	}
234 
235 	i = 0;
236 	while (i < pile->num_entries) {
237 		/* skip already allocated entries */
238 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
239 			i++;
240 			continue;
241 		}
242 
243 		/* do we have enough in this lump? */
244 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
245 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
246 				break;
247 		}
248 
249 		if (j == needed) {
250 			/* there was enough, so assign it to the requestor */
251 			for (j = 0; j < needed; j++)
252 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
253 			ret = i;
254 			break;
255 		}
256 
257 		/* not enough, so skip over it and continue looking */
258 		i += j;
259 	}
260 
261 	return ret;
262 }
263 
264 /**
265  * i40e_put_lump - return a lump of generic resource
266  * @pile: the pile of resource to search
267  * @index: the base item index
268  * @id: the owner id of the items assigned
269  *
270  * Returns the count of items in the lump
271  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)272 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
273 {
274 	int valid_id = (id | I40E_PILE_VALID_BIT);
275 	int count = 0;
276 	u16 i;
277 
278 	if (!pile || index >= pile->num_entries)
279 		return -EINVAL;
280 
281 	for (i = index;
282 	     i < pile->num_entries && pile->list[i] == valid_id;
283 	     i++) {
284 		pile->list[i] = 0;
285 		count++;
286 	}
287 
288 
289 	return count;
290 }
291 
292 /**
293  * i40e_find_vsi_from_id - searches for the vsi with the given id
294  * @pf: the pf structure to search for the vsi
295  * @id: id of the vsi it is searching for
296  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)297 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
298 {
299 	int i;
300 
301 	for (i = 0; i < pf->num_alloc_vsi; i++)
302 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
303 			return pf->vsi[i];
304 
305 	return NULL;
306 }
307 
308 /**
309  * i40e_service_event_schedule - Schedule the service task to wake up
310  * @pf: board private structure
311  *
312  * If not already scheduled, this puts the task into the work queue
313  **/
i40e_service_event_schedule(struct i40e_pf * pf)314 void i40e_service_event_schedule(struct i40e_pf *pf)
315 {
316 	if ((!test_bit(__I40E_DOWN, pf->state) &&
317 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
318 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
319 		queue_work(i40e_wq, &pf->service_task);
320 }
321 
322 /**
323  * i40e_tx_timeout - Respond to a Tx Hang
324  * @netdev: network interface device structure
325  * @txqueue: queue number timing out
326  *
327  * If any port has noticed a Tx timeout, it is likely that the whole
328  * device is munged, not just the one netdev port, so go for the full
329  * reset.
330  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)331 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
332 {
333 	struct i40e_netdev_priv *np = netdev_priv(netdev);
334 	struct i40e_vsi *vsi = np->vsi;
335 	struct i40e_pf *pf = vsi->back;
336 	struct i40e_ring *tx_ring = NULL;
337 	unsigned int i;
338 	u32 head, val;
339 
340 	pf->tx_timeout_count++;
341 
342 	/* with txqueue index, find the tx_ring struct */
343 	for (i = 0; i < vsi->num_queue_pairs; i++) {
344 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
345 			if (txqueue ==
346 			    vsi->tx_rings[i]->queue_index) {
347 				tx_ring = vsi->tx_rings[i];
348 				break;
349 			}
350 		}
351 	}
352 
353 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
354 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
355 	else if (time_before(jiffies,
356 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 		return;   /* don't do any new action before the next timeout */
358 
359 	/* don't kick off another recovery if one is already pending */
360 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
361 		return;
362 
363 	if (tx_ring) {
364 		head = i40e_get_head(tx_ring);
365 		/* Read interrupt register */
366 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
367 			val = rd32(&pf->hw,
368 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
369 						tx_ring->vsi->base_vector - 1));
370 		else
371 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
372 
373 		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",
374 			    vsi->seid, txqueue, tx_ring->next_to_clean,
375 			    head, tx_ring->next_to_use,
376 			    readl(tx_ring->tail), val);
377 	}
378 
379 	pf->tx_timeout_last_recovery = jiffies;
380 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
381 		    pf->tx_timeout_recovery_level, txqueue);
382 
383 	switch (pf->tx_timeout_recovery_level) {
384 	case 1:
385 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
386 		break;
387 	case 2:
388 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
389 		break;
390 	case 3:
391 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
392 		break;
393 	default:
394 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
395 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
396 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
397 		break;
398 	}
399 
400 	i40e_service_event_schedule(pf);
401 	pf->tx_timeout_recovery_level++;
402 }
403 
404 /**
405  * i40e_get_vsi_stats_struct - Get System Network Statistics
406  * @vsi: the VSI we care about
407  *
408  * Returns the address of the device statistics structure.
409  * The statistics are actually updated from the service task.
410  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)411 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
412 {
413 	return &vsi->net_stats;
414 }
415 
416 /**
417  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
418  * @ring: Tx ring to get statistics from
419  * @stats: statistics entry to be updated
420  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)421 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
422 					    struct rtnl_link_stats64 *stats)
423 {
424 	u64 bytes, packets;
425 	unsigned int start;
426 
427 	do {
428 		start = u64_stats_fetch_begin_irq(&ring->syncp);
429 		packets = ring->stats.packets;
430 		bytes   = ring->stats.bytes;
431 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
432 
433 	stats->tx_packets += packets;
434 	stats->tx_bytes   += bytes;
435 }
436 
437 /**
438  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
439  * @netdev: network interface device structure
440  * @stats: data structure to store statistics
441  *
442  * Returns the address of the device statistics structure.
443  * The statistics are actually updated from the service task.
444  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)445 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
446 				  struct rtnl_link_stats64 *stats)
447 {
448 	struct i40e_netdev_priv *np = netdev_priv(netdev);
449 	struct i40e_vsi *vsi = np->vsi;
450 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
451 	struct i40e_ring *ring;
452 	int i;
453 
454 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
455 		return;
456 
457 	if (!vsi->tx_rings)
458 		return;
459 
460 	rcu_read_lock();
461 	for (i = 0; i < vsi->num_queue_pairs; i++) {
462 		u64 bytes, packets;
463 		unsigned int start;
464 
465 		ring = READ_ONCE(vsi->tx_rings[i]);
466 		if (!ring)
467 			continue;
468 		i40e_get_netdev_stats_struct_tx(ring, stats);
469 
470 		if (i40e_enabled_xdp_vsi(vsi)) {
471 			ring = READ_ONCE(vsi->xdp_rings[i]);
472 			if (!ring)
473 				continue;
474 			i40e_get_netdev_stats_struct_tx(ring, stats);
475 		}
476 
477 		ring = READ_ONCE(vsi->rx_rings[i]);
478 		if (!ring)
479 			continue;
480 		do {
481 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
482 			packets = ring->stats.packets;
483 			bytes   = ring->stats.bytes;
484 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
485 
486 		stats->rx_packets += packets;
487 		stats->rx_bytes   += bytes;
488 
489 	}
490 	rcu_read_unlock();
491 
492 	/* following stats updated by i40e_watchdog_subtask() */
493 	stats->multicast	= vsi_stats->multicast;
494 	stats->tx_errors	= vsi_stats->tx_errors;
495 	stats->tx_dropped	= vsi_stats->tx_dropped;
496 	stats->rx_errors	= vsi_stats->rx_errors;
497 	stats->rx_dropped	= vsi_stats->rx_dropped;
498 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
499 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
500 }
501 
502 /**
503  * i40e_vsi_reset_stats - Resets all stats of the given vsi
504  * @vsi: the VSI to have its stats reset
505  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)506 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
507 {
508 	struct rtnl_link_stats64 *ns;
509 	int i;
510 
511 	if (!vsi)
512 		return;
513 
514 	ns = i40e_get_vsi_stats_struct(vsi);
515 	memset(ns, 0, sizeof(*ns));
516 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
517 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
518 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
519 	if (vsi->rx_rings && vsi->rx_rings[0]) {
520 		for (i = 0; i < vsi->num_queue_pairs; i++) {
521 			memset(&vsi->rx_rings[i]->stats, 0,
522 			       sizeof(vsi->rx_rings[i]->stats));
523 			memset(&vsi->rx_rings[i]->rx_stats, 0,
524 			       sizeof(vsi->rx_rings[i]->rx_stats));
525 			memset(&vsi->tx_rings[i]->stats, 0,
526 			       sizeof(vsi->tx_rings[i]->stats));
527 			memset(&vsi->tx_rings[i]->tx_stats, 0,
528 			       sizeof(vsi->tx_rings[i]->tx_stats));
529 		}
530 	}
531 	vsi->stat_offsets_loaded = false;
532 }
533 
534 /**
535  * i40e_pf_reset_stats - Reset all of the stats for the given PF
536  * @pf: the PF to be reset
537  **/
i40e_pf_reset_stats(struct i40e_pf * pf)538 void i40e_pf_reset_stats(struct i40e_pf *pf)
539 {
540 	int i;
541 
542 	memset(&pf->stats, 0, sizeof(pf->stats));
543 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
544 	pf->stat_offsets_loaded = false;
545 
546 	for (i = 0; i < I40E_MAX_VEB; i++) {
547 		if (pf->veb[i]) {
548 			memset(&pf->veb[i]->stats, 0,
549 			       sizeof(pf->veb[i]->stats));
550 			memset(&pf->veb[i]->stats_offsets, 0,
551 			       sizeof(pf->veb[i]->stats_offsets));
552 			memset(&pf->veb[i]->tc_stats, 0,
553 			       sizeof(pf->veb[i]->tc_stats));
554 			memset(&pf->veb[i]->tc_stats_offsets, 0,
555 			       sizeof(pf->veb[i]->tc_stats_offsets));
556 			pf->veb[i]->stat_offsets_loaded = false;
557 		}
558 	}
559 	pf->hw_csum_rx_error = 0;
560 }
561 
562 /**
563  * i40e_compute_pci_to_hw_id - compute index form PCI function.
564  * @vsi: ptr to the VSI to read from.
565  * @hw: ptr to the hardware info.
566  **/
i40e_compute_pci_to_hw_id(struct i40e_vsi * vsi,struct i40e_hw * hw)567 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
568 {
569 	int pf_count = i40e_get_pf_count(hw);
570 
571 	if (vsi->type == I40E_VSI_SRIOV)
572 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
573 
574 	return hw->port + BIT(7);
575 }
576 
577 /**
578  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
579  * @hw: ptr to the hardware info.
580  * @hireg: the high 32 bit reg to read.
581  * @loreg: the low 32 bit reg to read.
582  * @offset_loaded: has the initial offset been loaded yet.
583  * @offset: ptr to current offset value.
584  * @stat: ptr to the stat.
585  *
586  * Since the device stats are not reset at PFReset, they will not
587  * be zeroed when the driver starts.  We'll save the first values read
588  * and use them as offsets to be subtracted from the raw values in order
589  * to report stats that count from zero.
590  **/
i40e_stat_update64(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)591 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
592 			       bool offset_loaded, u64 *offset, u64 *stat)
593 {
594 	u64 new_data;
595 
596 	new_data = rd64(hw, loreg);
597 
598 	if (!offset_loaded || new_data < *offset)
599 		*offset = new_data;
600 	*stat = new_data - *offset;
601 }
602 
603 /**
604  * i40e_stat_update48 - read and update a 48 bit stat from the chip
605  * @hw: ptr to the hardware info
606  * @hireg: the high 32 bit reg to read
607  * @loreg: the low 32 bit reg to read
608  * @offset_loaded: has the initial offset been loaded yet
609  * @offset: ptr to current offset value
610  * @stat: ptr to the stat
611  *
612  * Since the device stats are not reset at PFReset, they likely will not
613  * be zeroed when the driver starts.  We'll save the first values read
614  * and use them as offsets to be subtracted from the raw values in order
615  * to report stats that count from zero.  In the process, we also manage
616  * the potential roll-over.
617  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)618 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
619 			       bool offset_loaded, u64 *offset, u64 *stat)
620 {
621 	u64 new_data;
622 
623 	if (hw->device_id == I40E_DEV_ID_QEMU) {
624 		new_data = rd32(hw, loreg);
625 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
626 	} else {
627 		new_data = rd64(hw, loreg);
628 	}
629 	if (!offset_loaded)
630 		*offset = new_data;
631 	if (likely(new_data >= *offset))
632 		*stat = new_data - *offset;
633 	else
634 		*stat = (new_data + BIT_ULL(48)) - *offset;
635 	*stat &= 0xFFFFFFFFFFFFULL;
636 }
637 
638 /**
639  * i40e_stat_update32 - read and update a 32 bit stat from the chip
640  * @hw: ptr to the hardware info
641  * @reg: the hw reg to read
642  * @offset_loaded: has the initial offset been loaded yet
643  * @offset: ptr to current offset value
644  * @stat: ptr to the stat
645  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)646 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
647 			       bool offset_loaded, u64 *offset, u64 *stat)
648 {
649 	u32 new_data;
650 
651 	new_data = rd32(hw, reg);
652 	if (!offset_loaded)
653 		*offset = new_data;
654 	if (likely(new_data >= *offset))
655 		*stat = (u32)(new_data - *offset);
656 	else
657 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
658 }
659 
660 /**
661  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
662  * @hw: ptr to the hardware info
663  * @reg: the hw reg to read and clear
664  * @stat: ptr to the stat
665  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)666 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
667 {
668 	u32 new_data = rd32(hw, reg);
669 
670 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
671 	*stat += new_data;
672 }
673 
674 /**
675  * i40e_stats_update_rx_discards - update rx_discards.
676  * @vsi: ptr to the VSI to be updated.
677  * @hw: ptr to the hardware info.
678  * @stat_idx: VSI's stat_counter_idx.
679  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
680  * @stat_offset: ptr to stat_offset to store first read of specific register.
681  * @stat: ptr to VSI's stat to be updated.
682  **/
683 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)684 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
685 			      int stat_idx, bool offset_loaded,
686 			      struct i40e_eth_stats *stat_offset,
687 			      struct i40e_eth_stats *stat)
688 {
689 	u64 rx_rdpc, rx_rxerr;
690 
691 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
692 			   &stat_offset->rx_discards, &rx_rdpc);
693 	i40e_stat_update64(hw,
694 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
695 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
696 			   offset_loaded, &stat_offset->rx_discards_other,
697 			   &rx_rxerr);
698 
699 	stat->rx_discards = rx_rdpc + rx_rxerr;
700 }
701 
702 /**
703  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
704  * @vsi: the VSI to be updated
705  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)706 void i40e_update_eth_stats(struct i40e_vsi *vsi)
707 {
708 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
709 	struct i40e_pf *pf = vsi->back;
710 	struct i40e_hw *hw = &pf->hw;
711 	struct i40e_eth_stats *oes;
712 	struct i40e_eth_stats *es;     /* device's eth stats */
713 
714 	es = &vsi->eth_stats;
715 	oes = &vsi->eth_stats_offsets;
716 
717 	/* Gather up the stats that the hw collects */
718 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
719 			   vsi->stat_offsets_loaded,
720 			   &oes->tx_errors, &es->tx_errors);
721 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
722 			   vsi->stat_offsets_loaded,
723 			   &oes->rx_discards, &es->rx_discards);
724 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
725 			   vsi->stat_offsets_loaded,
726 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
727 
728 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
729 			   I40E_GLV_GORCL(stat_idx),
730 			   vsi->stat_offsets_loaded,
731 			   &oes->rx_bytes, &es->rx_bytes);
732 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
733 			   I40E_GLV_UPRCL(stat_idx),
734 			   vsi->stat_offsets_loaded,
735 			   &oes->rx_unicast, &es->rx_unicast);
736 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
737 			   I40E_GLV_MPRCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_multicast, &es->rx_multicast);
740 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
741 			   I40E_GLV_BPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_broadcast, &es->rx_broadcast);
744 
745 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
746 			   I40E_GLV_GOTCL(stat_idx),
747 			   vsi->stat_offsets_loaded,
748 			   &oes->tx_bytes, &es->tx_bytes);
749 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
750 			   I40E_GLV_UPTCL(stat_idx),
751 			   vsi->stat_offsets_loaded,
752 			   &oes->tx_unicast, &es->tx_unicast);
753 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
754 			   I40E_GLV_MPTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_multicast, &es->tx_multicast);
757 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
758 			   I40E_GLV_BPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_broadcast, &es->tx_broadcast);
761 
762 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
763 				      vsi->stat_offsets_loaded, oes, es);
764 
765 	vsi->stat_offsets_loaded = true;
766 }
767 
768 /**
769  * i40e_update_veb_stats - Update Switch component statistics
770  * @veb: the VEB being updated
771  **/
i40e_update_veb_stats(struct i40e_veb * veb)772 void i40e_update_veb_stats(struct i40e_veb *veb)
773 {
774 	struct i40e_pf *pf = veb->pf;
775 	struct i40e_hw *hw = &pf->hw;
776 	struct i40e_eth_stats *oes;
777 	struct i40e_eth_stats *es;     /* device's eth stats */
778 	struct i40e_veb_tc_stats *veb_oes;
779 	struct i40e_veb_tc_stats *veb_es;
780 	int i, idx = 0;
781 
782 	idx = veb->stats_idx;
783 	es = &veb->stats;
784 	oes = &veb->stats_offsets;
785 	veb_es = &veb->tc_stats;
786 	veb_oes = &veb->tc_stats_offsets;
787 
788 	/* Gather up the stats that the hw collects */
789 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
790 			   veb->stat_offsets_loaded,
791 			   &oes->tx_discards, &es->tx_discards);
792 	if (hw->revision_id > 0)
793 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
794 				   veb->stat_offsets_loaded,
795 				   &oes->rx_unknown_protocol,
796 				   &es->rx_unknown_protocol);
797 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_bytes, &es->rx_bytes);
800 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
801 			   veb->stat_offsets_loaded,
802 			   &oes->rx_unicast, &es->rx_unicast);
803 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
804 			   veb->stat_offsets_loaded,
805 			   &oes->rx_multicast, &es->rx_multicast);
806 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
807 			   veb->stat_offsets_loaded,
808 			   &oes->rx_broadcast, &es->rx_broadcast);
809 
810 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_bytes, &es->tx_bytes);
813 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->tx_unicast, &es->tx_unicast);
816 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
817 			   veb->stat_offsets_loaded,
818 			   &oes->tx_multicast, &es->tx_multicast);
819 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
820 			   veb->stat_offsets_loaded,
821 			   &oes->tx_broadcast, &es->tx_broadcast);
822 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
823 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
824 				   I40E_GLVEBTC_RPCL(i, idx),
825 				   veb->stat_offsets_loaded,
826 				   &veb_oes->tc_rx_packets[i],
827 				   &veb_es->tc_rx_packets[i]);
828 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
829 				   I40E_GLVEBTC_RBCL(i, idx),
830 				   veb->stat_offsets_loaded,
831 				   &veb_oes->tc_rx_bytes[i],
832 				   &veb_es->tc_rx_bytes[i]);
833 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
834 				   I40E_GLVEBTC_TPCL(i, idx),
835 				   veb->stat_offsets_loaded,
836 				   &veb_oes->tc_tx_packets[i],
837 				   &veb_es->tc_tx_packets[i]);
838 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
839 				   I40E_GLVEBTC_TBCL(i, idx),
840 				   veb->stat_offsets_loaded,
841 				   &veb_oes->tc_tx_bytes[i],
842 				   &veb_es->tc_tx_bytes[i]);
843 	}
844 	veb->stat_offsets_loaded = true;
845 }
846 
847 /**
848  * i40e_update_vsi_stats - Update the vsi statistics counters.
849  * @vsi: the VSI to be updated
850  *
851  * There are a few instances where we store the same stat in a
852  * couple of different structs.  This is partly because we have
853  * the netdev stats that need to be filled out, which is slightly
854  * different from the "eth_stats" defined by the chip and used in
855  * VF communications.  We sort it out here.
856  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)857 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
858 {
859 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
860 	struct i40e_pf *pf = vsi->back;
861 	struct rtnl_link_stats64 *ons;
862 	struct rtnl_link_stats64 *ns;   /* netdev stats */
863 	struct i40e_eth_stats *oes;
864 	struct i40e_eth_stats *es;     /* device's eth stats */
865 	u64 tx_restart, tx_busy;
866 	struct i40e_ring *p;
867 	u64 bytes, packets;
868 	unsigned int start;
869 	u64 tx_linearize;
870 	u64 tx_force_wb;
871 	u64 tx_stopped;
872 	u64 rx_p, rx_b;
873 	u64 tx_p, tx_b;
874 	u16 q;
875 
876 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
877 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
878 		return;
879 
880 	ns = i40e_get_vsi_stats_struct(vsi);
881 	ons = &vsi->net_stats_offsets;
882 	es = &vsi->eth_stats;
883 	oes = &vsi->eth_stats_offsets;
884 
885 	/* Gather up the netdev and vsi stats that the driver collects
886 	 * on the fly during packet processing
887 	 */
888 	rx_b = rx_p = 0;
889 	tx_b = tx_p = 0;
890 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
891 	tx_stopped = 0;
892 	rx_page = 0;
893 	rx_buf = 0;
894 	rx_reuse = 0;
895 	rx_alloc = 0;
896 	rx_waive = 0;
897 	rx_busy = 0;
898 	rcu_read_lock();
899 	for (q = 0; q < vsi->num_queue_pairs; q++) {
900 		/* locate Tx ring */
901 		p = READ_ONCE(vsi->tx_rings[q]);
902 		if (!p)
903 			continue;
904 
905 		do {
906 			start = u64_stats_fetch_begin_irq(&p->syncp);
907 			packets = p->stats.packets;
908 			bytes = p->stats.bytes;
909 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
910 		tx_b += bytes;
911 		tx_p += packets;
912 		tx_restart += p->tx_stats.restart_queue;
913 		tx_busy += p->tx_stats.tx_busy;
914 		tx_linearize += p->tx_stats.tx_linearize;
915 		tx_force_wb += p->tx_stats.tx_force_wb;
916 		tx_stopped += p->tx_stats.tx_stopped;
917 
918 		/* locate Rx ring */
919 		p = READ_ONCE(vsi->rx_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 		rx_b += bytes;
929 		rx_p += packets;
930 		rx_buf += p->rx_stats.alloc_buff_failed;
931 		rx_page += p->rx_stats.alloc_page_failed;
932 		rx_reuse += p->rx_stats.page_reuse_count;
933 		rx_alloc += p->rx_stats.page_alloc_count;
934 		rx_waive += p->rx_stats.page_waive_count;
935 		rx_busy += p->rx_stats.page_busy_count;
936 
937 		if (i40e_enabled_xdp_vsi(vsi)) {
938 			/* locate XDP ring */
939 			p = READ_ONCE(vsi->xdp_rings[q]);
940 			if (!p)
941 				continue;
942 
943 			do {
944 				start = u64_stats_fetch_begin_irq(&p->syncp);
945 				packets = p->stats.packets;
946 				bytes = p->stats.bytes;
947 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
948 			tx_b += bytes;
949 			tx_p += packets;
950 			tx_restart += p->tx_stats.restart_queue;
951 			tx_busy += p->tx_stats.tx_busy;
952 			tx_linearize += p->tx_stats.tx_linearize;
953 			tx_force_wb += p->tx_stats.tx_force_wb;
954 		}
955 	}
956 	rcu_read_unlock();
957 	vsi->tx_restart = tx_restart;
958 	vsi->tx_busy = tx_busy;
959 	vsi->tx_linearize = tx_linearize;
960 	vsi->tx_force_wb = tx_force_wb;
961 	vsi->tx_stopped = tx_stopped;
962 	vsi->rx_page_failed = rx_page;
963 	vsi->rx_buf_failed = rx_buf;
964 	vsi->rx_page_reuse = rx_reuse;
965 	vsi->rx_page_alloc = rx_alloc;
966 	vsi->rx_page_waive = rx_waive;
967 	vsi->rx_page_busy = rx_busy;
968 
969 	ns->rx_packets = rx_p;
970 	ns->rx_bytes = rx_b;
971 	ns->tx_packets = tx_p;
972 	ns->tx_bytes = tx_b;
973 
974 	/* update netdev stats from eth stats */
975 	i40e_update_eth_stats(vsi);
976 	ons->tx_errors = oes->tx_errors;
977 	ns->tx_errors = es->tx_errors;
978 	ons->multicast = oes->rx_multicast;
979 	ns->multicast = es->rx_multicast;
980 	ons->rx_dropped = oes->rx_discards;
981 	ns->rx_dropped = es->rx_discards;
982 	ons->tx_dropped = oes->tx_discards;
983 	ns->tx_dropped = es->tx_discards;
984 
985 	/* pull in a couple PF stats if this is the main vsi */
986 	if (vsi == pf->vsi[pf->lan_vsi]) {
987 		ns->rx_crc_errors = pf->stats.crc_errors;
988 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
989 		ns->rx_length_errors = pf->stats.rx_length_errors;
990 	}
991 }
992 
993 /**
994  * i40e_update_pf_stats - Update the PF statistics counters.
995  * @pf: the PF to be updated
996  **/
i40e_update_pf_stats(struct i40e_pf * pf)997 static void i40e_update_pf_stats(struct i40e_pf *pf)
998 {
999 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1000 	struct i40e_hw_port_stats *nsd = &pf->stats;
1001 	struct i40e_hw *hw = &pf->hw;
1002 	u32 val;
1003 	int i;
1004 
1005 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1006 			   I40E_GLPRT_GORCL(hw->port),
1007 			   pf->stat_offsets_loaded,
1008 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1009 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1010 			   I40E_GLPRT_GOTCL(hw->port),
1011 			   pf->stat_offsets_loaded,
1012 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1013 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1014 			   pf->stat_offsets_loaded,
1015 			   &osd->eth.rx_discards,
1016 			   &nsd->eth.rx_discards);
1017 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1018 			   I40E_GLPRT_UPRCL(hw->port),
1019 			   pf->stat_offsets_loaded,
1020 			   &osd->eth.rx_unicast,
1021 			   &nsd->eth.rx_unicast);
1022 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1023 			   I40E_GLPRT_MPRCL(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->eth.rx_multicast,
1026 			   &nsd->eth.rx_multicast);
1027 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1028 			   I40E_GLPRT_BPRCL(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->eth.rx_broadcast,
1031 			   &nsd->eth.rx_broadcast);
1032 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1033 			   I40E_GLPRT_UPTCL(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->eth.tx_unicast,
1036 			   &nsd->eth.tx_unicast);
1037 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1038 			   I40E_GLPRT_MPTCL(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->eth.tx_multicast,
1041 			   &nsd->eth.tx_multicast);
1042 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1043 			   I40E_GLPRT_BPTCL(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->eth.tx_broadcast,
1046 			   &nsd->eth.tx_broadcast);
1047 
1048 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->tx_dropped_link_down,
1051 			   &nsd->tx_dropped_link_down);
1052 
1053 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->crc_errors, &nsd->crc_errors);
1056 
1057 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1058 			   pf->stat_offsets_loaded,
1059 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1060 
1061 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->mac_local_faults,
1064 			   &nsd->mac_local_faults);
1065 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1066 			   pf->stat_offsets_loaded,
1067 			   &osd->mac_remote_faults,
1068 			   &nsd->mac_remote_faults);
1069 
1070 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1071 			   pf->stat_offsets_loaded,
1072 			   &osd->rx_length_errors,
1073 			   &nsd->rx_length_errors);
1074 
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1078 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1081 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1082 			   pf->stat_offsets_loaded,
1083 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1084 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1087 
1088 	for (i = 0; i < 8; i++) {
1089 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1090 				   pf->stat_offsets_loaded,
1091 				   &osd->priority_xoff_rx[i],
1092 				   &nsd->priority_xoff_rx[i]);
1093 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1094 				   pf->stat_offsets_loaded,
1095 				   &osd->priority_xon_rx[i],
1096 				   &nsd->priority_xon_rx[i]);
1097 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1098 				   pf->stat_offsets_loaded,
1099 				   &osd->priority_xon_tx[i],
1100 				   &nsd->priority_xon_tx[i]);
1101 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1102 				   pf->stat_offsets_loaded,
1103 				   &osd->priority_xoff_tx[i],
1104 				   &nsd->priority_xoff_tx[i]);
1105 		i40e_stat_update32(hw,
1106 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1107 				   pf->stat_offsets_loaded,
1108 				   &osd->priority_xon_2_xoff[i],
1109 				   &nsd->priority_xon_2_xoff[i]);
1110 	}
1111 
1112 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1113 			   I40E_GLPRT_PRC64L(hw->port),
1114 			   pf->stat_offsets_loaded,
1115 			   &osd->rx_size_64, &nsd->rx_size_64);
1116 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1117 			   I40E_GLPRT_PRC127L(hw->port),
1118 			   pf->stat_offsets_loaded,
1119 			   &osd->rx_size_127, &nsd->rx_size_127);
1120 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1121 			   I40E_GLPRT_PRC255L(hw->port),
1122 			   pf->stat_offsets_loaded,
1123 			   &osd->rx_size_255, &nsd->rx_size_255);
1124 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1125 			   I40E_GLPRT_PRC511L(hw->port),
1126 			   pf->stat_offsets_loaded,
1127 			   &osd->rx_size_511, &nsd->rx_size_511);
1128 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1129 			   I40E_GLPRT_PRC1023L(hw->port),
1130 			   pf->stat_offsets_loaded,
1131 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1132 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1133 			   I40E_GLPRT_PRC1522L(hw->port),
1134 			   pf->stat_offsets_loaded,
1135 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1136 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1137 			   I40E_GLPRT_PRC9522L(hw->port),
1138 			   pf->stat_offsets_loaded,
1139 			   &osd->rx_size_big, &nsd->rx_size_big);
1140 
1141 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1142 			   I40E_GLPRT_PTC64L(hw->port),
1143 			   pf->stat_offsets_loaded,
1144 			   &osd->tx_size_64, &nsd->tx_size_64);
1145 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1146 			   I40E_GLPRT_PTC127L(hw->port),
1147 			   pf->stat_offsets_loaded,
1148 			   &osd->tx_size_127, &nsd->tx_size_127);
1149 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1150 			   I40E_GLPRT_PTC255L(hw->port),
1151 			   pf->stat_offsets_loaded,
1152 			   &osd->tx_size_255, &nsd->tx_size_255);
1153 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1154 			   I40E_GLPRT_PTC511L(hw->port),
1155 			   pf->stat_offsets_loaded,
1156 			   &osd->tx_size_511, &nsd->tx_size_511);
1157 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1158 			   I40E_GLPRT_PTC1023L(hw->port),
1159 			   pf->stat_offsets_loaded,
1160 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1161 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1162 			   I40E_GLPRT_PTC1522L(hw->port),
1163 			   pf->stat_offsets_loaded,
1164 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1165 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1166 			   I40E_GLPRT_PTC9522L(hw->port),
1167 			   pf->stat_offsets_loaded,
1168 			   &osd->tx_size_big, &nsd->tx_size_big);
1169 
1170 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_undersize, &nsd->rx_undersize);
1173 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->rx_fragments, &nsd->rx_fragments);
1176 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->rx_oversize, &nsd->rx_oversize);
1179 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1180 			   pf->stat_offsets_loaded,
1181 			   &osd->rx_jabber, &nsd->rx_jabber);
1182 
1183 	/* FDIR stats */
1184 	i40e_stat_update_and_clear32(hw,
1185 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1186 			&nsd->fd_atr_match);
1187 	i40e_stat_update_and_clear32(hw,
1188 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1189 			&nsd->fd_sb_match);
1190 	i40e_stat_update_and_clear32(hw,
1191 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1192 			&nsd->fd_atr_tunnel_match);
1193 
1194 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1195 	nsd->tx_lpi_status =
1196 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1197 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1198 	nsd->rx_lpi_status =
1199 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1200 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1201 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1202 			   pf->stat_offsets_loaded,
1203 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1204 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1205 			   pf->stat_offsets_loaded,
1206 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1207 
1208 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1209 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1210 		nsd->fd_sb_status = true;
1211 	else
1212 		nsd->fd_sb_status = false;
1213 
1214 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1215 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1216 		nsd->fd_atr_status = true;
1217 	else
1218 		nsd->fd_atr_status = false;
1219 
1220 	pf->stat_offsets_loaded = true;
1221 }
1222 
1223 /**
1224  * i40e_update_stats - Update the various statistics counters.
1225  * @vsi: the VSI to be updated
1226  *
1227  * Update the various stats for this VSI and its related entities.
1228  **/
i40e_update_stats(struct i40e_vsi * vsi)1229 void i40e_update_stats(struct i40e_vsi *vsi)
1230 {
1231 	struct i40e_pf *pf = vsi->back;
1232 
1233 	if (vsi == pf->vsi[pf->lan_vsi])
1234 		i40e_update_pf_stats(pf);
1235 
1236 	i40e_update_vsi_stats(vsi);
1237 }
1238 
1239 /**
1240  * i40e_count_filters - counts VSI mac filters
1241  * @vsi: the VSI to be searched
1242  *
1243  * Returns count of mac filters
1244  **/
i40e_count_filters(struct i40e_vsi * vsi)1245 int i40e_count_filters(struct i40e_vsi *vsi)
1246 {
1247 	struct i40e_mac_filter *f;
1248 	struct hlist_node *h;
1249 	int bkt;
1250 	int cnt = 0;
1251 
1252 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1253 		++cnt;
1254 
1255 	return cnt;
1256 }
1257 
1258 /**
1259  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1260  * @vsi: the VSI to be searched
1261  * @macaddr: the MAC address
1262  * @vlan: the vlan
1263  *
1264  * Returns ptr to the filter object or NULL
1265  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1266 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1267 						const u8 *macaddr, s16 vlan)
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 		    (vlan == f->vlan))
1279 			return f;
1280 	}
1281 	return NULL;
1282 }
1283 
1284 /**
1285  * i40e_find_mac - Find a mac addr in the macvlan filters list
1286  * @vsi: the VSI to be searched
1287  * @macaddr: the MAC address we are searching for
1288  *
1289  * Returns the first filter with the provided MAC address or NULL if
1290  * MAC address was not found
1291  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1292 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1293 {
1294 	struct i40e_mac_filter *f;
1295 	u64 key;
1296 
1297 	if (!vsi || !macaddr)
1298 		return NULL;
1299 
1300 	key = i40e_addr_to_hkey(macaddr);
1301 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1302 		if ((ether_addr_equal(macaddr, f->macaddr)))
1303 			return f;
1304 	}
1305 	return NULL;
1306 }
1307 
1308 /**
1309  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1310  * @vsi: the VSI to be searched
1311  *
1312  * Returns true if VSI is in vlan mode or false otherwise
1313  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1314 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1315 {
1316 	/* If we have a PVID, always operate in VLAN mode */
1317 	if (vsi->info.pvid)
1318 		return true;
1319 
1320 	/* We need to operate in VLAN mode whenever we have any filters with
1321 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1322 	 * time, incurring search cost repeatedly. However, we can notice two
1323 	 * things:
1324 	 *
1325 	 * 1) the only place where we can gain a VLAN filter is in
1326 	 *    i40e_add_filter.
1327 	 *
1328 	 * 2) the only place where filters are actually removed is in
1329 	 *    i40e_sync_filters_subtask.
1330 	 *
1331 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1332 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1333 	 * we have to perform the full search after deleting filters in
1334 	 * i40e_sync_filters_subtask, but we already have to search
1335 	 * filters here and can perform the check at the same time. This
1336 	 * results in avoiding embedding a loop for VLAN mode inside another
1337 	 * loop over all the filters, and should maintain correctness as noted
1338 	 * above.
1339 	 */
1340 	return vsi->has_vlan_filter;
1341 }
1342 
1343 /**
1344  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1345  * @vsi: the VSI to configure
1346  * @tmp_add_list: list of filters ready to be added
1347  * @tmp_del_list: list of filters ready to be deleted
1348  * @vlan_filters: the number of active VLAN filters
1349  *
1350  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1351  * behave as expected. If we have any active VLAN filters remaining or about
1352  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1353  * so that they only match against untagged traffic. If we no longer have any
1354  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1355  * so that they match against both tagged and untagged traffic. In this way,
1356  * we ensure that we correctly receive the desired traffic. This ensures that
1357  * when we have an active VLAN we will receive only untagged traffic and
1358  * traffic matching active VLANs. If we have no active VLANs then we will
1359  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1360  *
1361  * Finally, in a similar fashion, this function also corrects filters when
1362  * there is an active PVID assigned to this VSI.
1363  *
1364  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1365  *
1366  * This function is only expected to be called from within
1367  * i40e_sync_vsi_filters.
1368  *
1369  * NOTE: This function expects to be called while under the
1370  * mac_filter_hash_lock
1371  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1372 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1373 					 struct hlist_head *tmp_add_list,
1374 					 struct hlist_head *tmp_del_list,
1375 					 int vlan_filters)
1376 {
1377 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1378 	struct i40e_mac_filter *f, *add_head;
1379 	struct i40e_new_mac_filter *new;
1380 	struct hlist_node *h;
1381 	int bkt, new_vlan;
1382 
1383 	/* To determine if a particular filter needs to be replaced we
1384 	 * have the three following conditions:
1385 	 *
1386 	 * a) if we have a PVID assigned, then all filters which are
1387 	 *    not marked as VLAN=PVID must be replaced with filters that
1388 	 *    are.
1389 	 * b) otherwise, if we have any active VLANS, all filters
1390 	 *    which are marked as VLAN=-1 must be replaced with
1391 	 *    filters marked as VLAN=0
1392 	 * c) finally, if we do not have any active VLANS, all filters
1393 	 *    which are marked as VLAN=0 must be replaced with filters
1394 	 *    marked as VLAN=-1
1395 	 */
1396 
1397 	/* Update the filters about to be added in place */
1398 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1399 		if (pvid && new->f->vlan != pvid)
1400 			new->f->vlan = pvid;
1401 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1402 			new->f->vlan = 0;
1403 		else if (!vlan_filters && new->f->vlan == 0)
1404 			new->f->vlan = I40E_VLAN_ANY;
1405 	}
1406 
1407 	/* Update the remaining active filters */
1408 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1409 		/* Combine the checks for whether a filter needs to be changed
1410 		 * and then determine the new VLAN inside the if block, in
1411 		 * order to avoid duplicating code for adding the new filter
1412 		 * then deleting the old filter.
1413 		 */
1414 		if ((pvid && f->vlan != pvid) ||
1415 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1416 		    (!vlan_filters && f->vlan == 0)) {
1417 			/* Determine the new vlan we will be adding */
1418 			if (pvid)
1419 				new_vlan = pvid;
1420 			else if (vlan_filters)
1421 				new_vlan = 0;
1422 			else
1423 				new_vlan = I40E_VLAN_ANY;
1424 
1425 			/* Create the new filter */
1426 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1427 			if (!add_head)
1428 				return -ENOMEM;
1429 
1430 			/* Create a temporary i40e_new_mac_filter */
1431 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1432 			if (!new)
1433 				return -ENOMEM;
1434 
1435 			new->f = add_head;
1436 			new->state = add_head->state;
1437 
1438 			/* Add the new filter to the tmp list */
1439 			hlist_add_head(&new->hlist, tmp_add_list);
1440 
1441 			/* Put the original filter into the delete list */
1442 			f->state = I40E_FILTER_REMOVE;
1443 			hash_del(&f->hlist);
1444 			hlist_add_head(&f->hlist, tmp_del_list);
1445 		}
1446 	}
1447 
1448 	vsi->has_vlan_filter = !!vlan_filters;
1449 
1450 	return 0;
1451 }
1452 
1453 /**
1454  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1455  * @vsi: the vsi to configure
1456  * @new_mac: new mac filter to be added
1457  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1458  * @vlan_filters: the number of active VLAN filters
1459  * @trusted: flag if the VF is trusted
1460  *
1461  * Get new VLAN id based on current VLAN filters, trust, PVID
1462  * and vf-vlan-prune-disable flag.
1463  *
1464  * Returns the value of the new vlan filter or
1465  * the old value if no new filter is needed.
1466  */
i40e_get_vf_new_vlan(struct i40e_vsi * vsi,struct i40e_new_mac_filter * new_mac,struct i40e_mac_filter * f,int vlan_filters,bool trusted)1467 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1468 				struct i40e_new_mac_filter *new_mac,
1469 				struct i40e_mac_filter *f,
1470 				int vlan_filters,
1471 				bool trusted)
1472 {
1473 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1474 	struct i40e_pf *pf = vsi->back;
1475 	bool is_any;
1476 
1477 	if (new_mac)
1478 		f = new_mac->f;
1479 
1480 	if (pvid && f->vlan != pvid)
1481 		return pvid;
1482 
1483 	is_any = (trusted ||
1484 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1485 
1486 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1487 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1488 	    (is_any && !vlan_filters && f->vlan == 0)) {
1489 		if (is_any)
1490 			return I40E_VLAN_ANY;
1491 		else
1492 			return 0;
1493 	}
1494 
1495 	return f->vlan;
1496 }
1497 
1498 /**
1499  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1500  * @vsi: the vsi to configure
1501  * @tmp_add_list: list of filters ready to be added
1502  * @tmp_del_list: list of filters ready to be deleted
1503  * @vlan_filters: the number of active VLAN filters
1504  * @trusted: flag if the VF is trusted
1505  *
1506  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1507  * and vf-vlan-prune-disable flag.
1508  *
1509  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1510  *
1511  * This function is only expected to be called from within
1512  * i40e_sync_vsi_filters.
1513  *
1514  * NOTE: This function expects to be called while under the
1515  * mac_filter_hash_lock
1516  */
i40e_correct_vf_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters,bool trusted)1517 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1518 					    struct hlist_head *tmp_add_list,
1519 					    struct hlist_head *tmp_del_list,
1520 					    int vlan_filters,
1521 					    bool trusted)
1522 {
1523 	struct i40e_mac_filter *f, *add_head;
1524 	struct i40e_new_mac_filter *new_mac;
1525 	struct hlist_node *h;
1526 	int bkt, new_vlan;
1527 
1528 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1529 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1530 							vlan_filters, trusted);
1531 	}
1532 
1533 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1534 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1535 						trusted);
1536 		if (new_vlan != f->vlan) {
1537 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1538 			if (!add_head)
1539 				return -ENOMEM;
1540 			/* Create a temporary i40e_new_mac_filter */
1541 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1542 			if (!new_mac)
1543 				return -ENOMEM;
1544 			new_mac->f = add_head;
1545 			new_mac->state = add_head->state;
1546 
1547 			/* Add the new filter to the tmp list */
1548 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1549 
1550 			/* Put the original filter into the delete list */
1551 			f->state = I40E_FILTER_REMOVE;
1552 			hash_del(&f->hlist);
1553 			hlist_add_head(&f->hlist, tmp_del_list);
1554 		}
1555 	}
1556 
1557 	vsi->has_vlan_filter = !!vlan_filters;
1558 	return 0;
1559 }
1560 
1561 /**
1562  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1563  * @vsi: the PF Main VSI - inappropriate for any other VSI
1564  * @macaddr: the MAC address
1565  *
1566  * Remove whatever filter the firmware set up so the driver can manage
1567  * its own filtering intelligently.
1568  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1569 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1570 {
1571 	struct i40e_aqc_remove_macvlan_element_data element;
1572 	struct i40e_pf *pf = vsi->back;
1573 
1574 	/* Only appropriate for the PF main VSI */
1575 	if (vsi->type != I40E_VSI_MAIN)
1576 		return;
1577 
1578 	memset(&element, 0, sizeof(element));
1579 	ether_addr_copy(element.mac_addr, macaddr);
1580 	element.vlan_tag = 0;
1581 	/* Ignore error returns, some firmware does it this way... */
1582 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1583 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1584 
1585 	memset(&element, 0, sizeof(element));
1586 	ether_addr_copy(element.mac_addr, macaddr);
1587 	element.vlan_tag = 0;
1588 	/* ...and some firmware does it this way. */
1589 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1590 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1591 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1592 }
1593 
1594 /**
1595  * i40e_add_filter - Add a mac/vlan filter to the VSI
1596  * @vsi: the VSI to be searched
1597  * @macaddr: the MAC address
1598  * @vlan: the vlan
1599  *
1600  * Returns ptr to the filter object or NULL when no memory available.
1601  *
1602  * NOTE: This function is expected to be called with mac_filter_hash_lock
1603  * being held.
1604  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1605 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1606 					const u8 *macaddr, s16 vlan)
1607 {
1608 	struct i40e_mac_filter *f;
1609 	u64 key;
1610 
1611 	if (!vsi || !macaddr)
1612 		return NULL;
1613 
1614 	f = i40e_find_filter(vsi, macaddr, vlan);
1615 	if (!f) {
1616 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1617 		if (!f)
1618 			return NULL;
1619 
1620 		/* Update the boolean indicating if we need to function in
1621 		 * VLAN mode.
1622 		 */
1623 		if (vlan >= 0)
1624 			vsi->has_vlan_filter = true;
1625 
1626 		ether_addr_copy(f->macaddr, macaddr);
1627 		f->vlan = vlan;
1628 		f->state = I40E_FILTER_NEW;
1629 		INIT_HLIST_NODE(&f->hlist);
1630 
1631 		key = i40e_addr_to_hkey(macaddr);
1632 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1633 
1634 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1635 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1636 	}
1637 
1638 	/* If we're asked to add a filter that has been marked for removal, it
1639 	 * is safe to simply restore it to active state. __i40e_del_filter
1640 	 * will have simply deleted any filters which were previously marked
1641 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1642 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1643 	 * task, just restore this filter to the ACTIVE state so that the
1644 	 * sync task leaves it in place
1645 	 */
1646 	if (f->state == I40E_FILTER_REMOVE)
1647 		f->state = I40E_FILTER_ACTIVE;
1648 
1649 	return f;
1650 }
1651 
1652 /**
1653  * __i40e_del_filter - Remove a specific filter from the VSI
1654  * @vsi: VSI to remove from
1655  * @f: the filter to remove from the list
1656  *
1657  * This function should be called instead of i40e_del_filter only if you know
1658  * the exact filter you will remove already, such as via i40e_find_filter or
1659  * i40e_find_mac.
1660  *
1661  * NOTE: This function is expected to be called with mac_filter_hash_lock
1662  * being held.
1663  * ANOTHER NOTE: This function MUST be called from within the context of
1664  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1665  * instead of list_for_each_entry().
1666  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1667 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1668 {
1669 	if (!f)
1670 		return;
1671 
1672 	/* If the filter was never added to firmware then we can just delete it
1673 	 * directly and we don't want to set the status to remove or else an
1674 	 * admin queue command will unnecessarily fire.
1675 	 */
1676 	if ((f->state == I40E_FILTER_FAILED) ||
1677 	    (f->state == I40E_FILTER_NEW)) {
1678 		hash_del(&f->hlist);
1679 		kfree(f);
1680 	} else {
1681 		f->state = I40E_FILTER_REMOVE;
1682 	}
1683 
1684 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1685 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1686 }
1687 
1688 /**
1689  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1690  * @vsi: the VSI to be searched
1691  * @macaddr: the MAC address
1692  * @vlan: the VLAN
1693  *
1694  * NOTE: This function is expected to be called with mac_filter_hash_lock
1695  * being held.
1696  * ANOTHER NOTE: This function MUST be called from within the context of
1697  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1698  * instead of list_for_each_entry().
1699  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1700 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1701 {
1702 	struct i40e_mac_filter *f;
1703 
1704 	if (!vsi || !macaddr)
1705 		return;
1706 
1707 	f = i40e_find_filter(vsi, macaddr, vlan);
1708 	__i40e_del_filter(vsi, f);
1709 }
1710 
1711 /**
1712  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1713  * @vsi: the VSI to be searched
1714  * @macaddr: the mac address to be filtered
1715  *
1716  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1717  * go through all the macvlan filters and add a macvlan filter for each
1718  * unique vlan that already exists. If a PVID has been assigned, instead only
1719  * add the macaddr to that VLAN.
1720  *
1721  * Returns last filter added on success, else NULL
1722  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1723 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1724 					    const u8 *macaddr)
1725 {
1726 	struct i40e_mac_filter *f, *add = NULL;
1727 	struct hlist_node *h;
1728 	int bkt;
1729 
1730 	if (vsi->info.pvid)
1731 		return i40e_add_filter(vsi, macaddr,
1732 				       le16_to_cpu(vsi->info.pvid));
1733 
1734 	if (!i40e_is_vsi_in_vlan(vsi))
1735 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1736 
1737 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1738 		if (f->state == I40E_FILTER_REMOVE)
1739 			continue;
1740 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1741 		if (!add)
1742 			return NULL;
1743 	}
1744 
1745 	return add;
1746 }
1747 
1748 /**
1749  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1750  * @vsi: the VSI to be searched
1751  * @macaddr: the mac address to be removed
1752  *
1753  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1754  * associated with.
1755  *
1756  * Returns 0 for success, or error
1757  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1758 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1759 {
1760 	struct i40e_mac_filter *f;
1761 	struct hlist_node *h;
1762 	bool found = false;
1763 	int bkt;
1764 
1765 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1766 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1767 		if (ether_addr_equal(macaddr, f->macaddr)) {
1768 			__i40e_del_filter(vsi, f);
1769 			found = true;
1770 		}
1771 	}
1772 
1773 	if (found)
1774 		return 0;
1775 	else
1776 		return -ENOENT;
1777 }
1778 
1779 /**
1780  * i40e_set_mac - NDO callback to set mac address
1781  * @netdev: network interface device structure
1782  * @p: pointer to an address structure
1783  *
1784  * Returns 0 on success, negative on failure
1785  **/
i40e_set_mac(struct net_device * netdev,void * p)1786 static int i40e_set_mac(struct net_device *netdev, void *p)
1787 {
1788 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1789 	struct i40e_vsi *vsi = np->vsi;
1790 	struct i40e_pf *pf = vsi->back;
1791 	struct i40e_hw *hw = &pf->hw;
1792 	struct sockaddr *addr = p;
1793 
1794 	if (!is_valid_ether_addr(addr->sa_data))
1795 		return -EADDRNOTAVAIL;
1796 
1797 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1798 		netdev_info(netdev, "already using mac address %pM\n",
1799 			    addr->sa_data);
1800 		return 0;
1801 	}
1802 
1803 	if (test_bit(__I40E_DOWN, pf->state) ||
1804 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1805 		return -EADDRNOTAVAIL;
1806 
1807 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1808 		netdev_info(netdev, "returning to hw mac address %pM\n",
1809 			    hw->mac.addr);
1810 	else
1811 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1812 
1813 	/* Copy the address first, so that we avoid a possible race with
1814 	 * .set_rx_mode().
1815 	 * - Remove old address from MAC filter
1816 	 * - Copy new address
1817 	 * - Add new address to MAC filter
1818 	 */
1819 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1820 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1821 	eth_hw_addr_set(netdev, addr->sa_data);
1822 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1823 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1824 
1825 	if (vsi->type == I40E_VSI_MAIN) {
1826 		int ret;
1827 
1828 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1829 						addr->sa_data, NULL);
1830 		if (ret)
1831 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1832 				    ERR_PTR(ret),
1833 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1834 	}
1835 
1836 	/* schedule our worker thread which will take care of
1837 	 * applying the new filter changes
1838 	 */
1839 	i40e_service_event_schedule(pf);
1840 	return 0;
1841 }
1842 
1843 /**
1844  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1845  * @vsi: vsi structure
1846  * @seed: RSS hash seed
1847  * @lut: pointer to lookup table of lut_size
1848  * @lut_size: size of the lookup table
1849  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1850 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1851 			      u8 *lut, u16 lut_size)
1852 {
1853 	struct i40e_pf *pf = vsi->back;
1854 	struct i40e_hw *hw = &pf->hw;
1855 	int ret = 0;
1856 
1857 	if (seed) {
1858 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1859 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1860 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1861 		if (ret) {
1862 			dev_info(&pf->pdev->dev,
1863 				 "Cannot set RSS key, err %pe aq_err %s\n",
1864 				 ERR_PTR(ret),
1865 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1866 			return ret;
1867 		}
1868 	}
1869 	if (lut) {
1870 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1871 
1872 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1873 		if (ret) {
1874 			dev_info(&pf->pdev->dev,
1875 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1876 				 ERR_PTR(ret),
1877 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1878 			return ret;
1879 		}
1880 	}
1881 	return ret;
1882 }
1883 
1884 /**
1885  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1886  * @vsi: VSI structure
1887  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1888 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1889 {
1890 	struct i40e_pf *pf = vsi->back;
1891 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1892 	u8 *lut;
1893 	int ret;
1894 
1895 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1896 		return 0;
1897 	if (!vsi->rss_size)
1898 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1899 				      vsi->num_queue_pairs);
1900 	if (!vsi->rss_size)
1901 		return -EINVAL;
1902 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1903 	if (!lut)
1904 		return -ENOMEM;
1905 
1906 	/* Use the user configured hash keys and lookup table if there is one,
1907 	 * otherwise use default
1908 	 */
1909 	if (vsi->rss_lut_user)
1910 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1911 	else
1912 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1913 	if (vsi->rss_hkey_user)
1914 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1915 	else
1916 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1917 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1918 	kfree(lut);
1919 	return ret;
1920 }
1921 
1922 /**
1923  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1924  * @vsi: the VSI being configured,
1925  * @ctxt: VSI context structure
1926  * @enabled_tc: number of traffic classes to enable
1927  *
1928  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1929  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1930 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1931 					   struct i40e_vsi_context *ctxt,
1932 					   u8 enabled_tc)
1933 {
1934 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1935 	int i, override_q, pow, num_qps, ret;
1936 	u8 netdev_tc = 0, offset = 0;
1937 
1938 	if (vsi->type != I40E_VSI_MAIN)
1939 		return -EINVAL;
1940 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1941 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1942 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1943 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1944 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1945 
1946 	/* find the next higher power-of-2 of num queue pairs */
1947 	pow = ilog2(num_qps);
1948 	if (!is_power_of_2(num_qps))
1949 		pow++;
1950 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1951 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1952 
1953 	/* Setup queue offset/count for all TCs for given VSI */
1954 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1955 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1956 		/* See if the given TC is enabled for the given VSI */
1957 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1958 			offset = vsi->mqprio_qopt.qopt.offset[i];
1959 			qcount = vsi->mqprio_qopt.qopt.count[i];
1960 			if (qcount > max_qcount)
1961 				max_qcount = qcount;
1962 			vsi->tc_config.tc_info[i].qoffset = offset;
1963 			vsi->tc_config.tc_info[i].qcount = qcount;
1964 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1965 		} else {
1966 			/* TC is not enabled so set the offset to
1967 			 * default queue and allocate one queue
1968 			 * for the given TC.
1969 			 */
1970 			vsi->tc_config.tc_info[i].qoffset = 0;
1971 			vsi->tc_config.tc_info[i].qcount = 1;
1972 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1973 		}
1974 	}
1975 
1976 	/* Set actual Tx/Rx queue pairs */
1977 	vsi->num_queue_pairs = offset + qcount;
1978 
1979 	/* Setup queue TC[0].qmap for given VSI context */
1980 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1981 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1982 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1983 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1984 
1985 	/* Reconfigure RSS for main VSI with max queue count */
1986 	vsi->rss_size = max_qcount;
1987 	ret = i40e_vsi_config_rss(vsi);
1988 	if (ret) {
1989 		dev_info(&vsi->back->pdev->dev,
1990 			 "Failed to reconfig rss for num_queues (%u)\n",
1991 			 max_qcount);
1992 		return ret;
1993 	}
1994 	vsi->reconfig_rss = true;
1995 	dev_dbg(&vsi->back->pdev->dev,
1996 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1997 
1998 	/* Find queue count available for channel VSIs and starting offset
1999 	 * for channel VSIs
2000 	 */
2001 	override_q = vsi->mqprio_qopt.qopt.count[0];
2002 	if (override_q && override_q < vsi->num_queue_pairs) {
2003 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2004 		vsi->next_base_queue = override_q;
2005 	}
2006 	return 0;
2007 }
2008 
2009 /**
2010  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2011  * @vsi: the VSI being setup
2012  * @ctxt: VSI context structure
2013  * @enabled_tc: Enabled TCs bitmap
2014  * @is_add: True if called before Add VSI
2015  *
2016  * Setup VSI queue mapping for enabled traffic classes.
2017  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)2018 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2019 				     struct i40e_vsi_context *ctxt,
2020 				     u8 enabled_tc,
2021 				     bool is_add)
2022 {
2023 	struct i40e_pf *pf = vsi->back;
2024 	u16 num_tc_qps = 0;
2025 	u16 sections = 0;
2026 	u8 netdev_tc = 0;
2027 	u16 numtc = 1;
2028 	u16 qcount;
2029 	u8 offset;
2030 	u16 qmap;
2031 	int i;
2032 
2033 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2034 	offset = 0;
2035 	/* zero out queue mapping, it will get updated on the end of the function */
2036 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2037 
2038 	if (vsi->type == I40E_VSI_MAIN) {
2039 		/* This code helps add more queue to the VSI if we have
2040 		 * more cores than RSS can support, the higher cores will
2041 		 * be served by ATR or other filters. Furthermore, the
2042 		 * non-zero req_queue_pairs says that user requested a new
2043 		 * queue count via ethtool's set_channels, so use this
2044 		 * value for queues distribution across traffic classes
2045 		 * We need at least one queue pair for the interface
2046 		 * to be usable as we see in else statement.
2047 		 */
2048 		if (vsi->req_queue_pairs > 0)
2049 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2050 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2051 			vsi->num_queue_pairs = pf->num_lan_msix;
2052 		else
2053 			vsi->num_queue_pairs = 1;
2054 	}
2055 
2056 	/* Number of queues per enabled TC */
2057 	if (vsi->type == I40E_VSI_MAIN ||
2058 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2059 		num_tc_qps = vsi->num_queue_pairs;
2060 	else
2061 		num_tc_qps = vsi->alloc_queue_pairs;
2062 
2063 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2064 		/* Find numtc from enabled TC bitmap */
2065 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2066 			if (enabled_tc & BIT(i)) /* TC is enabled */
2067 				numtc++;
2068 		}
2069 		if (!numtc) {
2070 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2071 			numtc = 1;
2072 		}
2073 		num_tc_qps = num_tc_qps / numtc;
2074 		num_tc_qps = min_t(int, num_tc_qps,
2075 				   i40e_pf_get_max_q_per_tc(pf));
2076 	}
2077 
2078 	vsi->tc_config.numtc = numtc;
2079 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2080 
2081 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2082 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2083 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2084 
2085 	/* Setup queue offset/count for all TCs for given VSI */
2086 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2087 		/* See if the given TC is enabled for the given VSI */
2088 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2089 			/* TC is enabled */
2090 			int pow, num_qps;
2091 
2092 			switch (vsi->type) {
2093 			case I40E_VSI_MAIN:
2094 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2095 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2096 				    vsi->tc_config.enabled_tc != 1) {
2097 					qcount = min_t(int, pf->alloc_rss_size,
2098 						       num_tc_qps);
2099 					break;
2100 				}
2101 				fallthrough;
2102 			case I40E_VSI_FDIR:
2103 			case I40E_VSI_SRIOV:
2104 			case I40E_VSI_VMDQ2:
2105 			default:
2106 				qcount = num_tc_qps;
2107 				WARN_ON(i != 0);
2108 				break;
2109 			}
2110 			vsi->tc_config.tc_info[i].qoffset = offset;
2111 			vsi->tc_config.tc_info[i].qcount = qcount;
2112 
2113 			/* find the next higher power-of-2 of num queue pairs */
2114 			num_qps = qcount;
2115 			pow = 0;
2116 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2117 				pow++;
2118 				num_qps >>= 1;
2119 			}
2120 
2121 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2122 			qmap =
2123 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2124 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2125 
2126 			offset += qcount;
2127 		} else {
2128 			/* TC is not enabled so set the offset to
2129 			 * default queue and allocate one queue
2130 			 * for the given TC.
2131 			 */
2132 			vsi->tc_config.tc_info[i].qoffset = 0;
2133 			vsi->tc_config.tc_info[i].qcount = 1;
2134 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2135 
2136 			qmap = 0;
2137 		}
2138 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2139 	}
2140 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2141 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2142 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2143 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2144 		vsi->num_queue_pairs = offset;
2145 
2146 	/* Scheduler section valid can only be set for ADD VSI */
2147 	if (is_add) {
2148 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2149 
2150 		ctxt->info.up_enable_bits = enabled_tc;
2151 	}
2152 	if (vsi->type == I40E_VSI_SRIOV) {
2153 		ctxt->info.mapping_flags |=
2154 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2155 		for (i = 0; i < vsi->num_queue_pairs; i++)
2156 			ctxt->info.queue_mapping[i] =
2157 					       cpu_to_le16(vsi->base_queue + i);
2158 	} else {
2159 		ctxt->info.mapping_flags |=
2160 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2161 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2162 	}
2163 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2164 }
2165 
2166 /**
2167  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2168  * @netdev: the netdevice
2169  * @addr: address to add
2170  *
2171  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2172  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2173  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)2174 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2175 {
2176 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2177 	struct i40e_vsi *vsi = np->vsi;
2178 
2179 	if (i40e_add_mac_filter(vsi, addr))
2180 		return 0;
2181 	else
2182 		return -ENOMEM;
2183 }
2184 
2185 /**
2186  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2187  * @netdev: the netdevice
2188  * @addr: address to add
2189  *
2190  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2191  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2192  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)2193 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2194 {
2195 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2196 	struct i40e_vsi *vsi = np->vsi;
2197 
2198 	/* Under some circumstances, we might receive a request to delete
2199 	 * our own device address from our uc list. Because we store the
2200 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2201 	 * such requests and not delete our device address from this list.
2202 	 */
2203 	if (ether_addr_equal(addr, netdev->dev_addr))
2204 		return 0;
2205 
2206 	i40e_del_mac_filter(vsi, addr);
2207 
2208 	return 0;
2209 }
2210 
2211 /**
2212  * i40e_set_rx_mode - NDO callback to set the netdev filters
2213  * @netdev: network interface device structure
2214  **/
i40e_set_rx_mode(struct net_device * netdev)2215 static void i40e_set_rx_mode(struct net_device *netdev)
2216 {
2217 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2218 	struct i40e_vsi *vsi = np->vsi;
2219 
2220 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2221 
2222 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2223 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2224 
2225 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2226 
2227 	/* check for other flag changes */
2228 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2229 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2230 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2231 	}
2232 }
2233 
2234 /**
2235  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2236  * @vsi: Pointer to VSI struct
2237  * @from: Pointer to list which contains MAC filter entries - changes to
2238  *        those entries needs to be undone.
2239  *
2240  * MAC filter entries from this list were slated for deletion.
2241  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2242 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2243 					 struct hlist_head *from)
2244 {
2245 	struct i40e_mac_filter *f;
2246 	struct hlist_node *h;
2247 
2248 	hlist_for_each_entry_safe(f, h, from, hlist) {
2249 		u64 key = i40e_addr_to_hkey(f->macaddr);
2250 
2251 		/* Move the element back into MAC filter list*/
2252 		hlist_del(&f->hlist);
2253 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2254 	}
2255 }
2256 
2257 /**
2258  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2259  * @vsi: Pointer to vsi struct
2260  * @from: Pointer to list which contains MAC filter entries - changes to
2261  *        those entries needs to be undone.
2262  *
2263  * MAC filter entries from this list were slated for addition.
2264  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2265 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2266 					 struct hlist_head *from)
2267 {
2268 	struct i40e_new_mac_filter *new;
2269 	struct hlist_node *h;
2270 
2271 	hlist_for_each_entry_safe(new, h, from, hlist) {
2272 		/* We can simply free the wrapper structure */
2273 		hlist_del(&new->hlist);
2274 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2275 		kfree(new);
2276 	}
2277 }
2278 
2279 /**
2280  * i40e_next_filter - Get the next non-broadcast filter from a list
2281  * @next: pointer to filter in list
2282  *
2283  * Returns the next non-broadcast filter in the list. Required so that we
2284  * ignore broadcast filters within the list, since these are not handled via
2285  * the normal firmware update path.
2286  */
2287 static
i40e_next_filter(struct i40e_new_mac_filter * next)2288 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2289 {
2290 	hlist_for_each_entry_continue(next, hlist) {
2291 		if (!is_broadcast_ether_addr(next->f->macaddr))
2292 			return next;
2293 	}
2294 
2295 	return NULL;
2296 }
2297 
2298 /**
2299  * i40e_update_filter_state - Update filter state based on return data
2300  * from firmware
2301  * @count: Number of filters added
2302  * @add_list: return data from fw
2303  * @add_head: pointer to first filter in current batch
2304  *
2305  * MAC filter entries from list were slated to be added to device. Returns
2306  * number of successful filters. Note that 0 does NOT mean success!
2307  **/
2308 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2309 i40e_update_filter_state(int count,
2310 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2311 			 struct i40e_new_mac_filter *add_head)
2312 {
2313 	int retval = 0;
2314 	int i;
2315 
2316 	for (i = 0; i < count; i++) {
2317 		/* Always check status of each filter. We don't need to check
2318 		 * the firmware return status because we pre-set the filter
2319 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2320 		 * request to the adminq. Thus, if it no longer matches then
2321 		 * we know the filter is active.
2322 		 */
2323 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2324 			add_head->state = I40E_FILTER_FAILED;
2325 		} else {
2326 			add_head->state = I40E_FILTER_ACTIVE;
2327 			retval++;
2328 		}
2329 
2330 		add_head = i40e_next_filter(add_head);
2331 		if (!add_head)
2332 			break;
2333 	}
2334 
2335 	return retval;
2336 }
2337 
2338 /**
2339  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2340  * @vsi: ptr to the VSI
2341  * @vsi_name: name to display in messages
2342  * @list: the list of filters to send to firmware
2343  * @num_del: the number of filters to delete
2344  * @retval: Set to -EIO on failure to delete
2345  *
2346  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2347  * *retval instead of a return value so that success does not force ret_val to
2348  * be set to 0. This ensures that a sequence of calls to this function
2349  * preserve the previous value of *retval on successful delete.
2350  */
2351 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)2352 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2353 			  struct i40e_aqc_remove_macvlan_element_data *list,
2354 			  int num_del, int *retval)
2355 {
2356 	struct i40e_hw *hw = &vsi->back->hw;
2357 	enum i40e_admin_queue_err aq_status;
2358 	int aq_ret;
2359 
2360 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2361 					   &aq_status);
2362 
2363 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2364 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2365 		*retval = -EIO;
2366 		dev_info(&vsi->back->pdev->dev,
2367 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2368 			 vsi_name, ERR_PTR(aq_ret),
2369 			 i40e_aq_str(hw, aq_status));
2370 	}
2371 }
2372 
2373 /**
2374  * i40e_aqc_add_filters - Request firmware to add a set of filters
2375  * @vsi: ptr to the VSI
2376  * @vsi_name: name to display in messages
2377  * @list: the list of filters to send to firmware
2378  * @add_head: Position in the add hlist
2379  * @num_add: the number of filters to add
2380  *
2381  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2382  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2383  * space for more filters.
2384  */
2385 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)2386 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2387 			  struct i40e_aqc_add_macvlan_element_data *list,
2388 			  struct i40e_new_mac_filter *add_head,
2389 			  int num_add)
2390 {
2391 	struct i40e_hw *hw = &vsi->back->hw;
2392 	enum i40e_admin_queue_err aq_status;
2393 	int fcnt;
2394 
2395 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2396 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2397 
2398 	if (fcnt != num_add) {
2399 		if (vsi->type == I40E_VSI_MAIN) {
2400 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2401 			dev_warn(&vsi->back->pdev->dev,
2402 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2403 				 i40e_aq_str(hw, aq_status), vsi_name);
2404 		} else if (vsi->type == I40E_VSI_SRIOV ||
2405 			   vsi->type == I40E_VSI_VMDQ1 ||
2406 			   vsi->type == I40E_VSI_VMDQ2) {
2407 			dev_warn(&vsi->back->pdev->dev,
2408 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2409 				 i40e_aq_str(hw, aq_status), vsi_name,
2410 					     vsi_name);
2411 		} else {
2412 			dev_warn(&vsi->back->pdev->dev,
2413 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2414 				 i40e_aq_str(hw, aq_status), vsi_name,
2415 					     vsi->type);
2416 		}
2417 	}
2418 }
2419 
2420 /**
2421  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2422  * @vsi: pointer to the VSI
2423  * @vsi_name: the VSI name
2424  * @f: filter data
2425  *
2426  * This function sets or clears the promiscuous broadcast flags for VLAN
2427  * filters in order to properly receive broadcast frames. Assumes that only
2428  * broadcast filters are passed.
2429  *
2430  * Returns status indicating success or failure;
2431  **/
2432 static int
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2433 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2434 			  struct i40e_mac_filter *f)
2435 {
2436 	bool enable = f->state == I40E_FILTER_NEW;
2437 	struct i40e_hw *hw = &vsi->back->hw;
2438 	int aq_ret;
2439 
2440 	if (f->vlan == I40E_VLAN_ANY) {
2441 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2442 						   vsi->seid,
2443 						   enable,
2444 						   NULL);
2445 	} else {
2446 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2447 							    vsi->seid,
2448 							    enable,
2449 							    f->vlan,
2450 							    NULL);
2451 	}
2452 
2453 	if (aq_ret) {
2454 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2455 		dev_warn(&vsi->back->pdev->dev,
2456 			 "Error %s, forcing overflow promiscuous on %s\n",
2457 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2458 			 vsi_name);
2459 	}
2460 
2461 	return aq_ret;
2462 }
2463 
2464 /**
2465  * i40e_set_promiscuous - set promiscuous mode
2466  * @pf: board private structure
2467  * @promisc: promisc on or off
2468  *
2469  * There are different ways of setting promiscuous mode on a PF depending on
2470  * what state/environment we're in.  This identifies and sets it appropriately.
2471  * Returns 0 on success.
2472  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2473 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2474 {
2475 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2476 	struct i40e_hw *hw = &pf->hw;
2477 	int aq_ret;
2478 
2479 	if (vsi->type == I40E_VSI_MAIN &&
2480 	    pf->lan_veb != I40E_NO_VEB &&
2481 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2482 		/* set defport ON for Main VSI instead of true promisc
2483 		 * this way we will get all unicast/multicast and VLAN
2484 		 * promisc behavior but will not get VF or VMDq traffic
2485 		 * replicated on the Main VSI.
2486 		 */
2487 		if (promisc)
2488 			aq_ret = i40e_aq_set_default_vsi(hw,
2489 							 vsi->seid,
2490 							 NULL);
2491 		else
2492 			aq_ret = i40e_aq_clear_default_vsi(hw,
2493 							   vsi->seid,
2494 							   NULL);
2495 		if (aq_ret) {
2496 			dev_info(&pf->pdev->dev,
2497 				 "Set default VSI failed, err %pe, aq_err %s\n",
2498 				 ERR_PTR(aq_ret),
2499 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2500 		}
2501 	} else {
2502 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2503 						  hw,
2504 						  vsi->seid,
2505 						  promisc, NULL,
2506 						  true);
2507 		if (aq_ret) {
2508 			dev_info(&pf->pdev->dev,
2509 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2510 				 ERR_PTR(aq_ret),
2511 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2512 		}
2513 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2514 						  hw,
2515 						  vsi->seid,
2516 						  promisc, NULL);
2517 		if (aq_ret) {
2518 			dev_info(&pf->pdev->dev,
2519 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2520 				 ERR_PTR(aq_ret),
2521 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2522 		}
2523 	}
2524 
2525 	if (!aq_ret)
2526 		pf->cur_promisc = promisc;
2527 
2528 	return aq_ret;
2529 }
2530 
2531 /**
2532  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2533  * @vsi: ptr to the VSI
2534  *
2535  * Push any outstanding VSI filter changes through the AdminQ.
2536  *
2537  * Returns 0 or error value
2538  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2539 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2540 {
2541 	struct hlist_head tmp_add_list, tmp_del_list;
2542 	struct i40e_mac_filter *f;
2543 	struct i40e_new_mac_filter *new, *add_head = NULL;
2544 	struct i40e_hw *hw = &vsi->back->hw;
2545 	bool old_overflow, new_overflow;
2546 	unsigned int failed_filters = 0;
2547 	unsigned int vlan_filters = 0;
2548 	char vsi_name[16] = "PF";
2549 	int filter_list_len = 0;
2550 	u32 changed_flags = 0;
2551 	struct hlist_node *h;
2552 	struct i40e_pf *pf;
2553 	int num_add = 0;
2554 	int num_del = 0;
2555 	int aq_ret = 0;
2556 	int retval = 0;
2557 	u16 cmd_flags;
2558 	int list_size;
2559 	int bkt;
2560 
2561 	/* empty array typed pointers, kcalloc later */
2562 	struct i40e_aqc_add_macvlan_element_data *add_list;
2563 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2564 
2565 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2566 		usleep_range(1000, 2000);
2567 	pf = vsi->back;
2568 
2569 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2570 
2571 	if (vsi->netdev) {
2572 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2573 		vsi->current_netdev_flags = vsi->netdev->flags;
2574 	}
2575 
2576 	INIT_HLIST_HEAD(&tmp_add_list);
2577 	INIT_HLIST_HEAD(&tmp_del_list);
2578 
2579 	if (vsi->type == I40E_VSI_SRIOV)
2580 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2581 	else if (vsi->type != I40E_VSI_MAIN)
2582 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2583 
2584 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2585 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2586 
2587 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2588 		/* Create a list of filters to delete. */
2589 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2590 			if (f->state == I40E_FILTER_REMOVE) {
2591 				/* Move the element into temporary del_list */
2592 				hash_del(&f->hlist);
2593 				hlist_add_head(&f->hlist, &tmp_del_list);
2594 
2595 				/* Avoid counting removed filters */
2596 				continue;
2597 			}
2598 			if (f->state == I40E_FILTER_NEW) {
2599 				/* Create a temporary i40e_new_mac_filter */
2600 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2601 				if (!new)
2602 					goto err_no_memory_locked;
2603 
2604 				/* Store pointer to the real filter */
2605 				new->f = f;
2606 				new->state = f->state;
2607 
2608 				/* Add it to the hash list */
2609 				hlist_add_head(&new->hlist, &tmp_add_list);
2610 			}
2611 
2612 			/* Count the number of active (current and new) VLAN
2613 			 * filters we have now. Does not count filters which
2614 			 * are marked for deletion.
2615 			 */
2616 			if (f->vlan > 0)
2617 				vlan_filters++;
2618 		}
2619 
2620 		if (vsi->type != I40E_VSI_SRIOV)
2621 			retval = i40e_correct_mac_vlan_filters
2622 				(vsi, &tmp_add_list, &tmp_del_list,
2623 				 vlan_filters);
2624 		else if (pf->vf)
2625 			retval = i40e_correct_vf_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2628 
2629 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2630 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2631 
2632 		if (retval)
2633 			goto err_no_memory_locked;
2634 
2635 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2636 	}
2637 
2638 	/* Now process 'del_list' outside the lock */
2639 	if (!hlist_empty(&tmp_del_list)) {
2640 		filter_list_len = hw->aq.asq_buf_size /
2641 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2642 		list_size = filter_list_len *
2643 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2644 		del_list = kzalloc(list_size, GFP_ATOMIC);
2645 		if (!del_list)
2646 			goto err_no_memory;
2647 
2648 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2649 			cmd_flags = 0;
2650 
2651 			/* handle broadcast filters by updating the broadcast
2652 			 * promiscuous flag and release filter list.
2653 			 */
2654 			if (is_broadcast_ether_addr(f->macaddr)) {
2655 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2656 
2657 				hlist_del(&f->hlist);
2658 				kfree(f);
2659 				continue;
2660 			}
2661 
2662 			/* add to delete list */
2663 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2664 			if (f->vlan == I40E_VLAN_ANY) {
2665 				del_list[num_del].vlan_tag = 0;
2666 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2667 			} else {
2668 				del_list[num_del].vlan_tag =
2669 					cpu_to_le16((u16)(f->vlan));
2670 			}
2671 
2672 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2673 			del_list[num_del].flags = cmd_flags;
2674 			num_del++;
2675 
2676 			/* flush a full buffer */
2677 			if (num_del == filter_list_len) {
2678 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2679 						     num_del, &retval);
2680 				memset(del_list, 0, list_size);
2681 				num_del = 0;
2682 			}
2683 			/* Release memory for MAC filter entries which were
2684 			 * synced up with HW.
2685 			 */
2686 			hlist_del(&f->hlist);
2687 			kfree(f);
2688 		}
2689 
2690 		if (num_del) {
2691 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2692 					     num_del, &retval);
2693 		}
2694 
2695 		kfree(del_list);
2696 		del_list = NULL;
2697 	}
2698 
2699 	if (!hlist_empty(&tmp_add_list)) {
2700 		/* Do all the adds now. */
2701 		filter_list_len = hw->aq.asq_buf_size /
2702 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2703 		list_size = filter_list_len *
2704 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2705 		add_list = kzalloc(list_size, GFP_ATOMIC);
2706 		if (!add_list)
2707 			goto err_no_memory;
2708 
2709 		num_add = 0;
2710 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2711 			/* handle broadcast filters by updating the broadcast
2712 			 * promiscuous flag instead of adding a MAC filter.
2713 			 */
2714 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2715 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2716 							      new->f))
2717 					new->state = I40E_FILTER_FAILED;
2718 				else
2719 					new->state = I40E_FILTER_ACTIVE;
2720 				continue;
2721 			}
2722 
2723 			/* add to add array */
2724 			if (num_add == 0)
2725 				add_head = new;
2726 			cmd_flags = 0;
2727 			ether_addr_copy(add_list[num_add].mac_addr,
2728 					new->f->macaddr);
2729 			if (new->f->vlan == I40E_VLAN_ANY) {
2730 				add_list[num_add].vlan_tag = 0;
2731 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2732 			} else {
2733 				add_list[num_add].vlan_tag =
2734 					cpu_to_le16((u16)(new->f->vlan));
2735 			}
2736 			add_list[num_add].queue_number = 0;
2737 			/* set invalid match method for later detection */
2738 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2739 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2740 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2741 			num_add++;
2742 
2743 			/* flush a full buffer */
2744 			if (num_add == filter_list_len) {
2745 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2746 						     add_head, num_add);
2747 				memset(add_list, 0, list_size);
2748 				num_add = 0;
2749 			}
2750 		}
2751 		if (num_add) {
2752 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2753 					     num_add);
2754 		}
2755 		/* Now move all of the filters from the temp add list back to
2756 		 * the VSI's list.
2757 		 */
2758 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2759 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2760 			/* Only update the state if we're still NEW */
2761 			if (new->f->state == I40E_FILTER_NEW)
2762 				new->f->state = new->state;
2763 			hlist_del(&new->hlist);
2764 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2765 			kfree(new);
2766 		}
2767 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2768 		kfree(add_list);
2769 		add_list = NULL;
2770 	}
2771 
2772 	/* Determine the number of active and failed filters. */
2773 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2774 	vsi->active_filters = 0;
2775 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2776 		if (f->state == I40E_FILTER_ACTIVE)
2777 			vsi->active_filters++;
2778 		else if (f->state == I40E_FILTER_FAILED)
2779 			failed_filters++;
2780 	}
2781 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2782 
2783 	/* Check if we are able to exit overflow promiscuous mode. We can
2784 	 * safely exit if we didn't just enter, we no longer have any failed
2785 	 * filters, and we have reduced filters below the threshold value.
2786 	 */
2787 	if (old_overflow && !failed_filters &&
2788 	    vsi->active_filters < vsi->promisc_threshold) {
2789 		dev_info(&pf->pdev->dev,
2790 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2791 			 vsi_name);
2792 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 		vsi->promisc_threshold = 0;
2794 	}
2795 
2796 	/* if the VF is not trusted do not do promisc */
2797 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2798 	    !pf->vf[vsi->vf_id].trusted) {
2799 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2800 		goto out;
2801 	}
2802 
2803 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 
2805 	/* If we are entering overflow promiscuous, we need to calculate a new
2806 	 * threshold for when we are safe to exit
2807 	 */
2808 	if (!old_overflow && new_overflow)
2809 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2810 
2811 	/* check for changes in promiscuous modes */
2812 	if (changed_flags & IFF_ALLMULTI) {
2813 		bool cur_multipromisc;
2814 
2815 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2816 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2817 							       vsi->seid,
2818 							       cur_multipromisc,
2819 							       NULL);
2820 		if (aq_ret) {
2821 			retval = i40e_aq_rc_to_posix(aq_ret,
2822 						     hw->aq.asq_last_status);
2823 			dev_info(&pf->pdev->dev,
2824 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2825 				 vsi_name,
2826 				 ERR_PTR(aq_ret),
2827 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2828 		} else {
2829 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2830 				 cur_multipromisc ? "entering" : "leaving");
2831 		}
2832 	}
2833 
2834 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2835 		bool cur_promisc;
2836 
2837 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2838 			       new_overflow);
2839 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2840 		if (aq_ret) {
2841 			retval = i40e_aq_rc_to_posix(aq_ret,
2842 						     hw->aq.asq_last_status);
2843 			dev_info(&pf->pdev->dev,
2844 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2845 				 cur_promisc ? "on" : "off",
2846 				 vsi_name,
2847 				 ERR_PTR(aq_ret),
2848 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2849 		}
2850 	}
2851 out:
2852 	/* if something went wrong then set the changed flag so we try again */
2853 	if (retval)
2854 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2855 
2856 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2857 	return retval;
2858 
2859 err_no_memory:
2860 	/* Restore elements on the temporary add and delete lists */
2861 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2862 err_no_memory_locked:
2863 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2864 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2865 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2866 
2867 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2868 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2869 	return -ENOMEM;
2870 }
2871 
2872 /**
2873  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2874  * @pf: board private structure
2875  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2876 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2877 {
2878 	int v;
2879 
2880 	if (!pf)
2881 		return;
2882 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2883 		return;
2884 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2885 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2886 		return;
2887 	}
2888 
2889 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2890 		if (pf->vsi[v] &&
2891 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2892 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2893 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2894 
2895 			if (ret) {
2896 				/* come back and try again later */
2897 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2898 					pf->state);
2899 				break;
2900 			}
2901 		}
2902 	}
2903 }
2904 
2905 /**
2906  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2907  * @vsi: the vsi
2908  **/
i40e_max_xdp_frame_size(struct i40e_vsi * vsi)2909 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2910 {
2911 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2912 		return I40E_RXBUFFER_2048;
2913 	else
2914 		return I40E_RXBUFFER_3072;
2915 }
2916 
2917 /**
2918  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2919  * @netdev: network interface device structure
2920  * @new_mtu: new value for maximum frame size
2921  *
2922  * Returns 0 on success, negative on failure
2923  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2924 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2925 {
2926 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2927 	struct i40e_vsi *vsi = np->vsi;
2928 	struct i40e_pf *pf = vsi->back;
2929 
2930 	if (i40e_enabled_xdp_vsi(vsi)) {
2931 		int frame_size = new_mtu + I40E_PACKET_HDR_PAD;
2932 
2933 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2934 			return -EINVAL;
2935 	}
2936 
2937 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2938 		   netdev->mtu, new_mtu);
2939 	netdev->mtu = new_mtu;
2940 	if (netif_running(netdev))
2941 		i40e_vsi_reinit_locked(vsi);
2942 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2943 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2944 	return 0;
2945 }
2946 
2947 /**
2948  * i40e_ioctl - Access the hwtstamp interface
2949  * @netdev: network interface device structure
2950  * @ifr: interface request data
2951  * @cmd: ioctl command
2952  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2953 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2954 {
2955 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2956 	struct i40e_pf *pf = np->vsi->back;
2957 
2958 	switch (cmd) {
2959 	case SIOCGHWTSTAMP:
2960 		return i40e_ptp_get_ts_config(pf, ifr);
2961 	case SIOCSHWTSTAMP:
2962 		return i40e_ptp_set_ts_config(pf, ifr);
2963 	default:
2964 		return -EOPNOTSUPP;
2965 	}
2966 }
2967 
2968 /**
2969  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2970  * @vsi: the vsi being adjusted
2971  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2972 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2973 {
2974 	struct i40e_vsi_context ctxt;
2975 	int ret;
2976 
2977 	/* Don't modify stripping options if a port VLAN is active */
2978 	if (vsi->info.pvid)
2979 		return;
2980 
2981 	if ((vsi->info.valid_sections &
2982 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2983 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2984 		return;  /* already enabled */
2985 
2986 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2987 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2988 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2989 
2990 	ctxt.seid = vsi->seid;
2991 	ctxt.info = vsi->info;
2992 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2993 	if (ret) {
2994 		dev_info(&vsi->back->pdev->dev,
2995 			 "update vlan stripping failed, err %pe aq_err %s\n",
2996 			 ERR_PTR(ret),
2997 			 i40e_aq_str(&vsi->back->hw,
2998 				     vsi->back->hw.aq.asq_last_status));
2999 	}
3000 }
3001 
3002 /**
3003  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3004  * @vsi: the vsi being adjusted
3005  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)3006 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3007 {
3008 	struct i40e_vsi_context ctxt;
3009 	int ret;
3010 
3011 	/* Don't modify stripping options if a port VLAN is active */
3012 	if (vsi->info.pvid)
3013 		return;
3014 
3015 	if ((vsi->info.valid_sections &
3016 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3017 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3018 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3019 		return;  /* already disabled */
3020 
3021 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3022 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3023 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3024 
3025 	ctxt.seid = vsi->seid;
3026 	ctxt.info = vsi->info;
3027 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3028 	if (ret) {
3029 		dev_info(&vsi->back->pdev->dev,
3030 			 "update vlan stripping failed, err %pe aq_err %s\n",
3031 			 ERR_PTR(ret),
3032 			 i40e_aq_str(&vsi->back->hw,
3033 				     vsi->back->hw.aq.asq_last_status));
3034 	}
3035 }
3036 
3037 /**
3038  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3039  * @vsi: the vsi being configured
3040  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3041  *
3042  * This is a helper function for adding a new MAC/VLAN filter with the
3043  * specified VLAN for each existing MAC address already in the hash table.
3044  * This function does *not* perform any accounting to update filters based on
3045  * VLAN mode.
3046  *
3047  * NOTE: this function expects to be called while under the
3048  * mac_filter_hash_lock
3049  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3050 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3051 {
3052 	struct i40e_mac_filter *f, *add_f;
3053 	struct hlist_node *h;
3054 	int bkt;
3055 
3056 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3057 		/* If we're asked to add a filter that has been marked for
3058 		 * removal, it is safe to simply restore it to active state.
3059 		 * __i40e_del_filter will have simply deleted any filters which
3060 		 * were previously marked NEW or FAILED, so if it is currently
3061 		 * marked REMOVE it must have previously been ACTIVE. Since we
3062 		 * haven't yet run the sync filters task, just restore this
3063 		 * filter to the ACTIVE state so that the sync task leaves it
3064 		 * in place.
3065 		 */
3066 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3067 			f->state = I40E_FILTER_ACTIVE;
3068 			continue;
3069 		} else if (f->state == I40E_FILTER_REMOVE) {
3070 			continue;
3071 		}
3072 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3073 		if (!add_f) {
3074 			dev_info(&vsi->back->pdev->dev,
3075 				 "Could not add vlan filter %d for %pM\n",
3076 				 vid, f->macaddr);
3077 			return -ENOMEM;
3078 		}
3079 	}
3080 
3081 	return 0;
3082 }
3083 
3084 /**
3085  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3086  * @vsi: the VSI being configured
3087  * @vid: VLAN id to be added
3088  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)3089 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3090 {
3091 	int err;
3092 
3093 	if (vsi->info.pvid)
3094 		return -EINVAL;
3095 
3096 	/* The network stack will attempt to add VID=0, with the intention to
3097 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3098 	 * these packets by default when configured to receive untagged
3099 	 * packets, so we don't need to add a filter for this case.
3100 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3101 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3102 	 * Thus, we do not want to actually add a filter for VID=0
3103 	 */
3104 	if (!vid)
3105 		return 0;
3106 
3107 	/* Locked once because all functions invoked below iterates list*/
3108 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3109 	err = i40e_add_vlan_all_mac(vsi, vid);
3110 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3111 	if (err)
3112 		return err;
3113 
3114 	/* schedule our worker thread which will take care of
3115 	 * applying the new filter changes
3116 	 */
3117 	i40e_service_event_schedule(vsi->back);
3118 	return 0;
3119 }
3120 
3121 /**
3122  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3123  * @vsi: the vsi being configured
3124  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3125  *
3126  * This function should be used to remove all VLAN filters which match the
3127  * given VID. It does not schedule the service event and does not take the
3128  * mac_filter_hash_lock so it may be combined with other operations under
3129  * a single invocation of the mac_filter_hash_lock.
3130  *
3131  * NOTE: this function expects to be called while under the
3132  * mac_filter_hash_lock
3133  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)3134 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3135 {
3136 	struct i40e_mac_filter *f;
3137 	struct hlist_node *h;
3138 	int bkt;
3139 
3140 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3141 		if (f->vlan == vid)
3142 			__i40e_del_filter(vsi, f);
3143 	}
3144 }
3145 
3146 /**
3147  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3148  * @vsi: the VSI being configured
3149  * @vid: VLAN id to be removed
3150  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)3151 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3152 {
3153 	if (!vid || vsi->info.pvid)
3154 		return;
3155 
3156 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3157 	i40e_rm_vlan_all_mac(vsi, vid);
3158 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3159 
3160 	/* schedule our worker thread which will take care of
3161 	 * applying the new filter changes
3162 	 */
3163 	i40e_service_event_schedule(vsi->back);
3164 }
3165 
3166 /**
3167  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3168  * @netdev: network interface to be adjusted
3169  * @proto: unused protocol value
3170  * @vid: vlan id to be added
3171  *
3172  * net_device_ops implementation for adding vlan ids
3173  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3174 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3175 				__always_unused __be16 proto, u16 vid)
3176 {
3177 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3178 	struct i40e_vsi *vsi = np->vsi;
3179 	int ret = 0;
3180 
3181 	if (vid >= VLAN_N_VID)
3182 		return -EINVAL;
3183 
3184 	ret = i40e_vsi_add_vlan(vsi, vid);
3185 	if (!ret)
3186 		set_bit(vid, vsi->active_vlans);
3187 
3188 	return ret;
3189 }
3190 
3191 /**
3192  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3193  * @netdev: network interface to be adjusted
3194  * @proto: unused protocol value
3195  * @vid: vlan id to be added
3196  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3197 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3198 				    __always_unused __be16 proto, u16 vid)
3199 {
3200 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3201 	struct i40e_vsi *vsi = np->vsi;
3202 
3203 	if (vid >= VLAN_N_VID)
3204 		return;
3205 	set_bit(vid, vsi->active_vlans);
3206 }
3207 
3208 /**
3209  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3210  * @netdev: network interface to be adjusted
3211  * @proto: unused protocol value
3212  * @vid: vlan id to be removed
3213  *
3214  * net_device_ops implementation for removing vlan ids
3215  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3216 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3217 				 __always_unused __be16 proto, u16 vid)
3218 {
3219 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3220 	struct i40e_vsi *vsi = np->vsi;
3221 
3222 	/* return code is ignored as there is nothing a user
3223 	 * can do about failure to remove and a log message was
3224 	 * already printed from the other function
3225 	 */
3226 	i40e_vsi_kill_vlan(vsi, vid);
3227 
3228 	clear_bit(vid, vsi->active_vlans);
3229 
3230 	return 0;
3231 }
3232 
3233 /**
3234  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3235  * @vsi: the vsi being brought back up
3236  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3237 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3238 {
3239 	u16 vid;
3240 
3241 	if (!vsi->netdev)
3242 		return;
3243 
3244 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3245 		i40e_vlan_stripping_enable(vsi);
3246 	else
3247 		i40e_vlan_stripping_disable(vsi);
3248 
3249 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3250 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3251 					vid);
3252 }
3253 
3254 /**
3255  * i40e_vsi_add_pvid - Add pvid for the VSI
3256  * @vsi: the vsi being adjusted
3257  * @vid: the vlan id to set as a PVID
3258  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3259 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3260 {
3261 	struct i40e_vsi_context ctxt;
3262 	int ret;
3263 
3264 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3265 	vsi->info.pvid = cpu_to_le16(vid);
3266 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3267 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3268 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3269 
3270 	ctxt.seid = vsi->seid;
3271 	ctxt.info = vsi->info;
3272 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3273 	if (ret) {
3274 		dev_info(&vsi->back->pdev->dev,
3275 			 "add pvid failed, err %pe aq_err %s\n",
3276 			 ERR_PTR(ret),
3277 			 i40e_aq_str(&vsi->back->hw,
3278 				     vsi->back->hw.aq.asq_last_status));
3279 		return -ENOENT;
3280 	}
3281 
3282 	return 0;
3283 }
3284 
3285 /**
3286  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3287  * @vsi: the vsi being adjusted
3288  *
3289  * Just use the vlan_rx_register() service to put it back to normal
3290  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3291 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3292 {
3293 	vsi->info.pvid = 0;
3294 
3295 	i40e_vlan_stripping_disable(vsi);
3296 }
3297 
3298 /**
3299  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3300  * @vsi: ptr to the VSI
3301  *
3302  * If this function returns with an error, then it's possible one or
3303  * more of the rings is populated (while the rest are not).  It is the
3304  * callers duty to clean those orphaned rings.
3305  *
3306  * Return 0 on success, negative on failure
3307  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3308 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3309 {
3310 	int i, err = 0;
3311 
3312 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3313 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3314 
3315 	if (!i40e_enabled_xdp_vsi(vsi))
3316 		return err;
3317 
3318 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3319 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3320 
3321 	return err;
3322 }
3323 
3324 /**
3325  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3326  * @vsi: ptr to the VSI
3327  *
3328  * Free VSI's transmit software resources
3329  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3330 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3331 {
3332 	int i;
3333 
3334 	if (vsi->tx_rings) {
3335 		for (i = 0; i < vsi->num_queue_pairs; i++)
3336 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3337 				i40e_free_tx_resources(vsi->tx_rings[i]);
3338 	}
3339 
3340 	if (vsi->xdp_rings) {
3341 		for (i = 0; i < vsi->num_queue_pairs; i++)
3342 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3343 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3344 	}
3345 }
3346 
3347 /**
3348  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3349  * @vsi: ptr to the VSI
3350  *
3351  * If this function returns with an error, then it's possible one or
3352  * more of the rings is populated (while the rest are not).  It is the
3353  * callers duty to clean those orphaned rings.
3354  *
3355  * Return 0 on success, negative on failure
3356  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3357 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3358 {
3359 	int i, err = 0;
3360 
3361 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3362 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3363 	return err;
3364 }
3365 
3366 /**
3367  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3368  * @vsi: ptr to the VSI
3369  *
3370  * Free all receive software resources
3371  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3372 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3373 {
3374 	int i;
3375 
3376 	if (!vsi->rx_rings)
3377 		return;
3378 
3379 	for (i = 0; i < vsi->num_queue_pairs; i++)
3380 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3381 			i40e_free_rx_resources(vsi->rx_rings[i]);
3382 }
3383 
3384 /**
3385  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3386  * @ring: The Tx ring to configure
3387  *
3388  * This enables/disables XPS for a given Tx descriptor ring
3389  * based on the TCs enabled for the VSI that ring belongs to.
3390  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3391 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3392 {
3393 	int cpu;
3394 
3395 	if (!ring->q_vector || !ring->netdev || ring->ch)
3396 		return;
3397 
3398 	/* We only initialize XPS once, so as not to overwrite user settings */
3399 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3400 		return;
3401 
3402 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3403 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3404 			    ring->queue_index);
3405 }
3406 
3407 /**
3408  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3409  * @ring: The Tx or Rx ring
3410  *
3411  * Returns the AF_XDP buffer pool or NULL.
3412  **/
i40e_xsk_pool(struct i40e_ring * ring)3413 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3414 {
3415 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3416 	int qid = ring->queue_index;
3417 
3418 	if (ring_is_xdp(ring))
3419 		qid -= ring->vsi->alloc_queue_pairs;
3420 
3421 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3422 		return NULL;
3423 
3424 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3425 }
3426 
3427 /**
3428  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3429  * @ring: The Tx ring to configure
3430  *
3431  * Configure the Tx descriptor ring in the HMC context.
3432  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3433 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3434 {
3435 	struct i40e_vsi *vsi = ring->vsi;
3436 	u16 pf_q = vsi->base_queue + ring->queue_index;
3437 	struct i40e_hw *hw = &vsi->back->hw;
3438 	struct i40e_hmc_obj_txq tx_ctx;
3439 	u32 qtx_ctl = 0;
3440 	int err = 0;
3441 
3442 	if (ring_is_xdp(ring))
3443 		ring->xsk_pool = i40e_xsk_pool(ring);
3444 
3445 	/* some ATR related tx ring init */
3446 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3447 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3448 		ring->atr_count = 0;
3449 	} else {
3450 		ring->atr_sample_rate = 0;
3451 	}
3452 
3453 	/* configure XPS */
3454 	i40e_config_xps_tx_ring(ring);
3455 
3456 	/* clear the context structure first */
3457 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3458 
3459 	tx_ctx.new_context = 1;
3460 	tx_ctx.base = (ring->dma / 128);
3461 	tx_ctx.qlen = ring->count;
3462 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3463 					       I40E_FLAG_FD_ATR_ENABLED));
3464 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3465 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3466 	if (vsi->type != I40E_VSI_FDIR)
3467 		tx_ctx.head_wb_ena = 1;
3468 	tx_ctx.head_wb_addr = ring->dma +
3469 			      (ring->count * sizeof(struct i40e_tx_desc));
3470 
3471 	/* As part of VSI creation/update, FW allocates certain
3472 	 * Tx arbitration queue sets for each TC enabled for
3473 	 * the VSI. The FW returns the handles to these queue
3474 	 * sets as part of the response buffer to Add VSI,
3475 	 * Update VSI, etc. AQ commands. It is expected that
3476 	 * these queue set handles be associated with the Tx
3477 	 * queues by the driver as part of the TX queue context
3478 	 * initialization. This has to be done regardless of
3479 	 * DCB as by default everything is mapped to TC0.
3480 	 */
3481 
3482 	if (ring->ch)
3483 		tx_ctx.rdylist =
3484 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3485 
3486 	else
3487 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3488 
3489 	tx_ctx.rdylist_act = 0;
3490 
3491 	/* clear the context in the HMC */
3492 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3493 	if (err) {
3494 		dev_info(&vsi->back->pdev->dev,
3495 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3496 			 ring->queue_index, pf_q, err);
3497 		return -ENOMEM;
3498 	}
3499 
3500 	/* set the context in the HMC */
3501 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3502 	if (err) {
3503 		dev_info(&vsi->back->pdev->dev,
3504 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3505 			 ring->queue_index, pf_q, err);
3506 		return -ENOMEM;
3507 	}
3508 
3509 	/* Now associate this queue with this PCI function */
3510 	if (ring->ch) {
3511 		if (ring->ch->type == I40E_VSI_VMDQ2)
3512 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3513 		else
3514 			return -EINVAL;
3515 
3516 		qtx_ctl |= (ring->ch->vsi_number <<
3517 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3518 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3519 	} else {
3520 		if (vsi->type == I40E_VSI_VMDQ2) {
3521 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3522 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3523 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3524 		} else {
3525 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3526 		}
3527 	}
3528 
3529 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3530 		    I40E_QTX_CTL_PF_INDX_MASK);
3531 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3532 	i40e_flush(hw);
3533 
3534 	/* cache tail off for easier writes later */
3535 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3536 
3537 	return 0;
3538 }
3539 
3540 /**
3541  * i40e_rx_offset - Return expected offset into page to access data
3542  * @rx_ring: Ring we are requesting offset of
3543  *
3544  * Returns the offset value for ring into the data buffer.
3545  */
i40e_rx_offset(struct i40e_ring * rx_ring)3546 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3547 {
3548 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3549 }
3550 
3551 /**
3552  * i40e_configure_rx_ring - Configure a receive ring context
3553  * @ring: The Rx ring to configure
3554  *
3555  * Configure the Rx descriptor ring in the HMC context.
3556  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3557 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3558 {
3559 	struct i40e_vsi *vsi = ring->vsi;
3560 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3561 	u16 pf_q = vsi->base_queue + ring->queue_index;
3562 	struct i40e_hw *hw = &vsi->back->hw;
3563 	struct i40e_hmc_obj_rxq rx_ctx;
3564 	int err = 0;
3565 	bool ok;
3566 	int ret;
3567 
3568 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3569 
3570 	/* clear the context structure first */
3571 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3572 
3573 	if (ring->vsi->type == I40E_VSI_MAIN)
3574 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3575 
3576 	ring->xsk_pool = i40e_xsk_pool(ring);
3577 	if (ring->xsk_pool) {
3578 		ring->rx_buf_len =
3579 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3580 		/* For AF_XDP ZC, we disallow packets to span on
3581 		 * multiple buffers, thus letting us skip that
3582 		 * handling in the fast-path.
3583 		 */
3584 		chain_len = 1;
3585 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3586 						 MEM_TYPE_XSK_BUFF_POOL,
3587 						 NULL);
3588 		if (ret)
3589 			return ret;
3590 		dev_info(&vsi->back->pdev->dev,
3591 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3592 			 ring->queue_index);
3593 
3594 	} else {
3595 		ring->rx_buf_len = vsi->rx_buf_len;
3596 		if (ring->vsi->type == I40E_VSI_MAIN) {
3597 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3598 							 MEM_TYPE_PAGE_SHARED,
3599 							 NULL);
3600 			if (ret)
3601 				return ret;
3602 		}
3603 	}
3604 
3605 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3606 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3607 
3608 	rx_ctx.base = (ring->dma / 128);
3609 	rx_ctx.qlen = ring->count;
3610 
3611 	/* use 16 byte descriptors */
3612 	rx_ctx.dsize = 0;
3613 
3614 	/* descriptor type is always zero
3615 	 * rx_ctx.dtype = 0;
3616 	 */
3617 	rx_ctx.hsplit_0 = 0;
3618 
3619 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3620 	if (hw->revision_id == 0)
3621 		rx_ctx.lrxqthresh = 0;
3622 	else
3623 		rx_ctx.lrxqthresh = 1;
3624 	rx_ctx.crcstrip = 1;
3625 	rx_ctx.l2tsel = 1;
3626 	/* this controls whether VLAN is stripped from inner headers */
3627 	rx_ctx.showiv = 0;
3628 	/* set the prefena field to 1 because the manual says to */
3629 	rx_ctx.prefena = 1;
3630 
3631 	/* clear the context in the HMC */
3632 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3633 	if (err) {
3634 		dev_info(&vsi->back->pdev->dev,
3635 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3636 			 ring->queue_index, pf_q, err);
3637 		return -ENOMEM;
3638 	}
3639 
3640 	/* set the context in the HMC */
3641 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3642 	if (err) {
3643 		dev_info(&vsi->back->pdev->dev,
3644 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3645 			 ring->queue_index, pf_q, err);
3646 		return -ENOMEM;
3647 	}
3648 
3649 	/* configure Rx buffer alignment */
3650 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3651 		clear_ring_build_skb_enabled(ring);
3652 	else
3653 		set_ring_build_skb_enabled(ring);
3654 
3655 	ring->rx_offset = i40e_rx_offset(ring);
3656 
3657 	/* cache tail for quicker writes, and clear the reg before use */
3658 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3659 	writel(0, ring->tail);
3660 
3661 	if (ring->xsk_pool) {
3662 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3663 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3664 	} else {
3665 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3666 	}
3667 	if (!ok) {
3668 		/* Log this in case the user has forgotten to give the kernel
3669 		 * any buffers, even later in the application.
3670 		 */
3671 		dev_info(&vsi->back->pdev->dev,
3672 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3673 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3674 			 ring->queue_index, pf_q);
3675 	}
3676 
3677 	return 0;
3678 }
3679 
3680 /**
3681  * i40e_vsi_configure_tx - Configure the VSI for Tx
3682  * @vsi: VSI structure describing this set of rings and resources
3683  *
3684  * Configure the Tx VSI for operation.
3685  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3686 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3687 {
3688 	int err = 0;
3689 	u16 i;
3690 
3691 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3692 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3693 
3694 	if (err || !i40e_enabled_xdp_vsi(vsi))
3695 		return err;
3696 
3697 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3698 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3699 
3700 	return err;
3701 }
3702 
3703 /**
3704  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
3705  *
3706  * @vsi: VSI to calculate rx_buf_len from
3707  */
i40e_calculate_vsi_rx_buf_len(struct i40e_vsi * vsi)3708 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
3709 {
3710 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3711 		return I40E_RXBUFFER_2048;
3712 
3713 #if (PAGE_SIZE < 8192)
3714 	if (!I40E_2K_TOO_SMALL_WITH_PADDING && vsi->netdev->mtu <= ETH_DATA_LEN)
3715 		return I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3716 #endif
3717 
3718 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
3719 }
3720 
3721 /**
3722  * i40e_vsi_configure_rx - Configure the VSI for Rx
3723  * @vsi: the VSI being configured
3724  *
3725  * Configure the Rx VSI for operation.
3726  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3727 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3728 {
3729 	int err = 0;
3730 	u16 i;
3731 
3732 	vsi->max_frame = I40E_MAX_RXBUFFER;
3733 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3734 
3735 #if (PAGE_SIZE < 8192)
3736 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3737 	    vsi->netdev->mtu <= ETH_DATA_LEN)
3738 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3739 #endif
3740 
3741 	/* set up individual rings */
3742 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3743 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3744 
3745 	return err;
3746 }
3747 
3748 /**
3749  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3750  * @vsi: ptr to the VSI
3751  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3752 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3753 {
3754 	struct i40e_ring *tx_ring, *rx_ring;
3755 	u16 qoffset, qcount;
3756 	int i, n;
3757 
3758 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3759 		/* Reset the TC information */
3760 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3761 			rx_ring = vsi->rx_rings[i];
3762 			tx_ring = vsi->tx_rings[i];
3763 			rx_ring->dcb_tc = 0;
3764 			tx_ring->dcb_tc = 0;
3765 		}
3766 		return;
3767 	}
3768 
3769 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3770 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3771 			continue;
3772 
3773 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3774 		qcount = vsi->tc_config.tc_info[n].qcount;
3775 		for (i = qoffset; i < (qoffset + qcount); i++) {
3776 			rx_ring = vsi->rx_rings[i];
3777 			tx_ring = vsi->tx_rings[i];
3778 			rx_ring->dcb_tc = n;
3779 			tx_ring->dcb_tc = n;
3780 		}
3781 	}
3782 }
3783 
3784 /**
3785  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3786  * @vsi: ptr to the VSI
3787  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3788 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3789 {
3790 	if (vsi->netdev)
3791 		i40e_set_rx_mode(vsi->netdev);
3792 }
3793 
3794 /**
3795  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3796  * @pf: Pointer to the targeted PF
3797  *
3798  * Set all flow director counters to 0.
3799  */
i40e_reset_fdir_filter_cnt(struct i40e_pf * pf)3800 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3801 {
3802 	pf->fd_tcp4_filter_cnt = 0;
3803 	pf->fd_udp4_filter_cnt = 0;
3804 	pf->fd_sctp4_filter_cnt = 0;
3805 	pf->fd_ip4_filter_cnt = 0;
3806 	pf->fd_tcp6_filter_cnt = 0;
3807 	pf->fd_udp6_filter_cnt = 0;
3808 	pf->fd_sctp6_filter_cnt = 0;
3809 	pf->fd_ip6_filter_cnt = 0;
3810 }
3811 
3812 /**
3813  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3814  * @vsi: Pointer to the targeted VSI
3815  *
3816  * This function replays the hlist on the hw where all the SB Flow Director
3817  * filters were saved.
3818  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3819 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3820 {
3821 	struct i40e_fdir_filter *filter;
3822 	struct i40e_pf *pf = vsi->back;
3823 	struct hlist_node *node;
3824 
3825 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3826 		return;
3827 
3828 	/* Reset FDir counters as we're replaying all existing filters */
3829 	i40e_reset_fdir_filter_cnt(pf);
3830 
3831 	hlist_for_each_entry_safe(filter, node,
3832 				  &pf->fdir_filter_list, fdir_node) {
3833 		i40e_add_del_fdir(vsi, filter, true);
3834 	}
3835 }
3836 
3837 /**
3838  * i40e_vsi_configure - Set up the VSI for action
3839  * @vsi: the VSI being configured
3840  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3841 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3842 {
3843 	int err;
3844 
3845 	i40e_set_vsi_rx_mode(vsi);
3846 	i40e_restore_vlan(vsi);
3847 	i40e_vsi_config_dcb_rings(vsi);
3848 	err = i40e_vsi_configure_tx(vsi);
3849 	if (!err)
3850 		err = i40e_vsi_configure_rx(vsi);
3851 
3852 	return err;
3853 }
3854 
3855 /**
3856  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3857  * @vsi: the VSI being configured
3858  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3859 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3860 {
3861 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3862 	struct i40e_pf *pf = vsi->back;
3863 	struct i40e_hw *hw = &pf->hw;
3864 	u16 vector;
3865 	int i, q;
3866 	u32 qp;
3867 
3868 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3869 	 * and PFINT_LNKLSTn registers, e.g.:
3870 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3871 	 */
3872 	qp = vsi->base_queue;
3873 	vector = vsi->base_vector;
3874 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3875 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3876 
3877 		q_vector->rx.next_update = jiffies + 1;
3878 		q_vector->rx.target_itr =
3879 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3880 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3881 		     q_vector->rx.target_itr >> 1);
3882 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3883 
3884 		q_vector->tx.next_update = jiffies + 1;
3885 		q_vector->tx.target_itr =
3886 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3887 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3888 		     q_vector->tx.target_itr >> 1);
3889 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3890 
3891 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3892 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3893 
3894 		/* begin of linked list for RX queue assigned to this vector */
3895 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3896 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3897 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3898 			u32 val;
3899 
3900 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3901 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3902 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3903 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3904 			      (I40E_QUEUE_TYPE_TX <<
3905 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3906 
3907 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3908 
3909 			if (has_xdp) {
3910 				/* TX queue with next queue set to TX */
3911 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3912 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3913 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3914 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3915 				      (I40E_QUEUE_TYPE_TX <<
3916 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3917 
3918 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3919 			}
3920 			/* TX queue with next RX or end of linked list */
3921 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3922 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3923 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3924 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3925 			      (I40E_QUEUE_TYPE_RX <<
3926 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3927 
3928 			/* Terminate the linked list */
3929 			if (q == (q_vector->num_ringpairs - 1))
3930 				val |= (I40E_QUEUE_END_OF_LIST <<
3931 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3932 
3933 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3934 			qp++;
3935 		}
3936 	}
3937 
3938 	i40e_flush(hw);
3939 }
3940 
3941 /**
3942  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3943  * @pf: pointer to private device data structure
3944  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3945 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3946 {
3947 	struct i40e_hw *hw = &pf->hw;
3948 	u32 val;
3949 
3950 	/* clear things first */
3951 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3952 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3953 
3954 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3955 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3956 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3957 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3958 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3959 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3960 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3961 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3962 
3963 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3964 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3965 
3966 	if (pf->flags & I40E_FLAG_PTP)
3967 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3968 
3969 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3970 
3971 	/* SW_ITR_IDX = 0, but don't change INTENA */
3972 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3973 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3974 
3975 	/* OTHER_ITR_IDX = 0 */
3976 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3977 }
3978 
3979 /**
3980  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3981  * @vsi: the VSI being configured
3982  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3983 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3984 {
3985 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3986 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3987 	struct i40e_pf *pf = vsi->back;
3988 	struct i40e_hw *hw = &pf->hw;
3989 
3990 	/* set the ITR configuration */
3991 	q_vector->rx.next_update = jiffies + 1;
3992 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3993 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3994 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3995 	q_vector->tx.next_update = jiffies + 1;
3996 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3997 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3998 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3999 
4000 	i40e_enable_misc_int_causes(pf);
4001 
4002 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4003 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4004 
4005 	/* Associate the queue pair to the vector and enable the queue
4006 	 * interrupt RX queue in linked list with next queue set to TX
4007 	 */
4008 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4009 
4010 	if (i40e_enabled_xdp_vsi(vsi)) {
4011 		/* TX queue in linked list with next queue set to TX */
4012 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4013 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4014 	}
4015 
4016 	/* last TX queue so the next RX queue doesn't matter */
4017 	wr32(hw, I40E_QINT_TQCTL(0),
4018 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4019 	i40e_flush(hw);
4020 }
4021 
4022 /**
4023  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4024  * @pf: board private structure
4025  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)4026 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4027 {
4028 	struct i40e_hw *hw = &pf->hw;
4029 
4030 	wr32(hw, I40E_PFINT_DYN_CTL0,
4031 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4032 	i40e_flush(hw);
4033 }
4034 
4035 /**
4036  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4037  * @pf: board private structure
4038  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)4039 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4040 {
4041 	struct i40e_hw *hw = &pf->hw;
4042 	u32 val;
4043 
4044 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4045 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4046 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4047 
4048 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4049 	i40e_flush(hw);
4050 }
4051 
4052 /**
4053  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4054  * @irq: interrupt number
4055  * @data: pointer to a q_vector
4056  **/
i40e_msix_clean_rings(int irq,void * data)4057 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4058 {
4059 	struct i40e_q_vector *q_vector = data;
4060 
4061 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4062 		return IRQ_HANDLED;
4063 
4064 	napi_schedule_irqoff(&q_vector->napi);
4065 
4066 	return IRQ_HANDLED;
4067 }
4068 
4069 /**
4070  * i40e_irq_affinity_notify - Callback for affinity changes
4071  * @notify: context as to what irq was changed
4072  * @mask: the new affinity mask
4073  *
4074  * This is a callback function used by the irq_set_affinity_notifier function
4075  * so that we may register to receive changes to the irq affinity masks.
4076  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)4077 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4078 				     const cpumask_t *mask)
4079 {
4080 	struct i40e_q_vector *q_vector =
4081 		container_of(notify, struct i40e_q_vector, affinity_notify);
4082 
4083 	cpumask_copy(&q_vector->affinity_mask, mask);
4084 }
4085 
4086 /**
4087  * i40e_irq_affinity_release - Callback for affinity notifier release
4088  * @ref: internal core kernel usage
4089  *
4090  * This is a callback function used by the irq_set_affinity_notifier function
4091  * to inform the current notification subscriber that they will no longer
4092  * receive notifications.
4093  **/
i40e_irq_affinity_release(struct kref * ref)4094 static void i40e_irq_affinity_release(struct kref *ref) {}
4095 
4096 /**
4097  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4098  * @vsi: the VSI being configured
4099  * @basename: name for the vector
4100  *
4101  * Allocates MSI-X vectors and requests interrupts from the kernel.
4102  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)4103 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4104 {
4105 	int q_vectors = vsi->num_q_vectors;
4106 	struct i40e_pf *pf = vsi->back;
4107 	int base = vsi->base_vector;
4108 	int rx_int_idx = 0;
4109 	int tx_int_idx = 0;
4110 	int vector, err;
4111 	int irq_num;
4112 	int cpu;
4113 
4114 	for (vector = 0; vector < q_vectors; vector++) {
4115 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4116 
4117 		irq_num = pf->msix_entries[base + vector].vector;
4118 
4119 		if (q_vector->tx.ring && q_vector->rx.ring) {
4120 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4121 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4122 			tx_int_idx++;
4123 		} else if (q_vector->rx.ring) {
4124 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4125 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4126 		} else if (q_vector->tx.ring) {
4127 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4128 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4129 		} else {
4130 			/* skip this unused q_vector */
4131 			continue;
4132 		}
4133 		err = request_irq(irq_num,
4134 				  vsi->irq_handler,
4135 				  0,
4136 				  q_vector->name,
4137 				  q_vector);
4138 		if (err) {
4139 			dev_info(&pf->pdev->dev,
4140 				 "MSIX request_irq failed, error: %d\n", err);
4141 			goto free_queue_irqs;
4142 		}
4143 
4144 		/* register for affinity change notifications */
4145 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4146 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4147 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4148 		/* Spread affinity hints out across online CPUs.
4149 		 *
4150 		 * get_cpu_mask returns a static constant mask with
4151 		 * a permanent lifetime so it's ok to pass to
4152 		 * irq_update_affinity_hint without making a copy.
4153 		 */
4154 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4155 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4156 	}
4157 
4158 	vsi->irqs_ready = true;
4159 	return 0;
4160 
4161 free_queue_irqs:
4162 	while (vector) {
4163 		vector--;
4164 		irq_num = pf->msix_entries[base + vector].vector;
4165 		irq_set_affinity_notifier(irq_num, NULL);
4166 		irq_update_affinity_hint(irq_num, NULL);
4167 		free_irq(irq_num, &vsi->q_vectors[vector]);
4168 	}
4169 	return err;
4170 }
4171 
4172 /**
4173  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4174  * @vsi: the VSI being un-configured
4175  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)4176 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4177 {
4178 	struct i40e_pf *pf = vsi->back;
4179 	struct i40e_hw *hw = &pf->hw;
4180 	int base = vsi->base_vector;
4181 	int i;
4182 
4183 	/* disable interrupt causation from each queue */
4184 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4185 		u32 val;
4186 
4187 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4188 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4189 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4190 
4191 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4192 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4193 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4194 
4195 		if (!i40e_enabled_xdp_vsi(vsi))
4196 			continue;
4197 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4198 	}
4199 
4200 	/* disable each interrupt */
4201 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4202 		for (i = vsi->base_vector;
4203 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4204 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4205 
4206 		i40e_flush(hw);
4207 		for (i = 0; i < vsi->num_q_vectors; i++)
4208 			synchronize_irq(pf->msix_entries[i + base].vector);
4209 	} else {
4210 		/* Legacy and MSI mode - this stops all interrupt handling */
4211 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4212 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4213 		i40e_flush(hw);
4214 		synchronize_irq(pf->pdev->irq);
4215 	}
4216 }
4217 
4218 /**
4219  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4220  * @vsi: the VSI being configured
4221  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)4222 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4223 {
4224 	struct i40e_pf *pf = vsi->back;
4225 	int i;
4226 
4227 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4228 		for (i = 0; i < vsi->num_q_vectors; i++)
4229 			i40e_irq_dynamic_enable(vsi, i);
4230 	} else {
4231 		i40e_irq_dynamic_enable_icr0(pf);
4232 	}
4233 
4234 	i40e_flush(&pf->hw);
4235 	return 0;
4236 }
4237 
4238 /**
4239  * i40e_free_misc_vector - Free the vector that handles non-queue events
4240  * @pf: board private structure
4241  **/
i40e_free_misc_vector(struct i40e_pf * pf)4242 static void i40e_free_misc_vector(struct i40e_pf *pf)
4243 {
4244 	/* Disable ICR 0 */
4245 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4246 	i40e_flush(&pf->hw);
4247 
4248 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4249 		free_irq(pf->msix_entries[0].vector, pf);
4250 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4251 	}
4252 }
4253 
4254 /**
4255  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4256  * @irq: interrupt number
4257  * @data: pointer to a q_vector
4258  *
4259  * This is the handler used for all MSI/Legacy interrupts, and deals
4260  * with both queue and non-queue interrupts.  This is also used in
4261  * MSIX mode to handle the non-queue interrupts.
4262  **/
i40e_intr(int irq,void * data)4263 static irqreturn_t i40e_intr(int irq, void *data)
4264 {
4265 	struct i40e_pf *pf = (struct i40e_pf *)data;
4266 	struct i40e_hw *hw = &pf->hw;
4267 	irqreturn_t ret = IRQ_NONE;
4268 	u32 icr0, icr0_remaining;
4269 	u32 val, ena_mask;
4270 
4271 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4272 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4273 
4274 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4275 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4276 		goto enable_intr;
4277 
4278 	/* if interrupt but no bits showing, must be SWINT */
4279 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4280 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4281 		pf->sw_int_count++;
4282 
4283 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4284 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4285 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4286 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4287 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4288 	}
4289 
4290 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4291 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4292 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4293 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4294 
4295 		/* We do not have a way to disarm Queue causes while leaving
4296 		 * interrupt enabled for all other causes, ideally
4297 		 * interrupt should be disabled while we are in NAPI but
4298 		 * this is not a performance path and napi_schedule()
4299 		 * can deal with rescheduling.
4300 		 */
4301 		if (!test_bit(__I40E_DOWN, pf->state))
4302 			napi_schedule_irqoff(&q_vector->napi);
4303 	}
4304 
4305 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4306 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4307 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4308 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4309 	}
4310 
4311 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4312 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4313 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4314 	}
4315 
4316 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4317 		/* disable any further VFLR event notifications */
4318 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4319 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4320 
4321 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4322 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4323 		} else {
4324 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4325 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4326 		}
4327 	}
4328 
4329 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4330 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4331 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4332 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4333 		val = rd32(hw, I40E_GLGEN_RSTAT);
4334 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4335 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4336 		if (val == I40E_RESET_CORER) {
4337 			pf->corer_count++;
4338 		} else if (val == I40E_RESET_GLOBR) {
4339 			pf->globr_count++;
4340 		} else if (val == I40E_RESET_EMPR) {
4341 			pf->empr_count++;
4342 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4343 		}
4344 	}
4345 
4346 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4347 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4348 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4349 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4350 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4351 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4352 	}
4353 
4354 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4355 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4356 
4357 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4358 			schedule_work(&pf->ptp_extts0_work);
4359 
4360 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4361 			i40e_ptp_tx_hwtstamp(pf);
4362 
4363 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4364 	}
4365 
4366 	/* If a critical error is pending we have no choice but to reset the
4367 	 * device.
4368 	 * Report and mask out any remaining unexpected interrupts.
4369 	 */
4370 	icr0_remaining = icr0 & ena_mask;
4371 	if (icr0_remaining) {
4372 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4373 			 icr0_remaining);
4374 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4375 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4376 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4377 			dev_info(&pf->pdev->dev, "device will be reset\n");
4378 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4379 			i40e_service_event_schedule(pf);
4380 		}
4381 		ena_mask &= ~icr0_remaining;
4382 	}
4383 	ret = IRQ_HANDLED;
4384 
4385 enable_intr:
4386 	/* re-enable interrupt causes */
4387 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4388 	if (!test_bit(__I40E_DOWN, pf->state) ||
4389 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4390 		i40e_service_event_schedule(pf);
4391 		i40e_irq_dynamic_enable_icr0(pf);
4392 	}
4393 
4394 	return ret;
4395 }
4396 
4397 /**
4398  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4399  * @tx_ring:  tx ring to clean
4400  * @budget:   how many cleans we're allowed
4401  *
4402  * Returns true if there's any budget left (e.g. the clean is finished)
4403  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4404 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4405 {
4406 	struct i40e_vsi *vsi = tx_ring->vsi;
4407 	u16 i = tx_ring->next_to_clean;
4408 	struct i40e_tx_buffer *tx_buf;
4409 	struct i40e_tx_desc *tx_desc;
4410 
4411 	tx_buf = &tx_ring->tx_bi[i];
4412 	tx_desc = I40E_TX_DESC(tx_ring, i);
4413 	i -= tx_ring->count;
4414 
4415 	do {
4416 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4417 
4418 		/* if next_to_watch is not set then there is no work pending */
4419 		if (!eop_desc)
4420 			break;
4421 
4422 		/* prevent any other reads prior to eop_desc */
4423 		smp_rmb();
4424 
4425 		/* if the descriptor isn't done, no work yet to do */
4426 		if (!(eop_desc->cmd_type_offset_bsz &
4427 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4428 			break;
4429 
4430 		/* clear next_to_watch to prevent false hangs */
4431 		tx_buf->next_to_watch = NULL;
4432 
4433 		tx_desc->buffer_addr = 0;
4434 		tx_desc->cmd_type_offset_bsz = 0;
4435 		/* move past filter desc */
4436 		tx_buf++;
4437 		tx_desc++;
4438 		i++;
4439 		if (unlikely(!i)) {
4440 			i -= tx_ring->count;
4441 			tx_buf = tx_ring->tx_bi;
4442 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4443 		}
4444 		/* unmap skb header data */
4445 		dma_unmap_single(tx_ring->dev,
4446 				 dma_unmap_addr(tx_buf, dma),
4447 				 dma_unmap_len(tx_buf, len),
4448 				 DMA_TO_DEVICE);
4449 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4450 			kfree(tx_buf->raw_buf);
4451 
4452 		tx_buf->raw_buf = NULL;
4453 		tx_buf->tx_flags = 0;
4454 		tx_buf->next_to_watch = NULL;
4455 		dma_unmap_len_set(tx_buf, len, 0);
4456 		tx_desc->buffer_addr = 0;
4457 		tx_desc->cmd_type_offset_bsz = 0;
4458 
4459 		/* move us past the eop_desc for start of next FD desc */
4460 		tx_buf++;
4461 		tx_desc++;
4462 		i++;
4463 		if (unlikely(!i)) {
4464 			i -= tx_ring->count;
4465 			tx_buf = tx_ring->tx_bi;
4466 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4467 		}
4468 
4469 		/* update budget accounting */
4470 		budget--;
4471 	} while (likely(budget));
4472 
4473 	i += tx_ring->count;
4474 	tx_ring->next_to_clean = i;
4475 
4476 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4477 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4478 
4479 	return budget > 0;
4480 }
4481 
4482 /**
4483  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4484  * @irq: interrupt number
4485  * @data: pointer to a q_vector
4486  **/
i40e_fdir_clean_ring(int irq,void * data)4487 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4488 {
4489 	struct i40e_q_vector *q_vector = data;
4490 	struct i40e_vsi *vsi;
4491 
4492 	if (!q_vector->tx.ring)
4493 		return IRQ_HANDLED;
4494 
4495 	vsi = q_vector->tx.ring->vsi;
4496 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4497 
4498 	return IRQ_HANDLED;
4499 }
4500 
4501 /**
4502  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4503  * @vsi: the VSI being configured
4504  * @v_idx: vector index
4505  * @qp_idx: queue pair index
4506  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4507 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4508 {
4509 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4510 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4511 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4512 
4513 	tx_ring->q_vector = q_vector;
4514 	tx_ring->next = q_vector->tx.ring;
4515 	q_vector->tx.ring = tx_ring;
4516 	q_vector->tx.count++;
4517 
4518 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4519 	if (i40e_enabled_xdp_vsi(vsi)) {
4520 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4521 
4522 		xdp_ring->q_vector = q_vector;
4523 		xdp_ring->next = q_vector->tx.ring;
4524 		q_vector->tx.ring = xdp_ring;
4525 		q_vector->tx.count++;
4526 	}
4527 
4528 	rx_ring->q_vector = q_vector;
4529 	rx_ring->next = q_vector->rx.ring;
4530 	q_vector->rx.ring = rx_ring;
4531 	q_vector->rx.count++;
4532 }
4533 
4534 /**
4535  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4536  * @vsi: the VSI being configured
4537  *
4538  * This function maps descriptor rings to the queue-specific vectors
4539  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4540  * one vector per queue pair, but on a constrained vector budget, we
4541  * group the queue pairs as "efficiently" as possible.
4542  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4543 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4544 {
4545 	int qp_remaining = vsi->num_queue_pairs;
4546 	int q_vectors = vsi->num_q_vectors;
4547 	int num_ringpairs;
4548 	int v_start = 0;
4549 	int qp_idx = 0;
4550 
4551 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4552 	 * group them so there are multiple queues per vector.
4553 	 * It is also important to go through all the vectors available to be
4554 	 * sure that if we don't use all the vectors, that the remaining vectors
4555 	 * are cleared. This is especially important when decreasing the
4556 	 * number of queues in use.
4557 	 */
4558 	for (; v_start < q_vectors; v_start++) {
4559 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4560 
4561 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4562 
4563 		q_vector->num_ringpairs = num_ringpairs;
4564 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4565 
4566 		q_vector->rx.count = 0;
4567 		q_vector->tx.count = 0;
4568 		q_vector->rx.ring = NULL;
4569 		q_vector->tx.ring = NULL;
4570 
4571 		while (num_ringpairs--) {
4572 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4573 			qp_idx++;
4574 			qp_remaining--;
4575 		}
4576 	}
4577 }
4578 
4579 /**
4580  * i40e_vsi_request_irq - Request IRQ from the OS
4581  * @vsi: the VSI being configured
4582  * @basename: name for the vector
4583  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4584 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4585 {
4586 	struct i40e_pf *pf = vsi->back;
4587 	int err;
4588 
4589 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4590 		err = i40e_vsi_request_irq_msix(vsi, basename);
4591 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4592 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4593 				  pf->int_name, pf);
4594 	else
4595 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4596 				  pf->int_name, pf);
4597 
4598 	if (err)
4599 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4600 
4601 	return err;
4602 }
4603 
4604 #ifdef CONFIG_NET_POLL_CONTROLLER
4605 /**
4606  * i40e_netpoll - A Polling 'interrupt' handler
4607  * @netdev: network interface device structure
4608  *
4609  * This is used by netconsole to send skbs without having to re-enable
4610  * interrupts.  It's not called while the normal interrupt routine is executing.
4611  **/
i40e_netpoll(struct net_device * netdev)4612 static void i40e_netpoll(struct net_device *netdev)
4613 {
4614 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4615 	struct i40e_vsi *vsi = np->vsi;
4616 	struct i40e_pf *pf = vsi->back;
4617 	int i;
4618 
4619 	/* if interface is down do nothing */
4620 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4621 		return;
4622 
4623 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4624 		for (i = 0; i < vsi->num_q_vectors; i++)
4625 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4626 	} else {
4627 		i40e_intr(pf->pdev->irq, netdev);
4628 	}
4629 }
4630 #endif
4631 
4632 #define I40E_QTX_ENA_WAIT_COUNT 50
4633 
4634 /**
4635  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4636  * @pf: the PF being configured
4637  * @pf_q: the PF queue
4638  * @enable: enable or disable state of the queue
4639  *
4640  * This routine will wait for the given Tx queue of the PF to reach the
4641  * enabled or disabled state.
4642  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4643  * multiple retries; else will return 0 in case of success.
4644  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4645 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4646 {
4647 	int i;
4648 	u32 tx_reg;
4649 
4650 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4651 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4652 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4653 			break;
4654 
4655 		usleep_range(10, 20);
4656 	}
4657 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4658 		return -ETIMEDOUT;
4659 
4660 	return 0;
4661 }
4662 
4663 /**
4664  * i40e_control_tx_q - Start or stop a particular Tx queue
4665  * @pf: the PF structure
4666  * @pf_q: the PF queue to configure
4667  * @enable: start or stop the queue
4668  *
4669  * This function enables or disables a single queue. Note that any delay
4670  * required after the operation is expected to be handled by the caller of
4671  * this function.
4672  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4673 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4674 {
4675 	struct i40e_hw *hw = &pf->hw;
4676 	u32 tx_reg;
4677 	int i;
4678 
4679 	/* warn the TX unit of coming changes */
4680 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4681 	if (!enable)
4682 		usleep_range(10, 20);
4683 
4684 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4685 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4686 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4687 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4688 			break;
4689 		usleep_range(1000, 2000);
4690 	}
4691 
4692 	/* Skip if the queue is already in the requested state */
4693 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4694 		return;
4695 
4696 	/* turn on/off the queue */
4697 	if (enable) {
4698 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4699 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4700 	} else {
4701 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4702 	}
4703 
4704 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4705 }
4706 
4707 /**
4708  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4709  * @seid: VSI SEID
4710  * @pf: the PF structure
4711  * @pf_q: the PF queue to configure
4712  * @is_xdp: true if the queue is used for XDP
4713  * @enable: start or stop the queue
4714  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4715 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4716 			   bool is_xdp, bool enable)
4717 {
4718 	int ret;
4719 
4720 	i40e_control_tx_q(pf, pf_q, enable);
4721 
4722 	/* wait for the change to finish */
4723 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4724 	if (ret) {
4725 		dev_info(&pf->pdev->dev,
4726 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4727 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4728 			 (enable ? "en" : "dis"));
4729 	}
4730 
4731 	return ret;
4732 }
4733 
4734 /**
4735  * i40e_vsi_enable_tx - Start a VSI's rings
4736  * @vsi: the VSI being configured
4737  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4738 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4739 {
4740 	struct i40e_pf *pf = vsi->back;
4741 	int i, pf_q, ret = 0;
4742 
4743 	pf_q = vsi->base_queue;
4744 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4745 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4746 					     pf_q,
4747 					     false /*is xdp*/, true);
4748 		if (ret)
4749 			break;
4750 
4751 		if (!i40e_enabled_xdp_vsi(vsi))
4752 			continue;
4753 
4754 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4755 					     pf_q + vsi->alloc_queue_pairs,
4756 					     true /*is xdp*/, true);
4757 		if (ret)
4758 			break;
4759 	}
4760 	return ret;
4761 }
4762 
4763 /**
4764  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4765  * @pf: the PF being configured
4766  * @pf_q: the PF queue
4767  * @enable: enable or disable state of the queue
4768  *
4769  * This routine will wait for the given Rx queue of the PF to reach the
4770  * enabled or disabled state.
4771  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4772  * multiple retries; else will return 0 in case of success.
4773  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4774 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4775 {
4776 	int i;
4777 	u32 rx_reg;
4778 
4779 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4780 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4781 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4782 			break;
4783 
4784 		usleep_range(10, 20);
4785 	}
4786 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4787 		return -ETIMEDOUT;
4788 
4789 	return 0;
4790 }
4791 
4792 /**
4793  * i40e_control_rx_q - Start or stop a particular Rx queue
4794  * @pf: the PF structure
4795  * @pf_q: the PF queue to configure
4796  * @enable: start or stop the queue
4797  *
4798  * This function enables or disables a single queue. Note that
4799  * any delay required after the operation is expected to be
4800  * handled by the caller of this function.
4801  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4802 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4803 {
4804 	struct i40e_hw *hw = &pf->hw;
4805 	u32 rx_reg;
4806 	int i;
4807 
4808 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4809 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4810 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4811 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4812 			break;
4813 		usleep_range(1000, 2000);
4814 	}
4815 
4816 	/* Skip if the queue is already in the requested state */
4817 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4818 		return;
4819 
4820 	/* turn on/off the queue */
4821 	if (enable)
4822 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4823 	else
4824 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4825 
4826 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4827 }
4828 
4829 /**
4830  * i40e_control_wait_rx_q
4831  * @pf: the PF structure
4832  * @pf_q: queue being configured
4833  * @enable: start or stop the rings
4834  *
4835  * This function enables or disables a single queue along with waiting
4836  * for the change to finish. The caller of this function should handle
4837  * the delays needed in the case of disabling queues.
4838  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4839 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4840 {
4841 	int ret = 0;
4842 
4843 	i40e_control_rx_q(pf, pf_q, enable);
4844 
4845 	/* wait for the change to finish */
4846 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4847 	if (ret)
4848 		return ret;
4849 
4850 	return ret;
4851 }
4852 
4853 /**
4854  * i40e_vsi_enable_rx - Start a VSI's rings
4855  * @vsi: the VSI being configured
4856  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4857 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4858 {
4859 	struct i40e_pf *pf = vsi->back;
4860 	int i, pf_q, ret = 0;
4861 
4862 	pf_q = vsi->base_queue;
4863 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4864 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4865 		if (ret) {
4866 			dev_info(&pf->pdev->dev,
4867 				 "VSI seid %d Rx ring %d enable timeout\n",
4868 				 vsi->seid, pf_q);
4869 			break;
4870 		}
4871 	}
4872 
4873 	return ret;
4874 }
4875 
4876 /**
4877  * i40e_vsi_start_rings - Start a VSI's rings
4878  * @vsi: the VSI being configured
4879  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4880 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4881 {
4882 	int ret = 0;
4883 
4884 	/* do rx first for enable and last for disable */
4885 	ret = i40e_vsi_enable_rx(vsi);
4886 	if (ret)
4887 		return ret;
4888 	ret = i40e_vsi_enable_tx(vsi);
4889 
4890 	return ret;
4891 }
4892 
4893 #define I40E_DISABLE_TX_GAP_MSEC	50
4894 
4895 /**
4896  * i40e_vsi_stop_rings - Stop a VSI's rings
4897  * @vsi: the VSI being configured
4898  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4899 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4900 {
4901 	struct i40e_pf *pf = vsi->back;
4902 	int pf_q, err, q_end;
4903 
4904 	/* When port TX is suspended, don't wait */
4905 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4906 		return i40e_vsi_stop_rings_no_wait(vsi);
4907 
4908 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4909 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4910 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4911 
4912 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4913 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4914 		if (err)
4915 			dev_info(&pf->pdev->dev,
4916 				 "VSI seid %d Rx ring %d disable timeout\n",
4917 				 vsi->seid, pf_q);
4918 	}
4919 
4920 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4921 	pf_q = vsi->base_queue;
4922 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4923 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4924 
4925 	i40e_vsi_wait_queues_disabled(vsi);
4926 }
4927 
4928 /**
4929  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4930  * @vsi: the VSI being shutdown
4931  *
4932  * This function stops all the rings for a VSI but does not delay to verify
4933  * that rings have been disabled. It is expected that the caller is shutting
4934  * down multiple VSIs at once and will delay together for all the VSIs after
4935  * initiating the shutdown. This is particularly useful for shutting down lots
4936  * of VFs together. Otherwise, a large delay can be incurred while configuring
4937  * each VSI in serial.
4938  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4939 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4940 {
4941 	struct i40e_pf *pf = vsi->back;
4942 	int i, pf_q;
4943 
4944 	pf_q = vsi->base_queue;
4945 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4946 		i40e_control_tx_q(pf, pf_q, false);
4947 		i40e_control_rx_q(pf, pf_q, false);
4948 	}
4949 }
4950 
4951 /**
4952  * i40e_vsi_free_irq - Free the irq association with the OS
4953  * @vsi: the VSI being configured
4954  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4955 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4956 {
4957 	struct i40e_pf *pf = vsi->back;
4958 	struct i40e_hw *hw = &pf->hw;
4959 	int base = vsi->base_vector;
4960 	u32 val, qp;
4961 	int i;
4962 
4963 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4964 		if (!vsi->q_vectors)
4965 			return;
4966 
4967 		if (!vsi->irqs_ready)
4968 			return;
4969 
4970 		vsi->irqs_ready = false;
4971 		for (i = 0; i < vsi->num_q_vectors; i++) {
4972 			int irq_num;
4973 			u16 vector;
4974 
4975 			vector = i + base;
4976 			irq_num = pf->msix_entries[vector].vector;
4977 
4978 			/* free only the irqs that were actually requested */
4979 			if (!vsi->q_vectors[i] ||
4980 			    !vsi->q_vectors[i]->num_ringpairs)
4981 				continue;
4982 
4983 			/* clear the affinity notifier in the IRQ descriptor */
4984 			irq_set_affinity_notifier(irq_num, NULL);
4985 			/* remove our suggested affinity mask for this IRQ */
4986 			irq_update_affinity_hint(irq_num, NULL);
4987 			free_irq(irq_num, vsi->q_vectors[i]);
4988 
4989 			/* Tear down the interrupt queue link list
4990 			 *
4991 			 * We know that they come in pairs and always
4992 			 * the Rx first, then the Tx.  To clear the
4993 			 * link list, stick the EOL value into the
4994 			 * next_q field of the registers.
4995 			 */
4996 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4997 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4998 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4999 			val |= I40E_QUEUE_END_OF_LIST
5000 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5001 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5002 
5003 			while (qp != I40E_QUEUE_END_OF_LIST) {
5004 				u32 next;
5005 
5006 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5007 
5008 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5009 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5010 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5011 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5012 
5013 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5014 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5015 
5016 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5017 
5018 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5019 
5020 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5021 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5022 
5023 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5024 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5025 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5026 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5027 
5028 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5029 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5030 
5031 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5032 				qp = next;
5033 			}
5034 		}
5035 	} else {
5036 		free_irq(pf->pdev->irq, pf);
5037 
5038 		val = rd32(hw, I40E_PFINT_LNKLST0);
5039 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5040 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5041 		val |= I40E_QUEUE_END_OF_LIST
5042 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5043 		wr32(hw, I40E_PFINT_LNKLST0, val);
5044 
5045 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5046 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5047 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5048 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5049 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5050 
5051 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5052 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5053 
5054 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5055 
5056 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5057 
5058 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5059 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5060 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5061 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5062 
5063 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5064 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5065 
5066 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5067 	}
5068 }
5069 
5070 /**
5071  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5072  * @vsi: the VSI being configured
5073  * @v_idx: Index of vector to be freed
5074  *
5075  * This function frees the memory allocated to the q_vector.  In addition if
5076  * NAPI is enabled it will delete any references to the NAPI struct prior
5077  * to freeing the q_vector.
5078  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)5079 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5080 {
5081 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5082 	struct i40e_ring *ring;
5083 
5084 	if (!q_vector)
5085 		return;
5086 
5087 	/* disassociate q_vector from rings */
5088 	i40e_for_each_ring(ring, q_vector->tx)
5089 		ring->q_vector = NULL;
5090 
5091 	i40e_for_each_ring(ring, q_vector->rx)
5092 		ring->q_vector = NULL;
5093 
5094 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5095 	if (vsi->netdev)
5096 		netif_napi_del(&q_vector->napi);
5097 
5098 	vsi->q_vectors[v_idx] = NULL;
5099 
5100 	kfree_rcu(q_vector, rcu);
5101 }
5102 
5103 /**
5104  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5105  * @vsi: the VSI being un-configured
5106  *
5107  * This frees the memory allocated to the q_vectors and
5108  * deletes references to the NAPI struct.
5109  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)5110 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5111 {
5112 	int v_idx;
5113 
5114 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5115 		i40e_free_q_vector(vsi, v_idx);
5116 }
5117 
5118 /**
5119  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5120  * @pf: board private structure
5121  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)5122 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5123 {
5124 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5125 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5126 		pci_disable_msix(pf->pdev);
5127 		kfree(pf->msix_entries);
5128 		pf->msix_entries = NULL;
5129 		kfree(pf->irq_pile);
5130 		pf->irq_pile = NULL;
5131 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5132 		pci_disable_msi(pf->pdev);
5133 	}
5134 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5135 }
5136 
5137 /**
5138  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5139  * @pf: board private structure
5140  *
5141  * We go through and clear interrupt specific resources and reset the structure
5142  * to pre-load conditions
5143  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)5144 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5145 {
5146 	int i;
5147 
5148 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5149 		i40e_free_misc_vector(pf);
5150 
5151 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5152 		      I40E_IWARP_IRQ_PILE_ID);
5153 
5154 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5155 	for (i = 0; i < pf->num_alloc_vsi; i++)
5156 		if (pf->vsi[i])
5157 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5158 	i40e_reset_interrupt_capability(pf);
5159 }
5160 
5161 /**
5162  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5163  * @vsi: the VSI being configured
5164  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)5165 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5166 {
5167 	int q_idx;
5168 
5169 	if (!vsi->netdev)
5170 		return;
5171 
5172 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5173 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5174 
5175 		if (q_vector->rx.ring || q_vector->tx.ring)
5176 			napi_enable(&q_vector->napi);
5177 	}
5178 }
5179 
5180 /**
5181  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5182  * @vsi: the VSI being configured
5183  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)5184 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5185 {
5186 	int q_idx;
5187 
5188 	if (!vsi->netdev)
5189 		return;
5190 
5191 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5192 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5193 
5194 		if (q_vector->rx.ring || q_vector->tx.ring)
5195 			napi_disable(&q_vector->napi);
5196 	}
5197 }
5198 
5199 /**
5200  * i40e_vsi_close - Shut down a VSI
5201  * @vsi: the vsi to be quelled
5202  **/
i40e_vsi_close(struct i40e_vsi * vsi)5203 static void i40e_vsi_close(struct i40e_vsi *vsi)
5204 {
5205 	struct i40e_pf *pf = vsi->back;
5206 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5207 		i40e_down(vsi);
5208 	i40e_vsi_free_irq(vsi);
5209 	i40e_vsi_free_tx_resources(vsi);
5210 	i40e_vsi_free_rx_resources(vsi);
5211 	vsi->current_netdev_flags = 0;
5212 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5213 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5214 		set_bit(__I40E_CLIENT_RESET, pf->state);
5215 }
5216 
5217 /**
5218  * i40e_quiesce_vsi - Pause a given VSI
5219  * @vsi: the VSI being paused
5220  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)5221 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5222 {
5223 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5224 		return;
5225 
5226 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5227 	if (vsi->netdev && netif_running(vsi->netdev))
5228 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5229 	else
5230 		i40e_vsi_close(vsi);
5231 }
5232 
5233 /**
5234  * i40e_unquiesce_vsi - Resume a given VSI
5235  * @vsi: the VSI being resumed
5236  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)5237 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5238 {
5239 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5240 		return;
5241 
5242 	if (vsi->netdev && netif_running(vsi->netdev))
5243 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5244 	else
5245 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5246 }
5247 
5248 /**
5249  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5250  * @pf: the PF
5251  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)5252 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5253 {
5254 	int v;
5255 
5256 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5257 		if (pf->vsi[v])
5258 			i40e_quiesce_vsi(pf->vsi[v]);
5259 	}
5260 }
5261 
5262 /**
5263  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5264  * @pf: the PF
5265  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5266 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5267 {
5268 	int v;
5269 
5270 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5271 		if (pf->vsi[v])
5272 			i40e_unquiesce_vsi(pf->vsi[v]);
5273 	}
5274 }
5275 
5276 /**
5277  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5278  * @vsi: the VSI being configured
5279  *
5280  * Wait until all queues on a given VSI have been disabled.
5281  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5282 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5283 {
5284 	struct i40e_pf *pf = vsi->back;
5285 	int i, pf_q, ret;
5286 
5287 	pf_q = vsi->base_queue;
5288 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5289 		/* Check and wait for the Tx queue */
5290 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5291 		if (ret) {
5292 			dev_info(&pf->pdev->dev,
5293 				 "VSI seid %d Tx ring %d disable timeout\n",
5294 				 vsi->seid, pf_q);
5295 			return ret;
5296 		}
5297 
5298 		if (!i40e_enabled_xdp_vsi(vsi))
5299 			goto wait_rx;
5300 
5301 		/* Check and wait for the XDP Tx queue */
5302 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5303 				       false);
5304 		if (ret) {
5305 			dev_info(&pf->pdev->dev,
5306 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5307 				 vsi->seid, pf_q);
5308 			return ret;
5309 		}
5310 wait_rx:
5311 		/* Check and wait for the Rx queue */
5312 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5313 		if (ret) {
5314 			dev_info(&pf->pdev->dev,
5315 				 "VSI seid %d Rx ring %d disable timeout\n",
5316 				 vsi->seid, pf_q);
5317 			return ret;
5318 		}
5319 	}
5320 
5321 	return 0;
5322 }
5323 
5324 #ifdef CONFIG_I40E_DCB
5325 /**
5326  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5327  * @pf: the PF
5328  *
5329  * This function waits for the queues to be in disabled state for all the
5330  * VSIs that are managed by this PF.
5331  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5332 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5333 {
5334 	int v, ret = 0;
5335 
5336 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5337 		if (pf->vsi[v]) {
5338 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5339 			if (ret)
5340 				break;
5341 		}
5342 	}
5343 
5344 	return ret;
5345 }
5346 
5347 #endif
5348 
5349 /**
5350  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5351  * @pf: pointer to PF
5352  *
5353  * Get TC map for ISCSI PF type that will include iSCSI TC
5354  * and LAN TC.
5355  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5356 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5357 {
5358 	struct i40e_dcb_app_priority_table app;
5359 	struct i40e_hw *hw = &pf->hw;
5360 	u8 enabled_tc = 1; /* TC0 is always enabled */
5361 	u8 tc, i;
5362 	/* Get the iSCSI APP TLV */
5363 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5364 
5365 	for (i = 0; i < dcbcfg->numapps; i++) {
5366 		app = dcbcfg->app[i];
5367 		if (app.selector == I40E_APP_SEL_TCPIP &&
5368 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5369 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5370 			enabled_tc |= BIT(tc);
5371 			break;
5372 		}
5373 	}
5374 
5375 	return enabled_tc;
5376 }
5377 
5378 /**
5379  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5380  * @dcbcfg: the corresponding DCBx configuration structure
5381  *
5382  * Return the number of TCs from given DCBx configuration
5383  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5384 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5385 {
5386 	int i, tc_unused = 0;
5387 	u8 num_tc = 0;
5388 	u8 ret = 0;
5389 
5390 	/* Scan the ETS Config Priority Table to find
5391 	 * traffic class enabled for a given priority
5392 	 * and create a bitmask of enabled TCs
5393 	 */
5394 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5395 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5396 
5397 	/* Now scan the bitmask to check for
5398 	 * contiguous TCs starting with TC0
5399 	 */
5400 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5401 		if (num_tc & BIT(i)) {
5402 			if (!tc_unused) {
5403 				ret++;
5404 			} else {
5405 				pr_err("Non-contiguous TC - Disabling DCB\n");
5406 				return 1;
5407 			}
5408 		} else {
5409 			tc_unused = 1;
5410 		}
5411 	}
5412 
5413 	/* There is always at least TC0 */
5414 	if (!ret)
5415 		ret = 1;
5416 
5417 	return ret;
5418 }
5419 
5420 /**
5421  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5422  * @dcbcfg: the corresponding DCBx configuration structure
5423  *
5424  * Query the current DCB configuration and return the number of
5425  * traffic classes enabled from the given DCBX config
5426  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5427 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5428 {
5429 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5430 	u8 enabled_tc = 1;
5431 	u8 i;
5432 
5433 	for (i = 0; i < num_tc; i++)
5434 		enabled_tc |= BIT(i);
5435 
5436 	return enabled_tc;
5437 }
5438 
5439 /**
5440  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5441  * @pf: PF being queried
5442  *
5443  * Query the current MQPRIO configuration and return the number of
5444  * traffic classes enabled.
5445  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5446 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5447 {
5448 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5449 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5450 	u8 enabled_tc = 1, i;
5451 
5452 	for (i = 1; i < num_tc; i++)
5453 		enabled_tc |= BIT(i);
5454 	return enabled_tc;
5455 }
5456 
5457 /**
5458  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5459  * @pf: PF being queried
5460  *
5461  * Return number of traffic classes enabled for the given PF
5462  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5463 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5464 {
5465 	struct i40e_hw *hw = &pf->hw;
5466 	u8 i, enabled_tc = 1;
5467 	u8 num_tc = 0;
5468 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5469 
5470 	if (i40e_is_tc_mqprio_enabled(pf))
5471 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5472 
5473 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5474 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5475 		return 1;
5476 
5477 	/* SFP mode will be enabled for all TCs on port */
5478 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5479 		return i40e_dcb_get_num_tc(dcbcfg);
5480 
5481 	/* MFP mode return count of enabled TCs for this PF */
5482 	if (pf->hw.func_caps.iscsi)
5483 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5484 	else
5485 		return 1; /* Only TC0 */
5486 
5487 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5488 		if (enabled_tc & BIT(i))
5489 			num_tc++;
5490 	}
5491 	return num_tc;
5492 }
5493 
5494 /**
5495  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5496  * @pf: PF being queried
5497  *
5498  * Return a bitmap for enabled traffic classes for this PF.
5499  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5500 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5501 {
5502 	if (i40e_is_tc_mqprio_enabled(pf))
5503 		return i40e_mqprio_get_enabled_tc(pf);
5504 
5505 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5506 	 * default TC
5507 	 */
5508 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5509 		return I40E_DEFAULT_TRAFFIC_CLASS;
5510 
5511 	/* SFP mode we want PF to be enabled for all TCs */
5512 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5513 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5514 
5515 	/* MFP enabled and iSCSI PF type */
5516 	if (pf->hw.func_caps.iscsi)
5517 		return i40e_get_iscsi_tc_map(pf);
5518 	else
5519 		return I40E_DEFAULT_TRAFFIC_CLASS;
5520 }
5521 
5522 /**
5523  * i40e_vsi_get_bw_info - Query VSI BW Information
5524  * @vsi: the VSI being queried
5525  *
5526  * Returns 0 on success, negative value on failure
5527  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5528 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5529 {
5530 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5531 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5532 	struct i40e_pf *pf = vsi->back;
5533 	struct i40e_hw *hw = &pf->hw;
5534 	u32 tc_bw_max;
5535 	int ret;
5536 	int i;
5537 
5538 	/* Get the VSI level BW configuration */
5539 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5540 	if (ret) {
5541 		dev_info(&pf->pdev->dev,
5542 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5543 			 ERR_PTR(ret),
5544 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5545 		return -EINVAL;
5546 	}
5547 
5548 	/* Get the VSI level BW configuration per TC */
5549 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5550 					       NULL);
5551 	if (ret) {
5552 		dev_info(&pf->pdev->dev,
5553 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5554 			 ERR_PTR(ret),
5555 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5556 		return -EINVAL;
5557 	}
5558 
5559 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5560 		dev_info(&pf->pdev->dev,
5561 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5562 			 bw_config.tc_valid_bits,
5563 			 bw_ets_config.tc_valid_bits);
5564 		/* Still continuing */
5565 	}
5566 
5567 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5568 	vsi->bw_max_quanta = bw_config.max_bw;
5569 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5570 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5571 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5572 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5573 		vsi->bw_ets_limit_credits[i] =
5574 					le16_to_cpu(bw_ets_config.credits[i]);
5575 		/* 3 bits out of 4 for each TC */
5576 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5577 	}
5578 
5579 	return 0;
5580 }
5581 
5582 /**
5583  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5584  * @vsi: the VSI being configured
5585  * @enabled_tc: TC bitmap
5586  * @bw_share: BW shared credits per TC
5587  *
5588  * Returns 0 on success, negative value on failure
5589  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5590 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5591 				       u8 *bw_share)
5592 {
5593 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5594 	struct i40e_pf *pf = vsi->back;
5595 	int ret;
5596 	int i;
5597 
5598 	/* There is no need to reset BW when mqprio mode is on.  */
5599 	if (i40e_is_tc_mqprio_enabled(pf))
5600 		return 0;
5601 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5602 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5603 		if (ret)
5604 			dev_info(&pf->pdev->dev,
5605 				 "Failed to reset tx rate for vsi->seid %u\n",
5606 				 vsi->seid);
5607 		return ret;
5608 	}
5609 	memset(&bw_data, 0, sizeof(bw_data));
5610 	bw_data.tc_valid_bits = enabled_tc;
5611 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5612 		bw_data.tc_bw_credits[i] = bw_share[i];
5613 
5614 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5615 	if (ret) {
5616 		dev_info(&pf->pdev->dev,
5617 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5618 			 pf->hw.aq.asq_last_status);
5619 		return -EINVAL;
5620 	}
5621 
5622 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5623 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5624 
5625 	return 0;
5626 }
5627 
5628 /**
5629  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5630  * @vsi: the VSI being configured
5631  * @enabled_tc: TC map to be enabled
5632  *
5633  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5634 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5635 {
5636 	struct net_device *netdev = vsi->netdev;
5637 	struct i40e_pf *pf = vsi->back;
5638 	struct i40e_hw *hw = &pf->hw;
5639 	u8 netdev_tc = 0;
5640 	int i;
5641 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5642 
5643 	if (!netdev)
5644 		return;
5645 
5646 	if (!enabled_tc) {
5647 		netdev_reset_tc(netdev);
5648 		return;
5649 	}
5650 
5651 	/* Set up actual enabled TCs on the VSI */
5652 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5653 		return;
5654 
5655 	/* set per TC queues for the VSI */
5656 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5657 		/* Only set TC queues for enabled tcs
5658 		 *
5659 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5660 		 * enabled_tc bitmap would be 0x00001001; the driver
5661 		 * will set the numtc for netdev as 2 that will be
5662 		 * referenced by the netdev layer as TC 0 and 1.
5663 		 */
5664 		if (vsi->tc_config.enabled_tc & BIT(i))
5665 			netdev_set_tc_queue(netdev,
5666 					vsi->tc_config.tc_info[i].netdev_tc,
5667 					vsi->tc_config.tc_info[i].qcount,
5668 					vsi->tc_config.tc_info[i].qoffset);
5669 	}
5670 
5671 	if (i40e_is_tc_mqprio_enabled(pf))
5672 		return;
5673 
5674 	/* Assign UP2TC map for the VSI */
5675 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5676 		/* Get the actual TC# for the UP */
5677 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5678 		/* Get the mapped netdev TC# for the UP */
5679 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5680 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5681 	}
5682 }
5683 
5684 /**
5685  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5686  * @vsi: the VSI being configured
5687  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5688  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5689 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5690 				      struct i40e_vsi_context *ctxt)
5691 {
5692 	/* copy just the sections touched not the entire info
5693 	 * since not all sections are valid as returned by
5694 	 * update vsi params
5695 	 */
5696 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5697 	memcpy(&vsi->info.queue_mapping,
5698 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5699 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5700 	       sizeof(vsi->info.tc_mapping));
5701 }
5702 
5703 /**
5704  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5705  * @vsi: the VSI being reconfigured
5706  * @vsi_offset: offset from main VF VSI
5707  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5708 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5709 {
5710 	struct i40e_vsi_context ctxt = {};
5711 	struct i40e_pf *pf;
5712 	struct i40e_hw *hw;
5713 	int ret;
5714 
5715 	if (!vsi)
5716 		return I40E_ERR_PARAM;
5717 	pf = vsi->back;
5718 	hw = &pf->hw;
5719 
5720 	ctxt.seid = vsi->seid;
5721 	ctxt.pf_num = hw->pf_id;
5722 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5723 	ctxt.uplink_seid = vsi->uplink_seid;
5724 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5725 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5726 	ctxt.info = vsi->info;
5727 
5728 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5729 				 false);
5730 	if (vsi->reconfig_rss) {
5731 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5732 				      vsi->num_queue_pairs);
5733 		ret = i40e_vsi_config_rss(vsi);
5734 		if (ret) {
5735 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5736 			return ret;
5737 		}
5738 		vsi->reconfig_rss = false;
5739 	}
5740 
5741 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5742 	if (ret) {
5743 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5744 			 ERR_PTR(ret),
5745 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5746 		return ret;
5747 	}
5748 	/* update the local VSI info with updated queue map */
5749 	i40e_vsi_update_queue_map(vsi, &ctxt);
5750 	vsi->info.valid_sections = 0;
5751 
5752 	return ret;
5753 }
5754 
5755 /**
5756  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5757  * @vsi: VSI to be configured
5758  * @enabled_tc: TC bitmap
5759  *
5760  * This configures a particular VSI for TCs that are mapped to the
5761  * given TC bitmap. It uses default bandwidth share for TCs across
5762  * VSIs to configure TC for a particular VSI.
5763  *
5764  * NOTE:
5765  * It is expected that the VSI queues have been quisced before calling
5766  * this function.
5767  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5768 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5769 {
5770 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5771 	struct i40e_pf *pf = vsi->back;
5772 	struct i40e_hw *hw = &pf->hw;
5773 	struct i40e_vsi_context ctxt;
5774 	int ret = 0;
5775 	int i;
5776 
5777 	/* Check if enabled_tc is same as existing or new TCs */
5778 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5779 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5780 		return ret;
5781 
5782 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5783 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5784 		if (enabled_tc & BIT(i))
5785 			bw_share[i] = 1;
5786 	}
5787 
5788 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5789 	if (ret) {
5790 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5791 
5792 		dev_info(&pf->pdev->dev,
5793 			 "Failed configuring TC map %d for VSI %d\n",
5794 			 enabled_tc, vsi->seid);
5795 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5796 						  &bw_config, NULL);
5797 		if (ret) {
5798 			dev_info(&pf->pdev->dev,
5799 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5800 				 ERR_PTR(ret),
5801 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5802 			goto out;
5803 		}
5804 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5805 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5806 
5807 			if (!valid_tc)
5808 				valid_tc = bw_config.tc_valid_bits;
5809 			/* Always enable TC0, no matter what */
5810 			valid_tc |= 1;
5811 			dev_info(&pf->pdev->dev,
5812 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5813 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5814 			enabled_tc = valid_tc;
5815 		}
5816 
5817 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5818 		if (ret) {
5819 			dev_err(&pf->pdev->dev,
5820 				"Unable to  configure TC map %d for VSI %d\n",
5821 				enabled_tc, vsi->seid);
5822 			goto out;
5823 		}
5824 	}
5825 
5826 	/* Update Queue Pairs Mapping for currently enabled UPs */
5827 	ctxt.seid = vsi->seid;
5828 	ctxt.pf_num = vsi->back->hw.pf_id;
5829 	ctxt.vf_num = 0;
5830 	ctxt.uplink_seid = vsi->uplink_seid;
5831 	ctxt.info = vsi->info;
5832 	if (i40e_is_tc_mqprio_enabled(pf)) {
5833 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5834 		if (ret)
5835 			goto out;
5836 	} else {
5837 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5838 	}
5839 
5840 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5841 	 * queues changed.
5842 	 */
5843 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5844 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5845 				      vsi->num_queue_pairs);
5846 		ret = i40e_vsi_config_rss(vsi);
5847 		if (ret) {
5848 			dev_info(&vsi->back->pdev->dev,
5849 				 "Failed to reconfig rss for num_queues\n");
5850 			return ret;
5851 		}
5852 		vsi->reconfig_rss = false;
5853 	}
5854 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5855 		ctxt.info.valid_sections |=
5856 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5857 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5858 	}
5859 
5860 	/* Update the VSI after updating the VSI queue-mapping
5861 	 * information
5862 	 */
5863 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5864 	if (ret) {
5865 		dev_info(&pf->pdev->dev,
5866 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5867 			 ERR_PTR(ret),
5868 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5869 		goto out;
5870 	}
5871 	/* update the local VSI info with updated queue map */
5872 	i40e_vsi_update_queue_map(vsi, &ctxt);
5873 	vsi->info.valid_sections = 0;
5874 
5875 	/* Update current VSI BW information */
5876 	ret = i40e_vsi_get_bw_info(vsi);
5877 	if (ret) {
5878 		dev_info(&pf->pdev->dev,
5879 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5880 			 ERR_PTR(ret),
5881 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5882 		goto out;
5883 	}
5884 
5885 	/* Update the netdev TC setup */
5886 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5887 out:
5888 	return ret;
5889 }
5890 
5891 /**
5892  * i40e_get_link_speed - Returns link speed for the interface
5893  * @vsi: VSI to be configured
5894  *
5895  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5896 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5897 {
5898 	struct i40e_pf *pf = vsi->back;
5899 
5900 	switch (pf->hw.phy.link_info.link_speed) {
5901 	case I40E_LINK_SPEED_40GB:
5902 		return 40000;
5903 	case I40E_LINK_SPEED_25GB:
5904 		return 25000;
5905 	case I40E_LINK_SPEED_20GB:
5906 		return 20000;
5907 	case I40E_LINK_SPEED_10GB:
5908 		return 10000;
5909 	case I40E_LINK_SPEED_1GB:
5910 		return 1000;
5911 	default:
5912 		return -EINVAL;
5913 	}
5914 }
5915 
5916 /**
5917  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5918  * @vsi: Pointer to vsi structure
5919  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5920  *
5921  * Helper function to convert units before send to set BW limit
5922  **/
i40e_bw_bytes_to_mbits(struct i40e_vsi * vsi,u64 max_tx_rate)5923 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5924 {
5925 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5926 		dev_warn(&vsi->back->pdev->dev,
5927 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5928 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5929 	} else {
5930 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5931 	}
5932 
5933 	return max_tx_rate;
5934 }
5935 
5936 /**
5937  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5938  * @vsi: VSI to be configured
5939  * @seid: seid of the channel/VSI
5940  * @max_tx_rate: max TX rate to be configured as BW limit
5941  *
5942  * Helper function to set BW limit for a given VSI
5943  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5944 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5945 {
5946 	struct i40e_pf *pf = vsi->back;
5947 	u64 credits = 0;
5948 	int speed = 0;
5949 	int ret = 0;
5950 
5951 	speed = i40e_get_link_speed(vsi);
5952 	if (max_tx_rate > speed) {
5953 		dev_err(&pf->pdev->dev,
5954 			"Invalid max tx rate %llu specified for VSI seid %d.",
5955 			max_tx_rate, seid);
5956 		return -EINVAL;
5957 	}
5958 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5959 		dev_warn(&pf->pdev->dev,
5960 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5961 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5962 	}
5963 
5964 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5965 	credits = max_tx_rate;
5966 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5967 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5968 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5969 	if (ret)
5970 		dev_err(&pf->pdev->dev,
5971 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5972 			max_tx_rate, seid, ERR_PTR(ret),
5973 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5974 	return ret;
5975 }
5976 
5977 /**
5978  * i40e_remove_queue_channels - Remove queue channels for the TCs
5979  * @vsi: VSI to be configured
5980  *
5981  * Remove queue channels for the TCs
5982  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5983 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5984 {
5985 	enum i40e_admin_queue_err last_aq_status;
5986 	struct i40e_cloud_filter *cfilter;
5987 	struct i40e_channel *ch, *ch_tmp;
5988 	struct i40e_pf *pf = vsi->back;
5989 	struct hlist_node *node;
5990 	int ret, i;
5991 
5992 	/* Reset rss size that was stored when reconfiguring rss for
5993 	 * channel VSIs with non-power-of-2 queue count.
5994 	 */
5995 	vsi->current_rss_size = 0;
5996 
5997 	/* perform cleanup for channels if they exist */
5998 	if (list_empty(&vsi->ch_list))
5999 		return;
6000 
6001 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6002 		struct i40e_vsi *p_vsi;
6003 
6004 		list_del(&ch->list);
6005 		p_vsi = ch->parent_vsi;
6006 		if (!p_vsi || !ch->initialized) {
6007 			kfree(ch);
6008 			continue;
6009 		}
6010 		/* Reset queue contexts */
6011 		for (i = 0; i < ch->num_queue_pairs; i++) {
6012 			struct i40e_ring *tx_ring, *rx_ring;
6013 			u16 pf_q;
6014 
6015 			pf_q = ch->base_queue + i;
6016 			tx_ring = vsi->tx_rings[pf_q];
6017 			tx_ring->ch = NULL;
6018 
6019 			rx_ring = vsi->rx_rings[pf_q];
6020 			rx_ring->ch = NULL;
6021 		}
6022 
6023 		/* Reset BW configured for this VSI via mqprio */
6024 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6025 		if (ret)
6026 			dev_info(&vsi->back->pdev->dev,
6027 				 "Failed to reset tx rate for ch->seid %u\n",
6028 				 ch->seid);
6029 
6030 		/* delete cloud filters associated with this channel */
6031 		hlist_for_each_entry_safe(cfilter, node,
6032 					  &pf->cloud_filter_list, cloud_node) {
6033 			if (cfilter->seid != ch->seid)
6034 				continue;
6035 
6036 			hash_del(&cfilter->cloud_node);
6037 			if (cfilter->dst_port)
6038 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6039 									cfilter,
6040 									false);
6041 			else
6042 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6043 								false);
6044 			last_aq_status = pf->hw.aq.asq_last_status;
6045 			if (ret)
6046 				dev_info(&pf->pdev->dev,
6047 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6048 					 ERR_PTR(ret),
6049 					 i40e_aq_str(&pf->hw, last_aq_status));
6050 			kfree(cfilter);
6051 		}
6052 
6053 		/* delete VSI from FW */
6054 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6055 					     NULL);
6056 		if (ret)
6057 			dev_err(&vsi->back->pdev->dev,
6058 				"unable to remove channel (%d) for parent VSI(%d)\n",
6059 				ch->seid, p_vsi->seid);
6060 		kfree(ch);
6061 	}
6062 	INIT_LIST_HEAD(&vsi->ch_list);
6063 }
6064 
6065 /**
6066  * i40e_get_max_queues_for_channel
6067  * @vsi: ptr to VSI to which channels are associated with
6068  *
6069  * Helper function which returns max value among the queue counts set on the
6070  * channels/TCs created.
6071  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)6072 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6073 {
6074 	struct i40e_channel *ch, *ch_tmp;
6075 	int max = 0;
6076 
6077 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6078 		if (!ch->initialized)
6079 			continue;
6080 		if (ch->num_queue_pairs > max)
6081 			max = ch->num_queue_pairs;
6082 	}
6083 
6084 	return max;
6085 }
6086 
6087 /**
6088  * i40e_validate_num_queues - validate num_queues w.r.t channel
6089  * @pf: ptr to PF device
6090  * @num_queues: number of queues
6091  * @vsi: the parent VSI
6092  * @reconfig_rss: indicates should the RSS be reconfigured or not
6093  *
6094  * This function validates number of queues in the context of new channel
6095  * which is being established and determines if RSS should be reconfigured
6096  * or not for parent VSI.
6097  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)6098 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6099 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6100 {
6101 	int max_ch_queues;
6102 
6103 	if (!reconfig_rss)
6104 		return -EINVAL;
6105 
6106 	*reconfig_rss = false;
6107 	if (vsi->current_rss_size) {
6108 		if (num_queues > vsi->current_rss_size) {
6109 			dev_dbg(&pf->pdev->dev,
6110 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6111 				num_queues, vsi->current_rss_size);
6112 			return -EINVAL;
6113 		} else if ((num_queues < vsi->current_rss_size) &&
6114 			   (!is_power_of_2(num_queues))) {
6115 			dev_dbg(&pf->pdev->dev,
6116 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6117 				num_queues, vsi->current_rss_size);
6118 			return -EINVAL;
6119 		}
6120 	}
6121 
6122 	if (!is_power_of_2(num_queues)) {
6123 		/* Find the max num_queues configured for channel if channel
6124 		 * exist.
6125 		 * if channel exist, then enforce 'num_queues' to be more than
6126 		 * max ever queues configured for channel.
6127 		 */
6128 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6129 		if (num_queues < max_ch_queues) {
6130 			dev_dbg(&pf->pdev->dev,
6131 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6132 				num_queues, max_ch_queues);
6133 			return -EINVAL;
6134 		}
6135 		*reconfig_rss = true;
6136 	}
6137 
6138 	return 0;
6139 }
6140 
6141 /**
6142  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6143  * @vsi: the VSI being setup
6144  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6145  *
6146  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6147  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)6148 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6149 {
6150 	struct i40e_pf *pf = vsi->back;
6151 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6152 	struct i40e_hw *hw = &pf->hw;
6153 	int local_rss_size;
6154 	u8 *lut;
6155 	int ret;
6156 
6157 	if (!vsi->rss_size)
6158 		return -EINVAL;
6159 
6160 	if (rss_size > vsi->rss_size)
6161 		return -EINVAL;
6162 
6163 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6164 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6165 	if (!lut)
6166 		return -ENOMEM;
6167 
6168 	/* Ignoring user configured lut if there is one */
6169 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6170 
6171 	/* Use user configured hash key if there is one, otherwise
6172 	 * use default.
6173 	 */
6174 	if (vsi->rss_hkey_user)
6175 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6176 	else
6177 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6178 
6179 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6180 	if (ret) {
6181 		dev_info(&pf->pdev->dev,
6182 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6183 			 ERR_PTR(ret),
6184 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6185 		kfree(lut);
6186 		return ret;
6187 	}
6188 	kfree(lut);
6189 
6190 	/* Do the update w.r.t. storing rss_size */
6191 	if (!vsi->orig_rss_size)
6192 		vsi->orig_rss_size = vsi->rss_size;
6193 	vsi->current_rss_size = local_rss_size;
6194 
6195 	return ret;
6196 }
6197 
6198 /**
6199  * i40e_channel_setup_queue_map - Setup a channel queue map
6200  * @pf: ptr to PF device
6201  * @ctxt: VSI context structure
6202  * @ch: ptr to channel structure
6203  *
6204  * Setup queue map for a specific channel
6205  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)6206 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6207 					 struct i40e_vsi_context *ctxt,
6208 					 struct i40e_channel *ch)
6209 {
6210 	u16 qcount, qmap, sections = 0;
6211 	u8 offset = 0;
6212 	int pow;
6213 
6214 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6215 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6216 
6217 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6218 	ch->num_queue_pairs = qcount;
6219 
6220 	/* find the next higher power-of-2 of num queue pairs */
6221 	pow = ilog2(qcount);
6222 	if (!is_power_of_2(qcount))
6223 		pow++;
6224 
6225 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6226 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6227 
6228 	/* Setup queue TC[0].qmap for given VSI context */
6229 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6230 
6231 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6232 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6233 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6234 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6235 }
6236 
6237 /**
6238  * i40e_add_channel - add a channel by adding VSI
6239  * @pf: ptr to PF device
6240  * @uplink_seid: underlying HW switching element (VEB) ID
6241  * @ch: ptr to channel structure
6242  *
6243  * Add a channel (VSI) using add_vsi and queue_map
6244  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)6245 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6246 			    struct i40e_channel *ch)
6247 {
6248 	struct i40e_hw *hw = &pf->hw;
6249 	struct i40e_vsi_context ctxt;
6250 	u8 enabled_tc = 0x1; /* TC0 enabled */
6251 	int ret;
6252 
6253 	if (ch->type != I40E_VSI_VMDQ2) {
6254 		dev_info(&pf->pdev->dev,
6255 			 "add new vsi failed, ch->type %d\n", ch->type);
6256 		return -EINVAL;
6257 	}
6258 
6259 	memset(&ctxt, 0, sizeof(ctxt));
6260 	ctxt.pf_num = hw->pf_id;
6261 	ctxt.vf_num = 0;
6262 	ctxt.uplink_seid = uplink_seid;
6263 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6264 	if (ch->type == I40E_VSI_VMDQ2)
6265 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6266 
6267 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6268 		ctxt.info.valid_sections |=
6269 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6270 		ctxt.info.switch_id =
6271 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6272 	}
6273 
6274 	/* Set queue map for a given VSI context */
6275 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6276 
6277 	/* Now time to create VSI */
6278 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6279 	if (ret) {
6280 		dev_info(&pf->pdev->dev,
6281 			 "add new vsi failed, err %pe aq_err %s\n",
6282 			 ERR_PTR(ret),
6283 			 i40e_aq_str(&pf->hw,
6284 				     pf->hw.aq.asq_last_status));
6285 		return -ENOENT;
6286 	}
6287 
6288 	/* Success, update channel, set enabled_tc only if the channel
6289 	 * is not a macvlan
6290 	 */
6291 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6292 	ch->seid = ctxt.seid;
6293 	ch->vsi_number = ctxt.vsi_number;
6294 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6295 
6296 	/* copy just the sections touched not the entire info
6297 	 * since not all sections are valid as returned by
6298 	 * update vsi params
6299 	 */
6300 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6301 	memcpy(&ch->info.queue_mapping,
6302 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6303 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6304 	       sizeof(ctxt.info.tc_mapping));
6305 
6306 	return 0;
6307 }
6308 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6309 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6310 				  u8 *bw_share)
6311 {
6312 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6313 	int ret;
6314 	int i;
6315 
6316 	memset(&bw_data, 0, sizeof(bw_data));
6317 	bw_data.tc_valid_bits = ch->enabled_tc;
6318 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6319 		bw_data.tc_bw_credits[i] = bw_share[i];
6320 
6321 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6322 				       &bw_data, NULL);
6323 	if (ret) {
6324 		dev_info(&vsi->back->pdev->dev,
6325 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6326 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6327 		return -EINVAL;
6328 	}
6329 
6330 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6331 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6332 
6333 	return 0;
6334 }
6335 
6336 /**
6337  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6338  * @pf: ptr to PF device
6339  * @vsi: the VSI being setup
6340  * @ch: ptr to channel structure
6341  *
6342  * Configure TX rings associated with channel (VSI) since queues are being
6343  * from parent VSI.
6344  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6345 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6346 				       struct i40e_vsi *vsi,
6347 				       struct i40e_channel *ch)
6348 {
6349 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6350 	int ret;
6351 	int i;
6352 
6353 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6354 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6355 		if (ch->enabled_tc & BIT(i))
6356 			bw_share[i] = 1;
6357 	}
6358 
6359 	/* configure BW for new VSI */
6360 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6361 	if (ret) {
6362 		dev_info(&vsi->back->pdev->dev,
6363 			 "Failed configuring TC map %d for channel (seid %u)\n",
6364 			 ch->enabled_tc, ch->seid);
6365 		return ret;
6366 	}
6367 
6368 	for (i = 0; i < ch->num_queue_pairs; i++) {
6369 		struct i40e_ring *tx_ring, *rx_ring;
6370 		u16 pf_q;
6371 
6372 		pf_q = ch->base_queue + i;
6373 
6374 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6375 		 * context
6376 		 */
6377 		tx_ring = vsi->tx_rings[pf_q];
6378 		tx_ring->ch = ch;
6379 
6380 		/* Get the RX ring ptr */
6381 		rx_ring = vsi->rx_rings[pf_q];
6382 		rx_ring->ch = ch;
6383 	}
6384 
6385 	return 0;
6386 }
6387 
6388 /**
6389  * i40e_setup_hw_channel - setup new channel
6390  * @pf: ptr to PF device
6391  * @vsi: the VSI being setup
6392  * @ch: ptr to channel structure
6393  * @uplink_seid: underlying HW switching element (VEB) ID
6394  * @type: type of channel to be created (VMDq2/VF)
6395  *
6396  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6397  * and configures TX rings accordingly
6398  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6399 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6400 					struct i40e_vsi *vsi,
6401 					struct i40e_channel *ch,
6402 					u16 uplink_seid, u8 type)
6403 {
6404 	int ret;
6405 
6406 	ch->initialized = false;
6407 	ch->base_queue = vsi->next_base_queue;
6408 	ch->type = type;
6409 
6410 	/* Proceed with creation of channel (VMDq2) VSI */
6411 	ret = i40e_add_channel(pf, uplink_seid, ch);
6412 	if (ret) {
6413 		dev_info(&pf->pdev->dev,
6414 			 "failed to add_channel using uplink_seid %u\n",
6415 			 uplink_seid);
6416 		return ret;
6417 	}
6418 
6419 	/* Mark the successful creation of channel */
6420 	ch->initialized = true;
6421 
6422 	/* Reconfigure TX queues using QTX_CTL register */
6423 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6424 	if (ret) {
6425 		dev_info(&pf->pdev->dev,
6426 			 "failed to configure TX rings for channel %u\n",
6427 			 ch->seid);
6428 		return ret;
6429 	}
6430 
6431 	/* update 'next_base_queue' */
6432 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6433 	dev_dbg(&pf->pdev->dev,
6434 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6435 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6436 		ch->num_queue_pairs,
6437 		vsi->next_base_queue);
6438 	return ret;
6439 }
6440 
6441 /**
6442  * i40e_setup_channel - setup new channel using uplink element
6443  * @pf: ptr to PF device
6444  * @vsi: pointer to the VSI to set up the channel within
6445  * @ch: ptr to channel structure
6446  *
6447  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6448  * and uplink switching element (uplink_seid)
6449  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6450 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6451 			       struct i40e_channel *ch)
6452 {
6453 	u8 vsi_type;
6454 	u16 seid;
6455 	int ret;
6456 
6457 	if (vsi->type == I40E_VSI_MAIN) {
6458 		vsi_type = I40E_VSI_VMDQ2;
6459 	} else {
6460 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6461 			vsi->type);
6462 		return false;
6463 	}
6464 
6465 	/* underlying switching element */
6466 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6467 
6468 	/* create channel (VSI), configure TX rings */
6469 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6470 	if (ret) {
6471 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6472 		return false;
6473 	}
6474 
6475 	return ch->initialized ? true : false;
6476 }
6477 
6478 /**
6479  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6480  * @vsi: ptr to VSI which has PF backing
6481  *
6482  * Sets up switch mode correctly if it needs to be changed and perform
6483  * what are allowed modes.
6484  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6485 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6486 {
6487 	u8 mode;
6488 	struct i40e_pf *pf = vsi->back;
6489 	struct i40e_hw *hw = &pf->hw;
6490 	int ret;
6491 
6492 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6493 	if (ret)
6494 		return -EINVAL;
6495 
6496 	if (hw->dev_caps.switch_mode) {
6497 		/* if switch mode is set, support mode2 (non-tunneled for
6498 		 * cloud filter) for now
6499 		 */
6500 		u32 switch_mode = hw->dev_caps.switch_mode &
6501 				  I40E_SWITCH_MODE_MASK;
6502 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6503 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6504 				return 0;
6505 			dev_err(&pf->pdev->dev,
6506 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6507 				hw->dev_caps.switch_mode);
6508 			return -EINVAL;
6509 		}
6510 	}
6511 
6512 	/* Set Bit 7 to be valid */
6513 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6514 
6515 	/* Set L4type for TCP support */
6516 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6517 
6518 	/* Set cloud filter mode */
6519 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6520 
6521 	/* Prep mode field for set_switch_config */
6522 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6523 					pf->last_sw_conf_valid_flags,
6524 					mode, NULL);
6525 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6526 		dev_err(&pf->pdev->dev,
6527 			"couldn't set switch config bits, err %pe aq_err %s\n",
6528 			ERR_PTR(ret),
6529 			i40e_aq_str(hw,
6530 				    hw->aq.asq_last_status));
6531 
6532 	return ret;
6533 }
6534 
6535 /**
6536  * i40e_create_queue_channel - function to create channel
6537  * @vsi: VSI to be configured
6538  * @ch: ptr to channel (it contains channel specific params)
6539  *
6540  * This function creates channel (VSI) using num_queues specified by user,
6541  * reconfigs RSS if needed.
6542  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6543 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6544 			      struct i40e_channel *ch)
6545 {
6546 	struct i40e_pf *pf = vsi->back;
6547 	bool reconfig_rss;
6548 	int err;
6549 
6550 	if (!ch)
6551 		return -EINVAL;
6552 
6553 	if (!ch->num_queue_pairs) {
6554 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6555 			ch->num_queue_pairs);
6556 		return -EINVAL;
6557 	}
6558 
6559 	/* validate user requested num_queues for channel */
6560 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6561 				       &reconfig_rss);
6562 	if (err) {
6563 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6564 			 ch->num_queue_pairs);
6565 		return -EINVAL;
6566 	}
6567 
6568 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6569 	 * VSI to be added switch to VEB mode.
6570 	 */
6571 
6572 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6573 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6574 
6575 		if (vsi->type == I40E_VSI_MAIN) {
6576 			if (i40e_is_tc_mqprio_enabled(pf))
6577 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6578 			else
6579 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6580 		}
6581 		/* now onwards for main VSI, number of queues will be value
6582 		 * of TC0's queue count
6583 		 */
6584 	}
6585 
6586 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6587 	 * it should be more than num_queues
6588 	 */
6589 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6590 		dev_dbg(&pf->pdev->dev,
6591 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6592 			vsi->cnt_q_avail, ch->num_queue_pairs);
6593 		return -EINVAL;
6594 	}
6595 
6596 	/* reconfig_rss only if vsi type is MAIN_VSI */
6597 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6598 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6599 		if (err) {
6600 			dev_info(&pf->pdev->dev,
6601 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6602 				 ch->num_queue_pairs);
6603 			return -EINVAL;
6604 		}
6605 	}
6606 
6607 	if (!i40e_setup_channel(pf, vsi, ch)) {
6608 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6609 		return -EINVAL;
6610 	}
6611 
6612 	dev_info(&pf->pdev->dev,
6613 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6614 		 ch->seid, ch->num_queue_pairs);
6615 
6616 	/* configure VSI for BW limit */
6617 	if (ch->max_tx_rate) {
6618 		u64 credits = ch->max_tx_rate;
6619 
6620 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6621 			return -EINVAL;
6622 
6623 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6624 		dev_dbg(&pf->pdev->dev,
6625 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6626 			ch->max_tx_rate,
6627 			credits,
6628 			ch->seid);
6629 	}
6630 
6631 	/* in case of VF, this will be main SRIOV VSI */
6632 	ch->parent_vsi = vsi;
6633 
6634 	/* and update main_vsi's count for queue_available to use */
6635 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6636 
6637 	return 0;
6638 }
6639 
6640 /**
6641  * i40e_configure_queue_channels - Add queue channel for the given TCs
6642  * @vsi: VSI to be configured
6643  *
6644  * Configures queue channel mapping to the given TCs
6645  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6646 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6647 {
6648 	struct i40e_channel *ch;
6649 	u64 max_rate = 0;
6650 	int ret = 0, i;
6651 
6652 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6653 	vsi->tc_seid_map[0] = vsi->seid;
6654 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6655 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6656 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6657 			if (!ch) {
6658 				ret = -ENOMEM;
6659 				goto err_free;
6660 			}
6661 
6662 			INIT_LIST_HEAD(&ch->list);
6663 			ch->num_queue_pairs =
6664 				vsi->tc_config.tc_info[i].qcount;
6665 			ch->base_queue =
6666 				vsi->tc_config.tc_info[i].qoffset;
6667 
6668 			/* Bandwidth limit through tc interface is in bytes/s,
6669 			 * change to Mbit/s
6670 			 */
6671 			max_rate = vsi->mqprio_qopt.max_rate[i];
6672 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6673 			ch->max_tx_rate = max_rate;
6674 
6675 			list_add_tail(&ch->list, &vsi->ch_list);
6676 
6677 			ret = i40e_create_queue_channel(vsi, ch);
6678 			if (ret) {
6679 				dev_err(&vsi->back->pdev->dev,
6680 					"Failed creating queue channel with TC%d: queues %d\n",
6681 					i, ch->num_queue_pairs);
6682 				goto err_free;
6683 			}
6684 			vsi->tc_seid_map[i] = ch->seid;
6685 		}
6686 	}
6687 
6688 	/* reset to reconfigure TX queue contexts */
6689 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6690 	return ret;
6691 
6692 err_free:
6693 	i40e_remove_queue_channels(vsi);
6694 	return ret;
6695 }
6696 
6697 /**
6698  * i40e_veb_config_tc - Configure TCs for given VEB
6699  * @veb: given VEB
6700  * @enabled_tc: TC bitmap
6701  *
6702  * Configures given TC bitmap for VEB (switching) element
6703  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6704 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6705 {
6706 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6707 	struct i40e_pf *pf = veb->pf;
6708 	int ret = 0;
6709 	int i;
6710 
6711 	/* No TCs or already enabled TCs just return */
6712 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6713 		return ret;
6714 
6715 	bw_data.tc_valid_bits = enabled_tc;
6716 	/* bw_data.absolute_credits is not set (relative) */
6717 
6718 	/* Enable ETS TCs with equal BW Share for now */
6719 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6720 		if (enabled_tc & BIT(i))
6721 			bw_data.tc_bw_share_credits[i] = 1;
6722 	}
6723 
6724 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6725 						   &bw_data, NULL);
6726 	if (ret) {
6727 		dev_info(&pf->pdev->dev,
6728 			 "VEB bw config failed, err %pe aq_err %s\n",
6729 			 ERR_PTR(ret),
6730 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6731 		goto out;
6732 	}
6733 
6734 	/* Update the BW information */
6735 	ret = i40e_veb_get_bw_info(veb);
6736 	if (ret) {
6737 		dev_info(&pf->pdev->dev,
6738 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6739 			 ERR_PTR(ret),
6740 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6741 	}
6742 
6743 out:
6744 	return ret;
6745 }
6746 
6747 #ifdef CONFIG_I40E_DCB
6748 /**
6749  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6750  * @pf: PF struct
6751  *
6752  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6753  * the caller would've quiesce all the VSIs before calling
6754  * this function
6755  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6756 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6757 {
6758 	u8 tc_map = 0;
6759 	int ret;
6760 	u8 v;
6761 
6762 	/* Enable the TCs available on PF to all VEBs */
6763 	tc_map = i40e_pf_get_tc_map(pf);
6764 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6765 		return;
6766 
6767 	for (v = 0; v < I40E_MAX_VEB; v++) {
6768 		if (!pf->veb[v])
6769 			continue;
6770 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6771 		if (ret) {
6772 			dev_info(&pf->pdev->dev,
6773 				 "Failed configuring TC for VEB seid=%d\n",
6774 				 pf->veb[v]->seid);
6775 			/* Will try to configure as many components */
6776 		}
6777 	}
6778 
6779 	/* Update each VSI */
6780 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6781 		if (!pf->vsi[v])
6782 			continue;
6783 
6784 		/* - Enable all TCs for the LAN VSI
6785 		 * - For all others keep them at TC0 for now
6786 		 */
6787 		if (v == pf->lan_vsi)
6788 			tc_map = i40e_pf_get_tc_map(pf);
6789 		else
6790 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6791 
6792 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6793 		if (ret) {
6794 			dev_info(&pf->pdev->dev,
6795 				 "Failed configuring TC for VSI seid=%d\n",
6796 				 pf->vsi[v]->seid);
6797 			/* Will try to configure as many components */
6798 		} else {
6799 			/* Re-configure VSI vectors based on updated TC map */
6800 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6801 			if (pf->vsi[v]->netdev)
6802 				i40e_dcbnl_set_all(pf->vsi[v]);
6803 		}
6804 	}
6805 }
6806 
6807 /**
6808  * i40e_resume_port_tx - Resume port Tx
6809  * @pf: PF struct
6810  *
6811  * Resume a port's Tx and issue a PF reset in case of failure to
6812  * resume.
6813  **/
i40e_resume_port_tx(struct i40e_pf * pf)6814 static int i40e_resume_port_tx(struct i40e_pf *pf)
6815 {
6816 	struct i40e_hw *hw = &pf->hw;
6817 	int ret;
6818 
6819 	ret = i40e_aq_resume_port_tx(hw, NULL);
6820 	if (ret) {
6821 		dev_info(&pf->pdev->dev,
6822 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6823 			  ERR_PTR(ret),
6824 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6825 		/* Schedule PF reset to recover */
6826 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6827 		i40e_service_event_schedule(pf);
6828 	}
6829 
6830 	return ret;
6831 }
6832 
6833 /**
6834  * i40e_suspend_port_tx - Suspend port Tx
6835  * @pf: PF struct
6836  *
6837  * Suspend a port's Tx and issue a PF reset in case of failure.
6838  **/
i40e_suspend_port_tx(struct i40e_pf * pf)6839 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6840 {
6841 	struct i40e_hw *hw = &pf->hw;
6842 	int ret;
6843 
6844 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6845 	if (ret) {
6846 		dev_info(&pf->pdev->dev,
6847 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6848 			 ERR_PTR(ret),
6849 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6850 		/* Schedule PF reset to recover */
6851 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6852 		i40e_service_event_schedule(pf);
6853 	}
6854 
6855 	return ret;
6856 }
6857 
6858 /**
6859  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6860  * @pf: PF being configured
6861  * @new_cfg: New DCBX configuration
6862  *
6863  * Program DCB settings into HW and reconfigure VEB/VSIs on
6864  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6865  **/
i40e_hw_set_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6866 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6867 				  struct i40e_dcbx_config *new_cfg)
6868 {
6869 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6870 	int ret;
6871 
6872 	/* Check if need reconfiguration */
6873 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6874 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6875 		return 0;
6876 	}
6877 
6878 	/* Config change disable all VSIs */
6879 	i40e_pf_quiesce_all_vsi(pf);
6880 
6881 	/* Copy the new config to the current config */
6882 	*old_cfg = *new_cfg;
6883 	old_cfg->etsrec = old_cfg->etscfg;
6884 	ret = i40e_set_dcb_config(&pf->hw);
6885 	if (ret) {
6886 		dev_info(&pf->pdev->dev,
6887 			 "Set DCB Config failed, err %pe aq_err %s\n",
6888 			 ERR_PTR(ret),
6889 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6890 		goto out;
6891 	}
6892 
6893 	/* Changes in configuration update VEB/VSI */
6894 	i40e_dcb_reconfigure(pf);
6895 out:
6896 	/* In case of reset do not try to resume anything */
6897 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6898 		/* Re-start the VSIs if disabled */
6899 		ret = i40e_resume_port_tx(pf);
6900 		/* In case of error no point in resuming VSIs */
6901 		if (ret)
6902 			goto err;
6903 		i40e_pf_unquiesce_all_vsi(pf);
6904 	}
6905 err:
6906 	return ret;
6907 }
6908 
6909 /**
6910  * i40e_hw_dcb_config - Program new DCBX settings into HW
6911  * @pf: PF being configured
6912  * @new_cfg: New DCBX configuration
6913  *
6914  * Program DCB settings into HW and reconfigure VEB/VSIs on
6915  * given PF
6916  **/
i40e_hw_dcb_config(struct i40e_pf * pf,struct i40e_dcbx_config * new_cfg)6917 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6918 {
6919 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6920 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6921 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6922 	struct i40e_dcbx_config *old_cfg;
6923 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6924 	struct i40e_rx_pb_config pb_cfg;
6925 	struct i40e_hw *hw = &pf->hw;
6926 	u8 num_ports = hw->num_ports;
6927 	bool need_reconfig;
6928 	int ret = -EINVAL;
6929 	u8 lltc_map = 0;
6930 	u8 tc_map = 0;
6931 	u8 new_numtc;
6932 	u8 i;
6933 
6934 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6935 	/* Un-pack information to Program ETS HW via shared API
6936 	 * numtc, tcmap
6937 	 * LLTC map
6938 	 * ETS/NON-ETS arbiter mode
6939 	 * max exponent (credit refills)
6940 	 * Total number of ports
6941 	 * PFC priority bit-map
6942 	 * Priority Table
6943 	 * BW % per TC
6944 	 * Arbiter mode between UPs sharing same TC
6945 	 * TSA table (ETS or non-ETS)
6946 	 * EEE enabled or not
6947 	 * MFS TC table
6948 	 */
6949 
6950 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6951 
6952 	memset(&ets_data, 0, sizeof(ets_data));
6953 	for (i = 0; i < new_numtc; i++) {
6954 		tc_map |= BIT(i);
6955 		switch (new_cfg->etscfg.tsatable[i]) {
6956 		case I40E_IEEE_TSA_ETS:
6957 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6958 			ets_data.tc_bw_share_credits[i] =
6959 					new_cfg->etscfg.tcbwtable[i];
6960 			break;
6961 		case I40E_IEEE_TSA_STRICT:
6962 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6963 			lltc_map |= BIT(i);
6964 			ets_data.tc_bw_share_credits[i] =
6965 					I40E_DCB_STRICT_PRIO_CREDITS;
6966 			break;
6967 		default:
6968 			/* Invalid TSA type */
6969 			need_reconfig = false;
6970 			goto out;
6971 		}
6972 	}
6973 
6974 	old_cfg = &hw->local_dcbx_config;
6975 	/* Check if need reconfiguration */
6976 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6977 
6978 	/* If needed, enable/disable frame tagging, disable all VSIs
6979 	 * and suspend port tx
6980 	 */
6981 	if (need_reconfig) {
6982 		/* Enable DCB tagging only when more than one TC */
6983 		if (new_numtc > 1)
6984 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6985 		else
6986 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6987 
6988 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6989 		/* Reconfiguration needed quiesce all VSIs */
6990 		i40e_pf_quiesce_all_vsi(pf);
6991 		ret = i40e_suspend_port_tx(pf);
6992 		if (ret)
6993 			goto err;
6994 	}
6995 
6996 	/* Configure Port ETS Tx Scheduler */
6997 	ets_data.tc_valid_bits = tc_map;
6998 	ets_data.tc_strict_priority_flags = lltc_map;
6999 	ret = i40e_aq_config_switch_comp_ets
7000 		(hw, pf->mac_seid, &ets_data,
7001 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7002 	if (ret) {
7003 		dev_info(&pf->pdev->dev,
7004 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7005 			 ERR_PTR(ret),
7006 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7007 		goto out;
7008 	}
7009 
7010 	/* Configure Rx ETS HW */
7011 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7012 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7013 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7014 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7015 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7016 				   lltc_map);
7017 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7018 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7019 				     prio_type);
7020 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7021 			       new_cfg->etscfg.prioritytable);
7022 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7023 
7024 	/* Configure Rx Packet Buffers in HW */
7025 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7026 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7027 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7028 	}
7029 
7030 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7031 					 false, new_cfg->pfc.pfcenable,
7032 					 mfs_tc, &pb_cfg);
7033 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7034 
7035 	/* Update the local Rx Packet buffer config */
7036 	pf->pb_cfg = pb_cfg;
7037 
7038 	/* Inform the FW about changes to DCB configuration */
7039 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7040 	if (ret) {
7041 		dev_info(&pf->pdev->dev,
7042 			 "DCB Updated failed, err %pe aq_err %s\n",
7043 			 ERR_PTR(ret),
7044 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7045 		goto out;
7046 	}
7047 
7048 	/* Update the port DCBx configuration */
7049 	*old_cfg = *new_cfg;
7050 
7051 	/* Changes in configuration update VEB/VSI */
7052 	i40e_dcb_reconfigure(pf);
7053 out:
7054 	/* Re-start the VSIs if disabled */
7055 	if (need_reconfig) {
7056 		ret = i40e_resume_port_tx(pf);
7057 
7058 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7059 		/* In case of error no point in resuming VSIs */
7060 		if (ret)
7061 			goto err;
7062 
7063 		/* Wait for the PF's queues to be disabled */
7064 		ret = i40e_pf_wait_queues_disabled(pf);
7065 		if (ret) {
7066 			/* Schedule PF reset to recover */
7067 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7068 			i40e_service_event_schedule(pf);
7069 			goto err;
7070 		} else {
7071 			i40e_pf_unquiesce_all_vsi(pf);
7072 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7073 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7074 		}
7075 		/* registers are set, lets apply */
7076 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7077 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7078 	}
7079 
7080 err:
7081 	return ret;
7082 }
7083 
7084 /**
7085  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7086  * @pf: PF being queried
7087  *
7088  * Set default DCB configuration in case DCB is to be done in SW.
7089  **/
i40e_dcb_sw_default_config(struct i40e_pf * pf)7090 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7091 {
7092 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7093 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7094 	struct i40e_hw *hw = &pf->hw;
7095 	int err;
7096 
7097 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7098 		/* Update the local cached instance with TC0 ETS */
7099 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7100 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7101 		pf->tmp_cfg.etscfg.maxtcs = 0;
7102 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7103 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7104 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7105 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7106 		/* FW needs one App to configure HW */
7107 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7108 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7109 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7110 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7111 
7112 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7113 	}
7114 
7115 	memset(&ets_data, 0, sizeof(ets_data));
7116 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7117 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7118 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7119 
7120 	/* Enable ETS on the Physical port */
7121 	err = i40e_aq_config_switch_comp_ets
7122 		(hw, pf->mac_seid, &ets_data,
7123 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7124 	if (err) {
7125 		dev_info(&pf->pdev->dev,
7126 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7127 			 ERR_PTR(err),
7128 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7129 		err = -ENOENT;
7130 		goto out;
7131 	}
7132 
7133 	/* Update the local cached instance with TC0 ETS */
7134 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7135 	dcb_cfg->etscfg.cbs = 0;
7136 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7137 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7138 
7139 out:
7140 	return err;
7141 }
7142 
7143 /**
7144  * i40e_init_pf_dcb - Initialize DCB configuration
7145  * @pf: PF being configured
7146  *
7147  * Query the current DCB configuration and cache it
7148  * in the hardware structure
7149  **/
i40e_init_pf_dcb(struct i40e_pf * pf)7150 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7151 {
7152 	struct i40e_hw *hw = &pf->hw;
7153 	int err;
7154 
7155 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7156 	 * Also do not enable DCBx if FW LLDP agent is disabled
7157 	 */
7158 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7159 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7160 		err = I40E_NOT_SUPPORTED;
7161 		goto out;
7162 	}
7163 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7164 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7165 		err = i40e_dcb_sw_default_config(pf);
7166 		if (err) {
7167 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7168 			goto out;
7169 		}
7170 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7171 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7172 			       DCB_CAP_DCBX_VER_IEEE;
7173 		/* at init capable but disabled */
7174 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7175 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7176 		goto out;
7177 	}
7178 	err = i40e_init_dcb(hw, true);
7179 	if (!err) {
7180 		/* Device/Function is not DCBX capable */
7181 		if ((!hw->func_caps.dcb) ||
7182 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7183 			dev_info(&pf->pdev->dev,
7184 				 "DCBX offload is not supported or is disabled for this PF.\n");
7185 		} else {
7186 			/* When status is not DISABLED then DCBX in FW */
7187 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7188 				       DCB_CAP_DCBX_VER_IEEE;
7189 
7190 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7191 			/* Enable DCB tagging only when more than one TC
7192 			 * or explicitly disable if only one TC
7193 			 */
7194 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7195 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7196 			else
7197 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7198 			dev_dbg(&pf->pdev->dev,
7199 				"DCBX offload is supported for this PF.\n");
7200 		}
7201 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7202 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7203 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7204 	} else {
7205 		dev_info(&pf->pdev->dev,
7206 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7207 			 ERR_PTR(err),
7208 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7209 	}
7210 
7211 out:
7212 	return err;
7213 }
7214 #endif /* CONFIG_I40E_DCB */
7215 
7216 /**
7217  * i40e_print_link_message - print link up or down
7218  * @vsi: the VSI for which link needs a message
7219  * @isup: true of link is up, false otherwise
7220  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)7221 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7222 {
7223 	enum i40e_aq_link_speed new_speed;
7224 	struct i40e_pf *pf = vsi->back;
7225 	char *speed = "Unknown";
7226 	char *fc = "Unknown";
7227 	char *fec = "";
7228 	char *req_fec = "";
7229 	char *an = "";
7230 
7231 	if (isup)
7232 		new_speed = pf->hw.phy.link_info.link_speed;
7233 	else
7234 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7235 
7236 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7237 		return;
7238 	vsi->current_isup = isup;
7239 	vsi->current_speed = new_speed;
7240 	if (!isup) {
7241 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7242 		return;
7243 	}
7244 
7245 	/* Warn user if link speed on NPAR enabled partition is not at
7246 	 * least 10GB
7247 	 */
7248 	if (pf->hw.func_caps.npar_enable &&
7249 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7250 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7251 		netdev_warn(vsi->netdev,
7252 			    "The partition detected link speed that is less than 10Gbps\n");
7253 
7254 	switch (pf->hw.phy.link_info.link_speed) {
7255 	case I40E_LINK_SPEED_40GB:
7256 		speed = "40 G";
7257 		break;
7258 	case I40E_LINK_SPEED_20GB:
7259 		speed = "20 G";
7260 		break;
7261 	case I40E_LINK_SPEED_25GB:
7262 		speed = "25 G";
7263 		break;
7264 	case I40E_LINK_SPEED_10GB:
7265 		speed = "10 G";
7266 		break;
7267 	case I40E_LINK_SPEED_5GB:
7268 		speed = "5 G";
7269 		break;
7270 	case I40E_LINK_SPEED_2_5GB:
7271 		speed = "2.5 G";
7272 		break;
7273 	case I40E_LINK_SPEED_1GB:
7274 		speed = "1000 M";
7275 		break;
7276 	case I40E_LINK_SPEED_100MB:
7277 		speed = "100 M";
7278 		break;
7279 	default:
7280 		break;
7281 	}
7282 
7283 	switch (pf->hw.fc.current_mode) {
7284 	case I40E_FC_FULL:
7285 		fc = "RX/TX";
7286 		break;
7287 	case I40E_FC_TX_PAUSE:
7288 		fc = "TX";
7289 		break;
7290 	case I40E_FC_RX_PAUSE:
7291 		fc = "RX";
7292 		break;
7293 	default:
7294 		fc = "None";
7295 		break;
7296 	}
7297 
7298 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7299 		req_fec = "None";
7300 		fec = "None";
7301 		an = "False";
7302 
7303 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7304 			an = "True";
7305 
7306 		if (pf->hw.phy.link_info.fec_info &
7307 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7308 			fec = "CL74 FC-FEC/BASE-R";
7309 		else if (pf->hw.phy.link_info.fec_info &
7310 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7311 			fec = "CL108 RS-FEC";
7312 
7313 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7314 		 * both RS and FC are requested
7315 		 */
7316 		if (vsi->back->hw.phy.link_info.req_fec_info &
7317 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7318 			if (vsi->back->hw.phy.link_info.req_fec_info &
7319 			    I40E_AQ_REQUEST_FEC_RS)
7320 				req_fec = "CL108 RS-FEC";
7321 			else
7322 				req_fec = "CL74 FC-FEC/BASE-R";
7323 		}
7324 		netdev_info(vsi->netdev,
7325 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7326 			    speed, req_fec, fec, an, fc);
7327 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7328 		req_fec = "None";
7329 		fec = "None";
7330 		an = "False";
7331 
7332 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7333 			an = "True";
7334 
7335 		if (pf->hw.phy.link_info.fec_info &
7336 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7337 			fec = "CL74 FC-FEC/BASE-R";
7338 
7339 		if (pf->hw.phy.link_info.req_fec_info &
7340 		    I40E_AQ_REQUEST_FEC_KR)
7341 			req_fec = "CL74 FC-FEC/BASE-R";
7342 
7343 		netdev_info(vsi->netdev,
7344 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7345 			    speed, req_fec, fec, an, fc);
7346 	} else {
7347 		netdev_info(vsi->netdev,
7348 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7349 			    speed, fc);
7350 	}
7351 
7352 }
7353 
7354 /**
7355  * i40e_up_complete - Finish the last steps of bringing up a connection
7356  * @vsi: the VSI being configured
7357  **/
i40e_up_complete(struct i40e_vsi * vsi)7358 static int i40e_up_complete(struct i40e_vsi *vsi)
7359 {
7360 	struct i40e_pf *pf = vsi->back;
7361 	int err;
7362 
7363 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7364 		i40e_vsi_configure_msix(vsi);
7365 	else
7366 		i40e_configure_msi_and_legacy(vsi);
7367 
7368 	/* start rings */
7369 	err = i40e_vsi_start_rings(vsi);
7370 	if (err)
7371 		return err;
7372 
7373 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7374 	i40e_napi_enable_all(vsi);
7375 	i40e_vsi_enable_irq(vsi);
7376 
7377 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7378 	    (vsi->netdev)) {
7379 		i40e_print_link_message(vsi, true);
7380 		netif_tx_start_all_queues(vsi->netdev);
7381 		netif_carrier_on(vsi->netdev);
7382 	}
7383 
7384 	/* replay FDIR SB filters */
7385 	if (vsi->type == I40E_VSI_FDIR) {
7386 		/* reset fd counters */
7387 		pf->fd_add_err = 0;
7388 		pf->fd_atr_cnt = 0;
7389 		i40e_fdir_filter_restore(vsi);
7390 	}
7391 
7392 	/* On the next run of the service_task, notify any clients of the new
7393 	 * opened netdev
7394 	 */
7395 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7396 	i40e_service_event_schedule(pf);
7397 
7398 	return 0;
7399 }
7400 
7401 /**
7402  * i40e_vsi_reinit_locked - Reset the VSI
7403  * @vsi: the VSI being configured
7404  *
7405  * Rebuild the ring structs after some configuration
7406  * has changed, e.g. MTU size.
7407  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)7408 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7409 {
7410 	struct i40e_pf *pf = vsi->back;
7411 
7412 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7413 		usleep_range(1000, 2000);
7414 	i40e_down(vsi);
7415 
7416 	i40e_up(vsi);
7417 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7418 }
7419 
7420 /**
7421  * i40e_force_link_state - Force the link status
7422  * @pf: board private structure
7423  * @is_up: whether the link state should be forced up or down
7424  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)7425 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7426 {
7427 	struct i40e_aq_get_phy_abilities_resp abilities;
7428 	struct i40e_aq_set_phy_config config = {0};
7429 	bool non_zero_phy_type = is_up;
7430 	struct i40e_hw *hw = &pf->hw;
7431 	u64 mask;
7432 	u8 speed;
7433 	int err;
7434 
7435 	/* Card might've been put in an unstable state by other drivers
7436 	 * and applications, which causes incorrect speed values being
7437 	 * set on startup. In order to clear speed registers, we call
7438 	 * get_phy_capabilities twice, once to get initial state of
7439 	 * available speeds, and once to get current PHY config.
7440 	 */
7441 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7442 					   NULL);
7443 	if (err) {
7444 		dev_err(&pf->pdev->dev,
7445 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7446 			ERR_PTR(err),
7447 			i40e_aq_str(hw, hw->aq.asq_last_status));
7448 		return err;
7449 	}
7450 	speed = abilities.link_speed;
7451 
7452 	/* Get the current phy config */
7453 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7454 					   NULL);
7455 	if (err) {
7456 		dev_err(&pf->pdev->dev,
7457 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7458 			ERR_PTR(err),
7459 			i40e_aq_str(hw, hw->aq.asq_last_status));
7460 		return err;
7461 	}
7462 
7463 	/* If link needs to go up, but was not forced to go down,
7464 	 * and its speed values are OK, no need for a flap
7465 	 * if non_zero_phy_type was set, still need to force up
7466 	 */
7467 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7468 		non_zero_phy_type = true;
7469 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7470 		return I40E_SUCCESS;
7471 
7472 	/* To force link we need to set bits for all supported PHY types,
7473 	 * but there are now more than 32, so we need to split the bitmap
7474 	 * across two fields.
7475 	 */
7476 	mask = I40E_PHY_TYPES_BITMASK;
7477 	config.phy_type =
7478 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7479 	config.phy_type_ext =
7480 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7481 	/* Copy the old settings, except of phy_type */
7482 	config.abilities = abilities.abilities;
7483 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7484 		if (is_up)
7485 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7486 		else
7487 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7488 	}
7489 	if (abilities.link_speed != 0)
7490 		config.link_speed = abilities.link_speed;
7491 	else
7492 		config.link_speed = speed;
7493 	config.eee_capability = abilities.eee_capability;
7494 	config.eeer = abilities.eeer_val;
7495 	config.low_power_ctrl = abilities.d3_lpan;
7496 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7497 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7498 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7499 
7500 	if (err) {
7501 		dev_err(&pf->pdev->dev,
7502 			"set phy config ret =  %pe last_status =  %s\n",
7503 			ERR_PTR(err),
7504 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7505 		return err;
7506 	}
7507 
7508 	/* Update the link info */
7509 	err = i40e_update_link_info(hw);
7510 	if (err) {
7511 		/* Wait a little bit (on 40G cards it sometimes takes a really
7512 		 * long time for link to come back from the atomic reset)
7513 		 * and try once more
7514 		 */
7515 		msleep(1000);
7516 		i40e_update_link_info(hw);
7517 	}
7518 
7519 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7520 
7521 	return I40E_SUCCESS;
7522 }
7523 
7524 /**
7525  * i40e_up - Bring the connection back up after being down
7526  * @vsi: the VSI being configured
7527  **/
i40e_up(struct i40e_vsi * vsi)7528 int i40e_up(struct i40e_vsi *vsi)
7529 {
7530 	int err;
7531 
7532 	if (vsi->type == I40E_VSI_MAIN &&
7533 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7534 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7535 		i40e_force_link_state(vsi->back, true);
7536 
7537 	err = i40e_vsi_configure(vsi);
7538 	if (!err)
7539 		err = i40e_up_complete(vsi);
7540 
7541 	return err;
7542 }
7543 
7544 /**
7545  * i40e_down - Shutdown the connection processing
7546  * @vsi: the VSI being stopped
7547  **/
i40e_down(struct i40e_vsi * vsi)7548 void i40e_down(struct i40e_vsi *vsi)
7549 {
7550 	int i;
7551 
7552 	/* It is assumed that the caller of this function
7553 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7554 	 */
7555 	if (vsi->netdev) {
7556 		netif_carrier_off(vsi->netdev);
7557 		netif_tx_disable(vsi->netdev);
7558 	}
7559 	i40e_vsi_disable_irq(vsi);
7560 	i40e_vsi_stop_rings(vsi);
7561 	if (vsi->type == I40E_VSI_MAIN &&
7562 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7563 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7564 		i40e_force_link_state(vsi->back, false);
7565 	i40e_napi_disable_all(vsi);
7566 
7567 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7568 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7569 		if (i40e_enabled_xdp_vsi(vsi)) {
7570 			/* Make sure that in-progress ndo_xdp_xmit and
7571 			 * ndo_xsk_wakeup calls are completed.
7572 			 */
7573 			synchronize_rcu();
7574 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7575 		}
7576 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7577 	}
7578 
7579 }
7580 
7581 /**
7582  * i40e_validate_mqprio_qopt- validate queue mapping info
7583  * @vsi: the VSI being configured
7584  * @mqprio_qopt: queue parametrs
7585  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7586 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7587 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7588 {
7589 	u64 sum_max_rate = 0;
7590 	u64 max_rate = 0;
7591 	int i;
7592 
7593 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7594 	    mqprio_qopt->qopt.num_tc < 1 ||
7595 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7596 		return -EINVAL;
7597 	for (i = 0; ; i++) {
7598 		if (!mqprio_qopt->qopt.count[i])
7599 			return -EINVAL;
7600 		if (mqprio_qopt->min_rate[i]) {
7601 			dev_err(&vsi->back->pdev->dev,
7602 				"Invalid min tx rate (greater than 0) specified\n");
7603 			return -EINVAL;
7604 		}
7605 		max_rate = mqprio_qopt->max_rate[i];
7606 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7607 		sum_max_rate += max_rate;
7608 
7609 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7610 			break;
7611 		if (mqprio_qopt->qopt.offset[i + 1] !=
7612 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7613 			return -EINVAL;
7614 	}
7615 	if (vsi->num_queue_pairs <
7616 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7617 		dev_err(&vsi->back->pdev->dev,
7618 			"Failed to create traffic channel, insufficient number of queues.\n");
7619 		return -EINVAL;
7620 	}
7621 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7622 		dev_err(&vsi->back->pdev->dev,
7623 			"Invalid max tx rate specified\n");
7624 		return -EINVAL;
7625 	}
7626 	return 0;
7627 }
7628 
7629 /**
7630  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7631  * @vsi: the VSI being configured
7632  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7633 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7634 {
7635 	u16 qcount;
7636 	int i;
7637 
7638 	/* Only TC0 is enabled */
7639 	vsi->tc_config.numtc = 1;
7640 	vsi->tc_config.enabled_tc = 1;
7641 	qcount = min_t(int, vsi->alloc_queue_pairs,
7642 		       i40e_pf_get_max_q_per_tc(vsi->back));
7643 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7644 		/* For the TC that is not enabled set the offset to default
7645 		 * queue and allocate one queue for the given TC.
7646 		 */
7647 		vsi->tc_config.tc_info[i].qoffset = 0;
7648 		if (i == 0)
7649 			vsi->tc_config.tc_info[i].qcount = qcount;
7650 		else
7651 			vsi->tc_config.tc_info[i].qcount = 1;
7652 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7653 	}
7654 }
7655 
7656 /**
7657  * i40e_del_macvlan_filter
7658  * @hw: pointer to the HW structure
7659  * @seid: seid of the channel VSI
7660  * @macaddr: the mac address to apply as a filter
7661  * @aq_err: store the admin Q error
7662  *
7663  * This function deletes a mac filter on the channel VSI which serves as the
7664  * macvlan. Returns 0 on success.
7665  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7666 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7667 				   const u8 *macaddr, int *aq_err)
7668 {
7669 	struct i40e_aqc_remove_macvlan_element_data element;
7670 	int status;
7671 
7672 	memset(&element, 0, sizeof(element));
7673 	ether_addr_copy(element.mac_addr, macaddr);
7674 	element.vlan_tag = 0;
7675 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7676 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7677 	*aq_err = hw->aq.asq_last_status;
7678 
7679 	return status;
7680 }
7681 
7682 /**
7683  * i40e_add_macvlan_filter
7684  * @hw: pointer to the HW structure
7685  * @seid: seid of the channel VSI
7686  * @macaddr: the mac address to apply as a filter
7687  * @aq_err: store the admin Q error
7688  *
7689  * This function adds a mac filter on the channel VSI which serves as the
7690  * macvlan. Returns 0 on success.
7691  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7692 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7693 				   const u8 *macaddr, int *aq_err)
7694 {
7695 	struct i40e_aqc_add_macvlan_element_data element;
7696 	u16 cmd_flags = 0;
7697 	int status;
7698 
7699 	ether_addr_copy(element.mac_addr, macaddr);
7700 	element.vlan_tag = 0;
7701 	element.queue_number = 0;
7702 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7703 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7704 	element.flags = cpu_to_le16(cmd_flags);
7705 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7706 	*aq_err = hw->aq.asq_last_status;
7707 
7708 	return status;
7709 }
7710 
7711 /**
7712  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7713  * @vsi: the VSI we want to access
7714  * @ch: the channel we want to access
7715  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7716 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7717 {
7718 	struct i40e_ring *tx_ring, *rx_ring;
7719 	u16 pf_q;
7720 	int i;
7721 
7722 	for (i = 0; i < ch->num_queue_pairs; i++) {
7723 		pf_q = ch->base_queue + i;
7724 		tx_ring = vsi->tx_rings[pf_q];
7725 		tx_ring->ch = NULL;
7726 		rx_ring = vsi->rx_rings[pf_q];
7727 		rx_ring->ch = NULL;
7728 	}
7729 }
7730 
7731 /**
7732  * i40e_free_macvlan_channels
7733  * @vsi: the VSI we want to access
7734  *
7735  * This function frees the Qs of the channel VSI from
7736  * the stack and also deletes the channel VSIs which
7737  * serve as macvlans.
7738  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7739 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7740 {
7741 	struct i40e_channel *ch, *ch_tmp;
7742 	int ret;
7743 
7744 	if (list_empty(&vsi->macvlan_list))
7745 		return;
7746 
7747 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7748 		struct i40e_vsi *parent_vsi;
7749 
7750 		if (i40e_is_channel_macvlan(ch)) {
7751 			i40e_reset_ch_rings(vsi, ch);
7752 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7753 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7754 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7755 			kfree(ch->fwd);
7756 			ch->fwd = NULL;
7757 		}
7758 
7759 		list_del(&ch->list);
7760 		parent_vsi = ch->parent_vsi;
7761 		if (!parent_vsi || !ch->initialized) {
7762 			kfree(ch);
7763 			continue;
7764 		}
7765 
7766 		/* remove the VSI */
7767 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7768 					     NULL);
7769 		if (ret)
7770 			dev_err(&vsi->back->pdev->dev,
7771 				"unable to remove channel (%d) for parent VSI(%d)\n",
7772 				ch->seid, parent_vsi->seid);
7773 		kfree(ch);
7774 	}
7775 	vsi->macvlan_cnt = 0;
7776 }
7777 
7778 /**
7779  * i40e_fwd_ring_up - bring the macvlan device up
7780  * @vsi: the VSI we want to access
7781  * @vdev: macvlan netdevice
7782  * @fwd: the private fwd structure
7783  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7784 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7785 			    struct i40e_fwd_adapter *fwd)
7786 {
7787 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7788 	int ret = 0, num_tc = 1,  i, aq_err;
7789 	struct i40e_pf *pf = vsi->back;
7790 	struct i40e_hw *hw = &pf->hw;
7791 
7792 	/* Go through the list and find an available channel */
7793 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7794 		if (!i40e_is_channel_macvlan(iter)) {
7795 			iter->fwd = fwd;
7796 			/* record configuration for macvlan interface in vdev */
7797 			for (i = 0; i < num_tc; i++)
7798 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7799 							     i,
7800 							     iter->num_queue_pairs,
7801 							     iter->base_queue);
7802 			for (i = 0; i < iter->num_queue_pairs; i++) {
7803 				struct i40e_ring *tx_ring, *rx_ring;
7804 				u16 pf_q;
7805 
7806 				pf_q = iter->base_queue + i;
7807 
7808 				/* Get to TX ring ptr */
7809 				tx_ring = vsi->tx_rings[pf_q];
7810 				tx_ring->ch = iter;
7811 
7812 				/* Get the RX ring ptr */
7813 				rx_ring = vsi->rx_rings[pf_q];
7814 				rx_ring->ch = iter;
7815 			}
7816 			ch = iter;
7817 			break;
7818 		}
7819 	}
7820 
7821 	if (!ch)
7822 		return -EINVAL;
7823 
7824 	/* Guarantee all rings are updated before we update the
7825 	 * MAC address filter.
7826 	 */
7827 	wmb();
7828 
7829 	/* Add a mac filter */
7830 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7831 	if (ret) {
7832 		/* if we cannot add the MAC rule then disable the offload */
7833 		macvlan_release_l2fw_offload(vdev);
7834 		for (i = 0; i < ch->num_queue_pairs; i++) {
7835 			struct i40e_ring *rx_ring;
7836 			u16 pf_q;
7837 
7838 			pf_q = ch->base_queue + i;
7839 			rx_ring = vsi->rx_rings[pf_q];
7840 			rx_ring->netdev = NULL;
7841 		}
7842 		dev_info(&pf->pdev->dev,
7843 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7844 			  ERR_PTR(ret),
7845 			  i40e_aq_str(hw, aq_err));
7846 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7847 	}
7848 
7849 	return ret;
7850 }
7851 
7852 /**
7853  * i40e_setup_macvlans - create the channels which will be macvlans
7854  * @vsi: the VSI we want to access
7855  * @macvlan_cnt: no. of macvlans to be setup
7856  * @qcnt: no. of Qs per macvlan
7857  * @vdev: macvlan netdevice
7858  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7859 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7860 			       struct net_device *vdev)
7861 {
7862 	struct i40e_pf *pf = vsi->back;
7863 	struct i40e_hw *hw = &pf->hw;
7864 	struct i40e_vsi_context ctxt;
7865 	u16 sections, qmap, num_qps;
7866 	struct i40e_channel *ch;
7867 	int i, pow, ret = 0;
7868 	u8 offset = 0;
7869 
7870 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7871 		return -EINVAL;
7872 
7873 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7874 
7875 	/* find the next higher power-of-2 of num queue pairs */
7876 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7877 
7878 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7879 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7880 
7881 	/* Setup context bits for the main VSI */
7882 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7883 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7884 	memset(&ctxt, 0, sizeof(ctxt));
7885 	ctxt.seid = vsi->seid;
7886 	ctxt.pf_num = vsi->back->hw.pf_id;
7887 	ctxt.vf_num = 0;
7888 	ctxt.uplink_seid = vsi->uplink_seid;
7889 	ctxt.info = vsi->info;
7890 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7891 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7892 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7893 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7894 
7895 	/* Reconfigure RSS for main VSI with new max queue count */
7896 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7897 	ret = i40e_vsi_config_rss(vsi);
7898 	if (ret) {
7899 		dev_info(&pf->pdev->dev,
7900 			 "Failed to reconfig RSS for num_queues (%u)\n",
7901 			 vsi->rss_size);
7902 		return ret;
7903 	}
7904 	vsi->reconfig_rss = true;
7905 	dev_dbg(&vsi->back->pdev->dev,
7906 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7907 	vsi->next_base_queue = num_qps;
7908 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7909 
7910 	/* Update the VSI after updating the VSI queue-mapping
7911 	 * information
7912 	 */
7913 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7914 	if (ret) {
7915 		dev_info(&pf->pdev->dev,
7916 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7917 			 ERR_PTR(ret),
7918 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7919 		return ret;
7920 	}
7921 	/* update the local VSI info with updated queue map */
7922 	i40e_vsi_update_queue_map(vsi, &ctxt);
7923 	vsi->info.valid_sections = 0;
7924 
7925 	/* Create channels for macvlans */
7926 	INIT_LIST_HEAD(&vsi->macvlan_list);
7927 	for (i = 0; i < macvlan_cnt; i++) {
7928 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7929 		if (!ch) {
7930 			ret = -ENOMEM;
7931 			goto err_free;
7932 		}
7933 		INIT_LIST_HEAD(&ch->list);
7934 		ch->num_queue_pairs = qcnt;
7935 		if (!i40e_setup_channel(pf, vsi, ch)) {
7936 			ret = -EINVAL;
7937 			kfree(ch);
7938 			goto err_free;
7939 		}
7940 		ch->parent_vsi = vsi;
7941 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7942 		vsi->macvlan_cnt++;
7943 		list_add_tail(&ch->list, &vsi->macvlan_list);
7944 	}
7945 
7946 	return ret;
7947 
7948 err_free:
7949 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7950 	i40e_free_macvlan_channels(vsi);
7951 
7952 	return ret;
7953 }
7954 
7955 /**
7956  * i40e_fwd_add - configure macvlans
7957  * @netdev: net device to configure
7958  * @vdev: macvlan netdevice
7959  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7960 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7961 {
7962 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7963 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7964 	struct i40e_vsi *vsi = np->vsi;
7965 	struct i40e_pf *pf = vsi->back;
7966 	struct i40e_fwd_adapter *fwd;
7967 	int avail_macvlan, ret;
7968 
7969 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7970 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7971 		return ERR_PTR(-EINVAL);
7972 	}
7973 	if (i40e_is_tc_mqprio_enabled(pf)) {
7974 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7975 		return ERR_PTR(-EINVAL);
7976 	}
7977 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7978 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7979 		return ERR_PTR(-EINVAL);
7980 	}
7981 
7982 	/* The macvlan device has to be a single Q device so that the
7983 	 * tc_to_txq field can be reused to pick the tx queue.
7984 	 */
7985 	if (netif_is_multiqueue(vdev))
7986 		return ERR_PTR(-ERANGE);
7987 
7988 	if (!vsi->macvlan_cnt) {
7989 		/* reserve bit 0 for the pf device */
7990 		set_bit(0, vsi->fwd_bitmask);
7991 
7992 		/* Try to reserve as many queues as possible for macvlans. First
7993 		 * reserve 3/4th of max vectors, then half, then quarter and
7994 		 * calculate Qs per macvlan as you go
7995 		 */
7996 		vectors = pf->num_lan_msix;
7997 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7998 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7999 			q_per_macvlan = 4;
8000 			macvlan_cnt = (vectors - 32) / 4;
8001 		} else if (vectors <= 64 && vectors > 32) {
8002 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8003 			q_per_macvlan = 2;
8004 			macvlan_cnt = (vectors - 16) / 2;
8005 		} else if (vectors <= 32 && vectors > 16) {
8006 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8007 			q_per_macvlan = 1;
8008 			macvlan_cnt = vectors - 16;
8009 		} else if (vectors <= 16 && vectors > 8) {
8010 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8011 			q_per_macvlan = 1;
8012 			macvlan_cnt = vectors - 8;
8013 		} else {
8014 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8015 			q_per_macvlan = 1;
8016 			macvlan_cnt = vectors - 1;
8017 		}
8018 
8019 		if (macvlan_cnt == 0)
8020 			return ERR_PTR(-EBUSY);
8021 
8022 		/* Quiesce VSI queues */
8023 		i40e_quiesce_vsi(vsi);
8024 
8025 		/* sets up the macvlans but does not "enable" them */
8026 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8027 					  vdev);
8028 		if (ret)
8029 			return ERR_PTR(ret);
8030 
8031 		/* Unquiesce VSI */
8032 		i40e_unquiesce_vsi(vsi);
8033 	}
8034 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8035 					    vsi->macvlan_cnt);
8036 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8037 		return ERR_PTR(-EBUSY);
8038 
8039 	/* create the fwd struct */
8040 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8041 	if (!fwd)
8042 		return ERR_PTR(-ENOMEM);
8043 
8044 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8045 	fwd->bit_no = avail_macvlan;
8046 	netdev_set_sb_channel(vdev, avail_macvlan);
8047 	fwd->netdev = vdev;
8048 
8049 	if (!netif_running(netdev))
8050 		return fwd;
8051 
8052 	/* Set fwd ring up */
8053 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8054 	if (ret) {
8055 		/* unbind the queues and drop the subordinate channel config */
8056 		netdev_unbind_sb_channel(netdev, vdev);
8057 		netdev_set_sb_channel(vdev, 0);
8058 
8059 		kfree(fwd);
8060 		return ERR_PTR(-EINVAL);
8061 	}
8062 
8063 	return fwd;
8064 }
8065 
8066 /**
8067  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8068  * @vsi: the VSI we want to access
8069  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)8070 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8071 {
8072 	struct i40e_channel *ch, *ch_tmp;
8073 	struct i40e_pf *pf = vsi->back;
8074 	struct i40e_hw *hw = &pf->hw;
8075 	int aq_err, ret = 0;
8076 
8077 	if (list_empty(&vsi->macvlan_list))
8078 		return;
8079 
8080 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8081 		if (i40e_is_channel_macvlan(ch)) {
8082 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8083 						      i40e_channel_mac(ch),
8084 						      &aq_err);
8085 			if (!ret) {
8086 				/* Reset queue contexts */
8087 				i40e_reset_ch_rings(vsi, ch);
8088 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8089 				netdev_unbind_sb_channel(vsi->netdev,
8090 							 ch->fwd->netdev);
8091 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8092 				kfree(ch->fwd);
8093 				ch->fwd = NULL;
8094 			}
8095 		}
8096 	}
8097 }
8098 
8099 /**
8100  * i40e_fwd_del - delete macvlan interfaces
8101  * @netdev: net device to configure
8102  * @vdev: macvlan netdevice
8103  */
i40e_fwd_del(struct net_device * netdev,void * vdev)8104 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8105 {
8106 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8107 	struct i40e_fwd_adapter *fwd = vdev;
8108 	struct i40e_channel *ch, *ch_tmp;
8109 	struct i40e_vsi *vsi = np->vsi;
8110 	struct i40e_pf *pf = vsi->back;
8111 	struct i40e_hw *hw = &pf->hw;
8112 	int aq_err, ret = 0;
8113 
8114 	/* Find the channel associated with the macvlan and del mac filter */
8115 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8116 		if (i40e_is_channel_macvlan(ch) &&
8117 		    ether_addr_equal(i40e_channel_mac(ch),
8118 				     fwd->netdev->dev_addr)) {
8119 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8120 						      i40e_channel_mac(ch),
8121 						      &aq_err);
8122 			if (!ret) {
8123 				/* Reset queue contexts */
8124 				i40e_reset_ch_rings(vsi, ch);
8125 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8126 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8127 				netdev_set_sb_channel(fwd->netdev, 0);
8128 				kfree(ch->fwd);
8129 				ch->fwd = NULL;
8130 			} else {
8131 				dev_info(&pf->pdev->dev,
8132 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8133 					  ERR_PTR(ret),
8134 					  i40e_aq_str(hw, aq_err));
8135 			}
8136 			break;
8137 		}
8138 	}
8139 }
8140 
8141 /**
8142  * i40e_setup_tc - configure multiple traffic classes
8143  * @netdev: net device to configure
8144  * @type_data: tc offload data
8145  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)8146 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8147 {
8148 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8149 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8150 	struct i40e_vsi *vsi = np->vsi;
8151 	struct i40e_pf *pf = vsi->back;
8152 	u8 enabled_tc = 0, num_tc, hw;
8153 	bool need_reset = false;
8154 	int old_queue_pairs;
8155 	int ret = -EINVAL;
8156 	u16 mode;
8157 	int i;
8158 
8159 	old_queue_pairs = vsi->num_queue_pairs;
8160 	num_tc = mqprio_qopt->qopt.num_tc;
8161 	hw = mqprio_qopt->qopt.hw;
8162 	mode = mqprio_qopt->mode;
8163 	if (!hw) {
8164 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8165 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8166 		goto config_tc;
8167 	}
8168 
8169 	/* Check if MFP enabled */
8170 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8171 		netdev_info(netdev,
8172 			    "Configuring TC not supported in MFP mode\n");
8173 		return ret;
8174 	}
8175 	switch (mode) {
8176 	case TC_MQPRIO_MODE_DCB:
8177 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8178 
8179 		/* Check if DCB enabled to continue */
8180 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8181 			netdev_info(netdev,
8182 				    "DCB is not enabled for adapter\n");
8183 			return ret;
8184 		}
8185 
8186 		/* Check whether tc count is within enabled limit */
8187 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8188 			netdev_info(netdev,
8189 				    "TC count greater than enabled on link for adapter\n");
8190 			return ret;
8191 		}
8192 		break;
8193 	case TC_MQPRIO_MODE_CHANNEL:
8194 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8195 			netdev_info(netdev,
8196 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8197 			return ret;
8198 		}
8199 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8200 			return ret;
8201 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8202 		if (ret)
8203 			return ret;
8204 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8205 		       sizeof(*mqprio_qopt));
8206 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8207 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8208 		break;
8209 	default:
8210 		return -EINVAL;
8211 	}
8212 
8213 config_tc:
8214 	/* Generate TC map for number of tc requested */
8215 	for (i = 0; i < num_tc; i++)
8216 		enabled_tc |= BIT(i);
8217 
8218 	/* Requesting same TC configuration as already enabled */
8219 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8220 	    mode != TC_MQPRIO_MODE_CHANNEL)
8221 		return 0;
8222 
8223 	/* Quiesce VSI queues */
8224 	i40e_quiesce_vsi(vsi);
8225 
8226 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8227 		i40e_remove_queue_channels(vsi);
8228 
8229 	/* Configure VSI for enabled TCs */
8230 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8231 	if (ret) {
8232 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8233 			    vsi->seid);
8234 		need_reset = true;
8235 		goto exit;
8236 	} else if (enabled_tc &&
8237 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8238 		netdev_info(netdev,
8239 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8240 			    vsi->tc_config.tc_info[0].qcount);
8241 		ret = -EINVAL;
8242 		need_reset = true;
8243 		goto exit;
8244 	}
8245 
8246 	dev_info(&vsi->back->pdev->dev,
8247 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8248 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8249 
8250 	if (i40e_is_tc_mqprio_enabled(pf)) {
8251 		if (vsi->mqprio_qopt.max_rate[0]) {
8252 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8253 						  vsi->mqprio_qopt.max_rate[0]);
8254 
8255 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8256 			if (!ret) {
8257 				u64 credits = max_tx_rate;
8258 
8259 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8260 				dev_dbg(&vsi->back->pdev->dev,
8261 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8262 					max_tx_rate,
8263 					credits,
8264 					vsi->seid);
8265 			} else {
8266 				need_reset = true;
8267 				goto exit;
8268 			}
8269 		}
8270 		ret = i40e_configure_queue_channels(vsi);
8271 		if (ret) {
8272 			vsi->num_queue_pairs = old_queue_pairs;
8273 			netdev_info(netdev,
8274 				    "Failed configuring queue channels\n");
8275 			need_reset = true;
8276 			goto exit;
8277 		}
8278 	}
8279 
8280 exit:
8281 	/* Reset the configuration data to defaults, only TC0 is enabled */
8282 	if (need_reset) {
8283 		i40e_vsi_set_default_tc_config(vsi);
8284 		need_reset = false;
8285 	}
8286 
8287 	/* Unquiesce VSI */
8288 	i40e_unquiesce_vsi(vsi);
8289 	return ret;
8290 }
8291 
8292 /**
8293  * i40e_set_cld_element - sets cloud filter element data
8294  * @filter: cloud filter rule
8295  * @cld: ptr to cloud filter element data
8296  *
8297  * This is helper function to copy data into cloud filter element
8298  **/
8299 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)8300 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8301 		     struct i40e_aqc_cloud_filters_element_data *cld)
8302 {
8303 	u32 ipa;
8304 	int i;
8305 
8306 	memset(cld, 0, sizeof(*cld));
8307 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8308 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8309 
8310 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8311 		return;
8312 
8313 	if (filter->n_proto == ETH_P_IPV6) {
8314 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8315 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8316 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8317 
8318 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8319 		}
8320 	} else {
8321 		ipa = be32_to_cpu(filter->dst_ipv4);
8322 
8323 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8324 	}
8325 
8326 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8327 
8328 	/* tenant_id is not supported by FW now, once the support is enabled
8329 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8330 	 */
8331 	if (filter->tenant_id)
8332 		return;
8333 }
8334 
8335 /**
8336  * i40e_add_del_cloud_filter - Add/del cloud filter
8337  * @vsi: pointer to VSI
8338  * @filter: cloud filter rule
8339  * @add: if true, add, if false, delete
8340  *
8341  * Add or delete a cloud filter for a specific flow spec.
8342  * Returns 0 if the filter were successfully added.
8343  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8344 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8345 			      struct i40e_cloud_filter *filter, bool add)
8346 {
8347 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8348 	struct i40e_pf *pf = vsi->back;
8349 	int ret;
8350 	static const u16 flag_table[128] = {
8351 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8352 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8353 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8354 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8355 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8356 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8357 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8358 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8359 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8360 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8361 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8362 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8363 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8364 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8365 	};
8366 
8367 	if (filter->flags >= ARRAY_SIZE(flag_table))
8368 		return I40E_ERR_CONFIG;
8369 
8370 	memset(&cld_filter, 0, sizeof(cld_filter));
8371 
8372 	/* copy element needed to add cloud filter from filter */
8373 	i40e_set_cld_element(filter, &cld_filter);
8374 
8375 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8376 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8377 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8378 
8379 	if (filter->n_proto == ETH_P_IPV6)
8380 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8381 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8382 	else
8383 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8384 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8385 
8386 	if (add)
8387 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8388 						&cld_filter, 1);
8389 	else
8390 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8391 						&cld_filter, 1);
8392 	if (ret)
8393 		dev_dbg(&pf->pdev->dev,
8394 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8395 			add ? "add" : "delete", filter->dst_port, ret,
8396 			pf->hw.aq.asq_last_status);
8397 	else
8398 		dev_info(&pf->pdev->dev,
8399 			 "%s cloud filter for VSI: %d\n",
8400 			 add ? "Added" : "Deleted", filter->seid);
8401 	return ret;
8402 }
8403 
8404 /**
8405  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8406  * @vsi: pointer to VSI
8407  * @filter: cloud filter rule
8408  * @add: if true, add, if false, delete
8409  *
8410  * Add or delete a cloud filter for a specific flow spec using big buffer.
8411  * Returns 0 if the filter were successfully added.
8412  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)8413 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8414 				      struct i40e_cloud_filter *filter,
8415 				      bool add)
8416 {
8417 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8418 	struct i40e_pf *pf = vsi->back;
8419 	int ret;
8420 
8421 	/* Both (src/dst) valid mac_addr are not supported */
8422 	if ((is_valid_ether_addr(filter->dst_mac) &&
8423 	     is_valid_ether_addr(filter->src_mac)) ||
8424 	    (is_multicast_ether_addr(filter->dst_mac) &&
8425 	     is_multicast_ether_addr(filter->src_mac)))
8426 		return -EOPNOTSUPP;
8427 
8428 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8429 	 * ports are not supported via big buffer now.
8430 	 */
8431 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8432 		return -EOPNOTSUPP;
8433 
8434 	/* adding filter using src_port/src_ip is not supported at this stage */
8435 	if (filter->src_port ||
8436 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8437 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8438 		return -EOPNOTSUPP;
8439 
8440 	memset(&cld_filter, 0, sizeof(cld_filter));
8441 
8442 	/* copy element needed to add cloud filter from filter */
8443 	i40e_set_cld_element(filter, &cld_filter.element);
8444 
8445 	if (is_valid_ether_addr(filter->dst_mac) ||
8446 	    is_valid_ether_addr(filter->src_mac) ||
8447 	    is_multicast_ether_addr(filter->dst_mac) ||
8448 	    is_multicast_ether_addr(filter->src_mac)) {
8449 		/* MAC + IP : unsupported mode */
8450 		if (filter->dst_ipv4)
8451 			return -EOPNOTSUPP;
8452 
8453 		/* since we validated that L4 port must be valid before
8454 		 * we get here, start with respective "flags" value
8455 		 * and update if vlan is present or not
8456 		 */
8457 		cld_filter.element.flags =
8458 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8459 
8460 		if (filter->vlan_id) {
8461 			cld_filter.element.flags =
8462 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8463 		}
8464 
8465 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8466 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8467 		cld_filter.element.flags =
8468 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8469 		if (filter->n_proto == ETH_P_IPV6)
8470 			cld_filter.element.flags |=
8471 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8472 		else
8473 			cld_filter.element.flags |=
8474 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8475 	} else {
8476 		dev_err(&pf->pdev->dev,
8477 			"either mac or ip has to be valid for cloud filter\n");
8478 		return -EINVAL;
8479 	}
8480 
8481 	/* Now copy L4 port in Byte 6..7 in general fields */
8482 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8483 						be16_to_cpu(filter->dst_port);
8484 
8485 	if (add) {
8486 		/* Validate current device switch mode, change if necessary */
8487 		ret = i40e_validate_and_set_switch_mode(vsi);
8488 		if (ret) {
8489 			dev_err(&pf->pdev->dev,
8490 				"failed to set switch mode, ret %d\n",
8491 				ret);
8492 			return ret;
8493 		}
8494 
8495 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8496 						   &cld_filter, 1);
8497 	} else {
8498 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8499 						   &cld_filter, 1);
8500 	}
8501 
8502 	if (ret)
8503 		dev_dbg(&pf->pdev->dev,
8504 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8505 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8506 	else
8507 		dev_info(&pf->pdev->dev,
8508 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8509 			 add ? "add" : "delete", filter->seid,
8510 			 ntohs(filter->dst_port));
8511 	return ret;
8512 }
8513 
8514 /**
8515  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8516  * @vsi: Pointer to VSI
8517  * @f: Pointer to struct flow_cls_offload
8518  * @filter: Pointer to cloud filter structure
8519  *
8520  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)8521 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8522 				 struct flow_cls_offload *f,
8523 				 struct i40e_cloud_filter *filter)
8524 {
8525 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8526 	struct flow_dissector *dissector = rule->match.dissector;
8527 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8528 	struct i40e_pf *pf = vsi->back;
8529 	u8 field_flags = 0;
8530 
8531 	if (dissector->used_keys &
8532 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8533 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8534 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8535 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8536 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8537 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8538 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8539 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8540 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8541 			dissector->used_keys);
8542 		return -EOPNOTSUPP;
8543 	}
8544 
8545 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8546 		struct flow_match_enc_keyid match;
8547 
8548 		flow_rule_match_enc_keyid(rule, &match);
8549 		if (match.mask->keyid != 0)
8550 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8551 
8552 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8553 	}
8554 
8555 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8556 		struct flow_match_basic match;
8557 
8558 		flow_rule_match_basic(rule, &match);
8559 		n_proto_key = ntohs(match.key->n_proto);
8560 		n_proto_mask = ntohs(match.mask->n_proto);
8561 
8562 		if (n_proto_key == ETH_P_ALL) {
8563 			n_proto_key = 0;
8564 			n_proto_mask = 0;
8565 		}
8566 		filter->n_proto = n_proto_key & n_proto_mask;
8567 		filter->ip_proto = match.key->ip_proto;
8568 	}
8569 
8570 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8571 		struct flow_match_eth_addrs match;
8572 
8573 		flow_rule_match_eth_addrs(rule, &match);
8574 
8575 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8576 		if (!is_zero_ether_addr(match.mask->dst)) {
8577 			if (is_broadcast_ether_addr(match.mask->dst)) {
8578 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8579 			} else {
8580 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8581 					match.mask->dst);
8582 				return I40E_ERR_CONFIG;
8583 			}
8584 		}
8585 
8586 		if (!is_zero_ether_addr(match.mask->src)) {
8587 			if (is_broadcast_ether_addr(match.mask->src)) {
8588 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8589 			} else {
8590 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8591 					match.mask->src);
8592 				return I40E_ERR_CONFIG;
8593 			}
8594 		}
8595 		ether_addr_copy(filter->dst_mac, match.key->dst);
8596 		ether_addr_copy(filter->src_mac, match.key->src);
8597 	}
8598 
8599 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8600 		struct flow_match_vlan match;
8601 
8602 		flow_rule_match_vlan(rule, &match);
8603 		if (match.mask->vlan_id) {
8604 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8605 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8606 
8607 			} else {
8608 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8609 					match.mask->vlan_id);
8610 				return I40E_ERR_CONFIG;
8611 			}
8612 		}
8613 
8614 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8615 	}
8616 
8617 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8618 		struct flow_match_control match;
8619 
8620 		flow_rule_match_control(rule, &match);
8621 		addr_type = match.key->addr_type;
8622 	}
8623 
8624 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8625 		struct flow_match_ipv4_addrs match;
8626 
8627 		flow_rule_match_ipv4_addrs(rule, &match);
8628 		if (match.mask->dst) {
8629 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8630 				field_flags |= I40E_CLOUD_FIELD_IIP;
8631 			} else {
8632 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8633 					&match.mask->dst);
8634 				return I40E_ERR_CONFIG;
8635 			}
8636 		}
8637 
8638 		if (match.mask->src) {
8639 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8640 				field_flags |= I40E_CLOUD_FIELD_IIP;
8641 			} else {
8642 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8643 					&match.mask->src);
8644 				return I40E_ERR_CONFIG;
8645 			}
8646 		}
8647 
8648 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8649 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8650 			return I40E_ERR_CONFIG;
8651 		}
8652 		filter->dst_ipv4 = match.key->dst;
8653 		filter->src_ipv4 = match.key->src;
8654 	}
8655 
8656 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8657 		struct flow_match_ipv6_addrs match;
8658 
8659 		flow_rule_match_ipv6_addrs(rule, &match);
8660 
8661 		/* src and dest IPV6 address should not be LOOPBACK
8662 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8663 		 */
8664 		if (ipv6_addr_loopback(&match.key->dst) ||
8665 		    ipv6_addr_loopback(&match.key->src)) {
8666 			dev_err(&pf->pdev->dev,
8667 				"Bad ipv6, addr is LOOPBACK\n");
8668 			return I40E_ERR_CONFIG;
8669 		}
8670 		if (!ipv6_addr_any(&match.mask->dst) ||
8671 		    !ipv6_addr_any(&match.mask->src))
8672 			field_flags |= I40E_CLOUD_FIELD_IIP;
8673 
8674 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8675 		       sizeof(filter->src_ipv6));
8676 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8677 		       sizeof(filter->dst_ipv6));
8678 	}
8679 
8680 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8681 		struct flow_match_ports match;
8682 
8683 		flow_rule_match_ports(rule, &match);
8684 		if (match.mask->src) {
8685 			if (match.mask->src == cpu_to_be16(0xffff)) {
8686 				field_flags |= I40E_CLOUD_FIELD_IIP;
8687 			} else {
8688 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8689 					be16_to_cpu(match.mask->src));
8690 				return I40E_ERR_CONFIG;
8691 			}
8692 		}
8693 
8694 		if (match.mask->dst) {
8695 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8696 				field_flags |= I40E_CLOUD_FIELD_IIP;
8697 			} else {
8698 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8699 					be16_to_cpu(match.mask->dst));
8700 				return I40E_ERR_CONFIG;
8701 			}
8702 		}
8703 
8704 		filter->dst_port = match.key->dst;
8705 		filter->src_port = match.key->src;
8706 
8707 		switch (filter->ip_proto) {
8708 		case IPPROTO_TCP:
8709 		case IPPROTO_UDP:
8710 			break;
8711 		default:
8712 			dev_err(&pf->pdev->dev,
8713 				"Only UDP and TCP transport are supported\n");
8714 			return -EINVAL;
8715 		}
8716 	}
8717 	filter->flags = field_flags;
8718 	return 0;
8719 }
8720 
8721 /**
8722  * i40e_handle_tclass: Forward to a traffic class on the device
8723  * @vsi: Pointer to VSI
8724  * @tc: traffic class index on the device
8725  * @filter: Pointer to cloud filter structure
8726  *
8727  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8728 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8729 			      struct i40e_cloud_filter *filter)
8730 {
8731 	struct i40e_channel *ch, *ch_tmp;
8732 
8733 	/* direct to a traffic class on the same device */
8734 	if (tc == 0) {
8735 		filter->seid = vsi->seid;
8736 		return 0;
8737 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8738 		if (!filter->dst_port) {
8739 			dev_err(&vsi->back->pdev->dev,
8740 				"Specify destination port to direct to traffic class that is not default\n");
8741 			return -EINVAL;
8742 		}
8743 		if (list_empty(&vsi->ch_list))
8744 			return -EINVAL;
8745 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8746 					 list) {
8747 			if (ch->seid == vsi->tc_seid_map[tc])
8748 				filter->seid = ch->seid;
8749 		}
8750 		return 0;
8751 	}
8752 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8753 	return -EINVAL;
8754 }
8755 
8756 /**
8757  * i40e_configure_clsflower - Configure tc flower filters
8758  * @vsi: Pointer to VSI
8759  * @cls_flower: Pointer to struct flow_cls_offload
8760  *
8761  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8762 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8763 				    struct flow_cls_offload *cls_flower)
8764 {
8765 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8766 	struct i40e_cloud_filter *filter = NULL;
8767 	struct i40e_pf *pf = vsi->back;
8768 	int err = 0;
8769 
8770 	if (tc < 0) {
8771 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8772 		return -EOPNOTSUPP;
8773 	}
8774 
8775 	if (!tc) {
8776 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8777 		return -EINVAL;
8778 	}
8779 
8780 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8781 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8782 		return -EBUSY;
8783 
8784 	if (pf->fdir_pf_active_filters ||
8785 	    (!hlist_empty(&pf->fdir_filter_list))) {
8786 		dev_err(&vsi->back->pdev->dev,
8787 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8788 		return -EINVAL;
8789 	}
8790 
8791 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8792 		dev_err(&vsi->back->pdev->dev,
8793 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8794 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8795 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8796 	}
8797 
8798 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8799 	if (!filter)
8800 		return -ENOMEM;
8801 
8802 	filter->cookie = cls_flower->cookie;
8803 
8804 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8805 	if (err < 0)
8806 		goto err;
8807 
8808 	err = i40e_handle_tclass(vsi, tc, filter);
8809 	if (err < 0)
8810 		goto err;
8811 
8812 	/* Add cloud filter */
8813 	if (filter->dst_port)
8814 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8815 	else
8816 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8817 
8818 	if (err) {
8819 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8820 			err);
8821 		goto err;
8822 	}
8823 
8824 	/* add filter to the ordered list */
8825 	INIT_HLIST_NODE(&filter->cloud_node);
8826 
8827 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8828 
8829 	pf->num_cloud_filters++;
8830 
8831 	return err;
8832 err:
8833 	kfree(filter);
8834 	return err;
8835 }
8836 
8837 /**
8838  * i40e_find_cloud_filter - Find the could filter in the list
8839  * @vsi: Pointer to VSI
8840  * @cookie: filter specific cookie
8841  *
8842  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8843 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8844 							unsigned long *cookie)
8845 {
8846 	struct i40e_cloud_filter *filter = NULL;
8847 	struct hlist_node *node2;
8848 
8849 	hlist_for_each_entry_safe(filter, node2,
8850 				  &vsi->back->cloud_filter_list, cloud_node)
8851 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8852 			return filter;
8853 	return NULL;
8854 }
8855 
8856 /**
8857  * i40e_delete_clsflower - Remove tc flower filters
8858  * @vsi: Pointer to VSI
8859  * @cls_flower: Pointer to struct flow_cls_offload
8860  *
8861  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8862 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8863 				 struct flow_cls_offload *cls_flower)
8864 {
8865 	struct i40e_cloud_filter *filter = NULL;
8866 	struct i40e_pf *pf = vsi->back;
8867 	int err = 0;
8868 
8869 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8870 
8871 	if (!filter)
8872 		return -EINVAL;
8873 
8874 	hash_del(&filter->cloud_node);
8875 
8876 	if (filter->dst_port)
8877 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8878 	else
8879 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8880 
8881 	kfree(filter);
8882 	if (err) {
8883 		dev_err(&pf->pdev->dev,
8884 			"Failed to delete cloud filter, err %pe\n",
8885 			ERR_PTR(err));
8886 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8887 	}
8888 
8889 	pf->num_cloud_filters--;
8890 	if (!pf->num_cloud_filters)
8891 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8892 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8893 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8894 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8895 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8896 		}
8897 	return 0;
8898 }
8899 
8900 /**
8901  * i40e_setup_tc_cls_flower - flower classifier offloads
8902  * @np: net device to configure
8903  * @cls_flower: offload data
8904  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8905 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8906 				    struct flow_cls_offload *cls_flower)
8907 {
8908 	struct i40e_vsi *vsi = np->vsi;
8909 
8910 	switch (cls_flower->command) {
8911 	case FLOW_CLS_REPLACE:
8912 		return i40e_configure_clsflower(vsi, cls_flower);
8913 	case FLOW_CLS_DESTROY:
8914 		return i40e_delete_clsflower(vsi, cls_flower);
8915 	case FLOW_CLS_STATS:
8916 		return -EOPNOTSUPP;
8917 	default:
8918 		return -EOPNOTSUPP;
8919 	}
8920 }
8921 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8922 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8923 				  void *cb_priv)
8924 {
8925 	struct i40e_netdev_priv *np = cb_priv;
8926 
8927 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8928 		return -EOPNOTSUPP;
8929 
8930 	switch (type) {
8931 	case TC_SETUP_CLSFLOWER:
8932 		return i40e_setup_tc_cls_flower(np, type_data);
8933 
8934 	default:
8935 		return -EOPNOTSUPP;
8936 	}
8937 }
8938 
8939 static LIST_HEAD(i40e_block_cb_list);
8940 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8941 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8942 			   void *type_data)
8943 {
8944 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8945 
8946 	switch (type) {
8947 	case TC_SETUP_QDISC_MQPRIO:
8948 		return i40e_setup_tc(netdev, type_data);
8949 	case TC_SETUP_BLOCK:
8950 		return flow_block_cb_setup_simple(type_data,
8951 						  &i40e_block_cb_list,
8952 						  i40e_setup_tc_block_cb,
8953 						  np, np, true);
8954 	default:
8955 		return -EOPNOTSUPP;
8956 	}
8957 }
8958 
8959 /**
8960  * i40e_open - Called when a network interface is made active
8961  * @netdev: network interface device structure
8962  *
8963  * The open entry point is called when a network interface is made
8964  * active by the system (IFF_UP).  At this point all resources needed
8965  * for transmit and receive operations are allocated, the interrupt
8966  * handler is registered with the OS, the netdev watchdog subtask is
8967  * enabled, and the stack is notified that the interface is ready.
8968  *
8969  * Returns 0 on success, negative value on failure
8970  **/
i40e_open(struct net_device * netdev)8971 int i40e_open(struct net_device *netdev)
8972 {
8973 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8974 	struct i40e_vsi *vsi = np->vsi;
8975 	struct i40e_pf *pf = vsi->back;
8976 	int err;
8977 
8978 	/* disallow open during test or if eeprom is broken */
8979 	if (test_bit(__I40E_TESTING, pf->state) ||
8980 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8981 		return -EBUSY;
8982 
8983 	netif_carrier_off(netdev);
8984 
8985 	if (i40e_force_link_state(pf, true))
8986 		return -EAGAIN;
8987 
8988 	err = i40e_vsi_open(vsi);
8989 	if (err)
8990 		return err;
8991 
8992 	/* configure global TSO hardware offload settings */
8993 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8994 						       TCP_FLAG_FIN) >> 16);
8995 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8996 						       TCP_FLAG_FIN |
8997 						       TCP_FLAG_CWR) >> 16);
8998 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8999 	udp_tunnel_get_rx_info(netdev);
9000 
9001 	return 0;
9002 }
9003 
9004 /**
9005  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9006  * @vsi: vsi structure
9007  *
9008  * This updates netdev's number of tx/rx queues
9009  *
9010  * Returns status of setting tx/rx queues
9011  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)9012 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9013 {
9014 	int ret;
9015 
9016 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9017 					   vsi->num_queue_pairs);
9018 	if (ret)
9019 		return ret;
9020 
9021 	return netif_set_real_num_tx_queues(vsi->netdev,
9022 					    vsi->num_queue_pairs);
9023 }
9024 
9025 /**
9026  * i40e_vsi_open -
9027  * @vsi: the VSI to open
9028  *
9029  * Finish initialization of the VSI.
9030  *
9031  * Returns 0 on success, negative value on failure
9032  *
9033  * Note: expects to be called while under rtnl_lock()
9034  **/
i40e_vsi_open(struct i40e_vsi * vsi)9035 int i40e_vsi_open(struct i40e_vsi *vsi)
9036 {
9037 	struct i40e_pf *pf = vsi->back;
9038 	char int_name[I40E_INT_NAME_STR_LEN];
9039 	int err;
9040 
9041 	/* allocate descriptors */
9042 	err = i40e_vsi_setup_tx_resources(vsi);
9043 	if (err)
9044 		goto err_setup_tx;
9045 	err = i40e_vsi_setup_rx_resources(vsi);
9046 	if (err)
9047 		goto err_setup_rx;
9048 
9049 	err = i40e_vsi_configure(vsi);
9050 	if (err)
9051 		goto err_setup_rx;
9052 
9053 	if (vsi->netdev) {
9054 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9055 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9056 		err = i40e_vsi_request_irq(vsi, int_name);
9057 		if (err)
9058 			goto err_setup_rx;
9059 
9060 		/* Notify the stack of the actual queue counts. */
9061 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9062 		if (err)
9063 			goto err_set_queues;
9064 
9065 	} else if (vsi->type == I40E_VSI_FDIR) {
9066 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9067 			 dev_driver_string(&pf->pdev->dev),
9068 			 dev_name(&pf->pdev->dev));
9069 		err = i40e_vsi_request_irq(vsi, int_name);
9070 		if (err)
9071 			goto err_setup_rx;
9072 
9073 	} else {
9074 		err = -EINVAL;
9075 		goto err_setup_rx;
9076 	}
9077 
9078 	err = i40e_up_complete(vsi);
9079 	if (err)
9080 		goto err_up_complete;
9081 
9082 	return 0;
9083 
9084 err_up_complete:
9085 	i40e_down(vsi);
9086 err_set_queues:
9087 	i40e_vsi_free_irq(vsi);
9088 err_setup_rx:
9089 	i40e_vsi_free_rx_resources(vsi);
9090 err_setup_tx:
9091 	i40e_vsi_free_tx_resources(vsi);
9092 	if (vsi == pf->vsi[pf->lan_vsi])
9093 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9094 
9095 	return err;
9096 }
9097 
9098 /**
9099  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9100  * @pf: Pointer to PF
9101  *
9102  * This function destroys the hlist where all the Flow Director
9103  * filters were saved.
9104  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)9105 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9106 {
9107 	struct i40e_fdir_filter *filter;
9108 	struct i40e_flex_pit *pit_entry, *tmp;
9109 	struct hlist_node *node2;
9110 
9111 	hlist_for_each_entry_safe(filter, node2,
9112 				  &pf->fdir_filter_list, fdir_node) {
9113 		hlist_del(&filter->fdir_node);
9114 		kfree(filter);
9115 	}
9116 
9117 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9118 		list_del(&pit_entry->list);
9119 		kfree(pit_entry);
9120 	}
9121 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9122 
9123 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9124 		list_del(&pit_entry->list);
9125 		kfree(pit_entry);
9126 	}
9127 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9128 
9129 	pf->fdir_pf_active_filters = 0;
9130 	i40e_reset_fdir_filter_cnt(pf);
9131 
9132 	/* Reprogram the default input set for TCP/IPv4 */
9133 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9134 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9135 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9136 
9137 	/* Reprogram the default input set for TCP/IPv6 */
9138 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9139 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9140 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9141 
9142 	/* Reprogram the default input set for UDP/IPv4 */
9143 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9144 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9145 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9146 
9147 	/* Reprogram the default input set for UDP/IPv6 */
9148 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9149 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9150 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9151 
9152 	/* Reprogram the default input set for SCTP/IPv4 */
9153 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9154 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9155 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9156 
9157 	/* Reprogram the default input set for SCTP/IPv6 */
9158 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9159 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9160 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9161 
9162 	/* Reprogram the default input set for Other/IPv4 */
9163 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9164 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9165 
9166 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9167 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9168 
9169 	/* Reprogram the default input set for Other/IPv6 */
9170 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9171 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9172 
9173 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9174 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9175 }
9176 
9177 /**
9178  * i40e_cloud_filter_exit - Cleans up the cloud filters
9179  * @pf: Pointer to PF
9180  *
9181  * This function destroys the hlist where all the cloud filters
9182  * were saved.
9183  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)9184 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9185 {
9186 	struct i40e_cloud_filter *cfilter;
9187 	struct hlist_node *node;
9188 
9189 	hlist_for_each_entry_safe(cfilter, node,
9190 				  &pf->cloud_filter_list, cloud_node) {
9191 		hlist_del(&cfilter->cloud_node);
9192 		kfree(cfilter);
9193 	}
9194 	pf->num_cloud_filters = 0;
9195 
9196 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9197 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9198 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9199 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9200 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9201 	}
9202 }
9203 
9204 /**
9205  * i40e_close - Disables a network interface
9206  * @netdev: network interface device structure
9207  *
9208  * The close entry point is called when an interface is de-activated
9209  * by the OS.  The hardware is still under the driver's control, but
9210  * this netdev interface is disabled.
9211  *
9212  * Returns 0, this is not allowed to fail
9213  **/
i40e_close(struct net_device * netdev)9214 int i40e_close(struct net_device *netdev)
9215 {
9216 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9217 	struct i40e_vsi *vsi = np->vsi;
9218 
9219 	i40e_vsi_close(vsi);
9220 
9221 	return 0;
9222 }
9223 
9224 /**
9225  * i40e_do_reset - Start a PF or Core Reset sequence
9226  * @pf: board private structure
9227  * @reset_flags: which reset is requested
9228  * @lock_acquired: indicates whether or not the lock has been acquired
9229  * before this function was called.
9230  *
9231  * The essential difference in resets is that the PF Reset
9232  * doesn't clear the packet buffers, doesn't reset the PE
9233  * firmware, and doesn't bother the other PFs on the chip.
9234  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)9235 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9236 {
9237 	u32 val;
9238 
9239 	/* do the biggest reset indicated */
9240 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9241 
9242 		/* Request a Global Reset
9243 		 *
9244 		 * This will start the chip's countdown to the actual full
9245 		 * chip reset event, and a warning interrupt to be sent
9246 		 * to all PFs, including the requestor.  Our handler
9247 		 * for the warning interrupt will deal with the shutdown
9248 		 * and recovery of the switch setup.
9249 		 */
9250 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9251 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9252 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9253 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9254 
9255 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9256 
9257 		/* Request a Core Reset
9258 		 *
9259 		 * Same as Global Reset, except does *not* include the MAC/PHY
9260 		 */
9261 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9262 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9263 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9264 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9265 		i40e_flush(&pf->hw);
9266 
9267 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9268 
9269 		/* Request a PF Reset
9270 		 *
9271 		 * Resets only the PF-specific registers
9272 		 *
9273 		 * This goes directly to the tear-down and rebuild of
9274 		 * the switch, since we need to do all the recovery as
9275 		 * for the Core Reset.
9276 		 */
9277 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9278 		i40e_handle_reset_warning(pf, lock_acquired);
9279 
9280 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9281 		/* Request a PF Reset
9282 		 *
9283 		 * Resets PF and reinitializes PFs VSI.
9284 		 */
9285 		i40e_prep_for_reset(pf);
9286 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9287 		dev_info(&pf->pdev->dev,
9288 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9289 			 "FW LLDP is disabled\n" :
9290 			 "FW LLDP is enabled\n");
9291 
9292 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9293 		int v;
9294 
9295 		/* Find the VSI(s) that requested a re-init */
9296 		dev_info(&pf->pdev->dev,
9297 			 "VSI reinit requested\n");
9298 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9299 			struct i40e_vsi *vsi = pf->vsi[v];
9300 
9301 			if (vsi != NULL &&
9302 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9303 					       vsi->state))
9304 				i40e_vsi_reinit_locked(pf->vsi[v]);
9305 		}
9306 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9307 		int v;
9308 
9309 		/* Find the VSI(s) that needs to be brought down */
9310 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9311 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9312 			struct i40e_vsi *vsi = pf->vsi[v];
9313 
9314 			if (vsi != NULL &&
9315 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9316 					       vsi->state)) {
9317 				set_bit(__I40E_VSI_DOWN, vsi->state);
9318 				i40e_down(vsi);
9319 			}
9320 		}
9321 	} else {
9322 		dev_info(&pf->pdev->dev,
9323 			 "bad reset request 0x%08x\n", reset_flags);
9324 	}
9325 }
9326 
9327 #ifdef CONFIG_I40E_DCB
9328 /**
9329  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9330  * @pf: board private structure
9331  * @old_cfg: current DCB config
9332  * @new_cfg: new DCB config
9333  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)9334 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9335 			    struct i40e_dcbx_config *old_cfg,
9336 			    struct i40e_dcbx_config *new_cfg)
9337 {
9338 	bool need_reconfig = false;
9339 
9340 	/* Check if ETS configuration has changed */
9341 	if (memcmp(&new_cfg->etscfg,
9342 		   &old_cfg->etscfg,
9343 		   sizeof(new_cfg->etscfg))) {
9344 		/* If Priority Table has changed reconfig is needed */
9345 		if (memcmp(&new_cfg->etscfg.prioritytable,
9346 			   &old_cfg->etscfg.prioritytable,
9347 			   sizeof(new_cfg->etscfg.prioritytable))) {
9348 			need_reconfig = true;
9349 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9350 		}
9351 
9352 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9353 			   &old_cfg->etscfg.tcbwtable,
9354 			   sizeof(new_cfg->etscfg.tcbwtable)))
9355 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9356 
9357 		if (memcmp(&new_cfg->etscfg.tsatable,
9358 			   &old_cfg->etscfg.tsatable,
9359 			   sizeof(new_cfg->etscfg.tsatable)))
9360 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9361 	}
9362 
9363 	/* Check if PFC configuration has changed */
9364 	if (memcmp(&new_cfg->pfc,
9365 		   &old_cfg->pfc,
9366 		   sizeof(new_cfg->pfc))) {
9367 		need_reconfig = true;
9368 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9369 	}
9370 
9371 	/* Check if APP Table has changed */
9372 	if (memcmp(&new_cfg->app,
9373 		   &old_cfg->app,
9374 		   sizeof(new_cfg->app))) {
9375 		need_reconfig = true;
9376 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9377 	}
9378 
9379 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9380 	return need_reconfig;
9381 }
9382 
9383 /**
9384  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9385  * @pf: board private structure
9386  * @e: event info posted on ARQ
9387  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9388 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9389 				  struct i40e_arq_event_info *e)
9390 {
9391 	struct i40e_aqc_lldp_get_mib *mib =
9392 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9393 	struct i40e_hw *hw = &pf->hw;
9394 	struct i40e_dcbx_config tmp_dcbx_cfg;
9395 	bool need_reconfig = false;
9396 	int ret = 0;
9397 	u8 type;
9398 
9399 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9400 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9401 	    (hw->phy.link_info.link_speed &
9402 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9403 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9404 		/* let firmware decide if the DCB should be disabled */
9405 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9406 
9407 	/* Not DCB capable or capability disabled */
9408 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9409 		return ret;
9410 
9411 	/* Ignore if event is not for Nearest Bridge */
9412 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9413 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9414 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9415 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9416 		return ret;
9417 
9418 	/* Check MIB Type and return if event for Remote MIB update */
9419 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9420 	dev_dbg(&pf->pdev->dev,
9421 		"LLDP event mib type %s\n", type ? "remote" : "local");
9422 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9423 		/* Update the remote cached instance and return */
9424 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9425 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9426 				&hw->remote_dcbx_config);
9427 		goto exit;
9428 	}
9429 
9430 	/* Store the old configuration */
9431 	tmp_dcbx_cfg = hw->local_dcbx_config;
9432 
9433 	/* Reset the old DCBx configuration data */
9434 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9435 	/* Get updated DCBX data from firmware */
9436 	ret = i40e_get_dcb_config(&pf->hw);
9437 	if (ret) {
9438 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9439 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9440 		    (hw->phy.link_info.link_speed &
9441 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9442 			dev_warn(&pf->pdev->dev,
9443 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9444 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9445 		} else {
9446 			dev_info(&pf->pdev->dev,
9447 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9448 				 ERR_PTR(ret),
9449 				 i40e_aq_str(&pf->hw,
9450 					     pf->hw.aq.asq_last_status));
9451 		}
9452 		goto exit;
9453 	}
9454 
9455 	/* No change detected in DCBX configs */
9456 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9457 		    sizeof(tmp_dcbx_cfg))) {
9458 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9459 		goto exit;
9460 	}
9461 
9462 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9463 					       &hw->local_dcbx_config);
9464 
9465 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9466 
9467 	if (!need_reconfig)
9468 		goto exit;
9469 
9470 	/* Enable DCB tagging only when more than one TC */
9471 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9472 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9473 	else
9474 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9475 
9476 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9477 	/* Reconfiguration needed quiesce all VSIs */
9478 	i40e_pf_quiesce_all_vsi(pf);
9479 
9480 	/* Changes in configuration update VEB/VSI */
9481 	i40e_dcb_reconfigure(pf);
9482 
9483 	ret = i40e_resume_port_tx(pf);
9484 
9485 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9486 	/* In case of error no point in resuming VSIs */
9487 	if (ret)
9488 		goto exit;
9489 
9490 	/* Wait for the PF's queues to be disabled */
9491 	ret = i40e_pf_wait_queues_disabled(pf);
9492 	if (ret) {
9493 		/* Schedule PF reset to recover */
9494 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9495 		i40e_service_event_schedule(pf);
9496 	} else {
9497 		i40e_pf_unquiesce_all_vsi(pf);
9498 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9499 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9500 	}
9501 
9502 exit:
9503 	return ret;
9504 }
9505 #endif /* CONFIG_I40E_DCB */
9506 
9507 /**
9508  * i40e_do_reset_safe - Protected reset path for userland calls.
9509  * @pf: board private structure
9510  * @reset_flags: which reset is requested
9511  *
9512  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)9513 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9514 {
9515 	rtnl_lock();
9516 	i40e_do_reset(pf, reset_flags, true);
9517 	rtnl_unlock();
9518 }
9519 
9520 /**
9521  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9522  * @pf: board private structure
9523  * @e: event info posted on ARQ
9524  *
9525  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9526  * and VF queues
9527  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9528 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9529 					   struct i40e_arq_event_info *e)
9530 {
9531 	struct i40e_aqc_lan_overflow *data =
9532 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9533 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9534 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9535 	struct i40e_hw *hw = &pf->hw;
9536 	struct i40e_vf *vf;
9537 	u16 vf_id;
9538 
9539 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9540 		queue, qtx_ctl);
9541 
9542 	/* Queue belongs to VF, find the VF and issue VF reset */
9543 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9544 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9545 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9546 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9547 		vf_id -= hw->func_caps.vf_base_id;
9548 		vf = &pf->vf[vf_id];
9549 		i40e_vc_notify_vf_reset(vf);
9550 		/* Allow VF to process pending reset notification */
9551 		msleep(20);
9552 		i40e_reset_vf(vf, false);
9553 	}
9554 }
9555 
9556 /**
9557  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9558  * @pf: board private structure
9559  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)9560 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9561 {
9562 	u32 val, fcnt_prog;
9563 
9564 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9565 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9566 	return fcnt_prog;
9567 }
9568 
9569 /**
9570  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9571  * @pf: board private structure
9572  **/
i40e_get_current_fd_count(struct i40e_pf * pf)9573 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9574 {
9575 	u32 val, fcnt_prog;
9576 
9577 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9578 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9579 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9580 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9581 	return fcnt_prog;
9582 }
9583 
9584 /**
9585  * i40e_get_global_fd_count - Get total FD filters programmed on device
9586  * @pf: board private structure
9587  **/
i40e_get_global_fd_count(struct i40e_pf * pf)9588 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9589 {
9590 	u32 val, fcnt_prog;
9591 
9592 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9593 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9594 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9595 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9596 	return fcnt_prog;
9597 }
9598 
9599 /**
9600  * i40e_reenable_fdir_sb - Restore FDir SB capability
9601  * @pf: board private structure
9602  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)9603 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9604 {
9605 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9606 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9607 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9608 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9609 }
9610 
9611 /**
9612  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9613  * @pf: board private structure
9614  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)9615 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9616 {
9617 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9618 		/* ATR uses the same filtering logic as SB rules. It only
9619 		 * functions properly if the input set mask is at the default
9620 		 * settings. It is safe to restore the default input set
9621 		 * because there are no active TCPv4 filter rules.
9622 		 */
9623 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9624 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9625 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9626 
9627 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9628 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9629 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9630 	}
9631 }
9632 
9633 /**
9634  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9635  * @pf: board private structure
9636  * @filter: FDir filter to remove
9637  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)9638 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9639 				       struct i40e_fdir_filter *filter)
9640 {
9641 	/* Update counters */
9642 	pf->fdir_pf_active_filters--;
9643 	pf->fd_inv = 0;
9644 
9645 	switch (filter->flow_type) {
9646 	case TCP_V4_FLOW:
9647 		pf->fd_tcp4_filter_cnt--;
9648 		break;
9649 	case UDP_V4_FLOW:
9650 		pf->fd_udp4_filter_cnt--;
9651 		break;
9652 	case SCTP_V4_FLOW:
9653 		pf->fd_sctp4_filter_cnt--;
9654 		break;
9655 	case TCP_V6_FLOW:
9656 		pf->fd_tcp6_filter_cnt--;
9657 		break;
9658 	case UDP_V6_FLOW:
9659 		pf->fd_udp6_filter_cnt--;
9660 		break;
9661 	case SCTP_V6_FLOW:
9662 		pf->fd_udp6_filter_cnt--;
9663 		break;
9664 	case IP_USER_FLOW:
9665 		switch (filter->ipl4_proto) {
9666 		case IPPROTO_TCP:
9667 			pf->fd_tcp4_filter_cnt--;
9668 			break;
9669 		case IPPROTO_UDP:
9670 			pf->fd_udp4_filter_cnt--;
9671 			break;
9672 		case IPPROTO_SCTP:
9673 			pf->fd_sctp4_filter_cnt--;
9674 			break;
9675 		case IPPROTO_IP:
9676 			pf->fd_ip4_filter_cnt--;
9677 			break;
9678 		}
9679 		break;
9680 	case IPV6_USER_FLOW:
9681 		switch (filter->ipl4_proto) {
9682 		case IPPROTO_TCP:
9683 			pf->fd_tcp6_filter_cnt--;
9684 			break;
9685 		case IPPROTO_UDP:
9686 			pf->fd_udp6_filter_cnt--;
9687 			break;
9688 		case IPPROTO_SCTP:
9689 			pf->fd_sctp6_filter_cnt--;
9690 			break;
9691 		case IPPROTO_IP:
9692 			pf->fd_ip6_filter_cnt--;
9693 			break;
9694 		}
9695 		break;
9696 	}
9697 
9698 	/* Remove the filter from the list and free memory */
9699 	hlist_del(&filter->fdir_node);
9700 	kfree(filter);
9701 }
9702 
9703 /**
9704  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9705  * @pf: board private structure
9706  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9707 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9708 {
9709 	struct i40e_fdir_filter *filter;
9710 	u32 fcnt_prog, fcnt_avail;
9711 	struct hlist_node *node;
9712 
9713 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9714 		return;
9715 
9716 	/* Check if we have enough room to re-enable FDir SB capability. */
9717 	fcnt_prog = i40e_get_global_fd_count(pf);
9718 	fcnt_avail = pf->fdir_pf_filter_count;
9719 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9720 	    (pf->fd_add_err == 0) ||
9721 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9722 		i40e_reenable_fdir_sb(pf);
9723 
9724 	/* We should wait for even more space before re-enabling ATR.
9725 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9726 	 * rules active.
9727 	 */
9728 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9729 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9730 		i40e_reenable_fdir_atr(pf);
9731 
9732 	/* if hw had a problem adding a filter, delete it */
9733 	if (pf->fd_inv > 0) {
9734 		hlist_for_each_entry_safe(filter, node,
9735 					  &pf->fdir_filter_list, fdir_node)
9736 			if (filter->fd_id == pf->fd_inv)
9737 				i40e_delete_invalid_filter(pf, filter);
9738 	}
9739 }
9740 
9741 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9742 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9743 /**
9744  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9745  * @pf: board private structure
9746  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9747 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9748 {
9749 	unsigned long min_flush_time;
9750 	int flush_wait_retry = 50;
9751 	bool disable_atr = false;
9752 	int fd_room;
9753 	int reg;
9754 
9755 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9756 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9757 		return;
9758 
9759 	/* If the flush is happening too quick and we have mostly SB rules we
9760 	 * should not re-enable ATR for some time.
9761 	 */
9762 	min_flush_time = pf->fd_flush_timestamp +
9763 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9764 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9765 
9766 	if (!(time_after(jiffies, min_flush_time)) &&
9767 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9768 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9769 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9770 		disable_atr = true;
9771 	}
9772 
9773 	pf->fd_flush_timestamp = jiffies;
9774 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9775 	/* flush all filters */
9776 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9777 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9778 	i40e_flush(&pf->hw);
9779 	pf->fd_flush_cnt++;
9780 	pf->fd_add_err = 0;
9781 	do {
9782 		/* Check FD flush status every 5-6msec */
9783 		usleep_range(5000, 6000);
9784 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9785 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9786 			break;
9787 	} while (flush_wait_retry--);
9788 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9789 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9790 	} else {
9791 		/* replay sideband filters */
9792 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9793 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9794 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9795 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9796 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9797 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9798 	}
9799 }
9800 
9801 /**
9802  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9803  * @pf: board private structure
9804  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9805 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9806 {
9807 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9808 }
9809 
9810 /**
9811  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9812  * @pf: board private structure
9813  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9814 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9815 {
9816 
9817 	/* if interface is down do nothing */
9818 	if (test_bit(__I40E_DOWN, pf->state))
9819 		return;
9820 
9821 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9822 		i40e_fdir_flush_and_replay(pf);
9823 
9824 	i40e_fdir_check_and_reenable(pf);
9825 
9826 }
9827 
9828 /**
9829  * i40e_vsi_link_event - notify VSI of a link event
9830  * @vsi: vsi to be notified
9831  * @link_up: link up or down
9832  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9833 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9834 {
9835 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9836 		return;
9837 
9838 	switch (vsi->type) {
9839 	case I40E_VSI_MAIN:
9840 		if (!vsi->netdev || !vsi->netdev_registered)
9841 			break;
9842 
9843 		if (link_up) {
9844 			netif_carrier_on(vsi->netdev);
9845 			netif_tx_wake_all_queues(vsi->netdev);
9846 		} else {
9847 			netif_carrier_off(vsi->netdev);
9848 			netif_tx_stop_all_queues(vsi->netdev);
9849 		}
9850 		break;
9851 
9852 	case I40E_VSI_SRIOV:
9853 	case I40E_VSI_VMDQ2:
9854 	case I40E_VSI_CTRL:
9855 	case I40E_VSI_IWARP:
9856 	case I40E_VSI_MIRROR:
9857 	default:
9858 		/* there is no notification for other VSIs */
9859 		break;
9860 	}
9861 }
9862 
9863 /**
9864  * i40e_veb_link_event - notify elements on the veb of a link event
9865  * @veb: veb to be notified
9866  * @link_up: link up or down
9867  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9868 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9869 {
9870 	struct i40e_pf *pf;
9871 	int i;
9872 
9873 	if (!veb || !veb->pf)
9874 		return;
9875 	pf = veb->pf;
9876 
9877 	/* depth first... */
9878 	for (i = 0; i < I40E_MAX_VEB; i++)
9879 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9880 			i40e_veb_link_event(pf->veb[i], link_up);
9881 
9882 	/* ... now the local VSIs */
9883 	for (i = 0; i < pf->num_alloc_vsi; i++)
9884 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9885 			i40e_vsi_link_event(pf->vsi[i], link_up);
9886 }
9887 
9888 /**
9889  * i40e_link_event - Update netif_carrier status
9890  * @pf: board private structure
9891  **/
i40e_link_event(struct i40e_pf * pf)9892 static void i40e_link_event(struct i40e_pf *pf)
9893 {
9894 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9895 	u8 new_link_speed, old_link_speed;
9896 	bool new_link, old_link;
9897 	int status;
9898 #ifdef CONFIG_I40E_DCB
9899 	int err;
9900 #endif /* CONFIG_I40E_DCB */
9901 
9902 	/* set this to force the get_link_status call to refresh state */
9903 	pf->hw.phy.get_link_info = true;
9904 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9905 	status = i40e_get_link_status(&pf->hw, &new_link);
9906 
9907 	/* On success, disable temp link polling */
9908 	if (status == I40E_SUCCESS) {
9909 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9910 	} else {
9911 		/* Enable link polling temporarily until i40e_get_link_status
9912 		 * returns I40E_SUCCESS
9913 		 */
9914 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9915 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9916 			status);
9917 		return;
9918 	}
9919 
9920 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9921 	new_link_speed = pf->hw.phy.link_info.link_speed;
9922 
9923 	if (new_link == old_link &&
9924 	    new_link_speed == old_link_speed &&
9925 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9926 	     new_link == netif_carrier_ok(vsi->netdev)))
9927 		return;
9928 
9929 	i40e_print_link_message(vsi, new_link);
9930 
9931 	/* Notify the base of the switch tree connected to
9932 	 * the link.  Floating VEBs are not notified.
9933 	 */
9934 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9935 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9936 	else
9937 		i40e_vsi_link_event(vsi, new_link);
9938 
9939 	if (pf->vf)
9940 		i40e_vc_notify_link_state(pf);
9941 
9942 	if (pf->flags & I40E_FLAG_PTP)
9943 		i40e_ptp_set_increment(pf);
9944 #ifdef CONFIG_I40E_DCB
9945 	if (new_link == old_link)
9946 		return;
9947 	/* Not SW DCB so firmware will take care of default settings */
9948 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9949 		return;
9950 
9951 	/* We cover here only link down, as after link up in case of SW DCB
9952 	 * SW LLDP agent will take care of setting it up
9953 	 */
9954 	if (!new_link) {
9955 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9956 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9957 		err = i40e_dcb_sw_default_config(pf);
9958 		if (err) {
9959 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9960 				       I40E_FLAG_DCB_ENABLED);
9961 		} else {
9962 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9963 				       DCB_CAP_DCBX_VER_IEEE;
9964 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9965 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9966 		}
9967 	}
9968 #endif /* CONFIG_I40E_DCB */
9969 }
9970 
9971 /**
9972  * i40e_watchdog_subtask - periodic checks not using event driven response
9973  * @pf: board private structure
9974  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9975 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9976 {
9977 	int i;
9978 
9979 	/* if interface is down do nothing */
9980 	if (test_bit(__I40E_DOWN, pf->state) ||
9981 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9982 		return;
9983 
9984 	/* make sure we don't do these things too often */
9985 	if (time_before(jiffies, (pf->service_timer_previous +
9986 				  pf->service_timer_period)))
9987 		return;
9988 	pf->service_timer_previous = jiffies;
9989 
9990 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9991 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9992 		i40e_link_event(pf);
9993 
9994 	/* Update the stats for active netdevs so the network stack
9995 	 * can look at updated numbers whenever it cares to
9996 	 */
9997 	for (i = 0; i < pf->num_alloc_vsi; i++)
9998 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9999 			i40e_update_stats(pf->vsi[i]);
10000 
10001 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10002 		/* Update the stats for the active switching components */
10003 		for (i = 0; i < I40E_MAX_VEB; i++)
10004 			if (pf->veb[i])
10005 				i40e_update_veb_stats(pf->veb[i]);
10006 	}
10007 
10008 	i40e_ptp_rx_hang(pf);
10009 	i40e_ptp_tx_hang(pf);
10010 }
10011 
10012 /**
10013  * i40e_reset_subtask - Set up for resetting the device and driver
10014  * @pf: board private structure
10015  **/
i40e_reset_subtask(struct i40e_pf * pf)10016 static void i40e_reset_subtask(struct i40e_pf *pf)
10017 {
10018 	u32 reset_flags = 0;
10019 
10020 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10021 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10022 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10023 	}
10024 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10025 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10026 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10027 	}
10028 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10029 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10030 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10031 	}
10032 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10033 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10034 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10035 	}
10036 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10037 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10038 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10039 	}
10040 
10041 	/* If there's a recovery already waiting, it takes
10042 	 * precedence before starting a new reset sequence.
10043 	 */
10044 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10045 		i40e_prep_for_reset(pf);
10046 		i40e_reset(pf);
10047 		i40e_rebuild(pf, false, false);
10048 	}
10049 
10050 	/* If we're already down or resetting, just bail */
10051 	if (reset_flags &&
10052 	    !test_bit(__I40E_DOWN, pf->state) &&
10053 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10054 		i40e_do_reset(pf, reset_flags, false);
10055 	}
10056 }
10057 
10058 /**
10059  * i40e_handle_link_event - Handle link event
10060  * @pf: board private structure
10061  * @e: event info posted on ARQ
10062  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)10063 static void i40e_handle_link_event(struct i40e_pf *pf,
10064 				   struct i40e_arq_event_info *e)
10065 {
10066 	struct i40e_aqc_get_link_status *status =
10067 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10068 
10069 	/* Do a new status request to re-enable LSE reporting
10070 	 * and load new status information into the hw struct
10071 	 * This completely ignores any state information
10072 	 * in the ARQ event info, instead choosing to always
10073 	 * issue the AQ update link status command.
10074 	 */
10075 	i40e_link_event(pf);
10076 
10077 	/* Check if module meets thermal requirements */
10078 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10079 		dev_err(&pf->pdev->dev,
10080 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10081 		dev_err(&pf->pdev->dev,
10082 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10083 	} else {
10084 		/* check for unqualified module, if link is down, suppress
10085 		 * the message if link was forced to be down.
10086 		 */
10087 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10088 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10089 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10090 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10091 			dev_err(&pf->pdev->dev,
10092 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10093 			dev_err(&pf->pdev->dev,
10094 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10095 		}
10096 	}
10097 }
10098 
10099 /**
10100  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10101  * @pf: board private structure
10102  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)10103 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10104 {
10105 	struct i40e_arq_event_info event;
10106 	struct i40e_hw *hw = &pf->hw;
10107 	u16 pending, i = 0;
10108 	u16 opcode;
10109 	u32 oldval;
10110 	int ret;
10111 	u32 val;
10112 
10113 	/* Do not run clean AQ when PF reset fails */
10114 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10115 		return;
10116 
10117 	/* check for error indications */
10118 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10119 	oldval = val;
10120 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10121 		if (hw->debug_mask & I40E_DEBUG_AQ)
10122 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10123 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10124 	}
10125 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10126 		if (hw->debug_mask & I40E_DEBUG_AQ)
10127 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10128 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10129 		pf->arq_overflows++;
10130 	}
10131 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10132 		if (hw->debug_mask & I40E_DEBUG_AQ)
10133 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10134 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10135 	}
10136 	if (oldval != val)
10137 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10138 
10139 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10140 	oldval = val;
10141 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10142 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10143 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10144 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10145 	}
10146 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10147 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10148 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10149 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10150 	}
10151 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10152 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10153 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10154 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10155 	}
10156 	if (oldval != val)
10157 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10158 
10159 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10160 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10161 	if (!event.msg_buf)
10162 		return;
10163 
10164 	do {
10165 		ret = i40e_clean_arq_element(hw, &event, &pending);
10166 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10167 			break;
10168 		else if (ret) {
10169 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10170 			break;
10171 		}
10172 
10173 		opcode = le16_to_cpu(event.desc.opcode);
10174 		switch (opcode) {
10175 
10176 		case i40e_aqc_opc_get_link_status:
10177 			rtnl_lock();
10178 			i40e_handle_link_event(pf, &event);
10179 			rtnl_unlock();
10180 			break;
10181 		case i40e_aqc_opc_send_msg_to_pf:
10182 			ret = i40e_vc_process_vf_msg(pf,
10183 					le16_to_cpu(event.desc.retval),
10184 					le32_to_cpu(event.desc.cookie_high),
10185 					le32_to_cpu(event.desc.cookie_low),
10186 					event.msg_buf,
10187 					event.msg_len);
10188 			break;
10189 		case i40e_aqc_opc_lldp_update_mib:
10190 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10191 #ifdef CONFIG_I40E_DCB
10192 			rtnl_lock();
10193 			i40e_handle_lldp_event(pf, &event);
10194 			rtnl_unlock();
10195 #endif /* CONFIG_I40E_DCB */
10196 			break;
10197 		case i40e_aqc_opc_event_lan_overflow:
10198 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10199 			i40e_handle_lan_overflow_event(pf, &event);
10200 			break;
10201 		case i40e_aqc_opc_send_msg_to_peer:
10202 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10203 			break;
10204 		case i40e_aqc_opc_nvm_erase:
10205 		case i40e_aqc_opc_nvm_update:
10206 		case i40e_aqc_opc_oem_post_update:
10207 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10208 				   "ARQ NVM operation 0x%04x completed\n",
10209 				   opcode);
10210 			break;
10211 		default:
10212 			dev_info(&pf->pdev->dev,
10213 				 "ARQ: Unknown event 0x%04x ignored\n",
10214 				 opcode);
10215 			break;
10216 		}
10217 	} while (i++ < pf->adminq_work_limit);
10218 
10219 	if (i < pf->adminq_work_limit)
10220 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10221 
10222 	/* re-enable Admin queue interrupt cause */
10223 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10224 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10225 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10226 	i40e_flush(hw);
10227 
10228 	kfree(event.msg_buf);
10229 }
10230 
10231 /**
10232  * i40e_verify_eeprom - make sure eeprom is good to use
10233  * @pf: board private structure
10234  **/
i40e_verify_eeprom(struct i40e_pf * pf)10235 static void i40e_verify_eeprom(struct i40e_pf *pf)
10236 {
10237 	int err;
10238 
10239 	err = i40e_diag_eeprom_test(&pf->hw);
10240 	if (err) {
10241 		/* retry in case of garbage read */
10242 		err = i40e_diag_eeprom_test(&pf->hw);
10243 		if (err) {
10244 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10245 				 err);
10246 			set_bit(__I40E_BAD_EEPROM, pf->state);
10247 		}
10248 	}
10249 
10250 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10251 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10252 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10253 	}
10254 }
10255 
10256 /**
10257  * i40e_enable_pf_switch_lb
10258  * @pf: pointer to the PF structure
10259  *
10260  * enable switch loop back or die - no point in a return value
10261  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)10262 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10263 {
10264 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10265 	struct i40e_vsi_context ctxt;
10266 	int ret;
10267 
10268 	ctxt.seid = pf->main_vsi_seid;
10269 	ctxt.pf_num = pf->hw.pf_id;
10270 	ctxt.vf_num = 0;
10271 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10272 	if (ret) {
10273 		dev_info(&pf->pdev->dev,
10274 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10275 			 ERR_PTR(ret),
10276 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10277 		return;
10278 	}
10279 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10280 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10281 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10282 
10283 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10284 	if (ret) {
10285 		dev_info(&pf->pdev->dev,
10286 			 "update vsi switch failed, err %pe aq_err %s\n",
10287 			 ERR_PTR(ret),
10288 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10289 	}
10290 }
10291 
10292 /**
10293  * i40e_disable_pf_switch_lb
10294  * @pf: pointer to the PF structure
10295  *
10296  * disable switch loop back or die - no point in a return value
10297  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)10298 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10299 {
10300 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10301 	struct i40e_vsi_context ctxt;
10302 	int ret;
10303 
10304 	ctxt.seid = pf->main_vsi_seid;
10305 	ctxt.pf_num = pf->hw.pf_id;
10306 	ctxt.vf_num = 0;
10307 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10308 	if (ret) {
10309 		dev_info(&pf->pdev->dev,
10310 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10311 			 ERR_PTR(ret),
10312 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10313 		return;
10314 	}
10315 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10316 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10317 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10318 
10319 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10320 	if (ret) {
10321 		dev_info(&pf->pdev->dev,
10322 			 "update vsi switch failed, err %pe aq_err %s\n",
10323 			 ERR_PTR(ret),
10324 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10325 	}
10326 }
10327 
10328 /**
10329  * i40e_config_bridge_mode - Configure the HW bridge mode
10330  * @veb: pointer to the bridge instance
10331  *
10332  * Configure the loop back mode for the LAN VSI that is downlink to the
10333  * specified HW bridge instance. It is expected this function is called
10334  * when a new HW bridge is instantiated.
10335  **/
i40e_config_bridge_mode(struct i40e_veb * veb)10336 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10337 {
10338 	struct i40e_pf *pf = veb->pf;
10339 
10340 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10341 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10342 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10343 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10344 		i40e_disable_pf_switch_lb(pf);
10345 	else
10346 		i40e_enable_pf_switch_lb(pf);
10347 }
10348 
10349 /**
10350  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10351  * @veb: pointer to the VEB instance
10352  *
10353  * This is a recursive function that first builds the attached VSIs then
10354  * recurses in to build the next layer of VEB.  We track the connections
10355  * through our own index numbers because the seid's from the HW could
10356  * change across the reset.
10357  **/
i40e_reconstitute_veb(struct i40e_veb * veb)10358 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10359 {
10360 	struct i40e_vsi *ctl_vsi = NULL;
10361 	struct i40e_pf *pf = veb->pf;
10362 	int v, veb_idx;
10363 	int ret;
10364 
10365 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10366 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10367 		if (pf->vsi[v] &&
10368 		    pf->vsi[v]->veb_idx == veb->idx &&
10369 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10370 			ctl_vsi = pf->vsi[v];
10371 			break;
10372 		}
10373 	}
10374 	if (!ctl_vsi) {
10375 		dev_info(&pf->pdev->dev,
10376 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10377 		ret = -ENOENT;
10378 		goto end_reconstitute;
10379 	}
10380 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10381 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10382 	ret = i40e_add_vsi(ctl_vsi);
10383 	if (ret) {
10384 		dev_info(&pf->pdev->dev,
10385 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10386 			 veb->idx, ret);
10387 		goto end_reconstitute;
10388 	}
10389 	i40e_vsi_reset_stats(ctl_vsi);
10390 
10391 	/* create the VEB in the switch and move the VSI onto the VEB */
10392 	ret = i40e_add_veb(veb, ctl_vsi);
10393 	if (ret)
10394 		goto end_reconstitute;
10395 
10396 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10397 		veb->bridge_mode = BRIDGE_MODE_VEB;
10398 	else
10399 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10400 	i40e_config_bridge_mode(veb);
10401 
10402 	/* create the remaining VSIs attached to this VEB */
10403 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10404 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10405 			continue;
10406 
10407 		if (pf->vsi[v]->veb_idx == veb->idx) {
10408 			struct i40e_vsi *vsi = pf->vsi[v];
10409 
10410 			vsi->uplink_seid = veb->seid;
10411 			ret = i40e_add_vsi(vsi);
10412 			if (ret) {
10413 				dev_info(&pf->pdev->dev,
10414 					 "rebuild of vsi_idx %d failed: %d\n",
10415 					 v, ret);
10416 				goto end_reconstitute;
10417 			}
10418 			i40e_vsi_reset_stats(vsi);
10419 		}
10420 	}
10421 
10422 	/* create any VEBs attached to this VEB - RECURSION */
10423 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10424 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10425 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10426 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10427 			if (ret)
10428 				break;
10429 		}
10430 	}
10431 
10432 end_reconstitute:
10433 	return ret;
10434 }
10435 
10436 /**
10437  * i40e_get_capabilities - get info about the HW
10438  * @pf: the PF struct
10439  * @list_type: AQ capability to be queried
10440  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)10441 static int i40e_get_capabilities(struct i40e_pf *pf,
10442 				 enum i40e_admin_queue_opc list_type)
10443 {
10444 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10445 	u16 data_size;
10446 	int buf_len;
10447 	int err;
10448 
10449 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10450 	do {
10451 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10452 		if (!cap_buf)
10453 			return -ENOMEM;
10454 
10455 		/* this loads the data into the hw struct for us */
10456 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10457 						    &data_size, list_type,
10458 						    NULL);
10459 		/* data loaded, buffer no longer needed */
10460 		kfree(cap_buf);
10461 
10462 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10463 			/* retry with a larger buffer */
10464 			buf_len = data_size;
10465 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10466 			dev_info(&pf->pdev->dev,
10467 				 "capability discovery failed, err %pe aq_err %s\n",
10468 				 ERR_PTR(err),
10469 				 i40e_aq_str(&pf->hw,
10470 					     pf->hw.aq.asq_last_status));
10471 			return -ENODEV;
10472 		}
10473 	} while (err);
10474 
10475 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10476 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10477 			dev_info(&pf->pdev->dev,
10478 				 "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",
10479 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10480 				 pf->hw.func_caps.num_msix_vectors,
10481 				 pf->hw.func_caps.num_msix_vectors_vf,
10482 				 pf->hw.func_caps.fd_filters_guaranteed,
10483 				 pf->hw.func_caps.fd_filters_best_effort,
10484 				 pf->hw.func_caps.num_tx_qp,
10485 				 pf->hw.func_caps.num_vsis);
10486 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10487 			dev_info(&pf->pdev->dev,
10488 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10489 				 pf->hw.dev_caps.switch_mode,
10490 				 pf->hw.dev_caps.valid_functions);
10491 			dev_info(&pf->pdev->dev,
10492 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10493 				 pf->hw.dev_caps.sr_iov_1_1,
10494 				 pf->hw.dev_caps.num_vfs);
10495 			dev_info(&pf->pdev->dev,
10496 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10497 				 pf->hw.dev_caps.num_vsis,
10498 				 pf->hw.dev_caps.num_rx_qp,
10499 				 pf->hw.dev_caps.num_tx_qp);
10500 		}
10501 	}
10502 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10503 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10504 		       + pf->hw.func_caps.num_vfs)
10505 		if (pf->hw.revision_id == 0 &&
10506 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10507 			dev_info(&pf->pdev->dev,
10508 				 "got num_vsis %d, setting num_vsis to %d\n",
10509 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10510 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10511 		}
10512 	}
10513 	return 0;
10514 }
10515 
10516 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10517 
10518 /**
10519  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10520  * @pf: board private structure
10521  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)10522 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10523 {
10524 	struct i40e_vsi *vsi;
10525 
10526 	/* quick workaround for an NVM issue that leaves a critical register
10527 	 * uninitialized
10528 	 */
10529 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10530 		static const u32 hkey[] = {
10531 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10532 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10533 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10534 			0x95b3a76d};
10535 		int i;
10536 
10537 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10538 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10539 	}
10540 
10541 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10542 		return;
10543 
10544 	/* find existing VSI and see if it needs configuring */
10545 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10546 
10547 	/* create a new VSI if none exists */
10548 	if (!vsi) {
10549 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10550 				     pf->vsi[pf->lan_vsi]->seid, 0);
10551 		if (!vsi) {
10552 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10553 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10554 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10555 			return;
10556 		}
10557 	}
10558 
10559 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10560 }
10561 
10562 /**
10563  * i40e_fdir_teardown - release the Flow Director resources
10564  * @pf: board private structure
10565  **/
i40e_fdir_teardown(struct i40e_pf * pf)10566 static void i40e_fdir_teardown(struct i40e_pf *pf)
10567 {
10568 	struct i40e_vsi *vsi;
10569 
10570 	i40e_fdir_filter_exit(pf);
10571 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10572 	if (vsi)
10573 		i40e_vsi_release(vsi);
10574 }
10575 
10576 /**
10577  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10578  * @vsi: PF main vsi
10579  * @seid: seid of main or channel VSIs
10580  *
10581  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10582  * existed before reset
10583  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)10584 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10585 {
10586 	struct i40e_cloud_filter *cfilter;
10587 	struct i40e_pf *pf = vsi->back;
10588 	struct hlist_node *node;
10589 	int ret;
10590 
10591 	/* Add cloud filters back if they exist */
10592 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10593 				  cloud_node) {
10594 		if (cfilter->seid != seid)
10595 			continue;
10596 
10597 		if (cfilter->dst_port)
10598 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10599 								true);
10600 		else
10601 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10602 
10603 		if (ret) {
10604 			dev_dbg(&pf->pdev->dev,
10605 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10606 				ERR_PTR(ret),
10607 				i40e_aq_str(&pf->hw,
10608 					    pf->hw.aq.asq_last_status));
10609 			return ret;
10610 		}
10611 	}
10612 	return 0;
10613 }
10614 
10615 /**
10616  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10617  * @vsi: PF main vsi
10618  *
10619  * Rebuilds channel VSIs if they existed before reset
10620  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)10621 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10622 {
10623 	struct i40e_channel *ch, *ch_tmp;
10624 	int ret;
10625 
10626 	if (list_empty(&vsi->ch_list))
10627 		return 0;
10628 
10629 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10630 		if (!ch->initialized)
10631 			break;
10632 		/* Proceed with creation of channel (VMDq2) VSI */
10633 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10634 		if (ret) {
10635 			dev_info(&vsi->back->pdev->dev,
10636 				 "failed to rebuild channels using uplink_seid %u\n",
10637 				 vsi->uplink_seid);
10638 			return ret;
10639 		}
10640 		/* Reconfigure TX queues using QTX_CTL register */
10641 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10642 		if (ret) {
10643 			dev_info(&vsi->back->pdev->dev,
10644 				 "failed to configure TX rings for channel %u\n",
10645 				 ch->seid);
10646 			return ret;
10647 		}
10648 		/* update 'next_base_queue' */
10649 		vsi->next_base_queue = vsi->next_base_queue +
10650 							ch->num_queue_pairs;
10651 		if (ch->max_tx_rate) {
10652 			u64 credits = ch->max_tx_rate;
10653 
10654 			if (i40e_set_bw_limit(vsi, ch->seid,
10655 					      ch->max_tx_rate))
10656 				return -EINVAL;
10657 
10658 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10659 			dev_dbg(&vsi->back->pdev->dev,
10660 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10661 				ch->max_tx_rate,
10662 				credits,
10663 				ch->seid);
10664 		}
10665 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10666 		if (ret) {
10667 			dev_dbg(&vsi->back->pdev->dev,
10668 				"Failed to rebuild cloud filters for channel VSI %u\n",
10669 				ch->seid);
10670 			return ret;
10671 		}
10672 	}
10673 	return 0;
10674 }
10675 
10676 /**
10677  * i40e_clean_xps_state - clean xps state for every tx_ring
10678  * @vsi: ptr to the VSI
10679  **/
i40e_clean_xps_state(struct i40e_vsi * vsi)10680 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10681 {
10682 	int i;
10683 
10684 	if (vsi->tx_rings)
10685 		for (i = 0; i < vsi->num_queue_pairs; i++)
10686 			if (vsi->tx_rings[i])
10687 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10688 					  vsi->tx_rings[i]->state);
10689 }
10690 
10691 /**
10692  * i40e_prep_for_reset - prep for the core to reset
10693  * @pf: board private structure
10694  *
10695  * Close up the VFs and other things in prep for PF Reset.
10696   **/
i40e_prep_for_reset(struct i40e_pf * pf)10697 static void i40e_prep_for_reset(struct i40e_pf *pf)
10698 {
10699 	struct i40e_hw *hw = &pf->hw;
10700 	int ret = 0;
10701 	u32 v;
10702 
10703 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10704 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10705 		return;
10706 	if (i40e_check_asq_alive(&pf->hw))
10707 		i40e_vc_notify_reset(pf);
10708 
10709 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10710 
10711 	/* quiesce the VSIs and their queues that are not already DOWN */
10712 	i40e_pf_quiesce_all_vsi(pf);
10713 
10714 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10715 		if (pf->vsi[v]) {
10716 			i40e_clean_xps_state(pf->vsi[v]);
10717 			pf->vsi[v]->seid = 0;
10718 		}
10719 	}
10720 
10721 	i40e_shutdown_adminq(&pf->hw);
10722 
10723 	/* call shutdown HMC */
10724 	if (hw->hmc.hmc_obj) {
10725 		ret = i40e_shutdown_lan_hmc(hw);
10726 		if (ret)
10727 			dev_warn(&pf->pdev->dev,
10728 				 "shutdown_lan_hmc failed: %d\n", ret);
10729 	}
10730 
10731 	/* Save the current PTP time so that we can restore the time after the
10732 	 * reset completes.
10733 	 */
10734 	i40e_ptp_save_hw_time(pf);
10735 }
10736 
10737 /**
10738  * i40e_send_version - update firmware with driver version
10739  * @pf: PF struct
10740  */
i40e_send_version(struct i40e_pf * pf)10741 static void i40e_send_version(struct i40e_pf *pf)
10742 {
10743 	struct i40e_driver_version dv;
10744 
10745 	dv.major_version = 0xff;
10746 	dv.minor_version = 0xff;
10747 	dv.build_version = 0xff;
10748 	dv.subbuild_version = 0;
10749 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10750 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10751 }
10752 
10753 /**
10754  * i40e_get_oem_version - get OEM specific version information
10755  * @hw: pointer to the hardware structure
10756  **/
i40e_get_oem_version(struct i40e_hw * hw)10757 static void i40e_get_oem_version(struct i40e_hw *hw)
10758 {
10759 	u16 block_offset = 0xffff;
10760 	u16 block_length = 0;
10761 	u16 capabilities = 0;
10762 	u16 gen_snap = 0;
10763 	u16 release = 0;
10764 
10765 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10766 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10767 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10768 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10769 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10770 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10771 #define I40E_NVM_OEM_LENGTH			3
10772 
10773 	/* Check if pointer to OEM version block is valid. */
10774 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10775 	if (block_offset == 0xffff)
10776 		return;
10777 
10778 	/* Check if OEM version block has correct length. */
10779 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10780 			   &block_length);
10781 	if (block_length < I40E_NVM_OEM_LENGTH)
10782 		return;
10783 
10784 	/* Check if OEM version format is as expected. */
10785 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10786 			   &capabilities);
10787 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10788 		return;
10789 
10790 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10791 			   &gen_snap);
10792 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10793 			   &release);
10794 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10795 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10796 }
10797 
10798 /**
10799  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10800  * @pf: board private structure
10801  **/
i40e_reset(struct i40e_pf * pf)10802 static int i40e_reset(struct i40e_pf *pf)
10803 {
10804 	struct i40e_hw *hw = &pf->hw;
10805 	int ret;
10806 
10807 	ret = i40e_pf_reset(hw);
10808 	if (ret) {
10809 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10810 		set_bit(__I40E_RESET_FAILED, pf->state);
10811 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10812 	} else {
10813 		pf->pfr_count++;
10814 	}
10815 	return ret;
10816 }
10817 
10818 /**
10819  * i40e_rebuild - rebuild using a saved config
10820  * @pf: board private structure
10821  * @reinit: if the Main VSI needs to re-initialized.
10822  * @lock_acquired: indicates whether or not the lock has been acquired
10823  * before this function was called.
10824  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10825 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10826 {
10827 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10828 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10829 	struct i40e_hw *hw = &pf->hw;
10830 	int ret;
10831 	u32 val;
10832 	int v;
10833 
10834 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10835 	    is_recovery_mode_reported)
10836 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10837 
10838 	if (test_bit(__I40E_DOWN, pf->state) &&
10839 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10840 		goto clear_recovery;
10841 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10842 
10843 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10844 	ret = i40e_init_adminq(&pf->hw);
10845 	if (ret) {
10846 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10847 			 ERR_PTR(ret),
10848 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10849 		goto clear_recovery;
10850 	}
10851 	i40e_get_oem_version(&pf->hw);
10852 
10853 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10854 		/* The following delay is necessary for firmware update. */
10855 		mdelay(1000);
10856 	}
10857 
10858 	/* re-verify the eeprom if we just had an EMP reset */
10859 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10860 		i40e_verify_eeprom(pf);
10861 
10862 	/* if we are going out of or into recovery mode we have to act
10863 	 * accordingly with regard to resources initialization
10864 	 * and deinitialization
10865 	 */
10866 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10867 		if (i40e_get_capabilities(pf,
10868 					  i40e_aqc_opc_list_func_capabilities))
10869 			goto end_unlock;
10870 
10871 		if (is_recovery_mode_reported) {
10872 			/* we're staying in recovery mode so we'll reinitialize
10873 			 * misc vector here
10874 			 */
10875 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10876 				goto end_unlock;
10877 		} else {
10878 			if (!lock_acquired)
10879 				rtnl_lock();
10880 			/* we're going out of recovery mode so we'll free
10881 			 * the IRQ allocated specifically for recovery mode
10882 			 * and restore the interrupt scheme
10883 			 */
10884 			free_irq(pf->pdev->irq, pf);
10885 			i40e_clear_interrupt_scheme(pf);
10886 			if (i40e_restore_interrupt_scheme(pf))
10887 				goto end_unlock;
10888 		}
10889 
10890 		/* tell the firmware that we're starting */
10891 		i40e_send_version(pf);
10892 
10893 		/* bail out in case recovery mode was detected, as there is
10894 		 * no need for further configuration.
10895 		 */
10896 		goto end_unlock;
10897 	}
10898 
10899 	i40e_clear_pxe_mode(hw);
10900 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10901 	if (ret)
10902 		goto end_core_reset;
10903 
10904 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10905 				hw->func_caps.num_rx_qp, 0, 0);
10906 	if (ret) {
10907 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10908 		goto end_core_reset;
10909 	}
10910 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10911 	if (ret) {
10912 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10913 		goto end_core_reset;
10914 	}
10915 
10916 #ifdef CONFIG_I40E_DCB
10917 	/* Enable FW to write a default DCB config on link-up
10918 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10919 	 * is not supported with new link speed
10920 	 */
10921 	if (i40e_is_tc_mqprio_enabled(pf)) {
10922 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10923 	} else {
10924 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10925 		    (hw->phy.link_info.link_speed &
10926 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10927 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10928 			dev_warn(&pf->pdev->dev,
10929 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10930 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10931 		} else {
10932 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10933 			ret = i40e_init_pf_dcb(pf);
10934 			if (ret) {
10935 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10936 					 ret);
10937 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10938 				/* Continue without DCB enabled */
10939 			}
10940 		}
10941 	}
10942 
10943 #endif /* CONFIG_I40E_DCB */
10944 	if (!lock_acquired)
10945 		rtnl_lock();
10946 	ret = i40e_setup_pf_switch(pf, reinit, true);
10947 	if (ret)
10948 		goto end_unlock;
10949 
10950 	/* The driver only wants link up/down and module qualification
10951 	 * reports from firmware.  Note the negative logic.
10952 	 */
10953 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10954 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10955 					 I40E_AQ_EVENT_MEDIA_NA |
10956 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10957 	if (ret)
10958 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10959 			 ERR_PTR(ret),
10960 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10961 
10962 	/* Rebuild the VSIs and VEBs that existed before reset.
10963 	 * They are still in our local switch element arrays, so only
10964 	 * need to rebuild the switch model in the HW.
10965 	 *
10966 	 * If there were VEBs but the reconstitution failed, we'll try
10967 	 * to recover minimal use by getting the basic PF VSI working.
10968 	 */
10969 	if (vsi->uplink_seid != pf->mac_seid) {
10970 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10971 		/* find the one VEB connected to the MAC, and find orphans */
10972 		for (v = 0; v < I40E_MAX_VEB; v++) {
10973 			if (!pf->veb[v])
10974 				continue;
10975 
10976 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10977 			    pf->veb[v]->uplink_seid == 0) {
10978 				ret = i40e_reconstitute_veb(pf->veb[v]);
10979 
10980 				if (!ret)
10981 					continue;
10982 
10983 				/* If Main VEB failed, we're in deep doodoo,
10984 				 * so give up rebuilding the switch and set up
10985 				 * for minimal rebuild of PF VSI.
10986 				 * If orphan failed, we'll report the error
10987 				 * but try to keep going.
10988 				 */
10989 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10990 					dev_info(&pf->pdev->dev,
10991 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10992 						 ret);
10993 					vsi->uplink_seid = pf->mac_seid;
10994 					break;
10995 				} else if (pf->veb[v]->uplink_seid == 0) {
10996 					dev_info(&pf->pdev->dev,
10997 						 "rebuild of orphan VEB failed: %d\n",
10998 						 ret);
10999 				}
11000 			}
11001 		}
11002 	}
11003 
11004 	if (vsi->uplink_seid == pf->mac_seid) {
11005 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11006 		/* no VEB, so rebuild only the Main VSI */
11007 		ret = i40e_add_vsi(vsi);
11008 		if (ret) {
11009 			dev_info(&pf->pdev->dev,
11010 				 "rebuild of Main VSI failed: %d\n", ret);
11011 			goto end_unlock;
11012 		}
11013 	}
11014 
11015 	if (vsi->mqprio_qopt.max_rate[0]) {
11016 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11017 						  vsi->mqprio_qopt.max_rate[0]);
11018 		u64 credits = 0;
11019 
11020 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11021 		if (ret)
11022 			goto end_unlock;
11023 
11024 		credits = max_tx_rate;
11025 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11026 		dev_dbg(&vsi->back->pdev->dev,
11027 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11028 			max_tx_rate,
11029 			credits,
11030 			vsi->seid);
11031 	}
11032 
11033 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11034 	if (ret)
11035 		goto end_unlock;
11036 
11037 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11038 	 * for this main VSI if they exist
11039 	 */
11040 	ret = i40e_rebuild_channels(vsi);
11041 	if (ret)
11042 		goto end_unlock;
11043 
11044 	/* Reconfigure hardware for allowing smaller MSS in the case
11045 	 * of TSO, so that we avoid the MDD being fired and causing
11046 	 * a reset in the case of small MSS+TSO.
11047 	 */
11048 #define I40E_REG_MSS          0x000E64DC
11049 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11050 #define I40E_64BYTE_MSS       0x400000
11051 	val = rd32(hw, I40E_REG_MSS);
11052 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11053 		val &= ~I40E_REG_MSS_MIN_MASK;
11054 		val |= I40E_64BYTE_MSS;
11055 		wr32(hw, I40E_REG_MSS, val);
11056 	}
11057 
11058 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11059 		msleep(75);
11060 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11061 		if (ret)
11062 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11063 				 ERR_PTR(ret),
11064 				 i40e_aq_str(&pf->hw,
11065 					     pf->hw.aq.asq_last_status));
11066 	}
11067 	/* reinit the misc interrupt */
11068 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11069 		ret = i40e_setup_misc_vector(pf);
11070 		if (ret)
11071 			goto end_unlock;
11072 	}
11073 
11074 	/* Add a filter to drop all Flow control frames from any VSI from being
11075 	 * transmitted. By doing so we stop a malicious VF from sending out
11076 	 * PAUSE or PFC frames and potentially controlling traffic for other
11077 	 * PF/VF VSIs.
11078 	 * The FW can still send Flow control frames if enabled.
11079 	 */
11080 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11081 						       pf->main_vsi_seid);
11082 
11083 	/* restart the VSIs that were rebuilt and running before the reset */
11084 	i40e_pf_unquiesce_all_vsi(pf);
11085 
11086 	/* Release the RTNL lock before we start resetting VFs */
11087 	if (!lock_acquired)
11088 		rtnl_unlock();
11089 
11090 	/* Restore promiscuous settings */
11091 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11092 	if (ret)
11093 		dev_warn(&pf->pdev->dev,
11094 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11095 			 pf->cur_promisc ? "on" : "off",
11096 			 ERR_PTR(ret),
11097 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11098 
11099 	i40e_reset_all_vfs(pf, true);
11100 
11101 	/* tell the firmware that we're starting */
11102 	i40e_send_version(pf);
11103 
11104 	/* We've already released the lock, so don't do it again */
11105 	goto end_core_reset;
11106 
11107 end_unlock:
11108 	if (!lock_acquired)
11109 		rtnl_unlock();
11110 end_core_reset:
11111 	clear_bit(__I40E_RESET_FAILED, pf->state);
11112 clear_recovery:
11113 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11114 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11115 }
11116 
11117 /**
11118  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11119  * @pf: board private structure
11120  * @reinit: if the Main VSI needs to re-initialized.
11121  * @lock_acquired: indicates whether or not the lock has been acquired
11122  * before this function was called.
11123  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)11124 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11125 				   bool lock_acquired)
11126 {
11127 	int ret;
11128 
11129 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11130 		return;
11131 	/* Now we wait for GRST to settle out.
11132 	 * We don't have to delete the VEBs or VSIs from the hw switch
11133 	 * because the reset will make them disappear.
11134 	 */
11135 	ret = i40e_reset(pf);
11136 	if (!ret)
11137 		i40e_rebuild(pf, reinit, lock_acquired);
11138 }
11139 
11140 /**
11141  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11142  * @pf: board private structure
11143  *
11144  * Close up the VFs and other things in prep for a Core Reset,
11145  * then get ready to rebuild the world.
11146  * @lock_acquired: indicates whether or not the lock has been acquired
11147  * before this function was called.
11148  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)11149 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11150 {
11151 	i40e_prep_for_reset(pf);
11152 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11153 }
11154 
11155 /**
11156  * i40e_handle_mdd_event
11157  * @pf: pointer to the PF structure
11158  *
11159  * Called from the MDD irq handler to identify possibly malicious vfs
11160  **/
i40e_handle_mdd_event(struct i40e_pf * pf)11161 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11162 {
11163 	struct i40e_hw *hw = &pf->hw;
11164 	bool mdd_detected = false;
11165 	struct i40e_vf *vf;
11166 	u32 reg;
11167 	int i;
11168 
11169 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11170 		return;
11171 
11172 	/* find what triggered the MDD event */
11173 	reg = rd32(hw, I40E_GL_MDET_TX);
11174 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11175 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11176 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11177 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11178 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11179 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11180 				I40E_GL_MDET_TX_EVENT_SHIFT;
11181 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11182 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11183 				pf->hw.func_caps.base_queue;
11184 		if (netif_msg_tx_err(pf))
11185 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11186 				 event, queue, pf_num, vf_num);
11187 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11188 		mdd_detected = true;
11189 	}
11190 	reg = rd32(hw, I40E_GL_MDET_RX);
11191 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11192 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11193 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11194 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11195 				I40E_GL_MDET_RX_EVENT_SHIFT;
11196 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11197 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11198 				pf->hw.func_caps.base_queue;
11199 		if (netif_msg_rx_err(pf))
11200 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11201 				 event, queue, func);
11202 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11203 		mdd_detected = true;
11204 	}
11205 
11206 	if (mdd_detected) {
11207 		reg = rd32(hw, I40E_PF_MDET_TX);
11208 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11209 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11210 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11211 		}
11212 		reg = rd32(hw, I40E_PF_MDET_RX);
11213 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11214 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11215 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11216 		}
11217 	}
11218 
11219 	/* see if one of the VFs needs its hand slapped */
11220 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11221 		vf = &(pf->vf[i]);
11222 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11223 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11224 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11225 			vf->num_mdd_events++;
11226 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11227 				 i);
11228 			dev_info(&pf->pdev->dev,
11229 				 "Use PF Control I/F to re-enable the VF\n");
11230 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11231 		}
11232 
11233 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11234 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11235 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11236 			vf->num_mdd_events++;
11237 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11238 				 i);
11239 			dev_info(&pf->pdev->dev,
11240 				 "Use PF Control I/F to re-enable the VF\n");
11241 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11242 		}
11243 	}
11244 
11245 	/* re-enable mdd interrupt cause */
11246 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11247 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11248 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11249 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11250 	i40e_flush(hw);
11251 }
11252 
11253 /**
11254  * i40e_service_task - Run the driver's async subtasks
11255  * @work: pointer to work_struct containing our data
11256  **/
i40e_service_task(struct work_struct * work)11257 static void i40e_service_task(struct work_struct *work)
11258 {
11259 	struct i40e_pf *pf = container_of(work,
11260 					  struct i40e_pf,
11261 					  service_task);
11262 	unsigned long start_time = jiffies;
11263 
11264 	/* don't bother with service tasks if a reset is in progress */
11265 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11266 	    test_bit(__I40E_SUSPENDED, pf->state))
11267 		return;
11268 
11269 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11270 		return;
11271 
11272 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11273 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11274 		i40e_sync_filters_subtask(pf);
11275 		i40e_reset_subtask(pf);
11276 		i40e_handle_mdd_event(pf);
11277 		i40e_vc_process_vflr_event(pf);
11278 		i40e_watchdog_subtask(pf);
11279 		i40e_fdir_reinit_subtask(pf);
11280 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11281 			/* Client subtask will reopen next time through. */
11282 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11283 							   true);
11284 		} else {
11285 			i40e_client_subtask(pf);
11286 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11287 					       pf->state))
11288 				i40e_notify_client_of_l2_param_changes(
11289 								pf->vsi[pf->lan_vsi]);
11290 		}
11291 		i40e_sync_filters_subtask(pf);
11292 	} else {
11293 		i40e_reset_subtask(pf);
11294 	}
11295 
11296 	i40e_clean_adminq_subtask(pf);
11297 
11298 	/* flush memory to make sure state is correct before next watchdog */
11299 	smp_mb__before_atomic();
11300 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11301 
11302 	/* If the tasks have taken longer than one timer cycle or there
11303 	 * is more work to be done, reschedule the service task now
11304 	 * rather than wait for the timer to tick again.
11305 	 */
11306 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11307 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11308 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11309 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11310 		i40e_service_event_schedule(pf);
11311 }
11312 
11313 /**
11314  * i40e_service_timer - timer callback
11315  * @t: timer list pointer
11316  **/
i40e_service_timer(struct timer_list * t)11317 static void i40e_service_timer(struct timer_list *t)
11318 {
11319 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11320 
11321 	mod_timer(&pf->service_timer,
11322 		  round_jiffies(jiffies + pf->service_timer_period));
11323 	i40e_service_event_schedule(pf);
11324 }
11325 
11326 /**
11327  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11328  * @vsi: the VSI being configured
11329  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)11330 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11331 {
11332 	struct i40e_pf *pf = vsi->back;
11333 
11334 	switch (vsi->type) {
11335 	case I40E_VSI_MAIN:
11336 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11337 		if (!vsi->num_tx_desc)
11338 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11339 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11340 		if (!vsi->num_rx_desc)
11341 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11342 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11343 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11344 			vsi->num_q_vectors = pf->num_lan_msix;
11345 		else
11346 			vsi->num_q_vectors = 1;
11347 
11348 		break;
11349 
11350 	case I40E_VSI_FDIR:
11351 		vsi->alloc_queue_pairs = 1;
11352 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11353 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11354 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11355 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11356 		vsi->num_q_vectors = pf->num_fdsb_msix;
11357 		break;
11358 
11359 	case I40E_VSI_VMDQ2:
11360 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11361 		if (!vsi->num_tx_desc)
11362 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11363 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11364 		if (!vsi->num_rx_desc)
11365 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11366 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11367 		vsi->num_q_vectors = pf->num_vmdq_msix;
11368 		break;
11369 
11370 	case I40E_VSI_SRIOV:
11371 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11372 		if (!vsi->num_tx_desc)
11373 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11374 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11375 		if (!vsi->num_rx_desc)
11376 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11377 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11378 		break;
11379 
11380 	default:
11381 		WARN_ON(1);
11382 		return -ENODATA;
11383 	}
11384 
11385 	if (is_kdump_kernel()) {
11386 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11387 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11388 	}
11389 
11390 	return 0;
11391 }
11392 
11393 /**
11394  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11395  * @vsi: VSI pointer
11396  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11397  *
11398  * On error: returns error code (negative)
11399  * On success: returns 0
11400  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)11401 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11402 {
11403 	struct i40e_ring **next_rings;
11404 	int size;
11405 	int ret = 0;
11406 
11407 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11408 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11409 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11410 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11411 	if (!vsi->tx_rings)
11412 		return -ENOMEM;
11413 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11414 	if (i40e_enabled_xdp_vsi(vsi)) {
11415 		vsi->xdp_rings = next_rings;
11416 		next_rings += vsi->alloc_queue_pairs;
11417 	}
11418 	vsi->rx_rings = next_rings;
11419 
11420 	if (alloc_qvectors) {
11421 		/* allocate memory for q_vector pointers */
11422 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11423 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11424 		if (!vsi->q_vectors) {
11425 			ret = -ENOMEM;
11426 			goto err_vectors;
11427 		}
11428 	}
11429 	return ret;
11430 
11431 err_vectors:
11432 	kfree(vsi->tx_rings);
11433 	return ret;
11434 }
11435 
11436 /**
11437  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11438  * @pf: board private structure
11439  * @type: type of VSI
11440  *
11441  * On error: returns error code (negative)
11442  * On success: returns vsi index in PF (positive)
11443  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)11444 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11445 {
11446 	int ret = -ENODEV;
11447 	struct i40e_vsi *vsi;
11448 	int vsi_idx;
11449 	int i;
11450 
11451 	/* Need to protect the allocation of the VSIs at the PF level */
11452 	mutex_lock(&pf->switch_mutex);
11453 
11454 	/* VSI list may be fragmented if VSI creation/destruction has
11455 	 * been happening.  We can afford to do a quick scan to look
11456 	 * for any free VSIs in the list.
11457 	 *
11458 	 * find next empty vsi slot, looping back around if necessary
11459 	 */
11460 	i = pf->next_vsi;
11461 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11462 		i++;
11463 	if (i >= pf->num_alloc_vsi) {
11464 		i = 0;
11465 		while (i < pf->next_vsi && pf->vsi[i])
11466 			i++;
11467 	}
11468 
11469 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11470 		vsi_idx = i;             /* Found one! */
11471 	} else {
11472 		ret = -ENODEV;
11473 		goto unlock_pf;  /* out of VSI slots! */
11474 	}
11475 	pf->next_vsi = ++i;
11476 
11477 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11478 	if (!vsi) {
11479 		ret = -ENOMEM;
11480 		goto unlock_pf;
11481 	}
11482 	vsi->type = type;
11483 	vsi->back = pf;
11484 	set_bit(__I40E_VSI_DOWN, vsi->state);
11485 	vsi->flags = 0;
11486 	vsi->idx = vsi_idx;
11487 	vsi->int_rate_limit = 0;
11488 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11489 				pf->rss_table_size : 64;
11490 	vsi->netdev_registered = false;
11491 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11492 	hash_init(vsi->mac_filter_hash);
11493 	vsi->irqs_ready = false;
11494 
11495 	if (type == I40E_VSI_MAIN) {
11496 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11497 		if (!vsi->af_xdp_zc_qps)
11498 			goto err_rings;
11499 	}
11500 
11501 	ret = i40e_set_num_rings_in_vsi(vsi);
11502 	if (ret)
11503 		goto err_rings;
11504 
11505 	ret = i40e_vsi_alloc_arrays(vsi, true);
11506 	if (ret)
11507 		goto err_rings;
11508 
11509 	/* Setup default MSIX irq handler for VSI */
11510 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11511 
11512 	/* Initialize VSI lock */
11513 	spin_lock_init(&vsi->mac_filter_hash_lock);
11514 	pf->vsi[vsi_idx] = vsi;
11515 	ret = vsi_idx;
11516 	goto unlock_pf;
11517 
11518 err_rings:
11519 	bitmap_free(vsi->af_xdp_zc_qps);
11520 	pf->next_vsi = i - 1;
11521 	kfree(vsi);
11522 unlock_pf:
11523 	mutex_unlock(&pf->switch_mutex);
11524 	return ret;
11525 }
11526 
11527 /**
11528  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11529  * @vsi: VSI pointer
11530  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11531  *
11532  * On error: returns error code (negative)
11533  * On success: returns 0
11534  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)11535 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11536 {
11537 	/* free the ring and vector containers */
11538 	if (free_qvectors) {
11539 		kfree(vsi->q_vectors);
11540 		vsi->q_vectors = NULL;
11541 	}
11542 	kfree(vsi->tx_rings);
11543 	vsi->tx_rings = NULL;
11544 	vsi->rx_rings = NULL;
11545 	vsi->xdp_rings = NULL;
11546 }
11547 
11548 /**
11549  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11550  * and lookup table
11551  * @vsi: Pointer to VSI structure
11552  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)11553 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11554 {
11555 	if (!vsi)
11556 		return;
11557 
11558 	kfree(vsi->rss_hkey_user);
11559 	vsi->rss_hkey_user = NULL;
11560 
11561 	kfree(vsi->rss_lut_user);
11562 	vsi->rss_lut_user = NULL;
11563 }
11564 
11565 /**
11566  * i40e_vsi_clear - Deallocate the VSI provided
11567  * @vsi: the VSI being un-configured
11568  **/
i40e_vsi_clear(struct i40e_vsi * vsi)11569 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11570 {
11571 	struct i40e_pf *pf;
11572 
11573 	if (!vsi)
11574 		return 0;
11575 
11576 	if (!vsi->back)
11577 		goto free_vsi;
11578 	pf = vsi->back;
11579 
11580 	mutex_lock(&pf->switch_mutex);
11581 	if (!pf->vsi[vsi->idx]) {
11582 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11583 			vsi->idx, vsi->idx, vsi->type);
11584 		goto unlock_vsi;
11585 	}
11586 
11587 	if (pf->vsi[vsi->idx] != vsi) {
11588 		dev_err(&pf->pdev->dev,
11589 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11590 			pf->vsi[vsi->idx]->idx,
11591 			pf->vsi[vsi->idx]->type,
11592 			vsi->idx, vsi->type);
11593 		goto unlock_vsi;
11594 	}
11595 
11596 	/* updates the PF for this cleared vsi */
11597 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11598 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11599 
11600 	bitmap_free(vsi->af_xdp_zc_qps);
11601 	i40e_vsi_free_arrays(vsi, true);
11602 	i40e_clear_rss_config_user(vsi);
11603 
11604 	pf->vsi[vsi->idx] = NULL;
11605 	if (vsi->idx < pf->next_vsi)
11606 		pf->next_vsi = vsi->idx;
11607 
11608 unlock_vsi:
11609 	mutex_unlock(&pf->switch_mutex);
11610 free_vsi:
11611 	kfree(vsi);
11612 
11613 	return 0;
11614 }
11615 
11616 /**
11617  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11618  * @vsi: the VSI being cleaned
11619  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)11620 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11621 {
11622 	int i;
11623 
11624 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11625 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11626 			kfree_rcu(vsi->tx_rings[i], rcu);
11627 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11628 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11629 			if (vsi->xdp_rings)
11630 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11631 		}
11632 	}
11633 }
11634 
11635 /**
11636  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11637  * @vsi: the VSI being configured
11638  **/
i40e_alloc_rings(struct i40e_vsi * vsi)11639 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11640 {
11641 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11642 	struct i40e_pf *pf = vsi->back;
11643 	struct i40e_ring *ring;
11644 
11645 	/* Set basic values in the rings to be used later during open() */
11646 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11647 		/* allocate space for both Tx and Rx in one shot */
11648 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11649 		if (!ring)
11650 			goto err_out;
11651 
11652 		ring->queue_index = i;
11653 		ring->reg_idx = vsi->base_queue + i;
11654 		ring->ring_active = false;
11655 		ring->vsi = vsi;
11656 		ring->netdev = vsi->netdev;
11657 		ring->dev = &pf->pdev->dev;
11658 		ring->count = vsi->num_tx_desc;
11659 		ring->size = 0;
11660 		ring->dcb_tc = 0;
11661 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11662 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11663 		ring->itr_setting = pf->tx_itr_default;
11664 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11665 
11666 		if (!i40e_enabled_xdp_vsi(vsi))
11667 			goto setup_rx;
11668 
11669 		ring->queue_index = vsi->alloc_queue_pairs + i;
11670 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11671 		ring->ring_active = false;
11672 		ring->vsi = vsi;
11673 		ring->netdev = NULL;
11674 		ring->dev = &pf->pdev->dev;
11675 		ring->count = vsi->num_tx_desc;
11676 		ring->size = 0;
11677 		ring->dcb_tc = 0;
11678 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11679 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11680 		set_ring_xdp(ring);
11681 		ring->itr_setting = pf->tx_itr_default;
11682 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11683 
11684 setup_rx:
11685 		ring->queue_index = i;
11686 		ring->reg_idx = vsi->base_queue + i;
11687 		ring->ring_active = false;
11688 		ring->vsi = vsi;
11689 		ring->netdev = vsi->netdev;
11690 		ring->dev = &pf->pdev->dev;
11691 		ring->count = vsi->num_rx_desc;
11692 		ring->size = 0;
11693 		ring->dcb_tc = 0;
11694 		ring->itr_setting = pf->rx_itr_default;
11695 		WRITE_ONCE(vsi->rx_rings[i], ring);
11696 	}
11697 
11698 	return 0;
11699 
11700 err_out:
11701 	i40e_vsi_clear_rings(vsi);
11702 	return -ENOMEM;
11703 }
11704 
11705 /**
11706  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11707  * @pf: board private structure
11708  * @vectors: the number of MSI-X vectors to request
11709  *
11710  * Returns the number of vectors reserved, or error
11711  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)11712 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11713 {
11714 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11715 					I40E_MIN_MSIX, vectors);
11716 	if (vectors < 0) {
11717 		dev_info(&pf->pdev->dev,
11718 			 "MSI-X vector reservation failed: %d\n", vectors);
11719 		vectors = 0;
11720 	}
11721 
11722 	return vectors;
11723 }
11724 
11725 /**
11726  * i40e_init_msix - Setup the MSIX capability
11727  * @pf: board private structure
11728  *
11729  * Work with the OS to set up the MSIX vectors needed.
11730  *
11731  * Returns the number of vectors reserved or negative on failure
11732  **/
i40e_init_msix(struct i40e_pf * pf)11733 static int i40e_init_msix(struct i40e_pf *pf)
11734 {
11735 	struct i40e_hw *hw = &pf->hw;
11736 	int cpus, extra_vectors;
11737 	int vectors_left;
11738 	int v_budget, i;
11739 	int v_actual;
11740 	int iwarp_requested = 0;
11741 
11742 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11743 		return -ENODEV;
11744 
11745 	/* The number of vectors we'll request will be comprised of:
11746 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11747 	 *   - The number of LAN queue pairs
11748 	 *	- Queues being used for RSS.
11749 	 *		We don't need as many as max_rss_size vectors.
11750 	 *		use rss_size instead in the calculation since that
11751 	 *		is governed by number of cpus in the system.
11752 	 *	- assumes symmetric Tx/Rx pairing
11753 	 *   - The number of VMDq pairs
11754 	 *   - The CPU count within the NUMA node if iWARP is enabled
11755 	 * Once we count this up, try the request.
11756 	 *
11757 	 * If we can't get what we want, we'll simplify to nearly nothing
11758 	 * and try again.  If that still fails, we punt.
11759 	 */
11760 	vectors_left = hw->func_caps.num_msix_vectors;
11761 	v_budget = 0;
11762 
11763 	/* reserve one vector for miscellaneous handler */
11764 	if (vectors_left) {
11765 		v_budget++;
11766 		vectors_left--;
11767 	}
11768 
11769 	/* reserve some vectors for the main PF traffic queues. Initially we
11770 	 * only reserve at most 50% of the available vectors, in the case that
11771 	 * the number of online CPUs is large. This ensures that we can enable
11772 	 * extra features as well. Once we've enabled the other features, we
11773 	 * will use any remaining vectors to reach as close as we can to the
11774 	 * number of online CPUs.
11775 	 */
11776 	cpus = num_online_cpus();
11777 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11778 	vectors_left -= pf->num_lan_msix;
11779 
11780 	/* reserve one vector for sideband flow director */
11781 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11782 		if (vectors_left) {
11783 			pf->num_fdsb_msix = 1;
11784 			v_budget++;
11785 			vectors_left--;
11786 		} else {
11787 			pf->num_fdsb_msix = 0;
11788 		}
11789 	}
11790 
11791 	/* can we reserve enough for iWARP? */
11792 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11793 		iwarp_requested = pf->num_iwarp_msix;
11794 
11795 		if (!vectors_left)
11796 			pf->num_iwarp_msix = 0;
11797 		else if (vectors_left < pf->num_iwarp_msix)
11798 			pf->num_iwarp_msix = 1;
11799 		v_budget += pf->num_iwarp_msix;
11800 		vectors_left -= pf->num_iwarp_msix;
11801 	}
11802 
11803 	/* any vectors left over go for VMDq support */
11804 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11805 		if (!vectors_left) {
11806 			pf->num_vmdq_msix = 0;
11807 			pf->num_vmdq_qps = 0;
11808 		} else {
11809 			int vmdq_vecs_wanted =
11810 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11811 			int vmdq_vecs =
11812 				min_t(int, vectors_left, vmdq_vecs_wanted);
11813 
11814 			/* if we're short on vectors for what's desired, we limit
11815 			 * the queues per vmdq.  If this is still more than are
11816 			 * available, the user will need to change the number of
11817 			 * queues/vectors used by the PF later with the ethtool
11818 			 * channels command
11819 			 */
11820 			if (vectors_left < vmdq_vecs_wanted) {
11821 				pf->num_vmdq_qps = 1;
11822 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11823 				vmdq_vecs = min_t(int,
11824 						  vectors_left,
11825 						  vmdq_vecs_wanted);
11826 			}
11827 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11828 
11829 			v_budget += vmdq_vecs;
11830 			vectors_left -= vmdq_vecs;
11831 		}
11832 	}
11833 
11834 	/* On systems with a large number of SMP cores, we previously limited
11835 	 * the number of vectors for num_lan_msix to be at most 50% of the
11836 	 * available vectors, to allow for other features. Now, we add back
11837 	 * the remaining vectors. However, we ensure that the total
11838 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11839 	 * calculate the number of vectors we can add without going over the
11840 	 * cap of CPUs. For systems with a small number of CPUs this will be
11841 	 * zero.
11842 	 */
11843 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11844 	pf->num_lan_msix += extra_vectors;
11845 	vectors_left -= extra_vectors;
11846 
11847 	WARN(vectors_left < 0,
11848 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11849 
11850 	v_budget += pf->num_lan_msix;
11851 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11852 				   GFP_KERNEL);
11853 	if (!pf->msix_entries)
11854 		return -ENOMEM;
11855 
11856 	for (i = 0; i < v_budget; i++)
11857 		pf->msix_entries[i].entry = i;
11858 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11859 
11860 	if (v_actual < I40E_MIN_MSIX) {
11861 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11862 		kfree(pf->msix_entries);
11863 		pf->msix_entries = NULL;
11864 		pci_disable_msix(pf->pdev);
11865 		return -ENODEV;
11866 
11867 	} else if (v_actual == I40E_MIN_MSIX) {
11868 		/* Adjust for minimal MSIX use */
11869 		pf->num_vmdq_vsis = 0;
11870 		pf->num_vmdq_qps = 0;
11871 		pf->num_lan_qps = 1;
11872 		pf->num_lan_msix = 1;
11873 
11874 	} else if (v_actual != v_budget) {
11875 		/* If we have limited resources, we will start with no vectors
11876 		 * for the special features and then allocate vectors to some
11877 		 * of these features based on the policy and at the end disable
11878 		 * the features that did not get any vectors.
11879 		 */
11880 		int vec;
11881 
11882 		dev_info(&pf->pdev->dev,
11883 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11884 			 v_actual, v_budget);
11885 		/* reserve the misc vector */
11886 		vec = v_actual - 1;
11887 
11888 		/* Scale vector usage down */
11889 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11890 		pf->num_vmdq_vsis = 1;
11891 		pf->num_vmdq_qps = 1;
11892 
11893 		/* partition out the remaining vectors */
11894 		switch (vec) {
11895 		case 2:
11896 			pf->num_lan_msix = 1;
11897 			break;
11898 		case 3:
11899 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11900 				pf->num_lan_msix = 1;
11901 				pf->num_iwarp_msix = 1;
11902 			} else {
11903 				pf->num_lan_msix = 2;
11904 			}
11905 			break;
11906 		default:
11907 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11908 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11909 						 iwarp_requested);
11910 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11911 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11912 			} else {
11913 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11914 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11915 			}
11916 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11917 				pf->num_fdsb_msix = 1;
11918 				vec--;
11919 			}
11920 			pf->num_lan_msix = min_t(int,
11921 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11922 							      pf->num_lan_msix);
11923 			pf->num_lan_qps = pf->num_lan_msix;
11924 			break;
11925 		}
11926 	}
11927 
11928 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11929 	    (pf->num_fdsb_msix == 0)) {
11930 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11931 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11932 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11933 	}
11934 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11935 	    (pf->num_vmdq_msix == 0)) {
11936 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11937 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11938 	}
11939 
11940 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11941 	    (pf->num_iwarp_msix == 0)) {
11942 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11943 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11944 	}
11945 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11946 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11947 		   pf->num_lan_msix,
11948 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11949 		   pf->num_fdsb_msix,
11950 		   pf->num_iwarp_msix);
11951 
11952 	return v_actual;
11953 }
11954 
11955 /**
11956  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11957  * @vsi: the VSI being configured
11958  * @v_idx: index of the vector in the vsi struct
11959  *
11960  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11961  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)11962 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11963 {
11964 	struct i40e_q_vector *q_vector;
11965 
11966 	/* allocate q_vector */
11967 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11968 	if (!q_vector)
11969 		return -ENOMEM;
11970 
11971 	q_vector->vsi = vsi;
11972 	q_vector->v_idx = v_idx;
11973 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11974 
11975 	if (vsi->netdev)
11976 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11977 
11978 	/* tie q_vector and vsi together */
11979 	vsi->q_vectors[v_idx] = q_vector;
11980 
11981 	return 0;
11982 }
11983 
11984 /**
11985  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11986  * @vsi: the VSI being configured
11987  *
11988  * We allocate one q_vector per queue interrupt.  If allocation fails we
11989  * return -ENOMEM.
11990  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)11991 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11992 {
11993 	struct i40e_pf *pf = vsi->back;
11994 	int err, v_idx, num_q_vectors;
11995 
11996 	/* if not MSIX, give the one vector only to the LAN VSI */
11997 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11998 		num_q_vectors = vsi->num_q_vectors;
11999 	else if (vsi == pf->vsi[pf->lan_vsi])
12000 		num_q_vectors = 1;
12001 	else
12002 		return -EINVAL;
12003 
12004 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12005 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12006 		if (err)
12007 			goto err_out;
12008 	}
12009 
12010 	return 0;
12011 
12012 err_out:
12013 	while (v_idx--)
12014 		i40e_free_q_vector(vsi, v_idx);
12015 
12016 	return err;
12017 }
12018 
12019 /**
12020  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12021  * @pf: board private structure to initialize
12022  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)12023 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12024 {
12025 	int vectors = 0;
12026 	ssize_t size;
12027 
12028 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12029 		vectors = i40e_init_msix(pf);
12030 		if (vectors < 0) {
12031 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12032 				       I40E_FLAG_IWARP_ENABLED	|
12033 				       I40E_FLAG_RSS_ENABLED	|
12034 				       I40E_FLAG_DCB_CAPABLE	|
12035 				       I40E_FLAG_DCB_ENABLED	|
12036 				       I40E_FLAG_SRIOV_ENABLED	|
12037 				       I40E_FLAG_FD_SB_ENABLED	|
12038 				       I40E_FLAG_FD_ATR_ENABLED	|
12039 				       I40E_FLAG_VMDQ_ENABLED);
12040 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12041 
12042 			/* rework the queue expectations without MSIX */
12043 			i40e_determine_queue_usage(pf);
12044 		}
12045 	}
12046 
12047 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12048 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12049 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12050 		vectors = pci_enable_msi(pf->pdev);
12051 		if (vectors < 0) {
12052 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12053 				 vectors);
12054 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12055 		}
12056 		vectors = 1;  /* one MSI or Legacy vector */
12057 	}
12058 
12059 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12060 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12061 
12062 	/* set up vector assignment tracking */
12063 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12064 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12065 	if (!pf->irq_pile)
12066 		return -ENOMEM;
12067 
12068 	pf->irq_pile->num_entries = vectors;
12069 
12070 	/* track first vector for misc interrupts, ignore return */
12071 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12072 
12073 	return 0;
12074 }
12075 
12076 /**
12077  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12078  * @pf: private board data structure
12079  *
12080  * Restore the interrupt scheme that was cleared when we suspended the
12081  * device. This should be called during resume to re-allocate the q_vectors
12082  * and reacquire IRQs.
12083  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)12084 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12085 {
12086 	int err, i;
12087 
12088 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12089 	 * scheme. We need to re-enabled them here in order to attempt to
12090 	 * re-acquire the MSI or MSI-X vectors
12091 	 */
12092 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12093 
12094 	err = i40e_init_interrupt_scheme(pf);
12095 	if (err)
12096 		return err;
12097 
12098 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12099 	 * rings together again.
12100 	 */
12101 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12102 		if (pf->vsi[i]) {
12103 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12104 			if (err)
12105 				goto err_unwind;
12106 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12107 		}
12108 	}
12109 
12110 	err = i40e_setup_misc_vector(pf);
12111 	if (err)
12112 		goto err_unwind;
12113 
12114 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12115 		i40e_client_update_msix_info(pf);
12116 
12117 	return 0;
12118 
12119 err_unwind:
12120 	while (i--) {
12121 		if (pf->vsi[i])
12122 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12123 	}
12124 
12125 	return err;
12126 }
12127 
12128 /**
12129  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12130  * non queue events in recovery mode
12131  * @pf: board private structure
12132  *
12133  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12134  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12135  * This is handled differently than in recovery mode since no Tx/Rx resources
12136  * are being allocated.
12137  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)12138 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12139 {
12140 	int err;
12141 
12142 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12143 		err = i40e_setup_misc_vector(pf);
12144 
12145 		if (err) {
12146 			dev_info(&pf->pdev->dev,
12147 				 "MSI-X misc vector request failed, error %d\n",
12148 				 err);
12149 			return err;
12150 		}
12151 	} else {
12152 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12153 
12154 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12155 				  pf->int_name, pf);
12156 
12157 		if (err) {
12158 			dev_info(&pf->pdev->dev,
12159 				 "MSI/legacy misc vector request failed, error %d\n",
12160 				 err);
12161 			return err;
12162 		}
12163 		i40e_enable_misc_int_causes(pf);
12164 		i40e_irq_dynamic_enable_icr0(pf);
12165 	}
12166 
12167 	return 0;
12168 }
12169 
12170 /**
12171  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12172  * @pf: board private structure
12173  *
12174  * This sets up the handler for MSIX 0, which is used to manage the
12175  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12176  * when in MSI or Legacy interrupt mode.
12177  **/
i40e_setup_misc_vector(struct i40e_pf * pf)12178 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12179 {
12180 	struct i40e_hw *hw = &pf->hw;
12181 	int err = 0;
12182 
12183 	/* Only request the IRQ once, the first time through. */
12184 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12185 		err = request_irq(pf->msix_entries[0].vector,
12186 				  i40e_intr, 0, pf->int_name, pf);
12187 		if (err) {
12188 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12189 			dev_info(&pf->pdev->dev,
12190 				 "request_irq for %s failed: %d\n",
12191 				 pf->int_name, err);
12192 			return -EFAULT;
12193 		}
12194 	}
12195 
12196 	i40e_enable_misc_int_causes(pf);
12197 
12198 	/* associate no queues to the misc vector */
12199 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12200 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12201 
12202 	i40e_flush(hw);
12203 
12204 	i40e_irq_dynamic_enable_icr0(pf);
12205 
12206 	return err;
12207 }
12208 
12209 /**
12210  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12211  * @vsi: Pointer to vsi structure
12212  * @seed: Buffter to store the hash keys
12213  * @lut: Buffer to store the lookup table entries
12214  * @lut_size: Size of buffer to store the lookup table entries
12215  *
12216  * Return 0 on success, negative on failure
12217  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)12218 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12219 			   u8 *lut, u16 lut_size)
12220 {
12221 	struct i40e_pf *pf = vsi->back;
12222 	struct i40e_hw *hw = &pf->hw;
12223 	int ret = 0;
12224 
12225 	if (seed) {
12226 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12227 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12228 		if (ret) {
12229 			dev_info(&pf->pdev->dev,
12230 				 "Cannot get RSS key, err %pe aq_err %s\n",
12231 				 ERR_PTR(ret),
12232 				 i40e_aq_str(&pf->hw,
12233 					     pf->hw.aq.asq_last_status));
12234 			return ret;
12235 		}
12236 	}
12237 
12238 	if (lut) {
12239 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12240 
12241 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12242 		if (ret) {
12243 			dev_info(&pf->pdev->dev,
12244 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12245 				 ERR_PTR(ret),
12246 				 i40e_aq_str(&pf->hw,
12247 					     pf->hw.aq.asq_last_status));
12248 			return ret;
12249 		}
12250 	}
12251 
12252 	return ret;
12253 }
12254 
12255 /**
12256  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12257  * @vsi: Pointer to vsi structure
12258  * @seed: RSS hash seed
12259  * @lut: Lookup table
12260  * @lut_size: Lookup table size
12261  *
12262  * Returns 0 on success, negative on failure
12263  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)12264 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12265 			       const u8 *lut, u16 lut_size)
12266 {
12267 	struct i40e_pf *pf = vsi->back;
12268 	struct i40e_hw *hw = &pf->hw;
12269 	u16 vf_id = vsi->vf_id;
12270 	u8 i;
12271 
12272 	/* Fill out hash function seed */
12273 	if (seed) {
12274 		u32 *seed_dw = (u32 *)seed;
12275 
12276 		if (vsi->type == I40E_VSI_MAIN) {
12277 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12278 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12279 		} else if (vsi->type == I40E_VSI_SRIOV) {
12280 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12281 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12282 		} else {
12283 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12284 		}
12285 	}
12286 
12287 	if (lut) {
12288 		u32 *lut_dw = (u32 *)lut;
12289 
12290 		if (vsi->type == I40E_VSI_MAIN) {
12291 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12292 				return -EINVAL;
12293 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12294 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12295 		} else if (vsi->type == I40E_VSI_SRIOV) {
12296 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12297 				return -EINVAL;
12298 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12299 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12300 		} else {
12301 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12302 		}
12303 	}
12304 	i40e_flush(hw);
12305 
12306 	return 0;
12307 }
12308 
12309 /**
12310  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12311  * @vsi: Pointer to VSI structure
12312  * @seed: Buffer to store the keys
12313  * @lut: Buffer to store the lookup table entries
12314  * @lut_size: Size of buffer to store the lookup table entries
12315  *
12316  * Returns 0 on success, negative on failure
12317  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12318 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12319 			    u8 *lut, u16 lut_size)
12320 {
12321 	struct i40e_pf *pf = vsi->back;
12322 	struct i40e_hw *hw = &pf->hw;
12323 	u16 i;
12324 
12325 	if (seed) {
12326 		u32 *seed_dw = (u32 *)seed;
12327 
12328 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12329 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12330 	}
12331 	if (lut) {
12332 		u32 *lut_dw = (u32 *)lut;
12333 
12334 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12335 			return -EINVAL;
12336 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12337 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12338 	}
12339 
12340 	return 0;
12341 }
12342 
12343 /**
12344  * i40e_config_rss - Configure RSS keys and lut
12345  * @vsi: Pointer to VSI structure
12346  * @seed: RSS hash seed
12347  * @lut: Lookup table
12348  * @lut_size: Lookup table size
12349  *
12350  * Returns 0 on success, negative on failure
12351  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12352 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12353 {
12354 	struct i40e_pf *pf = vsi->back;
12355 
12356 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12357 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12358 	else
12359 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12360 }
12361 
12362 /**
12363  * i40e_get_rss - Get RSS keys and lut
12364  * @vsi: Pointer to VSI structure
12365  * @seed: Buffer to store the keys
12366  * @lut: Buffer to store the lookup table entries
12367  * @lut_size: Size of buffer to store the lookup table entries
12368  *
12369  * Returns 0 on success, negative on failure
12370  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)12371 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12372 {
12373 	struct i40e_pf *pf = vsi->back;
12374 
12375 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12376 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12377 	else
12378 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12379 }
12380 
12381 /**
12382  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12383  * @pf: Pointer to board private structure
12384  * @lut: Lookup table
12385  * @rss_table_size: Lookup table size
12386  * @rss_size: Range of queue number for hashing
12387  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)12388 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12389 		       u16 rss_table_size, u16 rss_size)
12390 {
12391 	u16 i;
12392 
12393 	for (i = 0; i < rss_table_size; i++)
12394 		lut[i] = i % rss_size;
12395 }
12396 
12397 /**
12398  * i40e_pf_config_rss - Prepare for RSS if used
12399  * @pf: board private structure
12400  **/
i40e_pf_config_rss(struct i40e_pf * pf)12401 static int i40e_pf_config_rss(struct i40e_pf *pf)
12402 {
12403 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12404 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12405 	u8 *lut;
12406 	struct i40e_hw *hw = &pf->hw;
12407 	u32 reg_val;
12408 	u64 hena;
12409 	int ret;
12410 
12411 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12412 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12413 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12414 	hena |= i40e_pf_get_default_rss_hena(pf);
12415 
12416 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12417 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12418 
12419 	/* Determine the RSS table size based on the hardware capabilities */
12420 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12421 	reg_val = (pf->rss_table_size == 512) ?
12422 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12423 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12424 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12425 
12426 	/* Determine the RSS size of the VSI */
12427 	if (!vsi->rss_size) {
12428 		u16 qcount;
12429 		/* If the firmware does something weird during VSI init, we
12430 		 * could end up with zero TCs. Check for that to avoid
12431 		 * divide-by-zero. It probably won't pass traffic, but it also
12432 		 * won't panic.
12433 		 */
12434 		qcount = vsi->num_queue_pairs /
12435 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12436 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12437 	}
12438 	if (!vsi->rss_size)
12439 		return -EINVAL;
12440 
12441 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12442 	if (!lut)
12443 		return -ENOMEM;
12444 
12445 	/* Use user configured lut if there is one, otherwise use default */
12446 	if (vsi->rss_lut_user)
12447 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12448 	else
12449 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12450 
12451 	/* Use user configured hash key if there is one, otherwise
12452 	 * use default.
12453 	 */
12454 	if (vsi->rss_hkey_user)
12455 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12456 	else
12457 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12458 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12459 	kfree(lut);
12460 
12461 	return ret;
12462 }
12463 
12464 /**
12465  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12466  * @pf: board private structure
12467  * @queue_count: the requested queue count for rss.
12468  *
12469  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12470  * count which may be different from the requested queue count.
12471  * Note: expects to be called while under rtnl_lock()
12472  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)12473 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12474 {
12475 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12476 	int new_rss_size;
12477 
12478 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12479 		return 0;
12480 
12481 	queue_count = min_t(int, queue_count, num_online_cpus());
12482 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12483 
12484 	if (queue_count != vsi->num_queue_pairs) {
12485 		u16 qcount;
12486 
12487 		vsi->req_queue_pairs = queue_count;
12488 		i40e_prep_for_reset(pf);
12489 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12490 			return pf->alloc_rss_size;
12491 
12492 		pf->alloc_rss_size = new_rss_size;
12493 
12494 		i40e_reset_and_rebuild(pf, true, true);
12495 
12496 		/* Discard the user configured hash keys and lut, if less
12497 		 * queues are enabled.
12498 		 */
12499 		if (queue_count < vsi->rss_size) {
12500 			i40e_clear_rss_config_user(vsi);
12501 			dev_dbg(&pf->pdev->dev,
12502 				"discard user configured hash keys and lut\n");
12503 		}
12504 
12505 		/* Reset vsi->rss_size, as number of enabled queues changed */
12506 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12507 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12508 
12509 		i40e_pf_config_rss(pf);
12510 	}
12511 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12512 		 vsi->req_queue_pairs, pf->rss_size_max);
12513 	return pf->alloc_rss_size;
12514 }
12515 
12516 /**
12517  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12518  * @pf: board private structure
12519  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)12520 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12521 {
12522 	bool min_valid, max_valid;
12523 	u32 max_bw, min_bw;
12524 	int status;
12525 
12526 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12527 					   &min_valid, &max_valid);
12528 
12529 	if (!status) {
12530 		if (min_valid)
12531 			pf->min_bw = min_bw;
12532 		if (max_valid)
12533 			pf->max_bw = max_bw;
12534 	}
12535 
12536 	return status;
12537 }
12538 
12539 /**
12540  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12541  * @pf: board private structure
12542  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)12543 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12544 {
12545 	struct i40e_aqc_configure_partition_bw_data bw_data;
12546 	int status;
12547 
12548 	memset(&bw_data, 0, sizeof(bw_data));
12549 
12550 	/* Set the valid bit for this PF */
12551 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12552 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12553 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12554 
12555 	/* Set the new bandwidths */
12556 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12557 
12558 	return status;
12559 }
12560 
12561 /**
12562  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12563  * @pf: board private structure
12564  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)12565 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12566 {
12567 	/* Commit temporary BW setting to permanent NVM image */
12568 	enum i40e_admin_queue_err last_aq_status;
12569 	u16 nvm_word;
12570 	int ret;
12571 
12572 	if (pf->hw.partition_id != 1) {
12573 		dev_info(&pf->pdev->dev,
12574 			 "Commit BW only works on partition 1! This is partition %d",
12575 			 pf->hw.partition_id);
12576 		ret = I40E_NOT_SUPPORTED;
12577 		goto bw_commit_out;
12578 	}
12579 
12580 	/* Acquire NVM for read access */
12581 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12582 	last_aq_status = pf->hw.aq.asq_last_status;
12583 	if (ret) {
12584 		dev_info(&pf->pdev->dev,
12585 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12586 			 ERR_PTR(ret),
12587 			 i40e_aq_str(&pf->hw, last_aq_status));
12588 		goto bw_commit_out;
12589 	}
12590 
12591 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12592 	ret = i40e_aq_read_nvm(&pf->hw,
12593 			       I40E_SR_NVM_CONTROL_WORD,
12594 			       0x10, sizeof(nvm_word), &nvm_word,
12595 			       false, NULL);
12596 	/* Save off last admin queue command status before releasing
12597 	 * the NVM
12598 	 */
12599 	last_aq_status = pf->hw.aq.asq_last_status;
12600 	i40e_release_nvm(&pf->hw);
12601 	if (ret) {
12602 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12603 			 ERR_PTR(ret),
12604 			 i40e_aq_str(&pf->hw, last_aq_status));
12605 		goto bw_commit_out;
12606 	}
12607 
12608 	/* Wait a bit for NVM release to complete */
12609 	msleep(50);
12610 
12611 	/* Acquire NVM for write access */
12612 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12613 	last_aq_status = pf->hw.aq.asq_last_status;
12614 	if (ret) {
12615 		dev_info(&pf->pdev->dev,
12616 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12617 			 ERR_PTR(ret),
12618 			 i40e_aq_str(&pf->hw, last_aq_status));
12619 		goto bw_commit_out;
12620 	}
12621 	/* Write it back out unchanged to initiate update NVM,
12622 	 * which will force a write of the shadow (alt) RAM to
12623 	 * the NVM - thus storing the bandwidth values permanently.
12624 	 */
12625 	ret = i40e_aq_update_nvm(&pf->hw,
12626 				 I40E_SR_NVM_CONTROL_WORD,
12627 				 0x10, sizeof(nvm_word),
12628 				 &nvm_word, true, 0, NULL);
12629 	/* Save off last admin queue command status before releasing
12630 	 * the NVM
12631 	 */
12632 	last_aq_status = pf->hw.aq.asq_last_status;
12633 	i40e_release_nvm(&pf->hw);
12634 	if (ret)
12635 		dev_info(&pf->pdev->dev,
12636 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12637 			 ERR_PTR(ret),
12638 			 i40e_aq_str(&pf->hw, last_aq_status));
12639 bw_commit_out:
12640 
12641 	return ret;
12642 }
12643 
12644 /**
12645  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12646  * if total port shutdown feature is enabled for this PF
12647  * @pf: board private structure
12648  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)12649 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12650 {
12651 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12652 #define I40E_FEATURES_ENABLE_PTR		0x2A
12653 #define I40E_CURRENT_SETTING_PTR		0x2B
12654 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12655 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12656 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12657 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12658 	int read_status = I40E_SUCCESS;
12659 	u16 sr_emp_sr_settings_ptr = 0;
12660 	u16 features_enable = 0;
12661 	u16 link_behavior = 0;
12662 	bool ret = false;
12663 
12664 	read_status = i40e_read_nvm_word(&pf->hw,
12665 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12666 					 &sr_emp_sr_settings_ptr);
12667 	if (read_status)
12668 		goto err_nvm;
12669 	read_status = i40e_read_nvm_word(&pf->hw,
12670 					 sr_emp_sr_settings_ptr +
12671 					 I40E_FEATURES_ENABLE_PTR,
12672 					 &features_enable);
12673 	if (read_status)
12674 		goto err_nvm;
12675 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12676 		read_status = i40e_read_nvm_module_data(&pf->hw,
12677 							I40E_SR_EMP_SR_SETTINGS_PTR,
12678 							I40E_CURRENT_SETTING_PTR,
12679 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12680 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12681 							&link_behavior);
12682 		if (read_status)
12683 			goto err_nvm;
12684 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12685 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12686 	}
12687 	return ret;
12688 
12689 err_nvm:
12690 	dev_warn(&pf->pdev->dev,
12691 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12692 		 ERR_PTR(read_status));
12693 	return ret;
12694 }
12695 
12696 /**
12697  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12698  * @pf: board private structure to initialize
12699  *
12700  * i40e_sw_init initializes the Adapter private data structure.
12701  * Fields are initialized based on PCI device information and
12702  * OS network device settings (MTU size).
12703  **/
i40e_sw_init(struct i40e_pf * pf)12704 static int i40e_sw_init(struct i40e_pf *pf)
12705 {
12706 	int err = 0;
12707 	int size;
12708 	u16 pow;
12709 
12710 	/* Set default capability flags */
12711 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12712 		    I40E_FLAG_MSI_ENABLED     |
12713 		    I40E_FLAG_MSIX_ENABLED;
12714 
12715 	/* Set default ITR */
12716 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12717 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12718 
12719 	/* Depending on PF configurations, it is possible that the RSS
12720 	 * maximum might end up larger than the available queues
12721 	 */
12722 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12723 	pf->alloc_rss_size = 1;
12724 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12725 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12726 				 pf->hw.func_caps.num_tx_qp);
12727 
12728 	/* find the next higher power-of-2 of num cpus */
12729 	pow = roundup_pow_of_two(num_online_cpus());
12730 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12731 
12732 	if (pf->hw.func_caps.rss) {
12733 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12734 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12735 					   num_online_cpus());
12736 	}
12737 
12738 	/* MFP mode enabled */
12739 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12740 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12741 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12742 		if (i40e_get_partition_bw_setting(pf)) {
12743 			dev_warn(&pf->pdev->dev,
12744 				 "Could not get partition bw settings\n");
12745 		} else {
12746 			dev_info(&pf->pdev->dev,
12747 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12748 				 pf->min_bw, pf->max_bw);
12749 
12750 			/* nudge the Tx scheduler */
12751 			i40e_set_partition_bw_setting(pf);
12752 		}
12753 	}
12754 
12755 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12756 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12757 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12758 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12759 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12760 		    pf->hw.num_partitions > 1)
12761 			dev_info(&pf->pdev->dev,
12762 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12763 		else
12764 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12765 		pf->fdir_pf_filter_count =
12766 				 pf->hw.func_caps.fd_filters_guaranteed;
12767 		pf->hw.fdir_shared_filter_count =
12768 				 pf->hw.func_caps.fd_filters_best_effort;
12769 	}
12770 
12771 	if (pf->hw.mac.type == I40E_MAC_X722) {
12772 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12773 				    I40E_HW_128_QP_RSS_CAPABLE |
12774 				    I40E_HW_ATR_EVICT_CAPABLE |
12775 				    I40E_HW_WB_ON_ITR_CAPABLE |
12776 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12777 				    I40E_HW_NO_PCI_LINK_CHECK |
12778 				    I40E_HW_USE_SET_LLDP_MIB |
12779 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12780 				    I40E_HW_PTP_L4_CAPABLE |
12781 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12782 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12783 
12784 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12785 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12786 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12787 			dev_warn(&pf->pdev->dev,
12788 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12789 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12790 		}
12791 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12792 		   ((pf->hw.aq.api_maj_ver == 1) &&
12793 		    (pf->hw.aq.api_min_ver > 4))) {
12794 		/* Supported in FW API version higher than 1.4 */
12795 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12796 	}
12797 
12798 	/* Enable HW ATR eviction if possible */
12799 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12800 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12801 
12802 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12803 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12804 	    (pf->hw.aq.fw_maj_ver < 4))) {
12805 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12806 		/* No DCB support  for FW < v4.33 */
12807 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12808 	}
12809 
12810 	/* Disable FW LLDP if FW < v4.3 */
12811 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12812 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12813 	    (pf->hw.aq.fw_maj_ver < 4)))
12814 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12815 
12816 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12817 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12818 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12819 	    (pf->hw.aq.fw_maj_ver >= 5)))
12820 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12821 
12822 	/* Enable PTP L4 if FW > v6.0 */
12823 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12824 	    pf->hw.aq.fw_maj_ver >= 6)
12825 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12826 
12827 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12828 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12829 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12830 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12831 	}
12832 
12833 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12834 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12835 		/* IWARP needs one extra vector for CQP just like MISC.*/
12836 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12837 	}
12838 	/* Stopping FW LLDP engine is supported on XL710 and X722
12839 	 * starting from FW versions determined in i40e_init_adminq.
12840 	 * Stopping the FW LLDP engine is not supported on XL710
12841 	 * if NPAR is functioning so unset this hw flag in this case.
12842 	 */
12843 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12844 	    pf->hw.func_caps.npar_enable &&
12845 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12846 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12847 
12848 #ifdef CONFIG_PCI_IOV
12849 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12850 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12851 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12852 		pf->num_req_vfs = min_t(int,
12853 					pf->hw.func_caps.num_vfs,
12854 					I40E_MAX_VF_COUNT);
12855 	}
12856 #endif /* CONFIG_PCI_IOV */
12857 	pf->eeprom_version = 0xDEAD;
12858 	pf->lan_veb = I40E_NO_VEB;
12859 	pf->lan_vsi = I40E_NO_VSI;
12860 
12861 	/* By default FW has this off for performance reasons */
12862 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12863 
12864 	/* set up queue assignment tracking */
12865 	size = sizeof(struct i40e_lump_tracking)
12866 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12867 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12868 	if (!pf->qp_pile) {
12869 		err = -ENOMEM;
12870 		goto sw_init_done;
12871 	}
12872 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12873 
12874 	pf->tx_timeout_recovery_level = 1;
12875 
12876 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12877 	    i40e_is_total_port_shutdown_enabled(pf)) {
12878 		/* Link down on close must be on when total port shutdown
12879 		 * is enabled for a given port
12880 		 */
12881 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12882 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12883 		dev_info(&pf->pdev->dev,
12884 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12885 	}
12886 	mutex_init(&pf->switch_mutex);
12887 
12888 sw_init_done:
12889 	return err;
12890 }
12891 
12892 /**
12893  * i40e_set_ntuple - set the ntuple feature flag and take action
12894  * @pf: board private structure to initialize
12895  * @features: the feature set that the stack is suggesting
12896  *
12897  * returns a bool to indicate if reset needs to happen
12898  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12899 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12900 {
12901 	bool need_reset = false;
12902 
12903 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12904 	 * the state changed, we need to reset.
12905 	 */
12906 	if (features & NETIF_F_NTUPLE) {
12907 		/* Enable filters and mark for reset */
12908 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12909 			need_reset = true;
12910 		/* enable FD_SB only if there is MSI-X vector and no cloud
12911 		 * filters exist
12912 		 */
12913 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12914 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12915 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12916 		}
12917 	} else {
12918 		/* turn off filters, mark for reset and clear SW filter list */
12919 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12920 			need_reset = true;
12921 			i40e_fdir_filter_exit(pf);
12922 		}
12923 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12924 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12925 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12926 
12927 		/* reset fd counters */
12928 		pf->fd_add_err = 0;
12929 		pf->fd_atr_cnt = 0;
12930 		/* if ATR was auto disabled it can be re-enabled. */
12931 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12932 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12933 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12934 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12935 	}
12936 	return need_reset;
12937 }
12938 
12939 /**
12940  * i40e_clear_rss_lut - clear the rx hash lookup table
12941  * @vsi: the VSI being configured
12942  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12943 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12944 {
12945 	struct i40e_pf *pf = vsi->back;
12946 	struct i40e_hw *hw = &pf->hw;
12947 	u16 vf_id = vsi->vf_id;
12948 	u8 i;
12949 
12950 	if (vsi->type == I40E_VSI_MAIN) {
12951 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12952 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12953 	} else if (vsi->type == I40E_VSI_SRIOV) {
12954 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12955 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12956 	} else {
12957 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12958 	}
12959 }
12960 
12961 /**
12962  * i40e_set_features - set the netdev feature flags
12963  * @netdev: ptr to the netdev being adjusted
12964  * @features: the feature set that the stack is suggesting
12965  * Note: expects to be called while under rtnl_lock()
12966  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12967 static int i40e_set_features(struct net_device *netdev,
12968 			     netdev_features_t features)
12969 {
12970 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12971 	struct i40e_vsi *vsi = np->vsi;
12972 	struct i40e_pf *pf = vsi->back;
12973 	bool need_reset;
12974 
12975 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12976 		i40e_pf_config_rss(pf);
12977 	else if (!(features & NETIF_F_RXHASH) &&
12978 		 netdev->features & NETIF_F_RXHASH)
12979 		i40e_clear_rss_lut(vsi);
12980 
12981 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12982 		i40e_vlan_stripping_enable(vsi);
12983 	else
12984 		i40e_vlan_stripping_disable(vsi);
12985 
12986 	if (!(features & NETIF_F_HW_TC) &&
12987 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12988 		dev_err(&pf->pdev->dev,
12989 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12990 		return -EINVAL;
12991 	}
12992 
12993 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12994 		i40e_del_all_macvlans(vsi);
12995 
12996 	need_reset = i40e_set_ntuple(pf, features);
12997 
12998 	if (need_reset)
12999 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13000 
13001 	return 0;
13002 }
13003 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13004 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13005 				    unsigned int table, unsigned int idx,
13006 				    struct udp_tunnel_info *ti)
13007 {
13008 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13009 	struct i40e_hw *hw = &np->vsi->back->hw;
13010 	u8 type, filter_index;
13011 	int ret;
13012 
13013 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13014 						   I40E_AQC_TUNNEL_TYPE_NGE;
13015 
13016 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13017 				     NULL);
13018 	if (ret) {
13019 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13020 			    ERR_PTR(ret),
13021 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13022 		return -EIO;
13023 	}
13024 
13025 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13026 	return 0;
13027 }
13028 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)13029 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13030 				      unsigned int table, unsigned int idx,
13031 				      struct udp_tunnel_info *ti)
13032 {
13033 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13034 	struct i40e_hw *hw = &np->vsi->back->hw;
13035 	int ret;
13036 
13037 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13038 	if (ret) {
13039 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13040 			    ERR_PTR(ret),
13041 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13042 		return -EIO;
13043 	}
13044 
13045 	return 0;
13046 }
13047 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)13048 static int i40e_get_phys_port_id(struct net_device *netdev,
13049 				 struct netdev_phys_item_id *ppid)
13050 {
13051 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13052 	struct i40e_pf *pf = np->vsi->back;
13053 	struct i40e_hw *hw = &pf->hw;
13054 
13055 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13056 		return -EOPNOTSUPP;
13057 
13058 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13059 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13060 
13061 	return 0;
13062 }
13063 
13064 /**
13065  * i40e_ndo_fdb_add - add an entry to the hardware database
13066  * @ndm: the input from the stack
13067  * @tb: pointer to array of nladdr (unused)
13068  * @dev: the net device pointer
13069  * @addr: the MAC address entry being added
13070  * @vid: VLAN ID
13071  * @flags: instructions from stack about fdb operation
13072  * @extack: netlink extended ack, unused currently
13073  */
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)13074 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13075 			    struct net_device *dev,
13076 			    const unsigned char *addr, u16 vid,
13077 			    u16 flags,
13078 			    struct netlink_ext_ack *extack)
13079 {
13080 	struct i40e_netdev_priv *np = netdev_priv(dev);
13081 	struct i40e_pf *pf = np->vsi->back;
13082 	int err = 0;
13083 
13084 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13085 		return -EOPNOTSUPP;
13086 
13087 	if (vid) {
13088 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13089 		return -EINVAL;
13090 	}
13091 
13092 	/* Hardware does not support aging addresses so if a
13093 	 * ndm_state is given only allow permanent addresses
13094 	 */
13095 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13096 		netdev_info(dev, "FDB only supports static addresses\n");
13097 		return -EINVAL;
13098 	}
13099 
13100 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13101 		err = dev_uc_add_excl(dev, addr);
13102 	else if (is_multicast_ether_addr(addr))
13103 		err = dev_mc_add_excl(dev, addr);
13104 	else
13105 		err = -EINVAL;
13106 
13107 	/* Only return duplicate errors if NLM_F_EXCL is set */
13108 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13109 		err = 0;
13110 
13111 	return err;
13112 }
13113 
13114 /**
13115  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13116  * @dev: the netdev being configured
13117  * @nlh: RTNL message
13118  * @flags: bridge flags
13119  * @extack: netlink extended ack
13120  *
13121  * Inserts a new hardware bridge if not already created and
13122  * enables the bridging mode requested (VEB or VEPA). If the
13123  * hardware bridge has already been inserted and the request
13124  * is to change the mode then that requires a PF reset to
13125  * allow rebuild of the components with required hardware
13126  * bridge mode enabled.
13127  *
13128  * Note: expects to be called while under rtnl_lock()
13129  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13130 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13131 				   struct nlmsghdr *nlh,
13132 				   u16 flags,
13133 				   struct netlink_ext_ack *extack)
13134 {
13135 	struct i40e_netdev_priv *np = netdev_priv(dev);
13136 	struct i40e_vsi *vsi = np->vsi;
13137 	struct i40e_pf *pf = vsi->back;
13138 	struct i40e_veb *veb = NULL;
13139 	struct nlattr *attr, *br_spec;
13140 	int i, rem;
13141 
13142 	/* Only for PF VSI for now */
13143 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13144 		return -EOPNOTSUPP;
13145 
13146 	/* Find the HW bridge for PF VSI */
13147 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13148 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13149 			veb = pf->veb[i];
13150 	}
13151 
13152 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13153 	if (!br_spec)
13154 		return -EINVAL;
13155 
13156 	nla_for_each_nested(attr, br_spec, rem) {
13157 		__u16 mode;
13158 
13159 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13160 			continue;
13161 
13162 		mode = nla_get_u16(attr);
13163 		if ((mode != BRIDGE_MODE_VEPA) &&
13164 		    (mode != BRIDGE_MODE_VEB))
13165 			return -EINVAL;
13166 
13167 		/* Insert a new HW bridge */
13168 		if (!veb) {
13169 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13170 					     vsi->tc_config.enabled_tc);
13171 			if (veb) {
13172 				veb->bridge_mode = mode;
13173 				i40e_config_bridge_mode(veb);
13174 			} else {
13175 				/* No Bridge HW offload available */
13176 				return -ENOENT;
13177 			}
13178 			break;
13179 		} else if (mode != veb->bridge_mode) {
13180 			/* Existing HW bridge but different mode needs reset */
13181 			veb->bridge_mode = mode;
13182 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13183 			if (mode == BRIDGE_MODE_VEB)
13184 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13185 			else
13186 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13187 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13188 			break;
13189 		}
13190 	}
13191 
13192 	return 0;
13193 }
13194 
13195 /**
13196  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13197  * @skb: skb buff
13198  * @pid: process id
13199  * @seq: RTNL message seq #
13200  * @dev: the netdev being configured
13201  * @filter_mask: unused
13202  * @nlflags: netlink flags passed in
13203  *
13204  * Return the mode in which the hardware bridge is operating in
13205  * i.e VEB or VEPA.
13206  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)13207 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13208 				   struct net_device *dev,
13209 				   u32 __always_unused filter_mask,
13210 				   int nlflags)
13211 {
13212 	struct i40e_netdev_priv *np = netdev_priv(dev);
13213 	struct i40e_vsi *vsi = np->vsi;
13214 	struct i40e_pf *pf = vsi->back;
13215 	struct i40e_veb *veb = NULL;
13216 	int i;
13217 
13218 	/* Only for PF VSI for now */
13219 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13220 		return -EOPNOTSUPP;
13221 
13222 	/* Find the HW bridge for the PF VSI */
13223 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13224 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13225 			veb = pf->veb[i];
13226 	}
13227 
13228 	if (!veb)
13229 		return 0;
13230 
13231 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13232 				       0, 0, nlflags, filter_mask, NULL);
13233 }
13234 
13235 /**
13236  * i40e_features_check - Validate encapsulated packet conforms to limits
13237  * @skb: skb buff
13238  * @dev: This physical port's netdev
13239  * @features: Offload features that the stack believes apply
13240  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13241 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13242 					     struct net_device *dev,
13243 					     netdev_features_t features)
13244 {
13245 	size_t len;
13246 
13247 	/* No point in doing any of this if neither checksum nor GSO are
13248 	 * being requested for this frame.  We can rule out both by just
13249 	 * checking for CHECKSUM_PARTIAL
13250 	 */
13251 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13252 		return features;
13253 
13254 	/* We cannot support GSO if the MSS is going to be less than
13255 	 * 64 bytes.  If it is then we need to drop support for GSO.
13256 	 */
13257 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13258 		features &= ~NETIF_F_GSO_MASK;
13259 
13260 	/* MACLEN can support at most 63 words */
13261 	len = skb_network_header(skb) - skb->data;
13262 	if (len & ~(63 * 2))
13263 		goto out_err;
13264 
13265 	/* IPLEN and EIPLEN can support at most 127 dwords */
13266 	len = skb_transport_header(skb) - skb_network_header(skb);
13267 	if (len & ~(127 * 4))
13268 		goto out_err;
13269 
13270 	if (skb->encapsulation) {
13271 		/* L4TUNLEN can support 127 words */
13272 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13273 		if (len & ~(127 * 2))
13274 			goto out_err;
13275 
13276 		/* IPLEN can support at most 127 dwords */
13277 		len = skb_inner_transport_header(skb) -
13278 		      skb_inner_network_header(skb);
13279 		if (len & ~(127 * 4))
13280 			goto out_err;
13281 	}
13282 
13283 	/* No need to validate L4LEN as TCP is the only protocol with a
13284 	 * flexible value and we support all possible values supported
13285 	 * by TCP, which is at most 15 dwords
13286 	 */
13287 
13288 	return features;
13289 out_err:
13290 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13291 }
13292 
13293 /**
13294  * i40e_xdp_setup - add/remove an XDP program
13295  * @vsi: VSI to changed
13296  * @prog: XDP program
13297  * @extack: netlink extended ack
13298  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)13299 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13300 			  struct netlink_ext_ack *extack)
13301 {
13302 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13303 	struct i40e_pf *pf = vsi->back;
13304 	struct bpf_prog *old_prog;
13305 	bool need_reset;
13306 	int i;
13307 
13308 	/* Don't allow frames that span over multiple buffers */
13309 	if (frame_size > i40e_calculate_vsi_rx_buf_len(vsi)) {
13310 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13311 		return -EINVAL;
13312 	}
13313 
13314 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13315 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13316 
13317 	if (need_reset)
13318 		i40e_prep_for_reset(pf);
13319 
13320 	/* VSI shall be deleted in a moment, just return EINVAL */
13321 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13322 		return -EINVAL;
13323 
13324 	old_prog = xchg(&vsi->xdp_prog, prog);
13325 
13326 	if (need_reset) {
13327 		if (!prog)
13328 			/* Wait until ndo_xsk_wakeup completes. */
13329 			synchronize_rcu();
13330 		i40e_reset_and_rebuild(pf, true, true);
13331 	}
13332 
13333 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13334 		if (i40e_realloc_rx_bi_zc(vsi, true))
13335 			return -ENOMEM;
13336 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13337 		if (i40e_realloc_rx_bi_zc(vsi, false))
13338 			return -ENOMEM;
13339 	}
13340 
13341 	for (i = 0; i < vsi->num_queue_pairs; i++)
13342 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13343 
13344 	if (old_prog)
13345 		bpf_prog_put(old_prog);
13346 
13347 	/* Kick start the NAPI context if there is an AF_XDP socket open
13348 	 * on that queue id. This so that receiving will start.
13349 	 */
13350 	if (need_reset && prog)
13351 		for (i = 0; i < vsi->num_queue_pairs; i++)
13352 			if (vsi->xdp_rings[i]->xsk_pool)
13353 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13354 						      XDP_WAKEUP_RX);
13355 
13356 	return 0;
13357 }
13358 
13359 /**
13360  * i40e_enter_busy_conf - Enters busy config state
13361  * @vsi: vsi
13362  *
13363  * Returns 0 on success, <0 for failure.
13364  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)13365 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13366 {
13367 	struct i40e_pf *pf = vsi->back;
13368 	int timeout = 50;
13369 
13370 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13371 		timeout--;
13372 		if (!timeout)
13373 			return -EBUSY;
13374 		usleep_range(1000, 2000);
13375 	}
13376 
13377 	return 0;
13378 }
13379 
13380 /**
13381  * i40e_exit_busy_conf - Exits busy config state
13382  * @vsi: vsi
13383  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)13384 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13385 {
13386 	struct i40e_pf *pf = vsi->back;
13387 
13388 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13389 }
13390 
13391 /**
13392  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13393  * @vsi: vsi
13394  * @queue_pair: queue pair
13395  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)13396 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13397 {
13398 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13399 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13400 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13401 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13402 	if (i40e_enabled_xdp_vsi(vsi)) {
13403 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13404 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13405 	}
13406 }
13407 
13408 /**
13409  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13410  * @vsi: vsi
13411  * @queue_pair: queue pair
13412  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)13413 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13414 {
13415 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13416 	if (i40e_enabled_xdp_vsi(vsi)) {
13417 		/* Make sure that in-progress ndo_xdp_xmit calls are
13418 		 * completed.
13419 		 */
13420 		synchronize_rcu();
13421 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13422 	}
13423 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13424 }
13425 
13426 /**
13427  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13428  * @vsi: vsi
13429  * @queue_pair: queue pair
13430  * @enable: true for enable, false for disable
13431  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)13432 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13433 					bool enable)
13434 {
13435 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13436 	struct i40e_q_vector *q_vector = rxr->q_vector;
13437 
13438 	if (!vsi->netdev)
13439 		return;
13440 
13441 	/* All rings in a qp belong to the same qvector. */
13442 	if (q_vector->rx.ring || q_vector->tx.ring) {
13443 		if (enable)
13444 			napi_enable(&q_vector->napi);
13445 		else
13446 			napi_disable(&q_vector->napi);
13447 	}
13448 }
13449 
13450 /**
13451  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13452  * @vsi: vsi
13453  * @queue_pair: queue pair
13454  * @enable: true for enable, false for disable
13455  *
13456  * Returns 0 on success, <0 on failure.
13457  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)13458 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13459 					bool enable)
13460 {
13461 	struct i40e_pf *pf = vsi->back;
13462 	int pf_q, ret = 0;
13463 
13464 	pf_q = vsi->base_queue + queue_pair;
13465 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13466 				     false /*is xdp*/, enable);
13467 	if (ret) {
13468 		dev_info(&pf->pdev->dev,
13469 			 "VSI seid %d Tx ring %d %sable timeout\n",
13470 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13471 		return ret;
13472 	}
13473 
13474 	i40e_control_rx_q(pf, pf_q, enable);
13475 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13476 	if (ret) {
13477 		dev_info(&pf->pdev->dev,
13478 			 "VSI seid %d Rx ring %d %sable timeout\n",
13479 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13480 		return ret;
13481 	}
13482 
13483 	/* Due to HW errata, on Rx disable only, the register can
13484 	 * indicate done before it really is. Needs 50ms to be sure
13485 	 */
13486 	if (!enable)
13487 		mdelay(50);
13488 
13489 	if (!i40e_enabled_xdp_vsi(vsi))
13490 		return ret;
13491 
13492 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13493 				     pf_q + vsi->alloc_queue_pairs,
13494 				     true /*is xdp*/, enable);
13495 	if (ret) {
13496 		dev_info(&pf->pdev->dev,
13497 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13498 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13499 	}
13500 
13501 	return ret;
13502 }
13503 
13504 /**
13505  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13506  * @vsi: vsi
13507  * @queue_pair: queue_pair
13508  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)13509 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13510 {
13511 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13512 	struct i40e_pf *pf = vsi->back;
13513 	struct i40e_hw *hw = &pf->hw;
13514 
13515 	/* All rings in a qp belong to the same qvector. */
13516 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13517 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13518 	else
13519 		i40e_irq_dynamic_enable_icr0(pf);
13520 
13521 	i40e_flush(hw);
13522 }
13523 
13524 /**
13525  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13526  * @vsi: vsi
13527  * @queue_pair: queue_pair
13528  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)13529 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13530 {
13531 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13532 	struct i40e_pf *pf = vsi->back;
13533 	struct i40e_hw *hw = &pf->hw;
13534 
13535 	/* For simplicity, instead of removing the qp interrupt causes
13536 	 * from the interrupt linked list, we simply disable the interrupt, and
13537 	 * leave the list intact.
13538 	 *
13539 	 * All rings in a qp belong to the same qvector.
13540 	 */
13541 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13542 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13543 
13544 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13545 		i40e_flush(hw);
13546 		synchronize_irq(pf->msix_entries[intpf].vector);
13547 	} else {
13548 		/* Legacy and MSI mode - this stops all interrupt handling */
13549 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13550 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13551 		i40e_flush(hw);
13552 		synchronize_irq(pf->pdev->irq);
13553 	}
13554 }
13555 
13556 /**
13557  * i40e_queue_pair_disable - Disables a queue pair
13558  * @vsi: vsi
13559  * @queue_pair: queue pair
13560  *
13561  * Returns 0 on success, <0 on failure.
13562  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)13563 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13564 {
13565 	int err;
13566 
13567 	err = i40e_enter_busy_conf(vsi);
13568 	if (err)
13569 		return err;
13570 
13571 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13572 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13573 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13574 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13575 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13576 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13577 
13578 	return err;
13579 }
13580 
13581 /**
13582  * i40e_queue_pair_enable - Enables a queue pair
13583  * @vsi: vsi
13584  * @queue_pair: queue pair
13585  *
13586  * Returns 0 on success, <0 on failure.
13587  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)13588 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13589 {
13590 	int err;
13591 
13592 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13593 	if (err)
13594 		return err;
13595 
13596 	if (i40e_enabled_xdp_vsi(vsi)) {
13597 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13598 		if (err)
13599 			return err;
13600 	}
13601 
13602 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13603 	if (err)
13604 		return err;
13605 
13606 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13607 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13608 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13609 
13610 	i40e_exit_busy_conf(vsi);
13611 
13612 	return err;
13613 }
13614 
13615 /**
13616  * i40e_xdp - implements ndo_bpf for i40e
13617  * @dev: netdevice
13618  * @xdp: XDP command
13619  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)13620 static int i40e_xdp(struct net_device *dev,
13621 		    struct netdev_bpf *xdp)
13622 {
13623 	struct i40e_netdev_priv *np = netdev_priv(dev);
13624 	struct i40e_vsi *vsi = np->vsi;
13625 
13626 	if (vsi->type != I40E_VSI_MAIN)
13627 		return -EINVAL;
13628 
13629 	switch (xdp->command) {
13630 	case XDP_SETUP_PROG:
13631 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13632 	case XDP_SETUP_XSK_POOL:
13633 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13634 					   xdp->xsk.queue_id);
13635 	default:
13636 		return -EINVAL;
13637 	}
13638 }
13639 
13640 static const struct net_device_ops i40e_netdev_ops = {
13641 	.ndo_open		= i40e_open,
13642 	.ndo_stop		= i40e_close,
13643 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13644 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13645 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13646 	.ndo_validate_addr	= eth_validate_addr,
13647 	.ndo_set_mac_address	= i40e_set_mac,
13648 	.ndo_change_mtu		= i40e_change_mtu,
13649 	.ndo_eth_ioctl		= i40e_ioctl,
13650 	.ndo_tx_timeout		= i40e_tx_timeout,
13651 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13652 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13653 #ifdef CONFIG_NET_POLL_CONTROLLER
13654 	.ndo_poll_controller	= i40e_netpoll,
13655 #endif
13656 	.ndo_setup_tc		= __i40e_setup_tc,
13657 	.ndo_select_queue	= i40e_lan_select_queue,
13658 	.ndo_set_features	= i40e_set_features,
13659 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13660 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13661 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13662 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13663 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13664 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13665 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13666 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13667 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13668 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13669 	.ndo_features_check	= i40e_features_check,
13670 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13671 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13672 	.ndo_bpf		= i40e_xdp,
13673 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13674 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13675 	.ndo_dfwd_add_station	= i40e_fwd_add,
13676 	.ndo_dfwd_del_station	= i40e_fwd_del,
13677 };
13678 
13679 /**
13680  * i40e_config_netdev - Setup the netdev flags
13681  * @vsi: the VSI being configured
13682  *
13683  * Returns 0 on success, negative value on failure
13684  **/
i40e_config_netdev(struct i40e_vsi * vsi)13685 static int i40e_config_netdev(struct i40e_vsi *vsi)
13686 {
13687 	struct i40e_pf *pf = vsi->back;
13688 	struct i40e_hw *hw = &pf->hw;
13689 	struct i40e_netdev_priv *np;
13690 	struct net_device *netdev;
13691 	u8 broadcast[ETH_ALEN];
13692 	u8 mac_addr[ETH_ALEN];
13693 	int etherdev_size;
13694 	netdev_features_t hw_enc_features;
13695 	netdev_features_t hw_features;
13696 
13697 	etherdev_size = sizeof(struct i40e_netdev_priv);
13698 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13699 	if (!netdev)
13700 		return -ENOMEM;
13701 
13702 	vsi->netdev = netdev;
13703 	np = netdev_priv(netdev);
13704 	np->vsi = vsi;
13705 
13706 	hw_enc_features = NETIF_F_SG			|
13707 			  NETIF_F_HW_CSUM		|
13708 			  NETIF_F_HIGHDMA		|
13709 			  NETIF_F_SOFT_FEATURES		|
13710 			  NETIF_F_TSO			|
13711 			  NETIF_F_TSO_ECN		|
13712 			  NETIF_F_TSO6			|
13713 			  NETIF_F_GSO_GRE		|
13714 			  NETIF_F_GSO_GRE_CSUM		|
13715 			  NETIF_F_GSO_PARTIAL		|
13716 			  NETIF_F_GSO_IPXIP4		|
13717 			  NETIF_F_GSO_IPXIP6		|
13718 			  NETIF_F_GSO_UDP_TUNNEL	|
13719 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13720 			  NETIF_F_GSO_UDP_L4		|
13721 			  NETIF_F_SCTP_CRC		|
13722 			  NETIF_F_RXHASH		|
13723 			  NETIF_F_RXCSUM		|
13724 			  0;
13725 
13726 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13727 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13728 
13729 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13730 
13731 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13732 
13733 	netdev->hw_enc_features |= hw_enc_features;
13734 
13735 	/* record features VLANs can make use of */
13736 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13737 
13738 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13739 				   NETIF_F_GSO_GRE_CSUM |	\
13740 				   NETIF_F_GSO_IPXIP4 |		\
13741 				   NETIF_F_GSO_IPXIP6 |		\
13742 				   NETIF_F_GSO_UDP_TUNNEL |	\
13743 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13744 
13745 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13746 	netdev->features |= NETIF_F_GSO_PARTIAL |
13747 			    I40E_GSO_PARTIAL_FEATURES;
13748 
13749 	netdev->mpls_features |= NETIF_F_SG;
13750 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13751 	netdev->mpls_features |= NETIF_F_TSO;
13752 	netdev->mpls_features |= NETIF_F_TSO6;
13753 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13754 
13755 	/* enable macvlan offloads */
13756 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13757 
13758 	hw_features = hw_enc_features		|
13759 		      NETIF_F_HW_VLAN_CTAG_TX	|
13760 		      NETIF_F_HW_VLAN_CTAG_RX;
13761 
13762 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13763 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13764 
13765 	netdev->hw_features |= hw_features;
13766 
13767 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13768 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13769 
13770 	netdev->features &= ~NETIF_F_HW_TC;
13771 
13772 	if (vsi->type == I40E_VSI_MAIN) {
13773 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13774 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13775 		/* The following steps are necessary for two reasons. First,
13776 		 * some older NVM configurations load a default MAC-VLAN
13777 		 * filter that will accept any tagged packet, and we want to
13778 		 * replace this with a normal filter. Additionally, it is
13779 		 * possible our MAC address was provided by the platform using
13780 		 * Open Firmware or similar.
13781 		 *
13782 		 * Thus, we need to remove the default filter and install one
13783 		 * specific to the MAC address.
13784 		 */
13785 		i40e_rm_default_mac_filter(vsi, mac_addr);
13786 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13787 		i40e_add_mac_filter(vsi, mac_addr);
13788 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13789 	} else {
13790 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13791 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13792 		 * the end, which is 4 bytes long, so force truncation of the
13793 		 * original name by IFNAMSIZ - 4
13794 		 */
13795 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13796 			 IFNAMSIZ - 4,
13797 			 pf->vsi[pf->lan_vsi]->netdev->name);
13798 		eth_random_addr(mac_addr);
13799 
13800 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13801 		i40e_add_mac_filter(vsi, mac_addr);
13802 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13803 	}
13804 
13805 	/* Add the broadcast filter so that we initially will receive
13806 	 * broadcast packets. Note that when a new VLAN is first added the
13807 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13808 	 * specific filters as part of transitioning into "vlan" operation.
13809 	 * When more VLANs are added, the driver will copy each existing MAC
13810 	 * filter and add it for the new VLAN.
13811 	 *
13812 	 * Broadcast filters are handled specially by
13813 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13814 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13815 	 * filter. The subtask will update the correct broadcast promiscuous
13816 	 * bits as VLANs become active or inactive.
13817 	 */
13818 	eth_broadcast_addr(broadcast);
13819 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13820 	i40e_add_mac_filter(vsi, broadcast);
13821 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13822 
13823 	eth_hw_addr_set(netdev, mac_addr);
13824 	ether_addr_copy(netdev->perm_addr, mac_addr);
13825 
13826 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13827 	netdev->neigh_priv_len = sizeof(u32) * 4;
13828 
13829 	netdev->priv_flags |= IFF_UNICAST_FLT;
13830 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13831 	/* Setup netdev TC information */
13832 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13833 
13834 	netdev->netdev_ops = &i40e_netdev_ops;
13835 	netdev->watchdog_timeo = 5 * HZ;
13836 	i40e_set_ethtool_ops(netdev);
13837 
13838 	/* MTU range: 68 - 9706 */
13839 	netdev->min_mtu = ETH_MIN_MTU;
13840 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13841 
13842 	return 0;
13843 }
13844 
13845 /**
13846  * i40e_vsi_delete - Delete a VSI from the switch
13847  * @vsi: the VSI being removed
13848  *
13849  * Returns 0 on success, negative value on failure
13850  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13851 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13852 {
13853 	/* remove default VSI is not allowed */
13854 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13855 		return;
13856 
13857 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13858 }
13859 
13860 /**
13861  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13862  * @vsi: the VSI being queried
13863  *
13864  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13865  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13866 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13867 {
13868 	struct i40e_veb *veb;
13869 	struct i40e_pf *pf = vsi->back;
13870 
13871 	/* Uplink is not a bridge so default to VEB */
13872 	if (vsi->veb_idx >= I40E_MAX_VEB)
13873 		return 1;
13874 
13875 	veb = pf->veb[vsi->veb_idx];
13876 	if (!veb) {
13877 		dev_info(&pf->pdev->dev,
13878 			 "There is no veb associated with the bridge\n");
13879 		return -ENOENT;
13880 	}
13881 
13882 	/* Uplink is a bridge in VEPA mode */
13883 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13884 		return 0;
13885 	} else {
13886 		/* Uplink is a bridge in VEB mode */
13887 		return 1;
13888 	}
13889 
13890 	/* VEPA is now default bridge, so return 0 */
13891 	return 0;
13892 }
13893 
13894 /**
13895  * i40e_add_vsi - Add a VSI to the switch
13896  * @vsi: the VSI being configured
13897  *
13898  * This initializes a VSI context depending on the VSI type to be added and
13899  * passes it down to the add_vsi aq command.
13900  **/
i40e_add_vsi(struct i40e_vsi * vsi)13901 static int i40e_add_vsi(struct i40e_vsi *vsi)
13902 {
13903 	int ret = -ENODEV;
13904 	struct i40e_pf *pf = vsi->back;
13905 	struct i40e_hw *hw = &pf->hw;
13906 	struct i40e_vsi_context ctxt;
13907 	struct i40e_mac_filter *f;
13908 	struct hlist_node *h;
13909 	int bkt;
13910 
13911 	u8 enabled_tc = 0x1; /* TC0 enabled */
13912 	int f_count = 0;
13913 
13914 	memset(&ctxt, 0, sizeof(ctxt));
13915 	switch (vsi->type) {
13916 	case I40E_VSI_MAIN:
13917 		/* The PF's main VSI is already setup as part of the
13918 		 * device initialization, so we'll not bother with
13919 		 * the add_vsi call, but we will retrieve the current
13920 		 * VSI context.
13921 		 */
13922 		ctxt.seid = pf->main_vsi_seid;
13923 		ctxt.pf_num = pf->hw.pf_id;
13924 		ctxt.vf_num = 0;
13925 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13926 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13927 		if (ret) {
13928 			dev_info(&pf->pdev->dev,
13929 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13930 				 ERR_PTR(ret),
13931 				 i40e_aq_str(&pf->hw,
13932 					     pf->hw.aq.asq_last_status));
13933 			return -ENOENT;
13934 		}
13935 		vsi->info = ctxt.info;
13936 		vsi->info.valid_sections = 0;
13937 
13938 		vsi->seid = ctxt.seid;
13939 		vsi->id = ctxt.vsi_number;
13940 
13941 		enabled_tc = i40e_pf_get_tc_map(pf);
13942 
13943 		/* Source pruning is enabled by default, so the flag is
13944 		 * negative logic - if it's set, we need to fiddle with
13945 		 * the VSI to disable source pruning.
13946 		 */
13947 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13948 			memset(&ctxt, 0, sizeof(ctxt));
13949 			ctxt.seid = pf->main_vsi_seid;
13950 			ctxt.pf_num = pf->hw.pf_id;
13951 			ctxt.vf_num = 0;
13952 			ctxt.info.valid_sections |=
13953 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13954 			ctxt.info.switch_id =
13955 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13956 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13957 			if (ret) {
13958 				dev_info(&pf->pdev->dev,
13959 					 "update vsi failed, err %d aq_err %s\n",
13960 					 ret,
13961 					 i40e_aq_str(&pf->hw,
13962 						     pf->hw.aq.asq_last_status));
13963 				ret = -ENOENT;
13964 				goto err;
13965 			}
13966 		}
13967 
13968 		/* MFP mode setup queue map and update VSI */
13969 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13970 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13971 			memset(&ctxt, 0, sizeof(ctxt));
13972 			ctxt.seid = pf->main_vsi_seid;
13973 			ctxt.pf_num = pf->hw.pf_id;
13974 			ctxt.vf_num = 0;
13975 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13976 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13977 			if (ret) {
13978 				dev_info(&pf->pdev->dev,
13979 					 "update vsi failed, err %pe aq_err %s\n",
13980 					 ERR_PTR(ret),
13981 					 i40e_aq_str(&pf->hw,
13982 						    pf->hw.aq.asq_last_status));
13983 				ret = -ENOENT;
13984 				goto err;
13985 			}
13986 			/* update the local VSI info queue map */
13987 			i40e_vsi_update_queue_map(vsi, &ctxt);
13988 			vsi->info.valid_sections = 0;
13989 		} else {
13990 			/* Default/Main VSI is only enabled for TC0
13991 			 * reconfigure it to enable all TCs that are
13992 			 * available on the port in SFP mode.
13993 			 * For MFP case the iSCSI PF would use this
13994 			 * flow to enable LAN+iSCSI TC.
13995 			 */
13996 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13997 			if (ret) {
13998 				/* Single TC condition is not fatal,
13999 				 * message and continue
14000 				 */
14001 				dev_info(&pf->pdev->dev,
14002 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14003 					 enabled_tc,
14004 					 ERR_PTR(ret),
14005 					 i40e_aq_str(&pf->hw,
14006 						    pf->hw.aq.asq_last_status));
14007 			}
14008 		}
14009 		break;
14010 
14011 	case I40E_VSI_FDIR:
14012 		ctxt.pf_num = hw->pf_id;
14013 		ctxt.vf_num = 0;
14014 		ctxt.uplink_seid = vsi->uplink_seid;
14015 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14016 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14017 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14018 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14019 			ctxt.info.valid_sections |=
14020 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14021 			ctxt.info.switch_id =
14022 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14023 		}
14024 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14025 		break;
14026 
14027 	case I40E_VSI_VMDQ2:
14028 		ctxt.pf_num = hw->pf_id;
14029 		ctxt.vf_num = 0;
14030 		ctxt.uplink_seid = vsi->uplink_seid;
14031 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14032 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14033 
14034 		/* This VSI is connected to VEB so the switch_id
14035 		 * should be set to zero by default.
14036 		 */
14037 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14038 			ctxt.info.valid_sections |=
14039 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14040 			ctxt.info.switch_id =
14041 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14042 		}
14043 
14044 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14045 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14046 		break;
14047 
14048 	case I40E_VSI_SRIOV:
14049 		ctxt.pf_num = hw->pf_id;
14050 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14051 		ctxt.uplink_seid = vsi->uplink_seid;
14052 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14053 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14054 
14055 		/* This VSI is connected to VEB so the switch_id
14056 		 * should be set to zero by default.
14057 		 */
14058 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14059 			ctxt.info.valid_sections |=
14060 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14061 			ctxt.info.switch_id =
14062 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14063 		}
14064 
14065 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14066 			ctxt.info.valid_sections |=
14067 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14068 			ctxt.info.queueing_opt_flags |=
14069 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14070 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14071 		}
14072 
14073 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14074 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14075 		if (pf->vf[vsi->vf_id].spoofchk) {
14076 			ctxt.info.valid_sections |=
14077 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14078 			ctxt.info.sec_flags |=
14079 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14080 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14081 		}
14082 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14083 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14084 		break;
14085 
14086 	case I40E_VSI_IWARP:
14087 		/* send down message to iWARP */
14088 		break;
14089 
14090 	default:
14091 		return -ENODEV;
14092 	}
14093 
14094 	if (vsi->type != I40E_VSI_MAIN) {
14095 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14096 		if (ret) {
14097 			dev_info(&vsi->back->pdev->dev,
14098 				 "add vsi failed, err %pe aq_err %s\n",
14099 				 ERR_PTR(ret),
14100 				 i40e_aq_str(&pf->hw,
14101 					     pf->hw.aq.asq_last_status));
14102 			ret = -ENOENT;
14103 			goto err;
14104 		}
14105 		vsi->info = ctxt.info;
14106 		vsi->info.valid_sections = 0;
14107 		vsi->seid = ctxt.seid;
14108 		vsi->id = ctxt.vsi_number;
14109 	}
14110 
14111 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14112 	vsi->active_filters = 0;
14113 	/* If macvlan filters already exist, force them to get loaded */
14114 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14115 		f->state = I40E_FILTER_NEW;
14116 		f_count++;
14117 	}
14118 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14119 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14120 
14121 	if (f_count) {
14122 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14123 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14124 	}
14125 
14126 	/* Update VSI BW information */
14127 	ret = i40e_vsi_get_bw_info(vsi);
14128 	if (ret) {
14129 		dev_info(&pf->pdev->dev,
14130 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14131 			 ERR_PTR(ret),
14132 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14133 		/* VSI is already added so not tearing that up */
14134 		ret = 0;
14135 	}
14136 
14137 err:
14138 	return ret;
14139 }
14140 
14141 /**
14142  * i40e_vsi_release - Delete a VSI and free its resources
14143  * @vsi: the VSI being removed
14144  *
14145  * Returns 0 on success or < 0 on error
14146  **/
i40e_vsi_release(struct i40e_vsi * vsi)14147 int i40e_vsi_release(struct i40e_vsi *vsi)
14148 {
14149 	struct i40e_mac_filter *f;
14150 	struct hlist_node *h;
14151 	struct i40e_veb *veb = NULL;
14152 	struct i40e_pf *pf;
14153 	u16 uplink_seid;
14154 	int i, n, bkt;
14155 
14156 	pf = vsi->back;
14157 
14158 	/* release of a VEB-owner or last VSI is not allowed */
14159 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14160 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14161 			 vsi->seid, vsi->uplink_seid);
14162 		return -ENODEV;
14163 	}
14164 	if (vsi == pf->vsi[pf->lan_vsi] &&
14165 	    !test_bit(__I40E_DOWN, pf->state)) {
14166 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14167 		return -ENODEV;
14168 	}
14169 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14170 	uplink_seid = vsi->uplink_seid;
14171 	if (vsi->type != I40E_VSI_SRIOV) {
14172 		if (vsi->netdev_registered) {
14173 			vsi->netdev_registered = false;
14174 			if (vsi->netdev) {
14175 				/* results in a call to i40e_close() */
14176 				unregister_netdev(vsi->netdev);
14177 			}
14178 		} else {
14179 			i40e_vsi_close(vsi);
14180 		}
14181 		i40e_vsi_disable_irq(vsi);
14182 	}
14183 
14184 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14185 
14186 	/* clear the sync flag on all filters */
14187 	if (vsi->netdev) {
14188 		__dev_uc_unsync(vsi->netdev, NULL);
14189 		__dev_mc_unsync(vsi->netdev, NULL);
14190 	}
14191 
14192 	/* make sure any remaining filters are marked for deletion */
14193 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14194 		__i40e_del_filter(vsi, f);
14195 
14196 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14197 
14198 	i40e_sync_vsi_filters(vsi);
14199 
14200 	i40e_vsi_delete(vsi);
14201 	i40e_vsi_free_q_vectors(vsi);
14202 	if (vsi->netdev) {
14203 		free_netdev(vsi->netdev);
14204 		vsi->netdev = NULL;
14205 	}
14206 	i40e_vsi_clear_rings(vsi);
14207 	i40e_vsi_clear(vsi);
14208 
14209 	/* If this was the last thing on the VEB, except for the
14210 	 * controlling VSI, remove the VEB, which puts the controlling
14211 	 * VSI onto the next level down in the switch.
14212 	 *
14213 	 * Well, okay, there's one more exception here: don't remove
14214 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14215 	 * from up the network stack.
14216 	 */
14217 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14218 		if (pf->vsi[i] &&
14219 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14220 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14221 			n++;      /* count the VSIs */
14222 		}
14223 	}
14224 	for (i = 0; i < I40E_MAX_VEB; i++) {
14225 		if (!pf->veb[i])
14226 			continue;
14227 		if (pf->veb[i]->uplink_seid == uplink_seid)
14228 			n++;     /* count the VEBs */
14229 		if (pf->veb[i]->seid == uplink_seid)
14230 			veb = pf->veb[i];
14231 	}
14232 	if (n == 0 && veb && veb->uplink_seid != 0)
14233 		i40e_veb_release(veb);
14234 
14235 	return 0;
14236 }
14237 
14238 /**
14239  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14240  * @vsi: ptr to the VSI
14241  *
14242  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14243  * corresponding SW VSI structure and initializes num_queue_pairs for the
14244  * newly allocated VSI.
14245  *
14246  * Returns 0 on success or negative on failure
14247  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)14248 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14249 {
14250 	int ret = -ENOENT;
14251 	struct i40e_pf *pf = vsi->back;
14252 
14253 	if (vsi->q_vectors[0]) {
14254 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14255 			 vsi->seid);
14256 		return -EEXIST;
14257 	}
14258 
14259 	if (vsi->base_vector) {
14260 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14261 			 vsi->seid, vsi->base_vector);
14262 		return -EEXIST;
14263 	}
14264 
14265 	ret = i40e_vsi_alloc_q_vectors(vsi);
14266 	if (ret) {
14267 		dev_info(&pf->pdev->dev,
14268 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14269 			 vsi->num_q_vectors, vsi->seid, ret);
14270 		vsi->num_q_vectors = 0;
14271 		goto vector_setup_out;
14272 	}
14273 
14274 	/* In Legacy mode, we do not have to get any other vector since we
14275 	 * piggyback on the misc/ICR0 for queue interrupts.
14276 	*/
14277 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14278 		return ret;
14279 	if (vsi->num_q_vectors)
14280 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14281 						 vsi->num_q_vectors, vsi->idx);
14282 	if (vsi->base_vector < 0) {
14283 		dev_info(&pf->pdev->dev,
14284 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14285 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14286 		i40e_vsi_free_q_vectors(vsi);
14287 		ret = -ENOENT;
14288 		goto vector_setup_out;
14289 	}
14290 
14291 vector_setup_out:
14292 	return ret;
14293 }
14294 
14295 /**
14296  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14297  * @vsi: pointer to the vsi.
14298  *
14299  * This re-allocates a vsi's queue resources.
14300  *
14301  * Returns pointer to the successfully allocated and configured VSI sw struct
14302  * on success, otherwise returns NULL on failure.
14303  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)14304 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14305 {
14306 	u16 alloc_queue_pairs;
14307 	struct i40e_pf *pf;
14308 	u8 enabled_tc;
14309 	int ret;
14310 
14311 	if (!vsi)
14312 		return NULL;
14313 
14314 	pf = vsi->back;
14315 
14316 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14317 	i40e_vsi_clear_rings(vsi);
14318 
14319 	i40e_vsi_free_arrays(vsi, false);
14320 	i40e_set_num_rings_in_vsi(vsi);
14321 	ret = i40e_vsi_alloc_arrays(vsi, false);
14322 	if (ret)
14323 		goto err_vsi;
14324 
14325 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14326 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14327 
14328 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14329 	if (ret < 0) {
14330 		dev_info(&pf->pdev->dev,
14331 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14332 			 alloc_queue_pairs, vsi->seid, ret);
14333 		goto err_vsi;
14334 	}
14335 	vsi->base_queue = ret;
14336 
14337 	/* Update the FW view of the VSI. Force a reset of TC and queue
14338 	 * layout configurations.
14339 	 */
14340 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14341 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14342 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14343 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14344 	if (vsi->type == I40E_VSI_MAIN)
14345 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14346 
14347 	/* assign it some queues */
14348 	ret = i40e_alloc_rings(vsi);
14349 	if (ret)
14350 		goto err_rings;
14351 
14352 	/* map all of the rings to the q_vectors */
14353 	i40e_vsi_map_rings_to_vectors(vsi);
14354 	return vsi;
14355 
14356 err_rings:
14357 	i40e_vsi_free_q_vectors(vsi);
14358 	if (vsi->netdev_registered) {
14359 		vsi->netdev_registered = false;
14360 		unregister_netdev(vsi->netdev);
14361 		free_netdev(vsi->netdev);
14362 		vsi->netdev = NULL;
14363 	}
14364 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14365 err_vsi:
14366 	i40e_vsi_clear(vsi);
14367 	return NULL;
14368 }
14369 
14370 /**
14371  * i40e_vsi_setup - Set up a VSI by a given type
14372  * @pf: board private structure
14373  * @type: VSI type
14374  * @uplink_seid: the switch element to link to
14375  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14376  *
14377  * This allocates the sw VSI structure and its queue resources, then add a VSI
14378  * to the identified VEB.
14379  *
14380  * Returns pointer to the successfully allocated and configure VSI sw struct on
14381  * success, otherwise returns NULL on failure.
14382  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)14383 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14384 				u16 uplink_seid, u32 param1)
14385 {
14386 	struct i40e_vsi *vsi = NULL;
14387 	struct i40e_veb *veb = NULL;
14388 	u16 alloc_queue_pairs;
14389 	int ret, i;
14390 	int v_idx;
14391 
14392 	/* The requested uplink_seid must be either
14393 	 *     - the PF's port seid
14394 	 *              no VEB is needed because this is the PF
14395 	 *              or this is a Flow Director special case VSI
14396 	 *     - seid of an existing VEB
14397 	 *     - seid of a VSI that owns an existing VEB
14398 	 *     - seid of a VSI that doesn't own a VEB
14399 	 *              a new VEB is created and the VSI becomes the owner
14400 	 *     - seid of the PF VSI, which is what creates the first VEB
14401 	 *              this is a special case of the previous
14402 	 *
14403 	 * Find which uplink_seid we were given and create a new VEB if needed
14404 	 */
14405 	for (i = 0; i < I40E_MAX_VEB; i++) {
14406 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14407 			veb = pf->veb[i];
14408 			break;
14409 		}
14410 	}
14411 
14412 	if (!veb && uplink_seid != pf->mac_seid) {
14413 
14414 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14415 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14416 				vsi = pf->vsi[i];
14417 				break;
14418 			}
14419 		}
14420 		if (!vsi) {
14421 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14422 				 uplink_seid);
14423 			return NULL;
14424 		}
14425 
14426 		if (vsi->uplink_seid == pf->mac_seid)
14427 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14428 					     vsi->tc_config.enabled_tc);
14429 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14430 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14431 					     vsi->tc_config.enabled_tc);
14432 		if (veb) {
14433 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14434 				dev_info(&vsi->back->pdev->dev,
14435 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14436 				return NULL;
14437 			}
14438 			/* We come up by default in VEPA mode if SRIOV is not
14439 			 * already enabled, in which case we can't force VEPA
14440 			 * mode.
14441 			 */
14442 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14443 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14444 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14445 			}
14446 			i40e_config_bridge_mode(veb);
14447 		}
14448 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14449 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14450 				veb = pf->veb[i];
14451 		}
14452 		if (!veb) {
14453 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14454 			return NULL;
14455 		}
14456 
14457 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14458 		uplink_seid = veb->seid;
14459 	}
14460 
14461 	/* get vsi sw struct */
14462 	v_idx = i40e_vsi_mem_alloc(pf, type);
14463 	if (v_idx < 0)
14464 		goto err_alloc;
14465 	vsi = pf->vsi[v_idx];
14466 	if (!vsi)
14467 		goto err_alloc;
14468 	vsi->type = type;
14469 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14470 
14471 	if (type == I40E_VSI_MAIN)
14472 		pf->lan_vsi = v_idx;
14473 	else if (type == I40E_VSI_SRIOV)
14474 		vsi->vf_id = param1;
14475 	/* assign it some queues */
14476 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14477 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14478 
14479 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14480 	if (ret < 0) {
14481 		dev_info(&pf->pdev->dev,
14482 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14483 			 alloc_queue_pairs, vsi->seid, ret);
14484 		goto err_vsi;
14485 	}
14486 	vsi->base_queue = ret;
14487 
14488 	/* get a VSI from the hardware */
14489 	vsi->uplink_seid = uplink_seid;
14490 	ret = i40e_add_vsi(vsi);
14491 	if (ret)
14492 		goto err_vsi;
14493 
14494 	switch (vsi->type) {
14495 	/* setup the netdev if needed */
14496 	case I40E_VSI_MAIN:
14497 	case I40E_VSI_VMDQ2:
14498 		ret = i40e_config_netdev(vsi);
14499 		if (ret)
14500 			goto err_netdev;
14501 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14502 		if (ret)
14503 			goto err_netdev;
14504 		ret = register_netdev(vsi->netdev);
14505 		if (ret)
14506 			goto err_netdev;
14507 		vsi->netdev_registered = true;
14508 		netif_carrier_off(vsi->netdev);
14509 #ifdef CONFIG_I40E_DCB
14510 		/* Setup DCB netlink interface */
14511 		i40e_dcbnl_setup(vsi);
14512 #endif /* CONFIG_I40E_DCB */
14513 		fallthrough;
14514 	case I40E_VSI_FDIR:
14515 		/* set up vectors and rings if needed */
14516 		ret = i40e_vsi_setup_vectors(vsi);
14517 		if (ret)
14518 			goto err_msix;
14519 
14520 		ret = i40e_alloc_rings(vsi);
14521 		if (ret)
14522 			goto err_rings;
14523 
14524 		/* map all of the rings to the q_vectors */
14525 		i40e_vsi_map_rings_to_vectors(vsi);
14526 
14527 		i40e_vsi_reset_stats(vsi);
14528 		break;
14529 	default:
14530 		/* no netdev or rings for the other VSI types */
14531 		break;
14532 	}
14533 
14534 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14535 	    (vsi->type == I40E_VSI_VMDQ2)) {
14536 		ret = i40e_vsi_config_rss(vsi);
14537 	}
14538 	return vsi;
14539 
14540 err_rings:
14541 	i40e_vsi_free_q_vectors(vsi);
14542 err_msix:
14543 	if (vsi->netdev_registered) {
14544 		vsi->netdev_registered = false;
14545 		unregister_netdev(vsi->netdev);
14546 		free_netdev(vsi->netdev);
14547 		vsi->netdev = NULL;
14548 	}
14549 err_netdev:
14550 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14551 err_vsi:
14552 	i40e_vsi_clear(vsi);
14553 err_alloc:
14554 	return NULL;
14555 }
14556 
14557 /**
14558  * i40e_veb_get_bw_info - Query VEB BW information
14559  * @veb: the veb to query
14560  *
14561  * Query the Tx scheduler BW configuration data for given VEB
14562  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)14563 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14564 {
14565 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14566 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14567 	struct i40e_pf *pf = veb->pf;
14568 	struct i40e_hw *hw = &pf->hw;
14569 	u32 tc_bw_max;
14570 	int ret = 0;
14571 	int i;
14572 
14573 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14574 						  &bw_data, NULL);
14575 	if (ret) {
14576 		dev_info(&pf->pdev->dev,
14577 			 "query veb bw config failed, err %pe aq_err %s\n",
14578 			 ERR_PTR(ret),
14579 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14580 		goto out;
14581 	}
14582 
14583 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14584 						   &ets_data, NULL);
14585 	if (ret) {
14586 		dev_info(&pf->pdev->dev,
14587 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14588 			 ERR_PTR(ret),
14589 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14590 		goto out;
14591 	}
14592 
14593 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14594 	veb->bw_max_quanta = ets_data.tc_bw_max;
14595 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14596 	veb->enabled_tc = ets_data.tc_valid_bits;
14597 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14598 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14599 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14600 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14601 		veb->bw_tc_limit_credits[i] =
14602 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14603 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14604 	}
14605 
14606 out:
14607 	return ret;
14608 }
14609 
14610 /**
14611  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14612  * @pf: board private structure
14613  *
14614  * On error: returns error code (negative)
14615  * On success: returns vsi index in PF (positive)
14616  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)14617 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14618 {
14619 	int ret = -ENOENT;
14620 	struct i40e_veb *veb;
14621 	int i;
14622 
14623 	/* Need to protect the allocation of switch elements at the PF level */
14624 	mutex_lock(&pf->switch_mutex);
14625 
14626 	/* VEB list may be fragmented if VEB creation/destruction has
14627 	 * been happening.  We can afford to do a quick scan to look
14628 	 * for any free slots in the list.
14629 	 *
14630 	 * find next empty veb slot, looping back around if necessary
14631 	 */
14632 	i = 0;
14633 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14634 		i++;
14635 	if (i >= I40E_MAX_VEB) {
14636 		ret = -ENOMEM;
14637 		goto err_alloc_veb;  /* out of VEB slots! */
14638 	}
14639 
14640 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14641 	if (!veb) {
14642 		ret = -ENOMEM;
14643 		goto err_alloc_veb;
14644 	}
14645 	veb->pf = pf;
14646 	veb->idx = i;
14647 	veb->enabled_tc = 1;
14648 
14649 	pf->veb[i] = veb;
14650 	ret = i;
14651 err_alloc_veb:
14652 	mutex_unlock(&pf->switch_mutex);
14653 	return ret;
14654 }
14655 
14656 /**
14657  * i40e_switch_branch_release - Delete a branch of the switch tree
14658  * @branch: where to start deleting
14659  *
14660  * This uses recursion to find the tips of the branch to be
14661  * removed, deleting until we get back to and can delete this VEB.
14662  **/
i40e_switch_branch_release(struct i40e_veb * branch)14663 static void i40e_switch_branch_release(struct i40e_veb *branch)
14664 {
14665 	struct i40e_pf *pf = branch->pf;
14666 	u16 branch_seid = branch->seid;
14667 	u16 veb_idx = branch->idx;
14668 	int i;
14669 
14670 	/* release any VEBs on this VEB - RECURSION */
14671 	for (i = 0; i < I40E_MAX_VEB; i++) {
14672 		if (!pf->veb[i])
14673 			continue;
14674 		if (pf->veb[i]->uplink_seid == branch->seid)
14675 			i40e_switch_branch_release(pf->veb[i]);
14676 	}
14677 
14678 	/* Release the VSIs on this VEB, but not the owner VSI.
14679 	 *
14680 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14681 	 *       the VEB itself, so don't use (*branch) after this loop.
14682 	 */
14683 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14684 		if (!pf->vsi[i])
14685 			continue;
14686 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14687 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14688 			i40e_vsi_release(pf->vsi[i]);
14689 		}
14690 	}
14691 
14692 	/* There's one corner case where the VEB might not have been
14693 	 * removed, so double check it here and remove it if needed.
14694 	 * This case happens if the veb was created from the debugfs
14695 	 * commands and no VSIs were added to it.
14696 	 */
14697 	if (pf->veb[veb_idx])
14698 		i40e_veb_release(pf->veb[veb_idx]);
14699 }
14700 
14701 /**
14702  * i40e_veb_clear - remove veb struct
14703  * @veb: the veb to remove
14704  **/
i40e_veb_clear(struct i40e_veb * veb)14705 static void i40e_veb_clear(struct i40e_veb *veb)
14706 {
14707 	if (!veb)
14708 		return;
14709 
14710 	if (veb->pf) {
14711 		struct i40e_pf *pf = veb->pf;
14712 
14713 		mutex_lock(&pf->switch_mutex);
14714 		if (pf->veb[veb->idx] == veb)
14715 			pf->veb[veb->idx] = NULL;
14716 		mutex_unlock(&pf->switch_mutex);
14717 	}
14718 
14719 	kfree(veb);
14720 }
14721 
14722 /**
14723  * i40e_veb_release - Delete a VEB and free its resources
14724  * @veb: the VEB being removed
14725  **/
i40e_veb_release(struct i40e_veb * veb)14726 void i40e_veb_release(struct i40e_veb *veb)
14727 {
14728 	struct i40e_vsi *vsi = NULL;
14729 	struct i40e_pf *pf;
14730 	int i, n = 0;
14731 
14732 	pf = veb->pf;
14733 
14734 	/* find the remaining VSI and check for extras */
14735 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14736 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14737 			n++;
14738 			vsi = pf->vsi[i];
14739 		}
14740 	}
14741 	if (n != 1) {
14742 		dev_info(&pf->pdev->dev,
14743 			 "can't remove VEB %d with %d VSIs left\n",
14744 			 veb->seid, n);
14745 		return;
14746 	}
14747 
14748 	/* move the remaining VSI to uplink veb */
14749 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14750 	if (veb->uplink_seid) {
14751 		vsi->uplink_seid = veb->uplink_seid;
14752 		if (veb->uplink_seid == pf->mac_seid)
14753 			vsi->veb_idx = I40E_NO_VEB;
14754 		else
14755 			vsi->veb_idx = veb->veb_idx;
14756 	} else {
14757 		/* floating VEB */
14758 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14759 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14760 	}
14761 
14762 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14763 	i40e_veb_clear(veb);
14764 }
14765 
14766 /**
14767  * i40e_add_veb - create the VEB in the switch
14768  * @veb: the VEB to be instantiated
14769  * @vsi: the controlling VSI
14770  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)14771 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14772 {
14773 	struct i40e_pf *pf = veb->pf;
14774 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14775 	int ret;
14776 
14777 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14778 			      veb->enabled_tc, false,
14779 			      &veb->seid, enable_stats, NULL);
14780 
14781 	/* get a VEB from the hardware */
14782 	if (ret) {
14783 		dev_info(&pf->pdev->dev,
14784 			 "couldn't add VEB, err %pe aq_err %s\n",
14785 			 ERR_PTR(ret),
14786 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14787 		return -EPERM;
14788 	}
14789 
14790 	/* get statistics counter */
14791 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14792 					 &veb->stats_idx, NULL, NULL, NULL);
14793 	if (ret) {
14794 		dev_info(&pf->pdev->dev,
14795 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14796 			 ERR_PTR(ret),
14797 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14798 		return -EPERM;
14799 	}
14800 	ret = i40e_veb_get_bw_info(veb);
14801 	if (ret) {
14802 		dev_info(&pf->pdev->dev,
14803 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14804 			 ERR_PTR(ret),
14805 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14806 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14807 		return -ENOENT;
14808 	}
14809 
14810 	vsi->uplink_seid = veb->seid;
14811 	vsi->veb_idx = veb->idx;
14812 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14813 
14814 	return 0;
14815 }
14816 
14817 /**
14818  * i40e_veb_setup - Set up a VEB
14819  * @pf: board private structure
14820  * @flags: VEB setup flags
14821  * @uplink_seid: the switch element to link to
14822  * @vsi_seid: the initial VSI seid
14823  * @enabled_tc: Enabled TC bit-map
14824  *
14825  * This allocates the sw VEB structure and links it into the switch
14826  * It is possible and legal for this to be a duplicate of an already
14827  * existing VEB.  It is also possible for both uplink and vsi seids
14828  * to be zero, in order to create a floating VEB.
14829  *
14830  * Returns pointer to the successfully allocated VEB sw struct on
14831  * success, otherwise returns NULL on failure.
14832  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14833 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14834 				u16 uplink_seid, u16 vsi_seid,
14835 				u8 enabled_tc)
14836 {
14837 	struct i40e_veb *veb, *uplink_veb = NULL;
14838 	int vsi_idx, veb_idx;
14839 	int ret;
14840 
14841 	/* if one seid is 0, the other must be 0 to create a floating relay */
14842 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14843 	    (uplink_seid + vsi_seid != 0)) {
14844 		dev_info(&pf->pdev->dev,
14845 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14846 			 uplink_seid, vsi_seid);
14847 		return NULL;
14848 	}
14849 
14850 	/* make sure there is such a vsi and uplink */
14851 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14852 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14853 			break;
14854 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14855 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14856 			 vsi_seid);
14857 		return NULL;
14858 	}
14859 
14860 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14861 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14862 			if (pf->veb[veb_idx] &&
14863 			    pf->veb[veb_idx]->seid == uplink_seid) {
14864 				uplink_veb = pf->veb[veb_idx];
14865 				break;
14866 			}
14867 		}
14868 		if (!uplink_veb) {
14869 			dev_info(&pf->pdev->dev,
14870 				 "uplink seid %d not found\n", uplink_seid);
14871 			return NULL;
14872 		}
14873 	}
14874 
14875 	/* get veb sw struct */
14876 	veb_idx = i40e_veb_mem_alloc(pf);
14877 	if (veb_idx < 0)
14878 		goto err_alloc;
14879 	veb = pf->veb[veb_idx];
14880 	veb->flags = flags;
14881 	veb->uplink_seid = uplink_seid;
14882 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14883 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14884 
14885 	/* create the VEB in the switch */
14886 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14887 	if (ret)
14888 		goto err_veb;
14889 	if (vsi_idx == pf->lan_vsi)
14890 		pf->lan_veb = veb->idx;
14891 
14892 	return veb;
14893 
14894 err_veb:
14895 	i40e_veb_clear(veb);
14896 err_alloc:
14897 	return NULL;
14898 }
14899 
14900 /**
14901  * i40e_setup_pf_switch_element - set PF vars based on switch type
14902  * @pf: board private structure
14903  * @ele: element we are building info from
14904  * @num_reported: total number of elements
14905  * @printconfig: should we print the contents
14906  *
14907  * helper function to assist in extracting a few useful SEID values.
14908  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14909 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14910 				struct i40e_aqc_switch_config_element_resp *ele,
14911 				u16 num_reported, bool printconfig)
14912 {
14913 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14914 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14915 	u8 element_type = ele->element_type;
14916 	u16 seid = le16_to_cpu(ele->seid);
14917 
14918 	if (printconfig)
14919 		dev_info(&pf->pdev->dev,
14920 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14921 			 element_type, seid, uplink_seid, downlink_seid);
14922 
14923 	switch (element_type) {
14924 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14925 		pf->mac_seid = seid;
14926 		break;
14927 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14928 		/* Main VEB? */
14929 		if (uplink_seid != pf->mac_seid)
14930 			break;
14931 		if (pf->lan_veb >= I40E_MAX_VEB) {
14932 			int v;
14933 
14934 			/* find existing or else empty VEB */
14935 			for (v = 0; v < I40E_MAX_VEB; v++) {
14936 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14937 					pf->lan_veb = v;
14938 					break;
14939 				}
14940 			}
14941 			if (pf->lan_veb >= I40E_MAX_VEB) {
14942 				v = i40e_veb_mem_alloc(pf);
14943 				if (v < 0)
14944 					break;
14945 				pf->lan_veb = v;
14946 			}
14947 		}
14948 		if (pf->lan_veb >= I40E_MAX_VEB)
14949 			break;
14950 
14951 		pf->veb[pf->lan_veb]->seid = seid;
14952 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14953 		pf->veb[pf->lan_veb]->pf = pf;
14954 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14955 		break;
14956 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14957 		if (num_reported != 1)
14958 			break;
14959 		/* This is immediately after a reset so we can assume this is
14960 		 * the PF's VSI
14961 		 */
14962 		pf->mac_seid = uplink_seid;
14963 		pf->pf_seid = downlink_seid;
14964 		pf->main_vsi_seid = seid;
14965 		if (printconfig)
14966 			dev_info(&pf->pdev->dev,
14967 				 "pf_seid=%d main_vsi_seid=%d\n",
14968 				 pf->pf_seid, pf->main_vsi_seid);
14969 		break;
14970 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14971 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14972 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14973 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14974 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14975 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14976 		/* ignore these for now */
14977 		break;
14978 	default:
14979 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14980 			 element_type, seid);
14981 		break;
14982 	}
14983 }
14984 
14985 /**
14986  * i40e_fetch_switch_configuration - Get switch config from firmware
14987  * @pf: board private structure
14988  * @printconfig: should we print the contents
14989  *
14990  * Get the current switch configuration from the device and
14991  * extract a few useful SEID values.
14992  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14993 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14994 {
14995 	struct i40e_aqc_get_switch_config_resp *sw_config;
14996 	u16 next_seid = 0;
14997 	int ret = 0;
14998 	u8 *aq_buf;
14999 	int i;
15000 
15001 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15002 	if (!aq_buf)
15003 		return -ENOMEM;
15004 
15005 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15006 	do {
15007 		u16 num_reported, num_total;
15008 
15009 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15010 						I40E_AQ_LARGE_BUF,
15011 						&next_seid, NULL);
15012 		if (ret) {
15013 			dev_info(&pf->pdev->dev,
15014 				 "get switch config failed err %d aq_err %s\n",
15015 				 ret,
15016 				 i40e_aq_str(&pf->hw,
15017 					     pf->hw.aq.asq_last_status));
15018 			kfree(aq_buf);
15019 			return -ENOENT;
15020 		}
15021 
15022 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15023 		num_total = le16_to_cpu(sw_config->header.num_total);
15024 
15025 		if (printconfig)
15026 			dev_info(&pf->pdev->dev,
15027 				 "header: %d reported %d total\n",
15028 				 num_reported, num_total);
15029 
15030 		for (i = 0; i < num_reported; i++) {
15031 			struct i40e_aqc_switch_config_element_resp *ele =
15032 				&sw_config->element[i];
15033 
15034 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15035 						     printconfig);
15036 		}
15037 	} while (next_seid != 0);
15038 
15039 	kfree(aq_buf);
15040 	return ret;
15041 }
15042 
15043 /**
15044  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15045  * @pf: board private structure
15046  * @reinit: if the Main VSI needs to re-initialized.
15047  * @lock_acquired: indicates whether or not the lock has been acquired
15048  *
15049  * Returns 0 on success, negative value on failure
15050  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)15051 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15052 {
15053 	u16 flags = 0;
15054 	int ret;
15055 
15056 	/* find out what's out there already */
15057 	ret = i40e_fetch_switch_configuration(pf, false);
15058 	if (ret) {
15059 		dev_info(&pf->pdev->dev,
15060 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15061 			 ERR_PTR(ret),
15062 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15063 		return ret;
15064 	}
15065 	i40e_pf_reset_stats(pf);
15066 
15067 	/* set the switch config bit for the whole device to
15068 	 * support limited promisc or true promisc
15069 	 * when user requests promisc. The default is limited
15070 	 * promisc.
15071 	*/
15072 
15073 	if ((pf->hw.pf_id == 0) &&
15074 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15075 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15076 		pf->last_sw_conf_flags = flags;
15077 	}
15078 
15079 	if (pf->hw.pf_id == 0) {
15080 		u16 valid_flags;
15081 
15082 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15083 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15084 						NULL);
15085 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15086 			dev_info(&pf->pdev->dev,
15087 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15088 				 ERR_PTR(ret),
15089 				 i40e_aq_str(&pf->hw,
15090 					     pf->hw.aq.asq_last_status));
15091 			/* not a fatal problem, just keep going */
15092 		}
15093 		pf->last_sw_conf_valid_flags = valid_flags;
15094 	}
15095 
15096 	/* first time setup */
15097 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15098 		struct i40e_vsi *vsi = NULL;
15099 		u16 uplink_seid;
15100 
15101 		/* Set up the PF VSI associated with the PF's main VSI
15102 		 * that is already in the HW switch
15103 		 */
15104 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15105 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15106 		else
15107 			uplink_seid = pf->mac_seid;
15108 		if (pf->lan_vsi == I40E_NO_VSI)
15109 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15110 		else if (reinit)
15111 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15112 		if (!vsi) {
15113 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15114 			i40e_cloud_filter_exit(pf);
15115 			i40e_fdir_teardown(pf);
15116 			return -EAGAIN;
15117 		}
15118 	} else {
15119 		/* force a reset of TC and queue layout configurations */
15120 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15121 
15122 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15123 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15124 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15125 	}
15126 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15127 
15128 	i40e_fdir_sb_setup(pf);
15129 
15130 	/* Setup static PF queue filter control settings */
15131 	ret = i40e_setup_pf_filter_control(pf);
15132 	if (ret) {
15133 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15134 			 ret);
15135 		/* Failure here should not stop continuing other steps */
15136 	}
15137 
15138 	/* enable RSS in the HW, even for only one queue, as the stack can use
15139 	 * the hash
15140 	 */
15141 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15142 		i40e_pf_config_rss(pf);
15143 
15144 	/* fill in link information and enable LSE reporting */
15145 	i40e_link_event(pf);
15146 
15147 	/* Initialize user-specific link properties */
15148 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15149 				  I40E_AQ_AN_COMPLETED) ? true : false);
15150 
15151 	i40e_ptp_init(pf);
15152 
15153 	if (!lock_acquired)
15154 		rtnl_lock();
15155 
15156 	/* repopulate tunnel port filters */
15157 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15158 
15159 	if (!lock_acquired)
15160 		rtnl_unlock();
15161 
15162 	return ret;
15163 }
15164 
15165 /**
15166  * i40e_determine_queue_usage - Work out queue distribution
15167  * @pf: board private structure
15168  **/
i40e_determine_queue_usage(struct i40e_pf * pf)15169 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15170 {
15171 	int queues_left;
15172 	int q_max;
15173 
15174 	pf->num_lan_qps = 0;
15175 
15176 	/* Find the max queues to be put into basic use.  We'll always be
15177 	 * using TC0, whether or not DCB is running, and TC0 will get the
15178 	 * big RSS set.
15179 	 */
15180 	queues_left = pf->hw.func_caps.num_tx_qp;
15181 
15182 	if ((queues_left == 1) ||
15183 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15184 		/* one qp for PF, no queues for anything else */
15185 		queues_left = 0;
15186 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15187 
15188 		/* make sure all the fancies are disabled */
15189 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15190 			       I40E_FLAG_IWARP_ENABLED	|
15191 			       I40E_FLAG_FD_SB_ENABLED	|
15192 			       I40E_FLAG_FD_ATR_ENABLED	|
15193 			       I40E_FLAG_DCB_CAPABLE	|
15194 			       I40E_FLAG_DCB_ENABLED	|
15195 			       I40E_FLAG_SRIOV_ENABLED	|
15196 			       I40E_FLAG_VMDQ_ENABLED);
15197 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15198 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15199 				  I40E_FLAG_FD_SB_ENABLED |
15200 				  I40E_FLAG_FD_ATR_ENABLED |
15201 				  I40E_FLAG_DCB_CAPABLE))) {
15202 		/* one qp for PF */
15203 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15204 		queues_left -= pf->num_lan_qps;
15205 
15206 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15207 			       I40E_FLAG_IWARP_ENABLED	|
15208 			       I40E_FLAG_FD_SB_ENABLED	|
15209 			       I40E_FLAG_FD_ATR_ENABLED	|
15210 			       I40E_FLAG_DCB_ENABLED	|
15211 			       I40E_FLAG_VMDQ_ENABLED);
15212 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15213 	} else {
15214 		/* Not enough queues for all TCs */
15215 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15216 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15217 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15218 					I40E_FLAG_DCB_ENABLED);
15219 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15220 		}
15221 
15222 		/* limit lan qps to the smaller of qps, cpus or msix */
15223 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15224 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15225 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15226 		pf->num_lan_qps = q_max;
15227 
15228 		queues_left -= pf->num_lan_qps;
15229 	}
15230 
15231 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15232 		if (queues_left > 1) {
15233 			queues_left -= 1; /* save 1 queue for FD */
15234 		} else {
15235 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15236 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15237 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15238 		}
15239 	}
15240 
15241 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15242 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15243 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15244 					(queues_left / pf->num_vf_qps));
15245 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15246 	}
15247 
15248 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15249 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15250 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15251 					  (queues_left / pf->num_vmdq_qps));
15252 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15253 	}
15254 
15255 	pf->queues_left = queues_left;
15256 	dev_dbg(&pf->pdev->dev,
15257 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15258 		pf->hw.func_caps.num_tx_qp,
15259 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15260 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15261 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15262 		queues_left);
15263 }
15264 
15265 /**
15266  * i40e_setup_pf_filter_control - Setup PF static filter control
15267  * @pf: PF to be setup
15268  *
15269  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15270  * settings. If PE/FCoE are enabled then it will also set the per PF
15271  * based filter sizes required for them. It also enables Flow director,
15272  * ethertype and macvlan type filter settings for the pf.
15273  *
15274  * Returns 0 on success, negative on failure
15275  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)15276 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15277 {
15278 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15279 
15280 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15281 
15282 	/* Flow Director is enabled */
15283 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15284 		settings->enable_fdir = true;
15285 
15286 	/* Ethtype and MACVLAN filters enabled for PF */
15287 	settings->enable_ethtype = true;
15288 	settings->enable_macvlan = true;
15289 
15290 	if (i40e_set_filter_control(&pf->hw, settings))
15291 		return -ENOENT;
15292 
15293 	return 0;
15294 }
15295 
15296 #define INFO_STRING_LEN 255
15297 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)15298 static void i40e_print_features(struct i40e_pf *pf)
15299 {
15300 	struct i40e_hw *hw = &pf->hw;
15301 	char *buf;
15302 	int i;
15303 
15304 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15305 	if (!buf)
15306 		return;
15307 
15308 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15309 #ifdef CONFIG_PCI_IOV
15310 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15311 #endif
15312 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15313 		      pf->hw.func_caps.num_vsis,
15314 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15315 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15316 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15317 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15318 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15319 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15320 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15321 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15322 	}
15323 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15324 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15325 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15326 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15327 	if (pf->flags & I40E_FLAG_PTP)
15328 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15329 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15330 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15331 	else
15332 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15333 
15334 	dev_info(&pf->pdev->dev, "%s\n", buf);
15335 	kfree(buf);
15336 	WARN_ON(i > INFO_STRING_LEN);
15337 }
15338 
15339 /**
15340  * i40e_get_platform_mac_addr - get platform-specific MAC address
15341  * @pdev: PCI device information struct
15342  * @pf: board private structure
15343  *
15344  * Look up the MAC address for the device. First we'll try
15345  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15346  * specific fallback. Otherwise, we'll default to the stored value in
15347  * firmware.
15348  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)15349 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15350 {
15351 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15352 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15353 }
15354 
15355 /**
15356  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15357  * @fec_cfg: FEC option to set in flags
15358  * @flags: ptr to flags in which we set FEC option
15359  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)15360 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15361 {
15362 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15363 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15364 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15365 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15366 		*flags |= I40E_FLAG_RS_FEC;
15367 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15368 	}
15369 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15370 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15371 		*flags |= I40E_FLAG_BASE_R_FEC;
15372 		*flags &= ~I40E_FLAG_RS_FEC;
15373 	}
15374 	if (fec_cfg == 0)
15375 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15376 }
15377 
15378 /**
15379  * i40e_check_recovery_mode - check if we are running transition firmware
15380  * @pf: board private structure
15381  *
15382  * Check registers indicating the firmware runs in recovery mode. Sets the
15383  * appropriate driver state.
15384  *
15385  * Returns true if the recovery mode was detected, false otherwise
15386  **/
i40e_check_recovery_mode(struct i40e_pf * pf)15387 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15388 {
15389 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15390 
15391 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15392 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15393 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15394 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15395 
15396 		return true;
15397 	}
15398 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15399 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15400 
15401 	return false;
15402 }
15403 
15404 /**
15405  * i40e_pf_loop_reset - perform reset in a loop.
15406  * @pf: board private structure
15407  *
15408  * This function is useful when a NIC is about to enter recovery mode.
15409  * When a NIC's internal data structures are corrupted the NIC's
15410  * firmware is going to enter recovery mode.
15411  * Right after a POR it takes about 7 minutes for firmware to enter
15412  * recovery mode. Until that time a NIC is in some kind of intermediate
15413  * state. After that time period the NIC almost surely enters
15414  * recovery mode. The only way for a driver to detect intermediate
15415  * state is to issue a series of pf-resets and check a return value.
15416  * If a PF reset returns success then the firmware could be in recovery
15417  * mode so the caller of this code needs to check for recovery mode
15418  * if this function returns success. There is a little chance that
15419  * firmware will hang in intermediate state forever.
15420  * Since waiting 7 minutes is quite a lot of time this function waits
15421  * 10 seconds and then gives up by returning an error.
15422  *
15423  * Return 0 on success, negative on failure.
15424  **/
i40e_pf_loop_reset(struct i40e_pf * pf)15425 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15426 {
15427 	/* wait max 10 seconds for PF reset to succeed */
15428 	const unsigned long time_end = jiffies + 10 * HZ;
15429 	struct i40e_hw *hw = &pf->hw;
15430 	int ret;
15431 
15432 	ret = i40e_pf_reset(hw);
15433 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15434 		usleep_range(10000, 20000);
15435 		ret = i40e_pf_reset(hw);
15436 	}
15437 
15438 	if (ret == I40E_SUCCESS)
15439 		pf->pfr_count++;
15440 	else
15441 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15442 
15443 	return ret;
15444 }
15445 
15446 /**
15447  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15448  * @pf: board private structure
15449  *
15450  * Check FW registers to determine if FW issued unexpected EMP Reset.
15451  * Every time when unexpected EMP Reset occurs the FW increments
15452  * a counter of unexpected EMP Resets. When the counter reaches 10
15453  * the FW should enter the Recovery mode
15454  *
15455  * Returns true if FW issued unexpected EMP Reset
15456  **/
i40e_check_fw_empr(struct i40e_pf * pf)15457 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15458 {
15459 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15460 			   I40E_GL_FWSTS_FWS1B_MASK;
15461 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15462 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15463 }
15464 
15465 /**
15466  * i40e_handle_resets - handle EMP resets and PF resets
15467  * @pf: board private structure
15468  *
15469  * Handle both EMP resets and PF resets and conclude whether there are
15470  * any issues regarding these resets. If there are any issues then
15471  * generate log entry.
15472  *
15473  * Return 0 if NIC is healthy or negative value when there are issues
15474  * with resets
15475  **/
i40e_handle_resets(struct i40e_pf * pf)15476 static int i40e_handle_resets(struct i40e_pf *pf)
15477 {
15478 	const int pfr = i40e_pf_loop_reset(pf);
15479 	const bool is_empr = i40e_check_fw_empr(pf);
15480 
15481 	if (is_empr || pfr != I40E_SUCCESS)
15482 		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");
15483 
15484 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15485 }
15486 
15487 /**
15488  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15489  * @pf: board private structure
15490  * @hw: ptr to the hardware info
15491  *
15492  * This function does a minimal setup of all subsystems needed for running
15493  * recovery mode.
15494  *
15495  * Returns 0 on success, negative on failure
15496  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)15497 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15498 {
15499 	struct i40e_vsi *vsi;
15500 	int err;
15501 	int v_idx;
15502 
15503 	pci_set_drvdata(pf->pdev, pf);
15504 	pci_save_state(pf->pdev);
15505 
15506 	/* set up periodic task facility */
15507 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15508 	pf->service_timer_period = HZ;
15509 
15510 	INIT_WORK(&pf->service_task, i40e_service_task);
15511 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15512 
15513 	err = i40e_init_interrupt_scheme(pf);
15514 	if (err)
15515 		goto err_switch_setup;
15516 
15517 	/* The number of VSIs reported by the FW is the minimum guaranteed
15518 	 * to us; HW supports far more and we share the remaining pool with
15519 	 * the other PFs. We allocate space for more than the guarantee with
15520 	 * the understanding that we might not get them all later.
15521 	 */
15522 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15523 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15524 	else
15525 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15526 
15527 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15528 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15529 			  GFP_KERNEL);
15530 	if (!pf->vsi) {
15531 		err = -ENOMEM;
15532 		goto err_switch_setup;
15533 	}
15534 
15535 	/* We allocate one VSI which is needed as absolute minimum
15536 	 * in order to register the netdev
15537 	 */
15538 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15539 	if (v_idx < 0) {
15540 		err = v_idx;
15541 		goto err_switch_setup;
15542 	}
15543 	pf->lan_vsi = v_idx;
15544 	vsi = pf->vsi[v_idx];
15545 	if (!vsi) {
15546 		err = -EFAULT;
15547 		goto err_switch_setup;
15548 	}
15549 	vsi->alloc_queue_pairs = 1;
15550 	err = i40e_config_netdev(vsi);
15551 	if (err)
15552 		goto err_switch_setup;
15553 	err = register_netdev(vsi->netdev);
15554 	if (err)
15555 		goto err_switch_setup;
15556 	vsi->netdev_registered = true;
15557 	i40e_dbg_pf_init(pf);
15558 
15559 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15560 	if (err)
15561 		goto err_switch_setup;
15562 
15563 	/* tell the firmware that we're starting */
15564 	i40e_send_version(pf);
15565 
15566 	/* since everything's happy, start the service_task timer */
15567 	mod_timer(&pf->service_timer,
15568 		  round_jiffies(jiffies + pf->service_timer_period));
15569 
15570 	return 0;
15571 
15572 err_switch_setup:
15573 	i40e_reset_interrupt_capability(pf);
15574 	del_timer_sync(&pf->service_timer);
15575 	i40e_shutdown_adminq(hw);
15576 	iounmap(hw->hw_addr);
15577 	pci_disable_pcie_error_reporting(pf->pdev);
15578 	pci_release_mem_regions(pf->pdev);
15579 	pci_disable_device(pf->pdev);
15580 	kfree(pf);
15581 
15582 	return err;
15583 }
15584 
15585 /**
15586  * i40e_set_subsystem_device_id - set subsystem device id
15587  * @hw: pointer to the hardware info
15588  *
15589  * Set PCI subsystem device id either from a pci_dev structure or
15590  * a specific FW register.
15591  **/
i40e_set_subsystem_device_id(struct i40e_hw * hw)15592 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15593 {
15594 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15595 
15596 	hw->subsystem_device_id = pdev->subsystem_device ?
15597 		pdev->subsystem_device :
15598 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15599 }
15600 
15601 /**
15602  * i40e_probe - Device initialization routine
15603  * @pdev: PCI device information struct
15604  * @ent: entry in i40e_pci_tbl
15605  *
15606  * i40e_probe initializes a PF identified by a pci_dev structure.
15607  * The OS initialization, configuring of the PF private structure,
15608  * and a hardware reset occur.
15609  *
15610  * Returns 0 on success, negative on failure
15611  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)15612 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15613 {
15614 	struct i40e_aq_get_phy_abilities_resp abilities;
15615 #ifdef CONFIG_I40E_DCB
15616 	enum i40e_get_fw_lldp_status_resp lldp_status;
15617 #endif /* CONFIG_I40E_DCB */
15618 	struct i40e_pf *pf;
15619 	struct i40e_hw *hw;
15620 	static u16 pfs_found;
15621 	u16 wol_nvm_bits;
15622 	u16 link_status;
15623 #ifdef CONFIG_I40E_DCB
15624 	int status;
15625 #endif /* CONFIG_I40E_DCB */
15626 	int err;
15627 	u32 val;
15628 	u32 i;
15629 
15630 	err = pci_enable_device_mem(pdev);
15631 	if (err)
15632 		return err;
15633 
15634 	/* set up for high or low dma */
15635 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15636 	if (err) {
15637 		dev_err(&pdev->dev,
15638 			"DMA configuration failed: 0x%x\n", err);
15639 		goto err_dma;
15640 	}
15641 
15642 	/* set up pci connections */
15643 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15644 	if (err) {
15645 		dev_info(&pdev->dev,
15646 			 "pci_request_selected_regions failed %d\n", err);
15647 		goto err_pci_reg;
15648 	}
15649 
15650 	pci_enable_pcie_error_reporting(pdev);
15651 	pci_set_master(pdev);
15652 
15653 	/* Now that we have a PCI connection, we need to do the
15654 	 * low level device setup.  This is primarily setting up
15655 	 * the Admin Queue structures and then querying for the
15656 	 * device's current profile information.
15657 	 */
15658 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15659 	if (!pf) {
15660 		err = -ENOMEM;
15661 		goto err_pf_alloc;
15662 	}
15663 	pf->next_vsi = 0;
15664 	pf->pdev = pdev;
15665 	set_bit(__I40E_DOWN, pf->state);
15666 
15667 	hw = &pf->hw;
15668 	hw->back = pf;
15669 
15670 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15671 				I40E_MAX_CSR_SPACE);
15672 	/* We believe that the highest register to read is
15673 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15674 	 * is not less than that before mapping to prevent a
15675 	 * kernel panic.
15676 	 */
15677 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15678 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15679 			pf->ioremap_len);
15680 		err = -ENOMEM;
15681 		goto err_ioremap;
15682 	}
15683 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15684 	if (!hw->hw_addr) {
15685 		err = -EIO;
15686 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15687 			 (unsigned int)pci_resource_start(pdev, 0),
15688 			 pf->ioremap_len, err);
15689 		goto err_ioremap;
15690 	}
15691 	hw->vendor_id = pdev->vendor;
15692 	hw->device_id = pdev->device;
15693 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15694 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15695 	i40e_set_subsystem_device_id(hw);
15696 	hw->bus.device = PCI_SLOT(pdev->devfn);
15697 	hw->bus.func = PCI_FUNC(pdev->devfn);
15698 	hw->bus.bus_id = pdev->bus->number;
15699 	pf->instance = pfs_found;
15700 
15701 	/* Select something other than the 802.1ad ethertype for the
15702 	 * switch to use internally and drop on ingress.
15703 	 */
15704 	hw->switch_tag = 0xffff;
15705 	hw->first_tag = ETH_P_8021AD;
15706 	hw->second_tag = ETH_P_8021Q;
15707 
15708 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15709 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15710 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15711 
15712 	/* set up the locks for the AQ, do this only once in probe
15713 	 * and destroy them only once in remove
15714 	 */
15715 	mutex_init(&hw->aq.asq_mutex);
15716 	mutex_init(&hw->aq.arq_mutex);
15717 
15718 	pf->msg_enable = netif_msg_init(debug,
15719 					NETIF_MSG_DRV |
15720 					NETIF_MSG_PROBE |
15721 					NETIF_MSG_LINK);
15722 	if (debug < -1)
15723 		pf->hw.debug_mask = debug;
15724 
15725 	/* do a special CORER for clearing PXE mode once at init */
15726 	if (hw->revision_id == 0 &&
15727 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15728 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15729 		i40e_flush(hw);
15730 		msleep(200);
15731 		pf->corer_count++;
15732 
15733 		i40e_clear_pxe_mode(hw);
15734 	}
15735 
15736 	/* Reset here to make sure all is clean and to define PF 'n' */
15737 	i40e_clear_hw(hw);
15738 
15739 	err = i40e_set_mac_type(hw);
15740 	if (err) {
15741 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15742 			 err);
15743 		goto err_pf_reset;
15744 	}
15745 
15746 	err = i40e_handle_resets(pf);
15747 	if (err)
15748 		goto err_pf_reset;
15749 
15750 	i40e_check_recovery_mode(pf);
15751 
15752 	if (is_kdump_kernel()) {
15753 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15754 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15755 	} else {
15756 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15757 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15758 	}
15759 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15760 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15761 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15762 
15763 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15764 		 "%s-%s:misc",
15765 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15766 
15767 	err = i40e_init_shared_code(hw);
15768 	if (err) {
15769 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15770 			 err);
15771 		goto err_pf_reset;
15772 	}
15773 
15774 	/* set up a default setting for link flow control */
15775 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15776 
15777 	err = i40e_init_adminq(hw);
15778 	if (err) {
15779 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15780 			dev_info(&pdev->dev,
15781 				 "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",
15782 				 hw->aq.api_maj_ver,
15783 				 hw->aq.api_min_ver,
15784 				 I40E_FW_API_VERSION_MAJOR,
15785 				 I40E_FW_MINOR_VERSION(hw));
15786 		else
15787 			dev_info(&pdev->dev,
15788 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15789 
15790 		goto err_pf_reset;
15791 	}
15792 	i40e_get_oem_version(hw);
15793 
15794 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15795 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15796 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15797 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15798 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15799 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15800 
15801 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15802 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15803 		dev_dbg(&pdev->dev,
15804 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15805 			 hw->aq.api_maj_ver,
15806 			 hw->aq.api_min_ver,
15807 			 I40E_FW_API_VERSION_MAJOR,
15808 			 I40E_FW_MINOR_VERSION(hw));
15809 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15810 		dev_info(&pdev->dev,
15811 			 "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",
15812 			 hw->aq.api_maj_ver,
15813 			 hw->aq.api_min_ver,
15814 			 I40E_FW_API_VERSION_MAJOR,
15815 			 I40E_FW_MINOR_VERSION(hw));
15816 
15817 	i40e_verify_eeprom(pf);
15818 
15819 	/* Rev 0 hardware was never productized */
15820 	if (hw->revision_id < 1)
15821 		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");
15822 
15823 	i40e_clear_pxe_mode(hw);
15824 
15825 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15826 	if (err)
15827 		goto err_adminq_setup;
15828 
15829 	err = i40e_sw_init(pf);
15830 	if (err) {
15831 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15832 		goto err_sw_init;
15833 	}
15834 
15835 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15836 		return i40e_init_recovery_mode(pf, hw);
15837 
15838 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15839 				hw->func_caps.num_rx_qp, 0, 0);
15840 	if (err) {
15841 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15842 		goto err_init_lan_hmc;
15843 	}
15844 
15845 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15846 	if (err) {
15847 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15848 		err = -ENOENT;
15849 		goto err_configure_lan_hmc;
15850 	}
15851 
15852 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15853 	 * Ignore error return codes because if it was already disabled via
15854 	 * hardware settings this will fail
15855 	 */
15856 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15857 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15858 		i40e_aq_stop_lldp(hw, true, false, NULL);
15859 	}
15860 
15861 	/* allow a platform config to override the HW addr */
15862 	i40e_get_platform_mac_addr(pdev, pf);
15863 
15864 	if (!is_valid_ether_addr(hw->mac.addr)) {
15865 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15866 		err = -EIO;
15867 		goto err_mac_addr;
15868 	}
15869 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15870 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15871 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15872 	if (is_valid_ether_addr(hw->mac.port_addr))
15873 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15874 
15875 	i40e_ptp_alloc_pins(pf);
15876 	pci_set_drvdata(pdev, pf);
15877 	pci_save_state(pdev);
15878 
15879 #ifdef CONFIG_I40E_DCB
15880 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15881 	(!status &&
15882 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15883 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15884 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15885 	dev_info(&pdev->dev,
15886 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15887 			"FW LLDP is disabled\n" :
15888 			"FW LLDP is enabled\n");
15889 
15890 	/* Enable FW to write default DCB config on link-up */
15891 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15892 
15893 	err = i40e_init_pf_dcb(pf);
15894 	if (err) {
15895 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15896 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15897 		/* Continue without DCB enabled */
15898 	}
15899 #endif /* CONFIG_I40E_DCB */
15900 
15901 	/* set up periodic task facility */
15902 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15903 	pf->service_timer_period = HZ;
15904 
15905 	INIT_WORK(&pf->service_task, i40e_service_task);
15906 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15907 
15908 	/* NVM bit on means WoL disabled for the port */
15909 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15910 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15911 		pf->wol_en = false;
15912 	else
15913 		pf->wol_en = true;
15914 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15915 
15916 	/* set up the main switch operations */
15917 	i40e_determine_queue_usage(pf);
15918 	err = i40e_init_interrupt_scheme(pf);
15919 	if (err)
15920 		goto err_switch_setup;
15921 
15922 	/* Reduce Tx and Rx pairs for kdump
15923 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15924 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15925 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15926 	 */
15927 	if (is_kdump_kernel())
15928 		pf->num_lan_msix = 1;
15929 
15930 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15931 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15932 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15933 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15934 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15935 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15936 						    UDP_TUNNEL_TYPE_GENEVE;
15937 
15938 	/* The number of VSIs reported by the FW is the minimum guaranteed
15939 	 * to us; HW supports far more and we share the remaining pool with
15940 	 * the other PFs. We allocate space for more than the guarantee with
15941 	 * the understanding that we might not get them all later.
15942 	 */
15943 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15944 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15945 	else
15946 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15947 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15948 		dev_warn(&pf->pdev->dev,
15949 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15950 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15951 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15952 	}
15953 
15954 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15955 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15956 			  GFP_KERNEL);
15957 	if (!pf->vsi) {
15958 		err = -ENOMEM;
15959 		goto err_switch_setup;
15960 	}
15961 
15962 #ifdef CONFIG_PCI_IOV
15963 	/* prep for VF support */
15964 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15965 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15966 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15967 		if (pci_num_vf(pdev))
15968 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15969 	}
15970 #endif
15971 	err = i40e_setup_pf_switch(pf, false, false);
15972 	if (err) {
15973 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15974 		goto err_vsis;
15975 	}
15976 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15977 
15978 	/* if FDIR VSI was set up, start it now */
15979 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15980 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15981 			i40e_vsi_open(pf->vsi[i]);
15982 			break;
15983 		}
15984 	}
15985 
15986 	/* The driver only wants link up/down and module qualification
15987 	 * reports from firmware.  Note the negative logic.
15988 	 */
15989 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15990 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15991 					 I40E_AQ_EVENT_MEDIA_NA |
15992 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15993 	if (err)
15994 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15995 			 ERR_PTR(err),
15996 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15997 
15998 	/* Reconfigure hardware for allowing smaller MSS in the case
15999 	 * of TSO, so that we avoid the MDD being fired and causing
16000 	 * a reset in the case of small MSS+TSO.
16001 	 */
16002 	val = rd32(hw, I40E_REG_MSS);
16003 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16004 		val &= ~I40E_REG_MSS_MIN_MASK;
16005 		val |= I40E_64BYTE_MSS;
16006 		wr32(hw, I40E_REG_MSS, val);
16007 	}
16008 
16009 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16010 		msleep(75);
16011 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16012 		if (err)
16013 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16014 				 ERR_PTR(err),
16015 				 i40e_aq_str(&pf->hw,
16016 					     pf->hw.aq.asq_last_status));
16017 	}
16018 	/* The main driver is (mostly) up and happy. We need to set this state
16019 	 * before setting up the misc vector or we get a race and the vector
16020 	 * ends up disabled forever.
16021 	 */
16022 	clear_bit(__I40E_DOWN, pf->state);
16023 
16024 	/* In case of MSIX we are going to setup the misc vector right here
16025 	 * to handle admin queue events etc. In case of legacy and MSI
16026 	 * the misc functionality and queue processing is combined in
16027 	 * the same vector and that gets setup at open.
16028 	 */
16029 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16030 		err = i40e_setup_misc_vector(pf);
16031 		if (err) {
16032 			dev_info(&pdev->dev,
16033 				 "setup of misc vector failed: %d\n", err);
16034 			i40e_cloud_filter_exit(pf);
16035 			i40e_fdir_teardown(pf);
16036 			goto err_vsis;
16037 		}
16038 	}
16039 
16040 #ifdef CONFIG_PCI_IOV
16041 	/* prep for VF support */
16042 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16043 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16044 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16045 		/* disable link interrupts for VFs */
16046 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16047 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16048 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16049 		i40e_flush(hw);
16050 
16051 		if (pci_num_vf(pdev)) {
16052 			dev_info(&pdev->dev,
16053 				 "Active VFs found, allocating resources.\n");
16054 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16055 			if (err)
16056 				dev_info(&pdev->dev,
16057 					 "Error %d allocating resources for existing VFs\n",
16058 					 err);
16059 		}
16060 	}
16061 #endif /* CONFIG_PCI_IOV */
16062 
16063 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16064 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16065 						      pf->num_iwarp_msix,
16066 						      I40E_IWARP_IRQ_PILE_ID);
16067 		if (pf->iwarp_base_vector < 0) {
16068 			dev_info(&pdev->dev,
16069 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16070 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16071 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16072 		}
16073 	}
16074 
16075 	i40e_dbg_pf_init(pf);
16076 
16077 	/* tell the firmware that we're starting */
16078 	i40e_send_version(pf);
16079 
16080 	/* since everything's happy, start the service_task timer */
16081 	mod_timer(&pf->service_timer,
16082 		  round_jiffies(jiffies + pf->service_timer_period));
16083 
16084 	/* add this PF to client device list and launch a client service task */
16085 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16086 		err = i40e_lan_add_device(pf);
16087 		if (err)
16088 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16089 				 err);
16090 	}
16091 
16092 #define PCI_SPEED_SIZE 8
16093 #define PCI_WIDTH_SIZE 8
16094 	/* Devices on the IOSF bus do not have this information
16095 	 * and will report PCI Gen 1 x 1 by default so don't bother
16096 	 * checking them.
16097 	 */
16098 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16099 		char speed[PCI_SPEED_SIZE] = "Unknown";
16100 		char width[PCI_WIDTH_SIZE] = "Unknown";
16101 
16102 		/* Get the negotiated link width and speed from PCI config
16103 		 * space
16104 		 */
16105 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16106 					  &link_status);
16107 
16108 		i40e_set_pci_config_data(hw, link_status);
16109 
16110 		switch (hw->bus.speed) {
16111 		case i40e_bus_speed_8000:
16112 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16113 		case i40e_bus_speed_5000:
16114 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16115 		case i40e_bus_speed_2500:
16116 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16117 		default:
16118 			break;
16119 		}
16120 		switch (hw->bus.width) {
16121 		case i40e_bus_width_pcie_x8:
16122 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16123 		case i40e_bus_width_pcie_x4:
16124 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16125 		case i40e_bus_width_pcie_x2:
16126 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16127 		case i40e_bus_width_pcie_x1:
16128 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16129 		default:
16130 			break;
16131 		}
16132 
16133 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16134 			 speed, width);
16135 
16136 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16137 		    hw->bus.speed < i40e_bus_speed_8000) {
16138 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16139 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16140 		}
16141 	}
16142 
16143 	/* get the requested speeds from the fw */
16144 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16145 	if (err)
16146 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16147 			ERR_PTR(err),
16148 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16149 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16150 
16151 	/* set the FEC config due to the board capabilities */
16152 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16153 
16154 	/* get the supported phy types from the fw */
16155 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16156 	if (err)
16157 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16158 			ERR_PTR(err),
16159 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16160 
16161 	/* make sure the MFS hasn't been set lower than the default */
16162 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16163 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16164 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16165 	if (val < MAX_FRAME_SIZE_DEFAULT)
16166 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16167 			 pf->hw.port, val);
16168 
16169 	/* Add a filter to drop all Flow control frames from any VSI from being
16170 	 * transmitted. By doing so we stop a malicious VF from sending out
16171 	 * PAUSE or PFC frames and potentially controlling traffic for other
16172 	 * PF/VF VSIs.
16173 	 * The FW can still send Flow control frames if enabled.
16174 	 */
16175 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16176 						       pf->main_vsi_seid);
16177 
16178 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16179 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16180 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16181 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16182 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16183 	/* print a string summarizing features */
16184 	i40e_print_features(pf);
16185 
16186 	return 0;
16187 
16188 	/* Unwind what we've done if something failed in the setup */
16189 err_vsis:
16190 	set_bit(__I40E_DOWN, pf->state);
16191 	i40e_clear_interrupt_scheme(pf);
16192 	kfree(pf->vsi);
16193 err_switch_setup:
16194 	i40e_reset_interrupt_capability(pf);
16195 	del_timer_sync(&pf->service_timer);
16196 err_mac_addr:
16197 err_configure_lan_hmc:
16198 	(void)i40e_shutdown_lan_hmc(hw);
16199 err_init_lan_hmc:
16200 	kfree(pf->qp_pile);
16201 err_sw_init:
16202 err_adminq_setup:
16203 err_pf_reset:
16204 	iounmap(hw->hw_addr);
16205 err_ioremap:
16206 	kfree(pf);
16207 err_pf_alloc:
16208 	pci_disable_pcie_error_reporting(pdev);
16209 	pci_release_mem_regions(pdev);
16210 err_pci_reg:
16211 err_dma:
16212 	pci_disable_device(pdev);
16213 	return err;
16214 }
16215 
16216 /**
16217  * i40e_remove - Device removal routine
16218  * @pdev: PCI device information struct
16219  *
16220  * i40e_remove is called by the PCI subsystem to alert the driver
16221  * that is should release a PCI device.  This could be caused by a
16222  * Hot-Plug event, or because the driver is going to be removed from
16223  * memory.
16224  **/
i40e_remove(struct pci_dev * pdev)16225 static void i40e_remove(struct pci_dev *pdev)
16226 {
16227 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16228 	struct i40e_hw *hw = &pf->hw;
16229 	int ret_code;
16230 	int i;
16231 
16232 	i40e_dbg_pf_exit(pf);
16233 
16234 	i40e_ptp_stop(pf);
16235 
16236 	/* Disable RSS in hw */
16237 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16238 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16239 
16240 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16241 	 * flags, once they are set, i40e_rebuild should not be called as
16242 	 * i40e_prep_for_reset always returns early.
16243 	 */
16244 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16245 		usleep_range(1000, 2000);
16246 	set_bit(__I40E_IN_REMOVE, pf->state);
16247 
16248 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16249 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16250 		i40e_free_vfs(pf);
16251 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16252 	}
16253 	/* no more scheduling of any task */
16254 	set_bit(__I40E_SUSPENDED, pf->state);
16255 	set_bit(__I40E_DOWN, pf->state);
16256 	if (pf->service_timer.function)
16257 		del_timer_sync(&pf->service_timer);
16258 	if (pf->service_task.func)
16259 		cancel_work_sync(&pf->service_task);
16260 
16261 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16262 		struct i40e_vsi *vsi = pf->vsi[0];
16263 
16264 		/* We know that we have allocated only one vsi for this PF,
16265 		 * it was just for registering netdevice, so the interface
16266 		 * could be visible in the 'ifconfig' output
16267 		 */
16268 		unregister_netdev(vsi->netdev);
16269 		free_netdev(vsi->netdev);
16270 
16271 		goto unmap;
16272 	}
16273 
16274 	/* Client close must be called explicitly here because the timer
16275 	 * has been stopped.
16276 	 */
16277 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16278 
16279 	i40e_fdir_teardown(pf);
16280 
16281 	/* If there is a switch structure or any orphans, remove them.
16282 	 * This will leave only the PF's VSI remaining.
16283 	 */
16284 	for (i = 0; i < I40E_MAX_VEB; i++) {
16285 		if (!pf->veb[i])
16286 			continue;
16287 
16288 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16289 		    pf->veb[i]->uplink_seid == 0)
16290 			i40e_switch_branch_release(pf->veb[i]);
16291 	}
16292 
16293 	/* Now we can shutdown the PF's VSIs, just before we kill
16294 	 * adminq and hmc.
16295 	 */
16296 	for (i = pf->num_alloc_vsi; i--;)
16297 		if (pf->vsi[i]) {
16298 			i40e_vsi_close(pf->vsi[i]);
16299 			i40e_vsi_release(pf->vsi[i]);
16300 			pf->vsi[i] = NULL;
16301 		}
16302 
16303 	i40e_cloud_filter_exit(pf);
16304 
16305 	/* remove attached clients */
16306 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16307 		ret_code = i40e_lan_del_device(pf);
16308 		if (ret_code)
16309 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16310 				 ret_code);
16311 	}
16312 
16313 	/* shutdown and destroy the HMC */
16314 	if (hw->hmc.hmc_obj) {
16315 		ret_code = i40e_shutdown_lan_hmc(hw);
16316 		if (ret_code)
16317 			dev_warn(&pdev->dev,
16318 				 "Failed to destroy the HMC resources: %d\n",
16319 				 ret_code);
16320 	}
16321 
16322 unmap:
16323 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16324 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16325 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16326 		free_irq(pf->pdev->irq, pf);
16327 
16328 	/* shutdown the adminq */
16329 	i40e_shutdown_adminq(hw);
16330 
16331 	/* destroy the locks only once, here */
16332 	mutex_destroy(&hw->aq.arq_mutex);
16333 	mutex_destroy(&hw->aq.asq_mutex);
16334 
16335 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16336 	rtnl_lock();
16337 	i40e_clear_interrupt_scheme(pf);
16338 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16339 		if (pf->vsi[i]) {
16340 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16341 				i40e_vsi_clear_rings(pf->vsi[i]);
16342 			i40e_vsi_clear(pf->vsi[i]);
16343 			pf->vsi[i] = NULL;
16344 		}
16345 	}
16346 	rtnl_unlock();
16347 
16348 	for (i = 0; i < I40E_MAX_VEB; i++) {
16349 		kfree(pf->veb[i]);
16350 		pf->veb[i] = NULL;
16351 	}
16352 
16353 	kfree(pf->qp_pile);
16354 	kfree(pf->vsi);
16355 
16356 	iounmap(hw->hw_addr);
16357 	kfree(pf);
16358 	pci_release_mem_regions(pdev);
16359 
16360 	pci_disable_pcie_error_reporting(pdev);
16361 	pci_disable_device(pdev);
16362 }
16363 
16364 /**
16365  * i40e_pci_error_detected - warning that something funky happened in PCI land
16366  * @pdev: PCI device information struct
16367  * @error: the type of PCI error
16368  *
16369  * Called to warn that something happened and the error handling steps
16370  * are in progress.  Allows the driver to quiesce things, be ready for
16371  * remediation.
16372  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)16373 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16374 						pci_channel_state_t error)
16375 {
16376 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16377 
16378 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16379 
16380 	if (!pf) {
16381 		dev_info(&pdev->dev,
16382 			 "Cannot recover - error happened during device probe\n");
16383 		return PCI_ERS_RESULT_DISCONNECT;
16384 	}
16385 
16386 	/* shutdown all operations */
16387 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16388 		i40e_prep_for_reset(pf);
16389 
16390 	/* Request a slot reset */
16391 	return PCI_ERS_RESULT_NEED_RESET;
16392 }
16393 
16394 /**
16395  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16396  * @pdev: PCI device information struct
16397  *
16398  * Called to find if the driver can work with the device now that
16399  * the pci slot has been reset.  If a basic connection seems good
16400  * (registers are readable and have sane content) then return a
16401  * happy little PCI_ERS_RESULT_xxx.
16402  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)16403 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16404 {
16405 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16406 	pci_ers_result_t result;
16407 	u32 reg;
16408 
16409 	dev_dbg(&pdev->dev, "%s\n", __func__);
16410 	if (pci_enable_device_mem(pdev)) {
16411 		dev_info(&pdev->dev,
16412 			 "Cannot re-enable PCI device after reset.\n");
16413 		result = PCI_ERS_RESULT_DISCONNECT;
16414 	} else {
16415 		pci_set_master(pdev);
16416 		pci_restore_state(pdev);
16417 		pci_save_state(pdev);
16418 		pci_wake_from_d3(pdev, false);
16419 
16420 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16421 		if (reg == 0)
16422 			result = PCI_ERS_RESULT_RECOVERED;
16423 		else
16424 			result = PCI_ERS_RESULT_DISCONNECT;
16425 	}
16426 
16427 	return result;
16428 }
16429 
16430 /**
16431  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16432  * @pdev: PCI device information struct
16433  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)16434 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16435 {
16436 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16437 
16438 	i40e_prep_for_reset(pf);
16439 }
16440 
16441 /**
16442  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16443  * @pdev: PCI device information struct
16444  */
i40e_pci_error_reset_done(struct pci_dev * pdev)16445 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16446 {
16447 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16448 
16449 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16450 		return;
16451 
16452 	i40e_reset_and_rebuild(pf, false, false);
16453 #ifdef CONFIG_PCI_IOV
16454 	i40e_restore_all_vfs_msi_state(pdev);
16455 #endif /* CONFIG_PCI_IOV */
16456 }
16457 
16458 /**
16459  * i40e_pci_error_resume - restart operations after PCI error recovery
16460  * @pdev: PCI device information struct
16461  *
16462  * Called to allow the driver to bring things back up after PCI error
16463  * and/or reset recovery has finished.
16464  **/
i40e_pci_error_resume(struct pci_dev * pdev)16465 static void i40e_pci_error_resume(struct pci_dev *pdev)
16466 {
16467 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16468 
16469 	dev_dbg(&pdev->dev, "%s\n", __func__);
16470 	if (test_bit(__I40E_SUSPENDED, pf->state))
16471 		return;
16472 
16473 	i40e_handle_reset_warning(pf, false);
16474 }
16475 
16476 /**
16477  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16478  * using the mac_address_write admin q function
16479  * @pf: pointer to i40e_pf struct
16480  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)16481 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16482 {
16483 	struct i40e_hw *hw = &pf->hw;
16484 	u8 mac_addr[6];
16485 	u16 flags = 0;
16486 	int ret;
16487 
16488 	/* Get current MAC address in case it's an LAA */
16489 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16490 		ether_addr_copy(mac_addr,
16491 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16492 	} else {
16493 		dev_err(&pf->pdev->dev,
16494 			"Failed to retrieve MAC address; using default\n");
16495 		ether_addr_copy(mac_addr, hw->mac.addr);
16496 	}
16497 
16498 	/* The FW expects the mac address write cmd to first be called with
16499 	 * one of these flags before calling it again with the multicast
16500 	 * enable flags.
16501 	 */
16502 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16503 
16504 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16505 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16506 
16507 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16508 	if (ret) {
16509 		dev_err(&pf->pdev->dev,
16510 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16511 		return;
16512 	}
16513 
16514 	flags = I40E_AQC_MC_MAG_EN
16515 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16516 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16517 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16518 	if (ret)
16519 		dev_err(&pf->pdev->dev,
16520 			"Failed to enable Multicast Magic Packet wake up\n");
16521 }
16522 
16523 /**
16524  * i40e_shutdown - PCI callback for shutting down
16525  * @pdev: PCI device information struct
16526  **/
i40e_shutdown(struct pci_dev * pdev)16527 static void i40e_shutdown(struct pci_dev *pdev)
16528 {
16529 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16530 	struct i40e_hw *hw = &pf->hw;
16531 
16532 	set_bit(__I40E_SUSPENDED, pf->state);
16533 	set_bit(__I40E_DOWN, pf->state);
16534 
16535 	del_timer_sync(&pf->service_timer);
16536 	cancel_work_sync(&pf->service_task);
16537 	i40e_cloud_filter_exit(pf);
16538 	i40e_fdir_teardown(pf);
16539 
16540 	/* Client close must be called explicitly here because the timer
16541 	 * has been stopped.
16542 	 */
16543 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16544 
16545 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16546 		i40e_enable_mc_magic_wake(pf);
16547 
16548 	i40e_prep_for_reset(pf);
16549 
16550 	wr32(hw, I40E_PFPM_APM,
16551 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16552 	wr32(hw, I40E_PFPM_WUFC,
16553 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16554 
16555 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16556 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16557 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16558 		free_irq(pf->pdev->irq, pf);
16559 
16560 	/* Since we're going to destroy queues during the
16561 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16562 	 * whole section
16563 	 */
16564 	rtnl_lock();
16565 	i40e_clear_interrupt_scheme(pf);
16566 	rtnl_unlock();
16567 
16568 	if (system_state == SYSTEM_POWER_OFF) {
16569 		pci_wake_from_d3(pdev, pf->wol_en);
16570 		pci_set_power_state(pdev, PCI_D3hot);
16571 	}
16572 }
16573 
16574 /**
16575  * i40e_suspend - PM callback for moving to D3
16576  * @dev: generic device information structure
16577  **/
i40e_suspend(struct device * dev)16578 static int __maybe_unused i40e_suspend(struct device *dev)
16579 {
16580 	struct i40e_pf *pf = dev_get_drvdata(dev);
16581 	struct i40e_hw *hw = &pf->hw;
16582 
16583 	/* If we're already suspended, then there is nothing to do */
16584 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16585 		return 0;
16586 
16587 	set_bit(__I40E_DOWN, pf->state);
16588 
16589 	/* Ensure service task will not be running */
16590 	del_timer_sync(&pf->service_timer);
16591 	cancel_work_sync(&pf->service_task);
16592 
16593 	/* Client close must be called explicitly here because the timer
16594 	 * has been stopped.
16595 	 */
16596 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16597 
16598 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16599 		i40e_enable_mc_magic_wake(pf);
16600 
16601 	/* Since we're going to destroy queues during the
16602 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16603 	 * whole section
16604 	 */
16605 	rtnl_lock();
16606 
16607 	i40e_prep_for_reset(pf);
16608 
16609 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16610 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16611 
16612 	/* Clear the interrupt scheme and release our IRQs so that the system
16613 	 * can safely hibernate even when there are a large number of CPUs.
16614 	 * Otherwise hibernation might fail when mapping all the vectors back
16615 	 * to CPU0.
16616 	 */
16617 	i40e_clear_interrupt_scheme(pf);
16618 
16619 	rtnl_unlock();
16620 
16621 	return 0;
16622 }
16623 
16624 /**
16625  * i40e_resume - PM callback for waking up from D3
16626  * @dev: generic device information structure
16627  **/
i40e_resume(struct device * dev)16628 static int __maybe_unused i40e_resume(struct device *dev)
16629 {
16630 	struct i40e_pf *pf = dev_get_drvdata(dev);
16631 	int err;
16632 
16633 	/* If we're not suspended, then there is nothing to do */
16634 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16635 		return 0;
16636 
16637 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16638 	 * since we're going to be restoring queues
16639 	 */
16640 	rtnl_lock();
16641 
16642 	/* We cleared the interrupt scheme when we suspended, so we need to
16643 	 * restore it now to resume device functionality.
16644 	 */
16645 	err = i40e_restore_interrupt_scheme(pf);
16646 	if (err) {
16647 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16648 			err);
16649 	}
16650 
16651 	clear_bit(__I40E_DOWN, pf->state);
16652 	i40e_reset_and_rebuild(pf, false, true);
16653 
16654 	rtnl_unlock();
16655 
16656 	/* Clear suspended state last after everything is recovered */
16657 	clear_bit(__I40E_SUSPENDED, pf->state);
16658 
16659 	/* Restart the service task */
16660 	mod_timer(&pf->service_timer,
16661 		  round_jiffies(jiffies + pf->service_timer_period));
16662 
16663 	return 0;
16664 }
16665 
16666 static const struct pci_error_handlers i40e_err_handler = {
16667 	.error_detected = i40e_pci_error_detected,
16668 	.slot_reset = i40e_pci_error_slot_reset,
16669 	.reset_prepare = i40e_pci_error_reset_prepare,
16670 	.reset_done = i40e_pci_error_reset_done,
16671 	.resume = i40e_pci_error_resume,
16672 };
16673 
16674 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16675 
16676 static struct pci_driver i40e_driver = {
16677 	.name     = i40e_driver_name,
16678 	.id_table = i40e_pci_tbl,
16679 	.probe    = i40e_probe,
16680 	.remove   = i40e_remove,
16681 	.driver   = {
16682 		.pm = &i40e_pm_ops,
16683 	},
16684 	.shutdown = i40e_shutdown,
16685 	.err_handler = &i40e_err_handler,
16686 	.sriov_configure = i40e_pci_sriov_configure,
16687 };
16688 
16689 /**
16690  * i40e_init_module - Driver registration routine
16691  *
16692  * i40e_init_module is the first routine called when the driver is
16693  * loaded. All it does is register with the PCI subsystem.
16694  **/
i40e_init_module(void)16695 static int __init i40e_init_module(void)
16696 {
16697 	int err;
16698 
16699 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16700 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16701 
16702 	/* There is no need to throttle the number of active tasks because
16703 	 * each device limits its own task using a state bit for scheduling
16704 	 * the service task, and the device tasks do not interfere with each
16705 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16706 	 * since we need to be able to guarantee forward progress even under
16707 	 * memory pressure.
16708 	 */
16709 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16710 	if (!i40e_wq) {
16711 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16712 		return -ENOMEM;
16713 	}
16714 
16715 	i40e_dbg_init();
16716 	err = pci_register_driver(&i40e_driver);
16717 	if (err) {
16718 		destroy_workqueue(i40e_wq);
16719 		i40e_dbg_exit();
16720 		return err;
16721 	}
16722 
16723 	return 0;
16724 }
16725 module_init(i40e_init_module);
16726 
16727 /**
16728  * i40e_exit_module - Driver exit cleanup routine
16729  *
16730  * i40e_exit_module is called just before the driver is removed
16731  * from memory.
16732  **/
i40e_exit_module(void)16733 static void __exit i40e_exit_module(void)
16734 {
16735 	pci_unregister_driver(&i40e_driver);
16736 	destroy_workqueue(i40e_wq);
16737 	ida_destroy(&i40e_client_ida);
16738 	i40e_dbg_exit();
16739 }
16740 module_exit(i40e_exit_module);
16741