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_10G_BASE_T), 0},
70 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
78 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
79 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
80 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
87 /* required last entry */
90 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
92 #define I40E_MAX_VF_COUNT 128
93 static int debug = -1;
94 module_param(debug, uint, 0);
95 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
97 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
98 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
99 MODULE_LICENSE("GPL v2");
101 static struct workqueue_struct *i40e_wq;
103 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
104 struct net_device *netdev, int delta)
106 struct netdev_hw_addr *ha;
111 netdev_for_each_mc_addr(ha, netdev) {
112 if (ether_addr_equal(ha->addr, f->macaddr)) {
113 ha->refcount += delta;
114 if (ha->refcount <= 0)
122 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
123 * @hw: pointer to the HW structure
124 * @mem: ptr to mem struct to fill out
125 * @size: size of memory requested
126 * @alignment: what to align the allocation to
128 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
129 u64 size, u32 alignment)
131 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
133 mem->size = ALIGN(size, alignment);
134 mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
143 * i40e_free_dma_mem_d - OS specific memory free for shared code
144 * @hw: pointer to the HW structure
145 * @mem: ptr to mem struct to free
147 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
149 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
151 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
160 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
161 * @hw: pointer to the HW structure
162 * @mem: ptr to mem struct to fill out
163 * @size: size of memory requested
165 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
169 mem->va = kzalloc(size, GFP_KERNEL);
178 * i40e_free_virt_mem_d - OS specific memory free for shared code
179 * @hw: pointer to the HW structure
180 * @mem: ptr to mem struct to free
182 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
184 /* it's ok to kfree a NULL pointer */
193 * i40e_get_lump - find a lump of free generic resource
194 * @pf: board private structure
195 * @pile: the pile of resource to search
196 * @needed: the number of items needed
197 * @id: an owner id to stick on the items assigned
199 * Returns the base item index of the lump, or negative for error
201 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
207 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
208 dev_info(&pf->pdev->dev,
209 "param err: pile=%s needed=%d id=0x%04x\n",
210 pile ? "<valid>" : "<null>", needed, id);
214 /* Allocate last queue in the pile for FDIR VSI queue
215 * so it doesn't fragment the qp_pile
217 if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
218 if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
219 dev_err(&pf->pdev->dev,
220 "Cannot allocate queue %d for I40E_VSI_FDIR\n",
221 pile->num_entries - 1);
224 pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
225 return pile->num_entries - 1;
229 while (i < pile->num_entries) {
230 /* skip already allocated entries */
231 if (pile->list[i] & I40E_PILE_VALID_BIT) {
236 /* do we have enough in this lump? */
237 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
238 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
243 /* there was enough, so assign it to the requestor */
244 for (j = 0; j < needed; j++)
245 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
250 /* not enough, so skip over it and continue looking */
258 * i40e_put_lump - return a lump of generic resource
259 * @pile: the pile of resource to search
260 * @index: the base item index
261 * @id: the owner id of the items assigned
263 * Returns the count of items in the lump
265 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
267 int valid_id = (id | I40E_PILE_VALID_BIT);
271 if (!pile || index >= pile->num_entries)
275 i < pile->num_entries && pile->list[i] == valid_id;
286 * i40e_find_vsi_from_id - searches for the vsi with the given id
287 * @pf: the pf structure to search for the vsi
288 * @id: id of the vsi it is searching for
290 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
294 for (i = 0; i < pf->num_alloc_vsi; i++)
295 if (pf->vsi[i] && (pf->vsi[i]->id == id))
302 * i40e_service_event_schedule - Schedule the service task to wake up
303 * @pf: board private structure
305 * If not already scheduled, this puts the task into the work queue
307 void i40e_service_event_schedule(struct i40e_pf *pf)
309 if ((!test_bit(__I40E_DOWN, pf->state) &&
310 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
311 test_bit(__I40E_RECOVERY_MODE, pf->state))
312 queue_work(i40e_wq, &pf->service_task);
316 * i40e_tx_timeout - Respond to a Tx Hang
317 * @netdev: network interface device structure
318 * @txqueue: queue number timing out
320 * If any port has noticed a Tx timeout, it is likely that the whole
321 * device is munged, not just the one netdev port, so go for the full
324 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
326 struct i40e_netdev_priv *np = netdev_priv(netdev);
327 struct i40e_vsi *vsi = np->vsi;
328 struct i40e_pf *pf = vsi->back;
329 struct i40e_ring *tx_ring = NULL;
333 pf->tx_timeout_count++;
335 /* with txqueue index, find the tx_ring struct */
336 for (i = 0; i < vsi->num_queue_pairs; i++) {
337 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 vsi->tx_rings[i]->queue_index) {
340 tx_ring = vsi->tx_rings[i];
346 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
347 pf->tx_timeout_recovery_level = 1; /* reset after some time */
348 else if (time_before(jiffies,
349 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 return; /* don't do any new action before the next timeout */
352 /* don't kick off another recovery if one is already pending */
353 if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
357 head = i40e_get_head(tx_ring);
358 /* Read interrupt register */
359 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
361 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
362 tx_ring->vsi->base_vector - 1));
364 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
366 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",
367 vsi->seid, txqueue, tx_ring->next_to_clean,
368 head, tx_ring->next_to_use,
369 readl(tx_ring->tail), val);
372 pf->tx_timeout_last_recovery = jiffies;
373 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
374 pf->tx_timeout_recovery_level, txqueue);
376 switch (pf->tx_timeout_recovery_level) {
378 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
381 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
384 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
387 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
391 i40e_service_event_schedule(pf);
392 pf->tx_timeout_recovery_level++;
396 * i40e_get_vsi_stats_struct - Get System Network Statistics
397 * @vsi: the VSI we care about
399 * Returns the address of the device statistics structure.
400 * The statistics are actually updated from the service task.
402 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
404 return &vsi->net_stats;
408 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
409 * @ring: Tx ring to get statistics from
410 * @stats: statistics entry to be updated
412 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
413 struct rtnl_link_stats64 *stats)
419 start = u64_stats_fetch_begin_irq(&ring->syncp);
420 packets = ring->stats.packets;
421 bytes = ring->stats.bytes;
422 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
424 stats->tx_packets += packets;
425 stats->tx_bytes += bytes;
429 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430 * @netdev: network interface device structure
431 * @stats: data structure to store statistics
433 * Returns the address of the device statistics structure.
434 * The statistics are actually updated from the service task.
436 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
437 struct rtnl_link_stats64 *stats)
439 struct i40e_netdev_priv *np = netdev_priv(netdev);
440 struct i40e_vsi *vsi = np->vsi;
441 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 struct i40e_ring *ring;
445 if (test_bit(__I40E_VSI_DOWN, vsi->state))
452 for (i = 0; i < vsi->num_queue_pairs; i++) {
456 ring = READ_ONCE(vsi->tx_rings[i]);
459 i40e_get_netdev_stats_struct_tx(ring, stats);
461 if (i40e_enabled_xdp_vsi(vsi)) {
462 ring = READ_ONCE(vsi->xdp_rings[i]);
465 i40e_get_netdev_stats_struct_tx(ring, stats);
468 ring = READ_ONCE(vsi->rx_rings[i]);
472 start = u64_stats_fetch_begin_irq(&ring->syncp);
473 packets = ring->stats.packets;
474 bytes = ring->stats.bytes;
475 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
477 stats->rx_packets += packets;
478 stats->rx_bytes += bytes;
483 /* following stats updated by i40e_watchdog_subtask() */
484 stats->multicast = vsi_stats->multicast;
485 stats->tx_errors = vsi_stats->tx_errors;
486 stats->tx_dropped = vsi_stats->tx_dropped;
487 stats->rx_errors = vsi_stats->rx_errors;
488 stats->rx_dropped = vsi_stats->rx_dropped;
489 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
490 stats->rx_length_errors = vsi_stats->rx_length_errors;
494 * i40e_vsi_reset_stats - Resets all stats of the given vsi
495 * @vsi: the VSI to have its stats reset
497 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
499 struct rtnl_link_stats64 *ns;
505 ns = i40e_get_vsi_stats_struct(vsi);
506 memset(ns, 0, sizeof(*ns));
507 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
508 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
509 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
510 if (vsi->rx_rings && vsi->rx_rings[0]) {
511 for (i = 0; i < vsi->num_queue_pairs; i++) {
512 memset(&vsi->rx_rings[i]->stats, 0,
513 sizeof(vsi->rx_rings[i]->stats));
514 memset(&vsi->rx_rings[i]->rx_stats, 0,
515 sizeof(vsi->rx_rings[i]->rx_stats));
516 memset(&vsi->tx_rings[i]->stats, 0,
517 sizeof(vsi->tx_rings[i]->stats));
518 memset(&vsi->tx_rings[i]->tx_stats, 0,
519 sizeof(vsi->tx_rings[i]->tx_stats));
522 vsi->stat_offsets_loaded = false;
526 * i40e_pf_reset_stats - Reset all of the stats for the given PF
527 * @pf: the PF to be reset
529 void i40e_pf_reset_stats(struct i40e_pf *pf)
533 memset(&pf->stats, 0, sizeof(pf->stats));
534 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
535 pf->stat_offsets_loaded = false;
537 for (i = 0; i < I40E_MAX_VEB; i++) {
539 memset(&pf->veb[i]->stats, 0,
540 sizeof(pf->veb[i]->stats));
541 memset(&pf->veb[i]->stats_offsets, 0,
542 sizeof(pf->veb[i]->stats_offsets));
543 memset(&pf->veb[i]->tc_stats, 0,
544 sizeof(pf->veb[i]->tc_stats));
545 memset(&pf->veb[i]->tc_stats_offsets, 0,
546 sizeof(pf->veb[i]->tc_stats_offsets));
547 pf->veb[i]->stat_offsets_loaded = false;
550 pf->hw_csum_rx_error = 0;
554 * i40e_compute_pci_to_hw_id - compute index form PCI function.
555 * @vsi: ptr to the VSI to read from.
556 * @hw: ptr to the hardware info.
558 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
560 int pf_count = i40e_get_pf_count(hw);
562 if (vsi->type == I40E_VSI_SRIOV)
563 return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
565 return hw->port + BIT(7);
569 * i40e_stat_update64 - read and update a 64 bit stat from the chip.
570 * @hw: ptr to the hardware info.
571 * @hireg: the high 32 bit reg to read.
572 * @loreg: the low 32 bit reg to read.
573 * @offset_loaded: has the initial offset been loaded yet.
574 * @offset: ptr to current offset value.
575 * @stat: ptr to the stat.
577 * Since the device stats are not reset at PFReset, they will not
578 * be zeroed when the driver starts. We'll save the first values read
579 * and use them as offsets to be subtracted from the raw values in order
580 * to report stats that count from zero.
582 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
583 bool offset_loaded, u64 *offset, u64 *stat)
587 new_data = rd64(hw, loreg);
589 if (!offset_loaded || new_data < *offset)
591 *stat = new_data - *offset;
595 * i40e_stat_update48 - read and update a 48 bit stat from the chip
596 * @hw: ptr to the hardware info
597 * @hireg: the high 32 bit reg to read
598 * @loreg: the low 32 bit reg to read
599 * @offset_loaded: has the initial offset been loaded yet
600 * @offset: ptr to current offset value
601 * @stat: ptr to the stat
603 * Since the device stats are not reset at PFReset, they likely will not
604 * be zeroed when the driver starts. We'll save the first values read
605 * and use them as offsets to be subtracted from the raw values in order
606 * to report stats that count from zero. In the process, we also manage
607 * the potential roll-over.
609 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
610 bool offset_loaded, u64 *offset, u64 *stat)
614 if (hw->device_id == I40E_DEV_ID_QEMU) {
615 new_data = rd32(hw, loreg);
616 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
618 new_data = rd64(hw, loreg);
622 if (likely(new_data >= *offset))
623 *stat = new_data - *offset;
625 *stat = (new_data + BIT_ULL(48)) - *offset;
626 *stat &= 0xFFFFFFFFFFFFULL;
630 * i40e_stat_update32 - read and update a 32 bit stat from the chip
631 * @hw: ptr to the hardware info
632 * @reg: the hw reg to read
633 * @offset_loaded: has the initial offset been loaded yet
634 * @offset: ptr to current offset value
635 * @stat: ptr to the stat
637 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
638 bool offset_loaded, u64 *offset, u64 *stat)
642 new_data = rd32(hw, reg);
645 if (likely(new_data >= *offset))
646 *stat = (u32)(new_data - *offset);
648 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
652 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
653 * @hw: ptr to the hardware info
654 * @reg: the hw reg to read and clear
655 * @stat: ptr to the stat
657 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
659 u32 new_data = rd32(hw, reg);
661 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
666 * i40e_stats_update_rx_discards - update rx_discards.
667 * @vsi: ptr to the VSI to be updated.
668 * @hw: ptr to the hardware info.
669 * @stat_idx: VSI's stat_counter_idx.
670 * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
671 * @stat_offset: ptr to stat_offset to store first read of specific register.
672 * @stat: ptr to VSI's stat to be updated.
675 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
676 int stat_idx, bool offset_loaded,
677 struct i40e_eth_stats *stat_offset,
678 struct i40e_eth_stats *stat)
680 u64 rx_rdpc, rx_rxerr;
682 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
683 &stat_offset->rx_discards, &rx_rdpc);
684 i40e_stat_update64(hw,
685 I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
686 I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
687 offset_loaded, &stat_offset->rx_discards_other,
690 stat->rx_discards = rx_rdpc + rx_rxerr;
694 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
695 * @vsi: the VSI to be updated
697 void i40e_update_eth_stats(struct i40e_vsi *vsi)
699 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
700 struct i40e_pf *pf = vsi->back;
701 struct i40e_hw *hw = &pf->hw;
702 struct i40e_eth_stats *oes;
703 struct i40e_eth_stats *es; /* device's eth stats */
705 es = &vsi->eth_stats;
706 oes = &vsi->eth_stats_offsets;
708 /* Gather up the stats that the hw collects */
709 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
710 vsi->stat_offsets_loaded,
711 &oes->tx_errors, &es->tx_errors);
712 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
713 vsi->stat_offsets_loaded,
714 &oes->rx_discards, &es->rx_discards);
715 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
716 vsi->stat_offsets_loaded,
717 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
719 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
720 I40E_GLV_GORCL(stat_idx),
721 vsi->stat_offsets_loaded,
722 &oes->rx_bytes, &es->rx_bytes);
723 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
724 I40E_GLV_UPRCL(stat_idx),
725 vsi->stat_offsets_loaded,
726 &oes->rx_unicast, &es->rx_unicast);
727 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
728 I40E_GLV_MPRCL(stat_idx),
729 vsi->stat_offsets_loaded,
730 &oes->rx_multicast, &es->rx_multicast);
731 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
732 I40E_GLV_BPRCL(stat_idx),
733 vsi->stat_offsets_loaded,
734 &oes->rx_broadcast, &es->rx_broadcast);
736 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
737 I40E_GLV_GOTCL(stat_idx),
738 vsi->stat_offsets_loaded,
739 &oes->tx_bytes, &es->tx_bytes);
740 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
741 I40E_GLV_UPTCL(stat_idx),
742 vsi->stat_offsets_loaded,
743 &oes->tx_unicast, &es->tx_unicast);
744 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
745 I40E_GLV_MPTCL(stat_idx),
746 vsi->stat_offsets_loaded,
747 &oes->tx_multicast, &es->tx_multicast);
748 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
749 I40E_GLV_BPTCL(stat_idx),
750 vsi->stat_offsets_loaded,
751 &oes->tx_broadcast, &es->tx_broadcast);
753 i40e_stats_update_rx_discards(vsi, hw, stat_idx,
754 vsi->stat_offsets_loaded, oes, es);
756 vsi->stat_offsets_loaded = true;
760 * i40e_update_veb_stats - Update Switch component statistics
761 * @veb: the VEB being updated
763 void i40e_update_veb_stats(struct i40e_veb *veb)
765 struct i40e_pf *pf = veb->pf;
766 struct i40e_hw *hw = &pf->hw;
767 struct i40e_eth_stats *oes;
768 struct i40e_eth_stats *es; /* device's eth stats */
769 struct i40e_veb_tc_stats *veb_oes;
770 struct i40e_veb_tc_stats *veb_es;
773 idx = veb->stats_idx;
775 oes = &veb->stats_offsets;
776 veb_es = &veb->tc_stats;
777 veb_oes = &veb->tc_stats_offsets;
779 /* Gather up the stats that the hw collects */
780 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
781 veb->stat_offsets_loaded,
782 &oes->tx_discards, &es->tx_discards);
783 if (hw->revision_id > 0)
784 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
785 veb->stat_offsets_loaded,
786 &oes->rx_unknown_protocol,
787 &es->rx_unknown_protocol);
788 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
789 veb->stat_offsets_loaded,
790 &oes->rx_bytes, &es->rx_bytes);
791 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
792 veb->stat_offsets_loaded,
793 &oes->rx_unicast, &es->rx_unicast);
794 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
795 veb->stat_offsets_loaded,
796 &oes->rx_multicast, &es->rx_multicast);
797 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
798 veb->stat_offsets_loaded,
799 &oes->rx_broadcast, &es->rx_broadcast);
801 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
802 veb->stat_offsets_loaded,
803 &oes->tx_bytes, &es->tx_bytes);
804 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
805 veb->stat_offsets_loaded,
806 &oes->tx_unicast, &es->tx_unicast);
807 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
808 veb->stat_offsets_loaded,
809 &oes->tx_multicast, &es->tx_multicast);
810 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
811 veb->stat_offsets_loaded,
812 &oes->tx_broadcast, &es->tx_broadcast);
813 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
814 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
815 I40E_GLVEBTC_RPCL(i, idx),
816 veb->stat_offsets_loaded,
817 &veb_oes->tc_rx_packets[i],
818 &veb_es->tc_rx_packets[i]);
819 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
820 I40E_GLVEBTC_RBCL(i, idx),
821 veb->stat_offsets_loaded,
822 &veb_oes->tc_rx_bytes[i],
823 &veb_es->tc_rx_bytes[i]);
824 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
825 I40E_GLVEBTC_TPCL(i, idx),
826 veb->stat_offsets_loaded,
827 &veb_oes->tc_tx_packets[i],
828 &veb_es->tc_tx_packets[i]);
829 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
830 I40E_GLVEBTC_TBCL(i, idx),
831 veb->stat_offsets_loaded,
832 &veb_oes->tc_tx_bytes[i],
833 &veb_es->tc_tx_bytes[i]);
835 veb->stat_offsets_loaded = true;
839 * i40e_update_vsi_stats - Update the vsi statistics counters.
840 * @vsi: the VSI to be updated
842 * There are a few instances where we store the same stat in a
843 * couple of different structs. This is partly because we have
844 * the netdev stats that need to be filled out, which is slightly
845 * different from the "eth_stats" defined by the chip and used in
846 * VF communications. We sort it out here.
848 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
850 u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
851 struct i40e_pf *pf = vsi->back;
852 struct rtnl_link_stats64 *ons;
853 struct rtnl_link_stats64 *ns; /* netdev stats */
854 struct i40e_eth_stats *oes;
855 struct i40e_eth_stats *es; /* device's eth stats */
856 u64 tx_restart, tx_busy;
867 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
868 test_bit(__I40E_CONFIG_BUSY, pf->state))
871 ns = i40e_get_vsi_stats_struct(vsi);
872 ons = &vsi->net_stats_offsets;
873 es = &vsi->eth_stats;
874 oes = &vsi->eth_stats_offsets;
876 /* Gather up the netdev and vsi stats that the driver collects
877 * on the fly during packet processing
881 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
890 for (q = 0; q < vsi->num_queue_pairs; q++) {
892 p = READ_ONCE(vsi->tx_rings[q]);
897 start = u64_stats_fetch_begin_irq(&p->syncp);
898 packets = p->stats.packets;
899 bytes = p->stats.bytes;
900 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
903 tx_restart += p->tx_stats.restart_queue;
904 tx_busy += p->tx_stats.tx_busy;
905 tx_linearize += p->tx_stats.tx_linearize;
906 tx_force_wb += p->tx_stats.tx_force_wb;
907 tx_stopped += p->tx_stats.tx_stopped;
910 p = READ_ONCE(vsi->rx_rings[q]);
915 start = u64_stats_fetch_begin_irq(&p->syncp);
916 packets = p->stats.packets;
917 bytes = p->stats.bytes;
918 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
921 rx_buf += p->rx_stats.alloc_buff_failed;
922 rx_page += p->rx_stats.alloc_page_failed;
923 rx_reuse += p->rx_stats.page_reuse_count;
924 rx_alloc += p->rx_stats.page_alloc_count;
925 rx_waive += p->rx_stats.page_waive_count;
926 rx_busy += p->rx_stats.page_busy_count;
928 if (i40e_enabled_xdp_vsi(vsi)) {
929 /* locate XDP ring */
930 p = READ_ONCE(vsi->xdp_rings[q]);
935 start = u64_stats_fetch_begin_irq(&p->syncp);
936 packets = p->stats.packets;
937 bytes = p->stats.bytes;
938 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
941 tx_restart += p->tx_stats.restart_queue;
942 tx_busy += p->tx_stats.tx_busy;
943 tx_linearize += p->tx_stats.tx_linearize;
944 tx_force_wb += p->tx_stats.tx_force_wb;
948 vsi->tx_restart = tx_restart;
949 vsi->tx_busy = tx_busy;
950 vsi->tx_linearize = tx_linearize;
951 vsi->tx_force_wb = tx_force_wb;
952 vsi->tx_stopped = tx_stopped;
953 vsi->rx_page_failed = rx_page;
954 vsi->rx_buf_failed = rx_buf;
955 vsi->rx_page_reuse = rx_reuse;
956 vsi->rx_page_alloc = rx_alloc;
957 vsi->rx_page_waive = rx_waive;
958 vsi->rx_page_busy = rx_busy;
960 ns->rx_packets = rx_p;
962 ns->tx_packets = tx_p;
965 /* update netdev stats from eth stats */
966 i40e_update_eth_stats(vsi);
967 ons->tx_errors = oes->tx_errors;
968 ns->tx_errors = es->tx_errors;
969 ons->multicast = oes->rx_multicast;
970 ns->multicast = es->rx_multicast;
971 ons->rx_dropped = oes->rx_discards;
972 ns->rx_dropped = es->rx_discards;
973 ons->tx_dropped = oes->tx_discards;
974 ns->tx_dropped = es->tx_discards;
976 /* pull in a couple PF stats if this is the main vsi */
977 if (vsi == pf->vsi[pf->lan_vsi]) {
978 ns->rx_crc_errors = pf->stats.crc_errors;
979 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
980 ns->rx_length_errors = pf->stats.rx_length_errors;
985 * i40e_update_pf_stats - Update the PF statistics counters.
986 * @pf: the PF to be updated
988 static void i40e_update_pf_stats(struct i40e_pf *pf)
990 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
991 struct i40e_hw_port_stats *nsd = &pf->stats;
992 struct i40e_hw *hw = &pf->hw;
996 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
997 I40E_GLPRT_GORCL(hw->port),
998 pf->stat_offsets_loaded,
999 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1000 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1001 I40E_GLPRT_GOTCL(hw->port),
1002 pf->stat_offsets_loaded,
1003 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1004 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1005 pf->stat_offsets_loaded,
1006 &osd->eth.rx_discards,
1007 &nsd->eth.rx_discards);
1008 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1009 I40E_GLPRT_UPRCL(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->eth.rx_unicast,
1012 &nsd->eth.rx_unicast);
1013 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1014 I40E_GLPRT_MPRCL(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->eth.rx_multicast,
1017 &nsd->eth.rx_multicast);
1018 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1019 I40E_GLPRT_BPRCL(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->eth.rx_broadcast,
1022 &nsd->eth.rx_broadcast);
1023 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1024 I40E_GLPRT_UPTCL(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->eth.tx_unicast,
1027 &nsd->eth.tx_unicast);
1028 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1029 I40E_GLPRT_MPTCL(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->eth.tx_multicast,
1032 &nsd->eth.tx_multicast);
1033 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1034 I40E_GLPRT_BPTCL(hw->port),
1035 pf->stat_offsets_loaded,
1036 &osd->eth.tx_broadcast,
1037 &nsd->eth.tx_broadcast);
1039 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->tx_dropped_link_down,
1042 &nsd->tx_dropped_link_down);
1044 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1045 pf->stat_offsets_loaded,
1046 &osd->crc_errors, &nsd->crc_errors);
1048 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1049 pf->stat_offsets_loaded,
1050 &osd->illegal_bytes, &nsd->illegal_bytes);
1052 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->mac_local_faults,
1055 &nsd->mac_local_faults);
1056 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1057 pf->stat_offsets_loaded,
1058 &osd->mac_remote_faults,
1059 &nsd->mac_remote_faults);
1061 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->rx_length_errors,
1064 &nsd->rx_length_errors);
1066 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1067 pf->stat_offsets_loaded,
1068 &osd->link_xon_rx, &nsd->link_xon_rx);
1069 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->link_xon_tx, &nsd->link_xon_tx);
1072 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->link_xoff_rx, &nsd->link_xoff_rx);
1075 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->link_xoff_tx, &nsd->link_xoff_tx);
1079 for (i = 0; i < 8; i++) {
1080 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1081 pf->stat_offsets_loaded,
1082 &osd->priority_xoff_rx[i],
1083 &nsd->priority_xoff_rx[i]);
1084 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1085 pf->stat_offsets_loaded,
1086 &osd->priority_xon_rx[i],
1087 &nsd->priority_xon_rx[i]);
1088 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1089 pf->stat_offsets_loaded,
1090 &osd->priority_xon_tx[i],
1091 &nsd->priority_xon_tx[i]);
1092 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1093 pf->stat_offsets_loaded,
1094 &osd->priority_xoff_tx[i],
1095 &nsd->priority_xoff_tx[i]);
1096 i40e_stat_update32(hw,
1097 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1098 pf->stat_offsets_loaded,
1099 &osd->priority_xon_2_xoff[i],
1100 &nsd->priority_xon_2_xoff[i]);
1103 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1104 I40E_GLPRT_PRC64L(hw->port),
1105 pf->stat_offsets_loaded,
1106 &osd->rx_size_64, &nsd->rx_size_64);
1107 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1108 I40E_GLPRT_PRC127L(hw->port),
1109 pf->stat_offsets_loaded,
1110 &osd->rx_size_127, &nsd->rx_size_127);
1111 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1112 I40E_GLPRT_PRC255L(hw->port),
1113 pf->stat_offsets_loaded,
1114 &osd->rx_size_255, &nsd->rx_size_255);
1115 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1116 I40E_GLPRT_PRC511L(hw->port),
1117 pf->stat_offsets_loaded,
1118 &osd->rx_size_511, &nsd->rx_size_511);
1119 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1120 I40E_GLPRT_PRC1023L(hw->port),
1121 pf->stat_offsets_loaded,
1122 &osd->rx_size_1023, &nsd->rx_size_1023);
1123 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1124 I40E_GLPRT_PRC1522L(hw->port),
1125 pf->stat_offsets_loaded,
1126 &osd->rx_size_1522, &nsd->rx_size_1522);
1127 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1128 I40E_GLPRT_PRC9522L(hw->port),
1129 pf->stat_offsets_loaded,
1130 &osd->rx_size_big, &nsd->rx_size_big);
1132 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1133 I40E_GLPRT_PTC64L(hw->port),
1134 pf->stat_offsets_loaded,
1135 &osd->tx_size_64, &nsd->tx_size_64);
1136 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1137 I40E_GLPRT_PTC127L(hw->port),
1138 pf->stat_offsets_loaded,
1139 &osd->tx_size_127, &nsd->tx_size_127);
1140 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1141 I40E_GLPRT_PTC255L(hw->port),
1142 pf->stat_offsets_loaded,
1143 &osd->tx_size_255, &nsd->tx_size_255);
1144 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1145 I40E_GLPRT_PTC511L(hw->port),
1146 pf->stat_offsets_loaded,
1147 &osd->tx_size_511, &nsd->tx_size_511);
1148 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1149 I40E_GLPRT_PTC1023L(hw->port),
1150 pf->stat_offsets_loaded,
1151 &osd->tx_size_1023, &nsd->tx_size_1023);
1152 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1153 I40E_GLPRT_PTC1522L(hw->port),
1154 pf->stat_offsets_loaded,
1155 &osd->tx_size_1522, &nsd->tx_size_1522);
1156 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1157 I40E_GLPRT_PTC9522L(hw->port),
1158 pf->stat_offsets_loaded,
1159 &osd->tx_size_big, &nsd->tx_size_big);
1161 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1162 pf->stat_offsets_loaded,
1163 &osd->rx_undersize, &nsd->rx_undersize);
1164 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1165 pf->stat_offsets_loaded,
1166 &osd->rx_fragments, &nsd->rx_fragments);
1167 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1168 pf->stat_offsets_loaded,
1169 &osd->rx_oversize, &nsd->rx_oversize);
1170 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1171 pf->stat_offsets_loaded,
1172 &osd->rx_jabber, &nsd->rx_jabber);
1175 i40e_stat_update_and_clear32(hw,
1176 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1177 &nsd->fd_atr_match);
1178 i40e_stat_update_and_clear32(hw,
1179 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1181 i40e_stat_update_and_clear32(hw,
1182 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1183 &nsd->fd_atr_tunnel_match);
1185 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1186 nsd->tx_lpi_status =
1187 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1188 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1189 nsd->rx_lpi_status =
1190 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1191 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1192 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1193 pf->stat_offsets_loaded,
1194 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1195 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1196 pf->stat_offsets_loaded,
1197 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1199 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1200 !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1201 nsd->fd_sb_status = true;
1203 nsd->fd_sb_status = false;
1205 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1206 !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1207 nsd->fd_atr_status = true;
1209 nsd->fd_atr_status = false;
1211 pf->stat_offsets_loaded = true;
1215 * i40e_update_stats - Update the various statistics counters.
1216 * @vsi: the VSI to be updated
1218 * Update the various stats for this VSI and its related entities.
1220 void i40e_update_stats(struct i40e_vsi *vsi)
1222 struct i40e_pf *pf = vsi->back;
1224 if (vsi == pf->vsi[pf->lan_vsi])
1225 i40e_update_pf_stats(pf);
1227 i40e_update_vsi_stats(vsi);
1231 * i40e_count_filters - counts VSI mac filters
1232 * @vsi: the VSI to be searched
1234 * Returns count of mac filters
1236 int i40e_count_filters(struct i40e_vsi *vsi)
1238 struct i40e_mac_filter *f;
1239 struct hlist_node *h;
1243 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1250 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1251 * @vsi: the VSI to be searched
1252 * @macaddr: the MAC address
1255 * Returns ptr to the filter object or NULL
1257 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1258 const u8 *macaddr, s16 vlan)
1260 struct i40e_mac_filter *f;
1263 if (!vsi || !macaddr)
1266 key = i40e_addr_to_hkey(macaddr);
1267 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1268 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1276 * i40e_find_mac - Find a mac addr in the macvlan filters list
1277 * @vsi: the VSI to be searched
1278 * @macaddr: the MAC address we are searching for
1280 * Returns the first filter with the provided MAC address or NULL if
1281 * MAC address was not found
1283 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1285 struct i40e_mac_filter *f;
1288 if (!vsi || !macaddr)
1291 key = i40e_addr_to_hkey(macaddr);
1292 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1293 if ((ether_addr_equal(macaddr, f->macaddr)))
1300 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1301 * @vsi: the VSI to be searched
1303 * Returns true if VSI is in vlan mode or false otherwise
1305 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1307 /* If we have a PVID, always operate in VLAN mode */
1311 /* We need to operate in VLAN mode whenever we have any filters with
1312 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1313 * time, incurring search cost repeatedly. However, we can notice two
1316 * 1) the only place where we can gain a VLAN filter is in
1319 * 2) the only place where filters are actually removed is in
1320 * i40e_sync_filters_subtask.
1322 * Thus, we can simply use a boolean value, has_vlan_filters which we
1323 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1324 * we have to perform the full search after deleting filters in
1325 * i40e_sync_filters_subtask, but we already have to search
1326 * filters here and can perform the check at the same time. This
1327 * results in avoiding embedding a loop for VLAN mode inside another
1328 * loop over all the filters, and should maintain correctness as noted
1331 return vsi->has_vlan_filter;
1335 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1336 * @vsi: the VSI to configure
1337 * @tmp_add_list: list of filters ready to be added
1338 * @tmp_del_list: list of filters ready to be deleted
1339 * @vlan_filters: the number of active VLAN filters
1341 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1342 * behave as expected. If we have any active VLAN filters remaining or about
1343 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1344 * so that they only match against untagged traffic. If we no longer have any
1345 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1346 * so that they match against both tagged and untagged traffic. In this way,
1347 * we ensure that we correctly receive the desired traffic. This ensures that
1348 * when we have an active VLAN we will receive only untagged traffic and
1349 * traffic matching active VLANs. If we have no active VLANs then we will
1350 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1352 * Finally, in a similar fashion, this function also corrects filters when
1353 * there is an active PVID assigned to this VSI.
1355 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1357 * This function is only expected to be called from within
1358 * i40e_sync_vsi_filters.
1360 * NOTE: This function expects to be called while under the
1361 * mac_filter_hash_lock
1363 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1364 struct hlist_head *tmp_add_list,
1365 struct hlist_head *tmp_del_list,
1368 s16 pvid = le16_to_cpu(vsi->info.pvid);
1369 struct i40e_mac_filter *f, *add_head;
1370 struct i40e_new_mac_filter *new;
1371 struct hlist_node *h;
1374 /* To determine if a particular filter needs to be replaced we
1375 * have the three following conditions:
1377 * a) if we have a PVID assigned, then all filters which are
1378 * not marked as VLAN=PVID must be replaced with filters that
1380 * b) otherwise, if we have any active VLANS, all filters
1381 * which are marked as VLAN=-1 must be replaced with
1382 * filters marked as VLAN=0
1383 * c) finally, if we do not have any active VLANS, all filters
1384 * which are marked as VLAN=0 must be replaced with filters
1388 /* Update the filters about to be added in place */
1389 hlist_for_each_entry(new, tmp_add_list, hlist) {
1390 if (pvid && new->f->vlan != pvid)
1391 new->f->vlan = pvid;
1392 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1394 else if (!vlan_filters && new->f->vlan == 0)
1395 new->f->vlan = I40E_VLAN_ANY;
1398 /* Update the remaining active filters */
1399 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1400 /* Combine the checks for whether a filter needs to be changed
1401 * and then determine the new VLAN inside the if block, in
1402 * order to avoid duplicating code for adding the new filter
1403 * then deleting the old filter.
1405 if ((pvid && f->vlan != pvid) ||
1406 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1407 (!vlan_filters && f->vlan == 0)) {
1408 /* Determine the new vlan we will be adding */
1411 else if (vlan_filters)
1414 new_vlan = I40E_VLAN_ANY;
1416 /* Create the new filter */
1417 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1421 /* Create a temporary i40e_new_mac_filter */
1422 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1427 new->state = add_head->state;
1429 /* Add the new filter to the tmp list */
1430 hlist_add_head(&new->hlist, tmp_add_list);
1432 /* Put the original filter into the delete list */
1433 f->state = I40E_FILTER_REMOVE;
1434 hash_del(&f->hlist);
1435 hlist_add_head(&f->hlist, tmp_del_list);
1439 vsi->has_vlan_filter = !!vlan_filters;
1445 * i40e_get_vf_new_vlan - Get new vlan id on a vf
1446 * @vsi: the vsi to configure
1447 * @new_mac: new mac filter to be added
1448 * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1449 * @vlan_filters: the number of active VLAN filters
1450 * @trusted: flag if the VF is trusted
1452 * Get new VLAN id based on current VLAN filters, trust, PVID
1453 * and vf-vlan-prune-disable flag.
1455 * Returns the value of the new vlan filter or
1456 * the old value if no new filter is needed.
1458 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1459 struct i40e_new_mac_filter *new_mac,
1460 struct i40e_mac_filter *f,
1464 s16 pvid = le16_to_cpu(vsi->info.pvid);
1465 struct i40e_pf *pf = vsi->back;
1471 if (pvid && f->vlan != pvid)
1474 is_any = (trusted ||
1475 !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1477 if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1478 (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1479 (is_any && !vlan_filters && f->vlan == 0)) {
1481 return I40E_VLAN_ANY;
1490 * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1491 * @vsi: the vsi to configure
1492 * @tmp_add_list: list of filters ready to be added
1493 * @tmp_del_list: list of filters ready to be deleted
1494 * @vlan_filters: the number of active VLAN filters
1495 * @trusted: flag if the VF is trusted
1497 * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1498 * and vf-vlan-prune-disable flag.
1500 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1502 * This function is only expected to be called from within
1503 * i40e_sync_vsi_filters.
1505 * NOTE: This function expects to be called while under the
1506 * mac_filter_hash_lock
1508 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1509 struct hlist_head *tmp_add_list,
1510 struct hlist_head *tmp_del_list,
1514 struct i40e_mac_filter *f, *add_head;
1515 struct i40e_new_mac_filter *new_mac;
1516 struct hlist_node *h;
1519 hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1520 new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1521 vlan_filters, trusted);
1524 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1525 new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1527 if (new_vlan != f->vlan) {
1528 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1531 /* Create a temporary i40e_new_mac_filter */
1532 new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1535 new_mac->f = add_head;
1536 new_mac->state = add_head->state;
1538 /* Add the new filter to the tmp list */
1539 hlist_add_head(&new_mac->hlist, tmp_add_list);
1541 /* Put the original filter into the delete list */
1542 f->state = I40E_FILTER_REMOVE;
1543 hash_del(&f->hlist);
1544 hlist_add_head(&f->hlist, tmp_del_list);
1548 vsi->has_vlan_filter = !!vlan_filters;
1553 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1554 * @vsi: the PF Main VSI - inappropriate for any other VSI
1555 * @macaddr: the MAC address
1557 * Remove whatever filter the firmware set up so the driver can manage
1558 * its own filtering intelligently.
1560 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1562 struct i40e_aqc_remove_macvlan_element_data element;
1563 struct i40e_pf *pf = vsi->back;
1565 /* Only appropriate for the PF main VSI */
1566 if (vsi->type != I40E_VSI_MAIN)
1569 memset(&element, 0, sizeof(element));
1570 ether_addr_copy(element.mac_addr, macaddr);
1571 element.vlan_tag = 0;
1572 /* Ignore error returns, some firmware does it this way... */
1573 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1574 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1576 memset(&element, 0, sizeof(element));
1577 ether_addr_copy(element.mac_addr, macaddr);
1578 element.vlan_tag = 0;
1579 /* ...and some firmware does it this way. */
1580 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1581 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1582 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1586 * i40e_add_filter - Add a mac/vlan filter to the VSI
1587 * @vsi: the VSI to be searched
1588 * @macaddr: the MAC address
1591 * Returns ptr to the filter object or NULL when no memory available.
1593 * NOTE: This function is expected to be called with mac_filter_hash_lock
1596 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1597 const u8 *macaddr, s16 vlan)
1599 struct i40e_mac_filter *f;
1602 if (!vsi || !macaddr)
1605 f = i40e_find_filter(vsi, macaddr, vlan);
1607 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1611 /* Update the boolean indicating if we need to function in
1615 vsi->has_vlan_filter = true;
1617 ether_addr_copy(f->macaddr, macaddr);
1619 f->state = I40E_FILTER_NEW;
1620 INIT_HLIST_NODE(&f->hlist);
1622 key = i40e_addr_to_hkey(macaddr);
1623 hash_add(vsi->mac_filter_hash, &f->hlist, key);
1625 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1626 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1629 /* If we're asked to add a filter that has been marked for removal, it
1630 * is safe to simply restore it to active state. __i40e_del_filter
1631 * will have simply deleted any filters which were previously marked
1632 * NEW or FAILED, so if it is currently marked REMOVE it must have
1633 * previously been ACTIVE. Since we haven't yet run the sync filters
1634 * task, just restore this filter to the ACTIVE state so that the
1635 * sync task leaves it in place
1637 if (f->state == I40E_FILTER_REMOVE)
1638 f->state = I40E_FILTER_ACTIVE;
1644 * __i40e_del_filter - Remove a specific filter from the VSI
1645 * @vsi: VSI to remove from
1646 * @f: the filter to remove from the list
1648 * This function should be called instead of i40e_del_filter only if you know
1649 * the exact filter you will remove already, such as via i40e_find_filter or
1652 * NOTE: This function is expected to be called with mac_filter_hash_lock
1654 * ANOTHER NOTE: This function MUST be called from within the context of
1655 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1656 * instead of list_for_each_entry().
1658 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1663 /* If the filter was never added to firmware then we can just delete it
1664 * directly and we don't want to set the status to remove or else an
1665 * admin queue command will unnecessarily fire.
1667 if ((f->state == I40E_FILTER_FAILED) ||
1668 (f->state == I40E_FILTER_NEW)) {
1669 hash_del(&f->hlist);
1672 f->state = I40E_FILTER_REMOVE;
1675 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1676 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1680 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1681 * @vsi: the VSI to be searched
1682 * @macaddr: the MAC address
1685 * NOTE: This function is expected to be called with mac_filter_hash_lock
1687 * ANOTHER NOTE: This function MUST be called from within the context of
1688 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1689 * instead of list_for_each_entry().
1691 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1693 struct i40e_mac_filter *f;
1695 if (!vsi || !macaddr)
1698 f = i40e_find_filter(vsi, macaddr, vlan);
1699 __i40e_del_filter(vsi, f);
1703 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1704 * @vsi: the VSI to be searched
1705 * @macaddr: the mac address to be filtered
1707 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1708 * go through all the macvlan filters and add a macvlan filter for each
1709 * unique vlan that already exists. If a PVID has been assigned, instead only
1710 * add the macaddr to that VLAN.
1712 * Returns last filter added on success, else NULL
1714 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1717 struct i40e_mac_filter *f, *add = NULL;
1718 struct hlist_node *h;
1722 return i40e_add_filter(vsi, macaddr,
1723 le16_to_cpu(vsi->info.pvid));
1725 if (!i40e_is_vsi_in_vlan(vsi))
1726 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1728 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1729 if (f->state == I40E_FILTER_REMOVE)
1731 add = i40e_add_filter(vsi, macaddr, f->vlan);
1740 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1741 * @vsi: the VSI to be searched
1742 * @macaddr: the mac address to be removed
1744 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1747 * Returns 0 for success, or error
1749 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1751 struct i40e_mac_filter *f;
1752 struct hlist_node *h;
1756 lockdep_assert_held(&vsi->mac_filter_hash_lock);
1757 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1758 if (ether_addr_equal(macaddr, f->macaddr)) {
1759 __i40e_del_filter(vsi, f);
1771 * i40e_set_mac - NDO callback to set mac address
1772 * @netdev: network interface device structure
1773 * @p: pointer to an address structure
1775 * Returns 0 on success, negative on failure
1777 static int i40e_set_mac(struct net_device *netdev, void *p)
1779 struct i40e_netdev_priv *np = netdev_priv(netdev);
1780 struct i40e_vsi *vsi = np->vsi;
1781 struct i40e_pf *pf = vsi->back;
1782 struct i40e_hw *hw = &pf->hw;
1783 struct sockaddr *addr = p;
1785 if (!is_valid_ether_addr(addr->sa_data))
1786 return -EADDRNOTAVAIL;
1788 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1789 netdev_info(netdev, "already using mac address %pM\n",
1794 if (test_bit(__I40E_DOWN, pf->state) ||
1795 test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1796 return -EADDRNOTAVAIL;
1798 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1799 netdev_info(netdev, "returning to hw mac address %pM\n",
1802 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1804 /* Copy the address first, so that we avoid a possible race with
1806 * - Remove old address from MAC filter
1807 * - Copy new address
1808 * - Add new address to MAC filter
1810 spin_lock_bh(&vsi->mac_filter_hash_lock);
1811 i40e_del_mac_filter(vsi, netdev->dev_addr);
1812 eth_hw_addr_set(netdev, addr->sa_data);
1813 i40e_add_mac_filter(vsi, netdev->dev_addr);
1814 spin_unlock_bh(&vsi->mac_filter_hash_lock);
1816 if (vsi->type == I40E_VSI_MAIN) {
1819 ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1820 addr->sa_data, NULL);
1822 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1823 i40e_stat_str(hw, ret),
1824 i40e_aq_str(hw, hw->aq.asq_last_status));
1827 /* schedule our worker thread which will take care of
1828 * applying the new filter changes
1830 i40e_service_event_schedule(pf);
1835 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1836 * @vsi: vsi structure
1837 * @seed: RSS hash seed
1838 * @lut: pointer to lookup table of lut_size
1839 * @lut_size: size of the lookup table
1841 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1842 u8 *lut, u16 lut_size)
1844 struct i40e_pf *pf = vsi->back;
1845 struct i40e_hw *hw = &pf->hw;
1849 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1850 (struct i40e_aqc_get_set_rss_key_data *)seed;
1851 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1853 dev_info(&pf->pdev->dev,
1854 "Cannot set RSS key, err %s aq_err %s\n",
1855 i40e_stat_str(hw, ret),
1856 i40e_aq_str(hw, hw->aq.asq_last_status));
1861 bool pf_lut = vsi->type == I40E_VSI_MAIN;
1863 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1865 dev_info(&pf->pdev->dev,
1866 "Cannot set RSS lut, err %s aq_err %s\n",
1867 i40e_stat_str(hw, ret),
1868 i40e_aq_str(hw, hw->aq.asq_last_status));
1876 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1877 * @vsi: VSI structure
1879 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1881 struct i40e_pf *pf = vsi->back;
1882 u8 seed[I40E_HKEY_ARRAY_SIZE];
1886 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1889 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1890 vsi->num_queue_pairs);
1893 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1897 /* Use the user configured hash keys and lookup table if there is one,
1898 * otherwise use default
1900 if (vsi->rss_lut_user)
1901 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1903 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1904 if (vsi->rss_hkey_user)
1905 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1907 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1908 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1914 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1915 * @vsi: the VSI being configured,
1916 * @ctxt: VSI context structure
1917 * @enabled_tc: number of traffic classes to enable
1919 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1921 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1922 struct i40e_vsi_context *ctxt,
1925 u16 qcount = 0, max_qcount, qmap, sections = 0;
1926 int i, override_q, pow, num_qps, ret;
1927 u8 netdev_tc = 0, offset = 0;
1929 if (vsi->type != I40E_VSI_MAIN)
1931 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1932 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1933 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1934 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1935 num_qps = vsi->mqprio_qopt.qopt.count[0];
1937 /* find the next higher power-of-2 of num queue pairs */
1938 pow = ilog2(num_qps);
1939 if (!is_power_of_2(num_qps))
1941 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1942 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1944 /* Setup queue offset/count for all TCs for given VSI */
1945 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1946 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1947 /* See if the given TC is enabled for the given VSI */
1948 if (vsi->tc_config.enabled_tc & BIT(i)) {
1949 offset = vsi->mqprio_qopt.qopt.offset[i];
1950 qcount = vsi->mqprio_qopt.qopt.count[i];
1951 if (qcount > max_qcount)
1952 max_qcount = qcount;
1953 vsi->tc_config.tc_info[i].qoffset = offset;
1954 vsi->tc_config.tc_info[i].qcount = qcount;
1955 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1957 /* TC is not enabled so set the offset to
1958 * default queue and allocate one queue
1961 vsi->tc_config.tc_info[i].qoffset = 0;
1962 vsi->tc_config.tc_info[i].qcount = 1;
1963 vsi->tc_config.tc_info[i].netdev_tc = 0;
1967 /* Set actual Tx/Rx queue pairs */
1968 vsi->num_queue_pairs = offset + qcount;
1970 /* Setup queue TC[0].qmap for given VSI context */
1971 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1972 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1973 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1974 ctxt->info.valid_sections |= cpu_to_le16(sections);
1976 /* Reconfigure RSS for main VSI with max queue count */
1977 vsi->rss_size = max_qcount;
1978 ret = i40e_vsi_config_rss(vsi);
1980 dev_info(&vsi->back->pdev->dev,
1981 "Failed to reconfig rss for num_queues (%u)\n",
1985 vsi->reconfig_rss = true;
1986 dev_dbg(&vsi->back->pdev->dev,
1987 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1989 /* Find queue count available for channel VSIs and starting offset
1992 override_q = vsi->mqprio_qopt.qopt.count[0];
1993 if (override_q && override_q < vsi->num_queue_pairs) {
1994 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1995 vsi->next_base_queue = override_q;
2001 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2002 * @vsi: the VSI being setup
2003 * @ctxt: VSI context structure
2004 * @enabled_tc: Enabled TCs bitmap
2005 * @is_add: True if called before Add VSI
2007 * Setup VSI queue mapping for enabled traffic classes.
2009 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2010 struct i40e_vsi_context *ctxt,
2014 struct i40e_pf *pf = vsi->back;
2024 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2026 /* zero out queue mapping, it will get updated on the end of the function */
2027 memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2029 if (vsi->type == I40E_VSI_MAIN) {
2030 /* This code helps add more queue to the VSI if we have
2031 * more cores than RSS can support, the higher cores will
2032 * be served by ATR or other filters. Furthermore, the
2033 * non-zero req_queue_pairs says that user requested a new
2034 * queue count via ethtool's set_channels, so use this
2035 * value for queues distribution across traffic classes
2036 * We need at least one queue pair for the interface
2037 * to be usable as we see in else statement.
2039 if (vsi->req_queue_pairs > 0)
2040 vsi->num_queue_pairs = vsi->req_queue_pairs;
2041 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2042 vsi->num_queue_pairs = pf->num_lan_msix;
2044 vsi->num_queue_pairs = 1;
2047 /* Number of queues per enabled TC */
2048 if (vsi->type == I40E_VSI_MAIN ||
2049 (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2050 num_tc_qps = vsi->num_queue_pairs;
2052 num_tc_qps = vsi->alloc_queue_pairs;
2054 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2055 /* Find numtc from enabled TC bitmap */
2056 for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2057 if (enabled_tc & BIT(i)) /* TC is enabled */
2061 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2064 num_tc_qps = num_tc_qps / numtc;
2065 num_tc_qps = min_t(int, num_tc_qps,
2066 i40e_pf_get_max_q_per_tc(pf));
2069 vsi->tc_config.numtc = numtc;
2070 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2072 /* Do not allow use more TC queue pairs than MSI-X vectors exist */
2073 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2074 num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2076 /* Setup queue offset/count for all TCs for given VSI */
2077 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2078 /* See if the given TC is enabled for the given VSI */
2079 if (vsi->tc_config.enabled_tc & BIT(i)) {
2083 switch (vsi->type) {
2085 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2086 I40E_FLAG_FD_ATR_ENABLED)) ||
2087 vsi->tc_config.enabled_tc != 1) {
2088 qcount = min_t(int, pf->alloc_rss_size,
2094 case I40E_VSI_SRIOV:
2095 case I40E_VSI_VMDQ2:
2097 qcount = num_tc_qps;
2101 vsi->tc_config.tc_info[i].qoffset = offset;
2102 vsi->tc_config.tc_info[i].qcount = qcount;
2104 /* find the next higher power-of-2 of num queue pairs */
2107 while (num_qps && (BIT_ULL(pow) < qcount)) {
2112 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2114 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2115 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2119 /* TC is not enabled so set the offset to
2120 * default queue and allocate one queue
2123 vsi->tc_config.tc_info[i].qoffset = 0;
2124 vsi->tc_config.tc_info[i].qcount = 1;
2125 vsi->tc_config.tc_info[i].netdev_tc = 0;
2129 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2131 /* Do not change previously set num_queue_pairs for PFs and VFs*/
2132 if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2133 (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2134 (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2135 vsi->num_queue_pairs = offset;
2137 /* Scheduler section valid can only be set for ADD VSI */
2139 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2141 ctxt->info.up_enable_bits = enabled_tc;
2143 if (vsi->type == I40E_VSI_SRIOV) {
2144 ctxt->info.mapping_flags |=
2145 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2146 for (i = 0; i < vsi->num_queue_pairs; i++)
2147 ctxt->info.queue_mapping[i] =
2148 cpu_to_le16(vsi->base_queue + i);
2150 ctxt->info.mapping_flags |=
2151 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2152 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2154 ctxt->info.valid_sections |= cpu_to_le16(sections);
2158 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2159 * @netdev: the netdevice
2160 * @addr: address to add
2162 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2163 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2165 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2167 struct i40e_netdev_priv *np = netdev_priv(netdev);
2168 struct i40e_vsi *vsi = np->vsi;
2170 if (i40e_add_mac_filter(vsi, addr))
2177 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2178 * @netdev: the netdevice
2179 * @addr: address to add
2181 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2182 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2184 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2186 struct i40e_netdev_priv *np = netdev_priv(netdev);
2187 struct i40e_vsi *vsi = np->vsi;
2189 /* Under some circumstances, we might receive a request to delete
2190 * our own device address from our uc list. Because we store the
2191 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2192 * such requests and not delete our device address from this list.
2194 if (ether_addr_equal(addr, netdev->dev_addr))
2197 i40e_del_mac_filter(vsi, addr);
2203 * i40e_set_rx_mode - NDO callback to set the netdev filters
2204 * @netdev: network interface device structure
2206 static void i40e_set_rx_mode(struct net_device *netdev)
2208 struct i40e_netdev_priv *np = netdev_priv(netdev);
2209 struct i40e_vsi *vsi = np->vsi;
2211 spin_lock_bh(&vsi->mac_filter_hash_lock);
2213 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2214 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2216 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2218 /* check for other flag changes */
2219 if (vsi->current_netdev_flags != vsi->netdev->flags) {
2220 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2221 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2226 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2227 * @vsi: Pointer to VSI struct
2228 * @from: Pointer to list which contains MAC filter entries - changes to
2229 * those entries needs to be undone.
2231 * MAC filter entries from this list were slated for deletion.
2233 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2234 struct hlist_head *from)
2236 struct i40e_mac_filter *f;
2237 struct hlist_node *h;
2239 hlist_for_each_entry_safe(f, h, from, hlist) {
2240 u64 key = i40e_addr_to_hkey(f->macaddr);
2242 /* Move the element back into MAC filter list*/
2243 hlist_del(&f->hlist);
2244 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2249 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2250 * @vsi: Pointer to vsi struct
2251 * @from: Pointer to list which contains MAC filter entries - changes to
2252 * those entries needs to be undone.
2254 * MAC filter entries from this list were slated for addition.
2256 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2257 struct hlist_head *from)
2259 struct i40e_new_mac_filter *new;
2260 struct hlist_node *h;
2262 hlist_for_each_entry_safe(new, h, from, hlist) {
2263 /* We can simply free the wrapper structure */
2264 hlist_del(&new->hlist);
2265 netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2271 * i40e_next_filter - Get the next non-broadcast filter from a list
2272 * @next: pointer to filter in list
2274 * Returns the next non-broadcast filter in the list. Required so that we
2275 * ignore broadcast filters within the list, since these are not handled via
2276 * the normal firmware update path.
2279 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2281 hlist_for_each_entry_continue(next, hlist) {
2282 if (!is_broadcast_ether_addr(next->f->macaddr))
2290 * i40e_update_filter_state - Update filter state based on return data
2292 * @count: Number of filters added
2293 * @add_list: return data from fw
2294 * @add_head: pointer to first filter in current batch
2296 * MAC filter entries from list were slated to be added to device. Returns
2297 * number of successful filters. Note that 0 does NOT mean success!
2300 i40e_update_filter_state(int count,
2301 struct i40e_aqc_add_macvlan_element_data *add_list,
2302 struct i40e_new_mac_filter *add_head)
2307 for (i = 0; i < count; i++) {
2308 /* Always check status of each filter. We don't need to check
2309 * the firmware return status because we pre-set the filter
2310 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2311 * request to the adminq. Thus, if it no longer matches then
2312 * we know the filter is active.
2314 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2315 add_head->state = I40E_FILTER_FAILED;
2317 add_head->state = I40E_FILTER_ACTIVE;
2321 add_head = i40e_next_filter(add_head);
2330 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2331 * @vsi: ptr to the VSI
2332 * @vsi_name: name to display in messages
2333 * @list: the list of filters to send to firmware
2334 * @num_del: the number of filters to delete
2335 * @retval: Set to -EIO on failure to delete
2337 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2338 * *retval instead of a return value so that success does not force ret_val to
2339 * be set to 0. This ensures that a sequence of calls to this function
2340 * preserve the previous value of *retval on successful delete.
2343 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2344 struct i40e_aqc_remove_macvlan_element_data *list,
2345 int num_del, int *retval)
2347 struct i40e_hw *hw = &vsi->back->hw;
2348 enum i40e_admin_queue_err aq_status;
2351 aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2354 /* Explicitly ignore and do not report when firmware returns ENOENT */
2355 if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2357 dev_info(&vsi->back->pdev->dev,
2358 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2359 vsi_name, i40e_stat_str(hw, aq_ret),
2360 i40e_aq_str(hw, aq_status));
2365 * i40e_aqc_add_filters - Request firmware to add a set of filters
2366 * @vsi: ptr to the VSI
2367 * @vsi_name: name to display in messages
2368 * @list: the list of filters to send to firmware
2369 * @add_head: Position in the add hlist
2370 * @num_add: the number of filters to add
2372 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2373 * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2374 * space for more filters.
2377 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2378 struct i40e_aqc_add_macvlan_element_data *list,
2379 struct i40e_new_mac_filter *add_head,
2382 struct i40e_hw *hw = &vsi->back->hw;
2383 enum i40e_admin_queue_err aq_status;
2386 i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2387 fcnt = i40e_update_filter_state(num_add, list, add_head);
2389 if (fcnt != num_add) {
2390 if (vsi->type == I40E_VSI_MAIN) {
2391 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2392 dev_warn(&vsi->back->pdev->dev,
2393 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2394 i40e_aq_str(hw, aq_status), vsi_name);
2395 } else if (vsi->type == I40E_VSI_SRIOV ||
2396 vsi->type == I40E_VSI_VMDQ1 ||
2397 vsi->type == I40E_VSI_VMDQ2) {
2398 dev_warn(&vsi->back->pdev->dev,
2399 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2400 i40e_aq_str(hw, aq_status), vsi_name,
2403 dev_warn(&vsi->back->pdev->dev,
2404 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2405 i40e_aq_str(hw, aq_status), vsi_name,
2412 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2413 * @vsi: pointer to the VSI
2414 * @vsi_name: the VSI name
2417 * This function sets or clears the promiscuous broadcast flags for VLAN
2418 * filters in order to properly receive broadcast frames. Assumes that only
2419 * broadcast filters are passed.
2421 * Returns status indicating success or failure;
2424 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2425 struct i40e_mac_filter *f)
2427 bool enable = f->state == I40E_FILTER_NEW;
2428 struct i40e_hw *hw = &vsi->back->hw;
2431 if (f->vlan == I40E_VLAN_ANY) {
2432 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2437 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2445 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2446 dev_warn(&vsi->back->pdev->dev,
2447 "Error %s, forcing overflow promiscuous on %s\n",
2448 i40e_aq_str(hw, hw->aq.asq_last_status),
2456 * i40e_set_promiscuous - set promiscuous mode
2457 * @pf: board private structure
2458 * @promisc: promisc on or off
2460 * There are different ways of setting promiscuous mode on a PF depending on
2461 * what state/environment we're in. This identifies and sets it appropriately.
2462 * Returns 0 on success.
2464 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2466 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2467 struct i40e_hw *hw = &pf->hw;
2470 if (vsi->type == I40E_VSI_MAIN &&
2471 pf->lan_veb != I40E_NO_VEB &&
2472 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2473 /* set defport ON for Main VSI instead of true promisc
2474 * this way we will get all unicast/multicast and VLAN
2475 * promisc behavior but will not get VF or VMDq traffic
2476 * replicated on the Main VSI.
2479 aq_ret = i40e_aq_set_default_vsi(hw,
2483 aq_ret = i40e_aq_clear_default_vsi(hw,
2487 dev_info(&pf->pdev->dev,
2488 "Set default VSI failed, err %s, aq_err %s\n",
2489 i40e_stat_str(hw, aq_ret),
2490 i40e_aq_str(hw, hw->aq.asq_last_status));
2493 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2499 dev_info(&pf->pdev->dev,
2500 "set unicast promisc failed, err %s, aq_err %s\n",
2501 i40e_stat_str(hw, aq_ret),
2502 i40e_aq_str(hw, hw->aq.asq_last_status));
2504 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2509 dev_info(&pf->pdev->dev,
2510 "set multicast promisc failed, err %s, aq_err %s\n",
2511 i40e_stat_str(hw, aq_ret),
2512 i40e_aq_str(hw, hw->aq.asq_last_status));
2517 pf->cur_promisc = promisc;
2523 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2524 * @vsi: ptr to the VSI
2526 * Push any outstanding VSI filter changes through the AdminQ.
2528 * Returns 0 or error value
2530 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2532 struct hlist_head tmp_add_list, tmp_del_list;
2533 struct i40e_mac_filter *f;
2534 struct i40e_new_mac_filter *new, *add_head = NULL;
2535 struct i40e_hw *hw = &vsi->back->hw;
2536 bool old_overflow, new_overflow;
2537 unsigned int failed_filters = 0;
2538 unsigned int vlan_filters = 0;
2539 char vsi_name[16] = "PF";
2540 int filter_list_len = 0;
2541 i40e_status aq_ret = 0;
2542 u32 changed_flags = 0;
2543 struct hlist_node *h;
2552 /* empty array typed pointers, kcalloc later */
2553 struct i40e_aqc_add_macvlan_element_data *add_list;
2554 struct i40e_aqc_remove_macvlan_element_data *del_list;
2556 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2557 usleep_range(1000, 2000);
2560 old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2563 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2564 vsi->current_netdev_flags = vsi->netdev->flags;
2567 INIT_HLIST_HEAD(&tmp_add_list);
2568 INIT_HLIST_HEAD(&tmp_del_list);
2570 if (vsi->type == I40E_VSI_SRIOV)
2571 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2572 else if (vsi->type != I40E_VSI_MAIN)
2573 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2575 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2576 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2578 spin_lock_bh(&vsi->mac_filter_hash_lock);
2579 /* Create a list of filters to delete. */
2580 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2581 if (f->state == I40E_FILTER_REMOVE) {
2582 /* Move the element into temporary del_list */
2583 hash_del(&f->hlist);
2584 hlist_add_head(&f->hlist, &tmp_del_list);
2586 /* Avoid counting removed filters */
2589 if (f->state == I40E_FILTER_NEW) {
2590 /* Create a temporary i40e_new_mac_filter */
2591 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2593 goto err_no_memory_locked;
2595 /* Store pointer to the real filter */
2597 new->state = f->state;
2599 /* Add it to the hash list */
2600 hlist_add_head(&new->hlist, &tmp_add_list);
2603 /* Count the number of active (current and new) VLAN
2604 * filters we have now. Does not count filters which
2605 * are marked for deletion.
2611 if (vsi->type != I40E_VSI_SRIOV)
2612 retval = i40e_correct_mac_vlan_filters
2613 (vsi, &tmp_add_list, &tmp_del_list,
2616 retval = i40e_correct_vf_mac_vlan_filters
2617 (vsi, &tmp_add_list, &tmp_del_list,
2618 vlan_filters, pf->vf[vsi->vf_id].trusted);
2620 hlist_for_each_entry(new, &tmp_add_list, hlist)
2621 netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2624 goto err_no_memory_locked;
2626 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2629 /* Now process 'del_list' outside the lock */
2630 if (!hlist_empty(&tmp_del_list)) {
2631 filter_list_len = hw->aq.asq_buf_size /
2632 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2633 list_size = filter_list_len *
2634 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2635 del_list = kzalloc(list_size, GFP_ATOMIC);
2639 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2642 /* handle broadcast filters by updating the broadcast
2643 * promiscuous flag and release filter list.
2645 if (is_broadcast_ether_addr(f->macaddr)) {
2646 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2648 hlist_del(&f->hlist);
2653 /* add to delete list */
2654 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2655 if (f->vlan == I40E_VLAN_ANY) {
2656 del_list[num_del].vlan_tag = 0;
2657 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2659 del_list[num_del].vlan_tag =
2660 cpu_to_le16((u16)(f->vlan));
2663 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2664 del_list[num_del].flags = cmd_flags;
2667 /* flush a full buffer */
2668 if (num_del == filter_list_len) {
2669 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2671 memset(del_list, 0, list_size);
2674 /* Release memory for MAC filter entries which were
2675 * synced up with HW.
2677 hlist_del(&f->hlist);
2682 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2690 if (!hlist_empty(&tmp_add_list)) {
2691 /* Do all the adds now. */
2692 filter_list_len = hw->aq.asq_buf_size /
2693 sizeof(struct i40e_aqc_add_macvlan_element_data);
2694 list_size = filter_list_len *
2695 sizeof(struct i40e_aqc_add_macvlan_element_data);
2696 add_list = kzalloc(list_size, GFP_ATOMIC);
2701 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2702 /* handle broadcast filters by updating the broadcast
2703 * promiscuous flag instead of adding a MAC filter.
2705 if (is_broadcast_ether_addr(new->f->macaddr)) {
2706 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2708 new->state = I40E_FILTER_FAILED;
2710 new->state = I40E_FILTER_ACTIVE;
2714 /* add to add array */
2718 ether_addr_copy(add_list[num_add].mac_addr,
2720 if (new->f->vlan == I40E_VLAN_ANY) {
2721 add_list[num_add].vlan_tag = 0;
2722 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2724 add_list[num_add].vlan_tag =
2725 cpu_to_le16((u16)(new->f->vlan));
2727 add_list[num_add].queue_number = 0;
2728 /* set invalid match method for later detection */
2729 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2730 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2731 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2734 /* flush a full buffer */
2735 if (num_add == filter_list_len) {
2736 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2738 memset(add_list, 0, list_size);
2743 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2746 /* Now move all of the filters from the temp add list back to
2749 spin_lock_bh(&vsi->mac_filter_hash_lock);
2750 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2751 /* Only update the state if we're still NEW */
2752 if (new->f->state == I40E_FILTER_NEW)
2753 new->f->state = new->state;
2754 hlist_del(&new->hlist);
2755 netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2758 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2763 /* Determine the number of active and failed filters. */
2764 spin_lock_bh(&vsi->mac_filter_hash_lock);
2765 vsi->active_filters = 0;
2766 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2767 if (f->state == I40E_FILTER_ACTIVE)
2768 vsi->active_filters++;
2769 else if (f->state == I40E_FILTER_FAILED)
2772 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2774 /* Check if we are able to exit overflow promiscuous mode. We can
2775 * safely exit if we didn't just enter, we no longer have any failed
2776 * filters, and we have reduced filters below the threshold value.
2778 if (old_overflow && !failed_filters &&
2779 vsi->active_filters < vsi->promisc_threshold) {
2780 dev_info(&pf->pdev->dev,
2781 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2783 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2784 vsi->promisc_threshold = 0;
2787 /* if the VF is not trusted do not do promisc */
2788 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2789 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2795 /* If we are entering overflow promiscuous, we need to calculate a new
2796 * threshold for when we are safe to exit
2798 if (!old_overflow && new_overflow)
2799 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2801 /* check for changes in promiscuous modes */
2802 if (changed_flags & IFF_ALLMULTI) {
2803 bool cur_multipromisc;
2805 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2806 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2811 retval = i40e_aq_rc_to_posix(aq_ret,
2812 hw->aq.asq_last_status);
2813 dev_info(&pf->pdev->dev,
2814 "set multi promisc failed on %s, err %s aq_err %s\n",
2816 i40e_stat_str(hw, aq_ret),
2817 i40e_aq_str(hw, hw->aq.asq_last_status));
2819 dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2820 cur_multipromisc ? "entering" : "leaving");
2824 if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2827 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2829 aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2831 retval = i40e_aq_rc_to_posix(aq_ret,
2832 hw->aq.asq_last_status);
2833 dev_info(&pf->pdev->dev,
2834 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2835 cur_promisc ? "on" : "off",
2837 i40e_stat_str(hw, aq_ret),
2838 i40e_aq_str(hw, hw->aq.asq_last_status));
2842 /* if something went wrong then set the changed flag so we try again */
2844 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2846 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2850 /* Restore elements on the temporary add and delete lists */
2851 spin_lock_bh(&vsi->mac_filter_hash_lock);
2852 err_no_memory_locked:
2853 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2854 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2855 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2857 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2858 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2863 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2864 * @pf: board private structure
2866 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2872 if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2874 if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2875 set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2879 for (v = 0; v < pf->num_alloc_vsi; v++) {
2881 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2882 !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2883 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2886 /* come back and try again later */
2887 set_bit(__I40E_MACVLAN_SYNC_PENDING,
2896 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2899 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2901 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2902 return I40E_RXBUFFER_2048;
2904 return I40E_RXBUFFER_3072;
2908 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2909 * @netdev: network interface device structure
2910 * @new_mtu: new value for maximum frame size
2912 * Returns 0 on success, negative on failure
2914 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2916 struct i40e_netdev_priv *np = netdev_priv(netdev);
2917 struct i40e_vsi *vsi = np->vsi;
2918 struct i40e_pf *pf = vsi->back;
2920 if (i40e_enabled_xdp_vsi(vsi)) {
2921 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2923 if (frame_size > i40e_max_xdp_frame_size(vsi))
2927 netdev_dbg(netdev, "changing MTU from %d to %d\n",
2928 netdev->mtu, new_mtu);
2929 netdev->mtu = new_mtu;
2930 if (netif_running(netdev))
2931 i40e_vsi_reinit_locked(vsi);
2932 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2933 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2938 * i40e_ioctl - Access the hwtstamp interface
2939 * @netdev: network interface device structure
2940 * @ifr: interface request data
2941 * @cmd: ioctl command
2943 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2945 struct i40e_netdev_priv *np = netdev_priv(netdev);
2946 struct i40e_pf *pf = np->vsi->back;
2950 return i40e_ptp_get_ts_config(pf, ifr);
2952 return i40e_ptp_set_ts_config(pf, ifr);
2959 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2960 * @vsi: the vsi being adjusted
2962 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2964 struct i40e_vsi_context ctxt;
2967 /* Don't modify stripping options if a port VLAN is active */
2971 if ((vsi->info.valid_sections &
2972 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2973 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2974 return; /* already enabled */
2976 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2977 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2978 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2980 ctxt.seid = vsi->seid;
2981 ctxt.info = vsi->info;
2982 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2984 dev_info(&vsi->back->pdev->dev,
2985 "update vlan stripping failed, err %s aq_err %s\n",
2986 i40e_stat_str(&vsi->back->hw, ret),
2987 i40e_aq_str(&vsi->back->hw,
2988 vsi->back->hw.aq.asq_last_status));
2993 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2994 * @vsi: the vsi being adjusted
2996 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2998 struct i40e_vsi_context ctxt;
3001 /* Don't modify stripping options if a port VLAN is active */
3005 if ((vsi->info.valid_sections &
3006 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3007 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3008 I40E_AQ_VSI_PVLAN_EMOD_MASK))
3009 return; /* already disabled */
3011 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3012 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3013 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3015 ctxt.seid = vsi->seid;
3016 ctxt.info = vsi->info;
3017 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3019 dev_info(&vsi->back->pdev->dev,
3020 "update vlan stripping failed, err %s aq_err %s\n",
3021 i40e_stat_str(&vsi->back->hw, ret),
3022 i40e_aq_str(&vsi->back->hw,
3023 vsi->back->hw.aq.asq_last_status));
3028 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3029 * @vsi: the vsi being configured
3030 * @vid: vlan id to be added (0 = untagged only , -1 = any)
3032 * This is a helper function for adding a new MAC/VLAN filter with the
3033 * specified VLAN for each existing MAC address already in the hash table.
3034 * This function does *not* perform any accounting to update filters based on
3037 * NOTE: this function expects to be called while under the
3038 * mac_filter_hash_lock
3040 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3042 struct i40e_mac_filter *f, *add_f;
3043 struct hlist_node *h;
3046 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3047 /* If we're asked to add a filter that has been marked for
3048 * removal, it is safe to simply restore it to active state.
3049 * __i40e_del_filter will have simply deleted any filters which
3050 * were previously marked NEW or FAILED, so if it is currently
3051 * marked REMOVE it must have previously been ACTIVE. Since we
3052 * haven't yet run the sync filters task, just restore this
3053 * filter to the ACTIVE state so that the sync task leaves it
3056 if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3057 f->state = I40E_FILTER_ACTIVE;
3059 } else if (f->state == I40E_FILTER_REMOVE) {
3062 add_f = i40e_add_filter(vsi, f->macaddr, vid);
3064 dev_info(&vsi->back->pdev->dev,
3065 "Could not add vlan filter %d for %pM\n",
3075 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3076 * @vsi: the VSI being configured
3077 * @vid: VLAN id to be added
3079 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3086 /* The network stack will attempt to add VID=0, with the intention to
3087 * receive priority tagged packets with a VLAN of 0. Our HW receives
3088 * these packets by default when configured to receive untagged
3089 * packets, so we don't need to add a filter for this case.
3090 * Additionally, HW interprets adding a VID=0 filter as meaning to
3091 * receive *only* tagged traffic and stops receiving untagged traffic.
3092 * Thus, we do not want to actually add a filter for VID=0
3097 /* Locked once because all functions invoked below iterates list*/
3098 spin_lock_bh(&vsi->mac_filter_hash_lock);
3099 err = i40e_add_vlan_all_mac(vsi, vid);
3100 spin_unlock_bh(&vsi->mac_filter_hash_lock);
3104 /* schedule our worker thread which will take care of
3105 * applying the new filter changes
3107 i40e_service_event_schedule(vsi->back);
3112 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3113 * @vsi: the vsi being configured
3114 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3116 * This function should be used to remove all VLAN filters which match the
3117 * given VID. It does not schedule the service event and does not take the
3118 * mac_filter_hash_lock so it may be combined with other operations under
3119 * a single invocation of the mac_filter_hash_lock.
3121 * NOTE: this function expects to be called while under the
3122 * mac_filter_hash_lock
3124 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3126 struct i40e_mac_filter *f;
3127 struct hlist_node *h;
3130 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3132 __i40e_del_filter(vsi, f);
3137 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3138 * @vsi: the VSI being configured
3139 * @vid: VLAN id to be removed
3141 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3143 if (!vid || vsi->info.pvid)
3146 spin_lock_bh(&vsi->mac_filter_hash_lock);
3147 i40e_rm_vlan_all_mac(vsi, vid);
3148 spin_unlock_bh(&vsi->mac_filter_hash_lock);
3150 /* schedule our worker thread which will take care of
3151 * applying the new filter changes
3153 i40e_service_event_schedule(vsi->back);
3157 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3158 * @netdev: network interface to be adjusted
3159 * @proto: unused protocol value
3160 * @vid: vlan id to be added
3162 * net_device_ops implementation for adding vlan ids
3164 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3165 __always_unused __be16 proto, u16 vid)
3167 struct i40e_netdev_priv *np = netdev_priv(netdev);
3168 struct i40e_vsi *vsi = np->vsi;
3171 if (vid >= VLAN_N_VID)
3174 ret = i40e_vsi_add_vlan(vsi, vid);
3176 set_bit(vid, vsi->active_vlans);
3182 * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3183 * @netdev: network interface to be adjusted
3184 * @proto: unused protocol value
3185 * @vid: vlan id to be added
3187 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3188 __always_unused __be16 proto, u16 vid)
3190 struct i40e_netdev_priv *np = netdev_priv(netdev);
3191 struct i40e_vsi *vsi = np->vsi;
3193 if (vid >= VLAN_N_VID)
3195 set_bit(vid, vsi->active_vlans);
3199 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3200 * @netdev: network interface to be adjusted
3201 * @proto: unused protocol value
3202 * @vid: vlan id to be removed
3204 * net_device_ops implementation for removing vlan ids
3206 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3207 __always_unused __be16 proto, u16 vid)
3209 struct i40e_netdev_priv *np = netdev_priv(netdev);
3210 struct i40e_vsi *vsi = np->vsi;
3212 /* return code is ignored as there is nothing a user
3213 * can do about failure to remove and a log message was
3214 * already printed from the other function
3216 i40e_vsi_kill_vlan(vsi, vid);
3218 clear_bit(vid, vsi->active_vlans);
3224 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3225 * @vsi: the vsi being brought back up
3227 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3234 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3235 i40e_vlan_stripping_enable(vsi);
3237 i40e_vlan_stripping_disable(vsi);
3239 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3240 i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3245 * i40e_vsi_add_pvid - Add pvid for the VSI
3246 * @vsi: the vsi being adjusted
3247 * @vid: the vlan id to set as a PVID
3249 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3251 struct i40e_vsi_context ctxt;
3254 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3255 vsi->info.pvid = cpu_to_le16(vid);
3256 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3257 I40E_AQ_VSI_PVLAN_INSERT_PVID |
3258 I40E_AQ_VSI_PVLAN_EMOD_STR;
3260 ctxt.seid = vsi->seid;
3261 ctxt.info = vsi->info;
3262 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3264 dev_info(&vsi->back->pdev->dev,
3265 "add pvid failed, err %s aq_err %s\n",
3266 i40e_stat_str(&vsi->back->hw, ret),
3267 i40e_aq_str(&vsi->back->hw,
3268 vsi->back->hw.aq.asq_last_status));
3276 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3277 * @vsi: the vsi being adjusted
3279 * Just use the vlan_rx_register() service to put it back to normal
3281 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3285 i40e_vlan_stripping_disable(vsi);
3289 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3290 * @vsi: ptr to the VSI
3292 * If this function returns with an error, then it's possible one or
3293 * more of the rings is populated (while the rest are not). It is the
3294 * callers duty to clean those orphaned rings.
3296 * Return 0 on success, negative on failure
3298 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3302 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3303 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3305 if (!i40e_enabled_xdp_vsi(vsi))
3308 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3309 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3315 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3316 * @vsi: ptr to the VSI
3318 * Free VSI's transmit software resources
3320 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3324 if (vsi->tx_rings) {
3325 for (i = 0; i < vsi->num_queue_pairs; i++)
3326 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3327 i40e_free_tx_resources(vsi->tx_rings[i]);
3330 if (vsi->xdp_rings) {
3331 for (i = 0; i < vsi->num_queue_pairs; i++)
3332 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3333 i40e_free_tx_resources(vsi->xdp_rings[i]);
3338 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3339 * @vsi: ptr to the VSI
3341 * If this function returns with an error, then it's possible one or
3342 * more of the rings is populated (while the rest are not). It is the
3343 * callers duty to clean those orphaned rings.
3345 * Return 0 on success, negative on failure
3347 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3351 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3352 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3357 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3358 * @vsi: ptr to the VSI
3360 * Free all receive software resources
3362 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3369 for (i = 0; i < vsi->num_queue_pairs; i++)
3370 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3371 i40e_free_rx_resources(vsi->rx_rings[i]);
3375 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3376 * @ring: The Tx ring to configure
3378 * This enables/disables XPS for a given Tx descriptor ring
3379 * based on the TCs enabled for the VSI that ring belongs to.
3381 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3385 if (!ring->q_vector || !ring->netdev || ring->ch)
3388 /* We only initialize XPS once, so as not to overwrite user settings */
3389 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3392 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3393 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3398 * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3399 * @ring: The Tx or Rx ring
3401 * Returns the AF_XDP buffer pool or NULL.
3403 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3405 bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3406 int qid = ring->queue_index;
3408 if (ring_is_xdp(ring))
3409 qid -= ring->vsi->alloc_queue_pairs;
3411 if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3414 return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3418 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3419 * @ring: The Tx ring to configure
3421 * Configure the Tx descriptor ring in the HMC context.
3423 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3425 struct i40e_vsi *vsi = ring->vsi;
3426 u16 pf_q = vsi->base_queue + ring->queue_index;
3427 struct i40e_hw *hw = &vsi->back->hw;
3428 struct i40e_hmc_obj_txq tx_ctx;
3429 i40e_status err = 0;
3432 if (ring_is_xdp(ring))
3433 ring->xsk_pool = i40e_xsk_pool(ring);
3435 /* some ATR related tx ring init */
3436 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3437 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3438 ring->atr_count = 0;
3440 ring->atr_sample_rate = 0;
3444 i40e_config_xps_tx_ring(ring);
3446 /* clear the context structure first */
3447 memset(&tx_ctx, 0, sizeof(tx_ctx));
3449 tx_ctx.new_context = 1;
3450 tx_ctx.base = (ring->dma / 128);
3451 tx_ctx.qlen = ring->count;
3452 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3453 I40E_FLAG_FD_ATR_ENABLED));
3454 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3455 /* FDIR VSI tx ring can still use RS bit and writebacks */
3456 if (vsi->type != I40E_VSI_FDIR)
3457 tx_ctx.head_wb_ena = 1;
3458 tx_ctx.head_wb_addr = ring->dma +
3459 (ring->count * sizeof(struct i40e_tx_desc));
3461 /* As part of VSI creation/update, FW allocates certain
3462 * Tx arbitration queue sets for each TC enabled for
3463 * the VSI. The FW returns the handles to these queue
3464 * sets as part of the response buffer to Add VSI,
3465 * Update VSI, etc. AQ commands. It is expected that
3466 * these queue set handles be associated with the Tx
3467 * queues by the driver as part of the TX queue context
3468 * initialization. This has to be done regardless of
3469 * DCB as by default everything is mapped to TC0.
3474 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3477 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3479 tx_ctx.rdylist_act = 0;
3481 /* clear the context in the HMC */
3482 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3484 dev_info(&vsi->back->pdev->dev,
3485 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3486 ring->queue_index, pf_q, err);
3490 /* set the context in the HMC */
3491 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3493 dev_info(&vsi->back->pdev->dev,
3494 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3495 ring->queue_index, pf_q, err);
3499 /* Now associate this queue with this PCI function */
3501 if (ring->ch->type == I40E_VSI_VMDQ2)
3502 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3506 qtx_ctl |= (ring->ch->vsi_number <<
3507 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3508 I40E_QTX_CTL_VFVM_INDX_MASK;
3510 if (vsi->type == I40E_VSI_VMDQ2) {
3511 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3512 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3513 I40E_QTX_CTL_VFVM_INDX_MASK;
3515 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3519 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3520 I40E_QTX_CTL_PF_INDX_MASK);
3521 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3524 /* cache tail off for easier writes later */
3525 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3531 * i40e_rx_offset - Return expected offset into page to access data
3532 * @rx_ring: Ring we are requesting offset of
3534 * Returns the offset value for ring into the data buffer.
3536 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3538 return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3542 * i40e_configure_rx_ring - Configure a receive ring context
3543 * @ring: The Rx ring to configure
3545 * Configure the Rx descriptor ring in the HMC context.
3547 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3549 struct i40e_vsi *vsi = ring->vsi;
3550 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3551 u16 pf_q = vsi->base_queue + ring->queue_index;
3552 struct i40e_hw *hw = &vsi->back->hw;
3553 struct i40e_hmc_obj_rxq rx_ctx;
3554 i40e_status err = 0;
3558 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3560 /* clear the context structure first */
3561 memset(&rx_ctx, 0, sizeof(rx_ctx));
3563 if (ring->vsi->type == I40E_VSI_MAIN)
3564 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3567 ring->xsk_pool = i40e_xsk_pool(ring);
3568 if (ring->xsk_pool) {
3569 ret = i40e_alloc_rx_bi_zc(ring);
3573 xsk_pool_get_rx_frame_size(ring->xsk_pool);
3574 /* For AF_XDP ZC, we disallow packets to span on
3575 * multiple buffers, thus letting us skip that
3576 * handling in the fast-path.
3579 ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3580 MEM_TYPE_XSK_BUFF_POOL,
3584 dev_info(&vsi->back->pdev->dev,
3585 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3589 ret = i40e_alloc_rx_bi(ring);
3592 ring->rx_buf_len = vsi->rx_buf_len;
3593 if (ring->vsi->type == I40E_VSI_MAIN) {
3594 ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3595 MEM_TYPE_PAGE_SHARED,
3602 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3603 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3605 rx_ctx.base = (ring->dma / 128);
3606 rx_ctx.qlen = ring->count;
3608 /* use 16 byte descriptors */
3611 /* descriptor type is always zero
3614 rx_ctx.hsplit_0 = 0;
3616 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3617 if (hw->revision_id == 0)
3618 rx_ctx.lrxqthresh = 0;
3620 rx_ctx.lrxqthresh = 1;
3621 rx_ctx.crcstrip = 1;
3623 /* this controls whether VLAN is stripped from inner headers */
3625 /* set the prefena field to 1 because the manual says to */
3628 /* clear the context in the HMC */
3629 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3631 dev_info(&vsi->back->pdev->dev,
3632 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3633 ring->queue_index, pf_q, err);
3637 /* set the context in the HMC */
3638 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3640 dev_info(&vsi->back->pdev->dev,
3641 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3642 ring->queue_index, pf_q, err);
3646 /* configure Rx buffer alignment */
3647 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3648 clear_ring_build_skb_enabled(ring);
3650 set_ring_build_skb_enabled(ring);
3652 ring->rx_offset = i40e_rx_offset(ring);
3654 /* cache tail for quicker writes, and clear the reg before use */
3655 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3656 writel(0, ring->tail);
3658 if (ring->xsk_pool) {
3659 xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3660 ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3662 ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3665 /* Log this in case the user has forgotten to give the kernel
3666 * any buffers, even later in the application.
3668 dev_info(&vsi->back->pdev->dev,
3669 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3670 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3671 ring->queue_index, pf_q);
3678 * i40e_vsi_configure_tx - Configure the VSI for Tx
3679 * @vsi: VSI structure describing this set of rings and resources
3681 * Configure the Tx VSI for operation.
3683 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3688 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3689 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3691 if (err || !i40e_enabled_xdp_vsi(vsi))
3694 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3695 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3701 * i40e_vsi_configure_rx - Configure the VSI for Rx
3702 * @vsi: the VSI being configured
3704 * Configure the Rx VSI for operation.
3706 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3711 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3712 vsi->max_frame = I40E_MAX_RXBUFFER;
3713 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3714 #if (PAGE_SIZE < 8192)
3715 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3716 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3717 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3718 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3721 vsi->max_frame = I40E_MAX_RXBUFFER;
3722 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3726 /* set up individual rings */
3727 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3728 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3734 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3735 * @vsi: ptr to the VSI
3737 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3739 struct i40e_ring *tx_ring, *rx_ring;
3740 u16 qoffset, qcount;
3743 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3744 /* Reset the TC information */
3745 for (i = 0; i < vsi->num_queue_pairs; i++) {
3746 rx_ring = vsi->rx_rings[i];
3747 tx_ring = vsi->tx_rings[i];
3748 rx_ring->dcb_tc = 0;
3749 tx_ring->dcb_tc = 0;
3754 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3755 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3758 qoffset = vsi->tc_config.tc_info[n].qoffset;
3759 qcount = vsi->tc_config.tc_info[n].qcount;
3760 for (i = qoffset; i < (qoffset + qcount); i++) {
3761 rx_ring = vsi->rx_rings[i];
3762 tx_ring = vsi->tx_rings[i];
3763 rx_ring->dcb_tc = n;
3764 tx_ring->dcb_tc = n;
3770 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3771 * @vsi: ptr to the VSI
3773 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3776 i40e_set_rx_mode(vsi->netdev);
3780 * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3781 * @pf: Pointer to the targeted PF
3783 * Set all flow director counters to 0.
3785 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3787 pf->fd_tcp4_filter_cnt = 0;
3788 pf->fd_udp4_filter_cnt = 0;
3789 pf->fd_sctp4_filter_cnt = 0;
3790 pf->fd_ip4_filter_cnt = 0;
3791 pf->fd_tcp6_filter_cnt = 0;
3792 pf->fd_udp6_filter_cnt = 0;
3793 pf->fd_sctp6_filter_cnt = 0;
3794 pf->fd_ip6_filter_cnt = 0;
3798 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3799 * @vsi: Pointer to the targeted VSI
3801 * This function replays the hlist on the hw where all the SB Flow Director
3802 * filters were saved.
3804 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3806 struct i40e_fdir_filter *filter;
3807 struct i40e_pf *pf = vsi->back;
3808 struct hlist_node *node;
3810 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3813 /* Reset FDir counters as we're replaying all existing filters */
3814 i40e_reset_fdir_filter_cnt(pf);
3816 hlist_for_each_entry_safe(filter, node,
3817 &pf->fdir_filter_list, fdir_node) {
3818 i40e_add_del_fdir(vsi, filter, true);
3823 * i40e_vsi_configure - Set up the VSI for action
3824 * @vsi: the VSI being configured
3826 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3830 i40e_set_vsi_rx_mode(vsi);
3831 i40e_restore_vlan(vsi);
3832 i40e_vsi_config_dcb_rings(vsi);
3833 err = i40e_vsi_configure_tx(vsi);
3835 err = i40e_vsi_configure_rx(vsi);
3841 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3842 * @vsi: the VSI being configured
3844 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3846 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3847 struct i40e_pf *pf = vsi->back;
3848 struct i40e_hw *hw = &pf->hw;
3853 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3854 * and PFINT_LNKLSTn registers, e.g.:
3855 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3857 qp = vsi->base_queue;
3858 vector = vsi->base_vector;
3859 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3860 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3862 q_vector->rx.next_update = jiffies + 1;
3863 q_vector->rx.target_itr =
3864 ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3865 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3866 q_vector->rx.target_itr >> 1);
3867 q_vector->rx.current_itr = q_vector->rx.target_itr;
3869 q_vector->tx.next_update = jiffies + 1;
3870 q_vector->tx.target_itr =
3871 ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3872 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3873 q_vector->tx.target_itr >> 1);
3874 q_vector->tx.current_itr = q_vector->tx.target_itr;
3876 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3877 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3879 /* Linked list for the queuepairs assigned to this vector */
3880 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3881 for (q = 0; q < q_vector->num_ringpairs; q++) {
3882 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3885 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3886 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3887 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3888 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3889 (I40E_QUEUE_TYPE_TX <<
3890 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3892 wr32(hw, I40E_QINT_RQCTL(qp), val);
3895 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3896 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3897 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3898 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3899 (I40E_QUEUE_TYPE_TX <<
3900 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3902 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3905 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3906 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3907 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3908 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3909 (I40E_QUEUE_TYPE_RX <<
3910 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3912 /* Terminate the linked list */
3913 if (q == (q_vector->num_ringpairs - 1))
3914 val |= (I40E_QUEUE_END_OF_LIST <<
3915 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3917 wr32(hw, I40E_QINT_TQCTL(qp), val);
3926 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3927 * @pf: pointer to private device data structure
3929 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3931 struct i40e_hw *hw = &pf->hw;
3934 /* clear things first */
3935 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3936 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3938 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3939 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3940 I40E_PFINT_ICR0_ENA_GRST_MASK |
3941 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3942 I40E_PFINT_ICR0_ENA_GPIO_MASK |
3943 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3944 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3945 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3947 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3948 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3950 if (pf->flags & I40E_FLAG_PTP)
3951 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3953 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3955 /* SW_ITR_IDX = 0, but don't change INTENA */
3956 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3957 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3959 /* OTHER_ITR_IDX = 0 */
3960 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3964 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3965 * @vsi: the VSI being configured
3967 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3969 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3970 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3971 struct i40e_pf *pf = vsi->back;
3972 struct i40e_hw *hw = &pf->hw;
3975 /* set the ITR configuration */
3976 q_vector->rx.next_update = jiffies + 1;
3977 q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3978 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3979 q_vector->rx.current_itr = q_vector->rx.target_itr;
3980 q_vector->tx.next_update = jiffies + 1;
3981 q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3982 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3983 q_vector->tx.current_itr = q_vector->tx.target_itr;
3985 i40e_enable_misc_int_causes(pf);
3987 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3988 wr32(hw, I40E_PFINT_LNKLST0, 0);
3990 /* Associate the queue pair to the vector and enable the queue int */
3991 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3992 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3993 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3994 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3996 wr32(hw, I40E_QINT_RQCTL(0), val);
3998 if (i40e_enabled_xdp_vsi(vsi)) {
3999 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4000 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
4002 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
4004 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
4007 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4008 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
4009 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
4011 wr32(hw, I40E_QINT_TQCTL(0), val);
4016 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4017 * @pf: board private structure
4019 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4021 struct i40e_hw *hw = &pf->hw;
4023 wr32(hw, I40E_PFINT_DYN_CTL0,
4024 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4029 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4030 * @pf: board private structure
4032 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4034 struct i40e_hw *hw = &pf->hw;
4037 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
4038 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4039 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4041 wr32(hw, I40E_PFINT_DYN_CTL0, val);
4046 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4047 * @irq: interrupt number
4048 * @data: pointer to a q_vector
4050 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4052 struct i40e_q_vector *q_vector = data;
4054 if (!q_vector->tx.ring && !q_vector->rx.ring)
4057 napi_schedule_irqoff(&q_vector->napi);
4063 * i40e_irq_affinity_notify - Callback for affinity changes
4064 * @notify: context as to what irq was changed
4065 * @mask: the new affinity mask
4067 * This is a callback function used by the irq_set_affinity_notifier function
4068 * so that we may register to receive changes to the irq affinity masks.
4070 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4071 const cpumask_t *mask)
4073 struct i40e_q_vector *q_vector =
4074 container_of(notify, struct i40e_q_vector, affinity_notify);
4076 cpumask_copy(&q_vector->affinity_mask, mask);
4080 * i40e_irq_affinity_release - Callback for affinity notifier release
4081 * @ref: internal core kernel usage
4083 * This is a callback function used by the irq_set_affinity_notifier function
4084 * to inform the current notification subscriber that they will no longer
4085 * receive notifications.
4087 static void i40e_irq_affinity_release(struct kref *ref) {}
4090 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4091 * @vsi: the VSI being configured
4092 * @basename: name for the vector
4094 * Allocates MSI-X vectors and requests interrupts from the kernel.
4096 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4098 int q_vectors = vsi->num_q_vectors;
4099 struct i40e_pf *pf = vsi->back;
4100 int base = vsi->base_vector;
4107 for (vector = 0; vector < q_vectors; vector++) {
4108 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4110 irq_num = pf->msix_entries[base + vector].vector;
4112 if (q_vector->tx.ring && q_vector->rx.ring) {
4113 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4114 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4116 } else if (q_vector->rx.ring) {
4117 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4118 "%s-%s-%d", basename, "rx", rx_int_idx++);
4119 } else if (q_vector->tx.ring) {
4120 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4121 "%s-%s-%d", basename, "tx", tx_int_idx++);
4123 /* skip this unused q_vector */
4126 err = request_irq(irq_num,
4132 dev_info(&pf->pdev->dev,
4133 "MSIX request_irq failed, error: %d\n", err);
4134 goto free_queue_irqs;
4137 /* register for affinity change notifications */
4138 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4139 q_vector->affinity_notify.release = i40e_irq_affinity_release;
4140 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4141 /* Spread affinity hints out across online CPUs.
4143 * get_cpu_mask returns a static constant mask with
4144 * a permanent lifetime so it's ok to pass to
4145 * irq_update_affinity_hint without making a copy.
4147 cpu = cpumask_local_spread(q_vector->v_idx, -1);
4148 irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4151 vsi->irqs_ready = true;
4157 irq_num = pf->msix_entries[base + vector].vector;
4158 irq_set_affinity_notifier(irq_num, NULL);
4159 irq_update_affinity_hint(irq_num, NULL);
4160 free_irq(irq_num, &vsi->q_vectors[vector]);
4166 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4167 * @vsi: the VSI being un-configured
4169 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4171 struct i40e_pf *pf = vsi->back;
4172 struct i40e_hw *hw = &pf->hw;
4173 int base = vsi->base_vector;
4176 /* disable interrupt causation from each queue */
4177 for (i = 0; i < vsi->num_queue_pairs; i++) {
4180 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4181 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4182 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4184 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4185 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4186 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4188 if (!i40e_enabled_xdp_vsi(vsi))
4190 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4193 /* disable each interrupt */
4194 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4195 for (i = vsi->base_vector;
4196 i < (vsi->num_q_vectors + vsi->base_vector); i++)
4197 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4200 for (i = 0; i < vsi->num_q_vectors; i++)
4201 synchronize_irq(pf->msix_entries[i + base].vector);
4203 /* Legacy and MSI mode - this stops all interrupt handling */
4204 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4205 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4207 synchronize_irq(pf->pdev->irq);
4212 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4213 * @vsi: the VSI being configured
4215 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4217 struct i40e_pf *pf = vsi->back;
4220 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4221 for (i = 0; i < vsi->num_q_vectors; i++)
4222 i40e_irq_dynamic_enable(vsi, i);
4224 i40e_irq_dynamic_enable_icr0(pf);
4227 i40e_flush(&pf->hw);
4232 * i40e_free_misc_vector - Free the vector that handles non-queue events
4233 * @pf: board private structure
4235 static void i40e_free_misc_vector(struct i40e_pf *pf)
4238 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4239 i40e_flush(&pf->hw);
4241 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4242 free_irq(pf->msix_entries[0].vector, pf);
4243 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4248 * i40e_intr - MSI/Legacy and non-queue interrupt handler
4249 * @irq: interrupt number
4250 * @data: pointer to a q_vector
4252 * This is the handler used for all MSI/Legacy interrupts, and deals
4253 * with both queue and non-queue interrupts. This is also used in
4254 * MSIX mode to handle the non-queue interrupts.
4256 static irqreturn_t i40e_intr(int irq, void *data)
4258 struct i40e_pf *pf = (struct i40e_pf *)data;
4259 struct i40e_hw *hw = &pf->hw;
4260 irqreturn_t ret = IRQ_NONE;
4261 u32 icr0, icr0_remaining;
4264 icr0 = rd32(hw, I40E_PFINT_ICR0);
4265 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4267 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
4268 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4271 /* if interrupt but no bits showing, must be SWINT */
4272 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4273 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4276 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4277 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4278 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4279 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4280 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4283 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4284 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4285 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4286 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4288 /* We do not have a way to disarm Queue causes while leaving
4289 * interrupt enabled for all other causes, ideally
4290 * interrupt should be disabled while we are in NAPI but
4291 * this is not a performance path and napi_schedule()
4292 * can deal with rescheduling.
4294 if (!test_bit(__I40E_DOWN, pf->state))
4295 napi_schedule_irqoff(&q_vector->napi);
4298 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4299 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4300 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4301 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4304 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4305 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4306 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4309 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4310 /* disable any further VFLR event notifications */
4311 if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4312 u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4314 reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4315 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4317 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4318 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4322 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4323 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4324 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4325 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4326 val = rd32(hw, I40E_GLGEN_RSTAT);
4327 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4328 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4329 if (val == I40E_RESET_CORER) {
4331 } else if (val == I40E_RESET_GLOBR) {
4333 } else if (val == I40E_RESET_EMPR) {
4335 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4339 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4340 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4341 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4342 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4343 rd32(hw, I40E_PFHMC_ERRORINFO),
4344 rd32(hw, I40E_PFHMC_ERRORDATA));
4347 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4348 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4350 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4351 schedule_work(&pf->ptp_extts0_work);
4353 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4354 i40e_ptp_tx_hwtstamp(pf);
4356 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4359 /* If a critical error is pending we have no choice but to reset the
4361 * Report and mask out any remaining unexpected interrupts.
4363 icr0_remaining = icr0 & ena_mask;
4364 if (icr0_remaining) {
4365 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4367 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4368 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4369 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4370 dev_info(&pf->pdev->dev, "device will be reset\n");
4371 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4372 i40e_service_event_schedule(pf);
4374 ena_mask &= ~icr0_remaining;
4379 /* re-enable interrupt causes */
4380 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4381 if (!test_bit(__I40E_DOWN, pf->state) ||
4382 test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4383 i40e_service_event_schedule(pf);
4384 i40e_irq_dynamic_enable_icr0(pf);
4391 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4392 * @tx_ring: tx ring to clean
4393 * @budget: how many cleans we're allowed
4395 * Returns true if there's any budget left (e.g. the clean is finished)
4397 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4399 struct i40e_vsi *vsi = tx_ring->vsi;
4400 u16 i = tx_ring->next_to_clean;
4401 struct i40e_tx_buffer *tx_buf;
4402 struct i40e_tx_desc *tx_desc;
4404 tx_buf = &tx_ring->tx_bi[i];
4405 tx_desc = I40E_TX_DESC(tx_ring, i);
4406 i -= tx_ring->count;
4409 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4411 /* if next_to_watch is not set then there is no work pending */
4415 /* prevent any other reads prior to eop_desc */
4418 /* if the descriptor isn't done, no work yet to do */
4419 if (!(eop_desc->cmd_type_offset_bsz &
4420 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4423 /* clear next_to_watch to prevent false hangs */
4424 tx_buf->next_to_watch = NULL;
4426 tx_desc->buffer_addr = 0;
4427 tx_desc->cmd_type_offset_bsz = 0;
4428 /* move past filter desc */
4433 i -= tx_ring->count;
4434 tx_buf = tx_ring->tx_bi;
4435 tx_desc = I40E_TX_DESC(tx_ring, 0);
4437 /* unmap skb header data */
4438 dma_unmap_single(tx_ring->dev,
4439 dma_unmap_addr(tx_buf, dma),
4440 dma_unmap_len(tx_buf, len),
4442 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4443 kfree(tx_buf->raw_buf);
4445 tx_buf->raw_buf = NULL;
4446 tx_buf->tx_flags = 0;
4447 tx_buf->next_to_watch = NULL;
4448 dma_unmap_len_set(tx_buf, len, 0);
4449 tx_desc->buffer_addr = 0;
4450 tx_desc->cmd_type_offset_bsz = 0;
4452 /* move us past the eop_desc for start of next FD desc */
4457 i -= tx_ring->count;
4458 tx_buf = tx_ring->tx_bi;
4459 tx_desc = I40E_TX_DESC(tx_ring, 0);
4462 /* update budget accounting */
4464 } while (likely(budget));
4466 i += tx_ring->count;
4467 tx_ring->next_to_clean = i;
4469 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4470 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4476 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4477 * @irq: interrupt number
4478 * @data: pointer to a q_vector
4480 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4482 struct i40e_q_vector *q_vector = data;
4483 struct i40e_vsi *vsi;
4485 if (!q_vector->tx.ring)
4488 vsi = q_vector->tx.ring->vsi;
4489 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4495 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4496 * @vsi: the VSI being configured
4497 * @v_idx: vector index
4498 * @qp_idx: queue pair index
4500 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4502 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4503 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4504 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4506 tx_ring->q_vector = q_vector;
4507 tx_ring->next = q_vector->tx.ring;
4508 q_vector->tx.ring = tx_ring;
4509 q_vector->tx.count++;
4511 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4512 if (i40e_enabled_xdp_vsi(vsi)) {
4513 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4515 xdp_ring->q_vector = q_vector;
4516 xdp_ring->next = q_vector->tx.ring;
4517 q_vector->tx.ring = xdp_ring;
4518 q_vector->tx.count++;
4521 rx_ring->q_vector = q_vector;
4522 rx_ring->next = q_vector->rx.ring;
4523 q_vector->rx.ring = rx_ring;
4524 q_vector->rx.count++;
4528 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4529 * @vsi: the VSI being configured
4531 * This function maps descriptor rings to the queue-specific vectors
4532 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4533 * one vector per queue pair, but on a constrained vector budget, we
4534 * group the queue pairs as "efficiently" as possible.
4536 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4538 int qp_remaining = vsi->num_queue_pairs;
4539 int q_vectors = vsi->num_q_vectors;
4544 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4545 * group them so there are multiple queues per vector.
4546 * It is also important to go through all the vectors available to be
4547 * sure that if we don't use all the vectors, that the remaining vectors
4548 * are cleared. This is especially important when decreasing the
4549 * number of queues in use.
4551 for (; v_start < q_vectors; v_start++) {
4552 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4554 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4556 q_vector->num_ringpairs = num_ringpairs;
4557 q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4559 q_vector->rx.count = 0;
4560 q_vector->tx.count = 0;
4561 q_vector->rx.ring = NULL;
4562 q_vector->tx.ring = NULL;
4564 while (num_ringpairs--) {
4565 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4573 * i40e_vsi_request_irq - Request IRQ from the OS
4574 * @vsi: the VSI being configured
4575 * @basename: name for the vector
4577 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4579 struct i40e_pf *pf = vsi->back;
4582 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4583 err = i40e_vsi_request_irq_msix(vsi, basename);
4584 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4585 err = request_irq(pf->pdev->irq, i40e_intr, 0,
4588 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4592 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4597 #ifdef CONFIG_NET_POLL_CONTROLLER
4599 * i40e_netpoll - A Polling 'interrupt' handler
4600 * @netdev: network interface device structure
4602 * This is used by netconsole to send skbs without having to re-enable
4603 * interrupts. It's not called while the normal interrupt routine is executing.
4605 static void i40e_netpoll(struct net_device *netdev)
4607 struct i40e_netdev_priv *np = netdev_priv(netdev);
4608 struct i40e_vsi *vsi = np->vsi;
4609 struct i40e_pf *pf = vsi->back;
4612 /* if interface is down do nothing */
4613 if (test_bit(__I40E_VSI_DOWN, vsi->state))
4616 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4617 for (i = 0; i < vsi->num_q_vectors; i++)
4618 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4620 i40e_intr(pf->pdev->irq, netdev);
4625 #define I40E_QTX_ENA_WAIT_COUNT 50
4628 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4629 * @pf: the PF being configured
4630 * @pf_q: the PF queue
4631 * @enable: enable or disable state of the queue
4633 * This routine will wait for the given Tx queue of the PF to reach the
4634 * enabled or disabled state.
4635 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4636 * multiple retries; else will return 0 in case of success.
4638 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4643 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4644 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4645 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4648 usleep_range(10, 20);
4650 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4657 * i40e_control_tx_q - Start or stop a particular Tx queue
4658 * @pf: the PF structure
4659 * @pf_q: the PF queue to configure
4660 * @enable: start or stop the queue
4662 * This function enables or disables a single queue. Note that any delay
4663 * required after the operation is expected to be handled by the caller of
4666 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4668 struct i40e_hw *hw = &pf->hw;
4672 /* warn the TX unit of coming changes */
4673 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4675 usleep_range(10, 20);
4677 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4678 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4679 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4680 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4682 usleep_range(1000, 2000);
4685 /* Skip if the queue is already in the requested state */
4686 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4689 /* turn on/off the queue */
4691 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4692 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4694 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4697 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4701 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4703 * @pf: the PF structure
4704 * @pf_q: the PF queue to configure
4705 * @is_xdp: true if the queue is used for XDP
4706 * @enable: start or stop the queue
4708 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4709 bool is_xdp, bool enable)
4713 i40e_control_tx_q(pf, pf_q, enable);
4715 /* wait for the change to finish */
4716 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4718 dev_info(&pf->pdev->dev,
4719 "VSI seid %d %sTx ring %d %sable timeout\n",
4720 seid, (is_xdp ? "XDP " : ""), pf_q,
4721 (enable ? "en" : "dis"));
4728 * i40e_vsi_enable_tx - Start a VSI's rings
4729 * @vsi: the VSI being configured
4731 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4733 struct i40e_pf *pf = vsi->back;
4734 int i, pf_q, ret = 0;
4736 pf_q = vsi->base_queue;
4737 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4738 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4740 false /*is xdp*/, true);
4744 if (!i40e_enabled_xdp_vsi(vsi))
4747 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4748 pf_q + vsi->alloc_queue_pairs,
4749 true /*is xdp*/, true);
4757 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4758 * @pf: the PF being configured
4759 * @pf_q: the PF queue
4760 * @enable: enable or disable state of the queue
4762 * This routine will wait for the given Rx queue of the PF to reach the
4763 * enabled or disabled state.
4764 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4765 * multiple retries; else will return 0 in case of success.
4767 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4772 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4773 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4774 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4777 usleep_range(10, 20);
4779 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4786 * i40e_control_rx_q - Start or stop a particular Rx queue
4787 * @pf: the PF structure
4788 * @pf_q: the PF queue to configure
4789 * @enable: start or stop the queue
4791 * This function enables or disables a single queue. Note that
4792 * any delay required after the operation is expected to be
4793 * handled by the caller of this function.
4795 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4797 struct i40e_hw *hw = &pf->hw;
4801 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4802 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4803 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4804 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4806 usleep_range(1000, 2000);
4809 /* Skip if the queue is already in the requested state */
4810 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4813 /* turn on/off the queue */
4815 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4817 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4819 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4823 * i40e_control_wait_rx_q
4824 * @pf: the PF structure
4825 * @pf_q: queue being configured
4826 * @enable: start or stop the rings
4828 * This function enables or disables a single queue along with waiting
4829 * for the change to finish. The caller of this function should handle
4830 * the delays needed in the case of disabling queues.
4832 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4836 i40e_control_rx_q(pf, pf_q, enable);
4838 /* wait for the change to finish */
4839 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4847 * i40e_vsi_enable_rx - Start a VSI's rings
4848 * @vsi: the VSI being configured
4850 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4852 struct i40e_pf *pf = vsi->back;
4853 int i, pf_q, ret = 0;
4855 pf_q = vsi->base_queue;
4856 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4857 ret = i40e_control_wait_rx_q(pf, pf_q, true);
4859 dev_info(&pf->pdev->dev,
4860 "VSI seid %d Rx ring %d enable timeout\n",
4870 * i40e_vsi_start_rings - Start a VSI's rings
4871 * @vsi: the VSI being configured
4873 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4877 /* do rx first for enable and last for disable */
4878 ret = i40e_vsi_enable_rx(vsi);
4881 ret = i40e_vsi_enable_tx(vsi);
4886 #define I40E_DISABLE_TX_GAP_MSEC 50
4889 * i40e_vsi_stop_rings - Stop a VSI's rings
4890 * @vsi: the VSI being configured
4892 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4894 struct i40e_pf *pf = vsi->back;
4895 int pf_q, err, q_end;
4897 /* When port TX is suspended, don't wait */
4898 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4899 return i40e_vsi_stop_rings_no_wait(vsi);
4901 q_end = vsi->base_queue + vsi->num_queue_pairs;
4902 for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4903 i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4905 for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4906 err = i40e_control_wait_rx_q(pf, pf_q, false);
4908 dev_info(&pf->pdev->dev,
4909 "VSI seid %d Rx ring %d disable timeout\n",
4913 msleep(I40E_DISABLE_TX_GAP_MSEC);
4914 pf_q = vsi->base_queue;
4915 for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4916 wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4918 i40e_vsi_wait_queues_disabled(vsi);
4922 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4923 * @vsi: the VSI being shutdown
4925 * This function stops all the rings for a VSI but does not delay to verify
4926 * that rings have been disabled. It is expected that the caller is shutting
4927 * down multiple VSIs at once and will delay together for all the VSIs after
4928 * initiating the shutdown. This is particularly useful for shutting down lots
4929 * of VFs together. Otherwise, a large delay can be incurred while configuring
4930 * each VSI in serial.
4932 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4934 struct i40e_pf *pf = vsi->back;
4937 pf_q = vsi->base_queue;
4938 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4939 i40e_control_tx_q(pf, pf_q, false);
4940 i40e_control_rx_q(pf, pf_q, false);
4945 * i40e_vsi_free_irq - Free the irq association with the OS
4946 * @vsi: the VSI being configured
4948 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4950 struct i40e_pf *pf = vsi->back;
4951 struct i40e_hw *hw = &pf->hw;
4952 int base = vsi->base_vector;
4956 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4957 if (!vsi->q_vectors)
4960 if (!vsi->irqs_ready)
4963 vsi->irqs_ready = false;
4964 for (i = 0; i < vsi->num_q_vectors; i++) {
4969 irq_num = pf->msix_entries[vector].vector;
4971 /* free only the irqs that were actually requested */
4972 if (!vsi->q_vectors[i] ||
4973 !vsi->q_vectors[i]->num_ringpairs)
4976 /* clear the affinity notifier in the IRQ descriptor */
4977 irq_set_affinity_notifier(irq_num, NULL);
4978 /* remove our suggested affinity mask for this IRQ */
4979 irq_update_affinity_hint(irq_num, NULL);
4980 free_irq(irq_num, vsi->q_vectors[i]);
4982 /* Tear down the interrupt queue link list
4984 * We know that they come in pairs and always
4985 * the Rx first, then the Tx. To clear the
4986 * link list, stick the EOL value into the
4987 * next_q field of the registers.
4989 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4990 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4991 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4992 val |= I40E_QUEUE_END_OF_LIST
4993 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4994 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4996 while (qp != I40E_QUEUE_END_OF_LIST) {
4999 val = rd32(hw, I40E_QINT_RQCTL(qp));
5001 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
5002 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5003 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
5004 I40E_QINT_RQCTL_INTEVENT_MASK);
5006 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5007 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5009 wr32(hw, I40E_QINT_RQCTL(qp), val);
5011 val = rd32(hw, I40E_QINT_TQCTL(qp));
5013 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5014 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5016 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
5017 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5018 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
5019 I40E_QINT_TQCTL_INTEVENT_MASK);
5021 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5022 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5024 wr32(hw, I40E_QINT_TQCTL(qp), val);
5029 free_irq(pf->pdev->irq, pf);
5031 val = rd32(hw, I40E_PFINT_LNKLST0);
5032 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5033 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5034 val |= I40E_QUEUE_END_OF_LIST
5035 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5036 wr32(hw, I40E_PFINT_LNKLST0, val);
5038 val = rd32(hw, I40E_QINT_RQCTL(qp));
5039 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
5040 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5041 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
5042 I40E_QINT_RQCTL_INTEVENT_MASK);
5044 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5045 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5047 wr32(hw, I40E_QINT_RQCTL(qp), val);
5049 val = rd32(hw, I40E_QINT_TQCTL(qp));
5051 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
5052 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5053 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
5054 I40E_QINT_TQCTL_INTEVENT_MASK);
5056 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5057 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5059 wr32(hw, I40E_QINT_TQCTL(qp), val);
5064 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5065 * @vsi: the VSI being configured
5066 * @v_idx: Index of vector to be freed
5068 * This function frees the memory allocated to the q_vector. In addition if
5069 * NAPI is enabled it will delete any references to the NAPI struct prior
5070 * to freeing the q_vector.
5072 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5074 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5075 struct i40e_ring *ring;
5080 /* disassociate q_vector from rings */
5081 i40e_for_each_ring(ring, q_vector->tx)
5082 ring->q_vector = NULL;
5084 i40e_for_each_ring(ring, q_vector->rx)
5085 ring->q_vector = NULL;
5087 /* only VSI w/ an associated netdev is set up w/ NAPI */
5089 netif_napi_del(&q_vector->napi);
5091 vsi->q_vectors[v_idx] = NULL;
5093 kfree_rcu(q_vector, rcu);
5097 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5098 * @vsi: the VSI being un-configured
5100 * This frees the memory allocated to the q_vectors and
5101 * deletes references to the NAPI struct.
5103 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5107 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5108 i40e_free_q_vector(vsi, v_idx);
5112 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5113 * @pf: board private structure
5115 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5117 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5118 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5119 pci_disable_msix(pf->pdev);
5120 kfree(pf->msix_entries);
5121 pf->msix_entries = NULL;
5122 kfree(pf->irq_pile);
5123 pf->irq_pile = NULL;
5124 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5125 pci_disable_msi(pf->pdev);
5127 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5131 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5132 * @pf: board private structure
5134 * We go through and clear interrupt specific resources and reset the structure
5135 * to pre-load conditions
5137 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5141 if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5142 i40e_free_misc_vector(pf);
5144 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5145 I40E_IWARP_IRQ_PILE_ID);
5147 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5148 for (i = 0; i < pf->num_alloc_vsi; i++)
5150 i40e_vsi_free_q_vectors(pf->vsi[i]);
5151 i40e_reset_interrupt_capability(pf);
5155 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5156 * @vsi: the VSI being configured
5158 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5165 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5166 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5168 if (q_vector->rx.ring || q_vector->tx.ring)
5169 napi_enable(&q_vector->napi);
5174 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5175 * @vsi: the VSI being configured
5177 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5184 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5185 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5187 if (q_vector->rx.ring || q_vector->tx.ring)
5188 napi_disable(&q_vector->napi);
5193 * i40e_vsi_close - Shut down a VSI
5194 * @vsi: the vsi to be quelled
5196 static void i40e_vsi_close(struct i40e_vsi *vsi)
5198 struct i40e_pf *pf = vsi->back;
5199 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5201 i40e_vsi_free_irq(vsi);
5202 i40e_vsi_free_tx_resources(vsi);
5203 i40e_vsi_free_rx_resources(vsi);
5204 vsi->current_netdev_flags = 0;
5205 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5206 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5207 set_bit(__I40E_CLIENT_RESET, pf->state);
5211 * i40e_quiesce_vsi - Pause a given VSI
5212 * @vsi: the VSI being paused
5214 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5216 if (test_bit(__I40E_VSI_DOWN, vsi->state))
5219 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5220 if (vsi->netdev && netif_running(vsi->netdev))
5221 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5223 i40e_vsi_close(vsi);
5227 * i40e_unquiesce_vsi - Resume a given VSI
5228 * @vsi: the VSI being resumed
5230 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5232 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5235 if (vsi->netdev && netif_running(vsi->netdev))
5236 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5238 i40e_vsi_open(vsi); /* this clears the DOWN bit */
5242 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5245 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5249 for (v = 0; v < pf->num_alloc_vsi; v++) {
5251 i40e_quiesce_vsi(pf->vsi[v]);
5256 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5259 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5263 for (v = 0; v < pf->num_alloc_vsi; v++) {
5265 i40e_unquiesce_vsi(pf->vsi[v]);
5270 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5271 * @vsi: the VSI being configured
5273 * Wait until all queues on a given VSI have been disabled.
5275 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5277 struct i40e_pf *pf = vsi->back;
5280 pf_q = vsi->base_queue;
5281 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5282 /* Check and wait for the Tx queue */
5283 ret = i40e_pf_txq_wait(pf, pf_q, false);
5285 dev_info(&pf->pdev->dev,
5286 "VSI seid %d Tx ring %d disable timeout\n",
5291 if (!i40e_enabled_xdp_vsi(vsi))
5294 /* Check and wait for the XDP Tx queue */
5295 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5298 dev_info(&pf->pdev->dev,
5299 "VSI seid %d XDP Tx ring %d disable timeout\n",
5304 /* Check and wait for the Rx queue */
5305 ret = i40e_pf_rxq_wait(pf, pf_q, false);
5307 dev_info(&pf->pdev->dev,
5308 "VSI seid %d Rx ring %d disable timeout\n",
5317 #ifdef CONFIG_I40E_DCB
5319 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5322 * This function waits for the queues to be in disabled state for all the
5323 * VSIs that are managed by this PF.
5325 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5329 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5331 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5343 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5344 * @pf: pointer to PF
5346 * Get TC map for ISCSI PF type that will include iSCSI TC
5349 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5351 struct i40e_dcb_app_priority_table app;
5352 struct i40e_hw *hw = &pf->hw;
5353 u8 enabled_tc = 1; /* TC0 is always enabled */
5355 /* Get the iSCSI APP TLV */
5356 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5358 for (i = 0; i < dcbcfg->numapps; i++) {
5359 app = dcbcfg->app[i];
5360 if (app.selector == I40E_APP_SEL_TCPIP &&
5361 app.protocolid == I40E_APP_PROTOID_ISCSI) {
5362 tc = dcbcfg->etscfg.prioritytable[app.priority];
5363 enabled_tc |= BIT(tc);
5372 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
5373 * @dcbcfg: the corresponding DCBx configuration structure
5375 * Return the number of TCs from given DCBx configuration
5377 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5379 int i, tc_unused = 0;
5383 /* Scan the ETS Config Priority Table to find
5384 * traffic class enabled for a given priority
5385 * and create a bitmask of enabled TCs
5387 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5388 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5390 /* Now scan the bitmask to check for
5391 * contiguous TCs starting with TC0
5393 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5394 if (num_tc & BIT(i)) {
5398 pr_err("Non-contiguous TC - Disabling DCB\n");
5406 /* There is always at least TC0 */
5414 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5415 * @dcbcfg: the corresponding DCBx configuration structure
5417 * Query the current DCB configuration and return the number of
5418 * traffic classes enabled from the given DCBX config
5420 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5422 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5426 for (i = 0; i < num_tc; i++)
5427 enabled_tc |= BIT(i);
5433 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5434 * @pf: PF being queried
5436 * Query the current MQPRIO configuration and return the number of
5437 * traffic classes enabled.
5439 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5441 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5442 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5443 u8 enabled_tc = 1, i;
5445 for (i = 1; i < num_tc; i++)
5446 enabled_tc |= BIT(i);
5451 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5452 * @pf: PF being queried
5454 * Return number of traffic classes enabled for the given PF
5456 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5458 struct i40e_hw *hw = &pf->hw;
5459 u8 i, enabled_tc = 1;
5461 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5463 if (i40e_is_tc_mqprio_enabled(pf))
5464 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5466 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
5467 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5470 /* SFP mode will be enabled for all TCs on port */
5471 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5472 return i40e_dcb_get_num_tc(dcbcfg);
5474 /* MFP mode return count of enabled TCs for this PF */
5475 if (pf->hw.func_caps.iscsi)
5476 enabled_tc = i40e_get_iscsi_tc_map(pf);
5478 return 1; /* Only TC0 */
5480 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5481 if (enabled_tc & BIT(i))
5488 * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5489 * @pf: PF being queried
5491 * Return a bitmap for enabled traffic classes for this PF.
5493 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5495 if (i40e_is_tc_mqprio_enabled(pf))
5496 return i40e_mqprio_get_enabled_tc(pf);
5498 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5501 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5502 return I40E_DEFAULT_TRAFFIC_CLASS;
5504 /* SFP mode we want PF to be enabled for all TCs */
5505 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5506 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5508 /* MFP enabled and iSCSI PF type */
5509 if (pf->hw.func_caps.iscsi)
5510 return i40e_get_iscsi_tc_map(pf);
5512 return I40E_DEFAULT_TRAFFIC_CLASS;
5516 * i40e_vsi_get_bw_info - Query VSI BW Information
5517 * @vsi: the VSI being queried
5519 * Returns 0 on success, negative value on failure
5521 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5523 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5524 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5525 struct i40e_pf *pf = vsi->back;
5526 struct i40e_hw *hw = &pf->hw;
5531 /* Get the VSI level BW configuration */
5532 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5534 dev_info(&pf->pdev->dev,
5535 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5536 i40e_stat_str(&pf->hw, ret),
5537 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5541 /* Get the VSI level BW configuration per TC */
5542 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5545 dev_info(&pf->pdev->dev,
5546 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5547 i40e_stat_str(&pf->hw, ret),
5548 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5552 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5553 dev_info(&pf->pdev->dev,
5554 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5555 bw_config.tc_valid_bits,
5556 bw_ets_config.tc_valid_bits);
5557 /* Still continuing */
5560 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5561 vsi->bw_max_quanta = bw_config.max_bw;
5562 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5563 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5564 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5565 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5566 vsi->bw_ets_limit_credits[i] =
5567 le16_to_cpu(bw_ets_config.credits[i]);
5568 /* 3 bits out of 4 for each TC */
5569 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5576 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5577 * @vsi: the VSI being configured
5578 * @enabled_tc: TC bitmap
5579 * @bw_share: BW shared credits per TC
5581 * Returns 0 on success, negative value on failure
5583 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5586 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5587 struct i40e_pf *pf = vsi->back;
5591 /* There is no need to reset BW when mqprio mode is on. */
5592 if (i40e_is_tc_mqprio_enabled(pf))
5594 if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5595 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5597 dev_info(&pf->pdev->dev,
5598 "Failed to reset tx rate for vsi->seid %u\n",
5602 memset(&bw_data, 0, sizeof(bw_data));
5603 bw_data.tc_valid_bits = enabled_tc;
5604 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5605 bw_data.tc_bw_credits[i] = bw_share[i];
5607 ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5609 dev_info(&pf->pdev->dev,
5610 "AQ command Config VSI BW allocation per TC failed = %d\n",
5611 pf->hw.aq.asq_last_status);
5615 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5616 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5622 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5623 * @vsi: the VSI being configured
5624 * @enabled_tc: TC map to be enabled
5627 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5629 struct net_device *netdev = vsi->netdev;
5630 struct i40e_pf *pf = vsi->back;
5631 struct i40e_hw *hw = &pf->hw;
5634 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5640 netdev_reset_tc(netdev);
5644 /* Set up actual enabled TCs on the VSI */
5645 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5648 /* set per TC queues for the VSI */
5649 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5650 /* Only set TC queues for enabled tcs
5652 * e.g. For a VSI that has TC0 and TC3 enabled the
5653 * enabled_tc bitmap would be 0x00001001; the driver
5654 * will set the numtc for netdev as 2 that will be
5655 * referenced by the netdev layer as TC 0 and 1.
5657 if (vsi->tc_config.enabled_tc & BIT(i))
5658 netdev_set_tc_queue(netdev,
5659 vsi->tc_config.tc_info[i].netdev_tc,
5660 vsi->tc_config.tc_info[i].qcount,
5661 vsi->tc_config.tc_info[i].qoffset);
5664 if (i40e_is_tc_mqprio_enabled(pf))
5667 /* Assign UP2TC map for the VSI */
5668 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5669 /* Get the actual TC# for the UP */
5670 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5671 /* Get the mapped netdev TC# for the UP */
5672 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5673 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5678 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5679 * @vsi: the VSI being configured
5680 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5682 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5683 struct i40e_vsi_context *ctxt)
5685 /* copy just the sections touched not the entire info
5686 * since not all sections are valid as returned by
5689 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5690 memcpy(&vsi->info.queue_mapping,
5691 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5692 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5693 sizeof(vsi->info.tc_mapping));
5697 * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5698 * @vsi: the VSI being reconfigured
5699 * @vsi_offset: offset from main VF VSI
5701 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5703 struct i40e_vsi_context ctxt = {};
5709 return I40E_ERR_PARAM;
5713 ctxt.seid = vsi->seid;
5714 ctxt.pf_num = hw->pf_id;
5715 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5716 ctxt.uplink_seid = vsi->uplink_seid;
5717 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5718 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5719 ctxt.info = vsi->info;
5721 i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5723 if (vsi->reconfig_rss) {
5724 vsi->rss_size = min_t(int, pf->alloc_rss_size,
5725 vsi->num_queue_pairs);
5726 ret = i40e_vsi_config_rss(vsi);
5728 dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5731 vsi->reconfig_rss = false;
5734 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5736 dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5737 i40e_stat_str(hw, ret),
5738 i40e_aq_str(hw, hw->aq.asq_last_status));
5741 /* update the local VSI info with updated queue map */
5742 i40e_vsi_update_queue_map(vsi, &ctxt);
5743 vsi->info.valid_sections = 0;
5749 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5750 * @vsi: VSI to be configured
5751 * @enabled_tc: TC bitmap
5753 * This configures a particular VSI for TCs that are mapped to the
5754 * given TC bitmap. It uses default bandwidth share for TCs across
5755 * VSIs to configure TC for a particular VSI.
5758 * It is expected that the VSI queues have been quisced before calling
5761 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5763 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5764 struct i40e_pf *pf = vsi->back;
5765 struct i40e_hw *hw = &pf->hw;
5766 struct i40e_vsi_context ctxt;
5770 /* Check if enabled_tc is same as existing or new TCs */
5771 if (vsi->tc_config.enabled_tc == enabled_tc &&
5772 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5775 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5776 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5777 if (enabled_tc & BIT(i))
5781 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5783 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5785 dev_info(&pf->pdev->dev,
5786 "Failed configuring TC map %d for VSI %d\n",
5787 enabled_tc, vsi->seid);
5788 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5791 dev_info(&pf->pdev->dev,
5792 "Failed querying vsi bw info, err %s aq_err %s\n",
5793 i40e_stat_str(hw, ret),
5794 i40e_aq_str(hw, hw->aq.asq_last_status));
5797 if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5798 u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5801 valid_tc = bw_config.tc_valid_bits;
5802 /* Always enable TC0, no matter what */
5804 dev_info(&pf->pdev->dev,
5805 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5806 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5807 enabled_tc = valid_tc;
5810 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5812 dev_err(&pf->pdev->dev,
5813 "Unable to configure TC map %d for VSI %d\n",
5814 enabled_tc, vsi->seid);
5819 /* Update Queue Pairs Mapping for currently enabled UPs */
5820 ctxt.seid = vsi->seid;
5821 ctxt.pf_num = vsi->back->hw.pf_id;
5823 ctxt.uplink_seid = vsi->uplink_seid;
5824 ctxt.info = vsi->info;
5825 if (i40e_is_tc_mqprio_enabled(pf)) {
5826 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5830 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5833 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5836 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5837 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5838 vsi->num_queue_pairs);
5839 ret = i40e_vsi_config_rss(vsi);
5841 dev_info(&vsi->back->pdev->dev,
5842 "Failed to reconfig rss for num_queues\n");
5845 vsi->reconfig_rss = false;
5847 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5848 ctxt.info.valid_sections |=
5849 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5850 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5853 /* Update the VSI after updating the VSI queue-mapping
5856 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5858 dev_info(&pf->pdev->dev,
5859 "Update vsi tc config failed, err %s aq_err %s\n",
5860 i40e_stat_str(hw, ret),
5861 i40e_aq_str(hw, hw->aq.asq_last_status));
5864 /* update the local VSI info with updated queue map */
5865 i40e_vsi_update_queue_map(vsi, &ctxt);
5866 vsi->info.valid_sections = 0;
5868 /* Update current VSI BW information */
5869 ret = i40e_vsi_get_bw_info(vsi);
5871 dev_info(&pf->pdev->dev,
5872 "Failed updating vsi bw info, err %s aq_err %s\n",
5873 i40e_stat_str(hw, ret),
5874 i40e_aq_str(hw, hw->aq.asq_last_status));
5878 /* Update the netdev TC setup */
5879 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5885 * i40e_get_link_speed - Returns link speed for the interface
5886 * @vsi: VSI to be configured
5889 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5891 struct i40e_pf *pf = vsi->back;
5893 switch (pf->hw.phy.link_info.link_speed) {
5894 case I40E_LINK_SPEED_40GB:
5896 case I40E_LINK_SPEED_25GB:
5898 case I40E_LINK_SPEED_20GB:
5900 case I40E_LINK_SPEED_10GB:
5902 case I40E_LINK_SPEED_1GB:
5910 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5911 * @vsi: VSI to be configured
5912 * @seid: seid of the channel/VSI
5913 * @max_tx_rate: max TX rate to be configured as BW limit
5915 * Helper function to set BW limit for a given VSI
5917 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5919 struct i40e_pf *pf = vsi->back;
5924 speed = i40e_get_link_speed(vsi);
5925 if (max_tx_rate > speed) {
5926 dev_err(&pf->pdev->dev,
5927 "Invalid max tx rate %llu specified for VSI seid %d.",
5931 if (max_tx_rate && max_tx_rate < 50) {
5932 dev_warn(&pf->pdev->dev,
5933 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5937 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
5938 credits = max_tx_rate;
5939 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5940 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5941 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5943 dev_err(&pf->pdev->dev,
5944 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5945 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5946 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5951 * i40e_remove_queue_channels - Remove queue channels for the TCs
5952 * @vsi: VSI to be configured
5954 * Remove queue channels for the TCs
5956 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5958 enum i40e_admin_queue_err last_aq_status;
5959 struct i40e_cloud_filter *cfilter;
5960 struct i40e_channel *ch, *ch_tmp;
5961 struct i40e_pf *pf = vsi->back;
5962 struct hlist_node *node;
5965 /* Reset rss size that was stored when reconfiguring rss for
5966 * channel VSIs with non-power-of-2 queue count.
5968 vsi->current_rss_size = 0;
5970 /* perform cleanup for channels if they exist */
5971 if (list_empty(&vsi->ch_list))
5974 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5975 struct i40e_vsi *p_vsi;
5977 list_del(&ch->list);
5978 p_vsi = ch->parent_vsi;
5979 if (!p_vsi || !ch->initialized) {
5983 /* Reset queue contexts */
5984 for (i = 0; i < ch->num_queue_pairs; i++) {
5985 struct i40e_ring *tx_ring, *rx_ring;
5988 pf_q = ch->base_queue + i;
5989 tx_ring = vsi->tx_rings[pf_q];
5992 rx_ring = vsi->rx_rings[pf_q];
5996 /* Reset BW configured for this VSI via mqprio */
5997 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5999 dev_info(&vsi->back->pdev->dev,
6000 "Failed to reset tx rate for ch->seid %u\n",
6003 /* delete cloud filters associated with this channel */
6004 hlist_for_each_entry_safe(cfilter, node,
6005 &pf->cloud_filter_list, cloud_node) {
6006 if (cfilter->seid != ch->seid)
6009 hash_del(&cfilter->cloud_node);
6010 if (cfilter->dst_port)
6011 ret = i40e_add_del_cloud_filter_big_buf(vsi,
6015 ret = i40e_add_del_cloud_filter(vsi, cfilter,
6017 last_aq_status = pf->hw.aq.asq_last_status;
6019 dev_info(&pf->pdev->dev,
6020 "Failed to delete cloud filter, err %s aq_err %s\n",
6021 i40e_stat_str(&pf->hw, ret),
6022 i40e_aq_str(&pf->hw, last_aq_status));
6026 /* delete VSI from FW */
6027 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6030 dev_err(&vsi->back->pdev->dev,
6031 "unable to remove channel (%d) for parent VSI(%d)\n",
6032 ch->seid, p_vsi->seid);
6035 INIT_LIST_HEAD(&vsi->ch_list);
6039 * i40e_get_max_queues_for_channel
6040 * @vsi: ptr to VSI to which channels are associated with
6042 * Helper function which returns max value among the queue counts set on the
6043 * channels/TCs created.
6045 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6047 struct i40e_channel *ch, *ch_tmp;
6050 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6051 if (!ch->initialized)
6053 if (ch->num_queue_pairs > max)
6054 max = ch->num_queue_pairs;
6061 * i40e_validate_num_queues - validate num_queues w.r.t channel
6062 * @pf: ptr to PF device
6063 * @num_queues: number of queues
6064 * @vsi: the parent VSI
6065 * @reconfig_rss: indicates should the RSS be reconfigured or not
6067 * This function validates number of queues in the context of new channel
6068 * which is being established and determines if RSS should be reconfigured
6069 * or not for parent VSI.
6071 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6072 struct i40e_vsi *vsi, bool *reconfig_rss)
6079 *reconfig_rss = false;
6080 if (vsi->current_rss_size) {
6081 if (num_queues > vsi->current_rss_size) {
6082 dev_dbg(&pf->pdev->dev,
6083 "Error: num_queues (%d) > vsi's current_size(%d)\n",
6084 num_queues, vsi->current_rss_size);
6086 } else if ((num_queues < vsi->current_rss_size) &&
6087 (!is_power_of_2(num_queues))) {
6088 dev_dbg(&pf->pdev->dev,
6089 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6090 num_queues, vsi->current_rss_size);
6095 if (!is_power_of_2(num_queues)) {
6096 /* Find the max num_queues configured for channel if channel
6098 * if channel exist, then enforce 'num_queues' to be more than
6099 * max ever queues configured for channel.
6101 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6102 if (num_queues < max_ch_queues) {
6103 dev_dbg(&pf->pdev->dev,
6104 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
6105 num_queues, max_ch_queues);
6108 *reconfig_rss = true;
6115 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6116 * @vsi: the VSI being setup
6117 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6119 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6121 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6123 struct i40e_pf *pf = vsi->back;
6124 u8 seed[I40E_HKEY_ARRAY_SIZE];
6125 struct i40e_hw *hw = &pf->hw;
6133 if (rss_size > vsi->rss_size)
6136 local_rss_size = min_t(int, vsi->rss_size, rss_size);
6137 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6141 /* Ignoring user configured lut if there is one */
6142 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6144 /* Use user configured hash key if there is one, otherwise
6147 if (vsi->rss_hkey_user)
6148 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6150 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6152 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6154 dev_info(&pf->pdev->dev,
6155 "Cannot set RSS lut, err %s aq_err %s\n",
6156 i40e_stat_str(hw, ret),
6157 i40e_aq_str(hw, hw->aq.asq_last_status));
6163 /* Do the update w.r.t. storing rss_size */
6164 if (!vsi->orig_rss_size)
6165 vsi->orig_rss_size = vsi->rss_size;
6166 vsi->current_rss_size = local_rss_size;
6172 * i40e_channel_setup_queue_map - Setup a channel queue map
6173 * @pf: ptr to PF device
6174 * @ctxt: VSI context structure
6175 * @ch: ptr to channel structure
6177 * Setup queue map for a specific channel
6179 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6180 struct i40e_vsi_context *ctxt,
6181 struct i40e_channel *ch)
6183 u16 qcount, qmap, sections = 0;
6187 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6188 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6190 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6191 ch->num_queue_pairs = qcount;
6193 /* find the next higher power-of-2 of num queue pairs */
6194 pow = ilog2(qcount);
6195 if (!is_power_of_2(qcount))
6198 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6199 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6201 /* Setup queue TC[0].qmap for given VSI context */
6202 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6204 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6205 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6206 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6207 ctxt->info.valid_sections |= cpu_to_le16(sections);
6211 * i40e_add_channel - add a channel by adding VSI
6212 * @pf: ptr to PF device
6213 * @uplink_seid: underlying HW switching element (VEB) ID
6214 * @ch: ptr to channel structure
6216 * Add a channel (VSI) using add_vsi and queue_map
6218 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6219 struct i40e_channel *ch)
6221 struct i40e_hw *hw = &pf->hw;
6222 struct i40e_vsi_context ctxt;
6223 u8 enabled_tc = 0x1; /* TC0 enabled */
6226 if (ch->type != I40E_VSI_VMDQ2) {
6227 dev_info(&pf->pdev->dev,
6228 "add new vsi failed, ch->type %d\n", ch->type);
6232 memset(&ctxt, 0, sizeof(ctxt));
6233 ctxt.pf_num = hw->pf_id;
6235 ctxt.uplink_seid = uplink_seid;
6236 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6237 if (ch->type == I40E_VSI_VMDQ2)
6238 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6240 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6241 ctxt.info.valid_sections |=
6242 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6243 ctxt.info.switch_id =
6244 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6247 /* Set queue map for a given VSI context */
6248 i40e_channel_setup_queue_map(pf, &ctxt, ch);
6250 /* Now time to create VSI */
6251 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6253 dev_info(&pf->pdev->dev,
6254 "add new vsi failed, err %s aq_err %s\n",
6255 i40e_stat_str(&pf->hw, ret),
6256 i40e_aq_str(&pf->hw,
6257 pf->hw.aq.asq_last_status));
6261 /* Success, update channel, set enabled_tc only if the channel
6264 ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6265 ch->seid = ctxt.seid;
6266 ch->vsi_number = ctxt.vsi_number;
6267 ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6269 /* copy just the sections touched not the entire info
6270 * since not all sections are valid as returned by
6273 ch->info.mapping_flags = ctxt.info.mapping_flags;
6274 memcpy(&ch->info.queue_mapping,
6275 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6276 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6277 sizeof(ctxt.info.tc_mapping));
6282 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6285 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6289 memset(&bw_data, 0, sizeof(bw_data));
6290 bw_data.tc_valid_bits = ch->enabled_tc;
6291 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6292 bw_data.tc_bw_credits[i] = bw_share[i];
6294 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6297 dev_info(&vsi->back->pdev->dev,
6298 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6299 vsi->back->hw.aq.asq_last_status, ch->seid);
6303 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6304 ch->info.qs_handle[i] = bw_data.qs_handles[i];
6310 * i40e_channel_config_tx_ring - config TX ring associated with new channel
6311 * @pf: ptr to PF device
6312 * @vsi: the VSI being setup
6313 * @ch: ptr to channel structure
6315 * Configure TX rings associated with channel (VSI) since queues are being
6318 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6319 struct i40e_vsi *vsi,
6320 struct i40e_channel *ch)
6324 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6326 /* Enable ETS TCs with equal BW Share for now across all VSIs */
6327 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6328 if (ch->enabled_tc & BIT(i))
6332 /* configure BW for new VSI */
6333 ret = i40e_channel_config_bw(vsi, ch, bw_share);
6335 dev_info(&vsi->back->pdev->dev,
6336 "Failed configuring TC map %d for channel (seid %u)\n",
6337 ch->enabled_tc, ch->seid);
6341 for (i = 0; i < ch->num_queue_pairs; i++) {
6342 struct i40e_ring *tx_ring, *rx_ring;
6345 pf_q = ch->base_queue + i;
6347 /* Get to TX ring ptr of main VSI, for re-setup TX queue
6350 tx_ring = vsi->tx_rings[pf_q];
6353 /* Get the RX ring ptr */
6354 rx_ring = vsi->rx_rings[pf_q];
6362 * i40e_setup_hw_channel - setup new channel
6363 * @pf: ptr to PF device
6364 * @vsi: the VSI being setup
6365 * @ch: ptr to channel structure
6366 * @uplink_seid: underlying HW switching element (VEB) ID
6367 * @type: type of channel to be created (VMDq2/VF)
6369 * Setup new channel (VSI) based on specified type (VMDq2/VF)
6370 * and configures TX rings accordingly
6372 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6373 struct i40e_vsi *vsi,
6374 struct i40e_channel *ch,
6375 u16 uplink_seid, u8 type)
6379 ch->initialized = false;
6380 ch->base_queue = vsi->next_base_queue;
6383 /* Proceed with creation of channel (VMDq2) VSI */
6384 ret = i40e_add_channel(pf, uplink_seid, ch);
6386 dev_info(&pf->pdev->dev,
6387 "failed to add_channel using uplink_seid %u\n",
6392 /* Mark the successful creation of channel */
6393 ch->initialized = true;
6395 /* Reconfigure TX queues using QTX_CTL register */
6396 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6398 dev_info(&pf->pdev->dev,
6399 "failed to configure TX rings for channel %u\n",
6404 /* update 'next_base_queue' */
6405 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6406 dev_dbg(&pf->pdev->dev,
6407 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6408 ch->seid, ch->vsi_number, ch->stat_counter_idx,
6409 ch->num_queue_pairs,
6410 vsi->next_base_queue);
6415 * i40e_setup_channel - setup new channel using uplink element
6416 * @pf: ptr to PF device
6417 * @vsi: pointer to the VSI to set up the channel within
6418 * @ch: ptr to channel structure
6420 * Setup new channel (VSI) based on specified type (VMDq2/VF)
6421 * and uplink switching element (uplink_seid)
6423 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6424 struct i40e_channel *ch)
6430 if (vsi->type == I40E_VSI_MAIN) {
6431 vsi_type = I40E_VSI_VMDQ2;
6433 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6438 /* underlying switching element */
6439 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6441 /* create channel (VSI), configure TX rings */
6442 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6444 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6448 return ch->initialized ? true : false;
6452 * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6453 * @vsi: ptr to VSI which has PF backing
6455 * Sets up switch mode correctly if it needs to be changed and perform
6456 * what are allowed modes.
6458 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6461 struct i40e_pf *pf = vsi->back;
6462 struct i40e_hw *hw = &pf->hw;
6465 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6469 if (hw->dev_caps.switch_mode) {
6470 /* if switch mode is set, support mode2 (non-tunneled for
6471 * cloud filter) for now
6473 u32 switch_mode = hw->dev_caps.switch_mode &
6474 I40E_SWITCH_MODE_MASK;
6475 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6476 if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6478 dev_err(&pf->pdev->dev,
6479 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6480 hw->dev_caps.switch_mode);
6485 /* Set Bit 7 to be valid */
6486 mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6488 /* Set L4type for TCP support */
6489 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6491 /* Set cloud filter mode */
6492 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6494 /* Prep mode field for set_switch_config */
6495 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6496 pf->last_sw_conf_valid_flags,
6498 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6499 dev_err(&pf->pdev->dev,
6500 "couldn't set switch config bits, err %s aq_err %s\n",
6501 i40e_stat_str(hw, ret),
6503 hw->aq.asq_last_status));
6509 * i40e_create_queue_channel - function to create channel
6510 * @vsi: VSI to be configured
6511 * @ch: ptr to channel (it contains channel specific params)
6513 * This function creates channel (VSI) using num_queues specified by user,
6514 * reconfigs RSS if needed.
6516 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6517 struct i40e_channel *ch)
6519 struct i40e_pf *pf = vsi->back;
6526 if (!ch->num_queue_pairs) {
6527 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6528 ch->num_queue_pairs);
6532 /* validate user requested num_queues for channel */
6533 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6536 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6537 ch->num_queue_pairs);
6541 /* By default we are in VEPA mode, if this is the first VF/VMDq
6542 * VSI to be added switch to VEB mode.
6545 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6546 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6548 if (vsi->type == I40E_VSI_MAIN) {
6549 if (i40e_is_tc_mqprio_enabled(pf))
6550 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6552 i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6554 /* now onwards for main VSI, number of queues will be value
6555 * of TC0's queue count
6559 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6560 * it should be more than num_queues
6562 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6563 dev_dbg(&pf->pdev->dev,
6564 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6565 vsi->cnt_q_avail, ch->num_queue_pairs);
6569 /* reconfig_rss only if vsi type is MAIN_VSI */
6570 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6571 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6573 dev_info(&pf->pdev->dev,
6574 "Error: unable to reconfig rss for num_queues (%u)\n",
6575 ch->num_queue_pairs);
6580 if (!i40e_setup_channel(pf, vsi, ch)) {
6581 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6585 dev_info(&pf->pdev->dev,
6586 "Setup channel (id:%u) utilizing num_queues %d\n",
6587 ch->seid, ch->num_queue_pairs);
6589 /* configure VSI for BW limit */
6590 if (ch->max_tx_rate) {
6591 u64 credits = ch->max_tx_rate;
6593 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6596 do_div(credits, I40E_BW_CREDIT_DIVISOR);
6597 dev_dbg(&pf->pdev->dev,
6598 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6604 /* in case of VF, this will be main SRIOV VSI */
6605 ch->parent_vsi = vsi;
6607 /* and update main_vsi's count for queue_available to use */
6608 vsi->cnt_q_avail -= ch->num_queue_pairs;
6614 * i40e_configure_queue_channels - Add queue channel for the given TCs
6615 * @vsi: VSI to be configured
6617 * Configures queue channel mapping to the given TCs
6619 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6621 struct i40e_channel *ch;
6625 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6626 vsi->tc_seid_map[0] = vsi->seid;
6627 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6628 if (vsi->tc_config.enabled_tc & BIT(i)) {
6629 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6635 INIT_LIST_HEAD(&ch->list);
6636 ch->num_queue_pairs =
6637 vsi->tc_config.tc_info[i].qcount;
6639 vsi->tc_config.tc_info[i].qoffset;
6641 /* Bandwidth limit through tc interface is in bytes/s,
6644 max_rate = vsi->mqprio_qopt.max_rate[i];
6645 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6646 ch->max_tx_rate = max_rate;
6648 list_add_tail(&ch->list, &vsi->ch_list);
6650 ret = i40e_create_queue_channel(vsi, ch);
6652 dev_err(&vsi->back->pdev->dev,
6653 "Failed creating queue channel with TC%d: queues %d\n",
6654 i, ch->num_queue_pairs);
6657 vsi->tc_seid_map[i] = ch->seid;
6663 i40e_remove_queue_channels(vsi);
6668 * i40e_veb_config_tc - Configure TCs for given VEB
6670 * @enabled_tc: TC bitmap
6672 * Configures given TC bitmap for VEB (switching) element
6674 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6676 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6677 struct i40e_pf *pf = veb->pf;
6681 /* No TCs or already enabled TCs just return */
6682 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6685 bw_data.tc_valid_bits = enabled_tc;
6686 /* bw_data.absolute_credits is not set (relative) */
6688 /* Enable ETS TCs with equal BW Share for now */
6689 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6690 if (enabled_tc & BIT(i))
6691 bw_data.tc_bw_share_credits[i] = 1;
6694 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6697 dev_info(&pf->pdev->dev,
6698 "VEB bw config failed, err %s aq_err %s\n",
6699 i40e_stat_str(&pf->hw, ret),
6700 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6704 /* Update the BW information */
6705 ret = i40e_veb_get_bw_info(veb);
6707 dev_info(&pf->pdev->dev,
6708 "Failed getting veb bw config, err %s aq_err %s\n",
6709 i40e_stat_str(&pf->hw, ret),
6710 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6717 #ifdef CONFIG_I40E_DCB
6719 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6722 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6723 * the caller would've quiesce all the VSIs before calling
6726 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6732 /* Enable the TCs available on PF to all VEBs */
6733 tc_map = i40e_pf_get_tc_map(pf);
6734 if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6737 for (v = 0; v < I40E_MAX_VEB; v++) {
6740 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6742 dev_info(&pf->pdev->dev,
6743 "Failed configuring TC for VEB seid=%d\n",
6745 /* Will try to configure as many components */
6749 /* Update each VSI */
6750 for (v = 0; v < pf->num_alloc_vsi; v++) {
6754 /* - Enable all TCs for the LAN VSI
6755 * - For all others keep them at TC0 for now
6757 if (v == pf->lan_vsi)
6758 tc_map = i40e_pf_get_tc_map(pf);
6760 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6762 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6764 dev_info(&pf->pdev->dev,
6765 "Failed configuring TC for VSI seid=%d\n",
6767 /* Will try to configure as many components */
6769 /* Re-configure VSI vectors based on updated TC map */
6770 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6771 if (pf->vsi[v]->netdev)
6772 i40e_dcbnl_set_all(pf->vsi[v]);
6778 * i40e_resume_port_tx - Resume port Tx
6781 * Resume a port's Tx and issue a PF reset in case of failure to
6784 static int i40e_resume_port_tx(struct i40e_pf *pf)
6786 struct i40e_hw *hw = &pf->hw;
6789 ret = i40e_aq_resume_port_tx(hw, NULL);
6791 dev_info(&pf->pdev->dev,
6792 "Resume Port Tx failed, err %s aq_err %s\n",
6793 i40e_stat_str(&pf->hw, ret),
6794 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6795 /* Schedule PF reset to recover */
6796 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6797 i40e_service_event_schedule(pf);
6804 * i40e_suspend_port_tx - Suspend port Tx
6807 * Suspend a port's Tx and issue a PF reset in case of failure.
6809 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6811 struct i40e_hw *hw = &pf->hw;
6814 ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6816 dev_info(&pf->pdev->dev,
6817 "Suspend Port Tx failed, err %s aq_err %s\n",
6818 i40e_stat_str(&pf->hw, ret),
6819 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6820 /* Schedule PF reset to recover */
6821 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6822 i40e_service_event_schedule(pf);
6829 * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6830 * @pf: PF being configured
6831 * @new_cfg: New DCBX configuration
6833 * Program DCB settings into HW and reconfigure VEB/VSIs on
6834 * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6836 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6837 struct i40e_dcbx_config *new_cfg)
6839 struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6842 /* Check if need reconfiguration */
6843 if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6844 dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6848 /* Config change disable all VSIs */
6849 i40e_pf_quiesce_all_vsi(pf);
6851 /* Copy the new config to the current config */
6852 *old_cfg = *new_cfg;
6853 old_cfg->etsrec = old_cfg->etscfg;
6854 ret = i40e_set_dcb_config(&pf->hw);
6856 dev_info(&pf->pdev->dev,
6857 "Set DCB Config failed, err %s aq_err %s\n",
6858 i40e_stat_str(&pf->hw, ret),
6859 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6863 /* Changes in configuration update VEB/VSI */
6864 i40e_dcb_reconfigure(pf);
6866 /* In case of reset do not try to resume anything */
6867 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6868 /* Re-start the VSIs if disabled */
6869 ret = i40e_resume_port_tx(pf);
6870 /* In case of error no point in resuming VSIs */
6873 i40e_pf_unquiesce_all_vsi(pf);
6880 * i40e_hw_dcb_config - Program new DCBX settings into HW
6881 * @pf: PF being configured
6882 * @new_cfg: New DCBX configuration
6884 * Program DCB settings into HW and reconfigure VEB/VSIs on
6887 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6889 struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6890 u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6891 u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6892 struct i40e_dcbx_config *old_cfg;
6893 u8 mode[I40E_MAX_TRAFFIC_CLASS];
6894 struct i40e_rx_pb_config pb_cfg;
6895 struct i40e_hw *hw = &pf->hw;
6896 u8 num_ports = hw->num_ports;
6904 dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6905 /* Un-pack information to Program ETS HW via shared API
6908 * ETS/NON-ETS arbiter mode
6909 * max exponent (credit refills)
6910 * Total number of ports
6911 * PFC priority bit-map
6914 * Arbiter mode between UPs sharing same TC
6915 * TSA table (ETS or non-ETS)
6916 * EEE enabled or not
6920 new_numtc = i40e_dcb_get_num_tc(new_cfg);
6922 memset(&ets_data, 0, sizeof(ets_data));
6923 for (i = 0; i < new_numtc; i++) {
6925 switch (new_cfg->etscfg.tsatable[i]) {
6926 case I40E_IEEE_TSA_ETS:
6927 prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6928 ets_data.tc_bw_share_credits[i] =
6929 new_cfg->etscfg.tcbwtable[i];
6931 case I40E_IEEE_TSA_STRICT:
6932 prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6934 ets_data.tc_bw_share_credits[i] =
6935 I40E_DCB_STRICT_PRIO_CREDITS;
6938 /* Invalid TSA type */
6939 need_reconfig = false;
6944 old_cfg = &hw->local_dcbx_config;
6945 /* Check if need reconfiguration */
6946 need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6948 /* If needed, enable/disable frame tagging, disable all VSIs
6949 * and suspend port tx
6951 if (need_reconfig) {
6952 /* Enable DCB tagging only when more than one TC */
6954 pf->flags |= I40E_FLAG_DCB_ENABLED;
6956 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6958 set_bit(__I40E_PORT_SUSPENDED, pf->state);
6959 /* Reconfiguration needed quiesce all VSIs */
6960 i40e_pf_quiesce_all_vsi(pf);
6961 ret = i40e_suspend_port_tx(pf);
6966 /* Configure Port ETS Tx Scheduler */
6967 ets_data.tc_valid_bits = tc_map;
6968 ets_data.tc_strict_priority_flags = lltc_map;
6969 ret = i40e_aq_config_switch_comp_ets
6970 (hw, pf->mac_seid, &ets_data,
6971 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6973 dev_info(&pf->pdev->dev,
6974 "Modify Port ETS failed, err %s aq_err %s\n",
6975 i40e_stat_str(&pf->hw, ret),
6976 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6980 /* Configure Rx ETS HW */
6981 memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6982 i40e_dcb_hw_set_num_tc(hw, new_numtc);
6983 i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6984 I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6985 I40E_DCB_DEFAULT_MAX_EXPONENT,
6987 i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
6988 i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
6990 i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
6991 new_cfg->etscfg.prioritytable);
6992 i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
6994 /* Configure Rx Packet Buffers in HW */
6995 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6996 mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
6997 mfs_tc[i] += I40E_PACKET_HDR_PAD;
7000 i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7001 false, new_cfg->pfc.pfcenable,
7003 i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7005 /* Update the local Rx Packet buffer config */
7006 pf->pb_cfg = pb_cfg;
7008 /* Inform the FW about changes to DCB configuration */
7009 ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7011 dev_info(&pf->pdev->dev,
7012 "DCB Updated failed, err %s aq_err %s\n",
7013 i40e_stat_str(&pf->hw, ret),
7014 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7018 /* Update the port DCBx configuration */
7019 *old_cfg = *new_cfg;
7021 /* Changes in configuration update VEB/VSI */
7022 i40e_dcb_reconfigure(pf);
7024 /* Re-start the VSIs if disabled */
7025 if (need_reconfig) {
7026 ret = i40e_resume_port_tx(pf);
7028 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7029 /* In case of error no point in resuming VSIs */
7033 /* Wait for the PF's queues to be disabled */
7034 ret = i40e_pf_wait_queues_disabled(pf);
7036 /* Schedule PF reset to recover */
7037 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7038 i40e_service_event_schedule(pf);
7041 i40e_pf_unquiesce_all_vsi(pf);
7042 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7043 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7045 /* registers are set, lets apply */
7046 if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7047 ret = i40e_hw_set_dcb_config(pf, new_cfg);
7055 * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7056 * @pf: PF being queried
7058 * Set default DCB configuration in case DCB is to be done in SW.
7060 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7062 struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7063 struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7064 struct i40e_hw *hw = &pf->hw;
7067 if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7068 /* Update the local cached instance with TC0 ETS */
7069 memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7070 pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7071 pf->tmp_cfg.etscfg.maxtcs = 0;
7072 pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7073 pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7074 pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7075 pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7076 /* FW needs one App to configure HW */
7077 pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7078 pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7079 pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7080 pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7082 return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7085 memset(&ets_data, 0, sizeof(ets_data));
7086 ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7087 ets_data.tc_strict_priority_flags = 0; /* ETS */
7088 ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7090 /* Enable ETS on the Physical port */
7091 err = i40e_aq_config_switch_comp_ets
7092 (hw, pf->mac_seid, &ets_data,
7093 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7095 dev_info(&pf->pdev->dev,
7096 "Enable Port ETS failed, err %s aq_err %s\n",
7097 i40e_stat_str(&pf->hw, err),
7098 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7103 /* Update the local cached instance with TC0 ETS */
7104 dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7105 dcb_cfg->etscfg.cbs = 0;
7106 dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7107 dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7114 * i40e_init_pf_dcb - Initialize DCB configuration
7115 * @pf: PF being configured
7117 * Query the current DCB configuration and cache it
7118 * in the hardware structure
7120 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7122 struct i40e_hw *hw = &pf->hw;
7125 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7126 * Also do not enable DCBx if FW LLDP agent is disabled
7128 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7129 dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7130 err = I40E_NOT_SUPPORTED;
7133 if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7134 dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7135 err = i40e_dcb_sw_default_config(pf);
7137 dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7140 dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7141 pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7142 DCB_CAP_DCBX_VER_IEEE;
7143 /* at init capable but disabled */
7144 pf->flags |= I40E_FLAG_DCB_CAPABLE;
7145 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7148 err = i40e_init_dcb(hw, true);
7150 /* Device/Function is not DCBX capable */
7151 if ((!hw->func_caps.dcb) ||
7152 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7153 dev_info(&pf->pdev->dev,
7154 "DCBX offload is not supported or is disabled for this PF.\n");
7156 /* When status is not DISABLED then DCBX in FW */
7157 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7158 DCB_CAP_DCBX_VER_IEEE;
7160 pf->flags |= I40E_FLAG_DCB_CAPABLE;
7161 /* Enable DCB tagging only when more than one TC
7162 * or explicitly disable if only one TC
7164 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7165 pf->flags |= I40E_FLAG_DCB_ENABLED;
7167 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7168 dev_dbg(&pf->pdev->dev,
7169 "DCBX offload is supported for this PF.\n");
7171 } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7172 dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7173 pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7175 dev_info(&pf->pdev->dev,
7176 "Query for DCB configuration failed, err %s aq_err %s\n",
7177 i40e_stat_str(&pf->hw, err),
7178 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7184 #endif /* CONFIG_I40E_DCB */
7187 * i40e_print_link_message - print link up or down
7188 * @vsi: the VSI for which link needs a message
7189 * @isup: true of link is up, false otherwise
7191 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7193 enum i40e_aq_link_speed new_speed;
7194 struct i40e_pf *pf = vsi->back;
7195 char *speed = "Unknown";
7196 char *fc = "Unknown";
7202 new_speed = pf->hw.phy.link_info.link_speed;
7204 new_speed = I40E_LINK_SPEED_UNKNOWN;
7206 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7208 vsi->current_isup = isup;
7209 vsi->current_speed = new_speed;
7211 netdev_info(vsi->netdev, "NIC Link is Down\n");
7215 /* Warn user if link speed on NPAR enabled partition is not at
7218 if (pf->hw.func_caps.npar_enable &&
7219 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7220 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7221 netdev_warn(vsi->netdev,
7222 "The partition detected link speed that is less than 10Gbps\n");
7224 switch (pf->hw.phy.link_info.link_speed) {
7225 case I40E_LINK_SPEED_40GB:
7228 case I40E_LINK_SPEED_20GB:
7231 case I40E_LINK_SPEED_25GB:
7234 case I40E_LINK_SPEED_10GB:
7237 case I40E_LINK_SPEED_5GB:
7240 case I40E_LINK_SPEED_2_5GB:
7243 case I40E_LINK_SPEED_1GB:
7246 case I40E_LINK_SPEED_100MB:
7253 switch (pf->hw.fc.current_mode) {
7257 case I40E_FC_TX_PAUSE:
7260 case I40E_FC_RX_PAUSE:
7268 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7273 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7276 if (pf->hw.phy.link_info.fec_info &
7277 I40E_AQ_CONFIG_FEC_KR_ENA)
7278 fec = "CL74 FC-FEC/BASE-R";
7279 else if (pf->hw.phy.link_info.fec_info &
7280 I40E_AQ_CONFIG_FEC_RS_ENA)
7281 fec = "CL108 RS-FEC";
7283 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
7284 * both RS and FC are requested
7286 if (vsi->back->hw.phy.link_info.req_fec_info &
7287 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7288 if (vsi->back->hw.phy.link_info.req_fec_info &
7289 I40E_AQ_REQUEST_FEC_RS)
7290 req_fec = "CL108 RS-FEC";
7292 req_fec = "CL74 FC-FEC/BASE-R";
7294 netdev_info(vsi->netdev,
7295 "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7296 speed, req_fec, fec, an, fc);
7297 } else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7302 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7305 if (pf->hw.phy.link_info.fec_info &
7306 I40E_AQ_CONFIG_FEC_KR_ENA)
7307 fec = "CL74 FC-FEC/BASE-R";
7309 if (pf->hw.phy.link_info.req_fec_info &
7310 I40E_AQ_REQUEST_FEC_KR)
7311 req_fec = "CL74 FC-FEC/BASE-R";
7313 netdev_info(vsi->netdev,
7314 "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7315 speed, req_fec, fec, an, fc);
7317 netdev_info(vsi->netdev,
7318 "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7325 * i40e_up_complete - Finish the last steps of bringing up a connection
7326 * @vsi: the VSI being configured
7328 static int i40e_up_complete(struct i40e_vsi *vsi)
7330 struct i40e_pf *pf = vsi->back;
7333 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7334 i40e_vsi_configure_msix(vsi);
7336 i40e_configure_msi_and_legacy(vsi);
7339 err = i40e_vsi_start_rings(vsi);
7343 clear_bit(__I40E_VSI_DOWN, vsi->state);
7344 i40e_napi_enable_all(vsi);
7345 i40e_vsi_enable_irq(vsi);
7347 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7349 i40e_print_link_message(vsi, true);
7350 netif_tx_start_all_queues(vsi->netdev);
7351 netif_carrier_on(vsi->netdev);
7354 /* replay FDIR SB filters */
7355 if (vsi->type == I40E_VSI_FDIR) {
7356 /* reset fd counters */
7359 i40e_fdir_filter_restore(vsi);
7362 /* On the next run of the service_task, notify any clients of the new
7365 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7366 i40e_service_event_schedule(pf);
7372 * i40e_vsi_reinit_locked - Reset the VSI
7373 * @vsi: the VSI being configured
7375 * Rebuild the ring structs after some configuration
7376 * has changed, e.g. MTU size.
7378 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7380 struct i40e_pf *pf = vsi->back;
7382 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7383 usleep_range(1000, 2000);
7387 clear_bit(__I40E_CONFIG_BUSY, pf->state);
7391 * i40e_force_link_state - Force the link status
7392 * @pf: board private structure
7393 * @is_up: whether the link state should be forced up or down
7395 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7397 struct i40e_aq_get_phy_abilities_resp abilities;
7398 struct i40e_aq_set_phy_config config = {0};
7399 bool non_zero_phy_type = is_up;
7400 struct i40e_hw *hw = &pf->hw;
7405 /* Card might've been put in an unstable state by other drivers
7406 * and applications, which causes incorrect speed values being
7407 * set on startup. In order to clear speed registers, we call
7408 * get_phy_capabilities twice, once to get initial state of
7409 * available speeds, and once to get current PHY config.
7411 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7414 dev_err(&pf->pdev->dev,
7415 "failed to get phy cap., ret = %s last_status = %s\n",
7416 i40e_stat_str(hw, err),
7417 i40e_aq_str(hw, hw->aq.asq_last_status));
7420 speed = abilities.link_speed;
7422 /* Get the current phy config */
7423 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7426 dev_err(&pf->pdev->dev,
7427 "failed to get phy cap., ret = %s last_status = %s\n",
7428 i40e_stat_str(hw, err),
7429 i40e_aq_str(hw, hw->aq.asq_last_status));
7433 /* If link needs to go up, but was not forced to go down,
7434 * and its speed values are OK, no need for a flap
7435 * if non_zero_phy_type was set, still need to force up
7437 if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7438 non_zero_phy_type = true;
7439 else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7440 return I40E_SUCCESS;
7442 /* To force link we need to set bits for all supported PHY types,
7443 * but there are now more than 32, so we need to split the bitmap
7444 * across two fields.
7446 mask = I40E_PHY_TYPES_BITMASK;
7448 non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7449 config.phy_type_ext =
7450 non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7451 /* Copy the old settings, except of phy_type */
7452 config.abilities = abilities.abilities;
7453 if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7455 config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7457 config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7459 if (abilities.link_speed != 0)
7460 config.link_speed = abilities.link_speed;
7462 config.link_speed = speed;
7463 config.eee_capability = abilities.eee_capability;
7464 config.eeer = abilities.eeer_val;
7465 config.low_power_ctrl = abilities.d3_lpan;
7466 config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7467 I40E_AQ_PHY_FEC_CONFIG_MASK;
7468 err = i40e_aq_set_phy_config(hw, &config, NULL);
7471 dev_err(&pf->pdev->dev,
7472 "set phy config ret = %s last_status = %s\n",
7473 i40e_stat_str(&pf->hw, err),
7474 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7478 /* Update the link info */
7479 err = i40e_update_link_info(hw);
7481 /* Wait a little bit (on 40G cards it sometimes takes a really
7482 * long time for link to come back from the atomic reset)
7486 i40e_update_link_info(hw);
7489 i40e_aq_set_link_restart_an(hw, is_up, NULL);
7491 return I40E_SUCCESS;
7495 * i40e_up - Bring the connection back up after being down
7496 * @vsi: the VSI being configured
7498 int i40e_up(struct i40e_vsi *vsi)
7502 if (vsi->type == I40E_VSI_MAIN &&
7503 (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7504 vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7505 i40e_force_link_state(vsi->back, true);
7507 err = i40e_vsi_configure(vsi);
7509 err = i40e_up_complete(vsi);
7515 * i40e_down - Shutdown the connection processing
7516 * @vsi: the VSI being stopped
7518 void i40e_down(struct i40e_vsi *vsi)
7522 /* It is assumed that the caller of this function
7523 * sets the vsi->state __I40E_VSI_DOWN bit.
7526 netif_carrier_off(vsi->netdev);
7527 netif_tx_disable(vsi->netdev);
7529 i40e_vsi_disable_irq(vsi);
7530 i40e_vsi_stop_rings(vsi);
7531 if (vsi->type == I40E_VSI_MAIN &&
7532 (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7533 vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7534 i40e_force_link_state(vsi->back, false);
7535 i40e_napi_disable_all(vsi);
7537 for (i = 0; i < vsi->num_queue_pairs; i++) {
7538 i40e_clean_tx_ring(vsi->tx_rings[i]);
7539 if (i40e_enabled_xdp_vsi(vsi)) {
7540 /* Make sure that in-progress ndo_xdp_xmit and
7541 * ndo_xsk_wakeup calls are completed.
7544 i40e_clean_tx_ring(vsi->xdp_rings[i]);
7546 i40e_clean_rx_ring(vsi->rx_rings[i]);
7552 * i40e_validate_mqprio_qopt- validate queue mapping info
7553 * @vsi: the VSI being configured
7554 * @mqprio_qopt: queue parametrs
7556 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7557 struct tc_mqprio_qopt_offload *mqprio_qopt)
7559 u64 sum_max_rate = 0;
7563 if (mqprio_qopt->qopt.offset[0] != 0 ||
7564 mqprio_qopt->qopt.num_tc < 1 ||
7565 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7567 for (i = 0; ; i++) {
7568 if (!mqprio_qopt->qopt.count[i])
7570 if (mqprio_qopt->min_rate[i]) {
7571 dev_err(&vsi->back->pdev->dev,
7572 "Invalid min tx rate (greater than 0) specified\n");
7575 max_rate = mqprio_qopt->max_rate[i];
7576 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7577 sum_max_rate += max_rate;
7579 if (i >= mqprio_qopt->qopt.num_tc - 1)
7581 if (mqprio_qopt->qopt.offset[i + 1] !=
7582 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7585 if (vsi->num_queue_pairs <
7586 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7587 dev_err(&vsi->back->pdev->dev,
7588 "Failed to create traffic channel, insufficient number of queues.\n");
7591 if (sum_max_rate > i40e_get_link_speed(vsi)) {
7592 dev_err(&vsi->back->pdev->dev,
7593 "Invalid max tx rate specified\n");
7600 * i40e_vsi_set_default_tc_config - set default values for tc configuration
7601 * @vsi: the VSI being configured
7603 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7608 /* Only TC0 is enabled */
7609 vsi->tc_config.numtc = 1;
7610 vsi->tc_config.enabled_tc = 1;
7611 qcount = min_t(int, vsi->alloc_queue_pairs,
7612 i40e_pf_get_max_q_per_tc(vsi->back));
7613 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7614 /* For the TC that is not enabled set the offset to default
7615 * queue and allocate one queue for the given TC.
7617 vsi->tc_config.tc_info[i].qoffset = 0;
7619 vsi->tc_config.tc_info[i].qcount = qcount;
7621 vsi->tc_config.tc_info[i].qcount = 1;
7622 vsi->tc_config.tc_info[i].netdev_tc = 0;
7627 * i40e_del_macvlan_filter
7628 * @hw: pointer to the HW structure
7629 * @seid: seid of the channel VSI
7630 * @macaddr: the mac address to apply as a filter
7631 * @aq_err: store the admin Q error
7633 * This function deletes a mac filter on the channel VSI which serves as the
7634 * macvlan. Returns 0 on success.
7636 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7637 const u8 *macaddr, int *aq_err)
7639 struct i40e_aqc_remove_macvlan_element_data element;
7642 memset(&element, 0, sizeof(element));
7643 ether_addr_copy(element.mac_addr, macaddr);
7644 element.vlan_tag = 0;
7645 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7646 status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7647 *aq_err = hw->aq.asq_last_status;
7653 * i40e_add_macvlan_filter
7654 * @hw: pointer to the HW structure
7655 * @seid: seid of the channel VSI
7656 * @macaddr: the mac address to apply as a filter
7657 * @aq_err: store the admin Q error
7659 * This function adds a mac filter on the channel VSI which serves as the
7660 * macvlan. Returns 0 on success.
7662 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7663 const u8 *macaddr, int *aq_err)
7665 struct i40e_aqc_add_macvlan_element_data element;
7669 ether_addr_copy(element.mac_addr, macaddr);
7670 element.vlan_tag = 0;
7671 element.queue_number = 0;
7672 element.match_method = I40E_AQC_MM_ERR_NO_RES;
7673 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7674 element.flags = cpu_to_le16(cmd_flags);
7675 status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7676 *aq_err = hw->aq.asq_last_status;
7682 * i40e_reset_ch_rings - Reset the queue contexts in a channel
7683 * @vsi: the VSI we want to access
7684 * @ch: the channel we want to access
7686 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7688 struct i40e_ring *tx_ring, *rx_ring;
7692 for (i = 0; i < ch->num_queue_pairs; i++) {
7693 pf_q = ch->base_queue + i;
7694 tx_ring = vsi->tx_rings[pf_q];
7696 rx_ring = vsi->rx_rings[pf_q];
7702 * i40e_free_macvlan_channels
7703 * @vsi: the VSI we want to access
7705 * This function frees the Qs of the channel VSI from
7706 * the stack and also deletes the channel VSIs which
7707 * serve as macvlans.
7709 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7711 struct i40e_channel *ch, *ch_tmp;
7714 if (list_empty(&vsi->macvlan_list))
7717 list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7718 struct i40e_vsi *parent_vsi;
7720 if (i40e_is_channel_macvlan(ch)) {
7721 i40e_reset_ch_rings(vsi, ch);
7722 clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7723 netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7724 netdev_set_sb_channel(ch->fwd->netdev, 0);
7729 list_del(&ch->list);
7730 parent_vsi = ch->parent_vsi;
7731 if (!parent_vsi || !ch->initialized) {
7736 /* remove the VSI */
7737 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7740 dev_err(&vsi->back->pdev->dev,
7741 "unable to remove channel (%d) for parent VSI(%d)\n",
7742 ch->seid, parent_vsi->seid);
7745 vsi->macvlan_cnt = 0;
7749 * i40e_fwd_ring_up - bring the macvlan device up
7750 * @vsi: the VSI we want to access
7751 * @vdev: macvlan netdevice
7752 * @fwd: the private fwd structure
7754 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7755 struct i40e_fwd_adapter *fwd)
7757 struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7758 int ret = 0, num_tc = 1, i, aq_err;
7759 struct i40e_pf *pf = vsi->back;
7760 struct i40e_hw *hw = &pf->hw;
7762 /* Go through the list and find an available channel */
7763 list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7764 if (!i40e_is_channel_macvlan(iter)) {
7766 /* record configuration for macvlan interface in vdev */
7767 for (i = 0; i < num_tc; i++)
7768 netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7770 iter->num_queue_pairs,
7772 for (i = 0; i < iter->num_queue_pairs; i++) {
7773 struct i40e_ring *tx_ring, *rx_ring;
7776 pf_q = iter->base_queue + i;
7778 /* Get to TX ring ptr */
7779 tx_ring = vsi->tx_rings[pf_q];
7782 /* Get the RX ring ptr */
7783 rx_ring = vsi->rx_rings[pf_q];
7794 /* Guarantee all rings are updated before we update the
7795 * MAC address filter.
7799 /* Add a mac filter */
7800 ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7802 /* if we cannot add the MAC rule then disable the offload */
7803 macvlan_release_l2fw_offload(vdev);
7804 for (i = 0; i < ch->num_queue_pairs; i++) {
7805 struct i40e_ring *rx_ring;
7808 pf_q = ch->base_queue + i;
7809 rx_ring = vsi->rx_rings[pf_q];
7810 rx_ring->netdev = NULL;
7812 dev_info(&pf->pdev->dev,
7813 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7814 i40e_stat_str(hw, ret),
7815 i40e_aq_str(hw, aq_err));
7816 netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7823 * i40e_setup_macvlans - create the channels which will be macvlans
7824 * @vsi: the VSI we want to access
7825 * @macvlan_cnt: no. of macvlans to be setup
7826 * @qcnt: no. of Qs per macvlan
7827 * @vdev: macvlan netdevice
7829 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7830 struct net_device *vdev)
7832 struct i40e_pf *pf = vsi->back;
7833 struct i40e_hw *hw = &pf->hw;
7834 struct i40e_vsi_context ctxt;
7835 u16 sections, qmap, num_qps;
7836 struct i40e_channel *ch;
7837 int i, pow, ret = 0;
7840 if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7843 num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7845 /* find the next higher power-of-2 of num queue pairs */
7846 pow = fls(roundup_pow_of_two(num_qps) - 1);
7848 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7849 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7851 /* Setup context bits for the main VSI */
7852 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7853 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7854 memset(&ctxt, 0, sizeof(ctxt));
7855 ctxt.seid = vsi->seid;
7856 ctxt.pf_num = vsi->back->hw.pf_id;
7858 ctxt.uplink_seid = vsi->uplink_seid;
7859 ctxt.info = vsi->info;
7860 ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7861 ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7862 ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7863 ctxt.info.valid_sections |= cpu_to_le16(sections);
7865 /* Reconfigure RSS for main VSI with new max queue count */
7866 vsi->rss_size = max_t(u16, num_qps, qcnt);
7867 ret = i40e_vsi_config_rss(vsi);
7869 dev_info(&pf->pdev->dev,
7870 "Failed to reconfig RSS for num_queues (%u)\n",
7874 vsi->reconfig_rss = true;
7875 dev_dbg(&vsi->back->pdev->dev,
7876 "Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7877 vsi->next_base_queue = num_qps;
7878 vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7880 /* Update the VSI after updating the VSI queue-mapping
7883 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7885 dev_info(&pf->pdev->dev,
7886 "Update vsi tc config failed, err %s aq_err %s\n",
7887 i40e_stat_str(hw, ret),
7888 i40e_aq_str(hw, hw->aq.asq_last_status));
7891 /* update the local VSI info with updated queue map */
7892 i40e_vsi_update_queue_map(vsi, &ctxt);
7893 vsi->info.valid_sections = 0;
7895 /* Create channels for macvlans */
7896 INIT_LIST_HEAD(&vsi->macvlan_list);
7897 for (i = 0; i < macvlan_cnt; i++) {
7898 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7903 INIT_LIST_HEAD(&ch->list);
7904 ch->num_queue_pairs = qcnt;
7905 if (!i40e_setup_channel(pf, vsi, ch)) {
7910 ch->parent_vsi = vsi;
7911 vsi->cnt_q_avail -= ch->num_queue_pairs;
7913 list_add_tail(&ch->list, &vsi->macvlan_list);
7919 dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7920 i40e_free_macvlan_channels(vsi);
7926 * i40e_fwd_add - configure macvlans
7927 * @netdev: net device to configure
7928 * @vdev: macvlan netdevice
7930 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7932 struct i40e_netdev_priv *np = netdev_priv(netdev);
7933 u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7934 struct i40e_vsi *vsi = np->vsi;
7935 struct i40e_pf *pf = vsi->back;
7936 struct i40e_fwd_adapter *fwd;
7937 int avail_macvlan, ret;
7939 if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7940 netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7941 return ERR_PTR(-EINVAL);
7943 if (i40e_is_tc_mqprio_enabled(pf)) {
7944 netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7945 return ERR_PTR(-EINVAL);
7947 if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7948 netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7949 return ERR_PTR(-EINVAL);
7952 /* The macvlan device has to be a single Q device so that the
7953 * tc_to_txq field can be reused to pick the tx queue.
7955 if (netif_is_multiqueue(vdev))
7956 return ERR_PTR(-ERANGE);
7958 if (!vsi->macvlan_cnt) {
7959 /* reserve bit 0 for the pf device */
7960 set_bit(0, vsi->fwd_bitmask);
7962 /* Try to reserve as many queues as possible for macvlans. First
7963 * reserve 3/4th of max vectors, then half, then quarter and
7964 * calculate Qs per macvlan as you go
7966 vectors = pf->num_lan_msix;
7967 if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7968 /* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7970 macvlan_cnt = (vectors - 32) / 4;
7971 } else if (vectors <= 64 && vectors > 32) {
7972 /* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7974 macvlan_cnt = (vectors - 16) / 2;
7975 } else if (vectors <= 32 && vectors > 16) {
7976 /* allocate 1 Q per macvlan and 16 Qs to the PF*/
7978 macvlan_cnt = vectors - 16;
7979 } else if (vectors <= 16 && vectors > 8) {
7980 /* allocate 1 Q per macvlan and 8 Qs to the PF */
7982 macvlan_cnt = vectors - 8;
7984 /* allocate 1 Q per macvlan and 1 Q to the PF */
7986 macvlan_cnt = vectors - 1;
7989 if (macvlan_cnt == 0)
7990 return ERR_PTR(-EBUSY);
7992 /* Quiesce VSI queues */
7993 i40e_quiesce_vsi(vsi);
7995 /* sets up the macvlans but does not "enable" them */
7996 ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7999 return ERR_PTR(ret);
8002 i40e_unquiesce_vsi(vsi);
8004 avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8006 if (avail_macvlan >= I40E_MAX_MACVLANS)
8007 return ERR_PTR(-EBUSY);
8009 /* create the fwd struct */
8010 fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8012 return ERR_PTR(-ENOMEM);
8014 set_bit(avail_macvlan, vsi->fwd_bitmask);
8015 fwd->bit_no = avail_macvlan;
8016 netdev_set_sb_channel(vdev, avail_macvlan);
8019 if (!netif_running(netdev))
8022 /* Set fwd ring up */
8023 ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8025 /* unbind the queues and drop the subordinate channel config */
8026 netdev_unbind_sb_channel(netdev, vdev);
8027 netdev_set_sb_channel(vdev, 0);
8030 return ERR_PTR(-EINVAL);
8037 * i40e_del_all_macvlans - Delete all the mac filters on the channels
8038 * @vsi: the VSI we want to access
8040 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8042 struct i40e_channel *ch, *ch_tmp;
8043 struct i40e_pf *pf = vsi->back;
8044 struct i40e_hw *hw = &pf->hw;
8045 int aq_err, ret = 0;
8047 if (list_empty(&vsi->macvlan_list))
8050 list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8051 if (i40e_is_channel_macvlan(ch)) {
8052 ret = i40e_del_macvlan_filter(hw, ch->seid,
8053 i40e_channel_mac(ch),
8056 /* Reset queue contexts */
8057 i40e_reset_ch_rings(vsi, ch);
8058 clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8059 netdev_unbind_sb_channel(vsi->netdev,
8061 netdev_set_sb_channel(ch->fwd->netdev, 0);
8070 * i40e_fwd_del - delete macvlan interfaces
8071 * @netdev: net device to configure
8072 * @vdev: macvlan netdevice
8074 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8076 struct i40e_netdev_priv *np = netdev_priv(netdev);
8077 struct i40e_fwd_adapter *fwd = vdev;
8078 struct i40e_channel *ch, *ch_tmp;
8079 struct i40e_vsi *vsi = np->vsi;
8080 struct i40e_pf *pf = vsi->back;
8081 struct i40e_hw *hw = &pf->hw;
8082 int aq_err, ret = 0;
8084 /* Find the channel associated with the macvlan and del mac filter */
8085 list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8086 if (i40e_is_channel_macvlan(ch) &&
8087 ether_addr_equal(i40e_channel_mac(ch),
8088 fwd->netdev->dev_addr)) {
8089 ret = i40e_del_macvlan_filter(hw, ch->seid,
8090 i40e_channel_mac(ch),
8093 /* Reset queue contexts */
8094 i40e_reset_ch_rings(vsi, ch);
8095 clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8096 netdev_unbind_sb_channel(netdev, fwd->netdev);
8097 netdev_set_sb_channel(fwd->netdev, 0);
8101 dev_info(&pf->pdev->dev,
8102 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8103 i40e_stat_str(hw, ret),
8104 i40e_aq_str(hw, aq_err));
8112 * i40e_setup_tc - configure multiple traffic classes
8113 * @netdev: net device to configure
8114 * @type_data: tc offload data
8116 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8118 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8119 struct i40e_netdev_priv *np = netdev_priv(netdev);
8120 struct i40e_vsi *vsi = np->vsi;
8121 struct i40e_pf *pf = vsi->back;
8122 u8 enabled_tc = 0, num_tc, hw;
8123 bool need_reset = false;
8124 int old_queue_pairs;
8129 old_queue_pairs = vsi->num_queue_pairs;
8130 num_tc = mqprio_qopt->qopt.num_tc;
8131 hw = mqprio_qopt->qopt.hw;
8132 mode = mqprio_qopt->mode;
8134 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8135 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8139 /* Check if MFP enabled */
8140 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8142 "Configuring TC not supported in MFP mode\n");
8146 case TC_MQPRIO_MODE_DCB:
8147 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8149 /* Check if DCB enabled to continue */
8150 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8152 "DCB is not enabled for adapter\n");
8156 /* Check whether tc count is within enabled limit */
8157 if (num_tc > i40e_pf_get_num_tc(pf)) {
8159 "TC count greater than enabled on link for adapter\n");
8163 case TC_MQPRIO_MODE_CHANNEL:
8164 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8166 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8169 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8171 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8174 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8175 sizeof(*mqprio_qopt));
8176 pf->flags |= I40E_FLAG_TC_MQPRIO;
8177 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8184 /* Generate TC map for number of tc requested */
8185 for (i = 0; i < num_tc; i++)
8186 enabled_tc |= BIT(i);
8188 /* Requesting same TC configuration as already enabled */
8189 if (enabled_tc == vsi->tc_config.enabled_tc &&
8190 mode != TC_MQPRIO_MODE_CHANNEL)
8193 /* Quiesce VSI queues */
8194 i40e_quiesce_vsi(vsi);
8196 if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8197 i40e_remove_queue_channels(vsi);
8199 /* Configure VSI for enabled TCs */
8200 ret = i40e_vsi_config_tc(vsi, enabled_tc);
8202 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8206 } else if (enabled_tc &&
8207 (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8209 "Failed to create channel. Override queues (%u) not power of 2\n",
8210 vsi->tc_config.tc_info[0].qcount);
8216 dev_info(&vsi->back->pdev->dev,
8217 "Setup channel (id:%u) utilizing num_queues %d\n",
8218 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8220 if (i40e_is_tc_mqprio_enabled(pf)) {
8221 if (vsi->mqprio_qopt.max_rate[0]) {
8222 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8224 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
8225 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8227 u64 credits = max_tx_rate;
8229 do_div(credits, I40E_BW_CREDIT_DIVISOR);
8230 dev_dbg(&vsi->back->pdev->dev,
8231 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8240 ret = i40e_configure_queue_channels(vsi);
8242 vsi->num_queue_pairs = old_queue_pairs;
8244 "Failed configuring queue channels\n");
8251 /* Reset the configuration data to defaults, only TC0 is enabled */
8253 i40e_vsi_set_default_tc_config(vsi);
8258 i40e_unquiesce_vsi(vsi);
8263 * i40e_set_cld_element - sets cloud filter element data
8264 * @filter: cloud filter rule
8265 * @cld: ptr to cloud filter element data
8267 * This is helper function to copy data into cloud filter element
8270 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8271 struct i40e_aqc_cloud_filters_element_data *cld)
8276 memset(cld, 0, sizeof(*cld));
8277 ether_addr_copy(cld->outer_mac, filter->dst_mac);
8278 ether_addr_copy(cld->inner_mac, filter->src_mac);
8280 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8283 if (filter->n_proto == ETH_P_IPV6) {
8284 #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
8285 for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8286 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8288 *(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8291 ipa = be32_to_cpu(filter->dst_ipv4);
8293 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8296 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8298 /* tenant_id is not supported by FW now, once the support is enabled
8299 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8301 if (filter->tenant_id)
8306 * i40e_add_del_cloud_filter - Add/del cloud filter
8307 * @vsi: pointer to VSI
8308 * @filter: cloud filter rule
8309 * @add: if true, add, if false, delete
8311 * Add or delete a cloud filter for a specific flow spec.
8312 * Returns 0 if the filter were successfully added.
8314 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8315 struct i40e_cloud_filter *filter, bool add)
8317 struct i40e_aqc_cloud_filters_element_data cld_filter;
8318 struct i40e_pf *pf = vsi->back;
8320 static const u16 flag_table[128] = {
8321 [I40E_CLOUD_FILTER_FLAGS_OMAC] =
8322 I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8323 [I40E_CLOUD_FILTER_FLAGS_IMAC] =
8324 I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8325 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
8326 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8327 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8328 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8329 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8330 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8331 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8332 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8333 [I40E_CLOUD_FILTER_FLAGS_IIP] =
8334 I40E_AQC_ADD_CLOUD_FILTER_IIP,
8337 if (filter->flags >= ARRAY_SIZE(flag_table))
8338 return I40E_ERR_CONFIG;
8340 memset(&cld_filter, 0, sizeof(cld_filter));
8342 /* copy element needed to add cloud filter from filter */
8343 i40e_set_cld_element(filter, &cld_filter);
8345 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8346 cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8347 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8349 if (filter->n_proto == ETH_P_IPV6)
8350 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8351 I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8353 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8354 I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8357 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8360 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8363 dev_dbg(&pf->pdev->dev,
8364 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8365 add ? "add" : "delete", filter->dst_port, ret,
8366 pf->hw.aq.asq_last_status);
8368 dev_info(&pf->pdev->dev,
8369 "%s cloud filter for VSI: %d\n",
8370 add ? "Added" : "Deleted", filter->seid);
8375 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8376 * @vsi: pointer to VSI
8377 * @filter: cloud filter rule
8378 * @add: if true, add, if false, delete
8380 * Add or delete a cloud filter for a specific flow spec using big buffer.
8381 * Returns 0 if the filter were successfully added.
8383 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8384 struct i40e_cloud_filter *filter,
8387 struct i40e_aqc_cloud_filters_element_bb cld_filter;
8388 struct i40e_pf *pf = vsi->back;
8391 /* Both (src/dst) valid mac_addr are not supported */
8392 if ((is_valid_ether_addr(filter->dst_mac) &&
8393 is_valid_ether_addr(filter->src_mac)) ||
8394 (is_multicast_ether_addr(filter->dst_mac) &&
8395 is_multicast_ether_addr(filter->src_mac)))
8398 /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8399 * ports are not supported via big buffer now.
8401 if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8404 /* adding filter using src_port/src_ip is not supported at this stage */
8405 if (filter->src_port ||
8406 (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8407 !ipv6_addr_any(&filter->ip.v6.src_ip6))
8410 memset(&cld_filter, 0, sizeof(cld_filter));
8412 /* copy element needed to add cloud filter from filter */
8413 i40e_set_cld_element(filter, &cld_filter.element);
8415 if (is_valid_ether_addr(filter->dst_mac) ||
8416 is_valid_ether_addr(filter->src_mac) ||
8417 is_multicast_ether_addr(filter->dst_mac) ||
8418 is_multicast_ether_addr(filter->src_mac)) {
8419 /* MAC + IP : unsupported mode */
8420 if (filter->dst_ipv4)
8423 /* since we validated that L4 port must be valid before
8424 * we get here, start with respective "flags" value
8425 * and update if vlan is present or not
8427 cld_filter.element.flags =
8428 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8430 if (filter->vlan_id) {
8431 cld_filter.element.flags =
8432 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8435 } else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8436 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8437 cld_filter.element.flags =
8438 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8439 if (filter->n_proto == ETH_P_IPV6)
8440 cld_filter.element.flags |=
8441 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8443 cld_filter.element.flags |=
8444 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8446 dev_err(&pf->pdev->dev,
8447 "either mac or ip has to be valid for cloud filter\n");
8451 /* Now copy L4 port in Byte 6..7 in general fields */
8452 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8453 be16_to_cpu(filter->dst_port);
8456 /* Validate current device switch mode, change if necessary */
8457 ret = i40e_validate_and_set_switch_mode(vsi);
8459 dev_err(&pf->pdev->dev,
8460 "failed to set switch mode, ret %d\n",
8465 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8468 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8473 dev_dbg(&pf->pdev->dev,
8474 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8475 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8477 dev_info(&pf->pdev->dev,
8478 "%s cloud filter for VSI: %d, L4 port: %d\n",
8479 add ? "add" : "delete", filter->seid,
8480 ntohs(filter->dst_port));
8485 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8486 * @vsi: Pointer to VSI
8487 * @f: Pointer to struct flow_cls_offload
8488 * @filter: Pointer to cloud filter structure
8491 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8492 struct flow_cls_offload *f,
8493 struct i40e_cloud_filter *filter)
8495 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8496 struct flow_dissector *dissector = rule->match.dissector;
8497 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8498 struct i40e_pf *pf = vsi->back;
8501 if (dissector->used_keys &
8502 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8503 BIT(FLOW_DISSECTOR_KEY_BASIC) |
8504 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8505 BIT(FLOW_DISSECTOR_KEY_VLAN) |
8506 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8507 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8508 BIT(FLOW_DISSECTOR_KEY_PORTS) |
8509 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8510 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8511 dissector->used_keys);
8515 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8516 struct flow_match_enc_keyid match;
8518 flow_rule_match_enc_keyid(rule, &match);
8519 if (match.mask->keyid != 0)
8520 field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8522 filter->tenant_id = be32_to_cpu(match.key->keyid);
8525 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8526 struct flow_match_basic match;
8528 flow_rule_match_basic(rule, &match);
8529 n_proto_key = ntohs(match.key->n_proto);
8530 n_proto_mask = ntohs(match.mask->n_proto);
8532 if (n_proto_key == ETH_P_ALL) {
8536 filter->n_proto = n_proto_key & n_proto_mask;
8537 filter->ip_proto = match.key->ip_proto;
8540 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8541 struct flow_match_eth_addrs match;
8543 flow_rule_match_eth_addrs(rule, &match);
8545 /* use is_broadcast and is_zero to check for all 0xf or 0 */
8546 if (!is_zero_ether_addr(match.mask->dst)) {
8547 if (is_broadcast_ether_addr(match.mask->dst)) {
8548 field_flags |= I40E_CLOUD_FIELD_OMAC;
8550 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8552 return I40E_ERR_CONFIG;
8556 if (!is_zero_ether_addr(match.mask->src)) {
8557 if (is_broadcast_ether_addr(match.mask->src)) {
8558 field_flags |= I40E_CLOUD_FIELD_IMAC;
8560 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8562 return I40E_ERR_CONFIG;
8565 ether_addr_copy(filter->dst_mac, match.key->dst);
8566 ether_addr_copy(filter->src_mac, match.key->src);
8569 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8570 struct flow_match_vlan match;
8572 flow_rule_match_vlan(rule, &match);
8573 if (match.mask->vlan_id) {
8574 if (match.mask->vlan_id == VLAN_VID_MASK) {
8575 field_flags |= I40E_CLOUD_FIELD_IVLAN;
8578 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8579 match.mask->vlan_id);
8580 return I40E_ERR_CONFIG;
8584 filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8587 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8588 struct flow_match_control match;
8590 flow_rule_match_control(rule, &match);
8591 addr_type = match.key->addr_type;
8594 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8595 struct flow_match_ipv4_addrs match;
8597 flow_rule_match_ipv4_addrs(rule, &match);
8598 if (match.mask->dst) {
8599 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8600 field_flags |= I40E_CLOUD_FIELD_IIP;
8602 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8604 return I40E_ERR_CONFIG;
8608 if (match.mask->src) {
8609 if (match.mask->src == cpu_to_be32(0xffffffff)) {
8610 field_flags |= I40E_CLOUD_FIELD_IIP;
8612 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8614 return I40E_ERR_CONFIG;
8618 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8619 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8620 return I40E_ERR_CONFIG;
8622 filter->dst_ipv4 = match.key->dst;
8623 filter->src_ipv4 = match.key->src;
8626 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8627 struct flow_match_ipv6_addrs match;
8629 flow_rule_match_ipv6_addrs(rule, &match);
8631 /* src and dest IPV6 address should not be LOOPBACK
8632 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8634 if (ipv6_addr_loopback(&match.key->dst) ||
8635 ipv6_addr_loopback(&match.key->src)) {
8636 dev_err(&pf->pdev->dev,
8637 "Bad ipv6, addr is LOOPBACK\n");
8638 return I40E_ERR_CONFIG;
8640 if (!ipv6_addr_any(&match.mask->dst) ||
8641 !ipv6_addr_any(&match.mask->src))
8642 field_flags |= I40E_CLOUD_FIELD_IIP;
8644 memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8645 sizeof(filter->src_ipv6));
8646 memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8647 sizeof(filter->dst_ipv6));
8650 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8651 struct flow_match_ports match;
8653 flow_rule_match_ports(rule, &match);
8654 if (match.mask->src) {
8655 if (match.mask->src == cpu_to_be16(0xffff)) {
8656 field_flags |= I40E_CLOUD_FIELD_IIP;
8658 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8659 be16_to_cpu(match.mask->src));
8660 return I40E_ERR_CONFIG;
8664 if (match.mask->dst) {
8665 if (match.mask->dst == cpu_to_be16(0xffff)) {
8666 field_flags |= I40E_CLOUD_FIELD_IIP;
8668 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8669 be16_to_cpu(match.mask->dst));
8670 return I40E_ERR_CONFIG;
8674 filter->dst_port = match.key->dst;
8675 filter->src_port = match.key->src;
8677 switch (filter->ip_proto) {
8682 dev_err(&pf->pdev->dev,
8683 "Only UDP and TCP transport are supported\n");
8687 filter->flags = field_flags;
8692 * i40e_handle_tclass: Forward to a traffic class on the device
8693 * @vsi: Pointer to VSI
8694 * @tc: traffic class index on the device
8695 * @filter: Pointer to cloud filter structure
8698 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8699 struct i40e_cloud_filter *filter)
8701 struct i40e_channel *ch, *ch_tmp;
8703 /* direct to a traffic class on the same device */
8705 filter->seid = vsi->seid;
8707 } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8708 if (!filter->dst_port) {
8709 dev_err(&vsi->back->pdev->dev,
8710 "Specify destination port to direct to traffic class that is not default\n");
8713 if (list_empty(&vsi->ch_list))
8715 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8717 if (ch->seid == vsi->tc_seid_map[tc])
8718 filter->seid = ch->seid;
8722 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8727 * i40e_configure_clsflower - Configure tc flower filters
8728 * @vsi: Pointer to VSI
8729 * @cls_flower: Pointer to struct flow_cls_offload
8732 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8733 struct flow_cls_offload *cls_flower)
8735 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8736 struct i40e_cloud_filter *filter = NULL;
8737 struct i40e_pf *pf = vsi->back;
8741 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8746 dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8750 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8751 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8754 if (pf->fdir_pf_active_filters ||
8755 (!hlist_empty(&pf->fdir_filter_list))) {
8756 dev_err(&vsi->back->pdev->dev,
8757 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8761 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8762 dev_err(&vsi->back->pdev->dev,
8763 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8764 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8765 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8768 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8772 filter->cookie = cls_flower->cookie;
8774 err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8778 err = i40e_handle_tclass(vsi, tc, filter);
8782 /* Add cloud filter */
8783 if (filter->dst_port)
8784 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8786 err = i40e_add_del_cloud_filter(vsi, filter, true);
8789 dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8794 /* add filter to the ordered list */
8795 INIT_HLIST_NODE(&filter->cloud_node);
8797 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8799 pf->num_cloud_filters++;
8808 * i40e_find_cloud_filter - Find the could filter in the list
8809 * @vsi: Pointer to VSI
8810 * @cookie: filter specific cookie
8813 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8814 unsigned long *cookie)
8816 struct i40e_cloud_filter *filter = NULL;
8817 struct hlist_node *node2;
8819 hlist_for_each_entry_safe(filter, node2,
8820 &vsi->back->cloud_filter_list, cloud_node)
8821 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8827 * i40e_delete_clsflower - Remove tc flower filters
8828 * @vsi: Pointer to VSI
8829 * @cls_flower: Pointer to struct flow_cls_offload
8832 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8833 struct flow_cls_offload *cls_flower)
8835 struct i40e_cloud_filter *filter = NULL;
8836 struct i40e_pf *pf = vsi->back;
8839 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8844 hash_del(&filter->cloud_node);
8846 if (filter->dst_port)
8847 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8849 err = i40e_add_del_cloud_filter(vsi, filter, false);
8853 dev_err(&pf->pdev->dev,
8854 "Failed to delete cloud filter, err %s\n",
8855 i40e_stat_str(&pf->hw, err));
8856 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8859 pf->num_cloud_filters--;
8860 if (!pf->num_cloud_filters)
8861 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8862 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8863 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8864 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8865 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8871 * i40e_setup_tc_cls_flower - flower classifier offloads
8872 * @np: net device to configure
8873 * @cls_flower: offload data
8875 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8876 struct flow_cls_offload *cls_flower)
8878 struct i40e_vsi *vsi = np->vsi;
8880 switch (cls_flower->command) {
8881 case FLOW_CLS_REPLACE:
8882 return i40e_configure_clsflower(vsi, cls_flower);
8883 case FLOW_CLS_DESTROY:
8884 return i40e_delete_clsflower(vsi, cls_flower);
8885 case FLOW_CLS_STATS:
8892 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8895 struct i40e_netdev_priv *np = cb_priv;
8897 if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8901 case TC_SETUP_CLSFLOWER:
8902 return i40e_setup_tc_cls_flower(np, type_data);
8909 static LIST_HEAD(i40e_block_cb_list);
8911 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8914 struct i40e_netdev_priv *np = netdev_priv(netdev);
8917 case TC_SETUP_QDISC_MQPRIO:
8918 return i40e_setup_tc(netdev, type_data);
8919 case TC_SETUP_BLOCK:
8920 return flow_block_cb_setup_simple(type_data,
8921 &i40e_block_cb_list,
8922 i40e_setup_tc_block_cb,
8930 * i40e_open - Called when a network interface is made active
8931 * @netdev: network interface device structure
8933 * The open entry point is called when a network interface is made
8934 * active by the system (IFF_UP). At this point all resources needed
8935 * for transmit and receive operations are allocated, the interrupt
8936 * handler is registered with the OS, the netdev watchdog subtask is
8937 * enabled, and the stack is notified that the interface is ready.
8939 * Returns 0 on success, negative value on failure
8941 int i40e_open(struct net_device *netdev)
8943 struct i40e_netdev_priv *np = netdev_priv(netdev);
8944 struct i40e_vsi *vsi = np->vsi;
8945 struct i40e_pf *pf = vsi->back;
8948 /* disallow open during test or if eeprom is broken */
8949 if (test_bit(__I40E_TESTING, pf->state) ||
8950 test_bit(__I40E_BAD_EEPROM, pf->state))
8953 netif_carrier_off(netdev);
8955 if (i40e_force_link_state(pf, true))
8958 err = i40e_vsi_open(vsi);
8962 /* configure global TSO hardware offload settings */
8963 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8964 TCP_FLAG_FIN) >> 16);
8965 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8967 TCP_FLAG_CWR) >> 16);
8968 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8969 udp_tunnel_get_rx_info(netdev);
8975 * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8976 * @vsi: vsi structure
8978 * This updates netdev's number of tx/rx queues
8980 * Returns status of setting tx/rx queues
8982 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8986 ret = netif_set_real_num_rx_queues(vsi->netdev,
8987 vsi->num_queue_pairs);
8991 return netif_set_real_num_tx_queues(vsi->netdev,
8992 vsi->num_queue_pairs);
8997 * @vsi: the VSI to open
8999 * Finish initialization of the VSI.
9001 * Returns 0 on success, negative value on failure
9003 * Note: expects to be called while under rtnl_lock()
9005 int i40e_vsi_open(struct i40e_vsi *vsi)
9007 struct i40e_pf *pf = vsi->back;
9008 char int_name[I40E_INT_NAME_STR_LEN];
9011 /* allocate descriptors */
9012 err = i40e_vsi_setup_tx_resources(vsi);
9015 err = i40e_vsi_setup_rx_resources(vsi);
9019 err = i40e_vsi_configure(vsi);
9024 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9025 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9026 err = i40e_vsi_request_irq(vsi, int_name);
9030 /* Notify the stack of the actual queue counts. */
9031 err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9033 goto err_set_queues;
9035 } else if (vsi->type == I40E_VSI_FDIR) {
9036 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9037 dev_driver_string(&pf->pdev->dev),
9038 dev_name(&pf->pdev->dev));
9039 err = i40e_vsi_request_irq(vsi, int_name);
9048 err = i40e_up_complete(vsi);
9050 goto err_up_complete;
9057 i40e_vsi_free_irq(vsi);
9059 i40e_vsi_free_rx_resources(vsi);
9061 i40e_vsi_free_tx_resources(vsi);
9062 if (vsi == pf->vsi[pf->lan_vsi])
9063 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9069 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9070 * @pf: Pointer to PF
9072 * This function destroys the hlist where all the Flow Director
9073 * filters were saved.
9075 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9077 struct i40e_fdir_filter *filter;
9078 struct i40e_flex_pit *pit_entry, *tmp;
9079 struct hlist_node *node2;
9081 hlist_for_each_entry_safe(filter, node2,
9082 &pf->fdir_filter_list, fdir_node) {
9083 hlist_del(&filter->fdir_node);
9087 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9088 list_del(&pit_entry->list);
9091 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9093 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9094 list_del(&pit_entry->list);
9097 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9099 pf->fdir_pf_active_filters = 0;
9100 i40e_reset_fdir_filter_cnt(pf);
9102 /* Reprogram the default input set for TCP/IPv4 */
9103 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9104 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9105 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9107 /* Reprogram the default input set for TCP/IPv6 */
9108 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9109 I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9110 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9112 /* Reprogram the default input set for UDP/IPv4 */
9113 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9114 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9115 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9117 /* Reprogram the default input set for UDP/IPv6 */
9118 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9119 I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9120 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9122 /* Reprogram the default input set for SCTP/IPv4 */
9123 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9124 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9125 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9127 /* Reprogram the default input set for SCTP/IPv6 */
9128 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9129 I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9130 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9132 /* Reprogram the default input set for Other/IPv4 */
9133 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9134 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9136 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9137 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9139 /* Reprogram the default input set for Other/IPv6 */
9140 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9141 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9143 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9144 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9148 * i40e_cloud_filter_exit - Cleans up the cloud filters
9149 * @pf: Pointer to PF
9151 * This function destroys the hlist where all the cloud filters
9154 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9156 struct i40e_cloud_filter *cfilter;
9157 struct hlist_node *node;
9159 hlist_for_each_entry_safe(cfilter, node,
9160 &pf->cloud_filter_list, cloud_node) {
9161 hlist_del(&cfilter->cloud_node);
9164 pf->num_cloud_filters = 0;
9166 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9167 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9168 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9169 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9170 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9175 * i40e_close - Disables a network interface
9176 * @netdev: network interface device structure
9178 * The close entry point is called when an interface is de-activated
9179 * by the OS. The hardware is still under the driver's control, but
9180 * this netdev interface is disabled.
9182 * Returns 0, this is not allowed to fail
9184 int i40e_close(struct net_device *netdev)
9186 struct i40e_netdev_priv *np = netdev_priv(netdev);
9187 struct i40e_vsi *vsi = np->vsi;
9189 i40e_vsi_close(vsi);
9195 * i40e_do_reset - Start a PF or Core Reset sequence
9196 * @pf: board private structure
9197 * @reset_flags: which reset is requested
9198 * @lock_acquired: indicates whether or not the lock has been acquired
9199 * before this function was called.
9201 * The essential difference in resets is that the PF Reset
9202 * doesn't clear the packet buffers, doesn't reset the PE
9203 * firmware, and doesn't bother the other PFs on the chip.
9205 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9209 /* do the biggest reset indicated */
9210 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9212 /* Request a Global Reset
9214 * This will start the chip's countdown to the actual full
9215 * chip reset event, and a warning interrupt to be sent
9216 * to all PFs, including the requestor. Our handler
9217 * for the warning interrupt will deal with the shutdown
9218 * and recovery of the switch setup.
9220 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9221 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9222 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9223 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9225 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9227 /* Request a Core Reset
9229 * Same as Global Reset, except does *not* include the MAC/PHY
9231 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9232 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9233 val |= I40E_GLGEN_RTRIG_CORER_MASK;
9234 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9235 i40e_flush(&pf->hw);
9237 } else if (reset_flags & I40E_PF_RESET_FLAG) {
9239 /* Request a PF Reset
9241 * Resets only the PF-specific registers
9243 * This goes directly to the tear-down and rebuild of
9244 * the switch, since we need to do all the recovery as
9245 * for the Core Reset.
9247 dev_dbg(&pf->pdev->dev, "PFR requested\n");
9248 i40e_handle_reset_warning(pf, lock_acquired);
9250 } else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9251 /* Request a PF Reset
9253 * Resets PF and reinitializes PFs VSI.
9255 i40e_prep_for_reset(pf);
9256 i40e_reset_and_rebuild(pf, true, lock_acquired);
9257 dev_info(&pf->pdev->dev,
9258 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9259 "FW LLDP is disabled\n" :
9260 "FW LLDP is enabled\n");
9262 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9265 /* Find the VSI(s) that requested a re-init */
9266 dev_info(&pf->pdev->dev,
9267 "VSI reinit requested\n");
9268 for (v = 0; v < pf->num_alloc_vsi; v++) {
9269 struct i40e_vsi *vsi = pf->vsi[v];
9272 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9274 i40e_vsi_reinit_locked(pf->vsi[v]);
9276 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9279 /* Find the VSI(s) that needs to be brought down */
9280 dev_info(&pf->pdev->dev, "VSI down requested\n");
9281 for (v = 0; v < pf->num_alloc_vsi; v++) {
9282 struct i40e_vsi *vsi = pf->vsi[v];
9285 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9287 set_bit(__I40E_VSI_DOWN, vsi->state);
9292 dev_info(&pf->pdev->dev,
9293 "bad reset request 0x%08x\n", reset_flags);
9297 #ifdef CONFIG_I40E_DCB
9299 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9300 * @pf: board private structure
9301 * @old_cfg: current DCB config
9302 * @new_cfg: new DCB config
9304 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9305 struct i40e_dcbx_config *old_cfg,
9306 struct i40e_dcbx_config *new_cfg)
9308 bool need_reconfig = false;
9310 /* Check if ETS configuration has changed */
9311 if (memcmp(&new_cfg->etscfg,
9313 sizeof(new_cfg->etscfg))) {
9314 /* If Priority Table has changed reconfig is needed */
9315 if (memcmp(&new_cfg->etscfg.prioritytable,
9316 &old_cfg->etscfg.prioritytable,
9317 sizeof(new_cfg->etscfg.prioritytable))) {
9318 need_reconfig = true;
9319 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9322 if (memcmp(&new_cfg->etscfg.tcbwtable,
9323 &old_cfg->etscfg.tcbwtable,
9324 sizeof(new_cfg->etscfg.tcbwtable)))
9325 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9327 if (memcmp(&new_cfg->etscfg.tsatable,
9328 &old_cfg->etscfg.tsatable,
9329 sizeof(new_cfg->etscfg.tsatable)))
9330 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9333 /* Check if PFC configuration has changed */
9334 if (memcmp(&new_cfg->pfc,
9336 sizeof(new_cfg->pfc))) {
9337 need_reconfig = true;
9338 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9341 /* Check if APP Table has changed */
9342 if (memcmp(&new_cfg->app,
9344 sizeof(new_cfg->app))) {
9345 need_reconfig = true;
9346 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9349 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9350 return need_reconfig;
9354 * i40e_handle_lldp_event - Handle LLDP Change MIB event
9355 * @pf: board private structure
9356 * @e: event info posted on ARQ
9358 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9359 struct i40e_arq_event_info *e)
9361 struct i40e_aqc_lldp_get_mib *mib =
9362 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9363 struct i40e_hw *hw = &pf->hw;
9364 struct i40e_dcbx_config tmp_dcbx_cfg;
9365 bool need_reconfig = false;
9369 /* X710-T*L 2.5G and 5G speeds don't support DCB */
9370 if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9371 (hw->phy.link_info.link_speed &
9372 ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9373 !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9374 /* let firmware decide if the DCB should be disabled */
9375 pf->flags |= I40E_FLAG_DCB_CAPABLE;
9377 /* Not DCB capable or capability disabled */
9378 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9381 /* Ignore if event is not for Nearest Bridge */
9382 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9383 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9384 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9385 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9388 /* Check MIB Type and return if event for Remote MIB update */
9389 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9390 dev_dbg(&pf->pdev->dev,
9391 "LLDP event mib type %s\n", type ? "remote" : "local");
9392 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9393 /* Update the remote cached instance and return */
9394 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9395 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9396 &hw->remote_dcbx_config);
9400 /* Store the old configuration */
9401 tmp_dcbx_cfg = hw->local_dcbx_config;
9403 /* Reset the old DCBx configuration data */
9404 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9405 /* Get updated DCBX data from firmware */
9406 ret = i40e_get_dcb_config(&pf->hw);
9408 /* X710-T*L 2.5G and 5G speeds don't support DCB */
9409 if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9410 (hw->phy.link_info.link_speed &
9411 (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9412 dev_warn(&pf->pdev->dev,
9413 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9414 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9416 dev_info(&pf->pdev->dev,
9417 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9418 i40e_stat_str(&pf->hw, ret),
9419 i40e_aq_str(&pf->hw,
9420 pf->hw.aq.asq_last_status));
9425 /* No change detected in DCBX configs */
9426 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9427 sizeof(tmp_dcbx_cfg))) {
9428 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9432 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9433 &hw->local_dcbx_config);
9435 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9440 /* Enable DCB tagging only when more than one TC */
9441 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9442 pf->flags |= I40E_FLAG_DCB_ENABLED;
9444 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9446 set_bit(__I40E_PORT_SUSPENDED, pf->state);
9447 /* Reconfiguration needed quiesce all VSIs */
9448 i40e_pf_quiesce_all_vsi(pf);
9450 /* Changes in configuration update VEB/VSI */
9451 i40e_dcb_reconfigure(pf);
9453 ret = i40e_resume_port_tx(pf);
9455 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9456 /* In case of error no point in resuming VSIs */
9460 /* Wait for the PF's queues to be disabled */
9461 ret = i40e_pf_wait_queues_disabled(pf);
9463 /* Schedule PF reset to recover */
9464 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9465 i40e_service_event_schedule(pf);
9467 i40e_pf_unquiesce_all_vsi(pf);
9468 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9469 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9475 #endif /* CONFIG_I40E_DCB */
9478 * i40e_do_reset_safe - Protected reset path for userland calls.
9479 * @pf: board private structure
9480 * @reset_flags: which reset is requested
9483 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9486 i40e_do_reset(pf, reset_flags, true);
9491 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9492 * @pf: board private structure
9493 * @e: event info posted on ARQ
9495 * Handler for LAN Queue Overflow Event generated by the firmware for PF
9498 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9499 struct i40e_arq_event_info *e)
9501 struct i40e_aqc_lan_overflow *data =
9502 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9503 u32 queue = le32_to_cpu(data->prtdcb_rupto);
9504 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9505 struct i40e_hw *hw = &pf->hw;
9509 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9512 /* Queue belongs to VF, find the VF and issue VF reset */
9513 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9514 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9515 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9516 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9517 vf_id -= hw->func_caps.vf_base_id;
9518 vf = &pf->vf[vf_id];
9519 i40e_vc_notify_vf_reset(vf);
9520 /* Allow VF to process pending reset notification */
9522 i40e_reset_vf(vf, false);
9527 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9528 * @pf: board private structure
9530 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9534 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9535 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9540 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9541 * @pf: board private structure
9543 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9547 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9548 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9549 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9550 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9555 * i40e_get_global_fd_count - Get total FD filters programmed on device
9556 * @pf: board private structure
9558 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9562 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9563 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9564 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9565 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9570 * i40e_reenable_fdir_sb - Restore FDir SB capability
9571 * @pf: board private structure
9573 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9575 if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9576 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9577 (I40E_DEBUG_FD & pf->hw.debug_mask))
9578 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9582 * i40e_reenable_fdir_atr - Restore FDir ATR capability
9583 * @pf: board private structure
9585 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9587 if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9588 /* ATR uses the same filtering logic as SB rules. It only
9589 * functions properly if the input set mask is at the default
9590 * settings. It is safe to restore the default input set
9591 * because there are no active TCPv4 filter rules.
9593 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9594 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9595 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9597 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9598 (I40E_DEBUG_FD & pf->hw.debug_mask))
9599 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9604 * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9605 * @pf: board private structure
9606 * @filter: FDir filter to remove
9608 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9609 struct i40e_fdir_filter *filter)
9611 /* Update counters */
9612 pf->fdir_pf_active_filters--;
9615 switch (filter->flow_type) {
9617 pf->fd_tcp4_filter_cnt--;
9620 pf->fd_udp4_filter_cnt--;
9623 pf->fd_sctp4_filter_cnt--;
9626 pf->fd_tcp6_filter_cnt--;
9629 pf->fd_udp6_filter_cnt--;
9632 pf->fd_udp6_filter_cnt--;
9635 switch (filter->ipl4_proto) {
9637 pf->fd_tcp4_filter_cnt--;
9640 pf->fd_udp4_filter_cnt--;
9643 pf->fd_sctp4_filter_cnt--;
9646 pf->fd_ip4_filter_cnt--;
9650 case IPV6_USER_FLOW:
9651 switch (filter->ipl4_proto) {
9653 pf->fd_tcp6_filter_cnt--;
9656 pf->fd_udp6_filter_cnt--;
9659 pf->fd_sctp6_filter_cnt--;
9662 pf->fd_ip6_filter_cnt--;
9668 /* Remove the filter from the list and free memory */
9669 hlist_del(&filter->fdir_node);
9674 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9675 * @pf: board private structure
9677 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9679 struct i40e_fdir_filter *filter;
9680 u32 fcnt_prog, fcnt_avail;
9681 struct hlist_node *node;
9683 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9686 /* Check if we have enough room to re-enable FDir SB capability. */
9687 fcnt_prog = i40e_get_global_fd_count(pf);
9688 fcnt_avail = pf->fdir_pf_filter_count;
9689 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9690 (pf->fd_add_err == 0) ||
9691 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9692 i40e_reenable_fdir_sb(pf);
9694 /* We should wait for even more space before re-enabling ATR.
9695 * Additionally, we cannot enable ATR as long as we still have TCP SB
9698 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9699 pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9700 i40e_reenable_fdir_atr(pf);
9702 /* if hw had a problem adding a filter, delete it */
9703 if (pf->fd_inv > 0) {
9704 hlist_for_each_entry_safe(filter, node,
9705 &pf->fdir_filter_list, fdir_node)
9706 if (filter->fd_id == pf->fd_inv)
9707 i40e_delete_invalid_filter(pf, filter);
9711 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9712 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9714 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9715 * @pf: board private structure
9717 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9719 unsigned long min_flush_time;
9720 int flush_wait_retry = 50;
9721 bool disable_atr = false;
9725 if (!time_after(jiffies, pf->fd_flush_timestamp +
9726 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9729 /* If the flush is happening too quick and we have mostly SB rules we
9730 * should not re-enable ATR for some time.
9732 min_flush_time = pf->fd_flush_timestamp +
9733 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9734 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9736 if (!(time_after(jiffies, min_flush_time)) &&
9737 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9738 if (I40E_DEBUG_FD & pf->hw.debug_mask)
9739 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9743 pf->fd_flush_timestamp = jiffies;
9744 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9745 /* flush all filters */
9746 wr32(&pf->hw, I40E_PFQF_CTL_1,
9747 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9748 i40e_flush(&pf->hw);
9752 /* Check FD flush status every 5-6msec */
9753 usleep_range(5000, 6000);
9754 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9755 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9757 } while (flush_wait_retry--);
9758 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9759 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9761 /* replay sideband filters */
9762 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9763 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9764 clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9765 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9766 if (I40E_DEBUG_FD & pf->hw.debug_mask)
9767 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9772 * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9773 * @pf: board private structure
9775 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9777 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9781 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9782 * @pf: board private structure
9784 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9787 /* if interface is down do nothing */
9788 if (test_bit(__I40E_DOWN, pf->state))
9791 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9792 i40e_fdir_flush_and_replay(pf);
9794 i40e_fdir_check_and_reenable(pf);
9799 * i40e_vsi_link_event - notify VSI of a link event
9800 * @vsi: vsi to be notified
9801 * @link_up: link up or down
9803 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9805 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9808 switch (vsi->type) {
9810 if (!vsi->netdev || !vsi->netdev_registered)
9814 netif_carrier_on(vsi->netdev);
9815 netif_tx_wake_all_queues(vsi->netdev);
9817 netif_carrier_off(vsi->netdev);
9818 netif_tx_stop_all_queues(vsi->netdev);
9822 case I40E_VSI_SRIOV:
9823 case I40E_VSI_VMDQ2:
9825 case I40E_VSI_IWARP:
9826 case I40E_VSI_MIRROR:
9828 /* there is no notification for other VSIs */
9834 * i40e_veb_link_event - notify elements on the veb of a link event
9835 * @veb: veb to be notified
9836 * @link_up: link up or down
9838 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9843 if (!veb || !veb->pf)
9847 /* depth first... */
9848 for (i = 0; i < I40E_MAX_VEB; i++)
9849 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9850 i40e_veb_link_event(pf->veb[i], link_up);
9852 /* ... now the local VSIs */
9853 for (i = 0; i < pf->num_alloc_vsi; i++)
9854 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9855 i40e_vsi_link_event(pf->vsi[i], link_up);
9859 * i40e_link_event - Update netif_carrier status
9860 * @pf: board private structure
9862 static void i40e_link_event(struct i40e_pf *pf)
9864 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9865 u8 new_link_speed, old_link_speed;
9867 bool new_link, old_link;
9868 #ifdef CONFIG_I40E_DCB
9870 #endif /* CONFIG_I40E_DCB */
9872 /* set this to force the get_link_status call to refresh state */
9873 pf->hw.phy.get_link_info = true;
9874 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9875 status = i40e_get_link_status(&pf->hw, &new_link);
9877 /* On success, disable temp link polling */
9878 if (status == I40E_SUCCESS) {
9879 clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9881 /* Enable link polling temporarily until i40e_get_link_status
9882 * returns I40E_SUCCESS
9884 set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9885 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9890 old_link_speed = pf->hw.phy.link_info_old.link_speed;
9891 new_link_speed = pf->hw.phy.link_info.link_speed;
9893 if (new_link == old_link &&
9894 new_link_speed == old_link_speed &&
9895 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9896 new_link == netif_carrier_ok(vsi->netdev)))
9899 i40e_print_link_message(vsi, new_link);
9901 /* Notify the base of the switch tree connected to
9902 * the link. Floating VEBs are not notified.
9904 if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9905 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9907 i40e_vsi_link_event(vsi, new_link);
9910 i40e_vc_notify_link_state(pf);
9912 if (pf->flags & I40E_FLAG_PTP)
9913 i40e_ptp_set_increment(pf);
9914 #ifdef CONFIG_I40E_DCB
9915 if (new_link == old_link)
9917 /* Not SW DCB so firmware will take care of default settings */
9918 if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9921 /* We cover here only link down, as after link up in case of SW DCB
9922 * SW LLDP agent will take care of setting it up
9925 dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9926 memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9927 err = i40e_dcb_sw_default_config(pf);
9929 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9930 I40E_FLAG_DCB_ENABLED);
9932 pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9933 DCB_CAP_DCBX_VER_IEEE;
9934 pf->flags |= I40E_FLAG_DCB_CAPABLE;
9935 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9938 #endif /* CONFIG_I40E_DCB */
9942 * i40e_watchdog_subtask - periodic checks not using event driven response
9943 * @pf: board private structure
9945 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9949 /* if interface is down do nothing */
9950 if (test_bit(__I40E_DOWN, pf->state) ||
9951 test_bit(__I40E_CONFIG_BUSY, pf->state))
9954 /* make sure we don't do these things too often */
9955 if (time_before(jiffies, (pf->service_timer_previous +
9956 pf->service_timer_period)))
9958 pf->service_timer_previous = jiffies;
9960 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9961 test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9962 i40e_link_event(pf);
9964 /* Update the stats for active netdevs so the network stack
9965 * can look at updated numbers whenever it cares to
9967 for (i = 0; i < pf->num_alloc_vsi; i++)
9968 if (pf->vsi[i] && pf->vsi[i]->netdev)
9969 i40e_update_stats(pf->vsi[i]);
9971 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9972 /* Update the stats for the active switching components */
9973 for (i = 0; i < I40E_MAX_VEB; i++)
9975 i40e_update_veb_stats(pf->veb[i]);
9978 i40e_ptp_rx_hang(pf);
9979 i40e_ptp_tx_hang(pf);
9983 * i40e_reset_subtask - Set up for resetting the device and driver
9984 * @pf: board private structure
9986 static void i40e_reset_subtask(struct i40e_pf *pf)
9988 u32 reset_flags = 0;
9990 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9991 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9992 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9994 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9995 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9996 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9998 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9999 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10000 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10002 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10003 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10004 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10006 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10007 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10008 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10011 /* If there's a recovery already waiting, it takes
10012 * precedence before starting a new reset sequence.
10014 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10015 i40e_prep_for_reset(pf);
10017 i40e_rebuild(pf, false, false);
10020 /* If we're already down or resetting, just bail */
10022 !test_bit(__I40E_DOWN, pf->state) &&
10023 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10024 i40e_do_reset(pf, reset_flags, false);
10029 * i40e_handle_link_event - Handle link event
10030 * @pf: board private structure
10031 * @e: event info posted on ARQ
10033 static void i40e_handle_link_event(struct i40e_pf *pf,
10034 struct i40e_arq_event_info *e)
10036 struct i40e_aqc_get_link_status *status =
10037 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10039 /* Do a new status request to re-enable LSE reporting
10040 * and load new status information into the hw struct
10041 * This completely ignores any state information
10042 * in the ARQ event info, instead choosing to always
10043 * issue the AQ update link status command.
10045 i40e_link_event(pf);
10047 /* Check if module meets thermal requirements */
10048 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10049 dev_err(&pf->pdev->dev,
10050 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10051 dev_err(&pf->pdev->dev,
10052 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10054 /* check for unqualified module, if link is down, suppress
10055 * the message if link was forced to be down.
10057 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10058 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10059 (!(status->link_info & I40E_AQ_LINK_UP)) &&
10060 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10061 dev_err(&pf->pdev->dev,
10062 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10063 dev_err(&pf->pdev->dev,
10064 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10070 * i40e_clean_adminq_subtask - Clean the AdminQ rings
10071 * @pf: board private structure
10073 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10075 struct i40e_arq_event_info event;
10076 struct i40e_hw *hw = &pf->hw;
10077 u16 pending, i = 0;
10083 /* Do not run clean AQ when PF reset fails */
10084 if (test_bit(__I40E_RESET_FAILED, pf->state))
10087 /* check for error indications */
10088 val = rd32(&pf->hw, pf->hw.aq.arq.len);
10090 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10091 if (hw->debug_mask & I40E_DEBUG_AQ)
10092 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10093 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10095 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10096 if (hw->debug_mask & I40E_DEBUG_AQ)
10097 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10098 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10099 pf->arq_overflows++;
10101 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10102 if (hw->debug_mask & I40E_DEBUG_AQ)
10103 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10104 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10107 wr32(&pf->hw, pf->hw.aq.arq.len, val);
10109 val = rd32(&pf->hw, pf->hw.aq.asq.len);
10111 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10112 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10113 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10114 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10116 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10117 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10118 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10119 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10121 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10122 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10123 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10124 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10127 wr32(&pf->hw, pf->hw.aq.asq.len, val);
10129 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10130 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10131 if (!event.msg_buf)
10135 ret = i40e_clean_arq_element(hw, &event, &pending);
10136 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10139 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10143 opcode = le16_to_cpu(event.desc.opcode);
10146 case i40e_aqc_opc_get_link_status:
10148 i40e_handle_link_event(pf, &event);
10151 case i40e_aqc_opc_send_msg_to_pf:
10152 ret = i40e_vc_process_vf_msg(pf,
10153 le16_to_cpu(event.desc.retval),
10154 le32_to_cpu(event.desc.cookie_high),
10155 le32_to_cpu(event.desc.cookie_low),
10159 case i40e_aqc_opc_lldp_update_mib:
10160 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10161 #ifdef CONFIG_I40E_DCB
10163 i40e_handle_lldp_event(pf, &event);
10165 #endif /* CONFIG_I40E_DCB */
10167 case i40e_aqc_opc_event_lan_overflow:
10168 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10169 i40e_handle_lan_overflow_event(pf, &event);
10171 case i40e_aqc_opc_send_msg_to_peer:
10172 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10174 case i40e_aqc_opc_nvm_erase:
10175 case i40e_aqc_opc_nvm_update:
10176 case i40e_aqc_opc_oem_post_update:
10177 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10178 "ARQ NVM operation 0x%04x completed\n",
10182 dev_info(&pf->pdev->dev,
10183 "ARQ: Unknown event 0x%04x ignored\n",
10187 } while (i++ < pf->adminq_work_limit);
10189 if (i < pf->adminq_work_limit)
10190 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10192 /* re-enable Admin queue interrupt cause */
10193 val = rd32(hw, I40E_PFINT_ICR0_ENA);
10194 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10195 wr32(hw, I40E_PFINT_ICR0_ENA, val);
10198 kfree(event.msg_buf);
10202 * i40e_verify_eeprom - make sure eeprom is good to use
10203 * @pf: board private structure
10205 static void i40e_verify_eeprom(struct i40e_pf *pf)
10209 err = i40e_diag_eeprom_test(&pf->hw);
10211 /* retry in case of garbage read */
10212 err = i40e_diag_eeprom_test(&pf->hw);
10214 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10216 set_bit(__I40E_BAD_EEPROM, pf->state);
10220 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10221 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10222 clear_bit(__I40E_BAD_EEPROM, pf->state);
10227 * i40e_enable_pf_switch_lb
10228 * @pf: pointer to the PF structure
10230 * enable switch loop back or die - no point in a return value
10232 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10234 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10235 struct i40e_vsi_context ctxt;
10238 ctxt.seid = pf->main_vsi_seid;
10239 ctxt.pf_num = pf->hw.pf_id;
10241 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10243 dev_info(&pf->pdev->dev,
10244 "couldn't get PF vsi config, err %s aq_err %s\n",
10245 i40e_stat_str(&pf->hw, ret),
10246 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10249 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10250 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10251 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10253 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10255 dev_info(&pf->pdev->dev,
10256 "update vsi switch failed, err %s aq_err %s\n",
10257 i40e_stat_str(&pf->hw, ret),
10258 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10263 * i40e_disable_pf_switch_lb
10264 * @pf: pointer to the PF structure
10266 * disable switch loop back or die - no point in a return value
10268 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10270 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10271 struct i40e_vsi_context ctxt;
10274 ctxt.seid = pf->main_vsi_seid;
10275 ctxt.pf_num = pf->hw.pf_id;
10277 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10279 dev_info(&pf->pdev->dev,
10280 "couldn't get PF vsi config, err %s aq_err %s\n",
10281 i40e_stat_str(&pf->hw, ret),
10282 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10285 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10286 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10287 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10289 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10291 dev_info(&pf->pdev->dev,
10292 "update vsi switch failed, err %s aq_err %s\n",
10293 i40e_stat_str(&pf->hw, ret),
10294 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10299 * i40e_config_bridge_mode - Configure the HW bridge mode
10300 * @veb: pointer to the bridge instance
10302 * Configure the loop back mode for the LAN VSI that is downlink to the
10303 * specified HW bridge instance. It is expected this function is called
10304 * when a new HW bridge is instantiated.
10306 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10308 struct i40e_pf *pf = veb->pf;
10310 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10311 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10312 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10313 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10314 i40e_disable_pf_switch_lb(pf);
10316 i40e_enable_pf_switch_lb(pf);
10320 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10321 * @veb: pointer to the VEB instance
10323 * This is a recursive function that first builds the attached VSIs then
10324 * recurses in to build the next layer of VEB. We track the connections
10325 * through our own index numbers because the seid's from the HW could
10326 * change across the reset.
10328 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10330 struct i40e_vsi *ctl_vsi = NULL;
10331 struct i40e_pf *pf = veb->pf;
10335 /* build VSI that owns this VEB, temporarily attached to base VEB */
10336 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10338 pf->vsi[v]->veb_idx == veb->idx &&
10339 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10340 ctl_vsi = pf->vsi[v];
10345 dev_info(&pf->pdev->dev,
10346 "missing owner VSI for veb_idx %d\n", veb->idx);
10348 goto end_reconstitute;
10350 if (ctl_vsi != pf->vsi[pf->lan_vsi])
10351 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10352 ret = i40e_add_vsi(ctl_vsi);
10354 dev_info(&pf->pdev->dev,
10355 "rebuild of veb_idx %d owner VSI failed: %d\n",
10357 goto end_reconstitute;
10359 i40e_vsi_reset_stats(ctl_vsi);
10361 /* create the VEB in the switch and move the VSI onto the VEB */
10362 ret = i40e_add_veb(veb, ctl_vsi);
10364 goto end_reconstitute;
10366 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10367 veb->bridge_mode = BRIDGE_MODE_VEB;
10369 veb->bridge_mode = BRIDGE_MODE_VEPA;
10370 i40e_config_bridge_mode(veb);
10372 /* create the remaining VSIs attached to this VEB */
10373 for (v = 0; v < pf->num_alloc_vsi; v++) {
10374 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10377 if (pf->vsi[v]->veb_idx == veb->idx) {
10378 struct i40e_vsi *vsi = pf->vsi[v];
10380 vsi->uplink_seid = veb->seid;
10381 ret = i40e_add_vsi(vsi);
10383 dev_info(&pf->pdev->dev,
10384 "rebuild of vsi_idx %d failed: %d\n",
10386 goto end_reconstitute;
10388 i40e_vsi_reset_stats(vsi);
10392 /* create any VEBs attached to this VEB - RECURSION */
10393 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10394 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10395 pf->veb[veb_idx]->uplink_seid = veb->seid;
10396 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10407 * i40e_get_capabilities - get info about the HW
10408 * @pf: the PF struct
10409 * @list_type: AQ capability to be queried
10411 static int i40e_get_capabilities(struct i40e_pf *pf,
10412 enum i40e_admin_queue_opc list_type)
10414 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10419 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10421 cap_buf = kzalloc(buf_len, GFP_KERNEL);
10425 /* this loads the data into the hw struct for us */
10426 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10427 &data_size, list_type,
10429 /* data loaded, buffer no longer needed */
10432 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10433 /* retry with a larger buffer */
10434 buf_len = data_size;
10435 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10436 dev_info(&pf->pdev->dev,
10437 "capability discovery failed, err %s aq_err %s\n",
10438 i40e_stat_str(&pf->hw, err),
10439 i40e_aq_str(&pf->hw,
10440 pf->hw.aq.asq_last_status));
10445 if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10446 if (list_type == i40e_aqc_opc_list_func_capabilities) {
10447 dev_info(&pf->pdev->dev,
10448 "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",
10449 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10450 pf->hw.func_caps.num_msix_vectors,
10451 pf->hw.func_caps.num_msix_vectors_vf,
10452 pf->hw.func_caps.fd_filters_guaranteed,
10453 pf->hw.func_caps.fd_filters_best_effort,
10454 pf->hw.func_caps.num_tx_qp,
10455 pf->hw.func_caps.num_vsis);
10456 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10457 dev_info(&pf->pdev->dev,
10458 "switch_mode=0x%04x, function_valid=0x%08x\n",
10459 pf->hw.dev_caps.switch_mode,
10460 pf->hw.dev_caps.valid_functions);
10461 dev_info(&pf->pdev->dev,
10462 "SR-IOV=%d, num_vfs for all function=%u\n",
10463 pf->hw.dev_caps.sr_iov_1_1,
10464 pf->hw.dev_caps.num_vfs);
10465 dev_info(&pf->pdev->dev,
10466 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10467 pf->hw.dev_caps.num_vsis,
10468 pf->hw.dev_caps.num_rx_qp,
10469 pf->hw.dev_caps.num_tx_qp);
10472 if (list_type == i40e_aqc_opc_list_func_capabilities) {
10473 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10474 + pf->hw.func_caps.num_vfs)
10475 if (pf->hw.revision_id == 0 &&
10476 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10477 dev_info(&pf->pdev->dev,
10478 "got num_vsis %d, setting num_vsis to %d\n",
10479 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10480 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10486 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10489 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10490 * @pf: board private structure
10492 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10494 struct i40e_vsi *vsi;
10496 /* quick workaround for an NVM issue that leaves a critical register
10499 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10500 static const u32 hkey[] = {
10501 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10502 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10503 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10507 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10508 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10511 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10514 /* find existing VSI and see if it needs configuring */
10515 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10517 /* create a new VSI if none exists */
10519 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10520 pf->vsi[pf->lan_vsi]->seid, 0);
10522 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10523 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10524 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10529 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10533 * i40e_fdir_teardown - release the Flow Director resources
10534 * @pf: board private structure
10536 static void i40e_fdir_teardown(struct i40e_pf *pf)
10538 struct i40e_vsi *vsi;
10540 i40e_fdir_filter_exit(pf);
10541 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10543 i40e_vsi_release(vsi);
10547 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10548 * @vsi: PF main vsi
10549 * @seid: seid of main or channel VSIs
10551 * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10552 * existed before reset
10554 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10556 struct i40e_cloud_filter *cfilter;
10557 struct i40e_pf *pf = vsi->back;
10558 struct hlist_node *node;
10561 /* Add cloud filters back if they exist */
10562 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10564 if (cfilter->seid != seid)
10567 if (cfilter->dst_port)
10568 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10571 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10574 dev_dbg(&pf->pdev->dev,
10575 "Failed to rebuild cloud filter, err %s aq_err %s\n",
10576 i40e_stat_str(&pf->hw, ret),
10577 i40e_aq_str(&pf->hw,
10578 pf->hw.aq.asq_last_status));
10586 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10587 * @vsi: PF main vsi
10589 * Rebuilds channel VSIs if they existed before reset
10591 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10593 struct i40e_channel *ch, *ch_tmp;
10596 if (list_empty(&vsi->ch_list))
10599 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10600 if (!ch->initialized)
10602 /* Proceed with creation of channel (VMDq2) VSI */
10603 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10605 dev_info(&vsi->back->pdev->dev,
10606 "failed to rebuild channels using uplink_seid %u\n",
10610 /* Reconfigure TX queues using QTX_CTL register */
10611 ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10613 dev_info(&vsi->back->pdev->dev,
10614 "failed to configure TX rings for channel %u\n",
10618 /* update 'next_base_queue' */
10619 vsi->next_base_queue = vsi->next_base_queue +
10620 ch->num_queue_pairs;
10621 if (ch->max_tx_rate) {
10622 u64 credits = ch->max_tx_rate;
10624 if (i40e_set_bw_limit(vsi, ch->seid,
10628 do_div(credits, I40E_BW_CREDIT_DIVISOR);
10629 dev_dbg(&vsi->back->pdev->dev,
10630 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10635 ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10637 dev_dbg(&vsi->back->pdev->dev,
10638 "Failed to rebuild cloud filters for channel VSI %u\n",
10647 * i40e_prep_for_reset - prep for the core to reset
10648 * @pf: board private structure
10650 * Close up the VFs and other things in prep for PF Reset.
10652 static void i40e_prep_for_reset(struct i40e_pf *pf)
10654 struct i40e_hw *hw = &pf->hw;
10655 i40e_status ret = 0;
10658 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10659 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10661 if (i40e_check_asq_alive(&pf->hw))
10662 i40e_vc_notify_reset(pf);
10664 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10666 /* quiesce the VSIs and their queues that are not already DOWN */
10667 i40e_pf_quiesce_all_vsi(pf);
10669 for (v = 0; v < pf->num_alloc_vsi; v++) {
10671 pf->vsi[v]->seid = 0;
10674 i40e_shutdown_adminq(&pf->hw);
10676 /* call shutdown HMC */
10677 if (hw->hmc.hmc_obj) {
10678 ret = i40e_shutdown_lan_hmc(hw);
10680 dev_warn(&pf->pdev->dev,
10681 "shutdown_lan_hmc failed: %d\n", ret);
10684 /* Save the current PTP time so that we can restore the time after the
10687 i40e_ptp_save_hw_time(pf);
10691 * i40e_send_version - update firmware with driver version
10694 static void i40e_send_version(struct i40e_pf *pf)
10696 struct i40e_driver_version dv;
10698 dv.major_version = 0xff;
10699 dv.minor_version = 0xff;
10700 dv.build_version = 0xff;
10701 dv.subbuild_version = 0;
10702 strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10703 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10707 * i40e_get_oem_version - get OEM specific version information
10708 * @hw: pointer to the hardware structure
10710 static void i40e_get_oem_version(struct i40e_hw *hw)
10712 u16 block_offset = 0xffff;
10713 u16 block_length = 0;
10714 u16 capabilities = 0;
10718 #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
10719 #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
10720 #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
10721 #define I40E_NVM_OEM_GEN_OFFSET 0x02
10722 #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
10723 #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
10724 #define I40E_NVM_OEM_LENGTH 3
10726 /* Check if pointer to OEM version block is valid. */
10727 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10728 if (block_offset == 0xffff)
10731 /* Check if OEM version block has correct length. */
10732 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10734 if (block_length < I40E_NVM_OEM_LENGTH)
10737 /* Check if OEM version format is as expected. */
10738 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10740 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10743 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10745 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10747 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10748 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10752 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10753 * @pf: board private structure
10755 static int i40e_reset(struct i40e_pf *pf)
10757 struct i40e_hw *hw = &pf->hw;
10760 ret = i40e_pf_reset(hw);
10762 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10763 set_bit(__I40E_RESET_FAILED, pf->state);
10764 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10772 * i40e_rebuild - rebuild using a saved config
10773 * @pf: board private structure
10774 * @reinit: if the Main VSI needs to re-initialized.
10775 * @lock_acquired: indicates whether or not the lock has been acquired
10776 * before this function was called.
10778 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10780 const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10781 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10782 struct i40e_hw *hw = &pf->hw;
10787 if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10788 is_recovery_mode_reported)
10789 i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10791 if (test_bit(__I40E_DOWN, pf->state) &&
10792 !test_bit(__I40E_RECOVERY_MODE, pf->state))
10793 goto clear_recovery;
10794 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10796 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10797 ret = i40e_init_adminq(&pf->hw);
10799 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10800 i40e_stat_str(&pf->hw, ret),
10801 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10802 goto clear_recovery;
10804 i40e_get_oem_version(&pf->hw);
10806 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10807 /* The following delay is necessary for firmware update. */
10811 /* re-verify the eeprom if we just had an EMP reset */
10812 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10813 i40e_verify_eeprom(pf);
10815 /* if we are going out of or into recovery mode we have to act
10816 * accordingly with regard to resources initialization
10817 * and deinitialization
10819 if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10820 if (i40e_get_capabilities(pf,
10821 i40e_aqc_opc_list_func_capabilities))
10824 if (is_recovery_mode_reported) {
10825 /* we're staying in recovery mode so we'll reinitialize
10828 if (i40e_setup_misc_vector_for_recovery_mode(pf))
10831 if (!lock_acquired)
10833 /* we're going out of recovery mode so we'll free
10834 * the IRQ allocated specifically for recovery mode
10835 * and restore the interrupt scheme
10837 free_irq(pf->pdev->irq, pf);
10838 i40e_clear_interrupt_scheme(pf);
10839 if (i40e_restore_interrupt_scheme(pf))
10843 /* tell the firmware that we're starting */
10844 i40e_send_version(pf);
10846 /* bail out in case recovery mode was detected, as there is
10847 * no need for further configuration.
10852 i40e_clear_pxe_mode(hw);
10853 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10855 goto end_core_reset;
10857 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10858 hw->func_caps.num_rx_qp, 0, 0);
10860 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10861 goto end_core_reset;
10863 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10865 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10866 goto end_core_reset;
10869 #ifdef CONFIG_I40E_DCB
10870 /* Enable FW to write a default DCB config on link-up
10871 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10872 * is not supported with new link speed
10874 if (i40e_is_tc_mqprio_enabled(pf)) {
10875 i40e_aq_set_dcb_parameters(hw, false, NULL);
10877 if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10878 (hw->phy.link_info.link_speed &
10879 (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10880 i40e_aq_set_dcb_parameters(hw, false, NULL);
10881 dev_warn(&pf->pdev->dev,
10882 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10883 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10885 i40e_aq_set_dcb_parameters(hw, true, NULL);
10886 ret = i40e_init_pf_dcb(pf);
10888 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10890 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10891 /* Continue without DCB enabled */
10896 #endif /* CONFIG_I40E_DCB */
10897 if (!lock_acquired)
10899 ret = i40e_setup_pf_switch(pf, reinit, true);
10903 /* The driver only wants link up/down and module qualification
10904 * reports from firmware. Note the negative logic.
10906 ret = i40e_aq_set_phy_int_mask(&pf->hw,
10907 ~(I40E_AQ_EVENT_LINK_UPDOWN |
10908 I40E_AQ_EVENT_MEDIA_NA |
10909 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10911 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10912 i40e_stat_str(&pf->hw, ret),
10913 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10915 /* Rebuild the VSIs and VEBs that existed before reset.
10916 * They are still in our local switch element arrays, so only
10917 * need to rebuild the switch model in the HW.
10919 * If there were VEBs but the reconstitution failed, we'll try
10920 * to recover minimal use by getting the basic PF VSI working.
10922 if (vsi->uplink_seid != pf->mac_seid) {
10923 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10924 /* find the one VEB connected to the MAC, and find orphans */
10925 for (v = 0; v < I40E_MAX_VEB; v++) {
10929 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10930 pf->veb[v]->uplink_seid == 0) {
10931 ret = i40e_reconstitute_veb(pf->veb[v]);
10936 /* If Main VEB failed, we're in deep doodoo,
10937 * so give up rebuilding the switch and set up
10938 * for minimal rebuild of PF VSI.
10939 * If orphan failed, we'll report the error
10940 * but try to keep going.
10942 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10943 dev_info(&pf->pdev->dev,
10944 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10946 vsi->uplink_seid = pf->mac_seid;
10948 } else if (pf->veb[v]->uplink_seid == 0) {
10949 dev_info(&pf->pdev->dev,
10950 "rebuild of orphan VEB failed: %d\n",
10957 if (vsi->uplink_seid == pf->mac_seid) {
10958 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10959 /* no VEB, so rebuild only the Main VSI */
10960 ret = i40e_add_vsi(vsi);
10962 dev_info(&pf->pdev->dev,
10963 "rebuild of Main VSI failed: %d\n", ret);
10968 if (vsi->mqprio_qopt.max_rate[0]) {
10969 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10972 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10973 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10977 credits = max_tx_rate;
10978 do_div(credits, I40E_BW_CREDIT_DIVISOR);
10979 dev_dbg(&vsi->back->pdev->dev,
10980 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10986 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10990 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10991 * for this main VSI if they exist
10993 ret = i40e_rebuild_channels(vsi);
10997 /* Reconfigure hardware for allowing smaller MSS in the case
10998 * of TSO, so that we avoid the MDD being fired and causing
10999 * a reset in the case of small MSS+TSO.
11001 #define I40E_REG_MSS 0x000E64DC
11002 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11003 #define I40E_64BYTE_MSS 0x400000
11004 val = rd32(hw, I40E_REG_MSS);
11005 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11006 val &= ~I40E_REG_MSS_MIN_MASK;
11007 val |= I40E_64BYTE_MSS;
11008 wr32(hw, I40E_REG_MSS, val);
11011 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11013 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11015 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11016 i40e_stat_str(&pf->hw, ret),
11017 i40e_aq_str(&pf->hw,
11018 pf->hw.aq.asq_last_status));
11020 /* reinit the misc interrupt */
11021 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11022 ret = i40e_setup_misc_vector(pf);
11024 /* Add a filter to drop all Flow control frames from any VSI from being
11025 * transmitted. By doing so we stop a malicious VF from sending out
11026 * PAUSE or PFC frames and potentially controlling traffic for other
11028 * The FW can still send Flow control frames if enabled.
11030 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11031 pf->main_vsi_seid);
11033 /* restart the VSIs that were rebuilt and running before the reset */
11034 i40e_pf_unquiesce_all_vsi(pf);
11036 /* Release the RTNL lock before we start resetting VFs */
11037 if (!lock_acquired)
11040 /* Restore promiscuous settings */
11041 ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11043 dev_warn(&pf->pdev->dev,
11044 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11045 pf->cur_promisc ? "on" : "off",
11046 i40e_stat_str(&pf->hw, ret),
11047 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11049 i40e_reset_all_vfs(pf, true);
11051 /* tell the firmware that we're starting */
11052 i40e_send_version(pf);
11054 /* We've already released the lock, so don't do it again */
11055 goto end_core_reset;
11058 if (!lock_acquired)
11061 clear_bit(__I40E_RESET_FAILED, pf->state);
11063 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11064 clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11068 * i40e_reset_and_rebuild - reset and rebuild using a saved config
11069 * @pf: board private structure
11070 * @reinit: if the Main VSI needs to re-initialized.
11071 * @lock_acquired: indicates whether or not the lock has been acquired
11072 * before this function was called.
11074 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11075 bool lock_acquired)
11079 if (test_bit(__I40E_IN_REMOVE, pf->state))
11081 /* Now we wait for GRST to settle out.
11082 * We don't have to delete the VEBs or VSIs from the hw switch
11083 * because the reset will make them disappear.
11085 ret = i40e_reset(pf);
11087 i40e_rebuild(pf, reinit, lock_acquired);
11091 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11092 * @pf: board private structure
11094 * Close up the VFs and other things in prep for a Core Reset,
11095 * then get ready to rebuild the world.
11096 * @lock_acquired: indicates whether or not the lock has been acquired
11097 * before this function was called.
11099 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11101 i40e_prep_for_reset(pf);
11102 i40e_reset_and_rebuild(pf, false, lock_acquired);
11106 * i40e_handle_mdd_event
11107 * @pf: pointer to the PF structure
11109 * Called from the MDD irq handler to identify possibly malicious vfs
11111 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11113 struct i40e_hw *hw = &pf->hw;
11114 bool mdd_detected = false;
11115 struct i40e_vf *vf;
11119 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11122 /* find what triggered the MDD event */
11123 reg = rd32(hw, I40E_GL_MDET_TX);
11124 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11125 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11126 I40E_GL_MDET_TX_PF_NUM_SHIFT;
11127 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11128 I40E_GL_MDET_TX_VF_NUM_SHIFT;
11129 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11130 I40E_GL_MDET_TX_EVENT_SHIFT;
11131 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11132 I40E_GL_MDET_TX_QUEUE_SHIFT) -
11133 pf->hw.func_caps.base_queue;
11134 if (netif_msg_tx_err(pf))
11135 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11136 event, queue, pf_num, vf_num);
11137 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11138 mdd_detected = true;
11140 reg = rd32(hw, I40E_GL_MDET_RX);
11141 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11142 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11143 I40E_GL_MDET_RX_FUNCTION_SHIFT;
11144 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11145 I40E_GL_MDET_RX_EVENT_SHIFT;
11146 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11147 I40E_GL_MDET_RX_QUEUE_SHIFT) -
11148 pf->hw.func_caps.base_queue;
11149 if (netif_msg_rx_err(pf))
11150 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11151 event, queue, func);
11152 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11153 mdd_detected = true;
11156 if (mdd_detected) {
11157 reg = rd32(hw, I40E_PF_MDET_TX);
11158 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11159 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11160 dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11162 reg = rd32(hw, I40E_PF_MDET_RX);
11163 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11164 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11165 dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11169 /* see if one of the VFs needs its hand slapped */
11170 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11172 reg = rd32(hw, I40E_VP_MDET_TX(i));
11173 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11174 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11175 vf->num_mdd_events++;
11176 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11178 dev_info(&pf->pdev->dev,
11179 "Use PF Control I/F to re-enable the VF\n");
11180 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11183 reg = rd32(hw, I40E_VP_MDET_RX(i));
11184 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11185 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11186 vf->num_mdd_events++;
11187 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11189 dev_info(&pf->pdev->dev,
11190 "Use PF Control I/F to re-enable the VF\n");
11191 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11195 /* re-enable mdd interrupt cause */
11196 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11197 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11198 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11199 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11204 * i40e_service_task - Run the driver's async subtasks
11205 * @work: pointer to work_struct containing our data
11207 static void i40e_service_task(struct work_struct *work)
11209 struct i40e_pf *pf = container_of(work,
11212 unsigned long start_time = jiffies;
11214 /* don't bother with service tasks if a reset is in progress */
11215 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11216 test_bit(__I40E_SUSPENDED, pf->state))
11219 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11222 if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11223 i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11224 i40e_sync_filters_subtask(pf);
11225 i40e_reset_subtask(pf);
11226 i40e_handle_mdd_event(pf);
11227 i40e_vc_process_vflr_event(pf);
11228 i40e_watchdog_subtask(pf);
11229 i40e_fdir_reinit_subtask(pf);
11230 if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11231 /* Client subtask will reopen next time through. */
11232 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11235 i40e_client_subtask(pf);
11236 if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11238 i40e_notify_client_of_l2_param_changes(
11239 pf->vsi[pf->lan_vsi]);
11241 i40e_sync_filters_subtask(pf);
11243 i40e_reset_subtask(pf);
11246 i40e_clean_adminq_subtask(pf);
11248 /* flush memory to make sure state is correct before next watchdog */
11249 smp_mb__before_atomic();
11250 clear_bit(__I40E_SERVICE_SCHED, pf->state);
11252 /* If the tasks have taken longer than one timer cycle or there
11253 * is more work to be done, reschedule the service task now
11254 * rather than wait for the timer to tick again.
11256 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11257 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
11258 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
11259 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11260 i40e_service_event_schedule(pf);
11264 * i40e_service_timer - timer callback
11265 * @t: timer list pointer
11267 static void i40e_service_timer(struct timer_list *t)
11269 struct i40e_pf *pf = from_timer(pf, t, service_timer);
11271 mod_timer(&pf->service_timer,
11272 round_jiffies(jiffies + pf->service_timer_period));
11273 i40e_service_event_schedule(pf);
11277 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11278 * @vsi: the VSI being configured
11280 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11282 struct i40e_pf *pf = vsi->back;
11284 switch (vsi->type) {
11285 case I40E_VSI_MAIN:
11286 vsi->alloc_queue_pairs = pf->num_lan_qps;
11287 if (!vsi->num_tx_desc)
11288 vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11289 I40E_REQ_DESCRIPTOR_MULTIPLE);
11290 if (!vsi->num_rx_desc)
11291 vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11292 I40E_REQ_DESCRIPTOR_MULTIPLE);
11293 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11294 vsi->num_q_vectors = pf->num_lan_msix;
11296 vsi->num_q_vectors = 1;
11300 case I40E_VSI_FDIR:
11301 vsi->alloc_queue_pairs = 1;
11302 vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11303 I40E_REQ_DESCRIPTOR_MULTIPLE);
11304 vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11305 I40E_REQ_DESCRIPTOR_MULTIPLE);
11306 vsi->num_q_vectors = pf->num_fdsb_msix;
11309 case I40E_VSI_VMDQ2:
11310 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11311 if (!vsi->num_tx_desc)
11312 vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11313 I40E_REQ_DESCRIPTOR_MULTIPLE);
11314 if (!vsi->num_rx_desc)
11315 vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11316 I40E_REQ_DESCRIPTOR_MULTIPLE);
11317 vsi->num_q_vectors = pf->num_vmdq_msix;
11320 case I40E_VSI_SRIOV:
11321 vsi->alloc_queue_pairs = pf->num_vf_qps;
11322 if (!vsi->num_tx_desc)
11323 vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11324 I40E_REQ_DESCRIPTOR_MULTIPLE);
11325 if (!vsi->num_rx_desc)
11326 vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11327 I40E_REQ_DESCRIPTOR_MULTIPLE);
11335 if (is_kdump_kernel()) {
11336 vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11337 vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11344 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11345 * @vsi: VSI pointer
11346 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11348 * On error: returns error code (negative)
11349 * On success: returns 0
11351 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11353 struct i40e_ring **next_rings;
11357 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11358 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11359 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11360 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11361 if (!vsi->tx_rings)
11363 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11364 if (i40e_enabled_xdp_vsi(vsi)) {
11365 vsi->xdp_rings = next_rings;
11366 next_rings += vsi->alloc_queue_pairs;
11368 vsi->rx_rings = next_rings;
11370 if (alloc_qvectors) {
11371 /* allocate memory for q_vector pointers */
11372 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11373 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11374 if (!vsi->q_vectors) {
11382 kfree(vsi->tx_rings);
11387 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11388 * @pf: board private structure
11389 * @type: type of VSI
11391 * On error: returns error code (negative)
11392 * On success: returns vsi index in PF (positive)
11394 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11397 struct i40e_vsi *vsi;
11401 /* Need to protect the allocation of the VSIs at the PF level */
11402 mutex_lock(&pf->switch_mutex);
11404 /* VSI list may be fragmented if VSI creation/destruction has
11405 * been happening. We can afford to do a quick scan to look
11406 * for any free VSIs in the list.
11408 * find next empty vsi slot, looping back around if necessary
11411 while (i < pf->num_alloc_vsi && pf->vsi[i])
11413 if (i >= pf->num_alloc_vsi) {
11415 while (i < pf->next_vsi && pf->vsi[i])
11419 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11420 vsi_idx = i; /* Found one! */
11423 goto unlock_pf; /* out of VSI slots! */
11425 pf->next_vsi = ++i;
11427 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11434 set_bit(__I40E_VSI_DOWN, vsi->state);
11436 vsi->idx = vsi_idx;
11437 vsi->int_rate_limit = 0;
11438 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11439 pf->rss_table_size : 64;
11440 vsi->netdev_registered = false;
11441 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11442 hash_init(vsi->mac_filter_hash);
11443 vsi->irqs_ready = false;
11445 if (type == I40E_VSI_MAIN) {
11446 vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11447 if (!vsi->af_xdp_zc_qps)
11451 ret = i40e_set_num_rings_in_vsi(vsi);
11455 ret = i40e_vsi_alloc_arrays(vsi, true);
11459 /* Setup default MSIX irq handler for VSI */
11460 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11462 /* Initialize VSI lock */
11463 spin_lock_init(&vsi->mac_filter_hash_lock);
11464 pf->vsi[vsi_idx] = vsi;
11469 bitmap_free(vsi->af_xdp_zc_qps);
11470 pf->next_vsi = i - 1;
11473 mutex_unlock(&pf->switch_mutex);
11478 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11479 * @vsi: VSI pointer
11480 * @free_qvectors: a bool to specify if q_vectors need to be freed.
11482 * On error: returns error code (negative)
11483 * On success: returns 0
11485 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11487 /* free the ring and vector containers */
11488 if (free_qvectors) {
11489 kfree(vsi->q_vectors);
11490 vsi->q_vectors = NULL;
11492 kfree(vsi->tx_rings);
11493 vsi->tx_rings = NULL;
11494 vsi->rx_rings = NULL;
11495 vsi->xdp_rings = NULL;
11499 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11501 * @vsi: Pointer to VSI structure
11503 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11508 kfree(vsi->rss_hkey_user);
11509 vsi->rss_hkey_user = NULL;
11511 kfree(vsi->rss_lut_user);
11512 vsi->rss_lut_user = NULL;
11516 * i40e_vsi_clear - Deallocate the VSI provided
11517 * @vsi: the VSI being un-configured
11519 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11521 struct i40e_pf *pf;
11530 mutex_lock(&pf->switch_mutex);
11531 if (!pf->vsi[vsi->idx]) {
11532 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11533 vsi->idx, vsi->idx, vsi->type);
11537 if (pf->vsi[vsi->idx] != vsi) {
11538 dev_err(&pf->pdev->dev,
11539 "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11540 pf->vsi[vsi->idx]->idx,
11541 pf->vsi[vsi->idx]->type,
11542 vsi->idx, vsi->type);
11546 /* updates the PF for this cleared vsi */
11547 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11548 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11550 bitmap_free(vsi->af_xdp_zc_qps);
11551 i40e_vsi_free_arrays(vsi, true);
11552 i40e_clear_rss_config_user(vsi);
11554 pf->vsi[vsi->idx] = NULL;
11555 if (vsi->idx < pf->next_vsi)
11556 pf->next_vsi = vsi->idx;
11559 mutex_unlock(&pf->switch_mutex);
11567 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11568 * @vsi: the VSI being cleaned
11570 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11574 if (vsi->tx_rings && vsi->tx_rings[0]) {
11575 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11576 kfree_rcu(vsi->tx_rings[i], rcu);
11577 WRITE_ONCE(vsi->tx_rings[i], NULL);
11578 WRITE_ONCE(vsi->rx_rings[i], NULL);
11579 if (vsi->xdp_rings)
11580 WRITE_ONCE(vsi->xdp_rings[i], NULL);
11586 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11587 * @vsi: the VSI being configured
11589 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11591 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11592 struct i40e_pf *pf = vsi->back;
11593 struct i40e_ring *ring;
11595 /* Set basic values in the rings to be used later during open() */
11596 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11597 /* allocate space for both Tx and Rx in one shot */
11598 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11602 ring->queue_index = i;
11603 ring->reg_idx = vsi->base_queue + i;
11604 ring->ring_active = false;
11606 ring->netdev = vsi->netdev;
11607 ring->dev = &pf->pdev->dev;
11608 ring->count = vsi->num_tx_desc;
11611 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11612 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11613 ring->itr_setting = pf->tx_itr_default;
11614 WRITE_ONCE(vsi->tx_rings[i], ring++);
11616 if (!i40e_enabled_xdp_vsi(vsi))
11619 ring->queue_index = vsi->alloc_queue_pairs + i;
11620 ring->reg_idx = vsi->base_queue + ring->queue_index;
11621 ring->ring_active = false;
11623 ring->netdev = NULL;
11624 ring->dev = &pf->pdev->dev;
11625 ring->count = vsi->num_tx_desc;
11628 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11629 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11630 set_ring_xdp(ring);
11631 ring->itr_setting = pf->tx_itr_default;
11632 WRITE_ONCE(vsi->xdp_rings[i], ring++);
11635 ring->queue_index = i;
11636 ring->reg_idx = vsi->base_queue + i;
11637 ring->ring_active = false;
11639 ring->netdev = vsi->netdev;
11640 ring->dev = &pf->pdev->dev;
11641 ring->count = vsi->num_rx_desc;
11644 ring->itr_setting = pf->rx_itr_default;
11645 WRITE_ONCE(vsi->rx_rings[i], ring);
11651 i40e_vsi_clear_rings(vsi);
11656 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11657 * @pf: board private structure
11658 * @vectors: the number of MSI-X vectors to request
11660 * Returns the number of vectors reserved, or error
11662 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11664 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11665 I40E_MIN_MSIX, vectors);
11667 dev_info(&pf->pdev->dev,
11668 "MSI-X vector reservation failed: %d\n", vectors);
11676 * i40e_init_msix - Setup the MSIX capability
11677 * @pf: board private structure
11679 * Work with the OS to set up the MSIX vectors needed.
11681 * Returns the number of vectors reserved or negative on failure
11683 static int i40e_init_msix(struct i40e_pf *pf)
11685 struct i40e_hw *hw = &pf->hw;
11686 int cpus, extra_vectors;
11690 int iwarp_requested = 0;
11692 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11695 /* The number of vectors we'll request will be comprised of:
11696 * - Add 1 for "other" cause for Admin Queue events, etc.
11697 * - The number of LAN queue pairs
11698 * - Queues being used for RSS.
11699 * We don't need as many as max_rss_size vectors.
11700 * use rss_size instead in the calculation since that
11701 * is governed by number of cpus in the system.
11702 * - assumes symmetric Tx/Rx pairing
11703 * - The number of VMDq pairs
11704 * - The CPU count within the NUMA node if iWARP is enabled
11705 * Once we count this up, try the request.
11707 * If we can't get what we want, we'll simplify to nearly nothing
11708 * and try again. If that still fails, we punt.
11710 vectors_left = hw->func_caps.num_msix_vectors;
11713 /* reserve one vector for miscellaneous handler */
11714 if (vectors_left) {
11719 /* reserve some vectors for the main PF traffic queues. Initially we
11720 * only reserve at most 50% of the available vectors, in the case that
11721 * the number of online CPUs is large. This ensures that we can enable
11722 * extra features as well. Once we've enabled the other features, we
11723 * will use any remaining vectors to reach as close as we can to the
11724 * number of online CPUs.
11726 cpus = num_online_cpus();
11727 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11728 vectors_left -= pf->num_lan_msix;
11730 /* reserve one vector for sideband flow director */
11731 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11732 if (vectors_left) {
11733 pf->num_fdsb_msix = 1;
11737 pf->num_fdsb_msix = 0;
11741 /* can we reserve enough for iWARP? */
11742 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11743 iwarp_requested = pf->num_iwarp_msix;
11746 pf->num_iwarp_msix = 0;
11747 else if (vectors_left < pf->num_iwarp_msix)
11748 pf->num_iwarp_msix = 1;
11749 v_budget += pf->num_iwarp_msix;
11750 vectors_left -= pf->num_iwarp_msix;
11753 /* any vectors left over go for VMDq support */
11754 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11755 if (!vectors_left) {
11756 pf->num_vmdq_msix = 0;
11757 pf->num_vmdq_qps = 0;
11759 int vmdq_vecs_wanted =
11760 pf->num_vmdq_vsis * pf->num_vmdq_qps;
11762 min_t(int, vectors_left, vmdq_vecs_wanted);
11764 /* if we're short on vectors for what's desired, we limit
11765 * the queues per vmdq. If this is still more than are
11766 * available, the user will need to change the number of
11767 * queues/vectors used by the PF later with the ethtool
11770 if (vectors_left < vmdq_vecs_wanted) {
11771 pf->num_vmdq_qps = 1;
11772 vmdq_vecs_wanted = pf->num_vmdq_vsis;
11773 vmdq_vecs = min_t(int,
11777 pf->num_vmdq_msix = pf->num_vmdq_qps;
11779 v_budget += vmdq_vecs;
11780 vectors_left -= vmdq_vecs;
11784 /* On systems with a large number of SMP cores, we previously limited
11785 * the number of vectors for num_lan_msix to be at most 50% of the
11786 * available vectors, to allow for other features. Now, we add back
11787 * the remaining vectors. However, we ensure that the total
11788 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11789 * calculate the number of vectors we can add without going over the
11790 * cap of CPUs. For systems with a small number of CPUs this will be
11793 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11794 pf->num_lan_msix += extra_vectors;
11795 vectors_left -= extra_vectors;
11797 WARN(vectors_left < 0,
11798 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11800 v_budget += pf->num_lan_msix;
11801 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11803 if (!pf->msix_entries)
11806 for (i = 0; i < v_budget; i++)
11807 pf->msix_entries[i].entry = i;
11808 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11810 if (v_actual < I40E_MIN_MSIX) {
11811 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11812 kfree(pf->msix_entries);
11813 pf->msix_entries = NULL;
11814 pci_disable_msix(pf->pdev);
11817 } else if (v_actual == I40E_MIN_MSIX) {
11818 /* Adjust for minimal MSIX use */
11819 pf->num_vmdq_vsis = 0;
11820 pf->num_vmdq_qps = 0;
11821 pf->num_lan_qps = 1;
11822 pf->num_lan_msix = 1;
11824 } else if (v_actual != v_budget) {
11825 /* If we have limited resources, we will start with no vectors
11826 * for the special features and then allocate vectors to some
11827 * of these features based on the policy and at the end disable
11828 * the features that did not get any vectors.
11832 dev_info(&pf->pdev->dev,
11833 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11834 v_actual, v_budget);
11835 /* reserve the misc vector */
11836 vec = v_actual - 1;
11838 /* Scale vector usage down */
11839 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
11840 pf->num_vmdq_vsis = 1;
11841 pf->num_vmdq_qps = 1;
11843 /* partition out the remaining vectors */
11846 pf->num_lan_msix = 1;
11849 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11850 pf->num_lan_msix = 1;
11851 pf->num_iwarp_msix = 1;
11853 pf->num_lan_msix = 2;
11857 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11858 pf->num_iwarp_msix = min_t(int, (vec / 3),
11860 pf->num_vmdq_vsis = min_t(int, (vec / 3),
11861 I40E_DEFAULT_NUM_VMDQ_VSI);
11863 pf->num_vmdq_vsis = min_t(int, (vec / 2),
11864 I40E_DEFAULT_NUM_VMDQ_VSI);
11866 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11867 pf->num_fdsb_msix = 1;
11870 pf->num_lan_msix = min_t(int,
11871 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11873 pf->num_lan_qps = pf->num_lan_msix;
11878 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11879 (pf->num_fdsb_msix == 0)) {
11880 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11881 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11882 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11884 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11885 (pf->num_vmdq_msix == 0)) {
11886 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11887 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11890 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11891 (pf->num_iwarp_msix == 0)) {
11892 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11893 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11895 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11896 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11898 pf->num_vmdq_msix * pf->num_vmdq_vsis,
11900 pf->num_iwarp_msix);
11906 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11907 * @vsi: the VSI being configured
11908 * @v_idx: index of the vector in the vsi struct
11910 * We allocate one q_vector. If allocation fails we return -ENOMEM.
11912 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11914 struct i40e_q_vector *q_vector;
11916 /* allocate q_vector */
11917 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11921 q_vector->vsi = vsi;
11922 q_vector->v_idx = v_idx;
11923 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11926 netif_napi_add(vsi->netdev, &q_vector->napi,
11927 i40e_napi_poll, NAPI_POLL_WEIGHT);
11929 /* tie q_vector and vsi together */
11930 vsi->q_vectors[v_idx] = q_vector;
11936 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11937 * @vsi: the VSI being configured
11939 * We allocate one q_vector per queue interrupt. If allocation fails we
11942 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11944 struct i40e_pf *pf = vsi->back;
11945 int err, v_idx, num_q_vectors;
11947 /* if not MSIX, give the one vector only to the LAN VSI */
11948 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11949 num_q_vectors = vsi->num_q_vectors;
11950 else if (vsi == pf->vsi[pf->lan_vsi])
11955 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11956 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11965 i40e_free_q_vector(vsi, v_idx);
11971 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11972 * @pf: board private structure to initialize
11974 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11979 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11980 vectors = i40e_init_msix(pf);
11982 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
11983 I40E_FLAG_IWARP_ENABLED |
11984 I40E_FLAG_RSS_ENABLED |
11985 I40E_FLAG_DCB_CAPABLE |
11986 I40E_FLAG_DCB_ENABLED |
11987 I40E_FLAG_SRIOV_ENABLED |
11988 I40E_FLAG_FD_SB_ENABLED |
11989 I40E_FLAG_FD_ATR_ENABLED |
11990 I40E_FLAG_VMDQ_ENABLED);
11991 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11993 /* rework the queue expectations without MSIX */
11994 i40e_determine_queue_usage(pf);
11998 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11999 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12000 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12001 vectors = pci_enable_msi(pf->pdev);
12003 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12005 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12007 vectors = 1; /* one MSI or Legacy vector */
12010 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12011 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12013 /* set up vector assignment tracking */
12014 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12015 pf->irq_pile = kzalloc(size, GFP_KERNEL);
12019 pf->irq_pile->num_entries = vectors;
12021 /* track first vector for misc interrupts, ignore return */
12022 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12028 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12029 * @pf: private board data structure
12031 * Restore the interrupt scheme that was cleared when we suspended the
12032 * device. This should be called during resume to re-allocate the q_vectors
12033 * and reacquire IRQs.
12035 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12039 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
12040 * scheme. We need to re-enabled them here in order to attempt to
12041 * re-acquire the MSI or MSI-X vectors
12043 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12045 err = i40e_init_interrupt_scheme(pf);
12049 /* Now that we've re-acquired IRQs, we need to remap the vectors and
12050 * rings together again.
12052 for (i = 0; i < pf->num_alloc_vsi; i++) {
12054 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12057 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12061 err = i40e_setup_misc_vector(pf);
12065 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12066 i40e_client_update_msix_info(pf);
12073 i40e_vsi_free_q_vectors(pf->vsi[i]);
12080 * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12081 * non queue events in recovery mode
12082 * @pf: board private structure
12084 * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12085 * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12086 * This is handled differently than in recovery mode since no Tx/Rx resources
12087 * are being allocated.
12089 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12093 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12094 err = i40e_setup_misc_vector(pf);
12097 dev_info(&pf->pdev->dev,
12098 "MSI-X misc vector request failed, error %d\n",
12103 u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12105 err = request_irq(pf->pdev->irq, i40e_intr, flags,
12109 dev_info(&pf->pdev->dev,
12110 "MSI/legacy misc vector request failed, error %d\n",
12114 i40e_enable_misc_int_causes(pf);
12115 i40e_irq_dynamic_enable_icr0(pf);
12122 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12123 * @pf: board private structure
12125 * This sets up the handler for MSIX 0, which is used to manage the
12126 * non-queue interrupts, e.g. AdminQ and errors. This is not used
12127 * when in MSI or Legacy interrupt mode.
12129 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12131 struct i40e_hw *hw = &pf->hw;
12134 /* Only request the IRQ once, the first time through. */
12135 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12136 err = request_irq(pf->msix_entries[0].vector,
12137 i40e_intr, 0, pf->int_name, pf);
12139 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12140 dev_info(&pf->pdev->dev,
12141 "request_irq for %s failed: %d\n",
12142 pf->int_name, err);
12147 i40e_enable_misc_int_causes(pf);
12149 /* associate no queues to the misc vector */
12150 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12151 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12155 i40e_irq_dynamic_enable_icr0(pf);
12161 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12162 * @vsi: Pointer to vsi structure
12163 * @seed: Buffter to store the hash keys
12164 * @lut: Buffer to store the lookup table entries
12165 * @lut_size: Size of buffer to store the lookup table entries
12167 * Return 0 on success, negative on failure
12169 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12170 u8 *lut, u16 lut_size)
12172 struct i40e_pf *pf = vsi->back;
12173 struct i40e_hw *hw = &pf->hw;
12177 ret = i40e_aq_get_rss_key(hw, vsi->id,
12178 (struct i40e_aqc_get_set_rss_key_data *)seed);
12180 dev_info(&pf->pdev->dev,
12181 "Cannot get RSS key, err %s aq_err %s\n",
12182 i40e_stat_str(&pf->hw, ret),
12183 i40e_aq_str(&pf->hw,
12184 pf->hw.aq.asq_last_status));
12190 bool pf_lut = vsi->type == I40E_VSI_MAIN;
12192 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12194 dev_info(&pf->pdev->dev,
12195 "Cannot get RSS lut, err %s aq_err %s\n",
12196 i40e_stat_str(&pf->hw, ret),
12197 i40e_aq_str(&pf->hw,
12198 pf->hw.aq.asq_last_status));
12207 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12208 * @vsi: Pointer to vsi structure
12209 * @seed: RSS hash seed
12210 * @lut: Lookup table
12211 * @lut_size: Lookup table size
12213 * Returns 0 on success, negative on failure
12215 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12216 const u8 *lut, u16 lut_size)
12218 struct i40e_pf *pf = vsi->back;
12219 struct i40e_hw *hw = &pf->hw;
12220 u16 vf_id = vsi->vf_id;
12223 /* Fill out hash function seed */
12225 u32 *seed_dw = (u32 *)seed;
12227 if (vsi->type == I40E_VSI_MAIN) {
12228 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12229 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12230 } else if (vsi->type == I40E_VSI_SRIOV) {
12231 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12232 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12234 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12239 u32 *lut_dw = (u32 *)lut;
12241 if (vsi->type == I40E_VSI_MAIN) {
12242 if (lut_size != I40E_HLUT_ARRAY_SIZE)
12244 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12245 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12246 } else if (vsi->type == I40E_VSI_SRIOV) {
12247 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12249 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12250 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12252 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12261 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12262 * @vsi: Pointer to VSI structure
12263 * @seed: Buffer to store the keys
12264 * @lut: Buffer to store the lookup table entries
12265 * @lut_size: Size of buffer to store the lookup table entries
12267 * Returns 0 on success, negative on failure
12269 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12270 u8 *lut, u16 lut_size)
12272 struct i40e_pf *pf = vsi->back;
12273 struct i40e_hw *hw = &pf->hw;
12277 u32 *seed_dw = (u32 *)seed;
12279 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12280 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12283 u32 *lut_dw = (u32 *)lut;
12285 if (lut_size != I40E_HLUT_ARRAY_SIZE)
12287 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12288 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12295 * i40e_config_rss - Configure RSS keys and lut
12296 * @vsi: Pointer to VSI structure
12297 * @seed: RSS hash seed
12298 * @lut: Lookup table
12299 * @lut_size: Lookup table size
12301 * Returns 0 on success, negative on failure
12303 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12305 struct i40e_pf *pf = vsi->back;
12307 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12308 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12310 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12314 * i40e_get_rss - Get RSS keys and lut
12315 * @vsi: Pointer to VSI structure
12316 * @seed: Buffer to store the keys
12317 * @lut: Buffer to store the lookup table entries
12318 * @lut_size: Size of buffer to store the lookup table entries
12320 * Returns 0 on success, negative on failure
12322 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12324 struct i40e_pf *pf = vsi->back;
12326 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12327 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12329 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12333 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12334 * @pf: Pointer to board private structure
12335 * @lut: Lookup table
12336 * @rss_table_size: Lookup table size
12337 * @rss_size: Range of queue number for hashing
12339 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12340 u16 rss_table_size, u16 rss_size)
12344 for (i = 0; i < rss_table_size; i++)
12345 lut[i] = i % rss_size;
12349 * i40e_pf_config_rss - Prepare for RSS if used
12350 * @pf: board private structure
12352 static int i40e_pf_config_rss(struct i40e_pf *pf)
12354 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12355 u8 seed[I40E_HKEY_ARRAY_SIZE];
12357 struct i40e_hw *hw = &pf->hw;
12362 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12363 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12364 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12365 hena |= i40e_pf_get_default_rss_hena(pf);
12367 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12368 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12370 /* Determine the RSS table size based on the hardware capabilities */
12371 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12372 reg_val = (pf->rss_table_size == 512) ?
12373 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12374 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12375 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12377 /* Determine the RSS size of the VSI */
12378 if (!vsi->rss_size) {
12380 /* If the firmware does something weird during VSI init, we
12381 * could end up with zero TCs. Check for that to avoid
12382 * divide-by-zero. It probably won't pass traffic, but it also
12385 qcount = vsi->num_queue_pairs /
12386 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12387 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12389 if (!vsi->rss_size)
12392 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12396 /* Use user configured lut if there is one, otherwise use default */
12397 if (vsi->rss_lut_user)
12398 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12400 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12402 /* Use user configured hash key if there is one, otherwise
12405 if (vsi->rss_hkey_user)
12406 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12408 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12409 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12416 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12417 * @pf: board private structure
12418 * @queue_count: the requested queue count for rss.
12420 * returns 0 if rss is not enabled, if enabled returns the final rss queue
12421 * count which may be different from the requested queue count.
12422 * Note: expects to be called while under rtnl_lock()
12424 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12426 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12429 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12432 queue_count = min_t(int, queue_count, num_online_cpus());
12433 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12435 if (queue_count != vsi->num_queue_pairs) {
12438 vsi->req_queue_pairs = queue_count;
12439 i40e_prep_for_reset(pf);
12440 if (test_bit(__I40E_IN_REMOVE, pf->state))
12441 return pf->alloc_rss_size;
12443 pf->alloc_rss_size = new_rss_size;
12445 i40e_reset_and_rebuild(pf, true, true);
12447 /* Discard the user configured hash keys and lut, if less
12448 * queues are enabled.
12450 if (queue_count < vsi->rss_size) {
12451 i40e_clear_rss_config_user(vsi);
12452 dev_dbg(&pf->pdev->dev,
12453 "discard user configured hash keys and lut\n");
12456 /* Reset vsi->rss_size, as number of enabled queues changed */
12457 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12458 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12460 i40e_pf_config_rss(pf);
12462 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
12463 vsi->req_queue_pairs, pf->rss_size_max);
12464 return pf->alloc_rss_size;
12468 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12469 * @pf: board private structure
12471 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12473 i40e_status status;
12474 bool min_valid, max_valid;
12475 u32 max_bw, min_bw;
12477 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12478 &min_valid, &max_valid);
12482 pf->min_bw = min_bw;
12484 pf->max_bw = max_bw;
12491 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12492 * @pf: board private structure
12494 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12496 struct i40e_aqc_configure_partition_bw_data bw_data;
12497 i40e_status status;
12499 memset(&bw_data, 0, sizeof(bw_data));
12501 /* Set the valid bit for this PF */
12502 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12503 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12504 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12506 /* Set the new bandwidths */
12507 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12513 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12514 * @pf: board private structure
12516 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12518 /* Commit temporary BW setting to permanent NVM image */
12519 enum i40e_admin_queue_err last_aq_status;
12523 if (pf->hw.partition_id != 1) {
12524 dev_info(&pf->pdev->dev,
12525 "Commit BW only works on partition 1! This is partition %d",
12526 pf->hw.partition_id);
12527 ret = I40E_NOT_SUPPORTED;
12528 goto bw_commit_out;
12531 /* Acquire NVM for read access */
12532 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12533 last_aq_status = pf->hw.aq.asq_last_status;
12535 dev_info(&pf->pdev->dev,
12536 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12537 i40e_stat_str(&pf->hw, ret),
12538 i40e_aq_str(&pf->hw, last_aq_status));
12539 goto bw_commit_out;
12542 /* Read word 0x10 of NVM - SW compatibility word 1 */
12543 ret = i40e_aq_read_nvm(&pf->hw,
12544 I40E_SR_NVM_CONTROL_WORD,
12545 0x10, sizeof(nvm_word), &nvm_word,
12547 /* Save off last admin queue command status before releasing
12550 last_aq_status = pf->hw.aq.asq_last_status;
12551 i40e_release_nvm(&pf->hw);
12553 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12554 i40e_stat_str(&pf->hw, ret),
12555 i40e_aq_str(&pf->hw, last_aq_status));
12556 goto bw_commit_out;
12559 /* Wait a bit for NVM release to complete */
12562 /* Acquire NVM for write access */
12563 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12564 last_aq_status = pf->hw.aq.asq_last_status;
12566 dev_info(&pf->pdev->dev,
12567 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12568 i40e_stat_str(&pf->hw, ret),
12569 i40e_aq_str(&pf->hw, last_aq_status));
12570 goto bw_commit_out;
12572 /* Write it back out unchanged to initiate update NVM,
12573 * which will force a write of the shadow (alt) RAM to
12574 * the NVM - thus storing the bandwidth values permanently.
12576 ret = i40e_aq_update_nvm(&pf->hw,
12577 I40E_SR_NVM_CONTROL_WORD,
12578 0x10, sizeof(nvm_word),
12579 &nvm_word, true, 0, NULL);
12580 /* Save off last admin queue command status before releasing
12583 last_aq_status = pf->hw.aq.asq_last_status;
12584 i40e_release_nvm(&pf->hw);
12586 dev_info(&pf->pdev->dev,
12587 "BW settings NOT SAVED, err %s aq_err %s\n",
12588 i40e_stat_str(&pf->hw, ret),
12589 i40e_aq_str(&pf->hw, last_aq_status));
12596 * i40e_is_total_port_shutdown_enabled - read NVM and return value
12597 * if total port shutdown feature is enabled for this PF
12598 * @pf: board private structure
12600 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12602 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED BIT(4)
12603 #define I40E_FEATURES_ENABLE_PTR 0x2A
12604 #define I40E_CURRENT_SETTING_PTR 0x2B
12605 #define I40E_LINK_BEHAVIOR_WORD_OFFSET 0x2D
12606 #define I40E_LINK_BEHAVIOR_WORD_LENGTH 0x1
12607 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED BIT(0)
12608 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH 4
12609 i40e_status read_status = I40E_SUCCESS;
12610 u16 sr_emp_sr_settings_ptr = 0;
12611 u16 features_enable = 0;
12612 u16 link_behavior = 0;
12615 read_status = i40e_read_nvm_word(&pf->hw,
12616 I40E_SR_EMP_SR_SETTINGS_PTR,
12617 &sr_emp_sr_settings_ptr);
12620 read_status = i40e_read_nvm_word(&pf->hw,
12621 sr_emp_sr_settings_ptr +
12622 I40E_FEATURES_ENABLE_PTR,
12626 if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12627 read_status = i40e_read_nvm_module_data(&pf->hw,
12628 I40E_SR_EMP_SR_SETTINGS_PTR,
12629 I40E_CURRENT_SETTING_PTR,
12630 I40E_LINK_BEHAVIOR_WORD_OFFSET,
12631 I40E_LINK_BEHAVIOR_WORD_LENGTH,
12635 link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12636 ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12641 dev_warn(&pf->pdev->dev,
12642 "total-port-shutdown feature is off due to read nvm error: %s\n",
12643 i40e_stat_str(&pf->hw, read_status));
12648 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12649 * @pf: board private structure to initialize
12651 * i40e_sw_init initializes the Adapter private data structure.
12652 * Fields are initialized based on PCI device information and
12653 * OS network device settings (MTU size).
12655 static int i40e_sw_init(struct i40e_pf *pf)
12661 /* Set default capability flags */
12662 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12663 I40E_FLAG_MSI_ENABLED |
12664 I40E_FLAG_MSIX_ENABLED;
12666 /* Set default ITR */
12667 pf->rx_itr_default = I40E_ITR_RX_DEF;
12668 pf->tx_itr_default = I40E_ITR_TX_DEF;
12670 /* Depending on PF configurations, it is possible that the RSS
12671 * maximum might end up larger than the available queues
12673 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12674 pf->alloc_rss_size = 1;
12675 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12676 pf->rss_size_max = min_t(int, pf->rss_size_max,
12677 pf->hw.func_caps.num_tx_qp);
12679 /* find the next higher power-of-2 of num cpus */
12680 pow = roundup_pow_of_two(num_online_cpus());
12681 pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12683 if (pf->hw.func_caps.rss) {
12684 pf->flags |= I40E_FLAG_RSS_ENABLED;
12685 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12686 num_online_cpus());
12689 /* MFP mode enabled */
12690 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12691 pf->flags |= I40E_FLAG_MFP_ENABLED;
12692 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12693 if (i40e_get_partition_bw_setting(pf)) {
12694 dev_warn(&pf->pdev->dev,
12695 "Could not get partition bw settings\n");
12697 dev_info(&pf->pdev->dev,
12698 "Partition BW Min = %8.8x, Max = %8.8x\n",
12699 pf->min_bw, pf->max_bw);
12701 /* nudge the Tx scheduler */
12702 i40e_set_partition_bw_setting(pf);
12706 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12707 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12708 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12709 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12710 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12711 pf->hw.num_partitions > 1)
12712 dev_info(&pf->pdev->dev,
12713 "Flow Director Sideband mode Disabled in MFP mode\n");
12715 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12716 pf->fdir_pf_filter_count =
12717 pf->hw.func_caps.fd_filters_guaranteed;
12718 pf->hw.fdir_shared_filter_count =
12719 pf->hw.func_caps.fd_filters_best_effort;
12722 if (pf->hw.mac.type == I40E_MAC_X722) {
12723 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12724 I40E_HW_128_QP_RSS_CAPABLE |
12725 I40E_HW_ATR_EVICT_CAPABLE |
12726 I40E_HW_WB_ON_ITR_CAPABLE |
12727 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12728 I40E_HW_NO_PCI_LINK_CHECK |
12729 I40E_HW_USE_SET_LLDP_MIB |
12730 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12731 I40E_HW_PTP_L4_CAPABLE |
12732 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12733 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12735 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12736 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12737 I40E_FDEVICT_PCTYPE_DEFAULT) {
12738 dev_warn(&pf->pdev->dev,
12739 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12740 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12742 } else if ((pf->hw.aq.api_maj_ver > 1) ||
12743 ((pf->hw.aq.api_maj_ver == 1) &&
12744 (pf->hw.aq.api_min_ver > 4))) {
12745 /* Supported in FW API version higher than 1.4 */
12746 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12749 /* Enable HW ATR eviction if possible */
12750 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12751 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12753 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12754 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12755 (pf->hw.aq.fw_maj_ver < 4))) {
12756 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12757 /* No DCB support for FW < v4.33 */
12758 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12761 /* Disable FW LLDP if FW < v4.3 */
12762 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12763 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12764 (pf->hw.aq.fw_maj_ver < 4)))
12765 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12767 /* Use the FW Set LLDP MIB API if FW > v4.40 */
12768 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12769 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12770 (pf->hw.aq.fw_maj_ver >= 5)))
12771 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12773 /* Enable PTP L4 if FW > v6.0 */
12774 if (pf->hw.mac.type == I40E_MAC_XL710 &&
12775 pf->hw.aq.fw_maj_ver >= 6)
12776 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12778 if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12779 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12780 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12781 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12784 if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12785 pf->flags |= I40E_FLAG_IWARP_ENABLED;
12786 /* IWARP needs one extra vector for CQP just like MISC.*/
12787 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12789 /* Stopping FW LLDP engine is supported on XL710 and X722
12790 * starting from FW versions determined in i40e_init_adminq.
12791 * Stopping the FW LLDP engine is not supported on XL710
12792 * if NPAR is functioning so unset this hw flag in this case.
12794 if (pf->hw.mac.type == I40E_MAC_XL710 &&
12795 pf->hw.func_caps.npar_enable &&
12796 (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12797 pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12799 #ifdef CONFIG_PCI_IOV
12800 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12801 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12802 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12803 pf->num_req_vfs = min_t(int,
12804 pf->hw.func_caps.num_vfs,
12805 I40E_MAX_VF_COUNT);
12807 #endif /* CONFIG_PCI_IOV */
12808 pf->eeprom_version = 0xDEAD;
12809 pf->lan_veb = I40E_NO_VEB;
12810 pf->lan_vsi = I40E_NO_VSI;
12812 /* By default FW has this off for performance reasons */
12813 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12815 /* set up queue assignment tracking */
12816 size = sizeof(struct i40e_lump_tracking)
12817 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12818 pf->qp_pile = kzalloc(size, GFP_KERNEL);
12819 if (!pf->qp_pile) {
12823 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12825 pf->tx_timeout_recovery_level = 1;
12827 if (pf->hw.mac.type != I40E_MAC_X722 &&
12828 i40e_is_total_port_shutdown_enabled(pf)) {
12829 /* Link down on close must be on when total port shutdown
12830 * is enabled for a given port
12832 pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12833 I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12834 dev_info(&pf->pdev->dev,
12835 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12837 mutex_init(&pf->switch_mutex);
12844 * i40e_set_ntuple - set the ntuple feature flag and take action
12845 * @pf: board private structure to initialize
12846 * @features: the feature set that the stack is suggesting
12848 * returns a bool to indicate if reset needs to happen
12850 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12852 bool need_reset = false;
12854 /* Check if Flow Director n-tuple support was enabled or disabled. If
12855 * the state changed, we need to reset.
12857 if (features & NETIF_F_NTUPLE) {
12858 /* Enable filters and mark for reset */
12859 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12861 /* enable FD_SB only if there is MSI-X vector and no cloud
12864 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12865 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12866 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12869 /* turn off filters, mark for reset and clear SW filter list */
12870 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12872 i40e_fdir_filter_exit(pf);
12874 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12875 clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12876 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12878 /* reset fd counters */
12879 pf->fd_add_err = 0;
12880 pf->fd_atr_cnt = 0;
12881 /* if ATR was auto disabled it can be re-enabled. */
12882 if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12883 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12884 (I40E_DEBUG_FD & pf->hw.debug_mask))
12885 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12891 * i40e_clear_rss_lut - clear the rx hash lookup table
12892 * @vsi: the VSI being configured
12894 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12896 struct i40e_pf *pf = vsi->back;
12897 struct i40e_hw *hw = &pf->hw;
12898 u16 vf_id = vsi->vf_id;
12901 if (vsi->type == I40E_VSI_MAIN) {
12902 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12903 wr32(hw, I40E_PFQF_HLUT(i), 0);
12904 } else if (vsi->type == I40E_VSI_SRIOV) {
12905 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12906 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12908 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12913 * i40e_set_features - set the netdev feature flags
12914 * @netdev: ptr to the netdev being adjusted
12915 * @features: the feature set that the stack is suggesting
12916 * Note: expects to be called while under rtnl_lock()
12918 static int i40e_set_features(struct net_device *netdev,
12919 netdev_features_t features)
12921 struct i40e_netdev_priv *np = netdev_priv(netdev);
12922 struct i40e_vsi *vsi = np->vsi;
12923 struct i40e_pf *pf = vsi->back;
12926 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12927 i40e_pf_config_rss(pf);
12928 else if (!(features & NETIF_F_RXHASH) &&
12929 netdev->features & NETIF_F_RXHASH)
12930 i40e_clear_rss_lut(vsi);
12932 if (features & NETIF_F_HW_VLAN_CTAG_RX)
12933 i40e_vlan_stripping_enable(vsi);
12935 i40e_vlan_stripping_disable(vsi);
12937 if (!(features & NETIF_F_HW_TC) &&
12938 (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12939 dev_err(&pf->pdev->dev,
12940 "Offloaded tc filters active, can't turn hw_tc_offload off");
12944 if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12945 i40e_del_all_macvlans(vsi);
12947 need_reset = i40e_set_ntuple(pf, features);
12950 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12955 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12956 unsigned int table, unsigned int idx,
12957 struct udp_tunnel_info *ti)
12959 struct i40e_netdev_priv *np = netdev_priv(netdev);
12960 struct i40e_hw *hw = &np->vsi->back->hw;
12961 u8 type, filter_index;
12964 type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12965 I40E_AQC_TUNNEL_TYPE_NGE;
12967 ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12970 netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12971 i40e_stat_str(hw, ret),
12972 i40e_aq_str(hw, hw->aq.asq_last_status));
12976 udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12980 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12981 unsigned int table, unsigned int idx,
12982 struct udp_tunnel_info *ti)
12984 struct i40e_netdev_priv *np = netdev_priv(netdev);
12985 struct i40e_hw *hw = &np->vsi->back->hw;
12988 ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12990 netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12991 i40e_stat_str(hw, ret),
12992 i40e_aq_str(hw, hw->aq.asq_last_status));
12999 static int i40e_get_phys_port_id(struct net_device *netdev,
13000 struct netdev_phys_item_id *ppid)
13002 struct i40e_netdev_priv *np = netdev_priv(netdev);
13003 struct i40e_pf *pf = np->vsi->back;
13004 struct i40e_hw *hw = &pf->hw;
13006 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13007 return -EOPNOTSUPP;
13009 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13010 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13016 * i40e_ndo_fdb_add - add an entry to the hardware database
13017 * @ndm: the input from the stack
13018 * @tb: pointer to array of nladdr (unused)
13019 * @dev: the net device pointer
13020 * @addr: the MAC address entry being added
13022 * @flags: instructions from stack about fdb operation
13023 * @extack: netlink extended ack, unused currently
13025 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13026 struct net_device *dev,
13027 const unsigned char *addr, u16 vid,
13029 struct netlink_ext_ack *extack)
13031 struct i40e_netdev_priv *np = netdev_priv(dev);
13032 struct i40e_pf *pf = np->vsi->back;
13035 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13036 return -EOPNOTSUPP;
13039 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13043 /* Hardware does not support aging addresses so if a
13044 * ndm_state is given only allow permanent addresses
13046 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13047 netdev_info(dev, "FDB only supports static addresses\n");
13051 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13052 err = dev_uc_add_excl(dev, addr);
13053 else if (is_multicast_ether_addr(addr))
13054 err = dev_mc_add_excl(dev, addr);
13058 /* Only return duplicate errors if NLM_F_EXCL is set */
13059 if (err == -EEXIST && !(flags & NLM_F_EXCL))
13066 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13067 * @dev: the netdev being configured
13068 * @nlh: RTNL message
13069 * @flags: bridge flags
13070 * @extack: netlink extended ack
13072 * Inserts a new hardware bridge if not already created and
13073 * enables the bridging mode requested (VEB or VEPA). If the
13074 * hardware bridge has already been inserted and the request
13075 * is to change the mode then that requires a PF reset to
13076 * allow rebuild of the components with required hardware
13077 * bridge mode enabled.
13079 * Note: expects to be called while under rtnl_lock()
13081 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13082 struct nlmsghdr *nlh,
13084 struct netlink_ext_ack *extack)
13086 struct i40e_netdev_priv *np = netdev_priv(dev);
13087 struct i40e_vsi *vsi = np->vsi;
13088 struct i40e_pf *pf = vsi->back;
13089 struct i40e_veb *veb = NULL;
13090 struct nlattr *attr, *br_spec;
13093 /* Only for PF VSI for now */
13094 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13095 return -EOPNOTSUPP;
13097 /* Find the HW bridge for PF VSI */
13098 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13099 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13103 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13105 nla_for_each_nested(attr, br_spec, rem) {
13108 if (nla_type(attr) != IFLA_BRIDGE_MODE)
13111 mode = nla_get_u16(attr);
13112 if ((mode != BRIDGE_MODE_VEPA) &&
13113 (mode != BRIDGE_MODE_VEB))
13116 /* Insert a new HW bridge */
13118 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13119 vsi->tc_config.enabled_tc);
13121 veb->bridge_mode = mode;
13122 i40e_config_bridge_mode(veb);
13124 /* No Bridge HW offload available */
13128 } else if (mode != veb->bridge_mode) {
13129 /* Existing HW bridge but different mode needs reset */
13130 veb->bridge_mode = mode;
13131 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13132 if (mode == BRIDGE_MODE_VEB)
13133 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13135 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13136 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13145 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13148 * @seq: RTNL message seq #
13149 * @dev: the netdev being configured
13150 * @filter_mask: unused
13151 * @nlflags: netlink flags passed in
13153 * Return the mode in which the hardware bridge is operating in
13156 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13157 struct net_device *dev,
13158 u32 __always_unused filter_mask,
13161 struct i40e_netdev_priv *np = netdev_priv(dev);
13162 struct i40e_vsi *vsi = np->vsi;
13163 struct i40e_pf *pf = vsi->back;
13164 struct i40e_veb *veb = NULL;
13167 /* Only for PF VSI for now */
13168 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13169 return -EOPNOTSUPP;
13171 /* Find the HW bridge for the PF VSI */
13172 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13173 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13180 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13181 0, 0, nlflags, filter_mask, NULL);
13185 * i40e_features_check - Validate encapsulated packet conforms to limits
13187 * @dev: This physical port's netdev
13188 * @features: Offload features that the stack believes apply
13190 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13191 struct net_device *dev,
13192 netdev_features_t features)
13196 /* No point in doing any of this if neither checksum nor GSO are
13197 * being requested for this frame. We can rule out both by just
13198 * checking for CHECKSUM_PARTIAL
13200 if (skb->ip_summed != CHECKSUM_PARTIAL)
13203 /* We cannot support GSO if the MSS is going to be less than
13204 * 64 bytes. If it is then we need to drop support for GSO.
13206 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13207 features &= ~NETIF_F_GSO_MASK;
13209 /* MACLEN can support at most 63 words */
13210 len = skb_network_header(skb) - skb->data;
13211 if (len & ~(63 * 2))
13214 /* IPLEN and EIPLEN can support at most 127 dwords */
13215 len = skb_transport_header(skb) - skb_network_header(skb);
13216 if (len & ~(127 * 4))
13219 if (skb->encapsulation) {
13220 /* L4TUNLEN can support 127 words */
13221 len = skb_inner_network_header(skb) - skb_transport_header(skb);
13222 if (len & ~(127 * 2))
13225 /* IPLEN can support at most 127 dwords */
13226 len = skb_inner_transport_header(skb) -
13227 skb_inner_network_header(skb);
13228 if (len & ~(127 * 4))
13232 /* No need to validate L4LEN as TCP is the only protocol with a
13233 * flexible value and we support all possible values supported
13234 * by TCP, which is at most 15 dwords
13239 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13243 * i40e_xdp_setup - add/remove an XDP program
13244 * @vsi: VSI to changed
13245 * @prog: XDP program
13246 * @extack: netlink extended ack
13248 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13249 struct netlink_ext_ack *extack)
13251 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13252 struct i40e_pf *pf = vsi->back;
13253 struct bpf_prog *old_prog;
13257 /* Don't allow frames that span over multiple buffers */
13258 if (frame_size > vsi->rx_buf_len) {
13259 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13263 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
13264 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13267 i40e_prep_for_reset(pf);
13269 /* VSI shall be deleted in a moment, just return EINVAL */
13270 if (test_bit(__I40E_IN_REMOVE, pf->state))
13273 old_prog = xchg(&vsi->xdp_prog, prog);
13277 /* Wait until ndo_xsk_wakeup completes. */
13279 i40e_reset_and_rebuild(pf, true, true);
13282 for (i = 0; i < vsi->num_queue_pairs; i++)
13283 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13286 bpf_prog_put(old_prog);
13288 /* Kick start the NAPI context if there is an AF_XDP socket open
13289 * on that queue id. This so that receiving will start.
13291 if (need_reset && prog)
13292 for (i = 0; i < vsi->num_queue_pairs; i++)
13293 if (vsi->xdp_rings[i]->xsk_pool)
13294 (void)i40e_xsk_wakeup(vsi->netdev, i,
13301 * i40e_enter_busy_conf - Enters busy config state
13304 * Returns 0 on success, <0 for failure.
13306 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13308 struct i40e_pf *pf = vsi->back;
13311 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13315 usleep_range(1000, 2000);
13322 * i40e_exit_busy_conf - Exits busy config state
13325 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13327 struct i40e_pf *pf = vsi->back;
13329 clear_bit(__I40E_CONFIG_BUSY, pf->state);
13333 * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13335 * @queue_pair: queue pair
13337 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13339 memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13340 sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13341 memset(&vsi->tx_rings[queue_pair]->stats, 0,
13342 sizeof(vsi->tx_rings[queue_pair]->stats));
13343 if (i40e_enabled_xdp_vsi(vsi)) {
13344 memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13345 sizeof(vsi->xdp_rings[queue_pair]->stats));
13350 * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13352 * @queue_pair: queue pair
13354 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13356 i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13357 if (i40e_enabled_xdp_vsi(vsi)) {
13358 /* Make sure that in-progress ndo_xdp_xmit calls are
13362 i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13364 i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13368 * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13370 * @queue_pair: queue pair
13371 * @enable: true for enable, false for disable
13373 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13376 struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13377 struct i40e_q_vector *q_vector = rxr->q_vector;
13382 /* All rings in a qp belong to the same qvector. */
13383 if (q_vector->rx.ring || q_vector->tx.ring) {
13385 napi_enable(&q_vector->napi);
13387 napi_disable(&q_vector->napi);
13392 * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13394 * @queue_pair: queue pair
13395 * @enable: true for enable, false for disable
13397 * Returns 0 on success, <0 on failure.
13399 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13402 struct i40e_pf *pf = vsi->back;
13405 pf_q = vsi->base_queue + queue_pair;
13406 ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13407 false /*is xdp*/, enable);
13409 dev_info(&pf->pdev->dev,
13410 "VSI seid %d Tx ring %d %sable timeout\n",
13411 vsi->seid, pf_q, (enable ? "en" : "dis"));
13415 i40e_control_rx_q(pf, pf_q, enable);
13416 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13418 dev_info(&pf->pdev->dev,
13419 "VSI seid %d Rx ring %d %sable timeout\n",
13420 vsi->seid, pf_q, (enable ? "en" : "dis"));
13424 /* Due to HW errata, on Rx disable only, the register can
13425 * indicate done before it really is. Needs 50ms to be sure
13430 if (!i40e_enabled_xdp_vsi(vsi))
13433 ret = i40e_control_wait_tx_q(vsi->seid, pf,
13434 pf_q + vsi->alloc_queue_pairs,
13435 true /*is xdp*/, enable);
13437 dev_info(&pf->pdev->dev,
13438 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13439 vsi->seid, pf_q, (enable ? "en" : "dis"));
13446 * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13448 * @queue_pair: queue_pair
13450 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13452 struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13453 struct i40e_pf *pf = vsi->back;
13454 struct i40e_hw *hw = &pf->hw;
13456 /* All rings in a qp belong to the same qvector. */
13457 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13458 i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13460 i40e_irq_dynamic_enable_icr0(pf);
13466 * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13468 * @queue_pair: queue_pair
13470 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13472 struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13473 struct i40e_pf *pf = vsi->back;
13474 struct i40e_hw *hw = &pf->hw;
13476 /* For simplicity, instead of removing the qp interrupt causes
13477 * from the interrupt linked list, we simply disable the interrupt, and
13478 * leave the list intact.
13480 * All rings in a qp belong to the same qvector.
13482 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13483 u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13485 wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13487 synchronize_irq(pf->msix_entries[intpf].vector);
13489 /* Legacy and MSI mode - this stops all interrupt handling */
13490 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13491 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13493 synchronize_irq(pf->pdev->irq);
13498 * i40e_queue_pair_disable - Disables a queue pair
13500 * @queue_pair: queue pair
13502 * Returns 0 on success, <0 on failure.
13504 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13508 err = i40e_enter_busy_conf(vsi);
13512 i40e_queue_pair_disable_irq(vsi, queue_pair);
13513 err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13514 i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13515 i40e_queue_pair_clean_rings(vsi, queue_pair);
13516 i40e_queue_pair_reset_stats(vsi, queue_pair);
13522 * i40e_queue_pair_enable - Enables a queue pair
13524 * @queue_pair: queue pair
13526 * Returns 0 on success, <0 on failure.
13528 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13532 err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13536 if (i40e_enabled_xdp_vsi(vsi)) {
13537 err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13542 err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13546 err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13547 i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13548 i40e_queue_pair_enable_irq(vsi, queue_pair);
13550 i40e_exit_busy_conf(vsi);
13556 * i40e_xdp - implements ndo_bpf for i40e
13558 * @xdp: XDP command
13560 static int i40e_xdp(struct net_device *dev,
13561 struct netdev_bpf *xdp)
13563 struct i40e_netdev_priv *np = netdev_priv(dev);
13564 struct i40e_vsi *vsi = np->vsi;
13566 if (vsi->type != I40E_VSI_MAIN)
13569 switch (xdp->command) {
13570 case XDP_SETUP_PROG:
13571 return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13572 case XDP_SETUP_XSK_POOL:
13573 return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13574 xdp->xsk.queue_id);
13580 static const struct net_device_ops i40e_netdev_ops = {
13581 .ndo_open = i40e_open,
13582 .ndo_stop = i40e_close,
13583 .ndo_start_xmit = i40e_lan_xmit_frame,
13584 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
13585 .ndo_set_rx_mode = i40e_set_rx_mode,
13586 .ndo_validate_addr = eth_validate_addr,
13587 .ndo_set_mac_address = i40e_set_mac,
13588 .ndo_change_mtu = i40e_change_mtu,
13589 .ndo_eth_ioctl = i40e_ioctl,
13590 .ndo_tx_timeout = i40e_tx_timeout,
13591 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
13592 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
13593 #ifdef CONFIG_NET_POLL_CONTROLLER
13594 .ndo_poll_controller = i40e_netpoll,
13596 .ndo_setup_tc = __i40e_setup_tc,
13597 .ndo_select_queue = i40e_lan_select_queue,
13598 .ndo_set_features = i40e_set_features,
13599 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
13600 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
13601 .ndo_get_vf_stats = i40e_get_vf_stats,
13602 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
13603 .ndo_get_vf_config = i40e_ndo_get_vf_config,
13604 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
13605 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
13606 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
13607 .ndo_get_phys_port_id = i40e_get_phys_port_id,
13608 .ndo_fdb_add = i40e_ndo_fdb_add,
13609 .ndo_features_check = i40e_features_check,
13610 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
13611 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
13612 .ndo_bpf = i40e_xdp,
13613 .ndo_xdp_xmit = i40e_xdp_xmit,
13614 .ndo_xsk_wakeup = i40e_xsk_wakeup,
13615 .ndo_dfwd_add_station = i40e_fwd_add,
13616 .ndo_dfwd_del_station = i40e_fwd_del,
13620 * i40e_config_netdev - Setup the netdev flags
13621 * @vsi: the VSI being configured
13623 * Returns 0 on success, negative value on failure
13625 static int i40e_config_netdev(struct i40e_vsi *vsi)
13627 struct i40e_pf *pf = vsi->back;
13628 struct i40e_hw *hw = &pf->hw;
13629 struct i40e_netdev_priv *np;
13630 struct net_device *netdev;
13631 u8 broadcast[ETH_ALEN];
13632 u8 mac_addr[ETH_ALEN];
13634 netdev_features_t hw_enc_features;
13635 netdev_features_t hw_features;
13637 etherdev_size = sizeof(struct i40e_netdev_priv);
13638 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13642 vsi->netdev = netdev;
13643 np = netdev_priv(netdev);
13646 hw_enc_features = NETIF_F_SG |
13649 NETIF_F_SOFT_FEATURES |
13654 NETIF_F_GSO_GRE_CSUM |
13655 NETIF_F_GSO_PARTIAL |
13656 NETIF_F_GSO_IPXIP4 |
13657 NETIF_F_GSO_IPXIP6 |
13658 NETIF_F_GSO_UDP_TUNNEL |
13659 NETIF_F_GSO_UDP_TUNNEL_CSUM |
13660 NETIF_F_GSO_UDP_L4 |
13666 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13667 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13669 netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13671 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13673 netdev->hw_enc_features |= hw_enc_features;
13675 /* record features VLANs can make use of */
13676 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13678 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
13679 NETIF_F_GSO_GRE_CSUM | \
13680 NETIF_F_GSO_IPXIP4 | \
13681 NETIF_F_GSO_IPXIP6 | \
13682 NETIF_F_GSO_UDP_TUNNEL | \
13683 NETIF_F_GSO_UDP_TUNNEL_CSUM)
13685 netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13686 netdev->features |= NETIF_F_GSO_PARTIAL |
13687 I40E_GSO_PARTIAL_FEATURES;
13689 netdev->mpls_features |= NETIF_F_SG;
13690 netdev->mpls_features |= NETIF_F_HW_CSUM;
13691 netdev->mpls_features |= NETIF_F_TSO;
13692 netdev->mpls_features |= NETIF_F_TSO6;
13693 netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13695 /* enable macvlan offloads */
13696 netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13698 hw_features = hw_enc_features |
13699 NETIF_F_HW_VLAN_CTAG_TX |
13700 NETIF_F_HW_VLAN_CTAG_RX;
13702 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13703 hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13705 netdev->hw_features |= hw_features;
13707 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13708 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13710 netdev->features &= ~NETIF_F_HW_TC;
13712 if (vsi->type == I40E_VSI_MAIN) {
13713 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13714 ether_addr_copy(mac_addr, hw->mac.perm_addr);
13715 /* The following steps are necessary for two reasons. First,
13716 * some older NVM configurations load a default MAC-VLAN
13717 * filter that will accept any tagged packet, and we want to
13718 * replace this with a normal filter. Additionally, it is
13719 * possible our MAC address was provided by the platform using
13720 * Open Firmware or similar.
13722 * Thus, we need to remove the default filter and install one
13723 * specific to the MAC address.
13725 i40e_rm_default_mac_filter(vsi, mac_addr);
13726 spin_lock_bh(&vsi->mac_filter_hash_lock);
13727 i40e_add_mac_filter(vsi, mac_addr);
13728 spin_unlock_bh(&vsi->mac_filter_hash_lock);
13730 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13731 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13732 * the end, which is 4 bytes long, so force truncation of the
13733 * original name by IFNAMSIZ - 4
13735 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13737 pf->vsi[pf->lan_vsi]->netdev->name);
13738 eth_random_addr(mac_addr);
13740 spin_lock_bh(&vsi->mac_filter_hash_lock);
13741 i40e_add_mac_filter(vsi, mac_addr);
13742 spin_unlock_bh(&vsi->mac_filter_hash_lock);
13745 /* Add the broadcast filter so that we initially will receive
13746 * broadcast packets. Note that when a new VLAN is first added the
13747 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13748 * specific filters as part of transitioning into "vlan" operation.
13749 * When more VLANs are added, the driver will copy each existing MAC
13750 * filter and add it for the new VLAN.
13752 * Broadcast filters are handled specially by
13753 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13754 * promiscuous bit instead of adding this directly as a MAC/VLAN
13755 * filter. The subtask will update the correct broadcast promiscuous
13756 * bits as VLANs become active or inactive.
13758 eth_broadcast_addr(broadcast);
13759 spin_lock_bh(&vsi->mac_filter_hash_lock);
13760 i40e_add_mac_filter(vsi, broadcast);
13761 spin_unlock_bh(&vsi->mac_filter_hash_lock);
13763 eth_hw_addr_set(netdev, mac_addr);
13764 ether_addr_copy(netdev->perm_addr, mac_addr);
13766 /* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13767 netdev->neigh_priv_len = sizeof(u32) * 4;
13769 netdev->priv_flags |= IFF_UNICAST_FLT;
13770 netdev->priv_flags |= IFF_SUPP_NOFCS;
13771 /* Setup netdev TC information */
13772 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13774 netdev->netdev_ops = &i40e_netdev_ops;
13775 netdev->watchdog_timeo = 5 * HZ;
13776 i40e_set_ethtool_ops(netdev);
13778 /* MTU range: 68 - 9706 */
13779 netdev->min_mtu = ETH_MIN_MTU;
13780 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13786 * i40e_vsi_delete - Delete a VSI from the switch
13787 * @vsi: the VSI being removed
13789 * Returns 0 on success, negative value on failure
13791 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13793 /* remove default VSI is not allowed */
13794 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13797 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13801 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13802 * @vsi: the VSI being queried
13804 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13806 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13808 struct i40e_veb *veb;
13809 struct i40e_pf *pf = vsi->back;
13811 /* Uplink is not a bridge so default to VEB */
13812 if (vsi->veb_idx >= I40E_MAX_VEB)
13815 veb = pf->veb[vsi->veb_idx];
13817 dev_info(&pf->pdev->dev,
13818 "There is no veb associated with the bridge\n");
13822 /* Uplink is a bridge in VEPA mode */
13823 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13826 /* Uplink is a bridge in VEB mode */
13830 /* VEPA is now default bridge, so return 0 */
13835 * i40e_add_vsi - Add a VSI to the switch
13836 * @vsi: the VSI being configured
13838 * This initializes a VSI context depending on the VSI type to be added and
13839 * passes it down to the add_vsi aq command.
13841 static int i40e_add_vsi(struct i40e_vsi *vsi)
13844 struct i40e_pf *pf = vsi->back;
13845 struct i40e_hw *hw = &pf->hw;
13846 struct i40e_vsi_context ctxt;
13847 struct i40e_mac_filter *f;
13848 struct hlist_node *h;
13851 u8 enabled_tc = 0x1; /* TC0 enabled */
13854 memset(&ctxt, 0, sizeof(ctxt));
13855 switch (vsi->type) {
13856 case I40E_VSI_MAIN:
13857 /* The PF's main VSI is already setup as part of the
13858 * device initialization, so we'll not bother with
13859 * the add_vsi call, but we will retrieve the current
13862 ctxt.seid = pf->main_vsi_seid;
13863 ctxt.pf_num = pf->hw.pf_id;
13865 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13866 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13868 dev_info(&pf->pdev->dev,
13869 "couldn't get PF vsi config, err %s aq_err %s\n",
13870 i40e_stat_str(&pf->hw, ret),
13871 i40e_aq_str(&pf->hw,
13872 pf->hw.aq.asq_last_status));
13875 vsi->info = ctxt.info;
13876 vsi->info.valid_sections = 0;
13878 vsi->seid = ctxt.seid;
13879 vsi->id = ctxt.vsi_number;
13881 enabled_tc = i40e_pf_get_tc_map(pf);
13883 /* Source pruning is enabled by default, so the flag is
13884 * negative logic - if it's set, we need to fiddle with
13885 * the VSI to disable source pruning.
13887 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13888 memset(&ctxt, 0, sizeof(ctxt));
13889 ctxt.seid = pf->main_vsi_seid;
13890 ctxt.pf_num = pf->hw.pf_id;
13892 ctxt.info.valid_sections |=
13893 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13894 ctxt.info.switch_id =
13895 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13896 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13898 dev_info(&pf->pdev->dev,
13899 "update vsi failed, err %s aq_err %s\n",
13900 i40e_stat_str(&pf->hw, ret),
13901 i40e_aq_str(&pf->hw,
13902 pf->hw.aq.asq_last_status));
13908 /* MFP mode setup queue map and update VSI */
13909 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13910 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13911 memset(&ctxt, 0, sizeof(ctxt));
13912 ctxt.seid = pf->main_vsi_seid;
13913 ctxt.pf_num = pf->hw.pf_id;
13915 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13916 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13918 dev_info(&pf->pdev->dev,
13919 "update vsi failed, err %s aq_err %s\n",
13920 i40e_stat_str(&pf->hw, ret),
13921 i40e_aq_str(&pf->hw,
13922 pf->hw.aq.asq_last_status));
13926 /* update the local VSI info queue map */
13927 i40e_vsi_update_queue_map(vsi, &ctxt);
13928 vsi->info.valid_sections = 0;
13930 /* Default/Main VSI is only enabled for TC0
13931 * reconfigure it to enable all TCs that are
13932 * available on the port in SFP mode.
13933 * For MFP case the iSCSI PF would use this
13934 * flow to enable LAN+iSCSI TC.
13936 ret = i40e_vsi_config_tc(vsi, enabled_tc);
13938 /* Single TC condition is not fatal,
13939 * message and continue
13941 dev_info(&pf->pdev->dev,
13942 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13944 i40e_stat_str(&pf->hw, ret),
13945 i40e_aq_str(&pf->hw,
13946 pf->hw.aq.asq_last_status));
13951 case I40E_VSI_FDIR:
13952 ctxt.pf_num = hw->pf_id;
13954 ctxt.uplink_seid = vsi->uplink_seid;
13955 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13956 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13957 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13958 (i40e_is_vsi_uplink_mode_veb(vsi))) {
13959 ctxt.info.valid_sections |=
13960 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13961 ctxt.info.switch_id =
13962 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13964 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13967 case I40E_VSI_VMDQ2:
13968 ctxt.pf_num = hw->pf_id;
13970 ctxt.uplink_seid = vsi->uplink_seid;
13971 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13972 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13974 /* This VSI is connected to VEB so the switch_id
13975 * should be set to zero by default.
13977 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13978 ctxt.info.valid_sections |=
13979 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13980 ctxt.info.switch_id =
13981 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13984 /* Setup the VSI tx/rx queue map for TC0 only for now */
13985 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13988 case I40E_VSI_SRIOV:
13989 ctxt.pf_num = hw->pf_id;
13990 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13991 ctxt.uplink_seid = vsi->uplink_seid;
13992 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13993 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13995 /* This VSI is connected to VEB so the switch_id
13996 * should be set to zero by default.
13998 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13999 ctxt.info.valid_sections |=
14000 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14001 ctxt.info.switch_id =
14002 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14005 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14006 ctxt.info.valid_sections |=
14007 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14008 ctxt.info.queueing_opt_flags |=
14009 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14010 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14013 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14014 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14015 if (pf->vf[vsi->vf_id].spoofchk) {
14016 ctxt.info.valid_sections |=
14017 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14018 ctxt.info.sec_flags |=
14019 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14020 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14022 /* Setup the VSI tx/rx queue map for TC0 only for now */
14023 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14026 case I40E_VSI_IWARP:
14027 /* send down message to iWARP */
14034 if (vsi->type != I40E_VSI_MAIN) {
14035 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14037 dev_info(&vsi->back->pdev->dev,
14038 "add vsi failed, err %s aq_err %s\n",
14039 i40e_stat_str(&pf->hw, ret),
14040 i40e_aq_str(&pf->hw,
14041 pf->hw.aq.asq_last_status));
14045 vsi->info = ctxt.info;
14046 vsi->info.valid_sections = 0;
14047 vsi->seid = ctxt.seid;
14048 vsi->id = ctxt.vsi_number;
14051 vsi->active_filters = 0;
14052 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14053 spin_lock_bh(&vsi->mac_filter_hash_lock);
14054 /* If macvlan filters already exist, force them to get loaded */
14055 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14056 f->state = I40E_FILTER_NEW;
14059 spin_unlock_bh(&vsi->mac_filter_hash_lock);
14062 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14063 set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14066 /* Update VSI BW information */
14067 ret = i40e_vsi_get_bw_info(vsi);
14069 dev_info(&pf->pdev->dev,
14070 "couldn't get vsi bw info, err %s aq_err %s\n",
14071 i40e_stat_str(&pf->hw, ret),
14072 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14073 /* VSI is already added so not tearing that up */
14082 * i40e_vsi_release - Delete a VSI and free its resources
14083 * @vsi: the VSI being removed
14085 * Returns 0 on success or < 0 on error
14087 int i40e_vsi_release(struct i40e_vsi *vsi)
14089 struct i40e_mac_filter *f;
14090 struct hlist_node *h;
14091 struct i40e_veb *veb = NULL;
14092 struct i40e_pf *pf;
14098 /* release of a VEB-owner or last VSI is not allowed */
14099 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14100 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14101 vsi->seid, vsi->uplink_seid);
14104 if (vsi == pf->vsi[pf->lan_vsi] &&
14105 !test_bit(__I40E_DOWN, pf->state)) {
14106 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14109 set_bit(__I40E_VSI_RELEASING, vsi->state);
14110 uplink_seid = vsi->uplink_seid;
14111 if (vsi->type != I40E_VSI_SRIOV) {
14112 if (vsi->netdev_registered) {
14113 vsi->netdev_registered = false;
14115 /* results in a call to i40e_close() */
14116 unregister_netdev(vsi->netdev);
14119 i40e_vsi_close(vsi);
14121 i40e_vsi_disable_irq(vsi);
14124 spin_lock_bh(&vsi->mac_filter_hash_lock);
14126 /* clear the sync flag on all filters */
14128 __dev_uc_unsync(vsi->netdev, NULL);
14129 __dev_mc_unsync(vsi->netdev, NULL);
14132 /* make sure any remaining filters are marked for deletion */
14133 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14134 __i40e_del_filter(vsi, f);
14136 spin_unlock_bh(&vsi->mac_filter_hash_lock);
14138 i40e_sync_vsi_filters(vsi);
14140 i40e_vsi_delete(vsi);
14141 i40e_vsi_free_q_vectors(vsi);
14143 free_netdev(vsi->netdev);
14144 vsi->netdev = NULL;
14146 i40e_vsi_clear_rings(vsi);
14147 i40e_vsi_clear(vsi);
14149 /* If this was the last thing on the VEB, except for the
14150 * controlling VSI, remove the VEB, which puts the controlling
14151 * VSI onto the next level down in the switch.
14153 * Well, okay, there's one more exception here: don't remove
14154 * the orphan VEBs yet. We'll wait for an explicit remove request
14155 * from up the network stack.
14157 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14159 pf->vsi[i]->uplink_seid == uplink_seid &&
14160 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14161 n++; /* count the VSIs */
14164 for (i = 0; i < I40E_MAX_VEB; i++) {
14167 if (pf->veb[i]->uplink_seid == uplink_seid)
14168 n++; /* count the VEBs */
14169 if (pf->veb[i]->seid == uplink_seid)
14172 if (n == 0 && veb && veb->uplink_seid != 0)
14173 i40e_veb_release(veb);
14179 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14180 * @vsi: ptr to the VSI
14182 * This should only be called after i40e_vsi_mem_alloc() which allocates the
14183 * corresponding SW VSI structure and initializes num_queue_pairs for the
14184 * newly allocated VSI.
14186 * Returns 0 on success or negative on failure
14188 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14191 struct i40e_pf *pf = vsi->back;
14193 if (vsi->q_vectors[0]) {
14194 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14199 if (vsi->base_vector) {
14200 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14201 vsi->seid, vsi->base_vector);
14205 ret = i40e_vsi_alloc_q_vectors(vsi);
14207 dev_info(&pf->pdev->dev,
14208 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14209 vsi->num_q_vectors, vsi->seid, ret);
14210 vsi->num_q_vectors = 0;
14211 goto vector_setup_out;
14214 /* In Legacy mode, we do not have to get any other vector since we
14215 * piggyback on the misc/ICR0 for queue interrupts.
14217 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14219 if (vsi->num_q_vectors)
14220 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14221 vsi->num_q_vectors, vsi->idx);
14222 if (vsi->base_vector < 0) {
14223 dev_info(&pf->pdev->dev,
14224 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14225 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14226 i40e_vsi_free_q_vectors(vsi);
14228 goto vector_setup_out;
14236 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14237 * @vsi: pointer to the vsi.
14239 * This re-allocates a vsi's queue resources.
14241 * Returns pointer to the successfully allocated and configured VSI sw struct
14242 * on success, otherwise returns NULL on failure.
14244 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14246 u16 alloc_queue_pairs;
14247 struct i40e_pf *pf;
14256 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14257 i40e_vsi_clear_rings(vsi);
14259 i40e_vsi_free_arrays(vsi, false);
14260 i40e_set_num_rings_in_vsi(vsi);
14261 ret = i40e_vsi_alloc_arrays(vsi, false);
14265 alloc_queue_pairs = vsi->alloc_queue_pairs *
14266 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14268 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14270 dev_info(&pf->pdev->dev,
14271 "failed to get tracking for %d queues for VSI %d err %d\n",
14272 alloc_queue_pairs, vsi->seid, ret);
14275 vsi->base_queue = ret;
14277 /* Update the FW view of the VSI. Force a reset of TC and queue
14278 * layout configurations.
14280 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14281 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14282 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14283 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14284 if (vsi->type == I40E_VSI_MAIN)
14285 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14287 /* assign it some queues */
14288 ret = i40e_alloc_rings(vsi);
14292 /* map all of the rings to the q_vectors */
14293 i40e_vsi_map_rings_to_vectors(vsi);
14297 i40e_vsi_free_q_vectors(vsi);
14298 if (vsi->netdev_registered) {
14299 vsi->netdev_registered = false;
14300 unregister_netdev(vsi->netdev);
14301 free_netdev(vsi->netdev);
14302 vsi->netdev = NULL;
14304 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14306 i40e_vsi_clear(vsi);
14311 * i40e_vsi_setup - Set up a VSI by a given type
14312 * @pf: board private structure
14314 * @uplink_seid: the switch element to link to
14315 * @param1: usage depends upon VSI type. For VF types, indicates VF id
14317 * This allocates the sw VSI structure and its queue resources, then add a VSI
14318 * to the identified VEB.
14320 * Returns pointer to the successfully allocated and configure VSI sw struct on
14321 * success, otherwise returns NULL on failure.
14323 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14324 u16 uplink_seid, u32 param1)
14326 struct i40e_vsi *vsi = NULL;
14327 struct i40e_veb *veb = NULL;
14328 u16 alloc_queue_pairs;
14332 /* The requested uplink_seid must be either
14333 * - the PF's port seid
14334 * no VEB is needed because this is the PF
14335 * or this is a Flow Director special case VSI
14336 * - seid of an existing VEB
14337 * - seid of a VSI that owns an existing VEB
14338 * - seid of a VSI that doesn't own a VEB
14339 * a new VEB is created and the VSI becomes the owner
14340 * - seid of the PF VSI, which is what creates the first VEB
14341 * this is a special case of the previous
14343 * Find which uplink_seid we were given and create a new VEB if needed
14345 for (i = 0; i < I40E_MAX_VEB; i++) {
14346 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14352 if (!veb && uplink_seid != pf->mac_seid) {
14354 for (i = 0; i < pf->num_alloc_vsi; i++) {
14355 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14361 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14366 if (vsi->uplink_seid == pf->mac_seid)
14367 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14368 vsi->tc_config.enabled_tc);
14369 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14370 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14371 vsi->tc_config.enabled_tc);
14373 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14374 dev_info(&vsi->back->pdev->dev,
14375 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14378 /* We come up by default in VEPA mode if SRIOV is not
14379 * already enabled, in which case we can't force VEPA
14382 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14383 veb->bridge_mode = BRIDGE_MODE_VEPA;
14384 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14386 i40e_config_bridge_mode(veb);
14388 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14389 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14393 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14397 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14398 uplink_seid = veb->seid;
14401 /* get vsi sw struct */
14402 v_idx = i40e_vsi_mem_alloc(pf, type);
14405 vsi = pf->vsi[v_idx];
14409 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14411 if (type == I40E_VSI_MAIN)
14412 pf->lan_vsi = v_idx;
14413 else if (type == I40E_VSI_SRIOV)
14414 vsi->vf_id = param1;
14415 /* assign it some queues */
14416 alloc_queue_pairs = vsi->alloc_queue_pairs *
14417 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14419 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14421 dev_info(&pf->pdev->dev,
14422 "failed to get tracking for %d queues for VSI %d err=%d\n",
14423 alloc_queue_pairs, vsi->seid, ret);
14426 vsi->base_queue = ret;
14428 /* get a VSI from the hardware */
14429 vsi->uplink_seid = uplink_seid;
14430 ret = i40e_add_vsi(vsi);
14434 switch (vsi->type) {
14435 /* setup the netdev if needed */
14436 case I40E_VSI_MAIN:
14437 case I40E_VSI_VMDQ2:
14438 ret = i40e_config_netdev(vsi);
14441 ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14444 ret = register_netdev(vsi->netdev);
14447 vsi->netdev_registered = true;
14448 netif_carrier_off(vsi->netdev);
14449 #ifdef CONFIG_I40E_DCB
14450 /* Setup DCB netlink interface */
14451 i40e_dcbnl_setup(vsi);
14452 #endif /* CONFIG_I40E_DCB */
14454 case I40E_VSI_FDIR:
14455 /* set up vectors and rings if needed */
14456 ret = i40e_vsi_setup_vectors(vsi);
14460 ret = i40e_alloc_rings(vsi);
14464 /* map all of the rings to the q_vectors */
14465 i40e_vsi_map_rings_to_vectors(vsi);
14467 i40e_vsi_reset_stats(vsi);
14470 /* no netdev or rings for the other VSI types */
14474 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14475 (vsi->type == I40E_VSI_VMDQ2)) {
14476 ret = i40e_vsi_config_rss(vsi);
14481 i40e_vsi_free_q_vectors(vsi);
14483 if (vsi->netdev_registered) {
14484 vsi->netdev_registered = false;
14485 unregister_netdev(vsi->netdev);
14486 free_netdev(vsi->netdev);
14487 vsi->netdev = NULL;
14490 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14492 i40e_vsi_clear(vsi);
14498 * i40e_veb_get_bw_info - Query VEB BW information
14499 * @veb: the veb to query
14501 * Query the Tx scheduler BW configuration data for given VEB
14503 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14505 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14506 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14507 struct i40e_pf *pf = veb->pf;
14508 struct i40e_hw *hw = &pf->hw;
14513 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14516 dev_info(&pf->pdev->dev,
14517 "query veb bw config failed, err %s aq_err %s\n",
14518 i40e_stat_str(&pf->hw, ret),
14519 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14523 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14526 dev_info(&pf->pdev->dev,
14527 "query veb bw ets config failed, err %s aq_err %s\n",
14528 i40e_stat_str(&pf->hw, ret),
14529 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14533 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14534 veb->bw_max_quanta = ets_data.tc_bw_max;
14535 veb->is_abs_credits = bw_data.absolute_credits_enable;
14536 veb->enabled_tc = ets_data.tc_valid_bits;
14537 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14538 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14539 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14540 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14541 veb->bw_tc_limit_credits[i] =
14542 le16_to_cpu(bw_data.tc_bw_limits[i]);
14543 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14551 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14552 * @pf: board private structure
14554 * On error: returns error code (negative)
14555 * On success: returns vsi index in PF (positive)
14557 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14560 struct i40e_veb *veb;
14563 /* Need to protect the allocation of switch elements at the PF level */
14564 mutex_lock(&pf->switch_mutex);
14566 /* VEB list may be fragmented if VEB creation/destruction has
14567 * been happening. We can afford to do a quick scan to look
14568 * for any free slots in the list.
14570 * find next empty veb slot, looping back around if necessary
14573 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14575 if (i >= I40E_MAX_VEB) {
14577 goto err_alloc_veb; /* out of VEB slots! */
14580 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14583 goto err_alloc_veb;
14587 veb->enabled_tc = 1;
14592 mutex_unlock(&pf->switch_mutex);
14597 * i40e_switch_branch_release - Delete a branch of the switch tree
14598 * @branch: where to start deleting
14600 * This uses recursion to find the tips of the branch to be
14601 * removed, deleting until we get back to and can delete this VEB.
14603 static void i40e_switch_branch_release(struct i40e_veb *branch)
14605 struct i40e_pf *pf = branch->pf;
14606 u16 branch_seid = branch->seid;
14607 u16 veb_idx = branch->idx;
14610 /* release any VEBs on this VEB - RECURSION */
14611 for (i = 0; i < I40E_MAX_VEB; i++) {
14614 if (pf->veb[i]->uplink_seid == branch->seid)
14615 i40e_switch_branch_release(pf->veb[i]);
14618 /* Release the VSIs on this VEB, but not the owner VSI.
14620 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14621 * the VEB itself, so don't use (*branch) after this loop.
14623 for (i = 0; i < pf->num_alloc_vsi; i++) {
14626 if (pf->vsi[i]->uplink_seid == branch_seid &&
14627 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14628 i40e_vsi_release(pf->vsi[i]);
14632 /* There's one corner case where the VEB might not have been
14633 * removed, so double check it here and remove it if needed.
14634 * This case happens if the veb was created from the debugfs
14635 * commands and no VSIs were added to it.
14637 if (pf->veb[veb_idx])
14638 i40e_veb_release(pf->veb[veb_idx]);
14642 * i40e_veb_clear - remove veb struct
14643 * @veb: the veb to remove
14645 static void i40e_veb_clear(struct i40e_veb *veb)
14651 struct i40e_pf *pf = veb->pf;
14653 mutex_lock(&pf->switch_mutex);
14654 if (pf->veb[veb->idx] == veb)
14655 pf->veb[veb->idx] = NULL;
14656 mutex_unlock(&pf->switch_mutex);
14663 * i40e_veb_release - Delete a VEB and free its resources
14664 * @veb: the VEB being removed
14666 void i40e_veb_release(struct i40e_veb *veb)
14668 struct i40e_vsi *vsi = NULL;
14669 struct i40e_pf *pf;
14674 /* find the remaining VSI and check for extras */
14675 for (i = 0; i < pf->num_alloc_vsi; i++) {
14676 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14682 dev_info(&pf->pdev->dev,
14683 "can't remove VEB %d with %d VSIs left\n",
14688 /* move the remaining VSI to uplink veb */
14689 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14690 if (veb->uplink_seid) {
14691 vsi->uplink_seid = veb->uplink_seid;
14692 if (veb->uplink_seid == pf->mac_seid)
14693 vsi->veb_idx = I40E_NO_VEB;
14695 vsi->veb_idx = veb->veb_idx;
14698 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14699 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14702 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14703 i40e_veb_clear(veb);
14707 * i40e_add_veb - create the VEB in the switch
14708 * @veb: the VEB to be instantiated
14709 * @vsi: the controlling VSI
14711 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14713 struct i40e_pf *pf = veb->pf;
14714 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14717 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14718 veb->enabled_tc, false,
14719 &veb->seid, enable_stats, NULL);
14721 /* get a VEB from the hardware */
14723 dev_info(&pf->pdev->dev,
14724 "couldn't add VEB, err %s aq_err %s\n",
14725 i40e_stat_str(&pf->hw, ret),
14726 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14730 /* get statistics counter */
14731 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14732 &veb->stats_idx, NULL, NULL, NULL);
14734 dev_info(&pf->pdev->dev,
14735 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14736 i40e_stat_str(&pf->hw, ret),
14737 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14740 ret = i40e_veb_get_bw_info(veb);
14742 dev_info(&pf->pdev->dev,
14743 "couldn't get VEB bw info, err %s aq_err %s\n",
14744 i40e_stat_str(&pf->hw, ret),
14745 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14746 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14750 vsi->uplink_seid = veb->seid;
14751 vsi->veb_idx = veb->idx;
14752 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14758 * i40e_veb_setup - Set up a VEB
14759 * @pf: board private structure
14760 * @flags: VEB setup flags
14761 * @uplink_seid: the switch element to link to
14762 * @vsi_seid: the initial VSI seid
14763 * @enabled_tc: Enabled TC bit-map
14765 * This allocates the sw VEB structure and links it into the switch
14766 * It is possible and legal for this to be a duplicate of an already
14767 * existing VEB. It is also possible for both uplink and vsi seids
14768 * to be zero, in order to create a floating VEB.
14770 * Returns pointer to the successfully allocated VEB sw struct on
14771 * success, otherwise returns NULL on failure.
14773 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14774 u16 uplink_seid, u16 vsi_seid,
14777 struct i40e_veb *veb, *uplink_veb = NULL;
14778 int vsi_idx, veb_idx;
14781 /* if one seid is 0, the other must be 0 to create a floating relay */
14782 if ((uplink_seid == 0 || vsi_seid == 0) &&
14783 (uplink_seid + vsi_seid != 0)) {
14784 dev_info(&pf->pdev->dev,
14785 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14786 uplink_seid, vsi_seid);
14790 /* make sure there is such a vsi and uplink */
14791 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14792 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14794 if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14795 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14800 if (uplink_seid && uplink_seid != pf->mac_seid) {
14801 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14802 if (pf->veb[veb_idx] &&
14803 pf->veb[veb_idx]->seid == uplink_seid) {
14804 uplink_veb = pf->veb[veb_idx];
14809 dev_info(&pf->pdev->dev,
14810 "uplink seid %d not found\n", uplink_seid);
14815 /* get veb sw struct */
14816 veb_idx = i40e_veb_mem_alloc(pf);
14819 veb = pf->veb[veb_idx];
14820 veb->flags = flags;
14821 veb->uplink_seid = uplink_seid;
14822 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14823 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14825 /* create the VEB in the switch */
14826 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14829 if (vsi_idx == pf->lan_vsi)
14830 pf->lan_veb = veb->idx;
14835 i40e_veb_clear(veb);
14841 * i40e_setup_pf_switch_element - set PF vars based on switch type
14842 * @pf: board private structure
14843 * @ele: element we are building info from
14844 * @num_reported: total number of elements
14845 * @printconfig: should we print the contents
14847 * helper function to assist in extracting a few useful SEID values.
14849 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14850 struct i40e_aqc_switch_config_element_resp *ele,
14851 u16 num_reported, bool printconfig)
14853 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14854 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14855 u8 element_type = ele->element_type;
14856 u16 seid = le16_to_cpu(ele->seid);
14859 dev_info(&pf->pdev->dev,
14860 "type=%d seid=%d uplink=%d downlink=%d\n",
14861 element_type, seid, uplink_seid, downlink_seid);
14863 switch (element_type) {
14864 case I40E_SWITCH_ELEMENT_TYPE_MAC:
14865 pf->mac_seid = seid;
14867 case I40E_SWITCH_ELEMENT_TYPE_VEB:
14869 if (uplink_seid != pf->mac_seid)
14871 if (pf->lan_veb >= I40E_MAX_VEB) {
14874 /* find existing or else empty VEB */
14875 for (v = 0; v < I40E_MAX_VEB; v++) {
14876 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14881 if (pf->lan_veb >= I40E_MAX_VEB) {
14882 v = i40e_veb_mem_alloc(pf);
14888 if (pf->lan_veb >= I40E_MAX_VEB)
14891 pf->veb[pf->lan_veb]->seid = seid;
14892 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14893 pf->veb[pf->lan_veb]->pf = pf;
14894 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14896 case I40E_SWITCH_ELEMENT_TYPE_VSI:
14897 if (num_reported != 1)
14899 /* This is immediately after a reset so we can assume this is
14902 pf->mac_seid = uplink_seid;
14903 pf->pf_seid = downlink_seid;
14904 pf->main_vsi_seid = seid;
14906 dev_info(&pf->pdev->dev,
14907 "pf_seid=%d main_vsi_seid=%d\n",
14908 pf->pf_seid, pf->main_vsi_seid);
14910 case I40E_SWITCH_ELEMENT_TYPE_PF:
14911 case I40E_SWITCH_ELEMENT_TYPE_VF:
14912 case I40E_SWITCH_ELEMENT_TYPE_EMP:
14913 case I40E_SWITCH_ELEMENT_TYPE_BMC:
14914 case I40E_SWITCH_ELEMENT_TYPE_PE:
14915 case I40E_SWITCH_ELEMENT_TYPE_PA:
14916 /* ignore these for now */
14919 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14920 element_type, seid);
14926 * i40e_fetch_switch_configuration - Get switch config from firmware
14927 * @pf: board private structure
14928 * @printconfig: should we print the contents
14930 * Get the current switch configuration from the device and
14931 * extract a few useful SEID values.
14933 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14935 struct i40e_aqc_get_switch_config_resp *sw_config;
14941 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14945 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14947 u16 num_reported, num_total;
14949 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14953 dev_info(&pf->pdev->dev,
14954 "get switch config failed err %s aq_err %s\n",
14955 i40e_stat_str(&pf->hw, ret),
14956 i40e_aq_str(&pf->hw,
14957 pf->hw.aq.asq_last_status));
14962 num_reported = le16_to_cpu(sw_config->header.num_reported);
14963 num_total = le16_to_cpu(sw_config->header.num_total);
14966 dev_info(&pf->pdev->dev,
14967 "header: %d reported %d total\n",
14968 num_reported, num_total);
14970 for (i = 0; i < num_reported; i++) {
14971 struct i40e_aqc_switch_config_element_resp *ele =
14972 &sw_config->element[i];
14974 i40e_setup_pf_switch_element(pf, ele, num_reported,
14977 } while (next_seid != 0);
14984 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14985 * @pf: board private structure
14986 * @reinit: if the Main VSI needs to re-initialized.
14987 * @lock_acquired: indicates whether or not the lock has been acquired
14989 * Returns 0 on success, negative value on failure
14991 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14996 /* find out what's out there already */
14997 ret = i40e_fetch_switch_configuration(pf, false);
14999 dev_info(&pf->pdev->dev,
15000 "couldn't fetch switch config, err %s aq_err %s\n",
15001 i40e_stat_str(&pf->hw, ret),
15002 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15005 i40e_pf_reset_stats(pf);
15007 /* set the switch config bit for the whole device to
15008 * support limited promisc or true promisc
15009 * when user requests promisc. The default is limited
15013 if ((pf->hw.pf_id == 0) &&
15014 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15015 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15016 pf->last_sw_conf_flags = flags;
15019 if (pf->hw.pf_id == 0) {
15022 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15023 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15025 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15026 dev_info(&pf->pdev->dev,
15027 "couldn't set switch config bits, err %s aq_err %s\n",
15028 i40e_stat_str(&pf->hw, ret),
15029 i40e_aq_str(&pf->hw,
15030 pf->hw.aq.asq_last_status));
15031 /* not a fatal problem, just keep going */
15033 pf->last_sw_conf_valid_flags = valid_flags;
15036 /* first time setup */
15037 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15038 struct i40e_vsi *vsi = NULL;
15041 /* Set up the PF VSI associated with the PF's main VSI
15042 * that is already in the HW switch
15044 if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15045 uplink_seid = pf->veb[pf->lan_veb]->seid;
15047 uplink_seid = pf->mac_seid;
15048 if (pf->lan_vsi == I40E_NO_VSI)
15049 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15051 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15053 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15054 i40e_cloud_filter_exit(pf);
15055 i40e_fdir_teardown(pf);
15059 /* force a reset of TC and queue layout configurations */
15060 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15062 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15063 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15064 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15066 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15068 i40e_fdir_sb_setup(pf);
15070 /* Setup static PF queue filter control settings */
15071 ret = i40e_setup_pf_filter_control(pf);
15073 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15075 /* Failure here should not stop continuing other steps */
15078 /* enable RSS in the HW, even for only one queue, as the stack can use
15081 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15082 i40e_pf_config_rss(pf);
15084 /* fill in link information and enable LSE reporting */
15085 i40e_link_event(pf);
15087 /* Initialize user-specific link properties */
15088 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15089 I40E_AQ_AN_COMPLETED) ? true : false);
15093 if (!lock_acquired)
15096 /* repopulate tunnel port filters */
15097 udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15099 if (!lock_acquired)
15106 * i40e_determine_queue_usage - Work out queue distribution
15107 * @pf: board private structure
15109 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15114 pf->num_lan_qps = 0;
15116 /* Find the max queues to be put into basic use. We'll always be
15117 * using TC0, whether or not DCB is running, and TC0 will get the
15120 queues_left = pf->hw.func_caps.num_tx_qp;
15122 if ((queues_left == 1) ||
15123 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15124 /* one qp for PF, no queues for anything else */
15126 pf->alloc_rss_size = pf->num_lan_qps = 1;
15128 /* make sure all the fancies are disabled */
15129 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
15130 I40E_FLAG_IWARP_ENABLED |
15131 I40E_FLAG_FD_SB_ENABLED |
15132 I40E_FLAG_FD_ATR_ENABLED |
15133 I40E_FLAG_DCB_CAPABLE |
15134 I40E_FLAG_DCB_ENABLED |
15135 I40E_FLAG_SRIOV_ENABLED |
15136 I40E_FLAG_VMDQ_ENABLED);
15137 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15138 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15139 I40E_FLAG_FD_SB_ENABLED |
15140 I40E_FLAG_FD_ATR_ENABLED |
15141 I40E_FLAG_DCB_CAPABLE))) {
15142 /* one qp for PF */
15143 pf->alloc_rss_size = pf->num_lan_qps = 1;
15144 queues_left -= pf->num_lan_qps;
15146 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
15147 I40E_FLAG_IWARP_ENABLED |
15148 I40E_FLAG_FD_SB_ENABLED |
15149 I40E_FLAG_FD_ATR_ENABLED |
15150 I40E_FLAG_DCB_ENABLED |
15151 I40E_FLAG_VMDQ_ENABLED);
15152 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15154 /* Not enough queues for all TCs */
15155 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15156 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15157 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15158 I40E_FLAG_DCB_ENABLED);
15159 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15162 /* limit lan qps to the smaller of qps, cpus or msix */
15163 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15164 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15165 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15166 pf->num_lan_qps = q_max;
15168 queues_left -= pf->num_lan_qps;
15171 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15172 if (queues_left > 1) {
15173 queues_left -= 1; /* save 1 queue for FD */
15175 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15176 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15177 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15181 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15182 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15183 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15184 (queues_left / pf->num_vf_qps));
15185 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15188 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15189 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15190 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15191 (queues_left / pf->num_vmdq_qps));
15192 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15195 pf->queues_left = queues_left;
15196 dev_dbg(&pf->pdev->dev,
15197 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15198 pf->hw.func_caps.num_tx_qp,
15199 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15200 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15201 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15206 * i40e_setup_pf_filter_control - Setup PF static filter control
15207 * @pf: PF to be setup
15209 * i40e_setup_pf_filter_control sets up a PF's initial filter control
15210 * settings. If PE/FCoE are enabled then it will also set the per PF
15211 * based filter sizes required for them. It also enables Flow director,
15212 * ethertype and macvlan type filter settings for the pf.
15214 * Returns 0 on success, negative on failure
15216 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15218 struct i40e_filter_control_settings *settings = &pf->filter_settings;
15220 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15222 /* Flow Director is enabled */
15223 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15224 settings->enable_fdir = true;
15226 /* Ethtype and MACVLAN filters enabled for PF */
15227 settings->enable_ethtype = true;
15228 settings->enable_macvlan = true;
15230 if (i40e_set_filter_control(&pf->hw, settings))
15236 #define INFO_STRING_LEN 255
15237 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15238 static void i40e_print_features(struct i40e_pf *pf)
15240 struct i40e_hw *hw = &pf->hw;
15244 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15248 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15249 #ifdef CONFIG_PCI_IOV
15250 i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15252 i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15253 pf->hw.func_caps.num_vsis,
15254 pf->vsi[pf->lan_vsi]->num_queue_pairs);
15255 if (pf->flags & I40E_FLAG_RSS_ENABLED)
15256 i += scnprintf(&buf[i], REMAIN(i), " RSS");
15257 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15258 i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15259 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15260 i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15261 i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15263 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15264 i += scnprintf(&buf[i], REMAIN(i), " DCB");
15265 i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15266 i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15267 if (pf->flags & I40E_FLAG_PTP)
15268 i += scnprintf(&buf[i], REMAIN(i), " PTP");
15269 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15270 i += scnprintf(&buf[i], REMAIN(i), " VEB");
15272 i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15274 dev_info(&pf->pdev->dev, "%s\n", buf);
15276 WARN_ON(i > INFO_STRING_LEN);
15280 * i40e_get_platform_mac_addr - get platform-specific MAC address
15281 * @pdev: PCI device information struct
15282 * @pf: board private structure
15284 * Look up the MAC address for the device. First we'll try
15285 * eth_platform_get_mac_address, which will check Open Firmware, or arch
15286 * specific fallback. Otherwise, we'll default to the stored value in
15289 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15291 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15292 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15296 * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15297 * @fec_cfg: FEC option to set in flags
15298 * @flags: ptr to flags in which we set FEC option
15300 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15302 if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15303 *flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15304 if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15305 (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15306 *flags |= I40E_FLAG_RS_FEC;
15307 *flags &= ~I40E_FLAG_BASE_R_FEC;
15309 if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15310 (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15311 *flags |= I40E_FLAG_BASE_R_FEC;
15312 *flags &= ~I40E_FLAG_RS_FEC;
15315 *flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15319 * i40e_check_recovery_mode - check if we are running transition firmware
15320 * @pf: board private structure
15322 * Check registers indicating the firmware runs in recovery mode. Sets the
15323 * appropriate driver state.
15325 * Returns true if the recovery mode was detected, false otherwise
15327 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15329 u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15331 if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15332 dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15333 dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15334 set_bit(__I40E_RECOVERY_MODE, pf->state);
15338 if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15339 dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15345 * i40e_pf_loop_reset - perform reset in a loop.
15346 * @pf: board private structure
15348 * This function is useful when a NIC is about to enter recovery mode.
15349 * When a NIC's internal data structures are corrupted the NIC's
15350 * firmware is going to enter recovery mode.
15351 * Right after a POR it takes about 7 minutes for firmware to enter
15352 * recovery mode. Until that time a NIC is in some kind of intermediate
15353 * state. After that time period the NIC almost surely enters
15354 * recovery mode. The only way for a driver to detect intermediate
15355 * state is to issue a series of pf-resets and check a return value.
15356 * If a PF reset returns success then the firmware could be in recovery
15357 * mode so the caller of this code needs to check for recovery mode
15358 * if this function returns success. There is a little chance that
15359 * firmware will hang in intermediate state forever.
15360 * Since waiting 7 minutes is quite a lot of time this function waits
15361 * 10 seconds and then gives up by returning an error.
15363 * Return 0 on success, negative on failure.
15365 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15367 /* wait max 10 seconds for PF reset to succeed */
15368 const unsigned long time_end = jiffies + 10 * HZ;
15370 struct i40e_hw *hw = &pf->hw;
15373 ret = i40e_pf_reset(hw);
15374 while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15375 usleep_range(10000, 20000);
15376 ret = i40e_pf_reset(hw);
15379 if (ret == I40E_SUCCESS)
15382 dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15388 * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15389 * @pf: board private structure
15391 * Check FW registers to determine if FW issued unexpected EMP Reset.
15392 * Every time when unexpected EMP Reset occurs the FW increments
15393 * a counter of unexpected EMP Resets. When the counter reaches 10
15394 * the FW should enter the Recovery mode
15396 * Returns true if FW issued unexpected EMP Reset
15398 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15400 const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15401 I40E_GL_FWSTS_FWS1B_MASK;
15402 return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15403 (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15407 * i40e_handle_resets - handle EMP resets and PF resets
15408 * @pf: board private structure
15410 * Handle both EMP resets and PF resets and conclude whether there are
15411 * any issues regarding these resets. If there are any issues then
15412 * generate log entry.
15414 * Return 0 if NIC is healthy or negative value when there are issues
15417 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15419 const i40e_status pfr = i40e_pf_loop_reset(pf);
15420 const bool is_empr = i40e_check_fw_empr(pf);
15422 if (is_empr || pfr != I40E_SUCCESS)
15423 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");
15425 return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15429 * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15430 * @pf: board private structure
15431 * @hw: ptr to the hardware info
15433 * This function does a minimal setup of all subsystems needed for running
15436 * Returns 0 on success, negative on failure
15438 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15440 struct i40e_vsi *vsi;
15444 pci_save_state(pf->pdev);
15446 /* set up periodic task facility */
15447 timer_setup(&pf->service_timer, i40e_service_timer, 0);
15448 pf->service_timer_period = HZ;
15450 INIT_WORK(&pf->service_task, i40e_service_task);
15451 clear_bit(__I40E_SERVICE_SCHED, pf->state);
15453 err = i40e_init_interrupt_scheme(pf);
15455 goto err_switch_setup;
15457 /* The number of VSIs reported by the FW is the minimum guaranteed
15458 * to us; HW supports far more and we share the remaining pool with
15459 * the other PFs. We allocate space for more than the guarantee with
15460 * the understanding that we might not get them all later.
15462 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15463 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15465 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15467 /* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15468 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15472 goto err_switch_setup;
15475 /* We allocate one VSI which is needed as absolute minimum
15476 * in order to register the netdev
15478 v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15481 goto err_switch_setup;
15483 pf->lan_vsi = v_idx;
15484 vsi = pf->vsi[v_idx];
15487 goto err_switch_setup;
15489 vsi->alloc_queue_pairs = 1;
15490 err = i40e_config_netdev(vsi);
15492 goto err_switch_setup;
15493 err = register_netdev(vsi->netdev);
15495 goto err_switch_setup;
15496 vsi->netdev_registered = true;
15497 i40e_dbg_pf_init(pf);
15499 err = i40e_setup_misc_vector_for_recovery_mode(pf);
15501 goto err_switch_setup;
15503 /* tell the firmware that we're starting */
15504 i40e_send_version(pf);
15506 /* since everything's happy, start the service_task timer */
15507 mod_timer(&pf->service_timer,
15508 round_jiffies(jiffies + pf->service_timer_period));
15513 i40e_reset_interrupt_capability(pf);
15514 del_timer_sync(&pf->service_timer);
15515 i40e_shutdown_adminq(hw);
15516 iounmap(hw->hw_addr);
15517 pci_disable_pcie_error_reporting(pf->pdev);
15518 pci_release_mem_regions(pf->pdev);
15519 pci_disable_device(pf->pdev);
15526 * i40e_set_subsystem_device_id - set subsystem device id
15527 * @hw: pointer to the hardware info
15529 * Set PCI subsystem device id either from a pci_dev structure or
15530 * a specific FW register.
15532 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15534 struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15536 hw->subsystem_device_id = pdev->subsystem_device ?
15537 pdev->subsystem_device :
15538 (ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15542 * i40e_probe - Device initialization routine
15543 * @pdev: PCI device information struct
15544 * @ent: entry in i40e_pci_tbl
15546 * i40e_probe initializes a PF identified by a pci_dev structure.
15547 * The OS initialization, configuring of the PF private structure,
15548 * and a hardware reset occur.
15550 * Returns 0 on success, negative on failure
15552 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15554 struct i40e_aq_get_phy_abilities_resp abilities;
15555 #ifdef CONFIG_I40E_DCB
15556 enum i40e_get_fw_lldp_status_resp lldp_status;
15557 i40e_status status;
15558 #endif /* CONFIG_I40E_DCB */
15559 struct i40e_pf *pf;
15560 struct i40e_hw *hw;
15561 static u16 pfs_found;
15568 err = pci_enable_device_mem(pdev);
15572 /* set up for high or low dma */
15573 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15575 dev_err(&pdev->dev,
15576 "DMA configuration failed: 0x%x\n", err);
15580 /* set up pci connections */
15581 err = pci_request_mem_regions(pdev, i40e_driver_name);
15583 dev_info(&pdev->dev,
15584 "pci_request_selected_regions failed %d\n", err);
15588 pci_enable_pcie_error_reporting(pdev);
15589 pci_set_master(pdev);
15591 /* Now that we have a PCI connection, we need to do the
15592 * low level device setup. This is primarily setting up
15593 * the Admin Queue structures and then querying for the
15594 * device's current profile information.
15596 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15603 set_bit(__I40E_DOWN, pf->state);
15608 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15609 I40E_MAX_CSR_SPACE);
15610 /* We believe that the highest register to read is
15611 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15612 * is not less than that before mapping to prevent a
15615 if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15616 dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15621 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15622 if (!hw->hw_addr) {
15624 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15625 (unsigned int)pci_resource_start(pdev, 0),
15626 pf->ioremap_len, err);
15629 hw->vendor_id = pdev->vendor;
15630 hw->device_id = pdev->device;
15631 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15632 hw->subsystem_vendor_id = pdev->subsystem_vendor;
15633 i40e_set_subsystem_device_id(hw);
15634 hw->bus.device = PCI_SLOT(pdev->devfn);
15635 hw->bus.func = PCI_FUNC(pdev->devfn);
15636 hw->bus.bus_id = pdev->bus->number;
15637 pf->instance = pfs_found;
15639 /* Select something other than the 802.1ad ethertype for the
15640 * switch to use internally and drop on ingress.
15642 hw->switch_tag = 0xffff;
15643 hw->first_tag = ETH_P_8021AD;
15644 hw->second_tag = ETH_P_8021Q;
15646 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15647 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15648 INIT_LIST_HEAD(&pf->ddp_old_prof);
15650 /* set up the locks for the AQ, do this only once in probe
15651 * and destroy them only once in remove
15653 mutex_init(&hw->aq.asq_mutex);
15654 mutex_init(&hw->aq.arq_mutex);
15656 pf->msg_enable = netif_msg_init(debug,
15661 pf->hw.debug_mask = debug;
15663 /* do a special CORER for clearing PXE mode once at init */
15664 if (hw->revision_id == 0 &&
15665 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15666 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15671 i40e_clear_pxe_mode(hw);
15674 /* Reset here to make sure all is clean and to define PF 'n' */
15677 err = i40e_set_mac_type(hw);
15679 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15684 err = i40e_handle_resets(pf);
15688 i40e_check_recovery_mode(pf);
15690 if (is_kdump_kernel()) {
15691 hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15692 hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15694 hw->aq.num_arq_entries = I40E_AQ_LEN;
15695 hw->aq.num_asq_entries = I40E_AQ_LEN;
15697 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15698 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15699 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15701 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15703 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15705 err = i40e_init_shared_code(hw);
15707 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15712 /* set up a default setting for link flow control */
15713 pf->hw.fc.requested_mode = I40E_FC_NONE;
15715 err = i40e_init_adminq(hw);
15717 if (err == I40E_ERR_FIRMWARE_API_VERSION)
15718 dev_info(&pdev->dev,
15719 "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",
15720 hw->aq.api_maj_ver,
15721 hw->aq.api_min_ver,
15722 I40E_FW_API_VERSION_MAJOR,
15723 I40E_FW_MINOR_VERSION(hw));
15725 dev_info(&pdev->dev,
15726 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15730 i40e_get_oem_version(hw);
15732 /* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15733 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15734 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15735 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15736 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15737 hw->subsystem_vendor_id, hw->subsystem_device_id);
15739 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15740 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15741 dev_dbg(&pdev->dev,
15742 "The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15743 hw->aq.api_maj_ver,
15744 hw->aq.api_min_ver,
15745 I40E_FW_API_VERSION_MAJOR,
15746 I40E_FW_MINOR_VERSION(hw));
15747 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15748 dev_info(&pdev->dev,
15749 "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",
15750 hw->aq.api_maj_ver,
15751 hw->aq.api_min_ver,
15752 I40E_FW_API_VERSION_MAJOR,
15753 I40E_FW_MINOR_VERSION(hw));
15755 i40e_verify_eeprom(pf);
15757 /* Rev 0 hardware was never productized */
15758 if (hw->revision_id < 1)
15759 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");
15761 i40e_clear_pxe_mode(hw);
15763 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15765 goto err_adminq_setup;
15767 err = i40e_sw_init(pf);
15769 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15773 if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15774 return i40e_init_recovery_mode(pf, hw);
15776 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15777 hw->func_caps.num_rx_qp, 0, 0);
15779 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15780 goto err_init_lan_hmc;
15783 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15785 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15787 goto err_configure_lan_hmc;
15790 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
15791 * Ignore error return codes because if it was already disabled via
15792 * hardware settings this will fail
15794 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15795 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15796 i40e_aq_stop_lldp(hw, true, false, NULL);
15799 /* allow a platform config to override the HW addr */
15800 i40e_get_platform_mac_addr(pdev, pf);
15802 if (!is_valid_ether_addr(hw->mac.addr)) {
15803 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15807 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15808 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15809 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15810 if (is_valid_ether_addr(hw->mac.port_addr))
15811 pf->hw_features |= I40E_HW_PORT_ID_VALID;
15813 i40e_ptp_alloc_pins(pf);
15814 pci_set_drvdata(pdev, pf);
15815 pci_save_state(pdev);
15817 #ifdef CONFIG_I40E_DCB
15818 status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15820 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15821 (pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15822 (pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15823 dev_info(&pdev->dev,
15824 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15825 "FW LLDP is disabled\n" :
15826 "FW LLDP is enabled\n");
15828 /* Enable FW to write default DCB config on link-up */
15829 i40e_aq_set_dcb_parameters(hw, true, NULL);
15831 err = i40e_init_pf_dcb(pf);
15833 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15834 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15835 /* Continue without DCB enabled */
15837 #endif /* CONFIG_I40E_DCB */
15839 /* set up periodic task facility */
15840 timer_setup(&pf->service_timer, i40e_service_timer, 0);
15841 pf->service_timer_period = HZ;
15843 INIT_WORK(&pf->service_task, i40e_service_task);
15844 clear_bit(__I40E_SERVICE_SCHED, pf->state);
15846 /* NVM bit on means WoL disabled for the port */
15847 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15848 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15849 pf->wol_en = false;
15852 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15854 /* set up the main switch operations */
15855 i40e_determine_queue_usage(pf);
15856 err = i40e_init_interrupt_scheme(pf);
15858 goto err_switch_setup;
15860 /* Reduce Tx and Rx pairs for kdump
15861 * When MSI-X is enabled, it's not allowed to use more TC queue
15862 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15863 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15865 if (is_kdump_kernel())
15866 pf->num_lan_msix = 1;
15868 pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15869 pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15870 pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15871 pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15872 pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15873 pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15874 UDP_TUNNEL_TYPE_GENEVE;
15876 /* The number of VSIs reported by the FW is the minimum guaranteed
15877 * to us; HW supports far more and we share the remaining pool with
15878 * the other PFs. We allocate space for more than the guarantee with
15879 * the understanding that we might not get them all later.
15881 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15882 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15884 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15885 if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15886 dev_warn(&pf->pdev->dev,
15887 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15888 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15889 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15892 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15893 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15897 goto err_switch_setup;
15900 #ifdef CONFIG_PCI_IOV
15901 /* prep for VF support */
15902 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15903 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15904 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15905 if (pci_num_vf(pdev))
15906 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15909 err = i40e_setup_pf_switch(pf, false, false);
15911 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15914 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15916 /* if FDIR VSI was set up, start it now */
15917 for (i = 0; i < pf->num_alloc_vsi; i++) {
15918 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15919 i40e_vsi_open(pf->vsi[i]);
15924 /* The driver only wants link up/down and module qualification
15925 * reports from firmware. Note the negative logic.
15927 err = i40e_aq_set_phy_int_mask(&pf->hw,
15928 ~(I40E_AQ_EVENT_LINK_UPDOWN |
15929 I40E_AQ_EVENT_MEDIA_NA |
15930 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15932 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15933 i40e_stat_str(&pf->hw, err),
15934 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15936 /* Reconfigure hardware for allowing smaller MSS in the case
15937 * of TSO, so that we avoid the MDD being fired and causing
15938 * a reset in the case of small MSS+TSO.
15940 val = rd32(hw, I40E_REG_MSS);
15941 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15942 val &= ~I40E_REG_MSS_MIN_MASK;
15943 val |= I40E_64BYTE_MSS;
15944 wr32(hw, I40E_REG_MSS, val);
15947 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15949 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15951 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15952 i40e_stat_str(&pf->hw, err),
15953 i40e_aq_str(&pf->hw,
15954 pf->hw.aq.asq_last_status));
15956 /* The main driver is (mostly) up and happy. We need to set this state
15957 * before setting up the misc vector or we get a race and the vector
15958 * ends up disabled forever.
15960 clear_bit(__I40E_DOWN, pf->state);
15962 /* In case of MSIX we are going to setup the misc vector right here
15963 * to handle admin queue events etc. In case of legacy and MSI
15964 * the misc functionality and queue processing is combined in
15965 * the same vector and that gets setup at open.
15967 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15968 err = i40e_setup_misc_vector(pf);
15970 dev_info(&pdev->dev,
15971 "setup of misc vector failed: %d\n", err);
15972 i40e_cloud_filter_exit(pf);
15973 i40e_fdir_teardown(pf);
15978 #ifdef CONFIG_PCI_IOV
15979 /* prep for VF support */
15980 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15981 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15982 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15983 /* disable link interrupts for VFs */
15984 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15985 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15986 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15989 if (pci_num_vf(pdev)) {
15990 dev_info(&pdev->dev,
15991 "Active VFs found, allocating resources.\n");
15992 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15994 dev_info(&pdev->dev,
15995 "Error %d allocating resources for existing VFs\n",
15999 #endif /* CONFIG_PCI_IOV */
16001 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16002 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16003 pf->num_iwarp_msix,
16004 I40E_IWARP_IRQ_PILE_ID);
16005 if (pf->iwarp_base_vector < 0) {
16006 dev_info(&pdev->dev,
16007 "failed to get tracking for %d vectors for IWARP err=%d\n",
16008 pf->num_iwarp_msix, pf->iwarp_base_vector);
16009 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16013 i40e_dbg_pf_init(pf);
16015 /* tell the firmware that we're starting */
16016 i40e_send_version(pf);
16018 /* since everything's happy, start the service_task timer */
16019 mod_timer(&pf->service_timer,
16020 round_jiffies(jiffies + pf->service_timer_period));
16022 /* add this PF to client device list and launch a client service task */
16023 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16024 err = i40e_lan_add_device(pf);
16026 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16030 #define PCI_SPEED_SIZE 8
16031 #define PCI_WIDTH_SIZE 8
16032 /* Devices on the IOSF bus do not have this information
16033 * and will report PCI Gen 1 x 1 by default so don't bother
16036 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16037 char speed[PCI_SPEED_SIZE] = "Unknown";
16038 char width[PCI_WIDTH_SIZE] = "Unknown";
16040 /* Get the negotiated link width and speed from PCI config
16043 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16046 i40e_set_pci_config_data(hw, link_status);
16048 switch (hw->bus.speed) {
16049 case i40e_bus_speed_8000:
16050 strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
16051 case i40e_bus_speed_5000:
16052 strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
16053 case i40e_bus_speed_2500:
16054 strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
16058 switch (hw->bus.width) {
16059 case i40e_bus_width_pcie_x8:
16060 strlcpy(width, "8", PCI_WIDTH_SIZE); break;
16061 case i40e_bus_width_pcie_x4:
16062 strlcpy(width, "4", PCI_WIDTH_SIZE); break;
16063 case i40e_bus_width_pcie_x2:
16064 strlcpy(width, "2", PCI_WIDTH_SIZE); break;
16065 case i40e_bus_width_pcie_x1:
16066 strlcpy(width, "1", PCI_WIDTH_SIZE); break;
16071 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16074 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16075 hw->bus.speed < i40e_bus_speed_8000) {
16076 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16077 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16081 /* get the requested speeds from the fw */
16082 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16084 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
16085 i40e_stat_str(&pf->hw, err),
16086 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16087 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16089 /* set the FEC config due to the board capabilities */
16090 i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16092 /* get the supported phy types from the fw */
16093 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16095 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
16096 i40e_stat_str(&pf->hw, err),
16097 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16099 /* make sure the MFS hasn't been set lower than the default */
16100 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16101 val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16102 I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16103 if (val < MAX_FRAME_SIZE_DEFAULT)
16104 dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16107 /* Add a filter to drop all Flow control frames from any VSI from being
16108 * transmitted. By doing so we stop a malicious VF from sending out
16109 * PAUSE or PFC frames and potentially controlling traffic for other
16111 * The FW can still send Flow control frames if enabled.
16113 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16114 pf->main_vsi_seid);
16116 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16117 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16118 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16119 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16120 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16121 /* print a string summarizing features */
16122 i40e_print_features(pf);
16126 /* Unwind what we've done if something failed in the setup */
16128 set_bit(__I40E_DOWN, pf->state);
16129 i40e_clear_interrupt_scheme(pf);
16132 i40e_reset_interrupt_capability(pf);
16133 del_timer_sync(&pf->service_timer);
16135 err_configure_lan_hmc:
16136 (void)i40e_shutdown_lan_hmc(hw);
16138 kfree(pf->qp_pile);
16142 iounmap(hw->hw_addr);
16146 pci_disable_pcie_error_reporting(pdev);
16147 pci_release_mem_regions(pdev);
16150 pci_disable_device(pdev);
16155 * i40e_remove - Device removal routine
16156 * @pdev: PCI device information struct
16158 * i40e_remove is called by the PCI subsystem to alert the driver
16159 * that is should release a PCI device. This could be caused by a
16160 * Hot-Plug event, or because the driver is going to be removed from
16163 static void i40e_remove(struct pci_dev *pdev)
16165 struct i40e_pf *pf = pci_get_drvdata(pdev);
16166 struct i40e_hw *hw = &pf->hw;
16167 i40e_status ret_code;
16170 i40e_dbg_pf_exit(pf);
16174 /* Disable RSS in hw */
16175 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16176 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16178 /* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16179 * flags, once they are set, i40e_rebuild should not be called as
16180 * i40e_prep_for_reset always returns early.
16182 while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16183 usleep_range(1000, 2000);
16184 set_bit(__I40E_IN_REMOVE, pf->state);
16186 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16187 set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16189 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16191 /* no more scheduling of any task */
16192 set_bit(__I40E_SUSPENDED, pf->state);
16193 set_bit(__I40E_DOWN, pf->state);
16194 if (pf->service_timer.function)
16195 del_timer_sync(&pf->service_timer);
16196 if (pf->service_task.func)
16197 cancel_work_sync(&pf->service_task);
16199 if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16200 struct i40e_vsi *vsi = pf->vsi[0];
16202 /* We know that we have allocated only one vsi for this PF,
16203 * it was just for registering netdevice, so the interface
16204 * could be visible in the 'ifconfig' output
16206 unregister_netdev(vsi->netdev);
16207 free_netdev(vsi->netdev);
16212 /* Client close must be called explicitly here because the timer
16213 * has been stopped.
16215 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16217 i40e_fdir_teardown(pf);
16219 /* If there is a switch structure or any orphans, remove them.
16220 * This will leave only the PF's VSI remaining.
16222 for (i = 0; i < I40E_MAX_VEB; i++) {
16226 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16227 pf->veb[i]->uplink_seid == 0)
16228 i40e_switch_branch_release(pf->veb[i]);
16231 /* Now we can shutdown the PF's VSI, just before we kill
16234 if (pf->vsi[pf->lan_vsi])
16235 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16237 i40e_cloud_filter_exit(pf);
16239 /* remove attached clients */
16240 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16241 ret_code = i40e_lan_del_device(pf);
16243 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16247 /* shutdown and destroy the HMC */
16248 if (hw->hmc.hmc_obj) {
16249 ret_code = i40e_shutdown_lan_hmc(hw);
16251 dev_warn(&pdev->dev,
16252 "Failed to destroy the HMC resources: %d\n",
16257 /* Free MSI/legacy interrupt 0 when in recovery mode. */
16258 if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16259 !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16260 free_irq(pf->pdev->irq, pf);
16262 /* shutdown the adminq */
16263 i40e_shutdown_adminq(hw);
16265 /* destroy the locks only once, here */
16266 mutex_destroy(&hw->aq.arq_mutex);
16267 mutex_destroy(&hw->aq.asq_mutex);
16269 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16271 i40e_clear_interrupt_scheme(pf);
16272 for (i = 0; i < pf->num_alloc_vsi; i++) {
16274 if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16275 i40e_vsi_clear_rings(pf->vsi[i]);
16276 i40e_vsi_clear(pf->vsi[i]);
16282 for (i = 0; i < I40E_MAX_VEB; i++) {
16287 kfree(pf->qp_pile);
16290 iounmap(hw->hw_addr);
16292 pci_release_mem_regions(pdev);
16294 pci_disable_pcie_error_reporting(pdev);
16295 pci_disable_device(pdev);
16299 * i40e_pci_error_detected - warning that something funky happened in PCI land
16300 * @pdev: PCI device information struct
16301 * @error: the type of PCI error
16303 * Called to warn that something happened and the error handling steps
16304 * are in progress. Allows the driver to quiesce things, be ready for
16307 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16308 pci_channel_state_t error)
16310 struct i40e_pf *pf = pci_get_drvdata(pdev);
16312 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16315 dev_info(&pdev->dev,
16316 "Cannot recover - error happened during device probe\n");
16317 return PCI_ERS_RESULT_DISCONNECT;
16320 /* shutdown all operations */
16321 if (!test_bit(__I40E_SUSPENDED, pf->state))
16322 i40e_prep_for_reset(pf);
16324 /* Request a slot reset */
16325 return PCI_ERS_RESULT_NEED_RESET;
16329 * i40e_pci_error_slot_reset - a PCI slot reset just happened
16330 * @pdev: PCI device information struct
16332 * Called to find if the driver can work with the device now that
16333 * the pci slot has been reset. If a basic connection seems good
16334 * (registers are readable and have sane content) then return a
16335 * happy little PCI_ERS_RESULT_xxx.
16337 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16339 struct i40e_pf *pf = pci_get_drvdata(pdev);
16340 pci_ers_result_t result;
16343 dev_dbg(&pdev->dev, "%s\n", __func__);
16344 if (pci_enable_device_mem(pdev)) {
16345 dev_info(&pdev->dev,
16346 "Cannot re-enable PCI device after reset.\n");
16347 result = PCI_ERS_RESULT_DISCONNECT;
16349 pci_set_master(pdev);
16350 pci_restore_state(pdev);
16351 pci_save_state(pdev);
16352 pci_wake_from_d3(pdev, false);
16354 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16356 result = PCI_ERS_RESULT_RECOVERED;
16358 result = PCI_ERS_RESULT_DISCONNECT;
16365 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16366 * @pdev: PCI device information struct
16368 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16370 struct i40e_pf *pf = pci_get_drvdata(pdev);
16372 i40e_prep_for_reset(pf);
16376 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16377 * @pdev: PCI device information struct
16379 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16381 struct i40e_pf *pf = pci_get_drvdata(pdev);
16383 if (test_bit(__I40E_IN_REMOVE, pf->state))
16386 i40e_reset_and_rebuild(pf, false, false);
16390 * i40e_pci_error_resume - restart operations after PCI error recovery
16391 * @pdev: PCI device information struct
16393 * Called to allow the driver to bring things back up after PCI error
16394 * and/or reset recovery has finished.
16396 static void i40e_pci_error_resume(struct pci_dev *pdev)
16398 struct i40e_pf *pf = pci_get_drvdata(pdev);
16400 dev_dbg(&pdev->dev, "%s\n", __func__);
16401 if (test_bit(__I40E_SUSPENDED, pf->state))
16404 i40e_handle_reset_warning(pf, false);
16408 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16409 * using the mac_address_write admin q function
16410 * @pf: pointer to i40e_pf struct
16412 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16414 struct i40e_hw *hw = &pf->hw;
16419 /* Get current MAC address in case it's an LAA */
16420 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16421 ether_addr_copy(mac_addr,
16422 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16424 dev_err(&pf->pdev->dev,
16425 "Failed to retrieve MAC address; using default\n");
16426 ether_addr_copy(mac_addr, hw->mac.addr);
16429 /* The FW expects the mac address write cmd to first be called with
16430 * one of these flags before calling it again with the multicast
16433 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16435 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16436 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16438 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16440 dev_err(&pf->pdev->dev,
16441 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16445 flags = I40E_AQC_MC_MAG_EN
16446 | I40E_AQC_WOL_PRESERVE_ON_PFR
16447 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16448 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16450 dev_err(&pf->pdev->dev,
16451 "Failed to enable Multicast Magic Packet wake up\n");
16455 * i40e_shutdown - PCI callback for shutting down
16456 * @pdev: PCI device information struct
16458 static void i40e_shutdown(struct pci_dev *pdev)
16460 struct i40e_pf *pf = pci_get_drvdata(pdev);
16461 struct i40e_hw *hw = &pf->hw;
16463 set_bit(__I40E_SUSPENDED, pf->state);
16464 set_bit(__I40E_DOWN, pf->state);
16466 del_timer_sync(&pf->service_timer);
16467 cancel_work_sync(&pf->service_task);
16468 i40e_cloud_filter_exit(pf);
16469 i40e_fdir_teardown(pf);
16471 /* Client close must be called explicitly here because the timer
16472 * has been stopped.
16474 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16476 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16477 i40e_enable_mc_magic_wake(pf);
16479 i40e_prep_for_reset(pf);
16481 wr32(hw, I40E_PFPM_APM,
16482 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16483 wr32(hw, I40E_PFPM_WUFC,
16484 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16486 /* Free MSI/legacy interrupt 0 when in recovery mode. */
16487 if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16488 !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16489 free_irq(pf->pdev->irq, pf);
16491 /* Since we're going to destroy queues during the
16492 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16496 i40e_clear_interrupt_scheme(pf);
16499 if (system_state == SYSTEM_POWER_OFF) {
16500 pci_wake_from_d3(pdev, pf->wol_en);
16501 pci_set_power_state(pdev, PCI_D3hot);
16506 * i40e_suspend - PM callback for moving to D3
16507 * @dev: generic device information structure
16509 static int __maybe_unused i40e_suspend(struct device *dev)
16511 struct i40e_pf *pf = dev_get_drvdata(dev);
16512 struct i40e_hw *hw = &pf->hw;
16514 /* If we're already suspended, then there is nothing to do */
16515 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16518 set_bit(__I40E_DOWN, pf->state);
16520 /* Ensure service task will not be running */
16521 del_timer_sync(&pf->service_timer);
16522 cancel_work_sync(&pf->service_task);
16524 /* Client close must be called explicitly here because the timer
16525 * has been stopped.
16527 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16529 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16530 i40e_enable_mc_magic_wake(pf);
16532 /* Since we're going to destroy queues during the
16533 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16538 i40e_prep_for_reset(pf);
16540 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16541 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16543 /* Clear the interrupt scheme and release our IRQs so that the system
16544 * can safely hibernate even when there are a large number of CPUs.
16545 * Otherwise hibernation might fail when mapping all the vectors back
16548 i40e_clear_interrupt_scheme(pf);
16556 * i40e_resume - PM callback for waking up from D3
16557 * @dev: generic device information structure
16559 static int __maybe_unused i40e_resume(struct device *dev)
16561 struct i40e_pf *pf = dev_get_drvdata(dev);
16564 /* If we're not suspended, then there is nothing to do */
16565 if (!test_bit(__I40E_SUSPENDED, pf->state))
16568 /* We need to hold the RTNL lock prior to restoring interrupt schemes,
16569 * since we're going to be restoring queues
16573 /* We cleared the interrupt scheme when we suspended, so we need to
16574 * restore it now to resume device functionality.
16576 err = i40e_restore_interrupt_scheme(pf);
16578 dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16582 clear_bit(__I40E_DOWN, pf->state);
16583 i40e_reset_and_rebuild(pf, false, true);
16587 /* Clear suspended state last after everything is recovered */
16588 clear_bit(__I40E_SUSPENDED, pf->state);
16590 /* Restart the service task */
16591 mod_timer(&pf->service_timer,
16592 round_jiffies(jiffies + pf->service_timer_period));
16597 static const struct pci_error_handlers i40e_err_handler = {
16598 .error_detected = i40e_pci_error_detected,
16599 .slot_reset = i40e_pci_error_slot_reset,
16600 .reset_prepare = i40e_pci_error_reset_prepare,
16601 .reset_done = i40e_pci_error_reset_done,
16602 .resume = i40e_pci_error_resume,
16605 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16607 static struct pci_driver i40e_driver = {
16608 .name = i40e_driver_name,
16609 .id_table = i40e_pci_tbl,
16610 .probe = i40e_probe,
16611 .remove = i40e_remove,
16613 .pm = &i40e_pm_ops,
16615 .shutdown = i40e_shutdown,
16616 .err_handler = &i40e_err_handler,
16617 .sriov_configure = i40e_pci_sriov_configure,
16621 * i40e_init_module - Driver registration routine
16623 * i40e_init_module is the first routine called when the driver is
16624 * loaded. All it does is register with the PCI subsystem.
16626 static int __init i40e_init_module(void)
16628 pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16629 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16631 /* There is no need to throttle the number of active tasks because
16632 * each device limits its own task using a state bit for scheduling
16633 * the service task, and the device tasks do not interfere with each
16634 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16635 * since we need to be able to guarantee forward progress even under
16638 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16640 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16645 return pci_register_driver(&i40e_driver);
16647 module_init(i40e_init_module);
16650 * i40e_exit_module - Driver exit cleanup routine
16652 * i40e_exit_module is called just before the driver is removed
16655 static void __exit i40e_exit_module(void)
16657 pci_unregister_driver(&i40e_driver);
16658 destroy_workqueue(i40e_wq);
16659 ida_destroy(&i40e_client_ida);
16662 module_exit(i40e_exit_module);