1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
13 #include "i40e_diag.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
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 "Intel(R) Ethernet Connection XL710 Network Driver";
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
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,
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);
54 /* i40e_pci_tbl - PCI Device ID Table
56 * Last entry must be all 0s
58 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59 * Class, Class Mask, private data (not used) }
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 */
91 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
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)");
99 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
100 MODULE_LICENSE("GPL v2");
102 static struct workqueue_struct *i40e_wq;
104 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
105 struct net_device *netdev, int delta)
107 struct netdev_hw_addr *ha;
112 netdev_for_each_mc_addr(ha, netdev) {
113 if (ether_addr_equal(ha->addr, f->macaddr)) {
114 ha->refcount += delta;
115 if (ha->refcount <= 0)
123 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
124 * @hw: pointer to the HW structure
125 * @mem: ptr to mem struct to fill out
126 * @size: size of memory requested
127 * @alignment: what to align the allocation to
129 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
130 u64 size, u32 alignment)
132 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
134 mem->size = ALIGN(size, alignment);
135 mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
144 * i40e_free_dma_mem_d - OS specific memory free for shared code
145 * @hw: pointer to the HW structure
146 * @mem: ptr to mem struct to free
148 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
150 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
152 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
161 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
162 * @hw: pointer to the HW structure
163 * @mem: ptr to mem struct to fill out
164 * @size: size of memory requested
166 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
170 mem->va = kzalloc(size, GFP_KERNEL);
179 * i40e_free_virt_mem_d - OS specific memory free for shared code
180 * @hw: pointer to the HW structure
181 * @mem: ptr to mem struct to free
183 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
185 /* it's ok to kfree a NULL pointer */
194 * i40e_get_lump - find a lump of free generic resource
195 * @pf: board private structure
196 * @pile: the pile of resource to search
197 * @needed: the number of items needed
198 * @id: an owner id to stick on the items assigned
200 * Returns the base item index of the lump, or negative for error
202 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
208 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
209 dev_info(&pf->pdev->dev,
210 "param err: pile=%s needed=%d id=0x%04x\n",
211 pile ? "<valid>" : "<null>", needed, id);
215 /* Allocate last queue in the pile for FDIR VSI queue
216 * so it doesn't fragment the qp_pile
218 if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
219 if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
220 dev_err(&pf->pdev->dev,
221 "Cannot allocate queue %d for I40E_VSI_FDIR\n",
222 pile->num_entries - 1);
225 pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
226 return pile->num_entries - 1;
230 while (i < pile->num_entries) {
231 /* skip already allocated entries */
232 if (pile->list[i] & I40E_PILE_VALID_BIT) {
237 /* do we have enough in this lump? */
238 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
239 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
244 /* there was enough, so assign it to the requestor */
245 for (j = 0; j < needed; j++)
246 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
251 /* not enough, so skip over it and continue looking */
259 * i40e_put_lump - return a lump of generic resource
260 * @pile: the pile of resource to search
261 * @index: the base item index
262 * @id: the owner id of the items assigned
264 * Returns the count of items in the lump
266 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
268 int valid_id = (id | I40E_PILE_VALID_BIT);
272 if (!pile || index >= pile->num_entries)
276 i < pile->num_entries && pile->list[i] == valid_id;
287 * i40e_find_vsi_from_id - searches for the vsi with the given id
288 * @pf: the pf structure to search for the vsi
289 * @id: id of the vsi it is searching for
291 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
295 for (i = 0; i < pf->num_alloc_vsi; i++)
296 if (pf->vsi[i] && (pf->vsi[i]->id == id))
303 * i40e_service_event_schedule - Schedule the service task to wake up
304 * @pf: board private structure
306 * If not already scheduled, this puts the task into the work queue
308 void i40e_service_event_schedule(struct i40e_pf *pf)
310 if ((!test_bit(__I40E_DOWN, pf->state) &&
311 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
312 test_bit(__I40E_RECOVERY_MODE, pf->state))
313 queue_work(i40e_wq, &pf->service_task);
317 * i40e_tx_timeout - Respond to a Tx Hang
318 * @netdev: network interface device structure
319 * @txqueue: queue number timing out
321 * If any port has noticed a Tx timeout, it is likely that the whole
322 * device is munged, not just the one netdev port, so go for the full
325 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
327 struct i40e_netdev_priv *np = netdev_priv(netdev);
328 struct i40e_vsi *vsi = np->vsi;
329 struct i40e_pf *pf = vsi->back;
330 struct i40e_ring *tx_ring = NULL;
334 pf->tx_timeout_count++;
336 /* with txqueue index, find the tx_ring struct */
337 for (i = 0; i < vsi->num_queue_pairs; i++) {
338 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
340 vsi->tx_rings[i]->queue_index) {
341 tx_ring = vsi->tx_rings[i];
347 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
348 pf->tx_timeout_recovery_level = 1; /* reset after some time */
349 else if (time_before(jiffies,
350 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 return; /* don't do any new action before the next timeout */
353 /* don't kick off another recovery if one is already pending */
354 if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
358 head = i40e_get_head(tx_ring);
359 /* Read interrupt register */
360 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
362 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
363 tx_ring->vsi->base_vector - 1));
365 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
367 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",
368 vsi->seid, txqueue, tx_ring->next_to_clean,
369 head, tx_ring->next_to_use,
370 readl(tx_ring->tail), val);
373 pf->tx_timeout_last_recovery = jiffies;
374 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
375 pf->tx_timeout_recovery_level, txqueue);
377 switch (pf->tx_timeout_recovery_level) {
379 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
382 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
385 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
388 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
389 set_bit(__I40E_DOWN_REQUESTED, pf->state);
390 set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
394 i40e_service_event_schedule(pf);
395 pf->tx_timeout_recovery_level++;
399 * i40e_get_vsi_stats_struct - Get System Network Statistics
400 * @vsi: the VSI we care about
402 * Returns the address of the device statistics structure.
403 * The statistics are actually updated from the service task.
405 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
407 return &vsi->net_stats;
411 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412 * @ring: Tx ring to get statistics from
413 * @stats: statistics entry to be updated
415 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 struct rtnl_link_stats64 *stats)
422 start = u64_stats_fetch_begin(&ring->syncp);
423 packets = ring->stats.packets;
424 bytes = ring->stats.bytes;
425 } while (u64_stats_fetch_retry(&ring->syncp, start));
427 stats->tx_packets += packets;
428 stats->tx_bytes += bytes;
432 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433 * @netdev: network interface device structure
434 * @stats: data structure to store statistics
436 * Returns the address of the device statistics structure.
437 * The statistics are actually updated from the service task.
439 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
440 struct rtnl_link_stats64 *stats)
442 struct i40e_netdev_priv *np = netdev_priv(netdev);
443 struct i40e_vsi *vsi = np->vsi;
444 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 struct i40e_ring *ring;
448 if (test_bit(__I40E_VSI_DOWN, vsi->state))
455 for (i = 0; i < vsi->num_queue_pairs; i++) {
459 ring = READ_ONCE(vsi->tx_rings[i]);
462 i40e_get_netdev_stats_struct_tx(ring, stats);
464 if (i40e_enabled_xdp_vsi(vsi)) {
465 ring = READ_ONCE(vsi->xdp_rings[i]);
468 i40e_get_netdev_stats_struct_tx(ring, stats);
471 ring = READ_ONCE(vsi->rx_rings[i]);
475 start = u64_stats_fetch_begin(&ring->syncp);
476 packets = ring->stats.packets;
477 bytes = ring->stats.bytes;
478 } while (u64_stats_fetch_retry(&ring->syncp, start));
480 stats->rx_packets += packets;
481 stats->rx_bytes += bytes;
486 /* following stats updated by i40e_watchdog_subtask() */
487 stats->multicast = vsi_stats->multicast;
488 stats->tx_errors = vsi_stats->tx_errors;
489 stats->tx_dropped = vsi_stats->tx_dropped;
490 stats->rx_errors = vsi_stats->rx_errors;
491 stats->rx_dropped = vsi_stats->rx_dropped;
492 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
493 stats->rx_length_errors = vsi_stats->rx_length_errors;
497 * i40e_vsi_reset_stats - Resets all stats of the given vsi
498 * @vsi: the VSI to have its stats reset
500 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
502 struct rtnl_link_stats64 *ns;
508 ns = i40e_get_vsi_stats_struct(vsi);
509 memset(ns, 0, sizeof(*ns));
510 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
511 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
512 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
513 if (vsi->rx_rings && vsi->rx_rings[0]) {
514 for (i = 0; i < vsi->num_queue_pairs; i++) {
515 memset(&vsi->rx_rings[i]->stats, 0,
516 sizeof(vsi->rx_rings[i]->stats));
517 memset(&vsi->rx_rings[i]->rx_stats, 0,
518 sizeof(vsi->rx_rings[i]->rx_stats));
519 memset(&vsi->tx_rings[i]->stats, 0,
520 sizeof(vsi->tx_rings[i]->stats));
521 memset(&vsi->tx_rings[i]->tx_stats, 0,
522 sizeof(vsi->tx_rings[i]->tx_stats));
525 vsi->stat_offsets_loaded = false;
529 * i40e_pf_reset_stats - Reset all of the stats for the given PF
530 * @pf: the PF to be reset
532 void i40e_pf_reset_stats(struct i40e_pf *pf)
536 memset(&pf->stats, 0, sizeof(pf->stats));
537 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
538 pf->stat_offsets_loaded = false;
540 for (i = 0; i < I40E_MAX_VEB; i++) {
542 memset(&pf->veb[i]->stats, 0,
543 sizeof(pf->veb[i]->stats));
544 memset(&pf->veb[i]->stats_offsets, 0,
545 sizeof(pf->veb[i]->stats_offsets));
546 memset(&pf->veb[i]->tc_stats, 0,
547 sizeof(pf->veb[i]->tc_stats));
548 memset(&pf->veb[i]->tc_stats_offsets, 0,
549 sizeof(pf->veb[i]->tc_stats_offsets));
550 pf->veb[i]->stat_offsets_loaded = false;
553 pf->hw_csum_rx_error = 0;
557 * i40e_compute_pci_to_hw_id - compute index form PCI function.
558 * @vsi: ptr to the VSI to read from.
559 * @hw: ptr to the hardware info.
561 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
563 int pf_count = i40e_get_pf_count(hw);
565 if (vsi->type == I40E_VSI_SRIOV)
566 return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
568 return hw->port + BIT(7);
572 * i40e_stat_update64 - read and update a 64 bit stat from the chip.
573 * @hw: ptr to the hardware info.
574 * @hireg: the high 32 bit reg to read.
575 * @loreg: the low 32 bit reg to read.
576 * @offset_loaded: has the initial offset been loaded yet.
577 * @offset: ptr to current offset value.
578 * @stat: ptr to the stat.
580 * Since the device stats are not reset at PFReset, they will not
581 * be zeroed when the driver starts. We'll save the first values read
582 * and use them as offsets to be subtracted from the raw values in order
583 * to report stats that count from zero.
585 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
586 bool offset_loaded, u64 *offset, u64 *stat)
590 new_data = rd64(hw, loreg);
592 if (!offset_loaded || new_data < *offset)
594 *stat = new_data - *offset;
598 * i40e_stat_update48 - read and update a 48 bit stat from the chip
599 * @hw: ptr to the hardware info
600 * @hireg: the high 32 bit reg to read
601 * @loreg: the low 32 bit reg to read
602 * @offset_loaded: has the initial offset been loaded yet
603 * @offset: ptr to current offset value
604 * @stat: ptr to the stat
606 * Since the device stats are not reset at PFReset, they likely will not
607 * be zeroed when the driver starts. We'll save the first values read
608 * and use them as offsets to be subtracted from the raw values in order
609 * to report stats that count from zero. In the process, we also manage
610 * the potential roll-over.
612 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
613 bool offset_loaded, u64 *offset, u64 *stat)
617 if (hw->device_id == I40E_DEV_ID_QEMU) {
618 new_data = rd32(hw, loreg);
619 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
621 new_data = rd64(hw, loreg);
625 if (likely(new_data >= *offset))
626 *stat = new_data - *offset;
628 *stat = (new_data + BIT_ULL(48)) - *offset;
629 *stat &= 0xFFFFFFFFFFFFULL;
633 * i40e_stat_update32 - read and update a 32 bit stat from the chip
634 * @hw: ptr to the hardware info
635 * @reg: the hw reg to read
636 * @offset_loaded: has the initial offset been loaded yet
637 * @offset: ptr to current offset value
638 * @stat: ptr to the stat
640 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
641 bool offset_loaded, u64 *offset, u64 *stat)
645 new_data = rd32(hw, reg);
648 if (likely(new_data >= *offset))
649 *stat = (u32)(new_data - *offset);
651 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
655 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
656 * @hw: ptr to the hardware info
657 * @reg: the hw reg to read and clear
658 * @stat: ptr to the stat
660 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
662 u32 new_data = rd32(hw, reg);
664 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
669 * i40e_stats_update_rx_discards - update rx_discards.
670 * @vsi: ptr to the VSI to be updated.
671 * @hw: ptr to the hardware info.
672 * @stat_idx: VSI's stat_counter_idx.
673 * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
674 * @stat_offset: ptr to stat_offset to store first read of specific register.
675 * @stat: ptr to VSI's stat to be updated.
678 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
679 int stat_idx, bool offset_loaded,
680 struct i40e_eth_stats *stat_offset,
681 struct i40e_eth_stats *stat)
683 u64 rx_rdpc, rx_rxerr;
685 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
686 &stat_offset->rx_discards, &rx_rdpc);
687 i40e_stat_update64(hw,
688 I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
689 I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
690 offset_loaded, &stat_offset->rx_discards_other,
693 stat->rx_discards = rx_rdpc + rx_rxerr;
697 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
698 * @vsi: the VSI to be updated
700 void i40e_update_eth_stats(struct i40e_vsi *vsi)
702 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
703 struct i40e_pf *pf = vsi->back;
704 struct i40e_hw *hw = &pf->hw;
705 struct i40e_eth_stats *oes;
706 struct i40e_eth_stats *es; /* device's eth stats */
708 es = &vsi->eth_stats;
709 oes = &vsi->eth_stats_offsets;
711 /* Gather up the stats that the hw collects */
712 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
713 vsi->stat_offsets_loaded,
714 &oes->tx_errors, &es->tx_errors);
715 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
716 vsi->stat_offsets_loaded,
717 &oes->rx_discards, &es->rx_discards);
718 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
719 vsi->stat_offsets_loaded,
720 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
722 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
723 I40E_GLV_GORCL(stat_idx),
724 vsi->stat_offsets_loaded,
725 &oes->rx_bytes, &es->rx_bytes);
726 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
727 I40E_GLV_UPRCL(stat_idx),
728 vsi->stat_offsets_loaded,
729 &oes->rx_unicast, &es->rx_unicast);
730 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
731 I40E_GLV_MPRCL(stat_idx),
732 vsi->stat_offsets_loaded,
733 &oes->rx_multicast, &es->rx_multicast);
734 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
735 I40E_GLV_BPRCL(stat_idx),
736 vsi->stat_offsets_loaded,
737 &oes->rx_broadcast, &es->rx_broadcast);
739 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
740 I40E_GLV_GOTCL(stat_idx),
741 vsi->stat_offsets_loaded,
742 &oes->tx_bytes, &es->tx_bytes);
743 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
744 I40E_GLV_UPTCL(stat_idx),
745 vsi->stat_offsets_loaded,
746 &oes->tx_unicast, &es->tx_unicast);
747 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
748 I40E_GLV_MPTCL(stat_idx),
749 vsi->stat_offsets_loaded,
750 &oes->tx_multicast, &es->tx_multicast);
751 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
752 I40E_GLV_BPTCL(stat_idx),
753 vsi->stat_offsets_loaded,
754 &oes->tx_broadcast, &es->tx_broadcast);
756 i40e_stats_update_rx_discards(vsi, hw, stat_idx,
757 vsi->stat_offsets_loaded, oes, es);
759 vsi->stat_offsets_loaded = true;
763 * i40e_update_veb_stats - Update Switch component statistics
764 * @veb: the VEB being updated
766 void i40e_update_veb_stats(struct i40e_veb *veb)
768 struct i40e_pf *pf = veb->pf;
769 struct i40e_hw *hw = &pf->hw;
770 struct i40e_eth_stats *oes;
771 struct i40e_eth_stats *es; /* device's eth stats */
772 struct i40e_veb_tc_stats *veb_oes;
773 struct i40e_veb_tc_stats *veb_es;
776 idx = veb->stats_idx;
778 oes = &veb->stats_offsets;
779 veb_es = &veb->tc_stats;
780 veb_oes = &veb->tc_stats_offsets;
782 /* Gather up the stats that the hw collects */
783 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
784 veb->stat_offsets_loaded,
785 &oes->tx_discards, &es->tx_discards);
786 if (hw->revision_id > 0)
787 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
788 veb->stat_offsets_loaded,
789 &oes->rx_unknown_protocol,
790 &es->rx_unknown_protocol);
791 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
792 veb->stat_offsets_loaded,
793 &oes->rx_bytes, &es->rx_bytes);
794 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
795 veb->stat_offsets_loaded,
796 &oes->rx_unicast, &es->rx_unicast);
797 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
798 veb->stat_offsets_loaded,
799 &oes->rx_multicast, &es->rx_multicast);
800 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
801 veb->stat_offsets_loaded,
802 &oes->rx_broadcast, &es->rx_broadcast);
804 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
805 veb->stat_offsets_loaded,
806 &oes->tx_bytes, &es->tx_bytes);
807 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
808 veb->stat_offsets_loaded,
809 &oes->tx_unicast, &es->tx_unicast);
810 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
811 veb->stat_offsets_loaded,
812 &oes->tx_multicast, &es->tx_multicast);
813 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
814 veb->stat_offsets_loaded,
815 &oes->tx_broadcast, &es->tx_broadcast);
816 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
817 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
818 I40E_GLVEBTC_RPCL(i, idx),
819 veb->stat_offsets_loaded,
820 &veb_oes->tc_rx_packets[i],
821 &veb_es->tc_rx_packets[i]);
822 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
823 I40E_GLVEBTC_RBCL(i, idx),
824 veb->stat_offsets_loaded,
825 &veb_oes->tc_rx_bytes[i],
826 &veb_es->tc_rx_bytes[i]);
827 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
828 I40E_GLVEBTC_TPCL(i, idx),
829 veb->stat_offsets_loaded,
830 &veb_oes->tc_tx_packets[i],
831 &veb_es->tc_tx_packets[i]);
832 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
833 I40E_GLVEBTC_TBCL(i, idx),
834 veb->stat_offsets_loaded,
835 &veb_oes->tc_tx_bytes[i],
836 &veb_es->tc_tx_bytes[i]);
838 veb->stat_offsets_loaded = true;
842 * i40e_update_vsi_stats - Update the vsi statistics counters.
843 * @vsi: the VSI to be updated
845 * There are a few instances where we store the same stat in a
846 * couple of different structs. This is partly because we have
847 * the netdev stats that need to be filled out, which is slightly
848 * different from the "eth_stats" defined by the chip and used in
849 * VF communications. We sort it out here.
851 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
853 u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
854 struct i40e_pf *pf = vsi->back;
855 struct rtnl_link_stats64 *ons;
856 struct rtnl_link_stats64 *ns; /* netdev stats */
857 struct i40e_eth_stats *oes;
858 struct i40e_eth_stats *es; /* device's eth stats */
859 u64 tx_restart, tx_busy;
870 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
871 test_bit(__I40E_CONFIG_BUSY, pf->state))
874 ns = i40e_get_vsi_stats_struct(vsi);
875 ons = &vsi->net_stats_offsets;
876 es = &vsi->eth_stats;
877 oes = &vsi->eth_stats_offsets;
879 /* Gather up the netdev and vsi stats that the driver collects
880 * on the fly during packet processing
884 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
893 for (q = 0; q < vsi->num_queue_pairs; q++) {
895 p = READ_ONCE(vsi->tx_rings[q]);
900 start = u64_stats_fetch_begin(&p->syncp);
901 packets = p->stats.packets;
902 bytes = p->stats.bytes;
903 } while (u64_stats_fetch_retry(&p->syncp, start));
906 tx_restart += p->tx_stats.restart_queue;
907 tx_busy += p->tx_stats.tx_busy;
908 tx_linearize += p->tx_stats.tx_linearize;
909 tx_force_wb += p->tx_stats.tx_force_wb;
910 tx_stopped += p->tx_stats.tx_stopped;
913 p = READ_ONCE(vsi->rx_rings[q]);
918 start = u64_stats_fetch_begin(&p->syncp);
919 packets = p->stats.packets;
920 bytes = p->stats.bytes;
921 } while (u64_stats_fetch_retry(&p->syncp, start));
924 rx_buf += p->rx_stats.alloc_buff_failed;
925 rx_page += p->rx_stats.alloc_page_failed;
926 rx_reuse += p->rx_stats.page_reuse_count;
927 rx_alloc += p->rx_stats.page_alloc_count;
928 rx_waive += p->rx_stats.page_waive_count;
929 rx_busy += p->rx_stats.page_busy_count;
931 if (i40e_enabled_xdp_vsi(vsi)) {
932 /* locate XDP ring */
933 p = READ_ONCE(vsi->xdp_rings[q]);
938 start = u64_stats_fetch_begin(&p->syncp);
939 packets = p->stats.packets;
940 bytes = p->stats.bytes;
941 } while (u64_stats_fetch_retry(&p->syncp, start));
944 tx_restart += p->tx_stats.restart_queue;
945 tx_busy += p->tx_stats.tx_busy;
946 tx_linearize += p->tx_stats.tx_linearize;
947 tx_force_wb += p->tx_stats.tx_force_wb;
951 vsi->tx_restart = tx_restart;
952 vsi->tx_busy = tx_busy;
953 vsi->tx_linearize = tx_linearize;
954 vsi->tx_force_wb = tx_force_wb;
955 vsi->tx_stopped = tx_stopped;
956 vsi->rx_page_failed = rx_page;
957 vsi->rx_buf_failed = rx_buf;
958 vsi->rx_page_reuse = rx_reuse;
959 vsi->rx_page_alloc = rx_alloc;
960 vsi->rx_page_waive = rx_waive;
961 vsi->rx_page_busy = rx_busy;
963 ns->rx_packets = rx_p;
965 ns->tx_packets = tx_p;
968 /* update netdev stats from eth stats */
969 i40e_update_eth_stats(vsi);
970 ons->tx_errors = oes->tx_errors;
971 ns->tx_errors = es->tx_errors;
972 ons->multicast = oes->rx_multicast;
973 ns->multicast = es->rx_multicast;
974 ons->rx_dropped = oes->rx_discards;
975 ns->rx_dropped = es->rx_discards;
976 ons->tx_dropped = oes->tx_discards;
977 ns->tx_dropped = es->tx_discards;
979 /* pull in a couple PF stats if this is the main vsi */
980 if (vsi == pf->vsi[pf->lan_vsi]) {
981 ns->rx_crc_errors = pf->stats.crc_errors;
982 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
983 ns->rx_length_errors = pf->stats.rx_length_errors;
988 * i40e_update_pf_stats - Update the PF statistics counters.
989 * @pf: the PF to be updated
991 static void i40e_update_pf_stats(struct i40e_pf *pf)
993 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
994 struct i40e_hw_port_stats *nsd = &pf->stats;
995 struct i40e_hw *hw = &pf->hw;
999 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1000 I40E_GLPRT_GORCL(hw->port),
1001 pf->stat_offsets_loaded,
1002 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1003 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1004 I40E_GLPRT_GOTCL(hw->port),
1005 pf->stat_offsets_loaded,
1006 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1007 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1008 pf->stat_offsets_loaded,
1009 &osd->eth.rx_discards,
1010 &nsd->eth.rx_discards);
1011 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1012 I40E_GLPRT_UPRCL(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->eth.rx_unicast,
1015 &nsd->eth.rx_unicast);
1016 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1017 I40E_GLPRT_MPRCL(hw->port),
1018 pf->stat_offsets_loaded,
1019 &osd->eth.rx_multicast,
1020 &nsd->eth.rx_multicast);
1021 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1022 I40E_GLPRT_BPRCL(hw->port),
1023 pf->stat_offsets_loaded,
1024 &osd->eth.rx_broadcast,
1025 &nsd->eth.rx_broadcast);
1026 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1027 I40E_GLPRT_UPTCL(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->eth.tx_unicast,
1030 &nsd->eth.tx_unicast);
1031 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1032 I40E_GLPRT_MPTCL(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->eth.tx_multicast,
1035 &nsd->eth.tx_multicast);
1036 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1037 I40E_GLPRT_BPTCL(hw->port),
1038 pf->stat_offsets_loaded,
1039 &osd->eth.tx_broadcast,
1040 &nsd->eth.tx_broadcast);
1042 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1043 pf->stat_offsets_loaded,
1044 &osd->tx_dropped_link_down,
1045 &nsd->tx_dropped_link_down);
1047 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1048 pf->stat_offsets_loaded,
1049 &osd->crc_errors, &nsd->crc_errors);
1051 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1052 pf->stat_offsets_loaded,
1053 &osd->illegal_bytes, &nsd->illegal_bytes);
1055 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->mac_local_faults,
1058 &nsd->mac_local_faults);
1059 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1060 pf->stat_offsets_loaded,
1061 &osd->mac_remote_faults,
1062 &nsd->mac_remote_faults);
1064 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1065 pf->stat_offsets_loaded,
1066 &osd->rx_length_errors,
1067 &nsd->rx_length_errors);
1069 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->link_xon_rx, &nsd->link_xon_rx);
1072 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->link_xon_tx, &nsd->link_xon_tx);
1075 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->link_xoff_rx, &nsd->link_xoff_rx);
1078 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->link_xoff_tx, &nsd->link_xoff_tx);
1082 for (i = 0; i < 8; i++) {
1083 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1084 pf->stat_offsets_loaded,
1085 &osd->priority_xoff_rx[i],
1086 &nsd->priority_xoff_rx[i]);
1087 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1088 pf->stat_offsets_loaded,
1089 &osd->priority_xon_rx[i],
1090 &nsd->priority_xon_rx[i]);
1091 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1092 pf->stat_offsets_loaded,
1093 &osd->priority_xon_tx[i],
1094 &nsd->priority_xon_tx[i]);
1095 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1096 pf->stat_offsets_loaded,
1097 &osd->priority_xoff_tx[i],
1098 &nsd->priority_xoff_tx[i]);
1099 i40e_stat_update32(hw,
1100 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1101 pf->stat_offsets_loaded,
1102 &osd->priority_xon_2_xoff[i],
1103 &nsd->priority_xon_2_xoff[i]);
1106 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1107 I40E_GLPRT_PRC64L(hw->port),
1108 pf->stat_offsets_loaded,
1109 &osd->rx_size_64, &nsd->rx_size_64);
1110 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1111 I40E_GLPRT_PRC127L(hw->port),
1112 pf->stat_offsets_loaded,
1113 &osd->rx_size_127, &nsd->rx_size_127);
1114 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1115 I40E_GLPRT_PRC255L(hw->port),
1116 pf->stat_offsets_loaded,
1117 &osd->rx_size_255, &nsd->rx_size_255);
1118 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1119 I40E_GLPRT_PRC511L(hw->port),
1120 pf->stat_offsets_loaded,
1121 &osd->rx_size_511, &nsd->rx_size_511);
1122 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1123 I40E_GLPRT_PRC1023L(hw->port),
1124 pf->stat_offsets_loaded,
1125 &osd->rx_size_1023, &nsd->rx_size_1023);
1126 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1127 I40E_GLPRT_PRC1522L(hw->port),
1128 pf->stat_offsets_loaded,
1129 &osd->rx_size_1522, &nsd->rx_size_1522);
1130 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1131 I40E_GLPRT_PRC9522L(hw->port),
1132 pf->stat_offsets_loaded,
1133 &osd->rx_size_big, &nsd->rx_size_big);
1135 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1136 I40E_GLPRT_PTC64L(hw->port),
1137 pf->stat_offsets_loaded,
1138 &osd->tx_size_64, &nsd->tx_size_64);
1139 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1140 I40E_GLPRT_PTC127L(hw->port),
1141 pf->stat_offsets_loaded,
1142 &osd->tx_size_127, &nsd->tx_size_127);
1143 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1144 I40E_GLPRT_PTC255L(hw->port),
1145 pf->stat_offsets_loaded,
1146 &osd->tx_size_255, &nsd->tx_size_255);
1147 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1148 I40E_GLPRT_PTC511L(hw->port),
1149 pf->stat_offsets_loaded,
1150 &osd->tx_size_511, &nsd->tx_size_511);
1151 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1152 I40E_GLPRT_PTC1023L(hw->port),
1153 pf->stat_offsets_loaded,
1154 &osd->tx_size_1023, &nsd->tx_size_1023);
1155 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1156 I40E_GLPRT_PTC1522L(hw->port),
1157 pf->stat_offsets_loaded,
1158 &osd->tx_size_1522, &nsd->tx_size_1522);
1159 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1160 I40E_GLPRT_PTC9522L(hw->port),
1161 pf->stat_offsets_loaded,
1162 &osd->tx_size_big, &nsd->tx_size_big);
1164 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1165 pf->stat_offsets_loaded,
1166 &osd->rx_undersize, &nsd->rx_undersize);
1167 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1168 pf->stat_offsets_loaded,
1169 &osd->rx_fragments, &nsd->rx_fragments);
1170 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1171 pf->stat_offsets_loaded,
1172 &osd->rx_oversize, &nsd->rx_oversize);
1173 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1174 pf->stat_offsets_loaded,
1175 &osd->rx_jabber, &nsd->rx_jabber);
1178 i40e_stat_update_and_clear32(hw,
1179 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1180 &nsd->fd_atr_match);
1181 i40e_stat_update_and_clear32(hw,
1182 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1184 i40e_stat_update_and_clear32(hw,
1185 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1186 &nsd->fd_atr_tunnel_match);
1188 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1189 nsd->tx_lpi_status =
1190 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1191 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1192 nsd->rx_lpi_status =
1193 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1194 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1195 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1196 pf->stat_offsets_loaded,
1197 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1198 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1199 pf->stat_offsets_loaded,
1200 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1202 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1203 !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1204 nsd->fd_sb_status = true;
1206 nsd->fd_sb_status = false;
1208 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1209 !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1210 nsd->fd_atr_status = true;
1212 nsd->fd_atr_status = false;
1214 pf->stat_offsets_loaded = true;
1218 * i40e_update_stats - Update the various statistics counters.
1219 * @vsi: the VSI to be updated
1221 * Update the various stats for this VSI and its related entities.
1223 void i40e_update_stats(struct i40e_vsi *vsi)
1225 struct i40e_pf *pf = vsi->back;
1227 if (vsi == pf->vsi[pf->lan_vsi])
1228 i40e_update_pf_stats(pf);
1230 i40e_update_vsi_stats(vsi);
1234 * i40e_count_filters - counts VSI mac filters
1235 * @vsi: the VSI to be searched
1237 * Returns count of mac filters
1239 int i40e_count_filters(struct i40e_vsi *vsi)
1241 struct i40e_mac_filter *f;
1242 struct hlist_node *h;
1246 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1253 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1254 * @vsi: the VSI to be searched
1255 * @macaddr: the MAC address
1258 * Returns ptr to the filter object or NULL
1260 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1261 const u8 *macaddr, s16 vlan)
1263 struct i40e_mac_filter *f;
1266 if (!vsi || !macaddr)
1269 key = i40e_addr_to_hkey(macaddr);
1270 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1271 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1279 * i40e_find_mac - Find a mac addr in the macvlan filters list
1280 * @vsi: the VSI to be searched
1281 * @macaddr: the MAC address we are searching for
1283 * Returns the first filter with the provided MAC address or NULL if
1284 * MAC address was not found
1286 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1288 struct i40e_mac_filter *f;
1291 if (!vsi || !macaddr)
1294 key = i40e_addr_to_hkey(macaddr);
1295 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1296 if ((ether_addr_equal(macaddr, f->macaddr)))
1303 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1304 * @vsi: the VSI to be searched
1306 * Returns true if VSI is in vlan mode or false otherwise
1308 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1310 /* If we have a PVID, always operate in VLAN mode */
1314 /* We need to operate in VLAN mode whenever we have any filters with
1315 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1316 * time, incurring search cost repeatedly. However, we can notice two
1319 * 1) the only place where we can gain a VLAN filter is in
1322 * 2) the only place where filters are actually removed is in
1323 * i40e_sync_filters_subtask.
1325 * Thus, we can simply use a boolean value, has_vlan_filters which we
1326 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1327 * we have to perform the full search after deleting filters in
1328 * i40e_sync_filters_subtask, but we already have to search
1329 * filters here and can perform the check at the same time. This
1330 * results in avoiding embedding a loop for VLAN mode inside another
1331 * loop over all the filters, and should maintain correctness as noted
1334 return vsi->has_vlan_filter;
1338 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1339 * @vsi: the VSI to configure
1340 * @tmp_add_list: list of filters ready to be added
1341 * @tmp_del_list: list of filters ready to be deleted
1342 * @vlan_filters: the number of active VLAN filters
1344 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1345 * behave as expected. If we have any active VLAN filters remaining or about
1346 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1347 * so that they only match against untagged traffic. If we no longer have any
1348 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1349 * so that they match against both tagged and untagged traffic. In this way,
1350 * we ensure that we correctly receive the desired traffic. This ensures that
1351 * when we have an active VLAN we will receive only untagged traffic and
1352 * traffic matching active VLANs. If we have no active VLANs then we will
1353 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1355 * Finally, in a similar fashion, this function also corrects filters when
1356 * there is an active PVID assigned to this VSI.
1358 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1360 * This function is only expected to be called from within
1361 * i40e_sync_vsi_filters.
1363 * NOTE: This function expects to be called while under the
1364 * mac_filter_hash_lock
1366 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1367 struct hlist_head *tmp_add_list,
1368 struct hlist_head *tmp_del_list,
1371 s16 pvid = le16_to_cpu(vsi->info.pvid);
1372 struct i40e_mac_filter *f, *add_head;
1373 struct i40e_new_mac_filter *new;
1374 struct hlist_node *h;
1377 /* To determine if a particular filter needs to be replaced we
1378 * have the three following conditions:
1380 * a) if we have a PVID assigned, then all filters which are
1381 * not marked as VLAN=PVID must be replaced with filters that
1383 * b) otherwise, if we have any active VLANS, all filters
1384 * which are marked as VLAN=-1 must be replaced with
1385 * filters marked as VLAN=0
1386 * c) finally, if we do not have any active VLANS, all filters
1387 * which are marked as VLAN=0 must be replaced with filters
1391 /* Update the filters about to be added in place */
1392 hlist_for_each_entry(new, tmp_add_list, hlist) {
1393 if (pvid && new->f->vlan != pvid)
1394 new->f->vlan = pvid;
1395 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1397 else if (!vlan_filters && new->f->vlan == 0)
1398 new->f->vlan = I40E_VLAN_ANY;
1401 /* Update the remaining active filters */
1402 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1403 /* Combine the checks for whether a filter needs to be changed
1404 * and then determine the new VLAN inside the if block, in
1405 * order to avoid duplicating code for adding the new filter
1406 * then deleting the old filter.
1408 if ((pvid && f->vlan != pvid) ||
1409 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1410 (!vlan_filters && f->vlan == 0)) {
1411 /* Determine the new vlan we will be adding */
1414 else if (vlan_filters)
1417 new_vlan = I40E_VLAN_ANY;
1419 /* Create the new filter */
1420 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1424 /* Create a temporary i40e_new_mac_filter */
1425 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1430 new->state = add_head->state;
1432 /* Add the new filter to the tmp list */
1433 hlist_add_head(&new->hlist, tmp_add_list);
1435 /* Put the original filter into the delete list */
1436 f->state = I40E_FILTER_REMOVE;
1437 hash_del(&f->hlist);
1438 hlist_add_head(&f->hlist, tmp_del_list);
1442 vsi->has_vlan_filter = !!vlan_filters;
1448 * i40e_get_vf_new_vlan - Get new vlan id on a vf
1449 * @vsi: the vsi to configure
1450 * @new_mac: new mac filter to be added
1451 * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1452 * @vlan_filters: the number of active VLAN filters
1453 * @trusted: flag if the VF is trusted
1455 * Get new VLAN id based on current VLAN filters, trust, PVID
1456 * and vf-vlan-prune-disable flag.
1458 * Returns the value of the new vlan filter or
1459 * the old value if no new filter is needed.
1461 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1462 struct i40e_new_mac_filter *new_mac,
1463 struct i40e_mac_filter *f,
1467 s16 pvid = le16_to_cpu(vsi->info.pvid);
1468 struct i40e_pf *pf = vsi->back;
1474 if (pvid && f->vlan != pvid)
1477 is_any = (trusted ||
1478 !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1480 if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1481 (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1482 (is_any && !vlan_filters && f->vlan == 0)) {
1484 return I40E_VLAN_ANY;
1493 * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1494 * @vsi: the vsi to configure
1495 * @tmp_add_list: list of filters ready to be added
1496 * @tmp_del_list: list of filters ready to be deleted
1497 * @vlan_filters: the number of active VLAN filters
1498 * @trusted: flag if the VF is trusted
1500 * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1501 * and vf-vlan-prune-disable flag.
1503 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1505 * This function is only expected to be called from within
1506 * i40e_sync_vsi_filters.
1508 * NOTE: This function expects to be called while under the
1509 * mac_filter_hash_lock
1511 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1512 struct hlist_head *tmp_add_list,
1513 struct hlist_head *tmp_del_list,
1517 struct i40e_mac_filter *f, *add_head;
1518 struct i40e_new_mac_filter *new_mac;
1519 struct hlist_node *h;
1522 hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1523 new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1524 vlan_filters, trusted);
1527 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1528 new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1530 if (new_vlan != f->vlan) {
1531 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1534 /* Create a temporary i40e_new_mac_filter */
1535 new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1538 new_mac->f = add_head;
1539 new_mac->state = add_head->state;
1541 /* Add the new filter to the tmp list */
1542 hlist_add_head(&new_mac->hlist, tmp_add_list);
1544 /* Put the original filter into the delete list */
1545 f->state = I40E_FILTER_REMOVE;
1546 hash_del(&f->hlist);
1547 hlist_add_head(&f->hlist, tmp_del_list);
1551 vsi->has_vlan_filter = !!vlan_filters;
1556 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1557 * @vsi: the PF Main VSI - inappropriate for any other VSI
1558 * @macaddr: the MAC address
1560 * Remove whatever filter the firmware set up so the driver can manage
1561 * its own filtering intelligently.
1563 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1565 struct i40e_aqc_remove_macvlan_element_data element;
1566 struct i40e_pf *pf = vsi->back;
1568 /* Only appropriate for the PF main VSI */
1569 if (vsi->type != I40E_VSI_MAIN)
1572 memset(&element, 0, sizeof(element));
1573 ether_addr_copy(element.mac_addr, macaddr);
1574 element.vlan_tag = 0;
1575 /* Ignore error returns, some firmware does it this way... */
1576 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1577 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1579 memset(&element, 0, sizeof(element));
1580 ether_addr_copy(element.mac_addr, macaddr);
1581 element.vlan_tag = 0;
1582 /* ...and some firmware does it this way. */
1583 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1584 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1585 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1589 * i40e_add_filter - Add a mac/vlan filter to the VSI
1590 * @vsi: the VSI to be searched
1591 * @macaddr: the MAC address
1594 * Returns ptr to the filter object or NULL when no memory available.
1596 * NOTE: This function is expected to be called with mac_filter_hash_lock
1599 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1600 const u8 *macaddr, s16 vlan)
1602 struct i40e_mac_filter *f;
1605 if (!vsi || !macaddr)
1608 f = i40e_find_filter(vsi, macaddr, vlan);
1610 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1614 /* Update the boolean indicating if we need to function in
1618 vsi->has_vlan_filter = true;
1620 ether_addr_copy(f->macaddr, macaddr);
1622 f->state = I40E_FILTER_NEW;
1623 INIT_HLIST_NODE(&f->hlist);
1625 key = i40e_addr_to_hkey(macaddr);
1626 hash_add(vsi->mac_filter_hash, &f->hlist, key);
1628 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1629 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1632 /* If we're asked to add a filter that has been marked for removal, it
1633 * is safe to simply restore it to active state. __i40e_del_filter
1634 * will have simply deleted any filters which were previously marked
1635 * NEW or FAILED, so if it is currently marked REMOVE it must have
1636 * previously been ACTIVE. Since we haven't yet run the sync filters
1637 * task, just restore this filter to the ACTIVE state so that the
1638 * sync task leaves it in place
1640 if (f->state == I40E_FILTER_REMOVE)
1641 f->state = I40E_FILTER_ACTIVE;
1647 * __i40e_del_filter - Remove a specific filter from the VSI
1648 * @vsi: VSI to remove from
1649 * @f: the filter to remove from the list
1651 * This function should be called instead of i40e_del_filter only if you know
1652 * the exact filter you will remove already, such as via i40e_find_filter or
1655 * NOTE: This function is expected to be called with mac_filter_hash_lock
1657 * ANOTHER NOTE: This function MUST be called from within the context of
1658 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1659 * instead of list_for_each_entry().
1661 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1666 /* If the filter was never added to firmware then we can just delete it
1667 * directly and we don't want to set the status to remove or else an
1668 * admin queue command will unnecessarily fire.
1670 if ((f->state == I40E_FILTER_FAILED) ||
1671 (f->state == I40E_FILTER_NEW)) {
1672 hash_del(&f->hlist);
1675 f->state = I40E_FILTER_REMOVE;
1678 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1679 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1683 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1684 * @vsi: the VSI to be searched
1685 * @macaddr: the MAC address
1688 * NOTE: This function is expected to be called with mac_filter_hash_lock
1690 * ANOTHER NOTE: This function MUST be called from within the context of
1691 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1692 * instead of list_for_each_entry().
1694 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1696 struct i40e_mac_filter *f;
1698 if (!vsi || !macaddr)
1701 f = i40e_find_filter(vsi, macaddr, vlan);
1702 __i40e_del_filter(vsi, f);
1706 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1707 * @vsi: the VSI to be searched
1708 * @macaddr: the mac address to be filtered
1710 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1711 * go through all the macvlan filters and add a macvlan filter for each
1712 * unique vlan that already exists. If a PVID has been assigned, instead only
1713 * add the macaddr to that VLAN.
1715 * Returns last filter added on success, else NULL
1717 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1720 struct i40e_mac_filter *f, *add = NULL;
1721 struct hlist_node *h;
1725 return i40e_add_filter(vsi, macaddr,
1726 le16_to_cpu(vsi->info.pvid));
1728 if (!i40e_is_vsi_in_vlan(vsi))
1729 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1731 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1732 if (f->state == I40E_FILTER_REMOVE)
1734 add = i40e_add_filter(vsi, macaddr, f->vlan);
1743 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1744 * @vsi: the VSI to be searched
1745 * @macaddr: the mac address to be removed
1747 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1750 * Returns 0 for success, or error
1752 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1754 struct i40e_mac_filter *f;
1755 struct hlist_node *h;
1759 lockdep_assert_held(&vsi->mac_filter_hash_lock);
1760 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1761 if (ether_addr_equal(macaddr, f->macaddr)) {
1762 __i40e_del_filter(vsi, f);
1774 * i40e_set_mac - NDO callback to set mac address
1775 * @netdev: network interface device structure
1776 * @p: pointer to an address structure
1778 * Returns 0 on success, negative on failure
1780 static int i40e_set_mac(struct net_device *netdev, void *p)
1782 struct i40e_netdev_priv *np = netdev_priv(netdev);
1783 struct i40e_vsi *vsi = np->vsi;
1784 struct i40e_pf *pf = vsi->back;
1785 struct i40e_hw *hw = &pf->hw;
1786 struct sockaddr *addr = p;
1788 if (!is_valid_ether_addr(addr->sa_data))
1789 return -EADDRNOTAVAIL;
1791 if (test_bit(__I40E_DOWN, pf->state) ||
1792 test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1793 return -EADDRNOTAVAIL;
1795 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1796 netdev_info(netdev, "returning to hw mac address %pM\n",
1799 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1801 /* Copy the address first, so that we avoid a possible race with
1803 * - Remove old address from MAC filter
1804 * - Copy new address
1805 * - Add new address to MAC filter
1807 spin_lock_bh(&vsi->mac_filter_hash_lock);
1808 i40e_del_mac_filter(vsi, netdev->dev_addr);
1809 eth_hw_addr_set(netdev, addr->sa_data);
1810 i40e_add_mac_filter(vsi, netdev->dev_addr);
1811 spin_unlock_bh(&vsi->mac_filter_hash_lock);
1813 if (vsi->type == I40E_VSI_MAIN) {
1816 ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1817 addr->sa_data, NULL);
1819 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1821 i40e_aq_str(hw, hw->aq.asq_last_status));
1824 /* schedule our worker thread which will take care of
1825 * applying the new filter changes
1827 i40e_service_event_schedule(pf);
1832 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1833 * @vsi: vsi structure
1834 * @seed: RSS hash seed
1835 * @lut: pointer to lookup table of lut_size
1836 * @lut_size: size of the lookup table
1838 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1839 u8 *lut, u16 lut_size)
1841 struct i40e_pf *pf = vsi->back;
1842 struct i40e_hw *hw = &pf->hw;
1846 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1847 (struct i40e_aqc_get_set_rss_key_data *)seed;
1848 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1850 dev_info(&pf->pdev->dev,
1851 "Cannot set RSS key, err %pe aq_err %s\n",
1853 i40e_aq_str(hw, hw->aq.asq_last_status));
1858 bool pf_lut = vsi->type == I40E_VSI_MAIN;
1860 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1862 dev_info(&pf->pdev->dev,
1863 "Cannot set RSS lut, err %pe aq_err %s\n",
1865 i40e_aq_str(hw, hw->aq.asq_last_status));
1873 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1874 * @vsi: VSI structure
1876 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1878 struct i40e_pf *pf = vsi->back;
1879 u8 seed[I40E_HKEY_ARRAY_SIZE];
1883 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1886 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1887 vsi->num_queue_pairs);
1890 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1894 /* Use the user configured hash keys and lookup table if there is one,
1895 * otherwise use default
1897 if (vsi->rss_lut_user)
1898 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1900 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1901 if (vsi->rss_hkey_user)
1902 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1904 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1905 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1911 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1912 * @vsi: the VSI being configured,
1913 * @ctxt: VSI context structure
1914 * @enabled_tc: number of traffic classes to enable
1916 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1918 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1919 struct i40e_vsi_context *ctxt,
1922 u16 qcount = 0, max_qcount, qmap, sections = 0;
1923 int i, override_q, pow, num_qps, ret;
1924 u8 netdev_tc = 0, offset = 0;
1926 if (vsi->type != I40E_VSI_MAIN)
1928 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1929 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1930 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1931 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1932 num_qps = vsi->mqprio_qopt.qopt.count[0];
1934 /* find the next higher power-of-2 of num queue pairs */
1935 pow = ilog2(num_qps);
1936 if (!is_power_of_2(num_qps))
1938 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1939 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1941 /* Setup queue offset/count for all TCs for given VSI */
1942 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1943 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1944 /* See if the given TC is enabled for the given VSI */
1945 if (vsi->tc_config.enabled_tc & BIT(i)) {
1946 offset = vsi->mqprio_qopt.qopt.offset[i];
1947 qcount = vsi->mqprio_qopt.qopt.count[i];
1948 if (qcount > max_qcount)
1949 max_qcount = qcount;
1950 vsi->tc_config.tc_info[i].qoffset = offset;
1951 vsi->tc_config.tc_info[i].qcount = qcount;
1952 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1954 /* TC is not enabled so set the offset to
1955 * default queue and allocate one queue
1958 vsi->tc_config.tc_info[i].qoffset = 0;
1959 vsi->tc_config.tc_info[i].qcount = 1;
1960 vsi->tc_config.tc_info[i].netdev_tc = 0;
1964 /* Set actual Tx/Rx queue pairs */
1965 vsi->num_queue_pairs = offset + qcount;
1967 /* Setup queue TC[0].qmap for given VSI context */
1968 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1969 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1970 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1971 ctxt->info.valid_sections |= cpu_to_le16(sections);
1973 /* Reconfigure RSS for main VSI with max queue count */
1974 vsi->rss_size = max_qcount;
1975 ret = i40e_vsi_config_rss(vsi);
1977 dev_info(&vsi->back->pdev->dev,
1978 "Failed to reconfig rss for num_queues (%u)\n",
1982 vsi->reconfig_rss = true;
1983 dev_dbg(&vsi->back->pdev->dev,
1984 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1986 /* Find queue count available for channel VSIs and starting offset
1989 override_q = vsi->mqprio_qopt.qopt.count[0];
1990 if (override_q && override_q < vsi->num_queue_pairs) {
1991 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1992 vsi->next_base_queue = override_q;
1998 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1999 * @vsi: the VSI being setup
2000 * @ctxt: VSI context structure
2001 * @enabled_tc: Enabled TCs bitmap
2002 * @is_add: True if called before Add VSI
2004 * Setup VSI queue mapping for enabled traffic classes.
2006 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2007 struct i40e_vsi_context *ctxt,
2011 struct i40e_pf *pf = vsi->back;
2021 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2023 /* zero out queue mapping, it will get updated on the end of the function */
2024 memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2026 if (vsi->type == I40E_VSI_MAIN) {
2027 /* This code helps add more queue to the VSI if we have
2028 * more cores than RSS can support, the higher cores will
2029 * be served by ATR or other filters. Furthermore, the
2030 * non-zero req_queue_pairs says that user requested a new
2031 * queue count via ethtool's set_channels, so use this
2032 * value for queues distribution across traffic classes
2033 * We need at least one queue pair for the interface
2034 * to be usable as we see in else statement.
2036 if (vsi->req_queue_pairs > 0)
2037 vsi->num_queue_pairs = vsi->req_queue_pairs;
2038 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2039 vsi->num_queue_pairs = pf->num_lan_msix;
2041 vsi->num_queue_pairs = 1;
2044 /* Number of queues per enabled TC */
2045 if (vsi->type == I40E_VSI_MAIN ||
2046 (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2047 num_tc_qps = vsi->num_queue_pairs;
2049 num_tc_qps = vsi->alloc_queue_pairs;
2051 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2052 /* Find numtc from enabled TC bitmap */
2053 for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2054 if (enabled_tc & BIT(i)) /* TC is enabled */
2058 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2061 num_tc_qps = num_tc_qps / numtc;
2062 num_tc_qps = min_t(int, num_tc_qps,
2063 i40e_pf_get_max_q_per_tc(pf));
2066 vsi->tc_config.numtc = numtc;
2067 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2069 /* Do not allow use more TC queue pairs than MSI-X vectors exist */
2070 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2071 num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2073 /* Setup queue offset/count for all TCs for given VSI */
2074 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2075 /* See if the given TC is enabled for the given VSI */
2076 if (vsi->tc_config.enabled_tc & BIT(i)) {
2080 switch (vsi->type) {
2082 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2083 I40E_FLAG_FD_ATR_ENABLED)) ||
2084 vsi->tc_config.enabled_tc != 1) {
2085 qcount = min_t(int, pf->alloc_rss_size,
2091 case I40E_VSI_SRIOV:
2092 case I40E_VSI_VMDQ2:
2094 qcount = num_tc_qps;
2098 vsi->tc_config.tc_info[i].qoffset = offset;
2099 vsi->tc_config.tc_info[i].qcount = qcount;
2101 /* find the next higher power-of-2 of num queue pairs */
2104 while (num_qps && (BIT_ULL(pow) < qcount)) {
2109 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2111 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2112 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2116 /* TC is not enabled so set the offset to
2117 * default queue and allocate one queue
2120 vsi->tc_config.tc_info[i].qoffset = 0;
2121 vsi->tc_config.tc_info[i].qcount = 1;
2122 vsi->tc_config.tc_info[i].netdev_tc = 0;
2126 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2128 /* Do not change previously set num_queue_pairs for PFs and VFs*/
2129 if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2130 (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2131 (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2132 vsi->num_queue_pairs = offset;
2134 /* Scheduler section valid can only be set for ADD VSI */
2136 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2138 ctxt->info.up_enable_bits = enabled_tc;
2140 if (vsi->type == I40E_VSI_SRIOV) {
2141 ctxt->info.mapping_flags |=
2142 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2143 for (i = 0; i < vsi->num_queue_pairs; i++)
2144 ctxt->info.queue_mapping[i] =
2145 cpu_to_le16(vsi->base_queue + i);
2147 ctxt->info.mapping_flags |=
2148 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2149 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2151 ctxt->info.valid_sections |= cpu_to_le16(sections);
2155 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2156 * @netdev: the netdevice
2157 * @addr: address to add
2159 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2160 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2162 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2164 struct i40e_netdev_priv *np = netdev_priv(netdev);
2165 struct i40e_vsi *vsi = np->vsi;
2167 if (i40e_add_mac_filter(vsi, addr))
2174 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2175 * @netdev: the netdevice
2176 * @addr: address to add
2178 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2179 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2181 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2183 struct i40e_netdev_priv *np = netdev_priv(netdev);
2184 struct i40e_vsi *vsi = np->vsi;
2186 /* Under some circumstances, we might receive a request to delete
2187 * our own device address from our uc list. Because we store the
2188 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2189 * such requests and not delete our device address from this list.
2191 if (ether_addr_equal(addr, netdev->dev_addr))
2194 i40e_del_mac_filter(vsi, addr);
2200 * i40e_set_rx_mode - NDO callback to set the netdev filters
2201 * @netdev: network interface device structure
2203 static void i40e_set_rx_mode(struct net_device *netdev)
2205 struct i40e_netdev_priv *np = netdev_priv(netdev);
2206 struct i40e_vsi *vsi = np->vsi;
2208 spin_lock_bh(&vsi->mac_filter_hash_lock);
2210 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2211 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2213 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2215 /* check for other flag changes */
2216 if (vsi->current_netdev_flags != vsi->netdev->flags) {
2217 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2218 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2223 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2224 * @vsi: Pointer to VSI struct
2225 * @from: Pointer to list which contains MAC filter entries - changes to
2226 * those entries needs to be undone.
2228 * MAC filter entries from this list were slated for deletion.
2230 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2231 struct hlist_head *from)
2233 struct i40e_mac_filter *f;
2234 struct hlist_node *h;
2236 hlist_for_each_entry_safe(f, h, from, hlist) {
2237 u64 key = i40e_addr_to_hkey(f->macaddr);
2239 /* Move the element back into MAC filter list*/
2240 hlist_del(&f->hlist);
2241 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2246 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2247 * @vsi: Pointer to vsi struct
2248 * @from: Pointer to list which contains MAC filter entries - changes to
2249 * those entries needs to be undone.
2251 * MAC filter entries from this list were slated for addition.
2253 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2254 struct hlist_head *from)
2256 struct i40e_new_mac_filter *new;
2257 struct hlist_node *h;
2259 hlist_for_each_entry_safe(new, h, from, hlist) {
2260 /* We can simply free the wrapper structure */
2261 hlist_del(&new->hlist);
2262 netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2268 * i40e_next_filter - Get the next non-broadcast filter from a list
2269 * @next: pointer to filter in list
2271 * Returns the next non-broadcast filter in the list. Required so that we
2272 * ignore broadcast filters within the list, since these are not handled via
2273 * the normal firmware update path.
2276 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2278 hlist_for_each_entry_continue(next, hlist) {
2279 if (!is_broadcast_ether_addr(next->f->macaddr))
2287 * i40e_update_filter_state - Update filter state based on return data
2289 * @count: Number of filters added
2290 * @add_list: return data from fw
2291 * @add_head: pointer to first filter in current batch
2293 * MAC filter entries from list were slated to be added to device. Returns
2294 * number of successful filters. Note that 0 does NOT mean success!
2297 i40e_update_filter_state(int count,
2298 struct i40e_aqc_add_macvlan_element_data *add_list,
2299 struct i40e_new_mac_filter *add_head)
2304 for (i = 0; i < count; i++) {
2305 /* Always check status of each filter. We don't need to check
2306 * the firmware return status because we pre-set the filter
2307 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2308 * request to the adminq. Thus, if it no longer matches then
2309 * we know the filter is active.
2311 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2312 add_head->state = I40E_FILTER_FAILED;
2314 add_head->state = I40E_FILTER_ACTIVE;
2318 add_head = i40e_next_filter(add_head);
2327 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2328 * @vsi: ptr to the VSI
2329 * @vsi_name: name to display in messages
2330 * @list: the list of filters to send to firmware
2331 * @num_del: the number of filters to delete
2332 * @retval: Set to -EIO on failure to delete
2334 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2335 * *retval instead of a return value so that success does not force ret_val to
2336 * be set to 0. This ensures that a sequence of calls to this function
2337 * preserve the previous value of *retval on successful delete.
2340 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2341 struct i40e_aqc_remove_macvlan_element_data *list,
2342 int num_del, int *retval)
2344 struct i40e_hw *hw = &vsi->back->hw;
2345 enum i40e_admin_queue_err aq_status;
2348 aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2351 /* Explicitly ignore and do not report when firmware returns ENOENT */
2352 if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2354 dev_info(&vsi->back->pdev->dev,
2355 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2356 vsi_name, ERR_PTR(aq_ret),
2357 i40e_aq_str(hw, aq_status));
2362 * i40e_aqc_add_filters - Request firmware to add a set of filters
2363 * @vsi: ptr to the VSI
2364 * @vsi_name: name to display in messages
2365 * @list: the list of filters to send to firmware
2366 * @add_head: Position in the add hlist
2367 * @num_add: the number of filters to add
2369 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2370 * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2371 * space for more filters.
2374 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2375 struct i40e_aqc_add_macvlan_element_data *list,
2376 struct i40e_new_mac_filter *add_head,
2379 struct i40e_hw *hw = &vsi->back->hw;
2380 enum i40e_admin_queue_err aq_status;
2383 i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2384 fcnt = i40e_update_filter_state(num_add, list, add_head);
2386 if (fcnt != num_add) {
2387 if (vsi->type == I40E_VSI_MAIN) {
2388 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2389 dev_warn(&vsi->back->pdev->dev,
2390 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2391 i40e_aq_str(hw, aq_status), vsi_name);
2392 } else if (vsi->type == I40E_VSI_SRIOV ||
2393 vsi->type == I40E_VSI_VMDQ1 ||
2394 vsi->type == I40E_VSI_VMDQ2) {
2395 dev_warn(&vsi->back->pdev->dev,
2396 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2397 i40e_aq_str(hw, aq_status), vsi_name,
2400 dev_warn(&vsi->back->pdev->dev,
2401 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2402 i40e_aq_str(hw, aq_status), vsi_name,
2409 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2410 * @vsi: pointer to the VSI
2411 * @vsi_name: the VSI name
2414 * This function sets or clears the promiscuous broadcast flags for VLAN
2415 * filters in order to properly receive broadcast frames. Assumes that only
2416 * broadcast filters are passed.
2418 * Returns status indicating success or failure;
2421 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2422 struct i40e_mac_filter *f)
2424 bool enable = f->state == I40E_FILTER_NEW;
2425 struct i40e_hw *hw = &vsi->back->hw;
2428 if (f->vlan == I40E_VLAN_ANY) {
2429 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2434 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2442 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2443 dev_warn(&vsi->back->pdev->dev,
2444 "Error %s, forcing overflow promiscuous on %s\n",
2445 i40e_aq_str(hw, hw->aq.asq_last_status),
2453 * i40e_set_promiscuous - set promiscuous mode
2454 * @pf: board private structure
2455 * @promisc: promisc on or off
2457 * There are different ways of setting promiscuous mode on a PF depending on
2458 * what state/environment we're in. This identifies and sets it appropriately.
2459 * Returns 0 on success.
2461 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2463 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2464 struct i40e_hw *hw = &pf->hw;
2467 if (vsi->type == I40E_VSI_MAIN &&
2468 pf->lan_veb != I40E_NO_VEB &&
2469 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2470 /* set defport ON for Main VSI instead of true promisc
2471 * this way we will get all unicast/multicast and VLAN
2472 * promisc behavior but will not get VF or VMDq traffic
2473 * replicated on the Main VSI.
2476 aq_ret = i40e_aq_set_default_vsi(hw,
2480 aq_ret = i40e_aq_clear_default_vsi(hw,
2484 dev_info(&pf->pdev->dev,
2485 "Set default VSI failed, err %pe, aq_err %s\n",
2487 i40e_aq_str(hw, hw->aq.asq_last_status));
2490 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2496 dev_info(&pf->pdev->dev,
2497 "set unicast promisc failed, err %pe, aq_err %s\n",
2499 i40e_aq_str(hw, hw->aq.asq_last_status));
2501 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2506 dev_info(&pf->pdev->dev,
2507 "set multicast promisc failed, err %pe, aq_err %s\n",
2509 i40e_aq_str(hw, hw->aq.asq_last_status));
2514 pf->cur_promisc = promisc;
2520 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2521 * @vsi: ptr to the VSI
2523 * Push any outstanding VSI filter changes through the AdminQ.
2525 * Returns 0 or error value
2527 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2529 struct hlist_head tmp_add_list, tmp_del_list;
2530 struct i40e_mac_filter *f;
2531 struct i40e_new_mac_filter *new, *add_head = NULL;
2532 struct i40e_hw *hw = &vsi->back->hw;
2533 bool old_overflow, new_overflow;
2534 unsigned int failed_filters = 0;
2535 unsigned int vlan_filters = 0;
2536 char vsi_name[16] = "PF";
2537 int filter_list_len = 0;
2538 u32 changed_flags = 0;
2539 struct hlist_node *h;
2549 /* empty array typed pointers, kcalloc later */
2550 struct i40e_aqc_add_macvlan_element_data *add_list;
2551 struct i40e_aqc_remove_macvlan_element_data *del_list;
2553 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2554 usleep_range(1000, 2000);
2557 old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2560 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2561 vsi->current_netdev_flags = vsi->netdev->flags;
2564 INIT_HLIST_HEAD(&tmp_add_list);
2565 INIT_HLIST_HEAD(&tmp_del_list);
2567 if (vsi->type == I40E_VSI_SRIOV)
2568 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2569 else if (vsi->type != I40E_VSI_MAIN)
2570 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2572 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2573 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2575 spin_lock_bh(&vsi->mac_filter_hash_lock);
2576 /* Create a list of filters to delete. */
2577 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2578 if (f->state == I40E_FILTER_REMOVE) {
2579 /* Move the element into temporary del_list */
2580 hash_del(&f->hlist);
2581 hlist_add_head(&f->hlist, &tmp_del_list);
2583 /* Avoid counting removed filters */
2586 if (f->state == I40E_FILTER_NEW) {
2587 /* Create a temporary i40e_new_mac_filter */
2588 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2590 goto err_no_memory_locked;
2592 /* Store pointer to the real filter */
2594 new->state = f->state;
2596 /* Add it to the hash list */
2597 hlist_add_head(&new->hlist, &tmp_add_list);
2600 /* Count the number of active (current and new) VLAN
2601 * filters we have now. Does not count filters which
2602 * are marked for deletion.
2608 if (vsi->type != I40E_VSI_SRIOV)
2609 retval = i40e_correct_mac_vlan_filters
2610 (vsi, &tmp_add_list, &tmp_del_list,
2613 retval = i40e_correct_vf_mac_vlan_filters
2614 (vsi, &tmp_add_list, &tmp_del_list,
2615 vlan_filters, pf->vf[vsi->vf_id].trusted);
2617 hlist_for_each_entry(new, &tmp_add_list, hlist)
2618 netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2621 goto err_no_memory_locked;
2623 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2626 /* Now process 'del_list' outside the lock */
2627 if (!hlist_empty(&tmp_del_list)) {
2628 filter_list_len = hw->aq.asq_buf_size /
2629 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2630 list_size = filter_list_len *
2631 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2632 del_list = kzalloc(list_size, GFP_ATOMIC);
2636 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2639 /* handle broadcast filters by updating the broadcast
2640 * promiscuous flag and release filter list.
2642 if (is_broadcast_ether_addr(f->macaddr)) {
2643 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2645 hlist_del(&f->hlist);
2650 /* add to delete list */
2651 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2652 if (f->vlan == I40E_VLAN_ANY) {
2653 del_list[num_del].vlan_tag = 0;
2654 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2656 del_list[num_del].vlan_tag =
2657 cpu_to_le16((u16)(f->vlan));
2660 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2661 del_list[num_del].flags = cmd_flags;
2664 /* flush a full buffer */
2665 if (num_del == filter_list_len) {
2666 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2668 memset(del_list, 0, list_size);
2671 /* Release memory for MAC filter entries which were
2672 * synced up with HW.
2674 hlist_del(&f->hlist);
2679 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2687 if (!hlist_empty(&tmp_add_list)) {
2688 /* Do all the adds now. */
2689 filter_list_len = hw->aq.asq_buf_size /
2690 sizeof(struct i40e_aqc_add_macvlan_element_data);
2691 list_size = filter_list_len *
2692 sizeof(struct i40e_aqc_add_macvlan_element_data);
2693 add_list = kzalloc(list_size, GFP_ATOMIC);
2698 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2699 /* handle broadcast filters by updating the broadcast
2700 * promiscuous flag instead of adding a MAC filter.
2702 if (is_broadcast_ether_addr(new->f->macaddr)) {
2703 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2705 new->state = I40E_FILTER_FAILED;
2707 new->state = I40E_FILTER_ACTIVE;
2711 /* add to add array */
2715 ether_addr_copy(add_list[num_add].mac_addr,
2717 if (new->f->vlan == I40E_VLAN_ANY) {
2718 add_list[num_add].vlan_tag = 0;
2719 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2721 add_list[num_add].vlan_tag =
2722 cpu_to_le16((u16)(new->f->vlan));
2724 add_list[num_add].queue_number = 0;
2725 /* set invalid match method for later detection */
2726 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2727 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2728 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2731 /* flush a full buffer */
2732 if (num_add == filter_list_len) {
2733 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2735 memset(add_list, 0, list_size);
2740 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2743 /* Now move all of the filters from the temp add list back to
2746 spin_lock_bh(&vsi->mac_filter_hash_lock);
2747 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2748 /* Only update the state if we're still NEW */
2749 if (new->f->state == I40E_FILTER_NEW)
2750 new->f->state = new->state;
2751 hlist_del(&new->hlist);
2752 netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2755 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2760 /* Determine the number of active and failed filters. */
2761 spin_lock_bh(&vsi->mac_filter_hash_lock);
2762 vsi->active_filters = 0;
2763 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2764 if (f->state == I40E_FILTER_ACTIVE)
2765 vsi->active_filters++;
2766 else if (f->state == I40E_FILTER_FAILED)
2769 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2771 /* Check if we are able to exit overflow promiscuous mode. We can
2772 * safely exit if we didn't just enter, we no longer have any failed
2773 * filters, and we have reduced filters below the threshold value.
2775 if (old_overflow && !failed_filters &&
2776 vsi->active_filters < vsi->promisc_threshold) {
2777 dev_info(&pf->pdev->dev,
2778 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2780 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2781 vsi->promisc_threshold = 0;
2784 /* if the VF is not trusted do not do promisc */
2785 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2786 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2790 new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2792 /* If we are entering overflow promiscuous, we need to calculate a new
2793 * threshold for when we are safe to exit
2795 if (!old_overflow && new_overflow)
2796 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2798 /* check for changes in promiscuous modes */
2799 if (changed_flags & IFF_ALLMULTI) {
2800 bool cur_multipromisc;
2802 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2803 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2808 retval = i40e_aq_rc_to_posix(aq_ret,
2809 hw->aq.asq_last_status);
2810 dev_info(&pf->pdev->dev,
2811 "set multi promisc failed on %s, err %pe aq_err %s\n",
2814 i40e_aq_str(hw, hw->aq.asq_last_status));
2816 dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2817 cur_multipromisc ? "entering" : "leaving");
2821 if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2824 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2826 aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2828 retval = i40e_aq_rc_to_posix(aq_ret,
2829 hw->aq.asq_last_status);
2830 dev_info(&pf->pdev->dev,
2831 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2832 cur_promisc ? "on" : "off",
2835 i40e_aq_str(hw, hw->aq.asq_last_status));
2839 /* if something went wrong then set the changed flag so we try again */
2841 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2843 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2847 /* Restore elements on the temporary add and delete lists */
2848 spin_lock_bh(&vsi->mac_filter_hash_lock);
2849 err_no_memory_locked:
2850 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2851 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2852 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2854 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2855 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2860 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2861 * @pf: board private structure
2863 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2869 if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2871 if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2872 set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2876 for (v = 0; v < pf->num_alloc_vsi; v++) {
2878 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2879 !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2880 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2883 /* come back and try again later */
2884 set_bit(__I40E_MACVLAN_SYNC_PENDING,
2893 * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2895 * @vsi: VSI to calculate rx_buf_len from
2897 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2899 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2900 return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2902 return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2906 * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2908 * @xdp_prog: XDP program
2910 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2911 struct bpf_prog *xdp_prog)
2913 u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2916 if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2919 chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2921 return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2925 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2926 * @netdev: network interface device structure
2927 * @new_mtu: new value for maximum frame size
2929 * Returns 0 on success, negative on failure
2931 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2933 struct i40e_netdev_priv *np = netdev_priv(netdev);
2934 struct i40e_vsi *vsi = np->vsi;
2935 struct i40e_pf *pf = vsi->back;
2938 frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2939 if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2940 netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2941 new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2945 netdev_dbg(netdev, "changing MTU from %d to %d\n",
2946 netdev->mtu, new_mtu);
2947 netdev->mtu = new_mtu;
2948 if (netif_running(netdev))
2949 i40e_vsi_reinit_locked(vsi);
2950 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2951 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2956 * i40e_ioctl - Access the hwtstamp interface
2957 * @netdev: network interface device structure
2958 * @ifr: interface request data
2959 * @cmd: ioctl command
2961 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2963 struct i40e_netdev_priv *np = netdev_priv(netdev);
2964 struct i40e_pf *pf = np->vsi->back;
2968 return i40e_ptp_get_ts_config(pf, ifr);
2970 return i40e_ptp_set_ts_config(pf, ifr);
2977 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2978 * @vsi: the vsi being adjusted
2980 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2982 struct i40e_vsi_context ctxt;
2985 /* Don't modify stripping options if a port VLAN is active */
2989 if ((vsi->info.valid_sections &
2990 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2991 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2992 return; /* already enabled */
2994 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2995 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2996 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2998 ctxt.seid = vsi->seid;
2999 ctxt.info = vsi->info;
3000 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3002 dev_info(&vsi->back->pdev->dev,
3003 "update vlan stripping failed, err %pe aq_err %s\n",
3005 i40e_aq_str(&vsi->back->hw,
3006 vsi->back->hw.aq.asq_last_status));
3011 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3012 * @vsi: the vsi being adjusted
3014 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3016 struct i40e_vsi_context ctxt;
3019 /* Don't modify stripping options if a port VLAN is active */
3023 if ((vsi->info.valid_sections &
3024 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3025 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3026 I40E_AQ_VSI_PVLAN_EMOD_MASK))
3027 return; /* already disabled */
3029 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3030 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3031 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3033 ctxt.seid = vsi->seid;
3034 ctxt.info = vsi->info;
3035 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3037 dev_info(&vsi->back->pdev->dev,
3038 "update vlan stripping failed, err %pe aq_err %s\n",
3040 i40e_aq_str(&vsi->back->hw,
3041 vsi->back->hw.aq.asq_last_status));
3046 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3047 * @vsi: the vsi being configured
3048 * @vid: vlan id to be added (0 = untagged only , -1 = any)
3050 * This is a helper function for adding a new MAC/VLAN filter with the
3051 * specified VLAN for each existing MAC address already in the hash table.
3052 * This function does *not* perform any accounting to update filters based on
3055 * NOTE: this function expects to be called while under the
3056 * mac_filter_hash_lock
3058 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3060 struct i40e_mac_filter *f, *add_f;
3061 struct hlist_node *h;
3064 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3065 /* If we're asked to add a filter that has been marked for
3066 * removal, it is safe to simply restore it to active state.
3067 * __i40e_del_filter will have simply deleted any filters which
3068 * were previously marked NEW or FAILED, so if it is currently
3069 * marked REMOVE it must have previously been ACTIVE. Since we
3070 * haven't yet run the sync filters task, just restore this
3071 * filter to the ACTIVE state so that the sync task leaves it
3074 if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3075 f->state = I40E_FILTER_ACTIVE;
3077 } else if (f->state == I40E_FILTER_REMOVE) {
3080 add_f = i40e_add_filter(vsi, f->macaddr, vid);
3082 dev_info(&vsi->back->pdev->dev,
3083 "Could not add vlan filter %d for %pM\n",
3093 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3094 * @vsi: the VSI being configured
3095 * @vid: VLAN id to be added
3097 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3104 /* The network stack will attempt to add VID=0, with the intention to
3105 * receive priority tagged packets with a VLAN of 0. Our HW receives
3106 * these packets by default when configured to receive untagged
3107 * packets, so we don't need to add a filter for this case.
3108 * Additionally, HW interprets adding a VID=0 filter as meaning to
3109 * receive *only* tagged traffic and stops receiving untagged traffic.
3110 * Thus, we do not want to actually add a filter for VID=0
3115 /* Locked once because all functions invoked below iterates list*/
3116 spin_lock_bh(&vsi->mac_filter_hash_lock);
3117 err = i40e_add_vlan_all_mac(vsi, vid);
3118 spin_unlock_bh(&vsi->mac_filter_hash_lock);
3122 /* schedule our worker thread which will take care of
3123 * applying the new filter changes
3125 i40e_service_event_schedule(vsi->back);
3130 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3131 * @vsi: the vsi being configured
3132 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3134 * This function should be used to remove all VLAN filters which match the
3135 * given VID. It does not schedule the service event and does not take the
3136 * mac_filter_hash_lock so it may be combined with other operations under
3137 * a single invocation of the mac_filter_hash_lock.
3139 * NOTE: this function expects to be called while under the
3140 * mac_filter_hash_lock
3142 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3144 struct i40e_mac_filter *f;
3145 struct hlist_node *h;
3148 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3150 __i40e_del_filter(vsi, f);
3155 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3156 * @vsi: the VSI being configured
3157 * @vid: VLAN id to be removed
3159 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3161 if (!vid || vsi->info.pvid)
3164 spin_lock_bh(&vsi->mac_filter_hash_lock);
3165 i40e_rm_vlan_all_mac(vsi, vid);
3166 spin_unlock_bh(&vsi->mac_filter_hash_lock);
3168 /* schedule our worker thread which will take care of
3169 * applying the new filter changes
3171 i40e_service_event_schedule(vsi->back);
3175 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3176 * @netdev: network interface to be adjusted
3177 * @proto: unused protocol value
3178 * @vid: vlan id to be added
3180 * net_device_ops implementation for adding vlan ids
3182 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3183 __always_unused __be16 proto, u16 vid)
3185 struct i40e_netdev_priv *np = netdev_priv(netdev);
3186 struct i40e_vsi *vsi = np->vsi;
3189 if (vid >= VLAN_N_VID)
3192 ret = i40e_vsi_add_vlan(vsi, vid);
3194 set_bit(vid, vsi->active_vlans);
3200 * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3201 * @netdev: network interface to be adjusted
3202 * @proto: unused protocol value
3203 * @vid: vlan id to be added
3205 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3206 __always_unused __be16 proto, u16 vid)
3208 struct i40e_netdev_priv *np = netdev_priv(netdev);
3209 struct i40e_vsi *vsi = np->vsi;
3211 if (vid >= VLAN_N_VID)
3213 set_bit(vid, vsi->active_vlans);
3217 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3218 * @netdev: network interface to be adjusted
3219 * @proto: unused protocol value
3220 * @vid: vlan id to be removed
3222 * net_device_ops implementation for removing vlan ids
3224 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3225 __always_unused __be16 proto, u16 vid)
3227 struct i40e_netdev_priv *np = netdev_priv(netdev);
3228 struct i40e_vsi *vsi = np->vsi;
3230 /* return code is ignored as there is nothing a user
3231 * can do about failure to remove and a log message was
3232 * already printed from the other function
3234 i40e_vsi_kill_vlan(vsi, vid);
3236 clear_bit(vid, vsi->active_vlans);
3242 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3243 * @vsi: the vsi being brought back up
3245 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3252 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3253 i40e_vlan_stripping_enable(vsi);
3255 i40e_vlan_stripping_disable(vsi);
3257 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3258 i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3263 * i40e_vsi_add_pvid - Add pvid for the VSI
3264 * @vsi: the vsi being adjusted
3265 * @vid: the vlan id to set as a PVID
3267 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3269 struct i40e_vsi_context ctxt;
3272 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3273 vsi->info.pvid = cpu_to_le16(vid);
3274 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3275 I40E_AQ_VSI_PVLAN_INSERT_PVID |
3276 I40E_AQ_VSI_PVLAN_EMOD_STR;
3278 ctxt.seid = vsi->seid;
3279 ctxt.info = vsi->info;
3280 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3282 dev_info(&vsi->back->pdev->dev,
3283 "add pvid failed, err %pe aq_err %s\n",
3285 i40e_aq_str(&vsi->back->hw,
3286 vsi->back->hw.aq.asq_last_status));
3294 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3295 * @vsi: the vsi being adjusted
3297 * Just use the vlan_rx_register() service to put it back to normal
3299 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3303 i40e_vlan_stripping_disable(vsi);
3307 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3308 * @vsi: ptr to the VSI
3310 * If this function returns with an error, then it's possible one or
3311 * more of the rings is populated (while the rest are not). It is the
3312 * callers duty to clean those orphaned rings.
3314 * Return 0 on success, negative on failure
3316 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3320 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3321 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3323 if (!i40e_enabled_xdp_vsi(vsi))
3326 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3327 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3333 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3334 * @vsi: ptr to the VSI
3336 * Free VSI's transmit software resources
3338 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3342 if (vsi->tx_rings) {
3343 for (i = 0; i < vsi->num_queue_pairs; i++)
3344 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3345 i40e_free_tx_resources(vsi->tx_rings[i]);
3348 if (vsi->xdp_rings) {
3349 for (i = 0; i < vsi->num_queue_pairs; i++)
3350 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3351 i40e_free_tx_resources(vsi->xdp_rings[i]);
3356 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3357 * @vsi: ptr to the VSI
3359 * If this function returns with an error, then it's possible one or
3360 * more of the rings is populated (while the rest are not). It is the
3361 * callers duty to clean those orphaned rings.
3363 * Return 0 on success, negative on failure
3365 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3369 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3370 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3375 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3376 * @vsi: ptr to the VSI
3378 * Free all receive software resources
3380 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3387 for (i = 0; i < vsi->num_queue_pairs; i++)
3388 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3389 i40e_free_rx_resources(vsi->rx_rings[i]);
3393 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3394 * @ring: The Tx ring to configure
3396 * This enables/disables XPS for a given Tx descriptor ring
3397 * based on the TCs enabled for the VSI that ring belongs to.
3399 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3403 if (!ring->q_vector || !ring->netdev || ring->ch)
3406 /* We only initialize XPS once, so as not to overwrite user settings */
3407 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3410 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3411 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3416 * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3417 * @ring: The Tx or Rx ring
3419 * Returns the AF_XDP buffer pool or NULL.
3421 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3423 bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3424 int qid = ring->queue_index;
3426 if (ring_is_xdp(ring))
3427 qid -= ring->vsi->alloc_queue_pairs;
3429 if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3432 return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3436 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3437 * @ring: The Tx ring to configure
3439 * Configure the Tx descriptor ring in the HMC context.
3441 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3443 struct i40e_vsi *vsi = ring->vsi;
3444 u16 pf_q = vsi->base_queue + ring->queue_index;
3445 struct i40e_hw *hw = &vsi->back->hw;
3446 struct i40e_hmc_obj_txq tx_ctx;
3450 if (ring_is_xdp(ring))
3451 ring->xsk_pool = i40e_xsk_pool(ring);
3453 /* some ATR related tx ring init */
3454 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3455 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3456 ring->atr_count = 0;
3458 ring->atr_sample_rate = 0;
3462 i40e_config_xps_tx_ring(ring);
3464 /* clear the context structure first */
3465 memset(&tx_ctx, 0, sizeof(tx_ctx));
3467 tx_ctx.new_context = 1;
3468 tx_ctx.base = (ring->dma / 128);
3469 tx_ctx.qlen = ring->count;
3470 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3471 I40E_FLAG_FD_ATR_ENABLED));
3472 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3473 /* FDIR VSI tx ring can still use RS bit and writebacks */
3474 if (vsi->type != I40E_VSI_FDIR)
3475 tx_ctx.head_wb_ena = 1;
3476 tx_ctx.head_wb_addr = ring->dma +
3477 (ring->count * sizeof(struct i40e_tx_desc));
3479 /* As part of VSI creation/update, FW allocates certain
3480 * Tx arbitration queue sets for each TC enabled for
3481 * the VSI. The FW returns the handles to these queue
3482 * sets as part of the response buffer to Add VSI,
3483 * Update VSI, etc. AQ commands. It is expected that
3484 * these queue set handles be associated with the Tx
3485 * queues by the driver as part of the TX queue context
3486 * initialization. This has to be done regardless of
3487 * DCB as by default everything is mapped to TC0.
3492 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3495 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3497 tx_ctx.rdylist_act = 0;
3499 /* clear the context in the HMC */
3500 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3502 dev_info(&vsi->back->pdev->dev,
3503 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3504 ring->queue_index, pf_q, err);
3508 /* set the context in the HMC */
3509 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3511 dev_info(&vsi->back->pdev->dev,
3512 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3513 ring->queue_index, pf_q, err);
3517 /* Now associate this queue with this PCI function */
3519 if (ring->ch->type == I40E_VSI_VMDQ2)
3520 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3524 qtx_ctl |= (ring->ch->vsi_number <<
3525 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3526 I40E_QTX_CTL_VFVM_INDX_MASK;
3528 if (vsi->type == I40E_VSI_VMDQ2) {
3529 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3530 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3531 I40E_QTX_CTL_VFVM_INDX_MASK;
3533 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3537 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3538 I40E_QTX_CTL_PF_INDX_MASK);
3539 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3542 /* cache tail off for easier writes later */
3543 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3549 * i40e_rx_offset - Return expected offset into page to access data
3550 * @rx_ring: Ring we are requesting offset of
3552 * Returns the offset value for ring into the data buffer.
3554 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3556 return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3560 * i40e_configure_rx_ring - Configure a receive ring context
3561 * @ring: The Rx ring to configure
3563 * Configure the Rx descriptor ring in the HMC context.
3565 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3567 struct i40e_vsi *vsi = ring->vsi;
3568 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3569 u16 pf_q = vsi->base_queue + ring->queue_index;
3570 struct i40e_hw *hw = &vsi->back->hw;
3571 struct i40e_hmc_obj_rxq rx_ctx;
3576 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3578 /* clear the context structure first */
3579 memset(&rx_ctx, 0, sizeof(rx_ctx));
3581 if (ring->vsi->type == I40E_VSI_MAIN)
3582 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3584 ring->xsk_pool = i40e_xsk_pool(ring);
3585 if (ring->xsk_pool) {
3587 xsk_pool_get_rx_frame_size(ring->xsk_pool);
3588 /* For AF_XDP ZC, we disallow packets to span on
3589 * multiple buffers, thus letting us skip that
3590 * handling in the fast-path.
3593 ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3594 MEM_TYPE_XSK_BUFF_POOL,
3598 dev_info(&vsi->back->pdev->dev,
3599 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3603 ring->rx_buf_len = vsi->rx_buf_len;
3604 if (ring->vsi->type == I40E_VSI_MAIN) {
3605 ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3606 MEM_TYPE_PAGE_SHARED,
3613 xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3615 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3616 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3618 rx_ctx.base = (ring->dma / 128);
3619 rx_ctx.qlen = ring->count;
3621 /* use 16 byte descriptors */
3624 /* descriptor type is always zero
3627 rx_ctx.hsplit_0 = 0;
3629 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3630 if (hw->revision_id == 0)
3631 rx_ctx.lrxqthresh = 0;
3633 rx_ctx.lrxqthresh = 1;
3634 rx_ctx.crcstrip = 1;
3636 /* this controls whether VLAN is stripped from inner headers */
3638 /* set the prefena field to 1 because the manual says to */
3641 /* clear the context in the HMC */
3642 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3644 dev_info(&vsi->back->pdev->dev,
3645 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3646 ring->queue_index, pf_q, err);
3650 /* set the context in the HMC */
3651 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3653 dev_info(&vsi->back->pdev->dev,
3654 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3655 ring->queue_index, pf_q, err);
3659 /* configure Rx buffer alignment */
3660 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3661 if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3662 dev_info(&vsi->back->pdev->dev,
3663 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3666 clear_ring_build_skb_enabled(ring);
3668 set_ring_build_skb_enabled(ring);
3671 ring->rx_offset = i40e_rx_offset(ring);
3673 /* cache tail for quicker writes, and clear the reg before use */
3674 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3675 writel(0, ring->tail);
3677 if (ring->xsk_pool) {
3678 xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3679 ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3681 ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3684 /* Log this in case the user has forgotten to give the kernel
3685 * any buffers, even later in the application.
3687 dev_info(&vsi->back->pdev->dev,
3688 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3689 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3690 ring->queue_index, pf_q);
3697 * i40e_vsi_configure_tx - Configure the VSI for Tx
3698 * @vsi: VSI structure describing this set of rings and resources
3700 * Configure the Tx VSI for operation.
3702 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3707 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3708 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3710 if (err || !i40e_enabled_xdp_vsi(vsi))
3713 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3714 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3720 * i40e_vsi_configure_rx - Configure the VSI for Rx
3721 * @vsi: the VSI being configured
3723 * Configure the Rx VSI for operation.
3725 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3730 vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3731 vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3733 #if (PAGE_SIZE < 8192)
3734 if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3735 vsi->netdev->mtu <= ETH_DATA_LEN) {
3736 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3737 vsi->max_frame = vsi->rx_buf_len;
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]);
3749 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3750 * @vsi: ptr to the VSI
3752 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3754 struct i40e_ring *tx_ring, *rx_ring;
3755 u16 qoffset, qcount;
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;
3769 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3770 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
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;
3785 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3786 * @vsi: ptr to the VSI
3788 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3791 i40e_set_rx_mode(vsi->netdev);
3795 * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3796 * @pf: Pointer to the targeted PF
3798 * Set all flow director counters to 0.
3800 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
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;
3813 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3814 * @vsi: Pointer to the targeted VSI
3816 * This function replays the hlist on the hw where all the SB Flow Director
3817 * filters were saved.
3819 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3821 struct i40e_fdir_filter *filter;
3822 struct i40e_pf *pf = vsi->back;
3823 struct hlist_node *node;
3825 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3828 /* Reset FDir counters as we're replaying all existing filters */
3829 i40e_reset_fdir_filter_cnt(pf);
3831 hlist_for_each_entry_safe(filter, node,
3832 &pf->fdir_filter_list, fdir_node) {
3833 i40e_add_del_fdir(vsi, filter, true);
3838 * i40e_vsi_configure - Set up the VSI for action
3839 * @vsi: the VSI being configured
3841 static int i40e_vsi_configure(struct i40e_vsi *vsi)
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);
3850 err = i40e_vsi_configure_rx(vsi);
3856 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3857 * @vsi: the VSI being configured
3859 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3861 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3862 struct i40e_pf *pf = vsi->back;
3863 struct i40e_hw *hw = &pf->hw;
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)
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];
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;
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;
3891 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3892 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
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;
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);
3907 wr32(hw, I40E_QINT_RQCTL(qp), val);
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);
3918 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
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);
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);
3933 wr32(hw, I40E_QINT_TQCTL(qp), val);
3942 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3943 * @pf: pointer to private device data structure
3945 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3947 struct i40e_hw *hw = &pf->hw;
3950 /* clear things first */
3951 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3952 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
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;
3963 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3964 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3966 if (pf->flags & I40E_FLAG_PTP)
3967 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3969 wr32(hw, I40E_PFINT_ICR0_ENA, val);
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);
3975 /* OTHER_ITR_IDX = 0 */
3976 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3980 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3981 * @vsi: the VSI being configured
3983 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
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;
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;
4000 i40e_enable_misc_int_causes(pf);
4002 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4003 wr32(hw, I40E_PFINT_LNKLST0, 0);
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
4008 wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
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));
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));
4023 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4024 * @pf: board private structure
4026 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4028 struct i40e_hw *hw = &pf->hw;
4030 wr32(hw, I40E_PFINT_DYN_CTL0,
4031 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4036 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4037 * @pf: board private structure
4039 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4041 struct i40e_hw *hw = &pf->hw;
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);
4048 wr32(hw, I40E_PFINT_DYN_CTL0, val);
4053 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4054 * @irq: interrupt number
4055 * @data: pointer to a q_vector
4057 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4059 struct i40e_q_vector *q_vector = data;
4061 if (!q_vector->tx.ring && !q_vector->rx.ring)
4064 napi_schedule_irqoff(&q_vector->napi);
4070 * i40e_irq_affinity_notify - Callback for affinity changes
4071 * @notify: context as to what irq was changed
4072 * @mask: the new affinity mask
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.
4077 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4078 const cpumask_t *mask)
4080 struct i40e_q_vector *q_vector =
4081 container_of(notify, struct i40e_q_vector, affinity_notify);
4083 cpumask_copy(&q_vector->affinity_mask, mask);
4087 * i40e_irq_affinity_release - Callback for affinity notifier release
4088 * @ref: internal core kernel usage
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.
4094 static void i40e_irq_affinity_release(struct kref *ref) {}
4097 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4098 * @vsi: the VSI being configured
4099 * @basename: name for the vector
4101 * Allocates MSI-X vectors and requests interrupts from the kernel.
4103 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4105 int q_vectors = vsi->num_q_vectors;
4106 struct i40e_pf *pf = vsi->back;
4107 int base = vsi->base_vector;
4114 for (vector = 0; vector < q_vectors; vector++) {
4115 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4117 irq_num = pf->msix_entries[base + vector].vector;
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++);
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++);
4130 /* skip this unused q_vector */
4133 err = request_irq(irq_num,
4139 dev_info(&pf->pdev->dev,
4140 "MSIX request_irq failed, error: %d\n", err);
4141 goto free_queue_irqs;
4144 /* register for affinity change notifications */
4145 q_vector->irq_num = irq_num;
4146 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4147 q_vector->affinity_notify.release = i40e_irq_affinity_release;
4148 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4149 /* Spread affinity hints out across online CPUs.
4151 * get_cpu_mask returns a static constant mask with
4152 * a permanent lifetime so it's ok to pass to
4153 * irq_update_affinity_hint without making a copy.
4155 cpu = cpumask_local_spread(q_vector->v_idx, -1);
4156 irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4159 vsi->irqs_ready = true;
4165 irq_num = pf->msix_entries[base + vector].vector;
4166 irq_set_affinity_notifier(irq_num, NULL);
4167 irq_update_affinity_hint(irq_num, NULL);
4168 free_irq(irq_num, &vsi->q_vectors[vector]);
4174 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4175 * @vsi: the VSI being un-configured
4177 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4179 struct i40e_pf *pf = vsi->back;
4180 struct i40e_hw *hw = &pf->hw;
4181 int base = vsi->base_vector;
4184 /* disable interrupt causation from each queue */
4185 for (i = 0; i < vsi->num_queue_pairs; i++) {
4188 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4189 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4190 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4192 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4193 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4194 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4196 if (!i40e_enabled_xdp_vsi(vsi))
4198 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4201 /* disable each interrupt */
4202 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4203 for (i = vsi->base_vector;
4204 i < (vsi->num_q_vectors + vsi->base_vector); i++)
4205 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4208 for (i = 0; i < vsi->num_q_vectors; i++)
4209 synchronize_irq(pf->msix_entries[i + base].vector);
4211 /* Legacy and MSI mode - this stops all interrupt handling */
4212 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4213 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4215 synchronize_irq(pf->pdev->irq);
4220 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4221 * @vsi: the VSI being configured
4223 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4225 struct i40e_pf *pf = vsi->back;
4228 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4229 for (i = 0; i < vsi->num_q_vectors; i++)
4230 i40e_irq_dynamic_enable(vsi, i);
4232 i40e_irq_dynamic_enable_icr0(pf);
4235 i40e_flush(&pf->hw);
4240 * i40e_free_misc_vector - Free the vector that handles non-queue events
4241 * @pf: board private structure
4243 static void i40e_free_misc_vector(struct i40e_pf *pf)
4246 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4247 i40e_flush(&pf->hw);
4249 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4250 free_irq(pf->msix_entries[0].vector, pf);
4251 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4256 * i40e_intr - MSI/Legacy and non-queue interrupt handler
4257 * @irq: interrupt number
4258 * @data: pointer to a q_vector
4260 * This is the handler used for all MSI/Legacy interrupts, and deals
4261 * with both queue and non-queue interrupts. This is also used in
4262 * MSIX mode to handle the non-queue interrupts.
4264 static irqreturn_t i40e_intr(int irq, void *data)
4266 struct i40e_pf *pf = (struct i40e_pf *)data;
4267 struct i40e_hw *hw = &pf->hw;
4268 irqreturn_t ret = IRQ_NONE;
4269 u32 icr0, icr0_remaining;
4272 icr0 = rd32(hw, I40E_PFINT_ICR0);
4273 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4275 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
4276 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4279 /* if interrupt but no bits showing, must be SWINT */
4280 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4281 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4284 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4285 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4286 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4287 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4288 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4291 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4292 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4293 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4294 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4296 /* We do not have a way to disarm Queue causes while leaving
4297 * interrupt enabled for all other causes, ideally
4298 * interrupt should be disabled while we are in NAPI but
4299 * this is not a performance path and napi_schedule()
4300 * can deal with rescheduling.
4302 if (!test_bit(__I40E_DOWN, pf->state))
4303 napi_schedule_irqoff(&q_vector->napi);
4306 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4307 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4308 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4309 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4312 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4313 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4314 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4317 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4318 /* disable any further VFLR event notifications */
4319 if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4320 u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4322 reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4323 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4325 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4326 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4330 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4331 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4332 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4333 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4334 val = rd32(hw, I40E_GLGEN_RSTAT);
4335 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4336 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4337 if (val == I40E_RESET_CORER) {
4339 } else if (val == I40E_RESET_GLOBR) {
4341 } else if (val == I40E_RESET_EMPR) {
4343 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4347 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4348 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4349 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4350 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4351 rd32(hw, I40E_PFHMC_ERRORINFO),
4352 rd32(hw, I40E_PFHMC_ERRORDATA));
4355 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4356 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4358 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4359 schedule_work(&pf->ptp_extts0_work);
4361 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4362 i40e_ptp_tx_hwtstamp(pf);
4364 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4367 /* If a critical error is pending we have no choice but to reset the
4369 * Report and mask out any remaining unexpected interrupts.
4371 icr0_remaining = icr0 & ena_mask;
4372 if (icr0_remaining) {
4373 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4375 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4376 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4377 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4378 dev_info(&pf->pdev->dev, "device will be reset\n");
4379 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4380 i40e_service_event_schedule(pf);
4382 ena_mask &= ~icr0_remaining;
4387 /* re-enable interrupt causes */
4388 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4389 if (!test_bit(__I40E_DOWN, pf->state) ||
4390 test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4391 i40e_service_event_schedule(pf);
4392 i40e_irq_dynamic_enable_icr0(pf);
4399 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4400 * @tx_ring: tx ring to clean
4401 * @budget: how many cleans we're allowed
4403 * Returns true if there's any budget left (e.g. the clean is finished)
4405 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4407 struct i40e_vsi *vsi = tx_ring->vsi;
4408 u16 i = tx_ring->next_to_clean;
4409 struct i40e_tx_buffer *tx_buf;
4410 struct i40e_tx_desc *tx_desc;
4412 tx_buf = &tx_ring->tx_bi[i];
4413 tx_desc = I40E_TX_DESC(tx_ring, i);
4414 i -= tx_ring->count;
4417 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4419 /* if next_to_watch is not set then there is no work pending */
4423 /* prevent any other reads prior to eop_desc */
4426 /* if the descriptor isn't done, no work yet to do */
4427 if (!(eop_desc->cmd_type_offset_bsz &
4428 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4431 /* clear next_to_watch to prevent false hangs */
4432 tx_buf->next_to_watch = NULL;
4434 tx_desc->buffer_addr = 0;
4435 tx_desc->cmd_type_offset_bsz = 0;
4436 /* move past filter desc */
4441 i -= tx_ring->count;
4442 tx_buf = tx_ring->tx_bi;
4443 tx_desc = I40E_TX_DESC(tx_ring, 0);
4445 /* unmap skb header data */
4446 dma_unmap_single(tx_ring->dev,
4447 dma_unmap_addr(tx_buf, dma),
4448 dma_unmap_len(tx_buf, len),
4450 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4451 kfree(tx_buf->raw_buf);
4453 tx_buf->raw_buf = NULL;
4454 tx_buf->tx_flags = 0;
4455 tx_buf->next_to_watch = NULL;
4456 dma_unmap_len_set(tx_buf, len, 0);
4457 tx_desc->buffer_addr = 0;
4458 tx_desc->cmd_type_offset_bsz = 0;
4460 /* move us past the eop_desc for start of next FD desc */
4465 i -= tx_ring->count;
4466 tx_buf = tx_ring->tx_bi;
4467 tx_desc = I40E_TX_DESC(tx_ring, 0);
4470 /* update budget accounting */
4472 } while (likely(budget));
4474 i += tx_ring->count;
4475 tx_ring->next_to_clean = i;
4477 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4478 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4484 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4485 * @irq: interrupt number
4486 * @data: pointer to a q_vector
4488 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4490 struct i40e_q_vector *q_vector = data;
4491 struct i40e_vsi *vsi;
4493 if (!q_vector->tx.ring)
4496 vsi = q_vector->tx.ring->vsi;
4497 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4503 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4504 * @vsi: the VSI being configured
4505 * @v_idx: vector index
4506 * @qp_idx: queue pair index
4508 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4510 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4511 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4512 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4514 tx_ring->q_vector = q_vector;
4515 tx_ring->next = q_vector->tx.ring;
4516 q_vector->tx.ring = tx_ring;
4517 q_vector->tx.count++;
4519 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4520 if (i40e_enabled_xdp_vsi(vsi)) {
4521 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4523 xdp_ring->q_vector = q_vector;
4524 xdp_ring->next = q_vector->tx.ring;
4525 q_vector->tx.ring = xdp_ring;
4526 q_vector->tx.count++;
4529 rx_ring->q_vector = q_vector;
4530 rx_ring->next = q_vector->rx.ring;
4531 q_vector->rx.ring = rx_ring;
4532 q_vector->rx.count++;
4536 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4537 * @vsi: the VSI being configured
4539 * This function maps descriptor rings to the queue-specific vectors
4540 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4541 * one vector per queue pair, but on a constrained vector budget, we
4542 * group the queue pairs as "efficiently" as possible.
4544 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4546 int qp_remaining = vsi->num_queue_pairs;
4547 int q_vectors = vsi->num_q_vectors;
4552 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4553 * group them so there are multiple queues per vector.
4554 * It is also important to go through all the vectors available to be
4555 * sure that if we don't use all the vectors, that the remaining vectors
4556 * are cleared. This is especially important when decreasing the
4557 * number of queues in use.
4559 for (; v_start < q_vectors; v_start++) {
4560 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4562 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4564 q_vector->num_ringpairs = num_ringpairs;
4565 q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4567 q_vector->rx.count = 0;
4568 q_vector->tx.count = 0;
4569 q_vector->rx.ring = NULL;
4570 q_vector->tx.ring = NULL;
4572 while (num_ringpairs--) {
4573 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4581 * i40e_vsi_request_irq - Request IRQ from the OS
4582 * @vsi: the VSI being configured
4583 * @basename: name for the vector
4585 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4587 struct i40e_pf *pf = vsi->back;
4590 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4591 err = i40e_vsi_request_irq_msix(vsi, basename);
4592 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4593 err = request_irq(pf->pdev->irq, i40e_intr, 0,
4596 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4600 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4605 #ifdef CONFIG_NET_POLL_CONTROLLER
4607 * i40e_netpoll - A Polling 'interrupt' handler
4608 * @netdev: network interface device structure
4610 * This is used by netconsole to send skbs without having to re-enable
4611 * interrupts. It's not called while the normal interrupt routine is executing.
4613 static void i40e_netpoll(struct net_device *netdev)
4615 struct i40e_netdev_priv *np = netdev_priv(netdev);
4616 struct i40e_vsi *vsi = np->vsi;
4617 struct i40e_pf *pf = vsi->back;
4620 /* if interface is down do nothing */
4621 if (test_bit(__I40E_VSI_DOWN, vsi->state))
4624 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4625 for (i = 0; i < vsi->num_q_vectors; i++)
4626 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4628 i40e_intr(pf->pdev->irq, netdev);
4633 #define I40E_QTX_ENA_WAIT_COUNT 50
4636 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4637 * @pf: the PF being configured
4638 * @pf_q: the PF queue
4639 * @enable: enable or disable state of the queue
4641 * This routine will wait for the given Tx queue of the PF to reach the
4642 * enabled or disabled state.
4643 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4644 * multiple retries; else will return 0 in case of success.
4646 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4651 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4652 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4653 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4656 usleep_range(10, 20);
4658 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4665 * i40e_control_tx_q - Start or stop a particular Tx queue
4666 * @pf: the PF structure
4667 * @pf_q: the PF queue to configure
4668 * @enable: start or stop the queue
4670 * This function enables or disables a single queue. Note that any delay
4671 * required after the operation is expected to be handled by the caller of
4674 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4676 struct i40e_hw *hw = &pf->hw;
4680 /* warn the TX unit of coming changes */
4681 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4683 usleep_range(10, 20);
4685 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4686 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4687 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4688 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4690 usleep_range(1000, 2000);
4693 /* Skip if the queue is already in the requested state */
4694 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4697 /* turn on/off the queue */
4699 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4700 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4702 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4705 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4709 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4711 * @pf: the PF structure
4712 * @pf_q: the PF queue to configure
4713 * @is_xdp: true if the queue is used for XDP
4714 * @enable: start or stop the queue
4716 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4717 bool is_xdp, bool enable)
4721 i40e_control_tx_q(pf, pf_q, enable);
4723 /* wait for the change to finish */
4724 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4726 dev_info(&pf->pdev->dev,
4727 "VSI seid %d %sTx ring %d %sable timeout\n",
4728 seid, (is_xdp ? "XDP " : ""), pf_q,
4729 (enable ? "en" : "dis"));
4736 * i40e_vsi_enable_tx - Start a VSI's rings
4737 * @vsi: the VSI being configured
4739 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4741 struct i40e_pf *pf = vsi->back;
4742 int i, pf_q, ret = 0;
4744 pf_q = vsi->base_queue;
4745 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4746 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4748 false /*is xdp*/, true);
4752 if (!i40e_enabled_xdp_vsi(vsi))
4755 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4756 pf_q + vsi->alloc_queue_pairs,
4757 true /*is xdp*/, true);
4765 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4766 * @pf: the PF being configured
4767 * @pf_q: the PF queue
4768 * @enable: enable or disable state of the queue
4770 * This routine will wait for the given Rx queue of the PF to reach the
4771 * enabled or disabled state.
4772 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4773 * multiple retries; else will return 0 in case of success.
4775 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4780 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4781 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4782 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4785 usleep_range(10, 20);
4787 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4794 * i40e_control_rx_q - Start or stop a particular Rx queue
4795 * @pf: the PF structure
4796 * @pf_q: the PF queue to configure
4797 * @enable: start or stop the queue
4799 * This function enables or disables a single queue. Note that
4800 * any delay required after the operation is expected to be
4801 * handled by the caller of this function.
4803 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4805 struct i40e_hw *hw = &pf->hw;
4809 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4810 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4811 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4812 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4814 usleep_range(1000, 2000);
4817 /* Skip if the queue is already in the requested state */
4818 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4821 /* turn on/off the queue */
4823 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4825 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4827 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4831 * i40e_control_wait_rx_q
4832 * @pf: the PF structure
4833 * @pf_q: queue being configured
4834 * @enable: start or stop the rings
4836 * This function enables or disables a single queue along with waiting
4837 * for the change to finish. The caller of this function should handle
4838 * the delays needed in the case of disabling queues.
4840 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4844 i40e_control_rx_q(pf, pf_q, enable);
4846 /* wait for the change to finish */
4847 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4855 * i40e_vsi_enable_rx - Start a VSI's rings
4856 * @vsi: the VSI being configured
4858 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4860 struct i40e_pf *pf = vsi->back;
4861 int i, pf_q, ret = 0;
4863 pf_q = vsi->base_queue;
4864 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4865 ret = i40e_control_wait_rx_q(pf, pf_q, true);
4867 dev_info(&pf->pdev->dev,
4868 "VSI seid %d Rx ring %d enable timeout\n",
4878 * i40e_vsi_start_rings - Start a VSI's rings
4879 * @vsi: the VSI being configured
4881 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4885 /* do rx first for enable and last for disable */
4886 ret = i40e_vsi_enable_rx(vsi);
4889 ret = i40e_vsi_enable_tx(vsi);
4894 #define I40E_DISABLE_TX_GAP_MSEC 50
4897 * i40e_vsi_stop_rings - Stop a VSI's rings
4898 * @vsi: the VSI being configured
4900 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4902 struct i40e_pf *pf = vsi->back;
4903 int pf_q, err, q_end;
4905 /* When port TX is suspended, don't wait */
4906 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4907 return i40e_vsi_stop_rings_no_wait(vsi);
4909 q_end = vsi->base_queue + vsi->num_queue_pairs;
4910 for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4911 i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4913 for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4914 err = i40e_control_wait_rx_q(pf, pf_q, false);
4916 dev_info(&pf->pdev->dev,
4917 "VSI seid %d Rx ring %d disable timeout\n",
4921 msleep(I40E_DISABLE_TX_GAP_MSEC);
4922 pf_q = vsi->base_queue;
4923 for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4924 wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4926 i40e_vsi_wait_queues_disabled(vsi);
4930 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4931 * @vsi: the VSI being shutdown
4933 * This function stops all the rings for a VSI but does not delay to verify
4934 * that rings have been disabled. It is expected that the caller is shutting
4935 * down multiple VSIs at once and will delay together for all the VSIs after
4936 * initiating the shutdown. This is particularly useful for shutting down lots
4937 * of VFs together. Otherwise, a large delay can be incurred while configuring
4938 * each VSI in serial.
4940 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4942 struct i40e_pf *pf = vsi->back;
4945 pf_q = vsi->base_queue;
4946 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4947 i40e_control_tx_q(pf, pf_q, false);
4948 i40e_control_rx_q(pf, pf_q, false);
4953 * i40e_vsi_free_irq - Free the irq association with the OS
4954 * @vsi: the VSI being configured
4956 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4958 struct i40e_pf *pf = vsi->back;
4959 struct i40e_hw *hw = &pf->hw;
4960 int base = vsi->base_vector;
4964 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4965 if (!vsi->q_vectors)
4968 if (!vsi->irqs_ready)
4971 vsi->irqs_ready = false;
4972 for (i = 0; i < vsi->num_q_vectors; i++) {
4977 irq_num = pf->msix_entries[vector].vector;
4979 /* free only the irqs that were actually requested */
4980 if (!vsi->q_vectors[i] ||
4981 !vsi->q_vectors[i]->num_ringpairs)
4984 /* clear the affinity notifier in the IRQ descriptor */
4985 irq_set_affinity_notifier(irq_num, NULL);
4986 /* remove our suggested affinity mask for this IRQ */
4987 irq_update_affinity_hint(irq_num, NULL);
4988 free_irq(irq_num, vsi->q_vectors[i]);
4990 /* Tear down the interrupt queue link list
4992 * We know that they come in pairs and always
4993 * the Rx first, then the Tx. To clear the
4994 * link list, stick the EOL value into the
4995 * next_q field of the registers.
4997 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4998 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4999 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5000 val |= I40E_QUEUE_END_OF_LIST
5001 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5002 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5004 while (qp != I40E_QUEUE_END_OF_LIST) {
5007 val = rd32(hw, I40E_QINT_RQCTL(qp));
5009 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
5010 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5011 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
5012 I40E_QINT_RQCTL_INTEVENT_MASK);
5014 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5015 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5017 wr32(hw, I40E_QINT_RQCTL(qp), val);
5019 val = rd32(hw, I40E_QINT_TQCTL(qp));
5021 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5022 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5024 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
5025 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5026 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
5027 I40E_QINT_TQCTL_INTEVENT_MASK);
5029 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5030 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5032 wr32(hw, I40E_QINT_TQCTL(qp), val);
5037 free_irq(pf->pdev->irq, pf);
5039 val = rd32(hw, I40E_PFINT_LNKLST0);
5040 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5041 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5042 val |= I40E_QUEUE_END_OF_LIST
5043 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5044 wr32(hw, I40E_PFINT_LNKLST0, val);
5046 val = rd32(hw, I40E_QINT_RQCTL(qp));
5047 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
5048 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5049 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
5050 I40E_QINT_RQCTL_INTEVENT_MASK);
5052 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5053 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5055 wr32(hw, I40E_QINT_RQCTL(qp), val);
5057 val = rd32(hw, I40E_QINT_TQCTL(qp));
5059 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
5060 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5061 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
5062 I40E_QINT_TQCTL_INTEVENT_MASK);
5064 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5065 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5067 wr32(hw, I40E_QINT_TQCTL(qp), val);
5072 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5073 * @vsi: the VSI being configured
5074 * @v_idx: Index of vector to be freed
5076 * This function frees the memory allocated to the q_vector. In addition if
5077 * NAPI is enabled it will delete any references to the NAPI struct prior
5078 * to freeing the q_vector.
5080 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5082 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5083 struct i40e_ring *ring;
5088 /* disassociate q_vector from rings */
5089 i40e_for_each_ring(ring, q_vector->tx)
5090 ring->q_vector = NULL;
5092 i40e_for_each_ring(ring, q_vector->rx)
5093 ring->q_vector = NULL;
5095 /* only VSI w/ an associated netdev is set up w/ NAPI */
5097 netif_napi_del(&q_vector->napi);
5099 vsi->q_vectors[v_idx] = NULL;
5101 kfree_rcu(q_vector, rcu);
5105 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5106 * @vsi: the VSI being un-configured
5108 * This frees the memory allocated to the q_vectors and
5109 * deletes references to the NAPI struct.
5111 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5115 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5116 i40e_free_q_vector(vsi, v_idx);
5120 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5121 * @pf: board private structure
5123 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5125 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5126 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5127 pci_disable_msix(pf->pdev);
5128 kfree(pf->msix_entries);
5129 pf->msix_entries = NULL;
5130 kfree(pf->irq_pile);
5131 pf->irq_pile = NULL;
5132 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5133 pci_disable_msi(pf->pdev);
5135 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5139 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5140 * @pf: board private structure
5142 * We go through and clear interrupt specific resources and reset the structure
5143 * to pre-load conditions
5145 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5149 if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5150 i40e_free_misc_vector(pf);
5152 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5153 I40E_IWARP_IRQ_PILE_ID);
5155 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5156 for (i = 0; i < pf->num_alloc_vsi; i++)
5158 i40e_vsi_free_q_vectors(pf->vsi[i]);
5159 i40e_reset_interrupt_capability(pf);
5163 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5164 * @vsi: the VSI being configured
5166 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5173 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5174 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5176 if (q_vector->rx.ring || q_vector->tx.ring)
5177 napi_enable(&q_vector->napi);
5182 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5183 * @vsi: the VSI being configured
5185 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5192 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5193 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5195 if (q_vector->rx.ring || q_vector->tx.ring)
5196 napi_disable(&q_vector->napi);
5201 * i40e_vsi_close - Shut down a VSI
5202 * @vsi: the vsi to be quelled
5204 static void i40e_vsi_close(struct i40e_vsi *vsi)
5206 struct i40e_pf *pf = vsi->back;
5207 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5209 i40e_vsi_free_irq(vsi);
5210 i40e_vsi_free_tx_resources(vsi);
5211 i40e_vsi_free_rx_resources(vsi);
5212 vsi->current_netdev_flags = 0;
5213 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5214 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5215 set_bit(__I40E_CLIENT_RESET, pf->state);
5219 * i40e_quiesce_vsi - Pause a given VSI
5220 * @vsi: the VSI being paused
5222 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5224 if (test_bit(__I40E_VSI_DOWN, vsi->state))
5227 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5228 if (vsi->netdev && netif_running(vsi->netdev))
5229 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5231 i40e_vsi_close(vsi);
5235 * i40e_unquiesce_vsi - Resume a given VSI
5236 * @vsi: the VSI being resumed
5238 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5240 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5243 if (vsi->netdev && netif_running(vsi->netdev))
5244 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5246 i40e_vsi_open(vsi); /* this clears the DOWN bit */
5250 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5253 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5257 for (v = 0; v < pf->num_alloc_vsi; v++) {
5259 i40e_quiesce_vsi(pf->vsi[v]);
5264 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5267 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5271 for (v = 0; v < pf->num_alloc_vsi; v++) {
5273 i40e_unquiesce_vsi(pf->vsi[v]);
5278 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5279 * @vsi: the VSI being configured
5281 * Wait until all queues on a given VSI have been disabled.
5283 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5285 struct i40e_pf *pf = vsi->back;
5288 pf_q = vsi->base_queue;
5289 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5290 /* Check and wait for the Tx queue */
5291 ret = i40e_pf_txq_wait(pf, pf_q, false);
5293 dev_info(&pf->pdev->dev,
5294 "VSI seid %d Tx ring %d disable timeout\n",
5299 if (!i40e_enabled_xdp_vsi(vsi))
5302 /* Check and wait for the XDP Tx queue */
5303 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5306 dev_info(&pf->pdev->dev,
5307 "VSI seid %d XDP Tx ring %d disable timeout\n",
5312 /* Check and wait for the Rx queue */
5313 ret = i40e_pf_rxq_wait(pf, pf_q, false);
5315 dev_info(&pf->pdev->dev,
5316 "VSI seid %d Rx ring %d disable timeout\n",
5325 #ifdef CONFIG_I40E_DCB
5327 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5330 * This function waits for the queues to be in disabled state for all the
5331 * VSIs that are managed by this PF.
5333 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5337 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5339 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5351 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5352 * @pf: pointer to PF
5354 * Get TC map for ISCSI PF type that will include iSCSI TC
5357 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5359 struct i40e_dcb_app_priority_table app;
5360 struct i40e_hw *hw = &pf->hw;
5361 u8 enabled_tc = 1; /* TC0 is always enabled */
5363 /* Get the iSCSI APP TLV */
5364 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5366 for (i = 0; i < dcbcfg->numapps; i++) {
5367 app = dcbcfg->app[i];
5368 if (app.selector == I40E_APP_SEL_TCPIP &&
5369 app.protocolid == I40E_APP_PROTOID_ISCSI) {
5370 tc = dcbcfg->etscfg.prioritytable[app.priority];
5371 enabled_tc |= BIT(tc);
5380 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
5381 * @dcbcfg: the corresponding DCBx configuration structure
5383 * Return the number of TCs from given DCBx configuration
5385 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5387 int i, tc_unused = 0;
5391 /* Scan the ETS Config Priority Table to find
5392 * traffic class enabled for a given priority
5393 * and create a bitmask of enabled TCs
5395 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5396 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5398 /* Now scan the bitmask to check for
5399 * contiguous TCs starting with TC0
5401 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5402 if (num_tc & BIT(i)) {
5406 pr_err("Non-contiguous TC - Disabling DCB\n");
5414 /* There is always at least TC0 */
5422 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5423 * @dcbcfg: the corresponding DCBx configuration structure
5425 * Query the current DCB configuration and return the number of
5426 * traffic classes enabled from the given DCBX config
5428 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5430 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5434 for (i = 0; i < num_tc; i++)
5435 enabled_tc |= BIT(i);
5441 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5442 * @pf: PF being queried
5444 * Query the current MQPRIO configuration and return the number of
5445 * traffic classes enabled.
5447 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5449 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5450 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5451 u8 enabled_tc = 1, i;
5453 for (i = 1; i < num_tc; i++)
5454 enabled_tc |= BIT(i);
5459 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5460 * @pf: PF being queried
5462 * Return number of traffic classes enabled for the given PF
5464 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5466 struct i40e_hw *hw = &pf->hw;
5467 u8 i, enabled_tc = 1;
5469 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5471 if (i40e_is_tc_mqprio_enabled(pf))
5472 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5474 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
5475 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5478 /* SFP mode will be enabled for all TCs on port */
5479 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5480 return i40e_dcb_get_num_tc(dcbcfg);
5482 /* MFP mode return count of enabled TCs for this PF */
5483 if (pf->hw.func_caps.iscsi)
5484 enabled_tc = i40e_get_iscsi_tc_map(pf);
5486 return 1; /* Only TC0 */
5488 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5489 if (enabled_tc & BIT(i))
5496 * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5497 * @pf: PF being queried
5499 * Return a bitmap for enabled traffic classes for this PF.
5501 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5503 if (i40e_is_tc_mqprio_enabled(pf))
5504 return i40e_mqprio_get_enabled_tc(pf);
5506 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5509 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5510 return I40E_DEFAULT_TRAFFIC_CLASS;
5512 /* SFP mode we want PF to be enabled for all TCs */
5513 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5514 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5516 /* MFP enabled and iSCSI PF type */
5517 if (pf->hw.func_caps.iscsi)
5518 return i40e_get_iscsi_tc_map(pf);
5520 return I40E_DEFAULT_TRAFFIC_CLASS;
5524 * i40e_vsi_get_bw_info - Query VSI BW Information
5525 * @vsi: the VSI being queried
5527 * Returns 0 on success, negative value on failure
5529 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5531 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5532 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5533 struct i40e_pf *pf = vsi->back;
5534 struct i40e_hw *hw = &pf->hw;
5539 /* Get the VSI level BW configuration */
5540 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5542 dev_info(&pf->pdev->dev,
5543 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5545 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5549 /* Get the VSI level BW configuration per TC */
5550 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5553 dev_info(&pf->pdev->dev,
5554 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5556 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5560 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5561 dev_info(&pf->pdev->dev,
5562 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5563 bw_config.tc_valid_bits,
5564 bw_ets_config.tc_valid_bits);
5565 /* Still continuing */
5568 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5569 vsi->bw_max_quanta = bw_config.max_bw;
5570 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5571 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5572 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5573 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5574 vsi->bw_ets_limit_credits[i] =
5575 le16_to_cpu(bw_ets_config.credits[i]);
5576 /* 3 bits out of 4 for each TC */
5577 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5584 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5585 * @vsi: the VSI being configured
5586 * @enabled_tc: TC bitmap
5587 * @bw_share: BW shared credits per TC
5589 * Returns 0 on success, negative value on failure
5591 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5594 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5595 struct i40e_pf *pf = vsi->back;
5599 /* There is no need to reset BW when mqprio mode is on. */
5600 if (i40e_is_tc_mqprio_enabled(pf))
5602 if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5603 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5605 dev_info(&pf->pdev->dev,
5606 "Failed to reset tx rate for vsi->seid %u\n",
5610 memset(&bw_data, 0, sizeof(bw_data));
5611 bw_data.tc_valid_bits = enabled_tc;
5612 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5613 bw_data.tc_bw_credits[i] = bw_share[i];
5615 ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5617 dev_info(&pf->pdev->dev,
5618 "AQ command Config VSI BW allocation per TC failed = %d\n",
5619 pf->hw.aq.asq_last_status);
5623 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5624 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5630 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5631 * @vsi: the VSI being configured
5632 * @enabled_tc: TC map to be enabled
5635 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5637 struct net_device *netdev = vsi->netdev;
5638 struct i40e_pf *pf = vsi->back;
5639 struct i40e_hw *hw = &pf->hw;
5642 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5648 netdev_reset_tc(netdev);
5652 /* Set up actual enabled TCs on the VSI */
5653 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5656 /* set per TC queues for the VSI */
5657 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5658 /* Only set TC queues for enabled tcs
5660 * e.g. For a VSI that has TC0 and TC3 enabled the
5661 * enabled_tc bitmap would be 0x00001001; the driver
5662 * will set the numtc for netdev as 2 that will be
5663 * referenced by the netdev layer as TC 0 and 1.
5665 if (vsi->tc_config.enabled_tc & BIT(i))
5666 netdev_set_tc_queue(netdev,
5667 vsi->tc_config.tc_info[i].netdev_tc,
5668 vsi->tc_config.tc_info[i].qcount,
5669 vsi->tc_config.tc_info[i].qoffset);
5672 if (i40e_is_tc_mqprio_enabled(pf))
5675 /* Assign UP2TC map for the VSI */
5676 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5677 /* Get the actual TC# for the UP */
5678 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5679 /* Get the mapped netdev TC# for the UP */
5680 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5681 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5686 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5687 * @vsi: the VSI being configured
5688 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5690 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5691 struct i40e_vsi_context *ctxt)
5693 /* copy just the sections touched not the entire info
5694 * since not all sections are valid as returned by
5697 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5698 memcpy(&vsi->info.queue_mapping,
5699 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5700 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5701 sizeof(vsi->info.tc_mapping));
5705 * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5706 * @vsi: the VSI being reconfigured
5707 * @vsi_offset: offset from main VF VSI
5709 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5711 struct i40e_vsi_context ctxt = {};
5717 return I40E_ERR_PARAM;
5721 ctxt.seid = vsi->seid;
5722 ctxt.pf_num = hw->pf_id;
5723 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5724 ctxt.uplink_seid = vsi->uplink_seid;
5725 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5726 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5727 ctxt.info = vsi->info;
5729 i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5731 if (vsi->reconfig_rss) {
5732 vsi->rss_size = min_t(int, pf->alloc_rss_size,
5733 vsi->num_queue_pairs);
5734 ret = i40e_vsi_config_rss(vsi);
5736 dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5739 vsi->reconfig_rss = false;
5742 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5744 dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5746 i40e_aq_str(hw, hw->aq.asq_last_status));
5749 /* update the local VSI info with updated queue map */
5750 i40e_vsi_update_queue_map(vsi, &ctxt);
5751 vsi->info.valid_sections = 0;
5757 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5758 * @vsi: VSI to be configured
5759 * @enabled_tc: TC bitmap
5761 * This configures a particular VSI for TCs that are mapped to the
5762 * given TC bitmap. It uses default bandwidth share for TCs across
5763 * VSIs to configure TC for a particular VSI.
5766 * It is expected that the VSI queues have been quisced before calling
5769 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5771 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5772 struct i40e_pf *pf = vsi->back;
5773 struct i40e_hw *hw = &pf->hw;
5774 struct i40e_vsi_context ctxt;
5778 /* Check if enabled_tc is same as existing or new TCs */
5779 if (vsi->tc_config.enabled_tc == enabled_tc &&
5780 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5783 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5784 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5785 if (enabled_tc & BIT(i))
5789 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5791 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5793 dev_info(&pf->pdev->dev,
5794 "Failed configuring TC map %d for VSI %d\n",
5795 enabled_tc, vsi->seid);
5796 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5799 dev_info(&pf->pdev->dev,
5800 "Failed querying vsi bw info, err %pe aq_err %s\n",
5802 i40e_aq_str(hw, hw->aq.asq_last_status));
5805 if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5806 u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5809 valid_tc = bw_config.tc_valid_bits;
5810 /* Always enable TC0, no matter what */
5812 dev_info(&pf->pdev->dev,
5813 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5814 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5815 enabled_tc = valid_tc;
5818 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5820 dev_err(&pf->pdev->dev,
5821 "Unable to configure TC map %d for VSI %d\n",
5822 enabled_tc, vsi->seid);
5827 /* Update Queue Pairs Mapping for currently enabled UPs */
5828 ctxt.seid = vsi->seid;
5829 ctxt.pf_num = vsi->back->hw.pf_id;
5831 ctxt.uplink_seid = vsi->uplink_seid;
5832 ctxt.info = vsi->info;
5833 if (i40e_is_tc_mqprio_enabled(pf)) {
5834 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5838 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5841 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5844 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5845 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5846 vsi->num_queue_pairs);
5847 ret = i40e_vsi_config_rss(vsi);
5849 dev_info(&vsi->back->pdev->dev,
5850 "Failed to reconfig rss for num_queues\n");
5853 vsi->reconfig_rss = false;
5855 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5856 ctxt.info.valid_sections |=
5857 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5858 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5861 /* Update the VSI after updating the VSI queue-mapping
5864 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5866 dev_info(&pf->pdev->dev,
5867 "Update vsi tc config failed, err %pe aq_err %s\n",
5869 i40e_aq_str(hw, hw->aq.asq_last_status));
5872 /* update the local VSI info with updated queue map */
5873 i40e_vsi_update_queue_map(vsi, &ctxt);
5874 vsi->info.valid_sections = 0;
5876 /* Update current VSI BW information */
5877 ret = i40e_vsi_get_bw_info(vsi);
5879 dev_info(&pf->pdev->dev,
5880 "Failed updating vsi bw info, err %pe aq_err %s\n",
5882 i40e_aq_str(hw, hw->aq.asq_last_status));
5886 /* Update the netdev TC setup */
5887 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5893 * i40e_get_link_speed - Returns link speed for the interface
5894 * @vsi: VSI to be configured
5897 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5899 struct i40e_pf *pf = vsi->back;
5901 switch (pf->hw.phy.link_info.link_speed) {
5902 case I40E_LINK_SPEED_40GB:
5904 case I40E_LINK_SPEED_25GB:
5906 case I40E_LINK_SPEED_20GB:
5908 case I40E_LINK_SPEED_10GB:
5910 case I40E_LINK_SPEED_1GB:
5918 * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5919 * @vsi: Pointer to vsi structure
5920 * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5922 * Helper function to convert units before send to set BW limit
5924 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5926 if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5927 dev_warn(&vsi->back->pdev->dev,
5928 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5929 max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5931 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5938 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5939 * @vsi: VSI to be configured
5940 * @seid: seid of the channel/VSI
5941 * @max_tx_rate: max TX rate to be configured as BW limit
5943 * Helper function to set BW limit for a given VSI
5945 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5947 struct i40e_pf *pf = vsi->back;
5952 speed = i40e_get_link_speed(vsi);
5953 if (max_tx_rate > speed) {
5954 dev_err(&pf->pdev->dev,
5955 "Invalid max tx rate %llu specified for VSI seid %d.",
5959 if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5960 dev_warn(&pf->pdev->dev,
5961 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5962 max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5965 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
5966 credits = max_tx_rate;
5967 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5968 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5969 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5971 dev_err(&pf->pdev->dev,
5972 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5973 max_tx_rate, seid, ERR_PTR(ret),
5974 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5979 * i40e_remove_queue_channels - Remove queue channels for the TCs
5980 * @vsi: VSI to be configured
5982 * Remove queue channels for the TCs
5984 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5986 enum i40e_admin_queue_err last_aq_status;
5987 struct i40e_cloud_filter *cfilter;
5988 struct i40e_channel *ch, *ch_tmp;
5989 struct i40e_pf *pf = vsi->back;
5990 struct hlist_node *node;
5993 /* Reset rss size that was stored when reconfiguring rss for
5994 * channel VSIs with non-power-of-2 queue count.
5996 vsi->current_rss_size = 0;
5998 /* perform cleanup for channels if they exist */
5999 if (list_empty(&vsi->ch_list))
6002 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6003 struct i40e_vsi *p_vsi;
6005 list_del(&ch->list);
6006 p_vsi = ch->parent_vsi;
6007 if (!p_vsi || !ch->initialized) {
6011 /* Reset queue contexts */
6012 for (i = 0; i < ch->num_queue_pairs; i++) {
6013 struct i40e_ring *tx_ring, *rx_ring;
6016 pf_q = ch->base_queue + i;
6017 tx_ring = vsi->tx_rings[pf_q];
6020 rx_ring = vsi->rx_rings[pf_q];
6024 /* Reset BW configured for this VSI via mqprio */
6025 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6027 dev_info(&vsi->back->pdev->dev,
6028 "Failed to reset tx rate for ch->seid %u\n",
6031 /* delete cloud filters associated with this channel */
6032 hlist_for_each_entry_safe(cfilter, node,
6033 &pf->cloud_filter_list, cloud_node) {
6034 if (cfilter->seid != ch->seid)
6037 hash_del(&cfilter->cloud_node);
6038 if (cfilter->dst_port)
6039 ret = i40e_add_del_cloud_filter_big_buf(vsi,
6043 ret = i40e_add_del_cloud_filter(vsi, cfilter,
6045 last_aq_status = pf->hw.aq.asq_last_status;
6047 dev_info(&pf->pdev->dev,
6048 "Failed to delete cloud filter, err %pe aq_err %s\n",
6050 i40e_aq_str(&pf->hw, last_aq_status));
6054 /* delete VSI from FW */
6055 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6058 dev_err(&vsi->back->pdev->dev,
6059 "unable to remove channel (%d) for parent VSI(%d)\n",
6060 ch->seid, p_vsi->seid);
6063 INIT_LIST_HEAD(&vsi->ch_list);
6067 * i40e_get_max_queues_for_channel
6068 * @vsi: ptr to VSI to which channels are associated with
6070 * Helper function which returns max value among the queue counts set on the
6071 * channels/TCs created.
6073 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6075 struct i40e_channel *ch, *ch_tmp;
6078 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6079 if (!ch->initialized)
6081 if (ch->num_queue_pairs > max)
6082 max = ch->num_queue_pairs;
6089 * i40e_validate_num_queues - validate num_queues w.r.t channel
6090 * @pf: ptr to PF device
6091 * @num_queues: number of queues
6092 * @vsi: the parent VSI
6093 * @reconfig_rss: indicates should the RSS be reconfigured or not
6095 * This function validates number of queues in the context of new channel
6096 * which is being established and determines if RSS should be reconfigured
6097 * or not for parent VSI.
6099 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6100 struct i40e_vsi *vsi, bool *reconfig_rss)
6107 *reconfig_rss = false;
6108 if (vsi->current_rss_size) {
6109 if (num_queues > vsi->current_rss_size) {
6110 dev_dbg(&pf->pdev->dev,
6111 "Error: num_queues (%d) > vsi's current_size(%d)\n",
6112 num_queues, vsi->current_rss_size);
6114 } else if ((num_queues < vsi->current_rss_size) &&
6115 (!is_power_of_2(num_queues))) {
6116 dev_dbg(&pf->pdev->dev,
6117 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6118 num_queues, vsi->current_rss_size);
6123 if (!is_power_of_2(num_queues)) {
6124 /* Find the max num_queues configured for channel if channel
6126 * if channel exist, then enforce 'num_queues' to be more than
6127 * max ever queues configured for channel.
6129 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6130 if (num_queues < max_ch_queues) {
6131 dev_dbg(&pf->pdev->dev,
6132 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
6133 num_queues, max_ch_queues);
6136 *reconfig_rss = true;
6143 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6144 * @vsi: the VSI being setup
6145 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6147 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6149 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6151 struct i40e_pf *pf = vsi->back;
6152 u8 seed[I40E_HKEY_ARRAY_SIZE];
6153 struct i40e_hw *hw = &pf->hw;
6161 if (rss_size > vsi->rss_size)
6164 local_rss_size = min_t(int, vsi->rss_size, rss_size);
6165 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6169 /* Ignoring user configured lut if there is one */
6170 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6172 /* Use user configured hash key if there is one, otherwise
6175 if (vsi->rss_hkey_user)
6176 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6178 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6180 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6182 dev_info(&pf->pdev->dev,
6183 "Cannot set RSS lut, err %pe aq_err %s\n",
6185 i40e_aq_str(hw, hw->aq.asq_last_status));
6191 /* Do the update w.r.t. storing rss_size */
6192 if (!vsi->orig_rss_size)
6193 vsi->orig_rss_size = vsi->rss_size;
6194 vsi->current_rss_size = local_rss_size;
6200 * i40e_channel_setup_queue_map - Setup a channel queue map
6201 * @pf: ptr to PF device
6202 * @ctxt: VSI context structure
6203 * @ch: ptr to channel structure
6205 * Setup queue map for a specific channel
6207 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6208 struct i40e_vsi_context *ctxt,
6209 struct i40e_channel *ch)
6211 u16 qcount, qmap, sections = 0;
6215 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6216 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6218 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6219 ch->num_queue_pairs = qcount;
6221 /* find the next higher power-of-2 of num queue pairs */
6222 pow = ilog2(qcount);
6223 if (!is_power_of_2(qcount))
6226 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6227 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6229 /* Setup queue TC[0].qmap for given VSI context */
6230 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6232 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6233 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6234 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6235 ctxt->info.valid_sections |= cpu_to_le16(sections);
6239 * i40e_add_channel - add a channel by adding VSI
6240 * @pf: ptr to PF device
6241 * @uplink_seid: underlying HW switching element (VEB) ID
6242 * @ch: ptr to channel structure
6244 * Add a channel (VSI) using add_vsi and queue_map
6246 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6247 struct i40e_channel *ch)
6249 struct i40e_hw *hw = &pf->hw;
6250 struct i40e_vsi_context ctxt;
6251 u8 enabled_tc = 0x1; /* TC0 enabled */
6254 if (ch->type != I40E_VSI_VMDQ2) {
6255 dev_info(&pf->pdev->dev,
6256 "add new vsi failed, ch->type %d\n", ch->type);
6260 memset(&ctxt, 0, sizeof(ctxt));
6261 ctxt.pf_num = hw->pf_id;
6263 ctxt.uplink_seid = uplink_seid;
6264 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6265 if (ch->type == I40E_VSI_VMDQ2)
6266 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6268 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6269 ctxt.info.valid_sections |=
6270 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6271 ctxt.info.switch_id =
6272 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6275 /* Set queue map for a given VSI context */
6276 i40e_channel_setup_queue_map(pf, &ctxt, ch);
6278 /* Now time to create VSI */
6279 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6281 dev_info(&pf->pdev->dev,
6282 "add new vsi failed, err %pe aq_err %s\n",
6284 i40e_aq_str(&pf->hw,
6285 pf->hw.aq.asq_last_status));
6289 /* Success, update channel, set enabled_tc only if the channel
6292 ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6293 ch->seid = ctxt.seid;
6294 ch->vsi_number = ctxt.vsi_number;
6295 ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6297 /* copy just the sections touched not the entire info
6298 * since not all sections are valid as returned by
6301 ch->info.mapping_flags = ctxt.info.mapping_flags;
6302 memcpy(&ch->info.queue_mapping,
6303 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6304 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6305 sizeof(ctxt.info.tc_mapping));
6310 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6313 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6317 memset(&bw_data, 0, sizeof(bw_data));
6318 bw_data.tc_valid_bits = ch->enabled_tc;
6319 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6320 bw_data.tc_bw_credits[i] = bw_share[i];
6322 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6325 dev_info(&vsi->back->pdev->dev,
6326 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6327 vsi->back->hw.aq.asq_last_status, ch->seid);
6331 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6332 ch->info.qs_handle[i] = bw_data.qs_handles[i];
6338 * i40e_channel_config_tx_ring - config TX ring associated with new channel
6339 * @pf: ptr to PF device
6340 * @vsi: the VSI being setup
6341 * @ch: ptr to channel structure
6343 * Configure TX rings associated with channel (VSI) since queues are being
6346 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6347 struct i40e_vsi *vsi,
6348 struct i40e_channel *ch)
6350 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6354 /* Enable ETS TCs with equal BW Share for now across all VSIs */
6355 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6356 if (ch->enabled_tc & BIT(i))
6360 /* configure BW for new VSI */
6361 ret = i40e_channel_config_bw(vsi, ch, bw_share);
6363 dev_info(&vsi->back->pdev->dev,
6364 "Failed configuring TC map %d for channel (seid %u)\n",
6365 ch->enabled_tc, ch->seid);
6369 for (i = 0; i < ch->num_queue_pairs; i++) {
6370 struct i40e_ring *tx_ring, *rx_ring;
6373 pf_q = ch->base_queue + i;
6375 /* Get to TX ring ptr of main VSI, for re-setup TX queue
6378 tx_ring = vsi->tx_rings[pf_q];
6381 /* Get the RX ring ptr */
6382 rx_ring = vsi->rx_rings[pf_q];
6390 * i40e_setup_hw_channel - setup new channel
6391 * @pf: ptr to PF device
6392 * @vsi: the VSI being setup
6393 * @ch: ptr to channel structure
6394 * @uplink_seid: underlying HW switching element (VEB) ID
6395 * @type: type of channel to be created (VMDq2/VF)
6397 * Setup new channel (VSI) based on specified type (VMDq2/VF)
6398 * and configures TX rings accordingly
6400 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6401 struct i40e_vsi *vsi,
6402 struct i40e_channel *ch,
6403 u16 uplink_seid, u8 type)
6407 ch->initialized = false;
6408 ch->base_queue = vsi->next_base_queue;
6411 /* Proceed with creation of channel (VMDq2) VSI */
6412 ret = i40e_add_channel(pf, uplink_seid, ch);
6414 dev_info(&pf->pdev->dev,
6415 "failed to add_channel using uplink_seid %u\n",
6420 /* Mark the successful creation of channel */
6421 ch->initialized = true;
6423 /* Reconfigure TX queues using QTX_CTL register */
6424 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6426 dev_info(&pf->pdev->dev,
6427 "failed to configure TX rings for channel %u\n",
6432 /* update 'next_base_queue' */
6433 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6434 dev_dbg(&pf->pdev->dev,
6435 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6436 ch->seid, ch->vsi_number, ch->stat_counter_idx,
6437 ch->num_queue_pairs,
6438 vsi->next_base_queue);
6443 * i40e_setup_channel - setup new channel using uplink element
6444 * @pf: ptr to PF device
6445 * @vsi: pointer to the VSI to set up the channel within
6446 * @ch: ptr to channel structure
6448 * Setup new channel (VSI) based on specified type (VMDq2/VF)
6449 * and uplink switching element (uplink_seid)
6451 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6452 struct i40e_channel *ch)
6458 if (vsi->type == I40E_VSI_MAIN) {
6459 vsi_type = I40E_VSI_VMDQ2;
6461 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6466 /* underlying switching element */
6467 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6469 /* create channel (VSI), configure TX rings */
6470 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6472 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6476 return ch->initialized ? true : false;
6480 * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6481 * @vsi: ptr to VSI which has PF backing
6483 * Sets up switch mode correctly if it needs to be changed and perform
6484 * what are allowed modes.
6486 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6489 struct i40e_pf *pf = vsi->back;
6490 struct i40e_hw *hw = &pf->hw;
6493 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6497 if (hw->dev_caps.switch_mode) {
6498 /* if switch mode is set, support mode2 (non-tunneled for
6499 * cloud filter) for now
6501 u32 switch_mode = hw->dev_caps.switch_mode &
6502 I40E_SWITCH_MODE_MASK;
6503 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6504 if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6506 dev_err(&pf->pdev->dev,
6507 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6508 hw->dev_caps.switch_mode);
6513 /* Set Bit 7 to be valid */
6514 mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6516 /* Set L4type for TCP support */
6517 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6519 /* Set cloud filter mode */
6520 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6522 /* Prep mode field for set_switch_config */
6523 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6524 pf->last_sw_conf_valid_flags,
6526 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6527 dev_err(&pf->pdev->dev,
6528 "couldn't set switch config bits, err %pe aq_err %s\n",
6531 hw->aq.asq_last_status));
6537 * i40e_create_queue_channel - function to create channel
6538 * @vsi: VSI to be configured
6539 * @ch: ptr to channel (it contains channel specific params)
6541 * This function creates channel (VSI) using num_queues specified by user,
6542 * reconfigs RSS if needed.
6544 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6545 struct i40e_channel *ch)
6547 struct i40e_pf *pf = vsi->back;
6554 if (!ch->num_queue_pairs) {
6555 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6556 ch->num_queue_pairs);
6560 /* validate user requested num_queues for channel */
6561 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6564 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6565 ch->num_queue_pairs);
6569 /* By default we are in VEPA mode, if this is the first VF/VMDq
6570 * VSI to be added switch to VEB mode.
6573 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6574 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6576 if (vsi->type == I40E_VSI_MAIN) {
6577 if (i40e_is_tc_mqprio_enabled(pf))
6578 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6580 i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6582 /* now onwards for main VSI, number of queues will be value
6583 * of TC0's queue count
6587 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6588 * it should be more than num_queues
6590 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6591 dev_dbg(&pf->pdev->dev,
6592 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6593 vsi->cnt_q_avail, ch->num_queue_pairs);
6597 /* reconfig_rss only if vsi type is MAIN_VSI */
6598 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6599 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6601 dev_info(&pf->pdev->dev,
6602 "Error: unable to reconfig rss for num_queues (%u)\n",
6603 ch->num_queue_pairs);
6608 if (!i40e_setup_channel(pf, vsi, ch)) {
6609 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6613 dev_info(&pf->pdev->dev,
6614 "Setup channel (id:%u) utilizing num_queues %d\n",
6615 ch->seid, ch->num_queue_pairs);
6617 /* configure VSI for BW limit */
6618 if (ch->max_tx_rate) {
6619 u64 credits = ch->max_tx_rate;
6621 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6624 do_div(credits, I40E_BW_CREDIT_DIVISOR);
6625 dev_dbg(&pf->pdev->dev,
6626 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6632 /* in case of VF, this will be main SRIOV VSI */
6633 ch->parent_vsi = vsi;
6635 /* and update main_vsi's count for queue_available to use */
6636 vsi->cnt_q_avail -= ch->num_queue_pairs;
6642 * i40e_configure_queue_channels - Add queue channel for the given TCs
6643 * @vsi: VSI to be configured
6645 * Configures queue channel mapping to the given TCs
6647 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6649 struct i40e_channel *ch;
6653 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6654 vsi->tc_seid_map[0] = vsi->seid;
6655 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6656 if (vsi->tc_config.enabled_tc & BIT(i)) {
6657 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6663 INIT_LIST_HEAD(&ch->list);
6664 ch->num_queue_pairs =
6665 vsi->tc_config.tc_info[i].qcount;
6667 vsi->tc_config.tc_info[i].qoffset;
6669 /* Bandwidth limit through tc interface is in bytes/s,
6672 max_rate = vsi->mqprio_qopt.max_rate[i];
6673 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6674 ch->max_tx_rate = max_rate;
6676 list_add_tail(&ch->list, &vsi->ch_list);
6678 ret = i40e_create_queue_channel(vsi, ch);
6680 dev_err(&vsi->back->pdev->dev,
6681 "Failed creating queue channel with TC%d: queues %d\n",
6682 i, ch->num_queue_pairs);
6685 vsi->tc_seid_map[i] = ch->seid;
6689 /* reset to reconfigure TX queue contexts */
6690 i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6694 i40e_remove_queue_channels(vsi);
6699 * i40e_veb_config_tc - Configure TCs for given VEB
6701 * @enabled_tc: TC bitmap
6703 * Configures given TC bitmap for VEB (switching) element
6705 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6707 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6708 struct i40e_pf *pf = veb->pf;
6712 /* No TCs or already enabled TCs just return */
6713 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6716 bw_data.tc_valid_bits = enabled_tc;
6717 /* bw_data.absolute_credits is not set (relative) */
6719 /* Enable ETS TCs with equal BW Share for now */
6720 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6721 if (enabled_tc & BIT(i))
6722 bw_data.tc_bw_share_credits[i] = 1;
6725 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6728 dev_info(&pf->pdev->dev,
6729 "VEB bw config failed, err %pe aq_err %s\n",
6731 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6735 /* Update the BW information */
6736 ret = i40e_veb_get_bw_info(veb);
6738 dev_info(&pf->pdev->dev,
6739 "Failed getting veb bw config, err %pe aq_err %s\n",
6741 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6748 #ifdef CONFIG_I40E_DCB
6750 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6753 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6754 * the caller would've quiesce all the VSIs before calling
6757 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6763 /* Enable the TCs available on PF to all VEBs */
6764 tc_map = i40e_pf_get_tc_map(pf);
6765 if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6768 for (v = 0; v < I40E_MAX_VEB; v++) {
6771 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6773 dev_info(&pf->pdev->dev,
6774 "Failed configuring TC for VEB seid=%d\n",
6776 /* Will try to configure as many components */
6780 /* Update each VSI */
6781 for (v = 0; v < pf->num_alloc_vsi; v++) {
6785 /* - Enable all TCs for the LAN VSI
6786 * - For all others keep them at TC0 for now
6788 if (v == pf->lan_vsi)
6789 tc_map = i40e_pf_get_tc_map(pf);
6791 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6793 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6795 dev_info(&pf->pdev->dev,
6796 "Failed configuring TC for VSI seid=%d\n",
6798 /* Will try to configure as many components */
6800 /* Re-configure VSI vectors based on updated TC map */
6801 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6802 if (pf->vsi[v]->netdev)
6803 i40e_dcbnl_set_all(pf->vsi[v]);
6809 * i40e_resume_port_tx - Resume port Tx
6812 * Resume a port's Tx and issue a PF reset in case of failure to
6815 static int i40e_resume_port_tx(struct i40e_pf *pf)
6817 struct i40e_hw *hw = &pf->hw;
6820 ret = i40e_aq_resume_port_tx(hw, NULL);
6822 dev_info(&pf->pdev->dev,
6823 "Resume Port Tx failed, err %pe aq_err %s\n",
6825 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6826 /* Schedule PF reset to recover */
6827 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6828 i40e_service_event_schedule(pf);
6835 * i40e_suspend_port_tx - Suspend port Tx
6838 * Suspend a port's Tx and issue a PF reset in case of failure.
6840 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6842 struct i40e_hw *hw = &pf->hw;
6845 ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6847 dev_info(&pf->pdev->dev,
6848 "Suspend Port Tx failed, err %pe aq_err %s\n",
6850 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6851 /* Schedule PF reset to recover */
6852 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6853 i40e_service_event_schedule(pf);
6860 * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6861 * @pf: PF being configured
6862 * @new_cfg: New DCBX configuration
6864 * Program DCB settings into HW and reconfigure VEB/VSIs on
6865 * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6867 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6868 struct i40e_dcbx_config *new_cfg)
6870 struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6873 /* Check if need reconfiguration */
6874 if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6875 dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6879 /* Config change disable all VSIs */
6880 i40e_pf_quiesce_all_vsi(pf);
6882 /* Copy the new config to the current config */
6883 *old_cfg = *new_cfg;
6884 old_cfg->etsrec = old_cfg->etscfg;
6885 ret = i40e_set_dcb_config(&pf->hw);
6887 dev_info(&pf->pdev->dev,
6888 "Set DCB Config failed, err %pe aq_err %s\n",
6890 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6894 /* Changes in configuration update VEB/VSI */
6895 i40e_dcb_reconfigure(pf);
6897 /* In case of reset do not try to resume anything */
6898 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6899 /* Re-start the VSIs if disabled */
6900 ret = i40e_resume_port_tx(pf);
6901 /* In case of error no point in resuming VSIs */
6904 i40e_pf_unquiesce_all_vsi(pf);
6911 * i40e_hw_dcb_config - Program new DCBX settings into HW
6912 * @pf: PF being configured
6913 * @new_cfg: New DCBX configuration
6915 * Program DCB settings into HW and reconfigure VEB/VSIs on
6918 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6920 struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6921 u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6922 u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6923 struct i40e_dcbx_config *old_cfg;
6924 u8 mode[I40E_MAX_TRAFFIC_CLASS];
6925 struct i40e_rx_pb_config pb_cfg;
6926 struct i40e_hw *hw = &pf->hw;
6927 u8 num_ports = hw->num_ports;
6935 dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6936 /* Un-pack information to Program ETS HW via shared API
6939 * ETS/NON-ETS arbiter mode
6940 * max exponent (credit refills)
6941 * Total number of ports
6942 * PFC priority bit-map
6945 * Arbiter mode between UPs sharing same TC
6946 * TSA table (ETS or non-ETS)
6947 * EEE enabled or not
6951 new_numtc = i40e_dcb_get_num_tc(new_cfg);
6953 memset(&ets_data, 0, sizeof(ets_data));
6954 for (i = 0; i < new_numtc; i++) {
6956 switch (new_cfg->etscfg.tsatable[i]) {
6957 case I40E_IEEE_TSA_ETS:
6958 prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6959 ets_data.tc_bw_share_credits[i] =
6960 new_cfg->etscfg.tcbwtable[i];
6962 case I40E_IEEE_TSA_STRICT:
6963 prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6965 ets_data.tc_bw_share_credits[i] =
6966 I40E_DCB_STRICT_PRIO_CREDITS;
6969 /* Invalid TSA type */
6970 need_reconfig = false;
6975 old_cfg = &hw->local_dcbx_config;
6976 /* Check if need reconfiguration */
6977 need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6979 /* If needed, enable/disable frame tagging, disable all VSIs
6980 * and suspend port tx
6982 if (need_reconfig) {
6983 /* Enable DCB tagging only when more than one TC */
6985 pf->flags |= I40E_FLAG_DCB_ENABLED;
6987 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6989 set_bit(__I40E_PORT_SUSPENDED, pf->state);
6990 /* Reconfiguration needed quiesce all VSIs */
6991 i40e_pf_quiesce_all_vsi(pf);
6992 ret = i40e_suspend_port_tx(pf);
6997 /* Configure Port ETS Tx Scheduler */
6998 ets_data.tc_valid_bits = tc_map;
6999 ets_data.tc_strict_priority_flags = lltc_map;
7000 ret = i40e_aq_config_switch_comp_ets
7001 (hw, pf->mac_seid, &ets_data,
7002 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7004 dev_info(&pf->pdev->dev,
7005 "Modify Port ETS failed, err %pe aq_err %s\n",
7007 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7011 /* Configure Rx ETS HW */
7012 memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7013 i40e_dcb_hw_set_num_tc(hw, new_numtc);
7014 i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7015 I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7016 I40E_DCB_DEFAULT_MAX_EXPONENT,
7018 i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7019 i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7021 i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7022 new_cfg->etscfg.prioritytable);
7023 i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7025 /* Configure Rx Packet Buffers in HW */
7026 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7027 mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7028 mfs_tc[i] += I40E_PACKET_HDR_PAD;
7031 i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7032 false, new_cfg->pfc.pfcenable,
7034 i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7036 /* Update the local Rx Packet buffer config */
7037 pf->pb_cfg = pb_cfg;
7039 /* Inform the FW about changes to DCB configuration */
7040 ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7042 dev_info(&pf->pdev->dev,
7043 "DCB Updated failed, err %pe aq_err %s\n",
7045 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7049 /* Update the port DCBx configuration */
7050 *old_cfg = *new_cfg;
7052 /* Changes in configuration update VEB/VSI */
7053 i40e_dcb_reconfigure(pf);
7055 /* Re-start the VSIs if disabled */
7056 if (need_reconfig) {
7057 ret = i40e_resume_port_tx(pf);
7059 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7060 /* In case of error no point in resuming VSIs */
7064 /* Wait for the PF's queues to be disabled */
7065 ret = i40e_pf_wait_queues_disabled(pf);
7067 /* Schedule PF reset to recover */
7068 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7069 i40e_service_event_schedule(pf);
7072 i40e_pf_unquiesce_all_vsi(pf);
7073 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7074 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7076 /* registers are set, lets apply */
7077 if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7078 ret = i40e_hw_set_dcb_config(pf, new_cfg);
7086 * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7087 * @pf: PF being queried
7089 * Set default DCB configuration in case DCB is to be done in SW.
7091 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7093 struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7094 struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7095 struct i40e_hw *hw = &pf->hw;
7098 if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7099 /* Update the local cached instance with TC0 ETS */
7100 memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7101 pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7102 pf->tmp_cfg.etscfg.maxtcs = 0;
7103 pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7104 pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7105 pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7106 pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7107 /* FW needs one App to configure HW */
7108 pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7109 pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7110 pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7111 pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7113 return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7116 memset(&ets_data, 0, sizeof(ets_data));
7117 ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7118 ets_data.tc_strict_priority_flags = 0; /* ETS */
7119 ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7121 /* Enable ETS on the Physical port */
7122 err = i40e_aq_config_switch_comp_ets
7123 (hw, pf->mac_seid, &ets_data,
7124 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7126 dev_info(&pf->pdev->dev,
7127 "Enable Port ETS failed, err %pe aq_err %s\n",
7129 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7134 /* Update the local cached instance with TC0 ETS */
7135 dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7136 dcb_cfg->etscfg.cbs = 0;
7137 dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7138 dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7145 * i40e_init_pf_dcb - Initialize DCB configuration
7146 * @pf: PF being configured
7148 * Query the current DCB configuration and cache it
7149 * in the hardware structure
7151 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7153 struct i40e_hw *hw = &pf->hw;
7156 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7157 * Also do not enable DCBx if FW LLDP agent is disabled
7159 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7160 dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7161 err = I40E_NOT_SUPPORTED;
7164 if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7165 dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7166 err = i40e_dcb_sw_default_config(pf);
7168 dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7171 dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7172 pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7173 DCB_CAP_DCBX_VER_IEEE;
7174 /* at init capable but disabled */
7175 pf->flags |= I40E_FLAG_DCB_CAPABLE;
7176 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7179 err = i40e_init_dcb(hw, true);
7181 /* Device/Function is not DCBX capable */
7182 if ((!hw->func_caps.dcb) ||
7183 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7184 dev_info(&pf->pdev->dev,
7185 "DCBX offload is not supported or is disabled for this PF.\n");
7187 /* When status is not DISABLED then DCBX in FW */
7188 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7189 DCB_CAP_DCBX_VER_IEEE;
7191 pf->flags |= I40E_FLAG_DCB_CAPABLE;
7192 /* Enable DCB tagging only when more than one TC
7193 * or explicitly disable if only one TC
7195 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7196 pf->flags |= I40E_FLAG_DCB_ENABLED;
7198 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7199 dev_dbg(&pf->pdev->dev,
7200 "DCBX offload is supported for this PF.\n");
7202 } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7203 dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7204 pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7206 dev_info(&pf->pdev->dev,
7207 "Query for DCB configuration failed, err %pe aq_err %s\n",
7209 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7215 #endif /* CONFIG_I40E_DCB */
7218 * i40e_print_link_message - print link up or down
7219 * @vsi: the VSI for which link needs a message
7220 * @isup: true of link is up, false otherwise
7222 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7224 enum i40e_aq_link_speed new_speed;
7225 struct i40e_pf *pf = vsi->back;
7226 char *speed = "Unknown";
7227 char *fc = "Unknown";
7233 new_speed = pf->hw.phy.link_info.link_speed;
7235 new_speed = I40E_LINK_SPEED_UNKNOWN;
7237 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7239 vsi->current_isup = isup;
7240 vsi->current_speed = new_speed;
7242 netdev_info(vsi->netdev, "NIC Link is Down\n");
7246 /* Warn user if link speed on NPAR enabled partition is not at
7249 if (pf->hw.func_caps.npar_enable &&
7250 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7251 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7252 netdev_warn(vsi->netdev,
7253 "The partition detected link speed that is less than 10Gbps\n");
7255 switch (pf->hw.phy.link_info.link_speed) {
7256 case I40E_LINK_SPEED_40GB:
7259 case I40E_LINK_SPEED_20GB:
7262 case I40E_LINK_SPEED_25GB:
7265 case I40E_LINK_SPEED_10GB:
7268 case I40E_LINK_SPEED_5GB:
7271 case I40E_LINK_SPEED_2_5GB:
7274 case I40E_LINK_SPEED_1GB:
7277 case I40E_LINK_SPEED_100MB:
7284 switch (pf->hw.fc.current_mode) {
7288 case I40E_FC_TX_PAUSE:
7291 case I40E_FC_RX_PAUSE:
7299 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7304 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7307 if (pf->hw.phy.link_info.fec_info &
7308 I40E_AQ_CONFIG_FEC_KR_ENA)
7309 fec = "CL74 FC-FEC/BASE-R";
7310 else if (pf->hw.phy.link_info.fec_info &
7311 I40E_AQ_CONFIG_FEC_RS_ENA)
7312 fec = "CL108 RS-FEC";
7314 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
7315 * both RS and FC are requested
7317 if (vsi->back->hw.phy.link_info.req_fec_info &
7318 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7319 if (vsi->back->hw.phy.link_info.req_fec_info &
7320 I40E_AQ_REQUEST_FEC_RS)
7321 req_fec = "CL108 RS-FEC";
7323 req_fec = "CL74 FC-FEC/BASE-R";
7325 netdev_info(vsi->netdev,
7326 "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7327 speed, req_fec, fec, an, fc);
7328 } else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7333 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7336 if (pf->hw.phy.link_info.fec_info &
7337 I40E_AQ_CONFIG_FEC_KR_ENA)
7338 fec = "CL74 FC-FEC/BASE-R";
7340 if (pf->hw.phy.link_info.req_fec_info &
7341 I40E_AQ_REQUEST_FEC_KR)
7342 req_fec = "CL74 FC-FEC/BASE-R";
7344 netdev_info(vsi->netdev,
7345 "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7346 speed, req_fec, fec, an, fc);
7348 netdev_info(vsi->netdev,
7349 "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7356 * i40e_up_complete - Finish the last steps of bringing up a connection
7357 * @vsi: the VSI being configured
7359 static int i40e_up_complete(struct i40e_vsi *vsi)
7361 struct i40e_pf *pf = vsi->back;
7364 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7365 i40e_vsi_configure_msix(vsi);
7367 i40e_configure_msi_and_legacy(vsi);
7370 err = i40e_vsi_start_rings(vsi);
7374 clear_bit(__I40E_VSI_DOWN, vsi->state);
7375 i40e_napi_enable_all(vsi);
7376 i40e_vsi_enable_irq(vsi);
7378 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7380 i40e_print_link_message(vsi, true);
7381 netif_tx_start_all_queues(vsi->netdev);
7382 netif_carrier_on(vsi->netdev);
7385 /* replay FDIR SB filters */
7386 if (vsi->type == I40E_VSI_FDIR) {
7387 /* reset fd counters */
7390 i40e_fdir_filter_restore(vsi);
7393 /* On the next run of the service_task, notify any clients of the new
7396 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7397 i40e_service_event_schedule(pf);
7403 * i40e_vsi_reinit_locked - Reset the VSI
7404 * @vsi: the VSI being configured
7406 * Rebuild the ring structs after some configuration
7407 * has changed, e.g. MTU size.
7409 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7411 struct i40e_pf *pf = vsi->back;
7413 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7414 usleep_range(1000, 2000);
7418 clear_bit(__I40E_CONFIG_BUSY, pf->state);
7422 * i40e_force_link_state - Force the link status
7423 * @pf: board private structure
7424 * @is_up: whether the link state should be forced up or down
7426 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7428 struct i40e_aq_get_phy_abilities_resp abilities;
7429 struct i40e_aq_set_phy_config config = {0};
7430 bool non_zero_phy_type = is_up;
7431 struct i40e_hw *hw = &pf->hw;
7436 /* Card might've been put in an unstable state by other drivers
7437 * and applications, which causes incorrect speed values being
7438 * set on startup. In order to clear speed registers, we call
7439 * get_phy_capabilities twice, once to get initial state of
7440 * available speeds, and once to get current PHY config.
7442 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7445 dev_err(&pf->pdev->dev,
7446 "failed to get phy cap., ret = %pe last_status = %s\n",
7448 i40e_aq_str(hw, hw->aq.asq_last_status));
7451 speed = abilities.link_speed;
7453 /* Get the current phy config */
7454 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7457 dev_err(&pf->pdev->dev,
7458 "failed to get phy cap., ret = %pe last_status = %s\n",
7460 i40e_aq_str(hw, hw->aq.asq_last_status));
7464 /* If link needs to go up, but was not forced to go down,
7465 * and its speed values are OK, no need for a flap
7466 * if non_zero_phy_type was set, still need to force up
7468 if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7469 non_zero_phy_type = true;
7470 else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7471 return I40E_SUCCESS;
7473 /* To force link we need to set bits for all supported PHY types,
7474 * but there are now more than 32, so we need to split the bitmap
7475 * across two fields.
7477 mask = I40E_PHY_TYPES_BITMASK;
7479 non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7480 config.phy_type_ext =
7481 non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7482 /* Copy the old settings, except of phy_type */
7483 config.abilities = abilities.abilities;
7484 if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7486 config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7488 config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7490 if (abilities.link_speed != 0)
7491 config.link_speed = abilities.link_speed;
7493 config.link_speed = speed;
7494 config.eee_capability = abilities.eee_capability;
7495 config.eeer = abilities.eeer_val;
7496 config.low_power_ctrl = abilities.d3_lpan;
7497 config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7498 I40E_AQ_PHY_FEC_CONFIG_MASK;
7499 err = i40e_aq_set_phy_config(hw, &config, NULL);
7502 dev_err(&pf->pdev->dev,
7503 "set phy config ret = %pe last_status = %s\n",
7505 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7509 /* Update the link info */
7510 err = i40e_update_link_info(hw);
7512 /* Wait a little bit (on 40G cards it sometimes takes a really
7513 * long time for link to come back from the atomic reset)
7517 i40e_update_link_info(hw);
7520 i40e_aq_set_link_restart_an(hw, is_up, NULL);
7522 return I40E_SUCCESS;
7526 * i40e_up - Bring the connection back up after being down
7527 * @vsi: the VSI being configured
7529 int i40e_up(struct i40e_vsi *vsi)
7533 if (vsi->type == I40E_VSI_MAIN &&
7534 (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7535 vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7536 i40e_force_link_state(vsi->back, true);
7538 err = i40e_vsi_configure(vsi);
7540 err = i40e_up_complete(vsi);
7546 * i40e_down - Shutdown the connection processing
7547 * @vsi: the VSI being stopped
7549 void i40e_down(struct i40e_vsi *vsi)
7553 /* It is assumed that the caller of this function
7554 * sets the vsi->state __I40E_VSI_DOWN bit.
7557 netif_carrier_off(vsi->netdev);
7558 netif_tx_disable(vsi->netdev);
7560 i40e_vsi_disable_irq(vsi);
7561 i40e_vsi_stop_rings(vsi);
7562 if (vsi->type == I40E_VSI_MAIN &&
7563 (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7564 vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7565 i40e_force_link_state(vsi->back, false);
7566 i40e_napi_disable_all(vsi);
7568 for (i = 0; i < vsi->num_queue_pairs; i++) {
7569 i40e_clean_tx_ring(vsi->tx_rings[i]);
7570 if (i40e_enabled_xdp_vsi(vsi)) {
7571 /* Make sure that in-progress ndo_xdp_xmit and
7572 * ndo_xsk_wakeup calls are completed.
7575 i40e_clean_tx_ring(vsi->xdp_rings[i]);
7577 i40e_clean_rx_ring(vsi->rx_rings[i]);
7583 * i40e_validate_mqprio_qopt- validate queue mapping info
7584 * @vsi: the VSI being configured
7585 * @mqprio_qopt: queue parametrs
7587 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7588 struct tc_mqprio_qopt_offload *mqprio_qopt)
7590 u64 sum_max_rate = 0;
7594 if (mqprio_qopt->qopt.offset[0] != 0 ||
7595 mqprio_qopt->qopt.num_tc < 1 ||
7596 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7598 for (i = 0; ; i++) {
7599 if (!mqprio_qopt->qopt.count[i])
7601 if (mqprio_qopt->min_rate[i]) {
7602 dev_err(&vsi->back->pdev->dev,
7603 "Invalid min tx rate (greater than 0) specified\n");
7606 max_rate = mqprio_qopt->max_rate[i];
7607 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7608 sum_max_rate += max_rate;
7610 if (i >= mqprio_qopt->qopt.num_tc - 1)
7612 if (mqprio_qopt->qopt.offset[i + 1] !=
7613 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7616 if (vsi->num_queue_pairs <
7617 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7618 dev_err(&vsi->back->pdev->dev,
7619 "Failed to create traffic channel, insufficient number of queues.\n");
7622 if (sum_max_rate > i40e_get_link_speed(vsi)) {
7623 dev_err(&vsi->back->pdev->dev,
7624 "Invalid max tx rate specified\n");
7631 * i40e_vsi_set_default_tc_config - set default values for tc configuration
7632 * @vsi: the VSI being configured
7634 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7639 /* Only TC0 is enabled */
7640 vsi->tc_config.numtc = 1;
7641 vsi->tc_config.enabled_tc = 1;
7642 qcount = min_t(int, vsi->alloc_queue_pairs,
7643 i40e_pf_get_max_q_per_tc(vsi->back));
7644 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7645 /* For the TC that is not enabled set the offset to default
7646 * queue and allocate one queue for the given TC.
7648 vsi->tc_config.tc_info[i].qoffset = 0;
7650 vsi->tc_config.tc_info[i].qcount = qcount;
7652 vsi->tc_config.tc_info[i].qcount = 1;
7653 vsi->tc_config.tc_info[i].netdev_tc = 0;
7658 * i40e_del_macvlan_filter
7659 * @hw: pointer to the HW structure
7660 * @seid: seid of the channel VSI
7661 * @macaddr: the mac address to apply as a filter
7662 * @aq_err: store the admin Q error
7664 * This function deletes a mac filter on the channel VSI which serves as the
7665 * macvlan. Returns 0 on success.
7667 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7668 const u8 *macaddr, int *aq_err)
7670 struct i40e_aqc_remove_macvlan_element_data element;
7673 memset(&element, 0, sizeof(element));
7674 ether_addr_copy(element.mac_addr, macaddr);
7675 element.vlan_tag = 0;
7676 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7677 status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7678 *aq_err = hw->aq.asq_last_status;
7684 * i40e_add_macvlan_filter
7685 * @hw: pointer to the HW structure
7686 * @seid: seid of the channel VSI
7687 * @macaddr: the mac address to apply as a filter
7688 * @aq_err: store the admin Q error
7690 * This function adds a mac filter on the channel VSI which serves as the
7691 * macvlan. Returns 0 on success.
7693 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7694 const u8 *macaddr, int *aq_err)
7696 struct i40e_aqc_add_macvlan_element_data element;
7700 ether_addr_copy(element.mac_addr, macaddr);
7701 element.vlan_tag = 0;
7702 element.queue_number = 0;
7703 element.match_method = I40E_AQC_MM_ERR_NO_RES;
7704 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7705 element.flags = cpu_to_le16(cmd_flags);
7706 status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7707 *aq_err = hw->aq.asq_last_status;
7713 * i40e_reset_ch_rings - Reset the queue contexts in a channel
7714 * @vsi: the VSI we want to access
7715 * @ch: the channel we want to access
7717 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7719 struct i40e_ring *tx_ring, *rx_ring;
7723 for (i = 0; i < ch->num_queue_pairs; i++) {
7724 pf_q = ch->base_queue + i;
7725 tx_ring = vsi->tx_rings[pf_q];
7727 rx_ring = vsi->rx_rings[pf_q];
7733 * i40e_free_macvlan_channels
7734 * @vsi: the VSI we want to access
7736 * This function frees the Qs of the channel VSI from
7737 * the stack and also deletes the channel VSIs which
7738 * serve as macvlans.
7740 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7742 struct i40e_channel *ch, *ch_tmp;
7745 if (list_empty(&vsi->macvlan_list))
7748 list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7749 struct i40e_vsi *parent_vsi;
7751 if (i40e_is_channel_macvlan(ch)) {
7752 i40e_reset_ch_rings(vsi, ch);
7753 clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7754 netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7755 netdev_set_sb_channel(ch->fwd->netdev, 0);
7760 list_del(&ch->list);
7761 parent_vsi = ch->parent_vsi;
7762 if (!parent_vsi || !ch->initialized) {
7767 /* remove the VSI */
7768 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7771 dev_err(&vsi->back->pdev->dev,
7772 "unable to remove channel (%d) for parent VSI(%d)\n",
7773 ch->seid, parent_vsi->seid);
7776 vsi->macvlan_cnt = 0;
7780 * i40e_fwd_ring_up - bring the macvlan device up
7781 * @vsi: the VSI we want to access
7782 * @vdev: macvlan netdevice
7783 * @fwd: the private fwd structure
7785 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7786 struct i40e_fwd_adapter *fwd)
7788 struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7789 int ret = 0, num_tc = 1, i, aq_err;
7790 struct i40e_pf *pf = vsi->back;
7791 struct i40e_hw *hw = &pf->hw;
7793 /* Go through the list and find an available channel */
7794 list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7795 if (!i40e_is_channel_macvlan(iter)) {
7797 /* record configuration for macvlan interface in vdev */
7798 for (i = 0; i < num_tc; i++)
7799 netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7801 iter->num_queue_pairs,
7803 for (i = 0; i < iter->num_queue_pairs; i++) {
7804 struct i40e_ring *tx_ring, *rx_ring;
7807 pf_q = iter->base_queue + i;
7809 /* Get to TX ring ptr */
7810 tx_ring = vsi->tx_rings[pf_q];
7813 /* Get the RX ring ptr */
7814 rx_ring = vsi->rx_rings[pf_q];
7825 /* Guarantee all rings are updated before we update the
7826 * MAC address filter.
7830 /* Add a mac filter */
7831 ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7833 /* if we cannot add the MAC rule then disable the offload */
7834 macvlan_release_l2fw_offload(vdev);
7835 for (i = 0; i < ch->num_queue_pairs; i++) {
7836 struct i40e_ring *rx_ring;
7839 pf_q = ch->base_queue + i;
7840 rx_ring = vsi->rx_rings[pf_q];
7841 rx_ring->netdev = NULL;
7843 dev_info(&pf->pdev->dev,
7844 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7846 i40e_aq_str(hw, aq_err));
7847 netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7854 * i40e_setup_macvlans - create the channels which will be macvlans
7855 * @vsi: the VSI we want to access
7856 * @macvlan_cnt: no. of macvlans to be setup
7857 * @qcnt: no. of Qs per macvlan
7858 * @vdev: macvlan netdevice
7860 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7861 struct net_device *vdev)
7863 struct i40e_pf *pf = vsi->back;
7864 struct i40e_hw *hw = &pf->hw;
7865 struct i40e_vsi_context ctxt;
7866 u16 sections, qmap, num_qps;
7867 struct i40e_channel *ch;
7868 int i, pow, ret = 0;
7871 if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7874 num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7876 /* find the next higher power-of-2 of num queue pairs */
7877 pow = fls(roundup_pow_of_two(num_qps) - 1);
7879 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7880 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7882 /* Setup context bits for the main VSI */
7883 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7884 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7885 memset(&ctxt, 0, sizeof(ctxt));
7886 ctxt.seid = vsi->seid;
7887 ctxt.pf_num = vsi->back->hw.pf_id;
7889 ctxt.uplink_seid = vsi->uplink_seid;
7890 ctxt.info = vsi->info;
7891 ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7892 ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7893 ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7894 ctxt.info.valid_sections |= cpu_to_le16(sections);
7896 /* Reconfigure RSS for main VSI with new max queue count */
7897 vsi->rss_size = max_t(u16, num_qps, qcnt);
7898 ret = i40e_vsi_config_rss(vsi);
7900 dev_info(&pf->pdev->dev,
7901 "Failed to reconfig RSS for num_queues (%u)\n",
7905 vsi->reconfig_rss = true;
7906 dev_dbg(&vsi->back->pdev->dev,
7907 "Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7908 vsi->next_base_queue = num_qps;
7909 vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7911 /* Update the VSI after updating the VSI queue-mapping
7914 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7916 dev_info(&pf->pdev->dev,
7917 "Update vsi tc config failed, err %pe aq_err %s\n",
7919 i40e_aq_str(hw, hw->aq.asq_last_status));
7922 /* update the local VSI info with updated queue map */
7923 i40e_vsi_update_queue_map(vsi, &ctxt);
7924 vsi->info.valid_sections = 0;
7926 /* Create channels for macvlans */
7927 INIT_LIST_HEAD(&vsi->macvlan_list);
7928 for (i = 0; i < macvlan_cnt; i++) {
7929 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7934 INIT_LIST_HEAD(&ch->list);
7935 ch->num_queue_pairs = qcnt;
7936 if (!i40e_setup_channel(pf, vsi, ch)) {
7941 ch->parent_vsi = vsi;
7942 vsi->cnt_q_avail -= ch->num_queue_pairs;
7944 list_add_tail(&ch->list, &vsi->macvlan_list);
7950 dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7951 i40e_free_macvlan_channels(vsi);
7957 * i40e_fwd_add - configure macvlans
7958 * @netdev: net device to configure
7959 * @vdev: macvlan netdevice
7961 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7963 struct i40e_netdev_priv *np = netdev_priv(netdev);
7964 u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7965 struct i40e_vsi *vsi = np->vsi;
7966 struct i40e_pf *pf = vsi->back;
7967 struct i40e_fwd_adapter *fwd;
7968 int avail_macvlan, ret;
7970 if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7971 netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7972 return ERR_PTR(-EINVAL);
7974 if (i40e_is_tc_mqprio_enabled(pf)) {
7975 netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7976 return ERR_PTR(-EINVAL);
7978 if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7979 netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7980 return ERR_PTR(-EINVAL);
7983 /* The macvlan device has to be a single Q device so that the
7984 * tc_to_txq field can be reused to pick the tx queue.
7986 if (netif_is_multiqueue(vdev))
7987 return ERR_PTR(-ERANGE);
7989 if (!vsi->macvlan_cnt) {
7990 /* reserve bit 0 for the pf device */
7991 set_bit(0, vsi->fwd_bitmask);
7993 /* Try to reserve as many queues as possible for macvlans. First
7994 * reserve 3/4th of max vectors, then half, then quarter and
7995 * calculate Qs per macvlan as you go
7997 vectors = pf->num_lan_msix;
7998 if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7999 /* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8001 macvlan_cnt = (vectors - 32) / 4;
8002 } else if (vectors <= 64 && vectors > 32) {
8003 /* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8005 macvlan_cnt = (vectors - 16) / 2;
8006 } else if (vectors <= 32 && vectors > 16) {
8007 /* allocate 1 Q per macvlan and 16 Qs to the PF*/
8009 macvlan_cnt = vectors - 16;
8010 } else if (vectors <= 16 && vectors > 8) {
8011 /* allocate 1 Q per macvlan and 8 Qs to the PF */
8013 macvlan_cnt = vectors - 8;
8015 /* allocate 1 Q per macvlan and 1 Q to the PF */
8017 macvlan_cnt = vectors - 1;
8020 if (macvlan_cnt == 0)
8021 return ERR_PTR(-EBUSY);
8023 /* Quiesce VSI queues */
8024 i40e_quiesce_vsi(vsi);
8026 /* sets up the macvlans but does not "enable" them */
8027 ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8030 return ERR_PTR(ret);
8033 i40e_unquiesce_vsi(vsi);
8035 avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8037 if (avail_macvlan >= I40E_MAX_MACVLANS)
8038 return ERR_PTR(-EBUSY);
8040 /* create the fwd struct */
8041 fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8043 return ERR_PTR(-ENOMEM);
8045 set_bit(avail_macvlan, vsi->fwd_bitmask);
8046 fwd->bit_no = avail_macvlan;
8047 netdev_set_sb_channel(vdev, avail_macvlan);
8050 if (!netif_running(netdev))
8053 /* Set fwd ring up */
8054 ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8056 /* unbind the queues and drop the subordinate channel config */
8057 netdev_unbind_sb_channel(netdev, vdev);
8058 netdev_set_sb_channel(vdev, 0);
8061 return ERR_PTR(-EINVAL);
8068 * i40e_del_all_macvlans - Delete all the mac filters on the channels
8069 * @vsi: the VSI we want to access
8071 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8073 struct i40e_channel *ch, *ch_tmp;
8074 struct i40e_pf *pf = vsi->back;
8075 struct i40e_hw *hw = &pf->hw;
8076 int aq_err, ret = 0;
8078 if (list_empty(&vsi->macvlan_list))
8081 list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8082 if (i40e_is_channel_macvlan(ch)) {
8083 ret = i40e_del_macvlan_filter(hw, ch->seid,
8084 i40e_channel_mac(ch),
8087 /* Reset queue contexts */
8088 i40e_reset_ch_rings(vsi, ch);
8089 clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8090 netdev_unbind_sb_channel(vsi->netdev,
8092 netdev_set_sb_channel(ch->fwd->netdev, 0);
8101 * i40e_fwd_del - delete macvlan interfaces
8102 * @netdev: net device to configure
8103 * @vdev: macvlan netdevice
8105 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8107 struct i40e_netdev_priv *np = netdev_priv(netdev);
8108 struct i40e_fwd_adapter *fwd = vdev;
8109 struct i40e_channel *ch, *ch_tmp;
8110 struct i40e_vsi *vsi = np->vsi;
8111 struct i40e_pf *pf = vsi->back;
8112 struct i40e_hw *hw = &pf->hw;
8113 int aq_err, ret = 0;
8115 /* Find the channel associated with the macvlan and del mac filter */
8116 list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8117 if (i40e_is_channel_macvlan(ch) &&
8118 ether_addr_equal(i40e_channel_mac(ch),
8119 fwd->netdev->dev_addr)) {
8120 ret = i40e_del_macvlan_filter(hw, ch->seid,
8121 i40e_channel_mac(ch),
8124 /* Reset queue contexts */
8125 i40e_reset_ch_rings(vsi, ch);
8126 clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8127 netdev_unbind_sb_channel(netdev, fwd->netdev);
8128 netdev_set_sb_channel(fwd->netdev, 0);
8132 dev_info(&pf->pdev->dev,
8133 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8135 i40e_aq_str(hw, aq_err));
8143 * i40e_setup_tc - configure multiple traffic classes
8144 * @netdev: net device to configure
8145 * @type_data: tc offload data
8147 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8149 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8150 struct i40e_netdev_priv *np = netdev_priv(netdev);
8151 struct i40e_vsi *vsi = np->vsi;
8152 struct i40e_pf *pf = vsi->back;
8153 u8 enabled_tc = 0, num_tc, hw;
8154 bool need_reset = false;
8155 int old_queue_pairs;
8160 old_queue_pairs = vsi->num_queue_pairs;
8161 num_tc = mqprio_qopt->qopt.num_tc;
8162 hw = mqprio_qopt->qopt.hw;
8163 mode = mqprio_qopt->mode;
8165 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8166 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8170 /* Check if MFP enabled */
8171 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8173 "Configuring TC not supported in MFP mode\n");
8177 case TC_MQPRIO_MODE_DCB:
8178 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8180 /* Check if DCB enabled to continue */
8181 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8183 "DCB is not enabled for adapter\n");
8187 /* Check whether tc count is within enabled limit */
8188 if (num_tc > i40e_pf_get_num_tc(pf)) {
8190 "TC count greater than enabled on link for adapter\n");
8194 case TC_MQPRIO_MODE_CHANNEL:
8195 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8197 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8200 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8202 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8205 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8206 sizeof(*mqprio_qopt));
8207 pf->flags |= I40E_FLAG_TC_MQPRIO;
8208 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8215 /* Generate TC map for number of tc requested */
8216 for (i = 0; i < num_tc; i++)
8217 enabled_tc |= BIT(i);
8219 /* Requesting same TC configuration as already enabled */
8220 if (enabled_tc == vsi->tc_config.enabled_tc &&
8221 mode != TC_MQPRIO_MODE_CHANNEL)
8224 /* Quiesce VSI queues */
8225 i40e_quiesce_vsi(vsi);
8227 if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8228 i40e_remove_queue_channels(vsi);
8230 /* Configure VSI for enabled TCs */
8231 ret = i40e_vsi_config_tc(vsi, enabled_tc);
8233 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8237 } else if (enabled_tc &&
8238 (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8240 "Failed to create channel. Override queues (%u) not power of 2\n",
8241 vsi->tc_config.tc_info[0].qcount);
8247 dev_info(&vsi->back->pdev->dev,
8248 "Setup channel (id:%u) utilizing num_queues %d\n",
8249 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8251 if (i40e_is_tc_mqprio_enabled(pf)) {
8252 if (vsi->mqprio_qopt.max_rate[0]) {
8253 u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8254 vsi->mqprio_qopt.max_rate[0]);
8256 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8258 u64 credits = max_tx_rate;
8260 do_div(credits, I40E_BW_CREDIT_DIVISOR);
8261 dev_dbg(&vsi->back->pdev->dev,
8262 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8271 ret = i40e_configure_queue_channels(vsi);
8273 vsi->num_queue_pairs = old_queue_pairs;
8275 "Failed configuring queue channels\n");
8282 /* Reset the configuration data to defaults, only TC0 is enabled */
8284 i40e_vsi_set_default_tc_config(vsi);
8289 i40e_unquiesce_vsi(vsi);
8294 * i40e_set_cld_element - sets cloud filter element data
8295 * @filter: cloud filter rule
8296 * @cld: ptr to cloud filter element data
8298 * This is helper function to copy data into cloud filter element
8301 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8302 struct i40e_aqc_cloud_filters_element_data *cld)
8307 memset(cld, 0, sizeof(*cld));
8308 ether_addr_copy(cld->outer_mac, filter->dst_mac);
8309 ether_addr_copy(cld->inner_mac, filter->src_mac);
8311 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8314 if (filter->n_proto == ETH_P_IPV6) {
8315 #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
8316 for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8317 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8319 *(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8322 ipa = be32_to_cpu(filter->dst_ipv4);
8324 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8327 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8329 /* tenant_id is not supported by FW now, once the support is enabled
8330 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8332 if (filter->tenant_id)
8337 * i40e_add_del_cloud_filter - Add/del cloud filter
8338 * @vsi: pointer to VSI
8339 * @filter: cloud filter rule
8340 * @add: if true, add, if false, delete
8342 * Add or delete a cloud filter for a specific flow spec.
8343 * Returns 0 if the filter were successfully added.
8345 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8346 struct i40e_cloud_filter *filter, bool add)
8348 struct i40e_aqc_cloud_filters_element_data cld_filter;
8349 struct i40e_pf *pf = vsi->back;
8351 static const u16 flag_table[128] = {
8352 [I40E_CLOUD_FILTER_FLAGS_OMAC] =
8353 I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8354 [I40E_CLOUD_FILTER_FLAGS_IMAC] =
8355 I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8356 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
8357 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8358 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8359 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8360 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8361 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8362 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8363 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8364 [I40E_CLOUD_FILTER_FLAGS_IIP] =
8365 I40E_AQC_ADD_CLOUD_FILTER_IIP,
8368 if (filter->flags >= ARRAY_SIZE(flag_table))
8369 return I40E_ERR_CONFIG;
8371 memset(&cld_filter, 0, sizeof(cld_filter));
8373 /* copy element needed to add cloud filter from filter */
8374 i40e_set_cld_element(filter, &cld_filter);
8376 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8377 cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8378 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8380 if (filter->n_proto == ETH_P_IPV6)
8381 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8382 I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8384 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8385 I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8388 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8391 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8394 dev_dbg(&pf->pdev->dev,
8395 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8396 add ? "add" : "delete", filter->dst_port, ret,
8397 pf->hw.aq.asq_last_status);
8399 dev_info(&pf->pdev->dev,
8400 "%s cloud filter for VSI: %d\n",
8401 add ? "Added" : "Deleted", filter->seid);
8406 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8407 * @vsi: pointer to VSI
8408 * @filter: cloud filter rule
8409 * @add: if true, add, if false, delete
8411 * Add or delete a cloud filter for a specific flow spec using big buffer.
8412 * Returns 0 if the filter were successfully added.
8414 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8415 struct i40e_cloud_filter *filter,
8418 struct i40e_aqc_cloud_filters_element_bb cld_filter;
8419 struct i40e_pf *pf = vsi->back;
8422 /* Both (src/dst) valid mac_addr are not supported */
8423 if ((is_valid_ether_addr(filter->dst_mac) &&
8424 is_valid_ether_addr(filter->src_mac)) ||
8425 (is_multicast_ether_addr(filter->dst_mac) &&
8426 is_multicast_ether_addr(filter->src_mac)))
8429 /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8430 * ports are not supported via big buffer now.
8432 if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8435 /* adding filter using src_port/src_ip is not supported at this stage */
8436 if (filter->src_port ||
8437 (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8438 !ipv6_addr_any(&filter->ip.v6.src_ip6))
8441 memset(&cld_filter, 0, sizeof(cld_filter));
8443 /* copy element needed to add cloud filter from filter */
8444 i40e_set_cld_element(filter, &cld_filter.element);
8446 if (is_valid_ether_addr(filter->dst_mac) ||
8447 is_valid_ether_addr(filter->src_mac) ||
8448 is_multicast_ether_addr(filter->dst_mac) ||
8449 is_multicast_ether_addr(filter->src_mac)) {
8450 /* MAC + IP : unsupported mode */
8451 if (filter->dst_ipv4)
8454 /* since we validated that L4 port must be valid before
8455 * we get here, start with respective "flags" value
8456 * and update if vlan is present or not
8458 cld_filter.element.flags =
8459 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8461 if (filter->vlan_id) {
8462 cld_filter.element.flags =
8463 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8466 } else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8467 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8468 cld_filter.element.flags =
8469 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8470 if (filter->n_proto == ETH_P_IPV6)
8471 cld_filter.element.flags |=
8472 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8474 cld_filter.element.flags |=
8475 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8477 dev_err(&pf->pdev->dev,
8478 "either mac or ip has to be valid for cloud filter\n");
8482 /* Now copy L4 port in Byte 6..7 in general fields */
8483 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8484 be16_to_cpu(filter->dst_port);
8487 /* Validate current device switch mode, change if necessary */
8488 ret = i40e_validate_and_set_switch_mode(vsi);
8490 dev_err(&pf->pdev->dev,
8491 "failed to set switch mode, ret %d\n",
8496 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8499 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8504 dev_dbg(&pf->pdev->dev,
8505 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8506 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8508 dev_info(&pf->pdev->dev,
8509 "%s cloud filter for VSI: %d, L4 port: %d\n",
8510 add ? "add" : "delete", filter->seid,
8511 ntohs(filter->dst_port));
8516 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8517 * @vsi: Pointer to VSI
8518 * @f: Pointer to struct flow_cls_offload
8519 * @filter: Pointer to cloud filter structure
8522 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8523 struct flow_cls_offload *f,
8524 struct i40e_cloud_filter *filter)
8526 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8527 struct flow_dissector *dissector = rule->match.dissector;
8528 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8529 struct i40e_pf *pf = vsi->back;
8532 if (dissector->used_keys &
8533 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8534 BIT(FLOW_DISSECTOR_KEY_BASIC) |
8535 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8536 BIT(FLOW_DISSECTOR_KEY_VLAN) |
8537 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8538 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8539 BIT(FLOW_DISSECTOR_KEY_PORTS) |
8540 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8541 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8542 dissector->used_keys);
8546 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8547 struct flow_match_enc_keyid match;
8549 flow_rule_match_enc_keyid(rule, &match);
8550 if (match.mask->keyid != 0)
8551 field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8553 filter->tenant_id = be32_to_cpu(match.key->keyid);
8556 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8557 struct flow_match_basic match;
8559 flow_rule_match_basic(rule, &match);
8560 n_proto_key = ntohs(match.key->n_proto);
8561 n_proto_mask = ntohs(match.mask->n_proto);
8563 if (n_proto_key == ETH_P_ALL) {
8567 filter->n_proto = n_proto_key & n_proto_mask;
8568 filter->ip_proto = match.key->ip_proto;
8571 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8572 struct flow_match_eth_addrs match;
8574 flow_rule_match_eth_addrs(rule, &match);
8576 /* use is_broadcast and is_zero to check for all 0xf or 0 */
8577 if (!is_zero_ether_addr(match.mask->dst)) {
8578 if (is_broadcast_ether_addr(match.mask->dst)) {
8579 field_flags |= I40E_CLOUD_FIELD_OMAC;
8581 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8583 return I40E_ERR_CONFIG;
8587 if (!is_zero_ether_addr(match.mask->src)) {
8588 if (is_broadcast_ether_addr(match.mask->src)) {
8589 field_flags |= I40E_CLOUD_FIELD_IMAC;
8591 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8593 return I40E_ERR_CONFIG;
8596 ether_addr_copy(filter->dst_mac, match.key->dst);
8597 ether_addr_copy(filter->src_mac, match.key->src);
8600 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8601 struct flow_match_vlan match;
8603 flow_rule_match_vlan(rule, &match);
8604 if (match.mask->vlan_id) {
8605 if (match.mask->vlan_id == VLAN_VID_MASK) {
8606 field_flags |= I40E_CLOUD_FIELD_IVLAN;
8609 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8610 match.mask->vlan_id);
8611 return I40E_ERR_CONFIG;
8615 filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8618 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8619 struct flow_match_control match;
8621 flow_rule_match_control(rule, &match);
8622 addr_type = match.key->addr_type;
8625 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8626 struct flow_match_ipv4_addrs match;
8628 flow_rule_match_ipv4_addrs(rule, &match);
8629 if (match.mask->dst) {
8630 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8631 field_flags |= I40E_CLOUD_FIELD_IIP;
8633 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8635 return I40E_ERR_CONFIG;
8639 if (match.mask->src) {
8640 if (match.mask->src == cpu_to_be32(0xffffffff)) {
8641 field_flags |= I40E_CLOUD_FIELD_IIP;
8643 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8645 return I40E_ERR_CONFIG;
8649 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8650 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8651 return I40E_ERR_CONFIG;
8653 filter->dst_ipv4 = match.key->dst;
8654 filter->src_ipv4 = match.key->src;
8657 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8658 struct flow_match_ipv6_addrs match;
8660 flow_rule_match_ipv6_addrs(rule, &match);
8662 /* src and dest IPV6 address should not be LOOPBACK
8663 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8665 if (ipv6_addr_loopback(&match.key->dst) ||
8666 ipv6_addr_loopback(&match.key->src)) {
8667 dev_err(&pf->pdev->dev,
8668 "Bad ipv6, addr is LOOPBACK\n");
8669 return I40E_ERR_CONFIG;
8671 if (!ipv6_addr_any(&match.mask->dst) ||
8672 !ipv6_addr_any(&match.mask->src))
8673 field_flags |= I40E_CLOUD_FIELD_IIP;
8675 memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8676 sizeof(filter->src_ipv6));
8677 memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8678 sizeof(filter->dst_ipv6));
8681 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8682 struct flow_match_ports match;
8684 flow_rule_match_ports(rule, &match);
8685 if (match.mask->src) {
8686 if (match.mask->src == cpu_to_be16(0xffff)) {
8687 field_flags |= I40E_CLOUD_FIELD_IIP;
8689 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8690 be16_to_cpu(match.mask->src));
8691 return I40E_ERR_CONFIG;
8695 if (match.mask->dst) {
8696 if (match.mask->dst == cpu_to_be16(0xffff)) {
8697 field_flags |= I40E_CLOUD_FIELD_IIP;
8699 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8700 be16_to_cpu(match.mask->dst));
8701 return I40E_ERR_CONFIG;
8705 filter->dst_port = match.key->dst;
8706 filter->src_port = match.key->src;
8708 switch (filter->ip_proto) {
8713 dev_err(&pf->pdev->dev,
8714 "Only UDP and TCP transport are supported\n");
8718 filter->flags = field_flags;
8723 * i40e_handle_tclass: Forward to a traffic class on the device
8724 * @vsi: Pointer to VSI
8725 * @tc: traffic class index on the device
8726 * @filter: Pointer to cloud filter structure
8729 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8730 struct i40e_cloud_filter *filter)
8732 struct i40e_channel *ch, *ch_tmp;
8734 /* direct to a traffic class on the same device */
8736 filter->seid = vsi->seid;
8738 } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8739 if (!filter->dst_port) {
8740 dev_err(&vsi->back->pdev->dev,
8741 "Specify destination port to direct to traffic class that is not default\n");
8744 if (list_empty(&vsi->ch_list))
8746 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8748 if (ch->seid == vsi->tc_seid_map[tc])
8749 filter->seid = ch->seid;
8753 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8758 * i40e_configure_clsflower - Configure tc flower filters
8759 * @vsi: Pointer to VSI
8760 * @cls_flower: Pointer to struct flow_cls_offload
8763 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8764 struct flow_cls_offload *cls_flower)
8766 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8767 struct i40e_cloud_filter *filter = NULL;
8768 struct i40e_pf *pf = vsi->back;
8772 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8777 dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8781 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8782 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8785 if (pf->fdir_pf_active_filters ||
8786 (!hlist_empty(&pf->fdir_filter_list))) {
8787 dev_err(&vsi->back->pdev->dev,
8788 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8792 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8793 dev_err(&vsi->back->pdev->dev,
8794 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8795 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8796 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8799 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8803 filter->cookie = cls_flower->cookie;
8805 err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8809 err = i40e_handle_tclass(vsi, tc, filter);
8813 /* Add cloud filter */
8814 if (filter->dst_port)
8815 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8817 err = i40e_add_del_cloud_filter(vsi, filter, true);
8820 dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8825 /* add filter to the ordered list */
8826 INIT_HLIST_NODE(&filter->cloud_node);
8828 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8830 pf->num_cloud_filters++;
8839 * i40e_find_cloud_filter - Find the could filter in the list
8840 * @vsi: Pointer to VSI
8841 * @cookie: filter specific cookie
8844 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8845 unsigned long *cookie)
8847 struct i40e_cloud_filter *filter = NULL;
8848 struct hlist_node *node2;
8850 hlist_for_each_entry_safe(filter, node2,
8851 &vsi->back->cloud_filter_list, cloud_node)
8852 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8858 * i40e_delete_clsflower - Remove tc flower filters
8859 * @vsi: Pointer to VSI
8860 * @cls_flower: Pointer to struct flow_cls_offload
8863 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8864 struct flow_cls_offload *cls_flower)
8866 struct i40e_cloud_filter *filter = NULL;
8867 struct i40e_pf *pf = vsi->back;
8870 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8875 hash_del(&filter->cloud_node);
8877 if (filter->dst_port)
8878 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8880 err = i40e_add_del_cloud_filter(vsi, filter, false);
8884 dev_err(&pf->pdev->dev,
8885 "Failed to delete cloud filter, err %pe\n",
8887 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8890 pf->num_cloud_filters--;
8891 if (!pf->num_cloud_filters)
8892 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8893 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8894 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8895 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8896 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8902 * i40e_setup_tc_cls_flower - flower classifier offloads
8903 * @np: net device to configure
8904 * @cls_flower: offload data
8906 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8907 struct flow_cls_offload *cls_flower)
8909 struct i40e_vsi *vsi = np->vsi;
8911 switch (cls_flower->command) {
8912 case FLOW_CLS_REPLACE:
8913 return i40e_configure_clsflower(vsi, cls_flower);
8914 case FLOW_CLS_DESTROY:
8915 return i40e_delete_clsflower(vsi, cls_flower);
8916 case FLOW_CLS_STATS:
8923 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8926 struct i40e_netdev_priv *np = cb_priv;
8928 if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8932 case TC_SETUP_CLSFLOWER:
8933 return i40e_setup_tc_cls_flower(np, type_data);
8940 static LIST_HEAD(i40e_block_cb_list);
8942 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8945 struct i40e_netdev_priv *np = netdev_priv(netdev);
8948 case TC_SETUP_QDISC_MQPRIO:
8949 return i40e_setup_tc(netdev, type_data);
8950 case TC_SETUP_BLOCK:
8951 return flow_block_cb_setup_simple(type_data,
8952 &i40e_block_cb_list,
8953 i40e_setup_tc_block_cb,
8961 * i40e_open - Called when a network interface is made active
8962 * @netdev: network interface device structure
8964 * The open entry point is called when a network interface is made
8965 * active by the system (IFF_UP). At this point all resources needed
8966 * for transmit and receive operations are allocated, the interrupt
8967 * handler is registered with the OS, the netdev watchdog subtask is
8968 * enabled, and the stack is notified that the interface is ready.
8970 * Returns 0 on success, negative value on failure
8972 int i40e_open(struct net_device *netdev)
8974 struct i40e_netdev_priv *np = netdev_priv(netdev);
8975 struct i40e_vsi *vsi = np->vsi;
8976 struct i40e_pf *pf = vsi->back;
8979 /* disallow open during test or if eeprom is broken */
8980 if (test_bit(__I40E_TESTING, pf->state) ||
8981 test_bit(__I40E_BAD_EEPROM, pf->state))
8984 netif_carrier_off(netdev);
8986 if (i40e_force_link_state(pf, true))
8989 err = i40e_vsi_open(vsi);
8993 /* configure global TSO hardware offload settings */
8994 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8995 TCP_FLAG_FIN) >> 16);
8996 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8998 TCP_FLAG_CWR) >> 16);
8999 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9000 udp_tunnel_get_rx_info(netdev);
9006 * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9007 * @vsi: vsi structure
9009 * This updates netdev's number of tx/rx queues
9011 * Returns status of setting tx/rx queues
9013 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9017 ret = netif_set_real_num_rx_queues(vsi->netdev,
9018 vsi->num_queue_pairs);
9022 return netif_set_real_num_tx_queues(vsi->netdev,
9023 vsi->num_queue_pairs);
9028 * @vsi: the VSI to open
9030 * Finish initialization of the VSI.
9032 * Returns 0 on success, negative value on failure
9034 * Note: expects to be called while under rtnl_lock()
9036 int i40e_vsi_open(struct i40e_vsi *vsi)
9038 struct i40e_pf *pf = vsi->back;
9039 char int_name[I40E_INT_NAME_STR_LEN];
9042 /* allocate descriptors */
9043 err = i40e_vsi_setup_tx_resources(vsi);
9046 err = i40e_vsi_setup_rx_resources(vsi);
9050 err = i40e_vsi_configure(vsi);
9055 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9056 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9057 err = i40e_vsi_request_irq(vsi, int_name);
9061 /* Notify the stack of the actual queue counts. */
9062 err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9064 goto err_set_queues;
9066 } else if (vsi->type == I40E_VSI_FDIR) {
9067 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9068 dev_driver_string(&pf->pdev->dev),
9069 dev_name(&pf->pdev->dev));
9070 err = i40e_vsi_request_irq(vsi, int_name);
9079 err = i40e_up_complete(vsi);
9081 goto err_up_complete;
9088 i40e_vsi_free_irq(vsi);
9090 i40e_vsi_free_rx_resources(vsi);
9092 i40e_vsi_free_tx_resources(vsi);
9093 if (vsi == pf->vsi[pf->lan_vsi])
9094 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9100 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9101 * @pf: Pointer to PF
9103 * This function destroys the hlist where all the Flow Director
9104 * filters were saved.
9106 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9108 struct i40e_fdir_filter *filter;
9109 struct i40e_flex_pit *pit_entry, *tmp;
9110 struct hlist_node *node2;
9112 hlist_for_each_entry_safe(filter, node2,
9113 &pf->fdir_filter_list, fdir_node) {
9114 hlist_del(&filter->fdir_node);
9118 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9119 list_del(&pit_entry->list);
9122 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9124 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9125 list_del(&pit_entry->list);
9128 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9130 pf->fdir_pf_active_filters = 0;
9131 i40e_reset_fdir_filter_cnt(pf);
9133 /* Reprogram the default input set for TCP/IPv4 */
9134 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9135 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9136 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9138 /* Reprogram the default input set for TCP/IPv6 */
9139 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9140 I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9141 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9143 /* Reprogram the default input set for UDP/IPv4 */
9144 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9145 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9146 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9148 /* Reprogram the default input set for UDP/IPv6 */
9149 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9150 I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9151 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9153 /* Reprogram the default input set for SCTP/IPv4 */
9154 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9155 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9156 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9158 /* Reprogram the default input set for SCTP/IPv6 */
9159 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9160 I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9161 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9163 /* Reprogram the default input set for Other/IPv4 */
9164 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9165 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9167 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9168 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9170 /* Reprogram the default input set for Other/IPv6 */
9171 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9172 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9174 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9175 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9179 * i40e_cloud_filter_exit - Cleans up the cloud filters
9180 * @pf: Pointer to PF
9182 * This function destroys the hlist where all the cloud filters
9185 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9187 struct i40e_cloud_filter *cfilter;
9188 struct hlist_node *node;
9190 hlist_for_each_entry_safe(cfilter, node,
9191 &pf->cloud_filter_list, cloud_node) {
9192 hlist_del(&cfilter->cloud_node);
9195 pf->num_cloud_filters = 0;
9197 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9198 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9199 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9200 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9201 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9206 * i40e_close - Disables a network interface
9207 * @netdev: network interface device structure
9209 * The close entry point is called when an interface is de-activated
9210 * by the OS. The hardware is still under the driver's control, but
9211 * this netdev interface is disabled.
9213 * Returns 0, this is not allowed to fail
9215 int i40e_close(struct net_device *netdev)
9217 struct i40e_netdev_priv *np = netdev_priv(netdev);
9218 struct i40e_vsi *vsi = np->vsi;
9220 i40e_vsi_close(vsi);
9226 * i40e_do_reset - Start a PF or Core Reset sequence
9227 * @pf: board private structure
9228 * @reset_flags: which reset is requested
9229 * @lock_acquired: indicates whether or not the lock has been acquired
9230 * before this function was called.
9232 * The essential difference in resets is that the PF Reset
9233 * doesn't clear the packet buffers, doesn't reset the PE
9234 * firmware, and doesn't bother the other PFs on the chip.
9236 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9240 /* do the biggest reset indicated */
9241 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9243 /* Request a Global Reset
9245 * This will start the chip's countdown to the actual full
9246 * chip reset event, and a warning interrupt to be sent
9247 * to all PFs, including the requestor. Our handler
9248 * for the warning interrupt will deal with the shutdown
9249 * and recovery of the switch setup.
9251 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9252 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9253 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9254 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9256 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9258 /* Request a Core Reset
9260 * Same as Global Reset, except does *not* include the MAC/PHY
9262 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9263 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9264 val |= I40E_GLGEN_RTRIG_CORER_MASK;
9265 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9266 i40e_flush(&pf->hw);
9268 } else if (reset_flags & I40E_PF_RESET_FLAG) {
9270 /* Request a PF Reset
9272 * Resets only the PF-specific registers
9274 * This goes directly to the tear-down and rebuild of
9275 * the switch, since we need to do all the recovery as
9276 * for the Core Reset.
9278 dev_dbg(&pf->pdev->dev, "PFR requested\n");
9279 i40e_handle_reset_warning(pf, lock_acquired);
9281 } else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9282 /* Request a PF Reset
9284 * Resets PF and reinitializes PFs VSI.
9286 i40e_prep_for_reset(pf);
9287 i40e_reset_and_rebuild(pf, true, lock_acquired);
9288 dev_info(&pf->pdev->dev,
9289 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9290 "FW LLDP is disabled\n" :
9291 "FW LLDP is enabled\n");
9293 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9296 /* Find the VSI(s) that requested a re-init */
9297 dev_info(&pf->pdev->dev,
9298 "VSI reinit requested\n");
9299 for (v = 0; v < pf->num_alloc_vsi; v++) {
9300 struct i40e_vsi *vsi = pf->vsi[v];
9303 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9305 i40e_vsi_reinit_locked(pf->vsi[v]);
9307 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9310 /* Find the VSI(s) that needs to be brought down */
9311 dev_info(&pf->pdev->dev, "VSI down requested\n");
9312 for (v = 0; v < pf->num_alloc_vsi; v++) {
9313 struct i40e_vsi *vsi = pf->vsi[v];
9316 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9318 set_bit(__I40E_VSI_DOWN, vsi->state);
9323 dev_info(&pf->pdev->dev,
9324 "bad reset request 0x%08x\n", reset_flags);
9328 #ifdef CONFIG_I40E_DCB
9330 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9331 * @pf: board private structure
9332 * @old_cfg: current DCB config
9333 * @new_cfg: new DCB config
9335 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9336 struct i40e_dcbx_config *old_cfg,
9337 struct i40e_dcbx_config *new_cfg)
9339 bool need_reconfig = false;
9341 /* Check if ETS configuration has changed */
9342 if (memcmp(&new_cfg->etscfg,
9344 sizeof(new_cfg->etscfg))) {
9345 /* If Priority Table has changed reconfig is needed */
9346 if (memcmp(&new_cfg->etscfg.prioritytable,
9347 &old_cfg->etscfg.prioritytable,
9348 sizeof(new_cfg->etscfg.prioritytable))) {
9349 need_reconfig = true;
9350 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9353 if (memcmp(&new_cfg->etscfg.tcbwtable,
9354 &old_cfg->etscfg.tcbwtable,
9355 sizeof(new_cfg->etscfg.tcbwtable)))
9356 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9358 if (memcmp(&new_cfg->etscfg.tsatable,
9359 &old_cfg->etscfg.tsatable,
9360 sizeof(new_cfg->etscfg.tsatable)))
9361 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9364 /* Check if PFC configuration has changed */
9365 if (memcmp(&new_cfg->pfc,
9367 sizeof(new_cfg->pfc))) {
9368 need_reconfig = true;
9369 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9372 /* Check if APP Table has changed */
9373 if (memcmp(&new_cfg->app,
9375 sizeof(new_cfg->app))) {
9376 need_reconfig = true;
9377 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9380 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9381 return need_reconfig;
9385 * i40e_handle_lldp_event - Handle LLDP Change MIB event
9386 * @pf: board private structure
9387 * @e: event info posted on ARQ
9389 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9390 struct i40e_arq_event_info *e)
9392 struct i40e_aqc_lldp_get_mib *mib =
9393 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9394 struct i40e_hw *hw = &pf->hw;
9395 struct i40e_dcbx_config tmp_dcbx_cfg;
9396 bool need_reconfig = false;
9400 /* X710-T*L 2.5G and 5G speeds don't support DCB */
9401 if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9402 (hw->phy.link_info.link_speed &
9403 ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9404 !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9405 /* let firmware decide if the DCB should be disabled */
9406 pf->flags |= I40E_FLAG_DCB_CAPABLE;
9408 /* Not DCB capable or capability disabled */
9409 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9412 /* Ignore if event is not for Nearest Bridge */
9413 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9414 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9415 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9416 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9419 /* Check MIB Type and return if event for Remote MIB update */
9420 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9421 dev_dbg(&pf->pdev->dev,
9422 "LLDP event mib type %s\n", type ? "remote" : "local");
9423 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9424 /* Update the remote cached instance and return */
9425 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9426 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9427 &hw->remote_dcbx_config);
9431 /* Store the old configuration */
9432 tmp_dcbx_cfg = hw->local_dcbx_config;
9434 /* Reset the old DCBx configuration data */
9435 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9436 /* Get updated DCBX data from firmware */
9437 ret = i40e_get_dcb_config(&pf->hw);
9439 /* X710-T*L 2.5G and 5G speeds don't support DCB */
9440 if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9441 (hw->phy.link_info.link_speed &
9442 (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9443 dev_warn(&pf->pdev->dev,
9444 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9445 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9447 dev_info(&pf->pdev->dev,
9448 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9450 i40e_aq_str(&pf->hw,
9451 pf->hw.aq.asq_last_status));
9456 /* No change detected in DCBX configs */
9457 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9458 sizeof(tmp_dcbx_cfg))) {
9459 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9463 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9464 &hw->local_dcbx_config);
9466 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9471 /* Enable DCB tagging only when more than one TC */
9472 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9473 pf->flags |= I40E_FLAG_DCB_ENABLED;
9475 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9477 set_bit(__I40E_PORT_SUSPENDED, pf->state);
9478 /* Reconfiguration needed quiesce all VSIs */
9479 i40e_pf_quiesce_all_vsi(pf);
9481 /* Changes in configuration update VEB/VSI */
9482 i40e_dcb_reconfigure(pf);
9484 ret = i40e_resume_port_tx(pf);
9486 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9487 /* In case of error no point in resuming VSIs */
9491 /* Wait for the PF's queues to be disabled */
9492 ret = i40e_pf_wait_queues_disabled(pf);
9494 /* Schedule PF reset to recover */
9495 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9496 i40e_service_event_schedule(pf);
9498 i40e_pf_unquiesce_all_vsi(pf);
9499 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9500 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9506 #endif /* CONFIG_I40E_DCB */
9509 * i40e_do_reset_safe - Protected reset path for userland calls.
9510 * @pf: board private structure
9511 * @reset_flags: which reset is requested
9514 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9517 i40e_do_reset(pf, reset_flags, true);
9522 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9523 * @pf: board private structure
9524 * @e: event info posted on ARQ
9526 * Handler for LAN Queue Overflow Event generated by the firmware for PF
9529 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9530 struct i40e_arq_event_info *e)
9532 struct i40e_aqc_lan_overflow *data =
9533 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9534 u32 queue = le32_to_cpu(data->prtdcb_rupto);
9535 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9536 struct i40e_hw *hw = &pf->hw;
9540 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9543 /* Queue belongs to VF, find the VF and issue VF reset */
9544 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9545 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9546 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9547 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9548 vf_id -= hw->func_caps.vf_base_id;
9549 vf = &pf->vf[vf_id];
9550 i40e_vc_notify_vf_reset(vf);
9551 /* Allow VF to process pending reset notification */
9553 i40e_reset_vf(vf, false);
9558 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9559 * @pf: board private structure
9561 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9565 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9566 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9571 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9572 * @pf: board private structure
9574 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9578 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9579 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9580 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9581 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9586 * i40e_get_global_fd_count - Get total FD filters programmed on device
9587 * @pf: board private structure
9589 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9593 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9594 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9595 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9596 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9601 * i40e_reenable_fdir_sb - Restore FDir SB capability
9602 * @pf: board private structure
9604 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9606 if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9607 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9608 (I40E_DEBUG_FD & pf->hw.debug_mask))
9609 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9613 * i40e_reenable_fdir_atr - Restore FDir ATR capability
9614 * @pf: board private structure
9616 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9618 if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9619 /* ATR uses the same filtering logic as SB rules. It only
9620 * functions properly if the input set mask is at the default
9621 * settings. It is safe to restore the default input set
9622 * because there are no active TCPv4 filter rules.
9624 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9625 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9626 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9628 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9629 (I40E_DEBUG_FD & pf->hw.debug_mask))
9630 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9635 * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9636 * @pf: board private structure
9637 * @filter: FDir filter to remove
9639 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9640 struct i40e_fdir_filter *filter)
9642 /* Update counters */
9643 pf->fdir_pf_active_filters--;
9646 switch (filter->flow_type) {
9648 pf->fd_tcp4_filter_cnt--;
9651 pf->fd_udp4_filter_cnt--;
9654 pf->fd_sctp4_filter_cnt--;
9657 pf->fd_tcp6_filter_cnt--;
9660 pf->fd_udp6_filter_cnt--;
9663 pf->fd_udp6_filter_cnt--;
9666 switch (filter->ipl4_proto) {
9668 pf->fd_tcp4_filter_cnt--;
9671 pf->fd_udp4_filter_cnt--;
9674 pf->fd_sctp4_filter_cnt--;
9677 pf->fd_ip4_filter_cnt--;
9681 case IPV6_USER_FLOW:
9682 switch (filter->ipl4_proto) {
9684 pf->fd_tcp6_filter_cnt--;
9687 pf->fd_udp6_filter_cnt--;
9690 pf->fd_sctp6_filter_cnt--;
9693 pf->fd_ip6_filter_cnt--;
9699 /* Remove the filter from the list and free memory */
9700 hlist_del(&filter->fdir_node);
9705 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9706 * @pf: board private structure
9708 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9710 struct i40e_fdir_filter *filter;
9711 u32 fcnt_prog, fcnt_avail;
9712 struct hlist_node *node;
9714 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9717 /* Check if we have enough room to re-enable FDir SB capability. */
9718 fcnt_prog = i40e_get_global_fd_count(pf);
9719 fcnt_avail = pf->fdir_pf_filter_count;
9720 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9721 (pf->fd_add_err == 0) ||
9722 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9723 i40e_reenable_fdir_sb(pf);
9725 /* We should wait for even more space before re-enabling ATR.
9726 * Additionally, we cannot enable ATR as long as we still have TCP SB
9729 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9730 pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9731 i40e_reenable_fdir_atr(pf);
9733 /* if hw had a problem adding a filter, delete it */
9734 if (pf->fd_inv > 0) {
9735 hlist_for_each_entry_safe(filter, node,
9736 &pf->fdir_filter_list, fdir_node)
9737 if (filter->fd_id == pf->fd_inv)
9738 i40e_delete_invalid_filter(pf, filter);
9742 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9743 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9745 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9746 * @pf: board private structure
9748 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9750 unsigned long min_flush_time;
9751 int flush_wait_retry = 50;
9752 bool disable_atr = false;
9756 if (!time_after(jiffies, pf->fd_flush_timestamp +
9757 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9760 /* If the flush is happening too quick and we have mostly SB rules we
9761 * should not re-enable ATR for some time.
9763 min_flush_time = pf->fd_flush_timestamp +
9764 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9765 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9767 if (!(time_after(jiffies, min_flush_time)) &&
9768 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9769 if (I40E_DEBUG_FD & pf->hw.debug_mask)
9770 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9774 pf->fd_flush_timestamp = jiffies;
9775 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9776 /* flush all filters */
9777 wr32(&pf->hw, I40E_PFQF_CTL_1,
9778 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9779 i40e_flush(&pf->hw);
9783 /* Check FD flush status every 5-6msec */
9784 usleep_range(5000, 6000);
9785 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9786 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9788 } while (flush_wait_retry--);
9789 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9790 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9792 /* replay sideband filters */
9793 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9794 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9795 clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9796 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9797 if (I40E_DEBUG_FD & pf->hw.debug_mask)
9798 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9803 * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9804 * @pf: board private structure
9806 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9808 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9812 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9813 * @pf: board private structure
9815 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9818 /* if interface is down do nothing */
9819 if (test_bit(__I40E_DOWN, pf->state))
9822 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9823 i40e_fdir_flush_and_replay(pf);
9825 i40e_fdir_check_and_reenable(pf);
9830 * i40e_vsi_link_event - notify VSI of a link event
9831 * @vsi: vsi to be notified
9832 * @link_up: link up or down
9834 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9836 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9839 switch (vsi->type) {
9841 if (!vsi->netdev || !vsi->netdev_registered)
9845 netif_carrier_on(vsi->netdev);
9846 netif_tx_wake_all_queues(vsi->netdev);
9848 netif_carrier_off(vsi->netdev);
9849 netif_tx_stop_all_queues(vsi->netdev);
9853 case I40E_VSI_SRIOV:
9854 case I40E_VSI_VMDQ2:
9856 case I40E_VSI_IWARP:
9857 case I40E_VSI_MIRROR:
9859 /* there is no notification for other VSIs */
9865 * i40e_veb_link_event - notify elements on the veb of a link event
9866 * @veb: veb to be notified
9867 * @link_up: link up or down
9869 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9874 if (!veb || !veb->pf)
9878 /* depth first... */
9879 for (i = 0; i < I40E_MAX_VEB; i++)
9880 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9881 i40e_veb_link_event(pf->veb[i], link_up);
9883 /* ... now the local VSIs */
9884 for (i = 0; i < pf->num_alloc_vsi; i++)
9885 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9886 i40e_vsi_link_event(pf->vsi[i], link_up);
9890 * i40e_link_event - Update netif_carrier status
9891 * @pf: board private structure
9893 static void i40e_link_event(struct i40e_pf *pf)
9895 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9896 u8 new_link_speed, old_link_speed;
9897 bool new_link, old_link;
9899 #ifdef CONFIG_I40E_DCB
9901 #endif /* CONFIG_I40E_DCB */
9903 /* set this to force the get_link_status call to refresh state */
9904 pf->hw.phy.get_link_info = true;
9905 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9906 status = i40e_get_link_status(&pf->hw, &new_link);
9908 /* On success, disable temp link polling */
9909 if (status == I40E_SUCCESS) {
9910 clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9912 /* Enable link polling temporarily until i40e_get_link_status
9913 * returns I40E_SUCCESS
9915 set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9916 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9921 old_link_speed = pf->hw.phy.link_info_old.link_speed;
9922 new_link_speed = pf->hw.phy.link_info.link_speed;
9924 if (new_link == old_link &&
9925 new_link_speed == old_link_speed &&
9926 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9927 new_link == netif_carrier_ok(vsi->netdev)))
9930 i40e_print_link_message(vsi, new_link);
9932 /* Notify the base of the switch tree connected to
9933 * the link. Floating VEBs are not notified.
9935 if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9936 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9938 i40e_vsi_link_event(vsi, new_link);
9941 i40e_vc_notify_link_state(pf);
9943 if (pf->flags & I40E_FLAG_PTP)
9944 i40e_ptp_set_increment(pf);
9945 #ifdef CONFIG_I40E_DCB
9946 if (new_link == old_link)
9948 /* Not SW DCB so firmware will take care of default settings */
9949 if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9952 /* We cover here only link down, as after link up in case of SW DCB
9953 * SW LLDP agent will take care of setting it up
9956 dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9957 memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9958 err = i40e_dcb_sw_default_config(pf);
9960 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9961 I40E_FLAG_DCB_ENABLED);
9963 pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9964 DCB_CAP_DCBX_VER_IEEE;
9965 pf->flags |= I40E_FLAG_DCB_CAPABLE;
9966 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9969 #endif /* CONFIG_I40E_DCB */
9973 * i40e_watchdog_subtask - periodic checks not using event driven response
9974 * @pf: board private structure
9976 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9980 /* if interface is down do nothing */
9981 if (test_bit(__I40E_DOWN, pf->state) ||
9982 test_bit(__I40E_CONFIG_BUSY, pf->state))
9985 /* make sure we don't do these things too often */
9986 if (time_before(jiffies, (pf->service_timer_previous +
9987 pf->service_timer_period)))
9989 pf->service_timer_previous = jiffies;
9991 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9992 test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9993 i40e_link_event(pf);
9995 /* Update the stats for active netdevs so the network stack
9996 * can look at updated numbers whenever it cares to
9998 for (i = 0; i < pf->num_alloc_vsi; i++)
9999 if (pf->vsi[i] && pf->vsi[i]->netdev)
10000 i40e_update_stats(pf->vsi[i]);
10002 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10003 /* Update the stats for the active switching components */
10004 for (i = 0; i < I40E_MAX_VEB; i++)
10006 i40e_update_veb_stats(pf->veb[i]);
10009 i40e_ptp_rx_hang(pf);
10010 i40e_ptp_tx_hang(pf);
10014 * i40e_reset_subtask - Set up for resetting the device and driver
10015 * @pf: board private structure
10017 static void i40e_reset_subtask(struct i40e_pf *pf)
10019 u32 reset_flags = 0;
10021 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10022 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10023 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10025 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10026 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10027 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10029 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10030 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10031 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10033 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10034 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10035 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10037 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10038 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10039 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10042 /* If there's a recovery already waiting, it takes
10043 * precedence before starting a new reset sequence.
10045 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10046 i40e_prep_for_reset(pf);
10048 i40e_rebuild(pf, false, false);
10051 /* If we're already down or resetting, just bail */
10053 !test_bit(__I40E_DOWN, pf->state) &&
10054 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10055 i40e_do_reset(pf, reset_flags, false);
10060 * i40e_handle_link_event - Handle link event
10061 * @pf: board private structure
10062 * @e: event info posted on ARQ
10064 static void i40e_handle_link_event(struct i40e_pf *pf,
10065 struct i40e_arq_event_info *e)
10067 struct i40e_aqc_get_link_status *status =
10068 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10070 /* Do a new status request to re-enable LSE reporting
10071 * and load new status information into the hw struct
10072 * This completely ignores any state information
10073 * in the ARQ event info, instead choosing to always
10074 * issue the AQ update link status command.
10076 i40e_link_event(pf);
10078 /* Check if module meets thermal requirements */
10079 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10080 dev_err(&pf->pdev->dev,
10081 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10082 dev_err(&pf->pdev->dev,
10083 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10085 /* check for unqualified module, if link is down, suppress
10086 * the message if link was forced to be down.
10088 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10089 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10090 (!(status->link_info & I40E_AQ_LINK_UP)) &&
10091 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10092 dev_err(&pf->pdev->dev,
10093 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10094 dev_err(&pf->pdev->dev,
10095 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10101 * i40e_clean_adminq_subtask - Clean the AdminQ rings
10102 * @pf: board private structure
10104 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10106 struct i40e_arq_event_info event;
10107 struct i40e_hw *hw = &pf->hw;
10108 u16 pending, i = 0;
10114 /* Do not run clean AQ when PF reset fails */
10115 if (test_bit(__I40E_RESET_FAILED, pf->state))
10118 /* check for error indications */
10119 val = rd32(&pf->hw, pf->hw.aq.arq.len);
10121 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10122 if (hw->debug_mask & I40E_DEBUG_AQ)
10123 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10124 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10126 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10127 if (hw->debug_mask & I40E_DEBUG_AQ)
10128 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10129 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10130 pf->arq_overflows++;
10132 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10133 if (hw->debug_mask & I40E_DEBUG_AQ)
10134 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10135 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10138 wr32(&pf->hw, pf->hw.aq.arq.len, val);
10140 val = rd32(&pf->hw, pf->hw.aq.asq.len);
10142 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10143 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10144 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10145 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10147 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10148 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10149 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10150 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10152 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10153 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10154 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10155 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10158 wr32(&pf->hw, pf->hw.aq.asq.len, val);
10160 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10161 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10162 if (!event.msg_buf)
10166 ret = i40e_clean_arq_element(hw, &event, &pending);
10167 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10170 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10174 opcode = le16_to_cpu(event.desc.opcode);
10177 case i40e_aqc_opc_get_link_status:
10179 i40e_handle_link_event(pf, &event);
10182 case i40e_aqc_opc_send_msg_to_pf:
10183 ret = i40e_vc_process_vf_msg(pf,
10184 le16_to_cpu(event.desc.retval),
10185 le32_to_cpu(event.desc.cookie_high),
10186 le32_to_cpu(event.desc.cookie_low),
10190 case i40e_aqc_opc_lldp_update_mib:
10191 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10192 #ifdef CONFIG_I40E_DCB
10194 i40e_handle_lldp_event(pf, &event);
10196 #endif /* CONFIG_I40E_DCB */
10198 case i40e_aqc_opc_event_lan_overflow:
10199 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10200 i40e_handle_lan_overflow_event(pf, &event);
10202 case i40e_aqc_opc_send_msg_to_peer:
10203 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10205 case i40e_aqc_opc_nvm_erase:
10206 case i40e_aqc_opc_nvm_update:
10207 case i40e_aqc_opc_oem_post_update:
10208 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10209 "ARQ NVM operation 0x%04x completed\n",
10213 dev_info(&pf->pdev->dev,
10214 "ARQ: Unknown event 0x%04x ignored\n",
10218 } while (i++ < pf->adminq_work_limit);
10220 if (i < pf->adminq_work_limit)
10221 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10223 /* re-enable Admin queue interrupt cause */
10224 val = rd32(hw, I40E_PFINT_ICR0_ENA);
10225 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10226 wr32(hw, I40E_PFINT_ICR0_ENA, val);
10229 kfree(event.msg_buf);
10233 * i40e_verify_eeprom - make sure eeprom is good to use
10234 * @pf: board private structure
10236 static void i40e_verify_eeprom(struct i40e_pf *pf)
10240 err = i40e_diag_eeprom_test(&pf->hw);
10242 /* retry in case of garbage read */
10243 err = i40e_diag_eeprom_test(&pf->hw);
10245 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10247 set_bit(__I40E_BAD_EEPROM, pf->state);
10251 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10252 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10253 clear_bit(__I40E_BAD_EEPROM, pf->state);
10258 * i40e_enable_pf_switch_lb
10259 * @pf: pointer to the PF structure
10261 * enable switch loop back or die - no point in a return value
10263 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10265 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10266 struct i40e_vsi_context ctxt;
10269 ctxt.seid = pf->main_vsi_seid;
10270 ctxt.pf_num = pf->hw.pf_id;
10272 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10274 dev_info(&pf->pdev->dev,
10275 "couldn't get PF vsi config, err %pe aq_err %s\n",
10277 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10280 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10281 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10282 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10284 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10286 dev_info(&pf->pdev->dev,
10287 "update vsi switch failed, err %pe aq_err %s\n",
10289 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10294 * i40e_disable_pf_switch_lb
10295 * @pf: pointer to the PF structure
10297 * disable switch loop back or die - no point in a return value
10299 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10301 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10302 struct i40e_vsi_context ctxt;
10305 ctxt.seid = pf->main_vsi_seid;
10306 ctxt.pf_num = pf->hw.pf_id;
10308 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10310 dev_info(&pf->pdev->dev,
10311 "couldn't get PF vsi config, err %pe aq_err %s\n",
10313 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10316 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10317 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10318 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10320 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10322 dev_info(&pf->pdev->dev,
10323 "update vsi switch failed, err %pe aq_err %s\n",
10325 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10330 * i40e_config_bridge_mode - Configure the HW bridge mode
10331 * @veb: pointer to the bridge instance
10333 * Configure the loop back mode for the LAN VSI that is downlink to the
10334 * specified HW bridge instance. It is expected this function is called
10335 * when a new HW bridge is instantiated.
10337 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10339 struct i40e_pf *pf = veb->pf;
10341 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10342 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10343 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10344 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10345 i40e_disable_pf_switch_lb(pf);
10347 i40e_enable_pf_switch_lb(pf);
10351 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10352 * @veb: pointer to the VEB instance
10354 * This is a recursive function that first builds the attached VSIs then
10355 * recurses in to build the next layer of VEB. We track the connections
10356 * through our own index numbers because the seid's from the HW could
10357 * change across the reset.
10359 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10361 struct i40e_vsi *ctl_vsi = NULL;
10362 struct i40e_pf *pf = veb->pf;
10366 /* build VSI that owns this VEB, temporarily attached to base VEB */
10367 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10369 pf->vsi[v]->veb_idx == veb->idx &&
10370 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10371 ctl_vsi = pf->vsi[v];
10376 dev_info(&pf->pdev->dev,
10377 "missing owner VSI for veb_idx %d\n", veb->idx);
10379 goto end_reconstitute;
10381 if (ctl_vsi != pf->vsi[pf->lan_vsi])
10382 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10383 ret = i40e_add_vsi(ctl_vsi);
10385 dev_info(&pf->pdev->dev,
10386 "rebuild of veb_idx %d owner VSI failed: %d\n",
10388 goto end_reconstitute;
10390 i40e_vsi_reset_stats(ctl_vsi);
10392 /* create the VEB in the switch and move the VSI onto the VEB */
10393 ret = i40e_add_veb(veb, ctl_vsi);
10395 goto end_reconstitute;
10397 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10398 veb->bridge_mode = BRIDGE_MODE_VEB;
10400 veb->bridge_mode = BRIDGE_MODE_VEPA;
10401 i40e_config_bridge_mode(veb);
10403 /* create the remaining VSIs attached to this VEB */
10404 for (v = 0; v < pf->num_alloc_vsi; v++) {
10405 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10408 if (pf->vsi[v]->veb_idx == veb->idx) {
10409 struct i40e_vsi *vsi = pf->vsi[v];
10411 vsi->uplink_seid = veb->seid;
10412 ret = i40e_add_vsi(vsi);
10414 dev_info(&pf->pdev->dev,
10415 "rebuild of vsi_idx %d failed: %d\n",
10417 goto end_reconstitute;
10419 i40e_vsi_reset_stats(vsi);
10423 /* create any VEBs attached to this VEB - RECURSION */
10424 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10425 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10426 pf->veb[veb_idx]->uplink_seid = veb->seid;
10427 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10438 * i40e_get_capabilities - get info about the HW
10439 * @pf: the PF struct
10440 * @list_type: AQ capability to be queried
10442 static int i40e_get_capabilities(struct i40e_pf *pf,
10443 enum i40e_admin_queue_opc list_type)
10445 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10450 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10452 cap_buf = kzalloc(buf_len, GFP_KERNEL);
10456 /* this loads the data into the hw struct for us */
10457 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10458 &data_size, list_type,
10460 /* data loaded, buffer no longer needed */
10463 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10464 /* retry with a larger buffer */
10465 buf_len = data_size;
10466 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10467 dev_info(&pf->pdev->dev,
10468 "capability discovery failed, err %pe aq_err %s\n",
10470 i40e_aq_str(&pf->hw,
10471 pf->hw.aq.asq_last_status));
10476 if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10477 if (list_type == i40e_aqc_opc_list_func_capabilities) {
10478 dev_info(&pf->pdev->dev,
10479 "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",
10480 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10481 pf->hw.func_caps.num_msix_vectors,
10482 pf->hw.func_caps.num_msix_vectors_vf,
10483 pf->hw.func_caps.fd_filters_guaranteed,
10484 pf->hw.func_caps.fd_filters_best_effort,
10485 pf->hw.func_caps.num_tx_qp,
10486 pf->hw.func_caps.num_vsis);
10487 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10488 dev_info(&pf->pdev->dev,
10489 "switch_mode=0x%04x, function_valid=0x%08x\n",
10490 pf->hw.dev_caps.switch_mode,
10491 pf->hw.dev_caps.valid_functions);
10492 dev_info(&pf->pdev->dev,
10493 "SR-IOV=%d, num_vfs for all function=%u\n",
10494 pf->hw.dev_caps.sr_iov_1_1,
10495 pf->hw.dev_caps.num_vfs);
10496 dev_info(&pf->pdev->dev,
10497 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10498 pf->hw.dev_caps.num_vsis,
10499 pf->hw.dev_caps.num_rx_qp,
10500 pf->hw.dev_caps.num_tx_qp);
10503 if (list_type == i40e_aqc_opc_list_func_capabilities) {
10504 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10505 + pf->hw.func_caps.num_vfs)
10506 if (pf->hw.revision_id == 0 &&
10507 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10508 dev_info(&pf->pdev->dev,
10509 "got num_vsis %d, setting num_vsis to %d\n",
10510 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10511 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10517 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10520 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10521 * @pf: board private structure
10523 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10525 struct i40e_vsi *vsi;
10527 /* quick workaround for an NVM issue that leaves a critical register
10530 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10531 static const u32 hkey[] = {
10532 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10533 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10534 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10538 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10539 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10542 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10545 /* find existing VSI and see if it needs configuring */
10546 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10548 /* create a new VSI if none exists */
10550 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10551 pf->vsi[pf->lan_vsi]->seid, 0);
10553 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10554 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10555 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10560 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10564 * i40e_fdir_teardown - release the Flow Director resources
10565 * @pf: board private structure
10567 static void i40e_fdir_teardown(struct i40e_pf *pf)
10569 struct i40e_vsi *vsi;
10571 i40e_fdir_filter_exit(pf);
10572 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10574 i40e_vsi_release(vsi);
10578 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10579 * @vsi: PF main vsi
10580 * @seid: seid of main or channel VSIs
10582 * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10583 * existed before reset
10585 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10587 struct i40e_cloud_filter *cfilter;
10588 struct i40e_pf *pf = vsi->back;
10589 struct hlist_node *node;
10592 /* Add cloud filters back if they exist */
10593 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10595 if (cfilter->seid != seid)
10598 if (cfilter->dst_port)
10599 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10602 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10605 dev_dbg(&pf->pdev->dev,
10606 "Failed to rebuild cloud filter, err %pe aq_err %s\n",
10608 i40e_aq_str(&pf->hw,
10609 pf->hw.aq.asq_last_status));
10617 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10618 * @vsi: PF main vsi
10620 * Rebuilds channel VSIs if they existed before reset
10622 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10624 struct i40e_channel *ch, *ch_tmp;
10627 if (list_empty(&vsi->ch_list))
10630 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10631 if (!ch->initialized)
10633 /* Proceed with creation of channel (VMDq2) VSI */
10634 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10636 dev_info(&vsi->back->pdev->dev,
10637 "failed to rebuild channels using uplink_seid %u\n",
10641 /* Reconfigure TX queues using QTX_CTL register */
10642 ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10644 dev_info(&vsi->back->pdev->dev,
10645 "failed to configure TX rings for channel %u\n",
10649 /* update 'next_base_queue' */
10650 vsi->next_base_queue = vsi->next_base_queue +
10651 ch->num_queue_pairs;
10652 if (ch->max_tx_rate) {
10653 u64 credits = ch->max_tx_rate;
10655 if (i40e_set_bw_limit(vsi, ch->seid,
10659 do_div(credits, I40E_BW_CREDIT_DIVISOR);
10660 dev_dbg(&vsi->back->pdev->dev,
10661 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10666 ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10668 dev_dbg(&vsi->back->pdev->dev,
10669 "Failed to rebuild cloud filters for channel VSI %u\n",
10678 * i40e_clean_xps_state - clean xps state for every tx_ring
10679 * @vsi: ptr to the VSI
10681 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10686 for (i = 0; i < vsi->num_queue_pairs; i++)
10687 if (vsi->tx_rings[i])
10688 clear_bit(__I40E_TX_XPS_INIT_DONE,
10689 vsi->tx_rings[i]->state);
10693 * i40e_prep_for_reset - prep for the core to reset
10694 * @pf: board private structure
10696 * Close up the VFs and other things in prep for PF Reset.
10698 static void i40e_prep_for_reset(struct i40e_pf *pf)
10700 struct i40e_hw *hw = &pf->hw;
10704 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10705 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10707 if (i40e_check_asq_alive(&pf->hw))
10708 i40e_vc_notify_reset(pf);
10710 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10712 /* quiesce the VSIs and their queues that are not already DOWN */
10713 i40e_pf_quiesce_all_vsi(pf);
10715 for (v = 0; v < pf->num_alloc_vsi; v++) {
10717 i40e_clean_xps_state(pf->vsi[v]);
10718 pf->vsi[v]->seid = 0;
10722 i40e_shutdown_adminq(&pf->hw);
10724 /* call shutdown HMC */
10725 if (hw->hmc.hmc_obj) {
10726 ret = i40e_shutdown_lan_hmc(hw);
10728 dev_warn(&pf->pdev->dev,
10729 "shutdown_lan_hmc failed: %d\n", ret);
10732 /* Save the current PTP time so that we can restore the time after the
10735 i40e_ptp_save_hw_time(pf);
10739 * i40e_send_version - update firmware with driver version
10742 static void i40e_send_version(struct i40e_pf *pf)
10744 struct i40e_driver_version dv;
10746 dv.major_version = 0xff;
10747 dv.minor_version = 0xff;
10748 dv.build_version = 0xff;
10749 dv.subbuild_version = 0;
10750 strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10751 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10755 * i40e_get_oem_version - get OEM specific version information
10756 * @hw: pointer to the hardware structure
10758 static void i40e_get_oem_version(struct i40e_hw *hw)
10760 u16 block_offset = 0xffff;
10761 u16 block_length = 0;
10762 u16 capabilities = 0;
10766 #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
10767 #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
10768 #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
10769 #define I40E_NVM_OEM_GEN_OFFSET 0x02
10770 #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
10771 #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
10772 #define I40E_NVM_OEM_LENGTH 3
10774 /* Check if pointer to OEM version block is valid. */
10775 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10776 if (block_offset == 0xffff)
10779 /* Check if OEM version block has correct length. */
10780 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10782 if (block_length < I40E_NVM_OEM_LENGTH)
10785 /* Check if OEM version format is as expected. */
10786 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10788 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10791 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10793 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10795 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10796 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10800 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10801 * @pf: board private structure
10803 static int i40e_reset(struct i40e_pf *pf)
10805 struct i40e_hw *hw = &pf->hw;
10808 ret = i40e_pf_reset(hw);
10810 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10811 set_bit(__I40E_RESET_FAILED, pf->state);
10812 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10820 * i40e_rebuild - rebuild using a saved config
10821 * @pf: board private structure
10822 * @reinit: if the Main VSI needs to re-initialized.
10823 * @lock_acquired: indicates whether or not the lock has been acquired
10824 * before this function was called.
10826 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10828 const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10829 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10830 struct i40e_hw *hw = &pf->hw;
10835 if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10836 is_recovery_mode_reported)
10837 i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10839 if (test_bit(__I40E_DOWN, pf->state) &&
10840 !test_bit(__I40E_RECOVERY_MODE, pf->state))
10841 goto clear_recovery;
10842 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10844 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10845 ret = i40e_init_adminq(&pf->hw);
10847 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10849 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10850 goto clear_recovery;
10852 i40e_get_oem_version(&pf->hw);
10854 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10855 /* The following delay is necessary for firmware update. */
10859 /* re-verify the eeprom if we just had an EMP reset */
10860 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10861 i40e_verify_eeprom(pf);
10863 /* if we are going out of or into recovery mode we have to act
10864 * accordingly with regard to resources initialization
10865 * and deinitialization
10867 if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10868 if (i40e_get_capabilities(pf,
10869 i40e_aqc_opc_list_func_capabilities))
10872 if (is_recovery_mode_reported) {
10873 /* we're staying in recovery mode so we'll reinitialize
10876 if (i40e_setup_misc_vector_for_recovery_mode(pf))
10879 if (!lock_acquired)
10881 /* we're going out of recovery mode so we'll free
10882 * the IRQ allocated specifically for recovery mode
10883 * and restore the interrupt scheme
10885 free_irq(pf->pdev->irq, pf);
10886 i40e_clear_interrupt_scheme(pf);
10887 if (i40e_restore_interrupt_scheme(pf))
10891 /* tell the firmware that we're starting */
10892 i40e_send_version(pf);
10894 /* bail out in case recovery mode was detected, as there is
10895 * no need for further configuration.
10900 i40e_clear_pxe_mode(hw);
10901 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10903 goto end_core_reset;
10905 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10906 hw->func_caps.num_rx_qp, 0, 0);
10908 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10909 goto end_core_reset;
10911 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10913 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10914 goto end_core_reset;
10917 #ifdef CONFIG_I40E_DCB
10918 /* Enable FW to write a default DCB config on link-up
10919 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10920 * is not supported with new link speed
10922 if (i40e_is_tc_mqprio_enabled(pf)) {
10923 i40e_aq_set_dcb_parameters(hw, false, NULL);
10925 if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10926 (hw->phy.link_info.link_speed &
10927 (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10928 i40e_aq_set_dcb_parameters(hw, false, NULL);
10929 dev_warn(&pf->pdev->dev,
10930 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10931 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10933 i40e_aq_set_dcb_parameters(hw, true, NULL);
10934 ret = i40e_init_pf_dcb(pf);
10936 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10938 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10939 /* Continue without DCB enabled */
10944 #endif /* CONFIG_I40E_DCB */
10945 if (!lock_acquired)
10947 ret = i40e_setup_pf_switch(pf, reinit, true);
10951 /* The driver only wants link up/down and module qualification
10952 * reports from firmware. Note the negative logic.
10954 ret = i40e_aq_set_phy_int_mask(&pf->hw,
10955 ~(I40E_AQ_EVENT_LINK_UPDOWN |
10956 I40E_AQ_EVENT_MEDIA_NA |
10957 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10959 dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10961 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10963 /* Rebuild the VSIs and VEBs that existed before reset.
10964 * They are still in our local switch element arrays, so only
10965 * need to rebuild the switch model in the HW.
10967 * If there were VEBs but the reconstitution failed, we'll try
10968 * to recover minimal use by getting the basic PF VSI working.
10970 if (vsi->uplink_seid != pf->mac_seid) {
10971 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10972 /* find the one VEB connected to the MAC, and find orphans */
10973 for (v = 0; v < I40E_MAX_VEB; v++) {
10977 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10978 pf->veb[v]->uplink_seid == 0) {
10979 ret = i40e_reconstitute_veb(pf->veb[v]);
10984 /* If Main VEB failed, we're in deep doodoo,
10985 * so give up rebuilding the switch and set up
10986 * for minimal rebuild of PF VSI.
10987 * If orphan failed, we'll report the error
10988 * but try to keep going.
10990 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10991 dev_info(&pf->pdev->dev,
10992 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10994 vsi->uplink_seid = pf->mac_seid;
10996 } else if (pf->veb[v]->uplink_seid == 0) {
10997 dev_info(&pf->pdev->dev,
10998 "rebuild of orphan VEB failed: %d\n",
11005 if (vsi->uplink_seid == pf->mac_seid) {
11006 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11007 /* no VEB, so rebuild only the Main VSI */
11008 ret = i40e_add_vsi(vsi);
11010 dev_info(&pf->pdev->dev,
11011 "rebuild of Main VSI failed: %d\n", ret);
11016 if (vsi->mqprio_qopt.max_rate[0]) {
11017 u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11018 vsi->mqprio_qopt.max_rate[0]);
11021 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11025 credits = max_tx_rate;
11026 do_div(credits, I40E_BW_CREDIT_DIVISOR);
11027 dev_dbg(&vsi->back->pdev->dev,
11028 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11034 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11038 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11039 * for this main VSI if they exist
11041 ret = i40e_rebuild_channels(vsi);
11045 /* Reconfigure hardware for allowing smaller MSS in the case
11046 * of TSO, so that we avoid the MDD being fired and causing
11047 * a reset in the case of small MSS+TSO.
11049 #define I40E_REG_MSS 0x000E64DC
11050 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11051 #define I40E_64BYTE_MSS 0x400000
11052 val = rd32(hw, I40E_REG_MSS);
11053 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11054 val &= ~I40E_REG_MSS_MIN_MASK;
11055 val |= I40E_64BYTE_MSS;
11056 wr32(hw, I40E_REG_MSS, val);
11059 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11061 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11063 dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11065 i40e_aq_str(&pf->hw,
11066 pf->hw.aq.asq_last_status));
11068 /* reinit the misc interrupt */
11069 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11070 ret = i40e_setup_misc_vector(pf);
11075 /* Add a filter to drop all Flow control frames from any VSI from being
11076 * transmitted. By doing so we stop a malicious VF from sending out
11077 * PAUSE or PFC frames and potentially controlling traffic for other
11079 * The FW can still send Flow control frames if enabled.
11081 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11082 pf->main_vsi_seid);
11084 /* restart the VSIs that were rebuilt and running before the reset */
11085 i40e_pf_unquiesce_all_vsi(pf);
11087 /* Release the RTNL lock before we start resetting VFs */
11088 if (!lock_acquired)
11091 /* Restore promiscuous settings */
11092 ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11094 dev_warn(&pf->pdev->dev,
11095 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11096 pf->cur_promisc ? "on" : "off",
11098 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11100 i40e_reset_all_vfs(pf, true);
11102 /* tell the firmware that we're starting */
11103 i40e_send_version(pf);
11105 /* We've already released the lock, so don't do it again */
11106 goto end_core_reset;
11109 if (!lock_acquired)
11112 clear_bit(__I40E_RESET_FAILED, pf->state);
11114 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11115 clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11119 * i40e_reset_and_rebuild - reset and rebuild using a saved config
11120 * @pf: board private structure
11121 * @reinit: if the Main VSI needs to re-initialized.
11122 * @lock_acquired: indicates whether or not the lock has been acquired
11123 * before this function was called.
11125 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11126 bool lock_acquired)
11130 if (test_bit(__I40E_IN_REMOVE, pf->state))
11132 /* Now we wait for GRST to settle out.
11133 * We don't have to delete the VEBs or VSIs from the hw switch
11134 * because the reset will make them disappear.
11136 ret = i40e_reset(pf);
11138 i40e_rebuild(pf, reinit, lock_acquired);
11142 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11143 * @pf: board private structure
11145 * Close up the VFs and other things in prep for a Core Reset,
11146 * then get ready to rebuild the world.
11147 * @lock_acquired: indicates whether or not the lock has been acquired
11148 * before this function was called.
11150 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11152 i40e_prep_for_reset(pf);
11153 i40e_reset_and_rebuild(pf, false, lock_acquired);
11157 * i40e_handle_mdd_event
11158 * @pf: pointer to the PF structure
11160 * Called from the MDD irq handler to identify possibly malicious vfs
11162 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11164 struct i40e_hw *hw = &pf->hw;
11165 bool mdd_detected = false;
11166 struct i40e_vf *vf;
11170 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11173 /* find what triggered the MDD event */
11174 reg = rd32(hw, I40E_GL_MDET_TX);
11175 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11176 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11177 I40E_GL_MDET_TX_PF_NUM_SHIFT;
11178 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11179 I40E_GL_MDET_TX_VF_NUM_SHIFT;
11180 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11181 I40E_GL_MDET_TX_EVENT_SHIFT;
11182 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11183 I40E_GL_MDET_TX_QUEUE_SHIFT) -
11184 pf->hw.func_caps.base_queue;
11185 if (netif_msg_tx_err(pf))
11186 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11187 event, queue, pf_num, vf_num);
11188 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11189 mdd_detected = true;
11191 reg = rd32(hw, I40E_GL_MDET_RX);
11192 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11193 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11194 I40E_GL_MDET_RX_FUNCTION_SHIFT;
11195 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11196 I40E_GL_MDET_RX_EVENT_SHIFT;
11197 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11198 I40E_GL_MDET_RX_QUEUE_SHIFT) -
11199 pf->hw.func_caps.base_queue;
11200 if (netif_msg_rx_err(pf))
11201 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11202 event, queue, func);
11203 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11204 mdd_detected = true;
11207 if (mdd_detected) {
11208 reg = rd32(hw, I40E_PF_MDET_TX);
11209 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11210 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11211 dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11213 reg = rd32(hw, I40E_PF_MDET_RX);
11214 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11215 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11216 dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11220 /* see if one of the VFs needs its hand slapped */
11221 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11223 reg = rd32(hw, I40E_VP_MDET_TX(i));
11224 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11225 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11226 vf->num_mdd_events++;
11227 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11229 dev_info(&pf->pdev->dev,
11230 "Use PF Control I/F to re-enable the VF\n");
11231 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11234 reg = rd32(hw, I40E_VP_MDET_RX(i));
11235 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11236 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11237 vf->num_mdd_events++;
11238 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11240 dev_info(&pf->pdev->dev,
11241 "Use PF Control I/F to re-enable the VF\n");
11242 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11246 /* re-enable mdd interrupt cause */
11247 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11248 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11249 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11250 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11255 * i40e_service_task - Run the driver's async subtasks
11256 * @work: pointer to work_struct containing our data
11258 static void i40e_service_task(struct work_struct *work)
11260 struct i40e_pf *pf = container_of(work,
11263 unsigned long start_time = jiffies;
11265 /* don't bother with service tasks if a reset is in progress */
11266 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11267 test_bit(__I40E_SUSPENDED, pf->state))
11270 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11273 if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11274 i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11275 i40e_sync_filters_subtask(pf);
11276 i40e_reset_subtask(pf);
11277 i40e_handle_mdd_event(pf);
11278 i40e_vc_process_vflr_event(pf);
11279 i40e_watchdog_subtask(pf);
11280 i40e_fdir_reinit_subtask(pf);
11281 if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11282 /* Client subtask will reopen next time through. */
11283 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11286 i40e_client_subtask(pf);
11287 if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11289 i40e_notify_client_of_l2_param_changes(
11290 pf->vsi[pf->lan_vsi]);
11292 i40e_sync_filters_subtask(pf);
11294 i40e_reset_subtask(pf);
11297 i40e_clean_adminq_subtask(pf);
11299 /* flush memory to make sure state is correct before next watchdog */
11300 smp_mb__before_atomic();
11301 clear_bit(__I40E_SERVICE_SCHED, pf->state);
11303 /* If the tasks have taken longer than one timer cycle or there
11304 * is more work to be done, reschedule the service task now
11305 * rather than wait for the timer to tick again.
11307 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11308 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
11309 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
11310 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11311 i40e_service_event_schedule(pf);
11315 * i40e_service_timer - timer callback
11316 * @t: timer list pointer
11318 static void i40e_service_timer(struct timer_list *t)
11320 struct i40e_pf *pf = from_timer(pf, t, service_timer);
11322 mod_timer(&pf->service_timer,
11323 round_jiffies(jiffies + pf->service_timer_period));
11324 i40e_service_event_schedule(pf);
11328 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11329 * @vsi: the VSI being configured
11331 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11333 struct i40e_pf *pf = vsi->back;
11335 switch (vsi->type) {
11336 case I40E_VSI_MAIN:
11337 vsi->alloc_queue_pairs = pf->num_lan_qps;
11338 if (!vsi->num_tx_desc)
11339 vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11340 I40E_REQ_DESCRIPTOR_MULTIPLE);
11341 if (!vsi->num_rx_desc)
11342 vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11343 I40E_REQ_DESCRIPTOR_MULTIPLE);
11344 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11345 vsi->num_q_vectors = pf->num_lan_msix;
11347 vsi->num_q_vectors = 1;
11351 case I40E_VSI_FDIR:
11352 vsi->alloc_queue_pairs = 1;
11353 vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11354 I40E_REQ_DESCRIPTOR_MULTIPLE);
11355 vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11356 I40E_REQ_DESCRIPTOR_MULTIPLE);
11357 vsi->num_q_vectors = pf->num_fdsb_msix;
11360 case I40E_VSI_VMDQ2:
11361 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11362 if (!vsi->num_tx_desc)
11363 vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11364 I40E_REQ_DESCRIPTOR_MULTIPLE);
11365 if (!vsi->num_rx_desc)
11366 vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11367 I40E_REQ_DESCRIPTOR_MULTIPLE);
11368 vsi->num_q_vectors = pf->num_vmdq_msix;
11371 case I40E_VSI_SRIOV:
11372 vsi->alloc_queue_pairs = pf->num_vf_qps;
11373 if (!vsi->num_tx_desc)
11374 vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11375 I40E_REQ_DESCRIPTOR_MULTIPLE);
11376 if (!vsi->num_rx_desc)
11377 vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11378 I40E_REQ_DESCRIPTOR_MULTIPLE);
11386 if (is_kdump_kernel()) {
11387 vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11388 vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11395 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11396 * @vsi: VSI pointer
11397 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11399 * On error: returns error code (negative)
11400 * On success: returns 0
11402 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11404 struct i40e_ring **next_rings;
11408 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11409 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11410 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11411 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11412 if (!vsi->tx_rings)
11414 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11415 if (i40e_enabled_xdp_vsi(vsi)) {
11416 vsi->xdp_rings = next_rings;
11417 next_rings += vsi->alloc_queue_pairs;
11419 vsi->rx_rings = next_rings;
11421 if (alloc_qvectors) {
11422 /* allocate memory for q_vector pointers */
11423 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11424 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11425 if (!vsi->q_vectors) {
11433 kfree(vsi->tx_rings);
11438 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11439 * @pf: board private structure
11440 * @type: type of VSI
11442 * On error: returns error code (negative)
11443 * On success: returns vsi index in PF (positive)
11445 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11448 struct i40e_vsi *vsi;
11452 /* Need to protect the allocation of the VSIs at the PF level */
11453 mutex_lock(&pf->switch_mutex);
11455 /* VSI list may be fragmented if VSI creation/destruction has
11456 * been happening. We can afford to do a quick scan to look
11457 * for any free VSIs in the list.
11459 * find next empty vsi slot, looping back around if necessary
11462 while (i < pf->num_alloc_vsi && pf->vsi[i])
11464 if (i >= pf->num_alloc_vsi) {
11466 while (i < pf->next_vsi && pf->vsi[i])
11470 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11471 vsi_idx = i; /* Found one! */
11474 goto unlock_pf; /* out of VSI slots! */
11476 pf->next_vsi = ++i;
11478 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11485 set_bit(__I40E_VSI_DOWN, vsi->state);
11487 vsi->idx = vsi_idx;
11488 vsi->int_rate_limit = 0;
11489 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11490 pf->rss_table_size : 64;
11491 vsi->netdev_registered = false;
11492 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11493 hash_init(vsi->mac_filter_hash);
11494 vsi->irqs_ready = false;
11496 if (type == I40E_VSI_MAIN) {
11497 vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11498 if (!vsi->af_xdp_zc_qps)
11502 ret = i40e_set_num_rings_in_vsi(vsi);
11506 ret = i40e_vsi_alloc_arrays(vsi, true);
11510 /* Setup default MSIX irq handler for VSI */
11511 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11513 /* Initialize VSI lock */
11514 spin_lock_init(&vsi->mac_filter_hash_lock);
11515 pf->vsi[vsi_idx] = vsi;
11520 bitmap_free(vsi->af_xdp_zc_qps);
11521 pf->next_vsi = i - 1;
11524 mutex_unlock(&pf->switch_mutex);
11529 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11530 * @vsi: VSI pointer
11531 * @free_qvectors: a bool to specify if q_vectors need to be freed.
11533 * On error: returns error code (negative)
11534 * On success: returns 0
11536 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11538 /* free the ring and vector containers */
11539 if (free_qvectors) {
11540 kfree(vsi->q_vectors);
11541 vsi->q_vectors = NULL;
11543 kfree(vsi->tx_rings);
11544 vsi->tx_rings = NULL;
11545 vsi->rx_rings = NULL;
11546 vsi->xdp_rings = NULL;
11550 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11552 * @vsi: Pointer to VSI structure
11554 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11559 kfree(vsi->rss_hkey_user);
11560 vsi->rss_hkey_user = NULL;
11562 kfree(vsi->rss_lut_user);
11563 vsi->rss_lut_user = NULL;
11567 * i40e_vsi_clear - Deallocate the VSI provided
11568 * @vsi: the VSI being un-configured
11570 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11572 struct i40e_pf *pf;
11581 mutex_lock(&pf->switch_mutex);
11582 if (!pf->vsi[vsi->idx]) {
11583 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11584 vsi->idx, vsi->idx, vsi->type);
11588 if (pf->vsi[vsi->idx] != vsi) {
11589 dev_err(&pf->pdev->dev,
11590 "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11591 pf->vsi[vsi->idx]->idx,
11592 pf->vsi[vsi->idx]->type,
11593 vsi->idx, vsi->type);
11597 /* updates the PF for this cleared vsi */
11598 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11599 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11601 bitmap_free(vsi->af_xdp_zc_qps);
11602 i40e_vsi_free_arrays(vsi, true);
11603 i40e_clear_rss_config_user(vsi);
11605 pf->vsi[vsi->idx] = NULL;
11606 if (vsi->idx < pf->next_vsi)
11607 pf->next_vsi = vsi->idx;
11610 mutex_unlock(&pf->switch_mutex);
11618 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11619 * @vsi: the VSI being cleaned
11621 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11625 if (vsi->tx_rings && vsi->tx_rings[0]) {
11626 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11627 kfree_rcu(vsi->tx_rings[i], rcu);
11628 WRITE_ONCE(vsi->tx_rings[i], NULL);
11629 WRITE_ONCE(vsi->rx_rings[i], NULL);
11630 if (vsi->xdp_rings)
11631 WRITE_ONCE(vsi->xdp_rings[i], NULL);
11637 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11638 * @vsi: the VSI being configured
11640 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11642 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11643 struct i40e_pf *pf = vsi->back;
11644 struct i40e_ring *ring;
11646 /* Set basic values in the rings to be used later during open() */
11647 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11648 /* allocate space for both Tx and Rx in one shot */
11649 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11653 ring->queue_index = i;
11654 ring->reg_idx = vsi->base_queue + i;
11655 ring->ring_active = false;
11657 ring->netdev = vsi->netdev;
11658 ring->dev = &pf->pdev->dev;
11659 ring->count = vsi->num_tx_desc;
11662 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11663 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11664 ring->itr_setting = pf->tx_itr_default;
11665 WRITE_ONCE(vsi->tx_rings[i], ring++);
11667 if (!i40e_enabled_xdp_vsi(vsi))
11670 ring->queue_index = vsi->alloc_queue_pairs + i;
11671 ring->reg_idx = vsi->base_queue + ring->queue_index;
11672 ring->ring_active = false;
11674 ring->netdev = NULL;
11675 ring->dev = &pf->pdev->dev;
11676 ring->count = vsi->num_tx_desc;
11679 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11680 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11681 set_ring_xdp(ring);
11682 ring->itr_setting = pf->tx_itr_default;
11683 WRITE_ONCE(vsi->xdp_rings[i], ring++);
11686 ring->queue_index = i;
11687 ring->reg_idx = vsi->base_queue + i;
11688 ring->ring_active = false;
11690 ring->netdev = vsi->netdev;
11691 ring->dev = &pf->pdev->dev;
11692 ring->count = vsi->num_rx_desc;
11695 ring->itr_setting = pf->rx_itr_default;
11696 WRITE_ONCE(vsi->rx_rings[i], ring);
11702 i40e_vsi_clear_rings(vsi);
11707 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11708 * @pf: board private structure
11709 * @vectors: the number of MSI-X vectors to request
11711 * Returns the number of vectors reserved, or error
11713 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11715 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11716 I40E_MIN_MSIX, vectors);
11718 dev_info(&pf->pdev->dev,
11719 "MSI-X vector reservation failed: %d\n", vectors);
11727 * i40e_init_msix - Setup the MSIX capability
11728 * @pf: board private structure
11730 * Work with the OS to set up the MSIX vectors needed.
11732 * Returns the number of vectors reserved or negative on failure
11734 static int i40e_init_msix(struct i40e_pf *pf)
11736 struct i40e_hw *hw = &pf->hw;
11737 int cpus, extra_vectors;
11741 int iwarp_requested = 0;
11743 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11746 /* The number of vectors we'll request will be comprised of:
11747 * - Add 1 for "other" cause for Admin Queue events, etc.
11748 * - The number of LAN queue pairs
11749 * - Queues being used for RSS.
11750 * We don't need as many as max_rss_size vectors.
11751 * use rss_size instead in the calculation since that
11752 * is governed by number of cpus in the system.
11753 * - assumes symmetric Tx/Rx pairing
11754 * - The number of VMDq pairs
11755 * - The CPU count within the NUMA node if iWARP is enabled
11756 * Once we count this up, try the request.
11758 * If we can't get what we want, we'll simplify to nearly nothing
11759 * and try again. If that still fails, we punt.
11761 vectors_left = hw->func_caps.num_msix_vectors;
11764 /* reserve one vector for miscellaneous handler */
11765 if (vectors_left) {
11770 /* reserve some vectors for the main PF traffic queues. Initially we
11771 * only reserve at most 50% of the available vectors, in the case that
11772 * the number of online CPUs is large. This ensures that we can enable
11773 * extra features as well. Once we've enabled the other features, we
11774 * will use any remaining vectors to reach as close as we can to the
11775 * number of online CPUs.
11777 cpus = num_online_cpus();
11778 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11779 vectors_left -= pf->num_lan_msix;
11781 /* reserve one vector for sideband flow director */
11782 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11783 if (vectors_left) {
11784 pf->num_fdsb_msix = 1;
11788 pf->num_fdsb_msix = 0;
11792 /* can we reserve enough for iWARP? */
11793 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11794 iwarp_requested = pf->num_iwarp_msix;
11797 pf->num_iwarp_msix = 0;
11798 else if (vectors_left < pf->num_iwarp_msix)
11799 pf->num_iwarp_msix = 1;
11800 v_budget += pf->num_iwarp_msix;
11801 vectors_left -= pf->num_iwarp_msix;
11804 /* any vectors left over go for VMDq support */
11805 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11806 if (!vectors_left) {
11807 pf->num_vmdq_msix = 0;
11808 pf->num_vmdq_qps = 0;
11810 int vmdq_vecs_wanted =
11811 pf->num_vmdq_vsis * pf->num_vmdq_qps;
11813 min_t(int, vectors_left, vmdq_vecs_wanted);
11815 /* if we're short on vectors for what's desired, we limit
11816 * the queues per vmdq. If this is still more than are
11817 * available, the user will need to change the number of
11818 * queues/vectors used by the PF later with the ethtool
11821 if (vectors_left < vmdq_vecs_wanted) {
11822 pf->num_vmdq_qps = 1;
11823 vmdq_vecs_wanted = pf->num_vmdq_vsis;
11824 vmdq_vecs = min_t(int,
11828 pf->num_vmdq_msix = pf->num_vmdq_qps;
11830 v_budget += vmdq_vecs;
11831 vectors_left -= vmdq_vecs;
11835 /* On systems with a large number of SMP cores, we previously limited
11836 * the number of vectors for num_lan_msix to be at most 50% of the
11837 * available vectors, to allow for other features. Now, we add back
11838 * the remaining vectors. However, we ensure that the total
11839 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11840 * calculate the number of vectors we can add without going over the
11841 * cap of CPUs. For systems with a small number of CPUs this will be
11844 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11845 pf->num_lan_msix += extra_vectors;
11846 vectors_left -= extra_vectors;
11848 WARN(vectors_left < 0,
11849 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11851 v_budget += pf->num_lan_msix;
11852 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11854 if (!pf->msix_entries)
11857 for (i = 0; i < v_budget; i++)
11858 pf->msix_entries[i].entry = i;
11859 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11861 if (v_actual < I40E_MIN_MSIX) {
11862 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11863 kfree(pf->msix_entries);
11864 pf->msix_entries = NULL;
11865 pci_disable_msix(pf->pdev);
11868 } else if (v_actual == I40E_MIN_MSIX) {
11869 /* Adjust for minimal MSIX use */
11870 pf->num_vmdq_vsis = 0;
11871 pf->num_vmdq_qps = 0;
11872 pf->num_lan_qps = 1;
11873 pf->num_lan_msix = 1;
11875 } else if (v_actual != v_budget) {
11876 /* If we have limited resources, we will start with no vectors
11877 * for the special features and then allocate vectors to some
11878 * of these features based on the policy and at the end disable
11879 * the features that did not get any vectors.
11883 dev_info(&pf->pdev->dev,
11884 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11885 v_actual, v_budget);
11886 /* reserve the misc vector */
11887 vec = v_actual - 1;
11889 /* Scale vector usage down */
11890 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
11891 pf->num_vmdq_vsis = 1;
11892 pf->num_vmdq_qps = 1;
11894 /* partition out the remaining vectors */
11897 pf->num_lan_msix = 1;
11900 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11901 pf->num_lan_msix = 1;
11902 pf->num_iwarp_msix = 1;
11904 pf->num_lan_msix = 2;
11908 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11909 pf->num_iwarp_msix = min_t(int, (vec / 3),
11911 pf->num_vmdq_vsis = min_t(int, (vec / 3),
11912 I40E_DEFAULT_NUM_VMDQ_VSI);
11914 pf->num_vmdq_vsis = min_t(int, (vec / 2),
11915 I40E_DEFAULT_NUM_VMDQ_VSI);
11917 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11918 pf->num_fdsb_msix = 1;
11921 pf->num_lan_msix = min_t(int,
11922 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11924 pf->num_lan_qps = pf->num_lan_msix;
11929 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11930 (pf->num_fdsb_msix == 0)) {
11931 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11932 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11933 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11935 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11936 (pf->num_vmdq_msix == 0)) {
11937 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11938 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11941 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11942 (pf->num_iwarp_msix == 0)) {
11943 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11944 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11946 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11947 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11949 pf->num_vmdq_msix * pf->num_vmdq_vsis,
11951 pf->num_iwarp_msix);
11957 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11958 * @vsi: the VSI being configured
11959 * @v_idx: index of the vector in the vsi struct
11961 * We allocate one q_vector. If allocation fails we return -ENOMEM.
11963 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11965 struct i40e_q_vector *q_vector;
11967 /* allocate q_vector */
11968 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11972 q_vector->vsi = vsi;
11973 q_vector->v_idx = v_idx;
11974 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11977 netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11979 /* tie q_vector and vsi together */
11980 vsi->q_vectors[v_idx] = q_vector;
11986 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11987 * @vsi: the VSI being configured
11989 * We allocate one q_vector per queue interrupt. If allocation fails we
11992 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11994 struct i40e_pf *pf = vsi->back;
11995 int err, v_idx, num_q_vectors;
11997 /* if not MSIX, give the one vector only to the LAN VSI */
11998 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11999 num_q_vectors = vsi->num_q_vectors;
12000 else if (vsi == pf->vsi[pf->lan_vsi])
12005 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12006 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12015 i40e_free_q_vector(vsi, v_idx);
12021 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12022 * @pf: board private structure to initialize
12024 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12029 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12030 vectors = i40e_init_msix(pf);
12032 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
12033 I40E_FLAG_IWARP_ENABLED |
12034 I40E_FLAG_RSS_ENABLED |
12035 I40E_FLAG_DCB_CAPABLE |
12036 I40E_FLAG_DCB_ENABLED |
12037 I40E_FLAG_SRIOV_ENABLED |
12038 I40E_FLAG_FD_SB_ENABLED |
12039 I40E_FLAG_FD_ATR_ENABLED |
12040 I40E_FLAG_VMDQ_ENABLED);
12041 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12043 /* rework the queue expectations without MSIX */
12044 i40e_determine_queue_usage(pf);
12048 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12049 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12050 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12051 vectors = pci_enable_msi(pf->pdev);
12053 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12055 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12057 vectors = 1; /* one MSI or Legacy vector */
12060 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12061 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12063 /* set up vector assignment tracking */
12064 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12065 pf->irq_pile = kzalloc(size, GFP_KERNEL);
12069 pf->irq_pile->num_entries = vectors;
12071 /* track first vector for misc interrupts, ignore return */
12072 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12078 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12079 * @pf: private board data structure
12081 * Restore the interrupt scheme that was cleared when we suspended the
12082 * device. This should be called during resume to re-allocate the q_vectors
12083 * and reacquire IRQs.
12085 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12089 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
12090 * scheme. We need to re-enabled them here in order to attempt to
12091 * re-acquire the MSI or MSI-X vectors
12093 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12095 err = i40e_init_interrupt_scheme(pf);
12099 /* Now that we've re-acquired IRQs, we need to remap the vectors and
12100 * rings together again.
12102 for (i = 0; i < pf->num_alloc_vsi; i++) {
12104 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12107 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12111 err = i40e_setup_misc_vector(pf);
12115 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12116 i40e_client_update_msix_info(pf);
12123 i40e_vsi_free_q_vectors(pf->vsi[i]);
12130 * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12131 * non queue events in recovery mode
12132 * @pf: board private structure
12134 * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12135 * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12136 * This is handled differently than in recovery mode since no Tx/Rx resources
12137 * are being allocated.
12139 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12143 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12144 err = i40e_setup_misc_vector(pf);
12147 dev_info(&pf->pdev->dev,
12148 "MSI-X misc vector request failed, error %d\n",
12153 u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12155 err = request_irq(pf->pdev->irq, i40e_intr, flags,
12159 dev_info(&pf->pdev->dev,
12160 "MSI/legacy misc vector request failed, error %d\n",
12164 i40e_enable_misc_int_causes(pf);
12165 i40e_irq_dynamic_enable_icr0(pf);
12172 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12173 * @pf: board private structure
12175 * This sets up the handler for MSIX 0, which is used to manage the
12176 * non-queue interrupts, e.g. AdminQ and errors. This is not used
12177 * when in MSI or Legacy interrupt mode.
12179 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12181 struct i40e_hw *hw = &pf->hw;
12184 /* Only request the IRQ once, the first time through. */
12185 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12186 err = request_irq(pf->msix_entries[0].vector,
12187 i40e_intr, 0, pf->int_name, pf);
12189 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12190 dev_info(&pf->pdev->dev,
12191 "request_irq for %s failed: %d\n",
12192 pf->int_name, err);
12197 i40e_enable_misc_int_causes(pf);
12199 /* associate no queues to the misc vector */
12200 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12201 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12205 i40e_irq_dynamic_enable_icr0(pf);
12211 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12212 * @vsi: Pointer to vsi structure
12213 * @seed: Buffter to store the hash keys
12214 * @lut: Buffer to store the lookup table entries
12215 * @lut_size: Size of buffer to store the lookup table entries
12217 * Return 0 on success, negative on failure
12219 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12220 u8 *lut, u16 lut_size)
12222 struct i40e_pf *pf = vsi->back;
12223 struct i40e_hw *hw = &pf->hw;
12227 ret = i40e_aq_get_rss_key(hw, vsi->id,
12228 (struct i40e_aqc_get_set_rss_key_data *)seed);
12230 dev_info(&pf->pdev->dev,
12231 "Cannot get RSS key, err %pe aq_err %s\n",
12233 i40e_aq_str(&pf->hw,
12234 pf->hw.aq.asq_last_status));
12240 bool pf_lut = vsi->type == I40E_VSI_MAIN;
12242 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12244 dev_info(&pf->pdev->dev,
12245 "Cannot get RSS lut, err %pe aq_err %s\n",
12247 i40e_aq_str(&pf->hw,
12248 pf->hw.aq.asq_last_status));
12257 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12258 * @vsi: Pointer to vsi structure
12259 * @seed: RSS hash seed
12260 * @lut: Lookup table
12261 * @lut_size: Lookup table size
12263 * Returns 0 on success, negative on failure
12265 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12266 const u8 *lut, u16 lut_size)
12268 struct i40e_pf *pf = vsi->back;
12269 struct i40e_hw *hw = &pf->hw;
12270 u16 vf_id = vsi->vf_id;
12273 /* Fill out hash function seed */
12275 u32 *seed_dw = (u32 *)seed;
12277 if (vsi->type == I40E_VSI_MAIN) {
12278 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12279 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12280 } else if (vsi->type == I40E_VSI_SRIOV) {
12281 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12282 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12284 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12289 u32 *lut_dw = (u32 *)lut;
12291 if (vsi->type == I40E_VSI_MAIN) {
12292 if (lut_size != I40E_HLUT_ARRAY_SIZE)
12294 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12295 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12296 } else if (vsi->type == I40E_VSI_SRIOV) {
12297 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12299 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12300 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12302 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12311 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12312 * @vsi: Pointer to VSI structure
12313 * @seed: Buffer to store the keys
12314 * @lut: Buffer to store the lookup table entries
12315 * @lut_size: Size of buffer to store the lookup table entries
12317 * Returns 0 on success, negative on failure
12319 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12320 u8 *lut, u16 lut_size)
12322 struct i40e_pf *pf = vsi->back;
12323 struct i40e_hw *hw = &pf->hw;
12327 u32 *seed_dw = (u32 *)seed;
12329 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12330 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12333 u32 *lut_dw = (u32 *)lut;
12335 if (lut_size != I40E_HLUT_ARRAY_SIZE)
12337 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12338 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12345 * i40e_config_rss - Configure RSS keys and lut
12346 * @vsi: Pointer to VSI structure
12347 * @seed: RSS hash seed
12348 * @lut: Lookup table
12349 * @lut_size: Lookup table size
12351 * Returns 0 on success, negative on failure
12353 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12355 struct i40e_pf *pf = vsi->back;
12357 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12358 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12360 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12364 * i40e_get_rss - Get RSS keys and lut
12365 * @vsi: Pointer to VSI structure
12366 * @seed: Buffer to store the keys
12367 * @lut: Buffer to store the lookup table entries
12368 * @lut_size: Size of buffer to store the lookup table entries
12370 * Returns 0 on success, negative on failure
12372 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12374 struct i40e_pf *pf = vsi->back;
12376 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12377 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12379 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12383 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12384 * @pf: Pointer to board private structure
12385 * @lut: Lookup table
12386 * @rss_table_size: Lookup table size
12387 * @rss_size: Range of queue number for hashing
12389 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12390 u16 rss_table_size, u16 rss_size)
12394 for (i = 0; i < rss_table_size; i++)
12395 lut[i] = i % rss_size;
12399 * i40e_pf_config_rss - Prepare for RSS if used
12400 * @pf: board private structure
12402 static int i40e_pf_config_rss(struct i40e_pf *pf)
12404 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12405 u8 seed[I40E_HKEY_ARRAY_SIZE];
12407 struct i40e_hw *hw = &pf->hw;
12412 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12413 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12414 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12415 hena |= i40e_pf_get_default_rss_hena(pf);
12417 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12418 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12420 /* Determine the RSS table size based on the hardware capabilities */
12421 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12422 reg_val = (pf->rss_table_size == 512) ?
12423 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12424 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12425 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12427 /* Determine the RSS size of the VSI */
12428 if (!vsi->rss_size) {
12430 /* If the firmware does something weird during VSI init, we
12431 * could end up with zero TCs. Check for that to avoid
12432 * divide-by-zero. It probably won't pass traffic, but it also
12435 qcount = vsi->num_queue_pairs /
12436 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12437 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12439 if (!vsi->rss_size)
12442 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12446 /* Use user configured lut if there is one, otherwise use default */
12447 if (vsi->rss_lut_user)
12448 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12450 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12452 /* Use user configured hash key if there is one, otherwise
12455 if (vsi->rss_hkey_user)
12456 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12458 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12459 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12466 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12467 * @pf: board private structure
12468 * @queue_count: the requested queue count for rss.
12470 * returns 0 if rss is not enabled, if enabled returns the final rss queue
12471 * count which may be different from the requested queue count.
12472 * Note: expects to be called while under rtnl_lock()
12474 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12476 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12479 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12482 queue_count = min_t(int, queue_count, num_online_cpus());
12483 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12485 if (queue_count != vsi->num_queue_pairs) {
12488 vsi->req_queue_pairs = queue_count;
12489 i40e_prep_for_reset(pf);
12490 if (test_bit(__I40E_IN_REMOVE, pf->state))
12491 return pf->alloc_rss_size;
12493 pf->alloc_rss_size = new_rss_size;
12495 i40e_reset_and_rebuild(pf, true, true);
12497 /* Discard the user configured hash keys and lut, if less
12498 * queues are enabled.
12500 if (queue_count < vsi->rss_size) {
12501 i40e_clear_rss_config_user(vsi);
12502 dev_dbg(&pf->pdev->dev,
12503 "discard user configured hash keys and lut\n");
12506 /* Reset vsi->rss_size, as number of enabled queues changed */
12507 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12508 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12510 i40e_pf_config_rss(pf);
12512 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
12513 vsi->req_queue_pairs, pf->rss_size_max);
12514 return pf->alloc_rss_size;
12518 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12519 * @pf: board private structure
12521 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12523 bool min_valid, max_valid;
12524 u32 max_bw, min_bw;
12527 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12528 &min_valid, &max_valid);
12532 pf->min_bw = min_bw;
12534 pf->max_bw = max_bw;
12541 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12542 * @pf: board private structure
12544 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12546 struct i40e_aqc_configure_partition_bw_data bw_data;
12549 memset(&bw_data, 0, sizeof(bw_data));
12551 /* Set the valid bit for this PF */
12552 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12553 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12554 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12556 /* Set the new bandwidths */
12557 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12563 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12564 * @pf: board private structure
12566 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12568 /* Commit temporary BW setting to permanent NVM image */
12569 enum i40e_admin_queue_err last_aq_status;
12573 if (pf->hw.partition_id != 1) {
12574 dev_info(&pf->pdev->dev,
12575 "Commit BW only works on partition 1! This is partition %d",
12576 pf->hw.partition_id);
12577 ret = I40E_NOT_SUPPORTED;
12578 goto bw_commit_out;
12581 /* Acquire NVM for read access */
12582 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12583 last_aq_status = pf->hw.aq.asq_last_status;
12585 dev_info(&pf->pdev->dev,
12586 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12588 i40e_aq_str(&pf->hw, last_aq_status));
12589 goto bw_commit_out;
12592 /* Read word 0x10 of NVM - SW compatibility word 1 */
12593 ret = i40e_aq_read_nvm(&pf->hw,
12594 I40E_SR_NVM_CONTROL_WORD,
12595 0x10, sizeof(nvm_word), &nvm_word,
12597 /* Save off last admin queue command status before releasing
12600 last_aq_status = pf->hw.aq.asq_last_status;
12601 i40e_release_nvm(&pf->hw);
12603 dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12605 i40e_aq_str(&pf->hw, last_aq_status));
12606 goto bw_commit_out;
12609 /* Wait a bit for NVM release to complete */
12612 /* Acquire NVM for write access */
12613 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12614 last_aq_status = pf->hw.aq.asq_last_status;
12616 dev_info(&pf->pdev->dev,
12617 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12619 i40e_aq_str(&pf->hw, last_aq_status));
12620 goto bw_commit_out;
12622 /* Write it back out unchanged to initiate update NVM,
12623 * which will force a write of the shadow (alt) RAM to
12624 * the NVM - thus storing the bandwidth values permanently.
12626 ret = i40e_aq_update_nvm(&pf->hw,
12627 I40E_SR_NVM_CONTROL_WORD,
12628 0x10, sizeof(nvm_word),
12629 &nvm_word, true, 0, NULL);
12630 /* Save off last admin queue command status before releasing
12633 last_aq_status = pf->hw.aq.asq_last_status;
12634 i40e_release_nvm(&pf->hw);
12636 dev_info(&pf->pdev->dev,
12637 "BW settings NOT SAVED, err %pe aq_err %s\n",
12639 i40e_aq_str(&pf->hw, last_aq_status));
12646 * i40e_is_total_port_shutdown_enabled - read NVM and return value
12647 * if total port shutdown feature is enabled for this PF
12648 * @pf: board private structure
12650 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12652 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED BIT(4)
12653 #define I40E_FEATURES_ENABLE_PTR 0x2A
12654 #define I40E_CURRENT_SETTING_PTR 0x2B
12655 #define I40E_LINK_BEHAVIOR_WORD_OFFSET 0x2D
12656 #define I40E_LINK_BEHAVIOR_WORD_LENGTH 0x1
12657 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED BIT(0)
12658 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH 4
12659 int read_status = I40E_SUCCESS;
12660 u16 sr_emp_sr_settings_ptr = 0;
12661 u16 features_enable = 0;
12662 u16 link_behavior = 0;
12665 read_status = i40e_read_nvm_word(&pf->hw,
12666 I40E_SR_EMP_SR_SETTINGS_PTR,
12667 &sr_emp_sr_settings_ptr);
12670 read_status = i40e_read_nvm_word(&pf->hw,
12671 sr_emp_sr_settings_ptr +
12672 I40E_FEATURES_ENABLE_PTR,
12676 if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12677 read_status = i40e_read_nvm_module_data(&pf->hw,
12678 I40E_SR_EMP_SR_SETTINGS_PTR,
12679 I40E_CURRENT_SETTING_PTR,
12680 I40E_LINK_BEHAVIOR_WORD_OFFSET,
12681 I40E_LINK_BEHAVIOR_WORD_LENGTH,
12685 link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12686 ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12691 dev_warn(&pf->pdev->dev,
12692 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12693 ERR_PTR(read_status));
12698 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12699 * @pf: board private structure to initialize
12701 * i40e_sw_init initializes the Adapter private data structure.
12702 * Fields are initialized based on PCI device information and
12703 * OS network device settings (MTU size).
12705 static int i40e_sw_init(struct i40e_pf *pf)
12711 /* Set default capability flags */
12712 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12713 I40E_FLAG_MSI_ENABLED |
12714 I40E_FLAG_MSIX_ENABLED;
12716 /* Set default ITR */
12717 pf->rx_itr_default = I40E_ITR_RX_DEF;
12718 pf->tx_itr_default = I40E_ITR_TX_DEF;
12720 /* Depending on PF configurations, it is possible that the RSS
12721 * maximum might end up larger than the available queues
12723 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12724 pf->alloc_rss_size = 1;
12725 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12726 pf->rss_size_max = min_t(int, pf->rss_size_max,
12727 pf->hw.func_caps.num_tx_qp);
12729 /* find the next higher power-of-2 of num cpus */
12730 pow = roundup_pow_of_two(num_online_cpus());
12731 pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12733 if (pf->hw.func_caps.rss) {
12734 pf->flags |= I40E_FLAG_RSS_ENABLED;
12735 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12736 num_online_cpus());
12739 /* MFP mode enabled */
12740 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12741 pf->flags |= I40E_FLAG_MFP_ENABLED;
12742 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12743 if (i40e_get_partition_bw_setting(pf)) {
12744 dev_warn(&pf->pdev->dev,
12745 "Could not get partition bw settings\n");
12747 dev_info(&pf->pdev->dev,
12748 "Partition BW Min = %8.8x, Max = %8.8x\n",
12749 pf->min_bw, pf->max_bw);
12751 /* nudge the Tx scheduler */
12752 i40e_set_partition_bw_setting(pf);
12756 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12757 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12758 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12759 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12760 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12761 pf->hw.num_partitions > 1)
12762 dev_info(&pf->pdev->dev,
12763 "Flow Director Sideband mode Disabled in MFP mode\n");
12765 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12766 pf->fdir_pf_filter_count =
12767 pf->hw.func_caps.fd_filters_guaranteed;
12768 pf->hw.fdir_shared_filter_count =
12769 pf->hw.func_caps.fd_filters_best_effort;
12772 if (pf->hw.mac.type == I40E_MAC_X722) {
12773 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12774 I40E_HW_128_QP_RSS_CAPABLE |
12775 I40E_HW_ATR_EVICT_CAPABLE |
12776 I40E_HW_WB_ON_ITR_CAPABLE |
12777 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12778 I40E_HW_NO_PCI_LINK_CHECK |
12779 I40E_HW_USE_SET_LLDP_MIB |
12780 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12781 I40E_HW_PTP_L4_CAPABLE |
12782 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12783 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12785 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12786 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12787 I40E_FDEVICT_PCTYPE_DEFAULT) {
12788 dev_warn(&pf->pdev->dev,
12789 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12790 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12792 } else if ((pf->hw.aq.api_maj_ver > 1) ||
12793 ((pf->hw.aq.api_maj_ver == 1) &&
12794 (pf->hw.aq.api_min_ver > 4))) {
12795 /* Supported in FW API version higher than 1.4 */
12796 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12799 /* Enable HW ATR eviction if possible */
12800 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12801 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12803 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12804 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12805 (pf->hw.aq.fw_maj_ver < 4))) {
12806 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12807 /* No DCB support for FW < v4.33 */
12808 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12811 /* Disable FW LLDP if FW < v4.3 */
12812 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12813 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12814 (pf->hw.aq.fw_maj_ver < 4)))
12815 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12817 /* Use the FW Set LLDP MIB API if FW > v4.40 */
12818 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12819 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12820 (pf->hw.aq.fw_maj_ver >= 5)))
12821 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12823 /* Enable PTP L4 if FW > v6.0 */
12824 if (pf->hw.mac.type == I40E_MAC_XL710 &&
12825 pf->hw.aq.fw_maj_ver >= 6)
12826 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12828 if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12829 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12830 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12831 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12834 if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12835 pf->flags |= I40E_FLAG_IWARP_ENABLED;
12836 /* IWARP needs one extra vector for CQP just like MISC.*/
12837 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12839 /* Stopping FW LLDP engine is supported on XL710 and X722
12840 * starting from FW versions determined in i40e_init_adminq.
12841 * Stopping the FW LLDP engine is not supported on XL710
12842 * if NPAR is functioning so unset this hw flag in this case.
12844 if (pf->hw.mac.type == I40E_MAC_XL710 &&
12845 pf->hw.func_caps.npar_enable &&
12846 (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12847 pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12849 #ifdef CONFIG_PCI_IOV
12850 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12851 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12852 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12853 pf->num_req_vfs = min_t(int,
12854 pf->hw.func_caps.num_vfs,
12855 I40E_MAX_VF_COUNT);
12857 #endif /* CONFIG_PCI_IOV */
12858 pf->eeprom_version = 0xDEAD;
12859 pf->lan_veb = I40E_NO_VEB;
12860 pf->lan_vsi = I40E_NO_VSI;
12862 /* By default FW has this off for performance reasons */
12863 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12865 /* set up queue assignment tracking */
12866 size = sizeof(struct i40e_lump_tracking)
12867 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12868 pf->qp_pile = kzalloc(size, GFP_KERNEL);
12869 if (!pf->qp_pile) {
12873 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12875 pf->tx_timeout_recovery_level = 1;
12877 if (pf->hw.mac.type != I40E_MAC_X722 &&
12878 i40e_is_total_port_shutdown_enabled(pf)) {
12879 /* Link down on close must be on when total port shutdown
12880 * is enabled for a given port
12882 pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12883 I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12884 dev_info(&pf->pdev->dev,
12885 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12887 mutex_init(&pf->switch_mutex);
12894 * i40e_set_ntuple - set the ntuple feature flag and take action
12895 * @pf: board private structure to initialize
12896 * @features: the feature set that the stack is suggesting
12898 * returns a bool to indicate if reset needs to happen
12900 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12902 bool need_reset = false;
12904 /* Check if Flow Director n-tuple support was enabled or disabled. If
12905 * the state changed, we need to reset.
12907 if (features & NETIF_F_NTUPLE) {
12908 /* Enable filters and mark for reset */
12909 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12911 /* enable FD_SB only if there is MSI-X vector and no cloud
12914 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12915 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12916 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12919 /* turn off filters, mark for reset and clear SW filter list */
12920 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12922 i40e_fdir_filter_exit(pf);
12924 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12925 clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12926 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12928 /* reset fd counters */
12929 pf->fd_add_err = 0;
12930 pf->fd_atr_cnt = 0;
12931 /* if ATR was auto disabled it can be re-enabled. */
12932 if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12933 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12934 (I40E_DEBUG_FD & pf->hw.debug_mask))
12935 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12941 * i40e_clear_rss_lut - clear the rx hash lookup table
12942 * @vsi: the VSI being configured
12944 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12946 struct i40e_pf *pf = vsi->back;
12947 struct i40e_hw *hw = &pf->hw;
12948 u16 vf_id = vsi->vf_id;
12951 if (vsi->type == I40E_VSI_MAIN) {
12952 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12953 wr32(hw, I40E_PFQF_HLUT(i), 0);
12954 } else if (vsi->type == I40E_VSI_SRIOV) {
12955 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12956 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12958 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12963 * i40e_set_loopback - turn on/off loopback mode on underlying PF
12965 * @ena: flag to indicate the on/off setting
12967 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12969 bool if_running = netif_running(vsi->netdev) &&
12970 !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12976 ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12978 netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12986 * i40e_set_features - set the netdev feature flags
12987 * @netdev: ptr to the netdev being adjusted
12988 * @features: the feature set that the stack is suggesting
12989 * Note: expects to be called while under rtnl_lock()
12991 static int i40e_set_features(struct net_device *netdev,
12992 netdev_features_t features)
12994 struct i40e_netdev_priv *np = netdev_priv(netdev);
12995 struct i40e_vsi *vsi = np->vsi;
12996 struct i40e_pf *pf = vsi->back;
12999 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13000 i40e_pf_config_rss(pf);
13001 else if (!(features & NETIF_F_RXHASH) &&
13002 netdev->features & NETIF_F_RXHASH)
13003 i40e_clear_rss_lut(vsi);
13005 if (features & NETIF_F_HW_VLAN_CTAG_RX)
13006 i40e_vlan_stripping_enable(vsi);
13008 i40e_vlan_stripping_disable(vsi);
13010 if (!(features & NETIF_F_HW_TC) &&
13011 (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13012 dev_err(&pf->pdev->dev,
13013 "Offloaded tc filters active, can't turn hw_tc_offload off");
13017 if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13018 i40e_del_all_macvlans(vsi);
13020 need_reset = i40e_set_ntuple(pf, features);
13023 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13025 if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13026 return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13031 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13032 unsigned int table, unsigned int idx,
13033 struct udp_tunnel_info *ti)
13035 struct i40e_netdev_priv *np = netdev_priv(netdev);
13036 struct i40e_hw *hw = &np->vsi->back->hw;
13037 u8 type, filter_index;
13040 type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13041 I40E_AQC_TUNNEL_TYPE_NGE;
13043 ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13046 netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13048 i40e_aq_str(hw, hw->aq.asq_last_status));
13052 udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13056 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13057 unsigned int table, unsigned int idx,
13058 struct udp_tunnel_info *ti)
13060 struct i40e_netdev_priv *np = netdev_priv(netdev);
13061 struct i40e_hw *hw = &np->vsi->back->hw;
13064 ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13066 netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13068 i40e_aq_str(hw, hw->aq.asq_last_status));
13075 static int i40e_get_phys_port_id(struct net_device *netdev,
13076 struct netdev_phys_item_id *ppid)
13078 struct i40e_netdev_priv *np = netdev_priv(netdev);
13079 struct i40e_pf *pf = np->vsi->back;
13080 struct i40e_hw *hw = &pf->hw;
13082 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13083 return -EOPNOTSUPP;
13085 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13086 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13092 * i40e_ndo_fdb_add - add an entry to the hardware database
13093 * @ndm: the input from the stack
13094 * @tb: pointer to array of nladdr (unused)
13095 * @dev: the net device pointer
13096 * @addr: the MAC address entry being added
13098 * @flags: instructions from stack about fdb operation
13099 * @extack: netlink extended ack, unused currently
13101 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13102 struct net_device *dev,
13103 const unsigned char *addr, u16 vid,
13105 struct netlink_ext_ack *extack)
13107 struct i40e_netdev_priv *np = netdev_priv(dev);
13108 struct i40e_pf *pf = np->vsi->back;
13111 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13112 return -EOPNOTSUPP;
13115 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13119 /* Hardware does not support aging addresses so if a
13120 * ndm_state is given only allow permanent addresses
13122 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13123 netdev_info(dev, "FDB only supports static addresses\n");
13127 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13128 err = dev_uc_add_excl(dev, addr);
13129 else if (is_multicast_ether_addr(addr))
13130 err = dev_mc_add_excl(dev, addr);
13134 /* Only return duplicate errors if NLM_F_EXCL is set */
13135 if (err == -EEXIST && !(flags & NLM_F_EXCL))
13142 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13143 * @dev: the netdev being configured
13144 * @nlh: RTNL message
13145 * @flags: bridge flags
13146 * @extack: netlink extended ack
13148 * Inserts a new hardware bridge if not already created and
13149 * enables the bridging mode requested (VEB or VEPA). If the
13150 * hardware bridge has already been inserted and the request
13151 * is to change the mode then that requires a PF reset to
13152 * allow rebuild of the components with required hardware
13153 * bridge mode enabled.
13155 * Note: expects to be called while under rtnl_lock()
13157 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13158 struct nlmsghdr *nlh,
13160 struct netlink_ext_ack *extack)
13162 struct i40e_netdev_priv *np = netdev_priv(dev);
13163 struct i40e_vsi *vsi = np->vsi;
13164 struct i40e_pf *pf = vsi->back;
13165 struct i40e_veb *veb = NULL;
13166 struct nlattr *attr, *br_spec;
13169 /* Only for PF VSI for now */
13170 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13171 return -EOPNOTSUPP;
13173 /* Find the HW bridge for PF VSI */
13174 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13175 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13179 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13183 nla_for_each_nested(attr, br_spec, rem) {
13186 if (nla_type(attr) != IFLA_BRIDGE_MODE)
13189 mode = nla_get_u16(attr);
13190 if ((mode != BRIDGE_MODE_VEPA) &&
13191 (mode != BRIDGE_MODE_VEB))
13194 /* Insert a new HW bridge */
13196 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13197 vsi->tc_config.enabled_tc);
13199 veb->bridge_mode = mode;
13200 i40e_config_bridge_mode(veb);
13202 /* No Bridge HW offload available */
13206 } else if (mode != veb->bridge_mode) {
13207 /* Existing HW bridge but different mode needs reset */
13208 veb->bridge_mode = mode;
13209 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13210 if (mode == BRIDGE_MODE_VEB)
13211 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13213 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13214 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13223 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13226 * @seq: RTNL message seq #
13227 * @dev: the netdev being configured
13228 * @filter_mask: unused
13229 * @nlflags: netlink flags passed in
13231 * Return the mode in which the hardware bridge is operating in
13234 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13235 struct net_device *dev,
13236 u32 __always_unused filter_mask,
13239 struct i40e_netdev_priv *np = netdev_priv(dev);
13240 struct i40e_vsi *vsi = np->vsi;
13241 struct i40e_pf *pf = vsi->back;
13242 struct i40e_veb *veb = NULL;
13245 /* Only for PF VSI for now */
13246 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13247 return -EOPNOTSUPP;
13249 /* Find the HW bridge for the PF VSI */
13250 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13251 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13258 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13259 0, 0, nlflags, filter_mask, NULL);
13263 * i40e_features_check - Validate encapsulated packet conforms to limits
13265 * @dev: This physical port's netdev
13266 * @features: Offload features that the stack believes apply
13268 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13269 struct net_device *dev,
13270 netdev_features_t features)
13274 /* No point in doing any of this if neither checksum nor GSO are
13275 * being requested for this frame. We can rule out both by just
13276 * checking for CHECKSUM_PARTIAL
13278 if (skb->ip_summed != CHECKSUM_PARTIAL)
13281 /* We cannot support GSO if the MSS is going to be less than
13282 * 64 bytes. If it is then we need to drop support for GSO.
13284 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13285 features &= ~NETIF_F_GSO_MASK;
13287 /* MACLEN can support at most 63 words */
13288 len = skb_network_header(skb) - skb->data;
13289 if (len & ~(63 * 2))
13292 /* IPLEN and EIPLEN can support at most 127 dwords */
13293 len = skb_transport_header(skb) - skb_network_header(skb);
13294 if (len & ~(127 * 4))
13297 if (skb->encapsulation) {
13298 /* L4TUNLEN can support 127 words */
13299 len = skb_inner_network_header(skb) - skb_transport_header(skb);
13300 if (len & ~(127 * 2))
13303 /* IPLEN can support at most 127 dwords */
13304 len = skb_inner_transport_header(skb) -
13305 skb_inner_network_header(skb);
13306 if (len & ~(127 * 4))
13310 /* No need to validate L4LEN as TCP is the only protocol with a
13311 * flexible value and we support all possible values supported
13312 * by TCP, which is at most 15 dwords
13317 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13321 * i40e_xdp_setup - add/remove an XDP program
13322 * @vsi: VSI to changed
13323 * @prog: XDP program
13324 * @extack: netlink extended ack
13326 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13327 struct netlink_ext_ack *extack)
13329 int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13330 struct i40e_pf *pf = vsi->back;
13331 struct bpf_prog *old_prog;
13335 /* Don't allow frames that span over multiple buffers */
13336 if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13337 NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13341 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
13342 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13345 i40e_prep_for_reset(pf);
13347 /* VSI shall be deleted in a moment, just return EINVAL */
13348 if (test_bit(__I40E_IN_REMOVE, pf->state))
13351 old_prog = xchg(&vsi->xdp_prog, prog);
13355 xdp_features_clear_redirect_target(vsi->netdev);
13356 /* Wait until ndo_xsk_wakeup completes. */
13359 i40e_reset_and_rebuild(pf, true, true);
13362 if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13363 if (i40e_realloc_rx_bi_zc(vsi, true))
13365 } else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13366 if (i40e_realloc_rx_bi_zc(vsi, false))
13370 for (i = 0; i < vsi->num_queue_pairs; i++)
13371 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13374 bpf_prog_put(old_prog);
13376 /* Kick start the NAPI context if there is an AF_XDP socket open
13377 * on that queue id. This so that receiving will start.
13379 if (need_reset && prog) {
13380 for (i = 0; i < vsi->num_queue_pairs; i++)
13381 if (vsi->xdp_rings[i]->xsk_pool)
13382 (void)i40e_xsk_wakeup(vsi->netdev, i,
13384 xdp_features_set_redirect_target(vsi->netdev, true);
13391 * i40e_enter_busy_conf - Enters busy config state
13394 * Returns 0 on success, <0 for failure.
13396 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13398 struct i40e_pf *pf = vsi->back;
13401 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13405 usleep_range(1000, 2000);
13412 * i40e_exit_busy_conf - Exits busy config state
13415 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13417 struct i40e_pf *pf = vsi->back;
13419 clear_bit(__I40E_CONFIG_BUSY, pf->state);
13423 * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13425 * @queue_pair: queue pair
13427 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13429 memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13430 sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13431 memset(&vsi->tx_rings[queue_pair]->stats, 0,
13432 sizeof(vsi->tx_rings[queue_pair]->stats));
13433 if (i40e_enabled_xdp_vsi(vsi)) {
13434 memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13435 sizeof(vsi->xdp_rings[queue_pair]->stats));
13440 * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13442 * @queue_pair: queue pair
13444 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13446 i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13447 if (i40e_enabled_xdp_vsi(vsi)) {
13448 /* Make sure that in-progress ndo_xdp_xmit calls are
13452 i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13454 i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13458 * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13460 * @queue_pair: queue pair
13461 * @enable: true for enable, false for disable
13463 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13466 struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13467 struct i40e_q_vector *q_vector = rxr->q_vector;
13472 /* All rings in a qp belong to the same qvector. */
13473 if (q_vector->rx.ring || q_vector->tx.ring) {
13475 napi_enable(&q_vector->napi);
13477 napi_disable(&q_vector->napi);
13482 * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13484 * @queue_pair: queue pair
13485 * @enable: true for enable, false for disable
13487 * Returns 0 on success, <0 on failure.
13489 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13492 struct i40e_pf *pf = vsi->back;
13495 pf_q = vsi->base_queue + queue_pair;
13496 ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13497 false /*is xdp*/, enable);
13499 dev_info(&pf->pdev->dev,
13500 "VSI seid %d Tx ring %d %sable timeout\n",
13501 vsi->seid, pf_q, (enable ? "en" : "dis"));
13505 i40e_control_rx_q(pf, pf_q, enable);
13506 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13508 dev_info(&pf->pdev->dev,
13509 "VSI seid %d Rx ring %d %sable timeout\n",
13510 vsi->seid, pf_q, (enable ? "en" : "dis"));
13514 /* Due to HW errata, on Rx disable only, the register can
13515 * indicate done before it really is. Needs 50ms to be sure
13520 if (!i40e_enabled_xdp_vsi(vsi))
13523 ret = i40e_control_wait_tx_q(vsi->seid, pf,
13524 pf_q + vsi->alloc_queue_pairs,
13525 true /*is xdp*/, enable);
13527 dev_info(&pf->pdev->dev,
13528 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13529 vsi->seid, pf_q, (enable ? "en" : "dis"));
13536 * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13538 * @queue_pair: queue_pair
13540 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13542 struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13543 struct i40e_pf *pf = vsi->back;
13544 struct i40e_hw *hw = &pf->hw;
13546 /* All rings in a qp belong to the same qvector. */
13547 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13548 i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13550 i40e_irq_dynamic_enable_icr0(pf);
13556 * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13558 * @queue_pair: queue_pair
13560 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13562 struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13563 struct i40e_pf *pf = vsi->back;
13564 struct i40e_hw *hw = &pf->hw;
13566 /* For simplicity, instead of removing the qp interrupt causes
13567 * from the interrupt linked list, we simply disable the interrupt, and
13568 * leave the list intact.
13570 * All rings in a qp belong to the same qvector.
13572 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13573 u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13575 wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13577 synchronize_irq(pf->msix_entries[intpf].vector);
13579 /* Legacy and MSI mode - this stops all interrupt handling */
13580 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13581 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13583 synchronize_irq(pf->pdev->irq);
13588 * i40e_queue_pair_disable - Disables a queue pair
13590 * @queue_pair: queue pair
13592 * Returns 0 on success, <0 on failure.
13594 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13598 err = i40e_enter_busy_conf(vsi);
13602 i40e_queue_pair_disable_irq(vsi, queue_pair);
13603 err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13604 i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13605 i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13606 i40e_queue_pair_clean_rings(vsi, queue_pair);
13607 i40e_queue_pair_reset_stats(vsi, queue_pair);
13613 * i40e_queue_pair_enable - Enables a queue pair
13615 * @queue_pair: queue pair
13617 * Returns 0 on success, <0 on failure.
13619 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13623 err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13627 if (i40e_enabled_xdp_vsi(vsi)) {
13628 err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13633 err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13637 err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13638 i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13639 i40e_queue_pair_enable_irq(vsi, queue_pair);
13641 i40e_exit_busy_conf(vsi);
13647 * i40e_xdp - implements ndo_bpf for i40e
13649 * @xdp: XDP command
13651 static int i40e_xdp(struct net_device *dev,
13652 struct netdev_bpf *xdp)
13654 struct i40e_netdev_priv *np = netdev_priv(dev);
13655 struct i40e_vsi *vsi = np->vsi;
13657 if (vsi->type != I40E_VSI_MAIN)
13660 switch (xdp->command) {
13661 case XDP_SETUP_PROG:
13662 return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13663 case XDP_SETUP_XSK_POOL:
13664 return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13665 xdp->xsk.queue_id);
13671 static const struct net_device_ops i40e_netdev_ops = {
13672 .ndo_open = i40e_open,
13673 .ndo_stop = i40e_close,
13674 .ndo_start_xmit = i40e_lan_xmit_frame,
13675 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
13676 .ndo_set_rx_mode = i40e_set_rx_mode,
13677 .ndo_validate_addr = eth_validate_addr,
13678 .ndo_set_mac_address = i40e_set_mac,
13679 .ndo_change_mtu = i40e_change_mtu,
13680 .ndo_eth_ioctl = i40e_ioctl,
13681 .ndo_tx_timeout = i40e_tx_timeout,
13682 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
13683 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
13684 #ifdef CONFIG_NET_POLL_CONTROLLER
13685 .ndo_poll_controller = i40e_netpoll,
13687 .ndo_setup_tc = __i40e_setup_tc,
13688 .ndo_select_queue = i40e_lan_select_queue,
13689 .ndo_set_features = i40e_set_features,
13690 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
13691 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
13692 .ndo_get_vf_stats = i40e_get_vf_stats,
13693 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
13694 .ndo_get_vf_config = i40e_ndo_get_vf_config,
13695 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
13696 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
13697 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
13698 .ndo_get_phys_port_id = i40e_get_phys_port_id,
13699 .ndo_fdb_add = i40e_ndo_fdb_add,
13700 .ndo_features_check = i40e_features_check,
13701 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
13702 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
13703 .ndo_bpf = i40e_xdp,
13704 .ndo_xdp_xmit = i40e_xdp_xmit,
13705 .ndo_xsk_wakeup = i40e_xsk_wakeup,
13706 .ndo_dfwd_add_station = i40e_fwd_add,
13707 .ndo_dfwd_del_station = i40e_fwd_del,
13711 * i40e_config_netdev - Setup the netdev flags
13712 * @vsi: the VSI being configured
13714 * Returns 0 on success, negative value on failure
13716 static int i40e_config_netdev(struct i40e_vsi *vsi)
13718 struct i40e_pf *pf = vsi->back;
13719 struct i40e_hw *hw = &pf->hw;
13720 struct i40e_netdev_priv *np;
13721 struct net_device *netdev;
13722 u8 broadcast[ETH_ALEN];
13723 u8 mac_addr[ETH_ALEN];
13725 netdev_features_t hw_enc_features;
13726 netdev_features_t hw_features;
13728 etherdev_size = sizeof(struct i40e_netdev_priv);
13729 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13733 vsi->netdev = netdev;
13734 np = netdev_priv(netdev);
13737 hw_enc_features = NETIF_F_SG |
13740 NETIF_F_SOFT_FEATURES |
13745 NETIF_F_GSO_GRE_CSUM |
13746 NETIF_F_GSO_PARTIAL |
13747 NETIF_F_GSO_IPXIP4 |
13748 NETIF_F_GSO_IPXIP6 |
13749 NETIF_F_GSO_UDP_TUNNEL |
13750 NETIF_F_GSO_UDP_TUNNEL_CSUM |
13751 NETIF_F_GSO_UDP_L4 |
13757 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13758 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13760 netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13762 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13764 netdev->hw_enc_features |= hw_enc_features;
13766 /* record features VLANs can make use of */
13767 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13769 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
13770 NETIF_F_GSO_GRE_CSUM | \
13771 NETIF_F_GSO_IPXIP4 | \
13772 NETIF_F_GSO_IPXIP6 | \
13773 NETIF_F_GSO_UDP_TUNNEL | \
13774 NETIF_F_GSO_UDP_TUNNEL_CSUM)
13776 netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13777 netdev->features |= NETIF_F_GSO_PARTIAL |
13778 I40E_GSO_PARTIAL_FEATURES;
13780 netdev->mpls_features |= NETIF_F_SG;
13781 netdev->mpls_features |= NETIF_F_HW_CSUM;
13782 netdev->mpls_features |= NETIF_F_TSO;
13783 netdev->mpls_features |= NETIF_F_TSO6;
13784 netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13786 /* enable macvlan offloads */
13787 netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13789 hw_features = hw_enc_features |
13790 NETIF_F_HW_VLAN_CTAG_TX |
13791 NETIF_F_HW_VLAN_CTAG_RX;
13793 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13794 hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13796 netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13798 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13799 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13801 netdev->features &= ~NETIF_F_HW_TC;
13803 if (vsi->type == I40E_VSI_MAIN) {
13804 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13805 ether_addr_copy(mac_addr, hw->mac.perm_addr);
13806 /* The following steps are necessary for two reasons. First,
13807 * some older NVM configurations load a default MAC-VLAN
13808 * filter that will accept any tagged packet, and we want to
13809 * replace this with a normal filter. Additionally, it is
13810 * possible our MAC address was provided by the platform using
13811 * Open Firmware or similar.
13813 * Thus, we need to remove the default filter and install one
13814 * specific to the MAC address.
13816 i40e_rm_default_mac_filter(vsi, mac_addr);
13817 spin_lock_bh(&vsi->mac_filter_hash_lock);
13818 i40e_add_mac_filter(vsi, mac_addr);
13819 spin_unlock_bh(&vsi->mac_filter_hash_lock);
13821 netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13822 NETDEV_XDP_ACT_REDIRECT |
13823 NETDEV_XDP_ACT_XSK_ZEROCOPY |
13824 NETDEV_XDP_ACT_RX_SG;
13826 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13827 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13828 * the end, which is 4 bytes long, so force truncation of the
13829 * original name by IFNAMSIZ - 4
13831 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13833 pf->vsi[pf->lan_vsi]->netdev->name);
13834 eth_random_addr(mac_addr);
13836 spin_lock_bh(&vsi->mac_filter_hash_lock);
13837 i40e_add_mac_filter(vsi, mac_addr);
13838 spin_unlock_bh(&vsi->mac_filter_hash_lock);
13841 /* Add the broadcast filter so that we initially will receive
13842 * broadcast packets. Note that when a new VLAN is first added the
13843 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13844 * specific filters as part of transitioning into "vlan" operation.
13845 * When more VLANs are added, the driver will copy each existing MAC
13846 * filter and add it for the new VLAN.
13848 * Broadcast filters are handled specially by
13849 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13850 * promiscuous bit instead of adding this directly as a MAC/VLAN
13851 * filter. The subtask will update the correct broadcast promiscuous
13852 * bits as VLANs become active or inactive.
13854 eth_broadcast_addr(broadcast);
13855 spin_lock_bh(&vsi->mac_filter_hash_lock);
13856 i40e_add_mac_filter(vsi, broadcast);
13857 spin_unlock_bh(&vsi->mac_filter_hash_lock);
13859 eth_hw_addr_set(netdev, mac_addr);
13860 ether_addr_copy(netdev->perm_addr, mac_addr);
13862 /* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13863 netdev->neigh_priv_len = sizeof(u32) * 4;
13865 netdev->priv_flags |= IFF_UNICAST_FLT;
13866 netdev->priv_flags |= IFF_SUPP_NOFCS;
13867 /* Setup netdev TC information */
13868 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13870 netdev->netdev_ops = &i40e_netdev_ops;
13871 netdev->watchdog_timeo = 5 * HZ;
13872 i40e_set_ethtool_ops(netdev);
13874 /* MTU range: 68 - 9706 */
13875 netdev->min_mtu = ETH_MIN_MTU;
13876 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13882 * i40e_vsi_delete - Delete a VSI from the switch
13883 * @vsi: the VSI being removed
13885 * Returns 0 on success, negative value on failure
13887 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13889 /* remove default VSI is not allowed */
13890 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13893 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13897 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13898 * @vsi: the VSI being queried
13900 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13902 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13904 struct i40e_veb *veb;
13905 struct i40e_pf *pf = vsi->back;
13907 /* Uplink is not a bridge so default to VEB */
13908 if (vsi->veb_idx >= I40E_MAX_VEB)
13911 veb = pf->veb[vsi->veb_idx];
13913 dev_info(&pf->pdev->dev,
13914 "There is no veb associated with the bridge\n");
13918 /* Uplink is a bridge in VEPA mode */
13919 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13922 /* Uplink is a bridge in VEB mode */
13926 /* VEPA is now default bridge, so return 0 */
13931 * i40e_add_vsi - Add a VSI to the switch
13932 * @vsi: the VSI being configured
13934 * This initializes a VSI context depending on the VSI type to be added and
13935 * passes it down to the add_vsi aq command.
13937 static int i40e_add_vsi(struct i40e_vsi *vsi)
13940 struct i40e_pf *pf = vsi->back;
13941 struct i40e_hw *hw = &pf->hw;
13942 struct i40e_vsi_context ctxt;
13943 struct i40e_mac_filter *f;
13944 struct hlist_node *h;
13947 u8 enabled_tc = 0x1; /* TC0 enabled */
13950 memset(&ctxt, 0, sizeof(ctxt));
13951 switch (vsi->type) {
13952 case I40E_VSI_MAIN:
13953 /* The PF's main VSI is already setup as part of the
13954 * device initialization, so we'll not bother with
13955 * the add_vsi call, but we will retrieve the current
13958 ctxt.seid = pf->main_vsi_seid;
13959 ctxt.pf_num = pf->hw.pf_id;
13961 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13962 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13964 dev_info(&pf->pdev->dev,
13965 "couldn't get PF vsi config, err %pe aq_err %s\n",
13967 i40e_aq_str(&pf->hw,
13968 pf->hw.aq.asq_last_status));
13971 vsi->info = ctxt.info;
13972 vsi->info.valid_sections = 0;
13974 vsi->seid = ctxt.seid;
13975 vsi->id = ctxt.vsi_number;
13977 enabled_tc = i40e_pf_get_tc_map(pf);
13979 /* Source pruning is enabled by default, so the flag is
13980 * negative logic - if it's set, we need to fiddle with
13981 * the VSI to disable source pruning.
13983 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13984 memset(&ctxt, 0, sizeof(ctxt));
13985 ctxt.seid = pf->main_vsi_seid;
13986 ctxt.pf_num = pf->hw.pf_id;
13988 ctxt.info.valid_sections |=
13989 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13990 ctxt.info.switch_id =
13991 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13992 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13994 dev_info(&pf->pdev->dev,
13995 "update vsi failed, err %d aq_err %s\n",
13997 i40e_aq_str(&pf->hw,
13998 pf->hw.aq.asq_last_status));
14004 /* MFP mode setup queue map and update VSI */
14005 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14006 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14007 memset(&ctxt, 0, sizeof(ctxt));
14008 ctxt.seid = pf->main_vsi_seid;
14009 ctxt.pf_num = pf->hw.pf_id;
14011 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14012 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14014 dev_info(&pf->pdev->dev,
14015 "update vsi failed, err %pe aq_err %s\n",
14017 i40e_aq_str(&pf->hw,
14018 pf->hw.aq.asq_last_status));
14022 /* update the local VSI info queue map */
14023 i40e_vsi_update_queue_map(vsi, &ctxt);
14024 vsi->info.valid_sections = 0;
14026 /* Default/Main VSI is only enabled for TC0
14027 * reconfigure it to enable all TCs that are
14028 * available on the port in SFP mode.
14029 * For MFP case the iSCSI PF would use this
14030 * flow to enable LAN+iSCSI TC.
14032 ret = i40e_vsi_config_tc(vsi, enabled_tc);
14034 /* Single TC condition is not fatal,
14035 * message and continue
14037 dev_info(&pf->pdev->dev,
14038 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14041 i40e_aq_str(&pf->hw,
14042 pf->hw.aq.asq_last_status));
14047 case I40E_VSI_FDIR:
14048 ctxt.pf_num = hw->pf_id;
14050 ctxt.uplink_seid = vsi->uplink_seid;
14051 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14052 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14053 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14054 (i40e_is_vsi_uplink_mode_veb(vsi))) {
14055 ctxt.info.valid_sections |=
14056 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14057 ctxt.info.switch_id =
14058 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14060 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14063 case I40E_VSI_VMDQ2:
14064 ctxt.pf_num = hw->pf_id;
14066 ctxt.uplink_seid = vsi->uplink_seid;
14067 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14068 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14070 /* This VSI is connected to VEB so the switch_id
14071 * should be set to zero by default.
14073 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14074 ctxt.info.valid_sections |=
14075 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14076 ctxt.info.switch_id =
14077 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14080 /* Setup the VSI tx/rx queue map for TC0 only for now */
14081 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14084 case I40E_VSI_SRIOV:
14085 ctxt.pf_num = hw->pf_id;
14086 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14087 ctxt.uplink_seid = vsi->uplink_seid;
14088 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14089 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14091 /* This VSI is connected to VEB so the switch_id
14092 * should be set to zero by default.
14094 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14095 ctxt.info.valid_sections |=
14096 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14097 ctxt.info.switch_id =
14098 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14101 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14102 ctxt.info.valid_sections |=
14103 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14104 ctxt.info.queueing_opt_flags |=
14105 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14106 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14109 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14110 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14111 if (pf->vf[vsi->vf_id].spoofchk) {
14112 ctxt.info.valid_sections |=
14113 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14114 ctxt.info.sec_flags |=
14115 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14116 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14118 /* Setup the VSI tx/rx queue map for TC0 only for now */
14119 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14122 case I40E_VSI_IWARP:
14123 /* send down message to iWARP */
14130 if (vsi->type != I40E_VSI_MAIN) {
14131 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14133 dev_info(&vsi->back->pdev->dev,
14134 "add vsi failed, err %pe aq_err %s\n",
14136 i40e_aq_str(&pf->hw,
14137 pf->hw.aq.asq_last_status));
14141 vsi->info = ctxt.info;
14142 vsi->info.valid_sections = 0;
14143 vsi->seid = ctxt.seid;
14144 vsi->id = ctxt.vsi_number;
14147 spin_lock_bh(&vsi->mac_filter_hash_lock);
14148 vsi->active_filters = 0;
14149 /* If macvlan filters already exist, force them to get loaded */
14150 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14151 f->state = I40E_FILTER_NEW;
14154 spin_unlock_bh(&vsi->mac_filter_hash_lock);
14155 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14158 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14159 set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14162 /* Update VSI BW information */
14163 ret = i40e_vsi_get_bw_info(vsi);
14165 dev_info(&pf->pdev->dev,
14166 "couldn't get vsi bw info, err %pe aq_err %s\n",
14168 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14169 /* VSI is already added so not tearing that up */
14178 * i40e_vsi_release - Delete a VSI and free its resources
14179 * @vsi: the VSI being removed
14181 * Returns 0 on success or < 0 on error
14183 int i40e_vsi_release(struct i40e_vsi *vsi)
14185 struct i40e_mac_filter *f;
14186 struct hlist_node *h;
14187 struct i40e_veb *veb = NULL;
14188 struct i40e_pf *pf;
14194 /* release of a VEB-owner or last VSI is not allowed */
14195 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14196 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14197 vsi->seid, vsi->uplink_seid);
14200 if (vsi == pf->vsi[pf->lan_vsi] &&
14201 !test_bit(__I40E_DOWN, pf->state)) {
14202 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14205 set_bit(__I40E_VSI_RELEASING, vsi->state);
14206 uplink_seid = vsi->uplink_seid;
14207 if (vsi->type != I40E_VSI_SRIOV) {
14208 if (vsi->netdev_registered) {
14209 vsi->netdev_registered = false;
14211 /* results in a call to i40e_close() */
14212 unregister_netdev(vsi->netdev);
14215 i40e_vsi_close(vsi);
14217 i40e_vsi_disable_irq(vsi);
14220 spin_lock_bh(&vsi->mac_filter_hash_lock);
14222 /* clear the sync flag on all filters */
14224 __dev_uc_unsync(vsi->netdev, NULL);
14225 __dev_mc_unsync(vsi->netdev, NULL);
14228 /* make sure any remaining filters are marked for deletion */
14229 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14230 __i40e_del_filter(vsi, f);
14232 spin_unlock_bh(&vsi->mac_filter_hash_lock);
14234 i40e_sync_vsi_filters(vsi);
14236 i40e_vsi_delete(vsi);
14237 i40e_vsi_free_q_vectors(vsi);
14239 free_netdev(vsi->netdev);
14240 vsi->netdev = NULL;
14242 i40e_vsi_clear_rings(vsi);
14243 i40e_vsi_clear(vsi);
14245 /* If this was the last thing on the VEB, except for the
14246 * controlling VSI, remove the VEB, which puts the controlling
14247 * VSI onto the next level down in the switch.
14249 * Well, okay, there's one more exception here: don't remove
14250 * the orphan VEBs yet. We'll wait for an explicit remove request
14251 * from up the network stack.
14253 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14255 pf->vsi[i]->uplink_seid == uplink_seid &&
14256 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14257 n++; /* count the VSIs */
14260 for (i = 0; i < I40E_MAX_VEB; i++) {
14263 if (pf->veb[i]->uplink_seid == uplink_seid)
14264 n++; /* count the VEBs */
14265 if (pf->veb[i]->seid == uplink_seid)
14268 if (n == 0 && veb && veb->uplink_seid != 0)
14269 i40e_veb_release(veb);
14275 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14276 * @vsi: ptr to the VSI
14278 * This should only be called after i40e_vsi_mem_alloc() which allocates the
14279 * corresponding SW VSI structure and initializes num_queue_pairs for the
14280 * newly allocated VSI.
14282 * Returns 0 on success or negative on failure
14284 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14287 struct i40e_pf *pf = vsi->back;
14289 if (vsi->q_vectors[0]) {
14290 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14295 if (vsi->base_vector) {
14296 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14297 vsi->seid, vsi->base_vector);
14301 ret = i40e_vsi_alloc_q_vectors(vsi);
14303 dev_info(&pf->pdev->dev,
14304 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14305 vsi->num_q_vectors, vsi->seid, ret);
14306 vsi->num_q_vectors = 0;
14307 goto vector_setup_out;
14310 /* In Legacy mode, we do not have to get any other vector since we
14311 * piggyback on the misc/ICR0 for queue interrupts.
14313 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14315 if (vsi->num_q_vectors)
14316 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14317 vsi->num_q_vectors, vsi->idx);
14318 if (vsi->base_vector < 0) {
14319 dev_info(&pf->pdev->dev,
14320 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14321 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14322 i40e_vsi_free_q_vectors(vsi);
14324 goto vector_setup_out;
14332 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14333 * @vsi: pointer to the vsi.
14335 * This re-allocates a vsi's queue resources.
14337 * Returns pointer to the successfully allocated and configured VSI sw struct
14338 * on success, otherwise returns NULL on failure.
14340 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14342 u16 alloc_queue_pairs;
14343 struct i40e_pf *pf;
14352 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14353 i40e_vsi_clear_rings(vsi);
14355 i40e_vsi_free_arrays(vsi, false);
14356 i40e_set_num_rings_in_vsi(vsi);
14357 ret = i40e_vsi_alloc_arrays(vsi, false);
14361 alloc_queue_pairs = vsi->alloc_queue_pairs *
14362 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14364 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14366 dev_info(&pf->pdev->dev,
14367 "failed to get tracking for %d queues for VSI %d err %d\n",
14368 alloc_queue_pairs, vsi->seid, ret);
14371 vsi->base_queue = ret;
14373 /* Update the FW view of the VSI. Force a reset of TC and queue
14374 * layout configurations.
14376 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14377 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14378 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14379 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14380 if (vsi->type == I40E_VSI_MAIN)
14381 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14383 /* assign it some queues */
14384 ret = i40e_alloc_rings(vsi);
14388 /* map all of the rings to the q_vectors */
14389 i40e_vsi_map_rings_to_vectors(vsi);
14393 i40e_vsi_free_q_vectors(vsi);
14394 if (vsi->netdev_registered) {
14395 vsi->netdev_registered = false;
14396 unregister_netdev(vsi->netdev);
14397 free_netdev(vsi->netdev);
14398 vsi->netdev = NULL;
14400 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14402 i40e_vsi_clear(vsi);
14407 * i40e_vsi_setup - Set up a VSI by a given type
14408 * @pf: board private structure
14410 * @uplink_seid: the switch element to link to
14411 * @param1: usage depends upon VSI type. For VF types, indicates VF id
14413 * This allocates the sw VSI structure and its queue resources, then add a VSI
14414 * to the identified VEB.
14416 * Returns pointer to the successfully allocated and configure VSI sw struct on
14417 * success, otherwise returns NULL on failure.
14419 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14420 u16 uplink_seid, u32 param1)
14422 struct i40e_vsi *vsi = NULL;
14423 struct i40e_veb *veb = NULL;
14424 u16 alloc_queue_pairs;
14428 /* The requested uplink_seid must be either
14429 * - the PF's port seid
14430 * no VEB is needed because this is the PF
14431 * or this is a Flow Director special case VSI
14432 * - seid of an existing VEB
14433 * - seid of a VSI that owns an existing VEB
14434 * - seid of a VSI that doesn't own a VEB
14435 * a new VEB is created and the VSI becomes the owner
14436 * - seid of the PF VSI, which is what creates the first VEB
14437 * this is a special case of the previous
14439 * Find which uplink_seid we were given and create a new VEB if needed
14441 for (i = 0; i < I40E_MAX_VEB; i++) {
14442 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14448 if (!veb && uplink_seid != pf->mac_seid) {
14450 for (i = 0; i < pf->num_alloc_vsi; i++) {
14451 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14457 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14462 if (vsi->uplink_seid == pf->mac_seid)
14463 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14464 vsi->tc_config.enabled_tc);
14465 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14466 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14467 vsi->tc_config.enabled_tc);
14469 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14470 dev_info(&vsi->back->pdev->dev,
14471 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14474 /* We come up by default in VEPA mode if SRIOV is not
14475 * already enabled, in which case we can't force VEPA
14478 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14479 veb->bridge_mode = BRIDGE_MODE_VEPA;
14480 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14482 i40e_config_bridge_mode(veb);
14484 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14485 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14489 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14493 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14494 uplink_seid = veb->seid;
14497 /* get vsi sw struct */
14498 v_idx = i40e_vsi_mem_alloc(pf, type);
14501 vsi = pf->vsi[v_idx];
14505 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14507 if (type == I40E_VSI_MAIN)
14508 pf->lan_vsi = v_idx;
14509 else if (type == I40E_VSI_SRIOV)
14510 vsi->vf_id = param1;
14511 /* assign it some queues */
14512 alloc_queue_pairs = vsi->alloc_queue_pairs *
14513 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14515 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14517 dev_info(&pf->pdev->dev,
14518 "failed to get tracking for %d queues for VSI %d err=%d\n",
14519 alloc_queue_pairs, vsi->seid, ret);
14522 vsi->base_queue = ret;
14524 /* get a VSI from the hardware */
14525 vsi->uplink_seid = uplink_seid;
14526 ret = i40e_add_vsi(vsi);
14530 switch (vsi->type) {
14531 /* setup the netdev if needed */
14532 case I40E_VSI_MAIN:
14533 case I40E_VSI_VMDQ2:
14534 ret = i40e_config_netdev(vsi);
14537 ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14540 ret = register_netdev(vsi->netdev);
14543 vsi->netdev_registered = true;
14544 netif_carrier_off(vsi->netdev);
14545 #ifdef CONFIG_I40E_DCB
14546 /* Setup DCB netlink interface */
14547 i40e_dcbnl_setup(vsi);
14548 #endif /* CONFIG_I40E_DCB */
14550 case I40E_VSI_FDIR:
14551 /* set up vectors and rings if needed */
14552 ret = i40e_vsi_setup_vectors(vsi);
14556 ret = i40e_alloc_rings(vsi);
14560 /* map all of the rings to the q_vectors */
14561 i40e_vsi_map_rings_to_vectors(vsi);
14563 i40e_vsi_reset_stats(vsi);
14566 /* no netdev or rings for the other VSI types */
14570 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14571 (vsi->type == I40E_VSI_VMDQ2)) {
14572 ret = i40e_vsi_config_rss(vsi);
14577 i40e_vsi_free_q_vectors(vsi);
14579 if (vsi->netdev_registered) {
14580 vsi->netdev_registered = false;
14581 unregister_netdev(vsi->netdev);
14582 free_netdev(vsi->netdev);
14583 vsi->netdev = NULL;
14586 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14588 i40e_vsi_clear(vsi);
14594 * i40e_veb_get_bw_info - Query VEB BW information
14595 * @veb: the veb to query
14597 * Query the Tx scheduler BW configuration data for given VEB
14599 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14601 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14602 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14603 struct i40e_pf *pf = veb->pf;
14604 struct i40e_hw *hw = &pf->hw;
14609 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14612 dev_info(&pf->pdev->dev,
14613 "query veb bw config failed, err %pe aq_err %s\n",
14615 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14619 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14622 dev_info(&pf->pdev->dev,
14623 "query veb bw ets config failed, err %pe aq_err %s\n",
14625 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14629 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14630 veb->bw_max_quanta = ets_data.tc_bw_max;
14631 veb->is_abs_credits = bw_data.absolute_credits_enable;
14632 veb->enabled_tc = ets_data.tc_valid_bits;
14633 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14634 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14635 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14636 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14637 veb->bw_tc_limit_credits[i] =
14638 le16_to_cpu(bw_data.tc_bw_limits[i]);
14639 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14647 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14648 * @pf: board private structure
14650 * On error: returns error code (negative)
14651 * On success: returns vsi index in PF (positive)
14653 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14656 struct i40e_veb *veb;
14659 /* Need to protect the allocation of switch elements at the PF level */
14660 mutex_lock(&pf->switch_mutex);
14662 /* VEB list may be fragmented if VEB creation/destruction has
14663 * been happening. We can afford to do a quick scan to look
14664 * for any free slots in the list.
14666 * find next empty veb slot, looping back around if necessary
14669 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14671 if (i >= I40E_MAX_VEB) {
14673 goto err_alloc_veb; /* out of VEB slots! */
14676 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14679 goto err_alloc_veb;
14683 veb->enabled_tc = 1;
14688 mutex_unlock(&pf->switch_mutex);
14693 * i40e_switch_branch_release - Delete a branch of the switch tree
14694 * @branch: where to start deleting
14696 * This uses recursion to find the tips of the branch to be
14697 * removed, deleting until we get back to and can delete this VEB.
14699 static void i40e_switch_branch_release(struct i40e_veb *branch)
14701 struct i40e_pf *pf = branch->pf;
14702 u16 branch_seid = branch->seid;
14703 u16 veb_idx = branch->idx;
14706 /* release any VEBs on this VEB - RECURSION */
14707 for (i = 0; i < I40E_MAX_VEB; i++) {
14710 if (pf->veb[i]->uplink_seid == branch->seid)
14711 i40e_switch_branch_release(pf->veb[i]);
14714 /* Release the VSIs on this VEB, but not the owner VSI.
14716 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14717 * the VEB itself, so don't use (*branch) after this loop.
14719 for (i = 0; i < pf->num_alloc_vsi; i++) {
14722 if (pf->vsi[i]->uplink_seid == branch_seid &&
14723 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14724 i40e_vsi_release(pf->vsi[i]);
14728 /* There's one corner case where the VEB might not have been
14729 * removed, so double check it here and remove it if needed.
14730 * This case happens if the veb was created from the debugfs
14731 * commands and no VSIs were added to it.
14733 if (pf->veb[veb_idx])
14734 i40e_veb_release(pf->veb[veb_idx]);
14738 * i40e_veb_clear - remove veb struct
14739 * @veb: the veb to remove
14741 static void i40e_veb_clear(struct i40e_veb *veb)
14747 struct i40e_pf *pf = veb->pf;
14749 mutex_lock(&pf->switch_mutex);
14750 if (pf->veb[veb->idx] == veb)
14751 pf->veb[veb->idx] = NULL;
14752 mutex_unlock(&pf->switch_mutex);
14759 * i40e_veb_release - Delete a VEB and free its resources
14760 * @veb: the VEB being removed
14762 void i40e_veb_release(struct i40e_veb *veb)
14764 struct i40e_vsi *vsi = NULL;
14765 struct i40e_pf *pf;
14770 /* find the remaining VSI and check for extras */
14771 for (i = 0; i < pf->num_alloc_vsi; i++) {
14772 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14778 dev_info(&pf->pdev->dev,
14779 "can't remove VEB %d with %d VSIs left\n",
14784 /* move the remaining VSI to uplink veb */
14785 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14786 if (veb->uplink_seid) {
14787 vsi->uplink_seid = veb->uplink_seid;
14788 if (veb->uplink_seid == pf->mac_seid)
14789 vsi->veb_idx = I40E_NO_VEB;
14791 vsi->veb_idx = veb->veb_idx;
14794 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14795 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14798 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14799 i40e_veb_clear(veb);
14803 * i40e_add_veb - create the VEB in the switch
14804 * @veb: the VEB to be instantiated
14805 * @vsi: the controlling VSI
14807 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14809 struct i40e_pf *pf = veb->pf;
14810 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14813 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14814 veb->enabled_tc, false,
14815 &veb->seid, enable_stats, NULL);
14817 /* get a VEB from the hardware */
14819 dev_info(&pf->pdev->dev,
14820 "couldn't add VEB, err %pe aq_err %s\n",
14822 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14826 /* get statistics counter */
14827 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14828 &veb->stats_idx, NULL, NULL, NULL);
14830 dev_info(&pf->pdev->dev,
14831 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14833 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14836 ret = i40e_veb_get_bw_info(veb);
14838 dev_info(&pf->pdev->dev,
14839 "couldn't get VEB bw info, err %pe aq_err %s\n",
14841 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14842 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14846 vsi->uplink_seid = veb->seid;
14847 vsi->veb_idx = veb->idx;
14848 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14854 * i40e_veb_setup - Set up a VEB
14855 * @pf: board private structure
14856 * @flags: VEB setup flags
14857 * @uplink_seid: the switch element to link to
14858 * @vsi_seid: the initial VSI seid
14859 * @enabled_tc: Enabled TC bit-map
14861 * This allocates the sw VEB structure and links it into the switch
14862 * It is possible and legal for this to be a duplicate of an already
14863 * existing VEB. It is also possible for both uplink and vsi seids
14864 * to be zero, in order to create a floating VEB.
14866 * Returns pointer to the successfully allocated VEB sw struct on
14867 * success, otherwise returns NULL on failure.
14869 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14870 u16 uplink_seid, u16 vsi_seid,
14873 struct i40e_veb *veb, *uplink_veb = NULL;
14874 int vsi_idx, veb_idx;
14877 /* if one seid is 0, the other must be 0 to create a floating relay */
14878 if ((uplink_seid == 0 || vsi_seid == 0) &&
14879 (uplink_seid + vsi_seid != 0)) {
14880 dev_info(&pf->pdev->dev,
14881 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14882 uplink_seid, vsi_seid);
14886 /* make sure there is such a vsi and uplink */
14887 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14888 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14890 if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14891 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14896 if (uplink_seid && uplink_seid != pf->mac_seid) {
14897 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14898 if (pf->veb[veb_idx] &&
14899 pf->veb[veb_idx]->seid == uplink_seid) {
14900 uplink_veb = pf->veb[veb_idx];
14905 dev_info(&pf->pdev->dev,
14906 "uplink seid %d not found\n", uplink_seid);
14911 /* get veb sw struct */
14912 veb_idx = i40e_veb_mem_alloc(pf);
14915 veb = pf->veb[veb_idx];
14916 veb->flags = flags;
14917 veb->uplink_seid = uplink_seid;
14918 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14919 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14921 /* create the VEB in the switch */
14922 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14925 if (vsi_idx == pf->lan_vsi)
14926 pf->lan_veb = veb->idx;
14931 i40e_veb_clear(veb);
14937 * i40e_setup_pf_switch_element - set PF vars based on switch type
14938 * @pf: board private structure
14939 * @ele: element we are building info from
14940 * @num_reported: total number of elements
14941 * @printconfig: should we print the contents
14943 * helper function to assist in extracting a few useful SEID values.
14945 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14946 struct i40e_aqc_switch_config_element_resp *ele,
14947 u16 num_reported, bool printconfig)
14949 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14950 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14951 u8 element_type = ele->element_type;
14952 u16 seid = le16_to_cpu(ele->seid);
14955 dev_info(&pf->pdev->dev,
14956 "type=%d seid=%d uplink=%d downlink=%d\n",
14957 element_type, seid, uplink_seid, downlink_seid);
14959 switch (element_type) {
14960 case I40E_SWITCH_ELEMENT_TYPE_MAC:
14961 pf->mac_seid = seid;
14963 case I40E_SWITCH_ELEMENT_TYPE_VEB:
14965 if (uplink_seid != pf->mac_seid)
14967 if (pf->lan_veb >= I40E_MAX_VEB) {
14970 /* find existing or else empty VEB */
14971 for (v = 0; v < I40E_MAX_VEB; v++) {
14972 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14977 if (pf->lan_veb >= I40E_MAX_VEB) {
14978 v = i40e_veb_mem_alloc(pf);
14984 if (pf->lan_veb >= I40E_MAX_VEB)
14987 pf->veb[pf->lan_veb]->seid = seid;
14988 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14989 pf->veb[pf->lan_veb]->pf = pf;
14990 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14992 case I40E_SWITCH_ELEMENT_TYPE_VSI:
14993 if (num_reported != 1)
14995 /* This is immediately after a reset so we can assume this is
14998 pf->mac_seid = uplink_seid;
14999 pf->pf_seid = downlink_seid;
15000 pf->main_vsi_seid = seid;
15002 dev_info(&pf->pdev->dev,
15003 "pf_seid=%d main_vsi_seid=%d\n",
15004 pf->pf_seid, pf->main_vsi_seid);
15006 case I40E_SWITCH_ELEMENT_TYPE_PF:
15007 case I40E_SWITCH_ELEMENT_TYPE_VF:
15008 case I40E_SWITCH_ELEMENT_TYPE_EMP:
15009 case I40E_SWITCH_ELEMENT_TYPE_BMC:
15010 case I40E_SWITCH_ELEMENT_TYPE_PE:
15011 case I40E_SWITCH_ELEMENT_TYPE_PA:
15012 /* ignore these for now */
15015 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15016 element_type, seid);
15022 * i40e_fetch_switch_configuration - Get switch config from firmware
15023 * @pf: board private structure
15024 * @printconfig: should we print the contents
15026 * Get the current switch configuration from the device and
15027 * extract a few useful SEID values.
15029 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15031 struct i40e_aqc_get_switch_config_resp *sw_config;
15037 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15041 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15043 u16 num_reported, num_total;
15045 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15049 dev_info(&pf->pdev->dev,
15050 "get switch config failed err %d aq_err %s\n",
15052 i40e_aq_str(&pf->hw,
15053 pf->hw.aq.asq_last_status));
15058 num_reported = le16_to_cpu(sw_config->header.num_reported);
15059 num_total = le16_to_cpu(sw_config->header.num_total);
15062 dev_info(&pf->pdev->dev,
15063 "header: %d reported %d total\n",
15064 num_reported, num_total);
15066 for (i = 0; i < num_reported; i++) {
15067 struct i40e_aqc_switch_config_element_resp *ele =
15068 &sw_config->element[i];
15070 i40e_setup_pf_switch_element(pf, ele, num_reported,
15073 } while (next_seid != 0);
15080 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15081 * @pf: board private structure
15082 * @reinit: if the Main VSI needs to re-initialized.
15083 * @lock_acquired: indicates whether or not the lock has been acquired
15085 * Returns 0 on success, negative value on failure
15087 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15092 /* find out what's out there already */
15093 ret = i40e_fetch_switch_configuration(pf, false);
15095 dev_info(&pf->pdev->dev,
15096 "couldn't fetch switch config, err %pe aq_err %s\n",
15098 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15101 i40e_pf_reset_stats(pf);
15103 /* set the switch config bit for the whole device to
15104 * support limited promisc or true promisc
15105 * when user requests promisc. The default is limited
15109 if ((pf->hw.pf_id == 0) &&
15110 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15111 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15112 pf->last_sw_conf_flags = flags;
15115 if (pf->hw.pf_id == 0) {
15118 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15119 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15121 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15122 dev_info(&pf->pdev->dev,
15123 "couldn't set switch config bits, err %pe aq_err %s\n",
15125 i40e_aq_str(&pf->hw,
15126 pf->hw.aq.asq_last_status));
15127 /* not a fatal problem, just keep going */
15129 pf->last_sw_conf_valid_flags = valid_flags;
15132 /* first time setup */
15133 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15134 struct i40e_vsi *vsi = NULL;
15137 /* Set up the PF VSI associated with the PF's main VSI
15138 * that is already in the HW switch
15140 if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15141 uplink_seid = pf->veb[pf->lan_veb]->seid;
15143 uplink_seid = pf->mac_seid;
15144 if (pf->lan_vsi == I40E_NO_VSI)
15145 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15147 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15149 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15150 i40e_cloud_filter_exit(pf);
15151 i40e_fdir_teardown(pf);
15155 /* force a reset of TC and queue layout configurations */
15156 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15158 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15159 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15160 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15162 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15164 i40e_fdir_sb_setup(pf);
15166 /* Setup static PF queue filter control settings */
15167 ret = i40e_setup_pf_filter_control(pf);
15169 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15171 /* Failure here should not stop continuing other steps */
15174 /* enable RSS in the HW, even for only one queue, as the stack can use
15177 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15178 i40e_pf_config_rss(pf);
15180 /* fill in link information and enable LSE reporting */
15181 i40e_link_event(pf);
15183 /* Initialize user-specific link properties */
15184 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15185 I40E_AQ_AN_COMPLETED) ? true : false);
15189 if (!lock_acquired)
15192 /* repopulate tunnel port filters */
15193 udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15195 if (!lock_acquired)
15202 * i40e_determine_queue_usage - Work out queue distribution
15203 * @pf: board private structure
15205 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15210 pf->num_lan_qps = 0;
15212 /* Find the max queues to be put into basic use. We'll always be
15213 * using TC0, whether or not DCB is running, and TC0 will get the
15216 queues_left = pf->hw.func_caps.num_tx_qp;
15218 if ((queues_left == 1) ||
15219 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15220 /* one qp for PF, no queues for anything else */
15222 pf->alloc_rss_size = pf->num_lan_qps = 1;
15224 /* make sure all the fancies are disabled */
15225 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
15226 I40E_FLAG_IWARP_ENABLED |
15227 I40E_FLAG_FD_SB_ENABLED |
15228 I40E_FLAG_FD_ATR_ENABLED |
15229 I40E_FLAG_DCB_CAPABLE |
15230 I40E_FLAG_DCB_ENABLED |
15231 I40E_FLAG_SRIOV_ENABLED |
15232 I40E_FLAG_VMDQ_ENABLED);
15233 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15234 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15235 I40E_FLAG_FD_SB_ENABLED |
15236 I40E_FLAG_FD_ATR_ENABLED |
15237 I40E_FLAG_DCB_CAPABLE))) {
15238 /* one qp for PF */
15239 pf->alloc_rss_size = pf->num_lan_qps = 1;
15240 queues_left -= pf->num_lan_qps;
15242 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
15243 I40E_FLAG_IWARP_ENABLED |
15244 I40E_FLAG_FD_SB_ENABLED |
15245 I40E_FLAG_FD_ATR_ENABLED |
15246 I40E_FLAG_DCB_ENABLED |
15247 I40E_FLAG_VMDQ_ENABLED);
15248 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15250 /* Not enough queues for all TCs */
15251 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15252 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15253 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15254 I40E_FLAG_DCB_ENABLED);
15255 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15258 /* limit lan qps to the smaller of qps, cpus or msix */
15259 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15260 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15261 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15262 pf->num_lan_qps = q_max;
15264 queues_left -= pf->num_lan_qps;
15267 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15268 if (queues_left > 1) {
15269 queues_left -= 1; /* save 1 queue for FD */
15271 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15272 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15273 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15277 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15278 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15279 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15280 (queues_left / pf->num_vf_qps));
15281 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15284 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15285 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15286 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15287 (queues_left / pf->num_vmdq_qps));
15288 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15291 pf->queues_left = queues_left;
15292 dev_dbg(&pf->pdev->dev,
15293 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15294 pf->hw.func_caps.num_tx_qp,
15295 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15296 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15297 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15302 * i40e_setup_pf_filter_control - Setup PF static filter control
15303 * @pf: PF to be setup
15305 * i40e_setup_pf_filter_control sets up a PF's initial filter control
15306 * settings. If PE/FCoE are enabled then it will also set the per PF
15307 * based filter sizes required for them. It also enables Flow director,
15308 * ethertype and macvlan type filter settings for the pf.
15310 * Returns 0 on success, negative on failure
15312 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15314 struct i40e_filter_control_settings *settings = &pf->filter_settings;
15316 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15318 /* Flow Director is enabled */
15319 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15320 settings->enable_fdir = true;
15322 /* Ethtype and MACVLAN filters enabled for PF */
15323 settings->enable_ethtype = true;
15324 settings->enable_macvlan = true;
15326 if (i40e_set_filter_control(&pf->hw, settings))
15332 #define INFO_STRING_LEN 255
15333 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15334 static void i40e_print_features(struct i40e_pf *pf)
15336 struct i40e_hw *hw = &pf->hw;
15340 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15344 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15345 #ifdef CONFIG_PCI_IOV
15346 i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15348 i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15349 pf->hw.func_caps.num_vsis,
15350 pf->vsi[pf->lan_vsi]->num_queue_pairs);
15351 if (pf->flags & I40E_FLAG_RSS_ENABLED)
15352 i += scnprintf(&buf[i], REMAIN(i), " RSS");
15353 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15354 i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15355 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15356 i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15357 i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15359 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15360 i += scnprintf(&buf[i], REMAIN(i), " DCB");
15361 i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15362 i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15363 if (pf->flags & I40E_FLAG_PTP)
15364 i += scnprintf(&buf[i], REMAIN(i), " PTP");
15365 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15366 i += scnprintf(&buf[i], REMAIN(i), " VEB");
15368 i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15370 dev_info(&pf->pdev->dev, "%s\n", buf);
15372 WARN_ON(i > INFO_STRING_LEN);
15376 * i40e_get_platform_mac_addr - get platform-specific MAC address
15377 * @pdev: PCI device information struct
15378 * @pf: board private structure
15380 * Look up the MAC address for the device. First we'll try
15381 * eth_platform_get_mac_address, which will check Open Firmware, or arch
15382 * specific fallback. Otherwise, we'll default to the stored value in
15385 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15387 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15388 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15392 * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15393 * @fec_cfg: FEC option to set in flags
15394 * @flags: ptr to flags in which we set FEC option
15396 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15398 if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15399 *flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15400 if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15401 (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15402 *flags |= I40E_FLAG_RS_FEC;
15403 *flags &= ~I40E_FLAG_BASE_R_FEC;
15405 if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15406 (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15407 *flags |= I40E_FLAG_BASE_R_FEC;
15408 *flags &= ~I40E_FLAG_RS_FEC;
15411 *flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15415 * i40e_check_recovery_mode - check if we are running transition firmware
15416 * @pf: board private structure
15418 * Check registers indicating the firmware runs in recovery mode. Sets the
15419 * appropriate driver state.
15421 * Returns true if the recovery mode was detected, false otherwise
15423 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15425 u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15427 if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15428 dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15429 dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15430 set_bit(__I40E_RECOVERY_MODE, pf->state);
15434 if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15435 dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15441 * i40e_pf_loop_reset - perform reset in a loop.
15442 * @pf: board private structure
15444 * This function is useful when a NIC is about to enter recovery mode.
15445 * When a NIC's internal data structures are corrupted the NIC's
15446 * firmware is going to enter recovery mode.
15447 * Right after a POR it takes about 7 minutes for firmware to enter
15448 * recovery mode. Until that time a NIC is in some kind of intermediate
15449 * state. After that time period the NIC almost surely enters
15450 * recovery mode. The only way for a driver to detect intermediate
15451 * state is to issue a series of pf-resets and check a return value.
15452 * If a PF reset returns success then the firmware could be in recovery
15453 * mode so the caller of this code needs to check for recovery mode
15454 * if this function returns success. There is a little chance that
15455 * firmware will hang in intermediate state forever.
15456 * Since waiting 7 minutes is quite a lot of time this function waits
15457 * 10 seconds and then gives up by returning an error.
15459 * Return 0 on success, negative on failure.
15461 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15463 /* wait max 10 seconds for PF reset to succeed */
15464 const unsigned long time_end = jiffies + 10 * HZ;
15465 struct i40e_hw *hw = &pf->hw;
15468 ret = i40e_pf_reset(hw);
15469 while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15470 usleep_range(10000, 20000);
15471 ret = i40e_pf_reset(hw);
15474 if (ret == I40E_SUCCESS)
15477 dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15483 * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15484 * @pf: board private structure
15486 * Check FW registers to determine if FW issued unexpected EMP Reset.
15487 * Every time when unexpected EMP Reset occurs the FW increments
15488 * a counter of unexpected EMP Resets. When the counter reaches 10
15489 * the FW should enter the Recovery mode
15491 * Returns true if FW issued unexpected EMP Reset
15493 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15495 const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15496 I40E_GL_FWSTS_FWS1B_MASK;
15497 return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15498 (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15502 * i40e_handle_resets - handle EMP resets and PF resets
15503 * @pf: board private structure
15505 * Handle both EMP resets and PF resets and conclude whether there are
15506 * any issues regarding these resets. If there are any issues then
15507 * generate log entry.
15509 * Return 0 if NIC is healthy or negative value when there are issues
15512 static int i40e_handle_resets(struct i40e_pf *pf)
15514 const int pfr = i40e_pf_loop_reset(pf);
15515 const bool is_empr = i40e_check_fw_empr(pf);
15517 if (is_empr || pfr != I40E_SUCCESS)
15518 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");
15520 return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15524 * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15525 * @pf: board private structure
15526 * @hw: ptr to the hardware info
15528 * This function does a minimal setup of all subsystems needed for running
15531 * Returns 0 on success, negative on failure
15533 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15535 struct i40e_vsi *vsi;
15539 pci_set_drvdata(pf->pdev, pf);
15540 pci_save_state(pf->pdev);
15542 /* set up periodic task facility */
15543 timer_setup(&pf->service_timer, i40e_service_timer, 0);
15544 pf->service_timer_period = HZ;
15546 INIT_WORK(&pf->service_task, i40e_service_task);
15547 clear_bit(__I40E_SERVICE_SCHED, pf->state);
15549 err = i40e_init_interrupt_scheme(pf);
15551 goto err_switch_setup;
15553 /* The number of VSIs reported by the FW is the minimum guaranteed
15554 * to us; HW supports far more and we share the remaining pool with
15555 * the other PFs. We allocate space for more than the guarantee with
15556 * the understanding that we might not get them all later.
15558 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15559 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15561 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15563 /* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15564 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15568 goto err_switch_setup;
15571 /* We allocate one VSI which is needed as absolute minimum
15572 * in order to register the netdev
15574 v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15577 goto err_switch_setup;
15579 pf->lan_vsi = v_idx;
15580 vsi = pf->vsi[v_idx];
15583 goto err_switch_setup;
15585 vsi->alloc_queue_pairs = 1;
15586 err = i40e_config_netdev(vsi);
15588 goto err_switch_setup;
15589 err = register_netdev(vsi->netdev);
15591 goto err_switch_setup;
15592 vsi->netdev_registered = true;
15593 i40e_dbg_pf_init(pf);
15595 err = i40e_setup_misc_vector_for_recovery_mode(pf);
15597 goto err_switch_setup;
15599 /* tell the firmware that we're starting */
15600 i40e_send_version(pf);
15602 /* since everything's happy, start the service_task timer */
15603 mod_timer(&pf->service_timer,
15604 round_jiffies(jiffies + pf->service_timer_period));
15609 i40e_reset_interrupt_capability(pf);
15610 timer_shutdown_sync(&pf->service_timer);
15611 i40e_shutdown_adminq(hw);
15612 iounmap(hw->hw_addr);
15613 pci_release_mem_regions(pf->pdev);
15614 pci_disable_device(pf->pdev);
15621 * i40e_set_subsystem_device_id - set subsystem device id
15622 * @hw: pointer to the hardware info
15624 * Set PCI subsystem device id either from a pci_dev structure or
15625 * a specific FW register.
15627 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15629 struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15631 hw->subsystem_device_id = pdev->subsystem_device ?
15632 pdev->subsystem_device :
15633 (ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15637 * i40e_probe - Device initialization routine
15638 * @pdev: PCI device information struct
15639 * @ent: entry in i40e_pci_tbl
15641 * i40e_probe initializes a PF identified by a pci_dev structure.
15642 * The OS initialization, configuring of the PF private structure,
15643 * and a hardware reset occur.
15645 * Returns 0 on success, negative on failure
15647 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15649 struct i40e_aq_get_phy_abilities_resp abilities;
15650 #ifdef CONFIG_I40E_DCB
15651 enum i40e_get_fw_lldp_status_resp lldp_status;
15652 #endif /* CONFIG_I40E_DCB */
15653 struct i40e_pf *pf;
15654 struct i40e_hw *hw;
15655 static u16 pfs_found;
15658 #ifdef CONFIG_I40E_DCB
15660 #endif /* CONFIG_I40E_DCB */
15665 err = pci_enable_device_mem(pdev);
15669 /* set up for high or low dma */
15670 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15672 dev_err(&pdev->dev,
15673 "DMA configuration failed: 0x%x\n", err);
15677 /* set up pci connections */
15678 err = pci_request_mem_regions(pdev, i40e_driver_name);
15680 dev_info(&pdev->dev,
15681 "pci_request_selected_regions failed %d\n", err);
15685 pci_set_master(pdev);
15687 /* Now that we have a PCI connection, we need to do the
15688 * low level device setup. This is primarily setting up
15689 * the Admin Queue structures and then querying for the
15690 * device's current profile information.
15692 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15699 set_bit(__I40E_DOWN, pf->state);
15704 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15705 I40E_MAX_CSR_SPACE);
15706 /* We believe that the highest register to read is
15707 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15708 * is not less than that before mapping to prevent a
15711 if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15712 dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15717 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15718 if (!hw->hw_addr) {
15720 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15721 (unsigned int)pci_resource_start(pdev, 0),
15722 pf->ioremap_len, err);
15725 hw->vendor_id = pdev->vendor;
15726 hw->device_id = pdev->device;
15727 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15728 hw->subsystem_vendor_id = pdev->subsystem_vendor;
15729 i40e_set_subsystem_device_id(hw);
15730 hw->bus.device = PCI_SLOT(pdev->devfn);
15731 hw->bus.func = PCI_FUNC(pdev->devfn);
15732 hw->bus.bus_id = pdev->bus->number;
15733 pf->instance = pfs_found;
15735 /* Select something other than the 802.1ad ethertype for the
15736 * switch to use internally and drop on ingress.
15738 hw->switch_tag = 0xffff;
15739 hw->first_tag = ETH_P_8021AD;
15740 hw->second_tag = ETH_P_8021Q;
15742 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15743 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15744 INIT_LIST_HEAD(&pf->ddp_old_prof);
15746 /* set up the locks for the AQ, do this only once in probe
15747 * and destroy them only once in remove
15749 mutex_init(&hw->aq.asq_mutex);
15750 mutex_init(&hw->aq.arq_mutex);
15752 pf->msg_enable = netif_msg_init(debug,
15757 pf->hw.debug_mask = debug;
15759 /* do a special CORER for clearing PXE mode once at init */
15760 if (hw->revision_id == 0 &&
15761 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15762 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15767 i40e_clear_pxe_mode(hw);
15770 /* Reset here to make sure all is clean and to define PF 'n' */
15773 err = i40e_set_mac_type(hw);
15775 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15780 err = i40e_handle_resets(pf);
15784 i40e_check_recovery_mode(pf);
15786 if (is_kdump_kernel()) {
15787 hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15788 hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15790 hw->aq.num_arq_entries = I40E_AQ_LEN;
15791 hw->aq.num_asq_entries = I40E_AQ_LEN;
15793 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15794 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15795 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15797 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15799 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15801 err = i40e_init_shared_code(hw);
15803 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15808 /* set up a default setting for link flow control */
15809 pf->hw.fc.requested_mode = I40E_FC_NONE;
15811 err = i40e_init_adminq(hw);
15813 if (err == I40E_ERR_FIRMWARE_API_VERSION)
15814 dev_info(&pdev->dev,
15815 "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",
15816 hw->aq.api_maj_ver,
15817 hw->aq.api_min_ver,
15818 I40E_FW_API_VERSION_MAJOR,
15819 I40E_FW_MINOR_VERSION(hw));
15821 dev_info(&pdev->dev,
15822 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15826 i40e_get_oem_version(hw);
15828 /* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15829 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15830 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15831 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15832 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15833 hw->subsystem_vendor_id, hw->subsystem_device_id);
15835 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15836 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15837 dev_dbg(&pdev->dev,
15838 "The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15839 hw->aq.api_maj_ver,
15840 hw->aq.api_min_ver,
15841 I40E_FW_API_VERSION_MAJOR,
15842 I40E_FW_MINOR_VERSION(hw));
15843 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15844 dev_info(&pdev->dev,
15845 "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",
15846 hw->aq.api_maj_ver,
15847 hw->aq.api_min_ver,
15848 I40E_FW_API_VERSION_MAJOR,
15849 I40E_FW_MINOR_VERSION(hw));
15851 i40e_verify_eeprom(pf);
15853 /* Rev 0 hardware was never productized */
15854 if (hw->revision_id < 1)
15855 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");
15857 i40e_clear_pxe_mode(hw);
15859 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15861 goto err_adminq_setup;
15863 err = i40e_sw_init(pf);
15865 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15869 if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15870 return i40e_init_recovery_mode(pf, hw);
15872 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15873 hw->func_caps.num_rx_qp, 0, 0);
15875 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15876 goto err_init_lan_hmc;
15879 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15881 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15883 goto err_configure_lan_hmc;
15886 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
15887 * Ignore error return codes because if it was already disabled via
15888 * hardware settings this will fail
15890 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15891 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15892 i40e_aq_stop_lldp(hw, true, false, NULL);
15895 /* allow a platform config to override the HW addr */
15896 i40e_get_platform_mac_addr(pdev, pf);
15898 if (!is_valid_ether_addr(hw->mac.addr)) {
15899 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15903 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15904 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15905 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15906 if (is_valid_ether_addr(hw->mac.port_addr))
15907 pf->hw_features |= I40E_HW_PORT_ID_VALID;
15909 i40e_ptp_alloc_pins(pf);
15910 pci_set_drvdata(pdev, pf);
15911 pci_save_state(pdev);
15913 #ifdef CONFIG_I40E_DCB
15914 status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15916 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15917 (pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15918 (pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15919 dev_info(&pdev->dev,
15920 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15921 "FW LLDP is disabled\n" :
15922 "FW LLDP is enabled\n");
15924 /* Enable FW to write default DCB config on link-up */
15925 i40e_aq_set_dcb_parameters(hw, true, NULL);
15927 err = i40e_init_pf_dcb(pf);
15929 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15930 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15931 /* Continue without DCB enabled */
15933 #endif /* CONFIG_I40E_DCB */
15935 /* set up periodic task facility */
15936 timer_setup(&pf->service_timer, i40e_service_timer, 0);
15937 pf->service_timer_period = HZ;
15939 INIT_WORK(&pf->service_task, i40e_service_task);
15940 clear_bit(__I40E_SERVICE_SCHED, pf->state);
15942 /* NVM bit on means WoL disabled for the port */
15943 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15944 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15945 pf->wol_en = false;
15948 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15950 /* set up the main switch operations */
15951 i40e_determine_queue_usage(pf);
15952 err = i40e_init_interrupt_scheme(pf);
15954 goto err_switch_setup;
15956 /* Reduce Tx and Rx pairs for kdump
15957 * When MSI-X is enabled, it's not allowed to use more TC queue
15958 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15959 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15961 if (is_kdump_kernel())
15962 pf->num_lan_msix = 1;
15964 pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15965 pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15966 pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15967 pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15968 pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15969 pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15970 UDP_TUNNEL_TYPE_GENEVE;
15972 /* The number of VSIs reported by the FW is the minimum guaranteed
15973 * to us; HW supports far more and we share the remaining pool with
15974 * the other PFs. We allocate space for more than the guarantee with
15975 * the understanding that we might not get them all later.
15977 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15978 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15980 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15981 if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15982 dev_warn(&pf->pdev->dev,
15983 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15984 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15985 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15988 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15989 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15993 goto err_switch_setup;
15996 #ifdef CONFIG_PCI_IOV
15997 /* prep for VF support */
15998 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15999 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16000 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16001 if (pci_num_vf(pdev))
16002 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16005 err = i40e_setup_pf_switch(pf, false, false);
16007 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16010 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16012 /* if FDIR VSI was set up, start it now */
16013 for (i = 0; i < pf->num_alloc_vsi; i++) {
16014 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16015 i40e_vsi_open(pf->vsi[i]);
16020 /* The driver only wants link up/down and module qualification
16021 * reports from firmware. Note the negative logic.
16023 err = i40e_aq_set_phy_int_mask(&pf->hw,
16024 ~(I40E_AQ_EVENT_LINK_UPDOWN |
16025 I40E_AQ_EVENT_MEDIA_NA |
16026 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16028 dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16030 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16032 /* Reconfigure hardware for allowing smaller MSS in the case
16033 * of TSO, so that we avoid the MDD being fired and causing
16034 * a reset in the case of small MSS+TSO.
16036 val = rd32(hw, I40E_REG_MSS);
16037 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16038 val &= ~I40E_REG_MSS_MIN_MASK;
16039 val |= I40E_64BYTE_MSS;
16040 wr32(hw, I40E_REG_MSS, val);
16043 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16045 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16047 dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16049 i40e_aq_str(&pf->hw,
16050 pf->hw.aq.asq_last_status));
16052 /* The main driver is (mostly) up and happy. We need to set this state
16053 * before setting up the misc vector or we get a race and the vector
16054 * ends up disabled forever.
16056 clear_bit(__I40E_DOWN, pf->state);
16058 /* In case of MSIX we are going to setup the misc vector right here
16059 * to handle admin queue events etc. In case of legacy and MSI
16060 * the misc functionality and queue processing is combined in
16061 * the same vector and that gets setup at open.
16063 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16064 err = i40e_setup_misc_vector(pf);
16066 dev_info(&pdev->dev,
16067 "setup of misc vector failed: %d\n", err);
16068 i40e_cloud_filter_exit(pf);
16069 i40e_fdir_teardown(pf);
16074 #ifdef CONFIG_PCI_IOV
16075 /* prep for VF support */
16076 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16077 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16078 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16079 /* disable link interrupts for VFs */
16080 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16081 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16082 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16085 if (pci_num_vf(pdev)) {
16086 dev_info(&pdev->dev,
16087 "Active VFs found, allocating resources.\n");
16088 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16090 dev_info(&pdev->dev,
16091 "Error %d allocating resources for existing VFs\n",
16095 #endif /* CONFIG_PCI_IOV */
16097 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16098 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16099 pf->num_iwarp_msix,
16100 I40E_IWARP_IRQ_PILE_ID);
16101 if (pf->iwarp_base_vector < 0) {
16102 dev_info(&pdev->dev,
16103 "failed to get tracking for %d vectors for IWARP err=%d\n",
16104 pf->num_iwarp_msix, pf->iwarp_base_vector);
16105 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16109 i40e_dbg_pf_init(pf);
16111 /* tell the firmware that we're starting */
16112 i40e_send_version(pf);
16114 /* since everything's happy, start the service_task timer */
16115 mod_timer(&pf->service_timer,
16116 round_jiffies(jiffies + pf->service_timer_period));
16118 /* add this PF to client device list and launch a client service task */
16119 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16120 err = i40e_lan_add_device(pf);
16122 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16126 #define PCI_SPEED_SIZE 8
16127 #define PCI_WIDTH_SIZE 8
16128 /* Devices on the IOSF bus do not have this information
16129 * and will report PCI Gen 1 x 1 by default so don't bother
16132 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16133 char speed[PCI_SPEED_SIZE] = "Unknown";
16134 char width[PCI_WIDTH_SIZE] = "Unknown";
16136 /* Get the negotiated link width and speed from PCI config
16139 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16142 i40e_set_pci_config_data(hw, link_status);
16144 switch (hw->bus.speed) {
16145 case i40e_bus_speed_8000:
16146 strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16147 case i40e_bus_speed_5000:
16148 strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16149 case i40e_bus_speed_2500:
16150 strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16154 switch (hw->bus.width) {
16155 case i40e_bus_width_pcie_x8:
16156 strscpy(width, "8", PCI_WIDTH_SIZE); break;
16157 case i40e_bus_width_pcie_x4:
16158 strscpy(width, "4", PCI_WIDTH_SIZE); break;
16159 case i40e_bus_width_pcie_x2:
16160 strscpy(width, "2", PCI_WIDTH_SIZE); break;
16161 case i40e_bus_width_pcie_x1:
16162 strscpy(width, "1", PCI_WIDTH_SIZE); break;
16167 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16170 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16171 hw->bus.speed < i40e_bus_speed_8000) {
16172 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16173 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16177 /* get the requested speeds from the fw */
16178 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16180 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %pe last_status = %s\n",
16182 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16183 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16185 /* set the FEC config due to the board capabilities */
16186 i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16188 /* get the supported phy types from the fw */
16189 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16191 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %pe last_status = %s\n",
16193 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16195 /* make sure the MFS hasn't been set lower than the default */
16196 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16197 val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16198 I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16199 if (val < MAX_FRAME_SIZE_DEFAULT)
16200 dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16203 /* Add a filter to drop all Flow control frames from any VSI from being
16204 * transmitted. By doing so we stop a malicious VF from sending out
16205 * PAUSE or PFC frames and potentially controlling traffic for other
16207 * The FW can still send Flow control frames if enabled.
16209 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16210 pf->main_vsi_seid);
16212 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16213 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16214 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16215 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16216 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16217 /* print a string summarizing features */
16218 i40e_print_features(pf);
16222 /* Unwind what we've done if something failed in the setup */
16224 set_bit(__I40E_DOWN, pf->state);
16225 i40e_clear_interrupt_scheme(pf);
16228 i40e_reset_interrupt_capability(pf);
16229 timer_shutdown_sync(&pf->service_timer);
16231 err_configure_lan_hmc:
16232 (void)i40e_shutdown_lan_hmc(hw);
16234 kfree(pf->qp_pile);
16238 iounmap(hw->hw_addr);
16242 pci_release_mem_regions(pdev);
16245 pci_disable_device(pdev);
16250 * i40e_remove - Device removal routine
16251 * @pdev: PCI device information struct
16253 * i40e_remove is called by the PCI subsystem to alert the driver
16254 * that is should release a PCI device. This could be caused by a
16255 * Hot-Plug event, or because the driver is going to be removed from
16258 static void i40e_remove(struct pci_dev *pdev)
16260 struct i40e_pf *pf = pci_get_drvdata(pdev);
16261 struct i40e_hw *hw = &pf->hw;
16265 i40e_dbg_pf_exit(pf);
16269 /* Disable RSS in hw */
16270 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16271 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16273 /* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16274 * flags, once they are set, i40e_rebuild should not be called as
16275 * i40e_prep_for_reset always returns early.
16277 while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16278 usleep_range(1000, 2000);
16279 set_bit(__I40E_IN_REMOVE, pf->state);
16281 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16282 set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16284 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16286 /* no more scheduling of any task */
16287 set_bit(__I40E_SUSPENDED, pf->state);
16288 set_bit(__I40E_DOWN, pf->state);
16289 if (pf->service_timer.function)
16290 timer_shutdown_sync(&pf->service_timer);
16291 if (pf->service_task.func)
16292 cancel_work_sync(&pf->service_task);
16294 if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16295 struct i40e_vsi *vsi = pf->vsi[0];
16297 /* We know that we have allocated only one vsi for this PF,
16298 * it was just for registering netdevice, so the interface
16299 * could be visible in the 'ifconfig' output
16301 unregister_netdev(vsi->netdev);
16302 free_netdev(vsi->netdev);
16307 /* Client close must be called explicitly here because the timer
16308 * has been stopped.
16310 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16312 i40e_fdir_teardown(pf);
16314 /* If there is a switch structure or any orphans, remove them.
16315 * This will leave only the PF's VSI remaining.
16317 for (i = 0; i < I40E_MAX_VEB; i++) {
16321 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16322 pf->veb[i]->uplink_seid == 0)
16323 i40e_switch_branch_release(pf->veb[i]);
16326 /* Now we can shutdown the PF's VSI, just before we kill
16329 if (pf->vsi[pf->lan_vsi])
16330 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16332 i40e_cloud_filter_exit(pf);
16334 /* remove attached clients */
16335 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16336 ret_code = i40e_lan_del_device(pf);
16338 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16342 /* shutdown and destroy the HMC */
16343 if (hw->hmc.hmc_obj) {
16344 ret_code = i40e_shutdown_lan_hmc(hw);
16346 dev_warn(&pdev->dev,
16347 "Failed to destroy the HMC resources: %d\n",
16352 /* Free MSI/legacy interrupt 0 when in recovery mode. */
16353 if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16354 !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16355 free_irq(pf->pdev->irq, pf);
16357 /* shutdown the adminq */
16358 i40e_shutdown_adminq(hw);
16360 /* destroy the locks only once, here */
16361 mutex_destroy(&hw->aq.arq_mutex);
16362 mutex_destroy(&hw->aq.asq_mutex);
16364 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16366 i40e_clear_interrupt_scheme(pf);
16367 for (i = 0; i < pf->num_alloc_vsi; i++) {
16369 if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16370 i40e_vsi_clear_rings(pf->vsi[i]);
16371 i40e_vsi_clear(pf->vsi[i]);
16377 for (i = 0; i < I40E_MAX_VEB; i++) {
16382 kfree(pf->qp_pile);
16385 iounmap(hw->hw_addr);
16387 pci_release_mem_regions(pdev);
16389 pci_disable_device(pdev);
16393 * i40e_pci_error_detected - warning that something funky happened in PCI land
16394 * @pdev: PCI device information struct
16395 * @error: the type of PCI error
16397 * Called to warn that something happened and the error handling steps
16398 * are in progress. Allows the driver to quiesce things, be ready for
16401 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16402 pci_channel_state_t error)
16404 struct i40e_pf *pf = pci_get_drvdata(pdev);
16406 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16409 dev_info(&pdev->dev,
16410 "Cannot recover - error happened during device probe\n");
16411 return PCI_ERS_RESULT_DISCONNECT;
16414 /* shutdown all operations */
16415 if (!test_bit(__I40E_SUSPENDED, pf->state))
16416 i40e_prep_for_reset(pf);
16418 /* Request a slot reset */
16419 return PCI_ERS_RESULT_NEED_RESET;
16423 * i40e_pci_error_slot_reset - a PCI slot reset just happened
16424 * @pdev: PCI device information struct
16426 * Called to find if the driver can work with the device now that
16427 * the pci slot has been reset. If a basic connection seems good
16428 * (registers are readable and have sane content) then return a
16429 * happy little PCI_ERS_RESULT_xxx.
16431 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16433 struct i40e_pf *pf = pci_get_drvdata(pdev);
16434 pci_ers_result_t result;
16437 dev_dbg(&pdev->dev, "%s\n", __func__);
16438 if (pci_enable_device_mem(pdev)) {
16439 dev_info(&pdev->dev,
16440 "Cannot re-enable PCI device after reset.\n");
16441 result = PCI_ERS_RESULT_DISCONNECT;
16443 pci_set_master(pdev);
16444 pci_restore_state(pdev);
16445 pci_save_state(pdev);
16446 pci_wake_from_d3(pdev, false);
16448 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16450 result = PCI_ERS_RESULT_RECOVERED;
16452 result = PCI_ERS_RESULT_DISCONNECT;
16459 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16460 * @pdev: PCI device information struct
16462 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16464 struct i40e_pf *pf = pci_get_drvdata(pdev);
16466 i40e_prep_for_reset(pf);
16470 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16471 * @pdev: PCI device information struct
16473 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16475 struct i40e_pf *pf = pci_get_drvdata(pdev);
16477 if (test_bit(__I40E_IN_REMOVE, pf->state))
16480 i40e_reset_and_rebuild(pf, false, false);
16484 * i40e_pci_error_resume - restart operations after PCI error recovery
16485 * @pdev: PCI device information struct
16487 * Called to allow the driver to bring things back up after PCI error
16488 * and/or reset recovery has finished.
16490 static void i40e_pci_error_resume(struct pci_dev *pdev)
16492 struct i40e_pf *pf = pci_get_drvdata(pdev);
16494 dev_dbg(&pdev->dev, "%s\n", __func__);
16495 if (test_bit(__I40E_SUSPENDED, pf->state))
16498 i40e_handle_reset_warning(pf, false);
16502 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16503 * using the mac_address_write admin q function
16504 * @pf: pointer to i40e_pf struct
16506 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16508 struct i40e_hw *hw = &pf->hw;
16513 /* Get current MAC address in case it's an LAA */
16514 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16515 ether_addr_copy(mac_addr,
16516 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16518 dev_err(&pf->pdev->dev,
16519 "Failed to retrieve MAC address; using default\n");
16520 ether_addr_copy(mac_addr, hw->mac.addr);
16523 /* The FW expects the mac address write cmd to first be called with
16524 * one of these flags before calling it again with the multicast
16527 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16529 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16530 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16532 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16534 dev_err(&pf->pdev->dev,
16535 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16539 flags = I40E_AQC_MC_MAG_EN
16540 | I40E_AQC_WOL_PRESERVE_ON_PFR
16541 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16542 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16544 dev_err(&pf->pdev->dev,
16545 "Failed to enable Multicast Magic Packet wake up\n");
16549 * i40e_shutdown - PCI callback for shutting down
16550 * @pdev: PCI device information struct
16552 static void i40e_shutdown(struct pci_dev *pdev)
16554 struct i40e_pf *pf = pci_get_drvdata(pdev);
16555 struct i40e_hw *hw = &pf->hw;
16557 set_bit(__I40E_SUSPENDED, pf->state);
16558 set_bit(__I40E_DOWN, pf->state);
16560 del_timer_sync(&pf->service_timer);
16561 cancel_work_sync(&pf->service_task);
16562 i40e_cloud_filter_exit(pf);
16563 i40e_fdir_teardown(pf);
16565 /* Client close must be called explicitly here because the timer
16566 * has been stopped.
16568 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16570 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16571 i40e_enable_mc_magic_wake(pf);
16573 i40e_prep_for_reset(pf);
16575 wr32(hw, I40E_PFPM_APM,
16576 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16577 wr32(hw, I40E_PFPM_WUFC,
16578 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16580 /* Free MSI/legacy interrupt 0 when in recovery mode. */
16581 if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16582 !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16583 free_irq(pf->pdev->irq, pf);
16585 /* Since we're going to destroy queues during the
16586 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16590 i40e_clear_interrupt_scheme(pf);
16593 if (system_state == SYSTEM_POWER_OFF) {
16594 pci_wake_from_d3(pdev, pf->wol_en);
16595 pci_set_power_state(pdev, PCI_D3hot);
16600 * i40e_suspend - PM callback for moving to D3
16601 * @dev: generic device information structure
16603 static int __maybe_unused i40e_suspend(struct device *dev)
16605 struct i40e_pf *pf = dev_get_drvdata(dev);
16606 struct i40e_hw *hw = &pf->hw;
16608 /* If we're already suspended, then there is nothing to do */
16609 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16612 set_bit(__I40E_DOWN, pf->state);
16614 /* Ensure service task will not be running */
16615 del_timer_sync(&pf->service_timer);
16616 cancel_work_sync(&pf->service_task);
16618 /* Client close must be called explicitly here because the timer
16619 * has been stopped.
16621 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16623 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16624 i40e_enable_mc_magic_wake(pf);
16626 /* Since we're going to destroy queues during the
16627 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16632 i40e_prep_for_reset(pf);
16634 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16635 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16637 /* Clear the interrupt scheme and release our IRQs so that the system
16638 * can safely hibernate even when there are a large number of CPUs.
16639 * Otherwise hibernation might fail when mapping all the vectors back
16642 i40e_clear_interrupt_scheme(pf);
16650 * i40e_resume - PM callback for waking up from D3
16651 * @dev: generic device information structure
16653 static int __maybe_unused i40e_resume(struct device *dev)
16655 struct i40e_pf *pf = dev_get_drvdata(dev);
16658 /* If we're not suspended, then there is nothing to do */
16659 if (!test_bit(__I40E_SUSPENDED, pf->state))
16662 /* We need to hold the RTNL lock prior to restoring interrupt schemes,
16663 * since we're going to be restoring queues
16667 /* We cleared the interrupt scheme when we suspended, so we need to
16668 * restore it now to resume device functionality.
16670 err = i40e_restore_interrupt_scheme(pf);
16672 dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16676 clear_bit(__I40E_DOWN, pf->state);
16677 i40e_reset_and_rebuild(pf, false, true);
16681 /* Clear suspended state last after everything is recovered */
16682 clear_bit(__I40E_SUSPENDED, pf->state);
16684 /* Restart the service task */
16685 mod_timer(&pf->service_timer,
16686 round_jiffies(jiffies + pf->service_timer_period));
16691 static const struct pci_error_handlers i40e_err_handler = {
16692 .error_detected = i40e_pci_error_detected,
16693 .slot_reset = i40e_pci_error_slot_reset,
16694 .reset_prepare = i40e_pci_error_reset_prepare,
16695 .reset_done = i40e_pci_error_reset_done,
16696 .resume = i40e_pci_error_resume,
16699 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16701 static struct pci_driver i40e_driver = {
16702 .name = i40e_driver_name,
16703 .id_table = i40e_pci_tbl,
16704 .probe = i40e_probe,
16705 .remove = i40e_remove,
16707 .pm = &i40e_pm_ops,
16709 .shutdown = i40e_shutdown,
16710 .err_handler = &i40e_err_handler,
16711 .sriov_configure = i40e_pci_sriov_configure,
16715 * i40e_init_module - Driver registration routine
16717 * i40e_init_module is the first routine called when the driver is
16718 * loaded. All it does is register with the PCI subsystem.
16720 static int __init i40e_init_module(void)
16724 pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16725 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16727 /* There is no need to throttle the number of active tasks because
16728 * each device limits its own task using a state bit for scheduling
16729 * the service task, and the device tasks do not interfere with each
16730 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16731 * since we need to be able to guarantee forward progress even under
16734 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16736 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16741 err = pci_register_driver(&i40e_driver);
16743 destroy_workqueue(i40e_wq);
16750 module_init(i40e_init_module);
16753 * i40e_exit_module - Driver exit cleanup routine
16755 * i40e_exit_module is called just before the driver is removed
16758 static void __exit i40e_exit_module(void)
16760 pci_unregister_driver(&i40e_driver);
16761 destroy_workqueue(i40e_wq);
16762 ida_destroy(&i40e_client_ida);
16765 module_exit(i40e_exit_module);